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