libfilezilla
Loading...
Searching...
No Matches
impersonation.hpp
Go to the documentation of this file.
1#ifndef LIBFILEZILLA_IMPERSONATION_HEADER
2#define LIBFILEZILLA_IMPERSONATION_HEADER
3
7
8#include <array>
9#include <cstddef>
10#include <functional>
11#include <initializer_list>
12#include <memory>
13#include <variant>
14#include <type_traits>
15#include <utility>
16
17#include "string.hpp"
18#include "logger.hpp"
19
20#ifdef FZ_WINDOWS
21#include "glue/windows.hpp"
22#endif
23
24namespace fz {
25
27
28class FZ_PUBLIC_SYMBOL user
29{
30 // An invalid user. Used only during default construction
31 struct FZ_PUBLIC_SYMBOL invalid
32 {
33 explicit operator bool() const;
34 };
35
36public:
38 struct FZ_PUBLIC_SYMBOL name
39 {
41
42 name() = default;
43 name(fz::native_string v);
44
45 explicit operator bool() const;
46 };
47
49 struct FZ_PUBLIC_SYMBOL id
50 {
51 std::string value;
52
53 explicit operator bool() const;
54 };
55
56 user() = default;
57
58 template <typename T, std::enable_if_t<std::is_constructible_v<name, T>>* = nullptr>
59 user(T && user_name)
60 : value_(name{std::forward<T>(user_name)})
61 {
62 }
63
64 user(id user_id);
65
66 explicit operator bool() const;
67
68 fz::native_string const *get_name() const;
69
70 std::string const *get_id() const;
71
72private:
73 std::variant<invalid, name, id> value_;
74};
75
76#if FZ_WINDOWS
77namespace impersonation_desktop
78{
85 struct automatic {};
86
88 struct isolated {
91 std::wstring desktop_name_prefix;
92 };
93
95 struct named {
96 std::wstring name;
97 };
98
100 using mode = std::variant<automatic, isolated, named>;
101};
102
115#endif
116
139
140class impersonation_token_impl;
142
143enum class impersonation_capability : std::size_t
144{
153
163
171};
172
190
191class FZ_PUBLIC_SYMBOL impersonation_capabilities final
192{
193public:
194 impersonation_capability_state operator[](impersonation_capability capability) const;
195
196private:
197 friend class impersonation_token;
198
199 std::array<impersonation_capability_state, 3> states_{};
200};
201
212class FZ_PUBLIC_SYMBOL impersonation_token final
213{
214public:
215 impersonation_token();
216
217 impersonation_token(impersonation_token&&) noexcept;
218 impersonation_token& operator=(impersonation_token&&) noexcept;
219
221 explicit impersonation_token(fz::user const& user, fz::native_string const &password, fz::logger_interface& logger = get_null_logger(), impersonation_options const& opts = {});
222 explicit impersonation_token(fz::user const& user, impersonation_options::pwless_type, fz::logger_interface& logger = get_null_logger(), impersonation_options const& opts = {});
223
226 static impersonation_capabilities get_capabilities(std::initializer_list<impersonation_capability> requested);
227
228 ~impersonation_token() noexcept;
229
230 explicit operator bool() const {
231 return impl_.operator bool();
232 }
233
234 bool operator==(impersonation_token const&) const;
235 bool operator<(impersonation_token const&) const;
236
239
242
244 std::size_t hash() const noexcept;
245
247 std::string uid() const;
248
249private:
250 impersonation_token(fz::user const& user, fz::native_string const *password, fz::logger_interface& logger = get_null_logger(), impersonation_options const& opts = {});
251
252 friend class impersonation_token_impl;
253 std::unique_ptr<impersonation_token_impl> impl_;
254};
255
256#if !FZ_WINDOWS
258bool FZ_PUBLIC_SYMBOL set_process_impersonation(impersonation_token const& token);
259#else
260HANDLE FZ_PUBLIC_SYMBOL get_handle(impersonation_token const& token);
261#endif
262
265
268std::string FZ_PUBLIC_SYMBOL get_user_id(native_string const& name);
269
271native_string FZ_PUBLIC_SYMBOL get_user_name(std::string const& id);
272
273}
274
275namespace std {
276
278template <>
279struct hash<fz::impersonation_token>
280{
281 std::size_t operator()(fz::impersonation_token const& op) const noexcept
282 {
283 return op.hash();
284 }
285};
286
287}
288
289#endif
Definition impersonation.hpp:192
Impersonation tokens for a given user can be used to spawn processes running as that user.
Definition impersonation.hpp:213
std::string uid() const
A opaque unique identifier.
fz::native_string home() const
Returns home directory, may be empty.
fz::native_string username() const
Returns the name of the impersonated user.
std::size_t hash() const noexcept
For std::hash.
impersonation_token(fz::user const &user, fz::native_string const &password, fz::logger_interface &logger=get_null_logger(), impersonation_options const &opts={})
Creates an impersonation token, verifying credentials in the process.
static impersonation_capabilities get_capabilities(std::initializer_list< impersonation_capability > requested)
Abstract interface for logging strings.
Definition logger.hpp:51
Definition impersonation.hpp:29
std::variant< automatic, isolated, named > mode
Window station and desktop selection mode for impersonated processes.
Definition impersonation.hpp:100
Interface for logging.
The namespace used by libfilezilla.
Definition apply.hpp:17
native_string current_username()
Returns the username the calling thread is running under.
std::string get_user_id(native_string const &name)
std::wstring native_string
A string in the system's native character type and encoding. Note: This typedef changes depending on...
Definition string.hpp:69
native_string get_user_name(std::string const &id)
impersonation_capability_state
Definition impersonation.hpp:174
@ indeterminate
The capability's prerequisites could not be determined.
Definition impersonation.hpp:182
@ not_applicable
The capability does not apply to the platform.
Definition impersonation.hpp:179
@ unavailable
The process does not satisfy the capability's known prerequisites.
Definition impersonation.hpp:185
@ not_requested
The capability was not requested.
Definition impersonation.hpp:176
@ available
The process satisfies the capability's known prerequisites.
Definition impersonation.hpp:188
impersonation_profile_mode
Definition impersonation.hpp:104
@ skip
Do not load the user's profile.
Definition impersonation.hpp:106
@ require_if_roaming
Definition impersonation.hpp:110
@ require
Load the user's profile and fail token construction if loading fails.
Definition impersonation.hpp:113
bool operator==(symmetric_key const &lhs, symmetric_key const &rhs)
Side-channel safe comparison.
bool operator<(strtokenizer< LhsString, LhsDelims > const &lhs, strtokenizer< RhsString, RhsDelims > const &rhs)
strtokenizer class less-than comparator.
Definition string.hpp:566
impersonation_capability
Definition impersonation.hpp:144
@ user_profile
Definition impersonation.hpp:170
@ password
Definition impersonation.hpp:152
@ passwordless
Definition impersonation.hpp:162
String types and assorted functions.
Definition impersonation.hpp:85
Create an isolated window station and desktop for spawned processes.
Definition impersonation.hpp:88
std::wstring desktop_name_prefix
Definition impersonation.hpp:91
Use a caller-provided desktop name, such as L"WinSta0\\Default".
Definition impersonation.hpp:95
Impersonate as any user without checking credentials.
Definition impersonation.hpp:120
Definition impersonation.hpp:118
bool drop_admin_privileges
If true, remove administrative privileges from the impersonation token.
Definition impersonation.hpp:133
impersonation_desktop::mode desktop
Definition impersonation.hpp:127
impersonation_profile_mode profile
Controls whether the user's profile is loaded.
Definition impersonation.hpp:130
Numeric UID on Unix-like systems, string SID on Windows.
Definition impersonation.hpp:50
A user name.
Definition impersonation.hpp:39