-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssh_api.c
441 lines (376 loc) · 10.8 KB
/
ssh_api.c
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/***************************************************************************
* Copyright (C) 2017 - 2020, Lanka Hsu, <lankahsu@gmail.com>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include "utilx9.h"
int sshX_open_shell(SSH_t *ssh_req)
{
int rc;
rc = ssh_channel_open_session(ssh_req->channel);
if (rc != SSH_OK)
{
DBG_ER_LN("ssh_channel_open_session error !!! (rc: %d, %d %s)", rc, ssh_get_error_code(ssh_req->session), ssh_get_error(ssh_req->session));
return rc;
}
return rc;
}
int sshX_request_pty(SSH_t *ssh_req)
{
int rc;
rc = ssh_channel_request_pty(ssh_req->channel);
if (rc != SSH_OK)
{
DBG_ER_LN("ssh_channel_request_pty error !!! (rc: %d, %d %s)", rc, ssh_get_error_code(ssh_req->session), ssh_get_error(ssh_req->session));
return rc;
}
if ((ssh_req->cols != 0) && (ssh_req->rows != 0))
{
rc = ssh_channel_change_pty_size(ssh_req->channel, ssh_req->cols, ssh_req->rows);
if (rc != SSH_OK)
{
DBG_ER_LN("ssh_channel_change_pty_size error !!! (rc: %d, %d %s)", rc, ssh_get_error_code(ssh_req->session), ssh_get_error(ssh_req->session));
return rc;
}
}
return rc;
}
int sshX_request_shell(SSH_t *ssh_req)
{
int rc;
rc = ssh_channel_request_shell(ssh_req->channel);
if (rc != SSH_OK)
{
DBG_ER_LN("ssh_channel_request_shell error !!! (rc: %d, %d %s)", rc, ssh_get_error_code(ssh_req->session), ssh_get_error(ssh_req->session));
return rc;
}
return rc;
}
void sshX_stream_copy(ssh_channel channel_frm, ssh_channel channel_to)
{
int nbytes = 0;
char buffer[LEN_OF_BUF1024];
memset(buffer, 0, sizeof(buffer));
while ((nbytes = ssh_channel_read_timeout(channel_frm, buffer, sizeof(buffer), 0, 100)) > 0)
{
nbytes = ssh_channel_write(channel_to, buffer, sizeof(buffer));
memset(buffer, 0, sizeof(buffer));
}
}
void sshX_select_loop_with_tunnel(SSH_t *ssh_req, SSH_t *ssh_req_frm)
{
ssh_session session = ssh_req->session;
ssh_channel channel = ssh_req->channel;
ssh_channel channel_frm = ssh_req_frm->channel;
ssh_connector connector_in, connector_out, connector_err;
int rc;
ssh_event event = ssh_event_new();
/* stdin */
connector_in = ssh_connector_new(session);
ssh_connector_set_out_channel(connector_in, channel, SSH_CONNECTOR_STDINOUT);
ssh_connector_set_in_channel(connector_in, channel_frm, SSH_CONNECTOR_STDINOUT);
ssh_event_add_connector(event, connector_in);
/* stdout */
connector_out = ssh_connector_new(session);
ssh_connector_set_out_channel(connector_out, channel_frm, SSH_CONNECTOR_STDINOUT);
ssh_connector_set_in_channel(connector_out, channel, SSH_CONNECTOR_STDINOUT);
ssh_event_add_connector(event, connector_out);
/* stderr */
connector_err = ssh_connector_new(session);
ssh_connector_set_out_channel(connector_err, channel_frm, SSH_CONNECTOR_STDINOUT);
ssh_connector_set_in_channel(connector_err, channel, SSH_CONNECTOR_STDERR);
ssh_event_add_connector(event, connector_err);
while ((ssh_channel_is_open(channel))
&& (ssh_channel_is_eof(channel) == 0)
&& (ssh_channel_is_open(channel_frm))
&& (ssh_channel_is_eof(channel_frm) == 0)
)
{
//if (signal_delayed) {
// sizechanged();
//}
rc = ssh_event_dopoll(event, 60000);
if (rc == SSH_ERROR)
{
DBG_ER_LN("ssh_event_dopoll error !!! (rc: %d, %d %s)", rc, ssh_get_error_code(session), ssh_get_error(session));
break;
}
}
if (connector_in)
{
ssh_event_remove_connector(event, connector_in);
ssh_connector_free(connector_in);
}
if (connector_out)
{
ssh_event_remove_connector(event, connector_out);
ssh_connector_free(connector_out);
}
if (connector_err)
{
ssh_event_remove_connector(event, connector_err);
ssh_connector_free(connector_err);
}
ssh_event_free(event);
}
void sshX_select_loop_with_fd(SSH_t *ssh_req, socket_t in_fd, socket_t out_fd, socket_t err_fd)
{
ssh_session session = ssh_req->session;
ssh_channel channel = ssh_req->channel;
ssh_connector connector_in, connector_out, connector_err;
int rc;
ssh_event event = ssh_event_new();
/* stdin */
connector_in = ssh_connector_new(session);
ssh_connector_set_out_channel(connector_in, channel, SSH_CONNECTOR_STDINOUT);
ssh_connector_set_in_fd(connector_in, in_fd);
ssh_event_add_connector(event, connector_in);
/* stdout */
connector_out = ssh_connector_new(session);
ssh_connector_set_out_fd(connector_out, out_fd);
ssh_connector_set_in_channel(connector_out, channel, SSH_CONNECTOR_STDINOUT);
ssh_event_add_connector(event, connector_out);
/* stderr */
connector_err = ssh_connector_new(session);
ssh_connector_set_out_fd(connector_err, err_fd);
ssh_connector_set_in_channel(connector_err, channel, SSH_CONNECTOR_STDERR);
ssh_event_add_connector(event, connector_err);
while (ssh_channel_is_open(channel))
{
//if (signal_delayed) {
// sizechanged();
//}
rc = ssh_event_dopoll(event, 60000);
if (rc == SSH_ERROR)
{
DBG_ER_LN("ssh_event_dopoll error !!! (rc: %d, %d %s)", rc, ssh_get_error_code(session), ssh_get_error(session));
break;
}
}
if (connector_in)
{
ssh_event_remove_connector(event, connector_in);
ssh_connector_free(connector_in);
}
if (connector_out)
{
ssh_event_remove_connector(event, connector_out);
ssh_connector_free(connector_out);
}
if (connector_err)
{
ssh_event_remove_connector(event, connector_err);
ssh_connector_free(connector_err);
}
ssh_event_free(event);
}
ssh_channel sshX_open_channel(SSH_t *ssh_req)
{
ssh_req->channel = ssh_channel_new(ssh_req->session);
return ssh_req->channel;
}
int sshX_interactive(SSH_t *ssh_req)
{
int ret = 0;
if (sshX_open_channel(ssh_req))
{
struct termios terminal;
struct termios terminal_local;
int interactive=isatty(0);
if (interactive)
{
tcgetattr(0, &terminal_local);
memcpy(&terminal, &terminal_local, sizeof(struct termios));
}
sshX_open_shell(ssh_req);
sshX_request_pty(ssh_req);
sshX_request_shell(ssh_req);
if (interactive)
{
cfmakeraw(&terminal_local);
tcsetattr(0, TCSANOW, &terminal_local);
//setsignal();
}
sshX_select_loop_with_fd(ssh_req, 0, 1, 2);
tcsetattr(0, TCSANOW, &terminal);
}
return ret;
}
int sshX_authenticate(SSH_t *ssh_req)
{
int rc = SSH_AUTH_SUCCESS;
DBG_TR_LN("call ssh_userauth_none ~~~");
rc = ssh_userauth_none(ssh_req->session, NULL);
if (rc == SSH_AUTH_ERROR)
{
return rc;
}
char *banner = ssh_get_issue_banner(ssh_req->session);
if (banner)
{
printf("%s\n",banner);
SSH_STRING_FREE_CHAR(banner);
}
int method = ssh_userauth_list(ssh_req->session, NULL);
DBG_DB_LN("(method: %d)", method);
while (rc != SSH_AUTH_SUCCESS)
{
if (method & SSH_AUTH_METHOD_GSSAPI_MIC)
{
rc = ssh_userauth_gssapi(ssh_req->session);
if (rc == SSH_AUTH_ERROR)
{
DBG_ER_LN("ssh_userauth_gssapi error !!! (rc: %d, %d %s)", rc, ssh_get_error_code(ssh_req->session), ssh_get_error(ssh_req->session));
return rc;
}
else if (rc == SSH_AUTH_SUCCESS)
{
break;
}
}
if (method & SSH_AUTH_METHOD_PUBLICKEY)
{
rc = ssh_userauth_publickey_auto(ssh_req->session, NULL, NULL);
if (rc == SSH_AUTH_ERROR)
{
DBG_ER_LN("ssh_userauth_publickey_auto error !!! (rc: %d, %d %s)", rc, ssh_get_error_code(ssh_req->session), ssh_get_error(ssh_req->session));
return rc;
}
else if (rc == SSH_AUTH_SUCCESS)
{
break;
}
}
if (method & SSH_AUTH_METHOD_PASSWORD)
{
char *password = NULL;
if (strlen(ssh_req->server_pass_dec) > 0)
{
SAFE_ASPRINTF(password, "%s", ssh_req->server_pass_dec);
}
else if (strlen(ssh_req->server_pass_enc) <= 0)
{
char prompt[LEN_OF_BUF256] = "";
SAFE_SNPRINTF(prompt, sizeof(prompt), "%s@%s's passowrd: ", ssh_req->server_user, ssh_req->server_ip);
password = SAFE_CALLOC(1, LEN_OF_PASS);
memset(password, 0, LEN_OF_PASS);
if (ssh_getpass(prompt, password, LEN_OF_PASS, 0, 0) < 0)
{
return SSH_AUTH_ERROR;
}
}
else
{
int server_pass_dec_len = 0;
password = sec_base64_dec(ssh_req->server_pass_enc, strlen(ssh_req->server_pass_enc), &server_pass_dec_len);
}
if (password)
{
//DBG_ER_LN("(password: %s)", password);
rc = ssh_userauth_password(ssh_req->session, NULL, password);
switch (rc)
{
case SSH_AUTH_SUCCESS:
break;
case SSH_AUTH_DENIED:
DBG_WN_LN("Permission denied, please try again !!!");
break;
default:
DBG_ER_LN("ssh_userauth_password error !!! (rc: %d, %d %s)", rc, ssh_get_error_code(ssh_req->session), ssh_get_error(ssh_req->session));
break;
}
SAFE_FREE(password);
}
}
}
return rc;
}
void sshX_close_channel(SSH_t *ssh_req)
{
if (ssh_req->channel)
{
ssh_channel_send_eof(ssh_req->channel);
ssh_channel_close(ssh_req->channel);
ssh_channel_free(ssh_req->channel);
ssh_req->channel = NULL;
}
}
void sshX_close_session(SSH_t *ssh_req)
{
if (ssh_req->session)
{
if (ssh_is_connected(ssh_req->session))
{
ssh_disconnect(ssh_req->session);
}
ssh_free(ssh_req->session);
ssh_req->session = NULL;
}
}
void sshX_stop(SSH_t *ssh_req)
{
sshX_close_channel(ssh_req);
sshX_close_session(ssh_req);
}
ssh_session sshX_client(SSH_t *ssh_req)
{
//int rc;
//ssh_init();
ssh_req->session = ssh_new();
if (ssh_req->session == NULL)
{
DBG_ER_LN("ssh_new error !!!");
goto exit_connect;
}
#if (0)
ssh_callbacks_init(&cb);
ssh_set_callbacks(ssh_req->session,&cb);
#endif
{
// connect
ssh_options_set(ssh_req->session, SSH_OPTIONS_LOG_VERBOSITY, &ssh_req->verbosity);
ssh_options_set(ssh_req->session, SSH_OPTIONS_HOST, ssh_req->server_ip);
if (strlen(ssh_req->server_user) > 0)
{
DBG_TR_LN("(ssh_req->server_user: %s)", ssh_req->server_user);
ssh_options_set(ssh_req->session, SSH_OPTIONS_USER, ssh_req->server_user);
}
if (ssh_req->server_port > 0)
{
ssh_options_set(ssh_req->session, SSH_OPTIONS_PORT, &ssh_req->server_port);
}
int process_config = 0;
ssh_options_set(ssh_req->session, SSH_OPTIONS_PROCESS_CONFIG, &process_config);
ssh_options_parse_config(ssh_req->session, NULL);
DBG_TR_LN("call ssh_connect ~~~");
if (ssh_connect(ssh_req->session))
{
DBG_ER_LN("ssh_new error !!! (%s)", ssh_get_error(ssh_req->session));
goto exit_connect;
}
char *banner = ssh_get_issue_banner(ssh_req->session);
if (banner)
{
DBG_DB_LN("(banner: %s)", banner);
SAFE_FREE(banner);
}
if (SSH_AUTH_SUCCESS != sshX_authenticate(ssh_req))
{
goto exit_connect;
}
}
return ssh_req->session ;
exit_connect:
sshX_close_session(ssh_req);
//ssh_finalize();
return ssh_req->session ;
}