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