blob: 2cc3dc8b28912bd6ad2cd66b6d199fd4c0a2520f [file] [log] [blame]
Ed Tanous11e8f602023-08-24 14:25:18 -07001#pragma once
2
Ed Tanous3bfa3b22024-01-31 12:18:03 -08003#include "logging.hpp"
4
Ed Tanous11e8f602023-08-24 14:25:18 -07005#include <boost/asio/buffer.hpp>
Ed Tanous3bfa3b22024-01-31 12:18:03 -08006#include <boost/asio/connect_pipe.hpp>
Ed Tanous11e8f602023-08-24 14:25:18 -07007#include <boost/asio/io_context.hpp>
Ed Tanous3bfa3b22024-01-31 12:18:03 -08008#include <boost/asio/readable_pipe.hpp>
9#include <boost/asio/writable_pipe.hpp>
Ed Tanous11e8f602023-08-24 14:25:18 -070010#include <boost/asio/write.hpp>
Ed Tanous11e8f602023-08-24 14:25:18 -070011
12#include <array>
13#include <string>
14
15// Wrapper for boost::async_pipe ensuring proper pipe cleanup
16class CredentialsPipe
17{
18 public:
Ed Tanous3bfa3b22024-01-31 12:18:03 -080019 explicit CredentialsPipe(boost::asio::io_context& io) : impl(io), read(io)
20 {
21 boost::system::error_code ec;
22 boost::asio::connect_pipe(read, impl, ec);
23 if (ec)
24 {
25 BMCWEB_LOG_CRITICAL("Failed to connect pipe {}", ec.what());
26 }
27 }
Ed Tanous11e8f602023-08-24 14:25:18 -070028
29 CredentialsPipe(const CredentialsPipe&) = delete;
30 CredentialsPipe(CredentialsPipe&&) = delete;
31 CredentialsPipe& operator=(const CredentialsPipe&) = delete;
32 CredentialsPipe& operator=(CredentialsPipe&&) = delete;
33
34 ~CredentialsPipe()
35 {
36 explicit_bzero(user.data(), user.capacity());
37 explicit_bzero(pass.data(), pass.capacity());
38 }
39
Ed Tanous3bfa3b22024-01-31 12:18:03 -080040 int releaseFd()
Ed Tanous11e8f602023-08-24 14:25:18 -070041 {
Ed Tanous3bfa3b22024-01-31 12:18:03 -080042 return read.release();
Ed Tanous11e8f602023-08-24 14:25:18 -070043 }
44
45 template <typename WriteHandler>
46 void asyncWrite(std::string&& username, std::string&& password,
47 WriteHandler&& handler)
48 {
49 user = std::move(username);
50 pass = std::move(password);
51
52 // Add +1 to ensure that the null terminator is included.
53 std::array<boost::asio::const_buffer, 2> buffer{
54 {{user.data(), user.size() + 1}, {pass.data(), pass.size() + 1}}};
55 boost::asio::async_write(impl, buffer,
56 std::forward<WriteHandler>(handler));
57 }
58
Ed Tanous3bfa3b22024-01-31 12:18:03 -080059 boost::asio::writable_pipe impl;
60 boost::asio::readable_pipe read;
Ed Tanous11e8f602023-08-24 14:25:18 -070061
62 private:
63 std::string user;
64 std::string pass;
65};