Joedb 9.1.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
4#include "joedb/ssh/ssh.h"
5
6#include <string>
7
8namespace joedb
9{
10 namespace ssh
11 {
12 ///////////////////////////////////////////////////////////////////////////
14 ///////////////////////////////////////////////////////////////////////////
15 {
16 protected:
17 const ssh_session session;
18
19 public:
21 {
23 }
24
27
28 ssh_session get() const
29 {
30 return session;
31 }
32
33 void check_result(int result) const
34 {
36 }
37
39 {
40 ssh_free(session);
41 }
42 };
43
44 ///////////////////////////////////////////////////////////////////////////
46 ///////////////////////////////////////////////////////////////////////////
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 ///////////////////////////////////////////////////////////////////////////
78 ///////////////////////////////////////////////////////////////////////////
79 {
80 private:
81 ssh_key key;
82
83 public:
84 Imported_Key(const char *b64_key, const char *passphrase): key(nullptr)
85 {
86 ssh_pki_import_privkey_base64
87 (
88 b64_key,
89 passphrase,
90 nullptr,
91 nullptr,
92 &key
93 );
94
95 if (key == nullptr)
96 throw Exception("Could not import private key");
97 }
98
99 Imported_Key(const Imported_Key &) = delete;
101
102 ssh_key get() const
103 {
104 return key;
105 }
106
108 {
109 ssh_key_free(key);
110 }
111 };
112
113 ///////////////////////////////////////////////////////////////////////////
115 ///////////////////////////////////////////////////////////////////////////
116 {
117 public:
119 (
120 const std::string &user,
121 const std::string &host,
122 const unsigned port,
123 const int verbosity,
124 const char * const b64_key = nullptr,
125 const char * const passphrase = nullptr
126 ):
127 Session_Connection(user.c_str(), host.c_str(), port, verbosity)
128 {
129 if (b64_key)
130 {
131 const Imported_Key key(b64_key, passphrase);
132 check_result(ssh_userauth_publickey(session, user.c_str(), key.get()));
133 }
134 else
135 {
136 check_result(ssh_userauth_publickey_auto(session, nullptr, passphrase));
137 }
138 }
139 };
140 }
141}
142
143#endif
Imported_Key(const char *b64_key, const char *passphrase)
Definition Session.h:84
Imported_Key & operator=(const Imported_Key &)=delete
ssh_key get() const
Definition Session.h:102
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:119
void check_not_null(void *p)
Definition ssh.h:13
void check_ssh_session_result(ssh_session session, int result)
Definition ssh.h:21
Definition Blob.h:7