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