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