blob: b2d6b612531ff95b7f849e446b1939edeb12439b [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
Michal Vaskoe22c6732016-01-29 11:03:02 +010049#include "session_client.h"
50#include "session_client_ch.h"
Radek Krejciac6d3472015-10-22 15:47:18 +020051#include "libnetconf.h"
52
Michal Vasko3031aae2016-01-27 16:07:18 +010053static struct nc_client_ssh_opts ssh_opts = {
Michal Vasko206d3b12015-12-04 11:08:42 +010054 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 3}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 1}}
Michal Vasko7b62fed2015-10-26 15:39:46 +010055};
Radek Krejciac6d3472015-10-22 15:47:18 +020056
Michal Vasko3031aae2016-01-27 16:07:18 +010057static struct nc_client_ssh_opts ssh_ch_opts = {
58 .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 1}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 3}}
59};
60
Michal Vaskoe22c6732016-01-29 11:03:02 +010061static void
62_nc_client_ssh_destroy_opts(struct nc_client_ssh_opts *opts)
Michal Vasko990089e2015-10-27 15:05:25 +010063{
64 int i;
65
Michal Vaskoe22c6732016-01-29 11:03:02 +010066 for (i = 0; i < opts->key_count; ++i) {
67 free(opts->keys[i].pubkey_path);
68 free(opts->keys[i].privkey_path);
Michal Vasko990089e2015-10-27 15:05:25 +010069 }
Michal Vaskoe22c6732016-01-29 11:03:02 +010070 free(opts->keys);
71 free(opts->username);
72}
Michal Vasko990089e2015-10-27 15:05:25 +010073
Michal Vaskoe22c6732016-01-29 11:03:02 +010074API void
75nc_client_ssh_destroy_opts(void)
76{
77 _nc_client_ssh_destroy_opts(&ssh_opts);
78 _nc_client_ssh_destroy_opts(&ssh_ch_opts);
Michal Vasko990089e2015-10-27 15:05:25 +010079}
80
Michal Vasko7b62fed2015-10-26 15:39:46 +010081static char *
82sshauth_password(const char *username, const char *hostname)
Radek Krejciac6d3472015-10-22 15:47:18 +020083{
Michal Vasko7b62fed2015-10-26 15:39:46 +010084 char *buf, *newbuf;
85 int buflen = 1024, len = 0;
86 char c = 0;
87 struct termios newterm, oldterm;
88 FILE *tty;
Radek Krejciac6d3472015-10-22 15:47:18 +020089
Michal Vasko7b62fed2015-10-26 15:39:46 +010090 if (!(tty = fopen("/dev/tty", "r+"))) {
Michal Vaskod083db62016-01-19 10:31:29 +010091 ERR("Unable to open the current terminal (%s).", strerror(errno));
Michal Vasko7b62fed2015-10-26 15:39:46 +010092 return NULL;
Radek Krejciac6d3472015-10-22 15:47:18 +020093 }
94
Michal Vasko7b62fed2015-10-26 15:39:46 +010095 if (tcgetattr(fileno(tty), &oldterm)) {
Michal Vaskod083db62016-01-19 10:31:29 +010096 ERR("Unable to get terminal settings (%s).", strerror(errno));
Michal Vasko11d142a2016-01-19 15:58:24 +010097 fclose(tty);
Michal Vasko7b62fed2015-10-26 15:39:46 +010098 return NULL;
99 }
Radek Krejciac6d3472015-10-22 15:47:18 +0200100
Michal Vasko7b62fed2015-10-26 15:39:46 +0100101 fprintf(tty, "%s@%s password: ", username, hostname);
102 fflush(tty);
Radek Krejciac6d3472015-10-22 15:47:18 +0200103
Michal Vasko7b62fed2015-10-26 15:39:46 +0100104 /* system("stty -echo"); */
105 newterm = oldterm;
106 newterm.c_lflag &= ~ECHO;
107 newterm.c_lflag &= ~ICANON;
108 tcflush(fileno(tty), TCIFLUSH);
109 if (tcsetattr(fileno(tty), TCSANOW, &newterm)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100110 ERR("Unable to change terminal settings for hiding password (%s).", strerror(errno));
Michal Vasko11d142a2016-01-19 15:58:24 +0100111 fclose(tty);
112 return NULL;
113 }
114
115 buf = malloc(buflen * sizeof *buf);
116 if (!buf) {
117 ERRMEM;
118 fclose(tty);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100119 return NULL;
120 }
121
122 while ((fread(&c, 1, 1, tty) == 1) && (c != '\n')) {
123 if (len >= buflen - 1) {
124 buflen *= 2;
125 newbuf = realloc(buf, buflen * sizeof *newbuf);
126 if (!newbuf) {
Michal Vaskod083db62016-01-19 10:31:29 +0100127 ERRMEM;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100128
129 /* remove content of the buffer */
130 memset(buf, 0, len);
131 free(buf);
132
133 /* restore terminal settings */
134 if (tcsetattr(fileno(tty), TCSANOW, &oldterm) != 0) {
Michal Vaskod083db62016-01-19 10:31:29 +0100135 ERR("Unable to restore terminal settings (%s).", strerror(errno));
Michal Vasko7b62fed2015-10-26 15:39:46 +0100136 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100137 fclose(tty);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100138 return NULL;
139 } else {
140 buf = newbuf;
141 }
142 }
143 buf[len++] = c;
144 }
145 buf[len++] = 0; /* terminating null byte */
146
147 /* system ("stty echo"); */
148 if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100149 ERR("Unable to restore terminal settings (%s).", strerror(errno));
Michal Vasko7b62fed2015-10-26 15:39:46 +0100150 /*
151 * terminal probably still hides input characters, but we have password
152 * and anyway we are unable to set terminal to the previous state, so
153 * just continue
154 */
155 }
156 fprintf(tty, "\n");
157
158 fclose(tty);
159 return buf;
160}
161
162static char *
163sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo)
164{
165 unsigned int buflen = 8, response_len;
166 char c = 0;
167 struct termios newterm, oldterm;
168 char *newtext, *response;
169 FILE *tty;
170
171 if (!(tty = fopen("/dev/tty", "r+"))) {
Michal Vaskod083db62016-01-19 10:31:29 +0100172 ERR("Unable to open the current terminal (%s).", strerror(errno));
Michal Vasko7b62fed2015-10-26 15:39:46 +0100173 return NULL;
174 }
175
176 if (tcgetattr(fileno(tty), &oldterm) != 0) {
Michal Vaskod083db62016-01-19 10:31:29 +0100177 ERR("Unable to get terminal settings (%s).", strerror(errno));
Michal Vasko11d142a2016-01-19 15:58:24 +0100178 fclose(tty);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100179 return NULL;
180 }
181
182 if (auth_name && (!fwrite(auth_name, sizeof(char), strlen(auth_name), tty)
183 || !fwrite("\n", sizeof(char), 1, tty))) {
184 ERR("Writing the auth method name into stdout failed.");
Michal Vasko11d142a2016-01-19 15:58:24 +0100185 fclose(tty);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100186 return NULL;
187 }
188
189 if (instruction && (!fwrite(instruction, sizeof(char), strlen(instruction), tty)
190 || !fwrite("\n", sizeof(char), 1, tty))) {
191 ERR("Writing the instruction into stdout failed.");
Michal Vasko11d142a2016-01-19 15:58:24 +0100192 fclose(tty);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100193 return NULL;
194 }
195
196 if (!fwrite(prompt, sizeof(char), strlen(prompt), tty)) {
197 ERR("Writing the authentication prompt into stdout failed.");
Michal Vasko11d142a2016-01-19 15:58:24 +0100198 fclose(tty);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100199 return NULL;
200 }
201 fflush(tty);
202 if (!echo) {
203 /* system("stty -echo"); */
204 newterm = oldterm;
205 newterm.c_lflag &= ~ECHO;
206 tcflush(fileno(tty), TCIFLUSH);
207 if (tcsetattr(fileno(tty), TCSANOW, &newterm)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100208 ERR("Unable to change terminal settings for hiding password (%s).", strerror(errno));
Michal Vasko11d142a2016-01-19 15:58:24 +0100209 fclose(tty);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100210 return NULL;
211 }
212 }
213
214 response = malloc(buflen * sizeof *response);
215 response_len = 0;
216 if (!response) {
217 ERRMEM;
218 /* restore terminal settings */
219 if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100220 ERR("Unable to restore terminal settings (%s).", strerror(errno));
Michal Vasko7b62fed2015-10-26 15:39:46 +0100221 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100222 fclose(tty);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100223 return NULL;
224 }
225
226 while ((fread(&c, 1, 1, tty) == 1) && (c != '\n')) {
227 if (response_len >= buflen - 1) {
228 buflen *= 2;
229 newtext = realloc(response, buflen * sizeof *newtext);
230 if (!newtext) {
Michal Vaskod083db62016-01-19 10:31:29 +0100231 ERRMEM;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100232 free(response);
233
234 /* restore terminal settings */
235 if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100236 ERR("Unable to restore terminal settings (%s).", strerror(errno));
Michal Vasko7b62fed2015-10-26 15:39:46 +0100237 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100238 fclose(tty);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100239 return NULL;
240 } else {
241 response = newtext;
242 }
243 }
244 response[response_len++] = c;
245 }
246 /* terminating null byte */
247 response[response_len++] = '\0';
248
249 /* system ("stty echo"); */
250 if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100251 ERR("Unable to restore terminal settings (%s).", strerror(errno));
Michal Vasko7b62fed2015-10-26 15:39:46 +0100252 /*
253 * terminal probably still hides input characters, but we have password
254 * and anyway we are unable to set terminal to the previous state, so
255 * just continue
256 */
257 }
258
259 fprintf(tty, "\n");
260 fclose(tty);
261 return response;
262}
263
264static char *
265sshauth_passphrase(const char* privkey_path)
266{
267 char c, *buf, *newbuf;
268 int buflen = 1024, len = 0;
269 struct termios newterm, oldterm;
270 FILE *tty;
271
272 buf = malloc(buflen * sizeof *buf);
273 if (!buf) {
Michal Vaskod083db62016-01-19 10:31:29 +0100274 ERRMEM;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100275 return NULL;
276 }
277
278 if (!(tty = fopen("/dev/tty", "r+"))) {
Michal Vaskod083db62016-01-19 10:31:29 +0100279 ERR("Unable to open the current terminal (%s).", strerror(errno));
Michal Vasko94e7c2d2016-01-21 15:57:57 +0100280 goto fail;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100281 }
282
283 if (tcgetattr(fileno(tty), &oldterm)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100284 ERR("Unable to get terminal settings (%s).", strerror(errno));
Michal Vasko94e7c2d2016-01-21 15:57:57 +0100285 goto fail;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100286 }
287
288 fprintf(tty, "Enter passphrase for the key '%s':", privkey_path);
289 fflush(tty);
290
291 /* system("stty -echo"); */
292 newterm = oldterm;
293 newterm.c_lflag &= ~ECHO;
294 newterm.c_lflag &= ~ICANON;
295 tcflush(fileno(tty), TCIFLUSH);
296 if (tcsetattr(fileno(tty), TCSANOW, &newterm)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100297 ERR("Unable to change terminal settings for hiding password (%s).", strerror(errno));
Michal Vasko94e7c2d2016-01-21 15:57:57 +0100298 goto fail;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100299 }
300
301 while ((fread(&c, 1, 1, tty) == 1) && (c != '\n')) {
302 if (len >= buflen - 1) {
303 buflen *= 2;
304 newbuf = realloc(buf, buflen * sizeof *newbuf);
305 if (!newbuf) {
306 ERRMEM;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100307 /* restore terminal settings */
308 if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100309 ERR("Unable to restore terminal settings (%s).", strerror(errno));
Michal Vasko7b62fed2015-10-26 15:39:46 +0100310 }
Michal Vasko94e7c2d2016-01-21 15:57:57 +0100311 goto fail;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100312 }
313 buf = newbuf;
314 }
315 buf[len++] = (char)c;
316 }
317 buf[len++] = 0; /* terminating null byte */
318
319 /* system ("stty echo"); */
320 if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100321 ERR("Unable to restore terminal settings (%s).", strerror(errno));
Michal Vasko7b62fed2015-10-26 15:39:46 +0100322 /*
323 * terminal probably still hides input characters, but we have password
324 * and anyway we are unable to set terminal to the previous state, so
325 * just continue
326 */
327 }
328 fprintf(tty, "\n");
329
330 fclose(tty);
331 return buf;
Michal Vasko94e7c2d2016-01-21 15:57:57 +0100332
333fail:
334 free(buf);
Michal Vaskode581a82016-01-22 13:15:35 +0100335 if (tty) {
336 fclose(tty);
337 }
Michal Vasko94e7c2d2016-01-21 15:57:57 +0100338 return NULL;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100339}
340
Michal Vasko7b62fed2015-10-26 15:39:46 +0100341#ifdef ENABLE_DNSSEC
342
343/* return 0 (DNSSEC + key valid), 1 (unsecure DNS + key valid), 2 (key not found or an error) */
344/* type - 1 (RSA), 2 (DSA), 3 (ECDSA); alg - 1 (SHA1), 2 (SHA-256) */
345static int
346sshauth_hostkey_hash_dnssec_check(const char *hostname, const char *sha1hash, int type, int alg) {
347 ns_msg handle;
348 ns_rr rr;
349 val_status_t val_status;
350 const unsigned char* rdata;
351 unsigned char buf[4096];
352 int buf_len = 4096;
353 int ret = 0, i, j, len;
354
355 /* class 1 - internet, type 44 - SSHFP */
356 len = val_res_query(NULL, hostname, 1, 44, buf, buf_len, &val_status);
357
358 if ((len < 0) || !val_istrusted(val_status)) {
359 ret = 2;
360 goto finish;
361 }
362
363 if (ns_initparse(buf, len, &handle) < 0) {
364 ERR("Failed to initialize DNSSEC response parser.");
365 ret = 2;
366 goto finish;
367 }
368
369 if ((i = libsres_msg_getflag(handle, ns_f_rcode))) {
370 ERR("DNSSEC query returned %d.", i);
371 ret = 2;
372 goto finish;
373 }
374
375 if (!libsres_msg_getflag(handle, ns_f_ad)) {
376 /* response not secured by DNSSEC */
377 ret = 1;
378 }
379
380 /* query section */
381 if (ns_parserr(&handle, ns_s_qd, 0, &rr)) {
382 ERROR("DNSSEC query section parser fail.");
383 ret = 2;
384 goto finish;
385 }
386
387 if (strcmp(hostname, ns_rr_name(rr)) || (ns_rr_type(rr) != 44) || (ns_rr_class(rr) != 1)) {
388 ERROR("DNSSEC query in the answer does not match the original query.");
389 ret = 2;
390 goto finish;
391 }
392
393 /* answer section */
394 i = 0;
395 while (!ns_parserr(&handle, ns_s_an, i, &rr)) {
396 if (ns_rr_type(rr) != 44) {
397 ++i;
398 continue;
399 }
400
401 rdata = ns_rr_rdata(rr);
402 if (rdata[0] != type) {
403 ++i;
404 continue;
405 }
406 if (rdata[1] != alg) {
407 ++i;
408 continue;
409 }
410
411 /* we found the correct SSHFP entry */
412 rdata += 2;
413 for (j = 0; j < 20; ++j) {
414 if (rdata[j] != (unsigned char)sha1hash[j]) {
415 ret = 2;
416 goto finish;
417 }
418 }
419
420 /* server fingerprint is supported by a DNS entry,
421 * we have already determined if DNSSEC was used or not
422 */
423 goto finish;
424 }
425
426 /* no match */
427 ret = 2;
428
429finish:
430 val_free_validator_state();
431 return ret;
432}
433
Michal Vaskoc61c4492016-01-25 11:13:34 +0100434#endif /* ENABLE_DNSSEC */
Michal Vasko7b62fed2015-10-26 15:39:46 +0100435
436static int
437sshauth_hostkey_check(const char *hostname, ssh_session session)
438{
439 char *hexa;
440 int c, state, ret;
441 ssh_key srv_pubkey;
442 unsigned char *hash_sha1 = NULL;
443 size_t hlen;
444 enum ssh_keytypes_e srv_pubkey_type;
445 char answer[5];
446
447 state = ssh_is_server_known(session);
448
449 ret = ssh_get_publickey(session, &srv_pubkey);
450 if (ret < 0) {
451 ERR("Unable to get server public key.");
Michal Vaskod083db62016-01-19 10:31:29 +0100452 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100453 }
454
455 srv_pubkey_type = ssh_key_type(srv_pubkey);
456 ret = ssh_get_publickey_hash(srv_pubkey, SSH_PUBLICKEY_HASH_SHA1, &hash_sha1, &hlen);
457 ssh_key_free(srv_pubkey);
458 if (ret < 0) {
459 ERR("Failed to calculate SHA1 hash of the server public key.");
Michal Vaskod083db62016-01-19 10:31:29 +0100460 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100461 }
462
463 hexa = ssh_get_hexa(hash_sha1, hlen);
464
465 switch (state) {
466 case SSH_SERVER_KNOWN_OK:
467 break; /* ok */
468
469 case SSH_SERVER_KNOWN_CHANGED:
470 ERR("Remote host key changed, the connection will be terminated!");
471 goto fail;
472
473 case SSH_SERVER_FOUND_OTHER:
Michal Vasko086311b2016-01-08 09:53:11 +0100474 WRN("Remote host key is not known, but a key of another type for this host is known. Continue with caution.");
475 goto hostkey_not_known;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100476
477 case SSH_SERVER_FILE_NOT_FOUND:
478 WRN("Could not find the known hosts file.");
Michal Vasko086311b2016-01-08 09:53:11 +0100479 goto hostkey_not_known;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100480
481 case SSH_SERVER_NOT_KNOWN:
Michal Vasko086311b2016-01-08 09:53:11 +0100482hostkey_not_known:
Michal Vasko7b62fed2015-10-26 15:39:46 +0100483#ifdef ENABLE_DNSSEC
484 if ((srv_pubkey_type != SSH_KEYTYPE_UNKNOWN) || (srv_pubkey_type != SSH_KEYTYPE_RSA1)) {
485 if (srv_pubkey_type == SSH_KEYTYPE_DSS) {
486 ret = callback_ssh_hostkey_hash_dnssec_check(hostname, hash_sha1, 2, 1);
487 } else if (srv_pubkey_type == SSH_KEYTYPE_RSA) {
488 ret = callback_ssh_hostkey_hash_dnssec_check(hostname, hash_sha1, 1, 1);
489 } else if (srv_pubkey_type == SSH_KEYTYPE_ECDSA) {
490 ret = callback_ssh_hostkey_hash_dnssec_check(hostname, hash_sha1, 3, 1);
491 }
492
493 /* DNSSEC SSHFP check successful, that's enough */
494 if (!ret) {
495 DBG("DNSSEC SSHFP check successful");
496 ssh_write_knownhost(session);
497 ssh_clean_pubkey_hash(&hash_sha1);
498 ssh_string_free_char(hexa);
Michal Vaskod083db62016-01-19 10:31:29 +0100499 return 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100500 }
501 }
502#endif
503
504 /* try to get result from user */
505 fprintf(stdout, "The authenticity of the host \'%s\' cannot be established.\n", hostname);
506 fprintf(stdout, "%s key fingerprint is %s.\n", ssh_key_type_to_char(srv_pubkey_type), hexa);
507
508#ifdef ENABLE_DNSSEC
509 if (ret == 2) {
510 fprintf(stdout, "No matching host key fingerprint found in DNS.\n");
511 } else if (ret == 1) {
512 fprintf(stdout, "Matching host key fingerprint found in DNS.\n");
513 }
514#endif
515
516 fprintf(stdout, "Are you sure you want to continue connecting (yes/no)? ");
517
518 do {
519 if (fscanf(stdin, "%4s", answer) == EOF) {
520 ERR("fscanf() failed (%s).", strerror(errno));
521 goto fail;
522 }
523 while (((c = getchar()) != EOF) && (c != '\n'));
524
525 fflush(stdin);
526 if (!strcmp("yes", answer)) {
527 /* store the key into the host file */
528 ret = ssh_write_knownhost(session);
529 if (ret < 0) {
Michal Vaskoc111ac52015-12-08 14:36:36 +0100530 WRN("Adding the known host %s failed (%s).", hostname, ssh_get_error(session));
Michal Vasko7b62fed2015-10-26 15:39:46 +0100531 }
532 } else if (!strcmp("no", answer)) {
533 goto fail;
534 } else {
535 fprintf(stdout, "Please type 'yes' or 'no': ");
536 }
537 } while (strcmp(answer, "yes") && strcmp(answer, "no"));
538
539 break;
540
541 case SSH_SERVER_ERROR:
542 ssh_clean_pubkey_hash(&hash_sha1);
543 fprintf(stderr,"%s",ssh_get_error(session));
544 return -1;
545 }
546
547 ssh_clean_pubkey_hash(&hash_sha1);
548 ssh_string_free_char(hexa);
Michal Vaskod083db62016-01-19 10:31:29 +0100549 return 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100550
551fail:
552 ssh_clean_pubkey_hash(&hash_sha1);
553 ssh_string_free_char(hexa);
Michal Vaskod083db62016-01-19 10:31:29 +0100554 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100555}
556
Michal Vasko3031aae2016-01-27 16:07:18 +0100557static int
558_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 +0100559{
560 int i;
561 FILE *key;
562 char line[128];
563
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100564 if (!pub_key || !priv_key) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100565 ERRARG;
566 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100567 }
568
Michal Vasko3031aae2016-01-27 16:07:18 +0100569 for (i = 0; i < opts->key_count; ++i) {
570 if (!strcmp(opts->keys[i].pubkey_path, pub_key) || !strcmp(opts->keys[i].privkey_path, priv_key)) {
571 if (strcmp(opts->keys[i].pubkey_path, pub_key)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100572 WRN("Private key \"%s\" found with another public key \"%s\".",
Michal Vasko3031aae2016-01-27 16:07:18 +0100573 priv_key, opts->keys[i].pubkey_path);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100574 continue;
Michal Vasko3031aae2016-01-27 16:07:18 +0100575 } else if (strcmp(opts->keys[i].privkey_path, priv_key)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100576 WRN("Public key \"%s\" found with another private key \"%s\".",
Michal Vasko3031aae2016-01-27 16:07:18 +0100577 pub_key, opts->keys[i].privkey_path);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100578 continue;
579 }
580
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100581 ERR("SSH key pair already set.");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100582 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100583 }
584 }
585
Michal Vasko3031aae2016-01-27 16:07:18 +0100586 /* add the keys */
587 ++opts->key_count;
588 opts->keys = realloc(opts->keys, opts->key_count * sizeof *opts->keys);
589 opts->keys[opts->key_count - 1].pubkey_path = strdup(pub_key);
590 opts->keys[opts->key_count - 1].privkey_path = strdup(priv_key);
591 opts->keys[opts->key_count - 1].privkey_crypt = 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100592
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100593 /* check encryption */
594 if ((key = fopen(priv_key, "r"))) {
595 /* 1st line - key type */
596 if (!fgets(line, sizeof line, key)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100597 fclose(key);
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100598 ERR("fgets() on %s failed.", priv_key);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100599 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100600 }
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100601 /* 2nd line - encryption information or key */
602 if (!fgets(line, sizeof line, key)) {
603 fclose(key);
604 ERR("fgets() on %s failed.", priv_key);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100605 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100606 }
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100607 fclose(key);
608 if (strcasestr(line, "encrypted")) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100609 opts->keys[opts->key_count - 1].privkey_crypt = 1;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100610 }
Michal Vasko7b62fed2015-10-26 15:39:46 +0100611 }
612
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100613 return 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100614}
615
616API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100617nc_client_ssh_add_keypair(const char *pub_key, const char *priv_key)
Michal Vasko7b62fed2015-10-26 15:39:46 +0100618{
Michal Vasko3031aae2016-01-27 16:07:18 +0100619 return _nc_client_ssh_add_keypair(pub_key, priv_key, &ssh_opts);
620}
621
622API int
623nc_client_ssh_ch_add_keypair(const char *pub_key, const char *priv_key)
624{
625 return _nc_client_ssh_add_keypair(pub_key, priv_key, &ssh_ch_opts);
626}
627
628static int
629_nc_client_ssh_del_keypair(int idx, struct nc_client_ssh_opts *opts)
630{
631 if (idx >= opts->key_count) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100632 ERRARG;
633 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100634 }
635
Michal Vasko3031aae2016-01-27 16:07:18 +0100636 free(opts->keys[idx].pubkey_path);
637 free(opts->keys[idx].privkey_path);
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100638
Michal Vasko3031aae2016-01-27 16:07:18 +0100639 --opts->key_count;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100640
Michal Vasko3031aae2016-01-27 16:07:18 +0100641 memcpy(opts->keys + idx, opts->keys + opts->key_count, sizeof *opts->keys);
642 opts->keys = realloc(opts->keys, opts->key_count * sizeof *opts->keys);
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100643
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100644 return 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100645}
646
647API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100648nc_client_ssh_del_keypair(int idx)
Michal Vasko7b62fed2015-10-26 15:39:46 +0100649{
Michal Vasko3031aae2016-01-27 16:07:18 +0100650 return _nc_client_ssh_del_keypair(idx, &ssh_opts);
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100651}
652
653API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100654nc_client_ssh_ch_del_keypair(int idx)
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100655{
Michal Vasko3031aae2016-01-27 16:07:18 +0100656 return _nc_client_ssh_del_keypair(idx, &ssh_ch_opts);
657}
658
659static int
660_nc_client_ssh_get_keypair_count(struct nc_client_ssh_opts *opts)
661{
662 return opts->key_count;
663}
664
665API int
666nc_client_ssh_get_keypair_count(void)
667{
668 return _nc_client_ssh_get_keypair_count(&ssh_opts);
669}
670
671API int
672nc_client_ssh_ch_get_keypair_count(void)
673{
674 return _nc_client_ssh_get_keypair_count(&ssh_ch_opts);
675}
676
677static int
678_nc_client_ssh_get_keypair(int idx, const char **pub_key, const char **priv_key, struct nc_client_ssh_opts *opts)
679{
680 if ((idx >= opts->key_count) || (!pub_key && !priv_key)) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100681 ERRARG;
682 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100683 }
684
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100685 if (pub_key) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100686 *pub_key = opts->keys[idx].pubkey_path;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100687 }
688 if (priv_key) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100689 *priv_key = opts->keys[idx].privkey_path;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100690 }
691
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100692 return 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100693}
694
Michal Vasko3031aae2016-01-27 16:07:18 +0100695API int
696nc_client_ssh_get_keypair(int idx, const char **pub_key, const char **priv_key)
697{
698 return _nc_client_ssh_get_keypair(idx, pub_key, priv_key, &ssh_opts);
699}
700
701API int
702nc_client_ssh_ch_get_keypair(int idx, const char **pub_key, const char **priv_key)
703{
704 return _nc_client_ssh_get_keypair(idx, pub_key, priv_key, &ssh_ch_opts);
705}
706
707static void
708_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 +0100709{
710 if (pref < 0) {
711 pref = -1;
712 }
713
714 if (auth_type == NC_SSH_AUTH_INTERACTIVE) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100715 opts->auth_pref[0].value = pref;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100716 } else if (auth_type == NC_SSH_AUTH_PASSWORD) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100717 opts->auth_pref[1].value = pref;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100718 } else if (auth_type == NC_SSH_AUTH_PUBLICKEY) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100719 opts->auth_pref[2].value = pref;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100720 }
721}
722
Michal Vasko3031aae2016-01-27 16:07:18 +0100723API void
724nc_client_ssh_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref)
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100725{
Michal Vasko3031aae2016-01-27 16:07:18 +0100726 _nc_client_ssh_set_auth_pref(auth_type, pref, &ssh_opts);
727}
728
729API void
730nc_client_ssh_ch_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref)
731{
732 _nc_client_ssh_set_auth_pref(auth_type, pref, &ssh_ch_opts);
733}
734
735static int16_t
736_nc_client_ssh_get_auth_pref(NC_SSH_AUTH_TYPE auth_type, struct nc_client_ssh_opts *opts)
737{
738 int16_t pref = 0;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100739
740 if (auth_type == NC_SSH_AUTH_INTERACTIVE) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100741 pref = opts->auth_pref[0].value;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100742 } else if (auth_type == NC_SSH_AUTH_PASSWORD) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100743 pref = opts->auth_pref[1].value;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100744 } else if (auth_type == NC_SSH_AUTH_PUBLICKEY) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100745 pref = opts->auth_pref[2].value;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100746 }
747
748 return pref;
749}
750
Michal Vasko3031aae2016-01-27 16:07:18 +0100751API int16_t
752nc_client_ssh_get_auth_pref(NC_SSH_AUTH_TYPE auth_type)
753{
754 return _nc_client_ssh_get_auth_pref(auth_type, &ssh_opts);
755}
756
757API int16_t
758nc_client_ssh_ch_get_auth_pref(NC_SSH_AUTH_TYPE auth_type)
759{
760 return _nc_client_ssh_get_auth_pref(auth_type, &ssh_ch_opts);
761}
762
763static int
764_nc_client_ssh_set_username(const char *username, struct nc_client_ssh_opts *opts)
765{
766 if (opts->username) {
767 free(opts->username);
768 }
769 if (username) {
770 opts->username = strdup(username);
771 if (!opts->username) {
772 ERRMEM;
773 return -1;
774 }
775 } else {
776 opts->username = NULL;
777 }
778
779 return 0;
780}
781
782API int
783nc_client_ssh_set_username(const char *username)
784{
785 return _nc_client_ssh_set_username(username, &ssh_opts);
786}
787
788API int
789nc_client_ssh_ch_set_username(const char *username)
790{
791 return _nc_client_ssh_set_username(username, &ssh_ch_opts);
792}
793
Michal Vaskoe22c6732016-01-29 11:03:02 +0100794static const char *
795_nc_client_ssh_get_username(struct nc_client_ssh_opts *opts)
796{
797 return opts->username;
798}
799
800API const char *
801nc_client_ssh_get_username(void)
802{
803 return _nc_client_ssh_get_username(&ssh_opts);
804}
805
806API const char *
807nc_client_ssh_ch_get_username(void)
808{
809 return _nc_client_ssh_get_username(&ssh_ch_opts);
810}
811
Michal Vasko3031aae2016-01-27 16:07:18 +0100812API int
813nc_client_ssh_ch_add_bind_listen(const char *address, uint16_t port)
814{
815 return nc_client_ch_add_bind_listen(address, port, NC_TI_LIBSSH);
816}
817
818API int
819nc_client_ssh_ch_del_bind(const char *address, uint16_t port)
820{
821 return nc_client_ch_del_bind(address, port, NC_TI_LIBSSH);
822}
823
Michal Vasko7b62fed2015-10-26 15:39:46 +0100824/* Establish a secure SSH connection, authenticate, and create a channel with the 'netconf' subsystem.
825 * Host, port, username, and a connected socket is expected to be set.
826 */
827static int
828connect_ssh_session_netconf(struct nc_session *session)
829{
Michal Vasko235efdc2015-12-17 12:05:04 +0100830 int j, ret_auth, userauthlist;
831 NC_SSH_AUTH_TYPE auth;
832 short int pref;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100833 const char* prompt;
834 char *s, *answer, echo;
835 ssh_key pubkey, privkey;
836 ssh_session ssh_sess;
837
838 ssh_sess = session->ti.libssh.session;
839
840 if (ssh_connect(ssh_sess) != SSH_OK) {
841 ERR("Starting the SSH session failed (%s)", ssh_get_error(ssh_sess));
842 DBG("Error code %d.", ssh_get_error_code(ssh_sess));
Michal Vaskod083db62016-01-19 10:31:29 +0100843 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100844 }
845
846 if (sshauth_hostkey_check(session->host, ssh_sess)) {
847 ERR("Checking the host key failed.");
Michal Vaskod083db62016-01-19 10:31:29 +0100848 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100849 }
850
851 if ((ret_auth = ssh_userauth_none(ssh_sess, NULL)) == SSH_AUTH_ERROR) {
852 ERR("Authentication failed (%s).", ssh_get_error(ssh_sess));
Michal Vaskod083db62016-01-19 10:31:29 +0100853 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100854 }
855
856 /* check what authentication methods are available */
857 userauthlist = ssh_userauth_list(ssh_sess, NULL);
Michal Vasko235efdc2015-12-17 12:05:04 +0100858
859 /* remove those disabled */
860 if (ssh_opts.auth_pref[0].value < 0) {
861 VRB("Interactive SSH authentication method was disabled.");
862 userauthlist &= ~SSH_AUTH_METHOD_INTERACTIVE;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100863 }
Michal Vasko235efdc2015-12-17 12:05:04 +0100864 if (ssh_opts.auth_pref[1].value < 0) {
865 VRB("Password SSH authentication method was disabled.");
866 userauthlist &= ~SSH_AUTH_METHOD_PASSWORD;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100867 }
Michal Vasko235efdc2015-12-17 12:05:04 +0100868 if (ssh_opts.auth_pref[2].value < 0) {
869 VRB("Publickey SSH authentication method was disabled.");
870 userauthlist &= ~SSH_AUTH_METHOD_PUBLICKEY;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100871 }
872
Michal Vasko235efdc2015-12-17 12:05:04 +0100873 while (ret_auth != SSH_AUTH_SUCCESS) {
874 auth = 0;
875 pref = 0;
876 if (userauthlist & SSH_AUTH_METHOD_INTERACTIVE) {
877 auth = NC_SSH_AUTH_INTERACTIVE;
878 pref = ssh_opts.auth_pref[0].value;
879 }
880 if ((userauthlist & SSH_AUTH_METHOD_PASSWORD) && (ssh_opts.auth_pref[1].value > pref)) {
881 auth = NC_SSH_AUTH_PASSWORD;
882 pref = ssh_opts.auth_pref[1].value;
883 }
884 if ((userauthlist & SSH_AUTH_METHOD_PUBLICKEY) && (ssh_opts.auth_pref[2].value > pref)) {
885 auth = NC_SSH_AUTH_PUBLICKEY;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100886 }
887
Michal Vasko235efdc2015-12-17 12:05:04 +0100888 if (!auth) {
Michal Vaskod083db62016-01-19 10:31:29 +0100889 ERR("Unable to authenticate to the remote server (no supported authentication methods left).");
Michal Vasko235efdc2015-12-17 12:05:04 +0100890 break;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100891 }
892
893 /* found common authentication method */
Michal Vasko235efdc2015-12-17 12:05:04 +0100894 switch (auth) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100895 case NC_SSH_AUTH_PASSWORD:
Michal Vasko235efdc2015-12-17 12:05:04 +0100896 userauthlist &= ~SSH_AUTH_METHOD_PASSWORD;
897
Michal Vaskoef578332016-01-25 13:20:09 +0100898 VRB("Password authentication (host \"%s\", user \"%s\").", session->host, session->username);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100899 s = sshauth_password(session->username, session->host);
900 if ((ret_auth = ssh_userauth_password(ssh_sess, session->username, s)) != SSH_AUTH_SUCCESS) {
901 memset(s, 0, strlen(s));
Michal Vaskod083db62016-01-19 10:31:29 +0100902 VRB("Authentication failed (%s).", ssh_get_error(ssh_sess));
Michal Vasko7b62fed2015-10-26 15:39:46 +0100903 }
904 free(s);
905 break;
906 case NC_SSH_AUTH_INTERACTIVE:
Michal Vasko235efdc2015-12-17 12:05:04 +0100907 userauthlist &= ~SSH_AUTH_METHOD_INTERACTIVE;
908
Michal Vaskod083db62016-01-19 10:31:29 +0100909 VRB("Keyboard-interactive authentication.");
Michal Vasko7b62fed2015-10-26 15:39:46 +0100910 while ((ret_auth = ssh_userauth_kbdint(ssh_sess, NULL, NULL)) == SSH_AUTH_INFO) {
911 for (j = 0; j < ssh_userauth_kbdint_getnprompts(ssh_sess); ++j) {
912 prompt = ssh_userauth_kbdint_getprompt(ssh_sess, j, &echo);
913 if (prompt == NULL) {
914 break;
915 }
916 answer = sshauth_interactive(ssh_userauth_kbdint_getname(ssh_sess),
917 ssh_userauth_kbdint_getinstruction(ssh_sess),
918 prompt, echo);
919 if (ssh_userauth_kbdint_setanswer(ssh_sess, j, answer) < 0) {
920 free(answer);
921 break;
922 }
923 free(answer);
924 }
925 }
926
927 if (ret_auth == SSH_AUTH_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100928 VRB("Authentication failed (%s).", ssh_get_error(ssh_sess));
Michal Vasko7b62fed2015-10-26 15:39:46 +0100929 }
930
931 break;
Michal Vasko206d3b12015-12-04 11:08:42 +0100932 case NC_SSH_AUTH_PUBLICKEY:
Michal Vasko235efdc2015-12-17 12:05:04 +0100933 userauthlist &= ~SSH_AUTH_METHOD_PUBLICKEY;
934
Michal Vaskod083db62016-01-19 10:31:29 +0100935 VRB("Publickey athentication.");
Michal Vasko7b62fed2015-10-26 15:39:46 +0100936
937 /* if publickeys path not provided, we cannot continue */
938 if (!ssh_opts.key_count) {
939 VRB("No key pair specified.");
940 break;
941 }
942
943 for (j = 0; j < ssh_opts.key_count; j++) {
Michal Vaskoef578332016-01-25 13:20:09 +0100944 VRB("Trying to authenticate using %spair \"%s\" \"%s\".",
Michal Vasko7b62fed2015-10-26 15:39:46 +0100945 ssh_opts.keys[j].privkey_crypt ? "password-protected " : "", ssh_opts.keys[j].privkey_path,
946 ssh_opts.keys[j].pubkey_path);
947
948 if (ssh_pki_import_pubkey_file(ssh_opts.keys[j].pubkey_path, &pubkey) != SSH_OK) {
949 WRN("Failed to import the key \"%s\".", ssh_opts.keys[j].pubkey_path);
950 continue;
951 }
952 ret_auth = ssh_userauth_try_publickey(ssh_sess, NULL, pubkey);
953 if ((ret_auth == SSH_AUTH_DENIED) || (ret_auth == SSH_AUTH_PARTIAL)) {
954 ssh_key_free(pubkey);
955 continue;
956 }
957 if (ret_auth == SSH_AUTH_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100958 ERR("Authentication failed (%s).", ssh_get_error(ssh_sess));
Michal Vasko7b62fed2015-10-26 15:39:46 +0100959 ssh_key_free(pubkey);
960 break;
961 }
962
963 if (ssh_opts.keys[j].privkey_crypt) {
964 s = sshauth_passphrase(ssh_opts.keys[j].privkey_path);
965 } else {
966 s = NULL;
967 }
968
969 if (ssh_pki_import_privkey_file(ssh_opts.keys[j].privkey_path, s, NULL, NULL, &privkey) != SSH_OK) {
970 WRN("Failed to import the key \"%s\".", ssh_opts.keys[j].privkey_path);
971 if (s) {
972 memset(s, 0, strlen(s));
973 free(s);
974 }
975 ssh_key_free(pubkey);
976 continue;
977 }
978
979 if (s) {
980 memset(s, 0, strlen(s));
981 free(s);
982 }
983
984 ret_auth = ssh_userauth_publickey(ssh_sess, NULL, privkey);
985 ssh_key_free(pubkey);
986 ssh_key_free(privkey);
987
988 if (ret_auth == SSH_AUTH_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100989 ERR("Authentication failed (%s).", ssh_get_error(ssh_sess));
Michal Vasko7b62fed2015-10-26 15:39:46 +0100990 }
991 if (ret_auth == SSH_AUTH_SUCCESS) {
992 break;
993 }
994 }
995 break;
996 }
Michal Vasko7b62fed2015-10-26 15:39:46 +0100997 }
998
999 /* check a state of authentication */
1000 if (ret_auth != SSH_AUTH_SUCCESS) {
Michal Vaskod083db62016-01-19 10:31:29 +01001001 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001002 }
1003
1004 /* open a channel */
1005 session->ti.libssh.channel = ssh_channel_new(ssh_sess);
1006 if (ssh_channel_open_session(session->ti.libssh.channel) != SSH_OK) {
1007 ssh_channel_free(session->ti.libssh.channel);
1008 session->ti.libssh.channel = NULL;
Michal Vaskod083db62016-01-19 10:31:29 +01001009 ERR("Opening an SSH channel failed (%s).", ssh_get_error(ssh_sess));
1010 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001011 }
1012
1013 /* execute the NETCONF subsystem on the channel */
1014 if (ssh_channel_request_subsystem(session->ti.libssh.channel, "netconf") != SSH_OK) {
1015 ssh_channel_free(session->ti.libssh.channel);
1016 session->ti.libssh.channel = NULL;
Michal Vaskod083db62016-01-19 10:31:29 +01001017 ERR("Starting the \"netconf\" SSH subsystem failed (%s).", ssh_get_error(ssh_sess));
1018 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001019 }
1020
1021 return EXIT_SUCCESS;
Radek Krejciac6d3472015-10-22 15:47:18 +02001022}
1023
1024API struct nc_session *
Michal Vasko3031aae2016-01-27 16:07:18 +01001025nc_connect_ssh(const char *host, uint16_t port, struct ly_ctx *ctx)
Radek Krejciac6d3472015-10-22 15:47:18 +02001026{
Michal Vaskoc111ac52015-12-08 14:36:36 +01001027 const int timeout = NC_SSH_TIMEOUT;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001028 int sock;
Michal Vasko3031aae2016-01-27 16:07:18 +01001029 char *username;
Radek Krejciac6d3472015-10-22 15:47:18 +02001030 struct passwd *pw;
1031 struct nc_session *session = NULL;
1032
1033 /* process parameters */
1034 if (!host || strisempty(host)) {
1035 host = "localhost";
1036 }
1037
1038 if (!port) {
1039 port = NC_PORT_SSH;
1040 }
1041
Michal Vasko3031aae2016-01-27 16:07:18 +01001042 if (!ssh_opts.username) {
Radek Krejciac6d3472015-10-22 15:47:18 +02001043 pw = getpwuid(getuid());
1044 if (!pw) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001045 ERR("Unknown username for the SSH connection (%s).", strerror(errno));
1046 return NULL;
Radek Krejciac6d3472015-10-22 15:47:18 +02001047 } else {
1048 username = pw->pw_name;
1049 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001050 } else {
1051 username = ssh_opts.username;
Radek Krejciac6d3472015-10-22 15:47:18 +02001052 }
1053
1054 /* prepare session structure */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001055 session = calloc(1, sizeof *session);
Radek Krejciac6d3472015-10-22 15:47:18 +02001056 if (!session) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001057 ERRMEM;
Radek Krejciac6d3472015-10-22 15:47:18 +02001058 return NULL;
1059 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001060 session->status = NC_STATUS_STARTING;
1061 session->side = NC_CLIENT;
Radek Krejciac6d3472015-10-22 15:47:18 +02001062
Michal Vasko7b62fed2015-10-26 15:39:46 +01001063 /* transport lock */
1064 session->ti_lock = malloc(sizeof *session->ti_lock);
1065 if (!session->ti_lock) {
1066 ERRMEM;
1067 goto fail;
1068 }
1069 pthread_mutex_init(session->ti_lock, NULL);
1070
1071 /* other transport-specific data */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001072 session->ti_type = NC_TI_LIBSSH;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001073 session->ti.libssh.session = ssh_new();
1074 if (!session->ti.libssh.session) {
1075 ERR("Unable to initialize SSH session.");
1076 goto fail;
1077 }
Radek Krejciac6d3472015-10-22 15:47:18 +02001078
Michal Vasko7b62fed2015-10-26 15:39:46 +01001079 /* set some basic SSH session options */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001080 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOST, host);
1081 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_PORT, &port);
1082 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_USER, username);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001083 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_TIMEOUT, &timeout);
Michal Vasko086311b2016-01-08 09:53:11 +01001084 if (ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOSTKEYS,
1085 "ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,"
1086 "ecdsa-sha2-nistp256,ssh-rsa,ssh-dss,ssh-rsa1")) {
1087 /* ecdsa is probably not supported... */
1088 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOSTKEYS, "ssh-ed25519,ssh-rsa,ssh-dss,ssh-rsa1");
1089 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001090
1091 /* create and assign communication socket */
Michal Vaskof05562c2016-01-20 12:06:43 +01001092 sock = nc_sock_connect(host, port);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001093 if (sock == -1) {
1094 goto fail;
1095 }
1096 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_FD, &sock);
1097
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001098 /* temporarily, for session connection */
1099 session->host = host;
1100 session->username = username;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001101 if (connect_ssh_session_netconf(session)) {
1102 goto fail;
Radek Krejciac6d3472015-10-22 15:47:18 +02001103 }
1104
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001105 /* assign context (dicionary needed for handshake) */
1106 if (!ctx) {
1107 ctx = ly_ctx_new(SCHEMAS_DIR);
1108 } else {
1109 session->flags |= NC_SESSION_SHAREDCTX;
1110 }
1111 session->ctx = ctx;
1112
Radek Krejciac6d3472015-10-22 15:47:18 +02001113 /* NETCONF handshake */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001114 if (nc_handshake(session)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001115 goto fail;
Radek Krejciac6d3472015-10-22 15:47:18 +02001116 }
Michal Vaskoad611702015-12-03 13:41:51 +01001117 session->status = NC_STATUS_RUNNING;
Radek Krejciac6d3472015-10-22 15:47:18 +02001118
Michal Vaskoef578332016-01-25 13:20:09 +01001119 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko57eb9402015-12-08 14:38:12 +01001120 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001121 }
1122
1123 /* store information into the dictionary */
1124 session->host = lydict_insert(ctx, host, 0);
1125 session->port = port;
1126 session->username = lydict_insert(ctx, username, 0);
1127
Radek Krejciac6d3472015-10-22 15:47:18 +02001128 return session;
1129
Michal Vasko7b62fed2015-10-26 15:39:46 +01001130fail:
Radek Krejciac6d3472015-10-22 15:47:18 +02001131 nc_session_free(session);
1132 return NULL;
1133}
1134
1135API struct nc_session *
1136nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx)
1137{
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001138 char *host = NULL, *username = NULL;
1139 unsigned short port = 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001140 int sock;
1141 struct passwd *pw;
1142 struct nc_session *session = NULL;
Radek Krejciac6d3472015-10-22 15:47:18 +02001143
Michal Vaskob7c4ff32016-01-21 15:35:54 +01001144 if (!ssh_session) {
1145 ERRARG;
1146 return NULL;
1147 }
1148
Michal Vasko7b62fed2015-10-26 15:39:46 +01001149 /* prepare session structure */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001150 session = calloc(1, sizeof *session);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001151 if (!session) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001152 ERRMEM;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001153 return NULL;
1154 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001155 session->status = NC_STATUS_STARTING;
1156 session->side = NC_CLIENT;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001157
1158 /* transport lock */
1159 session->ti_lock = malloc(sizeof *session->ti_lock);
1160 if (!session->ti_lock) {
1161 ERRMEM;
1162 goto fail;
1163 }
1164 pthread_mutex_init(session->ti_lock, NULL);
1165
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001166 session->ti_type = NC_TI_LIBSSH;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001167 session->ti.libssh.session = ssh_session;
1168
Michal Vasko745ff832015-12-08 14:40:29 +01001169 /* was port set? */
1170 ssh_options_get_port(ssh_session, (unsigned int *)&port);
1171
1172 if (ssh_options_get(ssh_session, SSH_OPTIONS_HOST, &host) != SSH_OK) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001173 /*
Michal Vasko745ff832015-12-08 14:40:29 +01001174 * There is no file descriptor (detected based on the host, there is no way to check
1175 * the SSH_OPTIONS_FD directly :/), we need to create it. (TCP/IP layer)
Michal Vasko7b62fed2015-10-26 15:39:46 +01001176 */
1177
Michal Vasko7b62fed2015-10-26 15:39:46 +01001178 /* remember host */
Michal Vasko745ff832015-12-08 14:40:29 +01001179 host = strdup("localhost");
1180 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOST, host);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001181
1182 /* create and connect socket */
Michal Vaskof05562c2016-01-20 12:06:43 +01001183 sock = nc_sock_connect(host, port);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001184 if (sock == -1) {
1185 goto fail;
1186 }
1187 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_FD, &sock);
1188 }
1189
Michal Vasko745ff832015-12-08 14:40:29 +01001190 /* was username set? */
1191 ssh_options_get(ssh_session, SSH_OPTIONS_USER, &username);
1192
Michal Vasko7b62fed2015-10-26 15:39:46 +01001193 if (!ssh_is_connected(ssh_session)) {
1194 /*
1195 * We are connected, but not SSH authenticated. (Transport layer)
1196 */
1197
Michal Vasko7b62fed2015-10-26 15:39:46 +01001198 /* remember username */
1199 if (!username) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001200 if (!ssh_opts.username) {
1201 pw = getpwuid(getuid());
1202 if (!pw) {
1203 ERR("Unknown username for the SSH connection (%s).", strerror(errno));
1204 goto fail;
1205 }
1206 username = strdup(pw->pw_name);
1207 } else {
1208 username = strdup(ssh_opts.username);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001209 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001210 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_USER, username);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001211 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001212
1213 /* authenticate SSH session */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001214 session->host = host;
1215 session->username = username;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001216 if (connect_ssh_session_netconf(session)) {
1217 goto fail;
1218 }
1219 }
1220
1221 /*
1222 * SSH session is established, create NETCONF session. (Application layer)
1223 */
1224
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001225 /* assign context (dicionary needed for handshake) */
1226 if (!ctx) {
1227 ctx = ly_ctx_new(SCHEMAS_DIR);
1228 } else {
1229 session->flags |= NC_SESSION_SHAREDCTX;
1230 }
1231 session->ctx = ctx;
1232
Michal Vasko7b62fed2015-10-26 15:39:46 +01001233 /* NETCONF handshake */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001234 if (nc_handshake(session)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001235 goto fail;
1236 }
Michal Vaskoad611702015-12-03 13:41:51 +01001237 session->status = NC_STATUS_RUNNING;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001238
Michal Vaskoef578332016-01-25 13:20:09 +01001239 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko57eb9402015-12-08 14:38:12 +01001240 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001241 }
1242
1243 /* store information into the dictionary */
1244 if (host) {
1245 session->host = lydict_insert_zc(ctx, host);
1246 }
1247 if (port) {
1248 session->port = port;
1249 }
1250 if (username) {
1251 session->username = lydict_insert_zc(ctx, username);
1252 }
1253
Michal Vasko7b62fed2015-10-26 15:39:46 +01001254 return session;
1255
1256fail:
1257 nc_session_free(session);
Radek Krejciac6d3472015-10-22 15:47:18 +02001258 return NULL;
1259}
1260
1261API struct nc_session *
Michal Vasko7b62fed2015-10-26 15:39:46 +01001262nc_connect_ssh_channel(struct nc_session *session, struct ly_ctx *ctx)
Radek Krejciac6d3472015-10-22 15:47:18 +02001263{
Michal Vasko7b62fed2015-10-26 15:39:46 +01001264 struct nc_session *new_session, *ptr;
Radek Krejciac6d3472015-10-22 15:47:18 +02001265
Michal Vaskob7c4ff32016-01-21 15:35:54 +01001266 if (!session) {
1267 ERRARG;
1268 return NULL;
1269 }
1270
Michal Vasko7b62fed2015-10-26 15:39:46 +01001271 /* prepare session structure */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001272 new_session = calloc(1, sizeof *new_session);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001273 if (!new_session) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001274 ERRMEM;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001275 return NULL;
1276 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001277 new_session->status = NC_STATUS_STARTING;
1278 new_session->side = NC_CLIENT;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001279
1280 /* share some parameters including the session lock */
1281 new_session->ti_type = NC_TI_LIBSSH;
1282 new_session->ti_lock = session->ti_lock;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001283 new_session->ti.libssh.session = session->ti.libssh.session;
1284
1285 /* create the channel safely */
1286 pthread_mutex_lock(new_session->ti_lock);
1287
1288 /* open a channel */
1289 new_session->ti.libssh.channel = ssh_channel_new(new_session->ti.libssh.session);
1290 if (ssh_channel_open_session(new_session->ti.libssh.channel) != SSH_OK) {
Michal Vasko56b5bf72016-01-19 11:20:35 +01001291 ERR("Opening an SSH channel failed (%s).", ssh_get_error(new_session->ti.libssh.session));
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001292 goto fail;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001293 }
1294 /* execute the NETCONF subsystem on the channel */
1295 if (ssh_channel_request_subsystem(new_session->ti.libssh.channel, "netconf") != SSH_OK) {
Michal Vasko56b5bf72016-01-19 11:20:35 +01001296 ERR("Starting the \"netconf\" SSH subsystem failed (%s).", ssh_get_error(new_session->ti.libssh.session));
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001297 goto fail;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001298 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001299
1300 /* assign context (dicionary needed for handshake) */
1301 if (!ctx) {
1302 ctx = ly_ctx_new(SCHEMAS_DIR);
1303 } else {
Michal Vasko56b5bf72016-01-19 11:20:35 +01001304 new_session->flags |= NC_SESSION_SHAREDCTX;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001305 }
Michal Vasko56b5bf72016-01-19 11:20:35 +01001306 new_session->ctx = ctx;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001307
Michal Vasko7b62fed2015-10-26 15:39:46 +01001308 /* NETCONF handshake */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001309 if (nc_handshake(new_session)) {
1310 goto fail;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001311 }
Michal Vaskoad611702015-12-03 13:41:51 +01001312 new_session->status = NC_STATUS_RUNNING;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001313
Michal Vasko56b5bf72016-01-19 11:20:35 +01001314 pthread_mutex_unlock(new_session->ti_lock);
1315
Michal Vaskoef578332016-01-25 13:20:09 +01001316 if (nc_ctx_check_and_fill(new_session) == -1) {
Michal Vasko57eb9402015-12-08 14:38:12 +01001317 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001318 }
1319
1320 /* store information into session and the dictionary */
Michal Vasko56b5bf72016-01-19 11:20:35 +01001321 new_session->host = lydict_insert(ctx, session->host, 0);
1322 new_session->port = session->port;
1323 new_session->username = lydict_insert(ctx, session->username, 0);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001324
1325 /* append to the session ring list */
1326 if (!session->ti.libssh.next) {
1327 session->ti.libssh.next = new_session;
1328 new_session->ti.libssh.next = session;
1329 } else {
1330 ptr = session->ti.libssh.next;
1331 session->ti.libssh.next = new_session;
1332 new_session->ti.libssh.next = ptr;
1333 }
1334
1335 return new_session;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001336
1337fail:
1338 nc_session_free(new_session);
1339 return NULL;
Radek Krejciac6d3472015-10-22 15:47:18 +02001340}
Michal Vasko80cad7f2015-12-08 14:42:27 +01001341
Michal Vasko3031aae2016-01-27 16:07:18 +01001342struct nc_session *
Michal Vaskoaf58cd92016-01-28 11:56:02 +01001343nc_accept_callhome_ssh_sock(int sock, const char *host, uint16_t port, struct ly_ctx *ctx)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001344{
1345 const int ssh_timeout = NC_SSH_TIMEOUT;
Michal Vasko3031aae2016-01-27 16:07:18 +01001346 struct passwd *pw;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001347 ssh_session sess;
1348
Michal Vasko80cad7f2015-12-08 14:42:27 +01001349 sess = ssh_new();
1350 if (!sess) {
1351 ERR("Unable to initialize an SSH session.");
1352 close(sock);
1353 return NULL;
1354 }
1355
1356 ssh_options_set(sess, SSH_OPTIONS_FD, &sock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001357 ssh_options_set(sess, SSH_OPTIONS_HOST, host);
Michal Vasko80cad7f2015-12-08 14:42:27 +01001358 ssh_options_set(sess, SSH_OPTIONS_PORT, &port);
1359 ssh_options_set(sess, SSH_OPTIONS_TIMEOUT, &ssh_timeout);
Michal Vasko3031aae2016-01-27 16:07:18 +01001360 if (!ssh_ch_opts.username) {
1361 pw = getpwuid(getuid());
1362 if (!pw) {
1363 ERR("Unknown username for the SSH connection (%s).", strerror(errno));
1364 return NULL;
1365 }
1366 ssh_options_set(sess, SSH_OPTIONS_USER, pw->pw_name);
1367 } else {
1368 ssh_options_set(sess, SSH_OPTIONS_USER, ssh_ch_opts.username);
Michal Vasko80cad7f2015-12-08 14:42:27 +01001369 }
Michal Vasko086311b2016-01-08 09:53:11 +01001370 if (ssh_options_set(sess, SSH_OPTIONS_HOSTKEYS,
1371 "ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,"
1372 "ecdsa-sha2-nistp256,ssh-rsa,ssh-dss,ssh-rsa1")) {
1373 /* ecdsa is probably not supported... */
1374 ssh_options_set(sess, SSH_OPTIONS_HOSTKEYS, "ssh-ed25519,ssh-rsa,ssh-dss,ssh-rsa1");
1375 }
Michal Vasko80cad7f2015-12-08 14:42:27 +01001376
Michal Vasko80cad7f2015-12-08 14:42:27 +01001377 return nc_connect_libssh(sess, ctx);
1378}