libsshpp v0.1.7
Modern C++17 wrapper for libssh
Loading...
Searching...
No Matches
handlers.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// Callback-style server API (event-driven, required for anything serving many
4// channels/clients). See docs/design/08 §8.6-§8.7.
5//
6// Scope note relative to the design doc: keyboard-interactive auth and
7// tcpip-forward/direct-tcpip channel handling are not wired here because the
8// installed libssh's ssh_server_callbacks_struct has no callback slots for
9// them (message-style server::Session::try_next_message() still covers those
10// via SSH_REQUEST_AUTH/keyboard-interactive and SSH_CHANNEL_DIRECT_TCPIP).
11// on_x11_request takes plain fields rather than a shared X11Request type so
12// that the server module has no hard dependency on LIBSSHPP_WITH_FORWARDING.
13#pragma once
14
15#include <sshpp/config.hpp>
16
17#include <sshpp/channel.hpp>
18#include <sshpp/error.hpp>
19#include <sshpp/export.hpp>
20#include <sshpp/key.hpp>
23#include <sshpp/types.hpp>
24
25#include <cstdint>
26#include <functional>
27#include <istream>
28#include <memory>
29#include <optional>
30#include <ostream>
31#include <string>
32#include <string_view>
33#include <unordered_map>
34#include <vector>
35
36namespace sshpp::server {
37
39
42class SSHPP_API SessionHandler {
43public:
44 virtual ~SessionHandler() = default;
45
46 virtual AuthResult on_auth_none(Session&, std::string_view user);
47 virtual AuthResult on_auth_password(Session&, std::string_view user, const SecureString& password);
50 virtual AuthResult on_auth_public_key(Session&, std::string_view user, const Key&, PublicKeyState state);
51 virtual AuthResult on_auth_gssapi_mic(Session&, std::string_view user, std::string_view principal);
52
54 virtual std::shared_ptr<class ChannelHandler> on_channel_open_session(Session&);
55
56 virtual void on_service_request(Session&, std::string_view service);
57 virtual void on_disconnect(Session&);
58 virtual void on_error(Session&, const ErrorInfo&);
59};
60
62class SSHPP_API ChannelHandler {
63public:
64 virtual ~ChannelHandler() = default;
65
66 virtual bool on_pty_request(Channel&, std::string_view term, PtySize);
67 virtual bool on_pty_resize(Channel&, PtySize);
68 virtual bool on_shell_request(Channel&);
69 virtual bool on_exec_request(Channel&, std::string_view command);
70 virtual bool on_subsystem_request(Channel&, std::string_view name);
71 virtual bool on_env_request(Channel&, std::string_view name, std::string_view value);
72 virtual void on_x11_request(Channel&, bool single_connection, std::string_view auth_protocol,
73 std::string_view auth_cookie, std::uint32_t screen_number);
74 virtual void on_signal(Channel&, std::string_view signal_name);
75
77 virtual std::size_t on_data(Channel&, ByteView, Stream);
78 virtual void on_eof(Channel&);
79 virtual void on_close(Channel&);
81 virtual void on_writable(Channel&);
82};
83
86class SSHPP_API SimpleAuthHandler : public SessionHandler {
87public:
88 void allow_password(std::string user, SecureString password);
89 void allow_public_key(std::string user, Key public_key);
90 void allow_none(std::string user); // testing only
91 void set_max_attempts(int n) noexcept { max_attempts_ = n; }
92
93 AuthResult on_auth_none(Session&, std::string_view user) override;
94 AuthResult on_auth_password(Session&, std::string_view user, const SecureString&) override;
95 AuthResult on_auth_public_key(Session&, std::string_view user, const Key&, PublicKeyState) override;
96
97private:
98 struct Entry {
99 std::optional<SecureString> password;
100 std::vector<Fingerprint> public_key_fingerprints;
101 bool allow_none = false;
102 };
103 Entry* find(std::string_view user);
104 bool record_attempt_and_check(std::string_view user);
105
106 std::unordered_map<std::string, Entry> entries_;
107 std::unordered_map<std::string, int> attempts_;
108 int max_attempts_ = 6;
109};
110
112class SSHPP_API CommandHandler : public ChannelHandler {
113public:
114 using Runner = std::function<int(std::string_view command, std::istream& in, std::ostream& out,
115 std::ostream& err)>;
116 explicit CommandHandler(Runner runner) : runner_(std::move(runner)) {}
117
118 bool on_exec_request(Channel&, std::string_view command) override;
119 std::size_t on_data(Channel&, ByteView, Stream) override;
120 void on_eof(Channel&) override;
121
122private:
123 Runner runner_;
124 std::string command_;
125 std::string input_buffer_;
126 bool eof_ = false;
127 bool ran_ = false;
128};
129
130} // namespace sshpp::server
131
132#if SSHPP_HEADER_ONLY
134#endif
Input byte range. Implicitly constructible from std::string_view / std::vector<std::byte>.
Definition types.hpp:54
Definition channel.hpp:36
Definition key.hpp:59
Definition types.hpp:132
Per-channel request/data handling. See docs/design/08 §8.6.
Definition handlers.hpp:62
virtual ~ChannelHandler()=default
Runs a fixed callback per exec request; no local process is spawned.
Definition handlers.hpp:112
CommandHandler(Runner runner)
Definition handlers.hpp:116
std::function< int(std::string_view command, std::istream &in, std::ostream &out, std::ostream &err)> Runner
Definition handlers.hpp:115
Definition handlers.hpp:42
virtual ~SessionHandler()=default
One accepted server-side connection. See docs/design/08 §8.4.
Definition server_session.hpp:32
Definition handlers.hpp:86
void set_max_attempts(int n) noexcept
Definition handlers.hpp:91
Definition server_bind.ipp:15
AuthResult
Definition handlers.hpp:38
PublicKeyState
Definition message.hpp:27
Stream
Definition channel.hpp:21
Definition error.hpp:110
Definition error.hpp:119
Definition channel.hpp:32