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