blob: 3f65d4c510c3ab465dbd4824c4787db6c212d9bf [file] [log] [blame]
Radek Krejciac6d3472015-10-22 15:47:18 +02001/**
Michal Vasko086311b2016-01-08 09:53:11 +01002 * \file session_client_ssh.c
Radek Krejciac6d3472015-10-22 15:47:18 +02003 * \author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko086311b2016-01-08 09:53:11 +01004 * \author Michal Vasko <mvasko@cesnet.cz>
5 * \brief libnetconf2 - SSH specific client session transport functions
Radek Krejciac6d3472015-10-22 15:47:18 +02006 *
7 * This source is compiled only with libssh.
8 *
9 * Copyright (c) 2015 CESNET, z.s.p.o.
10 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010011 * This source code is licensed under BSD 3-Clause License (the "License").
12 * You may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010014 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010015 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejciac6d3472015-10-22 15:47:18 +020016 */
17
Michal Vasko7b62fed2015-10-26 15:39:46 +010018#define _GNU_SOURCE
Radek Krejciac6d3472015-10-22 15:47:18 +020019#include <assert.h>
Michal Vasko7b62fed2015-10-26 15:39:46 +010020#include <stdlib.h>
21#include <stddef.h>
22#include <stdio.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020023#include <string.h>
Michal Vasko7b62fed2015-10-26 15:39:46 +010024#include <errno.h>
25#include <fcntl.h>
26#include <termios.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <pwd.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020030#include <unistd.h>
Michal Vasko36c7be82017-02-22 13:37:59 +010031#include <pthread.h>
32#include <time.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020033
Michal Vasko7b62fed2015-10-26 15:39:46 +010034#ifdef ENABLE_DNSSEC
35# include <validator/validator.h>
36# include <validator/resolver.h>
37# include <validator/validator-compat.h>
38#endif
39
Michal Vasko745ff832015-12-08 14:40:29 +010040#include <libssh/libssh.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020041#include <libyang/libyang.h>
42
Michal Vaskoe22c6732016-01-29 11:03:02 +010043#include "session_client.h"
44#include "session_client_ch.h"
Radek Krejciac6d3472015-10-22 15:47:18 +020045#include "libnetconf.h"
46
Radek Krejci62aa0642017-05-25 16:33:49 +020047struct nc_client_context *nc_client_context_location(void);
Radek Krejcifd5b6682017-06-13 15:52:53 +020048int nc_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx);
Michal Vasko30e2c872016-02-18 10:03:21 +010049
Radek Krejci62aa0642017-05-25 16:33:49 +020050#define client_opts nc_client_context_location()->opts
51#define ssh_opts nc_client_context_location()->ssh_opts
52#define ssh_ch_opts nc_client_context_location()->ssh_ch_opts
Michal Vasko3031aae2016-01-27 16:07:18 +010053
Michal Vaskoa43b8e32017-05-12 11:46:20 +020054static FILE *
55open_tty_noecho(const char *path, struct termios *oldterm)
56{
57 struct termios newterm;
58 FILE *ret;
59
60 if (!(ret = fopen(path, "r"))) {
Michal Vasko51228ac2018-03-29 14:57:53 +020061 ERR("Unable to open terminal \"%s\" for reading (%s).", path, strerror(errno));
Michal Vaskoa43b8e32017-05-12 11:46:20 +020062 return NULL;
63 }
64
65 if (tcgetattr(fileno(ret), oldterm)) {
Michal Vasko88583042018-03-29 09:18:58 +020066 ERR("Unable to get terminal \"%s\" settings (%s).", path, strerror(errno));
Michal Vaskoa43b8e32017-05-12 11:46:20 +020067 fclose(ret);
68 return NULL;
69 }
70
71 newterm = *oldterm;
72 newterm.c_lflag &= ~ECHO;
73 newterm.c_lflag &= ~ICANON;
74 tcflush(fileno(ret), TCIFLUSH);
75 if (tcsetattr(fileno(ret), TCSANOW, &newterm)) {
Michal Vasko88583042018-03-29 09:18:58 +020076 ERR("Unable to change terminal \"%s\" settings for hiding password (%s).", path, strerror(errno));
Michal Vaskoa43b8e32017-05-12 11:46:20 +020077 fclose(ret);
78 return NULL;
79 }
80
81 return ret;
82}
83
Michal Vasko51228ac2018-03-29 14:57:53 +020084static FILE *
85nc_open_in(int echo, struct termios *oldterm)
Michal Vaskoa43b8e32017-05-12 11:46:20 +020086{
Michal Vasko51228ac2018-03-29 14:57:53 +020087 char buf[512];
88 int buflen = 512, ret;
89 FILE *in;
90
91 if (!echo) {
92 in = open_tty_noecho("/dev/tty", oldterm);
93 } else {
94 in = fopen("/dev/tty", "r");
95 if (!in) {
96 ERR("Unable to open terminal \"/dev/tty\" for reading (%s).", strerror(errno));
97 }
Michal Vaskoa43b8e32017-05-12 11:46:20 +020098 }
Michal Vasko51228ac2018-03-29 14:57:53 +020099
100 if (!in) {
101 if ((ret = ttyname_r(STDIN_FILENO, buf, buflen))) {
102 ERR("ttyname_r failed (%s).", strerror(ret));
103 return NULL;
104 }
105
106 if (!echo) {
107 in = open_tty_noecho(buf, oldterm);
108 } else {
109 in = fopen(buf, "r");
110 if (!in) {
111 ERR("Unable to open terminal \"%s\" for reading (%s).", buf, strerror(errno));
112 }
113 }
114 }
115
116 return in;
117}
118
119static FILE *
120nc_open_out(void)
121{
122 char buf[512];
123 int buflen = 512, ret;
124 FILE *out;
125
126 out = fopen("/dev/tty", "w");
127 if (!out) {
128 ERR("Unable to open terminal \"/dev/tty\" for writing (%s).", strerror(errno));
129
130 if ((ret = ttyname_r(STDOUT_FILENO, buf, buflen))) {
131 ERR("ttyname_r failed (%s).", strerror(ret));
132 return NULL;
133 }
134
135 out = fopen(buf, "w");
136 if (!out) {
137 ERR("Unable to open terminal \"%s\" for writing (%s).", buf, strerror(errno));
138 }
139 }
140
141 return out;
142}
143
144static void
145nc_close_inout(FILE *inout, int echo, struct termios *oldterm)
146{
147 if (inout) {
148 if (!echo && (tcsetattr(fileno(inout), TCSANOW, oldterm) != 0)) {
149 ERR("Unable to restore terminal settings (%s).", strerror(errno));
150 }
151 fclose(inout);
152 }
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200153}
154
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400155void
Michal Vaskoe22c6732016-01-29 11:03:02 +0100156_nc_client_ssh_destroy_opts(struct nc_client_ssh_opts *opts)
Michal Vasko990089e2015-10-27 15:05:25 +0100157{
158 int i;
159
Michal Vaskoe22c6732016-01-29 11:03:02 +0100160 for (i = 0; i < opts->key_count; ++i) {
161 free(opts->keys[i].pubkey_path);
162 free(opts->keys[i].privkey_path);
Michal Vasko990089e2015-10-27 15:05:25 +0100163 }
Michal Vaskoe22c6732016-01-29 11:03:02 +0100164 free(opts->keys);
165 free(opts->username);
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200166 opts->keys = NULL;
167 opts->username = NULL;
Michal Vaskoe22c6732016-01-29 11:03:02 +0100168}
Michal Vasko990089e2015-10-27 15:05:25 +0100169
Michal Vaskob7558c52016-02-26 15:04:19 +0100170void
Michal Vaskoe22c6732016-01-29 11:03:02 +0100171nc_client_ssh_destroy_opts(void)
172{
173 _nc_client_ssh_destroy_opts(&ssh_opts);
174 _nc_client_ssh_destroy_opts(&ssh_ch_opts);
Michal Vasko990089e2015-10-27 15:05:25 +0100175}
176
Michal Vaskoef112d72016-02-18 13:28:25 +0100177#ifdef ENABLE_DNSSEC
178
179/* return 0 (DNSSEC + key valid), 1 (unsecure DNS + key valid), 2 (key not found or an error) */
180/* type - 1 (RSA), 2 (DSA), 3 (ECDSA); alg - 1 (SHA1), 2 (SHA-256) */
181static int
Michal Vasko650011a2016-02-25 14:49:29 +0100182sshauth_hostkey_hash_dnssec_check(const char *hostname, const unsigned char *sha1hash, int type, int alg) {
Michal Vaskoef112d72016-02-18 13:28:25 +0100183 ns_msg handle;
184 ns_rr rr;
185 val_status_t val_status;
186 const unsigned char* rdata;
187 unsigned char buf[4096];
188 int buf_len = 4096;
189 int ret = 0, i, j, len;
190
191 /* class 1 - internet, type 44 - SSHFP */
192 len = val_res_query(NULL, hostname, 1, 44, buf, buf_len, &val_status);
193
194 if ((len < 0) || !val_istrusted(val_status)) {
195 ret = 2;
196 goto finish;
197 }
198
199 if (ns_initparse(buf, len, &handle) < 0) {
200 ERR("Failed to initialize DNSSEC response parser.");
201 ret = 2;
202 goto finish;
203 }
204
205 if ((i = libsres_msg_getflag(handle, ns_f_rcode))) {
206 ERR("DNSSEC query returned %d.", i);
207 ret = 2;
208 goto finish;
209 }
210
211 if (!libsres_msg_getflag(handle, ns_f_ad)) {
212 /* response not secured by DNSSEC */
213 ret = 1;
214 }
215
216 /* query section */
217 if (ns_parserr(&handle, ns_s_qd, 0, &rr)) {
Michal Vasko650011a2016-02-25 14:49:29 +0100218 ERR("DNSSEC query section parser fail.");
Michal Vaskoef112d72016-02-18 13:28:25 +0100219 ret = 2;
220 goto finish;
221 }
222
223 if (strcmp(hostname, ns_rr_name(rr)) || (ns_rr_type(rr) != 44) || (ns_rr_class(rr) != 1)) {
Michal Vasko650011a2016-02-25 14:49:29 +0100224 ERR("DNSSEC query in the answer does not match the original query.");
Michal Vaskoef112d72016-02-18 13:28:25 +0100225 ret = 2;
226 goto finish;
227 }
228
229 /* answer section */
230 i = 0;
231 while (!ns_parserr(&handle, ns_s_an, i, &rr)) {
232 if (ns_rr_type(rr) != 44) {
233 ++i;
234 continue;
235 }
236
237 rdata = ns_rr_rdata(rr);
238 if (rdata[0] != type) {
239 ++i;
240 continue;
241 }
242 if (rdata[1] != alg) {
243 ++i;
244 continue;
245 }
246
247 /* we found the correct SSHFP entry */
248 rdata += 2;
249 for (j = 0; j < 20; ++j) {
250 if (rdata[j] != (unsigned char)sha1hash[j]) {
251 ret = 2;
252 goto finish;
253 }
254 }
255
256 /* server fingerprint is supported by a DNS entry,
257 * we have already determined if DNSSEC was used or not
258 */
259 goto finish;
260 }
261
262 /* no match */
263 ret = 2;
264
265finish:
266 val_free_validator_state();
267 return ret;
268}
269
270#endif /* ENABLE_DNSSEC */
271
Radek Krejci62aa0642017-05-25 16:33:49 +0200272int
Radek Krejci90a84a22017-05-25 13:53:00 +0200273sshauth_hostkey_check(const char *hostname, ssh_session session, void *UNUSED(priv))
Michal Vaskoef112d72016-02-18 13:28:25 +0100274{
Michal Vasko51228ac2018-03-29 14:57:53 +0200275 char *hexa = NULL;
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800276#if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0))
277 int c, ret;
278 enum ssh_known_hosts_e state;
279#else
Michal Vaskoef112d72016-02-18 13:28:25 +0100280 int c, state, ret;
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800281#endif
Michal Vaskoef112d72016-02-18 13:28:25 +0100282 ssh_key srv_pubkey;
283 unsigned char *hash_sha1 = NULL;
284 size_t hlen;
285 enum ssh_keytypes_e srv_pubkey_type;
286 char answer[5];
Michal Vasko51228ac2018-03-29 14:57:53 +0200287 FILE *out = NULL, *in = NULL;
Michal Vaskoef112d72016-02-18 13:28:25 +0100288
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800289#if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0))
290 state = ssh_session_is_known_server(session);
291#else
Michal Vaskoef112d72016-02-18 13:28:25 +0100292 state = ssh_is_server_known(session);
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800293#endif
Michal Vaskoef112d72016-02-18 13:28:25 +0100294
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800295#if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 8, 0))
296 ret = ssh_get_server_publickey(session, &srv_pubkey);
297#else
Michal Vaskocc0aa7d2016-05-31 12:48:42 +0200298 ret = ssh_get_publickey(session, &srv_pubkey);
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800299#endif
Michal Vaskoef112d72016-02-18 13:28:25 +0100300 if (ret < 0) {
301 ERR("Unable to get server public key.");
302 return -1;
303 }
304
305 srv_pubkey_type = ssh_key_type(srv_pubkey);
306 ret = ssh_get_publickey_hash(srv_pubkey, SSH_PUBLICKEY_HASH_SHA1, &hash_sha1, &hlen);
307 ssh_key_free(srv_pubkey);
308 if (ret < 0) {
309 ERR("Failed to calculate SHA1 hash of the server public key.");
310 return -1;
311 }
312
313 hexa = ssh_get_hexa(hash_sha1, hlen);
314
315 switch (state) {
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800316#if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0))
317 case SSH_KNOWN_HOSTS_OK:
318#else
Michal Vaskoef112d72016-02-18 13:28:25 +0100319 case SSH_SERVER_KNOWN_OK:
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800320#endif
Michal Vaskoef112d72016-02-18 13:28:25 +0100321 break; /* ok */
322
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800323#if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0))
324 case SSH_KNOWN_HOSTS_CHANGED:
325#else
Michal Vaskoef112d72016-02-18 13:28:25 +0100326 case SSH_SERVER_KNOWN_CHANGED:
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800327#endif
Michal Vaskoef112d72016-02-18 13:28:25 +0100328 ERR("Remote host key changed, the connection will be terminated!");
Michal Vasko51228ac2018-03-29 14:57:53 +0200329 goto error;
Michal Vaskoef112d72016-02-18 13:28:25 +0100330
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800331#if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0))
332 case SSH_KNOWN_HOSTS_OTHER:
333#else
Michal Vaskoef112d72016-02-18 13:28:25 +0100334 case SSH_SERVER_FOUND_OTHER:
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800335#endif
Michal Vaskoef112d72016-02-18 13:28:25 +0100336 WRN("Remote host key is not known, but a key of another type for this host is known. Continue with caution.");
337 goto hostkey_not_known;
338
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800339#if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0))
340 case SSH_KNOWN_HOSTS_NOT_FOUND:
341#else
Michal Vaskoef112d72016-02-18 13:28:25 +0100342 case SSH_SERVER_FILE_NOT_FOUND:
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800343#endif
Michal Vaskoef112d72016-02-18 13:28:25 +0100344 WRN("Could not find the known hosts file.");
345 goto hostkey_not_known;
346
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800347#if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0))
348 case SSH_KNOWN_HOSTS_UNKNOWN:
349#else
Michal Vaskoef112d72016-02-18 13:28:25 +0100350 case SSH_SERVER_NOT_KNOWN:
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800351#endif
Michal Vaskoef112d72016-02-18 13:28:25 +0100352hostkey_not_known:
353#ifdef ENABLE_DNSSEC
354 if ((srv_pubkey_type != SSH_KEYTYPE_UNKNOWN) || (srv_pubkey_type != SSH_KEYTYPE_RSA1)) {
355 if (srv_pubkey_type == SSH_KEYTYPE_DSS) {
Michal Vasko650011a2016-02-25 14:49:29 +0100356 ret = sshauth_hostkey_hash_dnssec_check(hostname, hash_sha1, 2, 1);
Michal Vaskoef112d72016-02-18 13:28:25 +0100357 } else if (srv_pubkey_type == SSH_KEYTYPE_RSA) {
Michal Vasko650011a2016-02-25 14:49:29 +0100358 ret = sshauth_hostkey_hash_dnssec_check(hostname, hash_sha1, 1, 1);
Michal Vaskoef112d72016-02-18 13:28:25 +0100359 } else if (srv_pubkey_type == SSH_KEYTYPE_ECDSA) {
Michal Vasko650011a2016-02-25 14:49:29 +0100360 ret = sshauth_hostkey_hash_dnssec_check(hostname, hash_sha1, 3, 1);
Michal Vaskoef112d72016-02-18 13:28:25 +0100361 }
362
363 /* DNSSEC SSHFP check successful, that's enough */
364 if (!ret) {
365 VRB("DNSSEC SSHFP check successful.");
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800366#if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0))
367 ssh_session_update_known_hosts(session);
368#else
Michal Vaskoef112d72016-02-18 13:28:25 +0100369 ssh_write_knownhost(session);
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800370#endif
Michal Vaskoef112d72016-02-18 13:28:25 +0100371 ssh_clean_pubkey_hash(&hash_sha1);
372 ssh_string_free_char(hexa);
373 return 0;
374 }
375 }
376#endif
377
Michal Vasko51228ac2018-03-29 14:57:53 +0200378 if (!(in = nc_open_in(1, NULL))) {
379 goto error;
380 }
381 if (!(out = nc_open_out())) {
382 goto error;
383 }
384
Michal Vaskoef112d72016-02-18 13:28:25 +0100385 /* try to get result from user */
Michal Vasko51228ac2018-03-29 14:57:53 +0200386 if (fprintf(out, "The authenticity of the host \'%s\' cannot be established.\n", hostname) < 1) {
387 ERR("Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
388 goto error;
389 }
390 if (fprintf(out, "%s key fingerprint is %s.\n", ssh_key_type_to_char(srv_pubkey_type), hexa) < 1) {
391 ERR("Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
392 goto error;
393 }
Michal Vaskoef112d72016-02-18 13:28:25 +0100394
395#ifdef ENABLE_DNSSEC
396 if (ret == 2) {
Michal Vasko51228ac2018-03-29 14:57:53 +0200397 if (fprintf(out, "No matching host key fingerprint found using DNS.\n") < 1) {
398 ERR("Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
399 goto error;
400 }
Michal Vaskoef112d72016-02-18 13:28:25 +0100401 } else if (ret == 1) {
Michal Vasko51228ac2018-03-29 14:57:53 +0200402 if (fprintf(out, "Matching host key fingerprint found using DNS.\n") < 1) {
403 ERR("Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
404 goto error;
405 }
Michal Vaskoef112d72016-02-18 13:28:25 +0100406 }
407#endif
408
Michal Vasko51228ac2018-03-29 14:57:53 +0200409 if (fprintf(out, "Are you sure you want to continue connecting (yes/no)? ") < 1) {
410 ERR("Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
411 goto error;
412 }
413 fflush(out);
Michal Vaskoef112d72016-02-18 13:28:25 +0100414
415 do {
Michal Vasko51228ac2018-03-29 14:57:53 +0200416 if (fscanf(in, "%4s", answer) == EOF) {
417 ERR("Reading from input failed (%s).", feof(in) ? "EOF" : strerror(errno));
418 goto error;
Michal Vaskoef112d72016-02-18 13:28:25 +0100419 }
Michal Vasko51228ac2018-03-29 14:57:53 +0200420 while (((c = getc(in)) != EOF) && (c != '\n'));
Michal Vaskoef112d72016-02-18 13:28:25 +0100421
Michal Vasko51228ac2018-03-29 14:57:53 +0200422 fflush(in);
Michal Vaskoef112d72016-02-18 13:28:25 +0100423 if (!strcmp("yes", answer)) {
424 /* store the key into the host file */
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800425#if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0))
426 ret = ssh_session_update_known_hosts(session);
427#else
Michal Vaskoef112d72016-02-18 13:28:25 +0100428 ret = ssh_write_knownhost(session);
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800429#endif
Michal Vaskoef112d72016-02-18 13:28:25 +0100430 if (ret != SSH_OK) {
431 WRN("Adding the known host \"%s\" failed (%s).", hostname, ssh_get_error(session));
432 }
433 } else if (!strcmp("no", answer)) {
Michal Vasko51228ac2018-03-29 14:57:53 +0200434 goto error;
Michal Vaskoef112d72016-02-18 13:28:25 +0100435 } else {
Michal Vasko51228ac2018-03-29 14:57:53 +0200436 if (fprintf(out, "Please type 'yes' or 'no': ") < 1) {
437 ERR("Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
438 goto error;
439 }
Michal Vasko5fb5f992020-11-26 15:19:31 +0100440 fflush(out);
Michal Vaskoef112d72016-02-18 13:28:25 +0100441 }
442 } while (strcmp(answer, "yes") && strcmp(answer, "no"));
443
444 break;
445
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800446#if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0))
447 case SSH_KNOWN_HOSTS_ERROR:
448#else
Michal Vaskoef112d72016-02-18 13:28:25 +0100449 case SSH_SERVER_ERROR:
Bi-Ruei, Chiu947d8122019-09-08 19:28:05 +0800450#endif
Michal Vasko3b49eb22018-05-24 09:17:49 +0200451 ERR("SSH error: %s", ssh_get_error(session));
Michal Vasko51228ac2018-03-29 14:57:53 +0200452 goto error;
Michal Vaskoef112d72016-02-18 13:28:25 +0100453 }
454
Michal Vasko51228ac2018-03-29 14:57:53 +0200455 nc_close_inout(in, 1, NULL);
456 nc_close_inout(out, 1, NULL);
Michal Vaskoef112d72016-02-18 13:28:25 +0100457 ssh_clean_pubkey_hash(&hash_sha1);
458 ssh_string_free_char(hexa);
459 return 0;
460
Michal Vasko51228ac2018-03-29 14:57:53 +0200461error:
462 nc_close_inout(in, 1, NULL);
463 nc_close_inout(out, 1, NULL);
Michal Vaskoef112d72016-02-18 13:28:25 +0100464 ssh_clean_pubkey_hash(&hash_sha1);
465 ssh_string_free_char(hexa);
466 return -1;
467}
468
Radek Krejci62aa0642017-05-25 16:33:49 +0200469char *
Radek Krejci90a84a22017-05-25 13:53:00 +0200470sshauth_password(const char *username, const char *hostname, void *UNUSED(priv))
Radek Krejciac6d3472015-10-22 15:47:18 +0200471{
Michal Vasko51228ac2018-03-29 14:57:53 +0200472 char *buf = NULL;
473 int c, buflen = 1024, len;
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200474 struct termios oldterm;
Michal Vasko51228ac2018-03-29 14:57:53 +0200475 FILE *in = NULL, *out = NULL;
Radek Krejciac6d3472015-10-22 15:47:18 +0200476
Michal Vasko11d142a2016-01-19 15:58:24 +0100477 buf = malloc(buflen * sizeof *buf);
478 if (!buf) {
479 ERRMEM;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100480 return NULL;
481 }
482
Michal Vasko51228ac2018-03-29 14:57:53 +0200483 if (!(in = nc_open_in(0, &oldterm))) {
484 goto error;
485 }
486 if (!(out = nc_open_out())) {
487 goto error;
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200488 }
489
Michal Vasko51228ac2018-03-29 14:57:53 +0200490 if (fprintf(out, "%s@%s password: ", username, hostname) < 1) {
491 ERR("Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
492 goto error;
Michal Vasko88583042018-03-29 09:18:58 +0200493 }
Michal Vasko51228ac2018-03-29 14:57:53 +0200494 fflush(out);
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200495
496 len = 0;
Michal Vasko51228ac2018-03-29 14:57:53 +0200497 while (((c = fgetc(in)) != EOF) && (c != '\n')) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100498 if (len >= buflen - 1) {
499 buflen *= 2;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100500 buf = nc_realloc(buf, buflen * sizeof *buf);
501 if (!buf) {
Michal Vaskod083db62016-01-19 10:31:29 +0100502 ERRMEM;
Michal Vasko51228ac2018-03-29 14:57:53 +0200503 goto error;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100504 }
505 }
Michal Vasko93ab6172018-02-16 15:58:12 +0100506 buf[len++] = (char)c;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100507 }
508 buf[len++] = 0; /* terminating null byte */
509
Michal Vasko51228ac2018-03-29 14:57:53 +0200510 fprintf(out, "\n");
511
512 nc_close_inout(in, 0, &oldterm);
513 nc_close_inout(out, 1, NULL);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100514 return buf;
Michal Vasko93f26d92018-02-01 09:08:35 +0100515
Michal Vasko51228ac2018-03-29 14:57:53 +0200516error:
517 nc_close_inout(in, 0, &oldterm);
518 nc_close_inout(out, 1, NULL);
Michal Vasko93f26d92018-02-01 09:08:35 +0100519 free(buf);
520 return NULL;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100521}
522
Radek Krejci62aa0642017-05-25 16:33:49 +0200523char *
Radek Krejci90a84a22017-05-25 13:53:00 +0200524sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *UNUSED(priv))
Michal Vasko7b62fed2015-10-26 15:39:46 +0100525{
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200526 unsigned int buflen = 64, cur_len;
Michal Vasko51228ac2018-03-29 14:57:53 +0200527 int c;
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200528 struct termios oldterm;
Michal Vasko51228ac2018-03-29 14:57:53 +0200529 char *buf = NULL;
530 FILE *in = NULL, *out = NULL;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100531
532 buf = malloc(buflen * sizeof *buf);
533 if (!buf) {
Michal Vaskod083db62016-01-19 10:31:29 +0100534 ERRMEM;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100535 return NULL;
536 }
537
Michal Vasko51228ac2018-03-29 14:57:53 +0200538 if (!(in = nc_open_in(echo, &oldterm))) {
539 goto error;
540 }
541 if (!(out = nc_open_out())) {
542 goto error;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100543 }
544
Michal Vasko51228ac2018-03-29 14:57:53 +0200545 if (auth_name && (fprintf(out, "%s\n", auth_name) < 1)) {
546 ERR("Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
547 goto error;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100548 }
Michal Vasko51228ac2018-03-29 14:57:53 +0200549 if (instruction && (fprintf(out, "%s\n", instruction) < 1)) {
550 ERR("Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
551 goto error;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100552 }
Michal Vasko51228ac2018-03-29 14:57:53 +0200553 if (fputs(prompt, out) == EOF) {
554 ERR("Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
555 goto error;
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200556 }
Michal Vasko51228ac2018-03-29 14:57:53 +0200557 fflush(out);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100558
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200559 cur_len = 0;
Michal Vasko51228ac2018-03-29 14:57:53 +0200560 while (((c = fgetc(in)) != EOF) && (c != '\n')) {
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200561 if (cur_len >= buflen - 1) {
562 buflen *= 2;
563 buf = nc_realloc(buf, buflen * sizeof *buf);
564 if (!buf) {
565 ERRMEM;
Michal Vasko51228ac2018-03-29 14:57:53 +0200566 goto error;
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200567 }
568 }
Michal Vasko93ab6172018-02-16 15:58:12 +0100569 buf[cur_len++] = (char)c;
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200570 }
571 /* terminating null byte */
572 buf[cur_len] = '\0';
573
Michal Vasko51228ac2018-03-29 14:57:53 +0200574 fprintf(out, "\n");
575
576 nc_close_inout(in, echo, &oldterm);
577 nc_close_inout(out, 1, NULL);
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200578 return buf;
579
Michal Vasko51228ac2018-03-29 14:57:53 +0200580error:
581 nc_close_inout(in, echo, &oldterm);
582 nc_close_inout(out, 1, NULL);
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200583 free(buf);
584 return NULL;
585}
586
Radek Krejci62aa0642017-05-25 16:33:49 +0200587char *
Radek Krejci90a84a22017-05-25 13:53:00 +0200588sshauth_privkey_passphrase(const char* privkey_path, void *UNUSED(priv))
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200589{
Michal Vasko51228ac2018-03-29 14:57:53 +0200590 char *buf = NULL;
591 int c, buflen = 1024, len;
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200592 struct termios oldterm;
Michal Vasko51228ac2018-03-29 14:57:53 +0200593 FILE *in = NULL, *out = NULL;
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200594
595 buf = malloc(buflen * sizeof *buf);
596 if (!buf) {
597 ERRMEM;
598 return NULL;
599 }
600
Michal Vasko51228ac2018-03-29 14:57:53 +0200601 if (!(in = nc_open_in(0, &oldterm))) {
602 goto error;
603 }
604 if (!(out = nc_open_out())) {
605 goto error;
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200606 }
607
Michal Vasko51228ac2018-03-29 14:57:53 +0200608 if (fprintf(out, "Enter passphrase for the key '%s': ", privkey_path) < 1) {
609 ERR("Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno));
610 goto error;
Michal Vasko88583042018-03-29 09:18:58 +0200611 }
Michal Vasko51228ac2018-03-29 14:57:53 +0200612 fflush(out);
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200613
614 len = 0;
Michal Vasko51228ac2018-03-29 14:57:53 +0200615 while (((c = fgetc(in)) != EOF) && (c != '\n')) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100616 if (len >= buflen - 1) {
617 buflen *= 2;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100618 buf = nc_realloc(buf, buflen * sizeof *buf);
619 if (!buf) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100620 ERRMEM;
Michal Vasko51228ac2018-03-29 14:57:53 +0200621 goto error;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100622 }
Michal Vasko7b62fed2015-10-26 15:39:46 +0100623 }
624 buf[len++] = (char)c;
625 }
Michal Vaskoa43b8e32017-05-12 11:46:20 +0200626 buf[len] = 0; /* terminating null byte */
Michal Vasko7b62fed2015-10-26 15:39:46 +0100627
Michal Vasko51228ac2018-03-29 14:57:53 +0200628 fprintf(out, "\n");
629
630 nc_close_inout(in, 0, &oldterm);
631 nc_close_inout(out, 1, NULL);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100632 return buf;
Michal Vasko94e7c2d2016-01-21 15:57:57 +0100633
Michal Vasko51228ac2018-03-29 14:57:53 +0200634error:
635 nc_close_inout(in, 0, &oldterm);
636 nc_close_inout(out, 1, NULL);
Michal Vasko94e7c2d2016-01-21 15:57:57 +0100637 free(buf);
Michal Vasko94e7c2d2016-01-21 15:57:57 +0100638 return NULL;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100639}
640
Michal Vaskoef112d72016-02-18 13:28:25 +0100641static void
Radek Krejci90a84a22017-05-25 13:53:00 +0200642_nc_client_ssh_set_auth_hostkey_check_clb(int (*auth_hostkey_check)(const char *hostname, ssh_session session, void *priv),
643 void *priv, struct nc_client_ssh_opts *opts)
Michal Vasko7b62fed2015-10-26 15:39:46 +0100644{
Michal Vaskoef112d72016-02-18 13:28:25 +0100645 if (auth_hostkey_check) {
646 opts->auth_hostkey_check = auth_hostkey_check;
Radek Krejci90a84a22017-05-25 13:53:00 +0200647 opts->auth_hostkey_check_priv = priv;
Michal Vaskoef112d72016-02-18 13:28:25 +0100648 } else {
649 opts->auth_hostkey_check = sshauth_hostkey_check;
Radek Krejci90a84a22017-05-25 13:53:00 +0200650 opts->auth_hostkey_check_priv = NULL;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100651 }
Michal Vasko7b62fed2015-10-26 15:39:46 +0100652}
653
Radek Krejci90a84a22017-05-25 13:53:00 +0200654static void
655_nc_client_ssh_get_auth_hostkey_check_clb(int (**auth_hostkey_check)(const char *hostname, ssh_session session, void *priv),
656 void **priv, struct nc_client_ssh_opts *opts)
Michal Vaskoef112d72016-02-18 13:28:25 +0100657{
Radek Krejci90a84a22017-05-25 13:53:00 +0200658 if (auth_hostkey_check) {
659 (*auth_hostkey_check) = opts->auth_hostkey_check == sshauth_hostkey_check ? NULL : opts->auth_hostkey_check;
660 }
661 if (priv) {
662 (*priv) = opts->auth_hostkey_check_priv;
663 }
664}
665
666
667API void
668nc_client_ssh_set_auth_hostkey_check_clb(int (*auth_hostkey_check)(const char *hostname, ssh_session session, void *priv),
669 void *priv)
670{
671 _nc_client_ssh_set_auth_hostkey_check_clb(auth_hostkey_check, priv, &ssh_opts);
Michal Vaskoef112d72016-02-18 13:28:25 +0100672}
673
674API void
Radek Krejci90a84a22017-05-25 13:53:00 +0200675nc_client_ssh_ch_set_auth_hostkey_check_clb(int (*auth_hostkey_check)(const char *hostname, ssh_session session, void *priv),
676 void *priv)
Michal Vaskoef112d72016-02-18 13:28:25 +0100677{
Radek Krejci90a84a22017-05-25 13:53:00 +0200678 _nc_client_ssh_set_auth_hostkey_check_clb(auth_hostkey_check, priv, &ssh_ch_opts);
Michal Vaskoef112d72016-02-18 13:28:25 +0100679}
680
Radek Krejci90a84a22017-05-25 13:53:00 +0200681API void
682nc_client_ssh_get_auth_hostkey_check_clb(int (**auth_hostkey_check)(const char *hostname, ssh_session session, void *priv),
683 void **priv)
684{
685 _nc_client_ssh_get_auth_hostkey_check_clb(auth_hostkey_check, priv, &ssh_opts);
686}
687
688API void
689nc_client_ssh_ch_get_auth_hostkey_check_clb(int (**auth_hostkey_check)(const char *hostname, ssh_session session, void *priv),
690 void **priv)
691{
692 _nc_client_ssh_get_auth_hostkey_check_clb(auth_hostkey_check, priv, &ssh_ch_opts);
693}
Michal Vaskoef112d72016-02-18 13:28:25 +0100694
Michal Vasko30e2c872016-02-18 10:03:21 +0100695static void
Radek Krejci90a84a22017-05-25 13:53:00 +0200696_nc_client_ssh_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname, void *priv),
697 void *priv, struct nc_client_ssh_opts *opts)
Michal Vasko30e2c872016-02-18 10:03:21 +0100698{
699 if (auth_password) {
700 opts->auth_password = auth_password;
Radek Krejci90a84a22017-05-25 13:53:00 +0200701 opts->auth_password_priv = priv;
Michal Vasko30e2c872016-02-18 10:03:21 +0100702 } else {
703 opts->auth_password = sshauth_password;
Radek Krejci90a84a22017-05-25 13:53:00 +0200704 opts->auth_password_priv = NULL;
705 }
706}
707
708static void
709_nc_client_ssh_get_auth_password_clb(char *(**auth_password)(const char *username, const char *hostname, void *priv),
710 void **priv, struct nc_client_ssh_opts *opts)
711{
712 if (auth_password) {
713 (*auth_password) = opts->auth_password == sshauth_password ? NULL : opts->auth_password;
714 }
715 if (priv) {
716 (*priv) = opts->auth_password_priv;
Michal Vasko30e2c872016-02-18 10:03:21 +0100717 }
718}
719
720API void
Radek Krejci90a84a22017-05-25 13:53:00 +0200721nc_client_ssh_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname, void *priv),
722 void *priv)
Michal Vasko30e2c872016-02-18 10:03:21 +0100723{
Radek Krejci90a84a22017-05-25 13:53:00 +0200724 _nc_client_ssh_set_auth_password_clb(auth_password, priv, &ssh_opts);
Michal Vasko30e2c872016-02-18 10:03:21 +0100725}
726
727API void
Radek Krejci90a84a22017-05-25 13:53:00 +0200728nc_client_ssh_ch_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname, void *priv),
729 void *priv)
Michal Vasko30e2c872016-02-18 10:03:21 +0100730{
Radek Krejci90a84a22017-05-25 13:53:00 +0200731 _nc_client_ssh_set_auth_password_clb(auth_password, priv, &ssh_ch_opts);
732}
733
734API void
735nc_client_ssh_get_auth_password_clb(char *(**auth_password)(const char *username, const char *hostname, void *priv),
736 void **priv)
737{
738 _nc_client_ssh_get_auth_password_clb(auth_password, priv, &ssh_opts);
739}
740
741API void
742nc_client_ssh_ch_get_auth_password_clb(char *(**auth_password)(const char *username, const char *hostname, void *priv),
743 void **priv)
744{
745 _nc_client_ssh_get_auth_password_clb(auth_password, priv, &ssh_ch_opts);
Michal Vasko30e2c872016-02-18 10:03:21 +0100746}
747
748static void
749_nc_client_ssh_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction,
Radek Krejci90a84a22017-05-25 13:53:00 +0200750 const char *prompt, int echo, void *priv),
751 void *priv, struct nc_client_ssh_opts *opts)
Michal Vasko30e2c872016-02-18 10:03:21 +0100752{
753 if (auth_interactive) {
754 opts->auth_interactive = auth_interactive;
Radek Krejci90a84a22017-05-25 13:53:00 +0200755 opts->auth_interactive_priv = priv;
Michal Vasko30e2c872016-02-18 10:03:21 +0100756 } else {
757 opts->auth_interactive = sshauth_interactive;
Radek Krejci90a84a22017-05-25 13:53:00 +0200758 opts->auth_interactive_priv = NULL;
759 }
760}
761
762static void
763_nc_client_ssh_get_auth_interactive_clb(char *(**auth_interactive)(const char *auth_name, const char *instruction,
764 const char *prompt, int echo, void *priv),
765 void **priv, struct nc_client_ssh_opts *opts)
766{
767 if (auth_interactive) {
768 (*auth_interactive) = opts->auth_interactive == sshauth_interactive ? NULL : opts->auth_interactive;
769 }
770 if (priv) {
771 (*priv) = opts->auth_interactive_priv;
Michal Vasko30e2c872016-02-18 10:03:21 +0100772 }
773}
774
775API void
776nc_client_ssh_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction,
Radek Krejci90a84a22017-05-25 13:53:00 +0200777 const char *prompt, int echo, void *priv),
778 void *priv)
Michal Vasko30e2c872016-02-18 10:03:21 +0100779{
Radek Krejci90a84a22017-05-25 13:53:00 +0200780 _nc_client_ssh_set_auth_interactive_clb(auth_interactive, priv, &ssh_opts);
Michal Vasko30e2c872016-02-18 10:03:21 +0100781}
782
783API void
784nc_client_ssh_ch_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction,
Radek Krejci90a84a22017-05-25 13:53:00 +0200785 const char *prompt, int echo, void *priv),
786 void *priv)
Michal Vasko30e2c872016-02-18 10:03:21 +0100787{
Radek Krejci90a84a22017-05-25 13:53:00 +0200788 _nc_client_ssh_set_auth_interactive_clb(auth_interactive, priv, &ssh_ch_opts);
789}
790
791API void
792nc_client_ssh_get_auth_interactive_clb(char *(**auth_interactive)(const char *auth_name, const char *instruction,
793 const char *prompt, int echo, void *priv),
794 void **priv)
795{
796 _nc_client_ssh_get_auth_interactive_clb(auth_interactive, priv, &ssh_opts);
797}
798
799API void
800nc_client_ssh_ch_get_auth_interactive_clb(char *(**auth_interactive)(const char *auth_name, const char *instruction,
801 const char *prompt, int echo, void *priv),
802 void **priv)
803{
804 _nc_client_ssh_get_auth_interactive_clb(auth_interactive, priv, &ssh_ch_opts);
Michal Vasko30e2c872016-02-18 10:03:21 +0100805}
806
807static void
Radek Krejci90a84a22017-05-25 13:53:00 +0200808_nc_client_ssh_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path, void *priv),
809 void *priv, struct nc_client_ssh_opts *opts)
Michal Vasko30e2c872016-02-18 10:03:21 +0100810{
811 if (auth_privkey_passphrase) {
812 opts->auth_privkey_passphrase = auth_privkey_passphrase;
Radek Krejci90a84a22017-05-25 13:53:00 +0200813 opts->auth_privkey_passphrase_priv = priv;
Michal Vasko30e2c872016-02-18 10:03:21 +0100814 } else {
815 opts->auth_privkey_passphrase = sshauth_privkey_passphrase;
Radek Krejci90a84a22017-05-25 13:53:00 +0200816 opts->auth_privkey_passphrase_priv = NULL;
817 }
818}
819
820static void
821_nc_client_ssh_get_auth_privkey_passphrase_clb(char *(**auth_privkey_passphrase)(const char *privkey_path, void *priv),
822 void **priv, struct nc_client_ssh_opts *opts)
823{
824 if (auth_privkey_passphrase) {
825 (*auth_privkey_passphrase) = opts->auth_privkey_passphrase == sshauth_privkey_passphrase ? NULL : opts->auth_privkey_passphrase;
826 }
827 if (priv) {
828 (*priv) = opts->auth_privkey_passphrase_priv;
Michal Vasko30e2c872016-02-18 10:03:21 +0100829 }
830}
831
832API void
Radek Krejci90a84a22017-05-25 13:53:00 +0200833nc_client_ssh_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path, void *priv),
834 void *priv)
Michal Vasko30e2c872016-02-18 10:03:21 +0100835{
Radek Krejci90a84a22017-05-25 13:53:00 +0200836 _nc_client_ssh_set_auth_privkey_passphrase_clb(auth_privkey_passphrase, priv, &ssh_opts);
Michal Vasko30e2c872016-02-18 10:03:21 +0100837}
838
839API void
Radek Krejci90a84a22017-05-25 13:53:00 +0200840nc_client_ssh_ch_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path, void *priv),
841 void *priv)
Michal Vasko30e2c872016-02-18 10:03:21 +0100842{
Radek Krejci90a84a22017-05-25 13:53:00 +0200843 _nc_client_ssh_set_auth_privkey_passphrase_clb(auth_privkey_passphrase, priv, &ssh_ch_opts);
844}
845
846API void
847nc_client_ssh_get_auth_privkey_passphrase_clb(char *(**auth_privkey_passphrase)(const char *privkey_path, void *priv),
848 void **priv)
849{
850 _nc_client_ssh_get_auth_privkey_passphrase_clb(auth_privkey_passphrase, priv, &ssh_opts);
851}
852
853API void
854nc_client_ssh_ch_get_auth_privkey_passphrase_clb(char *(**auth_privkey_passphrase)(const char *privkey_path, void *priv),
855 void **priv)
856{
857 _nc_client_ssh_get_auth_privkey_passphrase_clb(auth_privkey_passphrase, priv, &ssh_ch_opts);
Michal Vasko30e2c872016-02-18 10:03:21 +0100858}
859
Michal Vasko3031aae2016-01-27 16:07:18 +0100860static int
861_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 +0100862{
863 int i;
864 FILE *key;
865 char line[128];
866
Michal Vasko45e53ae2016-04-07 11:46:03 +0200867 if (!pub_key) {
868 ERRARG("pub_key");
869 return -1;
870 } else if (!priv_key) {
871 ERRARG("priv_key");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100872 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100873 }
874
Michal Vasko3031aae2016-01-27 16:07:18 +0100875 for (i = 0; i < opts->key_count; ++i) {
876 if (!strcmp(opts->keys[i].pubkey_path, pub_key) || !strcmp(opts->keys[i].privkey_path, priv_key)) {
877 if (strcmp(opts->keys[i].pubkey_path, pub_key)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100878 WRN("Private key \"%s\" found with another public key \"%s\".",
Michal Vasko3031aae2016-01-27 16:07:18 +0100879 priv_key, opts->keys[i].pubkey_path);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100880 continue;
Michal Vasko3031aae2016-01-27 16:07:18 +0100881 } else if (strcmp(opts->keys[i].privkey_path, priv_key)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100882 WRN("Public key \"%s\" found with another private key \"%s\".",
Michal Vasko3031aae2016-01-27 16:07:18 +0100883 pub_key, opts->keys[i].privkey_path);
Michal Vasko7b62fed2015-10-26 15:39:46 +0100884 continue;
885 }
886
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100887 ERR("SSH key pair already set.");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100888 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100889 }
890 }
891
Michal Vasko3031aae2016-01-27 16:07:18 +0100892 /* add the keys */
893 ++opts->key_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100894 opts->keys = nc_realloc(opts->keys, opts->key_count * sizeof *opts->keys);
895 if (!opts->keys) {
896 ERRMEM;
897 return -1;
898 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100899 opts->keys[opts->key_count - 1].pubkey_path = strdup(pub_key);
900 opts->keys[opts->key_count - 1].privkey_path = strdup(priv_key);
901 opts->keys[opts->key_count - 1].privkey_crypt = 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100902
Michal Vasko4eb3c312016-03-01 14:09:37 +0100903 if (!opts->keys[opts->key_count - 1].pubkey_path || !opts->keys[opts->key_count - 1].privkey_path) {
904 ERRMEM;
905 return -1;
906 }
907
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100908 /* check encryption */
909 if ((key = fopen(priv_key, "r"))) {
910 /* 1st line - key type */
911 if (!fgets(line, sizeof line, key)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +0100912 fclose(key);
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100913 ERR("fgets() on %s failed.", priv_key);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100914 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100915 }
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100916 /* 2nd line - encryption information or key */
917 if (!fgets(line, sizeof line, key)) {
918 fclose(key);
919 ERR("fgets() on %s failed.", priv_key);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100920 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100921 }
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100922 fclose(key);
923 if (strcasestr(line, "encrypted")) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100924 opts->keys[opts->key_count - 1].privkey_crypt = 1;
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100925 }
Michal Vasko7b62fed2015-10-26 15:39:46 +0100926 }
927
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100928 return 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100929}
930
931API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100932nc_client_ssh_add_keypair(const char *pub_key, const char *priv_key)
Michal Vasko7b62fed2015-10-26 15:39:46 +0100933{
Michal Vasko3031aae2016-01-27 16:07:18 +0100934 return _nc_client_ssh_add_keypair(pub_key, priv_key, &ssh_opts);
935}
936
937API int
938nc_client_ssh_ch_add_keypair(const char *pub_key, const char *priv_key)
939{
940 return _nc_client_ssh_add_keypair(pub_key, priv_key, &ssh_ch_opts);
941}
942
943static int
944_nc_client_ssh_del_keypair(int idx, struct nc_client_ssh_opts *opts)
945{
946 if (idx >= opts->key_count) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200947 ERRARG("idx");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100948 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100949 }
950
Michal Vasko3031aae2016-01-27 16:07:18 +0100951 free(opts->keys[idx].pubkey_path);
952 free(opts->keys[idx].privkey_path);
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100953
Michal Vasko3031aae2016-01-27 16:07:18 +0100954 --opts->key_count;
Michal Vaskoc0256492016-02-02 12:19:06 +0100955 if (idx < opts->key_count) {
956 memcpy(&opts->keys[idx], &opts->keys[opts->key_count], sizeof *opts->keys);
957 }
958 if (opts->key_count) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100959 opts->keys = nc_realloc(opts->keys, opts->key_count * sizeof *opts->keys);
960 if (!opts->keys) {
961 ERRMEM;
962 return -1;
963 }
Michal Vaskoc0256492016-02-02 12:19:06 +0100964 } else {
965 free(opts->keys);
966 opts->keys = NULL;
967 }
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100968
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100969 return 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +0100970}
971
972API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100973nc_client_ssh_del_keypair(int idx)
Michal Vasko7b62fed2015-10-26 15:39:46 +0100974{
Michal Vasko3031aae2016-01-27 16:07:18 +0100975 return _nc_client_ssh_del_keypair(idx, &ssh_opts);
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100976}
977
978API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100979nc_client_ssh_ch_del_keypair(int idx)
Michal Vaskoe9bc8142015-12-04 11:09:35 +0100980{
Michal Vasko3031aae2016-01-27 16:07:18 +0100981 return _nc_client_ssh_del_keypair(idx, &ssh_ch_opts);
982}
983
984static int
985_nc_client_ssh_get_keypair_count(struct nc_client_ssh_opts *opts)
986{
987 return opts->key_count;
988}
989
990API int
991nc_client_ssh_get_keypair_count(void)
992{
993 return _nc_client_ssh_get_keypair_count(&ssh_opts);
994}
995
996API int
997nc_client_ssh_ch_get_keypair_count(void)
998{
999 return _nc_client_ssh_get_keypair_count(&ssh_ch_opts);
1000}
1001
1002static int
1003_nc_client_ssh_get_keypair(int idx, const char **pub_key, const char **priv_key, struct nc_client_ssh_opts *opts)
1004{
Michal Vasko45e53ae2016-04-07 11:46:03 +02001005 if (idx >= opts->key_count) {
1006 ERRARG("idx");
1007 return -1;
1008 } else if (!pub_key && !priv_key) {
1009 ERRARG("pub_key and priv_key");
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001010 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001011 }
1012
Michal Vaskoe9bc8142015-12-04 11:09:35 +01001013 if (pub_key) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001014 *pub_key = opts->keys[idx].pubkey_path;
Michal Vaskoe9bc8142015-12-04 11:09:35 +01001015 }
1016 if (priv_key) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001017 *priv_key = opts->keys[idx].privkey_path;
Michal Vaskoe9bc8142015-12-04 11:09:35 +01001018 }
1019
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001020 return 0;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001021}
1022
Michal Vasko3031aae2016-01-27 16:07:18 +01001023API int
1024nc_client_ssh_get_keypair(int idx, const char **pub_key, const char **priv_key)
1025{
1026 return _nc_client_ssh_get_keypair(idx, pub_key, priv_key, &ssh_opts);
1027}
1028
1029API int
1030nc_client_ssh_ch_get_keypair(int idx, const char **pub_key, const char **priv_key)
1031{
1032 return _nc_client_ssh_get_keypair(idx, pub_key, priv_key, &ssh_ch_opts);
1033}
1034
1035static void
1036_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 +01001037{
1038 if (pref < 0) {
1039 pref = -1;
1040 }
1041
1042 if (auth_type == NC_SSH_AUTH_INTERACTIVE) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001043 opts->auth_pref[0].value = pref;
Michal Vaskoe9bc8142015-12-04 11:09:35 +01001044 } else if (auth_type == NC_SSH_AUTH_PASSWORD) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001045 opts->auth_pref[1].value = pref;
Michal Vaskoe9bc8142015-12-04 11:09:35 +01001046 } else if (auth_type == NC_SSH_AUTH_PUBLICKEY) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001047 opts->auth_pref[2].value = pref;
Michal Vaskoe9bc8142015-12-04 11:09:35 +01001048 }
1049}
1050
Michal Vasko3031aae2016-01-27 16:07:18 +01001051API void
1052nc_client_ssh_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref)
Michal Vaskoe9bc8142015-12-04 11:09:35 +01001053{
Michal Vasko3031aae2016-01-27 16:07:18 +01001054 _nc_client_ssh_set_auth_pref(auth_type, pref, &ssh_opts);
1055}
1056
1057API void
1058nc_client_ssh_ch_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref)
1059{
1060 _nc_client_ssh_set_auth_pref(auth_type, pref, &ssh_ch_opts);
1061}
1062
1063static int16_t
1064_nc_client_ssh_get_auth_pref(NC_SSH_AUTH_TYPE auth_type, struct nc_client_ssh_opts *opts)
1065{
1066 int16_t pref = 0;
Michal Vaskoe9bc8142015-12-04 11:09:35 +01001067
1068 if (auth_type == NC_SSH_AUTH_INTERACTIVE) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001069 pref = opts->auth_pref[0].value;
Michal Vaskoe9bc8142015-12-04 11:09:35 +01001070 } else if (auth_type == NC_SSH_AUTH_PASSWORD) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001071 pref = opts->auth_pref[1].value;
Michal Vaskoe9bc8142015-12-04 11:09:35 +01001072 } else if (auth_type == NC_SSH_AUTH_PUBLICKEY) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001073 pref = opts->auth_pref[2].value;
Michal Vaskoe9bc8142015-12-04 11:09:35 +01001074 }
1075
1076 return pref;
1077}
1078
Michal Vasko3031aae2016-01-27 16:07:18 +01001079API int16_t
1080nc_client_ssh_get_auth_pref(NC_SSH_AUTH_TYPE auth_type)
1081{
1082 return _nc_client_ssh_get_auth_pref(auth_type, &ssh_opts);
1083}
1084
1085API int16_t
1086nc_client_ssh_ch_get_auth_pref(NC_SSH_AUTH_TYPE auth_type)
1087{
1088 return _nc_client_ssh_get_auth_pref(auth_type, &ssh_ch_opts);
1089}
1090
1091static int
1092_nc_client_ssh_set_username(const char *username, struct nc_client_ssh_opts *opts)
1093{
1094 if (opts->username) {
1095 free(opts->username);
1096 }
1097 if (username) {
1098 opts->username = strdup(username);
1099 if (!opts->username) {
1100 ERRMEM;
1101 return -1;
1102 }
1103 } else {
1104 opts->username = NULL;
1105 }
1106
1107 return 0;
1108}
1109
1110API int
1111nc_client_ssh_set_username(const char *username)
1112{
1113 return _nc_client_ssh_set_username(username, &ssh_opts);
1114}
1115
1116API int
1117nc_client_ssh_ch_set_username(const char *username)
1118{
1119 return _nc_client_ssh_set_username(username, &ssh_ch_opts);
1120}
1121
Michal Vaskoe22c6732016-01-29 11:03:02 +01001122static const char *
1123_nc_client_ssh_get_username(struct nc_client_ssh_opts *opts)
1124{
1125 return opts->username;
1126}
1127
1128API const char *
1129nc_client_ssh_get_username(void)
1130{
1131 return _nc_client_ssh_get_username(&ssh_opts);
1132}
1133
1134API const char *
1135nc_client_ssh_ch_get_username(void)
1136{
1137 return _nc_client_ssh_get_username(&ssh_ch_opts);
1138}
1139
Michal Vasko3031aae2016-01-27 16:07:18 +01001140API int
1141nc_client_ssh_ch_add_bind_listen(const char *address, uint16_t port)
1142{
1143 return nc_client_ch_add_bind_listen(address, port, NC_TI_LIBSSH);
1144}
1145
1146API int
1147nc_client_ssh_ch_del_bind(const char *address, uint16_t port)
1148{
1149 return nc_client_ch_del_bind(address, port, NC_TI_LIBSSH);
1150}
1151
Michal Vasko8e2f4a62016-02-01 15:59:48 +01001152/* Establish a secure SSH connection and authenticate.
Michal Vasko7b62fed2015-10-26 15:39:46 +01001153 * Host, port, username, and a connected socket is expected to be set.
Radek Krejciae813f42018-07-02 13:38:30 +02001154 *
1155 * return values
1156 * -1 failure
1157 * 0 try again
1158 * 1 success
Michal Vasko7b62fed2015-10-26 15:39:46 +01001159 */
1160static int
Michal Vasko0190bc32016-03-02 15:47:49 +01001161connect_ssh_session(struct nc_session *session, struct nc_client_ssh_opts *opts, int timeout)
Michal Vasko7b62fed2015-10-26 15:39:46 +01001162{
Radek Krejciae813f42018-07-02 13:38:30 +02001163 int j, ret_auth, userauthlist, ret, attempt = 0;
Michal Vasko235efdc2015-12-17 12:05:04 +01001164 NC_SSH_AUTH_TYPE auth;
Michal Vasko0190bc32016-03-02 15:47:49 +01001165 int16_t pref;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001166 const char* prompt;
1167 char *s, *answer, echo;
1168 ssh_key pubkey, privkey;
1169 ssh_session ssh_sess;
Michal Vasko36c7be82017-02-22 13:37:59 +01001170 struct timespec ts_timeout, ts_cur;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001171
1172 ssh_sess = session->ti.libssh.session;
1173
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001174 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001175 nc_addtimespec(&ts_timeout, NC_TRANSPORT_TIMEOUT);
Michal Vasko0190bc32016-03-02 15:47:49 +01001176 while ((ret = ssh_connect(ssh_sess)) == SSH_AGAIN) {
1177 usleep(NC_TIMEOUT_STEP);
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001178 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001179 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001180 break;
1181 }
1182 }
1183 if (ret == SSH_AGAIN) {
1184 ERR("SSH connect timeout.");
1185 return 0;
1186 } else if (ret != SSH_OK) {
1187 ERR("Starting the SSH session failed (%s).", ssh_get_error(ssh_sess));
Michal Vasko7b62fed2015-10-26 15:39:46 +01001188 DBG("Error code %d.", ssh_get_error_code(ssh_sess));
Michal Vaskod083db62016-01-19 10:31:29 +01001189 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001190 }
1191
Radek Krejci90a84a22017-05-25 13:53:00 +02001192 if (opts->auth_hostkey_check(session->host, ssh_sess, opts->auth_hostkey_check_priv)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001193 ERR("Checking the host key failed.");
Michal Vaskod083db62016-01-19 10:31:29 +01001194 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001195 }
1196
Michal Vasko36c7be82017-02-22 13:37:59 +01001197 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001198 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001199 nc_addtimespec(&ts_timeout, timeout);
1200 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001201 while ((ret_auth = ssh_userauth_none(ssh_sess, NULL)) == SSH_AUTH_AGAIN) {
1202 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001203 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001204 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001205 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1206 break;
1207 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001208 }
1209 }
1210 if (ret_auth == SSH_AUTH_AGAIN) {
1211 ERR("Request authentication methods timeout.");
1212 return 0;
1213 } else if (ret_auth == SSH_AUTH_ERROR) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001214 ERR("Authentication failed (%s).", ssh_get_error(ssh_sess));
Michal Vaskod083db62016-01-19 10:31:29 +01001215 return -1;
Radek Krejciae813f42018-07-02 13:38:30 +02001216 } else if (ret_auth == SSH_AUTH_SUCCESS) {
1217 WRN("Server accepts \"none\" authentication method.")
1218 return 1;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001219 }
1220
1221 /* check what authentication methods are available */
1222 userauthlist = ssh_userauth_list(ssh_sess, NULL);
Michal Vasko235efdc2015-12-17 12:05:04 +01001223
1224 /* remove those disabled */
Michal Vasko30e2c872016-02-18 10:03:21 +01001225 if (opts->auth_pref[0].value < 0) {
Michal Vasko235efdc2015-12-17 12:05:04 +01001226 VRB("Interactive SSH authentication method was disabled.");
1227 userauthlist &= ~SSH_AUTH_METHOD_INTERACTIVE;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001228 }
Michal Vasko30e2c872016-02-18 10:03:21 +01001229 if (opts->auth_pref[1].value < 0) {
Michal Vasko235efdc2015-12-17 12:05:04 +01001230 VRB("Password SSH authentication method was disabled.");
1231 userauthlist &= ~SSH_AUTH_METHOD_PASSWORD;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001232 }
Michal Vasko30e2c872016-02-18 10:03:21 +01001233 if (opts->auth_pref[2].value < 0) {
Michal Vasko235efdc2015-12-17 12:05:04 +01001234 VRB("Publickey SSH authentication method was disabled.");
1235 userauthlist &= ~SSH_AUTH_METHOD_PUBLICKEY;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001236 }
1237
Michal Vasko0190bc32016-03-02 15:47:49 +01001238 do {
Michal Vasko235efdc2015-12-17 12:05:04 +01001239 auth = 0;
1240 pref = 0;
1241 if (userauthlist & SSH_AUTH_METHOD_INTERACTIVE) {
1242 auth = NC_SSH_AUTH_INTERACTIVE;
Michal Vasko30e2c872016-02-18 10:03:21 +01001243 pref = opts->auth_pref[0].value;
Michal Vasko235efdc2015-12-17 12:05:04 +01001244 }
Michal Vasko30e2c872016-02-18 10:03:21 +01001245 if ((userauthlist & SSH_AUTH_METHOD_PASSWORD) && (opts->auth_pref[1].value > pref)) {
Michal Vasko235efdc2015-12-17 12:05:04 +01001246 auth = NC_SSH_AUTH_PASSWORD;
Michal Vasko30e2c872016-02-18 10:03:21 +01001247 pref = opts->auth_pref[1].value;
Michal Vasko235efdc2015-12-17 12:05:04 +01001248 }
Michal Vasko30e2c872016-02-18 10:03:21 +01001249 if ((userauthlist & SSH_AUTH_METHOD_PUBLICKEY) && (opts->auth_pref[2].value > pref)) {
Michal Vasko235efdc2015-12-17 12:05:04 +01001250 auth = NC_SSH_AUTH_PUBLICKEY;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001251 }
1252
Michal Vasko235efdc2015-12-17 12:05:04 +01001253 if (!auth) {
Radek Krejciae813f42018-07-02 13:38:30 +02001254 if (!attempt) {
1255 ERR("Unable to authenticate to the remote server (no supported authentication methods detected).");
1256 } else {
1257 ERR("Unable to authenticate to the remote server (all attempts via supported authentication methods failed).");
1258 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001259 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001260 }
1261
1262 /* found common authentication method */
Michal Vasko235efdc2015-12-17 12:05:04 +01001263 switch (auth) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001264 case NC_SSH_AUTH_PASSWORD:
Michal Vasko235efdc2015-12-17 12:05:04 +01001265 userauthlist &= ~SSH_AUTH_METHOD_PASSWORD;
1266
Michal Vaskoef578332016-01-25 13:20:09 +01001267 VRB("Password authentication (host \"%s\", user \"%s\").", session->host, session->username);
Radek Krejci90a84a22017-05-25 13:53:00 +02001268 s = opts->auth_password(session->username, session->host, opts->auth_password_priv);
Michal Vasko88583042018-03-29 09:18:58 +02001269 if (s == NULL) {
1270 ERR("Unable to get the password.");
1271 return -1;
1272 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001273
Michal Vasko36c7be82017-02-22 13:37:59 +01001274 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001275 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001276 nc_addtimespec(&ts_timeout, timeout);
1277 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001278 while ((ret_auth = ssh_userauth_password(ssh_sess, session->username, s)) == SSH_AUTH_AGAIN) {
1279 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001280 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001281 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001282 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1283 break;
1284 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001285 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001286 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001287 memset(s, 0, strlen(s));
Michal Vasko7b62fed2015-10-26 15:39:46 +01001288 free(s);
1289 break;
Michal Vasko0190bc32016-03-02 15:47:49 +01001290
Michal Vasko7b62fed2015-10-26 15:39:46 +01001291 case NC_SSH_AUTH_INTERACTIVE:
Michal Vasko235efdc2015-12-17 12:05:04 +01001292 userauthlist &= ~SSH_AUTH_METHOD_INTERACTIVE;
1293
Michal Vaskod083db62016-01-19 10:31:29 +01001294 VRB("Keyboard-interactive authentication.");
Michal Vasko0190bc32016-03-02 15:47:49 +01001295
Michal Vasko36c7be82017-02-22 13:37:59 +01001296 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001297 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001298 nc_addtimespec(&ts_timeout, timeout);
1299 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001300 while (((ret_auth = ssh_userauth_kbdint(ssh_sess, NULL, NULL)) == SSH_AUTH_INFO)
1301 || (ret_auth == SSH_AUTH_AGAIN)) {
1302 if (ret_auth == SSH_AUTH_AGAIN) {
1303 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001304 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001305 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001306 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1307 break;
1308 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001309 }
1310 continue;
1311 }
1312
Michal Vasko7b62fed2015-10-26 15:39:46 +01001313 for (j = 0; j < ssh_userauth_kbdint_getnprompts(ssh_sess); ++j) {
1314 prompt = ssh_userauth_kbdint_getprompt(ssh_sess, j, &echo);
Michal Vasko0190bc32016-03-02 15:47:49 +01001315 if (!prompt) {
1316 ret_auth = SSH_AUTH_ERROR;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001317 break;
1318 }
Michal Vasko8bf28d12016-02-24 13:29:42 +01001319
1320 /* libssh BUG - echo is always 1 for some reason, assume always 0 */
1321 echo = 0;
1322
Michal Vasko30e2c872016-02-18 10:03:21 +01001323 answer = opts->auth_interactive(ssh_userauth_kbdint_getname(ssh_sess),
1324 ssh_userauth_kbdint_getinstruction(ssh_sess),
Radek Krejci90a84a22017-05-25 13:53:00 +02001325 prompt, echo, opts->auth_interactive_priv);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001326 if (ssh_userauth_kbdint_setanswer(ssh_sess, j, answer) < 0) {
1327 free(answer);
Michal Vasko0190bc32016-03-02 15:47:49 +01001328 ret_auth = SSH_AUTH_ERROR;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001329 break;
1330 }
1331 free(answer);
1332 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001333 if (ret_auth == SSH_AUTH_ERROR) {
1334 break;
1335 }
Michal Vasko36c7be82017-02-22 13:37:59 +01001336 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001337 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001338 nc_addtimespec(&ts_timeout, timeout);
1339 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001340 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001341 break;
Michal Vasko0190bc32016-03-02 15:47:49 +01001342
Michal Vasko206d3b12015-12-04 11:08:42 +01001343 case NC_SSH_AUTH_PUBLICKEY:
Michal Vasko235efdc2015-12-17 12:05:04 +01001344 userauthlist &= ~SSH_AUTH_METHOD_PUBLICKEY;
1345
Michal Vaskod083db62016-01-19 10:31:29 +01001346 VRB("Publickey athentication.");
Michal Vasko7b62fed2015-10-26 15:39:46 +01001347
1348 /* if publickeys path not provided, we cannot continue */
Michal Vasko30e2c872016-02-18 10:03:21 +01001349 if (!opts->key_count) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001350 VRB("No key pair specified.");
1351 break;
1352 }
1353
Michal Vasko30e2c872016-02-18 10:03:21 +01001354 for (j = 0; j < opts->key_count; j++) {
Michal Vaskoef578332016-01-25 13:20:09 +01001355 VRB("Trying to authenticate using %spair \"%s\" \"%s\".",
Michal Vasko30e2c872016-02-18 10:03:21 +01001356 opts->keys[j].privkey_crypt ? "password-protected " : "", opts->keys[j].privkey_path,
1357 opts->keys[j].pubkey_path);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001358
Michal Vasko7abcdeb2016-05-30 15:27:00 +02001359 ret = ssh_pki_import_pubkey_file(opts->keys[j].pubkey_path, &pubkey);
1360 if (ret == SSH_EOF) {
1361 WRN("Failed to import the key \"%s\" (File access problem).", opts->keys[j].pubkey_path);
1362 continue;
1363 } else if (ret == SSH_ERROR) {
1364 WRN("Failed to import the key \"%s\" (SSH error).", opts->keys[j].pubkey_path);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001365 continue;
1366 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001367
Michal Vasko36c7be82017-02-22 13:37:59 +01001368 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001369 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001370 nc_addtimespec(&ts_timeout, timeout);
1371 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001372 while ((ret_auth = ssh_userauth_try_publickey(ssh_sess, NULL, pubkey)) == SSH_AUTH_AGAIN) {
1373 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001374 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001375 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001376 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1377 break;
1378 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001379 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001380 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001381 ssh_key_free(pubkey);
1382
1383 if (ret_auth == SSH_AUTH_DENIED) {
1384 continue;
1385 } else if (ret_auth != SSH_AUTH_SUCCESS) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001386 break;
1387 }
1388
Michal Vasko30e2c872016-02-18 10:03:21 +01001389 if (opts->keys[j].privkey_crypt) {
Radek Krejci90a84a22017-05-25 13:53:00 +02001390 s = opts->auth_privkey_passphrase(opts->keys[j].privkey_path, opts->auth_privkey_passphrase_priv);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001391 } else {
1392 s = NULL;
1393 }
1394
Michal Vasko0190bc32016-03-02 15:47:49 +01001395 ret = ssh_pki_import_privkey_file(opts->keys[j].privkey_path, s, NULL, NULL, &privkey);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001396 if (s) {
1397 memset(s, 0, strlen(s));
1398 free(s);
1399 }
Michal Vasko7abcdeb2016-05-30 15:27:00 +02001400 if (ret == SSH_EOF) {
1401 WRN("Failed to import the key \"%s\" (File access problem).", opts->keys[j].privkey_path);
1402 continue;
1403 } else if (ret == SSH_ERROR) {
1404 WRN("Failed to import the key \"%s\" (SSH error).", opts->keys[j].privkey_path);
Michal Vasko0190bc32016-03-02 15:47:49 +01001405 continue;
1406 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001407
Michal Vasko36c7be82017-02-22 13:37:59 +01001408 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001409 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001410 nc_addtimespec(&ts_timeout, timeout);
1411 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001412 while ((ret_auth = ssh_userauth_publickey(ssh_sess, NULL, privkey)) == SSH_AUTH_AGAIN) {
1413 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001414 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001415 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001416 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1417 break;
1418 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001419 }
1420 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001421 ssh_key_free(privkey);
1422
Michal Vasko0190bc32016-03-02 15:47:49 +01001423 if (ret_auth != SSH_AUTH_DENIED) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001424 break;
1425 }
1426 }
1427 break;
1428 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001429
Michal Vasko0190bc32016-03-02 15:47:49 +01001430 switch (ret_auth) {
1431 case SSH_AUTH_AGAIN:
1432 ERR("Authentication response timeout.");
1433 return 0;
1434 case SSH_AUTH_ERROR:
1435 ERR("Authentication failed (%s).", ssh_get_error(ssh_sess));
1436 return -1;
1437 case SSH_AUTH_DENIED:
1438 WRN("Authentication denied.");
1439 break;
1440 case SSH_AUTH_PARTIAL:
1441 VRB("Partial authentication success.");
1442 break;
1443 case SSH_AUTH_SUCCESS:
1444 VRB("Authentication successful.");
1445 break;
1446 case SSH_AUTH_INFO:
1447 ERRINT;
1448 return -1;
1449 }
Radek Krejciae813f42018-07-02 13:38:30 +02001450
1451 attempt++;
Michal Vasko0190bc32016-03-02 15:47:49 +01001452 } while (ret_auth != SSH_AUTH_SUCCESS);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001453
Michal Vasko0190bc32016-03-02 15:47:49 +01001454 return 1;
Michal Vasko8e2f4a62016-02-01 15:59:48 +01001455}
1456
1457/* Open new SSH channel and request the 'netconf' subsystem.
1458 * SSH connection is expected to be established.
1459 */
1460static int
Michal Vasko0190bc32016-03-02 15:47:49 +01001461open_netconf_channel(struct nc_session *session, int timeout)
Michal Vasko8e2f4a62016-02-01 15:59:48 +01001462{
1463 ssh_session ssh_sess;
Michal Vasko36c7be82017-02-22 13:37:59 +01001464 int ret;
1465 struct timespec ts_timeout, ts_cur;
Michal Vasko8e2f4a62016-02-01 15:59:48 +01001466
1467 ssh_sess = session->ti.libssh.session;
1468
1469 if (!ssh_is_connected(ssh_sess)) {
1470 ERR("SSH session not connected.");
1471 return -1;
1472 }
1473
1474 if (session->ti.libssh.channel) {
1475 ERR("SSH channel already created.");
1476 return -1;
1477 }
1478
Michal Vasko7b62fed2015-10-26 15:39:46 +01001479 /* open a channel */
Michal Vasko36c7be82017-02-22 13:37:59 +01001480 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001481 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001482 nc_addtimespec(&ts_timeout, timeout);
1483 }
Michal Vasko7b62fed2015-10-26 15:39:46 +01001484 session->ti.libssh.channel = ssh_channel_new(ssh_sess);
Michal Vasko0190bc32016-03-02 15:47:49 +01001485 while ((ret = ssh_channel_open_session(session->ti.libssh.channel)) == SSH_AGAIN) {
1486 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001487 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001488 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001489 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1490 break;
1491 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001492 }
1493 }
1494 if (ret == SSH_AGAIN) {
1495 ERR("Opening an SSH channel timeout elapsed.");
Michal Vasko7b62fed2015-10-26 15:39:46 +01001496 ssh_channel_free(session->ti.libssh.channel);
1497 session->ti.libssh.channel = NULL;
Michal Vasko0190bc32016-03-02 15:47:49 +01001498 return 0;
1499 } else if (ret == SSH_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +01001500 ERR("Opening an SSH channel failed (%s).", ssh_get_error(ssh_sess));
Michal Vasko0190bc32016-03-02 15:47:49 +01001501 ssh_channel_free(session->ti.libssh.channel);
1502 session->ti.libssh.channel = NULL;
Michal Vaskod083db62016-01-19 10:31:29 +01001503 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001504 }
1505
1506 /* execute the NETCONF subsystem on the channel */
Michal Vasko36c7be82017-02-22 13:37:59 +01001507 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001508 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001509 nc_addtimespec(&ts_timeout, timeout);
1510 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001511 while ((ret = ssh_channel_request_subsystem(session->ti.libssh.channel, "netconf")) == SSH_AGAIN) {
1512 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001513 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001514 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001515 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1516 break;
1517 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001518 }
1519 }
1520 if (ret == SSH_AGAIN) {
1521 ERR("Starting the \"netconf\" SSH subsystem timeout elapsed.");
Michal Vasko7b62fed2015-10-26 15:39:46 +01001522 ssh_channel_free(session->ti.libssh.channel);
1523 session->ti.libssh.channel = NULL;
Michal Vasko0190bc32016-03-02 15:47:49 +01001524 return 0;
1525 } else if (ret == SSH_ERROR) {
Michal Vaskod083db62016-01-19 10:31:29 +01001526 ERR("Starting the \"netconf\" SSH subsystem failed (%s).", ssh_get_error(ssh_sess));
Michal Vasko0190bc32016-03-02 15:47:49 +01001527 ssh_channel_free(session->ti.libssh.channel);
1528 session->ti.libssh.channel = NULL;
Michal Vaskod083db62016-01-19 10:31:29 +01001529 return -1;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001530 }
1531
Michal Vasko0190bc32016-03-02 15:47:49 +01001532 return 1;
Radek Krejciac6d3472015-10-22 15:47:18 +02001533}
1534
Michal Vasko30e2c872016-02-18 10:03:21 +01001535static struct nc_session *
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001536_nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx, struct nc_keepalives *ka,
1537 struct nc_client_ssh_opts *opts, int timeout)
Michal Vasko30e2c872016-02-18 10:03:21 +01001538{
Michal Vasko66032bc2019-01-22 15:03:12 +01001539 char *host = NULL, *username = NULL, *ip_host;
Dragos Dan8ce3b7c2021-03-09 09:17:22 +02001540 unsigned int port = 0;
Michal Vasko30e2c872016-02-18 10:03:21 +01001541 int sock;
1542 struct passwd *pw;
1543 struct nc_session *session = NULL;
1544
1545 if (!ssh_session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001546 ERRARG("ssh_session");
Michal Vasko30e2c872016-02-18 10:03:21 +01001547 return NULL;
1548 }
1549
1550 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001551 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko30e2c872016-02-18 10:03:21 +01001552 if (!session) {
1553 ERRMEM;
1554 return NULL;
1555 }
1556 session->status = NC_STATUS_STARTING;
Michal Vasko30e2c872016-02-18 10:03:21 +01001557 session->ti_type = NC_TI_LIBSSH;
1558 session->ti.libssh.session = ssh_session;
1559
1560 /* was port set? */
1561 ssh_options_get_port(ssh_session, (unsigned int *)&port);
1562
1563 if (ssh_options_get(ssh_session, SSH_OPTIONS_HOST, &host) != SSH_OK) {
1564 /*
1565 * There is no file descriptor (detected based on the host, there is no way to check
1566 * the SSH_OPTIONS_FD directly :/), we need to create it. (TCP/IP layer)
1567 */
1568
1569 /* remember host */
1570 host = strdup("localhost");
Michal Vasko4eb3c312016-03-01 14:09:37 +01001571 if (!host) {
1572 ERRMEM;
1573 goto fail;
1574 }
Michal Vasko30e2c872016-02-18 10:03:21 +01001575 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOST, host);
1576
1577 /* create and connect socket */
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001578 sock = nc_sock_connect(host, port, -1, ka, NULL, &ip_host);
Michal Vasko30e2c872016-02-18 10:03:21 +01001579 if (sock == -1) {
Michal Vasko29af44b2016-10-13 10:59:55 +02001580 ERR("Unable to connect to %s:%u (%s).", host, port, strerror(errno));
Michal Vasko30e2c872016-02-18 10:03:21 +01001581 goto fail;
1582 }
1583 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_FD, &sock);
Michal Vasko0190bc32016-03-02 15:47:49 +01001584 ssh_set_blocking(session->ti.libssh.session, 0);
Michal Vasko66032bc2019-01-22 15:03:12 +01001585
1586 free(host);
1587 host = ip_host;
Michal Vasko30e2c872016-02-18 10:03:21 +01001588 }
1589
1590 /* was username set? */
1591 ssh_options_get(ssh_session, SSH_OPTIONS_USER, &username);
1592
1593 if (!ssh_is_connected(ssh_session)) {
1594 /*
1595 * We are connected, but not SSH authenticated. (Transport layer)
1596 */
1597
1598 /* remember username */
1599 if (!username) {
1600 if (!opts->username) {
1601 pw = getpwuid(getuid());
1602 if (!pw) {
1603 ERR("Unknown username for the SSH connection (%s).", strerror(errno));
1604 goto fail;
1605 }
1606 username = strdup(pw->pw_name);
1607 } else {
1608 username = strdup(opts->username);
1609 }
Michal Vasko4eb3c312016-03-01 14:09:37 +01001610 if (!username) {
1611 ERRMEM;
1612 goto fail;
1613 }
Michal Vasko30e2c872016-02-18 10:03:21 +01001614 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_USER, username);
1615 }
1616
1617 /* connect and authenticate SSH session */
1618 session->host = host;
1619 session->username = username;
Michal Vasko0190bc32016-03-02 15:47:49 +01001620 if (connect_ssh_session(session, opts, timeout) != 1) {
Michal Vasko30e2c872016-02-18 10:03:21 +01001621 goto fail;
1622 }
1623 }
1624
1625 /*
1626 * Almost done, open a netconf channel. (Transport layer / application layer)
1627 */
Michal Vasko0190bc32016-03-02 15:47:49 +01001628 if (open_netconf_channel(session, timeout) != 1) {
Michal Vasko30e2c872016-02-18 10:03:21 +01001629 goto fail;
1630 }
1631
1632 /*
1633 * SSH session is established and netconf channel opened, create a NETCONF session. (Application layer)
1634 */
1635
Radek Krejcifd5b6682017-06-13 15:52:53 +02001636 if (nc_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
1637 goto fail;
Michal Vasko30e2c872016-02-18 10:03:21 +01001638 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001639 ctx = session->ctx;
Michal Vasko30e2c872016-02-18 10:03:21 +01001640
1641 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001642 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko30e2c872016-02-18 10:03:21 +01001643 goto fail;
1644 }
1645 session->status = NC_STATUS_RUNNING;
1646
1647 if (nc_ctx_check_and_fill(session) == -1) {
1648 goto fail;
1649 }
1650
1651 /* store information into the dictionary */
1652 if (host) {
1653 session->host = lydict_insert_zc(ctx, host);
1654 }
1655 if (port) {
1656 session->port = port;
1657 }
1658 if (username) {
1659 session->username = lydict_insert_zc(ctx, username);
1660 }
1661
1662 return session;
1663
1664fail:
Michal Vasko3df5b252019-04-23 08:48:17 +02001665 free(host);
1666 session->host = NULL;
1667 free(username);
1668 session->username = NULL;
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001669 nc_session_free(session, NULL);
Michal Vasko30e2c872016-02-18 10:03:21 +01001670 return NULL;
1671}
1672
Radek Krejciac6d3472015-10-22 15:47:18 +02001673API struct nc_session *
Michal Vasko3031aae2016-01-27 16:07:18 +01001674nc_connect_ssh(const char *host, uint16_t port, struct ly_ctx *ctx)
Radek Krejciac6d3472015-10-22 15:47:18 +02001675{
Michal Vasko1f0563a2016-03-31 08:38:44 +02001676 const long timeout = NC_SSH_TIMEOUT;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001677 int sock;
Michal Vasko55fded62016-02-02 12:19:34 +01001678 uint32_t port_uint;
Michal Vasko66032bc2019-01-22 15:03:12 +01001679 char *username, *ip_host = NULL;
Radek Krejciac6d3472015-10-22 15:47:18 +02001680 struct passwd *pw;
1681 struct nc_session *session = NULL;
1682
1683 /* process parameters */
1684 if (!host || strisempty(host)) {
1685 host = "localhost";
1686 }
1687
1688 if (!port) {
1689 port = NC_PORT_SSH;
1690 }
Michal Vasko55fded62016-02-02 12:19:34 +01001691 port_uint = port;
Radek Krejciac6d3472015-10-22 15:47:18 +02001692
Michal Vasko3031aae2016-01-27 16:07:18 +01001693 if (!ssh_opts.username) {
Radek Krejciac6d3472015-10-22 15:47:18 +02001694 pw = getpwuid(getuid());
1695 if (!pw) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001696 ERR("Unknown username for the SSH connection (%s).", strerror(errno));
1697 return NULL;
Radek Krejciac6d3472015-10-22 15:47:18 +02001698 } else {
1699 username = pw->pw_name;
1700 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001701 } else {
1702 username = ssh_opts.username;
Radek Krejciac6d3472015-10-22 15:47:18 +02001703 }
1704
1705 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001706 session = nc_new_session(NC_CLIENT, 0);
Radek Krejciac6d3472015-10-22 15:47:18 +02001707 if (!session) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001708 ERRMEM;
Radek Krejciac6d3472015-10-22 15:47:18 +02001709 return NULL;
1710 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001711 session->status = NC_STATUS_STARTING;
Radek Krejciac6d3472015-10-22 15:47:18 +02001712
Michal Vasko131120a2018-05-29 15:44:02 +02001713 /* transport-specific data */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001714 session->ti_type = NC_TI_LIBSSH;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001715 session->ti.libssh.session = ssh_new();
1716 if (!session->ti.libssh.session) {
1717 ERR("Unable to initialize SSH session.");
1718 goto fail;
1719 }
Radek Krejciac6d3472015-10-22 15:47:18 +02001720
Michal Vasko7b62fed2015-10-26 15:39:46 +01001721 /* set some basic SSH session options */
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001722 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOST, host);
Michal Vasko55fded62016-02-02 12:19:34 +01001723 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_PORT, &port_uint);
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001724 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_USER, username);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001725 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_TIMEOUT, &timeout);
1726
1727 /* create and assign communication socket */
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001728 sock = nc_sock_connect(host, port, -1, &client_opts.ka, NULL, &ip_host);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001729 if (sock == -1) {
Michal Vasko29af44b2016-10-13 10:59:55 +02001730 ERR("Unable to connect to %s:%u (%s).", host, port, strerror(errno));
Michal Vasko7b62fed2015-10-26 15:39:46 +01001731 goto fail;
1732 }
1733 ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_FD, &sock);
Michal Vasko0190bc32016-03-02 15:47:49 +01001734 ssh_set_blocking(session->ti.libssh.session, 0);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001735
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001736 /* temporarily, for session connection */
1737 session->host = host;
1738 session->username = username;
Michal Vasko0190bc32016-03-02 15:47:49 +01001739 if ((connect_ssh_session(session, &ssh_opts, NC_TRANSPORT_TIMEOUT) != 1)
1740 || (open_netconf_channel(session, NC_TRANSPORT_TIMEOUT) != 1)) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001741 goto fail;
Radek Krejciac6d3472015-10-22 15:47:18 +02001742 }
1743
Radek Krejcifd5b6682017-06-13 15:52:53 +02001744 if (nc_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
1745 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001746 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001747 ctx = session->ctx;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001748
Radek Krejciac6d3472015-10-22 15:47:18 +02001749 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001750 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko7b62fed2015-10-26 15:39:46 +01001751 goto fail;
Radek Krejciac6d3472015-10-22 15:47:18 +02001752 }
Michal Vaskoad611702015-12-03 13:41:51 +01001753 session->status = NC_STATUS_RUNNING;
Radek Krejciac6d3472015-10-22 15:47:18 +02001754
Michal Vaskoef578332016-01-25 13:20:09 +01001755 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko57eb9402015-12-08 14:38:12 +01001756 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001757 }
1758
1759 /* store information into the dictionary */
Michal Vasko66032bc2019-01-22 15:03:12 +01001760 session->host = lydict_insert_zc(ctx, ip_host);
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001761 session->port = port;
1762 session->username = lydict_insert(ctx, username, 0);
1763
Radek Krejciac6d3472015-10-22 15:47:18 +02001764 return session;
1765
Michal Vasko7b62fed2015-10-26 15:39:46 +01001766fail:
Michal Vasko66032bc2019-01-22 15:03:12 +01001767 free(ip_host);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001768 nc_session_free(session, NULL);
Radek Krejciac6d3472015-10-22 15:47:18 +02001769 return NULL;
1770}
1771
1772API struct nc_session *
1773nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx)
1774{
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001775 return _nc_connect_libssh(ssh_session, ctx, &client_opts.ka, &ssh_opts, NC_TRANSPORT_TIMEOUT);
Radek Krejciac6d3472015-10-22 15:47:18 +02001776}
1777
1778API struct nc_session *
Michal Vasko7b62fed2015-10-26 15:39:46 +01001779nc_connect_ssh_channel(struct nc_session *session, struct ly_ctx *ctx)
Radek Krejciac6d3472015-10-22 15:47:18 +02001780{
Michal Vasko7b62fed2015-10-26 15:39:46 +01001781 struct nc_session *new_session, *ptr;
Radek Krejciac6d3472015-10-22 15:47:18 +02001782
Michal Vaskob7c4ff32016-01-21 15:35:54 +01001783 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001784 ERRARG("session");
Michal Vaskob7c4ff32016-01-21 15:35:54 +01001785 return NULL;
1786 }
1787
Michal Vasko7b62fed2015-10-26 15:39:46 +01001788 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +02001789 new_session = nc_new_session(NC_CLIENT, 1);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001790 if (!new_session) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001791 ERRMEM;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001792 return NULL;
1793 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001794 new_session->status = NC_STATUS_STARTING;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001795
Michal Vasko131120a2018-05-29 15:44:02 +02001796 /* share some parameters including the IO lock (we are using one socket for both sessions) */
Michal Vasko7b62fed2015-10-26 15:39:46 +01001797 new_session->ti_type = NC_TI_LIBSSH;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001798 new_session->ti.libssh.session = session->ti.libssh.session;
Michal Vasko131120a2018-05-29 15:44:02 +02001799 new_session->io_lock = session->io_lock;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001800
Michal Vasko1e7f9e72019-01-28 08:55:47 +01001801 /* append to the session ring list */
1802 if (!session->ti.libssh.next) {
1803 session->ti.libssh.next = new_session;
1804 new_session->ti.libssh.next = session;
1805 } else {
1806 ptr = session->ti.libssh.next;
1807 session->ti.libssh.next = new_session;
1808 new_session->ti.libssh.next = ptr;
1809 }
1810
Michal Vasko7b62fed2015-10-26 15:39:46 +01001811 /* create the channel safely */
Michal Vasko131120a2018-05-29 15:44:02 +02001812 if (nc_session_io_lock(new_session, -1, __func__) != 1) {
Michal Vaskoade892d2017-02-22 13:40:35 +01001813 goto fail;
1814 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001815 if (open_netconf_channel(new_session, NC_TRANSPORT_TIMEOUT) != 1) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001816 goto fail;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001817 }
Michal Vasko131120a2018-05-29 15:44:02 +02001818 nc_session_io_unlock(new_session, __func__);
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001819
Michal Vaskoedcf1f72017-10-19 11:30:46 +02001820 if (nc_session_new_ctx(new_session, ctx) != EXIT_SUCCESS) {
Radek Krejcifd5b6682017-06-13 15:52:53 +02001821 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001822 }
Radek Krejcifd5b6682017-06-13 15:52:53 +02001823 ctx = session->ctx;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001824
Michal Vasko7b62fed2015-10-26 15:39:46 +01001825 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001826 if (nc_handshake_io(new_session) != NC_MSG_HELLO) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001827 goto fail;
Michal Vasko7b62fed2015-10-26 15:39:46 +01001828 }
Michal Vaskoad611702015-12-03 13:41:51 +01001829 new_session->status = NC_STATUS_RUNNING;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001830
Michal Vaskoef578332016-01-25 13:20:09 +01001831 if (nc_ctx_check_and_fill(new_session) == -1) {
Michal Vasko57eb9402015-12-08 14:38:12 +01001832 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001833 }
1834
1835 /* store information into session and the dictionary */
Michal Vasko56b5bf72016-01-19 11:20:35 +01001836 new_session->host = lydict_insert(ctx, session->host, 0);
1837 new_session->port = session->port;
1838 new_session->username = lydict_insert(ctx, session->username, 0);
Michal Vasko7b62fed2015-10-26 15:39:46 +01001839
Michal Vasko7b62fed2015-10-26 15:39:46 +01001840 return new_session;
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001841
1842fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001843 nc_session_free(new_session, NULL);
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001844 return NULL;
Radek Krejciac6d3472015-10-22 15:47:18 +02001845}
Michal Vasko80cad7f2015-12-08 14:42:27 +01001846
Michal Vasko3031aae2016-01-27 16:07:18 +01001847struct nc_session *
Michal Vasko0190bc32016-03-02 15:47:49 +01001848nc_accept_callhome_ssh_sock(int sock, const char *host, uint16_t port, struct ly_ctx *ctx, int timeout)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001849{
Michal Vasko1f0563a2016-03-31 08:38:44 +02001850 const long ssh_timeout = NC_SSH_TIMEOUT;
Michal Vasko0cfa90c2016-10-13 10:34:46 +02001851 unsigned int uint_port;
Michal Vasko3031aae2016-01-27 16:07:18 +01001852 struct passwd *pw;
Michal Vasko30e2c872016-02-18 10:03:21 +01001853 struct nc_session *session;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001854 ssh_session sess;
1855
Michal Vasko80cad7f2015-12-08 14:42:27 +01001856 sess = ssh_new();
1857 if (!sess) {
1858 ERR("Unable to initialize an SSH session.");
1859 close(sock);
1860 return NULL;
1861 }
1862
1863 ssh_options_set(sess, SSH_OPTIONS_FD, &sock);
Michal Vasko0190bc32016-03-02 15:47:49 +01001864 ssh_set_blocking(sess, 0);
Michal Vasko3031aae2016-01-27 16:07:18 +01001865 ssh_options_set(sess, SSH_OPTIONS_HOST, host);
Michal Vasko0cfa90c2016-10-13 10:34:46 +02001866 uint_port = port;
1867 ssh_options_set(sess, SSH_OPTIONS_PORT, &uint_port);
Michal Vasko80cad7f2015-12-08 14:42:27 +01001868 ssh_options_set(sess, SSH_OPTIONS_TIMEOUT, &ssh_timeout);
Michal Vasko3031aae2016-01-27 16:07:18 +01001869 if (!ssh_ch_opts.username) {
1870 pw = getpwuid(getuid());
1871 if (!pw) {
1872 ERR("Unknown username for the SSH connection (%s).", strerror(errno));
Michal Vasko435e5cf2019-04-23 08:48:44 +02001873 ssh_free(sess);
Michal Vasko3031aae2016-01-27 16:07:18 +01001874 return NULL;
1875 }
1876 ssh_options_set(sess, SSH_OPTIONS_USER, pw->pw_name);
1877 } else {
1878 ssh_options_set(sess, SSH_OPTIONS_USER, ssh_ch_opts.username);
Michal Vasko80cad7f2015-12-08 14:42:27 +01001879 }
Michal Vasko8fd6fca2019-02-04 10:59:49 +01001880 ssh_options_set(sess, SSH_OPTIONS_HOSTKEYS, "ssh-ed25519,ecdsa-sha2-nistp256,"
1881 "ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-rsa,rsa-sha2-512,rsa-sha2-256,ssh-dss");
1882#ifdef HAVE_LIBSSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES
1883 ssh_options_set(sess, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES, "ssh-ed25519,ecdsa-sha2-nistp256,"
1884 "ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-rsa,rsa-sha2-512,rsa-sha2-256,ssh-dss");
1885#endif
Michal Vasko80cad7f2015-12-08 14:42:27 +01001886
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001887 session = _nc_connect_libssh(sess, ctx, &client_opts.ka, &ssh_ch_opts, timeout);
Michal Vasko435e5cf2019-04-23 08:48:44 +02001888 if (!session) {
Michal Vasko457f0532019-08-15 13:59:49 +02001889 /* sess is freed */
Michal Vasko435e5cf2019-04-23 08:48:44 +02001890 return NULL;
Michal Vasko4282fae2016-02-18 10:03:42 +01001891 }
1892
Michal Vasko435e5cf2019-04-23 08:48:44 +02001893 session->flags |= NC_SESSION_CALLHOME;
Michal Vasko30e2c872016-02-18 10:03:21 +01001894 return session;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001895}