libsshpp v0.1.7
Modern C++17 wrapper for libssh
Loading...
Searching...
No Matches
sftp_subsystem.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// A chroot-style SFTP server subsystem. See docs/design/08 §8.7.
4//
5// Deliberately NOT a ChannelHandler in the on_data() sense: libssh's SFTP
6// server helpers (sftp_server_new/sftp_get_client_message) do their own
7// blocking ssh_channel_read() internally, which cannot be driven from the
8// event-loop's async channel_data callback. try_serve() instead owns the
9// channel synchronously (typically from a dedicated thread, one per SFTP
10// subsystem request) -- see the "Concurrency" note below. This still
11// implements the ChannelHandler interface for convenience: on_subsystem_request
12// spawns that thread.
13//
14// This is the single most security-sensitive component in the library: every
15// client-supplied path is resolved with resolve() (join -> weakly_canonical ->
16// verify it stays under the canonicalized root) before touching the
17// filesystem; any failure is reported as SSH_FX_PERMISSION_DENIED /
18// SSH_FX_NO_SUCH_FILE rather than ever passing an unchecked path to a POSIX
19// call. There is no support for arbitrary absolute paths breaking out of root.
20#pragma once
21
22#include <sshpp/config.hpp>
23
24#include <sshpp/error.hpp>
25#include <sshpp/export.hpp>
26#include <sshpp/result.hpp>
28
29#include <cstdint>
30#include <filesystem>
31#include <thread>
32#include <vector>
33
34namespace sshpp::server {
35
44class SSHPP_API SftpSubsystemHandler : public ChannelHandler {
45public:
46 struct Options {
47 std::filesystem::path root;
48 bool read_only = false;
49 bool follow_symlinks_out_of_root = false;
50 std::uint64_t max_file_size = 0; // 0 = unlimited
51 };
52
53 explicit SftpSubsystemHandler(Options options) : options_(std::move(options)) {}
54 ~SftpSubsystemHandler() override;
55
58 Result<void> try_serve(Channel& channel) const;
59
60 // ---- ChannelHandler: spawns a dedicated thread running try_serve() ---------
61 bool on_subsystem_request(Channel&, std::string_view name) override;
62 void on_close(Channel&) override;
63
64private:
65 Options options_;
66 mutable std::vector<std::thread> worker_threads_;
67};
68
69} // namespace sshpp::server
70
71#if SSHPP_HEADER_ONLY
73#endif
Definition channel.hpp:36
Definition result.hpp:16
Per-channel request/data handling. See docs/design/08 §8.6.
Definition handlers.hpp:62
Definition sftp_subsystem.hpp:44
SftpSubsystemHandler(Options options)
Definition sftp_subsystem.hpp:53
Definition server_bind.ipp:15
Definition error.hpp:110
Definition sftp_subsystem.hpp:46
std::filesystem::path root
Definition sftp_subsystem.hpp:47