| #pragma once |
| |
| #include <boost/asio/buffer.hpp> |
| #include <boost/asio/io_context.hpp> |
| #include <boost/asio/write.hpp> |
| #include <boost/process/async_pipe.hpp> |
| |
| #include <array> |
| #include <string> |
| |
| // Wrapper for boost::async_pipe ensuring proper pipe cleanup |
| class CredentialsPipe |
| { |
| public: |
| explicit CredentialsPipe(boost::asio::io_context& io) : impl(io) {} |
| |
| CredentialsPipe(const CredentialsPipe&) = delete; |
| CredentialsPipe(CredentialsPipe&&) = delete; |
| CredentialsPipe& operator=(const CredentialsPipe&) = delete; |
| CredentialsPipe& operator=(CredentialsPipe&&) = delete; |
| |
| ~CredentialsPipe() |
| { |
| explicit_bzero(user.data(), user.capacity()); |
| explicit_bzero(pass.data(), pass.capacity()); |
| } |
| |
| int fd() const |
| { |
| return impl.native_source(); |
| } |
| |
| template <typename WriteHandler> |
| void asyncWrite(std::string&& username, std::string&& password, |
| WriteHandler&& handler) |
| { |
| user = std::move(username); |
| pass = std::move(password); |
| |
| // Add +1 to ensure that the null terminator is included. |
| std::array<boost::asio::const_buffer, 2> buffer{ |
| {{user.data(), user.size() + 1}, {pass.data(), pass.size() + 1}}}; |
| boost::asio::async_write(impl, buffer, |
| std::forward<WriteHandler>(handler)); |
| } |
| |
| boost::process::async_pipe impl; |
| |
| private: |
| std::string user; |
| std::string pass; |
| }; |