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> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame^] | 15 | #include <cstring> |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 16 | #include <string> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame^] | 17 | #include <utility> |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 18 | |
| 19 | // Wrapper for boost::async_pipe ensuring proper pipe cleanup |
| 20 | class CredentialsPipe |
| 21 | { |
| 22 | public: |
Ed Tanous | 3bfa3b2 | 2024-01-31 12:18:03 -0800 | [diff] [blame] | 23 | explicit CredentialsPipe(boost::asio::io_context& io) : impl(io), read(io) |
| 24 | { |
| 25 | boost::system::error_code ec; |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame^] | 26 | |
| 27 | // Unclear why tidy complains here. |
| 28 | // NOLINTNEXTLINE(misc-include-cleaner) |
Ed Tanous | 3bfa3b2 | 2024-01-31 12:18:03 -0800 | [diff] [blame] | 29 | boost::asio::connect_pipe(read, impl, ec); |
| 30 | if (ec) |
| 31 | { |
| 32 | BMCWEB_LOG_CRITICAL("Failed to connect pipe {}", ec.what()); |
| 33 | } |
| 34 | } |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 35 | |
| 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 Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame^] | 43 | // NOLINTNEXTLINE(misc-include-cleaner) |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 44 | explicit_bzero(user.data(), user.capacity()); |
| 45 | explicit_bzero(pass.data(), pass.capacity()); |
| 46 | } |
| 47 | |
Ed Tanous | 3bfa3b2 | 2024-01-31 12:18:03 -0800 | [diff] [blame] | 48 | int releaseFd() |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 49 | { |
Ed Tanous | 3bfa3b2 | 2024-01-31 12:18:03 -0800 | [diff] [blame] | 50 | return read.release(); |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 51 | } |
| 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 Tanous | 3bfa3b2 | 2024-01-31 12:18:03 -0800 | [diff] [blame] | 67 | boost::asio::writable_pipe impl; |
| 68 | boost::asio::readable_pipe read; |
Ed Tanous | 11e8f60 | 2023-08-24 14:25:18 -0700 | [diff] [blame] | 69 | |
| 70 | private: |
| 71 | std::string user; |
| 72 | std::string pass; |
| 73 | }; |