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