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