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 | |
| 49 | #include "libnetconf.h" |
| 50 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 51 | static struct nc_ssh_client_opts ssh_opts = { |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 52 | .auth_pref = {{NC_SSH_AUTH_INTERACTIVE, 3}, {NC_SSH_AUTH_PASSWORD, 2}, {NC_SSH_AUTH_PUBLICKEY, 1}} |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 53 | }; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 54 | |
Michal Vasko | 990089e | 2015-10-27 15:05:25 +0100 | [diff] [blame] | 55 | API void |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 56 | nc_ssh_client_destroy(void) |
Michal Vasko | 990089e | 2015-10-27 15:05:25 +0100 | [diff] [blame] | 57 | { |
| 58 | int i; |
| 59 | |
| 60 | for (i = 0; i < ssh_opts.key_count; ++i) { |
| 61 | free(ssh_opts.keys[i].pubkey_path); |
| 62 | free(ssh_opts.keys[i].privkey_path); |
| 63 | } |
| 64 | |
| 65 | free(ssh_opts.keys); |
| 66 | ssh_opts.keys = NULL; |
| 67 | ssh_opts.key_count = 0; |
| 68 | } |
| 69 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 70 | static char * |
| 71 | sshauth_password(const char *username, const char *hostname) |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 72 | { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 73 | char *buf, *newbuf; |
| 74 | int buflen = 1024, len = 0; |
| 75 | char c = 0; |
| 76 | struct termios newterm, oldterm; |
| 77 | FILE *tty; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 78 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 79 | buf = malloc(buflen * sizeof *buf); |
| 80 | if (!buf) { |
| 81 | ERRMEM; |
| 82 | return NULL; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 85 | if (!(tty = fopen("/dev/tty", "r+"))) { |
| 86 | ERR("Unable to open the current terminal (%s:%d - %s).", __FILE__, __LINE__, strerror(errno)); |
| 87 | return NULL; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 90 | if (tcgetattr(fileno(tty), &oldterm)) { |
| 91 | ERR("Unable to get terminal settings (%d: %s).", __LINE__, strerror(errno)); |
| 92 | return NULL; |
| 93 | } |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 94 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 95 | fprintf(tty, "%s@%s password: ", username, hostname); |
| 96 | fflush(tty); |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 97 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 98 | /* system("stty -echo"); */ |
| 99 | newterm = oldterm; |
| 100 | newterm.c_lflag &= ~ECHO; |
| 101 | newterm.c_lflag &= ~ICANON; |
| 102 | tcflush(fileno(tty), TCIFLUSH); |
| 103 | if (tcsetattr(fileno(tty), TCSANOW, &newterm)) { |
| 104 | ERR("Unable to change terminal settings for hiding password (%d: %s).", __LINE__, strerror(errno)); |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | while ((fread(&c, 1, 1, tty) == 1) && (c != '\n')) { |
| 109 | if (len >= buflen - 1) { |
| 110 | buflen *= 2; |
| 111 | newbuf = realloc(buf, buflen * sizeof *newbuf); |
| 112 | if (!newbuf) { |
| 113 | ERR("Memory allocation failed (%s:%d - %s).", __FILE__, __LINE__, strerror(errno)); |
| 114 | |
| 115 | /* remove content of the buffer */ |
| 116 | memset(buf, 0, len); |
| 117 | free(buf); |
| 118 | |
| 119 | /* restore terminal settings */ |
| 120 | if (tcsetattr(fileno(tty), TCSANOW, &oldterm) != 0) { |
| 121 | ERR("Unable to restore terminal settings (%d: %s).", __LINE__, strerror(errno)); |
| 122 | } |
| 123 | return NULL; |
| 124 | } else { |
| 125 | buf = newbuf; |
| 126 | } |
| 127 | } |
| 128 | buf[len++] = c; |
| 129 | } |
| 130 | buf[len++] = 0; /* terminating null byte */ |
| 131 | |
| 132 | /* system ("stty echo"); */ |
| 133 | if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) { |
| 134 | ERR("Unable to restore terminal settings (%d: %s).", __LINE__, strerror(errno)); |
| 135 | /* |
| 136 | * terminal probably still hides input characters, but we have password |
| 137 | * and anyway we are unable to set terminal to the previous state, so |
| 138 | * just continue |
| 139 | */ |
| 140 | } |
| 141 | fprintf(tty, "\n"); |
| 142 | |
| 143 | fclose(tty); |
| 144 | return buf; |
| 145 | } |
| 146 | |
| 147 | static char * |
| 148 | sshauth_interactive(const char *auth_name, const char *instruction, const char *prompt, int echo) |
| 149 | { |
| 150 | unsigned int buflen = 8, response_len; |
| 151 | char c = 0; |
| 152 | struct termios newterm, oldterm; |
| 153 | char *newtext, *response; |
| 154 | FILE *tty; |
| 155 | |
| 156 | if (!(tty = fopen("/dev/tty", "r+"))) { |
| 157 | ERR("Unable to open the current terminal (%s:%d - %s).", __FILE__, __LINE__, strerror(errno)); |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | if (tcgetattr(fileno(tty), &oldterm) != 0) { |
| 162 | ERR("Unable to get terminal settings (%d: %s).", __LINE__, strerror(errno)); |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | if (auth_name && (!fwrite(auth_name, sizeof(char), strlen(auth_name), tty) |
| 167 | || !fwrite("\n", sizeof(char), 1, tty))) { |
| 168 | ERR("Writing the auth method name into stdout failed."); |
| 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | if (instruction && (!fwrite(instruction, sizeof(char), strlen(instruction), tty) |
| 173 | || !fwrite("\n", sizeof(char), 1, tty))) { |
| 174 | ERR("Writing the instruction into stdout failed."); |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | if (!fwrite(prompt, sizeof(char), strlen(prompt), tty)) { |
| 179 | ERR("Writing the authentication prompt into stdout failed."); |
| 180 | return NULL; |
| 181 | } |
| 182 | fflush(tty); |
| 183 | if (!echo) { |
| 184 | /* system("stty -echo"); */ |
| 185 | newterm = oldterm; |
| 186 | newterm.c_lflag &= ~ECHO; |
| 187 | tcflush(fileno(tty), TCIFLUSH); |
| 188 | if (tcsetattr(fileno(tty), TCSANOW, &newterm)) { |
| 189 | ERR("Unable to change terminal settings for hiding password (%d: %s).", __LINE__, strerror(errno)); |
| 190 | return NULL; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | response = malloc(buflen * sizeof *response); |
| 195 | response_len = 0; |
| 196 | if (!response) { |
| 197 | ERRMEM; |
| 198 | /* restore terminal settings */ |
| 199 | if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) { |
| 200 | ERR("Unable to restore terminal settings (%d: %s).", __LINE__, strerror(errno)); |
| 201 | } |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | while ((fread(&c, 1, 1, tty) == 1) && (c != '\n')) { |
| 206 | if (response_len >= buflen - 1) { |
| 207 | buflen *= 2; |
| 208 | newtext = realloc(response, buflen * sizeof *newtext); |
| 209 | if (!newtext) { |
| 210 | ERR("Memory allocation failed (%s:%d - %s).", __FILE__, __LINE__, strerror(errno)); |
| 211 | free(response); |
| 212 | |
| 213 | /* restore terminal settings */ |
| 214 | if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) { |
| 215 | ERR("Unable to restore terminal settings (%d: %s).", __LINE__, strerror(errno)); |
| 216 | } |
| 217 | return NULL; |
| 218 | } else { |
| 219 | response = newtext; |
| 220 | } |
| 221 | } |
| 222 | response[response_len++] = c; |
| 223 | } |
| 224 | /* terminating null byte */ |
| 225 | response[response_len++] = '\0'; |
| 226 | |
| 227 | /* system ("stty echo"); */ |
| 228 | if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) { |
| 229 | ERR("Unable to restore terminal settings (%d: %s).", __LINE__, strerror(errno)); |
| 230 | /* |
| 231 | * terminal probably still hides input characters, but we have password |
| 232 | * and anyway we are unable to set terminal to the previous state, so |
| 233 | * just continue |
| 234 | */ |
| 235 | } |
| 236 | |
| 237 | fprintf(tty, "\n"); |
| 238 | fclose(tty); |
| 239 | return response; |
| 240 | } |
| 241 | |
| 242 | static char * |
| 243 | sshauth_passphrase(const char* privkey_path) |
| 244 | { |
| 245 | char c, *buf, *newbuf; |
| 246 | int buflen = 1024, len = 0; |
| 247 | struct termios newterm, oldterm; |
| 248 | FILE *tty; |
| 249 | |
| 250 | buf = malloc(buflen * sizeof *buf); |
| 251 | if (!buf) { |
| 252 | ERR("Memory allocation failed (%s:%d - %s).", __FILE__, __LINE__, strerror(errno)); |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | if (!(tty = fopen("/dev/tty", "r+"))) { |
| 257 | ERR("Unable to open the current terminal (%s:%d - %s).", __FILE__, __LINE__, strerror(errno)); |
| 258 | return NULL; |
| 259 | } |
| 260 | |
| 261 | if (tcgetattr(fileno(tty), &oldterm)) { |
| 262 | ERR("Unable to get terminal settings (%d: %s).", __LINE__, strerror(errno)); |
| 263 | return NULL; |
| 264 | } |
| 265 | |
| 266 | fprintf(tty, "Enter passphrase for the key '%s':", privkey_path); |
| 267 | fflush(tty); |
| 268 | |
| 269 | /* system("stty -echo"); */ |
| 270 | newterm = oldterm; |
| 271 | newterm.c_lflag &= ~ECHO; |
| 272 | newterm.c_lflag &= ~ICANON; |
| 273 | tcflush(fileno(tty), TCIFLUSH); |
| 274 | if (tcsetattr(fileno(tty), TCSANOW, &newterm)) { |
| 275 | ERR("Unable to change terminal settings for hiding password (%d: %s).", __LINE__, strerror(errno)); |
| 276 | return NULL; |
| 277 | } |
| 278 | |
| 279 | while ((fread(&c, 1, 1, tty) == 1) && (c != '\n')) { |
| 280 | if (len >= buflen - 1) { |
| 281 | buflen *= 2; |
| 282 | newbuf = realloc(buf, buflen * sizeof *newbuf); |
| 283 | if (!newbuf) { |
| 284 | ERRMEM; |
| 285 | /* remove content of the buffer */ |
| 286 | memset(buf, 0, len); |
| 287 | free(buf); |
| 288 | |
| 289 | /* restore terminal settings */ |
| 290 | if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) { |
| 291 | ERR("Unable to restore terminal settings (%d: %s).", __LINE__, strerror(errno)); |
| 292 | } |
| 293 | |
| 294 | return NULL; |
| 295 | } |
| 296 | buf = newbuf; |
| 297 | } |
| 298 | buf[len++] = (char)c; |
| 299 | } |
| 300 | buf[len++] = 0; /* terminating null byte */ |
| 301 | |
| 302 | /* system ("stty echo"); */ |
| 303 | if (tcsetattr(fileno(tty), TCSANOW, &oldterm)) { |
| 304 | ERR("Unable to restore terminal settings (%d: %s).", __LINE__, strerror(errno)); |
| 305 | /* |
| 306 | * terminal probably still hides input characters, but we have password |
| 307 | * and anyway we are unable to set terminal to the previous state, so |
| 308 | * just continue |
| 309 | */ |
| 310 | } |
| 311 | fprintf(tty, "\n"); |
| 312 | |
| 313 | fclose(tty); |
| 314 | return buf; |
| 315 | } |
| 316 | |
| 317 | /* TODO define this switch */ |
| 318 | #ifdef ENABLE_DNSSEC |
| 319 | |
| 320 | /* return 0 (DNSSEC + key valid), 1 (unsecure DNS + key valid), 2 (key not found or an error) */ |
| 321 | /* type - 1 (RSA), 2 (DSA), 3 (ECDSA); alg - 1 (SHA1), 2 (SHA-256) */ |
| 322 | static int |
| 323 | sshauth_hostkey_hash_dnssec_check(const char *hostname, const char *sha1hash, int type, int alg) { |
| 324 | ns_msg handle; |
| 325 | ns_rr rr; |
| 326 | val_status_t val_status; |
| 327 | const unsigned char* rdata; |
| 328 | unsigned char buf[4096]; |
| 329 | int buf_len = 4096; |
| 330 | int ret = 0, i, j, len; |
| 331 | |
| 332 | /* class 1 - internet, type 44 - SSHFP */ |
| 333 | len = val_res_query(NULL, hostname, 1, 44, buf, buf_len, &val_status); |
| 334 | |
| 335 | if ((len < 0) || !val_istrusted(val_status)) { |
| 336 | ret = 2; |
| 337 | goto finish; |
| 338 | } |
| 339 | |
| 340 | if (ns_initparse(buf, len, &handle) < 0) { |
| 341 | ERR("Failed to initialize DNSSEC response parser."); |
| 342 | ret = 2; |
| 343 | goto finish; |
| 344 | } |
| 345 | |
| 346 | if ((i = libsres_msg_getflag(handle, ns_f_rcode))) { |
| 347 | ERR("DNSSEC query returned %d.", i); |
| 348 | ret = 2; |
| 349 | goto finish; |
| 350 | } |
| 351 | |
| 352 | if (!libsres_msg_getflag(handle, ns_f_ad)) { |
| 353 | /* response not secured by DNSSEC */ |
| 354 | ret = 1; |
| 355 | } |
| 356 | |
| 357 | /* query section */ |
| 358 | if (ns_parserr(&handle, ns_s_qd, 0, &rr)) { |
| 359 | ERROR("DNSSEC query section parser fail."); |
| 360 | ret = 2; |
| 361 | goto finish; |
| 362 | } |
| 363 | |
| 364 | if (strcmp(hostname, ns_rr_name(rr)) || (ns_rr_type(rr) != 44) || (ns_rr_class(rr) != 1)) { |
| 365 | ERROR("DNSSEC query in the answer does not match the original query."); |
| 366 | ret = 2; |
| 367 | goto finish; |
| 368 | } |
| 369 | |
| 370 | /* answer section */ |
| 371 | i = 0; |
| 372 | while (!ns_parserr(&handle, ns_s_an, i, &rr)) { |
| 373 | if (ns_rr_type(rr) != 44) { |
| 374 | ++i; |
| 375 | continue; |
| 376 | } |
| 377 | |
| 378 | rdata = ns_rr_rdata(rr); |
| 379 | if (rdata[0] != type) { |
| 380 | ++i; |
| 381 | continue; |
| 382 | } |
| 383 | if (rdata[1] != alg) { |
| 384 | ++i; |
| 385 | continue; |
| 386 | } |
| 387 | |
| 388 | /* we found the correct SSHFP entry */ |
| 389 | rdata += 2; |
| 390 | for (j = 0; j < 20; ++j) { |
| 391 | if (rdata[j] != (unsigned char)sha1hash[j]) { |
| 392 | ret = 2; |
| 393 | goto finish; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /* server fingerprint is supported by a DNS entry, |
| 398 | * we have already determined if DNSSEC was used or not |
| 399 | */ |
| 400 | goto finish; |
| 401 | } |
| 402 | |
| 403 | /* no match */ |
| 404 | ret = 2; |
| 405 | |
| 406 | finish: |
| 407 | val_free_validator_state(); |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | #endif |
| 412 | |
| 413 | static int |
| 414 | sshauth_hostkey_check(const char *hostname, ssh_session session) |
| 415 | { |
| 416 | char *hexa; |
| 417 | int c, state, ret; |
| 418 | ssh_key srv_pubkey; |
| 419 | unsigned char *hash_sha1 = NULL; |
| 420 | size_t hlen; |
| 421 | enum ssh_keytypes_e srv_pubkey_type; |
| 422 | char answer[5]; |
| 423 | |
| 424 | state = ssh_is_server_known(session); |
| 425 | |
| 426 | ret = ssh_get_publickey(session, &srv_pubkey); |
| 427 | if (ret < 0) { |
| 428 | ERR("Unable to get server public key."); |
| 429 | return EXIT_FAILURE; |
| 430 | } |
| 431 | |
| 432 | srv_pubkey_type = ssh_key_type(srv_pubkey); |
| 433 | ret = ssh_get_publickey_hash(srv_pubkey, SSH_PUBLICKEY_HASH_SHA1, &hash_sha1, &hlen); |
| 434 | ssh_key_free(srv_pubkey); |
| 435 | if (ret < 0) { |
| 436 | ERR("Failed to calculate SHA1 hash of the server public key."); |
| 437 | return EXIT_FAILURE; |
| 438 | } |
| 439 | |
| 440 | hexa = ssh_get_hexa(hash_sha1, hlen); |
| 441 | |
| 442 | switch (state) { |
| 443 | case SSH_SERVER_KNOWN_OK: |
| 444 | break; /* ok */ |
| 445 | |
| 446 | case SSH_SERVER_KNOWN_CHANGED: |
| 447 | ERR("Remote host key changed, the connection will be terminated!"); |
| 448 | goto fail; |
| 449 | |
| 450 | case SSH_SERVER_FOUND_OTHER: |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 451 | WRN("Remote host key is not known, but a key of another type for this host is known. Continue with caution."); |
| 452 | goto hostkey_not_known; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 453 | |
| 454 | case SSH_SERVER_FILE_NOT_FOUND: |
| 455 | WRN("Could not find the known hosts file."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 456 | goto hostkey_not_known; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 457 | |
| 458 | case SSH_SERVER_NOT_KNOWN: |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 459 | hostkey_not_known: |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 460 | #ifdef ENABLE_DNSSEC |
| 461 | if ((srv_pubkey_type != SSH_KEYTYPE_UNKNOWN) || (srv_pubkey_type != SSH_KEYTYPE_RSA1)) { |
| 462 | if (srv_pubkey_type == SSH_KEYTYPE_DSS) { |
| 463 | ret = callback_ssh_hostkey_hash_dnssec_check(hostname, hash_sha1, 2, 1); |
| 464 | } else if (srv_pubkey_type == SSH_KEYTYPE_RSA) { |
| 465 | ret = callback_ssh_hostkey_hash_dnssec_check(hostname, hash_sha1, 1, 1); |
| 466 | } else if (srv_pubkey_type == SSH_KEYTYPE_ECDSA) { |
| 467 | ret = callback_ssh_hostkey_hash_dnssec_check(hostname, hash_sha1, 3, 1); |
| 468 | } |
| 469 | |
| 470 | /* DNSSEC SSHFP check successful, that's enough */ |
| 471 | if (!ret) { |
| 472 | DBG("DNSSEC SSHFP check successful"); |
| 473 | ssh_write_knownhost(session); |
| 474 | ssh_clean_pubkey_hash(&hash_sha1); |
| 475 | ssh_string_free_char(hexa); |
| 476 | return EXIT_SUCCESS; |
| 477 | } |
| 478 | } |
| 479 | #endif |
| 480 | |
| 481 | /* try to get result from user */ |
| 482 | fprintf(stdout, "The authenticity of the host \'%s\' cannot be established.\n", hostname); |
| 483 | fprintf(stdout, "%s key fingerprint is %s.\n", ssh_key_type_to_char(srv_pubkey_type), hexa); |
| 484 | |
| 485 | #ifdef ENABLE_DNSSEC |
| 486 | if (ret == 2) { |
| 487 | fprintf(stdout, "No matching host key fingerprint found in DNS.\n"); |
| 488 | } else if (ret == 1) { |
| 489 | fprintf(stdout, "Matching host key fingerprint found in DNS.\n"); |
| 490 | } |
| 491 | #endif |
| 492 | |
| 493 | fprintf(stdout, "Are you sure you want to continue connecting (yes/no)? "); |
| 494 | |
| 495 | do { |
| 496 | if (fscanf(stdin, "%4s", answer) == EOF) { |
| 497 | ERR("fscanf() failed (%s).", strerror(errno)); |
| 498 | goto fail; |
| 499 | } |
| 500 | while (((c = getchar()) != EOF) && (c != '\n')); |
| 501 | |
| 502 | fflush(stdin); |
| 503 | if (!strcmp("yes", answer)) { |
| 504 | /* store the key into the host file */ |
| 505 | ret = ssh_write_knownhost(session); |
| 506 | if (ret < 0) { |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 507 | WRN("Adding the known host %s failed (%s).", hostname, ssh_get_error(session)); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 508 | } |
| 509 | } else if (!strcmp("no", answer)) { |
| 510 | goto fail; |
| 511 | } else { |
| 512 | fprintf(stdout, "Please type 'yes' or 'no': "); |
| 513 | } |
| 514 | } while (strcmp(answer, "yes") && strcmp(answer, "no")); |
| 515 | |
| 516 | break; |
| 517 | |
| 518 | case SSH_SERVER_ERROR: |
| 519 | ssh_clean_pubkey_hash(&hash_sha1); |
| 520 | fprintf(stderr,"%s",ssh_get_error(session)); |
| 521 | return -1; |
| 522 | } |
| 523 | |
| 524 | ssh_clean_pubkey_hash(&hash_sha1); |
| 525 | ssh_string_free_char(hexa); |
| 526 | return EXIT_SUCCESS; |
| 527 | |
| 528 | fail: |
| 529 | ssh_clean_pubkey_hash(&hash_sha1); |
| 530 | ssh_string_free_char(hexa); |
| 531 | return EXIT_FAILURE; |
| 532 | } |
| 533 | |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 534 | API int |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 535 | nc_ssh_client_add_keypair(const char *pub_key, const char *priv_key) |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 536 | { |
| 537 | int i; |
| 538 | FILE *key; |
| 539 | char line[128]; |
| 540 | |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 541 | if (!pub_key || !priv_key) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 542 | return EXIT_FAILURE; |
| 543 | } |
| 544 | |
| 545 | for (i = 0; i < ssh_opts.key_count; ++i) { |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 546 | if (!strcmp(ssh_opts.keys[i].pubkey_path, pub_key) || !strcmp(ssh_opts.keys[i].privkey_path, priv_key)) { |
| 547 | if (strcmp(ssh_opts.keys[i].pubkey_path, pub_key)) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 548 | WRN("Private key \"%s\" found with another public key \"%s\".", |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 549 | priv_key, ssh_opts.keys[i].pubkey_path); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 550 | continue; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 551 | } else if (strcmp(ssh_opts.keys[i].privkey_path, priv_key)) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 552 | WRN("Public key \"%s\" found with another private key \"%s\".", |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 553 | pub_key, ssh_opts.keys[i].privkey_path); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 554 | continue; |
| 555 | } |
| 556 | |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 557 | ERR("SSH key pair already set."); |
| 558 | return EXIT_FAILURE; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 562 | /* add the keys safely */ |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 563 | ++ssh_opts.key_count; |
| 564 | ssh_opts.keys = realloc(ssh_opts.keys, ssh_opts.key_count * sizeof *ssh_opts.keys); |
| 565 | ssh_opts.keys[ssh_opts.key_count - 1].pubkey_path = strdup(pub_key); |
| 566 | ssh_opts.keys[ssh_opts.key_count - 1].privkey_path = strdup(priv_key); |
| 567 | ssh_opts.keys[ssh_opts.key_count - 1].privkey_crypt = 0; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 568 | |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 569 | /* check encryption */ |
| 570 | if ((key = fopen(priv_key, "r"))) { |
| 571 | /* 1st line - key type */ |
| 572 | if (!fgets(line, sizeof line, key)) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 573 | fclose(key); |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 574 | ERR("fgets() on %s failed.", priv_key); |
| 575 | return EXIT_FAILURE; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 576 | } |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 577 | /* 2nd line - encryption information or key */ |
| 578 | if (!fgets(line, sizeof line, key)) { |
| 579 | fclose(key); |
| 580 | ERR("fgets() on %s failed.", priv_key); |
| 581 | return EXIT_FAILURE; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 582 | } |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 583 | fclose(key); |
| 584 | if (strcasestr(line, "encrypted")) { |
| 585 | ssh_opts.keys[ssh_opts.key_count - 1].privkey_crypt = 1; |
| 586 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | return EXIT_SUCCESS; |
| 590 | } |
| 591 | |
| 592 | API int |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 593 | nc_ssh_client_del_keypair(int idx) |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 594 | { |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 595 | if (idx >= ssh_opts.key_count) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 596 | return EXIT_FAILURE; |
| 597 | } |
| 598 | |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 599 | free(ssh_opts.keys[idx].pubkey_path); |
| 600 | free(ssh_opts.keys[idx].privkey_path); |
| 601 | |
| 602 | --ssh_opts.key_count; |
| 603 | |
| 604 | memmove(ssh_opts.keys + idx, ssh_opts.keys + idx + 1, (ssh_opts.key_count - idx) * sizeof *ssh_opts.keys); |
| 605 | ssh_opts.keys = realloc(ssh_opts.keys, ssh_opts.key_count * sizeof *ssh_opts.keys); |
| 606 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 607 | return EXIT_SUCCESS; |
| 608 | } |
| 609 | |
| 610 | API int |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 611 | nc_ssh_client_get_keypair_count(void) |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 612 | { |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 613 | return ssh_opts.key_count; |
| 614 | } |
| 615 | |
| 616 | API int |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 617 | nc_ssh_client_get_keypair(int idx, const char **pub_key, const char **priv_key) |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 618 | { |
| 619 | if (idx >= ssh_opts.key_count) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 620 | return EXIT_FAILURE; |
| 621 | } |
| 622 | |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 623 | if (pub_key) { |
| 624 | *pub_key = ssh_opts.keys[idx].pubkey_path; |
| 625 | } |
| 626 | if (priv_key) { |
| 627 | *priv_key = ssh_opts.keys[idx].privkey_path; |
| 628 | } |
| 629 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 630 | return EXIT_SUCCESS; |
| 631 | } |
| 632 | |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 633 | API void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 634 | nc_ssh_client_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, short int pref) |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 635 | { |
| 636 | if (pref < 0) { |
| 637 | pref = -1; |
| 638 | } |
| 639 | |
| 640 | if (auth_type == NC_SSH_AUTH_INTERACTIVE) { |
| 641 | ssh_opts.auth_pref[0].value = pref; |
| 642 | } else if (auth_type == NC_SSH_AUTH_PASSWORD) { |
| 643 | ssh_opts.auth_pref[1].value = pref; |
| 644 | } else if (auth_type == NC_SSH_AUTH_PUBLICKEY) { |
| 645 | ssh_opts.auth_pref[2].value = pref; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | API short int |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 650 | nc_ssh_client_get_auth_pref(NC_SSH_AUTH_TYPE auth_type) |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 651 | { |
| 652 | short int pref = 0; |
| 653 | |
| 654 | if (auth_type == NC_SSH_AUTH_INTERACTIVE) { |
| 655 | pref = ssh_opts.auth_pref[0].value; |
| 656 | } else if (auth_type == NC_SSH_AUTH_PASSWORD) { |
| 657 | pref = ssh_opts.auth_pref[1].value; |
| 658 | } else if (auth_type == NC_SSH_AUTH_PUBLICKEY) { |
| 659 | pref = ssh_opts.auth_pref[2].value; |
| 660 | } |
| 661 | |
| 662 | return pref; |
| 663 | } |
| 664 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 665 | /* Establish a secure SSH connection, authenticate, and create a channel with the 'netconf' subsystem. |
| 666 | * Host, port, username, and a connected socket is expected to be set. |
| 667 | */ |
| 668 | static int |
| 669 | connect_ssh_session_netconf(struct nc_session *session) |
| 670 | { |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 671 | int j, ret_auth, userauthlist; |
| 672 | NC_SSH_AUTH_TYPE auth; |
| 673 | short int pref; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 674 | const char* prompt; |
| 675 | char *s, *answer, echo; |
| 676 | ssh_key pubkey, privkey; |
| 677 | ssh_session ssh_sess; |
| 678 | |
| 679 | ssh_sess = session->ti.libssh.session; |
| 680 | |
| 681 | if (ssh_connect(ssh_sess) != SSH_OK) { |
| 682 | ERR("Starting the SSH session failed (%s)", ssh_get_error(ssh_sess)); |
| 683 | DBG("Error code %d.", ssh_get_error_code(ssh_sess)); |
| 684 | return EXIT_FAILURE; |
| 685 | } |
| 686 | |
| 687 | if (sshauth_hostkey_check(session->host, ssh_sess)) { |
| 688 | ERR("Checking the host key failed."); |
| 689 | return EXIT_FAILURE; |
| 690 | } |
| 691 | |
| 692 | if ((ret_auth = ssh_userauth_none(ssh_sess, NULL)) == SSH_AUTH_ERROR) { |
| 693 | ERR("Authentication failed (%s).", ssh_get_error(ssh_sess)); |
| 694 | return EXIT_FAILURE; |
| 695 | } |
| 696 | |
| 697 | /* check what authentication methods are available */ |
| 698 | userauthlist = ssh_userauth_list(ssh_sess, NULL); |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 699 | |
| 700 | /* remove those disabled */ |
| 701 | if (ssh_opts.auth_pref[0].value < 0) { |
| 702 | VRB("Interactive SSH authentication method was disabled."); |
| 703 | userauthlist &= ~SSH_AUTH_METHOD_INTERACTIVE; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 704 | } |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 705 | if (ssh_opts.auth_pref[1].value < 0) { |
| 706 | VRB("Password SSH authentication method was disabled."); |
| 707 | userauthlist &= ~SSH_AUTH_METHOD_PASSWORD; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 708 | } |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 709 | if (ssh_opts.auth_pref[2].value < 0) { |
| 710 | VRB("Publickey SSH authentication method was disabled."); |
| 711 | userauthlist &= ~SSH_AUTH_METHOD_PUBLICKEY; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 712 | } |
| 713 | |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 714 | while (ret_auth != SSH_AUTH_SUCCESS) { |
| 715 | auth = 0; |
| 716 | pref = 0; |
| 717 | if (userauthlist & SSH_AUTH_METHOD_INTERACTIVE) { |
| 718 | auth = NC_SSH_AUTH_INTERACTIVE; |
| 719 | pref = ssh_opts.auth_pref[0].value; |
| 720 | } |
| 721 | if ((userauthlist & SSH_AUTH_METHOD_PASSWORD) && (ssh_opts.auth_pref[1].value > pref)) { |
| 722 | auth = NC_SSH_AUTH_PASSWORD; |
| 723 | pref = ssh_opts.auth_pref[1].value; |
| 724 | } |
| 725 | if ((userauthlist & SSH_AUTH_METHOD_PUBLICKEY) && (ssh_opts.auth_pref[2].value > pref)) { |
| 726 | auth = NC_SSH_AUTH_PUBLICKEY; |
| 727 | pref = ssh_opts.auth_pref[2].value; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 728 | } |
| 729 | |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 730 | if (!auth) { |
| 731 | ERR("Unable to authenticate to the remote server (No supported authentication methods left)."); |
| 732 | break; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | /* found common authentication method */ |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 736 | switch (auth) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 737 | case NC_SSH_AUTH_PASSWORD: |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 738 | userauthlist &= ~SSH_AUTH_METHOD_PASSWORD; |
| 739 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 740 | VRB("Password authentication (host %s, user %s)", session->host, session->username); |
| 741 | s = sshauth_password(session->username, session->host); |
| 742 | if ((ret_auth = ssh_userauth_password(ssh_sess, session->username, s)) != SSH_AUTH_SUCCESS) { |
| 743 | memset(s, 0, strlen(s)); |
| 744 | VRB("Authentication failed (%s)", ssh_get_error(ssh_sess)); |
| 745 | } |
| 746 | free(s); |
| 747 | break; |
| 748 | case NC_SSH_AUTH_INTERACTIVE: |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 749 | userauthlist &= ~SSH_AUTH_METHOD_INTERACTIVE; |
| 750 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 751 | VRB("Keyboard-interactive authentication"); |
| 752 | while ((ret_auth = ssh_userauth_kbdint(ssh_sess, NULL, NULL)) == SSH_AUTH_INFO) { |
| 753 | for (j = 0; j < ssh_userauth_kbdint_getnprompts(ssh_sess); ++j) { |
| 754 | prompt = ssh_userauth_kbdint_getprompt(ssh_sess, j, &echo); |
| 755 | if (prompt == NULL) { |
| 756 | break; |
| 757 | } |
| 758 | answer = sshauth_interactive(ssh_userauth_kbdint_getname(ssh_sess), |
| 759 | ssh_userauth_kbdint_getinstruction(ssh_sess), |
| 760 | prompt, echo); |
| 761 | if (ssh_userauth_kbdint_setanswer(ssh_sess, j, answer) < 0) { |
| 762 | free(answer); |
| 763 | break; |
| 764 | } |
| 765 | free(answer); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | if (ret_auth == SSH_AUTH_ERROR) { |
| 770 | VRB("Authentication failed (%s)", ssh_get_error(ssh_sess)); |
| 771 | } |
| 772 | |
| 773 | break; |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 774 | case NC_SSH_AUTH_PUBLICKEY: |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 775 | userauthlist &= ~SSH_AUTH_METHOD_PUBLICKEY; |
| 776 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 777 | VRB("Publickey athentication"); |
| 778 | |
| 779 | /* if publickeys path not provided, we cannot continue */ |
| 780 | if (!ssh_opts.key_count) { |
| 781 | VRB("No key pair specified."); |
| 782 | break; |
| 783 | } |
| 784 | |
| 785 | for (j = 0; j < ssh_opts.key_count; j++) { |
| 786 | VRB("Trying to authenticate using %spair %s %s", |
| 787 | ssh_opts.keys[j].privkey_crypt ? "password-protected " : "", ssh_opts.keys[j].privkey_path, |
| 788 | ssh_opts.keys[j].pubkey_path); |
| 789 | |
| 790 | if (ssh_pki_import_pubkey_file(ssh_opts.keys[j].pubkey_path, &pubkey) != SSH_OK) { |
| 791 | WRN("Failed to import the key \"%s\".", ssh_opts.keys[j].pubkey_path); |
| 792 | continue; |
| 793 | } |
| 794 | ret_auth = ssh_userauth_try_publickey(ssh_sess, NULL, pubkey); |
| 795 | if ((ret_auth == SSH_AUTH_DENIED) || (ret_auth == SSH_AUTH_PARTIAL)) { |
| 796 | ssh_key_free(pubkey); |
| 797 | continue; |
| 798 | } |
| 799 | if (ret_auth == SSH_AUTH_ERROR) { |
| 800 | ERR("Authentication failed (%s)", ssh_get_error(ssh_sess)); |
| 801 | ssh_key_free(pubkey); |
| 802 | break; |
| 803 | } |
| 804 | |
| 805 | if (ssh_opts.keys[j].privkey_crypt) { |
| 806 | s = sshauth_passphrase(ssh_opts.keys[j].privkey_path); |
| 807 | } else { |
| 808 | s = NULL; |
| 809 | } |
| 810 | |
| 811 | if (ssh_pki_import_privkey_file(ssh_opts.keys[j].privkey_path, s, NULL, NULL, &privkey) != SSH_OK) { |
| 812 | WRN("Failed to import the key \"%s\".", ssh_opts.keys[j].privkey_path); |
| 813 | if (s) { |
| 814 | memset(s, 0, strlen(s)); |
| 815 | free(s); |
| 816 | } |
| 817 | ssh_key_free(pubkey); |
| 818 | continue; |
| 819 | } |
| 820 | |
| 821 | if (s) { |
| 822 | memset(s, 0, strlen(s)); |
| 823 | free(s); |
| 824 | } |
| 825 | |
| 826 | ret_auth = ssh_userauth_publickey(ssh_sess, NULL, privkey); |
| 827 | ssh_key_free(pubkey); |
| 828 | ssh_key_free(privkey); |
| 829 | |
| 830 | if (ret_auth == SSH_AUTH_ERROR) { |
| 831 | ERR("Authentication failed (%s)", ssh_get_error(ssh_sess)); |
| 832 | } |
| 833 | if (ret_auth == SSH_AUTH_SUCCESS) { |
| 834 | break; |
| 835 | } |
| 836 | } |
| 837 | break; |
| 838 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | /* check a state of authentication */ |
| 842 | if (ret_auth != SSH_AUTH_SUCCESS) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 843 | return EXIT_FAILURE; |
| 844 | } |
| 845 | |
| 846 | /* open a channel */ |
| 847 | session->ti.libssh.channel = ssh_channel_new(ssh_sess); |
| 848 | if (ssh_channel_open_session(session->ti.libssh.channel) != SSH_OK) { |
| 849 | ssh_channel_free(session->ti.libssh.channel); |
| 850 | session->ti.libssh.channel = NULL; |
| 851 | ERR("Opening an SSH channel failed (%s)", ssh_get_error(ssh_sess)); |
| 852 | return EXIT_FAILURE; |
| 853 | } |
| 854 | |
| 855 | /* execute the NETCONF subsystem on the channel */ |
| 856 | if (ssh_channel_request_subsystem(session->ti.libssh.channel, "netconf") != SSH_OK) { |
| 857 | ssh_channel_free(session->ti.libssh.channel); |
| 858 | session->ti.libssh.channel = NULL; |
| 859 | ERR("Starting the \"netconf\" SSH subsystem failed (%s)", ssh_get_error(ssh_sess)); |
| 860 | return EXIT_FAILURE; |
| 861 | } |
| 862 | |
| 863 | return EXIT_SUCCESS; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | API struct nc_session * |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 867 | nc_connect_ssh(const char *host, uint16_t port, const char *username, struct ly_ctx *ctx) |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 868 | { |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 869 | const int timeout = NC_SSH_TIMEOUT; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 870 | int sock; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 871 | struct passwd *pw; |
| 872 | struct nc_session *session = NULL; |
| 873 | |
| 874 | /* process parameters */ |
| 875 | if (!host || strisempty(host)) { |
| 876 | host = "localhost"; |
| 877 | } |
| 878 | |
| 879 | if (!port) { |
| 880 | port = NC_PORT_SSH; |
| 881 | } |
| 882 | |
| 883 | if (!username) { |
| 884 | pw = getpwuid(getuid()); |
| 885 | if (!pw) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 886 | ERR("Unknown username for the SSH connection (%s).", strerror(errno)); |
| 887 | return NULL; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 888 | } else { |
| 889 | username = pw->pw_name; |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | /* prepare session structure */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 894 | session = calloc(1, sizeof *session); |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 895 | if (!session) { |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 896 | ERRMEM; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 897 | return NULL; |
| 898 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 899 | session->status = NC_STATUS_STARTING; |
| 900 | session->side = NC_CLIENT; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 901 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 902 | /* transport lock */ |
| 903 | session->ti_lock = malloc(sizeof *session->ti_lock); |
| 904 | if (!session->ti_lock) { |
| 905 | ERRMEM; |
| 906 | goto fail; |
| 907 | } |
| 908 | pthread_mutex_init(session->ti_lock, NULL); |
| 909 | |
| 910 | /* other transport-specific data */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 911 | session->ti_type = NC_TI_LIBSSH; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 912 | session->ti.libssh.session = ssh_new(); |
| 913 | if (!session->ti.libssh.session) { |
| 914 | ERR("Unable to initialize SSH session."); |
| 915 | goto fail; |
| 916 | } |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 917 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 918 | /* set some basic SSH session options */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 919 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOST, host); |
| 920 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_PORT, &port); |
| 921 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_USER, username); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 922 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_TIMEOUT, &timeout); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 923 | if (ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOSTKEYS, |
| 924 | "ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384," |
| 925 | "ecdsa-sha2-nistp256,ssh-rsa,ssh-dss,ssh-rsa1")) { |
| 926 | /* ecdsa is probably not supported... */ |
| 927 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOSTKEYS, "ssh-ed25519,ssh-rsa,ssh-dss,ssh-rsa1"); |
| 928 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 929 | |
| 930 | /* create and assign communication socket */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 931 | sock = nc_connect_getsocket(host, port); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 932 | if (sock == -1) { |
| 933 | goto fail; |
| 934 | } |
| 935 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_FD, &sock); |
| 936 | |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 937 | /* temporarily, for session connection */ |
| 938 | session->host = host; |
| 939 | session->username = username; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 940 | if (connect_ssh_session_netconf(session)) { |
| 941 | goto fail; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 942 | } |
| 943 | |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 944 | /* assign context (dicionary needed for handshake) */ |
| 945 | if (!ctx) { |
| 946 | ctx = ly_ctx_new(SCHEMAS_DIR); |
| 947 | } else { |
| 948 | session->flags |= NC_SESSION_SHAREDCTX; |
| 949 | } |
| 950 | session->ctx = ctx; |
| 951 | |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 952 | /* NETCONF handshake */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 953 | if (nc_handshake(session)) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 954 | goto fail; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 955 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 956 | session->status = NC_STATUS_RUNNING; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 957 | |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 958 | if (nc_ctx_check_and_fill(session)) { |
| 959 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /* store information into the dictionary */ |
| 963 | session->host = lydict_insert(ctx, host, 0); |
| 964 | session->port = port; |
| 965 | session->username = lydict_insert(ctx, username, 0); |
| 966 | |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 967 | return session; |
| 968 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 969 | fail: |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 970 | nc_session_free(session); |
| 971 | return NULL; |
| 972 | } |
| 973 | |
| 974 | API struct nc_session * |
| 975 | nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx) |
| 976 | { |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 977 | char *host = NULL, *username = NULL; |
| 978 | unsigned short port = 0; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 979 | int sock; |
| 980 | struct passwd *pw; |
| 981 | struct nc_session *session = NULL; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 982 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 983 | /* prepare session structure */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 984 | session = calloc(1, sizeof *session); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 985 | if (!session) { |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 986 | ERRMEM; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 987 | return NULL; |
| 988 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 989 | session->status = NC_STATUS_STARTING; |
| 990 | session->side = NC_CLIENT; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 991 | |
| 992 | /* transport lock */ |
| 993 | session->ti_lock = malloc(sizeof *session->ti_lock); |
| 994 | if (!session->ti_lock) { |
| 995 | ERRMEM; |
| 996 | goto fail; |
| 997 | } |
| 998 | pthread_mutex_init(session->ti_lock, NULL); |
| 999 | |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1000 | session->ti_type = NC_TI_LIBSSH; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1001 | session->ti.libssh.session = ssh_session; |
| 1002 | |
Michal Vasko | 745ff83 | 2015-12-08 14:40:29 +0100 | [diff] [blame] | 1003 | /* was port set? */ |
| 1004 | ssh_options_get_port(ssh_session, (unsigned int *)&port); |
| 1005 | |
| 1006 | if (ssh_options_get(ssh_session, SSH_OPTIONS_HOST, &host) != SSH_OK) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1007 | /* |
Michal Vasko | 745ff83 | 2015-12-08 14:40:29 +0100 | [diff] [blame] | 1008 | * There is no file descriptor (detected based on the host, there is no way to check |
| 1009 | * the SSH_OPTIONS_FD directly :/), we need to create it. (TCP/IP layer) |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1010 | */ |
| 1011 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1012 | /* remember host */ |
Michal Vasko | 745ff83 | 2015-12-08 14:40:29 +0100 | [diff] [blame] | 1013 | host = strdup("localhost"); |
| 1014 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOST, host); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1015 | |
| 1016 | /* create and connect socket */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1017 | sock = nc_connect_getsocket(host, port); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1018 | if (sock == -1) { |
| 1019 | goto fail; |
| 1020 | } |
| 1021 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_FD, &sock); |
| 1022 | } |
| 1023 | |
Michal Vasko | 745ff83 | 2015-12-08 14:40:29 +0100 | [diff] [blame] | 1024 | /* was username set? */ |
| 1025 | ssh_options_get(ssh_session, SSH_OPTIONS_USER, &username); |
| 1026 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1027 | if (!ssh_is_connected(ssh_session)) { |
| 1028 | /* |
| 1029 | * We are connected, but not SSH authenticated. (Transport layer) |
| 1030 | */ |
| 1031 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1032 | /* remember username */ |
| 1033 | if (!username) { |
| 1034 | pw = getpwuid(getuid()); |
| 1035 | if (!pw) { |
| 1036 | ERR("Unknown username for the SSH connection (%s).", strerror(errno)); |
| 1037 | goto fail; |
| 1038 | } |
| 1039 | |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1040 | username = strdup(pw->pw_name); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1041 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_USER, username); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1042 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1043 | |
| 1044 | /* authenticate SSH session */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1045 | session->host = host; |
| 1046 | session->username = username; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1047 | if (connect_ssh_session_netconf(session)) { |
| 1048 | goto fail; |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | * SSH session is established, create NETCONF session. (Application layer) |
| 1054 | */ |
| 1055 | |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1056 | /* assign context (dicionary needed for handshake) */ |
| 1057 | if (!ctx) { |
| 1058 | ctx = ly_ctx_new(SCHEMAS_DIR); |
| 1059 | } else { |
| 1060 | session->flags |= NC_SESSION_SHAREDCTX; |
| 1061 | } |
| 1062 | session->ctx = ctx; |
| 1063 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1064 | /* NETCONF handshake */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1065 | if (nc_handshake(session)) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1066 | goto fail; |
| 1067 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 1068 | session->status = NC_STATUS_RUNNING; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1069 | |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 1070 | if (nc_ctx_check_and_fill(session)) { |
| 1071 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | /* store information into the dictionary */ |
| 1075 | if (host) { |
| 1076 | session->host = lydict_insert_zc(ctx, host); |
| 1077 | } |
| 1078 | if (port) { |
| 1079 | session->port = port; |
| 1080 | } |
| 1081 | if (username) { |
| 1082 | session->username = lydict_insert_zc(ctx, username); |
| 1083 | } |
| 1084 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1085 | return session; |
| 1086 | |
| 1087 | fail: |
| 1088 | nc_session_free(session); |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1089 | return NULL; |
| 1090 | } |
| 1091 | |
| 1092 | API struct nc_session * |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1093 | nc_connect_ssh_channel(struct nc_session *session, struct ly_ctx *ctx) |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1094 | { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1095 | struct nc_session *new_session, *ptr; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1096 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1097 | /* prepare session structure */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1098 | new_session = calloc(1, sizeof *new_session); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1099 | if (!new_session) { |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1100 | ERRMEM; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1101 | return NULL; |
| 1102 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1103 | new_session->status = NC_STATUS_STARTING; |
| 1104 | new_session->side = NC_CLIENT; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1105 | |
| 1106 | /* share some parameters including the session lock */ |
| 1107 | new_session->ti_type = NC_TI_LIBSSH; |
| 1108 | new_session->ti_lock = session->ti_lock; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1109 | new_session->ti.libssh.session = session->ti.libssh.session; |
| 1110 | |
| 1111 | /* create the channel safely */ |
| 1112 | pthread_mutex_lock(new_session->ti_lock); |
| 1113 | |
| 1114 | /* open a channel */ |
| 1115 | new_session->ti.libssh.channel = ssh_channel_new(new_session->ti.libssh.session); |
| 1116 | if (ssh_channel_open_session(new_session->ti.libssh.channel) != SSH_OK) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1117 | ERR("Opening an SSH channel failed (%s)", ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1118 | goto fail; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1119 | } |
| 1120 | /* execute the NETCONF subsystem on the channel */ |
| 1121 | if (ssh_channel_request_subsystem(new_session->ti.libssh.channel, "netconf") != SSH_OK) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1122 | ERR("Starting the \"netconf\" SSH subsystem failed (%s)", ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1123 | goto fail; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1124 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1125 | |
| 1126 | /* assign context (dicionary needed for handshake) */ |
| 1127 | if (!ctx) { |
| 1128 | ctx = ly_ctx_new(SCHEMAS_DIR); |
| 1129 | } else { |
| 1130 | session->flags |= NC_SESSION_SHAREDCTX; |
| 1131 | } |
| 1132 | session->ctx = ctx; |
| 1133 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1134 | /* NETCONF handshake */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1135 | if (nc_handshake(new_session)) { |
| 1136 | goto fail; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1137 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 1138 | new_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1139 | |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 1140 | if (nc_ctx_check_and_fill(session)) { |
| 1141 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | /* store information into session and the dictionary */ |
| 1145 | session->host = lydict_insert(ctx, session->host, 0); |
| 1146 | session->port = session->port; |
| 1147 | session->username = lydict_insert(ctx, session->username, 0); |
| 1148 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1149 | pthread_mutex_unlock(new_session->ti_lock); |
| 1150 | |
| 1151 | /* append to the session ring list */ |
| 1152 | if (!session->ti.libssh.next) { |
| 1153 | session->ti.libssh.next = new_session; |
| 1154 | new_session->ti.libssh.next = session; |
| 1155 | } else { |
| 1156 | ptr = session->ti.libssh.next; |
| 1157 | session->ti.libssh.next = new_session; |
| 1158 | new_session->ti.libssh.next = ptr; |
| 1159 | } |
| 1160 | |
| 1161 | return new_session; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1162 | |
| 1163 | fail: |
| 1164 | nc_session_free(new_session); |
| 1165 | return NULL; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1166 | } |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1167 | |
| 1168 | API struct nc_session * |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1169 | nc_callhome_accept_ssh(uint16_t port, const char *username, int timeout, struct ly_ctx *ctx) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1170 | { |
| 1171 | const int ssh_timeout = NC_SSH_TIMEOUT; |
| 1172 | int sock; |
| 1173 | char *server_host; |
| 1174 | ssh_session sess; |
| 1175 | |
| 1176 | if (!port) { |
| 1177 | port = NC_PORT_CH_SSH; |
| 1178 | } |
| 1179 | |
| 1180 | sock = nc_callhome_accept_connection(port, timeout, NULL, &server_host); |
| 1181 | if (sock == -1) { |
| 1182 | return NULL; |
| 1183 | } |
| 1184 | |
| 1185 | sess = ssh_new(); |
| 1186 | if (!sess) { |
| 1187 | ERR("Unable to initialize an SSH session."); |
| 1188 | close(sock); |
| 1189 | return NULL; |
| 1190 | } |
| 1191 | |
| 1192 | ssh_options_set(sess, SSH_OPTIONS_FD, &sock); |
| 1193 | ssh_options_set(sess, SSH_OPTIONS_HOST, server_host); |
| 1194 | ssh_options_set(sess, SSH_OPTIONS_PORT, &port); |
| 1195 | ssh_options_set(sess, SSH_OPTIONS_TIMEOUT, &ssh_timeout); |
| 1196 | if (username) { |
| 1197 | ssh_options_set(sess, SSH_OPTIONS_USER, username); |
| 1198 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1199 | if (ssh_options_set(sess, SSH_OPTIONS_HOSTKEYS, |
| 1200 | "ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384," |
| 1201 | "ecdsa-sha2-nistp256,ssh-rsa,ssh-dss,ssh-rsa1")) { |
| 1202 | /* ecdsa is probably not supported... */ |
| 1203 | ssh_options_set(sess, SSH_OPTIONS_HOSTKEYS, "ssh-ed25519,ssh-rsa,ssh-dss,ssh-rsa1"); |
| 1204 | } |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1205 | |
| 1206 | free(server_host); |
| 1207 | |
| 1208 | return nc_connect_libssh(sess, ctx); |
| 1209 | } |