summaryrefslogtreecommitdiff
path: root/src/state/initial_state.hpp
blob: 24a95d8437fb012881ff3bb3e71ab434efd067a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "active_state.hpp"
#include "basic_state.hpp"
#include "ready_state.hpp"

struct InitialState : public BasicStateT<InitialState>
{
    static std::string_view stateName()
    {
        return "InitialState";
    }

    InitialState(interfaces::MountPointStateMachine& machine) :
        BasicStateT(machine){};

    std::unique_ptr<BasicState> handleEvent(RegisterDbusEvent event)
    {
        const bool isLegacy =
            (machine.getConfig().mode == Configuration::Mode::legacy);

#if !LEGACY_MODE_ENABLED
        if (isLegacy)
        {
            return std::make_unique<ReadyState>(machine,
                                                std::errc::invalid_argument,
                                                "Legacy mode is not supported");
        }
#endif
        addMountPointInterface(event);
        addProcessInterface(event);
        addServiceInterface(event, isLegacy);

        return std::make_unique<ReadyState>(machine);
    }

    template <class AnyEvent>
    std::unique_ptr<BasicState> handleEvent(AnyEvent event)
    {
        LogMsg(Logger::Error, "Invalid event: ", event.eventName);
        return nullptr;
    }

  private:
    static std::string
        getObjectPath(interfaces::MountPointStateMachine& machine)
    {
        LogMsg(Logger::Debug, "getObjectPath entry()");
        std::string objPath;
        if (machine.getConfig().mode == Configuration::Mode::proxy)
        {
            objPath = "/xyz/openbmc_project/VirtualMedia/Proxy/";
        }
        else
        {
            objPath = "/xyz/openbmc_project/VirtualMedia/Legacy/";
        }
        return objPath;
    }

    void addProcessInterface(const RegisterDbusEvent& event)
    {
        std::string objPath = getObjectPath(machine);

        auto processIface = event.objServer->add_interface(
            objPath + std::string(machine.getName()),
            "xyz.openbmc_project.VirtualMedia.Process");

        processIface->register_property(
            "Active", bool(false),
            [](const bool& req, bool& property) { return 0; },
            [&machine = machine](const bool& property) -> bool {
                return machine.getState().get_if<ActiveState>();
            });
        processIface->register_property(
            "ExitCode", int32_t(0),
            [](const int32_t& req, int32_t& property) { return 0; },
            [&machine = machine](const int32_t& property) {
                return machine.getExitCode();
            });
        processIface->initialize();
    }

    void addMountPointInterface(const RegisterDbusEvent& event)
    {
        std::string objPath = getObjectPath(machine);

        auto iface = event.objServer->add_interface(
            objPath + std::string(machine.getName()),
            "xyz.openbmc_project.VirtualMedia.MountPoint");
        iface->register_property("Device",
                                 machine.getConfig().nbdDevice.to_string());
        iface->register_property("EndpointId", machine.getConfig().endPointId);
        iface->register_property("Socket", machine.getConfig().unixSocket);
        iface->register_property(
            "ImageURL", std::string(),
            [](const std::string& req, std::string& property) {
                throw sdbusplus::exception::SdBusError(
                    EPERM, "Setting ImageURL property is not allowed");
                return -1;
            },
            [&target = machine.getTarget()](const std::string& property) {
                if (target)
                {
                    return target->imgUrl;
                }
                return std::string();
            });
        iface->register_property(
            "WriteProtected", bool(true),
            [](const bool& req, bool& property) { return 0; },
            [&target = machine.getTarget()](const bool& property) {
                if (target)
                {
                    return !target->rw;
                }
                return bool(true);
            });
        iface->register_property(
            "Timeout", machine.getConfig().timeout.value_or(
                           Configuration::MountPoint::defaultTimeout));
        iface->register_property(
            "RemainingInactivityTimeout", 0,
            [](const int& req, int& property) {
                throw sdbusplus::exception::SdBusError(
                    EPERM, "Setting RemainingInactivityTimeout property is "
                           "not allowed");
                return -1;
            },
            [&config = machine.getConfig()](const int& property) -> int {
                return config.remainingInactivityTimeout.count();
            });
        iface->initialize();
    }

