Joedb 10.0.4
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 long timeout = 300;
64 ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &timeout);
65 }
66
67 {
68 const int no_delay = 1;
69 ssh_options_set(session, SSH_OPTIONS_NODELAY, &no_delay);
70 }
71
72 check_result(ssh_connect(session));
73 }
74
76 {
77 ssh_disconnect(session);
78 }
79 };
80
81 /// @ingroup concurrency
83 {
84 private:
85 ssh_key key;
86
87 public:
88 Imported_Key(const char *b64_key, const char *passphrase): key(nullptr)
89 {
90 ssh_pki_import_privkey_base64
91 (
92 b64_key,
93 passphrase,
94 nullptr,
95 nullptr,
96 &key
97 );
98
100 }
101
102 Imported_Key(const Imported_Key &) = delete;
104
105 ssh_key get() const
106 {
107 return key;
108 }
109
111 {
112 ssh_key_free(key);
113 }
114 };
115
116 /// @ingroup concurrency
118 {
119 public:
121 (
122 const std::string &user,
123 const std::string &host,
124 const unsigned port,
125 const int verbosity,
126 const char * const b64_key = nullptr,
127 const char * const passphrase = nullptr
128 ):
129 Session_Connection(user.c_str(), host.c_str(), port, verbosity)
130 {
131 if (b64_key)
132 {
133 const Imported_Key key(b64_key, passphrase);
134 check_result(ssh_userauth_publickey(session, user.c_str(), key.get()));
135 }
136 else
137 {
138 check_result(ssh_userauth_publickey_auto(session, nullptr, passphrase));
139 }
140 }
141 };
142 }
143}
144
145#endif
Imported_Key(const char *b64_key, const char *passphrase)
Definition Session.h:88
Imported_Key & operator=(const Imported_Key &)=delete
ssh_key get() const
Definition Session.h:105
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:121
#define JOEDB_RELEASE_ASSERT(x)
always-tested assertion (release and debug mode)
Definition assert.h:24
Definition Blob.h:7