Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @file session_client_ssh.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 5 | * @brief libnetconf2 - SSH specific client session transport functions |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 6 | * |
| 7 | * This source is compiled only with libssh. |
| 8 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 9 | * @copyright |
Michal Vasko | 40899d8 | 2021-05-26 12:04:49 +0200 | [diff] [blame] | 10 | * Copyright (c) 2015 - 2021 CESNET, z.s.p.o. |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 13 | * You may not use this file except in compliance with the License. |
| 14 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 15 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 16 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 17 | */ |
| 18 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 19 | #define _GNU_SOURCE |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 20 | |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 21 | #include <assert.h> |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 24 | #include <pthread.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 25 | #include <pwd.h> |
| 26 | #include <stddef.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <termios.h> |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 33 | #include <time.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 34 | #include <unistd.h> |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 35 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 36 | #ifdef ENABLE_DNSSEC |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 37 | # include <validator/resolver.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 38 | # include <validator/validator.h> |
Michal Vasko | 40899d8 | 2021-05-26 12:04:49 +0200 | [diff] [blame] | 39 | # include <validator/validator-config.h> |
| 40 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 41 | # include <validator/validator-compat.h> |
| 42 | #endif |
| 43 | |
Michal Vasko | 745ff83 | 2015-12-08 14:40:29 +0100 | [diff] [blame] | 44 | #include <libssh/libssh.h> |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 45 | #include <libyang/libyang.h> |
| 46 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 47 | #include "compat.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame^] | 48 | #include "config.h" |
| 49 | #include "log_p.h" |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 50 | #include "session_client.h" |
| 51 | #include "session_client_ch.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame^] | 52 | #include "session_p.h" |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 53 | |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 54 | struct nc_client_context *nc_client_context_location(void); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 55 | |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 56 | #define client_opts nc_client_context_location()->opts |
| 57 | #define ssh_opts nc_client_context_location()->ssh_opts |
| 58 | #define ssh_ch_opts nc_client_context_location()->ssh_ch_opts |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 59 | |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 60 | static FILE * |
| 61 | open_tty_noecho(const char *path, struct termios *oldterm) |
| 62 | { |
| 63 | struct termios newterm; |
| 64 | FILE *ret; |
| 65 | |
| 66 | if (!(ret = fopen(path, "r"))) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 67 | ERR(NULL, "Unable to open terminal \"%s\" for reading (%s).", path, strerror(errno)); |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | if (tcgetattr(fileno(ret), oldterm)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 72 | ERR(NULL, "Unable to get terminal \"%s\" settings (%s).", path, strerror(errno)); |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 73 | fclose(ret); |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | newterm = *oldterm; |
| 78 | newterm.c_lflag &= ~ECHO; |
| 79 | newterm.c_lflag &= ~ICANON; |
| 80 | tcflush(fileno(ret), TCIFLUSH); |
| 81 | if (tcsetattr(fileno(ret), TCSANOW, &newterm)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 82 | ERR(NULL, "Unable to change terminal \"%s\" settings for hiding password (%s).", path, strerror(errno)); |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 83 | fclose(ret); |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 90 | static FILE * |
| 91 | nc_open_in(int echo, struct termios *oldterm) |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 92 | { |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 93 | char buf[512]; |
| 94 | int buflen = 512, ret; |
| 95 | FILE *in; |
| 96 | |
| 97 | if (!echo) { |
| 98 | in = open_tty_noecho("/dev/tty", oldterm); |
| 99 | } else { |
| 100 | in = fopen("/dev/tty", "r"); |
| 101 | if (!in) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 102 | ERR(NULL, "Unable to open terminal \"/dev/tty\" for reading (%s).", strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 103 | } |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 104 | } |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 105 | |
| 106 | if (!in) { |
| 107 | if ((ret = ttyname_r(STDIN_FILENO, buf, buflen))) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 108 | ERR(NULL, "ttyname_r failed (%s).", strerror(ret)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | if (!echo) { |
| 113 | in = open_tty_noecho(buf, oldterm); |
| 114 | } else { |
| 115 | in = fopen(buf, "r"); |
| 116 | if (!in) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 117 | ERR(NULL, "Unable to open terminal \"%s\" for reading (%s).", buf, strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return in; |
| 123 | } |
| 124 | |
| 125 | static FILE * |
| 126 | nc_open_out(void) |
| 127 | { |
| 128 | char buf[512]; |
| 129 | int buflen = 512, ret; |
| 130 | FILE *out; |
| 131 | |
| 132 | out = fopen("/dev/tty", "w"); |
| 133 | if (!out) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 134 | ERR(NULL, "Unable to open terminal \"/dev/tty\" for writing (%s).", strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 135 | |
| 136 | if ((ret = ttyname_r(STDOUT_FILENO, buf, buflen))) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 137 | ERR(NULL, "ttyname_r failed (%s).", strerror(ret)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | out = fopen(buf, "w"); |
| 142 | if (!out) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 143 | ERR(NULL, "Unable to open terminal \"%s\" for writing (%s).", buf, strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | return out; |
| 148 | } |
| 149 | |
| 150 | static void |
| 151 | nc_close_inout(FILE *inout, int echo, struct termios *oldterm) |
| 152 | { |
| 153 | if (inout) { |
| 154 | if (!echo && (tcsetattr(fileno(inout), TCSANOW, oldterm) != 0)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 155 | ERR(NULL, "Unable to restore terminal settings (%s).", strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 156 | } |
| 157 | fclose(inout); |
| 158 | } |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Kevin Barrett | d37d2fb | 2019-08-28 14:25:34 -0400 | [diff] [blame] | 161 | void |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 162 | _nc_client_ssh_destroy_opts(struct nc_client_ssh_opts *opts) |
Michal Vasko | 990089e | 2015-10-27 15:05:25 +0100 | [diff] [blame] | 163 | { |
| 164 | int i; |
| 165 | |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 166 | for (i = 0; i < opts->key_count; ++i) { |
| 167 | free(opts->keys[i].pubkey_path); |
| 168 | free(opts->keys[i].privkey_path); |
Michal Vasko | 990089e | 2015-10-27 15:05:25 +0100 | [diff] [blame] | 169 | } |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 170 | free(opts->keys); |
| 171 | free(opts->username); |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 172 | free(opts->knownhosts_path); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 173 | opts->key_count = 0; |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 174 | opts->keys = NULL; |
| 175 | opts->username = NULL; |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 176 | opts->knownhosts_path = NULL; |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 177 | } |
Michal Vasko | 990089e | 2015-10-27 15:05:25 +0100 | [diff] [blame] | 178 | |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 179 | void |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 180 | nc_client_ssh_destroy_opts(void) |
| 181 | { |
| 182 | _nc_client_ssh_destroy_opts(&ssh_opts); |
| 183 | _nc_client_ssh_destroy_opts(&ssh_ch_opts); |
Michal Vasko | 990089e | 2015-10-27 15:05:25 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 186 | #ifdef ENABLE_DNSSEC |
| 187 | |
| 188 | /* return 0 (DNSSEC + key valid), 1 (unsecure DNS + key valid), 2 (key not found or an error) */ |
| 189 | /* type - 1 (RSA), 2 (DSA), 3 (ECDSA); alg - 1 (SHA1), 2 (SHA-256) */ |
| 190 | static int |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 191 | sshauth_hostkey_hash_dnssec_check(const char *hostname, const unsigned char *sha1hash, int type, int alg) |
| 192 | { |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 193 | ns_msg handle; |
| 194 | ns_rr rr; |
| 195 | val_status_t val_status; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 196 | const unsigned char *rdata; |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 197 | unsigned char buf[4096]; |
| 198 | int buf_len = 4096; |
| 199 | int ret = 0, i, j, len; |
| 200 | |
| 201 | /* class 1 - internet, type 44 - SSHFP */ |
| 202 | len = val_res_query(NULL, hostname, 1, 44, buf, buf_len, &val_status); |
| 203 | |
| 204 | if ((len < 0) || !val_istrusted(val_status)) { |
| 205 | ret = 2; |
| 206 | goto finish; |
| 207 | } |
| 208 | |
| 209 | if (ns_initparse(buf, len, &handle) < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 210 | ERR(NULL, "Failed to initialize DNSSEC response parser."); |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 211 | ret = 2; |
| 212 | goto finish; |
| 213 | } |
| 214 | |
| 215 | if ((i = libsres_msg_getflag(handle, ns_f_rcode))) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 216 | ERR(NULL, "DNSSEC query returned %d.", i); |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 217 | ret = 2; |
| 218 | goto finish; |
| 219 | } |
| 220 | |
| 221 | if (!libsres_msg_getflag(handle, ns_f_ad)) { |
| 222 | /* response not secured by DNSSEC */ |
| 223 | ret = 1; |
| 224 | } |
| 225 | |
| 226 | /* query section */ |
| 227 | if (ns_parserr(&handle, ns_s_qd, 0, &rr)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 228 | ERR(NULL, "DNSSEC query section parser fail."); |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 229 | ret = 2; |
| 230 | goto finish; |
| 231 | } |
| 232 | |
| 233 | if (strcmp(hostname, ns_rr_name(rr)) || (ns_rr_type(rr) != 44) || (ns_rr_class(rr) != 1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 234 | ERR(NULL, "DNSSEC query in the answer does not match the original query."); |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 235 | ret = 2; |
| 236 | goto finish; |
| 237 | } |
| 238 | |
| 239 | /* answer section */ |
| 240 | i = 0; |
| 241 | while (!ns_parserr(&handle, ns_s_an, i, &rr)) { |
| 242 | if (ns_rr_type(rr) != 44) { |
| 243 | ++i; |
| 244 | continue; |
| 245 | } |
| 246 | |
| 247 | rdata = ns_rr_rdata(rr); |
| 248 | if (rdata[0] != type) { |
| 249 | ++i; |
| 250 | continue; |
| 251 | } |
| 252 | if (rdata[1] != alg) { |
| 253 | ++i; |
| 254 | continue; |
| 255 | } |
| 256 | |
| 257 | /* we found the correct SSHFP entry */ |
| 258 | rdata += 2; |
| 259 | for (j = 0; j < 20; ++j) { |
| 260 | if (rdata[j] != (unsigned char)sha1hash[j]) { |
| 261 | ret = 2; |
| 262 | goto finish; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* server fingerprint is supported by a DNS entry, |
| 267 | * we have already determined if DNSSEC was used or not |
| 268 | */ |
| 269 | goto finish; |
| 270 | } |
| 271 | |
| 272 | /* no match */ |
| 273 | ret = 2; |
| 274 | |
| 275 | finish: |
| 276 | val_free_validator_state(); |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | #endif /* ENABLE_DNSSEC */ |
| 281 | |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 282 | static int |
| 283 | nc_client_ssh_update_known_hosts(ssh_session session, const char *hostname) |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 284 | { |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 285 | int ret; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 286 | |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 287 | #if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0)) |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 288 | ret = ssh_session_update_known_hosts(session); |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 289 | #else |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 290 | ret = ssh_write_knownhost(session); |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 291 | #endif |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 292 | |
| 293 | if (ret != SSH_OK) { |
| 294 | WRN(NULL, "Adding the known host \"%s\" failed (%s).", hostname, ssh_get_error(session)); |
| 295 | } |
| 296 | |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | static int |
| 301 | nc_client_ssh_get_srv_pubkey_data(ssh_session session, enum ssh_keytypes_e *srv_pubkey_type, char **hexa, unsigned char **hash_sha1) |
| 302 | { |
| 303 | int ret; |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 304 | ssh_key srv_pubkey; |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 305 | size_t hlen; |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 306 | |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 307 | *hexa = NULL; |
| 308 | *hash_sha1 = NULL; |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 309 | |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 310 | #if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 8, 0)) |
| 311 | ret = ssh_get_server_publickey(session, &srv_pubkey); |
| 312 | #else |
Michal Vasko | cc0aa7d | 2016-05-31 12:48:42 +0200 | [diff] [blame] | 313 | ret = ssh_get_publickey(session, &srv_pubkey); |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 314 | #endif |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 315 | if (ret < 0) { |
roman | 6e5fd70 | 2023-04-27 14:30:27 +0200 | [diff] [blame] | 316 | ERR(NULL, "Unable to get server's public key."); |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 317 | return -1; |
| 318 | } |
| 319 | |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 320 | *srv_pubkey_type = ssh_key_type(srv_pubkey); |
| 321 | ret = ssh_get_publickey_hash(srv_pubkey, SSH_PUBLICKEY_HASH_SHA1, hash_sha1, &hlen); |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 322 | ssh_key_free(srv_pubkey); |
| 323 | if (ret < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 324 | ERR(NULL, "Failed to calculate SHA1 hash of the server public key."); |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 325 | return -1; |
| 326 | } |
| 327 | |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 328 | *hexa = ssh_get_hexa(*hash_sha1, hlen); |
| 329 | if (!*hexa) { |
| 330 | ERR(NULL, "Getting the hostkey's hex string failed."); |
| 331 | return -1; |
| 332 | } |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | #ifdef ENABLE_DNSSEC |
| 338 | static int |
| 339 | nc_client_ssh_do_dnssec_sshfp_check(ssh_session session, enum ssh_keytypes_e srv_pubkey_type, const char *hostname, unsigned char *hash_sha1) |
| 340 | { |
| 341 | int ret; |
| 342 | |
| 343 | if ((srv_pubkey_type != SSH_KEYTYPE_UNKNOWN) && (srv_pubkey_type != SSH_KEYTYPE_RSA1)) { |
| 344 | if (srv_pubkey_type == SSH_KEYTYPE_DSS) { |
| 345 | /* TODO else branch? */ |
| 346 | ret = sshauth_hostkey_hash_dnssec_check(hostname, hash_sha1, 2, 1); |
| 347 | } else if (srv_pubkey_type == SSH_KEYTYPE_RSA) { |
| 348 | ret = sshauth_hostkey_hash_dnssec_check(hostname, hash_sha1, 1, 1); |
| 349 | } else if (srv_pubkey_type == SSH_KEYTYPE_ECDSA) { |
| 350 | ret = sshauth_hostkey_hash_dnssec_check(hostname, hash_sha1, 3, 1); |
| 351 | } |
| 352 | |
| 353 | /* DNSSEC SSHFP check successful, that's enough */ |
| 354 | if (!ret) { |
| 355 | VRB(NULL, "DNSSEC SSHFP check successful."); |
| 356 | #if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0)) |
| 357 | ssh_session_update_known_hosts(session); |
| 358 | #else |
| 359 | ssh_write_knownhost(session); |
| 360 | #endif |
| 361 | } |
| 362 | |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | return 1; |
| 367 | } |
| 368 | |
| 369 | #endif |
| 370 | |
| 371 | static int |
| 372 | nc_client_ssh_auth_hostkey_check(const char *hostname, uint16_t port, ssh_session session) |
| 373 | { |
| 374 | char *hexa = NULL; |
| 375 | unsigned char *hash_sha1 = NULL; |
| 376 | NC_SSH_KNOWNHOSTS_MODE knownhosts_mode = ssh_opts.knownhosts_mode; |
| 377 | enum ssh_keytypes_e srv_pubkey_type; |
| 378 | char answer[5]; |
| 379 | FILE *out = NULL, *in = NULL; |
| 380 | int c, state; |
| 381 | |
| 382 | #ifdef ENABLE_DNSSEC |
| 383 | int dnssec_ret; |
| 384 | #endif |
| 385 | |
| 386 | if (knownhosts_mode == NC_SSH_KNOWNHOSTS_SKIP) { |
| 387 | /* skip all hostkey checks */ |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | if (nc_client_ssh_get_srv_pubkey_data(session, &srv_pubkey_type, &hexa, &hash_sha1)) { |
| 392 | goto error; |
| 393 | } |
| 394 | |
| 395 | #if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0)) |
| 396 | state = ssh_session_is_known_server(session); |
| 397 | #else |
| 398 | state = ssh_is_server_known(session); |
| 399 | #endif |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 400 | |
| 401 | switch (state) { |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 402 | #if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0)) |
| 403 | case SSH_KNOWN_HOSTS_OK: |
| 404 | #else |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 405 | case SSH_SERVER_KNOWN_OK: |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 406 | #endif |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 407 | break; /* ok */ |
| 408 | |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 409 | #if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0)) |
| 410 | case SSH_KNOWN_HOSTS_CHANGED: |
| 411 | #else |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 412 | case SSH_SERVER_KNOWN_CHANGED: |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 413 | #endif |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 414 | if (knownhosts_mode == NC_SSH_KNOWNHOSTS_ACCEPT) { |
| 415 | /* is the mode is set to accept, then accept any connection even if the remote key changed */ |
| 416 | WRN(NULL, "Remote host key changed, but you have requested accept mode so the connection will not be terminated."); |
| 417 | break; |
| 418 | } else { |
| 419 | ERR(NULL, "Remote host key changed, the connection will be terminated!"); |
| 420 | goto error; |
| 421 | } |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 422 | |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 423 | #if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0)) |
| 424 | case SSH_KNOWN_HOSTS_OTHER: |
| 425 | #else |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 426 | case SSH_SERVER_FOUND_OTHER: |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 427 | #endif |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 428 | WRN(NULL, "Remote host key is not known, but a key of another type for this host is known. Continue with caution."); |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 429 | goto hostkey_not_known; |
| 430 | |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 431 | #if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0)) |
| 432 | case SSH_KNOWN_HOSTS_NOT_FOUND: |
| 433 | #else |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 434 | case SSH_SERVER_FILE_NOT_FOUND: |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 435 | #endif |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 436 | WRN(NULL, "Could not find the known hosts file."); |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 437 | goto hostkey_not_known; |
| 438 | |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 439 | #if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0)) |
| 440 | case SSH_KNOWN_HOSTS_UNKNOWN: |
| 441 | #else |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 442 | case SSH_SERVER_NOT_KNOWN: |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 443 | #endif |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 444 | hostkey_not_known: |
| 445 | #ifdef ENABLE_DNSSEC |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 446 | /* do dnssec check, if it's ok then we're done otherwise continue */ |
| 447 | dnssec_ret = nc_client_ssh_do_dnssec_sshfp_check(session, srv_pubkey_type, hostname, hash_sha1); |
| 448 | if (!dnssec_ret) { |
| 449 | ssh_clean_pubkey_hash(&hash_sha1); |
| 450 | ssh_string_free_char(hexa); |
| 451 | return 0; |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 452 | } |
| 453 | #endif |
| 454 | |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 455 | /* open the files for reading/writing */ |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 456 | if (!(in = nc_open_in(1, NULL))) { |
| 457 | goto error; |
| 458 | } |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 459 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 460 | if (!(out = nc_open_out())) { |
| 461 | goto error; |
| 462 | } |
| 463 | |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 464 | if (knownhosts_mode == NC_SSH_KNOWNHOSTS_STRICT) { |
| 465 | /* do not connect if the hostkey is not present in known_hosts file in this mode */ |
| 466 | ERR(NULL, "No %s host key is known for [%s]:%hu and you have requested strict checking.\n", ssh_key_type_to_char(srv_pubkey_type), hostname, port); |
| 467 | goto error; |
| 468 | } else if ((knownhosts_mode == NC_SSH_KNOWNHOSTS_ACCEPT_NEW) || (knownhosts_mode == NC_SSH_KNOWNHOSTS_ACCEPT)) { |
| 469 | /* add a new entry to the known_hosts file without prompting */ |
| 470 | if (nc_client_ssh_update_known_hosts(session, hostname)) { |
| 471 | goto error; |
| 472 | } |
| 473 | |
| 474 | VRB(NULL, "Permanently added '[%s]:%hu' (%s) to the list of known hosts.", hostname, port, ssh_key_type_to_char(srv_pubkey_type)); |
| 475 | |
| 476 | break; |
| 477 | } |
| 478 | |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 479 | /* try to get result from user */ |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 480 | if (fprintf(out, "The authenticity of the host \'%s\' cannot be established.\n", hostname) < 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 481 | ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 482 | goto error; |
| 483 | } |
| 484 | if (fprintf(out, "%s key fingerprint is %s.\n", ssh_key_type_to_char(srv_pubkey_type), hexa) < 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 485 | ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 486 | goto error; |
| 487 | } |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 488 | |
| 489 | #ifdef ENABLE_DNSSEC |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 490 | if (dnssec_ret == 2) { |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 491 | if (fprintf(out, "No matching host key fingerprint found using DNS.\n") < 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 492 | ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 493 | goto error; |
| 494 | } |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 495 | } else if (dnssec_ret == 1) { |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 496 | if (fprintf(out, "Matching host key fingerprint found using DNS.\n") < 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 497 | ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 498 | goto error; |
| 499 | } |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 500 | } |
| 501 | #endif |
| 502 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 503 | if (fprintf(out, "Are you sure you want to continue connecting (yes/no)? ") < 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 504 | ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 505 | goto error; |
| 506 | } |
| 507 | fflush(out); |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 508 | |
| 509 | do { |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 510 | if (fscanf(in, "%4s", answer) == EOF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 511 | ERR(NULL, "Reading from input failed (%s).", feof(in) ? "EOF" : strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 512 | goto error; |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 513 | } |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 514 | while (((c = getc(in)) != EOF) && (c != '\n')) {} |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 515 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 516 | fflush(in); |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 517 | if (!strcmp("yes", answer)) { |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 518 | /* store the key into the known_hosts file */ |
| 519 | nc_client_ssh_update_known_hosts(session, hostname); |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 520 | } else if (!strcmp("no", answer)) { |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 521 | goto error; |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 522 | } else { |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 523 | if (fprintf(out, "Please type 'yes' or 'no': ") < 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 524 | ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 525 | goto error; |
| 526 | } |
Michal Vasko | 5fb5f99 | 2020-11-26 15:19:31 +0100 | [diff] [blame] | 527 | fflush(out); |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 528 | } |
| 529 | } while (strcmp(answer, "yes") && strcmp(answer, "no")); |
| 530 | |
| 531 | break; |
| 532 | |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 533 | #if (LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0)) |
| 534 | case SSH_KNOWN_HOSTS_ERROR: |
| 535 | #else |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 536 | case SSH_SERVER_ERROR: |
Bi-Ruei, Chiu | 947d812 | 2019-09-08 19:28:05 +0800 | [diff] [blame] | 537 | #endif |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 538 | ERR(NULL, "SSH error: %s", ssh_get_error(session)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 539 | goto error; |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 540 | } |
| 541 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 542 | nc_close_inout(in, 1, NULL); |
| 543 | nc_close_inout(out, 1, NULL); |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 544 | ssh_clean_pubkey_hash(&hash_sha1); |
| 545 | ssh_string_free_char(hexa); |
| 546 | return 0; |
| 547 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 548 | error: |
| 549 | nc_close_inout(in, 1, NULL); |
| 550 | nc_close_inout(out, 1, NULL); |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 551 | ssh_clean_pubkey_hash(&hash_sha1); |
| 552 | ssh_string_free_char(hexa); |
| 553 | return -1; |
| 554 | } |
| 555 | |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 556 | char * |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 557 | sshauth_password(const char *username, const char *hostname, void *UNUSED(priv)) |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 558 | { |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 559 | char *buf = NULL; |
| 560 | int c, buflen = 1024, len; |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 561 | struct termios oldterm; |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 562 | FILE *in = NULL, *out = NULL; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 563 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 564 | buf = malloc(buflen * sizeof *buf); |
| 565 | if (!buf) { |
| 566 | ERRMEM; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 567 | return NULL; |
| 568 | } |
| 569 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 570 | if (!(in = nc_open_in(0, &oldterm))) { |
| 571 | goto error; |
| 572 | } |
| 573 | if (!(out = nc_open_out())) { |
| 574 | goto error; |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 575 | } |
| 576 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 577 | if (fprintf(out, "%s@%s password: ", username, hostname) < 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 578 | ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 579 | goto error; |
Michal Vasko | 8858304 | 2018-03-29 09:18:58 +0200 | [diff] [blame] | 580 | } |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 581 | fflush(out); |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 582 | |
| 583 | len = 0; |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 584 | while (((c = fgetc(in)) != EOF) && (c != '\n')) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 585 | if (len >= buflen - 1) { |
| 586 | buflen *= 2; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 587 | buf = nc_realloc(buf, buflen * sizeof *buf); |
| 588 | if (!buf) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 589 | ERRMEM; |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 590 | goto error; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 591 | } |
| 592 | } |
Michal Vasko | 93ab617 | 2018-02-16 15:58:12 +0100 | [diff] [blame] | 593 | buf[len++] = (char)c; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 594 | } |
| 595 | buf[len++] = 0; /* terminating null byte */ |
| 596 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 597 | fprintf(out, "\n"); |
| 598 | |
| 599 | nc_close_inout(in, 0, &oldterm); |
| 600 | nc_close_inout(out, 1, NULL); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 601 | return buf; |
Michal Vasko | 93f26d9 | 2018-02-01 09:08:35 +0100 | [diff] [blame] | 602 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 603 | error: |
| 604 | nc_close_inout(in, 0, &oldterm); |
| 605 | nc_close_inout(out, 1, NULL); |
Michal Vasko | 93f26d9 | 2018-02-01 09:08:35 +0100 | [diff] [blame] | 606 | free(buf); |
| 607 | return NULL; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 608 | } |
| 609 | |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 610 | char * |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 611 | sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo, void *UNUSED(priv)) |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 612 | { |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 613 | unsigned int buflen = 64, cur_len; |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 614 | int c; |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 615 | struct termios oldterm; |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 616 | char *buf = NULL; |
| 617 | FILE *in = NULL, *out = NULL; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 618 | |
| 619 | buf = malloc(buflen * sizeof *buf); |
| 620 | if (!buf) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 621 | ERRMEM; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 622 | return NULL; |
| 623 | } |
| 624 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 625 | if (!(in = nc_open_in(echo, &oldterm))) { |
| 626 | goto error; |
| 627 | } |
| 628 | if (!(out = nc_open_out())) { |
| 629 | goto error; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 630 | } |
| 631 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 632 | if (auth_name && (fprintf(out, "%s\n", auth_name) < 1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 633 | ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 634 | goto error; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 635 | } |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 636 | if (instruction && (fprintf(out, "%s\n", instruction) < 1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 637 | ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 638 | goto error; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 639 | } |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 640 | if (fputs(prompt, out) == EOF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 641 | ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 642 | goto error; |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 643 | } |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 644 | fflush(out); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 645 | |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 646 | cur_len = 0; |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 647 | while (((c = fgetc(in)) != EOF) && (c != '\n')) { |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 648 | if (cur_len >= buflen - 1) { |
| 649 | buflen *= 2; |
| 650 | buf = nc_realloc(buf, buflen * sizeof *buf); |
| 651 | if (!buf) { |
| 652 | ERRMEM; |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 653 | goto error; |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 654 | } |
| 655 | } |
Michal Vasko | 93ab617 | 2018-02-16 15:58:12 +0100 | [diff] [blame] | 656 | buf[cur_len++] = (char)c; |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 657 | } |
| 658 | /* terminating null byte */ |
| 659 | buf[cur_len] = '\0'; |
| 660 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 661 | fprintf(out, "\n"); |
| 662 | |
| 663 | nc_close_inout(in, echo, &oldterm); |
| 664 | nc_close_inout(out, 1, NULL); |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 665 | return buf; |
| 666 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 667 | error: |
| 668 | nc_close_inout(in, echo, &oldterm); |
| 669 | nc_close_inout(out, 1, NULL); |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 670 | free(buf); |
| 671 | return NULL; |
| 672 | } |
| 673 | |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 674 | char * |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 675 | sshauth_privkey_passphrase(const char *privkey_path, void *UNUSED(priv)) |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 676 | { |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 677 | char *buf = NULL; |
| 678 | int c, buflen = 1024, len; |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 679 | struct termios oldterm; |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 680 | FILE *in = NULL, *out = NULL; |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 681 | |
| 682 | buf = malloc(buflen * sizeof *buf); |
| 683 | if (!buf) { |
| 684 | ERRMEM; |
| 685 | return NULL; |
| 686 | } |
| 687 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 688 | if (!(in = nc_open_in(0, &oldterm))) { |
| 689 | goto error; |
| 690 | } |
| 691 | if (!(out = nc_open_out())) { |
| 692 | goto error; |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 693 | } |
| 694 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 695 | if (fprintf(out, "Enter passphrase for the key '%s': ", privkey_path) < 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 696 | ERR(NULL, "Writing into output failed (%s).", feof(out) ? "EOF" : strerror(errno)); |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 697 | goto error; |
Michal Vasko | 8858304 | 2018-03-29 09:18:58 +0200 | [diff] [blame] | 698 | } |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 699 | fflush(out); |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 700 | |
| 701 | len = 0; |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 702 | while (((c = fgetc(in)) != EOF) && (c != '\n')) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 703 | if (len >= buflen - 1) { |
| 704 | buflen *= 2; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 705 | buf = nc_realloc(buf, buflen * sizeof *buf); |
| 706 | if (!buf) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 707 | ERRMEM; |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 708 | goto error; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 709 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 710 | } |
| 711 | buf[len++] = (char)c; |
| 712 | } |
Michal Vasko | a43b8e3 | 2017-05-12 11:46:20 +0200 | [diff] [blame] | 713 | buf[len] = 0; /* terminating null byte */ |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 714 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 715 | fprintf(out, "\n"); |
| 716 | |
| 717 | nc_close_inout(in, 0, &oldterm); |
| 718 | nc_close_inout(out, 1, NULL); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 719 | return buf; |
Michal Vasko | 94e7c2d | 2016-01-21 15:57:57 +0100 | [diff] [blame] | 720 | |
Michal Vasko | 51228ac | 2018-03-29 14:57:53 +0200 | [diff] [blame] | 721 | error: |
| 722 | nc_close_inout(in, 0, &oldterm); |
| 723 | nc_close_inout(out, 1, NULL); |
Michal Vasko | 94e7c2d | 2016-01-21 15:57:57 +0100 | [diff] [blame] | 724 | free(buf); |
Michal Vasko | 94e7c2d | 2016-01-21 15:57:57 +0100 | [diff] [blame] | 725 | return NULL; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 726 | } |
| 727 | |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 728 | API int |
| 729 | nc_client_ssh_set_knownhosts_path(const char *path) |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 730 | { |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 731 | free(ssh_opts.knownhosts_path); |
| 732 | |
| 733 | if (!path) { |
| 734 | ssh_opts.knownhosts_path = NULL; |
| 735 | return 0; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 736 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 737 | |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 738 | ssh_opts.knownhosts_path = strdup(path); |
| 739 | if (!ssh_opts.knownhosts_path) { |
| 740 | ERRMEM; |
| 741 | return 1; |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 742 | } |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 743 | |
| 744 | return 0; |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 745 | } |
| 746 | |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 747 | API void |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 748 | nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_MODE mode) |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 749 | { |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 750 | ssh_opts.knownhosts_mode = mode; |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 751 | } |
Michal Vasko | ef112d7 | 2016-02-18 13:28:25 +0100 | [diff] [blame] | 752 | |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 753 | static void |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 754 | _nc_client_ssh_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname, void *priv), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 755 | void *priv, struct nc_client_ssh_opts *opts) |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 756 | { |
| 757 | if (auth_password) { |
| 758 | opts->auth_password = auth_password; |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 759 | opts->auth_password_priv = priv; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 760 | } else { |
| 761 | opts->auth_password = sshauth_password; |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 762 | opts->auth_password_priv = NULL; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | static void |
| 767 | _nc_client_ssh_get_auth_password_clb(char *(**auth_password)(const char *username, const char *hostname, void *priv), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 768 | void **priv, struct nc_client_ssh_opts *opts) |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 769 | { |
| 770 | if (auth_password) { |
| 771 | (*auth_password) = opts->auth_password == sshauth_password ? NULL : opts->auth_password; |
| 772 | } |
| 773 | if (priv) { |
| 774 | (*priv) = opts->auth_password_priv; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | |
| 778 | API void |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 779 | nc_client_ssh_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname, void *priv), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 780 | void *priv) |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 781 | { |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 782 | _nc_client_ssh_set_auth_password_clb(auth_password, priv, &ssh_opts); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | API void |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 786 | nc_client_ssh_ch_set_auth_password_clb(char *(*auth_password)(const char *username, const char *hostname, void *priv), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 787 | void *priv) |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 788 | { |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 789 | _nc_client_ssh_set_auth_password_clb(auth_password, priv, &ssh_ch_opts); |
| 790 | } |
| 791 | |
| 792 | API void |
| 793 | nc_client_ssh_get_auth_password_clb(char *(**auth_password)(const char *username, const char *hostname, void *priv), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 794 | void **priv) |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 795 | { |
| 796 | _nc_client_ssh_get_auth_password_clb(auth_password, priv, &ssh_opts); |
| 797 | } |
| 798 | |
| 799 | API void |
| 800 | nc_client_ssh_ch_get_auth_password_clb(char *(**auth_password)(const char *username, const char *hostname, void *priv), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 801 | void **priv) |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 802 | { |
| 803 | _nc_client_ssh_get_auth_password_clb(auth_password, priv, &ssh_ch_opts); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static void |
| 807 | _nc_client_ssh_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 808 | const char *prompt, int echo, void *priv), |
| 809 | void *priv, struct nc_client_ssh_opts *opts) |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 810 | { |
| 811 | if (auth_interactive) { |
| 812 | opts->auth_interactive = auth_interactive; |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 813 | opts->auth_interactive_priv = priv; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 814 | } else { |
| 815 | opts->auth_interactive = sshauth_interactive; |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 816 | opts->auth_interactive_priv = NULL; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | static void |
| 821 | _nc_client_ssh_get_auth_interactive_clb(char *(**auth_interactive)(const char *auth_name, const char *instruction, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 822 | const char *prompt, int echo, void *priv), |
| 823 | void **priv, struct nc_client_ssh_opts *opts) |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 824 | { |
| 825 | if (auth_interactive) { |
| 826 | (*auth_interactive) = opts->auth_interactive == sshauth_interactive ? NULL : opts->auth_interactive; |
| 827 | } |
| 828 | if (priv) { |
| 829 | (*priv) = opts->auth_interactive_priv; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 830 | } |
| 831 | } |
| 832 | |
| 833 | API void |
| 834 | nc_client_ssh_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 835 | const char *prompt, int echo, void *priv), |
| 836 | void *priv) |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 837 | { |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 838 | _nc_client_ssh_set_auth_interactive_clb(auth_interactive, priv, &ssh_opts); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | API void |
| 842 | nc_client_ssh_ch_set_auth_interactive_clb(char *(*auth_interactive)(const char *auth_name, const char *instruction, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 843 | const char *prompt, int echo, void *priv), |
| 844 | void *priv) |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 845 | { |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 846 | _nc_client_ssh_set_auth_interactive_clb(auth_interactive, priv, &ssh_ch_opts); |
| 847 | } |
| 848 | |
| 849 | API void |
| 850 | nc_client_ssh_get_auth_interactive_clb(char *(**auth_interactive)(const char *auth_name, const char *instruction, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 851 | const char *prompt, int echo, void *priv), |
| 852 | void **priv) |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 853 | { |
| 854 | _nc_client_ssh_get_auth_interactive_clb(auth_interactive, priv, &ssh_opts); |
| 855 | } |
| 856 | |
| 857 | API void |
| 858 | nc_client_ssh_ch_get_auth_interactive_clb(char *(**auth_interactive)(const char *auth_name, const char *instruction, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 859 | const char *prompt, int echo, void *priv), |
| 860 | void **priv) |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 861 | { |
| 862 | _nc_client_ssh_get_auth_interactive_clb(auth_interactive, priv, &ssh_ch_opts); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | static void |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 866 | _nc_client_ssh_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path, void *priv), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 867 | void *priv, struct nc_client_ssh_opts *opts) |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 868 | { |
| 869 | if (auth_privkey_passphrase) { |
| 870 | opts->auth_privkey_passphrase = auth_privkey_passphrase; |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 871 | opts->auth_privkey_passphrase_priv = priv; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 872 | } else { |
| 873 | opts->auth_privkey_passphrase = sshauth_privkey_passphrase; |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 874 | opts->auth_privkey_passphrase_priv = NULL; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | static void |
| 879 | _nc_client_ssh_get_auth_privkey_passphrase_clb(char *(**auth_privkey_passphrase)(const char *privkey_path, void *priv), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 880 | void **priv, struct nc_client_ssh_opts *opts) |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 881 | { |
| 882 | if (auth_privkey_passphrase) { |
| 883 | (*auth_privkey_passphrase) = opts->auth_privkey_passphrase == sshauth_privkey_passphrase ? NULL : opts->auth_privkey_passphrase; |
| 884 | } |
| 885 | if (priv) { |
| 886 | (*priv) = opts->auth_privkey_passphrase_priv; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
| 890 | API void |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 891 | nc_client_ssh_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path, void *priv), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 892 | void *priv) |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 893 | { |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 894 | _nc_client_ssh_set_auth_privkey_passphrase_clb(auth_privkey_passphrase, priv, &ssh_opts); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | API void |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 898 | nc_client_ssh_ch_set_auth_privkey_passphrase_clb(char *(*auth_privkey_passphrase)(const char *privkey_path, void *priv), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 899 | void *priv) |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 900 | { |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 901 | _nc_client_ssh_set_auth_privkey_passphrase_clb(auth_privkey_passphrase, priv, &ssh_ch_opts); |
| 902 | } |
| 903 | |
| 904 | API void |
| 905 | nc_client_ssh_get_auth_privkey_passphrase_clb(char *(**auth_privkey_passphrase)(const char *privkey_path, void *priv), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 906 | void **priv) |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 907 | { |
| 908 | _nc_client_ssh_get_auth_privkey_passphrase_clb(auth_privkey_passphrase, priv, &ssh_opts); |
| 909 | } |
| 910 | |
| 911 | API void |
| 912 | nc_client_ssh_ch_get_auth_privkey_passphrase_clb(char *(**auth_privkey_passphrase)(const char *privkey_path, void *priv), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 913 | void **priv) |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 914 | { |
| 915 | _nc_client_ssh_get_auth_privkey_passphrase_clb(auth_privkey_passphrase, priv, &ssh_ch_opts); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 918 | static int |
| 919 | _nc_client_ssh_add_keypair(const char *pub_key, const char *priv_key, struct nc_client_ssh_opts *opts) |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 920 | { |
| 921 | int i; |
| 922 | FILE *key; |
| 923 | char line[128]; |
| 924 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 925 | NC_CHECK_ARG_RET(NULL, pub_key, priv_key, -1); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 926 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 927 | for (i = 0; i < opts->key_count; ++i) { |
| 928 | if (!strcmp(opts->keys[i].pubkey_path, pub_key) || !strcmp(opts->keys[i].privkey_path, priv_key)) { |
| 929 | if (strcmp(opts->keys[i].pubkey_path, pub_key)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 930 | WRN(NULL, "Private key \"%s\" found with another public key \"%s\".", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 931 | priv_key, opts->keys[i].pubkey_path); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 932 | continue; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 933 | } else if (strcmp(opts->keys[i].privkey_path, priv_key)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 934 | WRN(NULL, "Public key \"%s\" found with another private key \"%s\".", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 935 | pub_key, opts->keys[i].privkey_path); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 936 | continue; |
| 937 | } |
| 938 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 939 | ERR(NULL, "SSH key pair already set."); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 940 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 944 | /* add the keys */ |
| 945 | ++opts->key_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 946 | opts->keys = nc_realloc(opts->keys, opts->key_count * sizeof *opts->keys); |
| 947 | if (!opts->keys) { |
| 948 | ERRMEM; |
| 949 | return -1; |
| 950 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 951 | opts->keys[opts->key_count - 1].pubkey_path = strdup(pub_key); |
| 952 | opts->keys[opts->key_count - 1].privkey_path = strdup(priv_key); |
| 953 | opts->keys[opts->key_count - 1].privkey_crypt = 0; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 954 | |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 955 | if (!opts->keys[opts->key_count - 1].pubkey_path || !opts->keys[opts->key_count - 1].privkey_path) { |
| 956 | ERRMEM; |
| 957 | return -1; |
| 958 | } |
| 959 | |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 960 | /* check encryption */ |
| 961 | if ((key = fopen(priv_key, "r"))) { |
| 962 | /* 1st line - key type */ |
| 963 | if (!fgets(line, sizeof line, key)) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 964 | fclose(key); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 965 | ERR(NULL, "fgets() on %s failed.", priv_key); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 966 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 967 | } |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 968 | /* 2nd line - encryption information or key */ |
| 969 | if (!fgets(line, sizeof line, key)) { |
| 970 | fclose(key); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 971 | ERR(NULL, "fgets() on %s failed.", priv_key); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 972 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 973 | } |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 974 | fclose(key); |
| 975 | if (strcasestr(line, "encrypted")) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 976 | opts->keys[opts->key_count - 1].privkey_crypt = 1; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 977 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 978 | } |
| 979 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 980 | return 0; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 984 | nc_client_ssh_add_keypair(const char *pub_key, const char *priv_key) |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 985 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 986 | return _nc_client_ssh_add_keypair(pub_key, priv_key, &ssh_opts); |
| 987 | } |
| 988 | |
| 989 | API int |
| 990 | nc_client_ssh_ch_add_keypair(const char *pub_key, const char *priv_key) |
| 991 | { |
| 992 | return _nc_client_ssh_add_keypair(pub_key, priv_key, &ssh_ch_opts); |
| 993 | } |
| 994 | |
| 995 | static int |
| 996 | _nc_client_ssh_del_keypair(int idx, struct nc_client_ssh_opts *opts) |
| 997 | { |
| 998 | if (idx >= opts->key_count) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 999 | ERRARG(NULL, "idx"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1000 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1001 | } |
| 1002 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1003 | free(opts->keys[idx].pubkey_path); |
| 1004 | free(opts->keys[idx].privkey_path); |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 1005 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1006 | --opts->key_count; |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 1007 | if (idx < opts->key_count) { |
| 1008 | memcpy(&opts->keys[idx], &opts->keys[opts->key_count], sizeof *opts->keys); |
| 1009 | } |
| 1010 | if (opts->key_count) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1011 | opts->keys = nc_realloc(opts->keys, opts->key_count * sizeof *opts->keys); |
| 1012 | if (!opts->keys) { |
| 1013 | ERRMEM; |
| 1014 | return -1; |
| 1015 | } |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 1016 | } else { |
| 1017 | free(opts->keys); |
| 1018 | opts->keys = NULL; |
| 1019 | } |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 1020 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1021 | return 0; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1025 | nc_client_ssh_del_keypair(int idx) |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1026 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1027 | return _nc_client_ssh_del_keypair(idx, &ssh_opts); |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1031 | nc_client_ssh_ch_del_keypair(int idx) |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 1032 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1033 | return _nc_client_ssh_del_keypair(idx, &ssh_ch_opts); |
| 1034 | } |
| 1035 | |
| 1036 | static int |
| 1037 | _nc_client_ssh_get_keypair_count(struct nc_client_ssh_opts *opts) |
| 1038 | { |
| 1039 | return opts->key_count; |
| 1040 | } |
| 1041 | |
| 1042 | API int |
| 1043 | nc_client_ssh_get_keypair_count(void) |
| 1044 | { |
| 1045 | return _nc_client_ssh_get_keypair_count(&ssh_opts); |
| 1046 | } |
| 1047 | |
| 1048 | API int |
| 1049 | nc_client_ssh_ch_get_keypair_count(void) |
| 1050 | { |
| 1051 | return _nc_client_ssh_get_keypair_count(&ssh_ch_opts); |
| 1052 | } |
| 1053 | |
| 1054 | static int |
| 1055 | _nc_client_ssh_get_keypair(int idx, const char **pub_key, const char **priv_key, struct nc_client_ssh_opts *opts) |
| 1056 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1057 | if (idx >= opts->key_count) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1058 | ERRARG(NULL, "idx"); |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1059 | return -1; |
| 1060 | } else if (!pub_key && !priv_key) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1061 | ERRARG(NULL, "pub_key and priv_key"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1062 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1063 | } |
| 1064 | |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 1065 | if (pub_key) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1066 | *pub_key = opts->keys[idx].pubkey_path; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 1067 | } |
| 1068 | if (priv_key) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1069 | *priv_key = opts->keys[idx].privkey_path; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 1070 | } |
| 1071 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1072 | return 0; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1073 | } |
| 1074 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1075 | API int |
| 1076 | nc_client_ssh_get_keypair(int idx, const char **pub_key, const char **priv_key) |
| 1077 | { |
| 1078 | return _nc_client_ssh_get_keypair(idx, pub_key, priv_key, &ssh_opts); |
| 1079 | } |
| 1080 | |
| 1081 | API int |
| 1082 | nc_client_ssh_ch_get_keypair(int idx, const char **pub_key, const char **priv_key) |
| 1083 | { |
| 1084 | return _nc_client_ssh_get_keypair(idx, pub_key, priv_key, &ssh_ch_opts); |
| 1085 | } |
| 1086 | |
| 1087 | static void |
| 1088 | _nc_client_ssh_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref, struct nc_client_ssh_opts *opts) |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 1089 | { |
| 1090 | if (pref < 0) { |
| 1091 | pref = -1; |
| 1092 | } |
| 1093 | |
| 1094 | if (auth_type == NC_SSH_AUTH_INTERACTIVE) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1095 | opts->auth_pref[0].value = pref; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 1096 | } else if (auth_type == NC_SSH_AUTH_PASSWORD) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1097 | opts->auth_pref[1].value = pref; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 1098 | } else if (auth_type == NC_SSH_AUTH_PUBLICKEY) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1099 | opts->auth_pref[2].value = pref; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1103 | API void |
| 1104 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref) |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 1105 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1106 | _nc_client_ssh_set_auth_pref(auth_type, pref, &ssh_opts); |
| 1107 | } |
| 1108 | |
| 1109 | API void |
| 1110 | nc_client_ssh_ch_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref) |
| 1111 | { |
| 1112 | _nc_client_ssh_set_auth_pref(auth_type, pref, &ssh_ch_opts); |
| 1113 | } |
| 1114 | |
| 1115 | static int16_t |
| 1116 | _nc_client_ssh_get_auth_pref(NC_SSH_AUTH_TYPE auth_type, struct nc_client_ssh_opts *opts) |
| 1117 | { |
| 1118 | int16_t pref = 0; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 1119 | |
| 1120 | if (auth_type == NC_SSH_AUTH_INTERACTIVE) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1121 | pref = opts->auth_pref[0].value; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 1122 | } else if (auth_type == NC_SSH_AUTH_PASSWORD) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1123 | pref = opts->auth_pref[1].value; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 1124 | } else if (auth_type == NC_SSH_AUTH_PUBLICKEY) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1125 | pref = opts->auth_pref[2].value; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | return pref; |
| 1129 | } |
| 1130 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1131 | API int16_t |
| 1132 | nc_client_ssh_get_auth_pref(NC_SSH_AUTH_TYPE auth_type) |
| 1133 | { |
| 1134 | return _nc_client_ssh_get_auth_pref(auth_type, &ssh_opts); |
| 1135 | } |
| 1136 | |
| 1137 | API int16_t |
| 1138 | nc_client_ssh_ch_get_auth_pref(NC_SSH_AUTH_TYPE auth_type) |
| 1139 | { |
| 1140 | return _nc_client_ssh_get_auth_pref(auth_type, &ssh_ch_opts); |
| 1141 | } |
| 1142 | |
| 1143 | static int |
| 1144 | _nc_client_ssh_set_username(const char *username, struct nc_client_ssh_opts *opts) |
| 1145 | { |
| 1146 | if (opts->username) { |
| 1147 | free(opts->username); |
| 1148 | } |
| 1149 | if (username) { |
| 1150 | opts->username = strdup(username); |
| 1151 | if (!opts->username) { |
| 1152 | ERRMEM; |
| 1153 | return -1; |
| 1154 | } |
| 1155 | } else { |
| 1156 | opts->username = NULL; |
| 1157 | } |
| 1158 | |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | API int |
| 1163 | nc_client_ssh_set_username(const char *username) |
| 1164 | { |
| 1165 | return _nc_client_ssh_set_username(username, &ssh_opts); |
| 1166 | } |
| 1167 | |
| 1168 | API int |
| 1169 | nc_client_ssh_ch_set_username(const char *username) |
| 1170 | { |
| 1171 | return _nc_client_ssh_set_username(username, &ssh_ch_opts); |
| 1172 | } |
| 1173 | |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 1174 | static const char * |
| 1175 | _nc_client_ssh_get_username(struct nc_client_ssh_opts *opts) |
| 1176 | { |
| 1177 | return opts->username; |
| 1178 | } |
| 1179 | |
| 1180 | API const char * |
| 1181 | nc_client_ssh_get_username(void) |
| 1182 | { |
| 1183 | return _nc_client_ssh_get_username(&ssh_opts); |
| 1184 | } |
| 1185 | |
| 1186 | API const char * |
| 1187 | nc_client_ssh_ch_get_username(void) |
| 1188 | { |
| 1189 | return _nc_client_ssh_get_username(&ssh_ch_opts); |
| 1190 | } |
| 1191 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1192 | API int |
| 1193 | nc_client_ssh_ch_add_bind_listen(const char *address, uint16_t port) |
| 1194 | { |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 1195 | return nc_client_ch_add_bind_listen(address, port, NULL, NC_TI_LIBSSH); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | API int |
| 1199 | nc_client_ssh_ch_del_bind(const char *address, uint16_t port) |
| 1200 | { |
| 1201 | return nc_client_ch_del_bind(address, port, NC_TI_LIBSSH); |
| 1202 | } |
| 1203 | |
Michal Vasko | 8e2f4a6 | 2016-02-01 15:59:48 +0100 | [diff] [blame] | 1204 | /* Establish a secure SSH connection and authenticate. |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1205 | * Host, port, username, and a connected socket is expected to be set. |
Radek Krejci | ae813f4 | 2018-07-02 13:38:30 +0200 | [diff] [blame] | 1206 | * |
| 1207 | * return values |
| 1208 | * -1 failure |
| 1209 | * 0 try again |
| 1210 | * 1 success |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1211 | */ |
| 1212 | static int |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1213 | connect_ssh_session(struct nc_session *session, struct nc_client_ssh_opts *opts, int timeout) |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1214 | { |
Radek Krejci | ae813f4 | 2018-07-02 13:38:30 +0200 | [diff] [blame] | 1215 | int j, ret_auth, userauthlist, ret, attempt = 0; |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 1216 | NC_SSH_AUTH_TYPE auth; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1217 | int16_t pref; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1218 | const char *prompt; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1219 | char *s, *answer, echo; |
| 1220 | ssh_key pubkey, privkey; |
| 1221 | ssh_session ssh_sess; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1222 | struct timespec ts_timeout; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1223 | |
| 1224 | ssh_sess = session->ti.libssh.session; |
| 1225 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1226 | nc_timeouttime_get(&ts_timeout, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1227 | while ((ret = ssh_connect(ssh_sess)) == SSH_AGAIN) { |
| 1228 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1229 | if (nc_timeouttime_cur_diff(&ts_timeout) < 1) { |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1230 | break; |
| 1231 | } |
| 1232 | } |
| 1233 | if (ret == SSH_AGAIN) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1234 | ERR(session, "SSH connect timeout."); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1235 | return 0; |
| 1236 | } else if (ret != SSH_OK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1237 | ERR(session, "Starting the SSH session failed (%s).", ssh_get_error(ssh_sess)); |
| 1238 | DBG(session, "Error code %d.", ssh_get_error_code(ssh_sess)); |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1239 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1240 | } |
| 1241 | |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 1242 | if (nc_client_ssh_auth_hostkey_check(session->host, session->port, ssh_sess)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1243 | ERR(session, "Checking the host key failed."); |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1244 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1245 | } |
| 1246 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1247 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1248 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1249 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1250 | while ((ret_auth = ssh_userauth_none(ssh_sess, NULL)) == SSH_AUTH_AGAIN) { |
| 1251 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1252 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1253 | break; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | if (ret_auth == SSH_AUTH_AGAIN) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1257 | ERR(session, "Request authentication methods timeout."); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1258 | return 0; |
| 1259 | } else if (ret_auth == SSH_AUTH_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1260 | ERR(session, "Authentication failed (%s).", ssh_get_error(ssh_sess)); |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1261 | return -1; |
Radek Krejci | ae813f4 | 2018-07-02 13:38:30 +0200 | [diff] [blame] | 1262 | } else if (ret_auth == SSH_AUTH_SUCCESS) { |
Radek Krejci | ae813f4 | 2018-07-02 13:38:30 +0200 | [diff] [blame] | 1263 | return 1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | /* check what authentication methods are available */ |
| 1267 | userauthlist = ssh_userauth_list(ssh_sess, NULL); |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 1268 | |
| 1269 | /* remove those disabled */ |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1270 | if (opts->auth_pref[0].value < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1271 | VRB(session, "Interactive SSH authentication method was disabled."); |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 1272 | userauthlist &= ~SSH_AUTH_METHOD_INTERACTIVE; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1273 | } |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1274 | if (opts->auth_pref[1].value < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1275 | VRB(session, "Password SSH authentication method was disabled."); |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 1276 | userauthlist &= ~SSH_AUTH_METHOD_PASSWORD; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1277 | } |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1278 | if (opts->auth_pref[2].value < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1279 | VRB(session, "Publickey SSH authentication method was disabled."); |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 1280 | userauthlist &= ~SSH_AUTH_METHOD_PUBLICKEY; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1281 | } |
| 1282 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1283 | do { |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 1284 | auth = 0; |
| 1285 | pref = 0; |
| 1286 | if (userauthlist & SSH_AUTH_METHOD_INTERACTIVE) { |
| 1287 | auth = NC_SSH_AUTH_INTERACTIVE; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1288 | pref = opts->auth_pref[0].value; |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 1289 | } |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1290 | if ((userauthlist & SSH_AUTH_METHOD_PASSWORD) && (opts->auth_pref[1].value > pref)) { |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 1291 | auth = NC_SSH_AUTH_PASSWORD; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1292 | pref = opts->auth_pref[1].value; |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 1293 | } |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1294 | if ((userauthlist & SSH_AUTH_METHOD_PUBLICKEY) && (opts->auth_pref[2].value > pref)) { |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 1295 | auth = NC_SSH_AUTH_PUBLICKEY; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1296 | } |
| 1297 | |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 1298 | if (!auth) { |
Radek Krejci | ae813f4 | 2018-07-02 13:38:30 +0200 | [diff] [blame] | 1299 | if (!attempt) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1300 | ERR(session, "Unable to authenticate to the remote server (no supported authentication methods detected)."); |
Radek Krejci | ae813f4 | 2018-07-02 13:38:30 +0200 | [diff] [blame] | 1301 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1302 | ERR(session, "Unable to authenticate to the remote server (all attempts via supported authentication " |
| 1303 | "methods failed)."); |
Radek Krejci | ae813f4 | 2018-07-02 13:38:30 +0200 | [diff] [blame] | 1304 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1305 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | /* found common authentication method */ |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 1309 | switch (auth) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1310 | case NC_SSH_AUTH_PASSWORD: |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 1311 | userauthlist &= ~SSH_AUTH_METHOD_PASSWORD; |
| 1312 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1313 | VRB(session, "Password authentication (host \"%s\", user \"%s\").", session->host, session->username); |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 1314 | s = opts->auth_password(session->username, session->host, opts->auth_password_priv); |
Michal Vasko | 8858304 | 2018-03-29 09:18:58 +0200 | [diff] [blame] | 1315 | if (s == NULL) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1316 | ERR(session, "Unable to get the password."); |
Michal Vasko | 8858304 | 2018-03-29 09:18:58 +0200 | [diff] [blame] | 1317 | return -1; |
| 1318 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1319 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1320 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1321 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1322 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1323 | while ((ret_auth = ssh_userauth_password(ssh_sess, session->username, s)) == SSH_AUTH_AGAIN) { |
| 1324 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1325 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1326 | break; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1327 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1328 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1329 | memset(s, 0, strlen(s)); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1330 | free(s); |
| 1331 | break; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1332 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1333 | case NC_SSH_AUTH_INTERACTIVE: |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 1334 | userauthlist &= ~SSH_AUTH_METHOD_INTERACTIVE; |
| 1335 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1336 | VRB(session, "Keyboard-interactive authentication."); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1337 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1338 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1339 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1340 | } |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1341 | while (((ret_auth = ssh_userauth_kbdint(ssh_sess, NULL, NULL)) == SSH_AUTH_INFO) || |
| 1342 | (ret_auth == SSH_AUTH_AGAIN)) { |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1343 | if (ret_auth == SSH_AUTH_AGAIN) { |
| 1344 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1345 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1346 | break; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1347 | } |
| 1348 | continue; |
| 1349 | } |
| 1350 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1351 | for (j = 0; j < ssh_userauth_kbdint_getnprompts(ssh_sess); ++j) { |
| 1352 | prompt = ssh_userauth_kbdint_getprompt(ssh_sess, j, &echo); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1353 | if (!prompt) { |
| 1354 | ret_auth = SSH_AUTH_ERROR; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1355 | break; |
| 1356 | } |
Michal Vasko | 8bf28d1 | 2016-02-24 13:29:42 +0100 | [diff] [blame] | 1357 | |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1358 | answer = opts->auth_interactive(ssh_userauth_kbdint_getname(ssh_sess), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1359 | ssh_userauth_kbdint_getinstruction(ssh_sess), |
| 1360 | prompt, echo, opts->auth_interactive_priv); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1361 | if (ssh_userauth_kbdint_setanswer(ssh_sess, j, answer) < 0) { |
| 1362 | free(answer); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1363 | ret_auth = SSH_AUTH_ERROR; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1364 | break; |
| 1365 | } |
| 1366 | free(answer); |
| 1367 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1368 | if (ret_auth == SSH_AUTH_ERROR) { |
| 1369 | break; |
| 1370 | } |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1371 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1372 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1373 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1374 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1375 | break; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1376 | |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 1377 | case NC_SSH_AUTH_PUBLICKEY: |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 1378 | userauthlist &= ~SSH_AUTH_METHOD_PUBLICKEY; |
| 1379 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1380 | VRB(session, "Publickey athentication."); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1381 | |
| 1382 | /* if publickeys path not provided, we cannot continue */ |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1383 | if (!opts->key_count) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1384 | VRB(session, "No key pair specified."); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1385 | break; |
| 1386 | } |
| 1387 | |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1388 | for (j = 0; j < opts->key_count; j++) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1389 | VRB(session, "Trying to authenticate using %spair \"%s\" \"%s\".", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1390 | opts->keys[j].privkey_crypt ? "password-protected " : "", opts->keys[j].privkey_path, |
| 1391 | opts->keys[j].pubkey_path); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1392 | |
Michal Vasko | 7abcdeb | 2016-05-30 15:27:00 +0200 | [diff] [blame] | 1393 | ret = ssh_pki_import_pubkey_file(opts->keys[j].pubkey_path, &pubkey); |
| 1394 | if (ret == SSH_EOF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1395 | WRN(session, "Failed to import the key \"%s\" (File access problem).", opts->keys[j].pubkey_path); |
Michal Vasko | 7abcdeb | 2016-05-30 15:27:00 +0200 | [diff] [blame] | 1396 | continue; |
| 1397 | } else if (ret == SSH_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1398 | WRN(session, "Failed to import the key \"%s\" (SSH error).", opts->keys[j].pubkey_path); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1399 | continue; |
| 1400 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1401 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1402 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1403 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1404 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1405 | while ((ret_auth = ssh_userauth_try_publickey(ssh_sess, NULL, pubkey)) == SSH_AUTH_AGAIN) { |
| 1406 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1407 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1408 | break; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1409 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1410 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1411 | ssh_key_free(pubkey); |
| 1412 | |
| 1413 | if (ret_auth == SSH_AUTH_DENIED) { |
| 1414 | continue; |
| 1415 | } else if (ret_auth != SSH_AUTH_SUCCESS) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1416 | break; |
| 1417 | } |
| 1418 | |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1419 | if (opts->keys[j].privkey_crypt) { |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 1420 | s = opts->auth_privkey_passphrase(opts->keys[j].privkey_path, opts->auth_privkey_passphrase_priv); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1421 | } else { |
| 1422 | s = NULL; |
| 1423 | } |
| 1424 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1425 | ret = ssh_pki_import_privkey_file(opts->keys[j].privkey_path, s, NULL, NULL, &privkey); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1426 | if (s) { |
| 1427 | memset(s, 0, strlen(s)); |
| 1428 | free(s); |
| 1429 | } |
Michal Vasko | 7abcdeb | 2016-05-30 15:27:00 +0200 | [diff] [blame] | 1430 | if (ret == SSH_EOF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1431 | WRN(session, "Failed to import the key \"%s\" (File access problem).", opts->keys[j].privkey_path); |
Michal Vasko | 7abcdeb | 2016-05-30 15:27:00 +0200 | [diff] [blame] | 1432 | continue; |
| 1433 | } else if (ret == SSH_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1434 | WRN(session, "Failed to import the key \"%s\" (SSH error).", opts->keys[j].privkey_path); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1435 | continue; |
| 1436 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1437 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1438 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1439 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1440 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1441 | while ((ret_auth = ssh_userauth_publickey(ssh_sess, NULL, privkey)) == SSH_AUTH_AGAIN) { |
| 1442 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1443 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1444 | break; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1445 | } |
| 1446 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1447 | ssh_key_free(privkey); |
| 1448 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1449 | if (ret_auth != SSH_AUTH_DENIED) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1450 | break; |
| 1451 | } |
| 1452 | } |
| 1453 | break; |
| 1454 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1455 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1456 | switch (ret_auth) { |
| 1457 | case SSH_AUTH_AGAIN: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1458 | ERR(session, "Authentication response timeout."); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1459 | return 0; |
| 1460 | case SSH_AUTH_ERROR: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1461 | ERR(session, "Authentication failed (%s).", ssh_get_error(ssh_sess)); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1462 | return -1; |
| 1463 | case SSH_AUTH_DENIED: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1464 | WRN(session, "Authentication denied."); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1465 | break; |
| 1466 | case SSH_AUTH_PARTIAL: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1467 | VRB(session, "Partial authentication success."); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1468 | break; |
| 1469 | case SSH_AUTH_SUCCESS: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1470 | VRB(session, "Authentication successful."); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1471 | break; |
| 1472 | case SSH_AUTH_INFO: |
| 1473 | ERRINT; |
| 1474 | return -1; |
| 1475 | } |
Radek Krejci | ae813f4 | 2018-07-02 13:38:30 +0200 | [diff] [blame] | 1476 | |
| 1477 | attempt++; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1478 | } while (ret_auth != SSH_AUTH_SUCCESS); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1479 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1480 | return 1; |
Michal Vasko | 8e2f4a6 | 2016-02-01 15:59:48 +0100 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | /* Open new SSH channel and request the 'netconf' subsystem. |
| 1484 | * SSH connection is expected to be established. |
| 1485 | */ |
| 1486 | static int |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1487 | open_netconf_channel(struct nc_session *session, int timeout) |
Michal Vasko | 8e2f4a6 | 2016-02-01 15:59:48 +0100 | [diff] [blame] | 1488 | { |
| 1489 | ssh_session ssh_sess; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1490 | int ret; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1491 | struct timespec ts_timeout; |
Michal Vasko | 8e2f4a6 | 2016-02-01 15:59:48 +0100 | [diff] [blame] | 1492 | |
| 1493 | ssh_sess = session->ti.libssh.session; |
| 1494 | |
| 1495 | if (!ssh_is_connected(ssh_sess)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1496 | ERR(session, "SSH session not connected."); |
Michal Vasko | 8e2f4a6 | 2016-02-01 15:59:48 +0100 | [diff] [blame] | 1497 | return -1; |
| 1498 | } |
| 1499 | |
| 1500 | if (session->ti.libssh.channel) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1501 | ERR(session, "SSH channel already created."); |
Michal Vasko | 8e2f4a6 | 2016-02-01 15:59:48 +0100 | [diff] [blame] | 1502 | return -1; |
| 1503 | } |
| 1504 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1505 | /* open a channel */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1506 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1507 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1508 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1509 | session->ti.libssh.channel = ssh_channel_new(ssh_sess); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1510 | while ((ret = ssh_channel_open_session(session->ti.libssh.channel)) == SSH_AGAIN) { |
| 1511 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1512 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1513 | break; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1514 | } |
| 1515 | } |
| 1516 | if (ret == SSH_AGAIN) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1517 | ERR(session, "Opening an SSH channel timeout elapsed."); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1518 | ssh_channel_free(session->ti.libssh.channel); |
| 1519 | session->ti.libssh.channel = NULL; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1520 | return 0; |
| 1521 | } else if (ret == SSH_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1522 | ERR(session, "Opening an SSH channel failed (%s).", ssh_get_error(ssh_sess)); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1523 | ssh_channel_free(session->ti.libssh.channel); |
| 1524 | session->ti.libssh.channel = NULL; |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1525 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | /* execute the NETCONF subsystem on the channel */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1529 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1530 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1531 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1532 | while ((ret = ssh_channel_request_subsystem(session->ti.libssh.channel, "netconf")) == SSH_AGAIN) { |
| 1533 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1534 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1535 | break; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1536 | } |
| 1537 | } |
| 1538 | if (ret == SSH_AGAIN) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1539 | ERR(session, "Starting the \"netconf\" SSH subsystem timeout elapsed."); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1540 | ssh_channel_free(session->ti.libssh.channel); |
| 1541 | session->ti.libssh.channel = NULL; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1542 | return 0; |
| 1543 | } else if (ret == SSH_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1544 | ERR(session, "Starting the \"netconf\" SSH subsystem failed (%s).", ssh_get_error(ssh_sess)); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1545 | ssh_channel_free(session->ti.libssh.channel); |
| 1546 | session->ti.libssh.channel = NULL; |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1547 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1548 | } |
| 1549 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1550 | return 1; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1551 | } |
| 1552 | |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1553 | static struct nc_session * |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1554 | _nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx, struct nc_keepalives *ka, |
| 1555 | struct nc_client_ssh_opts *opts, int timeout) |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1556 | { |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1557 | char *host = NULL, *username = NULL, *ip_host; |
Dragos Dan | 8ce3b7c | 2021-03-09 09:17:22 +0200 | [diff] [blame] | 1558 | unsigned int port = 0; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1559 | int sock; |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1560 | struct passwd *pw, pw_buf; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1561 | struct nc_session *session = NULL; |
Michal Vasko | ccd2dd0 | 2021-10-11 09:13:01 +0200 | [diff] [blame] | 1562 | char *buf = NULL; |
| 1563 | size_t buf_len = 0; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1564 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1565 | NC_CHECK_ARG_RET(NULL, ssh_session, NULL); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1566 | |
| 1567 | /* prepare session structure */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1568 | session = nc_new_session(NC_CLIENT, 0); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1569 | if (!session) { |
| 1570 | ERRMEM; |
| 1571 | return NULL; |
| 1572 | } |
| 1573 | session->status = NC_STATUS_STARTING; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1574 | session->ti_type = NC_TI_LIBSSH; |
| 1575 | session->ti.libssh.session = ssh_session; |
| 1576 | |
| 1577 | /* was port set? */ |
Michal Vasko | 097fc85 | 2021-03-09 08:18:17 +0100 | [diff] [blame] | 1578 | ssh_options_get_port(ssh_session, &port); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1579 | |
| 1580 | if (ssh_options_get(ssh_session, SSH_OPTIONS_HOST, &host) != SSH_OK) { |
| 1581 | /* |
| 1582 | * There is no file descriptor (detected based on the host, there is no way to check |
| 1583 | * the SSH_OPTIONS_FD directly :/), we need to create it. (TCP/IP layer) |
| 1584 | */ |
| 1585 | |
| 1586 | /* remember host */ |
| 1587 | host = strdup("localhost"); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1588 | if (!host) { |
| 1589 | ERRMEM; |
| 1590 | goto fail; |
| 1591 | } |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1592 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOST, host); |
| 1593 | |
| 1594 | /* create and connect socket */ |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1595 | sock = nc_sock_connect(host, port, -1, ka, NULL, &ip_host); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1596 | if (sock == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1597 | ERR(NULL, "Unable to connect to %s:%u (%s).", host, port, strerror(errno)); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1598 | goto fail; |
| 1599 | } |
| 1600 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_FD, &sock); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1601 | ssh_set_blocking(session->ti.libssh.session, 0); |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1602 | |
| 1603 | free(host); |
| 1604 | host = ip_host; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | /* was username set? */ |
| 1608 | ssh_options_get(ssh_session, SSH_OPTIONS_USER, &username); |
| 1609 | |
| 1610 | if (!ssh_is_connected(ssh_session)) { |
| 1611 | /* |
| 1612 | * We are connected, but not SSH authenticated. (Transport layer) |
| 1613 | */ |
| 1614 | |
| 1615 | /* remember username */ |
| 1616 | if (!username) { |
| 1617 | if (!opts->username) { |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 1618 | pw = nc_getpw(getuid(), NULL, &pw_buf, &buf, &buf_len); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1619 | if (!pw) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1620 | ERR(NULL, "Unknown username for the SSH connection (%s).", strerror(errno)); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1621 | goto fail; |
| 1622 | } |
| 1623 | username = strdup(pw->pw_name); |
Michal Vasko | ccd2dd0 | 2021-10-11 09:13:01 +0200 | [diff] [blame] | 1624 | free(buf); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1625 | } else { |
| 1626 | username = strdup(opts->username); |
| 1627 | } |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1628 | if (!username) { |
| 1629 | ERRMEM; |
| 1630 | goto fail; |
| 1631 | } |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1632 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_USER, username); |
| 1633 | } |
| 1634 | |
| 1635 | /* connect and authenticate SSH session */ |
| 1636 | session->host = host; |
| 1637 | session->username = username; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1638 | if (connect_ssh_session(session, opts, timeout) != 1) { |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1639 | goto fail; |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | /* |
| 1644 | * Almost done, open a netconf channel. (Transport layer / application layer) |
| 1645 | */ |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1646 | if (open_netconf_channel(session, timeout) != 1) { |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1647 | goto fail; |
| 1648 | } |
| 1649 | |
| 1650 | /* |
| 1651 | * SSH session is established and netconf channel opened, create a NETCONF session. (Application layer) |
| 1652 | */ |
| 1653 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1654 | if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1655 | goto fail; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1656 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1657 | ctx = session->ctx; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1658 | |
| 1659 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1660 | if (nc_handshake_io(session) != NC_MSG_HELLO) { |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1661 | goto fail; |
| 1662 | } |
| 1663 | session->status = NC_STATUS_RUNNING; |
| 1664 | |
| 1665 | if (nc_ctx_check_and_fill(session) == -1) { |
| 1666 | goto fail; |
| 1667 | } |
| 1668 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1669 | /* store information if not previously */ |
| 1670 | session->host = host; |
| 1671 | session->port = port; |
| 1672 | session->username = username; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1673 | |
| 1674 | return session; |
| 1675 | |
| 1676 | fail: |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1677 | nc_session_free(session, NULL); |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1678 | return NULL; |
| 1679 | } |
| 1680 | |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1681 | API struct nc_session * |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1682 | nc_connect_ssh(const char *host, uint16_t port, struct ly_ctx *ctx) |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1683 | { |
Michal Vasko | 1f0563a | 2016-03-31 08:38:44 +0200 | [diff] [blame] | 1684 | const long timeout = NC_SSH_TIMEOUT; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1685 | int sock; |
Michal Vasko | 55fded6 | 2016-02-02 12:19:34 +0100 | [diff] [blame] | 1686 | uint32_t port_uint; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1687 | char *username, *ip_host = NULL; |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1688 | struct passwd *pw, pw_buf; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1689 | struct nc_session *session = NULL; |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1690 | char *buf = NULL; |
| 1691 | size_t buf_len = 0; |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 1692 | char *known_hosts_path = NULL; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1693 | |
| 1694 | /* process parameters */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame^] | 1695 | if (!host || (host[0] == '\0')) { |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1696 | host = "localhost"; |
| 1697 | } |
| 1698 | |
| 1699 | if (!port) { |
| 1700 | port = NC_PORT_SSH; |
| 1701 | } |
Michal Vasko | 55fded6 | 2016-02-02 12:19:34 +0100 | [diff] [blame] | 1702 | port_uint = port; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1703 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1704 | if (!ssh_opts.username) { |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 1705 | pw = nc_getpw(getuid(), NULL, &pw_buf, &buf, &buf_len); |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1706 | if (!pw) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1707 | ERR(session, "Unknown username for the SSH connection (%s).", strerror(errno)); |
Michal Vasko | 1d430d9 | 2021-10-11 09:30:43 +0200 | [diff] [blame] | 1708 | goto fail; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1709 | } else { |
| 1710 | username = pw->pw_name; |
| 1711 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1712 | } else { |
| 1713 | username = ssh_opts.username; |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 1714 | |
| 1715 | pw = nc_getpw(0, username, &pw_buf, &buf, &buf_len); |
| 1716 | } |
| 1717 | |
| 1718 | if (ssh_opts.knownhosts_path) { |
| 1719 | /* known_hosts file path was set so use it */ |
| 1720 | known_hosts_path = strdup(ssh_opts.knownhosts_path); |
| 1721 | if (!known_hosts_path) { |
| 1722 | ERRMEM; |
| 1723 | goto fail; |
| 1724 | } |
| 1725 | } else if (pw) { |
| 1726 | /* path not set explicitly, but current user's username found in /etc/passwd, so create the path */ |
| 1727 | if (asprintf(&known_hosts_path, "%s/.ssh/known_hosts", pw->pw_dir) == -1) { |
| 1728 | ERRMEM; |
| 1729 | goto fail; |
| 1730 | } |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | /* prepare session structure */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1734 | session = nc_new_session(NC_CLIENT, 0); |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1735 | if (!session) { |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1736 | ERRMEM; |
Michal Vasko | 1d430d9 | 2021-10-11 09:30:43 +0200 | [diff] [blame] | 1737 | goto fail; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1738 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1739 | session->status = NC_STATUS_STARTING; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1740 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1741 | /* transport-specific data */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1742 | session->ti_type = NC_TI_LIBSSH; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1743 | session->ti.libssh.session = ssh_new(); |
| 1744 | if (!session->ti.libssh.session) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1745 | ERR(session, "Unable to initialize SSH session."); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1746 | goto fail; |
| 1747 | } |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1748 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1749 | /* set some basic SSH session options */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1750 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOST, host); |
Michal Vasko | 55fded6 | 2016-02-02 12:19:34 +0100 | [diff] [blame] | 1751 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_PORT, &port_uint); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1752 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_USER, username); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1753 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_TIMEOUT, &timeout); |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 1754 | if (known_hosts_path) { |
| 1755 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_path); |
| 1756 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1757 | |
| 1758 | /* create and assign communication socket */ |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1759 | sock = nc_sock_connect(host, port, -1, &client_opts.ka, NULL, &ip_host); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1760 | if (sock == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1761 | ERR(session, "Unable to connect to %s:%u (%s).", host, port, strerror(errno)); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1762 | goto fail; |
| 1763 | } |
| 1764 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_FD, &sock); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1765 | ssh_set_blocking(session->ti.libssh.session, 0); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1766 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1767 | /* store information for session connection */ |
| 1768 | session->host = strdup(host); |
| 1769 | session->username = strdup(username); |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 1770 | session->port = port; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1771 | if ((connect_ssh_session(session, &ssh_opts, NC_TRANSPORT_TIMEOUT) != 1) || |
| 1772 | (open_netconf_channel(session, NC_TRANSPORT_TIMEOUT) != 1)) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1773 | goto fail; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1774 | } |
| 1775 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1776 | if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1777 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1778 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1779 | ctx = session->ctx; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1780 | |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1781 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1782 | if (nc_handshake_io(session) != NC_MSG_HELLO) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1783 | goto fail; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1784 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 1785 | session->status = NC_STATUS_RUNNING; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1786 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 1787 | if (nc_ctx_check_and_fill(session) == -1) { |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 1788 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1789 | } |
| 1790 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1791 | /* update information */ |
| 1792 | free(session->host); |
| 1793 | session->host = ip_host; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1794 | session->port = port; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1795 | |
Michal Vasko | ccd2dd0 | 2021-10-11 09:13:01 +0200 | [diff] [blame] | 1796 | free(buf); |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 1797 | free(known_hosts_path); |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1798 | return session; |
| 1799 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1800 | fail: |
Michal Vasko | ccd2dd0 | 2021-10-11 09:13:01 +0200 | [diff] [blame] | 1801 | free(buf); |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 1802 | free(known_hosts_path); |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1803 | free(ip_host); |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1804 | nc_session_free(session, NULL); |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1805 | return NULL; |
| 1806 | } |
| 1807 | |
| 1808 | API struct nc_session * |
| 1809 | nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx) |
| 1810 | { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1811 | return _nc_connect_libssh(ssh_session, ctx, &client_opts.ka, &ssh_opts, NC_TRANSPORT_TIMEOUT); |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | API struct nc_session * |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1815 | nc_connect_ssh_channel(struct nc_session *session, struct ly_ctx *ctx) |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1816 | { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1817 | struct nc_session *new_session, *ptr; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1818 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1819 | NC_CHECK_ARG_RET(session, session, NULL); |
Michal Vasko | b7c4ff3 | 2016-01-21 15:35:54 +0100 | [diff] [blame] | 1820 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1821 | /* prepare session structure */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1822 | new_session = nc_new_session(NC_CLIENT, 1); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1823 | if (!new_session) { |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1824 | ERRMEM; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1825 | return NULL; |
| 1826 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1827 | new_session->status = NC_STATUS_STARTING; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1828 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1829 | /* share some parameters including the IO lock (we are using one socket for both sessions) */ |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1830 | new_session->ti_type = NC_TI_LIBSSH; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1831 | new_session->ti.libssh.session = session->ti.libssh.session; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1832 | new_session->io_lock = session->io_lock; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1833 | |
Michal Vasko | 1e7f9e7 | 2019-01-28 08:55:47 +0100 | [diff] [blame] | 1834 | /* append to the session ring list */ |
| 1835 | if (!session->ti.libssh.next) { |
| 1836 | session->ti.libssh.next = new_session; |
| 1837 | new_session->ti.libssh.next = session; |
| 1838 | } else { |
| 1839 | ptr = session->ti.libssh.next; |
| 1840 | session->ti.libssh.next = new_session; |
| 1841 | new_session->ti.libssh.next = ptr; |
| 1842 | } |
| 1843 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1844 | /* create the channel safely */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1845 | if (nc_session_io_lock(new_session, -1, __func__) != 1) { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1846 | goto fail; |
| 1847 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1848 | if (open_netconf_channel(new_session, NC_TRANSPORT_TIMEOUT) != 1) { |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1849 | goto fail; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1850 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1851 | nc_session_io_unlock(new_session, __func__); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1852 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1853 | if (nc_client_session_new_ctx(new_session, ctx) != EXIT_SUCCESS) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1854 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1855 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 1856 | ctx = session->ctx; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1857 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1858 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1859 | if (nc_handshake_io(new_session) != NC_MSG_HELLO) { |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1860 | goto fail; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1861 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 1862 | new_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1863 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 1864 | if (nc_ctx_check_and_fill(new_session) == -1) { |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 1865 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1866 | } |
| 1867 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1868 | /* store information into session */ |
| 1869 | new_session->host = strdup(session->host); |
Michal Vasko | 56b5bf7 | 2016-01-19 11:20:35 +0100 | [diff] [blame] | 1870 | new_session->port = session->port; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1871 | new_session->username = strdup(session->username); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1872 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1873 | return new_session; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1874 | |
| 1875 | fail: |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1876 | nc_session_free(new_session, NULL); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1877 | return NULL; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1878 | } |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1879 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1880 | struct nc_session * |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1881 | nc_accept_callhome_ssh_sock(int sock, const char *host, uint16_t port, struct ly_ctx *ctx, int timeout) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1882 | { |
Michal Vasko | 1f0563a | 2016-03-31 08:38:44 +0200 | [diff] [blame] | 1883 | const long ssh_timeout = NC_SSH_TIMEOUT; |
Michal Vasko | 0cfa90c | 2016-10-13 10:34:46 +0200 | [diff] [blame] | 1884 | unsigned int uint_port; |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1885 | struct passwd *pw, pw_buf; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1886 | struct nc_session *session; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1887 | ssh_session sess; |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1888 | char *buf = NULL; |
| 1889 | size_t buf_len = 0; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1890 | |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1891 | sess = ssh_new(); |
| 1892 | if (!sess) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1893 | ERR(NULL, "Unable to initialize an SSH session."); |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1894 | close(sock); |
| 1895 | return NULL; |
| 1896 | } |
| 1897 | |
| 1898 | ssh_options_set(sess, SSH_OPTIONS_FD, &sock); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1899 | ssh_set_blocking(sess, 0); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1900 | ssh_options_set(sess, SSH_OPTIONS_HOST, host); |
Michal Vasko | 0cfa90c | 2016-10-13 10:34:46 +0200 | [diff] [blame] | 1901 | uint_port = port; |
| 1902 | ssh_options_set(sess, SSH_OPTIONS_PORT, &uint_port); |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1903 | ssh_options_set(sess, SSH_OPTIONS_TIMEOUT, &ssh_timeout); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1904 | if (!ssh_ch_opts.username) { |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 1905 | pw = nc_getpw(getuid(), NULL, &pw_buf, &buf, &buf_len); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1906 | if (!pw) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1907 | ERR(NULL, "Unknown username for the SSH connection (%s).", strerror(errno)); |
Michal Vasko | 435e5cf | 2019-04-23 08:48:44 +0200 | [diff] [blame] | 1908 | ssh_free(sess); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1909 | return NULL; |
| 1910 | } |
| 1911 | ssh_options_set(sess, SSH_OPTIONS_USER, pw->pw_name); |
Michal Vasko | ccd2dd0 | 2021-10-11 09:13:01 +0200 | [diff] [blame] | 1912 | free(buf); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1913 | } else { |
| 1914 | ssh_options_set(sess, SSH_OPTIONS_USER, ssh_ch_opts.username); |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1915 | } |
Michal Vasko | 8fd6fca | 2019-02-04 10:59:49 +0100 | [diff] [blame] | 1916 | ssh_options_set(sess, SSH_OPTIONS_HOSTKEYS, "ssh-ed25519,ecdsa-sha2-nistp256," |
| 1917 | "ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-rsa,rsa-sha2-512,rsa-sha2-256,ssh-dss"); |
| 1918 | #ifdef HAVE_LIBSSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES |
| 1919 | ssh_options_set(sess, SSH_OPTIONS_PUBLICKEY_ACCEPTED_TYPES, "ssh-ed25519,ecdsa-sha2-nistp256," |
| 1920 | "ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-rsa,rsa-sha2-512,rsa-sha2-256,ssh-dss"); |
| 1921 | #endif |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1922 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1923 | session = _nc_connect_libssh(sess, ctx, &client_opts.ka, &ssh_ch_opts, timeout); |
Michal Vasko | 435e5cf | 2019-04-23 08:48:44 +0200 | [diff] [blame] | 1924 | if (!session) { |
Michal Vasko | 457f053 | 2019-08-15 13:59:49 +0200 | [diff] [blame] | 1925 | /* sess is freed */ |
Michal Vasko | 435e5cf | 2019-04-23 08:48:44 +0200 | [diff] [blame] | 1926 | return NULL; |
Michal Vasko | 4282fae | 2016-02-18 10:03:42 +0100 | [diff] [blame] | 1927 | } |
| 1928 | |
Michal Vasko | 435e5cf | 2019-04-23 08:48:44 +0200 | [diff] [blame] | 1929 | session->flags |= NC_SESSION_CALLHOME; |
Michal Vasko | 30e2c87 | 2016-02-18 10:03:21 +0100 | [diff] [blame] | 1930 | return session; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1931 | } |