    void addServiceInterface(const RegisterDbusEvent& event,
                             const bool isLegacy)
    {
        const std::string name = "xyz.openbmc_project.VirtualMedia." +
                                 std::string(isLegacy ? "Legacy" : "Proxy");

        const std::string path =
            getObjectPath(machine) + std::string(machine.getName());

        auto iface = event.objServer->add_interface(path, name);

        const auto timerPeriod = std::chrono::milliseconds(100);
        const auto duration = std::chrono::seconds(
            machine.getConfig().timeout.value_or(
                Configuration::MountPoint::defaultTimeout) +
            5);
        const auto waitCnt =
            std::chrono::duration_cast<std::chrono::milliseconds>(duration) /
            timerPeriod;
        LogMsg(Logger::Debug, "[App] waitCnt == ", waitCnt);

        // Common unmount
        iface->register_method(
            "Unmount", [&machine = machine, waitCnt,
                        timerPeriod](boost::asio::yield_context yield) {
                LogMsg(Logger::Info, "[App]: Unmount called on ",
                       machine.getName());
                machine.emitUnmountEvent();

                auto repeats = waitCnt;
                boost::asio::steady_timer timer(machine.getIoc());
                while (repeats > 0)
                {
                    if (machine.getState().get_if<ReadyState>())
                    {
                        LogMsg(Logger::Debug, "[App] Unmount ok");
                        return true;
                    }
                    boost::system::error_code ignored_ec;
                    timer.expires_from_now(timerPeriod);
                    timer.async_wait(yield[ignored_ec]);
                    repeats--;
                }
                LogMsg(Logger::Error,
                       "[App] timedout when waiting for ReadyState");
                throw sdbusplus::exception::SdBusError(EBUSY,
                                                       "Resource is busy");
                return false;
            });

        // Common mount
        const auto handleMount =
            [waitCnt, timerPeriod](
                boost::asio::yield_context yield,
                interfaces::MountPointStateMachine& machine,
                std::optional<interfaces::MountPointStateMachine::Target>
                    target) {
                machine.emitMountEvent(std::move(target));

                auto repeats = waitCnt;
                boost::asio::steady_timer timer(machine.getIoc());
                while (repeats > 0)
                {
                    if (auto s = machine.getState().get_if<ReadyState>())
                    {
                        if (s->error)
                        {
                            LogMsg(Logger::Error, s->error->message.c_str());
                            throw sdbusplus::exception::SdBusError(
                                static_cast<int>(s->error->code),
                                s->error->message.c_str());
                        }
                        LogMsg(Logger::Error, "[App] Mount failed");
                        return false;
                    }
                    if (machine.getState().get_if<ActiveState>())
                    {
                        LogMsg(Logger::Debug, "[App] Mount ok");
                        return true;
                    }
                    boost::system::error_code ignored_ec;
                    timer.expires_from_now(timerPeriod);
                    timer.async_wait(yield[ignored_ec]);
                    repeats--;
                }
                LogMsg(Logger::Error,
                       "[App] timedout when waiting for ActiveState");
                throw sdbusplus::exception::SdBusError(EBUSY,
                                                       "Resource is busy");
                return false;
            };

        // Mount specialization
        if (isLegacy)
        {
            using sdbusplus::message::unix_fd;
            using optional_fd = std::variant<int, unix_fd>;

            iface->register_method(
                "Mount", [&machine = machine, handleMount](
                             boost::asio::yield_context yield,
                             std::string imgUrl, bool rw, optional_fd fd) {
                    LogMsg(Logger::Info, "[App]: Mount called on ",
                           getObjectPath(machine), machine.getName());

                    interfaces::MountPointStateMachine::Target target = {imgUrl,
                                                                         rw};

                    if (std::holds_alternative<unix_fd>(fd))
                    {
                        LogMsg(Logger::Debug, "[App] Extra data available");

                        // Open pipe and prepare output buffer
                        boost::asio::posix::stream_descriptor secretPipe(
                            machine.getIoc(), dup(std::get<unix_fd>(fd).fd));
                        std::array<char, utils::secretLimit> buf;

                        // Read data
                        auto size = secretPipe.async_read_some(
                            boost::asio::buffer(buf), yield);

                        // Validate number of NULL delimiters, ensures
                        // further operations are safe
                        auto nullCount =
                            std::count(buf.begin(), buf.begin() + size, '\0');
                        if (nullCount != 2)
                        {
                            throw sdbusplus::exception::SdBusError(
                                EINVAL, "Malformed extra data");
                        }

                        // First 'part' of payload
                        std::string user(buf.begin());
                        // Second 'part', after NULL delimiter
                        std::string pass(buf.begin() + user.length() + 1);

                        // Encapsulate credentials into safe buffer
                        target.credentials =
                            std::make_unique<utils::CredentialsProvider>(
                                std::move(user), std::move(pass));

                        // Cover the tracks
                        utils::secureCleanup(buf);
                    }

                    try
                    {
                        auto ret =
                            handleMount(yield, machine, std::move(target));
                        if (machine.getTarget())
                        {
                            machine.getTarget()->credentials.reset();
                        }
                        LogMsg(Logger::Debug, "[App]: mount completed ", ret);
                        return ret;
                    }
                    catch (const std::exception& e)
                    {
                        LogMsg(Logger::Error, e.what());
                        if (machine.getTarget())
                        {
                            machine.getTarget()->credentials.reset();
                        }
                        throw;
                        return false;
                    }
                    catch (...)
                    {
                        if (machine.getTarget())
                        {
                            machine.getTarget()->credentials.reset();
                        }
                        throw;
                        return false;
                    }
                });
        }
        else
        {
            iface->register_method(
                "Mount", [&machine = machine,
                          handleMount](boost::asio::yield_context yield) {
                    LogMsg(Logger::Info, "[App]: Mount called on ",
                           getObjectPath(machine), machine.getName());

                    return handleMount(yield, machine, std::nullopt);
                });
        }

        iface->initialize();
    }
};