-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheasylibssh.h
More file actions
342 lines (291 loc) · 9.1 KB
/
Copy patheasylibssh.h
File metadata and controls
342 lines (291 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "libssh_esp32.h"
#include "libssh_esp32_config.h"
#include <libssh/libssh.h>
#include <libssh/server.h>
ssh_session EASYLIBSSH_session = NULL;
ssh_bind EASYLIBSSH_sshbind = NULL;
ssh_key EASYLIBSSH_authorized_key[EASYLIBSSH_NBAUTHKEY] = { NULL };
void easylibssh_begin()
{
enum ssh_keytypes_e typekey = SSH_KEYTYPE_UNKNOWN;
int i,rc;
#ifdef EASYLIBSSH_DEBUG
Serial.println("Start of easylibssh_begin");
#endif
libssh_begin();
for(i=0;i<EASYLIBSSH_NBAUTHKEY;i++) {
#ifdef EASYLIBSSH_DEBUG
Serial.print("Ssh authorized key type ");
Serial.println(i);
#endif
typekey = ssh_key_type_from_name(EASYLIBSSH_TYPEKEY[i]);
#ifdef EASYLIBSSH_DEBUG
if (typekey == SSH_KEYTYPE_UNKNOWN) Serial.println("Failed to decode authorized key type");
Serial.print("Ssh authorized key ");
Serial.println(i);
#endif
rc = ssh_pki_import_pubkey_base64( EASYLIBSSH_AUTHKEY[i], typekey, &EASYLIBSSH_authorized_key[i]);
#ifdef EASYLIBSSH_DEBUG
if (rc != SSH_OK) Serial.println("Failed to decode b64 authorized key");
#endif
}
#ifdef EASYLIBSSH_DEBUG
Serial.println("End of easylibssh_begin");
#endif
}
ssh_channel easylibssh_loop_start()
{
ssh_message message = NULL;
ssh_channel chan = NULL;
int again = 1;
int patience = 0;
const int impatience = 10;
ssh_key host_key = NULL;
int i,rc;
#ifdef EASYLIBSSH_DEBUG
Serial.println("Start of easylibssh_loop_start");
Serial.println("Ssh bind new");
#endif
EASYLIBSSH_sshbind=ssh_bind_new();
if (EASYLIBSSH_sshbind == NULL) goto EASYLIBSSH_failed_bind_new;
#ifdef EASYLIBSSH_DEBUG
Serial.println("Ssh hostkey");
#endif
i = ssh_pki_import_privkey_base64( EASYLIBSSH_HOSTKEY, NULL, NULL, NULL, &host_key);
if (i != SSH_OK) goto EASYLIBSSH_failed_bind;
#ifdef EASYLIBSSH_DEBUG
Serial.println("Ssh bind import key");
Serial.println(ssh_key_type_to_char(ssh_key_type(host_key)));
#endif
if (ssh_bind_options_set(EASYLIBSSH_sshbind, SSH_BIND_OPTIONS_IMPORT_KEY, host_key) != SSH_OK) goto EASYLIBSSH_failed_bind;
#ifdef EASYLIBSSH_DEBUG
Serial.println("Ssh bind listen");
#endif
if (ssh_bind_listen(EASYLIBSSH_sshbind) != SSH_OK) goto EASYLIBSSH_failed_bind;
#ifdef EASYLIBSSH_DEBUG
Serial.println("Ssh new session");
#endif
EASYLIBSSH_session=ssh_new();
if (EASYLIBSSH_session == NULL) goto EASYLIBSSH_failed_bind;
#ifdef EASYLIBSSH_DEBUG
Serial.println("Ssh bind session");
#endif
if (ssh_bind_accept(EASYLIBSSH_sshbind, EASYLIBSSH_session) != SSH_OK) goto EASYLIBSSH_failed_session;
#ifdef EASYLIBSSH_DEBUG
Serial.println("Ssh key exchange");
#endif
if (ssh_handle_key_exchange(EASYLIBSSH_session) != SSH_OK) goto EASYLIBSSH_failed_session;
ssh_set_auth_methods(EASYLIBSSH_session, SSH_AUTH_METHOD_PUBLICKEY);
again = 1;
patience = 0;
do { // Ssh Authentification
#ifdef EASYLIBSSH_DEBUG
Serial.print("Authentification - New message - ");
Serial.println(patience);
#endif
message = ssh_message_get(EASYLIBSSH_session);
if(message) {
if (ssh_message_type(message) == SSH_REQUEST_AUTH && ssh_message_subtype(message) == SSH_AUTH_METHOD_PUBLICKEY) {
#ifdef EASYLIBSSH_DEBUG
Serial.print(ssh_message_auth_user(message));
Serial.println(" want to connect with pubkey");
#endif
for(i=0;i<EASYLIBSSH_NBAUTHKEY;i++) {
if (!ssh_key_cmp(ssh_message_auth_pubkey(message), EASYLIBSSH_authorized_key[i], SSH_KEY_CMP_PUBLIC)) {
switch (ssh_message_auth_publickey_state(message)) {
case SSH_PUBLICKEY_STATE_NONE:
#ifdef EASYLIBSSH_DEBUG
Serial.print("pubkey ok but not yet validated ");
Serial.println(i);
#endif
i=EASYLIBSSH_NBAUTHKEY;
ssh_message_auth_reply_pk_ok_simple(message);
break;
case SSH_PUBLICKEY_STATE_VALID:
#ifdef EASYLIBSSH_DEBUG
Serial.print("Pubkey ok and signature validated");
Serial.println(i);
#endif
i=EASYLIBSSH_NBAUTHKEY;
again=0;
ssh_message_auth_reply_success(message,0);
}
}
}
}
else {
ssh_message_auth_set_methods(message, SSH_AUTH_METHOD_PUBLICKEY);
#ifdef EASYLIBSSH_DEBUG
Serial.print("Rejected message type: ");
Serial.println(ssh_message_type(message));
Serial.print("Message subtype: ");
Serial.println(ssh_message_subtype(message));
#endif
ssh_message_reply_default(message);
}
ssh_message_free(message);
}
#ifdef EASYLIBSSH_DEBUG
else Serial.println("NULL message");
#endif
patience++;
} while(again && patience < impatience);
if (patience == impatience) {
#ifdef EASYLIBSSH_DEBUG
Serial.println("Too much failure");
#endif
goto EASYLIBSSH_failed_session;
}
patience=0;
do { // Ssh chanel
#ifdef EASYLIBSSH_DEBUG
Serial.print("Ssh Channel - New message - ");
Serial.println(patience);
#endif
message = ssh_message_get(EASYLIBSSH_session);
if(message) {
if(ssh_message_type(message) == SSH_REQUEST_CHANNEL_OPEN && ssh_message_subtype(message) == SSH_CHANNEL_SESSION) {
#ifdef EASYLIBSSH_DEBUG
Serial.println("Accepted");
#endif
chan = ssh_message_channel_request_open_reply_accept(message);
}
else {
#ifdef EASYLIBSSH_DEBUG
Serial.print("Rejected message type: ");
Serial.println(ssh_message_type(message));
Serial.print("Message subtype: ");
Serial.println(ssh_message_subtype(message));
#endif
ssh_message_reply_default(message);
}
ssh_message_free(message);
}
#ifdef EASYLIBSSH_DEBUG
else Serial.println("NULL message");
#endif
patience++;
} while(!chan && patience < impatience);
if (patience == impatience) {
#ifdef EASYLIBSSH_DEBUG
Serial.println("Too much failure");
#endif
goto EASYLIBSSH_failed_session;
}
again = 1;
patience = 0;
do { // Ssh Pty
#ifdef EASYLIBSSH_DEBUG
Serial.print("Ssh Pty - New message - ");
Serial.println(patience);
#endif
message = ssh_message_get(EASYLIBSSH_session);
if (message) {
if (ssh_message_type(message) == SSH_REQUEST_CHANNEL && ssh_message_subtype(message) == SSH_CHANNEL_REQUEST_PTY) {
again = 0;
ssh_message_channel_request_reply_success(message);
}
else {
#ifdef EASYLIBSSH_DEBUG
Serial.print("Rejected message type: ");
Serial.println(ssh_message_type(message));
Serial.print("Message subtype: ");
Serial.println(ssh_message_subtype(message));
#endif
ssh_message_reply_default(message);
}
ssh_message_free(message);
}
#ifdef EASYLIBSSH_DEBUG
else Serial.println("NULL message");
#endif
patience++;
} while(again && patience < impatience);
if (patience == impatience) {
#ifdef EASYLIBSSH_DEBUG
Serial.println("Too much failure");
#endif
goto EASYLIBSSH_failed_channel;
}
again = 1;
patience = 0;
do { // Ssh shell
#ifdef EASYLIBSSH_DEBUG
Serial.print("Ssh Shell - New message - ");
Serial.println(patience);
#endif
message = ssh_message_get(EASYLIBSSH_session);
if(message) {
if(ssh_message_type(message) == SSH_REQUEST_CHANNEL && ssh_message_subtype(message) == SSH_CHANNEL_REQUEST_SHELL) {
again = 0;
ssh_message_channel_request_reply_success(message);
}
else {
#ifdef EASYLIBSSH_DEBUG
Serial.print("Rejected message type: ");
Serial.println(ssh_message_type(message));
Serial.print("Message subtype: ");
Serial.println(ssh_message_subtype(message));
#endif
ssh_message_reply_default(message);
}
ssh_message_free(message);
}
#ifdef EASYLIBSSH_DEBUG
else Serial.println("NULL message");
#endif
patience++;
} while(again && patience < impatience);
if (patience == impatience) {
#ifdef EASYLIBSSH_DEBUG
Serial.println("Too much failure");
#endif
goto EASYLIBSSH_failed_channel;
}
ssh_set_blocking(EASYLIBSSH_session, 0);
#ifdef EASYLIBSSH_DEBUG
Serial.println("Start of pseudo Shell");
#endif
return chan;
EASYLIBSSH_failed_channel:
#ifdef EASYLIBSSH_DEBUG
Serial.println("free channel");
#endif
ssh_channel_free(chan);
EASYLIBSSH_failed_session:
#ifdef EASYLIBSSH_DEBUG
if (ssh_get_error_code(EASYLIBSSH_session) != SSH_NO_ERROR) {
Serial.print("Error session: ");
Serial.println(ssh_get_error(EASYLIBSSH_session));
}
Serial.println("Disconnect session");
#endif
ssh_disconnect(EASYLIBSSH_session);
EASYLIBSSH_failed_bind:
#ifdef EASYLIBSSH_DEBUG
if (ssh_get_error_code(EASYLIBSSH_sshbind) != SSH_NO_ERROR) {
Serial.print("Error ssh bind: ");
Serial.println(ssh_get_error(EASYLIBSSH_sshbind));
}
Serial.println("Free sshbind");
#endif
ssh_bind_free(EASYLIBSSH_sshbind);
EASYLIBSSH_failed_bind_new:
return NULL;
}
void easylibssh_loop_end(ssh_channel chan)
{
#ifdef EASYLIBSSH_DEBUG
Serial.println("End of pseudo Shell");
Serial.println("Free channel");
#endif
ssh_channel_free(chan);
#ifdef EASYLIBSSH_DEBUG
Serial.println("Disconnect session");
#endif
ssh_disconnect(EASYLIBSSH_session);
#ifdef EASYLIBSSH_DEBUG
Serial.println("Free sshbind");
#endif
ssh_bind_free(EASYLIBSSH_sshbind);
}