Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file session_server_ssh.c |
| 3 | * \author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * \brief libnetconf2 SSH server session manipulation functions |
| 5 | * |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 6 | * Copyright (c) 2017 CESNET, z.s.p.o. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 7 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #define _GNU_SOURCE |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 16 | #define _POSIX_SOURCE |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 17 | |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <pwd.h> |
| 22 | #include <shadow.h> |
| 23 | #include <crypt.h> |
| 24 | #include <errno.h> |
| 25 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 26 | #include "session_server.h" |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 27 | #include "session_server_ch.h" |
| 28 | #include "libnetconf.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 29 | |
| 30 | extern struct nc_server_opts server_opts; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 31 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 32 | static char * |
| 33 | base64der_key_to_tmp_file(const char *in, int rsa) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 34 | { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 35 | char path[12] = "/tmp/XXXXXX"; |
| 36 | int fd, written; |
| 37 | FILE *file; |
| 38 | |
| 39 | if (in == NULL) { |
| 40 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 43 | fd = mkstemp(path); |
| 44 | if (fd == -1) { |
| 45 | return NULL; |
| 46 | } |
| 47 | |
| 48 | file = fdopen(fd, "r"); |
| 49 | if (!file) { |
| 50 | close(fd); |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | /* write the key into the file */ |
| 55 | written = fwrite("-----BEGIN ", 1, 11, file); |
| 56 | written += fwrite((rsa ? "RSA" : "DSA"), 1, 3, file); |
| 57 | written += fwrite(" PRIVATE KEY-----\n", 1, 18, file); |
| 58 | written += fwrite(in, 1, strlen(in), file); |
| 59 | written += fwrite("\n-----END ", 1, 10, file); |
| 60 | written += fwrite((rsa ? "RSA" : "DSA"), 1, 3, file); |
| 61 | written += fwrite(" PRIVATE KEY-----", 1, 17, file); |
| 62 | |
| 63 | fclose(file); |
| 64 | if ((unsigned)written != 62 + strlen(in)) { |
| 65 | unlink(path); |
| 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | return strdup(path); |
| 70 | } |
| 71 | |
| 72 | static int |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 73 | nc_server_ssh_add_hostkey(const char *name, int16_t idx, struct nc_server_ssh_opts *opts) |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 74 | { |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame^] | 75 | uint8_t i; |
| 76 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 77 | if (!name) { |
| 78 | ERRARG("name"); |
Michal Vasko | 5fcc714 | 2016-02-02 12:21:10 +0100 | [diff] [blame] | 79 | return -1; |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 80 | } else if (idx > opts->hostkey_count) { |
| 81 | ERRARG("idx"); |
| 82 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 83 | } |
Michal Vasko | d45e25a | 2016-01-08 15:48:44 +0100 | [diff] [blame] | 84 | |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame^] | 85 | for (i = 0; i < opts->hostkey_count; ++i) { |
| 86 | if (!strcmp(opts->hostkeys[i], name)) { |
| 87 | ERRARG("name"); |
| 88 | return -1; |
| 89 | } |
| 90 | } |
| 91 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 92 | ++opts->hostkey_count; |
| 93 | opts->hostkeys = nc_realloc(opts->hostkeys, opts->hostkey_count * sizeof *opts->hostkeys); |
| 94 | if (!opts->hostkeys) { |
| 95 | ERRMEM; |
| 96 | return -1; |
| 97 | } |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 98 | |
| 99 | if (idx < 0) { |
| 100 | idx = opts->hostkey_count - 1; |
| 101 | } |
| 102 | if (idx != opts->hostkey_count - 1) { |
| 103 | memmove(opts->hostkeys + idx + 1, opts->hostkeys + idx, opts->hostkey_count - idx); |
| 104 | } |
| 105 | opts->hostkeys[idx] = lydict_insert(server_opts.ctx, name, 0); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 106 | |
Michal Vasko | 5fcc714 | 2016-02-02 12:21:10 +0100 | [diff] [blame] | 107 | return 0; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | API int |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 111 | nc_server_ssh_endpt_add_hostkey(const char *endpt_name, const char *name, int16_t idx) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 112 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 113 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 114 | struct nc_endpt *endpt; |
| 115 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 116 | /* LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 117 | endpt = nc_server_endpt_lock(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 118 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 119 | return -1; |
| 120 | } |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 121 | ret = nc_server_ssh_add_hostkey(name, idx, endpt->opts.ssh); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 122 | /* UNLOCK */ |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 123 | nc_server_endpt_unlock(endpt); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 124 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 125 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | API int |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 129 | nc_server_ssh_ch_client_add_hostkey(const char *client_name, const char *name, int16_t idx) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 130 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 131 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 132 | struct nc_ch_client *client; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 133 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 134 | /* LOCK */ |
| 135 | client = nc_server_ch_client_lock(client_name, NC_TI_LIBSSH, NULL); |
| 136 | if (!client) { |
| 137 | return -1; |
| 138 | } |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 139 | ret = nc_server_ssh_add_hostkey(name, idx, client->opts.ssh); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 140 | /* UNLOCK */ |
| 141 | nc_server_ch_client_unlock(client); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 142 | |
| 143 | return ret; |
| 144 | } |
| 145 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 146 | API void |
| 147 | nc_server_ssh_set_hostkey_clb(int (*hostkey_clb)(const char *name, void *user_data, char **privkey_path, |
| 148 | char **privkey_data, int *privkey_data_rsa), |
| 149 | void *user_data, void (*free_user_data)(void *user_data)) |
| 150 | { |
| 151 | if (!hostkey_clb) { |
| 152 | ERRARG("hostkey_clb"); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | server_opts.hostkey_clb = hostkey_clb; |
| 157 | server_opts.hostkey_data = user_data; |
| 158 | server_opts.hostkey_data_free = free_user_data; |
| 159 | } |
| 160 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 161 | static int |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 162 | nc_server_ssh_del_hostkey(const char *name, int16_t idx, struct nc_server_ssh_opts *opts) |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 163 | { |
| 164 | uint8_t i; |
| 165 | |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 166 | if (name && (idx > -1)) { |
| 167 | ERRARG("name and idx"); |
| 168 | return -1; |
| 169 | } else if (idx >= opts->hostkey_count) { |
| 170 | ERRARG("idx"); |
| 171 | } |
| 172 | |
| 173 | if (!name && (idx < 0)) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 174 | for (i = 0; i < opts->hostkey_count; ++i) { |
| 175 | lydict_remove(server_opts.ctx, opts->hostkeys[i]); |
| 176 | } |
| 177 | free(opts->hostkeys); |
| 178 | opts->hostkeys = NULL; |
| 179 | opts->hostkey_count = 0; |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 180 | } else if (name) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 181 | for (i = 0; i < opts->hostkey_count; ++i) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 182 | if (!strcmp(opts->hostkeys[i], name)) { |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 183 | idx = i; |
| 184 | goto remove_idx; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 188 | ERRARG("name"); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 189 | return -1; |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 190 | } else { |
| 191 | remove_idx: |
| 192 | --opts->hostkey_count; |
| 193 | lydict_remove(server_opts.ctx, opts->hostkeys[idx]); |
| 194 | if (idx < opts->hostkey_count - 1) { |
| 195 | memmove(opts->hostkeys + idx, opts->hostkeys + idx + 1, (opts->hostkey_count - idx) * sizeof *opts->hostkeys); |
| 196 | } |
| 197 | if (!opts->hostkey_count) { |
| 198 | free(opts->hostkeys); |
| 199 | opts->hostkeys = NULL; |
| 200 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | API int |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 207 | nc_server_ssh_endpt_del_hostkey(const char *endpt_name, const char *name, int16_t idx) |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 208 | { |
| 209 | int ret; |
| 210 | struct nc_endpt *endpt; |
| 211 | |
| 212 | /* LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 213 | endpt = nc_server_endpt_lock(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 214 | if (!endpt) { |
| 215 | return -1; |
| 216 | } |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 217 | ret = nc_server_ssh_del_hostkey(name, idx, endpt->opts.ssh); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 218 | /* UNLOCK */ |
| 219 | nc_server_endpt_unlock(endpt); |
| 220 | |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | API int |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 225 | nc_server_ssh_ch_client_del_hostkey(const char *client_name, const char *name, int16_t idx) |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 226 | { |
| 227 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 228 | struct nc_ch_client *client; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 229 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 230 | /* LOCK */ |
| 231 | client = nc_server_ch_client_lock(client_name, NC_TI_LIBSSH, NULL); |
| 232 | if (!client) { |
| 233 | return -1; |
| 234 | } |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 235 | ret = nc_server_ssh_del_hostkey(name, idx, client->opts.ssh); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 236 | /* UNLOCK */ |
| 237 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 238 | |
| 239 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static int |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame^] | 243 | nc_server_ssh_mov_hostkey(const char *key_mov, const char *key_after, struct nc_server_ssh_opts *opts) |
| 244 | { |
| 245 | uint8_t i; |
| 246 | int16_t mov_idx = -1, after_idx = -1; |
| 247 | const char *bckup; |
| 248 | |
| 249 | if (!key_mov) { |
| 250 | ERRARG("key_mov"); |
| 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | for (i = 0; i < opts->hostkey_count; ++i) { |
| 255 | if (key_after && (after_idx == -1) && !strcmp(opts->hostkeys[i], key_after)) { |
| 256 | after_idx = i; |
| 257 | } |
| 258 | if ((mov_idx == -1) && !strcmp(opts->hostkeys[i], key_mov)) { |
| 259 | mov_idx = i; |
| 260 | } |
| 261 | |
| 262 | if ((!key_after || (after_idx > -1)) && (mov_idx > -1)) { |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (key_after && (after_idx == -1)) { |
| 268 | ERRARG("key_after"); |
| 269 | return -1; |
| 270 | } |
| 271 | if (mov_idx == -1) { |
| 272 | ERRARG("key_mov"); |
| 273 | return -1; |
| 274 | } |
| 275 | if ((mov_idx == after_idx) || (mov_idx == after_idx + 1)) { |
| 276 | /* nothing to do */ |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | /* finally move the key */ |
| 281 | bckup = opts->hostkeys[mov_idx]; |
| 282 | if (mov_idx > after_idx) { |
| 283 | memmove(opts->hostkeys + after_idx + 2, opts->hostkeys + after_idx + 1, |
| 284 | ((mov_idx - after_idx) - 1) * sizeof *opts->hostkeys); |
| 285 | opts->hostkeys[after_idx + 1] = bckup; |
| 286 | } else { |
| 287 | memmove(opts->hostkeys + mov_idx, opts->hostkeys + mov_idx + 1, (after_idx - mov_idx) * sizeof *opts->hostkeys); |
| 288 | opts->hostkeys[after_idx] = bckup; |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | API int |
| 295 | nc_server_ssh_endpt_mov_hostkey(const char *endpt_name, const char *key_mov, const char *key_after) |
| 296 | { |
| 297 | int ret; |
| 298 | struct nc_endpt *endpt; |
| 299 | |
| 300 | /* LOCK */ |
| 301 | endpt = nc_server_endpt_lock(endpt_name, NC_TI_LIBSSH, NULL); |
| 302 | if (!endpt) { |
| 303 | return -1; |
| 304 | } |
| 305 | ret = nc_server_ssh_mov_hostkey(key_mov, key_after, endpt->opts.ssh); |
| 306 | /* UNLOCK */ |
| 307 | nc_server_endpt_unlock(endpt); |
| 308 | |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | API int |
| 313 | nc_server_ssh_ch_client_mov_hostkey(const char *client_name, const char *key_mov, const char *key_after) |
| 314 | { |
| 315 | int ret; |
| 316 | struct nc_ch_client *client; |
| 317 | |
| 318 | /* LOCK */ |
| 319 | client = nc_server_ch_client_lock(client_name, NC_TI_LIBSSH, NULL); |
| 320 | if (!client) { |
| 321 | return -1; |
| 322 | } |
| 323 | ret = nc_server_ssh_mov_hostkey(key_mov, key_after, client->opts.ssh); |
| 324 | /* UNLOCK */ |
| 325 | nc_server_ch_client_unlock(client); |
| 326 | |
| 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | static int |
| 331 | nc_server_ssh_mod_hostkey(const char *name, const char *new_name, struct nc_server_ssh_opts *opts) |
| 332 | { |
| 333 | uint8_t i; |
| 334 | |
| 335 | if (!name) { |
| 336 | ERRARG("name"); |
| 337 | return -1; |
| 338 | } else if (!new_name) { |
| 339 | ERRARG("new_name"); |
| 340 | return -1; |
| 341 | } |
| 342 | |
| 343 | for (i = 0; i < opts->hostkey_count; ++i) { |
| 344 | if (!strcmp(opts->hostkeys[i], name)) { |
| 345 | lydict_remove(server_opts.ctx, opts->hostkeys[i]); |
| 346 | opts->hostkeys[i] = lydict_insert(server_opts.ctx, new_name, 0); |
| 347 | return 0; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | ERRARG("name"); |
| 352 | return -1; |
| 353 | } |
| 354 | |
| 355 | API int |
| 356 | nc_server_ssh_endpt_mod_hostkey(const char *endpt_name, const char *name, const char *new_name) |
| 357 | { |
| 358 | int ret; |
| 359 | struct nc_endpt *endpt; |
| 360 | |
| 361 | /* LOCK */ |
| 362 | endpt = nc_server_endpt_lock(endpt_name, NC_TI_LIBSSH, NULL); |
| 363 | if (!endpt) { |
| 364 | return -1; |
| 365 | } |
| 366 | ret = nc_server_ssh_mov_hostkey(name, new_name, endpt->opts.ssh); |
| 367 | /* UNLOCK */ |
| 368 | nc_server_endpt_unlock(endpt); |
| 369 | |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | API int |
| 374 | nc_server_ssh_ch_client_mod_hostkey(const char *client_name, const char *name, const char *new_name) |
| 375 | { |
| 376 | int ret; |
| 377 | struct nc_ch_client *client; |
| 378 | |
| 379 | /* LOCK */ |
| 380 | client = nc_server_ch_client_lock(client_name, NC_TI_LIBSSH, NULL); |
| 381 | if (!client) { |
| 382 | return -1; |
| 383 | } |
| 384 | ret = nc_server_ssh_mod_hostkey(name, new_name, client->opts.ssh); |
| 385 | /* UNLOCK */ |
| 386 | nc_server_ch_client_unlock(client); |
| 387 | |
| 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | static int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 392 | nc_server_ssh_set_banner(const char *banner, struct nc_server_ssh_opts *opts) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 393 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 394 | if (!banner) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 395 | ERRARG("banner"); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 396 | return -1; |
| 397 | } |
| 398 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 399 | if (opts->banner) { |
| 400 | lydict_remove(server_opts.ctx, opts->banner); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 401 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 402 | opts->banner = lydict_insert(server_opts.ctx, banner, 0); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 403 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 407 | nc_server_ssh_endpt_set_banner(const char *endpt_name, const char *banner) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 408 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 409 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 410 | struct nc_endpt *endpt; |
| 411 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 412 | /* LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 413 | endpt = nc_server_endpt_lock(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 414 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 415 | return -1; |
| 416 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 417 | ret = nc_server_ssh_set_banner(banner, endpt->opts.ssh); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 418 | /* UNLOCK */ |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 419 | nc_server_endpt_unlock(endpt); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 420 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 421 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 425 | nc_server_ssh_ch_client_set_banner(const char *client_name, const char *banner) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 426 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 427 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 428 | struct nc_ch_client *client; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 429 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 430 | /* LOCK */ |
| 431 | client = nc_server_ch_client_lock(client_name, NC_TI_LIBSSH, NULL); |
| 432 | if (!client) { |
| 433 | return -1; |
| 434 | } |
| 435 | ret = nc_server_ssh_set_banner(banner, client->opts.ssh); |
| 436 | /* UNLOCK */ |
| 437 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 438 | |
| 439 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | static int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 443 | nc_server_ssh_set_auth_methods(int auth_methods, struct nc_server_ssh_opts *opts) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 444 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 445 | if (!(auth_methods & NC_SSH_AUTH_PUBLICKEY) && !(auth_methods & NC_SSH_AUTH_PASSWORD) |
| 446 | && !(auth_methods & NC_SSH_AUTH_INTERACTIVE)) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 447 | ERRARG("auth_methods"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 448 | return -1; |
| 449 | } |
| 450 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 451 | opts->auth_methods = auth_methods; |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 456 | nc_server_ssh_endpt_set_auth_methods(const char *endpt_name, int auth_methods) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 457 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 458 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 459 | struct nc_endpt *endpt; |
| 460 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 461 | /* LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 462 | endpt = nc_server_endpt_lock(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 463 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 464 | return -1; |
| 465 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 466 | ret = nc_server_ssh_set_auth_methods(auth_methods, endpt->opts.ssh); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 467 | /* UNLOCK */ |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 468 | nc_server_endpt_unlock(endpt); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 469 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 470 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 474 | nc_server_ssh_ch_client_set_auth_methods(const char *client_name, int auth_methods) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 475 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 476 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 477 | struct nc_ch_client *client; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 478 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 479 | /* LOCK */ |
| 480 | client = nc_server_ch_client_lock(client_name, NC_TI_LIBSSH, NULL); |
| 481 | if (!client) { |
| 482 | return -1; |
| 483 | } |
| 484 | ret = nc_server_ssh_set_auth_methods(auth_methods, client->opts.ssh); |
| 485 | /* UNLOCK */ |
| 486 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 487 | |
| 488 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | static int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 492 | nc_server_ssh_set_auth_attempts(uint16_t auth_attempts, struct nc_server_ssh_opts *opts) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 493 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 494 | if (!auth_attempts) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 495 | ERRARG("auth_attempts"); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 496 | return -1; |
| 497 | } |
| 498 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 499 | opts->auth_attempts = auth_attempts; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 504 | nc_server_ssh_endpt_set_auth_attempts(const char *endpt_name, uint16_t auth_attempts) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 505 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 506 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 507 | struct nc_endpt *endpt; |
| 508 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 509 | /* LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 510 | endpt = nc_server_endpt_lock(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 511 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 512 | return -1; |
| 513 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 514 | ret = nc_server_ssh_set_auth_attempts(auth_attempts, endpt->opts.ssh); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 515 | /* UNLOCK */ |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 516 | nc_server_endpt_unlock(endpt); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 517 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 518 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 522 | nc_server_ssh_set_ch_client_auth_attempts(const char *client_name, uint16_t auth_attempts) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 523 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 524 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 525 | struct nc_ch_client *client; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 526 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 527 | /* LOCK */ |
| 528 | client = nc_server_ch_client_lock(client_name, NC_TI_LIBSSH, NULL); |
| 529 | if (!client) { |
| 530 | return -1; |
| 531 | } |
| 532 | ret = nc_server_ssh_set_auth_attempts(auth_attempts, client->opts.ssh); |
| 533 | /* UNLOCK */ |
| 534 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 535 | |
| 536 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 540 | nc_server_ssh_set_auth_timeout(uint16_t auth_timeout, struct nc_server_ssh_opts *opts) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 541 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 542 | if (!auth_timeout) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 543 | ERRARG("auth_timeout"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 544 | return -1; |
| 545 | } |
| 546 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 547 | opts->auth_timeout = auth_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 552 | nc_server_ssh_endpt_set_auth_timeout(const char *endpt_name, uint16_t auth_timeout) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 553 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 554 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 555 | struct nc_endpt *endpt; |
| 556 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 557 | /* LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 558 | endpt = nc_server_endpt_lock(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 559 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 560 | return -1; |
| 561 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 562 | ret = nc_server_ssh_set_auth_timeout(auth_timeout, endpt->opts.ssh); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 563 | /* UNLOCK */ |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 564 | nc_server_endpt_unlock(endpt); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 565 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 566 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 570 | nc_server_ssh_ch_client_set_auth_timeout(const char *client_name, uint16_t auth_timeout) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 571 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 572 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 573 | struct nc_ch_client *client; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 574 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 575 | /* LOCK */ |
| 576 | client = nc_server_ch_client_lock(client_name, NC_TI_LIBSSH, NULL); |
| 577 | if (!client) { |
| 578 | return -1; |
| 579 | } |
| 580 | ret = nc_server_ssh_set_auth_timeout(auth_timeout, client->opts.ssh); |
| 581 | /* UNLOCK */ |
| 582 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 583 | |
| 584 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | static int |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 588 | _nc_server_ssh_add_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type, |
| 589 | const char *username) |
| 590 | { |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 591 | /* LOCK */ |
| 592 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 593 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 594 | ++server_opts.authkey_count; |
| 595 | server_opts.authkeys = nc_realloc(server_opts.authkeys, server_opts.authkey_count * sizeof *server_opts.authkeys); |
| 596 | if (!server_opts.authkeys) { |
| 597 | ERRMEM; |
| 598 | return -1; |
| 599 | } |
| 600 | server_opts.authkeys[server_opts.authkey_count - 1].path = lydict_insert(server_opts.ctx, pubkey_path, 0); |
| 601 | server_opts.authkeys[server_opts.authkey_count - 1].base64 = lydict_insert(server_opts.ctx, pubkey_base64, 0); |
| 602 | server_opts.authkeys[server_opts.authkey_count - 1].type = type; |
| 603 | server_opts.authkeys[server_opts.authkey_count - 1].username = lydict_insert(server_opts.ctx, username, 0); |
| 604 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 605 | /* UNLOCK */ |
| 606 | pthread_mutex_unlock(&server_opts.authkey_lock); |
| 607 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | API int |
| 612 | nc_server_ssh_add_authkey_path(const char *pubkey_path, const char *username) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 613 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 614 | if (!pubkey_path) { |
| 615 | ERRARG("pubkey_path"); |
| 616 | return -1; |
| 617 | } else if (!username) { |
| 618 | ERRARG("username"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 619 | return -1; |
| 620 | } |
| 621 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 622 | return _nc_server_ssh_add_authkey(pubkey_path, NULL, 0, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | API int |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 626 | nc_server_ssh_add_authkey(const char *pubkey_base64, NC_SSH_KEY_TYPE type, const char *username) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 627 | { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 628 | if (!pubkey_base64) { |
| 629 | ERRARG("pubkey_base64"); |
| 630 | return -1; |
| 631 | } else if (!type) { |
| 632 | ERRARG("type"); |
| 633 | return -1; |
| 634 | } else if (!username) { |
| 635 | ERRARG("username"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 636 | return -1; |
| 637 | } |
| 638 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 639 | return _nc_server_ssh_add_authkey(NULL, pubkey_base64, type, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | API int |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 643 | nc_server_ssh_del_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type, |
| 644 | const char *username) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 645 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 646 | uint32_t i; |
| 647 | int ret = -1; |
| 648 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 649 | /* LOCK */ |
| 650 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 651 | |
| 652 | if (!pubkey_path && !pubkey_base64 && !type && !username) { |
| 653 | for (i = 0; i < server_opts.authkey_count; ++i) { |
| 654 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].path); |
| 655 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].base64); |
| 656 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 657 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 658 | ret = 0; |
| 659 | } |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 660 | free(server_opts.authkeys); |
| 661 | server_opts.authkeys = NULL; |
| 662 | server_opts.authkey_count = 0; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 663 | } else { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 664 | for (i = 0; i < server_opts.authkey_count; ++i) { |
| 665 | if ((!pubkey_path || !strcmp(server_opts.authkeys[i].path, pubkey_path)) |
| 666 | && (!pubkey_base64 || strcmp(server_opts.authkeys[i].base64, pubkey_base64)) |
| 667 | && (!type || (server_opts.authkeys[i].type == type)) |
| 668 | && (!username || !strcmp(server_opts.authkeys[i].username, username))) { |
| 669 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].path); |
| 670 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].base64); |
| 671 | lydict_remove(server_opts.ctx, server_opts.authkeys[i].username); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 672 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 673 | --server_opts.authkey_count; |
| 674 | if (i < server_opts.authkey_count) { |
| 675 | memcpy(&server_opts.authkeys[i], &server_opts.authkeys[server_opts.authkey_count], |
| 676 | sizeof *server_opts.authkeys); |
| 677 | } else if (!server_opts.authkey_count) { |
| 678 | free(server_opts.authkeys); |
| 679 | server_opts.authkeys = NULL; |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 680 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 681 | |
| 682 | ret = 0; |
| 683 | } |
| 684 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 685 | } |
| 686 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 687 | /* UNLOCK */ |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 688 | pthread_mutex_unlock(&server_opts.authkey_lock); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 689 | |
| 690 | return ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | void |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 694 | nc_server_ssh_clear_opts(struct nc_server_ssh_opts *opts) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 695 | { |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 696 | nc_server_ssh_del_hostkey(NULL, -1, opts); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 697 | if (opts->banner) { |
| 698 | lydict_remove(server_opts.ctx, opts->banner); |
| 699 | opts->banner = NULL; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 700 | } |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 701 | } |
| 702 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 703 | static char * |
| 704 | auth_password_get_pwd_hash(const char *username) |
| 705 | { |
| 706 | struct passwd *pwd, pwd_buf; |
| 707 | struct spwd *spwd, spwd_buf; |
| 708 | char *pass_hash = NULL, buf[256]; |
| 709 | |
| 710 | getpwnam_r(username, &pwd_buf, buf, 256, &pwd); |
| 711 | if (!pwd) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 712 | VRB("User \"%s\" not found locally.", username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 713 | return NULL; |
| 714 | } |
| 715 | |
| 716 | if (!strcmp(pwd->pw_passwd, "x")) { |
| 717 | getspnam_r(username, &spwd_buf, buf, 256, &spwd); |
| 718 | if (!spwd) { |
| 719 | VRB("Failed to retrieve the shadow entry for \"%s\".", username); |
| 720 | return NULL; |
| 721 | } |
| 722 | |
| 723 | pass_hash = spwd->sp_pwdp; |
| 724 | } else { |
| 725 | pass_hash = pwd->pw_passwd; |
| 726 | } |
| 727 | |
| 728 | if (!pass_hash) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 729 | ERR("No password could be retrieved for \"%s\".", username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 730 | return NULL; |
| 731 | } |
| 732 | |
| 733 | /* check the hash structure for special meaning */ |
| 734 | if (!strcmp(pass_hash, "*") || !strcmp(pass_hash, "!")) { |
| 735 | VRB("User \"%s\" is not allowed to authenticate using a password.", username); |
| 736 | return NULL; |
| 737 | } |
| 738 | if (!strcmp(pass_hash, "*NP*")) { |
| 739 | VRB("Retrieving password for \"%s\" from a NIS+ server not supported.", username); |
| 740 | return NULL; |
| 741 | } |
| 742 | |
| 743 | return strdup(pass_hash); |
| 744 | } |
| 745 | |
| 746 | static int |
| 747 | auth_password_compare_pwd(const char *pass_hash, const char *pass_clear) |
| 748 | { |
| 749 | char *new_pass_hash; |
| 750 | struct crypt_data cdata; |
| 751 | |
| 752 | if (!pass_hash[0]) { |
| 753 | if (!pass_clear[0]) { |
| 754 | WRN("User authentication successful with an empty password!"); |
| 755 | return 0; |
| 756 | } else { |
| 757 | /* the user did now know he does not need any password, |
| 758 | * (which should not be used) so deny authentication */ |
| 759 | return 1; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | cdata.initialized = 0; |
| 764 | new_pass_hash = crypt_r(pass_clear, pass_hash, &cdata); |
| 765 | return strcmp(new_pass_hash, pass_hash); |
| 766 | } |
| 767 | |
| 768 | static void |
| 769 | nc_sshcb_auth_password(struct nc_session *session, ssh_message msg) |
| 770 | { |
| 771 | char *pass_hash; |
| 772 | |
| 773 | pass_hash = auth_password_get_pwd_hash(session->username); |
| 774 | if (pass_hash && !auth_password_compare_pwd(pass_hash, ssh_message_auth_password(msg))) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 775 | VRB("User \"%s\" authenticated.", session->username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 776 | ssh_message_auth_reply_success(msg, 0); |
| 777 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 778 | free(pass_hash); |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | free(pass_hash); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 783 | ++session->opts.server.ssh_auth_attempts; |
| 784 | VRB("Failed user \"%s\" authentication attempt (#%d).", session->username, session->opts.server.ssh_auth_attempts); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 785 | ssh_message_reply_default(msg); |
| 786 | } |
| 787 | |
| 788 | static void |
| 789 | nc_sshcb_auth_kbdint(struct nc_session *session, ssh_message msg) |
| 790 | { |
| 791 | char *pass_hash; |
| 792 | |
| 793 | if (!ssh_message_auth_kbdint_is_response(msg)) { |
| 794 | const char *prompts[] = {"Password: "}; |
| 795 | char echo[] = {0}; |
| 796 | |
| 797 | ssh_message_auth_interactive_request(msg, "Interactive SSH Authentication", "Type your password:", 1, prompts, echo); |
| 798 | } else { |
| 799 | if (ssh_userauth_kbdint_getnanswers(session->ti.libssh.session) != 1) { |
| 800 | ssh_message_reply_default(msg); |
| 801 | return; |
| 802 | } |
| 803 | pass_hash = auth_password_get_pwd_hash(session->username); |
| 804 | if (!pass_hash) { |
| 805 | ssh_message_reply_default(msg); |
| 806 | return; |
| 807 | } |
| 808 | if (!auth_password_compare_pwd(pass_hash, ssh_userauth_kbdint_getanswer(session->ti.libssh.session, 0))) { |
| 809 | VRB("User \"%s\" authenticated.", session->username); |
| 810 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 811 | ssh_message_auth_reply_success(msg, 0); |
| 812 | } else { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 813 | ++session->opts.server.ssh_auth_attempts; |
| 814 | VRB("Failed user \"%s\" authentication attempt (#%d).", session->username, session->opts.server.ssh_auth_attempts); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 815 | ssh_message_reply_default(msg); |
| 816 | } |
Radek Krejci | fb53374 | 2016-03-04 15:12:54 +0100 | [diff] [blame] | 817 | free(pass_hash); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
| 821 | static const char * |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 822 | auth_pubkey_compare_key(ssh_key key) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 823 | { |
| 824 | uint32_t i; |
| 825 | ssh_key pub_key; |
Michal Vasko | 76e3a35 | 2016-01-18 09:07:00 +0100 | [diff] [blame] | 826 | const char *username = NULL; |
Michal Vasko | 7abcdeb | 2016-05-30 15:27:00 +0200 | [diff] [blame] | 827 | int ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 828 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 829 | /* LOCK */ |
| 830 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 831 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 832 | for (i = 0; i < server_opts.authkey_count; ++i) { |
| 833 | switch (server_opts.authkeys[i].type) { |
| 834 | case NC_SSH_KEY_UNKNOWN: |
| 835 | ret = ssh_pki_import_pubkey_file(server_opts.authkeys[i].path, &pub_key); |
| 836 | break; |
| 837 | case NC_SSH_KEY_DSA: |
| 838 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_DSS, &pub_key); |
| 839 | break; |
| 840 | case NC_SSH_KEY_RSA: |
| 841 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_RSA, &pub_key); |
| 842 | break; |
| 843 | case NC_SSH_KEY_ECDSA: |
| 844 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_ECDSA, &pub_key); |
| 845 | break; |
| 846 | } |
| 847 | |
Michal Vasko | 7abcdeb | 2016-05-30 15:27:00 +0200 | [diff] [blame] | 848 | if (ret == SSH_EOF) { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 849 | WRN("Failed to import a public key of \"%s\" (File access problem).", server_opts.authkeys[i].username); |
Michal Vasko | 7abcdeb | 2016-05-30 15:27:00 +0200 | [diff] [blame] | 850 | continue; |
| 851 | } else if (ret == SSH_ERROR) { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 852 | WRN("Failed to import a public key of \"%s\" (SSH error).", server_opts.authkeys[i].username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 853 | continue; |
| 854 | } |
| 855 | |
| 856 | if (!ssh_key_cmp(key, pub_key, SSH_KEY_CMP_PUBLIC)) { |
| 857 | ssh_key_free(pub_key); |
| 858 | break; |
| 859 | } |
| 860 | |
| 861 | ssh_key_free(pub_key); |
| 862 | } |
| 863 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 864 | if (i < server_opts.authkey_count) { |
| 865 | username = server_opts.authkeys[i].username; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 866 | } |
| 867 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 868 | /* UNLOCK */ |
| 869 | pthread_mutex_unlock(&server_opts.authkey_lock); |
| 870 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 871 | return username; |
| 872 | } |
| 873 | |
| 874 | static void |
| 875 | nc_sshcb_auth_pubkey(struct nc_session *session, ssh_message msg) |
| 876 | { |
| 877 | const char *username; |
| 878 | int signature_state; |
| 879 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 880 | if ((username = auth_pubkey_compare_key(ssh_message_auth_pubkey(msg))) == NULL) { |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 881 | VRB("User \"%s\" tried to use an unknown (unauthorized) public key.", session->username); |
| 882 | goto fail; |
| 883 | } else if (strcmp(session->username, username)) { |
| 884 | VRB("User \"%s\" is not the username identified with the presented public key.", session->username); |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 885 | goto fail; |
| 886 | } |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 887 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 888 | signature_state = ssh_message_auth_publickey_state(msg); |
| 889 | if (signature_state == SSH_PUBLICKEY_STATE_VALID) { |
| 890 | VRB("User \"%s\" authenticated.", session->username); |
| 891 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 892 | ssh_message_auth_reply_success(msg, 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 893 | } else if (signature_state == SSH_PUBLICKEY_STATE_NONE) { |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 894 | /* accepting only the use of a public key */ |
| 895 | ssh_message_auth_reply_pk_ok_simple(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 896 | } |
| 897 | |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 898 | return; |
| 899 | |
| 900 | fail: |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 901 | ++session->opts.server.ssh_auth_attempts; |
| 902 | VRB("Failed user \"%s\" authentication attempt (#%d).", session->username, session->opts.server.ssh_auth_attempts); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 903 | ssh_message_reply_default(msg); |
| 904 | } |
| 905 | |
| 906 | static int |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 907 | nc_sshcb_channel_open(struct nc_session *session, ssh_message msg) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 908 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 909 | ssh_channel chan; |
| 910 | |
| 911 | /* first channel request */ |
| 912 | if (!session->ti.libssh.channel) { |
| 913 | if (session->status != NC_STATUS_STARTING) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 914 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 915 | return -1; |
| 916 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 917 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 918 | if (!chan) { |
| 919 | ERR("Failed to create a new SSH channel."); |
| 920 | return -1; |
| 921 | } |
| 922 | session->ti.libssh.channel = chan; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 923 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 924 | /* additional channel request */ |
| 925 | } else { |
| 926 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 927 | if (!chan) { |
| 928 | ERR("Session %u: failed to create a new SSH channel.", session->id); |
| 929 | return -1; |
| 930 | } |
| 931 | /* channel was created and libssh stored it internally in the ssh_session structure, good enough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 932 | } |
| 933 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | static int |
| 938 | nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem) |
| 939 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 940 | struct nc_session *new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 941 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 942 | if (strcmp(subsystem, "netconf")) { |
| 943 | WRN("Received an unknown subsystem \"%s\" request.", subsystem); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 944 | return -1; |
| 945 | } |
| 946 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 947 | if (session->ti.libssh.channel == channel) { |
| 948 | /* first channel requested */ |
| 949 | if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) { |
| 950 | ERRINT; |
| 951 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 952 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 953 | if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) { |
| 954 | ERR("Session %u: subsystem \"netconf\" requested for the second time.", session->id); |
| 955 | return -1; |
| 956 | } |
| 957 | |
| 958 | session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 959 | } else { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 960 | /* additional channel subsystem request, new session is ready as far as SSH is concerned */ |
| 961 | new_session = calloc(1, sizeof *new_session); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 962 | if (!new_session) { |
| 963 | ERRMEM; |
| 964 | return -1; |
| 965 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 966 | |
| 967 | /* insert the new session */ |
| 968 | if (!session->ti.libssh.next) { |
| 969 | new_session->ti.libssh.next = session; |
| 970 | } else { |
| 971 | new_session->ti.libssh.next = session->ti.libssh.next; |
| 972 | } |
| 973 | session->ti.libssh.next = new_session; |
| 974 | |
| 975 | new_session->status = NC_STATUS_STARTING; |
| 976 | new_session->side = NC_SERVER; |
| 977 | new_session->ti_type = NC_TI_LIBSSH; |
| 978 | new_session->ti_lock = session->ti_lock; |
| 979 | new_session->ti.libssh.channel = channel; |
| 980 | new_session->ti.libssh.session = session->ti.libssh.session; |
| 981 | new_session->username = lydict_insert(server_opts.ctx, session->username, 0); |
| 982 | new_session->host = lydict_insert(server_opts.ctx, session->host, 0); |
| 983 | new_session->port = session->port; |
| 984 | new_session->ctx = server_opts.ctx; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 985 | new_session->flags = NC_SESSION_SSH_AUTHENTICATED | NC_SESSION_SSH_SUBSYS_NETCONF | NC_SESSION_SHAREDCTX |
| 986 | | (session->flags & NC_SESSION_CALLHOME ? NC_SESSION_CALLHOME : 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | return 0; |
| 990 | } |
| 991 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 992 | int |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 993 | nc_sshcb_msg(ssh_session UNUSED(sshsession), ssh_message msg, void *data) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 994 | { |
| 995 | const char *str_type, *str_subtype = NULL, *username; |
| 996 | int subtype, type; |
| 997 | struct nc_session *session = (struct nc_session *)data; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 998 | |
| 999 | type = ssh_message_type(msg); |
| 1000 | subtype = ssh_message_subtype(msg); |
| 1001 | |
| 1002 | switch (type) { |
| 1003 | case SSH_REQUEST_AUTH: |
| 1004 | str_type = "request-auth"; |
| 1005 | switch (subtype) { |
| 1006 | case SSH_AUTH_METHOD_NONE: |
| 1007 | str_subtype = "none"; |
| 1008 | break; |
| 1009 | case SSH_AUTH_METHOD_PASSWORD: |
| 1010 | str_subtype = "password"; |
| 1011 | break; |
| 1012 | case SSH_AUTH_METHOD_PUBLICKEY: |
| 1013 | str_subtype = "publickey"; |
| 1014 | break; |
| 1015 | case SSH_AUTH_METHOD_HOSTBASED: |
| 1016 | str_subtype = "hostbased"; |
| 1017 | break; |
| 1018 | case SSH_AUTH_METHOD_INTERACTIVE: |
| 1019 | str_subtype = "interactive"; |
| 1020 | break; |
| 1021 | case SSH_AUTH_METHOD_GSSAPI_MIC: |
| 1022 | str_subtype = "gssapi-mic"; |
| 1023 | break; |
| 1024 | } |
| 1025 | break; |
| 1026 | |
| 1027 | case SSH_REQUEST_CHANNEL_OPEN: |
| 1028 | str_type = "request-channel-open"; |
| 1029 | switch (subtype) { |
| 1030 | case SSH_CHANNEL_SESSION: |
| 1031 | str_subtype = "session"; |
| 1032 | break; |
| 1033 | case SSH_CHANNEL_DIRECT_TCPIP: |
| 1034 | str_subtype = "direct-tcpip"; |
| 1035 | break; |
| 1036 | case SSH_CHANNEL_FORWARDED_TCPIP: |
| 1037 | str_subtype = "forwarded-tcpip"; |
| 1038 | break; |
| 1039 | case (int)SSH_CHANNEL_X11: |
| 1040 | str_subtype = "channel-x11"; |
| 1041 | break; |
| 1042 | case SSH_CHANNEL_UNKNOWN: |
| 1043 | /* fallthrough */ |
| 1044 | default: |
| 1045 | str_subtype = "unknown"; |
| 1046 | break; |
| 1047 | } |
| 1048 | break; |
| 1049 | |
| 1050 | case SSH_REQUEST_CHANNEL: |
| 1051 | str_type = "request-channel"; |
| 1052 | switch (subtype) { |
| 1053 | case SSH_CHANNEL_REQUEST_PTY: |
| 1054 | str_subtype = "pty"; |
| 1055 | break; |
| 1056 | case SSH_CHANNEL_REQUEST_EXEC: |
| 1057 | str_subtype = "exec"; |
| 1058 | break; |
| 1059 | case SSH_CHANNEL_REQUEST_SHELL: |
| 1060 | str_subtype = "shell"; |
| 1061 | break; |
| 1062 | case SSH_CHANNEL_REQUEST_ENV: |
| 1063 | str_subtype = "env"; |
| 1064 | break; |
| 1065 | case SSH_CHANNEL_REQUEST_SUBSYSTEM: |
| 1066 | str_subtype = "subsystem"; |
| 1067 | break; |
| 1068 | case SSH_CHANNEL_REQUEST_WINDOW_CHANGE: |
| 1069 | str_subtype = "window-change"; |
| 1070 | break; |
| 1071 | case SSH_CHANNEL_REQUEST_X11: |
| 1072 | str_subtype = "x11"; |
| 1073 | break; |
| 1074 | case SSH_CHANNEL_REQUEST_UNKNOWN: |
| 1075 | /* fallthrough */ |
| 1076 | default: |
| 1077 | str_subtype = "unknown"; |
| 1078 | break; |
| 1079 | } |
| 1080 | break; |
| 1081 | |
| 1082 | case SSH_REQUEST_SERVICE: |
| 1083 | str_type = "request-service"; |
| 1084 | str_subtype = ssh_message_service_service(msg); |
| 1085 | break; |
| 1086 | |
| 1087 | case SSH_REQUEST_GLOBAL: |
| 1088 | str_type = "request-global"; |
| 1089 | switch (subtype) { |
| 1090 | case SSH_GLOBAL_REQUEST_TCPIP_FORWARD: |
| 1091 | str_subtype = "tcpip-forward"; |
| 1092 | break; |
| 1093 | case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD: |
| 1094 | str_subtype = "cancel-tcpip-forward"; |
| 1095 | break; |
| 1096 | case SSH_GLOBAL_REQUEST_UNKNOWN: |
| 1097 | /* fallthrough */ |
| 1098 | default: |
| 1099 | str_subtype = "unknown"; |
| 1100 | break; |
| 1101 | } |
| 1102 | break; |
| 1103 | |
| 1104 | default: |
| 1105 | str_type = "unknown"; |
| 1106 | str_subtype = "unknown"; |
| 1107 | break; |
| 1108 | } |
| 1109 | |
| 1110 | VRB("Received an SSH message \"%s\" of subtype \"%s\".", str_type, str_subtype); |
Michal Vasko | ce31916 | 2016-02-03 15:33:08 +0100 | [diff] [blame] | 1111 | if ((session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) { |
| 1112 | /* "valid" situation if, for example, receiving some auth or channel request timeouted, |
| 1113 | * but we got it now, during session free */ |
| 1114 | VRB("SSH message arrived on a %s session, the request will be denied.", |
| 1115 | (session->status == NC_STATUS_CLOSING ? "closing" : "invalid")); |
| 1116 | ssh_message_reply_default(msg); |
| 1117 | return 0; |
| 1118 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1119 | session->flags |= NC_SESSION_SSH_NEW_MSG; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1120 | |
| 1121 | /* |
| 1122 | * process known messages |
| 1123 | */ |
| 1124 | if (type == SSH_REQUEST_AUTH) { |
| 1125 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
| 1126 | ERR("User \"%s\" authenticated, but requested another authentication.", session->username); |
| 1127 | ssh_message_reply_default(msg); |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1131 | /* save the username, do not let the client change it */ |
| 1132 | username = ssh_message_auth_user(msg); |
| 1133 | if (!session->username) { |
| 1134 | if (!username) { |
| 1135 | ERR("Denying an auth request without a username."); |
| 1136 | return 1; |
| 1137 | } |
| 1138 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1139 | session->username = lydict_insert(server_opts.ctx, username, 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1140 | } else if (username) { |
| 1141 | if (strcmp(username, session->username)) { |
| 1142 | ERR("User \"%s\" changed its username to \"%s\".", session->username, username); |
| 1143 | session->status = NC_STATUS_INVALID; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1144 | session->term_reason = NC_SESSION_TERM_OTHER; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1145 | return 1; |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | if (subtype == SSH_AUTH_METHOD_NONE) { |
| 1150 | /* libssh will return the supported auth methods */ |
| 1151 | return 1; |
| 1152 | } else if (subtype == SSH_AUTH_METHOD_PASSWORD) { |
| 1153 | nc_sshcb_auth_password(session, msg); |
| 1154 | return 0; |
| 1155 | } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) { |
| 1156 | nc_sshcb_auth_pubkey(session, msg); |
| 1157 | return 0; |
| 1158 | } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) { |
| 1159 | nc_sshcb_auth_kbdint(session, msg); |
| 1160 | return 0; |
| 1161 | } |
| 1162 | } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1163 | if ((type == SSH_REQUEST_CHANNEL_OPEN) && ((enum ssh_channel_type_e)subtype == SSH_CHANNEL_SESSION)) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1164 | if (nc_sshcb_channel_open(session, msg)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1165 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1166 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1167 | return 0; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1168 | |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1169 | } else if ((type == SSH_REQUEST_CHANNEL) && ((enum ssh_channel_requests_e)subtype == SSH_CHANNEL_REQUEST_SUBSYSTEM)) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1170 | if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg), |
| 1171 | ssh_message_channel_request_subsystem(msg))) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1172 | ssh_message_reply_default(msg); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1173 | } else { |
| 1174 | ssh_message_channel_request_reply_success(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1175 | } |
| 1176 | return 0; |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | /* we did not process it */ |
| 1181 | return 1; |
| 1182 | } |
| 1183 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1184 | /* ret 1 on success, 0 on timeout, -1 on error */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1185 | static int |
| 1186 | nc_open_netconf_channel(struct nc_session *session, int timeout) |
| 1187 | { |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 1188 | int elapsed_usec = 0, ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1189 | |
| 1190 | /* message callback is executed twice to give chance for the channel to be |
| 1191 | * created if timeout == 0 (it takes 2 messages, channel-open, subsystem-request) */ |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1192 | if (!timeout) { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1193 | if (!nc_session_is_connected(session)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1194 | ERR("Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1195 | return -1; |
| 1196 | } |
| 1197 | |
Michal vasko | 50cc94f | 2016-10-04 13:46:20 +0200 | [diff] [blame] | 1198 | ret = nc_timedlock(session->ti_lock, timeout, __func__); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1199 | if (ret != 1) { |
| 1200 | return ret; |
| 1201 | } |
| 1202 | |
| 1203 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1204 | if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1205 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1206 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1207 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1208 | return -1; |
| 1209 | } |
| 1210 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1211 | if (!session->ti.libssh.channel) { |
| 1212 | /* we did not receive channel-open, timeout */ |
| 1213 | pthread_mutex_unlock(session->ti_lock); |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
| 1217 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1218 | if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1219 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1220 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1221 | pthread_mutex_unlock(session->ti_lock); |
| 1222 | return -1; |
| 1223 | } |
| 1224 | pthread_mutex_unlock(session->ti_lock); |
| 1225 | |
| 1226 | if (!(session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1227 | /* we did not receive subsystem-request, timeout */ |
| 1228 | return 0; |
| 1229 | } |
| 1230 | |
| 1231 | return 1; |
| 1232 | } |
| 1233 | |
| 1234 | while (1) { |
| 1235 | if (!nc_session_is_connected(session)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1236 | ERR("Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1237 | return -1; |
| 1238 | } |
| 1239 | |
Michal vasko | 50cc94f | 2016-10-04 13:46:20 +0200 | [diff] [blame] | 1240 | ret = nc_timedlock(session->ti_lock, timeout, __func__); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1241 | if (ret != 1) { |
| 1242 | return ret; |
| 1243 | } |
| 1244 | |
| 1245 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1246 | if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1247 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1248 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1249 | pthread_mutex_unlock(session->ti_lock); |
| 1250 | return -1; |
| 1251 | } |
| 1252 | |
| 1253 | pthread_mutex_unlock(session->ti_lock); |
| 1254 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1255 | if (session->ti.libssh.channel && (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1256 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1257 | } |
| 1258 | |
Michal Vasko | 105bf27 | 2016-02-03 15:34:35 +0100 | [diff] [blame] | 1259 | if ((timeout != -1) && (elapsed_usec / 1000 >= timeout)) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1260 | /* timeout */ |
Michal Vasko | 842522c | 2016-03-01 09:20:13 +0100 | [diff] [blame] | 1261 | ERR("Failed to start \"netconf\" SSH subsystem for too long, disconnecting."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1262 | break; |
| 1263 | } |
| 1264 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1265 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | 105bf27 | 2016-02-03 15:34:35 +0100 | [diff] [blame] | 1266 | elapsed_usec += NC_TIMEOUT_STEP; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1267 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1268 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1269 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1270 | } |
| 1271 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1272 | static int |
| 1273 | nc_ssh_bind_add_hostkeys(ssh_bind sbind, const char **hostkeys, uint8_t hostkey_count) |
| 1274 | { |
| 1275 | uint8_t i; |
| 1276 | char *privkey_path, *privkey_data; |
| 1277 | int privkey_data_rsa, ret; |
| 1278 | |
| 1279 | if (!server_opts.hostkey_clb) { |
| 1280 | ERR("Callback for retrieving SSH host keys not set."); |
| 1281 | return -1; |
| 1282 | } |
| 1283 | |
| 1284 | for (i = 0; i < hostkey_count; ++i) { |
| 1285 | privkey_path = privkey_data = NULL; |
| 1286 | if (server_opts.hostkey_clb(hostkeys[i], server_opts.hostkey_data, &privkey_path, &privkey_data, &privkey_data_rsa)) { |
| 1287 | ERR("Host key callback failed."); |
| 1288 | return -1; |
| 1289 | } |
| 1290 | |
| 1291 | if (privkey_data) { |
| 1292 | privkey_path = base64der_key_to_tmp_file(privkey_data, privkey_data_rsa); |
| 1293 | if (!privkey_path) { |
| 1294 | ERR("Temporarily storing a host key into a file failed (%s).", strerror(errno)); |
| 1295 | free(privkey_data); |
| 1296 | return -1; |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path); |
| 1301 | |
| 1302 | /* cleanup */ |
| 1303 | if (privkey_data && unlink(privkey_path)) { |
| 1304 | WRN("Removing a temporary host key file \"%s\" failed (%s).", privkey_path, strerror(errno)); |
| 1305 | } |
| 1306 | free(privkey_path); |
| 1307 | free(privkey_data); |
| 1308 | |
| 1309 | if (ret != SSH_OK) { |
| 1310 | ERR("Failed to set hostkey \"%s\" (%s).", hostkeys[i], ssh_get_error(sbind)); |
| 1311 | return -1; |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1318 | int |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1319 | nc_accept_ssh_session(struct nc_session *session, int sock, int timeout) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1320 | { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1321 | ssh_bind sbind; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1322 | struct nc_server_ssh_opts *opts; |
Michal Vasko | 72387da | 2016-02-02 15:52:41 +0100 | [diff] [blame] | 1323 | int libssh_auth_methods = 0, elapsed_usec = 0, ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1324 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1325 | opts = session->data; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1326 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1327 | /* other transport-specific data */ |
| 1328 | session->ti_type = NC_TI_LIBSSH; |
| 1329 | session->ti.libssh.session = ssh_new(); |
| 1330 | if (!session->ti.libssh.session) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1331 | ERR("Failed to initialize a new SSH session."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1332 | close(sock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1333 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1334 | } |
| 1335 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1336 | if (opts->auth_methods & NC_SSH_AUTH_PUBLICKEY) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1337 | libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY; |
| 1338 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1339 | if (opts->auth_methods & NC_SSH_AUTH_PASSWORD) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1340 | libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD; |
| 1341 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1342 | if (opts->auth_methods & NC_SSH_AUTH_INTERACTIVE) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1343 | libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE; |
| 1344 | } |
| 1345 | ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods); |
| 1346 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1347 | sbind = ssh_bind_new(); |
| 1348 | if (!sbind) { |
| 1349 | ERR("Failed to create an SSH bind."); |
| 1350 | close(sock); |
| 1351 | return -1; |
| 1352 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1353 | |
| 1354 | if (nc_ssh_bind_add_hostkeys(sbind, opts->hostkeys, opts->hostkey_count)) { |
| 1355 | close(sock); |
| 1356 | ssh_bind_free(sbind); |
| 1357 | return -1; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1358 | } |
| 1359 | if (opts->banner) { |
| 1360 | ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_BANNER, opts->banner); |
| 1361 | } |
| 1362 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1363 | ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, session); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1364 | /* remember that this session was just set as nc_sshcb_msg() parameter */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1365 | session->flags |= NC_SESSION_SSH_MSG_CB; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1366 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1367 | if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) { |
| 1368 | ERR("SSH failed to accept a new connection (%s).", ssh_get_error(sbind)); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1369 | close(sock); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1370 | ssh_bind_free(sbind); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1371 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1372 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1373 | ssh_bind_free(sbind); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1374 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1375 | ssh_set_blocking(session->ti.libssh.session, 0); |
| 1376 | |
| 1377 | while ((ret = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) { |
Michal Vasko | e65b449 | 2016-03-03 11:57:51 +0100 | [diff] [blame] | 1378 | /* this tends to take longer */ |
| 1379 | usleep(NC_TIMEOUT_STEP * 20); |
| 1380 | elapsed_usec += NC_TIMEOUT_STEP * 20; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1381 | if ((timeout > -1) && (elapsed_usec / 1000 >= timeout)) { |
| 1382 | break; |
| 1383 | } |
| 1384 | } |
| 1385 | if (ret == SSH_AGAIN) { |
| 1386 | ERR("SSH key exchange timeout."); |
| 1387 | return 0; |
| 1388 | } else if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1389 | ERR("SSH key exchange error (%s).", ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1390 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | /* authenticate */ |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1394 | elapsed_usec = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1395 | do { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1396 | if (!nc_session_is_connected(session)) { |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1397 | ERR("Communication SSH socket unexpectedly closed."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1398 | return -1; |
| 1399 | } |
| 1400 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1401 | if (ssh_execute_message_callbacks(session->ti.libssh.session) != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1402 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1403 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1404 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1405 | } |
| 1406 | |
Michal Vasko | 145ae67 | 2017-02-07 10:57:27 +0100 | [diff] [blame] | 1407 | if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) { |
| 1408 | ERR("Too many failed authentication attempts of user \"%s\".", session->username); |
| 1409 | return -1; |
| 1410 | } |
| 1411 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1412 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
| 1413 | break; |
| 1414 | } |
| 1415 | |
| 1416 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | 72387da | 2016-02-02 15:52:41 +0100 | [diff] [blame] | 1417 | elapsed_usec += NC_TIMEOUT_STEP; |
Michal Vasko | ab63994 | 2016-02-29 16:23:47 +0100 | [diff] [blame] | 1418 | } while (!opts->auth_timeout || (elapsed_usec / 1000000 < opts->auth_timeout)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1419 | |
| 1420 | if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) { |
| 1421 | /* timeout */ |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1422 | if (session->username) { |
| 1423 | ERR("User \"%s\" failed to authenticate for too long, disconnecting.", session->username); |
| 1424 | } else { |
| 1425 | ERR("User failed to authenticate for too long, disconnecting."); |
| 1426 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1427 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1428 | } |
| 1429 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1430 | /* open channel */ |
Michal Vasko | ab63994 | 2016-02-29 16:23:47 +0100 | [diff] [blame] | 1431 | ret = nc_open_netconf_channel(session, opts->auth_timeout ? (opts->auth_timeout * 1000 - elapsed_usec / 1000) : -1); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1432 | if (ret < 1) { |
| 1433 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1434 | } |
| 1435 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1436 | session->flags &= ~NC_SESSION_SSH_NEW_MSG; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1437 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1438 | } |
| 1439 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1440 | API NC_MSG_TYPE |
| 1441 | nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session) |
| 1442 | { |
| 1443 | NC_MSG_TYPE msgtype; |
| 1444 | struct nc_session *new_session = NULL; |
| 1445 | |
| 1446 | if (!orig_session) { |
| 1447 | ERRARG("orig_session"); |
| 1448 | return NC_MSG_ERROR; |
| 1449 | } else if (!session) { |
| 1450 | ERRARG("session"); |
| 1451 | return NC_MSG_ERROR; |
| 1452 | } |
| 1453 | |
| 1454 | if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) |
| 1455 | && orig_session->ti.libssh.next) { |
| 1456 | for (new_session = orig_session->ti.libssh.next; |
| 1457 | new_session != orig_session; |
| 1458 | new_session = new_session->ti.libssh.next) { |
| 1459 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel |
| 1460 | && (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1461 | /* we found our session */ |
| 1462 | break; |
| 1463 | } |
| 1464 | } |
| 1465 | if (new_session == orig_session) { |
| 1466 | new_session = NULL; |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | if (!new_session) { |
| 1471 | ERR("Session does not have a NETCONF SSH channel ready."); |
| 1472 | return NC_MSG_ERROR; |
| 1473 | } |
| 1474 | |
| 1475 | /* assign new SID atomically */ |
| 1476 | pthread_spin_lock(&server_opts.sid_lock); |
| 1477 | new_session->id = server_opts.new_session_id++; |
| 1478 | pthread_spin_unlock(&server_opts.sid_lock); |
| 1479 | |
| 1480 | /* NETCONF handshake */ |
| 1481 | msgtype = nc_handshake(new_session); |
| 1482 | if (msgtype != NC_MSG_HELLO) { |
| 1483 | return msgtype; |
| 1484 | } |
| 1485 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1486 | new_session->opts.server.session_start = new_session->opts.server.last_rpc = time(NULL); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1487 | new_session->status = NC_STATUS_RUNNING; |
| 1488 | *session = new_session; |
| 1489 | |
| 1490 | return msgtype; |
| 1491 | } |
| 1492 | |
| 1493 | API NC_MSG_TYPE |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1494 | nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1495 | { |
Michal Vasko | bdcf236 | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1496 | uint8_t q_id; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1497 | NC_MSG_TYPE msgtype; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1498 | struct nc_session *new_session = NULL; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1499 | uint16_t i; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1500 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1501 | if (!ps) { |
| 1502 | ERRARG("ps"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1503 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1504 | } else if (!session) { |
| 1505 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1506 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1507 | } |
| 1508 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1509 | /* LOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1510 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1511 | return NC_MSG_ERROR; |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1512 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1513 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1514 | for (i = 0; i < ps->session_count; ++i) { |
| 1515 | if ((ps->sessions[i]->status == NC_STATUS_RUNNING) && (ps->sessions[i]->ti_type == NC_TI_LIBSSH) |
| 1516 | && ps->sessions[i]->ti.libssh.next) { |
| 1517 | /* an SSH session with more channels */ |
| 1518 | for (new_session = ps->sessions[i]->ti.libssh.next; |
| 1519 | new_session != ps->sessions[i]; |
| 1520 | new_session = new_session->ti.libssh.next) { |
| 1521 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel |
| 1522 | && (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1523 | /* we found our session */ |
| 1524 | break; |
| 1525 | } |
| 1526 | } |
| 1527 | if (new_session != ps->sessions[i]) { |
| 1528 | break; |
| 1529 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1530 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1531 | new_session = NULL; |
| 1532 | } |
| 1533 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1534 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1535 | /* UNLOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1536 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1537 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1538 | if (!new_session) { |
| 1539 | ERR("No session with a NETCONF SSH channel ready was found."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1540 | return NC_MSG_ERROR; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | /* assign new SID atomically */ |
| 1544 | pthread_spin_lock(&server_opts.sid_lock); |
| 1545 | new_session->id = server_opts.new_session_id++; |
| 1546 | pthread_spin_unlock(&server_opts.sid_lock); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1547 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1548 | /* NETCONF handshake */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1549 | msgtype = nc_handshake(new_session); |
| 1550 | if (msgtype != NC_MSG_HELLO) { |
| 1551 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1552 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1553 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1554 | new_session->opts.server.session_start = new_session->opts.server.last_rpc = time(NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1555 | new_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1556 | *session = new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1557 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1558 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1559 | } |