libfilezilla
impersonation.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_IMPERSONATION_HEADER
2 #define LIBFILEZILLA_IMPERSONATION_HEADER
3 
8 #include <memory>
9 #include <functional>
10 
11 #include "string.hpp"
12 #include "logger.hpp"
13 
14 #ifdef FZ_WINDOWS
15 #include "glue/windows.hpp"
16 #endif
17 
18 namespace fz {
19 
20 class logger_interface;
21 
23 {
25  struct pwless_type{};
26  static constexpr pwless_type pwless{};
27 
28 #if FZ_WINDOWS
29  bool drop_admin_privileges = true;
30 #else
31  fz::native_string group;
33 #endif
34 };
35 
36 class impersonation_token_impl;
37 
46 class FZ_PUBLIC_SYMBOL impersonation_token final
47 {
48 public:
50 
52  impersonation_token& operator=(impersonation_token&&) noexcept;
53 
55  explicit impersonation_token(fz::native_string const& username, fz::native_string const &password, fz::logger_interface& logger = get_null_logger(), impersonation_options const& opts = {});
56  explicit impersonation_token(fz::native_string const& username, impersonation_options::pwless_type, fz::logger_interface& logger = get_null_logger(), impersonation_options const& opts = {});
57 
58  ~impersonation_token() noexcept;
59 
60  explicit operator bool() const {
61  return impl_.operator bool();
62  }
63 
64  bool operator==(impersonation_token const&) const;
65  bool operator<(impersonation_token const&) const;
66 
68  fz::native_string username() const;
69 
71  fz::native_string home() const;
72 
74  std::size_t hash() const noexcept;
75 
77  std::string uid() const;
78 
79 private:
80  impersonation_token(fz::native_string const& username, fz::native_string const *password, fz::logger_interface& logger = get_null_logger(), impersonation_options const& opts = {});
81 
82  friend class impersonation_token_impl;
83  std::unique_ptr<impersonation_token_impl> impl_;
84 };
85 
86 #if !FZ_WINDOWS
87 bool FZ_PUBLIC_SYMBOL set_process_impersonation(impersonation_token const& token);
89 #endif
90 
92 native_string FZ_PUBLIC_SYMBOL current_username();
93 
96 std::string FZ_PUBLIC_SYMBOL get_user_uid(native_string const& username);
97 
98 }
99 
100 namespace std {
101 
103 template <>
104 struct hash<fz::impersonation_token>
105 {
106  std::size_t operator()(fz::impersonation_token const& op) const noexcept
107  {
108  return op.hash();
109  }
110 };
111 
112 }
113 
114 #endif
Definition: impersonation.hpp:22
bool operator==(symmetric_key const &lhs, symmetric_key const &rhs)
Side-channel safe comparison.
Definition: impersonation.hpp:100
Interface for logging.
Impersonation tokens for a given user can be used to spawn processes running as that user...
Definition: impersonation.hpp:46
native_string current_username()
Returns the username the calling thread is running under.
bool operator<(strtokenizer< LhsString, LhsDelims > const &lhs, strtokenizer< RhsString, RhsDelims > const &rhs)
strtokenizer class less-than comparator.
Definition: string.hpp:565
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
String types and assorted functions.
The namespace used by libfilezilla.
Definition: apply.hpp:17
Impersonate as any user without checking credentials.
Definition: impersonation.hpp:25
std::string get_user_uid(native_string const &username)
Abstract interface for logging strings.
Definition: logger.hpp:50