-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssh.c
679 lines (571 loc) · 15 KB
/
ssh.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
#include "common.h"
#include "ssh.h"
#include "conf.h"
#include "input.h"
#include "serverinfo.h"
#include "main.h"
static int ssh_socket(struct server_info *server);
static int ssh_auth(struct ssh_session *session, struct server_info *server);
static const char *ssh_error(struct ssh_session *session);
static void ssh_waitsocket(struct ssh_session *session);
static int ssh_sftp(struct ssh_session *session);
static char *last_passphrase = NULL;
static struct dict *persistent_connections = NULL;
void ssh_init()
{
persistent_connections = dict_create();
}
void ssh_fini()
{
dict_iter(node, persistent_connections)
{
ssh_close_persistent(node->data);
}
dict_free(persistent_connections);
xfree(last_passphrase);
}
void ssh_set_passphrase(const char *passphrase)
{
xfree(last_passphrase);
last_passphrase = strdup(passphrase);
}
static int ssh_socket(struct server_info *server)
{
struct hostent *hp;
struct sockaddr_in sin;
int sock;
if((hp = gethostbyname2(server->ssh_host, AF_INET)) == NULL)
{
error("[%s] Could not resolve %s", server->name, server->ssh_host);
return -1;
}
sock = socket(AF_INET, SOCK_STREAM, 0);
assert(sock >= 0);
memset(&sin, 0, sizeof(struct sockaddr_in));
sin.sin_family = AF_INET;
sin.sin_port = htons(atoi(server->ssh_port));
memcpy(&sin.sin_addr, hp->h_addr, sizeof(struct in_addr));
if(connect(sock, (struct sockaddr*)&sin, sizeof(struct sockaddr_in)) < 0)
{
error("[%s] Could not connect to %s:%s (IPv4): %s (%d)", server->name, server->ssh_host, server->ssh_port, strerror(errno), errno);
close(sock);
return -2;
}
return sock;
}
static int ssh_auth(struct ssh_session *session, struct server_info *server)
{
const char *methods;
const char *pubkey, *privkey;
const char *password = NULL;
// Cancel SSH auth with SIGINT
if(sigsetjmp(sigint_jmp_buf, 1) != 0)
return 2;
sigint_received = 0;
sigint_jmp_on = 1;
assert((methods = libssh2_userauth_list(session->session, server->ssh_user, strlen(server->ssh_user))));
//debug("Supported authentication methods: %s", methods);
if(strstr(methods, "publickey") && (pubkey = conf_str("sshkey/pub")) && (privkey = conf_str("sshkey/priv")))
{
//debug("Trying publickey auth");
int ask_passphrase = conf_bool("sshkey/ask_passphrase");
for(int i = 0; i < 3; i++)
{
if(ask_passphrase && !last_passphrase)
password = readline_noecho("SSH key passphrase");
else if(last_passphrase)
password = last_passphrase;
else
password = conf_str("sshkey/passphrase");
if(libssh2_userauth_publickey_fromfile(session->session, server->ssh_user, pubkey, privkey, password ? password : "") == 0)
{
if(last_passphrase != password)
{
xfree(last_passphrase);
last_passphrase = xstrdup(password);
}
sigint_jmp_on = 0;
return 0;
}
out("Pubkey auth failed: %s", ssh_error(session));
xfree(last_passphrase);
last_passphrase = NULL;
ask_passphrase = 1;
}
}
// TODO: Maybe implement keyboard-interactive authentication
// Do not try password auth in batch mode. We cannot ask for a password and sending 3 empty passwords is stupid
if(!batch_mode && strstr(methods, "password"))
{
//debug("Trying password auth");
for(int i = 0; i < 3; i++)
{
password = readline_noecho("SSH password");
if(libssh2_userauth_password(session->session, server->ssh_user, password ? password : "") == 0)
{
sigint_jmp_on = 0;
return 0;
}
out("Password auth failed");
}
}
sigint_jmp_on = 0;
return 1;
}
static const char *ssh_error(struct ssh_session *session)
{
static char buf[256];
char *err;
libssh2_session_last_error(session->session, &err, NULL, 0);
strlcpy(buf, err, sizeof(buf));
return trim(buf);
}
static void ssh_waitsocket(struct ssh_session *session)
{
struct timeval timeout;
fd_set fd;
fd_set *writefd = NULL;
fd_set *readfd = NULL;
int dir;
timeout.tv_sec = 2;
timeout.tv_usec = 0;
FD_ZERO(&fd);
FD_SET(session->fd, &fd);
dir = libssh2_session_block_directions(session->session);
if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
readfd = &fd;
if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
writefd = &fd;
select(session->fd + 1, readfd, writefd, NULL, &timeout);
}
struct ssh_session *ssh_open(struct server_info *server)
{
int sock = 0;
struct ssh_session *session;
char *name;
asprintf(&name, "%s@%s:%s", server->ssh_user, server->ssh_host, server->ssh_port);
if((session = dict_find(persistent_connections, name)))
{
free(name);
session->refs++;
return session;
}
// Create socket
if((sock = ssh_socket(server)) < 0)
return NULL;
session = malloc(sizeof(struct ssh_session));
memset(session, 0, sizeof(struct ssh_session));
session->name = name;
session->fd = sock;
// Init ssh session
if(!(session->session = libssh2_session_init()))
{
error("Could not init ssh session");
free(session);
return NULL;
}
// Startup ssh session (handshake etc.)
if(libssh2_session_startup(session->session, sock) != 0)
{
error("Could not startup ssh session: %s", ssh_error(session));
libssh2_session_disconnect(session->session, "Session startup failed");
libssh2_session_free(session->session);
close(sock);
free(session);
return NULL;
}
// Authenticate
if(ssh_auth(session, server) != 0)
{
libssh2_session_disconnect(session->session, "Authentication failed");
libssh2_session_free(session->session);
close(sock);
free(session);
return NULL;
}
// Authenticated successfully
session->refs = 1;
return session;
}
void ssh_persist(struct ssh_session *session)
{
if(session->persistent)
{
debug("SSH session %s is already persistent", session->name);
return;
}
session->persistent = 1;
session->refs++;
debug("SSH session %s is now persistent", session->name);
dict_insert(persistent_connections, session->name, session);
}
void ssh_unpersist(struct ssh_session *session)
{
if(!session->persistent)
return;
session->persistent = 0;
session->refs--;
dict_delete(persistent_connections, session->name);
}
void ssh_close_persistent(struct ssh_session *session)
{
debug("Closing persistent ssh connection %s", session->name);
assert(session->persistent);
assert(session->refs == 1); // one ref is used by the persistent connection itself
dict_delete(persistent_connections, session->name);
session->persistent = 0;
ssh_close(session);
}
static int ssh_sftp(struct ssh_session *session)
{
// Already initialized?
if(session->sftp)
return 0;
// Initialize new sftp session
if(!(session->sftp = libssh2_sftp_init(session->session)))
{
error("Could not init sftp session: %s", ssh_error(session));
return -1;
}
return 0;
}
void ssh_close(struct ssh_session *session)
{
session->refs--;
if(session->persistent)
return;
assert(session->refs == 0);
if(session->sftp)
libssh2_sftp_shutdown(session->sftp);
libssh2_session_disconnect(session->session, "Finished");
libssh2_session_free(session->session);
close(session->fd);
free(session->name);
free(session);
}
int ssh_scp_get(struct ssh_session *session, const char *remote_file, const char *local_file)
{
LIBSSH2_CHANNEL *channel;
struct stat fileinfo;
long long received = 0;
FILE *outfile;
if(!(outfile = fopen(local_file, "w")))
{
error("Could not open `%s' for writing: %s (%d)", local_file, strerror(errno), errno);
return 1;
}
if(!(channel = libssh2_scp_recv(session->session, remote_file, &fileinfo)))
{
error("Could not create scp receive channel: %s", ssh_error(session));
fclose(outfile);
unlink(local_file);
return 2;
}
while(received < fileinfo.st_size)
{
char buf[10240];
int res;
int readlen = min(fileinfo.st_size - received, sizeof(buf));
res = libssh2_channel_read(channel, buf, readlen);
if(res > 0)
{
assert(res == readlen);
fwrite(buf, 1, res, outfile);
}
else
{
if(received)
putc('\n', stdout);
error("Could not read from ssh channel: %s", ssh_error(session));
fclose(outfile);
unlink(local_file);
libssh2_channel_free(channel);
return 2;
}
received += res;
}
debug("Downloaded %llu/%llu bytes", (long long)received, (long long)fileinfo.st_size);
libssh2_channel_free(channel);
fclose(outfile);
return 0;
}
int ssh_scp_put(struct ssh_session *session, const char *local_file, const char *remote_file, int mode)
{
LIBSSH2_CHANNEL *channel;
struct stat fileinfo;
long long sent = 0;
FILE *infile;
if(stat(local_file, &fileinfo) != 0)
{
error("Could not stat() `%s': %s (%d)", local_file, strerror(errno), errno);
return 1;
}
if(!(infile = fopen(local_file, "r")))
{
error("Could not open `%s' for reading: %s (%d)", local_file, strerror(errno), errno);
return 1;
}
if(!(channel = libssh2_scp_send(session->session, remote_file, mode, fileinfo.st_size)))
{
error("Could not create scp send channel: %s", ssh_error(session));
fclose(infile);
return 2;
}
libssh2_channel_set_blocking(channel, 1);
while(sent < fileinfo.st_size)
{
char buf[10240];
char *ptr;
int res, block;
int readlen = min(fileinfo.st_size - sent, sizeof(buf));
block = fread(buf, 1, readlen, infile);
if(block <= 0)
{
error("Could not read from file");
break;
}
assert(block == readlen);
ptr = buf;
while(block)
{
res = libssh2_channel_write(channel, ptr, block);
if(res >= 0)
{
ptr += res;
sent += res;
block -= res;
}
else
{
if(sent)
putc('\n', stdout);
error("Could not write to ssh channel: %s", ssh_error(session));
libssh2_channel_send_eof(channel);
libssh2_channel_wait_eof(channel);
libssh2_channel_wait_closed(channel);
libssh2_channel_free(channel);
fclose(infile);
return 2;
}
}
}
debug("Uploaded %llu/%llu bytes", (long long)sent, (long long)fileinfo.st_size);
libssh2_channel_send_eof(channel);
libssh2_channel_wait_eof(channel);
libssh2_channel_wait_closed(channel);
libssh2_channel_free(channel);
fclose(infile);
return 0;
}
int ssh_exec(struct ssh_session *session, const char *command, char **output)
{
LIBSSH2_CHANNEL *channel;
char *buf;
int len, size;
int res, exitcode;
if(!(channel = libssh2_channel_open_session(session->session)))
{
error("Could not create session channel: %s", ssh_error(session));
ssh_unpersist(session);
return -1;
}
if(libssh2_channel_exec(channel, command) != 0)
{
error("Unable to execute command: %s", ssh_error(session));
libssh2_channel_free(channel);
return -1;
}
libssh2_channel_set_blocking(channel, 0);
len = 0;
size = 256;
buf = malloc(size);
while(1)
{
if(size - len < 2)
{
size <<= 1; // double size
buf = realloc(buf, size);
}
res = libssh2_channel_read(channel, buf + len, size - len - 1);
if(res > 0)
{
// If output is not wanted, simply read into the same buffer and overwrite old contents
if(output)
len += res;
continue;
}
else if(res == 0)
break;
else if(res != LIBSSH2_ERROR_EAGAIN)
{
error("Could not read from ssh channel: %s %d", ssh_error(session), res);
free(buf);
libssh2_channel_free(channel);
libssh2_session_set_blocking(session->session, 1);
return -1;
}
ssh_waitsocket(session);
}
buf[len] = '\0';
if(!output)
free(buf);
exitcode = 127;
while((res = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN)
ssh_waitsocket(session);
if(res == 0)
exitcode = libssh2_channel_get_exit_status(channel);
if(output)
*output = buf;
libssh2_channel_free(channel);
libssh2_session_set_blocking(session->session, 1);
return exitcode;
}
struct ssh_exec *ssh_exec_async(struct ssh_session *session, const char *command)
{
LIBSSH2_CHANNEL *channel;
struct ssh_exec *exec;
if(!(channel = libssh2_channel_open_session(session->session)))
{
error("Could not create session channel: %s", ssh_error(session));
ssh_unpersist(session);
return NULL;
}
libssh2_channel_handle_extended_data2(channel, LIBSSH2_CHANNEL_EXTENDED_DATA_MERGE);
if(libssh2_channel_exec(channel, command) != 0)
{
error("Unable to execute command: %s", ssh_error(session));
libssh2_channel_free(channel);
return NULL;
}
libssh2_channel_set_blocking(channel, 0);
exec = malloc(sizeof(struct ssh_exec));
memset(exec, 0, sizeof(struct ssh_exec));
exec->session = session;
exec->channel = channel;
return exec;
}
int ssh_exec_read(struct ssh_exec *exec, const char **line)
{
assert(line);
*line = NULL;
if(exec->finished)
return 1;
if(!exec->buf)
{
exec->size = 256;
exec->len = 0;
exec->buf = malloc(exec->size);
}
if(exec->getlen)
{
size_t retlen;
// Get rid of fetched data
memmove(exec->buf, exec->buf + exec->getlen, exec->size - exec->getlen);
exec->len -= exec->getlen;
exec->buf[exec->len] = '\0';
exec->getlen = 0;
// Check if there's more data to fetch
if((retlen = strcspn(exec->buf, "\n")) > 0 && *(exec->buf + retlen) == '\n')
{
exec->getlen = retlen + 1;
exec->buf[retlen] = '\0';
*line = exec->buf;
return 0;
}
else if(*exec->buf == '\n') // empty line
{
exec->getlen = 1;
exec->buf[0] = '\0';
*line = exec->buf; // empty string
return 0;
}
}
while(1)
{
int res;
size_t retlen;
if(exec->size - exec->len < 2)
{
exec->size <<= 1; // double size
exec->buf = realloc(exec->buf, exec->size);
}
res = libssh2_channel_read(exec->channel, exec->buf + exec->len, exec->size - exec->len - 1);
if(res > 0)
{
exec->len += res;
exec->buf[exec->len] = '\0';
// Return everything until \n and remove the read string and its \n from the buffer
if(((retlen = strcspn(exec->buf, "\n")) > 0 && *(exec->buf + retlen) == '\n') || (*exec->buf == '\n'))
{
exec->getlen = retlen + 1;
exec->buf[retlen] = '\0';
*line = exec->buf;
return 0;
}
else if(*exec->buf == '\n') // empty line
{
exec->getlen = 1;
exec->buf[0] = '\0';
*line = exec->buf; // empty string
return 0;
}
continue;
}
else if(res == 0)
{
// EOF (Channel closed)
break;
}
else if(res != LIBSSH2_ERROR_EAGAIN)
{
error("Could not read from ssh channel: %s", ssh_error(exec->session));
return -1;
}
ssh_waitsocket(exec->session);
}
if(!exec->len)
return 1;
exec->finished = 1;
exec->buf[exec->len] = '\0';
*line = exec->buf;
return 0;
}
int ssh_exec_close(struct ssh_exec *exec)
{
int res, exitcode = 127;
while((res = libssh2_channel_close(exec->channel)) == LIBSSH2_ERROR_EAGAIN)
ssh_waitsocket(exec->session);
if(res == 0)
exitcode = libssh2_channel_get_exit_status(exec->channel);
libssh2_channel_free(exec->channel);
libssh2_session_set_blocking(exec->session->session, 1);
if(exec->buf)
free(exec->buf);
free(exec);
return exitcode;
}
int ssh_exec_live(struct ssh_session *session, const char *command)
{
struct ssh_exec *exec;
const char *buf;
int ret;
if(!(exec = ssh_exec_async(session, command)))
return -1;
while(ssh_exec_read(exec, &buf) == 0)
{
if(buf)
out("%s", buf);
}
ret = ssh_exec_close(exec);
if(ret == 127)
error("Could not execute `%s'", command);
else if(ret != 0)
out_color(COLOR_BROWN, "Command exited with code %d", ret);
return ret;
}
int ssh_file_exists(struct ssh_session *session, const char *file)
{
LIBSSH2_SFTP_ATTRIBUTES attrs;
assert(ssh_sftp(session) == 0);
return (libssh2_sftp_stat(session->sftp, file, &attrs) == 0);
}