-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathsession_client_ssh.c
More file actions
2068 lines (1777 loc) · 59.8 KB
/
session_client_ssh.c
File metadata and controls
2068 lines (1777 loc) · 59.8 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
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file session_client_ssh.c
* @author Radek Krejci <rkrejci@cesnet.cz>
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief libnetconf2 - SSH specific client session transport functions
*
* This source is compiled only with libssh.
*
* @copyright
* Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <pthread.h>
#include <pwd.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#ifdef ENABLE_DNSSEC
# include <validator/resolver.h>
# include <validator/validator.h>
# include <validator/validator-config.h>
# include <validator/validator-compat.h>
#endif
#include <libssh/libssh.h>
#include <libyang/libyang.h>
#include "compat.h"
#include "config.h"
#include "log_p.h"
#include "session_client.h"
#include "session_client_ch.h"
#include "session_p.h"
/* must be after config.h */
#ifdef HAVE_TERMIOS
# include <termios.h>
#endif
#ifdef HAVE_TERMIOS
/**
* @brief Open a terminal FILE with no echo.
*
* @param[in] path Filesystem terminal path.
* @param[in] oldterm Old terminal options.
* @return Opened terminal;
* @return NULL on error.
*/
static FILE *
nc_open_tty_noecho(const char *path, struct termios *oldterm)
{
struct termios newterm;
FILE *ret;
if (!(ret = fopen(path, "r"))) {
ERR(NULL, "Unable to open terminal \"%s\" for reading (%s).", path, strerror(errno));
return NULL;
}
if (tcgetattr(fileno(ret), oldterm)) {
ERR(NULL, "Unable to get terminal \"%s\" settings (%s).", path, strerror(errno));
fclose(ret);
return NULL;
}
newterm = *oldterm;
/* turn off echo */
newterm.c_lflag &= ~ECHO;
/* get rid of any leftover characters */
tcflush(fileno(ret), TCIFLUSH);
if (tcsetattr(fileno(ret), TCSANOW, &newterm)) {
ERR(NULL, "Unable to change terminal \"%s\" settings for hiding password (%s).", path, strerror(errno));
fclose(ret);
return NULL;
}
return ret;
}
/**
* @brief Open an input terminal FILE.
*
* @param[in] echo Whether to turn echo on or off.
* @param[in] oldterm Old terminal options.
* @return Opened terminal;
* @return NULL on error.
*/
static FILE *
nc_open_in(int echo, struct termios *oldterm)
{
char buf[512];
int buflen = 512, ret;
FILE *in;
if (!echo) {
in = nc_open_tty_noecho("/dev/tty", oldterm);
} else {
in = fopen("/dev/tty", "r");
if (!in) {
ERR(NULL, "Unable to open terminal \"/dev/tty\" for reading (%s).", strerror(errno));
}
}
if (!in) {
if ((ret = ttyname_r(STDIN_FILENO, buf, buflen))) {
ERR(NULL, "ttyname_r failed (%s).", strerror(ret));
return NULL;
}
if (!echo) {
in = nc_open_tty_noecho(buf, oldterm);
} else {
in = fopen(buf, "r");
if (!in) {
ERR(NULL, "Unable to open terminal \"%s\" for reading (%s).", buf, strerror(errno));
}
}
}
return in;
}
/**
* @brief Open an output terminal FILE.
*
* @return Opened terminal;
* @return NULL on error.
*/
static FILE *
nc_open_out(void)
{
char buf[512];
int buflen = 512, ret;
FILE *out;
out = fopen("/dev/tty", "w");
if (!out) {
ERR(NULL, "Unable to open terminal \"/dev/tty\" for writing (%s).", strerror(errno));
if ((ret = ttyname_r(STDOUT_FILENO, buf, buflen))) {
ERR(NULL, "ttyname_r failed (%s).", strerror(ret));
return NULL;
}
out = fopen(buf, "w");
if (!out) {
ERR(NULL, "Unable to open terminal \"%s\" for writing (%s).", buf, strerror(errno));
}
}
return out;
}
/**
* @brief Close an input/output terminal FILE.
*
* @param[in] inout Terminal FILE to close.
* @param[in] echo Whether echo was turned on or off.
* @param[in] oldterm Old terminal options.
* @return Opened terminal;
* @return NULL on error.
*/
static void
nc_close_inout(FILE *inout, int echo, struct termios *oldterm)
{
if (inout) {
if (!echo && (tcsetattr(fileno(inout), TCSANOW, oldterm) != 0)) {
ERR(NULL, "Unable to restore terminal settings (%s).", strerror(errno));
}
fclose(inout);
}
}
#endif
void
_nc_client_ssh_destroy_opts(struct nc_client_ssh_opts *opts)
{
int i;
for (i = 0; i < opts->key_count; ++i) {
free(opts->keys[i].pubkey_path);
free(opts->keys[i].privkey_path);
}
free(opts->keys);
free(opts->username);
free(opts->knownhosts_path);
opts->key_count = 0;
opts->keys = NULL;
opts->username = NULL;
opts->knownhosts_path = NULL;
}
void
nc_client_ssh_destroy_opts(void)
{
_nc_client_ssh_destroy_opts(&client_ssh_opts);
_nc_client_ssh_destroy_opts(&client_ssh_ch_opts);
}
#ifdef ENABLE_DNSSEC
/* return 0 (DNSSEC + key valid), 1 (unsecure DNS + key valid), 2 (key not found or an error) */
/* type - 1 (RSA), 2 (DSA), 3 (ECDSA); alg - 1 (SHA1), 2 (SHA-256) */
static int
sshauth_hostkey_hash_dnssec_check(const char *hostname, const unsigned char *sha1hash, int type, int alg)
{
ns_msg handle;
ns_rr rr;
val_status_t val_status;
const unsigned char *rdata;
unsigned char buf[4096];
int buf_len = 4096;
int ret = 0, i, j, len;
/* class 1 - internet, type 44 - SSHFP */
len = val_res_query(NULL, hostname, 1, 44, buf, buf_len, &val_status);
if ((len < 0) || !val_istrusted(val_status)) {
ret = 2;
goto finish;
}
if (ns_initparse(buf, len, &handle) < 0) {
ERR(NULL, "Failed to initialize DNSSEC response parser.");
ret = 2;
goto finish;
}
if ((i = libsres_msg_getflag(handle, ns_f_rcode))) {
ERR(NULL, "DNSSEC query returned %d.", i);
ret = 2;
goto finish;
}
if (!libsres_msg_getflag(handle, ns_f_ad)) {
/* response not secured by DNSSEC */
ret = 1;
}
/* query section */
if (ns_parserr(&handle, ns_s_qd, 0, &rr)) {
ERR(NULL, "DNSSEC query section parser fail.");
ret = 2;
goto finish;
}
if (strcmp(hostname, ns_rr_name(rr)) || (ns_rr_type(rr) != 44) || (ns_rr_class(rr) != 1)) {
ERR(NULL, "DNSSEC query in the answer does not match the original query.");
ret = 2;
goto finish;
}
/* answer section */
i = 0;
while (!ns_parserr(&handle, ns_s_an, i, &rr)) {
if (ns_rr_type(rr) != 44) {
++i;
continue;
}
rdata = ns_rr_rdata(rr);
if (rdata[0] != type) {
++i;
continue;
}
if (rdata[1] != alg) {
++i;
continue;
}
/* we found the correct SSHFP entry */
rdata += 2;
for (j = 0; j < 20; ++j) {
if (rdata[j] != (unsigned char)sha1hash[j]) {
ret = 2;
goto finish;
}
}
/* server fingerprint is supported by a DNS entry,
* we have already determined if DNSSEC was used or not
*/
goto finish;
}
/* no match */
ret = 2;
finish:
val_free_validator_state();
return ret;
}
#endif /* ENABLE_DNSSEC */
static int
nc_client_ssh_update_known_hosts(ssh_session session, const char *hostname)
{
int ret;
ret = ssh_session_update_known_hosts(session);
if (ret != SSH_OK) {
WRN(NULL, "Adding the known host \"%s\" failed (%s).", hostname, ssh_get_error(session));
}
return ret;
}
static int
nc_client_ssh_get_srv_pubkey_data(ssh_session session, enum ssh_keytypes_e *srv_pubkey_type, char **hexa, unsigned char **hash_sha1)
{
int ret;
ssh_key srv_pubkey;
size_t hlen;
*hexa = NULL;
*hash_sha1 = NULL;
ret = ssh_get_server_publickey(session, &srv_pubkey);
if (ret < 0) {
ERR(NULL, "Unable to get server's public key.");
return -1;
}
*srv_pubkey_type = ssh_key_type(srv_pubkey);
ret = ssh_get_publickey_hash(srv_pubkey, SSH_PUBLICKEY_HASH_SHA1, hash_sha1, &hlen);
ssh_key_free(srv_pubkey);
if (ret < 0) {
ERR(NULL, "Failed to calculate SHA1 hash of the server public key.");
return -1;
}
*hexa = ssh_get_hexa(*hash_sha1, hlen);
if (!*hexa) {
ERR(NULL, "Getting the hostkey's hex string failed.");
return -1;
}
return 0;
}
#ifdef ENABLE_DNSSEC
static int
nc_client_ssh_do_dnssec_sshfp_check(ssh_session session, enum ssh_keytypes_e srv_pubkey_type, const char *hostname, unsigned char *hash_sha1)
{
int ret = 0;
if ((srv_pubkey_type != SSH_KEYTYPE_UNKNOWN) && (srv_pubkey_type != SSH_KEYTYPE_RSA1)) {
if (srv_pubkey_type == SSH_KEYTYPE_DSS) {
ret = sshauth_hostkey_hash_dnssec_check(hostname, hash_sha1, 2, 1);
} else if (srv_pubkey_type == SSH_KEYTYPE_RSA) {
ret = sshauth_hostkey_hash_dnssec_check(hostname, hash_sha1, 1, 1);
} else if (srv_pubkey_type == SSH_KEYTYPE_ECDSA) {
ret = sshauth_hostkey_hash_dnssec_check(hostname, hash_sha1, 3, 1);
} else {
/* other key types not supported */
ret = 1;
}
/* DNSSEC SSHFP check successful, that's enough */
if (!ret) {
VRB(NULL, "DNSSEC SSHFP check successful.");
ssh_session_update_known_hosts(session);
}
return ret;
}
return 1;
}
#endif
/**
* @brief Convert knownhosts mode to string.
*
* @param[in] knownhosts_mode Knownhosts mode.
* @return Knownhosts mode string.
*/
static const char *
nc_client_ssh_knownhosts_mode2str(NC_SSH_KNOWNHOSTS_MODE knownhosts_mode)
{
const char *mode_str;
switch (knownhosts_mode) {
case NC_SSH_KNOWNHOSTS_ASK:
mode_str = "ask";
break;
case NC_SSH_KNOWNHOSTS_STRICT:
mode_str = "strict";
break;
case NC_SSH_KNOWNHOSTS_ACCEPT_NEW:
mode_str = "accept-new";
break;
case NC_SSH_KNOWNHOSTS_ACCEPT:
mode_str = "accept";
break;
case NC_SSH_KNOWNHOSTS_SKIP:
mode_str = "skip";
break;
default:
mode_str = "unknown";
break;
}
return mode_str;
}
/**
* @brief Perform the hostkey check.
*
* @param[in] hostname Expected hostname.
* @param[in] port Expected port.
* @param[in] knownhosts_mode Knownhosts mode.
* @param[in] session libssh session.
* @return 0 on success, -1 on error.
*/
static int
nc_client_ssh_auth_hostkey_check(const char *hostname, uint16_t port,
NC_SSH_KNOWNHOSTS_MODE knownhosts_mode, ssh_session session)
{
char *hexa = NULL;
unsigned char *hash_sha1 = NULL;
enum ssh_keytypes_e srv_pubkey_type;
int state;
#ifdef HAVE_TERMIOS
int c;
char answer[5];
FILE *out = NULL, *in = NULL;
#endif
#ifdef ENABLE_DNSSEC
int dnssec_ret;
#endif
VRB(NULL, "Server hostkey check mode: %s.", nc_client_ssh_knownhosts_mode2str(knownhosts_mode));
if (knownhosts_mode == NC_SSH_KNOWNHOSTS_SKIP) {
/* skip all hostkey checks */
return 0;
}
if (nc_client_ssh_get_srv_pubkey_data(session, &srv_pubkey_type, &hexa, &hash_sha1)) {
goto error;
}
state = ssh_session_is_known_server(session);
switch (state) {
case SSH_KNOWN_HOSTS_OK:
break; /* ok */
case SSH_KNOWN_HOSTS_CHANGED:
if (knownhosts_mode == NC_SSH_KNOWNHOSTS_ACCEPT) {
/* is the mode is set to accept, then accept any connection even if the remote key changed */
WRN(NULL, "Remote host key changed!");
break;
} else {
ERR(NULL, "Remote host key changed, the connection will be terminated!");
goto error;
}
case SSH_KNOWN_HOSTS_OTHER:
WRN(NULL, "Remote host key is not known, but a key of another type for this host is known. Continue with caution.");
goto hostkey_not_known;
case SSH_KNOWN_HOSTS_NOT_FOUND:
WRN(NULL, "Could not find the known hosts file.");
goto hostkey_not_known;
case SSH_KNOWN_HOSTS_UNKNOWN:
hostkey_not_known:
#ifdef ENABLE_DNSSEC
/* do dnssec check, if it's ok then we're done otherwise continue */
dnssec_ret = nc_client_ssh_do_dnssec_sshfp_check(session, srv_pubkey_type, hostname, hash_sha1);
if (!dnssec_ret) {
ssh_clean_pubkey_hash(&hash_sha1);
ssh_string_free_char(hexa);
return 0;
}
#endif
if (knownhosts_mode == NC_SSH_KNOWNHOSTS_STRICT) {
/* do not connect if the hostkey is not present in known_hosts file in this mode */
ERR(NULL, "No %s host key is known for [%s]:%hu.\n", ssh_key_type_to_char(srv_pubkey_type), hostname, port);
goto error;
} else if ((knownhosts_mode == NC_SSH_KNOWNHOSTS_ACCEPT_NEW) || (knownhosts_mode == NC_SSH_KNOWNHOSTS_ACCEPT)) {
/* add a new entry to the known_hosts file without prompting */
if (nc_client_ssh_update_known_hosts(session, hostname)) {
goto error;
}
VRB(NULL, "Permanently added '[%s]:%hu' (%s) to the list of known hosts.", hostname, port, ssh_key_type_to_char(srv_pubkey_type));
break;
}
#ifdef HAVE_TERMIOS
/* open the files for reading/writing */
if (!(in = nc_open_in(1, NULL))) {
goto error;
}
if (!(out = nc_open_out())) {
goto error;
}
/* try to get result from user */
if (fprintf(out, "The authenticity of the host \'%s\' cannot be established.\n", hostname) < 1) {
ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
goto error;
}
if (fprintf(out, "%s key fingerprint is %s.\n", ssh_key_type_to_char(srv_pubkey_type), hexa) < 1) {
ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
goto error;
}
# ifdef ENABLE_DNSSEC
if (dnssec_ret == 2) {
if (fprintf(out, "No matching host key fingerprint found using DNS.\n") < 1) {
ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
goto error;
}
} else if (dnssec_ret == 1) {
if (fprintf(out, "Matching host key fingerprint found using DNS.\n") < 1) {
ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
goto error;
}
}
# endif
if (fprintf(out, "Are you sure you want to continue connecting (yes/no)? ") < 1) {
ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
goto error;
}
fflush(out);
do {
if (fscanf(in, "%4s", answer) == EOF) {
ERR(NULL, "Reading from input failed (%s).", feof(in) ? "EOF" : strerror(errno));
goto error;
}
while (((c = getc(in)) != EOF) && (c != '\n')) {}
fflush(in);
if (!strcmp("yes", answer)) {
/* store the key into the known_hosts file */
nc_client_ssh_update_known_hosts(session, hostname);
} else if (!strcmp("no", answer)) {
goto error;
} else {
if (fprintf(out, "Please type 'yes' or 'no': ") < 1) {
ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
goto error;
}
fflush(out);
}
} while (strcmp(answer, "yes") && strcmp(answer, "no"));
#else
ERR(NULL, "Unable to get input from user, terminate the connection.");
goto error;
#endif
break;
case SSH_KNOWN_HOSTS_ERROR:
ERR(NULL, "SSH error: %s", ssh_get_error(session));
goto error;
}
#ifdef HAVE_TERMIOS
nc_close_inout(in, 1, NULL);
nc_close_inout(out, 1, NULL);
#endif
ssh_clean_pubkey_hash(&hash_sha1);
ssh_string_free_char(hexa);
return 0;
error:
#ifdef HAVE_TERMIOS
nc_close_inout(in, 1, NULL);
nc_close_inout(out, 1, NULL);
#endif
ssh_clean_pubkey_hash(&hash_sha1);
ssh_string_free_char(hexa);
return -1;
}
char *
sshauth_password(const char *username, const char *hostname, void *UNUSED(priv))
{
#ifdef HAVE_TERMIOS
char *buf = NULL;
int c, buflen = 1024, len;
struct termios oldterm;
FILE *in = NULL, *out = NULL;
buf = malloc(buflen * sizeof *buf);
NC_CHECK_ERRMEM_RET(!buf, NULL);
if (!(in = nc_open_in(0, &oldterm))) {
goto error;
}
if (!(out = nc_open_out())) {
goto error;
}
if (fprintf(out, "%s@%s password: ", username, hostname) < 1) {
ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
goto error;
}
fflush(out);
len = 0;
while (((c = fgetc(in)) != EOF) && (c != '\n')) {
if (len >= buflen - 1) {
buflen *= 2;
buf = nc_realloc(buf, buflen * sizeof *buf);
NC_CHECK_ERRMEM_GOTO(!buf, , error);
}
buf[len++] = (char)c;
}
buf[len++] = 0; /* terminating null byte */
fprintf(out, "\n");
nc_close_inout(in, 0, &oldterm);
nc_close_inout(out, 1, NULL);
return buf;
error:
nc_close_inout(in, 0, &oldterm);
nc_close_inout(out, 1, NULL);
free(buf);
return NULL;
#else
(void)username;
(void)hostname;
ERR(NULL, "Unable to get input from user, authentication failed.");
return NULL;
#endif
}
char *
sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *UNUSED(priv))
{
#ifdef HAVE_TERMIOS
uint32_t buflen = 64, cur_len;
int c;
struct termios oldterm;
char *buf = NULL;
FILE *in = NULL, *out = NULL;
buf = malloc(buflen * sizeof *buf);
NC_CHECK_ERRMEM_RET(!buf, NULL);
if (!(in = nc_open_in(echo, &oldterm))) {
goto error;
}
if (!(out = nc_open_out())) {
goto error;
}
if (auth_name && (fprintf(out, "%s\n", auth_name) < 1)) {
ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
goto error;
}
if (instruction && (fprintf(out, "%s\n", instruction) < 1)) {
ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
goto error;
}
if (fputs(prompt, out) == EOF) {
ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
goto error;
}
fflush(out);
cur_len = 0;
while (((c = fgetc(in)) != EOF) && (c != '\n')) {
if (cur_len >= buflen - 1) {
buflen *= 2;
buf = nc_realloc(buf, buflen * sizeof *buf);
NC_CHECK_ERRMEM_GOTO(!buf, , error);
}
buf[cur_len++] = (char)c;
}
/* terminating null byte */
buf[cur_len] = '\0';
fprintf(out, "\n");
nc_close_inout(in, echo, &oldterm);
nc_close_inout(out, 1, NULL);
return buf;
error:
nc_close_inout(in, echo, &oldterm);
nc_close_inout(out, 1, NULL);
free(buf);
return NULL;
#else
(void)auth_name;
(void)instruction;
(void)prompt;
(void)echo;
ERR(NULL, "Unable to get input from user, authentication failed.");
return NULL;
#endif
}
char *
sshauth_privkey_passphrase(const char *privkey_path, void *UNUSED(priv))
{
#ifdef HAVE_TERMIOS
char *buf = NULL;
int c, buflen = 1024, len;
struct termios oldterm;
FILE *in = NULL, *out = NULL;
buf = malloc(buflen * sizeof *buf);
NC_CHECK_ERRMEM_RET(!buf, NULL);
if (!(in = nc_open_in(0, &oldterm))) {
goto error;
}
if (!(out = nc_open_out())) {
goto error;
}
if (fprintf(out, "Enter passphrase for the key '%s': ", privkey_path) < 1) {
ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
goto error;
}
fflush(out);
len = 0;
while (((c = fgetc(in)) != EOF) && (c != '\n')) {
if (len >= buflen - 1) {
buflen *= 2;
buf = nc_realloc(buf, buflen * sizeof *buf);
NC_CHECK_ERRMEM_GOTO(!buf, , error);
}
buf[len++] = (char)c;
}
buf[len] = 0; /* terminating null byte */
fprintf(out, "\n");
nc_close_inout(in, 0, &oldterm);
nc_close_inout(out, 1, NULL);
return buf;
error:
nc_close_inout(in, 0, &oldterm);
nc_close_inout(out, 1, NULL);
free(buf);
return NULL;
#else
(void)privkey_path;
ERR(NULL, "Unable to get input from user, encrypted private key unusable.");
return NULL;
#endif
}
static int
_nc_client_ssh_set_knownhosts_path(const char *path, struct nc_client_ssh_opts *opts)
{
free(opts->knownhosts_path);
if (!path) {
opts->knownhosts_path = NULL;
return 0;
}
opts->knownhosts_path = strdup(path);
NC_CHECK_ERRMEM_RET(!opts->knownhosts_path, 1);
return 0;
}
API int
nc_client_ssh_set_knownhosts_path(const char *path)
{
return _nc_client_ssh_set_knownhosts_path(path, &client_ssh_opts);
}
API int
nc_client_ssh_ch_set_knownhosts_path(const char *path)
{
return _nc_client_ssh_set_knownhosts_path(path, &client_ssh_ch_opts);
}
API void
nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_MODE mode)
{
client_ssh_opts.knownhosts_mode = mode;
}
API void
nc_client_ssh_ch_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_MODE mode)
{
client_ssh_ch_opts.knownhosts_mode = mode;
}
static void
_nc_client_ssh_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname, void *priv),
void *priv, struct nc_client_ssh_opts *opts)
{
if (auth_password) {
opts->auth_password = auth_password;
opts->auth_password_priv = priv;
} else {
opts->auth_password = sshauth_password;
opts->auth_password_priv = NULL;
}
}
static void
_nc_client_ssh_get_auth_password_clb(char *(**auth_password)(const char *username, const char *hostname, void *priv),
void **priv, struct nc_client_ssh_opts *opts)
{
if (auth_password) {
(*auth_password) = opts->auth_password == sshauth_password ? NULL : opts->auth_password;
}
if (priv) {
(*priv) = opts->auth_password_priv;
}
}
API void
nc_client_ssh_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname, void *priv),
void *priv)
{
_nc_client_ssh_set_auth_password_clb(auth_password, priv, &client_ssh_opts);
}
API void
nc_client_ssh_ch_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname, void *priv),
void *priv)
{
_nc_client_ssh_set_auth_password_clb(auth_password, priv, &client_ssh_ch_opts);
}
API void
nc_client_ssh_get_auth_password_clb(char *(**auth_password)(const char *username, const char *hostname, void *priv),
void **priv)
{
_nc_client_ssh_get_auth_password_clb(auth_password, priv, &client_ssh_opts);
}
API void
nc_client_ssh_ch_get_auth_password_clb(char *(**auth_password)(const char *username, const char *hostname, void *priv),
void **priv)
{
_nc_client_ssh_get_auth_password_clb(auth_password, priv, &client_ssh_ch_opts);
}
static void
_nc_client_ssh_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction,
const char *prompt, int echo, void *priv),
void *priv, struct nc_client_ssh_opts *opts)
{
if (auth_interactive) {
opts->auth_interactive = auth_interactive;
opts->auth_interactive_priv = priv;
} else {
opts->auth_interactive = sshauth_interactive;
opts->auth_interactive_priv = NULL;
}
}
static void
_nc_client_ssh_get_auth_interactive_clb(char *(**auth_interactive)(const char *auth_name, const char *instruction,
const char *prompt, int echo, void *priv),
void **priv, struct nc_client_ssh_opts *opts)
{
if (auth_interactive) {
(*auth_interactive) = opts->auth_interactive == sshauth_interactive ? NULL : opts->auth_interactive;
}
if (priv) {
(*priv) = opts->auth_interactive_priv;
}
}
API void
nc_client_ssh_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction,
const char *prompt, int echo, void *priv),
void *priv)
{
_nc_client_ssh_set_auth_interactive_clb(auth_interactive, priv, &client_ssh_opts);
}
API void
nc_client_ssh_ch_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction,
const char *prompt, int echo, void *priv),
void *priv)
{
_nc_client_ssh_set_auth_interactive_clb(auth_interactive, priv, &client_ssh_ch_opts);
}
API void
nc_client_ssh_get_auth_interactive_clb(char *(**auth_interactive)(const char *auth_name, const char *instruction,
const char *prompt, int echo, void *priv),
void **priv)
{
_nc_client_ssh_get_auth_interactive_clb(auth_interactive, priv, &client_ssh_opts);
}
API void
nc_client_ssh_ch_get_auth_interactive_clb(char *(**auth_interactive)(const char *auth_name, const char *instruction,
const char *prompt, int echo, void *priv),
void **priv)
{
_nc_client_ssh_get_auth_interactive_clb(auth_interactive, priv, &client_ssh_ch_opts);
}
static void
_nc_client_ssh_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path, void *priv),
void *priv, struct nc_client_ssh_opts *opts)
{
if (auth_privkey_passphrase) {
opts->auth_privkey_passphrase = auth_privkey_passphrase;
opts->auth_privkey_passphrase_priv = priv;
} else {
opts->auth_privkey_passphrase = sshauth_privkey_passphrase;
opts->auth_privkey_passphrase_priv = NULL;
}
}
static void
_nc_client_ssh_get_auth_privkey_passphrase_clb(char *(**auth_privkey_passphrase)(const char *privkey_path, void *priv),
void **priv, struct nc_client_ssh_opts *opts)
{
if (auth_privkey_passphrase) {
(*auth_privkey_passphrase) = opts->auth_privkey_passphrase == sshauth_privkey_passphrase ? NULL : opts->auth_privkey_passphrase;
}
if (priv) {
(*priv) = opts->auth_privkey_passphrase_priv;
}
}
API void
nc_client_ssh_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path, void *priv),
void *priv)
{
_nc_client_ssh_set_auth_privkey_passphrase_clb(auth_privkey_passphrase, priv, &client_ssh_opts);
}
API void
nc_client_ssh_ch_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path, void *priv),
void *priv)
{
_nc_client_ssh_set_auth_privkey_passphrase_clb(auth_privkey_passphrase, priv, &client_ssh_ch_opts);
}
API void
nc_client_ssh_get_auth_privkey_passphrase_clb(char *(**auth_privkey_passphrase)(const char *privkey_path, void *priv),
void **priv)
{
_nc_client_ssh_get_auth_privkey_passphrase_clb(auth_privkey_passphrase, priv, &client_ssh_opts);
}
API void
nc_client_ssh_ch_get_auth_privkey_passphrase_clb(char *(**auth_privkey_passphrase)(const char *privkey_path, void *priv),
void **priv)
{
_nc_client_ssh_get_auth_privkey_passphrase_clb(auth_privkey_passphrase, priv, &client_ssh_ch_opts);
}
static int
_nc_client_ssh_add_keypair(const char *pub_key, const char *priv_key, struct nc_client_ssh_opts *opts)
{
int i;
FILE *key;
char line[128];
NC_CHECK_ARG_RET(NULL, pub_key, priv_key, -1);