Joedb 9.5.0
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 ///////////////////////////////////////////////////////////////////////////
15 ///////////////////////////////////////////////////////////////////////////
16 {
17 protected:
18 const ssh_session session;
19
20 public:
25
28
29 ssh_session get() const
30 {
31 return session;
32 }
33
34 void check_result(int result) const
35 {
36 if (result != SSH_OK)
37 throw Exception(ssh_get_error(session));
38 }
39
41 {
42 ssh_free(session);
43 }
44 };
45
46 ///////////////////////////////////////////////////////////////////////////
48 ///////////////////////////////////////////////////////////////////////////
49 {
50 public:
52 (
53 const char *user,
54 const char *host,
55 unsigned port,
56 int verbosity
57 )
58 {
59 ssh_options_set(session, SSH_OPTIONS_HOST, host);
60 ssh_options_set(session, SSH_OPTIONS_USER, user);
61 ssh_options_set(session, SSH_OPTIONS_PORT, &port);
62 ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
63
64 {
65 const int no_delay = 1;
66 ssh_options_set(session, SSH_OPTIONS_NODELAY, &no_delay);
67 }
68
69 check_result(ssh_connect(session));
70 }
71
73 {
74 ssh_disconnect(session);
75 }
76 };
77
78 ///////////////////////////////////////////////////////////////////////////
80 ///////////////////////////////////////////////////////////////////////////
81 {
82 private:
83 ssh_key key;
84
85 public:
86 Imported_Key(const char *b64_key, const char *passphrase): key(nullptr)
87 {
88 ssh_pki_import_privkey_base64
89 (
90 b64_key,
91 passphrase,
92 nullptr,
93 nullptr,
94 &key
95 );
96
98 }
99
100 Imported_Key(const Imported_Key &) = delete;
102
103 ssh_key get() const
104 {
105 return key;
106 }
107
109 {
110 ssh_key_free(key);
111 }
112 };
113
114 ///////////////////////////////////////////////////////////////////////////
116 ///////////////////////////////////////////////////////////////////////////
117 {
118 public:
120 (
121 const std::string &user,
122 const std::string &host,
123 const unsigned port,
124 const int verbosity,
125 const char * const b64_key = nullptr,
126 const char * const passphrase = nullptr
127 ):
128 Session_Connection(user.c_str(), host.c_str(), port, verbosity)
129 {
130 if (b64_key)
131 {
132 const Imported_Key key(b64_key, passphrase);
133 check_result(ssh_userauth_publickey(session, user.c_str(), key.get()));
134 }
135 else
136 {
137 check_result(ssh_userauth_publickey_auto(session, nullptr, passphrase));
138 }
139 }
140 };
141 }
142}
143
144#endif
Imported_Key(const char *b64_key, const char *passphrase)
Definition Session.h:86
Imported_Key & operator=(const Imported_Key &)=delete
ssh_key get() const
Definition Session.h:103
Imported_Key(const Imported_Key &)=delete
const ssh_session session
Definition Session.h:18
void check_result(int result) const
Definition Session.h:34
ssh_session get() const
Definition Session.h:29
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:52
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:120
#define JOEDB_RELEASE_ASSERT(x)
Definition assert.h:24
Definition Blob.h:7