blob: 5b175dbe38e6d12a6f0a1f3cb80e1ba2ff5df0d5 [file] [log] [blame]
Radek Krejciac6d3472015-10-22 15:47:18 +02001/**
Michal Vasko086311b2016-01-08 09:53:11 +01002 * \file session_client_ssh.c
Radek Krejciac6d3472015-10-22 15:47:18 +02003 * \author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko086311b2016-01-08 09:53:11 +01004 * \author Michal Vasko <mvasko@cesnet.cz>
5 * \brief libnetconf2 - SSH specific client session transport functions
Radek Krejciac6d3472015-10-22 15:47:18 +02006 *
7 * This source is compiled only with libssh.
8 *
9 * Copyright (c) 2015 CESNET, z.s.p.o.
10 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010011 * This source code is licensed under BSD 3-Clause License (the "License").
12 * You may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010014 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010015 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejciac6d3472015-10-22 15:47:18 +020016 */
17
Michal Vasko7b62fed2015-10-26 15:39:46 +010018#define _GNU_SOURCE
Radek Krejciac6d3472015-10-22 15:47:18 +020019#include <assert.h>
Michal Vasko7b62fed2015-10-26 15:39:46 +010020#include <stdlib.h>
21#include <stddef.h>
22#include <stdio.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020023#include <string.h>
Michal Vasko7b62fed2015-10-26 15:39:46 +010024#include <errno.h>
25#include <fcntl.h>
26#include <termios.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <pwd.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020030#include <unistd.h>
Michal Vasko36c7be82017-02-22 13:37:59 +010031#include <pthread.h>
32#include <time.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020033
Michal Vasko7b62fed2015-10-26 15:39:46 +010034#ifdef ENABLE_DNSSEC
35# include <validator/validator.h>
36# include <validator/resolver.h>
37# include <validator/validator-compat.h>
38#endif
39
Michal Vasko745ff832015-12-08 14:40:29 +010040#include <libssh/libssh.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020041#include <libyang/libyang.h>
42
Michal Vaskoe22c6732016-01-29 11:03:02 +010043#include "session_client.h"
44#include "session_client_ch.h"
Radek Krejciac6d3472015-10-22 15:47:18 +020045#include "libnetconf.h"
46
Michal Vaskoef112d72016-02-18 13:28:25 +010047static int sshauth_hostkey_check(const char *hostname, ssh_session session);
Michal Vasko30e2c872016-02-18 10:03:21 +010048static char *sshauth_password(const char *username, const char *hostname);
49static char *sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo);
50static char *sshauth_privkey_passphrase(const char* privkey_path);
51
Michal Vaskodaf9a092016-02-09 10:42:05 +010052extern struct nc_client_opts client_opts;
53
Michal Vasko3031aae2016-01-27 16:07:18 +010054static struct nc_client_ssh_opts ssh_opts = {
Michal Vasko30e2c872016-02-18 10:03:21 +010055 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 3}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 1}},
Michal Vaskoef112d72016-02-18 13:28:25 +010056 .auth_hostkey_check = sshauth_hostkey_check,
Michal Vasko30e2c872016-02-18 10:03:21 +010057 .auth_password = sshauth_password,
58 .auth_interactive = sshauth_interactive,
59 .auth_privkey_passphrase = sshauth_privkey_passphrase
Michal Vasko7b62fed2015-10-26 15:39:46 +010060};
Radek Krejciac6d3472015-10-22 15:47:18 +020061
Michal Vasko3031aae2016-01-27 16:07:18 +010062static struct nc_client_ssh_opts ssh_ch_opts = {
Michal Vasko30e2c872016-02-18 10:03:21 +010063 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}},
Michal Vaskoef112d72016-02-18 13:28:25 +010064 .auth_hostkey_check = sshauth_hostkey_check,
Michal Vasko30e2c872016-02-18 10:03:21 +010065 .auth_password = sshauth_password,
66 .auth_interactive = sshauth_interactive,
67 .auth_privkey_passphrase = sshauth_privkey_passphrase
Michal Vasko3031aae2016-01-27 16:07:18 +010068};
69
Michal Vaskoa43b8e32017-05-12 11:46:20 +020070static FILE *
71open_tty_noecho(const char *path, struct termios *oldterm)
72{
73 struct termios newterm;
74 FILE *ret;
75
76 if (!(ret = fopen(path, "r"))) {
77 ERR("Unable to open the current terminal (%s).", strerror(errno));
78 return NULL;
79 }
80
81 if (tcgetattr(fileno(ret), oldterm)) {
82 ERR("Unable to get terminal settings (%s).", strerror(errno));
83 fclose(ret);
84 return NULL;
85 }
86
87 newterm = *oldterm;
88 newterm.c_lflag &= ~ECHO;
89 newterm.c_lflag &= ~ICANON;
90 tcflush(fileno(ret), TCIFLUSH);
91 if (tcsetattr(fileno(ret), TCSANOW, &newterm)) {
92 ERR("Unable to change terminal settings for hiding password (%s).", strerror(errno));
93 fclose(ret);
94 return NULL;
95 }
96
97 return ret;
98}
99
100static void
101restore_tty_close(FILE *tty, struct termios *oldterm)
102{
103 if (tcsetattr(fileno(tty), TCSANOW, oldterm) != 0) {
104 ERR("Unable to restore terminal settings (%s).", strerror(errno));
105 }
106 fclose(tty);
107}
108
Michal Vaskoe22c6732016-01-29 11:03:02 +0100109static void
110_nc_client_ssh_destroy_opts(struct nc_client_ssh_opts *opts)
Michal Vasko990089e2015-10-27 15:05:25 +0100111{
112 int i;
113
Michal Vaskoe22c6732016-01-29 11:03:02 +0100114 for (i = 0; i < opts->key_count; ++i) {
115 free(opts->keys[i].pubkey_path);
116 free(opts->keys[i].privkey_path);
Michal Vasko990089e2015-10-27 15:05:25 +0100117 }
Michal Vaskoe22c6732016-01-29 11:03:02 +0100118 free(opts->keys);
119 free(opts->username);
120}
Michal Vasko990089e2015-10-27 15:05:25 +0100121
Michal Vaskob7558c52016-02-26 15:04:19 +0100122void
Michal Vaskoe22c6732016-01-29 11:03:02 +0100123nc_client_ssh_destroy_opts(void)
124{
125 _nc_client_ssh_destroy_opts(&ssh_opts);
126 _nc_client_ssh_destroy_opts(&ssh_ch_opts);
Michal Vasko990089e2015-10-27 15:05:25 +0100127}
128
Michal Vaskoef112d72016-02-18 13:28:25 +0100129#ifdef ENABLE_DNSSEC
130
131/* return 0 (DNSSEC + key valid), 1 (unsecure DNS + key valid), 2 (key not found or an error) */
132/* type - 1 (RSA), 2 (DSA), 3 (ECDSA); alg - 1 (SHA1), 2 (SHA-256) */
133static int
Michal Vasko650011a2016-02-25 14:49:29 +0100134sshauth_hostkey_hash_dnssec_check(const char *hostname, const unsigned char *sha1hash, int type, int alg) {
Michal Vaskoef112d72016-02-18 13:28:25 +0100135 ns_msg handle;
136 ns_rr rr;
137 val_status_t val_status;
138 const unsigned char* rdata;
139 unsigned char buf[4096];
140 int buf_len = 4096;
141 int ret = 0, i, j, len;
142
143 /* class 1 - internet, type 44 - SSHFP */
144 len = val_res_query(NULL, hostname, 1, 44, buf, buf_len, &val_status);
145
146 if ((len < 0) || !val_istrusted(val_status)) {
147 ret = 2;
148 goto finish;
149 }
150
151 if (ns_initparse(buf, len, &handle) < 0) {
152 ERR("Failed to initialize DNSSEC response parser.");
153 ret = 2;
154 goto finish;
155 }
156
157 if ((i = libsres_msg_getflag(handle, ns_f_rcode))) {
158 ERR("DNSSEC query returned %d.", i);
159 ret = 2;
160 goto finish;
161 }
162
163 if (!libsres_msg_getflag(handle, ns_f_ad)) {
164 /* response not secured by DNSSEC */
165 ret = 1;
166 }
167
168 /* query section */
169 if (ns_parserr(&handle, ns_s_qd, 0, &rr)) {
Michal Vasko650011a2016-02-25 14:49:29 +0100170 ERR("DNSSEC query section parser fail.");
Michal Vaskoef112d72016-02-18 13:28:25 +0100171 ret = 2;
172 goto finish;
173 }
174
175 if (strcmp(hostname, ns_rr_name(rr)) || (ns_rr_type(rr) != 44) || (ns_rr_class(rr) != 1)) {
Michal Vasko650011a2016-02-25 14:49:29 +0100176 ERR("DNSSEC query in the answer does not match the original query.");
Michal Vaskoef112d72016-02-18 13:28:25 +0100177 ret = 2;
178 goto finish;
179 }
180
181 /* answer section */
182 i = 0;
183 while (!ns_parserr(&handle, ns_s_an, i, &rr)) {
184 if (ns_rr_type(rr) != 44) {
185 ++i;
186 continue;
187 }
188
189 rdata = ns_rr_rdata(rr);
190 if (rdata[0] != type) {
191 ++i;
192 continue;
193 }
194 if (rdata[1] != alg) {
195 ++i;
196 continue;
197 }
198
199 /* we found the correct SSHFP entry */
200 rdata += 2;
201 for (j = 0; j < 20; ++j) {
202 if (rdata[j] != (unsigned char)sha1hash[j]) {
203 ret = 2;
204 goto finish;
205 }
206 }
207
208 /* server fingerprint is supported by a DNS entry,
209 * we have already determined if DNSSEC was used or not
210 */
211 goto finish;
212 }
213
214 /* no match */
215 ret = 2;
216
217finish:
218 val_free_validator_state();
219 return ret;
220}
221
222#endif /* ENABLE_DNSSEC */
223
224static int
225sshauth_hostkey_check(const char *hostname, ssh_session session)
226{
227 char *hexa;
228 int c, state, ret;
229 ssh_key srv_pubkey;
230 unsigned char *hash_sha1 = NULL;
231 size_t hlen;
232 enum ssh_keytypes_e srv_pubkey_type;
233 char answer[5];
234
235 state = ssh_is_server_known(session);
236
Michal Vaskocc0aa7d2016-05-31 12:48:42 +0200237 ret = ssh_get_publickey(session, &srv_pubkey);
Michal Vaskoef112d72016-02-18 13:28:25 +0100238 if (ret < 0) {
239 ERR("Unable to get server public key.");
240 return -1;
241 }
242
243 srv_pubkey_type = ssh_key_type(srv_pubkey);
244 ret = ssh_get_publickey_hash(srv_pubkey, SSH_PUBLICKEY_HASH_SHA1, &hash_sha1, &hlen);
245 ssh_key_free(srv_pubkey);
246 if (ret < 0) {
247 ERR("Failed to calculate SHA1 hash of the server public key.");
248 return -1;
249 }
250
251 hexa = ssh_get_hexa(hash_sha1, hlen);
252
253 switch (state) {
254 case SSH_SERVER_KNOWN_OK:
255 break; /* ok */
256
257 case SSH_SERVER_KNOWN_CHANGED:
258 ERR("Remote host key changed, the connection will be terminated!");
259 goto fail;
260
261 case SSH_SERVER_FOUND_OTHER:
262 WRN("Remote host key is not known, but a key of another type for this host is known. Continue with caution.");
263 goto hostkey_not_known;
264
265 case SSH_SERVER_FILE_NOT_FOUND:
266 WRN("Could not find the known hosts file.");
267 goto hostkey_not_known;
268
269 case SSH_SERVER_NOT_KNOWN:
270hostkey_not_known:
271#ifdef ENABLE_DNSSEC
272 if ((srv_pubkey_type != SSH_KEYTYPE_UNKNOWN) || (srv_pubkey_type != SSH_KEYTYPE_RSA1)) {
273 if (srv_pubkey_type == SSH_KEYTYPE_DSS) {
Michal Vasko650011a2016-02-25 14:49:29 +0100274 ret = sshauth_hostkey_hash_dnssec_check(hostname, hash_sha1, 2, 1);
Michal Vaskoef112d72016-02-18 13:28:25 +0100275 } else if (srv_pubkey_type == SSH_KEYTYPE_RSA) {
Michal Vasko650011a2016-02-25 14:49:29 +0100276 ret = sshauth_hostkey_hash_dnssec_check(hostname, hash_sha1, 1, 1);
Michal Vaskoef112d72016-02-18 13:28:25 +0100277 } else if (srv_pubkey_type == SSH_KEYTYPE_ECDSA) {
Michal Vasko650011a2016-02-25 14:49:29 +0100278 ret = sshauth_hostkey_hash_dnssec_check(hostname, hash_sha1, 3, 1);
Michal Vaskoef112d72016-02-18 13:28:25 +0100279 }
280
281 /* DNSSEC SSHFP check successful, that's enough */
282 if (!ret) {
283 VRB("DNSSEC SSHFP check successful.");
284 ssh_write_knownhost(session);
285 ssh_clean_pubkey_hash(&hash_sha1);
286 ssh_string_free_char(hexa);
287 return 0;
288 }
289 }
290#endif
291
292 /* try to get result from user */
293 fprintf(stdout, "The authenticity of the host \'%s\' cannot be established.\n", hostname);
294 fprintf(stdout, "%s key fingerprint is %s.\n", ssh_key_type_to_char(srv_pubkey_type), hexa);
295
296#ifdef ENABLE_DNSSEC
297 if (ret == 2) {
298 fprintf(stdout, "No matching host key fingerprint found using DNS.\n");
299 } else if (ret == 1) {
300 fprintf(stdout, "Matching host key fingerprint found using DNS.\n");
301 }
302#endif
303
304 fprintf(stdout, "Are you sure you want to continue connecting (yes/no)? ");
305
306 do {
307 if (fscanf(stdin, "%4s", answer) == EOF) {
308 ERR("fscanf() failed (%s).", strerror(errno));
309 goto fail;
310 }
311 while (((c = getchar()) != EOF) && (c != '\n'));
312
313 fflush(stdin);
314 if (!strcmp("yes", answer)) {
315 /* store the key into the host file */
316 ret = ssh_write_knownhost(session);
317 if (ret != SSH_OK) {
318 WRN("Adding the known host \"%s\" failed (%s).", hostname, ssh_get_error(session));
319 }
320 } else if (!strcmp("no", answer)) {
321 goto fail;
322 } else {
323 fprintf(stdout, "Please type 'yes' or 'no': ");
324 }
325 } while (strcmp(answer, "yes") && strcmp(answer, "no"));
326
327 break;
328
329 case SSH_SERVER_ERROR:
330 ssh_clean_pubkey_hash(&hash_sha1);
331 fprintf(stderr,"%s",ssh_get_error(session));
332 return -1;
333 }
334
335 ssh_clean_pubkey_hash(&hash_sha1);
336 ssh_string_free_char(hexa);
337 return 0;
338
339fail:
340 ssh_clean_pubkey_hash(&hash_sha1);
341 ssh_string_free_char(hexa);
342 return -1;
343}
344
Michal Vasko7b62fed2015-10-26 15:39:46 +0100345static char *
346sshauth_password(const char *username, const char *hostname)
Radek Krejciac6d3472015-10-22 15:47:18 +0200347{
Michal Vasko4eb3c312016-03-01 14:09:37 +0100348 char *buf;
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200349 int buflen = 1024, len, ret;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100350 char c = 0;
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200351 struct termios oldterm;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100352 FILE *tty;
Radek Krejciac6d3472015-10-22 15:47:18 +0200353
Michal Vasko11d142a2016-01-19 15:58:24 +0100354 buf = malloc(buflen * sizeof *buf);
355 if (!buf) {
356 ERRMEM;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100357 return NULL;
358 }
359
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200360 if ((ret = ttyname_r(STDIN_FILENO, buf, buflen))) {
361 ERR("ttyname_r failed (%s).", strerror(ret));
362 free(buf);
363 return NULL;
364 }
365
366 if (!(tty = open_tty_noecho(buf, &oldterm))) {
367 free(buf);
368 return NULL;
369 }
370
371 fprintf(stdout, "%s@%s password: ", username, hostname);
372 fflush(stdout);
373
374 len = 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100375 while ((fread(&c, 1, 1, tty) == 1) && (c != '\n')) {
376 if (len >= buflen - 1) {
377 buflen *= 2;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100378 buf = nc_realloc(buf, buflen * sizeof *buf);
379 if (!buf) {
Michal Vaskod083db62016-01-19 10:31:29 +0100380 ERRMEM;
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200381 restore_tty_close(tty, &oldterm);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100382 return NULL;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100383 }
384 }
385 buf[len++] = c;
386 }
387 buf[len++] = 0; /* terminating null byte */
388
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200389 fprintf(stdout, "\n");
390 restore_tty_close(tty, &oldterm);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100391 return buf;
392}
393
394static char *
395sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo)
396{
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200397 unsigned int buflen = 64, cur_len;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100398 char c = 0;
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200399 int ret;
400 struct termios oldterm;
401 char *buf;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100402 FILE *tty;
403
404 buf = malloc(buflen * sizeof *buf);
405 if (!buf) {
Michal Vaskod083db62016-01-19 10:31:29 +0100406 ERRMEM;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100407 return NULL;
408 }
409
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200410 if ((ret = ttyname_r(STDIN_FILENO, buf, buflen))) {
411 ERR("ttyname_r failed (%s).", strerror(ret));
412 free(buf);
413 return NULL;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100414 }
415
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200416 if (!echo) {
417 if (!(tty = open_tty_noecho(buf, &oldterm))) {
418 free(buf);
419 return NULL;
420 }
421 } else {
422 tty = stdin;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100423 }
424
Michal Vasko7b62fed2015-10-26 15:39:46 +0100425
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200426 if (auth_name && (!fwrite(auth_name, sizeof *auth_name, strlen(auth_name), stdout)
427 || !fwrite("\n", sizeof(char), 1, stdout))) {
428 ERR("Writing the auth method name into stdout failed.");
Michal Vasko94e7c2d2016-01-21 15:57:57 +0100429 goto fail;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100430 }
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200431 if (instruction && (!fwrite(instruction, sizeof *auth_name, strlen(instruction), stdout)
432 || !fwrite("\n", sizeof(char), 1, stdout))) {
433 ERR("Writing the instruction into stdout failed.");
434 goto fail;
435 }
436 if (!fwrite(prompt, sizeof *prompt, strlen(prompt), stdout)) {
437 ERR("Writing the authentication prompt into stdout failed.");
438 goto fail;
439 }
440 fflush(stdout);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100441
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200442 cur_len = 0;
443 while ((fread(&c, 1, 1, tty) == 1) && (c != '\n')) {
444 if (cur_len >= buflen - 1) {
445 buflen *= 2;
446 buf = nc_realloc(buf, buflen * sizeof *buf);
447 if (!buf) {
448 ERRMEM;
449 goto fail;
450 }
451 }
452 buf[cur_len++] = c;
453 }
454 /* terminating null byte */
455 buf[cur_len] = '\0';
456
457 fprintf(stdout, "\n");
458 if (!echo) {
459 restore_tty_close(tty, &oldterm);
460 }
461 return buf;
462
463fail:
464 if (!echo) {
465 restore_tty_close(tty, &oldterm);
466 }
467 free(buf);
468 return NULL;
469}
470
471static char *
472sshauth_privkey_passphrase(const char* privkey_path)
473{
474 char c, *buf;
475 int buflen = 1024, len, ret;
476 struct termios oldterm;
477 FILE *tty;
478
479 buf = malloc(buflen * sizeof *buf);
480 if (!buf) {
481 ERRMEM;
482 return NULL;
483 }
484
485 if ((ret = ttyname_r(STDIN_FILENO, buf, buflen))) {
486 ERR("ttyname_r failed (%s).", strerror(ret));
487 free(buf);
488 return NULL;
489 }
490
491 if (!(tty = open_tty_noecho(buf, &oldterm))) {
492 free(buf);
493 return NULL;
494 }
495
496 fprintf(stdout, "Enter passphrase for the key '%s':", privkey_path);
497 fflush(stdout);
498
499 len = 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100500 while ((fread(&c, 1, 1, tty) == 1) && (c != '\n')) {
501 if (len >= buflen - 1) {
502 buflen *= 2;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100503 buf = nc_realloc(buf, buflen * sizeof *buf);
504 if (!buf) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100505 ERRMEM;
Michal Vasko94e7c2d2016-01-21 15:57:57 +0100506 goto fail;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100507 }
Michal Vasko7b62fed2015-10-26 15:39:46 +0100508 }
509 buf[len++] = (char)c;
510 }
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200511 buf[len] = 0; /* terminating null byte */
Michal Vasko7b62fed2015-10-26 15:39:46 +0100512
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200513 fprintf(stdout, "\n");
514 restore_tty_close(tty, &oldterm);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100515 return buf;
Michal Vasko94e7c2d2016-01-21 15:57:57 +0100516
517fail:
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200518 restore_tty_close(tty, &oldterm);
Michal Vasko94e7c2d2016-01-21 15:57:57 +0100519 free(buf);
Michal Vasko94e7c2d2016-01-21 15:57:57 +0100520 return NULL;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100521}
522
Michal Vaskoef112d72016-02-18 13:28:25 +0100523static void
524_nc_client_ssh_set_auth_hostkey_check_clb(int (*auth_hostkey_check)(const char *hostname, ssh_session session),
525 struct nc_client_ssh_opts *opts)
Michal Vasko7b62fed2015-10-26 15:39:46 +0100526{
Michal Vaskoef112d72016-02-18 13:28:25 +0100527 if (auth_hostkey_check) {
528 opts->auth_hostkey_check = auth_hostkey_check;
529 } else {
530 opts->auth_hostkey_check = sshauth_hostkey_check;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100531 }
Michal Vasko7b62fed2015-10-26 15:39:46 +0100532}
533
Michal Vaskoef112d72016-02-18 13:28:25 +0100534API void
535nc_client_ssh_set_auth_hostkey_check_clb(int (*auth_hostkey_check)(const char *hostname, ssh_session session))
536{
537 _nc_client_ssh_set_auth_hostkey_check_clb(auth_hostkey_check, &ssh_opts);
538}
539
540API void
541nc_client_ssh_ch_set_auth_hostkey_check_clb(int (*auth_hostkey_check)(const char *hostname, ssh_session session))
542{
543 _nc_client_ssh_set_auth_hostkey_check_clb(auth_hostkey_check, &ssh_ch_opts);
544}
545
546
Michal Vasko30e2c872016-02-18 10:03:21 +0100547static void
548_nc_client_ssh_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname),
549 struct nc_client_ssh_opts *opts)
550{
551 if (auth_password) {
552 opts->auth_password = auth_password;
553 } else {
554 opts->auth_password = sshauth_password;
555 }
556}
557
558API void
559nc_client_ssh_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname))
560{
561 _nc_client_ssh_set_auth_password_clb(auth_password, &ssh_opts);
562}
563
564API void
565nc_client_ssh_ch_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname))
566{
567 _nc_client_ssh_set_auth_password_clb(auth_password, &ssh_ch_opts);
568}
569
570static void
571_nc_client_ssh_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction,
572 const char *prompt, int echo),
573 struct nc_client_ssh_opts *opts)
574{
575 if (auth_interactive) {
576 opts->auth_interactive = auth_interactive;
577 } else {
578 opts->auth_interactive = sshauth_interactive;
579 }
580}
581
582API void
583nc_client_ssh_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction,
584 const char *prompt, int echo))
585{
586 _nc_client_ssh_set_auth_interactive_clb(auth_interactive, &ssh_opts);
587}
588
589API void
590nc_client_ssh_ch_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction,
591 const char *prompt, int echo))
592{
593 _nc_client_ssh_set_auth_interactive_clb(auth_interactive, &ssh_ch_opts);
594}
595
596static void
597_nc_client_ssh_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path),
598 struct nc_client_ssh_opts *opts)
599{
600 if (auth_privkey_passphrase) {
601 opts->auth_privkey_passphrase = auth_privkey_passphrase;
602 } else {
603 opts->auth_privkey_passphrase = sshauth_privkey_passphrase;
604 }
605}
606
607API void
608nc_client_ssh_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path))
609{
610 _nc_client_ssh_set_auth_privkey_passphrase_clb(auth_privkey_passphrase, &ssh_opts);
611}
612
613API void
614nc_client_ssh_ch_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path))
615{
616 _nc_client_ssh_set_auth_privkey_passphrase_clb(auth_privkey_passphrase, &ssh_ch_opts);
617}
618
Michal Vasko3031aae2016-01-27 16:07:18 +0100619static int
620_nc_client_ssh_add_keypair(const char *pub_key, const char *priv_key, struct nc_client_ssh_opts *opts)
Michal Vasko7b62fed2015-10-26 15:39:46 +0100621{
622 int i;
623 FILE *key;
624 char line[128];
625
Michal Vasko45e53ae2016-04-07 11:46:03 +0200626 if (!pub_key) {
627 ERRARG("pub_key");
628 return -1;
629 } else if (!priv_key) {
630 ERRARG("priv_key");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100631 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100632 }
633
Michal Vasko3031aae2016-01-27 16:07:18 +0100634 for (i = 0; i < opts->key_count; ++i) {
635 if (!strcmp(opts->keys[i].pubkey_path, pub_key) || !strcmp(opts->keys[i].privkey_path, priv_key)) {
636 if (strcmp(opts->keys[i].pubkey_path, pub_key)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100637 WRN("Private key \"%s\" found with another public key \"%s\".",
Michal Vasko3031aae2016-01-27 16:07:18 +0100638 priv_key, opts->keys[i].pubkey_path);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100639 continue;
Michal Vasko3031aae2016-01-27 16:07:18 +0100640 } else if (strcmp(opts->keys[i].privkey_path, priv_key)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100641 WRN("Public key \"%s\" found with another private key \"%s\".",
Michal Vasko3031aae2016-01-27 16:07:18 +0100642 pub_key, opts->keys[i].privkey_path);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100643 continue;
644 }
645
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100646 ERR("SSH key pair already set.");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100647 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100648 }
649 }
650
Michal Vasko3031aae2016-01-27 16:07:18 +0100651 /* add the keys */
652 ++opts->key_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100653 opts->keys = nc_realloc(opts->keys, opts->key_count * sizeof *opts->keys);
654 if (!opts->keys) {
655 ERRMEM;
656 return -1;
657 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100658 opts->keys[opts->key_count - 1].pubkey_path = strdup(pub_key);
659 opts->keys[opts->key_count - 1].privkey_path = strdup(priv_key);
660 opts->keys[opts->key_count - 1].privkey_crypt = 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100661
Michal Vasko4eb3c312016-03-01 14:09:37 +0100662 if (!opts->keys[opts->key_count - 1].pubkey_path || !opts->keys[opts->key_count - 1].privkey_path) {
663 ERRMEM;
664 return -1;
665 }
666
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100667 /* check encryption */
668 if ((key = fopen(priv_key, "r"))) {
669 /* 1st line - key type */
670 if (!fgets(line, sizeof line, key)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100671 fclose(key);
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100672 ERR("fgets() on %s failed.", priv_key);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100673 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100674 }
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100675 /* 2nd line - encryption information or key */
676 if (!fgets(line, sizeof line, key)) {
677 fclose(key);
678 ERR("fgets() on %s failed.", priv_key);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100679 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100680 }
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100681 fclose(key);
682 if (strcasestr(line, "encrypted")) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100683 opts->keys[opts->key_count - 1].privkey_crypt = 1;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100684 }
Michal Vasko7b62fed2015-10-26 15:39:46 +0100685 }
686
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100687 return 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100688}
689
690API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100691nc_client_ssh_add_keypair(const char *pub_key, const char *priv_key)
Michal Vasko7b62fed2015-10-26 15:39:46 +0100692{
Michal Vasko3031aae2016-01-27 16:07:18 +0100693 return _nc_client_ssh_add_keypair(pub_key, priv_key, &ssh_opts);
694}
695
696API int
697nc_client_ssh_ch_add_keypair(const char *pub_key, const char *priv_key)
698{
699 return _nc_client_ssh_add_keypair(pub_key, priv_key, &ssh_ch_opts);
700}
701
702static int
703_nc_client_ssh_del_keypair(int idx, struct nc_client_ssh_opts *opts)
704{
705 if (idx >= opts->key_count) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200706 ERRARG("idx");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100707 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100708 }
709
Michal Vasko3031aae2016-01-27 16:07:18 +0100710 free(opts->keys[idx].pubkey_path);
711 free(opts->keys[idx].privkey_path);
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100712
Michal Vasko3031aae2016-01-27 16:07:18 +0100713 --opts->key_count;
Michal Vaskoc0256492016-02-02 12:19:06 +0100714 if (idx < opts->key_count) {
715 memcpy(&opts->keys[idx], &opts->keys[opts->key_count], sizeof *opts->keys);
716 }
717 if (opts->key_count) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100718 opts->keys = nc_realloc(opts->keys, opts->key_count * sizeof *opts->keys);
719 if (!opts->keys) {
720 ERRMEM;
721 return -1;
722 }
Michal Vaskoc0256492016-02-02 12:19:06 +0100723 } else {
724 free(opts->keys);
725 opts->keys = NULL;
726 }
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100727
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100728 return 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100729}
730
731API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100732nc_client_ssh_del_keypair(int idx)
Michal Vasko7b62fed2015-10-26 15:39:46 +0100733{
Michal Vasko3031aae2016-01-27 16:07:18 +0100734 return _nc_client_ssh_del_keypair(idx, &ssh_opts);
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100735}
736
737API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100738nc_client_ssh_ch_del_keypair(int idx)
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100739{
Michal Vasko3031aae2016-01-27 16:07:18 +0100740 return _nc_client_ssh_del_keypair(idx, &ssh_ch_opts);
741}
742
743static int
744_nc_client_ssh_get_keypair_count(struct nc_client_ssh_opts *opts)
745{
746 return opts->key_count;
747}
748
749API int
750nc_client_ssh_get_keypair_count(void)
751{
752 return _nc_client_ssh_get_keypair_count(&ssh_opts);
753}
754
755API int
756nc_client_ssh_ch_get_keypair_count(void)
757{
758 return _nc_client_ssh_get_keypair_count(&ssh_ch_opts);
759}
760
761static int
762_nc_client_ssh_get_keypair(int idx, const char **pub_key, const char **priv_key, struct nc_client_ssh_opts *opts)
763{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200764 if (idx >= opts->key_count) {
765 ERRARG("idx");
766 return -1;
767 } else if (!pub_key && !priv_key) {
768 ERRARG("pub_key and priv_key");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100769 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100770 }
771
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100772 if (pub_key) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100773 *pub_key = opts->keys[idx].pubkey_path;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100774 }
775 if (priv_key) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100776 *priv_key = opts->keys[idx].privkey_path;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100777 }
778
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100779 return 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100780}
781
Michal Vasko3031aae2016-01-27 16:07:18 +0100782API int
783nc_client_ssh_get_keypair(int idx, const char **pub_key, const char **priv_key)
784{
785 return _nc_client_ssh_get_keypair(idx, pub_key, priv_key, &ssh_opts);
786}
787
788API int
789nc_client_ssh_ch_get_keypair(int idx, const char **pub_key, const char **priv_key)
790{
791 return _nc_client_ssh_get_keypair(idx, pub_key, priv_key, &ssh_ch_opts);
792}
793
794static void
795_nc_client_ssh_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref, struct nc_client_ssh_opts *opts)
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100796{
797 if (pref < 0) {
798 pref = -1;
799 }
800
801 if (auth_type == NC_SSH_AUTH_INTERACTIVE) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100802 opts->auth_pref[0].value = pref;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100803 } else if (auth_type == NC_SSH_AUTH_PASSWORD) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100804 opts->auth_pref[1].value = pref;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100805 } else if (auth_type == NC_SSH_AUTH_PUBLICKEY) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100806 opts->auth_pref[2].value = pref;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100807 }
808}
809
Michal Vasko3031aae2016-01-27 16:07:18 +0100810API void
811nc_client_ssh_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref)
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100812{
Michal Vasko3031aae2016-01-27 16:07:18 +0100813 _nc_client_ssh_set_auth_pref(auth_type, pref, &ssh_opts);
814}
815
816API void
817nc_client_ssh_ch_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref)
818{
819 _nc_client_ssh_set_auth_pref(auth_type, pref, &ssh_ch_opts);
820}
821
822static int16_t
823_nc_client_ssh_get_auth_pref(NC_SSH_AUTH_TYPE auth_type, struct nc_client_ssh_opts *opts)
824{
825 int16_t pref = 0;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100826
827 if (auth_type == NC_SSH_AUTH_INTERACTIVE) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100828 pref = opts->auth_pref[0].value;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100829 } else if (auth_type == NC_SSH_AUTH_PASSWORD) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100830 pref = opts->auth_pref[1].value;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100831 } else if (auth_type == NC_SSH_AUTH_PUBLICKEY) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100832 pref = opts->auth_pref[2].value;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100833 }
834
835 return pref;
836}
837
Michal Vasko3031aae2016-01-27 16:07:18 +0100838API int16_t
839nc_client_ssh_get_auth_pref(NC_SSH_AUTH_TYPE auth_type)
840{
841 return _nc_client_ssh_get_auth_pref(auth_type, &ssh_opts);
842}
843
844API int16_t
845nc_client_ssh_ch_get_auth_pref(NC_SSH_AUTH_TYPE auth_type)
846{
847 return _nc_client_ssh_get_auth_pref(auth_type, &ssh_ch_opts);
848}
849
850static int
851_nc_client_ssh_set_username(const char *username, struct nc_client_ssh_opts *opts)
852{
853 if (opts->username) {
854 free(opts->username);
855 }
856 if (username) {
857 opts->username = strdup(username);
858 if (!opts->username) {
859 ERRMEM;
860 return -1;
861 }
862 } else {
863 opts->username = NULL;
864 }
865
866 return 0;
867}
868
869API int
870nc_client_ssh_set_username(const char *username)
871{
872 return _nc_client_ssh_set_username(username, &ssh_opts);
873}
874
875API int
876nc_client_ssh_ch_set_username(const char *username)
877{
878 return _nc_client_ssh_set_username(username, &ssh_ch_opts);
879}
880
Michal Vaskoe22c6732016-01-29 11:03:02 +0100881static const char *
882_nc_client_ssh_get_username(struct nc_client_ssh_opts *opts)
883{
884 return opts->username;
885}
886
887API const char *
888nc_client_ssh_get_username(void)
889{
890 return _nc_client_ssh_get_username(&ssh_opts);
891}
892
893API const char *
894nc_client_ssh_ch_get_username(void)
895{
896 return _nc_client_ssh_get_username(&ssh_ch_opts);
897}
898
Michal Vasko3031aae2016-01-27 16:07:18 +0100899API int
900nc_client_ssh_ch_add_bind_listen(const char *address, uint16_t port)
901{
902 return nc_client_ch_add_bind_listen(address, port, NC_TI_LIBSSH);
903}
904
905API int
906nc_client_ssh_ch_del_bind(const char *address, uint16_t port)
907{
908 return nc_client_ch_del_bind(address, port, NC_TI_LIBSSH);
909}
910
Michal Vasko8e2f4a62016-02-01 15:59:48 +0100911/* Establish a secure SSH connection and authenticate.
Michal Vasko7b62fed2015-10-26 15:39:46 +0100912 * Host, port, username, and a connected socket is expected to be set.
913 */
914static int
Michal Vasko0190bc32016-03-02 15:47:49 +0100915connect_ssh_session(struct nc_session *session, struct nc_client_ssh_opts *opts, int timeout)
Michal Vasko7b62fed2015-10-26 15:39:46 +0100916{
Michal Vasko36c7be82017-02-22 13:37:59 +0100917 int j, ret_auth, userauthlist, ret;
Michal Vasko235efdc2015-12-17 12:05:04 +0100918 NC_SSH_AUTH_TYPE auth;
Michal Vasko0190bc32016-03-02 15:47:49 +0100919 int16_t pref;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100920 const char* prompt;
921 char *s, *answer, echo;
922 ssh_key pubkey, privkey;
923 ssh_session ssh_sess;
Michal Vasko36c7be82017-02-22 13:37:59 +0100924 struct timespec ts_timeout, ts_cur;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100925
926 ssh_sess = session->ti.libssh.session;
927
Michal Vasko36c7be82017-02-22 13:37:59 +0100928 nc_gettimespec(&ts_timeout);
929 nc_addtimespec(&ts_timeout, NC_TRANSPORT_TIMEOUT);
Michal Vasko0190bc32016-03-02 15:47:49 +0100930 while ((ret = ssh_connect(ssh_sess)) == SSH_AGAIN) {
931 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +0100932 nc_gettimespec(&ts_cur);
933 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
Michal Vasko0190bc32016-03-02 15:47:49 +0100934 break;
935 }
936 }
937 if (ret == SSH_AGAIN) {
938 ERR("SSH connect timeout.");
939 return 0;
940 } else if (ret != SSH_OK) {
941 ERR("Starting the SSH session failed (%s).", ssh_get_error(ssh_sess));
Michal Vasko7b62fed2015-10-26 15:39:46 +0100942 DBG("Error code %d.", ssh_get_error_code(ssh_sess));
Michal Vaskod083db62016-01-19 10:31:29 +0100943 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100944 }
945
Michal Vaskoef112d72016-02-18 13:28:25 +0100946 if (opts->auth_hostkey_check(session->host, ssh_sess)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100947 ERR("Checking the host key failed.");
Michal Vaskod083db62016-01-19 10:31:29 +0100948 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100949 }
950
Michal Vasko36c7be82017-02-22 13:37:59 +0100951 if (timeout > -1) {
952 nc_gettimespec(&ts_timeout);
953 nc_addtimespec(&ts_timeout, timeout);
954 }
Michal Vasko0190bc32016-03-02 15:47:49 +0100955 while ((ret_auth = ssh_userauth_none(ssh_sess, NULL)) == SSH_AUTH_AGAIN) {
956 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +0100957 if (timeout > -1) {
958 nc_gettimespec(&ts_cur);
959 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
960 break;
961 }
Michal Vasko0190bc32016-03-02 15:47:49 +0100962 }
963 }
964 if (ret_auth == SSH_AUTH_AGAIN) {
965 ERR("Request authentication methods timeout.");
966 return 0;
967 } else if (ret_auth == SSH_AUTH_ERROR) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100968 ERR("Authentication failed (%s).", ssh_get_error(ssh_sess));
Michal Vaskod083db62016-01-19 10:31:29 +0100969 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100970 }
971
972 /* check what authentication methods are available */
973 userauthlist = ssh_userauth_list(ssh_sess, NULL);
Michal Vasko235efdc2015-12-17 12:05:04 +0100974
975 /* remove those disabled */
Michal Vasko30e2c872016-02-18 10:03:21 +0100976 if (opts->auth_pref[0].value < 0) {
Michal Vasko235efdc2015-12-17 12:05:04 +0100977 VRB("Interactive SSH authentication method was disabled.");
978 userauthlist &= ~SSH_AUTH_METHOD_INTERACTIVE;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100979 }
Michal Vasko30e2c872016-02-18 10:03:21 +0100980 if (opts->auth_pref[1].value < 0) {
Michal Vasko235efdc2015-12-17 12:05:04 +0100981 VRB("Password SSH authentication method was disabled.");
982 userauthlist &= ~SSH_AUTH_METHOD_PASSWORD;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100983 }
Michal Vasko30e2c872016-02-18 10:03:21 +0100984 if (opts->auth_pref[2].value < 0) {
Michal Vasko235efdc2015-12-17 12:05:04 +0100985 VRB("Publickey SSH authentication method was disabled.");
986 userauthlist &= ~SSH_AUTH_METHOD_PUBLICKEY;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100987 }
988
Michal Vasko0190bc32016-03-02 15:47:49 +0100989 do {
Michal Vasko235efdc2015-12-17 12:05:04 +0100990 auth = 0;
991 pref = 0;
992 if (userauthlist & SSH_AUTH_METHOD_INTERACTIVE) {
993 auth = NC_SSH_AUTH_INTERACTIVE;
Michal Vasko30e2c872016-02-18 10:03:21 +0100994 pref = opts->auth_pref[0].value;
Michal Vasko235efdc2015-12-17 12:05:04 +0100995 }
Michal Vasko30e2c872016-02-18 10:03:21 +0100996 if ((userauthlist & SSH_AUTH_METHOD_PASSWORD) && (opts->auth_pref[1].value > pref)) {
Michal Vasko235efdc2015-12-17 12:05:04 +0100997 auth = NC_SSH_AUTH_PASSWORD;
Michal Vasko30e2c872016-02-18 10:03:21 +0100998 pref = opts->auth_pref[1].value;
Michal Vasko235efdc2015-12-17 12:05:04 +0100999 }
Michal Vasko30e2c872016-02-18 10:03:21 +01001000 if ((userauthlist & SSH_AUTH_METHOD_PUBLICKEY) && (opts->auth_pref[2].value > pref)) {
Michal Vasko235efdc2015-12-17 12:05:04 +01001001 auth = NC_SSH_AUTH_PUBLICKEY;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001002 }
1003
Michal Vasko235efdc2015-12-17 12:05:04 +01001004 if (!auth) {
Michal Vaskod083db62016-01-19 10:31:29 +01001005 ERR("Unable to authenticate to the remote server (no supported authentication methods left).");
Michal Vasko0190bc32016-03-02 15:47:49 +01001006 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001007 }
1008
1009 /* found common authentication method */
Michal Vasko235efdc2015-12-17 12:05:04 +01001010 switch (auth) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001011 case NC_SSH_AUTH_PASSWORD:
Michal Vasko235efdc2015-12-17 12:05:04 +01001012 userauthlist &= ~SSH_AUTH_METHOD_PASSWORD;
1013
Michal Vaskoef578332016-01-25 13:20:09 +01001014 VRB("Password authentication (host \"%s\", user \"%s\").", session->host, session->username);
Michal Vasko30e2c872016-02-18 10:03:21 +01001015 s = opts->auth_password(session->username, session->host);
Michal Vasko0190bc32016-03-02 15:47:49 +01001016
Michal Vasko36c7be82017-02-22 13:37:59 +01001017 if (timeout > -1) {
1018 nc_gettimespec(&ts_timeout);
1019 nc_addtimespec(&ts_timeout, timeout);
1020 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001021 while ((ret_auth = ssh_userauth_password(ssh_sess, session->username, s)) == SSH_AUTH_AGAIN) {
1022 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001023 if (timeout > -1) {
1024 nc_gettimespec(&ts_cur);
1025 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1026 break;
1027 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001028 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001029 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001030 memset(s, 0, strlen(s));
Michal Vasko7b62fed2015-10-26 15:39:46 +01001031 free(s);
1032 break;
Michal Vasko0190bc32016-03-02 15:47:49 +01001033
Michal Vasko7b62fed2015-10-26 15:39:46 +01001034 case NC_SSH_AUTH_INTERACTIVE:
Michal Vasko235efdc2015-12-17 12:05:04 +01001035 userauthlist &= ~SSH_AUTH_METHOD_INTERACTIVE;
1036
Michal Vaskod083db62016-01-19 10:31:29 +01001037 VRB("Keyboard-interactive authentication.");
Michal Vasko0190bc32016-03-02 15:47:49 +01001038
Michal Vasko36c7be82017-02-22 13:37:59 +01001039 if (timeout > -1) {
1040 nc_gettimespec(&ts_timeout);
1041 nc_addtimespec(&ts_timeout, timeout);
1042 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001043 while (((ret_auth = ssh_userauth_kbdint(ssh_sess, NULL, NULL)) == SSH_AUTH_INFO)
1044 || (ret_auth == SSH_AUTH_AGAIN)) {
1045 if (ret_auth == SSH_AUTH_AGAIN) {
1046 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001047 if (timeout > -1) {
1048 nc_gettimespec(&ts_cur);
1049 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1050 break;
1051 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001052 }
1053 continue;
1054 }
1055
Michal Vasko7b62fed2015-10-26 15:39:46 +01001056 for (j = 0; j < ssh_userauth_kbdint_getnprompts(ssh_sess); ++j) {
1057 prompt = ssh_userauth_kbdint_getprompt(ssh_sess, j, &echo);
Michal Vasko0190bc32016-03-02 15:47:49 +01001058 if (!prompt) {
1059 ret_auth = SSH_AUTH_ERROR;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001060 break;
1061 }
Michal Vasko8bf28d12016-02-24 13:29:42 +01001062
1063 /* libssh BUG - echo is always 1 for some reason, assume always 0 */
1064 echo = 0;
1065
Michal Vasko30e2c872016-02-18 10:03:21 +01001066 answer = opts->auth_interactive(ssh_userauth_kbdint_getname(ssh_sess),
1067 ssh_userauth_kbdint_getinstruction(ssh_sess),
1068 prompt, echo);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001069 if (ssh_userauth_kbdint_setanswer(ssh_sess, j, answer) < 0) {
1070 free(answer);
Michal Vasko0190bc32016-03-02 15:47:49 +01001071 ret_auth = SSH_AUTH_ERROR;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001072 break;
1073 }
1074 free(answer);
1075 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001076 if (ret_auth == SSH_AUTH_ERROR) {
1077 break;
1078 }
Michal Vasko36c7be82017-02-22 13:37:59 +01001079 if (timeout > -1) {
1080 nc_gettimespec(&ts_timeout);
1081 nc_addtimespec(&ts_timeout, timeout);
1082 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001083 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001084 break;
Michal Vasko0190bc32016-03-02 15:47:49 +01001085
Michal Vasko206d3b12015-12-04 11:08:42 +01001086 case NC_SSH_AUTH_PUBLICKEY:
Michal Vasko235efdc2015-12-17 12:05:04 +01001087 userauthlist &= ~SSH_AUTH_METHOD_PUBLICKEY;
1088
Michal Vaskod083db62016-01-19 10:31:29 +01001089 VRB("Publickey athentication.");
Michal Vasko7b62fed2015-10-26 15:39:46 +01001090
1091 /* if publickeys path not provided, we cannot continue */
Michal Vasko30e2c872016-02-18 10:03:21 +01001092 if (!opts->key_count) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001093 VRB("No key pair specified.");
1094 break;
1095 }
1096
Michal Vasko30e2c872016-02-18 10:03:21 +01001097 for (j = 0; j < opts->key_count; j++) {
Michal Vaskoef578332016-01-25 13:20:09 +01001098 VRB("Trying to authenticate using %spair \"%s\" \"%s\".",
Michal Vasko30e2c872016-02-18 10:03:21 +01001099 opts->keys[j].privkey_crypt ? "password-protected " : "", opts->keys[j].privkey_path,
1100 opts->keys[j].pubkey_path);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001101
Michal Vasko7abcdeb2016-05-30 15:27:00 +02001102 ret = ssh_pki_import_pubkey_file(opts->keys[j].pubkey_path, &pubkey);
1103 if (ret == SSH_EOF) {
1104 WRN("Failed to import the key \"%s\" (File access problem).", opts->keys[j].pubkey_path);
1105 continue;
1106 } else if (ret == SSH_ERROR) {
1107 WRN("Failed to import the key \"%s\" (SSH error).", opts->keys[j].pubkey_path);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001108 continue;
1109 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001110
Michal Vasko36c7be82017-02-22 13:37:59 +01001111 if (timeout > -1) {
1112 nc_gettimespec(&ts_timeout);
1113 nc_addtimespec(&ts_timeout, timeout);
1114 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001115 while ((ret_auth = ssh_userauth_try_publickey(ssh_sess, NULL, pubkey)) == SSH_AUTH_AGAIN) {
1116 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001117 if (timeout > -1) {
1118 nc_gettimespec(&ts_cur);
1119 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1120 break;
1121 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001122 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001123 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001124 ssh_key_free(pubkey);
1125
1126 if (ret_auth == SSH_AUTH_DENIED) {
1127 continue;
1128 } else if (ret_auth != SSH_AUTH_SUCCESS) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001129 break;
1130 }
1131
Michal Vasko30e2c872016-02-18 10:03:21 +01001132 if (opts->keys[j].privkey_crypt) {
1133 s = opts->auth_privkey_passphrase(opts->keys[j].privkey_path);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001134 } else {
1135 s = NULL;
1136 }
1137
Michal Vasko0190bc32016-03-02 15:47:49 +01001138 ret = ssh_pki_import_privkey_file(opts->keys[j].privkey_path, s, NULL, NULL, &privkey);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001139 if (s) {
1140 memset(s, 0, strlen(s));
1141 free(s);
1142 }
Michal Vasko7abcdeb2016-05-30 15:27:00 +02001143 if (ret == SSH_EOF) {
1144 WRN("Failed to import the key \"%s\" (File access problem).", opts->keys[j].privkey_path);
1145 continue;
1146 } else if (ret == SSH_ERROR) {
1147 WRN("Failed to import the key \"%s\" (SSH error).", opts->keys[j].privkey_path);
Michal Vasko0190bc32016-03-02 15:47:49 +01001148 continue;
1149 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001150
Michal Vasko36c7be82017-02-22 13:37:59 +01001151 if (timeout > -1) {
1152 nc_gettimespec(&ts_timeout);
1153 nc_addtimespec(&ts_timeout, timeout);
1154 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001155 while ((ret_auth = ssh_userauth_publickey(ssh_sess, NULL, privkey)) == SSH_AUTH_AGAIN) {
1156 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001157 if (timeout > -1) {
1158 nc_gettimespec(&ts_cur);
1159 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1160 break;
1161 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001162 }
1163 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001164 ssh_key_free(privkey);
1165
Michal Vasko0190bc32016-03-02 15:47:49 +01001166 if (ret_auth != SSH_AUTH_DENIED) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001167 break;
1168 }
1169 }
1170 break;
1171 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001172
Michal Vasko0190bc32016-03-02 15:47:49 +01001173 switch (ret_auth) {
1174 case SSH_AUTH_AGAIN:
1175 ERR("Authentication response timeout.");
1176 return 0;
1177 case SSH_AUTH_ERROR:
1178 ERR("Authentication failed (%s).", ssh_get_error(ssh_sess));
1179 return -1;
1180 case SSH_AUTH_DENIED:
1181 WRN("Authentication denied.");
1182 break;
1183 case SSH_AUTH_PARTIAL:
1184 VRB("Partial authentication success.");
1185 break;
1186 case SSH_AUTH_SUCCESS:
1187 VRB("Authentication successful.");
1188 break;
1189 case SSH_AUTH_INFO:
1190 ERRINT;
1191 return -1;
1192 }
1193 } while (ret_auth != SSH_AUTH_SUCCESS);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001194
Michal Vasko0190bc32016-03-02 15:47:49 +01001195 return 1;
Michal Vasko8e2f4a62016-02-01 15:59:48 +01001196}
1197
1198/* Open new SSH channel and request the 'netconf' subsystem.
1199 * SSH connection is expected to be established.
1200 */
1201static int
Michal Vasko0190bc32016-03-02 15:47:49 +01001202open_netconf_channel(struct nc_session *session, int timeout)
Michal Vasko8e2f4a62016-02-01 15:59:48 +01001203{
1204 ssh_session ssh_sess;
Michal Vasko36c7be82017-02-22 13:37:59 +01001205 int ret;
1206 struct timespec ts_timeout, ts_cur;
Michal Vasko8e2f4a62016-02-01 15:59:48 +01001207
1208 ssh_sess = session->ti.libssh.session;
1209
1210 if (!ssh_is_connected(ssh_sess)) {
1211 ERR("SSH session not connected.");
1212 return -1;
1213 }
1214
1215 if (session->ti.libssh.channel) {
1216 ERR("SSH channel already created.");
1217 return -1;
1218 }
1219
Michal Vasko7b62fed2015-10-26 15:39:46 +01001220 /* open a channel */
Michal Vasko36c7be82017-02-22 13:37:59 +01001221 if (timeout > -1) {
1222 nc_gettimespec(&ts_timeout);
1223 nc_addtimespec(&ts_timeout, timeout);
1224 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001225 session->ti.libssh.channel = ssh_channel_new(ssh_sess);
Michal Vasko0190bc32016-03-02 15:47:49 +01001226 while ((ret = ssh_channel_open_session(session->ti.libssh.channel)) == SSH_AGAIN) {
1227 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001228 if (timeout > -1) {
1229 nc_gettimespec(&ts_cur);
1230 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1231 break;
1232 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001233 }
1234 }
1235 if (ret == SSH_AGAIN) {
1236 ERR("Opening an SSH channel timeout elapsed.");
Michal Vasko7b62fed2015-10-26 15:39:46 +01001237 ssh_channel_free(session->ti.libssh.channel);
1238 session->ti.libssh.channel = NULL;
Michal Vasko0190bc32016-03-02 15:47:49 +01001239 return 0;
1240 } else if (ret == SSH_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +01001241 ERR("Opening an SSH channel failed (%s).", ssh_get_error(ssh_sess));
Michal Vasko0190bc32016-03-02 15:47:49 +01001242 ssh_channel_free(session->ti.libssh.channel);
1243 session->ti.libssh.channel = NULL;
Michal Vaskod083db62016-01-19 10:31:29 +01001244 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001245 }
1246
1247 /* execute the NETCONF subsystem on the channel */
Michal Vasko36c7be82017-02-22 13:37:59 +01001248 if (timeout > -1) {
1249 nc_gettimespec(&ts_timeout);
1250 nc_addtimespec(&ts_timeout, timeout);
1251 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001252 while ((ret = ssh_channel_request_subsystem(session->ti.libssh.channel, "netconf")) == SSH_AGAIN) {
1253 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001254 if (timeout > -1) {
1255 nc_gettimespec(&ts_cur);
1256 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1257 break;
1258 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001259 }
1260 }
1261 if (ret == SSH_AGAIN) {
1262 ERR("Starting the \"netconf\" SSH subsystem timeout elapsed.");
Michal Vasko7b62fed2015-10-26 15:39:46 +01001263 ssh_channel_free(session->ti.libssh.channel);
1264 session->ti.libssh.channel = NULL;
Michal Vasko0190bc32016-03-02 15:47:49 +01001265 return 0;
1266 } else if (ret == SSH_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +01001267 ERR("Starting the \"netconf\" SSH subsystem failed (%s).", ssh_get_error(ssh_sess));
Michal Vasko0190bc32016-03-02 15:47:49 +01001268 ssh_channel_free(session->ti.libssh.channel);
1269 session->ti.libssh.channel = NULL;
Michal Vaskod083db62016-01-19 10:31:29 +01001270 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001271 }
1272
Michal Vasko0190bc32016-03-02 15:47:49 +01001273 return 1;
Radek Krejciac6d3472015-10-22 15:47:18 +02001274}
1275
Michal Vasko30e2c872016-02-18 10:03:21 +01001276static struct nc_session *
Michal Vasko0190bc32016-03-02 15:47:49 +01001277_nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx, struct nc_client_ssh_opts *opts, int timeout)
Michal Vasko30e2c872016-02-18 10:03:21 +01001278{
1279 char *host = NULL, *username = NULL;
1280 unsigned short port = 0;
1281 int sock;
1282 struct passwd *pw;
1283 struct nc_session *session = NULL;
1284
1285 if (!ssh_session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001286 ERRARG("ssh_session");
Michal Vasko30e2c872016-02-18 10:03:21 +01001287 return NULL;
1288 }
1289
1290 /* prepare session structure */
Michal Vaskoade892d2017-02-22 13:40:35 +01001291 session = nc_new_session(0);
Michal Vasko30e2c872016-02-18 10:03:21 +01001292 if (!session) {
1293 ERRMEM;
1294 return NULL;
1295 }
1296 session->status = NC_STATUS_STARTING;
1297 session->side = NC_CLIENT;
1298
1299 /* transport lock */
Michal Vasko30e2c872016-02-18 10:03:21 +01001300 pthread_mutex_init(session->ti_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +01001301 pthread_cond_init(session->ti_cond, NULL);
1302 *session->ti_inuse = 0;
Michal Vasko30e2c872016-02-18 10:03:21 +01001303
1304 session->ti_type = NC_TI_LIBSSH;
1305 session->ti.libssh.session = ssh_session;
1306
1307 /* was port set? */
1308 ssh_options_get_port(ssh_session, (unsigned int *)&port);
1309
1310 if (ssh_options_get(ssh_session, SSH_OPTIONS_HOST, &host) != SSH_OK) {
1311 /*
1312 * There is no file descriptor (detected based on the host, there is no way to check
1313 * the SSH_OPTIONS_FD directly :/), we need to create it. (TCP/IP layer)
1314 */
1315
1316 /* remember host */
1317 host = strdup("localhost");
Michal Vasko4eb3c312016-03-01 14:09:37 +01001318 if (!host) {
1319 ERRMEM;
1320 goto fail;
1321 }
Michal Vasko30e2c872016-02-18 10:03:21 +01001322 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOST, host);
1323
1324 /* create and connect socket */
1325 sock = nc_sock_connect(host, port);
1326 if (sock == -1) {
Michal Vasko29af44b2016-10-13 10:59:55 +02001327 ERR("Unable to connect to %s:%u (%s).", host, port, strerror(errno));
Michal Vasko30e2c872016-02-18 10:03:21 +01001328 goto fail;
1329 }
1330 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_FD, &sock);
Michal Vasko0190bc32016-03-02 15:47:49 +01001331 ssh_set_blocking(session->ti.libssh.session, 0);
Michal Vasko30e2c872016-02-18 10:03:21 +01001332 }
1333
1334 /* was username set? */
1335 ssh_options_get(ssh_session, SSH_OPTIONS_USER, &username);
1336
1337 if (!ssh_is_connected(ssh_session)) {
1338 /*
1339 * We are connected, but not SSH authenticated. (Transport layer)
1340 */
1341
1342 /* remember username */
1343 if (!username) {
1344 if (!opts->username) {
1345 pw = getpwuid(getuid());
1346 if (!pw) {
1347 ERR("Unknown username for the SSH connection (%s).", strerror(errno));
1348 goto fail;
1349 }
1350 username = strdup(pw->pw_name);
1351 } else {
1352 username = strdup(opts->username);
1353 }
Michal Vasko4eb3c312016-03-01 14:09:37 +01001354 if (!username) {
1355 ERRMEM;
1356 goto fail;
1357 }
Michal Vasko30e2c872016-02-18 10:03:21 +01001358 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_USER, username);
1359 }
1360
1361 /* connect and authenticate SSH session */
1362 session->host = host;
1363 session->username = username;
Michal Vasko0190bc32016-03-02 15:47:49 +01001364 if (connect_ssh_session(session, opts, timeout) != 1) {
Michal Vasko30e2c872016-02-18 10:03:21 +01001365 goto fail;
1366 }
1367 }
1368
1369 /*
1370 * Almost done, open a netconf channel. (Transport layer / application layer)
1371 */
Michal Vasko0190bc32016-03-02 15:47:49 +01001372 if (open_netconf_channel(session, timeout) != 1) {
Michal Vasko30e2c872016-02-18 10:03:21 +01001373 goto fail;
1374 }
1375
1376 /*
1377 * SSH session is established and netconf channel opened, create a NETCONF session. (Application layer)
1378 */
1379
1380 /* assign context (dicionary needed for handshake) */
1381 if (!ctx) {
1382 if (client_opts.schema_searchpath) {
1383 ctx = ly_ctx_new(client_opts.schema_searchpath);
1384 } else {
1385 ctx = ly_ctx_new(SCHEMAS_DIR);
1386 }
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001387 /* definitely should not happen, but be ready */
1388 if (!ctx && !(ctx = ly_ctx_new(NULL))) {
1389 /* that's just it */
1390 goto fail;
1391 }
Michal Vasko30e2c872016-02-18 10:03:21 +01001392 } else {
1393 session->flags |= NC_SESSION_SHAREDCTX;
1394 }
1395 session->ctx = ctx;
1396
1397 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001398 if (nc_handshake(session) != NC_MSG_HELLO) {
Michal Vasko30e2c872016-02-18 10:03:21 +01001399 goto fail;
1400 }
1401 session->status = NC_STATUS_RUNNING;
1402
1403 if (nc_ctx_check_and_fill(session) == -1) {
1404 goto fail;
1405 }
1406
1407 /* store information into the dictionary */
1408 if (host) {
1409 session->host = lydict_insert_zc(ctx, host);
1410 }
1411 if (port) {
1412 session->port = port;
1413 }
1414 if (username) {
1415 session->username = lydict_insert_zc(ctx, username);
1416 }
1417
1418 return session;
1419
1420fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001421 nc_session_free(session, NULL);
Michal Vasko30e2c872016-02-18 10:03:21 +01001422 return NULL;
1423}
1424
Radek Krejciac6d3472015-10-22 15:47:18 +02001425API struct nc_session *
Michal Vasko3031aae2016-01-27 16:07:18 +01001426nc_connect_ssh(const char *host, uint16_t port, struct ly_ctx *ctx)
Radek Krejciac6d3472015-10-22 15:47:18 +02001427{
Michal Vasko1f0563a2016-03-31 08:38:44 +02001428 const long timeout = NC_SSH_TIMEOUT;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001429 int sock;
Michal Vasko55fded62016-02-02 12:19:34 +01001430 uint32_t port_uint;
Michal Vasko3031aae2016-01-27 16:07:18 +01001431 char *username;
Radek Krejciac6d3472015-10-22 15:47:18 +02001432 struct passwd *pw;
1433 struct nc_session *session = NULL;
1434
1435 /* process parameters */
1436 if (!host || strisempty(host)) {
1437 host = "localhost";
1438 }
1439
1440 if (!port) {
1441 port = NC_PORT_SSH;
1442 }
Michal Vasko55fded62016-02-02 12:19:34 +01001443 port_uint = port;
Radek Krejciac6d3472015-10-22 15:47:18 +02001444
Michal Vasko3031aae2016-01-27 16:07:18 +01001445 if (!ssh_opts.username) {
Radek Krejciac6d3472015-10-22 15:47:18 +02001446 pw = getpwuid(getuid());
1447 if (!pw) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001448 ERR("Unknown username for the SSH connection (%s).", strerror(errno));
1449 return NULL;
Radek Krejciac6d3472015-10-22 15:47:18 +02001450 } else {
1451 username = pw->pw_name;
1452 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001453 } else {
1454 username = ssh_opts.username;
Radek Krejciac6d3472015-10-22 15:47:18 +02001455 }
1456
1457 /* prepare session structure */
Michal Vaskoade892d2017-02-22 13:40:35 +01001458 session = nc_new_session(0);
Radek Krejciac6d3472015-10-22 15:47:18 +02001459 if (!session) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001460 ERRMEM;
Radek Krejciac6d3472015-10-22 15:47:18 +02001461 return NULL;
1462 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001463 session->status = NC_STATUS_STARTING;
1464 session->side = NC_CLIENT;
Radek Krejciac6d3472015-10-22 15:47:18 +02001465
Michal Vasko7b62fed2015-10-26 15:39:46 +01001466 /* transport lock */
Michal Vasko7b62fed2015-10-26 15:39:46 +01001467 pthread_mutex_init(session->ti_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +01001468 pthread_cond_init(session->ti_cond, NULL);
1469 *session->ti_inuse = 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001470
1471 /* other transport-specific data */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001472 session->ti_type = NC_TI_LIBSSH;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001473 session->ti.libssh.session = ssh_new();
1474 if (!session->ti.libssh.session) {
1475 ERR("Unable to initialize SSH session.");
1476 goto fail;
1477 }
Radek Krejciac6d3472015-10-22 15:47:18 +02001478
Michal Vasko7b62fed2015-10-26 15:39:46 +01001479 /* set some basic SSH session options */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001480 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOST, host);
Michal Vasko55fded62016-02-02 12:19:34 +01001481 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_PORT, &port_uint);
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001482 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_USER, username);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001483 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_TIMEOUT, &timeout);
Michal Vasko086311b2016-01-08 09:53:11 +01001484 if (ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOSTKEYS,
1485 "ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,"
1486 "ecdsa-sha2-nistp256,ssh-rsa,ssh-dss,ssh-rsa1")) {
1487 /* ecdsa is probably not supported... */
1488 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOSTKEYS, "ssh-ed25519,ssh-rsa,ssh-dss,ssh-rsa1");
1489 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001490
1491 /* create and assign communication socket */
Michal Vaskof05562c2016-01-20 12:06:43 +01001492 sock = nc_sock_connect(host, port);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001493 if (sock == -1) {
Michal Vasko29af44b2016-10-13 10:59:55 +02001494 ERR("Unable to connect to %s:%u (%s).", host, port, strerror(errno));
Michal Vasko7b62fed2015-10-26 15:39:46 +01001495 goto fail;
1496 }
1497 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_FD, &sock);
Michal Vasko0190bc32016-03-02 15:47:49 +01001498 ssh_set_blocking(session->ti.libssh.session, 0);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001499
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001500 /* temporarily, for session connection */
1501 session->host = host;
1502 session->username = username;
Michal Vasko0190bc32016-03-02 15:47:49 +01001503 if ((connect_ssh_session(session, &ssh_opts, NC_TRANSPORT_TIMEOUT) != 1)
1504 || (open_netconf_channel(session, NC_TRANSPORT_TIMEOUT) != 1)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001505 goto fail;
Radek Krejciac6d3472015-10-22 15:47:18 +02001506 }
1507
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001508 /* assign context (dicionary needed for handshake) */
1509 if (!ctx) {
Michal Vaskodaf9a092016-02-09 10:42:05 +01001510 if (client_opts.schema_searchpath) {
1511 ctx = ly_ctx_new(client_opts.schema_searchpath);
1512 } else {
1513 ctx = ly_ctx_new(SCHEMAS_DIR);
1514 }
Michal Vaskoe035b8e2016-03-11 10:10:03 +01001515 /* definitely should not happen, but be ready */
1516 if (!ctx && !(ctx = ly_ctx_new(NULL))) {
1517 /* that's just it */
1518 goto fail;
1519 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001520 } else {
1521 session->flags |= NC_SESSION_SHAREDCTX;
1522 }
1523 session->ctx = ctx;
1524
Radek Krejciac6d3472015-10-22 15:47:18 +02001525 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001526 if (nc_handshake(session) != NC_MSG_HELLO) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001527 goto fail;
Radek Krejciac6d3472015-10-22 15:47:18 +02001528 }
Michal Vaskoad611702015-12-03 13:41:51 +01001529 session->status = NC_STATUS_RUNNING;
Radek Krejciac6d3472015-10-22 15:47:18 +02001530
Michal Vaskoef578332016-01-25 13:20:09 +01001531 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko57eb9402015-12-08 14:38:12 +01001532 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001533 }
1534
1535 /* store information into the dictionary */
1536 session->host = lydict_insert(ctx, host, 0);
1537 session->port = port;
1538 session->username = lydict_insert(ctx, username, 0);
1539
Radek Krejciac6d3472015-10-22 15:47:18 +02001540 return session;
1541
Michal Vasko7b62fed2015-10-26 15:39:46 +01001542fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001543 nc_session_free(session, NULL);
Radek Krejciac6d3472015-10-22 15:47:18 +02001544 return NULL;
1545}
1546
1547API struct nc_session *
1548nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx)
1549{
Michal Vasko0190bc32016-03-02 15:47:49 +01001550 return _nc_connect_libssh(ssh_session, ctx, &ssh_opts, NC_TRANSPORT_TIMEOUT);
Radek Krejciac6d3472015-10-22 15:47:18 +02001551}
1552
1553API struct nc_session *
Michal Vasko7b62fed2015-10-26 15:39:46 +01001554nc_connect_ssh_channel(struct nc_session *session, struct ly_ctx *ctx)
Radek Krejciac6d3472015-10-22 15:47:18 +02001555{
Michal Vasko7b62fed2015-10-26 15:39:46 +01001556 struct nc_session *new_session, *ptr;
Radek Krejciac6d3472015-10-22 15:47:18 +02001557
Michal Vaskob7c4ff32016-01-21 15:35:54 +01001558 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001559 ERRARG("session");
Michal Vaskob7c4ff32016-01-21 15:35:54 +01001560 return NULL;
1561 }
1562
Michal Vasko7b62fed2015-10-26 15:39:46 +01001563 /* prepare session structure */
Michal Vaskoade892d2017-02-22 13:40:35 +01001564 new_session = nc_new_session(1);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001565 if (!new_session) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001566 ERRMEM;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001567 return NULL;
1568 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001569 new_session->status = NC_STATUS_STARTING;
1570 new_session->side = NC_CLIENT;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001571
1572 /* share some parameters including the session lock */
1573 new_session->ti_type = NC_TI_LIBSSH;
1574 new_session->ti_lock = session->ti_lock;
Michal Vaskoade892d2017-02-22 13:40:35 +01001575 new_session->ti_cond = session->ti_cond;
1576 new_session->ti_inuse = session->ti_inuse;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001577 new_session->ti.libssh.session = session->ti.libssh.session;
1578
1579 /* create the channel safely */
Michal Vaskoade892d2017-02-22 13:40:35 +01001580 if (nc_session_lock(new_session, -1, __func__)) {
1581 goto fail;
1582 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001583
1584 /* open a channel */
Michal Vasko0190bc32016-03-02 15:47:49 +01001585 if (open_netconf_channel(new_session, NC_TRANSPORT_TIMEOUT) != 1) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001586 goto fail;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001587 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001588
1589 /* assign context (dicionary needed for handshake) */
1590 if (!ctx) {
Michal Vaskodaf9a092016-02-09 10:42:05 +01001591 if (client_opts.schema_searchpath) {
1592 ctx = ly_ctx_new(client_opts.schema_searchpath);
1593 } else {
1594 ctx = ly_ctx_new(SCHEMAS_DIR);
1595 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001596 } else {
Michal Vasko56b5bf72016-01-19 11:20:35 +01001597 new_session->flags |= NC_SESSION_SHAREDCTX;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001598 }
Michal Vasko56b5bf72016-01-19 11:20:35 +01001599 new_session->ctx = ctx;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001600
Michal Vasko7b62fed2015-10-26 15:39:46 +01001601 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001602 if (nc_handshake(new_session) != NC_MSG_HELLO) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001603 goto fail;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001604 }
Michal Vaskoad611702015-12-03 13:41:51 +01001605 new_session->status = NC_STATUS_RUNNING;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001606
Michal Vaskoade892d2017-02-22 13:40:35 +01001607 nc_session_unlock(new_session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko56b5bf72016-01-19 11:20:35 +01001608
Michal Vaskoef578332016-01-25 13:20:09 +01001609 if (nc_ctx_check_and_fill(new_session) == -1) {
Michal Vasko57eb9402015-12-08 14:38:12 +01001610 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001611 }
1612
1613 /* store information into session and the dictionary */
Michal Vasko56b5bf72016-01-19 11:20:35 +01001614 new_session->host = lydict_insert(ctx, session->host, 0);
1615 new_session->port = session->port;
1616 new_session->username = lydict_insert(ctx, session->username, 0);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001617
1618 /* append to the session ring list */
1619 if (!session->ti.libssh.next) {
1620 session->ti.libssh.next = new_session;
1621 new_session->ti.libssh.next = session;
1622 } else {
1623 ptr = session->ti.libssh.next;
1624 session->ti.libssh.next = new_session;
1625 new_session->ti.libssh.next = ptr;
1626 }
1627
1628 return new_session;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001629
1630fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001631 nc_session_free(new_session, NULL);
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001632 return NULL;
Radek Krejciac6d3472015-10-22 15:47:18 +02001633}
Michal Vasko80cad7f2015-12-08 14:42:27 +01001634
Michal Vasko3031aae2016-01-27 16:07:18 +01001635struct nc_session *
Michal Vasko0190bc32016-03-02 15:47:49 +01001636nc_accept_callhome_ssh_sock(int sock, const char *host, uint16_t port, struct ly_ctx *ctx, int timeout)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001637{
Michal Vasko1f0563a2016-03-31 08:38:44 +02001638 const long ssh_timeout = NC_SSH_TIMEOUT;
Michal Vasko0cfa90c2016-10-13 10:34:46 +02001639 unsigned int uint_port;
Michal Vasko3031aae2016-01-27 16:07:18 +01001640 struct passwd *pw;
Michal Vasko30e2c872016-02-18 10:03:21 +01001641 struct nc_session *session;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001642 ssh_session sess;
1643
Michal Vasko80cad7f2015-12-08 14:42:27 +01001644 sess = ssh_new();
1645 if (!sess) {
1646 ERR("Unable to initialize an SSH session.");
1647 close(sock);
1648 return NULL;
1649 }
1650
1651 ssh_options_set(sess, SSH_OPTIONS_FD, &sock);
Michal Vasko0190bc32016-03-02 15:47:49 +01001652 ssh_set_blocking(sess, 0);
Michal Vasko3031aae2016-01-27 16:07:18 +01001653 ssh_options_set(sess, SSH_OPTIONS_HOST, host);
Michal Vasko0cfa90c2016-10-13 10:34:46 +02001654 uint_port = port;
1655 ssh_options_set(sess, SSH_OPTIONS_PORT, &uint_port);
Michal Vasko80cad7f2015-12-08 14:42:27 +01001656 ssh_options_set(sess, SSH_OPTIONS_TIMEOUT, &ssh_timeout);
Michal Vasko3031aae2016-01-27 16:07:18 +01001657 if (!ssh_ch_opts.username) {
1658 pw = getpwuid(getuid());
1659 if (!pw) {
1660 ERR("Unknown username for the SSH connection (%s).", strerror(errno));
1661 return NULL;
1662 }
1663 ssh_options_set(sess, SSH_OPTIONS_USER, pw->pw_name);
1664 } else {
1665 ssh_options_set(sess, SSH_OPTIONS_USER, ssh_ch_opts.username);
Michal Vasko80cad7f2015-12-08 14:42:27 +01001666 }
Michal Vasko086311b2016-01-08 09:53:11 +01001667 if (ssh_options_set(sess, SSH_OPTIONS_HOSTKEYS,
1668 "ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,"
1669 "ecdsa-sha2-nistp256,ssh-rsa,ssh-dss,ssh-rsa1")) {
1670 /* ecdsa is probably not supported... */
1671 ssh_options_set(sess, SSH_OPTIONS_HOSTKEYS, "ssh-ed25519,ssh-rsa,ssh-dss,ssh-rsa1");
1672 }
Michal Vasko80cad7f2015-12-08 14:42:27 +01001673
Michal Vasko0190bc32016-03-02 15:47:49 +01001674 session = _nc_connect_libssh(sess, ctx, &ssh_ch_opts, timeout);
Michal Vasko4282fae2016-02-18 10:03:42 +01001675 if (session) {
1676 session->flags |= NC_SESSION_CALLHOME;
1677 }
1678
Michal Vasko30e2c872016-02-18 10:03:21 +01001679 return session;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001680}