blob: 4e96adc6db5a4c2c6e7b90dc19e0ceb372161b86 [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 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 * 3. Neither the name of the Company nor the names of its contributors
21 * may be used to endorse or promote products derived from this
22 * software without specific prior written permission.
23 *
24 */
25
Michal Vasko7b62fed2015-10-26 15:39:46 +010026#define _GNU_SOURCE
Radek Krejciac6d3472015-10-22 15:47:18 +020027#include <assert.h>
Michal Vasko7b62fed2015-10-26 15:39:46 +010028#include <stdlib.h>
29#include <stddef.h>
30#include <stdio.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020031#include <string.h>
Michal Vasko7b62fed2015-10-26 15:39:46 +010032#include <errno.h>
33#include <fcntl.h>
34#include <termios.h>
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <pwd.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020038#include <unistd.h>
39
Michal Vasko7b62fed2015-10-26 15:39:46 +010040#ifdef ENABLE_DNSSEC
41# include <validator/validator.h>
42# include <validator/resolver.h>
43# include <validator/validator-compat.h>
44#endif
45
Michal Vasko745ff832015-12-08 14:40:29 +010046#include <libssh/libssh.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020047#include <libyang/libyang.h>
48
49#include "libnetconf.h"
50
Michal Vasko086311b2016-01-08 09:53:11 +010051static struct nc_ssh_client_opts ssh_opts = {
Michal Vasko206d3b12015-12-04 11:08:42 +010052 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 3}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 1}}
Michal Vasko7b62fed2015-10-26 15:39:46 +010053};
Radek Krejciac6d3472015-10-22 15:47:18 +020054
Michal Vasko990089e2015-10-27 15:05:25 +010055API void
Michal Vaskoc111ac52015-12-08 14:36:36 +010056nc_ssh_client_destroy(void)
Michal Vasko990089e2015-10-27 15:05:25 +010057{
58 int i;
59
60 for (i = 0; i < ssh_opts.key_count; ++i) {
61 free(ssh_opts.keys[i].pubkey_path);
62 free(ssh_opts.keys[i].privkey_path);
63 }
64
65 free(ssh_opts.keys);
66 ssh_opts.keys = NULL;
67 ssh_opts.key_count = 0;
68}
69
Michal Vasko7b62fed2015-10-26 15:39:46 +010070static char *
71sshauth_password(const char *username, const char *hostname)
Radek Krejciac6d3472015-10-22 15:47:18 +020072{
Michal Vasko7b62fed2015-10-26 15:39:46 +010073 char *buf, *newbuf;
74 int buflen = 1024, len = 0;
75 char c = 0;
76 struct termios newterm, oldterm;
77 FILE *tty;
Radek Krejciac6d3472015-10-22 15:47:18 +020078
Michal Vasko7b62fed2015-10-26 15:39:46 +010079 buf = malloc(buflen * sizeof *buf);
80 if (!buf) {
81 ERRMEM;
82 return NULL;
Radek Krejciac6d3472015-10-22 15:47:18 +020083 }
84
Michal Vasko7b62fed2015-10-26 15:39:46 +010085 if (!(tty = fopen("/dev/tty", "r+"))) {
86 ERR("Unable to open the current terminal (%s:%d - %s).", __FILE__, __LINE__, strerror(errno));
87 return NULL;
Radek Krejciac6d3472015-10-22 15:47:18 +020088 }
89
Michal Vasko7b62fed2015-10-26 15:39:46 +010090 if (tcgetattr(fileno(tty), &oldterm)) {
91 ERR("Unable to get terminal settings (%d: %s).", __LINE__, strerror(errno));
92 return NULL;
93 }
Radek Krejciac6d3472015-10-22 15:47:18 +020094
Michal Vasko7b62fed2015-10-26 15:39:46 +010095 fprintf(tty, "%s@%s password: ", username, hostname);
96 fflush(tty);
Radek Krejciac6d3472015-10-22 15:47:18 +020097
Michal Vasko7b62fed2015-10-26 15:39:46 +010098 /* system("stty -echo"); */
99 newterm = oldterm;
100 newterm.c_lflag &= ~ECHO;
101 newterm.c_lflag &= ~ICANON;
102 tcflush(fileno(tty), TCIFLUSH);
103 if (tcsetattr(fileno(tty), TCSANOW, &newterm)) {
104 ERR("Unable to change terminal settings for hiding password (%d: %s).", __LINE__, strerror(errno));
105 return NULL;
106 }
107
108 while ((fread(&c, 1, 1, tty) == 1) && (c != '\n')) {
109 if (len >= buflen - 1) {
110 buflen *= 2;
111 newbuf = realloc(buf, buflen * sizeof *newbuf);
112 if (!newbuf) {
113 ERR("Memory allocation failed (%s:%d - %s).", __FILE__, __LINE__, strerror(errno));
114
115 /* remove content of the buffer */
116 memset(buf, 0, len);
117 free(buf);
118
119 /* restore terminal settings */
120 if (tcsetattr(fileno(tty), TCSANOW, &oldterm) != 0) {
121 ERR("Unable to restore terminal settings (%d: %s).", __LINE__, strerror(errno));
122 }
123 return NULL;
124 } else {
125 buf = newbuf;
126 }
127 }
128 buf[len++] = c;
129 }
130 buf[len++] = 0; /* terminating null byte */
131
132 /* system ("stty echo"); */
133 if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) {
134 ERR("Unable to restore terminal settings (%d: %s).", __LINE__, strerror(errno));
135 /*
136 * terminal probably still hides input characters, but we have password
137 * and anyway we are unable to set terminal to the previous state, so
138 * just continue
139 */
140 }
141 fprintf(tty, "\n");
142
143 fclose(tty);
144 return buf;
145}
146
147static char *
148sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo)
149{
150 unsigned int buflen = 8, response_len;
151 char c = 0;
152 struct termios newterm, oldterm;
153 char *newtext, *response;
154 FILE *tty;
155
156 if (!(tty = fopen("/dev/tty", "r+"))) {
157 ERR("Unable to open the current terminal (%s:%d - %s).", __FILE__, __LINE__, strerror(errno));
158 return NULL;
159 }
160
161 if (tcgetattr(fileno(tty), &oldterm) != 0) {
162 ERR("Unable to get terminal settings (%d: %s).", __LINE__, strerror(errno));
163 return NULL;
164 }
165
166 if (auth_name && (!fwrite(auth_name, sizeof(char), strlen(auth_name), tty)
167 || !fwrite("\n", sizeof(char), 1, tty))) {
168 ERR("Writing the auth method name into stdout failed.");
169 return NULL;
170 }
171
172 if (instruction && (!fwrite(instruction, sizeof(char), strlen(instruction), tty)
173 || !fwrite("\n", sizeof(char), 1, tty))) {
174 ERR("Writing the instruction into stdout failed.");
175 return NULL;
176 }
177
178 if (!fwrite(prompt, sizeof(char), strlen(prompt), tty)) {
179 ERR("Writing the authentication prompt into stdout failed.");
180 return NULL;
181 }
182 fflush(tty);
183 if (!echo) {
184 /* system("stty -echo"); */
185 newterm = oldterm;
186 newterm.c_lflag &= ~ECHO;
187 tcflush(fileno(tty), TCIFLUSH);
188 if (tcsetattr(fileno(tty), TCSANOW, &newterm)) {
189 ERR("Unable to change terminal settings for hiding password (%d: %s).", __LINE__, strerror(errno));
190 return NULL;
191 }
192 }
193
194 response = malloc(buflen * sizeof *response);
195 response_len = 0;
196 if (!response) {
197 ERRMEM;
198 /* restore terminal settings */
199 if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) {
200 ERR("Unable to restore terminal settings (%d: %s).", __LINE__, strerror(errno));
201 }
202 return NULL;
203 }
204
205 while ((fread(&c, 1, 1, tty) == 1) && (c != '\n')) {
206 if (response_len >= buflen - 1) {
207 buflen *= 2;
208 newtext = realloc(response, buflen * sizeof *newtext);
209 if (!newtext) {
210 ERR("Memory allocation failed (%s:%d - %s).", __FILE__, __LINE__, strerror(errno));
211 free(response);
212
213 /* restore terminal settings */
214 if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) {
215 ERR("Unable to restore terminal settings (%d: %s).", __LINE__, strerror(errno));
216 }
217 return NULL;
218 } else {
219 response = newtext;
220 }
221 }
222 response[response_len++] = c;
223 }
224 /* terminating null byte */
225 response[response_len++] = '\0';
226
227 /* system ("stty echo"); */
228 if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) {
229 ERR("Unable to restore terminal settings (%d: %s).", __LINE__, strerror(errno));
230 /*
231 * terminal probably still hides input characters, but we have password
232 * and anyway we are unable to set terminal to the previous state, so
233 * just continue
234 */
235 }
236
237 fprintf(tty, "\n");
238 fclose(tty);
239 return response;
240}
241
242static char *
243sshauth_passphrase(const char* privkey_path)
244{
245 char c, *buf, *newbuf;
246 int buflen = 1024, len = 0;
247 struct termios newterm, oldterm;
248 FILE *tty;
249
250 buf = malloc(buflen * sizeof *buf);
251 if (!buf) {
252 ERR("Memory allocation failed (%s:%d - %s).", __FILE__, __LINE__, strerror(errno));
253 return NULL;
254 }
255
256 if (!(tty = fopen("/dev/tty", "r+"))) {
257 ERR("Unable to open the current terminal (%s:%d - %s).", __FILE__, __LINE__, strerror(errno));
258 return NULL;
259 }
260
261 if (tcgetattr(fileno(tty), &oldterm)) {
262 ERR("Unable to get terminal settings (%d: %s).", __LINE__, strerror(errno));
263 return NULL;
264 }
265
266 fprintf(tty, "Enter passphrase for the key '%s':", privkey_path);
267 fflush(tty);
268
269 /* system("stty -echo"); */
270 newterm = oldterm;
271 newterm.c_lflag &= ~ECHO;
272 newterm.c_lflag &= ~ICANON;
273 tcflush(fileno(tty), TCIFLUSH);
274 if (tcsetattr(fileno(tty), TCSANOW, &newterm)) {
275 ERR("Unable to change terminal settings for hiding password (%d: %s).", __LINE__, strerror(errno));
276 return NULL;
277 }
278
279 while ((fread(&c, 1, 1, tty) == 1) && (c != '\n')) {
280 if (len >= buflen - 1) {
281 buflen *= 2;
282 newbuf = realloc(buf, buflen * sizeof *newbuf);
283 if (!newbuf) {
284 ERRMEM;
285 /* remove content of the buffer */
286 memset(buf, 0, len);
287 free(buf);
288
289 /* restore terminal settings */
290 if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) {
291 ERR("Unable to restore terminal settings (%d: %s).", __LINE__, strerror(errno));
292 }
293
294 return NULL;
295 }
296 buf = newbuf;
297 }
298 buf[len++] = (char)c;
299 }
300 buf[len++] = 0; /* terminating null byte */
301
302 /* system ("stty echo"); */
303 if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) {
304 ERR("Unable to restore terminal settings (%d: %s).", __LINE__, strerror(errno));
305 /*
306 * terminal probably still hides input characters, but we have password
307 * and anyway we are unable to set terminal to the previous state, so
308 * just continue
309 */
310 }
311 fprintf(tty, "\n");
312
313 fclose(tty);
314 return buf;
315}
316
317/* TODO define this switch */
318#ifdef ENABLE_DNSSEC
319
320/* return 0 (DNSSEC + key valid), 1 (unsecure DNS + key valid), 2 (key not found or an error) */
321/* type - 1 (RSA), 2 (DSA), 3 (ECDSA); alg - 1 (SHA1), 2 (SHA-256) */
322static int
323sshauth_hostkey_hash_dnssec_check(const char *hostname, const char *sha1hash, int type, int alg) {
324 ns_msg handle;
325 ns_rr rr;
326 val_status_t val_status;
327 const unsigned char* rdata;
328 unsigned char buf[4096];
329 int buf_len = 4096;
330 int ret = 0, i, j, len;
331
332 /* class 1 - internet, type 44 - SSHFP */
333 len = val_res_query(NULL, hostname, 1, 44, buf, buf_len, &val_status);
334
335 if ((len < 0) || !val_istrusted(val_status)) {
336 ret = 2;
337 goto finish;
338 }
339
340 if (ns_initparse(buf, len, &handle) < 0) {
341 ERR("Failed to initialize DNSSEC response parser.");
342 ret = 2;
343 goto finish;
344 }
345
346 if ((i = libsres_msg_getflag(handle, ns_f_rcode))) {
347 ERR("DNSSEC query returned %d.", i);
348 ret = 2;
349 goto finish;
350 }
351
352 if (!libsres_msg_getflag(handle, ns_f_ad)) {
353 /* response not secured by DNSSEC */
354 ret = 1;
355 }
356
357 /* query section */
358 if (ns_parserr(&handle, ns_s_qd, 0, &rr)) {
359 ERROR("DNSSEC query section parser fail.");
360 ret = 2;
361 goto finish;
362 }
363
364 if (strcmp(hostname, ns_rr_name(rr)) || (ns_rr_type(rr) != 44) || (ns_rr_class(rr) != 1)) {
365 ERROR("DNSSEC query in the answer does not match the original query.");
366 ret = 2;
367 goto finish;
368 }
369
370 /* answer section */
371 i = 0;
372 while (!ns_parserr(&handle, ns_s_an, i, &rr)) {
373 if (ns_rr_type(rr) != 44) {
374 ++i;
375 continue;
376 }
377
378 rdata = ns_rr_rdata(rr);
379 if (rdata[0] != type) {
380 ++i;
381 continue;
382 }
383 if (rdata[1] != alg) {
384 ++i;
385 continue;
386 }
387
388 /* we found the correct SSHFP entry */
389 rdata += 2;
390 for (j = 0; j < 20; ++j) {
391 if (rdata[j] != (unsigned char)sha1hash[j]) {
392 ret = 2;
393 goto finish;
394 }
395 }
396
397 /* server fingerprint is supported by a DNS entry,
398 * we have already determined if DNSSEC was used or not
399 */
400 goto finish;
401 }
402
403 /* no match */
404 ret = 2;
405
406finish:
407 val_free_validator_state();
408 return ret;
409}
410
411#endif
412
413static int
414sshauth_hostkey_check(const char *hostname, ssh_session session)
415{
416 char *hexa;
417 int c, state, ret;
418 ssh_key srv_pubkey;
419 unsigned char *hash_sha1 = NULL;
420 size_t hlen;
421 enum ssh_keytypes_e srv_pubkey_type;
422 char answer[5];
423
424 state = ssh_is_server_known(session);
425
426 ret = ssh_get_publickey(session, &srv_pubkey);
427 if (ret < 0) {
428 ERR("Unable to get server public key.");
429 return EXIT_FAILURE;
430 }
431
432 srv_pubkey_type = ssh_key_type(srv_pubkey);
433 ret = ssh_get_publickey_hash(srv_pubkey, SSH_PUBLICKEY_HASH_SHA1, &hash_sha1, &hlen);
434 ssh_key_free(srv_pubkey);
435 if (ret < 0) {
436 ERR("Failed to calculate SHA1 hash of the server public key.");
437 return EXIT_FAILURE;
438 }
439
440 hexa = ssh_get_hexa(hash_sha1, hlen);
441
442 switch (state) {
443 case SSH_SERVER_KNOWN_OK:
444 break; /* ok */
445
446 case SSH_SERVER_KNOWN_CHANGED:
447 ERR("Remote host key changed, the connection will be terminated!");
448 goto fail;
449
450 case SSH_SERVER_FOUND_OTHER:
Michal Vasko086311b2016-01-08 09:53:11 +0100451 WRN("Remote host key is not known, but a key of another type for this host is known. Continue with caution.");
452 goto hostkey_not_known;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100453
454 case SSH_SERVER_FILE_NOT_FOUND:
455 WRN("Could not find the known hosts file.");
Michal Vasko086311b2016-01-08 09:53:11 +0100456 goto hostkey_not_known;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100457
458 case SSH_SERVER_NOT_KNOWN:
Michal Vasko086311b2016-01-08 09:53:11 +0100459hostkey_not_known:
Michal Vasko7b62fed2015-10-26 15:39:46 +0100460#ifdef ENABLE_DNSSEC
461 if ((srv_pubkey_type != SSH_KEYTYPE_UNKNOWN) || (srv_pubkey_type != SSH_KEYTYPE_RSA1)) {
462 if (srv_pubkey_type == SSH_KEYTYPE_DSS) {
463 ret = callback_ssh_hostkey_hash_dnssec_check(hostname, hash_sha1, 2, 1);
464 } else if (srv_pubkey_type == SSH_KEYTYPE_RSA) {
465 ret = callback_ssh_hostkey_hash_dnssec_check(hostname, hash_sha1, 1, 1);
466 } else if (srv_pubkey_type == SSH_KEYTYPE_ECDSA) {
467 ret = callback_ssh_hostkey_hash_dnssec_check(hostname, hash_sha1, 3, 1);
468 }
469
470 /* DNSSEC SSHFP check successful, that's enough */
471 if (!ret) {
472 DBG("DNSSEC SSHFP check successful");
473 ssh_write_knownhost(session);
474 ssh_clean_pubkey_hash(&hash_sha1);
475 ssh_string_free_char(hexa);
476 return EXIT_SUCCESS;
477 }
478 }
479#endif
480
481 /* try to get result from user */
482 fprintf(stdout, "The authenticity of the host \'%s\' cannot be established.\n", hostname);
483 fprintf(stdout, "%s key fingerprint is %s.\n", ssh_key_type_to_char(srv_pubkey_type), hexa);
484
485#ifdef ENABLE_DNSSEC
486 if (ret == 2) {
487 fprintf(stdout, "No matching host key fingerprint found in DNS.\n");
488 } else if (ret == 1) {
489 fprintf(stdout, "Matching host key fingerprint found in DNS.\n");
490 }
491#endif
492
493 fprintf(stdout, "Are you sure you want to continue connecting (yes/no)? ");
494
495 do {
496 if (fscanf(stdin, "%4s", answer) == EOF) {
497 ERR("fscanf() failed (%s).", strerror(errno));
498 goto fail;
499 }
500 while (((c = getchar()) != EOF) && (c != '\n'));
501
502 fflush(stdin);
503 if (!strcmp("yes", answer)) {
504 /* store the key into the host file */
505 ret = ssh_write_knownhost(session);
506 if (ret < 0) {
Michal Vaskoc111ac52015-12-08 14:36:36 +0100507 WRN("Adding the known host %s failed (%s).", hostname, ssh_get_error(session));
Michal Vasko7b62fed2015-10-26 15:39:46 +0100508 }
509 } else if (!strcmp("no", answer)) {
510 goto fail;
511 } else {
512 fprintf(stdout, "Please type 'yes' or 'no': ");
513 }
514 } while (strcmp(answer, "yes") && strcmp(answer, "no"));
515
516 break;
517
518 case SSH_SERVER_ERROR:
519 ssh_clean_pubkey_hash(&hash_sha1);
520 fprintf(stderr,"%s",ssh_get_error(session));
521 return -1;
522 }
523
524 ssh_clean_pubkey_hash(&hash_sha1);
525 ssh_string_free_char(hexa);
526 return EXIT_SUCCESS;
527
528fail:
529 ssh_clean_pubkey_hash(&hash_sha1);
530 ssh_string_free_char(hexa);
531 return EXIT_FAILURE;
532}
533
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100534API int
Michal Vasko086311b2016-01-08 09:53:11 +0100535nc_ssh_client_add_keypair(const char *pub_key, const char *priv_key)
Michal Vasko7b62fed2015-10-26 15:39:46 +0100536{
537 int i;
538 FILE *key;
539 char line[128];
540
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100541 if (!pub_key || !priv_key) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100542 return EXIT_FAILURE;
543 }
544
545 for (i = 0; i < ssh_opts.key_count; ++i) {
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100546 if (!strcmp(ssh_opts.keys[i].pubkey_path, pub_key) || !strcmp(ssh_opts.keys[i].privkey_path, priv_key)) {
547 if (strcmp(ssh_opts.keys[i].pubkey_path, pub_key)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100548 WRN("Private key \"%s\" found with another public key \"%s\".",
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100549 priv_key, ssh_opts.keys[i].pubkey_path);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100550 continue;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100551 } else if (strcmp(ssh_opts.keys[i].privkey_path, priv_key)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100552 WRN("Public key \"%s\" found with another private key \"%s\".",
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100553 pub_key, ssh_opts.keys[i].privkey_path);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100554 continue;
555 }
556
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100557 ERR("SSH key pair already set.");
558 return EXIT_FAILURE;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100559 }
560 }
561
Michal Vasko7b62fed2015-10-26 15:39:46 +0100562 /* add the keys safely */
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100563 ++ssh_opts.key_count;
564 ssh_opts.keys = realloc(ssh_opts.keys, ssh_opts.key_count * sizeof *ssh_opts.keys);
565 ssh_opts.keys[ssh_opts.key_count - 1].pubkey_path = strdup(pub_key);
566 ssh_opts.keys[ssh_opts.key_count - 1].privkey_path = strdup(priv_key);
567 ssh_opts.keys[ssh_opts.key_count - 1].privkey_crypt = 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100568
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100569 /* check encryption */
570 if ((key = fopen(priv_key, "r"))) {
571 /* 1st line - key type */
572 if (!fgets(line, sizeof line, key)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100573 fclose(key);
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100574 ERR("fgets() on %s failed.", priv_key);
575 return EXIT_FAILURE;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100576 }
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100577 /* 2nd line - encryption information or key */
578 if (!fgets(line, sizeof line, key)) {
579 fclose(key);
580 ERR("fgets() on %s failed.", priv_key);
581 return EXIT_FAILURE;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100582 }
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100583 fclose(key);
584 if (strcasestr(line, "encrypted")) {
585 ssh_opts.keys[ssh_opts.key_count - 1].privkey_crypt = 1;
586 }
Michal Vasko7b62fed2015-10-26 15:39:46 +0100587 }
588
589 return EXIT_SUCCESS;
590}
591
592API int
Michal Vasko086311b2016-01-08 09:53:11 +0100593nc_ssh_client_del_keypair(int idx)
Michal Vasko7b62fed2015-10-26 15:39:46 +0100594{
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100595 if (idx >= ssh_opts.key_count) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100596 return EXIT_FAILURE;
597 }
598
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100599 free(ssh_opts.keys[idx].pubkey_path);
600 free(ssh_opts.keys[idx].privkey_path);
601
602 --ssh_opts.key_count;
603
604 memmove(ssh_opts.keys + idx, ssh_opts.keys + idx + 1, (ssh_opts.key_count - idx) * sizeof *ssh_opts.keys);
605 ssh_opts.keys = realloc(ssh_opts.keys, ssh_opts.key_count * sizeof *ssh_opts.keys);
606
Michal Vasko7b62fed2015-10-26 15:39:46 +0100607 return EXIT_SUCCESS;
608}
609
610API int
Michal Vasko086311b2016-01-08 09:53:11 +0100611nc_ssh_client_get_keypair_count(void)
Michal Vasko7b62fed2015-10-26 15:39:46 +0100612{
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100613 return ssh_opts.key_count;
614}
615
616API int
Michal Vasko086311b2016-01-08 09:53:11 +0100617nc_ssh_client_get_keypair(int idx, const char **pub_key, const char **priv_key)
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100618{
619 if (idx >= ssh_opts.key_count) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100620 return EXIT_FAILURE;
621 }
622
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100623 if (pub_key) {
624 *pub_key = ssh_opts.keys[idx].pubkey_path;
625 }
626 if (priv_key) {
627 *priv_key = ssh_opts.keys[idx].privkey_path;
628 }
629
Michal Vasko7b62fed2015-10-26 15:39:46 +0100630 return EXIT_SUCCESS;
631}
632
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100633API void
Michal Vasko086311b2016-01-08 09:53:11 +0100634nc_ssh_client_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, short int pref)
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100635{
636 if (pref < 0) {
637 pref = -1;
638 }
639
640 if (auth_type == NC_SSH_AUTH_INTERACTIVE) {
641 ssh_opts.auth_pref[0].value = pref;
642 } else if (auth_type == NC_SSH_AUTH_PASSWORD) {
643 ssh_opts.auth_pref[1].value = pref;
644 } else if (auth_type == NC_SSH_AUTH_PUBLICKEY) {
645 ssh_opts.auth_pref[2].value = pref;
646 }
647}
648
649API short int
Michal Vasko086311b2016-01-08 09:53:11 +0100650nc_ssh_client_get_auth_pref(NC_SSH_AUTH_TYPE auth_type)
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100651{
652 short int pref = 0;
653
654 if (auth_type == NC_SSH_AUTH_INTERACTIVE) {
655 pref = ssh_opts.auth_pref[0].value;
656 } else if (auth_type == NC_SSH_AUTH_PASSWORD) {
657 pref = ssh_opts.auth_pref[1].value;
658 } else if (auth_type == NC_SSH_AUTH_PUBLICKEY) {
659 pref = ssh_opts.auth_pref[2].value;
660 }
661
662 return pref;
663}
664
Michal Vasko7b62fed2015-10-26 15:39:46 +0100665/* Establish a secure SSH connection, authenticate, and create a channel with the 'netconf' subsystem.
666 * Host, port, username, and a connected socket is expected to be set.
667 */
668static int
669connect_ssh_session_netconf(struct nc_session *session)
670{
Michal Vasko235efdc2015-12-17 12:05:04 +0100671 int j, ret_auth, userauthlist;
672 NC_SSH_AUTH_TYPE auth;
673 short int pref;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100674 const char* prompt;
675 char *s, *answer, echo;
676 ssh_key pubkey, privkey;
677 ssh_session ssh_sess;
678
679 ssh_sess = session->ti.libssh.session;
680
681 if (ssh_connect(ssh_sess) != SSH_OK) {
682 ERR("Starting the SSH session failed (%s)", ssh_get_error(ssh_sess));
683 DBG("Error code %d.", ssh_get_error_code(ssh_sess));
684 return EXIT_FAILURE;
685 }
686
687 if (sshauth_hostkey_check(session->host, ssh_sess)) {
688 ERR("Checking the host key failed.");
689 return EXIT_FAILURE;
690 }
691
692 if ((ret_auth = ssh_userauth_none(ssh_sess, NULL)) == SSH_AUTH_ERROR) {
693 ERR("Authentication failed (%s).", ssh_get_error(ssh_sess));
694 return EXIT_FAILURE;
695 }
696
697 /* check what authentication methods are available */
698 userauthlist = ssh_userauth_list(ssh_sess, NULL);
Michal Vasko235efdc2015-12-17 12:05:04 +0100699
700 /* remove those disabled */
701 if (ssh_opts.auth_pref[0].value < 0) {
702 VRB("Interactive SSH authentication method was disabled.");
703 userauthlist &= ~SSH_AUTH_METHOD_INTERACTIVE;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100704 }
Michal Vasko235efdc2015-12-17 12:05:04 +0100705 if (ssh_opts.auth_pref[1].value < 0) {
706 VRB("Password SSH authentication method was disabled.");
707 userauthlist &= ~SSH_AUTH_METHOD_PASSWORD;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100708 }
Michal Vasko235efdc2015-12-17 12:05:04 +0100709 if (ssh_opts.auth_pref[2].value < 0) {
710 VRB("Publickey SSH authentication method was disabled.");
711 userauthlist &= ~SSH_AUTH_METHOD_PUBLICKEY;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100712 }
713
Michal Vasko235efdc2015-12-17 12:05:04 +0100714 while (ret_auth != SSH_AUTH_SUCCESS) {
715 auth = 0;
716 pref = 0;
717 if (userauthlist & SSH_AUTH_METHOD_INTERACTIVE) {
718 auth = NC_SSH_AUTH_INTERACTIVE;
719 pref = ssh_opts.auth_pref[0].value;
720 }
721 if ((userauthlist & SSH_AUTH_METHOD_PASSWORD) && (ssh_opts.auth_pref[1].value > pref)) {
722 auth = NC_SSH_AUTH_PASSWORD;
723 pref = ssh_opts.auth_pref[1].value;
724 }
725 if ((userauthlist & SSH_AUTH_METHOD_PUBLICKEY) && (ssh_opts.auth_pref[2].value > pref)) {
726 auth = NC_SSH_AUTH_PUBLICKEY;
727 pref = ssh_opts.auth_pref[2].value;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100728 }
729
Michal Vasko235efdc2015-12-17 12:05:04 +0100730 if (!auth) {
731 ERR("Unable to authenticate to the remote server (No supported authentication methods left).");
732 break;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100733 }
734
735 /* found common authentication method */
Michal Vasko235efdc2015-12-17 12:05:04 +0100736 switch (auth) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100737 case NC_SSH_AUTH_PASSWORD:
Michal Vasko235efdc2015-12-17 12:05:04 +0100738 userauthlist &= ~SSH_AUTH_METHOD_PASSWORD;
739
Michal Vasko7b62fed2015-10-26 15:39:46 +0100740 VRB("Password authentication (host %s, user %s)", session->host, session->username);
741 s = sshauth_password(session->username, session->host);
742 if ((ret_auth = ssh_userauth_password(ssh_sess, session->username, s)) != SSH_AUTH_SUCCESS) {
743 memset(s, 0, strlen(s));
744 VRB("Authentication failed (%s)", ssh_get_error(ssh_sess));
745 }
746 free(s);
747 break;
748 case NC_SSH_AUTH_INTERACTIVE:
Michal Vasko235efdc2015-12-17 12:05:04 +0100749 userauthlist &= ~SSH_AUTH_METHOD_INTERACTIVE;
750
Michal Vasko7b62fed2015-10-26 15:39:46 +0100751 VRB("Keyboard-interactive authentication");
752 while ((ret_auth = ssh_userauth_kbdint(ssh_sess, NULL, NULL)) == SSH_AUTH_INFO) {
753 for (j = 0; j < ssh_userauth_kbdint_getnprompts(ssh_sess); ++j) {
754 prompt = ssh_userauth_kbdint_getprompt(ssh_sess, j, &echo);
755 if (prompt == NULL) {
756 break;
757 }
758 answer = sshauth_interactive(ssh_userauth_kbdint_getname(ssh_sess),
759 ssh_userauth_kbdint_getinstruction(ssh_sess),
760 prompt, echo);
761 if (ssh_userauth_kbdint_setanswer(ssh_sess, j, answer) < 0) {
762 free(answer);
763 break;
764 }
765 free(answer);
766 }
767 }
768
769 if (ret_auth == SSH_AUTH_ERROR) {
770 VRB("Authentication failed (%s)", ssh_get_error(ssh_sess));
771 }
772
773 break;
Michal Vasko206d3b12015-12-04 11:08:42 +0100774 case NC_SSH_AUTH_PUBLICKEY:
Michal Vasko235efdc2015-12-17 12:05:04 +0100775 userauthlist &= ~SSH_AUTH_METHOD_PUBLICKEY;
776
Michal Vasko7b62fed2015-10-26 15:39:46 +0100777 VRB("Publickey athentication");
778
779 /* if publickeys path not provided, we cannot continue */
780 if (!ssh_opts.key_count) {
781 VRB("No key pair specified.");
782 break;
783 }
784
785 for (j = 0; j < ssh_opts.key_count; j++) {
786 VRB("Trying to authenticate using %spair %s %s",
787 ssh_opts.keys[j].privkey_crypt ? "password-protected " : "", ssh_opts.keys[j].privkey_path,
788 ssh_opts.keys[j].pubkey_path);
789
790 if (ssh_pki_import_pubkey_file(ssh_opts.keys[j].pubkey_path, &pubkey) != SSH_OK) {
791 WRN("Failed to import the key \"%s\".", ssh_opts.keys[j].pubkey_path);
792 continue;
793 }
794 ret_auth = ssh_userauth_try_publickey(ssh_sess, NULL, pubkey);
795 if ((ret_auth == SSH_AUTH_DENIED) || (ret_auth == SSH_AUTH_PARTIAL)) {
796 ssh_key_free(pubkey);
797 continue;
798 }
799 if (ret_auth == SSH_AUTH_ERROR) {
800 ERR("Authentication failed (%s)", ssh_get_error(ssh_sess));
801 ssh_key_free(pubkey);
802 break;
803 }
804
805 if (ssh_opts.keys[j].privkey_crypt) {
806 s = sshauth_passphrase(ssh_opts.keys[j].privkey_path);
807 } else {
808 s = NULL;
809 }
810
811 if (ssh_pki_import_privkey_file(ssh_opts.keys[j].privkey_path, s, NULL, NULL, &privkey) != SSH_OK) {
812 WRN("Failed to import the key \"%s\".", ssh_opts.keys[j].privkey_path);
813 if (s) {
814 memset(s, 0, strlen(s));
815 free(s);
816 }
817 ssh_key_free(pubkey);
818 continue;
819 }
820
821 if (s) {
822 memset(s, 0, strlen(s));
823 free(s);
824 }
825
826 ret_auth = ssh_userauth_publickey(ssh_sess, NULL, privkey);
827 ssh_key_free(pubkey);
828 ssh_key_free(privkey);
829
830 if (ret_auth == SSH_AUTH_ERROR) {
831 ERR("Authentication failed (%s)", ssh_get_error(ssh_sess));
832 }
833 if (ret_auth == SSH_AUTH_SUCCESS) {
834 break;
835 }
836 }
837 break;
838 }
Michal Vasko7b62fed2015-10-26 15:39:46 +0100839 }
840
841 /* check a state of authentication */
842 if (ret_auth != SSH_AUTH_SUCCESS) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100843 return EXIT_FAILURE;
844 }
845
846 /* open a channel */
847 session->ti.libssh.channel = ssh_channel_new(ssh_sess);
848 if (ssh_channel_open_session(session->ti.libssh.channel) != SSH_OK) {
849 ssh_channel_free(session->ti.libssh.channel);
850 session->ti.libssh.channel = NULL;
851 ERR("Opening an SSH channel failed (%s)", ssh_get_error(ssh_sess));
852 return EXIT_FAILURE;
853 }
854
855 /* execute the NETCONF subsystem on the channel */
856 if (ssh_channel_request_subsystem(session->ti.libssh.channel, "netconf") != SSH_OK) {
857 ssh_channel_free(session->ti.libssh.channel);
858 session->ti.libssh.channel = NULL;
859 ERR("Starting the \"netconf\" SSH subsystem failed (%s)", ssh_get_error(ssh_sess));
860 return EXIT_FAILURE;
861 }
862
863 return EXIT_SUCCESS;
Radek Krejciac6d3472015-10-22 15:47:18 +0200864}
865
866API struct nc_session *
Michal Vaskoc111ac52015-12-08 14:36:36 +0100867nc_connect_ssh(const char *host, uint16_t port, const char *username, struct ly_ctx *ctx)
Radek Krejciac6d3472015-10-22 15:47:18 +0200868{
Michal Vaskoc111ac52015-12-08 14:36:36 +0100869 const int timeout = NC_SSH_TIMEOUT;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100870 int sock;
Radek Krejciac6d3472015-10-22 15:47:18 +0200871 struct passwd *pw;
872 struct nc_session *session = NULL;
873
874 /* process parameters */
875 if (!host || strisempty(host)) {
876 host = "localhost";
877 }
878
879 if (!port) {
880 port = NC_PORT_SSH;
881 }
882
883 if (!username) {
884 pw = getpwuid(getuid());
885 if (!pw) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100886 ERR("Unknown username for the SSH connection (%s).", strerror(errno));
887 return NULL;
Radek Krejciac6d3472015-10-22 15:47:18 +0200888 } else {
889 username = pw->pw_name;
890 }
891 }
892
893 /* prepare session structure */
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100894 session = calloc(1, sizeof *session);
Radek Krejciac6d3472015-10-22 15:47:18 +0200895 if (!session) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100896 ERRMEM;
Radek Krejciac6d3472015-10-22 15:47:18 +0200897 return NULL;
898 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100899 session->status = NC_STATUS_STARTING;
900 session->side = NC_CLIENT;
Radek Krejciac6d3472015-10-22 15:47:18 +0200901
Michal Vasko7b62fed2015-10-26 15:39:46 +0100902 /* transport lock */
903 session->ti_lock = malloc(sizeof *session->ti_lock);
904 if (!session->ti_lock) {
905 ERRMEM;
906 goto fail;
907 }
908 pthread_mutex_init(session->ti_lock, NULL);
909
910 /* other transport-specific data */
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100911 session->ti_type = NC_TI_LIBSSH;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100912 session->ti.libssh.session = ssh_new();
913 if (!session->ti.libssh.session) {
914 ERR("Unable to initialize SSH session.");
915 goto fail;
916 }
Radek Krejciac6d3472015-10-22 15:47:18 +0200917
Michal Vasko7b62fed2015-10-26 15:39:46 +0100918 /* set some basic SSH session options */
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100919 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOST, host);
920 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_PORT, &port);
921 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_USER, username);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100922 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_TIMEOUT, &timeout);
Michal Vasko086311b2016-01-08 09:53:11 +0100923 if (ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOSTKEYS,
924 "ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,"
925 "ecdsa-sha2-nistp256,ssh-rsa,ssh-dss,ssh-rsa1")) {
926 /* ecdsa is probably not supported... */
927 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOSTKEYS, "ssh-ed25519,ssh-rsa,ssh-dss,ssh-rsa1");
928 }
Michal Vasko7b62fed2015-10-26 15:39:46 +0100929
930 /* create and assign communication socket */
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100931 sock = nc_connect_getsocket(host, port);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100932 if (sock == -1) {
933 goto fail;
934 }
935 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_FD, &sock);
936
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100937 /* temporarily, for session connection */
938 session->host = host;
939 session->username = username;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100940 if (connect_ssh_session_netconf(session)) {
941 goto fail;
Radek Krejciac6d3472015-10-22 15:47:18 +0200942 }
943
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100944 /* assign context (dicionary needed for handshake) */
945 if (!ctx) {
946 ctx = ly_ctx_new(SCHEMAS_DIR);
947 } else {
948 session->flags |= NC_SESSION_SHAREDCTX;
949 }
950 session->ctx = ctx;
951
Radek Krejciac6d3472015-10-22 15:47:18 +0200952 /* NETCONF handshake */
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100953 if (nc_handshake(session)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100954 goto fail;
Radek Krejciac6d3472015-10-22 15:47:18 +0200955 }
Michal Vaskoad611702015-12-03 13:41:51 +0100956 session->status = NC_STATUS_RUNNING;
Radek Krejciac6d3472015-10-22 15:47:18 +0200957
Michal Vasko57eb9402015-12-08 14:38:12 +0100958 if (nc_ctx_check_and_fill(session)) {
959 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100960 }
961
962 /* store information into the dictionary */
963 session->host = lydict_insert(ctx, host, 0);
964 session->port = port;
965 session->username = lydict_insert(ctx, username, 0);
966
Radek Krejciac6d3472015-10-22 15:47:18 +0200967 return session;
968
Michal Vasko7b62fed2015-10-26 15:39:46 +0100969fail:
Radek Krejciac6d3472015-10-22 15:47:18 +0200970 nc_session_free(session);
971 return NULL;
972}
973
974API struct nc_session *
975nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx)
976{
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100977 char *host = NULL, *username = NULL;
978 unsigned short port = 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100979 int sock;
980 struct passwd *pw;
981 struct nc_session *session = NULL;
Radek Krejciac6d3472015-10-22 15:47:18 +0200982
Michal Vasko7b62fed2015-10-26 15:39:46 +0100983 /* prepare session structure */
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100984 session = calloc(1, sizeof *session);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100985 if (!session) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100986 ERRMEM;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100987 return NULL;
988 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100989 session->status = NC_STATUS_STARTING;
990 session->side = NC_CLIENT;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100991
992 /* transport lock */
993 session->ti_lock = malloc(sizeof *session->ti_lock);
994 if (!session->ti_lock) {
995 ERRMEM;
996 goto fail;
997 }
998 pthread_mutex_init(session->ti_lock, NULL);
999
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001000 session->ti_type = NC_TI_LIBSSH;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001001 session->ti.libssh.session = ssh_session;
1002
Michal Vasko745ff832015-12-08 14:40:29 +01001003 /* was port set? */
1004 ssh_options_get_port(ssh_session, (unsigned int *)&port);
1005
1006 if (ssh_options_get(ssh_session, SSH_OPTIONS_HOST, &host) != SSH_OK) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001007 /*
Michal Vasko745ff832015-12-08 14:40:29 +01001008 * There is no file descriptor (detected based on the host, there is no way to check
1009 * the SSH_OPTIONS_FD directly :/), we need to create it. (TCP/IP layer)
Michal Vasko7b62fed2015-10-26 15:39:46 +01001010 */
1011
Michal Vasko7b62fed2015-10-26 15:39:46 +01001012 /* remember host */
Michal Vasko745ff832015-12-08 14:40:29 +01001013 host = strdup("localhost");
1014 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOST, host);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001015
1016 /* create and connect socket */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001017 sock = nc_connect_getsocket(host, port);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001018 if (sock == -1) {
1019 goto fail;
1020 }
1021 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_FD, &sock);
1022 }
1023
Michal Vasko745ff832015-12-08 14:40:29 +01001024 /* was username set? */
1025 ssh_options_get(ssh_session, SSH_OPTIONS_USER, &username);
1026
Michal Vasko7b62fed2015-10-26 15:39:46 +01001027 if (!ssh_is_connected(ssh_session)) {
1028 /*
1029 * We are connected, but not SSH authenticated. (Transport layer)
1030 */
1031
Michal Vasko7b62fed2015-10-26 15:39:46 +01001032 /* remember username */
1033 if (!username) {
1034 pw = getpwuid(getuid());
1035 if (!pw) {
1036 ERR("Unknown username for the SSH connection (%s).", strerror(errno));
1037 goto fail;
1038 }
1039
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001040 username = strdup(pw->pw_name);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001041 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_USER, username);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001042 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001043
1044 /* authenticate SSH session */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001045 session->host = host;
1046 session->username = username;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001047 if (connect_ssh_session_netconf(session)) {
1048 goto fail;
1049 }
1050 }
1051
1052 /*
1053 * SSH session is established, create NETCONF session. (Application layer)
1054 */
1055
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001056 /* assign context (dicionary needed for handshake) */
1057 if (!ctx) {
1058 ctx = ly_ctx_new(SCHEMAS_DIR);
1059 } else {
1060 session->flags |= NC_SESSION_SHAREDCTX;
1061 }
1062 session->ctx = ctx;
1063
Michal Vasko7b62fed2015-10-26 15:39:46 +01001064 /* NETCONF handshake */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001065 if (nc_handshake(session)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001066 goto fail;
1067 }
Michal Vaskoad611702015-12-03 13:41:51 +01001068 session->status = NC_STATUS_RUNNING;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001069
Michal Vasko57eb9402015-12-08 14:38:12 +01001070 if (nc_ctx_check_and_fill(session)) {
1071 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001072 }
1073
1074 /* store information into the dictionary */
1075 if (host) {
1076 session->host = lydict_insert_zc(ctx, host);
1077 }
1078 if (port) {
1079 session->port = port;
1080 }
1081 if (username) {
1082 session->username = lydict_insert_zc(ctx, username);
1083 }
1084
Michal Vasko7b62fed2015-10-26 15:39:46 +01001085 return session;
1086
1087fail:
1088 nc_session_free(session);
Radek Krejciac6d3472015-10-22 15:47:18 +02001089 return NULL;
1090}
1091
1092API struct nc_session *
Michal Vasko7b62fed2015-10-26 15:39:46 +01001093nc_connect_ssh_channel(struct nc_session *session, struct ly_ctx *ctx)
Radek Krejciac6d3472015-10-22 15:47:18 +02001094{
Michal Vasko7b62fed2015-10-26 15:39:46 +01001095 struct nc_session *new_session, *ptr;
Radek Krejciac6d3472015-10-22 15:47:18 +02001096
Michal Vasko7b62fed2015-10-26 15:39:46 +01001097 /* prepare session structure */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001098 new_session = calloc(1, sizeof *new_session);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001099 if (!new_session) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001100 ERRMEM;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001101 return NULL;
1102 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001103 new_session->status = NC_STATUS_STARTING;
1104 new_session->side = NC_CLIENT;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001105
1106 /* share some parameters including the session lock */
1107 new_session->ti_type = NC_TI_LIBSSH;
1108 new_session->ti_lock = session->ti_lock;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001109 new_session->ti.libssh.session = session->ti.libssh.session;
1110
1111 /* create the channel safely */
1112 pthread_mutex_lock(new_session->ti_lock);
1113
1114 /* open a channel */
1115 new_session->ti.libssh.channel = ssh_channel_new(new_session->ti.libssh.session);
1116 if (ssh_channel_open_session(new_session->ti.libssh.channel) != SSH_OK) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001117 ERR("Opening an SSH channel failed (%s)", ssh_get_error(session->ti.libssh.session));
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001118 goto fail;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001119 }
1120 /* execute the NETCONF subsystem on the channel */
1121 if (ssh_channel_request_subsystem(new_session->ti.libssh.channel, "netconf") != SSH_OK) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001122 ERR("Starting the \"netconf\" SSH subsystem failed (%s)", ssh_get_error(session->ti.libssh.session));
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001123 goto fail;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001124 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001125
1126 /* assign context (dicionary needed for handshake) */
1127 if (!ctx) {
1128 ctx = ly_ctx_new(SCHEMAS_DIR);
1129 } else {
1130 session->flags |= NC_SESSION_SHAREDCTX;
1131 }
1132 session->ctx = ctx;
1133
Michal Vasko7b62fed2015-10-26 15:39:46 +01001134 /* NETCONF handshake */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001135 if (nc_handshake(new_session)) {
1136 goto fail;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001137 }
Michal Vaskoad611702015-12-03 13:41:51 +01001138 new_session->status = NC_STATUS_RUNNING;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001139
Michal Vasko57eb9402015-12-08 14:38:12 +01001140 if (nc_ctx_check_and_fill(session)) {
1141 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001142 }
1143
1144 /* store information into session and the dictionary */
1145 session->host = lydict_insert(ctx, session->host, 0);
1146 session->port = session->port;
1147 session->username = lydict_insert(ctx, session->username, 0);
1148
Michal Vasko7b62fed2015-10-26 15:39:46 +01001149 pthread_mutex_unlock(new_session->ti_lock);
1150
1151 /* append to the session ring list */
1152 if (!session->ti.libssh.next) {
1153 session->ti.libssh.next = new_session;
1154 new_session->ti.libssh.next = session;
1155 } else {
1156 ptr = session->ti.libssh.next;
1157 session->ti.libssh.next = new_session;
1158 new_session->ti.libssh.next = ptr;
1159 }
1160
1161 return new_session;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001162
1163fail:
1164 nc_session_free(new_session);
1165 return NULL;
Radek Krejciac6d3472015-10-22 15:47:18 +02001166}
Michal Vasko80cad7f2015-12-08 14:42:27 +01001167
1168API struct nc_session *
Michal Vasko9e036d52016-01-08 10:49:26 +01001169nc_callhome_accept_ssh(uint16_t port, const char *username, int timeout, struct ly_ctx *ctx)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001170{
1171 const int ssh_timeout = NC_SSH_TIMEOUT;
1172 int sock;
1173 char *server_host;
1174 ssh_session sess;
1175
1176 if (!port) {
1177 port = NC_PORT_CH_SSH;
1178 }
1179
1180 sock = nc_callhome_accept_connection(port, timeout, NULL, &server_host);
1181 if (sock == -1) {
1182 return NULL;
1183 }
1184
1185 sess = ssh_new();
1186 if (!sess) {
1187 ERR("Unable to initialize an SSH session.");
1188 close(sock);
1189 return NULL;
1190 }
1191
1192 ssh_options_set(sess, SSH_OPTIONS_FD, &sock);
1193 ssh_options_set(sess, SSH_OPTIONS_HOST, server_host);
1194 ssh_options_set(sess, SSH_OPTIONS_PORT, &port);
1195 ssh_options_set(sess, SSH_OPTIONS_TIMEOUT, &ssh_timeout);
1196 if (username) {
1197 ssh_options_set(sess, SSH_OPTIONS_USER, username);
1198 }
Michal Vasko086311b2016-01-08 09:53:11 +01001199 if (ssh_options_set(sess, SSH_OPTIONS_HOSTKEYS,
1200 "ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,"
1201 "ecdsa-sha2-nistp256,ssh-rsa,ssh-dss,ssh-rsa1")) {
1202 /* ecdsa is probably not supported... */
1203 ssh_options_set(sess, SSH_OPTIONS_HOSTKEYS, "ssh-ed25519,ssh-rsa,ssh-dss,ssh-rsa1");
1204 }
Michal Vasko80cad7f2015-12-08 14:42:27 +01001205
1206 free(server_host);
1207
1208 return nc_connect_libssh(sess, ctx);
1209}