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