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