Joedb 10.0.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Session.h
Go to the documentation of this file.
1#ifndef joedb_ssh_Session_declared
2#define joedb_ssh_Session_declared
3
5
6#include <string>
7#include <libssh/libssh.h>
8
9namespace joedb
10{
11 namespace ssh
12 {
13 /// @ingroup concurrency
15 {
16 protected:
17 const ssh_session session;
18
19 public:
24
27
28 ssh_session get() const
29 {
30 return session;
31 }
32
33 void check_result(int result) const
34 {
35 if (result != SSH_OK)
36 throw Exception(ssh_get_error(session));
37 }
38
40 {
41 ssh_free(session);
42 }
43 };
44
45 /// @ingroup concurrency
47 {
48 public:
50 (
51 const char *user,
52 const char *host,
53 unsigned port,
54 int verbosity
55 )
56 {
57 ssh_options_set(session, SSH_OPTIONS_HOST, host);
58 ssh_options_set(session, SSH_OPTIONS_USER, user);
59 ssh_options_set(session, SSH_OPTIONS_PORT, &port);
60 ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
61
62 {
63 const int no_delay = 1;
64 ssh_options_set(session, SSH_OPTIONS_NODELAY, &no_delay);
65 }
66
67 check_result(ssh_connect(session));
68 }
69
71 {
72 ssh_disconnect(session);
73 }
74 };
75
76 /// @ingroup concurrency
78 {
79 private:
80 ssh_key key;
81
82 public:
83 Imported_Key(const char *b64_key, const char *passphrase): key(nullptr)
84 {
85 ssh_pki_import_privkey_base64
86 (
87 b64_key,
88 passphrase,
89 nullptr,
90 nullptr,
91 &key
92 );
93
95 }
96
97 Imported_Key(const Imported_Key &) = delete;
99
100 ssh_key get() const
101 {
102 return key;
103 }
104
106 {
107 ssh_key_free(key);
108 }
109 };
110
111 /// @ingroup concurrency
113 {
114 public:
116 (
117 const std::string &user,
118 const std::string &host,
119 const unsigned port,
120 const int verbosity,
121 const char * const b64_key = nullptr,
122 const char * const passphrase = nullptr
123 ):
124 Session_Connection(user.c_str(), host.c_str(), port, verbosity)
125 {
126 if (b64_key)
127 {
128 const Imported_Key key(b64_key, passphrase);
129 check_result(ssh_userauth_publickey(session, user.c_str(), key.get()));
130 }
131 else
132 {
133 check_result(ssh_userauth_publickey_auto(session, nullptr, passphrase));
134 }
135 }
136 };
137 }
138}
139
140#endif
Imported_Key(const char *b64_key, const char *passphrase)
Definition Session.h:83
Imported_Key & operator=(const Imported_Key &)=delete
ssh_key get() const
Definition Session.h:100
Imported_Key(const Imported_Key &)=delete
const ssh_session session
Definition Session.h:17
void check_result(int result) const
Definition Session.h:33
ssh_session get() const
Definition Session.h:28
Session_Allocation & operator=(const Session_Allocation &)=delete
Session_Allocation(const Session_Allocation &)=delete
Session_Connection(const char *user, const char *host, unsigned port, int verbosity)
Definition Session.h:50
Session(const std::string &user, const std::string &host, const unsigned port, const int verbosity, const char *const b64_key=nullptr, const char *const passphrase=nullptr)
Definition Session.h:116
#define JOEDB_RELEASE_ASSERT(x)
always-tested assertion (release and debug mode)
Definition assert.h:24
Definition Blob.h:7