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