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