summaryrefslogtreecommitdiff
path: root/src/utils.hpp
blob: 1c03aa019e60553318789780a8d91c7b77239069 (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#pragma once

#include "logger.hpp"

#include <unistd.h>

#include <algorithm>
#include <boost/asio/steady_timer.hpp>
#include <boost/process/async_pipe.hpp>
#include <boost/type_traits/has_dereference.hpp>
#include <filesystem>
#include <fstream>
#include <memory>
#include <optional>
#include <sdbusplus/asio/object_server.hpp>
#include <string>
#include <system_error>
#include <vector>

namespace fs = std::filesystem;

namespace utils
{
constexpr const size_t secretLimit = 1024;

template <typename T>
static void secureCleanup(T& value)
{
    auto raw = const_cast<typename T::value_type*>(&value[0]);
    explicit_bzero(raw, value.size() * sizeof(*raw));
}

class Credentials
{
  public:
    Credentials(std::string&& user, std::string&& password) :
        userBuf(std::move(user)), passBuf(std::move(password))
    {
    }

    ~Credentials()
    {
        secureCleanup(userBuf);
        secureCleanup(passBuf);
    }

    const std::string& user()
    {
        return userBuf;
    }

    const std::string& password()
    {
        return passBuf;
    }

    void escapeCommas()
    {
        if (!commasEscaped)
        {
            escapeComma(passBuf);
            commasEscaped = true;
        }
    }

  private:
    Credentials() = delete;
    Credentials(const Credentials&) = delete;
    Credentials& operator=(const Credentials&) = delete;

    /* escape ',' (coma) by ',,' */
    void escapeComma(std::string& s)
    {
        std::string temp;
        std::for_each(s.begin(), s.end(), [&temp](const auto& c) {
            *std::back_inserter(temp) = c;
            if (c == ',')
            {
                *std::back_inserter(temp) = c;
            }
        });
        std::swap(s, temp);
        secureCleanup(temp);
    }

    std::string userBuf;
    std::string passBuf;
    bool commasEscaped{false};
};

class CredentialsProvider
{
  public:
    template <typename T>
    struct Deleter
    {
        void operator()(T* buff) const
        {
            if (buff)
            {
                secureCleanup(*buff);
                delete buff;
            }
        }
    };

    using Buffer = std::vector<char>;
    using SecureBuffer = std::unique_ptr<Buffer, Deleter<Buffer>>;
    // Using explicit definition instead of std::function to avoid implicit
    // conversions eg. stack copy instead of reference for parameters
    using FormatterFunc = void(const std::string& username,
                               const std::string& password, Buffer& dest);

    CredentialsProvider(std::string&& user, std::string&& password) :
        credentials(std::move(user), std::move(password))
    {
    }

    void escapeCommas()
    {
        credentials.escapeCommas();
    }

    const std::string& user()
    {
        return credentials.user();
    }

    const std::string& password()
    {
        return credentials.password();
    }

    SecureBuffer pack(const FormatterFunc formatter)
    {
        SecureBuffer packed{new Buffer{}};
        if (formatter)
        {
            formatter(credentials.user(), credentials.password(), *packed);
        }
        return packed;
    }

  private:
    Credentials credentials;
};

// Wrapper for boost::async_pipe ensuring proper pipe cleanup
template <typename Buffer>
class NamedPipe
{
  public:
    using unix_fd = sdbusplus::message::unix_fd;

    NamedPipe(boost::asio::io_context& io, const std::string name,
              Buffer&& buffer) :
        name(name),
        impl(io, name), buffer{std::move(buffer)}
    {
    }

    ~NamedPipe()
    {
        // Named pipe needs to be explicitly removed
        impl.close();
        ::unlink(name.c_str());
    }

    unix_fd fd()
    {
        return unix_fd{impl.native_sink()};
    }

    const std::string& file() const
    {
        return name;
    }

    template <typename WriteHandler>
    void async_write(WriteHandler&& handler)
    {
        impl.async_write_some(data(), std::forward<WriteHandler>(handler));
    }

  private:
    // Specialization for pointer types
    template <typename B = Buffer>
    typename std::enable_if<boost::has_dereference<B>::value,
                            boost::asio::const_buffer>::type
        data()
    {
        return boost::asio::buffer(*buffer);
    }

    template <typename B = Buffer>
    typename std::enable_if<!boost::has_dereference<B>::value,
                            boost::asio::const_buffer>::type
        data()
    {
        return boost::asio::buffer(buffer);
    }

    const std::string name;
    boost::process::async_pipe impl;
    Buffer buffer;
};

class VolatileFile
{
    using Buffer = CredentialsProvider::SecureBuffer;

  public:
    VolatileFile(Buffer&& contents) : size(contents->size())
    {
        auto data = std::move(contents);
        filePath = fs::temp_directory_path().c_str();
        filePath.append("/VM-XXXXXX");
        create(data);
    }

    ~VolatileFile()
    {
        purgeFileContents();
        fs::remove(filePath);
    }

    const std::string& path()
    {
        return filePath;
    }

    class FileObject
    {
      public:
        explicit FileObject(int fd) : fd(fd){};
        FileObject() = delete;
        FileObject(const FileObject&) = delete;
        FileObject& operator=(const FileObject&) = delete;

        ssize_t write(void* data, size_t nw)
        {
            return ::write(fd, data, nw);
        }

        ~FileObject()
        {
            close(fd);
        }

      private:
        int fd;
    };

  private:
    void create(const Buffer& data)
    {
        auto fd = mkstemp(filePath.data());

        FileObject file(fd);
        if (file.write(data->data(), data->size()) !=
            static_cast<ssize_t>(data->size()))
        {
            throw sdbusplus::exception::SdBusError(
                EIO, "I/O error on temporary file");
        }
    }

    void purgeFileContents()
    {
        if (std::ofstream file(filePath); file)
        {
            std::array<char, secretLimit> buf;
            buf.fill('*');

            std::size_t bytesWritten = 0;
            while (bytesWritten < size)
            {
                std::size_t bytesToWrite =
                    std::min(secretLimit, (size - bytesWritten));
                file.write(buf.data(),
                           static_cast<std::streamsize>(bytesToWrite));
                bytesWritten += bytesToWrite;
            }
        }
    }

    std::string filePath;
    const std::size_t size;
};

class SignalSender
{
  public:
    SignalSender(std::shared_ptr<sdbusplus::asio::connection> con,
                 const std::string& obj, const std::string& iface,
                 const std::string& name) :
        con(con),
        interface(iface), object(obj), name(name){};

    SignalSender() = delete;
    SignalSender(const SignalSender&) = delete;

    void send(std::optional<const std::error_code> status)
    {
        auto msgSignal =
            con->new_signal(object.c_str(), interface.c_str(), name.c_str());

        msgSignal.append(status.value_or(std::error_code{}).value());
        LogMsg(Logger::Debug, "Sending signal: Object: ", object,
               ", Interface: ", interface, ", Name: ", name,
               "Status: ", status.value_or(std::error_code{}).value());
        msgSignal.signal_send();
    }

  private:
    std::shared_ptr<sdbusplus::asio::connection> con;
    std::string interface;
    std::string object;
    std::string name;
};

class NotificationWrapper
{
  public:
    NotificationWrapper(std::unique_ptr<SignalSender> signal,
                        std::unique_ptr<boost::asio::steady_timer> timer) :
        signal(std::move(signal)),
        timer(std::move(timer))
    {
    }

    void start(std::function<void(const boost::system::error_code&)>&& handler,
               const std::chrono::seconds& duration)
    {
        LogMsg(Logger::Debug, "Notification initiated");
        started = true;
        timer->expires_from_now(duration);
        timer->async_wait([this, handler{std::move(handler)}](
                              const boost::system::error_code& ec) {
            started = false;
            handler(ec);
        });
    }

    void notify(const std::error_code& ec)
    {
        if (started)
        {
            timer->cancel();
            if (ec)
                signal->send((ec));
            else
                signal->send(std::nullopt);
            started = false;
            return;
        }
        LogMsg(Logger::Debug, "Notification(ec) supressed (not started)");
    }

  private:
    std::unique_ptr<SignalSender> signal;
    std::unique_ptr<boost::asio::steady_timer> timer;
    bool started{false};
};

} // namespace utils