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 | |
Michal Vasko | 3964a83 | 2018-09-18 14:37:39 +0200 | [diff] [blame^] | 56 | file = fdopen(fd, "w"); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 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); |
Robert Varga | ad7a553 | 2018-08-10 20:40:54 +0200 | [diff] [blame] | 858 | auth_ret = -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 859 | } else { |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 860 | if (ssh_userauth_kbdint_getnanswers(session->ti.libssh.session) != 1) {// failed session |
| 861 | ssh_message_reply_default(msg); |
| 862 | return; |
| 863 | } |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 864 | pass_hash = auth_password_get_pwd_hash(session->username);// get hashed password |
| 865 | if (pass_hash) { |
Robert Varga | ad7a553 | 2018-08-10 20:40:54 +0200 | [diff] [blame] | 866 | /* Normalize auth_password_compare_pwd result to 0 or 1 */ |
| 867 | auth_ret = !!auth_password_compare_pwd(pass_hash, ssh_userauth_kbdint_getanswer(session->ti.libssh.session, 0)); |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 868 | free(pass_hash);// free hashed password |
| 869 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 870 | } |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 871 | } |
| 872 | |
Robert Varga | ad7a553 | 2018-08-10 20:40:54 +0200 | [diff] [blame] | 873 | /* We have already sent a reply */ |
| 874 | if (auth_ret == -1) { |
| 875 | return; |
| 876 | } |
| 877 | |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 878 | /* Authenticate message based on outcome */ |
| 879 | if (!auth_ret) { |
| 880 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 881 | VRB("User \"%s\" authenticated.", session->username); |
| 882 | ssh_message_auth_reply_success(msg, 0); |
| 883 | } else { |
| 884 | ++session->opts.server.ssh_auth_attempts; |
| 885 | VRB("Failed user \"%s\" authentication attempt (#%d).", session->username, session->opts.server.ssh_auth_attempts); |
| 886 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
| 890 | static const char * |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 891 | auth_pubkey_compare_key(ssh_key key) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 892 | { |
| 893 | uint32_t i; |
| 894 | ssh_key pub_key; |
Michal Vasko | 76e3a35 | 2016-01-18 09:07:00 +0100 | [diff] [blame] | 895 | const char *username = NULL; |
Michal Vasko | 3e9d168 | 2017-02-24 09:50:15 +0100 | [diff] [blame] | 896 | int ret = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 897 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 898 | /* LOCK */ |
| 899 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 900 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 901 | for (i = 0; i < server_opts.authkey_count; ++i) { |
| 902 | switch (server_opts.authkeys[i].type) { |
| 903 | case NC_SSH_KEY_UNKNOWN: |
| 904 | ret = ssh_pki_import_pubkey_file(server_opts.authkeys[i].path, &pub_key); |
| 905 | break; |
| 906 | case NC_SSH_KEY_DSA: |
| 907 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_DSS, &pub_key); |
| 908 | break; |
| 909 | case NC_SSH_KEY_RSA: |
| 910 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_RSA, &pub_key); |
| 911 | break; |
| 912 | case NC_SSH_KEY_ECDSA: |
| 913 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_ECDSA, &pub_key); |
| 914 | break; |
| 915 | } |
| 916 | |
Michal Vasko | 7abcdeb | 2016-05-30 15:27:00 +0200 | [diff] [blame] | 917 | if (ret == SSH_EOF) { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 918 | 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] | 919 | continue; |
| 920 | } else if (ret == SSH_ERROR) { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 921 | 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] | 922 | continue; |
| 923 | } |
| 924 | |
| 925 | if (!ssh_key_cmp(key, pub_key, SSH_KEY_CMP_PUBLIC)) { |
| 926 | ssh_key_free(pub_key); |
| 927 | break; |
| 928 | } |
| 929 | |
| 930 | ssh_key_free(pub_key); |
| 931 | } |
| 932 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 933 | if (i < server_opts.authkey_count) { |
| 934 | username = server_opts.authkeys[i].username; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 935 | } |
| 936 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 937 | /* UNLOCK */ |
| 938 | pthread_mutex_unlock(&server_opts.authkey_lock); |
| 939 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 940 | return username; |
| 941 | } |
| 942 | |
| 943 | static void |
| 944 | nc_sshcb_auth_pubkey(struct nc_session *session, ssh_message msg) |
| 945 | { |
| 946 | const char *username; |
| 947 | int signature_state; |
| 948 | |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 949 | if (server_opts.pubkey_auth_clb) { |
| 950 | 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] | 951 | goto fail; |
| 952 | } |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 953 | } else { |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 954 | if ((username = auth_pubkey_compare_key(ssh_message_auth_pubkey(msg))) == NULL) { |
| 955 | VRB("User \"%s\" tried to use an unknown (unauthorized) public key.", session->username); |
| 956 | goto fail; |
| 957 | } else if (strcmp(session->username, username)) { |
| 958 | VRB("User \"%s\" is not the username identified with the presented public key.", session->username); |
| 959 | goto fail; |
| 960 | } |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 961 | } |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 962 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 963 | signature_state = ssh_message_auth_publickey_state(msg); |
| 964 | if (signature_state == SSH_PUBLICKEY_STATE_VALID) { |
| 965 | VRB("User \"%s\" authenticated.", session->username); |
| 966 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 967 | ssh_message_auth_reply_success(msg, 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 968 | } else if (signature_state == SSH_PUBLICKEY_STATE_NONE) { |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 969 | /* accepting only the use of a public key */ |
| 970 | ssh_message_auth_reply_pk_ok_simple(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 971 | } |
| 972 | |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 973 | return; |
| 974 | |
| 975 | fail: |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 976 | ++session->opts.server.ssh_auth_attempts; |
| 977 | 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] | 978 | ssh_message_reply_default(msg); |
| 979 | } |
| 980 | |
| 981 | static int |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 982 | nc_sshcb_channel_open(struct nc_session *session, ssh_message msg) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 983 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 984 | ssh_channel chan; |
| 985 | |
| 986 | /* first channel request */ |
| 987 | if (!session->ti.libssh.channel) { |
| 988 | if (session->status != NC_STATUS_STARTING) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 989 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 990 | return -1; |
| 991 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 992 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 993 | if (!chan) { |
| 994 | ERR("Failed to create a new SSH channel."); |
| 995 | return -1; |
| 996 | } |
| 997 | session->ti.libssh.channel = chan; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 998 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 999 | /* additional channel request */ |
| 1000 | } else { |
| 1001 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 1002 | if (!chan) { |
| 1003 | ERR("Session %u: failed to create a new SSH channel.", session->id); |
| 1004 | return -1; |
| 1005 | } |
| 1006 | /* 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] | 1007 | } |
| 1008 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | static int |
| 1013 | nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem) |
| 1014 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1015 | struct nc_session *new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1016 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1017 | if (strcmp(subsystem, "netconf")) { |
| 1018 | WRN("Received an unknown subsystem \"%s\" request.", subsystem); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1019 | return -1; |
| 1020 | } |
| 1021 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1022 | if (session->ti.libssh.channel == channel) { |
| 1023 | /* first channel requested */ |
| 1024 | if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) { |
| 1025 | ERRINT; |
| 1026 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1027 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1028 | if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) { |
| 1029 | ERR("Session %u: subsystem \"netconf\" requested for the second time.", session->id); |
| 1030 | return -1; |
| 1031 | } |
| 1032 | |
| 1033 | session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1034 | } else { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1035 | /* 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] | 1036 | new_session = nc_new_session(NC_SERVER, 1); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1037 | if (!new_session) { |
| 1038 | ERRMEM; |
| 1039 | return -1; |
| 1040 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1041 | |
| 1042 | /* insert the new session */ |
| 1043 | if (!session->ti.libssh.next) { |
| 1044 | new_session->ti.libssh.next = session; |
| 1045 | } else { |
| 1046 | new_session->ti.libssh.next = session->ti.libssh.next; |
| 1047 | } |
| 1048 | session->ti.libssh.next = new_session; |
| 1049 | |
| 1050 | new_session->status = NC_STATUS_STARTING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1051 | new_session->ti_type = NC_TI_LIBSSH; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1052 | new_session->io_lock = session->io_lock; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1053 | new_session->ti.libssh.channel = channel; |
| 1054 | new_session->ti.libssh.session = session->ti.libssh.session; |
| 1055 | new_session->username = lydict_insert(server_opts.ctx, session->username, 0); |
| 1056 | new_session->host = lydict_insert(server_opts.ctx, session->host, 0); |
| 1057 | new_session->port = session->port; |
| 1058 | new_session->ctx = server_opts.ctx; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1059 | new_session->flags = NC_SESSION_SSH_AUTHENTICATED | NC_SESSION_SSH_SUBSYS_NETCONF | NC_SESSION_SHAREDCTX |
| 1060 | | (session->flags & NC_SESSION_CALLHOME ? NC_SESSION_CALLHOME : 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | return 0; |
| 1064 | } |
| 1065 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1066 | int |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1067 | nc_sshcb_msg(ssh_session UNUSED(sshsession), ssh_message msg, void *data) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1068 | { |
| 1069 | const char *str_type, *str_subtype = NULL, *username; |
| 1070 | int subtype, type; |
| 1071 | struct nc_session *session = (struct nc_session *)data; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1072 | |
| 1073 | type = ssh_message_type(msg); |
| 1074 | subtype = ssh_message_subtype(msg); |
| 1075 | |
| 1076 | switch (type) { |
| 1077 | case SSH_REQUEST_AUTH: |
| 1078 | str_type = "request-auth"; |
| 1079 | switch (subtype) { |
| 1080 | case SSH_AUTH_METHOD_NONE: |
| 1081 | str_subtype = "none"; |
| 1082 | break; |
| 1083 | case SSH_AUTH_METHOD_PASSWORD: |
| 1084 | str_subtype = "password"; |
| 1085 | break; |
| 1086 | case SSH_AUTH_METHOD_PUBLICKEY: |
| 1087 | str_subtype = "publickey"; |
| 1088 | break; |
| 1089 | case SSH_AUTH_METHOD_HOSTBASED: |
| 1090 | str_subtype = "hostbased"; |
| 1091 | break; |
| 1092 | case SSH_AUTH_METHOD_INTERACTIVE: |
| 1093 | str_subtype = "interactive"; |
| 1094 | break; |
| 1095 | case SSH_AUTH_METHOD_GSSAPI_MIC: |
| 1096 | str_subtype = "gssapi-mic"; |
| 1097 | break; |
| 1098 | } |
| 1099 | break; |
| 1100 | |
| 1101 | case SSH_REQUEST_CHANNEL_OPEN: |
| 1102 | str_type = "request-channel-open"; |
| 1103 | switch (subtype) { |
| 1104 | case SSH_CHANNEL_SESSION: |
| 1105 | str_subtype = "session"; |
| 1106 | break; |
| 1107 | case SSH_CHANNEL_DIRECT_TCPIP: |
| 1108 | str_subtype = "direct-tcpip"; |
| 1109 | break; |
| 1110 | case SSH_CHANNEL_FORWARDED_TCPIP: |
| 1111 | str_subtype = "forwarded-tcpip"; |
| 1112 | break; |
| 1113 | case (int)SSH_CHANNEL_X11: |
| 1114 | str_subtype = "channel-x11"; |
| 1115 | break; |
| 1116 | case SSH_CHANNEL_UNKNOWN: |
| 1117 | /* fallthrough */ |
| 1118 | default: |
| 1119 | str_subtype = "unknown"; |
| 1120 | break; |
| 1121 | } |
| 1122 | break; |
| 1123 | |
| 1124 | case SSH_REQUEST_CHANNEL: |
| 1125 | str_type = "request-channel"; |
| 1126 | switch (subtype) { |
| 1127 | case SSH_CHANNEL_REQUEST_PTY: |
| 1128 | str_subtype = "pty"; |
| 1129 | break; |
| 1130 | case SSH_CHANNEL_REQUEST_EXEC: |
| 1131 | str_subtype = "exec"; |
| 1132 | break; |
| 1133 | case SSH_CHANNEL_REQUEST_SHELL: |
| 1134 | str_subtype = "shell"; |
| 1135 | break; |
| 1136 | case SSH_CHANNEL_REQUEST_ENV: |
| 1137 | str_subtype = "env"; |
| 1138 | break; |
| 1139 | case SSH_CHANNEL_REQUEST_SUBSYSTEM: |
| 1140 | str_subtype = "subsystem"; |
| 1141 | break; |
| 1142 | case SSH_CHANNEL_REQUEST_WINDOW_CHANGE: |
| 1143 | str_subtype = "window-change"; |
| 1144 | break; |
| 1145 | case SSH_CHANNEL_REQUEST_X11: |
| 1146 | str_subtype = "x11"; |
| 1147 | break; |
| 1148 | case SSH_CHANNEL_REQUEST_UNKNOWN: |
| 1149 | /* fallthrough */ |
| 1150 | default: |
| 1151 | str_subtype = "unknown"; |
| 1152 | break; |
| 1153 | } |
| 1154 | break; |
| 1155 | |
| 1156 | case SSH_REQUEST_SERVICE: |
| 1157 | str_type = "request-service"; |
| 1158 | str_subtype = ssh_message_service_service(msg); |
| 1159 | break; |
| 1160 | |
| 1161 | case SSH_REQUEST_GLOBAL: |
| 1162 | str_type = "request-global"; |
| 1163 | switch (subtype) { |
| 1164 | case SSH_GLOBAL_REQUEST_TCPIP_FORWARD: |
| 1165 | str_subtype = "tcpip-forward"; |
| 1166 | break; |
| 1167 | case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD: |
| 1168 | str_subtype = "cancel-tcpip-forward"; |
| 1169 | break; |
| 1170 | case SSH_GLOBAL_REQUEST_UNKNOWN: |
| 1171 | /* fallthrough */ |
| 1172 | default: |
| 1173 | str_subtype = "unknown"; |
| 1174 | break; |
| 1175 | } |
| 1176 | break; |
| 1177 | |
| 1178 | default: |
| 1179 | str_type = "unknown"; |
| 1180 | str_subtype = "unknown"; |
| 1181 | break; |
| 1182 | } |
| 1183 | |
| 1184 | 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] | 1185 | if ((session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) { |
| 1186 | /* "valid" situation if, for example, receiving some auth or channel request timeouted, |
| 1187 | * but we got it now, during session free */ |
| 1188 | VRB("SSH message arrived on a %s session, the request will be denied.", |
| 1189 | (session->status == NC_STATUS_CLOSING ? "closing" : "invalid")); |
| 1190 | ssh_message_reply_default(msg); |
| 1191 | return 0; |
| 1192 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1193 | session->flags |= NC_SESSION_SSH_NEW_MSG; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1194 | |
| 1195 | /* |
| 1196 | * process known messages |
| 1197 | */ |
| 1198 | if (type == SSH_REQUEST_AUTH) { |
| 1199 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
| 1200 | ERR("User \"%s\" authenticated, but requested another authentication.", session->username); |
| 1201 | ssh_message_reply_default(msg); |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1205 | /* save the username, do not let the client change it */ |
| 1206 | username = ssh_message_auth_user(msg); |
| 1207 | if (!session->username) { |
| 1208 | if (!username) { |
| 1209 | ERR("Denying an auth request without a username."); |
| 1210 | return 1; |
| 1211 | } |
| 1212 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1213 | session->username = lydict_insert(server_opts.ctx, username, 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1214 | } else if (username) { |
| 1215 | if (strcmp(username, session->username)) { |
| 1216 | ERR("User \"%s\" changed its username to \"%s\".", session->username, username); |
| 1217 | session->status = NC_STATUS_INVALID; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1218 | session->term_reason = NC_SESSION_TERM_OTHER; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1219 | return 1; |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | if (subtype == SSH_AUTH_METHOD_NONE) { |
| 1224 | /* libssh will return the supported auth methods */ |
| 1225 | return 1; |
| 1226 | } else if (subtype == SSH_AUTH_METHOD_PASSWORD) { |
| 1227 | nc_sshcb_auth_password(session, msg); |
| 1228 | return 0; |
| 1229 | } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) { |
| 1230 | nc_sshcb_auth_pubkey(session, msg); |
| 1231 | return 0; |
| 1232 | } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) { |
| 1233 | nc_sshcb_auth_kbdint(session, msg); |
| 1234 | return 0; |
| 1235 | } |
| 1236 | } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1237 | 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] | 1238 | if (nc_sshcb_channel_open(session, msg)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1239 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1240 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1241 | return 0; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1242 | |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1243 | } 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] | 1244 | if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg), |
| 1245 | ssh_message_channel_request_subsystem(msg))) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1246 | ssh_message_reply_default(msg); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1247 | } else { |
| 1248 | ssh_message_channel_request_reply_success(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1249 | } |
| 1250 | return 0; |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | /* we did not process it */ |
| 1255 | return 1; |
| 1256 | } |
| 1257 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1258 | /* ret 1 on success, 0 on timeout, -1 on error */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1259 | static int |
| 1260 | nc_open_netconf_channel(struct nc_session *session, int timeout) |
| 1261 | { |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1262 | int ret; |
| 1263 | struct timespec ts_timeout, ts_cur; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1264 | |
| 1265 | /* message callback is executed twice to give chance for the channel to be |
| 1266 | * created if timeout == 0 (it takes 2 messages, channel-open, subsystem-request) */ |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1267 | if (!timeout) { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1268 | if (!nc_session_is_connected(session)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1269 | ERR("Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1270 | return -1; |
| 1271 | } |
| 1272 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1273 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1274 | if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1275 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1276 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1277 | return -1; |
| 1278 | } |
| 1279 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1280 | if (!session->ti.libssh.channel) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1285 | if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1286 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1287 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1288 | return -1; |
| 1289 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1290 | |
| 1291 | if (!(session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1292 | /* we did not receive subsystem-request, timeout */ |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | return 1; |
| 1297 | } |
| 1298 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1299 | if (timeout > -1) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1300 | nc_gettimespec_mono(&ts_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1301 | nc_addtimespec(&ts_timeout, timeout); |
| 1302 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1303 | while (1) { |
| 1304 | if (!nc_session_is_connected(session)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1305 | ERR("Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1306 | return -1; |
| 1307 | } |
| 1308 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1309 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1310 | if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1311 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1312 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1313 | return -1; |
| 1314 | } |
| 1315 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1316 | if (session->ti.libssh.channel && (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1317 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1318 | } |
| 1319 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1320 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1321 | if (timeout > -1) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1322 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1323 | if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) { |
| 1324 | /* timeout */ |
| 1325 | ERR("Failed to start \"netconf\" SSH subsystem for too long, disconnecting."); |
| 1326 | break; |
| 1327 | } |
| 1328 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1329 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1330 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1331 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1332 | } |
| 1333 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1334 | static int |
| 1335 | nc_ssh_bind_add_hostkeys(ssh_bind sbind, const char **hostkeys, uint8_t hostkey_count) |
| 1336 | { |
| 1337 | uint8_t i; |
| 1338 | char *privkey_path, *privkey_data; |
| 1339 | int privkey_data_rsa, ret; |
| 1340 | |
| 1341 | if (!server_opts.hostkey_clb) { |
| 1342 | ERR("Callback for retrieving SSH host keys not set."); |
| 1343 | return -1; |
| 1344 | } |
| 1345 | |
| 1346 | for (i = 0; i < hostkey_count; ++i) { |
| 1347 | privkey_path = privkey_data = NULL; |
| 1348 | if (server_opts.hostkey_clb(hostkeys[i], server_opts.hostkey_data, &privkey_path, &privkey_data, &privkey_data_rsa)) { |
| 1349 | ERR("Host key callback failed."); |
| 1350 | return -1; |
| 1351 | } |
| 1352 | |
| 1353 | if (privkey_data) { |
| 1354 | privkey_path = base64der_key_to_tmp_file(privkey_data, privkey_data_rsa); |
| 1355 | if (!privkey_path) { |
| 1356 | ERR("Temporarily storing a host key into a file failed (%s).", strerror(errno)); |
| 1357 | free(privkey_data); |
| 1358 | return -1; |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path); |
| 1363 | |
| 1364 | /* cleanup */ |
| 1365 | if (privkey_data && unlink(privkey_path)) { |
| 1366 | WRN("Removing a temporary host key file \"%s\" failed (%s).", privkey_path, strerror(errno)); |
| 1367 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1368 | free(privkey_data); |
| 1369 | |
| 1370 | if (ret != SSH_OK) { |
Michal Vasko | 80075de | 2017-07-10 11:38:52 +0200 | [diff] [blame] | 1371 | ERR("Failed to set hostkey \"%s\" (%s).", hostkeys[i], privkey_path); |
| 1372 | } |
| 1373 | free(privkey_path); |
| 1374 | |
| 1375 | if (ret != SSH_OK) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1376 | return -1; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | return 0; |
| 1381 | } |
| 1382 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1383 | int |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1384 | nc_accept_ssh_session(struct nc_session *session, int sock, int timeout) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1385 | { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1386 | ssh_bind sbind; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1387 | struct nc_server_ssh_opts *opts; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1388 | int libssh_auth_methods = 0, ret; |
| 1389 | struct timespec ts_timeout, ts_cur; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1390 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1391 | opts = session->data; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1392 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1393 | /* other transport-specific data */ |
| 1394 | session->ti_type = NC_TI_LIBSSH; |
| 1395 | session->ti.libssh.session = ssh_new(); |
| 1396 | if (!session->ti.libssh.session) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1397 | ERR("Failed to initialize a new SSH session."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1398 | close(sock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1399 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1400 | } |
| 1401 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1402 | if (opts->auth_methods & NC_SSH_AUTH_PUBLICKEY) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1403 | libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY; |
| 1404 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1405 | if (opts->auth_methods & NC_SSH_AUTH_PASSWORD) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1406 | libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD; |
| 1407 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1408 | if (opts->auth_methods & NC_SSH_AUTH_INTERACTIVE) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1409 | libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE; |
| 1410 | } |
| 1411 | ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods); |
| 1412 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1413 | sbind = ssh_bind_new(); |
| 1414 | if (!sbind) { |
| 1415 | ERR("Failed to create an SSH bind."); |
| 1416 | close(sock); |
| 1417 | return -1; |
| 1418 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1419 | |
| 1420 | if (nc_ssh_bind_add_hostkeys(sbind, opts->hostkeys, opts->hostkey_count)) { |
| 1421 | close(sock); |
| 1422 | ssh_bind_free(sbind); |
| 1423 | return -1; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1424 | } |
| 1425 | if (opts->banner) { |
| 1426 | ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_BANNER, opts->banner); |
| 1427 | } |
| 1428 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1429 | ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, session); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1430 | /* remember that this session was just set as nc_sshcb_msg() parameter */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1431 | session->flags |= NC_SESSION_SSH_MSG_CB; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1432 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1433 | if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) { |
| 1434 | 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] | 1435 | close(sock); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1436 | ssh_bind_free(sbind); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1437 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1438 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1439 | ssh_bind_free(sbind); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1440 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1441 | ssh_set_blocking(session->ti.libssh.session, 0); |
| 1442 | |
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_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1445 | nc_addtimespec(&ts_timeout, timeout); |
| 1446 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1447 | while ((ret = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) { |
Michal Vasko | e65b449 | 2016-03-03 11:57:51 +0100 | [diff] [blame] | 1448 | /* this tends to take longer */ |
| 1449 | usleep(NC_TIMEOUT_STEP * 20); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1450 | if (timeout > -1) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1451 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1452 | if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) { |
| 1453 | break; |
| 1454 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1455 | } |
| 1456 | } |
| 1457 | if (ret == SSH_AGAIN) { |
| 1458 | ERR("SSH key exchange timeout."); |
| 1459 | return 0; |
| 1460 | } else if (ret != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1461 | 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] | 1462 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | /* authenticate */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1466 | if (opts->auth_timeout) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1467 | nc_gettimespec_mono(&ts_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1468 | nc_addtimespec(&ts_timeout, opts->auth_timeout * 1000); |
| 1469 | } |
| 1470 | while (1) { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1471 | if (!nc_session_is_connected(session)) { |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1472 | ERR("Communication SSH socket unexpectedly closed."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1473 | return -1; |
| 1474 | } |
| 1475 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1476 | if (ssh_execute_message_callbacks(session->ti.libssh.session) != SSH_OK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1477 | ERR("Failed to receive SSH messages on a session (%s).", |
| 1478 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1479 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1480 | } |
| 1481 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1482 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
| 1483 | break; |
| 1484 | } |
| 1485 | |
Michal Vasko | 145ae67 | 2017-02-07 10:57:27 +0100 | [diff] [blame] | 1486 | if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) { |
| 1487 | ERR("Too many failed authentication attempts of user \"%s\".", session->username); |
| 1488 | return -1; |
| 1489 | } |
| 1490 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1491 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1492 | if (opts->auth_timeout) { |
Michal Vasko | e852c8b | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1493 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1494 | if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) { |
| 1495 | /* timeout */ |
| 1496 | break; |
| 1497 | } |
| 1498 | } |
| 1499 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1500 | |
| 1501 | if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) { |
| 1502 | /* timeout */ |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1503 | if (session->username) { |
| 1504 | ERR("User \"%s\" failed to authenticate for too long, disconnecting.", session->username); |
| 1505 | } else { |
| 1506 | ERR("User failed to authenticate for too long, disconnecting."); |
| 1507 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1508 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1509 | } |
| 1510 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1511 | /* open channel */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1512 | ret = nc_open_netconf_channel(session, timeout); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1513 | if (ret < 1) { |
| 1514 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1515 | } |
| 1516 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1517 | session->flags &= ~NC_SESSION_SSH_NEW_MSG; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1518 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1519 | } |
| 1520 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1521 | API NC_MSG_TYPE |
| 1522 | nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session) |
| 1523 | { |
| 1524 | NC_MSG_TYPE msgtype; |
| 1525 | struct nc_session *new_session = NULL; |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1526 | struct timespec ts_cur; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1527 | |
| 1528 | if (!orig_session) { |
| 1529 | ERRARG("orig_session"); |
| 1530 | return NC_MSG_ERROR; |
| 1531 | } else if (!session) { |
| 1532 | ERRARG("session"); |
| 1533 | return NC_MSG_ERROR; |
| 1534 | } |
| 1535 | |
| 1536 | if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) |
| 1537 | && orig_session->ti.libssh.next) { |
| 1538 | for (new_session = orig_session->ti.libssh.next; |
| 1539 | new_session != orig_session; |
| 1540 | new_session = new_session->ti.libssh.next) { |
| 1541 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel |
| 1542 | && (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1543 | /* we found our session */ |
| 1544 | break; |
| 1545 | } |
| 1546 | } |
| 1547 | if (new_session == orig_session) { |
| 1548 | new_session = NULL; |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | if (!new_session) { |
| 1553 | ERR("Session does not have a NETCONF SSH channel ready."); |
| 1554 | return NC_MSG_ERROR; |
| 1555 | } |
| 1556 | |
| 1557 | /* assign new SID atomically */ |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 1558 | new_session->id = atomic_fetch_add(&server_opts.new_session_id, 1); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1559 | |
| 1560 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1561 | msgtype = nc_handshake_io(new_session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1562 | if (msgtype != NC_MSG_HELLO) { |
| 1563 | return msgtype; |
| 1564 | } |
| 1565 | |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1566 | nc_gettimespec_real(&ts_cur); |
| 1567 | new_session->opts.server.session_start = ts_cur.tv_sec; |
| 1568 | nc_gettimespec_mono(&ts_cur); |
| 1569 | new_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1570 | new_session->status = NC_STATUS_RUNNING; |
| 1571 | *session = new_session; |
| 1572 | |
| 1573 | return msgtype; |
| 1574 | } |
| 1575 | |
| 1576 | API NC_MSG_TYPE |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1577 | 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] | 1578 | { |
Michal Vasko | bdcf236 | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1579 | uint8_t q_id; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1580 | NC_MSG_TYPE msgtype; |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1581 | struct nc_session *new_session = NULL, *cur_session; |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1582 | struct timespec ts_cur; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1583 | uint16_t i; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1584 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1585 | if (!ps) { |
| 1586 | ERRARG("ps"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1587 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1588 | } else if (!session) { |
| 1589 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1590 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1591 | } |
| 1592 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1593 | /* LOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1594 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1595 | return NC_MSG_ERROR; |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1596 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1597 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1598 | for (i = 0; i < ps->session_count; ++i) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1599 | cur_session = ps->sessions[i]->session; |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1600 | if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH) |
| 1601 | && cur_session->ti.libssh.next) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1602 | /* an SSH session with more channels */ |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1603 | for (new_session = cur_session->ti.libssh.next; |
| 1604 | new_session != cur_session; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1605 | new_session = new_session->ti.libssh.next) { |
| 1606 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel |
| 1607 | && (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1608 | /* we found our session */ |
| 1609 | break; |
| 1610 | } |
| 1611 | } |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1612 | if (new_session != cur_session) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1613 | break; |
| 1614 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1615 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1616 | new_session = NULL; |
| 1617 | } |
| 1618 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1619 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1620 | /* UNLOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1621 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1622 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1623 | if (!new_session) { |
| 1624 | ERR("No session with a NETCONF SSH channel ready was found."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1625 | return NC_MSG_ERROR; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | /* assign new SID atomically */ |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 1629 | new_session->id = atomic_fetch_add(&server_opts.new_session_id, 1); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1630 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1631 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1632 | msgtype = nc_handshake_io(new_session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1633 | if (msgtype != NC_MSG_HELLO) { |
| 1634 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1635 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1636 | |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1637 | nc_gettimespec_real(&ts_cur); |
| 1638 | new_session->opts.server.session_start = ts_cur.tv_sec; |
| 1639 | nc_gettimespec_mono(&ts_cur); |
| 1640 | new_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1641 | new_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1642 | *session = new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1643 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1644 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1645 | } |