blob: ec159e60ac8d1ba2ff8ca7251fede9a396d622a8 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous11e8f602023-08-24 14:25:18 -07003#pragma once
4
Ed Tanous3bfa3b22024-01-31 12:18:03 -08005#include "logging.hpp"
6
Ed Tanous11e8f602023-08-24 14:25:18 -07007#include <boost/asio/buffer.hpp>
Ed Tanous3bfa3b22024-01-31 12:18:03 -08008#include <boost/asio/connect_pipe.hpp>
Ed Tanous11e8f602023-08-24 14:25:18 -07009#include <boost/asio/io_context.hpp>
Ed Tanous3bfa3b22024-01-31 12:18:03 -080010#include <boost/asio/readable_pipe.hpp>
11#include <boost/asio/writable_pipe.hpp>
Ed Tanous11e8f602023-08-24 14:25:18 -070012#include <boost/asio/write.hpp>
Ed Tanous11e8f602023-08-24 14:25:18 -070013
14#include <array>
Ed Tanousd7857202025-01-28 15:32:26 -080015#include <cstring>
Ed Tanous11e8f602023-08-24 14:25:18 -070016#include <string>
Ed Tanousd7857202025-01-28 15:32:26 -080017#include <utility>
Ed Tanous11e8f602023-08-24 14:25:18 -070018
19// Wrapper for boost::async_pipe ensuring proper pipe cleanup
20class CredentialsPipe
21{
22 public:
Ed Tanous3bfa3b22024-01-31 12:18:03 -080023 explicit CredentialsPipe(boost::asio::io_context& io) : impl(io), read(io)
24 {
25 boost::system::error_code ec;
Ed Tanousd7857202025-01-28 15:32:26 -080026
27 // Unclear why tidy complains here.
28 // NOLINTNEXTLINE(misc-include-cleaner)
Ed Tanous3bfa3b22024-01-31 12:18:03 -080029 boost::asio::connect_pipe(read, impl, ec);
30 if (ec)
31 {
32 BMCWEB_LOG_CRITICAL("Failed to connect pipe {}", ec.what());
33 }
34 }
Ed Tanous11e8f602023-08-24 14:25:18 -070035
36 CredentialsPipe(const CredentialsPipe&) = delete;
37 CredentialsPipe(CredentialsPipe&&) = delete;
38 CredentialsPipe& operator=(const CredentialsPipe&) = delete;
39 CredentialsPipe& operator=(CredentialsPipe&&) = delete;
40
41 ~CredentialsPipe()
42 {
Ed Tanousd7857202025-01-28 15:32:26 -080043 // NOLINTNEXTLINE(misc-include-cleaner)
Ed Tanous11e8f602023-08-24 14:25:18 -070044 explicit_bzero(user.data(), user.capacity());
45 explicit_bzero(pass.data(), pass.capacity());
46 }
47
Ed Tanous3bfa3b22024-01-31 12:18:03 -080048 int releaseFd()
Ed Tanous11e8f602023-08-24 14:25:18 -070049 {
Ed Tanous3bfa3b22024-01-31 12:18:03 -080050 return read.release();
Ed Tanous11e8f602023-08-24 14:25:18 -070051 }
52
53 template <typename WriteHandler>
54 void asyncWrite(std::string&& username, std::string&& password,
55 WriteHandler&& handler)
56 {
57 user = std::move(username);
58 pass = std::move(password);
59
60 // Add +1 to ensure that the null terminator is included.
61 std::array<boost::asio::const_buffer, 2> buffer{
62 {{user.data(), user.size() + 1}, {pass.data(), pass.size() + 1}}};
63 boost::asio::async_write(impl, buffer,
64 std::forward<WriteHandler>(handler));
65 }
66
Ed Tanous3bfa3b22024-01-31 12:18:03 -080067 boost::asio::writable_pipe impl;
68 boost::asio::readable_pipe read;
Ed Tanous11e8f602023-08-24 14:25:18 -070069
70 private:
71 std::string user;
72 std::string pass;
73};