Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame^] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 3 | #pragma once |
| 4 | |
Ed Tanous | 3bfa3b2 | 2024-01-31 12:18:03 -0800 | [diff] [blame] | 5 | #include "logging.hpp" |
| 6 | |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 7 | #include <boost/asio/buffer.hpp> |
Ed Tanous | 3bfa3b2 | 2024-01-31 12:18:03 -0800 | [diff] [blame] | 8 | #include <boost/asio/connect_pipe.hpp> |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 9 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 3bfa3b2 | 2024-01-31 12:18:03 -0800 | [diff] [blame] | 10 | #include <boost/asio/readable_pipe.hpp> |
| 11 | #include <boost/asio/writable_pipe.hpp> |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 12 | #include <boost/asio/write.hpp> |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 13 | |
| 14 | #include <array> |
| 15 | #include <string> |
| 16 | |
| 17 | // Wrapper for boost::async_pipe ensuring proper pipe cleanup |
| 18 | class CredentialsPipe |
| 19 | { |
| 20 | public: |
Ed Tanous | 3bfa3b2 | 2024-01-31 12:18:03 -0800 | [diff] [blame] | 21 | explicit CredentialsPipe(boost::asio::io_context& io) : impl(io), read(io) |
| 22 | { |
| 23 | boost::system::error_code ec; |
| 24 | boost::asio::connect_pipe(read, impl, ec); |
| 25 | if (ec) |
| 26 | { |
| 27 | BMCWEB_LOG_CRITICAL("Failed to connect pipe {}", ec.what()); |
| 28 | } |
| 29 | } |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 30 | |
| 31 | CredentialsPipe(const CredentialsPipe&) = delete; |
| 32 | CredentialsPipe(CredentialsPipe&&) = delete; |
| 33 | CredentialsPipe& operator=(const CredentialsPipe&) = delete; |
| 34 | CredentialsPipe& operator=(CredentialsPipe&&) = delete; |
| 35 | |
| 36 | ~CredentialsPipe() |
| 37 | { |
| 38 | explicit_bzero(user.data(), user.capacity()); |
| 39 | explicit_bzero(pass.data(), pass.capacity()); |
| 40 | } |
| 41 | |
Ed Tanous | 3bfa3b2 | 2024-01-31 12:18:03 -0800 | [diff] [blame] | 42 | int releaseFd() |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 43 | { |
Ed Tanous | 3bfa3b2 | 2024-01-31 12:18:03 -0800 | [diff] [blame] | 44 | return read.release(); |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | template <typename WriteHandler> |
| 48 | void asyncWrite(std::string&& username, std::string&& password, |
| 49 | WriteHandler&& handler) |
| 50 | { |
| 51 | user = std::move(username); |
| 52 | pass = std::move(password); |
| 53 | |
| 54 | // Add +1 to ensure that the null terminator is included. |
| 55 | std::array<boost::asio::const_buffer, 2> buffer{ |
| 56 | {{user.data(), user.size() + 1}, {pass.data(), pass.size() + 1}}}; |
| 57 | boost::asio::async_write(impl, buffer, |
| 58 | std::forward<WriteHandler>(handler)); |
| 59 | } |
| 60 | |
Ed Tanous | 3bfa3b2 | 2024-01-31 12:18:03 -0800 | [diff] [blame] | 61 | boost::asio::writable_pipe impl; |
| 62 | boost::asio::readable_pipe read; |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 63 | |
| 64 | private: |
| 65 | std::string user; |
| 66 | std::string pass; |
| 67 | }; |