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) { |
| 495 | DBG("DNSSEC SSHFP check successful"); |
| 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) { |
| 510 | fprintf(stdout, "No matching host key fingerprint found in DNS.\n"); |
| 511 | } else if (ret == 1) { |
| 512 | fprintf(stdout, "Matching host key fingerprint found in DNS.\n"); |
| 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); |
| 529 | if (ret < 0) { |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 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 | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 640 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 641 | memcpy(opts->keys + idx, opts->keys + opts->key_count, sizeof *opts->keys); |
| 642 | opts->keys = realloc(opts->keys, opts->key_count * sizeof *opts->keys); |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 643 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 644 | return 0; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 648 | nc_client_ssh_del_keypair(int idx) |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 649 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 650 | return _nc_client_ssh_del_keypair(idx, &ssh_opts); |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 654 | nc_client_ssh_ch_del_keypair(int idx) |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +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_ch_opts); |
| 657 | } |
| 658 | |
| 659 | static int |
| 660 | _nc_client_ssh_get_keypair_count(struct nc_client_ssh_opts *opts) |
| 661 | { |
| 662 | return opts->key_count; |
| 663 | } |
| 664 | |
| 665 | API int |
| 666 | nc_client_ssh_get_keypair_count(void) |
| 667 | { |
| 668 | return _nc_client_ssh_get_keypair_count(&ssh_opts); |
| 669 | } |
| 670 | |
| 671 | API int |
| 672 | nc_client_ssh_ch_get_keypair_count(void) |
| 673 | { |
| 674 | return _nc_client_ssh_get_keypair_count(&ssh_ch_opts); |
| 675 | } |
| 676 | |
| 677 | static int |
| 678 | _nc_client_ssh_get_keypair(int idx, const char **pub_key, const char **priv_key, struct nc_client_ssh_opts *opts) |
| 679 | { |
| 680 | if ((idx >= opts->key_count) || (!pub_key && !priv_key)) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 681 | ERRARG; |
| 682 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 683 | } |
| 684 | |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 685 | if (pub_key) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 686 | *pub_key = opts->keys[idx].pubkey_path; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 687 | } |
| 688 | if (priv_key) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 689 | *priv_key = opts->keys[idx].privkey_path; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 690 | } |
| 691 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 692 | return 0; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 693 | } |
| 694 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 695 | API int |
| 696 | nc_client_ssh_get_keypair(int idx, const char **pub_key, const char **priv_key) |
| 697 | { |
| 698 | return _nc_client_ssh_get_keypair(idx, pub_key, priv_key, &ssh_opts); |
| 699 | } |
| 700 | |
| 701 | API int |
| 702 | nc_client_ssh_ch_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_ch_opts); |
| 705 | } |
| 706 | |
| 707 | static void |
| 708 | _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] | 709 | { |
| 710 | if (pref < 0) { |
| 711 | pref = -1; |
| 712 | } |
| 713 | |
| 714 | if (auth_type == NC_SSH_AUTH_INTERACTIVE) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 715 | opts->auth_pref[0].value = pref; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 716 | } else if (auth_type == NC_SSH_AUTH_PASSWORD) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 717 | opts->auth_pref[1].value = pref; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 718 | } else if (auth_type == NC_SSH_AUTH_PUBLICKEY) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 719 | opts->auth_pref[2].value = pref; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 723 | API void |
| 724 | 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] | 725 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 726 | _nc_client_ssh_set_auth_pref(auth_type, pref, &ssh_opts); |
| 727 | } |
| 728 | |
| 729 | API void |
| 730 | nc_client_ssh_ch_set_auth_pref(NC_SSH_AUTH_TYPE auth_type, int16_t pref) |
| 731 | { |
| 732 | _nc_client_ssh_set_auth_pref(auth_type, pref, &ssh_ch_opts); |
| 733 | } |
| 734 | |
| 735 | static int16_t |
| 736 | _nc_client_ssh_get_auth_pref(NC_SSH_AUTH_TYPE auth_type, struct nc_client_ssh_opts *opts) |
| 737 | { |
| 738 | int16_t pref = 0; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 739 | |
| 740 | if (auth_type == NC_SSH_AUTH_INTERACTIVE) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 741 | pref = opts->auth_pref[0].value; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 742 | } else if (auth_type == NC_SSH_AUTH_PASSWORD) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 743 | pref = opts->auth_pref[1].value; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 744 | } else if (auth_type == NC_SSH_AUTH_PUBLICKEY) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 745 | pref = opts->auth_pref[2].value; |
Michal Vasko | e9bc814 | 2015-12-04 11:09:35 +0100 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | return pref; |
| 749 | } |
| 750 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 751 | API int16_t |
| 752 | nc_client_ssh_get_auth_pref(NC_SSH_AUTH_TYPE auth_type) |
| 753 | { |
| 754 | return _nc_client_ssh_get_auth_pref(auth_type, &ssh_opts); |
| 755 | } |
| 756 | |
| 757 | API int16_t |
| 758 | nc_client_ssh_ch_get_auth_pref(NC_SSH_AUTH_TYPE auth_type) |
| 759 | { |
| 760 | return _nc_client_ssh_get_auth_pref(auth_type, &ssh_ch_opts); |
| 761 | } |
| 762 | |
| 763 | static int |
| 764 | _nc_client_ssh_set_username(const char *username, struct nc_client_ssh_opts *opts) |
| 765 | { |
| 766 | if (opts->username) { |
| 767 | free(opts->username); |
| 768 | } |
| 769 | if (username) { |
| 770 | opts->username = strdup(username); |
| 771 | if (!opts->username) { |
| 772 | ERRMEM; |
| 773 | return -1; |
| 774 | } |
| 775 | } else { |
| 776 | opts->username = NULL; |
| 777 | } |
| 778 | |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | API int |
| 783 | nc_client_ssh_set_username(const char *username) |
| 784 | { |
| 785 | return _nc_client_ssh_set_username(username, &ssh_opts); |
| 786 | } |
| 787 | |
| 788 | API int |
| 789 | nc_client_ssh_ch_set_username(const char *username) |
| 790 | { |
| 791 | return _nc_client_ssh_set_username(username, &ssh_ch_opts); |
| 792 | } |
| 793 | |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 794 | static const char * |
| 795 | _nc_client_ssh_get_username(struct nc_client_ssh_opts *opts) |
| 796 | { |
| 797 | return opts->username; |
| 798 | } |
| 799 | |
| 800 | API const char * |
| 801 | nc_client_ssh_get_username(void) |
| 802 | { |
| 803 | return _nc_client_ssh_get_username(&ssh_opts); |
| 804 | } |
| 805 | |
| 806 | API const char * |
| 807 | nc_client_ssh_ch_get_username(void) |
| 808 | { |
| 809 | return _nc_client_ssh_get_username(&ssh_ch_opts); |
| 810 | } |
| 811 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 812 | API int |
| 813 | nc_client_ssh_ch_add_bind_listen(const char *address, uint16_t port) |
| 814 | { |
| 815 | return nc_client_ch_add_bind_listen(address, port, NC_TI_LIBSSH); |
| 816 | } |
| 817 | |
| 818 | API int |
| 819 | nc_client_ssh_ch_del_bind(const char *address, uint16_t port) |
| 820 | { |
| 821 | return nc_client_ch_del_bind(address, port, NC_TI_LIBSSH); |
| 822 | } |
| 823 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 824 | /* Establish a secure SSH connection, authenticate, and create a channel with the 'netconf' subsystem. |
| 825 | * Host, port, username, and a connected socket is expected to be set. |
| 826 | */ |
| 827 | static int |
| 828 | connect_ssh_session_netconf(struct nc_session *session) |
| 829 | { |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 830 | int j, ret_auth, userauthlist; |
| 831 | NC_SSH_AUTH_TYPE auth; |
| 832 | short int pref; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 833 | const char* prompt; |
| 834 | char *s, *answer, echo; |
| 835 | ssh_key pubkey, privkey; |
| 836 | ssh_session ssh_sess; |
| 837 | |
| 838 | ssh_sess = session->ti.libssh.session; |
| 839 | |
| 840 | if (ssh_connect(ssh_sess) != SSH_OK) { |
| 841 | ERR("Starting the SSH session failed (%s)", ssh_get_error(ssh_sess)); |
| 842 | DBG("Error code %d.", ssh_get_error_code(ssh_sess)); |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 843 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | if (sshauth_hostkey_check(session->host, ssh_sess)) { |
| 847 | ERR("Checking the host key failed."); |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 848 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | if ((ret_auth = ssh_userauth_none(ssh_sess, NULL)) == SSH_AUTH_ERROR) { |
| 852 | ERR("Authentication failed (%s).", ssh_get_error(ssh_sess)); |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 853 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | /* check what authentication methods are available */ |
| 857 | userauthlist = ssh_userauth_list(ssh_sess, NULL); |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 858 | |
| 859 | /* remove those disabled */ |
| 860 | if (ssh_opts.auth_pref[0].value < 0) { |
| 861 | VRB("Interactive SSH authentication method was disabled."); |
| 862 | userauthlist &= ~SSH_AUTH_METHOD_INTERACTIVE; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 863 | } |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 864 | if (ssh_opts.auth_pref[1].value < 0) { |
| 865 | VRB("Password SSH authentication method was disabled."); |
| 866 | userauthlist &= ~SSH_AUTH_METHOD_PASSWORD; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 867 | } |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 868 | if (ssh_opts.auth_pref[2].value < 0) { |
| 869 | VRB("Publickey SSH authentication method was disabled."); |
| 870 | userauthlist &= ~SSH_AUTH_METHOD_PUBLICKEY; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 871 | } |
| 872 | |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 873 | while (ret_auth != SSH_AUTH_SUCCESS) { |
| 874 | auth = 0; |
| 875 | pref = 0; |
| 876 | if (userauthlist & SSH_AUTH_METHOD_INTERACTIVE) { |
| 877 | auth = NC_SSH_AUTH_INTERACTIVE; |
| 878 | pref = ssh_opts.auth_pref[0].value; |
| 879 | } |
| 880 | if ((userauthlist & SSH_AUTH_METHOD_PASSWORD) && (ssh_opts.auth_pref[1].value > pref)) { |
| 881 | auth = NC_SSH_AUTH_PASSWORD; |
| 882 | pref = ssh_opts.auth_pref[1].value; |
| 883 | } |
| 884 | if ((userauthlist & SSH_AUTH_METHOD_PUBLICKEY) && (ssh_opts.auth_pref[2].value > pref)) { |
| 885 | auth = NC_SSH_AUTH_PUBLICKEY; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 886 | } |
| 887 | |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 888 | if (!auth) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 889 | 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] | 890 | break; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | /* found common authentication method */ |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 894 | switch (auth) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 895 | case NC_SSH_AUTH_PASSWORD: |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 896 | userauthlist &= ~SSH_AUTH_METHOD_PASSWORD; |
| 897 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 898 | VRB("Password authentication (host \"%s\", user \"%s\").", session->host, session->username); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 899 | s = sshauth_password(session->username, session->host); |
| 900 | if ((ret_auth = ssh_userauth_password(ssh_sess, session->username, s)) != SSH_AUTH_SUCCESS) { |
| 901 | memset(s, 0, strlen(s)); |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 902 | VRB("Authentication failed (%s).", ssh_get_error(ssh_sess)); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 903 | } |
| 904 | free(s); |
| 905 | break; |
| 906 | case NC_SSH_AUTH_INTERACTIVE: |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 907 | userauthlist &= ~SSH_AUTH_METHOD_INTERACTIVE; |
| 908 | |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 909 | VRB("Keyboard-interactive authentication."); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 910 | while ((ret_auth = ssh_userauth_kbdint(ssh_sess, NULL, NULL)) == SSH_AUTH_INFO) { |
| 911 | for (j = 0; j < ssh_userauth_kbdint_getnprompts(ssh_sess); ++j) { |
| 912 | prompt = ssh_userauth_kbdint_getprompt(ssh_sess, j, &echo); |
| 913 | if (prompt == NULL) { |
| 914 | break; |
| 915 | } |
| 916 | answer = sshauth_interactive(ssh_userauth_kbdint_getname(ssh_sess), |
| 917 | ssh_userauth_kbdint_getinstruction(ssh_sess), |
| 918 | prompt, echo); |
| 919 | if (ssh_userauth_kbdint_setanswer(ssh_sess, j, answer) < 0) { |
| 920 | free(answer); |
| 921 | break; |
| 922 | } |
| 923 | free(answer); |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | if (ret_auth == SSH_AUTH_ERROR) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 928 | VRB("Authentication failed (%s).", ssh_get_error(ssh_sess)); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | break; |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 932 | case NC_SSH_AUTH_PUBLICKEY: |
Michal Vasko | 235efdc | 2015-12-17 12:05:04 +0100 | [diff] [blame] | 933 | userauthlist &= ~SSH_AUTH_METHOD_PUBLICKEY; |
| 934 | |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 935 | VRB("Publickey athentication."); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 936 | |
| 937 | /* if publickeys path not provided, we cannot continue */ |
| 938 | if (!ssh_opts.key_count) { |
| 939 | VRB("No key pair specified."); |
| 940 | break; |
| 941 | } |
| 942 | |
| 943 | for (j = 0; j < ssh_opts.key_count; j++) { |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 944 | VRB("Trying to authenticate using %spair \"%s\" \"%s\".", |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 945 | ssh_opts.keys[j].privkey_crypt ? "password-protected " : "", ssh_opts.keys[j].privkey_path, |
| 946 | ssh_opts.keys[j].pubkey_path); |
| 947 | |
| 948 | if (ssh_pki_import_pubkey_file(ssh_opts.keys[j].pubkey_path, &pubkey) != SSH_OK) { |
| 949 | WRN("Failed to import the key \"%s\".", ssh_opts.keys[j].pubkey_path); |
| 950 | continue; |
| 951 | } |
| 952 | ret_auth = ssh_userauth_try_publickey(ssh_sess, NULL, pubkey); |
| 953 | if ((ret_auth == SSH_AUTH_DENIED) || (ret_auth == SSH_AUTH_PARTIAL)) { |
| 954 | ssh_key_free(pubkey); |
| 955 | continue; |
| 956 | } |
| 957 | if (ret_auth == SSH_AUTH_ERROR) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 958 | ERR("Authentication failed (%s).", ssh_get_error(ssh_sess)); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 959 | ssh_key_free(pubkey); |
| 960 | break; |
| 961 | } |
| 962 | |
| 963 | if (ssh_opts.keys[j].privkey_crypt) { |
| 964 | s = sshauth_passphrase(ssh_opts.keys[j].privkey_path); |
| 965 | } else { |
| 966 | s = NULL; |
| 967 | } |
| 968 | |
| 969 | if (ssh_pki_import_privkey_file(ssh_opts.keys[j].privkey_path, s, NULL, NULL, &privkey) != SSH_OK) { |
| 970 | WRN("Failed to import the key \"%s\".", ssh_opts.keys[j].privkey_path); |
| 971 | if (s) { |
| 972 | memset(s, 0, strlen(s)); |
| 973 | free(s); |
| 974 | } |
| 975 | ssh_key_free(pubkey); |
| 976 | continue; |
| 977 | } |
| 978 | |
| 979 | if (s) { |
| 980 | memset(s, 0, strlen(s)); |
| 981 | free(s); |
| 982 | } |
| 983 | |
| 984 | ret_auth = ssh_userauth_publickey(ssh_sess, NULL, privkey); |
| 985 | ssh_key_free(pubkey); |
| 986 | ssh_key_free(privkey); |
| 987 | |
| 988 | if (ret_auth == SSH_AUTH_ERROR) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 989 | ERR("Authentication failed (%s).", ssh_get_error(ssh_sess)); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 990 | } |
| 991 | if (ret_auth == SSH_AUTH_SUCCESS) { |
| 992 | break; |
| 993 | } |
| 994 | } |
| 995 | break; |
| 996 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | /* check a state of authentication */ |
| 1000 | if (ret_auth != SSH_AUTH_SUCCESS) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1001 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /* open a channel */ |
| 1005 | session->ti.libssh.channel = ssh_channel_new(ssh_sess); |
| 1006 | if (ssh_channel_open_session(session->ti.libssh.channel) != SSH_OK) { |
| 1007 | ssh_channel_free(session->ti.libssh.channel); |
| 1008 | session->ti.libssh.channel = NULL; |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1009 | ERR("Opening an SSH channel failed (%s).", ssh_get_error(ssh_sess)); |
| 1010 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | /* execute the NETCONF subsystem on the channel */ |
| 1014 | if (ssh_channel_request_subsystem(session->ti.libssh.channel, "netconf") != SSH_OK) { |
| 1015 | ssh_channel_free(session->ti.libssh.channel); |
| 1016 | session->ti.libssh.channel = NULL; |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1017 | ERR("Starting the \"netconf\" SSH subsystem failed (%s).", ssh_get_error(ssh_sess)); |
| 1018 | return -1; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | return EXIT_SUCCESS; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | API struct nc_session * |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1025 | 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] | 1026 | { |
Michal Vasko | c111ac5 | 2015-12-08 14:36:36 +0100 | [diff] [blame] | 1027 | const int timeout = NC_SSH_TIMEOUT; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1028 | int sock; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1029 | char *username; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1030 | struct passwd *pw; |
| 1031 | struct nc_session *session = NULL; |
| 1032 | |
| 1033 | /* process parameters */ |
| 1034 | if (!host || strisempty(host)) { |
| 1035 | host = "localhost"; |
| 1036 | } |
| 1037 | |
| 1038 | if (!port) { |
| 1039 | port = NC_PORT_SSH; |
| 1040 | } |
| 1041 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1042 | if (!ssh_opts.username) { |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1043 | pw = getpwuid(getuid()); |
| 1044 | if (!pw) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1045 | ERR("Unknown username for the SSH connection (%s).", strerror(errno)); |
| 1046 | return NULL; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1047 | } else { |
| 1048 | username = pw->pw_name; |
| 1049 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1050 | } else { |
| 1051 | username = ssh_opts.username; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /* prepare session structure */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1055 | session = calloc(1, sizeof *session); |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1056 | if (!session) { |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1057 | ERRMEM; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1058 | return NULL; |
| 1059 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1060 | session->status = NC_STATUS_STARTING; |
| 1061 | session->side = NC_CLIENT; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1062 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1063 | /* transport lock */ |
| 1064 | session->ti_lock = malloc(sizeof *session->ti_lock); |
| 1065 | if (!session->ti_lock) { |
| 1066 | ERRMEM; |
| 1067 | goto fail; |
| 1068 | } |
| 1069 | pthread_mutex_init(session->ti_lock, NULL); |
| 1070 | |
| 1071 | /* other transport-specific data */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1072 | session->ti_type = NC_TI_LIBSSH; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1073 | session->ti.libssh.session = ssh_new(); |
| 1074 | if (!session->ti.libssh.session) { |
| 1075 | ERR("Unable to initialize SSH session."); |
| 1076 | goto fail; |
| 1077 | } |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1078 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1079 | /* set some basic SSH session options */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1080 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOST, host); |
| 1081 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_PORT, &port); |
| 1082 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_USER, username); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1083 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_TIMEOUT, &timeout); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1084 | if (ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOSTKEYS, |
| 1085 | "ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384," |
| 1086 | "ecdsa-sha2-nistp256,ssh-rsa,ssh-dss,ssh-rsa1")) { |
| 1087 | /* ecdsa is probably not supported... */ |
| 1088 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOSTKEYS, "ssh-ed25519,ssh-rsa,ssh-dss,ssh-rsa1"); |
| 1089 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1090 | |
| 1091 | /* create and assign communication socket */ |
Michal Vasko | f05562c | 2016-01-20 12:06:43 +0100 | [diff] [blame] | 1092 | sock = nc_sock_connect(host, port); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1093 | if (sock == -1) { |
| 1094 | goto fail; |
| 1095 | } |
| 1096 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_FD, &sock); |
| 1097 | |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1098 | /* temporarily, for session connection */ |
| 1099 | session->host = host; |
| 1100 | session->username = username; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1101 | if (connect_ssh_session_netconf(session)) { |
| 1102 | goto fail; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1103 | } |
| 1104 | |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1105 | /* assign context (dicionary needed for handshake) */ |
| 1106 | if (!ctx) { |
| 1107 | ctx = ly_ctx_new(SCHEMAS_DIR); |
| 1108 | } else { |
| 1109 | session->flags |= NC_SESSION_SHAREDCTX; |
| 1110 | } |
| 1111 | session->ctx = ctx; |
| 1112 | |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1113 | /* NETCONF handshake */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1114 | if (nc_handshake(session)) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1115 | goto fail; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1116 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 1117 | session->status = NC_STATUS_RUNNING; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1118 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 1119 | if (nc_ctx_check_and_fill(session) == -1) { |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 1120 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | /* store information into the dictionary */ |
| 1124 | session->host = lydict_insert(ctx, host, 0); |
| 1125 | session->port = port; |
| 1126 | session->username = lydict_insert(ctx, username, 0); |
| 1127 | |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1128 | return session; |
| 1129 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1130 | fail: |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1131 | nc_session_free(session); |
| 1132 | return NULL; |
| 1133 | } |
| 1134 | |
| 1135 | API struct nc_session * |
| 1136 | nc_connect_libssh(ssh_session ssh_session, struct ly_ctx *ctx) |
| 1137 | { |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1138 | char *host = NULL, *username = NULL; |
| 1139 | unsigned short port = 0; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1140 | int sock; |
| 1141 | struct passwd *pw; |
| 1142 | struct nc_session *session = NULL; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1143 | |
Michal Vasko | b7c4ff3 | 2016-01-21 15:35:54 +0100 | [diff] [blame] | 1144 | if (!ssh_session) { |
| 1145 | ERRARG; |
| 1146 | return NULL; |
| 1147 | } |
| 1148 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1149 | /* prepare session structure */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1150 | session = calloc(1, sizeof *session); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1151 | if (!session) { |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1152 | ERRMEM; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1153 | return NULL; |
| 1154 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1155 | session->status = NC_STATUS_STARTING; |
| 1156 | session->side = NC_CLIENT; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1157 | |
| 1158 | /* transport lock */ |
| 1159 | session->ti_lock = malloc(sizeof *session->ti_lock); |
| 1160 | if (!session->ti_lock) { |
| 1161 | ERRMEM; |
| 1162 | goto fail; |
| 1163 | } |
| 1164 | pthread_mutex_init(session->ti_lock, NULL); |
| 1165 | |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1166 | session->ti_type = NC_TI_LIBSSH; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1167 | session->ti.libssh.session = ssh_session; |
| 1168 | |
Michal Vasko | 745ff83 | 2015-12-08 14:40:29 +0100 | [diff] [blame] | 1169 | /* was port set? */ |
| 1170 | ssh_options_get_port(ssh_session, (unsigned int *)&port); |
| 1171 | |
| 1172 | if (ssh_options_get(ssh_session, SSH_OPTIONS_HOST, &host) != SSH_OK) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1173 | /* |
Michal Vasko | 745ff83 | 2015-12-08 14:40:29 +0100 | [diff] [blame] | 1174 | * There is no file descriptor (detected based on the host, there is no way to check |
| 1175 | * 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] | 1176 | */ |
| 1177 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1178 | /* remember host */ |
Michal Vasko | 745ff83 | 2015-12-08 14:40:29 +0100 | [diff] [blame] | 1179 | host = strdup("localhost"); |
| 1180 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_HOST, host); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1181 | |
| 1182 | /* create and connect socket */ |
Michal Vasko | f05562c | 2016-01-20 12:06:43 +0100 | [diff] [blame] | 1183 | sock = nc_sock_connect(host, port); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1184 | if (sock == -1) { |
| 1185 | goto fail; |
| 1186 | } |
| 1187 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_FD, &sock); |
| 1188 | } |
| 1189 | |
Michal Vasko | 745ff83 | 2015-12-08 14:40:29 +0100 | [diff] [blame] | 1190 | /* was username set? */ |
| 1191 | ssh_options_get(ssh_session, SSH_OPTIONS_USER, &username); |
| 1192 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1193 | if (!ssh_is_connected(ssh_session)) { |
| 1194 | /* |
| 1195 | * We are connected, but not SSH authenticated. (Transport layer) |
| 1196 | */ |
| 1197 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1198 | /* remember username */ |
| 1199 | if (!username) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1200 | if (!ssh_opts.username) { |
| 1201 | pw = getpwuid(getuid()); |
| 1202 | if (!pw) { |
| 1203 | ERR("Unknown username for the SSH connection (%s).", strerror(errno)); |
| 1204 | goto fail; |
| 1205 | } |
| 1206 | username = strdup(pw->pw_name); |
| 1207 | } else { |
| 1208 | username = strdup(ssh_opts.username); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1209 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1210 | ssh_options_set(session->ti.libssh.session, SSH_OPTIONS_USER, username); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1211 | } |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1212 | |
| 1213 | /* authenticate SSH session */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1214 | session->host = host; |
| 1215 | session->username = username; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1216 | if (connect_ssh_session_netconf(session)) { |
| 1217 | goto fail; |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * SSH session is established, create NETCONF session. (Application layer) |
| 1223 | */ |
| 1224 | |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1225 | /* assign context (dicionary needed for handshake) */ |
| 1226 | if (!ctx) { |
| 1227 | ctx = ly_ctx_new(SCHEMAS_DIR); |
| 1228 | } else { |
| 1229 | session->flags |= NC_SESSION_SHAREDCTX; |
| 1230 | } |
| 1231 | session->ctx = ctx; |
| 1232 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1233 | /* NETCONF handshake */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1234 | if (nc_handshake(session)) { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1235 | goto fail; |
| 1236 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 1237 | session->status = NC_STATUS_RUNNING; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1238 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 1239 | if (nc_ctx_check_and_fill(session) == -1) { |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 1240 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | /* store information into the dictionary */ |
| 1244 | if (host) { |
| 1245 | session->host = lydict_insert_zc(ctx, host); |
| 1246 | } |
| 1247 | if (port) { |
| 1248 | session->port = port; |
| 1249 | } |
| 1250 | if (username) { |
| 1251 | session->username = lydict_insert_zc(ctx, username); |
| 1252 | } |
| 1253 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1254 | return session; |
| 1255 | |
| 1256 | fail: |
| 1257 | nc_session_free(session); |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1258 | return NULL; |
| 1259 | } |
| 1260 | |
| 1261 | API struct nc_session * |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1262 | nc_connect_ssh_channel(struct nc_session *session, struct ly_ctx *ctx) |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1263 | { |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1264 | struct nc_session *new_session, *ptr; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1265 | |
Michal Vasko | b7c4ff3 | 2016-01-21 15:35:54 +0100 | [diff] [blame] | 1266 | if (!session) { |
| 1267 | ERRARG; |
| 1268 | return NULL; |
| 1269 | } |
| 1270 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1271 | /* prepare session structure */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1272 | new_session = calloc(1, sizeof *new_session); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1273 | if (!new_session) { |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1274 | ERRMEM; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1275 | return NULL; |
| 1276 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1277 | new_session->status = NC_STATUS_STARTING; |
| 1278 | new_session->side = NC_CLIENT; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1279 | |
| 1280 | /* share some parameters including the session lock */ |
| 1281 | new_session->ti_type = NC_TI_LIBSSH; |
| 1282 | new_session->ti_lock = session->ti_lock; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1283 | new_session->ti.libssh.session = session->ti.libssh.session; |
| 1284 | |
| 1285 | /* create the channel safely */ |
| 1286 | pthread_mutex_lock(new_session->ti_lock); |
| 1287 | |
| 1288 | /* open a channel */ |
| 1289 | new_session->ti.libssh.channel = ssh_channel_new(new_session->ti.libssh.session); |
| 1290 | if (ssh_channel_open_session(new_session->ti.libssh.channel) != SSH_OK) { |
Michal Vasko | 56b5bf7 | 2016-01-19 11:20:35 +0100 | [diff] [blame] | 1291 | ERR("Opening an SSH channel failed (%s).", ssh_get_error(new_session->ti.libssh.session)); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1292 | goto fail; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1293 | } |
| 1294 | /* execute the NETCONF subsystem on the channel */ |
| 1295 | if (ssh_channel_request_subsystem(new_session->ti.libssh.channel, "netconf") != SSH_OK) { |
Michal Vasko | 56b5bf7 | 2016-01-19 11:20:35 +0100 | [diff] [blame] | 1296 | ERR("Starting the \"netconf\" SSH subsystem failed (%s).", ssh_get_error(new_session->ti.libssh.session)); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1297 | goto fail; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1298 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1299 | |
| 1300 | /* assign context (dicionary needed for handshake) */ |
| 1301 | if (!ctx) { |
| 1302 | ctx = ly_ctx_new(SCHEMAS_DIR); |
| 1303 | } else { |
Michal Vasko | 56b5bf7 | 2016-01-19 11:20:35 +0100 | [diff] [blame] | 1304 | new_session->flags |= NC_SESSION_SHAREDCTX; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1305 | } |
Michal Vasko | 56b5bf7 | 2016-01-19 11:20:35 +0100 | [diff] [blame] | 1306 | new_session->ctx = ctx; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1307 | |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1308 | /* NETCONF handshake */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1309 | if (nc_handshake(new_session)) { |
| 1310 | goto fail; |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1311 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 1312 | new_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1313 | |
Michal Vasko | 56b5bf7 | 2016-01-19 11:20:35 +0100 | [diff] [blame] | 1314 | pthread_mutex_unlock(new_session->ti_lock); |
| 1315 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 1316 | if (nc_ctx_check_and_fill(new_session) == -1) { |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 1317 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | /* store information into session and the dictionary */ |
Michal Vasko | 56b5bf7 | 2016-01-19 11:20:35 +0100 | [diff] [blame] | 1321 | new_session->host = lydict_insert(ctx, session->host, 0); |
| 1322 | new_session->port = session->port; |
| 1323 | new_session->username = lydict_insert(ctx, session->username, 0); |
Michal Vasko | 7b62fed | 2015-10-26 15:39:46 +0100 | [diff] [blame] | 1324 | |
| 1325 | /* append to the session ring list */ |
| 1326 | if (!session->ti.libssh.next) { |
| 1327 | session->ti.libssh.next = new_session; |
| 1328 | new_session->ti.libssh.next = session; |
| 1329 | } else { |
| 1330 | ptr = session->ti.libssh.next; |
| 1331 | session->ti.libssh.next = new_session; |
| 1332 | new_session->ti.libssh.next = ptr; |
| 1333 | } |
| 1334 | |
| 1335 | return new_session; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1336 | |
| 1337 | fail: |
| 1338 | nc_session_free(new_session); |
| 1339 | return NULL; |
Radek Krejci | ac6d347 | 2015-10-22 15:47:18 +0200 | [diff] [blame] | 1340 | } |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1341 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1342 | struct nc_session * |
Michal Vasko | af58cd9 | 2016-01-28 11:56:02 +0100 | [diff] [blame] | 1343 | 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] | 1344 | { |
| 1345 | const int ssh_timeout = NC_SSH_TIMEOUT; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1346 | struct passwd *pw; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1347 | ssh_session sess; |
| 1348 | |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1349 | sess = ssh_new(); |
| 1350 | if (!sess) { |
| 1351 | ERR("Unable to initialize an SSH session."); |
| 1352 | close(sock); |
| 1353 | return NULL; |
| 1354 | } |
| 1355 | |
| 1356 | ssh_options_set(sess, SSH_OPTIONS_FD, &sock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1357 | ssh_options_set(sess, SSH_OPTIONS_HOST, host); |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1358 | ssh_options_set(sess, SSH_OPTIONS_PORT, &port); |
| 1359 | ssh_options_set(sess, SSH_OPTIONS_TIMEOUT, &ssh_timeout); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1360 | if (!ssh_ch_opts.username) { |
| 1361 | pw = getpwuid(getuid()); |
| 1362 | if (!pw) { |
| 1363 | ERR("Unknown username for the SSH connection (%s).", strerror(errno)); |
| 1364 | return NULL; |
| 1365 | } |
| 1366 | ssh_options_set(sess, SSH_OPTIONS_USER, pw->pw_name); |
| 1367 | } else { |
| 1368 | ssh_options_set(sess, SSH_OPTIONS_USER, ssh_ch_opts.username); |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1369 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1370 | if (ssh_options_set(sess, SSH_OPTIONS_HOSTKEYS, |
| 1371 | "ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384," |
| 1372 | "ecdsa-sha2-nistp256,ssh-rsa,ssh-dss,ssh-rsa1")) { |
| 1373 | /* ecdsa is probably not supported... */ |
| 1374 | ssh_options_set(sess, SSH_OPTIONS_HOSTKEYS, "ssh-ed25519,ssh-rsa,ssh-dss,ssh-rsa1"); |
| 1375 | } |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1376 | |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1377 | return nc_connect_libssh(sess, ctx); |
| 1378 | } |