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