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