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