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