Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @file session_server_ssh.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libnetconf2 SSH server session manipulation functions |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 5 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 6 | * @copyright |
| 7 | * Copyright (c) 2017 - 2021 CESNET, z.s.p.o. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 8 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 12 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 13 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #define _GNU_SOURCE |
| 17 | |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 18 | #include "config.h" /* Expose HAVE_SHADOW and HAVE_CRYPT */ |
| 19 | |
| 20 | #ifdef HAVE_SHADOW |
| 21 | #include <shadow.h> |
| 22 | #endif |
| 23 | #ifdef HAVE_CRYPT |
| 24 | #include <crypt.h> |
| 25 | #endif |
| 26 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 27 | #include <errno.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <pwd.h> |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame^] | 30 | #include <security/pam_appl.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
Michal Vasko | 2725269 | 2017-03-21 15:34:13 +0100 | [diff] [blame] | 33 | #include <sys/stat.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 34 | #include <sys/types.h> |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 35 | #include <time.h> |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 36 | #include <unistd.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 37 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 38 | #include "compat.h" |
| 39 | #include "libnetconf.h" |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 40 | #include "session_server.h" |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 41 | #include "session_server_ch.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 42 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 43 | #if !defined (HAVE_CRYPT_R) |
Mislav Novakovic | ce9a7ef | 2017-08-08 13:45:52 +0200 | [diff] [blame] | 44 | pthread_mutex_t crypt_lock = PTHREAD_MUTEX_INITIALIZER; |
| 45 | #endif |
| 46 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 47 | extern struct nc_server_opts server_opts; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 48 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 49 | static char * |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 50 | base64der_key_to_tmp_file(const char *in, const char *key_str) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 51 | { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 52 | char path[12] = "/tmp/XXXXXX"; |
| 53 | int fd, written; |
Michal Vasko | 2725269 | 2017-03-21 15:34:13 +0100 | [diff] [blame] | 54 | mode_t umode; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 55 | FILE *file; |
| 56 | |
| 57 | if (in == NULL) { |
| 58 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 59 | } |
| 60 | |
mekleo | b31878b | 2019-09-09 14:10:47 +0200 | [diff] [blame] | 61 | umode = umask(0177); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 62 | fd = mkstemp(path); |
Michal Vasko | 2725269 | 2017-03-21 15:34:13 +0100 | [diff] [blame] | 63 | umask(umode); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 64 | if (fd == -1) { |
| 65 | return NULL; |
| 66 | } |
| 67 | |
Michal Vasko | 3964a83 | 2018-09-18 14:37:39 +0200 | [diff] [blame] | 68 | file = fdopen(fd, "w"); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 69 | if (!file) { |
| 70 | close(fd); |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | /* write the key into the file */ |
Michal Vasko | 68177b7 | 2020-04-27 15:46:53 +0200 | [diff] [blame] | 75 | if (key_str) { |
| 76 | written = fwrite("-----BEGIN ", 1, 11, file); |
| 77 | written += fwrite(key_str, 1, strlen(key_str), file); |
| 78 | written += fwrite(" PRIVATE KEY-----\n", 1, 18, file); |
| 79 | written += fwrite(in, 1, strlen(in), file); |
| 80 | written += fwrite("\n-----END ", 1, 10, file); |
| 81 | written += fwrite(key_str, 1, strlen(key_str), file); |
| 82 | written += fwrite(" PRIVATE KEY-----", 1, 17, file); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 83 | |
Michal Vasko | 68177b7 | 2020-04-27 15:46:53 +0200 | [diff] [blame] | 84 | fclose(file); |
| 85 | if ((unsigned)written != 11 + strlen(key_str) + 18 + strlen(in) + 10 + strlen(key_str) + 17) { |
| 86 | unlink(path); |
| 87 | return NULL; |
| 88 | } |
| 89 | } else { |
| 90 | written = fwrite("-----BEGIN PRIVATE KEY-----\n", 1, 28, file); |
| 91 | written += fwrite(in, 1, strlen(in), file); |
| 92 | written += fwrite("\n-----END PRIVATE KEY-----", 1, 26, file); |
| 93 | |
| 94 | fclose(file); |
| 95 | if ((unsigned)written != 28 + strlen(in) + 26) { |
| 96 | unlink(path); |
| 97 | return NULL; |
| 98 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | return strdup(path); |
| 102 | } |
| 103 | |
| 104 | static int |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 105 | 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] | 106 | { |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 107 | uint8_t i; |
| 108 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 109 | if (!name) { |
| 110 | ERRARG("name"); |
Michal Vasko | 5fcc714 | 2016-02-02 12:21:10 +0100 | [diff] [blame] | 111 | return -1; |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 112 | } else if (idx > opts->hostkey_count) { |
| 113 | ERRARG("idx"); |
| 114 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 115 | } |
Michal Vasko | d45e25a | 2016-01-08 15:48:44 +0100 | [diff] [blame] | 116 | |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 117 | for (i = 0; i < opts->hostkey_count; ++i) { |
| 118 | if (!strcmp(opts->hostkeys[i], name)) { |
| 119 | ERRARG("name"); |
| 120 | return -1; |
| 121 | } |
| 122 | } |
| 123 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 124 | ++opts->hostkey_count; |
| 125 | opts->hostkeys = nc_realloc(opts->hostkeys, opts->hostkey_count * sizeof *opts->hostkeys); |
| 126 | if (!opts->hostkeys) { |
| 127 | ERRMEM; |
| 128 | return -1; |
| 129 | } |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 130 | |
| 131 | if (idx < 0) { |
| 132 | idx = opts->hostkey_count - 1; |
| 133 | } |
| 134 | if (idx != opts->hostkey_count - 1) { |
| 135 | memmove(opts->hostkeys + idx + 1, opts->hostkeys + idx, opts->hostkey_count - idx); |
| 136 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 137 | opts->hostkeys[idx] = strdup(name); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 138 | |
Michal Vasko | 5fcc714 | 2016-02-02 12:21:10 +0100 | [diff] [blame] | 139 | return 0; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | API int |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 143 | 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] | 144 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 145 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 146 | struct nc_endpt *endpt; |
| 147 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 148 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 149 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 150 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 151 | return -1; |
| 152 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 153 | |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 154 | ret = nc_server_ssh_add_hostkey(name, idx, endpt->opts.ssh); |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 155 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 156 | /* UNLOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 157 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 158 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 159 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Michal Vasko | 974410a | 2018-04-03 09:36:57 +0200 | [diff] [blame] | 162 | API void |
| 163 | nc_server_ssh_set_passwd_auth_clb(int (*passwd_auth_clb)(const struct nc_session *session, const char *password, void *user_data), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 164 | void *user_data, void (*free_user_data)(void *user_data)) |
Michal Vasko | 974410a | 2018-04-03 09:36:57 +0200 | [diff] [blame] | 165 | { |
| 166 | server_opts.passwd_auth_clb = passwd_auth_clb; |
| 167 | server_opts.passwd_auth_data = user_data; |
| 168 | server_opts.passwd_auth_data_free = free_user_data; |
| 169 | } |
| 170 | |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 171 | API void |
| 172 | nc_server_ssh_set_interactive_auth_clb(int (*interactive_auth_clb)(const struct nc_session *session, ssh_message msg, void *user_data), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 173 | void *user_data, void (*free_user_data)(void *user_data)) |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 174 | { |
| 175 | server_opts.interactive_auth_clb = interactive_auth_clb; |
| 176 | server_opts.interactive_auth_data = user_data; |
| 177 | server_opts.interactive_auth_data_free = free_user_data; |
| 178 | } |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 179 | |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame^] | 180 | API int |
| 181 | nc_server_ssh_set_pam_conf_path(const char *conf_name, const char *conf_dir) |
| 182 | { |
| 183 | free(server_opts.conf_name); |
| 184 | free(server_opts.conf_dir); |
| 185 | server_opts.conf_name = NULL; |
| 186 | server_opts.conf_dir = NULL; |
| 187 | |
| 188 | if (conf_dir) { |
| 189 | #ifdef LIBPAM_HAVE_CONFDIR |
| 190 | server_opts.conf_dir = strdup(conf_dir); |
| 191 | if (!(server_opts.conf_dir)) { |
| 192 | ERRMEM; |
| 193 | return -1; |
| 194 | } |
| 195 | #else |
| 196 | ERR(NULL, "Failed to set PAM config directory because of old version of PAM. " |
| 197 | "Put the config file in the system directory (usually /etc/pam.d/)."); |
| 198 | return -1; |
| 199 | #endif |
| 200 | } |
| 201 | |
| 202 | if (conf_name) { |
| 203 | server_opts.conf_name = strdup(conf_name); |
| 204 | if (!(server_opts.conf_name)) { |
| 205 | ERRMEM; |
| 206 | return -1; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 213 | API void |
| 214 | nc_server_ssh_set_pubkey_auth_clb(int (*pubkey_auth_clb)(const struct nc_session *session, ssh_key key, void *user_data), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 215 | void *user_data, void (*free_user_data)(void *user_data)) |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 216 | { |
| 217 | server_opts.pubkey_auth_clb = pubkey_auth_clb; |
| 218 | server_opts.pubkey_auth_data = user_data; |
| 219 | server_opts.pubkey_auth_data_free = free_user_data; |
| 220 | } |
| 221 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 222 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 223 | 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] | 224 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 225 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 226 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 227 | struct nc_ch_endpt *endpt; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 228 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 229 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 230 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client); |
| 231 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 232 | return -1; |
| 233 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 234 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 235 | ret = nc_server_ssh_add_hostkey(name, idx, endpt->opts.ssh); |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 236 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 237 | /* UNLOCK */ |
| 238 | nc_server_ch_client_unlock(client); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 239 | |
| 240 | return ret; |
| 241 | } |
| 242 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 243 | API void |
| 244 | 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] | 245 | 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] | 246 | { |
| 247 | if (!hostkey_clb) { |
| 248 | ERRARG("hostkey_clb"); |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | server_opts.hostkey_clb = hostkey_clb; |
| 253 | server_opts.hostkey_data = user_data; |
| 254 | server_opts.hostkey_data_free = free_user_data; |
| 255 | } |
| 256 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 257 | static int |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 258 | 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] | 259 | { |
| 260 | uint8_t i; |
| 261 | |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 262 | if (name && (idx > -1)) { |
| 263 | ERRARG("name and idx"); |
| 264 | return -1; |
| 265 | } else if (idx >= opts->hostkey_count) { |
| 266 | ERRARG("idx"); |
| 267 | } |
| 268 | |
| 269 | if (!name && (idx < 0)) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 270 | for (i = 0; i < opts->hostkey_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 271 | free(opts->hostkeys[i]); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 272 | } |
| 273 | free(opts->hostkeys); |
| 274 | opts->hostkeys = NULL; |
| 275 | opts->hostkey_count = 0; |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 276 | } else if (name) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 277 | for (i = 0; i < opts->hostkey_count; ++i) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 278 | if (!strcmp(opts->hostkeys[i], name)) { |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 279 | idx = i; |
| 280 | goto remove_idx; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 284 | ERRARG("name"); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 285 | return -1; |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 286 | } else { |
| 287 | remove_idx: |
| 288 | --opts->hostkey_count; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 289 | free(opts->hostkeys[idx]); |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 290 | if (idx < opts->hostkey_count - 1) { |
| 291 | memmove(opts->hostkeys + idx, opts->hostkeys + idx + 1, (opts->hostkey_count - idx) * sizeof *opts->hostkeys); |
| 292 | } |
| 293 | if (!opts->hostkey_count) { |
| 294 | free(opts->hostkeys); |
| 295 | opts->hostkeys = NULL; |
| 296 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | API int |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 303 | 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] | 304 | { |
| 305 | int ret; |
| 306 | struct nc_endpt *endpt; |
| 307 | |
| 308 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 309 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 310 | if (!endpt) { |
| 311 | return -1; |
| 312 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 313 | |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 314 | ret = nc_server_ssh_del_hostkey(name, idx, endpt->opts.ssh); |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 315 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 316 | /* UNLOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 317 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 318 | |
| 319 | return ret; |
| 320 | } |
| 321 | |
| 322 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 323 | 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] | 324 | { |
| 325 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 326 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 327 | struct nc_ch_endpt *endpt; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 328 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 329 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 330 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client); |
| 331 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 332 | return -1; |
| 333 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 334 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 335 | ret = nc_server_ssh_del_hostkey(name, idx, endpt->opts.ssh); |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 336 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 337 | /* UNLOCK */ |
| 338 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 339 | |
| 340 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static int |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 344 | nc_server_ssh_mov_hostkey(const char *key_mov, const char *key_after, struct nc_server_ssh_opts *opts) |
| 345 | { |
| 346 | uint8_t i; |
| 347 | int16_t mov_idx = -1, after_idx = -1; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 348 | char *bckup; |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 349 | |
| 350 | if (!key_mov) { |
| 351 | ERRARG("key_mov"); |
| 352 | return -1; |
| 353 | } |
| 354 | |
| 355 | for (i = 0; i < opts->hostkey_count; ++i) { |
| 356 | if (key_after && (after_idx == -1) && !strcmp(opts->hostkeys[i], key_after)) { |
| 357 | after_idx = i; |
| 358 | } |
| 359 | if ((mov_idx == -1) && !strcmp(opts->hostkeys[i], key_mov)) { |
| 360 | mov_idx = i; |
| 361 | } |
| 362 | |
| 363 | if ((!key_after || (after_idx > -1)) && (mov_idx > -1)) { |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if (key_after && (after_idx == -1)) { |
| 369 | ERRARG("key_after"); |
| 370 | return -1; |
| 371 | } |
| 372 | if (mov_idx == -1) { |
| 373 | ERRARG("key_mov"); |
| 374 | return -1; |
| 375 | } |
| 376 | if ((mov_idx == after_idx) || (mov_idx == after_idx + 1)) { |
| 377 | /* nothing to do */ |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | /* finally move the key */ |
| 382 | bckup = opts->hostkeys[mov_idx]; |
| 383 | if (mov_idx > after_idx) { |
| 384 | memmove(opts->hostkeys + after_idx + 2, opts->hostkeys + after_idx + 1, |
| 385 | ((mov_idx - after_idx) - 1) * sizeof *opts->hostkeys); |
| 386 | opts->hostkeys[after_idx + 1] = bckup; |
| 387 | } else { |
| 388 | memmove(opts->hostkeys + mov_idx, opts->hostkeys + mov_idx + 1, (after_idx - mov_idx) * sizeof *opts->hostkeys); |
| 389 | opts->hostkeys[after_idx] = bckup; |
| 390 | } |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | API int |
| 396 | nc_server_ssh_endpt_mov_hostkey(const char *endpt_name, const char *key_mov, const char *key_after) |
| 397 | { |
| 398 | int ret; |
| 399 | struct nc_endpt *endpt; |
| 400 | |
| 401 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 402 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 403 | if (!endpt) { |
| 404 | return -1; |
| 405 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 406 | |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 407 | ret = nc_server_ssh_mov_hostkey(key_mov, key_after, endpt->opts.ssh); |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 408 | |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 409 | /* UNLOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 410 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 411 | |
| 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 416 | nc_server_ssh_ch_client_endpt_mov_hostkey(const char *client_name, const char *endpt_name, const char *key_mov, |
| 417 | const char *key_after) |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 418 | { |
| 419 | int ret; |
| 420 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 421 | struct nc_ch_endpt *endpt; |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 422 | |
| 423 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 424 | 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] | 425 | if (!endpt) { |
| 426 | return -1; |
| 427 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 428 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 429 | ret = nc_server_ssh_mov_hostkey(key_mov, key_after, endpt->opts.ssh); |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 430 | |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 431 | /* UNLOCK */ |
| 432 | nc_server_ch_client_unlock(client); |
| 433 | |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | static int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 438 | 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] | 439 | { |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame^] | 440 | if ((auth_methods & NC_SSH_AUTH_INTERACTIVE) && !server_opts.conf_name) { |
| 441 | /* path to a configuration file not set */ |
| 442 | ERR(NULL, "Unable to use Keyboard-Interactive authentication method without setting the name of the PAM configuration file first."); |
| 443 | return 1; |
| 444 | } |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 445 | opts->auth_methods = auth_methods; |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 450 | 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] | 451 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 452 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 453 | struct nc_endpt *endpt; |
| 454 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 455 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 456 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 457 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 458 | return -1; |
| 459 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 460 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 461 | ret = nc_server_ssh_set_auth_methods(auth_methods, endpt->opts.ssh); |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 462 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 463 | /* UNLOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 464 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 465 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 466 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 470 | 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] | 471 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 472 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 473 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 474 | struct nc_ch_endpt *endpt; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 475 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 476 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 477 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client); |
| 478 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 479 | return -1; |
| 480 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 481 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 482 | ret = nc_server_ssh_set_auth_methods(auth_methods, endpt->opts.ssh); |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 483 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 484 | /* UNLOCK */ |
| 485 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 486 | |
| 487 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 488 | } |
| 489 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 490 | API int |
| 491 | nc_server_ssh_endpt_get_auth_methods(const char *endpt_name) |
| 492 | { |
| 493 | int ret; |
| 494 | struct nc_endpt *endpt; |
| 495 | |
| 496 | /* LOCK */ |
| 497 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL); |
| 498 | if (!endpt) { |
| 499 | return -1; |
| 500 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 501 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 502 | ret = endpt->opts.ssh->auth_methods; |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 503 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 504 | /* UNLOCK */ |
| 505 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 506 | |
| 507 | return ret; |
| 508 | } |
| 509 | |
| 510 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 511 | 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] | 512 | { |
| 513 | int ret; |
| 514 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 515 | struct nc_ch_endpt *endpt; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 516 | |
| 517 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 518 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client); |
| 519 | if (!endpt) { |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 520 | return -1; |
| 521 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 522 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 523 | ret = endpt->opts.ssh->auth_methods; |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 524 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 525 | /* UNLOCK */ |
| 526 | nc_server_ch_client_unlock(client); |
| 527 | |
| 528 | return ret; |
| 529 | } |
| 530 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 531 | static int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 532 | 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] | 533 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 534 | if (!auth_attempts) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 535 | ERRARG("auth_attempts"); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 536 | return -1; |
| 537 | } |
| 538 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 539 | opts->auth_attempts = auth_attempts; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 544 | 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] | 545 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 546 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 547 | struct nc_endpt *endpt; |
| 548 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 549 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 550 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 551 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 552 | return -1; |
| 553 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 554 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 555 | ret = nc_server_ssh_set_auth_attempts(auth_attempts, endpt->opts.ssh); |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 556 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 557 | /* UNLOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 558 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 559 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 560 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | API int |
Michal Vasko | cbad4c5 | 2019-06-27 16:30:35 +0200 | [diff] [blame] | 564 | 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] | 565 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 566 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 567 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 568 | struct nc_ch_endpt *endpt; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 569 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 570 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 571 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client); |
| 572 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 573 | return -1; |
| 574 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 575 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 576 | ret = nc_server_ssh_set_auth_attempts(auth_attempts, endpt->opts.ssh); |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 577 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 578 | /* UNLOCK */ |
| 579 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 580 | |
| 581 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | static int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 585 | 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] | 586 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 587 | if (!auth_timeout) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 588 | ERRARG("auth_timeout"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 589 | return -1; |
| 590 | } |
| 591 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 592 | opts->auth_timeout = auth_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 597 | 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] | 598 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 599 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 600 | struct nc_endpt *endpt; |
| 601 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 602 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 603 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 604 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 605 | return -1; |
| 606 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 607 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 608 | ret = nc_server_ssh_set_auth_timeout(auth_timeout, endpt->opts.ssh); |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 609 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 610 | /* UNLOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 611 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 612 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 613 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | API int |
Michal Vasko | cbad4c5 | 2019-06-27 16:30:35 +0200 | [diff] [blame] | 617 | 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] | 618 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 619 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 620 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 621 | struct nc_ch_endpt *endpt; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 622 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 623 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 624 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client); |
| 625 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 626 | return -1; |
| 627 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 628 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 629 | ret = nc_server_ssh_set_auth_timeout(auth_timeout, endpt->opts.ssh); |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 630 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 631 | /* UNLOCK */ |
| 632 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 633 | |
| 634 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | static int |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 638 | _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] | 639 | { |
Michal Vasko | 0911189 | 2021-07-26 09:30:20 +0200 | [diff] [blame] | 640 | int ret = 0; |
| 641 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 642 | /* LOCK */ |
| 643 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 644 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 645 | ++server_opts.authkey_count; |
| 646 | server_opts.authkeys = nc_realloc(server_opts.authkeys, server_opts.authkey_count * sizeof *server_opts.authkeys); |
| 647 | if (!server_opts.authkeys) { |
| 648 | ERRMEM; |
Michal Vasko | 0911189 | 2021-07-26 09:30:20 +0200 | [diff] [blame] | 649 | ret = -1; |
| 650 | goto cleanup; |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 651 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 652 | server_opts.authkeys[server_opts.authkey_count - 1].path = pubkey_path ? strdup(pubkey_path) : NULL; |
| 653 | server_opts.authkeys[server_opts.authkey_count - 1].base64 = pubkey_base64 ? strdup(pubkey_base64) : NULL; |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 654 | server_opts.authkeys[server_opts.authkey_count - 1].type = type; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 655 | server_opts.authkeys[server_opts.authkey_count - 1].username = strdup(username); |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 656 | |
Michal Vasko | 0911189 | 2021-07-26 09:30:20 +0200 | [diff] [blame] | 657 | cleanup: |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 658 | /* UNLOCK */ |
| 659 | pthread_mutex_unlock(&server_opts.authkey_lock); |
Michal Vasko | 0911189 | 2021-07-26 09:30:20 +0200 | [diff] [blame] | 660 | return ret; |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | API int |
| 664 | 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] | 665 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 666 | if (!pubkey_path) { |
| 667 | ERRARG("pubkey_path"); |
| 668 | return -1; |
| 669 | } else if (!username) { |
| 670 | ERRARG("username"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 671 | return -1; |
| 672 | } |
| 673 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 674 | return _nc_server_ssh_add_authkey(pubkey_path, NULL, 0, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | API int |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 678 | 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] | 679 | { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 680 | if (!pubkey_base64) { |
| 681 | ERRARG("pubkey_base64"); |
| 682 | return -1; |
| 683 | } else if (!type) { |
| 684 | ERRARG("type"); |
| 685 | return -1; |
| 686 | } else if (!username) { |
| 687 | ERRARG("username"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 688 | return -1; |
| 689 | } |
| 690 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 691 | return _nc_server_ssh_add_authkey(NULL, pubkey_base64, type, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | API int |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 695 | nc_server_ssh_del_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 696 | const char *username) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 697 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 698 | uint32_t i; |
| 699 | int ret = -1; |
| 700 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 701 | /* LOCK */ |
| 702 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 703 | |
| 704 | if (!pubkey_path && !pubkey_base64 && !type && !username) { |
| 705 | for (i = 0; i < server_opts.authkey_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 706 | free(server_opts.authkeys[i].path); |
| 707 | free(server_opts.authkeys[i].base64); |
| 708 | free(server_opts.authkeys[i].username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 709 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 710 | ret = 0; |
| 711 | } |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 712 | free(server_opts.authkeys); |
| 713 | server_opts.authkeys = NULL; |
| 714 | server_opts.authkey_count = 0; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 715 | } else { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 716 | for (i = 0; i < server_opts.authkey_count; ++i) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 717 | if ((!pubkey_path || !strcmp(server_opts.authkeys[i].path, pubkey_path)) && |
| 718 | (!pubkey_base64 || !strcmp(server_opts.authkeys[i].base64, pubkey_base64)) && |
| 719 | (!type || (server_opts.authkeys[i].type == type)) && |
| 720 | (!username || !strcmp(server_opts.authkeys[i].username, username))) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 721 | free(server_opts.authkeys[i].path); |
| 722 | free(server_opts.authkeys[i].base64); |
| 723 | free(server_opts.authkeys[i].username); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 724 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 725 | --server_opts.authkey_count; |
| 726 | if (i < server_opts.authkey_count) { |
| 727 | memcpy(&server_opts.authkeys[i], &server_opts.authkeys[server_opts.authkey_count], |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 728 | sizeof *server_opts.authkeys); |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 729 | } else if (!server_opts.authkey_count) { |
| 730 | free(server_opts.authkeys); |
| 731 | server_opts.authkeys = NULL; |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 732 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 733 | |
| 734 | ret = 0; |
| 735 | } |
| 736 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 737 | } |
| 738 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 739 | /* UNLOCK */ |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 740 | pthread_mutex_unlock(&server_opts.authkey_lock); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 741 | |
| 742 | return ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | void |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 746 | nc_server_ssh_clear_opts(struct nc_server_ssh_opts *opts) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 747 | { |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 748 | nc_server_ssh_del_hostkey(NULL, -1, opts); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 749 | } |
| 750 | |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 751 | #ifdef HAVE_SHADOW |
| 752 | |
| 753 | static struct passwd * |
| 754 | auth_password_getpwnam(const char *username, struct passwd *pwd_buf, char **buf, size_t *buf_size) |
| 755 | { |
| 756 | struct passwd *pwd = NULL; |
| 757 | char *mem; |
Jan Kundrát | aa32310 | 2021-10-08 20:10:50 +0200 | [diff] [blame] | 758 | int r = 0; |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 759 | |
| 760 | do { |
Jan Kundrát | aa32310 | 2021-10-08 20:10:50 +0200 | [diff] [blame] | 761 | r = getpwnam_r(username, pwd_buf, *buf, *buf_size, &pwd); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 762 | if (pwd) { |
| 763 | /* entry found */ |
| 764 | break; |
| 765 | } |
| 766 | |
Jan Kundrát | aa32310 | 2021-10-08 20:10:50 +0200 | [diff] [blame] | 767 | if (r == ERANGE) { |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 768 | /* small buffer, enlarge */ |
| 769 | *buf_size <<= 2; |
| 770 | mem = realloc(*buf, *buf_size); |
| 771 | if (!mem) { |
| 772 | ERRMEM; |
| 773 | return NULL; |
| 774 | } |
| 775 | *buf = mem; |
| 776 | } |
Jan Kundrát | aa32310 | 2021-10-08 20:10:50 +0200 | [diff] [blame] | 777 | } while (r == ERANGE); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 778 | |
| 779 | return pwd; |
| 780 | } |
| 781 | |
| 782 | static struct spwd * |
| 783 | auth_password_getspnam(const char *username, struct spwd *spwd_buf, char **buf, size_t *buf_size) |
| 784 | { |
| 785 | struct spwd *spwd = NULL; |
| 786 | char *mem; |
Jan Kundrát | aa32310 | 2021-10-08 20:10:50 +0200 | [diff] [blame] | 787 | int r = 0; |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 788 | |
| 789 | do { |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 790 | # ifndef __QNXNTO__ |
Jan Kundrát | aa32310 | 2021-10-08 20:10:50 +0200 | [diff] [blame] | 791 | r = getspnam_r(username, spwd_buf, *buf, *buf_size, &spwd); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 792 | # else |
| 793 | spwd = getspnam_r(username, spwd_buf, *buf, *buf_size); |
| 794 | # endif |
| 795 | if (spwd) { |
| 796 | /* entry found */ |
| 797 | break; |
| 798 | } |
| 799 | |
Jan Kundrát | aa32310 | 2021-10-08 20:10:50 +0200 | [diff] [blame] | 800 | if (r == ERANGE) { |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 801 | /* small buffer, enlarge */ |
| 802 | *buf_size <<= 2; |
| 803 | mem = realloc(*buf, *buf_size); |
| 804 | if (!mem) { |
| 805 | ERRMEM; |
| 806 | return NULL; |
| 807 | } |
| 808 | *buf = mem; |
| 809 | } |
Jan Kundrát | aa32310 | 2021-10-08 20:10:50 +0200 | [diff] [blame] | 810 | } while (r == ERANGE); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 811 | |
| 812 | return spwd; |
| 813 | } |
| 814 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 815 | static char * |
| 816 | auth_password_get_pwd_hash(const char *username) |
| 817 | { |
| 818 | struct passwd *pwd, pwd_buf; |
| 819 | struct spwd *spwd, spwd_buf; |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 820 | char *pass_hash = NULL, *buf = NULL; |
| 821 | size_t buf_size = 256; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 822 | |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 823 | buf = malloc(buf_size); |
| 824 | if (!buf) { |
| 825 | ERRMEM; |
| 826 | goto error; |
| 827 | } |
| 828 | |
| 829 | pwd = auth_password_getpwnam(username, &pwd_buf, &buf, &buf_size); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 830 | if (!pwd) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 831 | VRB(NULL, "User \"%s\" not found locally.", username); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 832 | goto error; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | if (!strcmp(pwd->pw_passwd, "x")) { |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 836 | spwd = auth_password_getspnam(username, &spwd_buf, &buf, &buf_size); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 837 | if (!spwd) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 838 | VRB(NULL, "Failed to retrieve the shadow entry for \"%s\".", username); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 839 | goto error; |
Michal Vasko | 22b4fe7 | 2020-11-04 08:52:29 +0100 | [diff] [blame] | 840 | } else if ((spwd->sp_expire > -1) && (spwd->sp_expire <= (time(NULL) / (60 * 60 * 24)))) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 841 | WRN(NULL, "User \"%s\" account has expired.", username); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 842 | goto error; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | pass_hash = spwd->sp_pwdp; |
| 846 | } else { |
| 847 | pass_hash = pwd->pw_passwd; |
| 848 | } |
| 849 | |
| 850 | if (!pass_hash) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 851 | ERR(NULL, "No password could be retrieved for \"%s\".", username); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 852 | goto error; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | /* check the hash structure for special meaning */ |
| 856 | if (!strcmp(pass_hash, "*") || !strcmp(pass_hash, "!")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 857 | VRB(NULL, "User \"%s\" is not allowed to authenticate using a password.", username); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 858 | goto error; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 859 | } |
| 860 | if (!strcmp(pass_hash, "*NP*")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 861 | VRB(NULL, "Retrieving password for \"%s\" from a NIS+ server not supported.", username); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 862 | goto error; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 863 | } |
| 864 | |
Michal Vasko | 963f6c0 | 2021-07-26 15:55:44 +0200 | [diff] [blame] | 865 | pass_hash = strdup(pass_hash); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 866 | free(buf); |
Michal Vasko | 963f6c0 | 2021-07-26 15:55:44 +0200 | [diff] [blame] | 867 | return pass_hash; |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 868 | |
| 869 | error: |
| 870 | free(buf); |
| 871 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 872 | } |
| 873 | |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 874 | #else |
| 875 | |
| 876 | static char * |
| 877 | auth_password_get_pwd_hash(const char *username) |
| 878 | { |
| 879 | (void)username; |
| 880 | return strdup(""); |
| 881 | } |
| 882 | |
| 883 | #endif |
| 884 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 885 | static int |
| 886 | auth_password_compare_pwd(const char *pass_hash, const char *pass_clear) |
| 887 | { |
| 888 | char *new_pass_hash; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 889 | |
| 890 | #if defined (HAVE_CRYPT_R) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 891 | struct crypt_data cdata; |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 892 | #endif |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 893 | |
| 894 | if (!pass_hash[0]) { |
| 895 | if (!pass_clear[0]) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 896 | WRN(NULL, "User authentication successful with an empty password!"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 897 | return 0; |
| 898 | } else { |
| 899 | /* the user did now know he does not need any password, |
| 900 | * (which should not be used) so deny authentication */ |
| 901 | return 1; |
| 902 | } |
| 903 | } |
| 904 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 905 | #if defined (HAVE_CRYPT_R) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 906 | cdata.initialized = 0; |
| 907 | new_pass_hash = crypt_r(pass_clear, pass_hash, &cdata); |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 908 | #else |
Mislav Novakovic | ce9a7ef | 2017-08-08 13:45:52 +0200 | [diff] [blame] | 909 | pthread_mutex_lock(&crypt_lock); |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 910 | new_pass_hash = crypt(pass_clear, pass_hash); |
Mislav Novakovic | ce9a7ef | 2017-08-08 13:45:52 +0200 | [diff] [blame] | 911 | pthread_mutex_unlock(&crypt_lock); |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 912 | #endif |
Andrew Langefeld | 158d6fd | 2018-06-11 18:51:44 -0500 | [diff] [blame] | 913 | |
| 914 | if (!new_pass_hash) { |
| 915 | return 1; |
| 916 | } |
| 917 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 918 | return strcmp(new_pass_hash, pass_hash); |
| 919 | } |
| 920 | |
| 921 | static void |
| 922 | nc_sshcb_auth_password(struct nc_session *session, ssh_message msg) |
| 923 | { |
| 924 | char *pass_hash; |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 925 | int auth_ret = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 926 | |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 927 | if (server_opts.passwd_auth_clb) { |
| 928 | auth_ret = server_opts.passwd_auth_clb(session, ssh_message_auth_password(msg), server_opts.passwd_auth_data); |
| 929 | } else { |
| 930 | pass_hash = auth_password_get_pwd_hash(session->username); |
| 931 | if (pass_hash) { |
| 932 | auth_ret = auth_password_compare_pwd(pass_hash, ssh_message_auth_password(msg)); |
| 933 | free(pass_hash); |
| 934 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 935 | } |
| 936 | |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 937 | if (!auth_ret) { |
| 938 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 939 | VRB(session, "User \"%s\" authenticated.", session->username); |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 940 | ssh_message_auth_reply_success(msg, 0); |
| 941 | } else { |
| 942 | ++session->opts.server.ssh_auth_attempts; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 943 | VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username, |
| 944 | session->opts.server.ssh_auth_attempts); |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 945 | ssh_message_reply_default(msg); |
| 946 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 947 | } |
| 948 | |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame^] | 949 | /** |
| 950 | * @brief PAM conversation function, which serves as a callback for exchanging messages between the client and a PAM module. |
| 951 | * |
| 952 | * @param[in] n_messages Number of messages. |
| 953 | * @param[in] msg PAM module's messages. |
| 954 | * @param[out] resp User responses. |
| 955 | * @param[in] appdata_ptr Callback's data. |
| 956 | * @return PAM_SUCCESS on success; |
| 957 | * @return PAM_BUF_ERR on memory allocation error; |
| 958 | * @return PAM_CONV_ERR otherwise. |
| 959 | */ |
| 960 | static int |
| 961 | nc_pam_conv_clb(int n_messages, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr) |
| 962 | { |
| 963 | int i, j, t, r = PAM_SUCCESS, n_answers, n_requests = n_messages; |
| 964 | const char **prompts = NULL; |
| 965 | char *echo = NULL; |
| 966 | const char *name = "Keyboard-Interactive Authentication"; |
| 967 | const char *instruction = "Please enter your authentication token"; |
| 968 | ssh_message reply = NULL; |
| 969 | struct nc_pam_thread_arg *clb_data = appdata_ptr; |
| 970 | ssh_session libssh_session; |
| 971 | struct timespec ts_timeout; |
| 972 | struct nc_server_ssh_opts *opts; |
| 973 | |
| 974 | libssh_session = clb_data->session->ti.libssh.session; |
| 975 | opts = clb_data->session->data; |
| 976 | |
| 977 | /* PAM_MAX_NUM_MSG == 32 by default */ |
| 978 | if ((n_messages <= 0) || (n_messages >= PAM_MAX_NUM_MSG)) { |
| 979 | ERR(NULL, "Bad number of PAM messages (#%d).", n_messages); |
| 980 | r = PAM_CONV_ERR; |
| 981 | goto cleanup; |
| 982 | } |
| 983 | |
| 984 | /* only accepting these 4 types of messages */ |
| 985 | for (i = 0; i < n_messages; i++) { |
| 986 | t = msg[i]->msg_style; |
| 987 | if ((t != PAM_PROMPT_ECHO_OFF) && (t != PAM_PROMPT_ECHO_ON) && (t != PAM_TEXT_INFO) && (t != PAM_ERROR_MSG)) { |
| 988 | ERR(NULL, "PAM conversation callback received an unexpected type of message."); |
| 989 | r = PAM_CONV_ERR; |
| 990 | goto cleanup; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | /* display messages with errors and/or some information and count the amount of actual authentication challenges */ |
| 995 | for (i = 0; i < n_messages; i++) { |
| 996 | if (msg[i]->msg_style == PAM_TEXT_INFO) { |
| 997 | VRB(NULL, "PAM conversation callback received a message with some information for the client (%s).", msg[i]->msg); |
| 998 | n_requests--; |
| 999 | } |
| 1000 | if (msg[i]->msg_style == PAM_ERROR_MSG) { |
| 1001 | ERR(NULL, "PAM conversation callback received an error message (%s).", msg[i]->msg); |
| 1002 | r = PAM_CONV_ERR; |
| 1003 | goto cleanup; |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | /* there are no requests left for the user, only messages with some information for the client were sent */ |
| 1008 | if (n_requests <= 0) { |
| 1009 | r = PAM_SUCCESS; |
| 1010 | goto cleanup; |
| 1011 | } |
| 1012 | |
| 1013 | /* it is the PAM module's responsibility to release both, this array and the responses themselves */ |
| 1014 | *resp = calloc(n_requests, sizeof **resp); |
| 1015 | prompts = calloc(n_requests, sizeof *prompts); |
| 1016 | echo = calloc(n_requests, sizeof *echo); |
| 1017 | if (!(*resp) || !prompts || !echo) { |
| 1018 | ERRMEM; |
| 1019 | r = PAM_BUF_ERR; |
| 1020 | goto cleanup; |
| 1021 | } |
| 1022 | |
| 1023 | /* set the prompts for the user */ |
| 1024 | j = 0; |
| 1025 | for (i = 0; i < n_messages; i++) { |
| 1026 | if ((msg[i]->msg_style == PAM_PROMPT_ECHO_ON) || (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF)) { |
| 1027 | prompts[j++] = msg[i]->msg; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | /* iterate over all the messages and adjust the echo array accordingly */ |
| 1032 | j = 0; |
| 1033 | for (i = 0; i < n_messages; i++) { |
| 1034 | if (msg[i]->msg_style == PAM_PROMPT_ECHO_ON) { |
| 1035 | echo[j++] = 1; |
| 1036 | } |
| 1037 | if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF) { |
| 1038 | /* no need to set to 0 because of calloc */ |
| 1039 | j++; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | /* print all the keyboard-interactive challenges to the user */ |
| 1044 | r = ssh_message_auth_interactive_request(clb_data->msg, name, instruction, n_requests, prompts, echo); |
| 1045 | if (r != SSH_OK) { |
| 1046 | ERR(NULL, "Failed to send an authentication request."); |
| 1047 | r = PAM_CONV_ERR; |
| 1048 | goto cleanup; |
| 1049 | } |
| 1050 | |
| 1051 | if (opts->auth_timeout) { |
| 1052 | nc_gettimespec_mono_add(&ts_timeout, opts->auth_timeout * 1000); |
| 1053 | } |
| 1054 | |
| 1055 | /* get user's replies */ |
| 1056 | do { |
| 1057 | if (!nc_session_is_connected(clb_data->session)) { |
| 1058 | ERR(NULL, "Communication SSH socket unexpectedly closed."); |
| 1059 | r = PAM_CONV_ERR; |
| 1060 | goto cleanup; |
| 1061 | } |
| 1062 | |
| 1063 | reply = ssh_message_get(libssh_session); |
| 1064 | if (reply) { |
| 1065 | break; |
| 1066 | } |
| 1067 | |
| 1068 | usleep(NC_TIMEOUT_STEP); |
| 1069 | } while ((opts->auth_timeout) && (nc_difftimespec_cur(&ts_timeout) >= 1)); |
| 1070 | |
| 1071 | if (!reply) { |
| 1072 | ERR(NULL, "Authentication timeout."); |
| 1073 | r = PAM_CONV_ERR; |
| 1074 | goto cleanup; |
| 1075 | } |
| 1076 | |
| 1077 | /* check if the amount of replies matches the amount of requests */ |
| 1078 | n_answers = ssh_userauth_kbdint_getnanswers(libssh_session); |
| 1079 | if (n_answers != n_requests) { |
| 1080 | ERR(NULL, "Expected %d response(s), got %d.", n_requests, n_answers); |
| 1081 | r = PAM_CONV_ERR; |
| 1082 | goto cleanup; |
| 1083 | } |
| 1084 | |
| 1085 | /* give the replies to a PAM module */ |
| 1086 | for (i = 0; i < n_answers; i++) { |
| 1087 | (*resp)[i].resp = strdup(ssh_userauth_kbdint_getanswer(libssh_session, i)); |
| 1088 | /* it should be the caller's responsibility to free this, however if mem alloc fails, |
| 1089 | * it is safer to free the responses here and set them to NULL */ |
| 1090 | if ((*resp)[i].resp == NULL) { |
| 1091 | for (j = 0; j < i; j++) { |
| 1092 | free((*resp)[j].resp); |
| 1093 | (*resp)[j].resp = NULL; |
| 1094 | } |
| 1095 | ERRMEM; |
| 1096 | r = PAM_BUF_ERR; |
| 1097 | goto cleanup; |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | cleanup: |
| 1102 | ssh_message_free(reply); |
| 1103 | free(prompts); |
| 1104 | free(echo); |
| 1105 | return r; |
| 1106 | } |
| 1107 | |
| 1108 | /** |
| 1109 | * @brief Handles authentication via Linux PAM. |
| 1110 | * |
| 1111 | * @param[in] session NETCONF session. |
| 1112 | * @param[in] ssh_msg SSH message with a keyboard-interactive authentication request. |
| 1113 | * @return PAM_SUCCESS on success; |
| 1114 | * @return PAM error otherwise. |
| 1115 | */ |
| 1116 | static int |
| 1117 | nc_pam_auth(struct nc_session *session, ssh_message ssh_msg) |
| 1118 | { |
| 1119 | pam_handle_t *pam_h = NULL; |
| 1120 | int ret; |
| 1121 | struct nc_pam_thread_arg clb_data; |
| 1122 | struct pam_conv conv; |
| 1123 | |
| 1124 | /* structure holding callback's data */ |
| 1125 | clb_data.msg = ssh_msg; |
| 1126 | clb_data.session = session; |
| 1127 | |
| 1128 | /* PAM conversation structure holding the callback and it's data */ |
| 1129 | conv.conv = nc_pam_conv_clb; |
| 1130 | conv.appdata_ptr = &clb_data; |
| 1131 | |
| 1132 | /* initialize PAM and see if the given configuration file exists */ |
| 1133 | #ifdef LIBPAM_HAVE_CONFDIR |
| 1134 | /* PAM version >= 1.4 */ |
| 1135 | ret = pam_start_confdir(server_opts.conf_name, session->username, &conv, server_opts.conf_dir, &pam_h); |
| 1136 | #else |
| 1137 | /* PAM version < 1.4 */ |
| 1138 | ret = pam_start(server_opts.conf_name, session->username, &conv, &pam_h); |
| 1139 | #endif |
| 1140 | if (ret != PAM_SUCCESS) { |
| 1141 | ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 1142 | goto cleanup; |
| 1143 | } |
| 1144 | |
| 1145 | /* authentication based on the modules listed in the configuration file */ |
| 1146 | ret = pam_authenticate(pam_h, 0); |
| 1147 | if (ret != PAM_SUCCESS) { |
| 1148 | if (ret == PAM_ABORT) { |
| 1149 | ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 1150 | goto cleanup; |
| 1151 | } else { |
| 1152 | VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 1153 | goto cleanup; |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | /* correct token entered, check other requirements(the time of the day, expired token, ...) */ |
| 1158 | ret = pam_acct_mgmt(pam_h, 0); |
| 1159 | if ((ret != PAM_SUCCESS) && (ret != PAM_NEW_AUTHTOK_REQD)) { |
| 1160 | VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 1161 | goto cleanup; |
| 1162 | } |
| 1163 | |
| 1164 | /* if a token has expired a new one will be generated */ |
| 1165 | if (ret == PAM_NEW_AUTHTOK_REQD) { |
| 1166 | VRB(NULL, "PAM warning occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 1167 | ret = pam_chauthtok(pam_h, PAM_CHANGE_EXPIRED_AUTHTOK); |
| 1168 | if (ret == PAM_SUCCESS) { |
| 1169 | VRB(NULL, "The authentication token of user \"%s\" updated successfully.", session->username); |
| 1170 | } else { |
| 1171 | ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 1172 | goto cleanup; |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | cleanup: |
| 1177 | /* destroy the PAM context */ |
| 1178 | if (pam_end(pam_h, ret) != PAM_SUCCESS) { |
| 1179 | ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 1180 | } |
| 1181 | return ret; |
| 1182 | } |
| 1183 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1184 | static void |
| 1185 | nc_sshcb_auth_kbdint(struct nc_session *session, ssh_message msg) |
| 1186 | { |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 1187 | int auth_ret = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1188 | |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 1189 | if (server_opts.interactive_auth_clb) { |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 1190 | auth_ret = server_opts.interactive_auth_clb(session, msg, server_opts.interactive_auth_data); |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame^] | 1191 | } else if (nc_pam_auth(session, msg) == PAM_SUCCESS) { |
| 1192 | auth_ret = 0; |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 1193 | } |
| 1194 | |
Robert Varga | ad7a553 | 2018-08-10 20:40:54 +0200 | [diff] [blame] | 1195 | /* We have already sent a reply */ |
| 1196 | if (auth_ret == -1) { |
| 1197 | return; |
| 1198 | } |
| 1199 | |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 1200 | /* Authenticate message based on outcome */ |
| 1201 | if (!auth_ret) { |
| 1202 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1203 | VRB(session, "User \"%s\" authenticated.", session->username); |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 1204 | ssh_message_auth_reply_success(msg, 0); |
| 1205 | } else { |
| 1206 | ++session->opts.server.ssh_auth_attempts; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1207 | VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username, |
| 1208 | session->opts.server.ssh_auth_attempts); |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 1209 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | static const char * |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 1214 | auth_pubkey_compare_key(ssh_key key) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1215 | { |
| 1216 | uint32_t i; |
| 1217 | ssh_key pub_key; |
Michal Vasko | 76e3a35 | 2016-01-18 09:07:00 +0100 | [diff] [blame] | 1218 | const char *username = NULL; |
Michal Vasko | 3e9d168 | 2017-02-24 09:50:15 +0100 | [diff] [blame] | 1219 | int ret = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1220 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 1221 | /* LOCK */ |
| 1222 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 1223 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 1224 | for (i = 0; i < server_opts.authkey_count; ++i) { |
| 1225 | switch (server_opts.authkeys[i].type) { |
| 1226 | case NC_SSH_KEY_UNKNOWN: |
| 1227 | ret = ssh_pki_import_pubkey_file(server_opts.authkeys[i].path, &pub_key); |
| 1228 | break; |
| 1229 | case NC_SSH_KEY_DSA: |
| 1230 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_DSS, &pub_key); |
| 1231 | break; |
| 1232 | case NC_SSH_KEY_RSA: |
| 1233 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_RSA, &pub_key); |
| 1234 | break; |
| 1235 | case NC_SSH_KEY_ECDSA: |
| 1236 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_ECDSA, &pub_key); |
| 1237 | break; |
| 1238 | } |
| 1239 | |
Michal Vasko | 7abcdeb | 2016-05-30 15:27:00 +0200 | [diff] [blame] | 1240 | if (ret == SSH_EOF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1241 | WRN(NULL, "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] | 1242 | continue; |
| 1243 | } else if (ret == SSH_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1244 | WRN(NULL, "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] | 1245 | continue; |
| 1246 | } |
| 1247 | |
| 1248 | if (!ssh_key_cmp(key, pub_key, SSH_KEY_CMP_PUBLIC)) { |
| 1249 | ssh_key_free(pub_key); |
| 1250 | break; |
| 1251 | } |
| 1252 | |
| 1253 | ssh_key_free(pub_key); |
| 1254 | } |
| 1255 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 1256 | if (i < server_opts.authkey_count) { |
| 1257 | username = server_opts.authkeys[i].username; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1258 | } |
| 1259 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 1260 | /* UNLOCK */ |
| 1261 | pthread_mutex_unlock(&server_opts.authkey_lock); |
| 1262 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1263 | return username; |
| 1264 | } |
| 1265 | |
| 1266 | static void |
| 1267 | nc_sshcb_auth_pubkey(struct nc_session *session, ssh_message msg) |
| 1268 | { |
| 1269 | const char *username; |
| 1270 | int signature_state; |
| 1271 | |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 1272 | if (server_opts.pubkey_auth_clb) { |
| 1273 | 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] | 1274 | goto fail; |
| 1275 | } |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 1276 | } else { |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 1277 | if ((username = auth_pubkey_compare_key(ssh_message_auth_pubkey(msg))) == NULL) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1278 | VRB(session, "User \"%s\" tried to use an unknown (unauthorized) public key.", session->username); |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 1279 | goto fail; |
| 1280 | } else if (strcmp(session->username, username)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1281 | VRB(session, "User \"%s\" is not the username identified with the presented public key.", session->username); |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 1282 | goto fail; |
| 1283 | } |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 1284 | } |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 1285 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1286 | signature_state = ssh_message_auth_publickey_state(msg); |
| 1287 | if (signature_state == SSH_PUBLICKEY_STATE_VALID) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1288 | VRB(session, "User \"%s\" authenticated.", session->username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1289 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 1290 | ssh_message_auth_reply_success(msg, 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1291 | } else if (signature_state == SSH_PUBLICKEY_STATE_NONE) { |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 1292 | /* accepting only the use of a public key */ |
| 1293 | ssh_message_auth_reply_pk_ok_simple(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1294 | } |
| 1295 | |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 1296 | return; |
| 1297 | |
| 1298 | fail: |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1299 | ++session->opts.server.ssh_auth_attempts; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1300 | VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username, |
| 1301 | session->opts.server.ssh_auth_attempts); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1302 | ssh_message_reply_default(msg); |
| 1303 | } |
| 1304 | |
| 1305 | static int |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1306 | nc_sshcb_channel_open(struct nc_session *session, ssh_message msg) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1307 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1308 | ssh_channel chan; |
| 1309 | |
| 1310 | /* first channel request */ |
| 1311 | if (!session->ti.libssh.channel) { |
| 1312 | if (session->status != NC_STATUS_STARTING) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1313 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1314 | return -1; |
| 1315 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1316 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 1317 | if (!chan) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1318 | ERR(session, "Failed to create a new SSH channel."); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1319 | return -1; |
| 1320 | } |
| 1321 | session->ti.libssh.channel = chan; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1322 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1323 | /* additional channel request */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1324 | } else { |
| 1325 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 1326 | if (!chan) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1327 | ERR(session, "Session %u: failed to create a new SSH channel.", session->id); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1328 | return -1; |
| 1329 | } |
| 1330 | /* 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] | 1331 | } |
| 1332 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1333 | return 0; |
| 1334 | } |
| 1335 | |
| 1336 | static int |
| 1337 | nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem) |
| 1338 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1339 | struct nc_session *new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1340 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1341 | if (strcmp(subsystem, "netconf")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1342 | WRN(session, "Received an unknown subsystem \"%s\" request.", subsystem); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1343 | return -1; |
| 1344 | } |
| 1345 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1346 | if (session->ti.libssh.channel == channel) { |
| 1347 | /* first channel requested */ |
| 1348 | if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) { |
| 1349 | ERRINT; |
| 1350 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1351 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1352 | if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1353 | ERR(session, "Subsystem \"netconf\" requested for the second time."); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1354 | return -1; |
| 1355 | } |
| 1356 | |
| 1357 | session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1358 | } else { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1359 | /* 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] | 1360 | new_session = nc_new_session(NC_SERVER, 1); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1361 | if (!new_session) { |
| 1362 | ERRMEM; |
| 1363 | return -1; |
| 1364 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1365 | |
| 1366 | /* insert the new session */ |
| 1367 | if (!session->ti.libssh.next) { |
| 1368 | new_session->ti.libssh.next = session; |
| 1369 | } else { |
| 1370 | new_session->ti.libssh.next = session->ti.libssh.next; |
| 1371 | } |
| 1372 | session->ti.libssh.next = new_session; |
| 1373 | |
| 1374 | new_session->status = NC_STATUS_STARTING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1375 | new_session->ti_type = NC_TI_LIBSSH; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1376 | new_session->io_lock = session->io_lock; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1377 | new_session->ti.libssh.channel = channel; |
| 1378 | new_session->ti.libssh.session = session->ti.libssh.session; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1379 | new_session->username = strdup(session->username); |
| 1380 | new_session->host = strdup(session->host); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1381 | new_session->port = session->port; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1382 | new_session->ctx = (struct ly_ctx *)session->ctx; |
Michal Vasko | 83d1532 | 2018-09-27 09:44:02 +0200 | [diff] [blame] | 1383 | 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] | 1384 | } |
| 1385 | |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1389 | int |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1390 | nc_sshcb_msg(ssh_session UNUSED(sshsession), ssh_message msg, void *data) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1391 | { |
| 1392 | const char *str_type, *str_subtype = NULL, *username; |
| 1393 | int subtype, type; |
| 1394 | struct nc_session *session = (struct nc_session *)data; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1395 | |
| 1396 | type = ssh_message_type(msg); |
| 1397 | subtype = ssh_message_subtype(msg); |
| 1398 | |
| 1399 | switch (type) { |
| 1400 | case SSH_REQUEST_AUTH: |
| 1401 | str_type = "request-auth"; |
| 1402 | switch (subtype) { |
| 1403 | case SSH_AUTH_METHOD_NONE: |
| 1404 | str_subtype = "none"; |
| 1405 | break; |
| 1406 | case SSH_AUTH_METHOD_PASSWORD: |
| 1407 | str_subtype = "password"; |
| 1408 | break; |
| 1409 | case SSH_AUTH_METHOD_PUBLICKEY: |
| 1410 | str_subtype = "publickey"; |
| 1411 | break; |
| 1412 | case SSH_AUTH_METHOD_HOSTBASED: |
| 1413 | str_subtype = "hostbased"; |
| 1414 | break; |
| 1415 | case SSH_AUTH_METHOD_INTERACTIVE: |
| 1416 | str_subtype = "interactive"; |
| 1417 | break; |
| 1418 | case SSH_AUTH_METHOD_GSSAPI_MIC: |
| 1419 | str_subtype = "gssapi-mic"; |
| 1420 | break; |
| 1421 | } |
| 1422 | break; |
| 1423 | |
| 1424 | case SSH_REQUEST_CHANNEL_OPEN: |
| 1425 | str_type = "request-channel-open"; |
| 1426 | switch (subtype) { |
| 1427 | case SSH_CHANNEL_SESSION: |
| 1428 | str_subtype = "session"; |
| 1429 | break; |
| 1430 | case SSH_CHANNEL_DIRECT_TCPIP: |
| 1431 | str_subtype = "direct-tcpip"; |
| 1432 | break; |
| 1433 | case SSH_CHANNEL_FORWARDED_TCPIP: |
| 1434 | str_subtype = "forwarded-tcpip"; |
| 1435 | break; |
| 1436 | case (int)SSH_CHANNEL_X11: |
| 1437 | str_subtype = "channel-x11"; |
| 1438 | break; |
| 1439 | case SSH_CHANNEL_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1440 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1441 | default: |
| 1442 | str_subtype = "unknown"; |
| 1443 | break; |
| 1444 | } |
| 1445 | break; |
| 1446 | |
| 1447 | case SSH_REQUEST_CHANNEL: |
| 1448 | str_type = "request-channel"; |
| 1449 | switch (subtype) { |
| 1450 | case SSH_CHANNEL_REQUEST_PTY: |
| 1451 | str_subtype = "pty"; |
| 1452 | break; |
| 1453 | case SSH_CHANNEL_REQUEST_EXEC: |
| 1454 | str_subtype = "exec"; |
| 1455 | break; |
| 1456 | case SSH_CHANNEL_REQUEST_SHELL: |
| 1457 | str_subtype = "shell"; |
| 1458 | break; |
| 1459 | case SSH_CHANNEL_REQUEST_ENV: |
| 1460 | str_subtype = "env"; |
| 1461 | break; |
| 1462 | case SSH_CHANNEL_REQUEST_SUBSYSTEM: |
| 1463 | str_subtype = "subsystem"; |
| 1464 | break; |
| 1465 | case SSH_CHANNEL_REQUEST_WINDOW_CHANGE: |
| 1466 | str_subtype = "window-change"; |
| 1467 | break; |
| 1468 | case SSH_CHANNEL_REQUEST_X11: |
| 1469 | str_subtype = "x11"; |
| 1470 | break; |
| 1471 | case SSH_CHANNEL_REQUEST_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1472 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1473 | default: |
| 1474 | str_subtype = "unknown"; |
| 1475 | break; |
| 1476 | } |
| 1477 | break; |
| 1478 | |
| 1479 | case SSH_REQUEST_SERVICE: |
| 1480 | str_type = "request-service"; |
| 1481 | str_subtype = ssh_message_service_service(msg); |
| 1482 | break; |
| 1483 | |
| 1484 | case SSH_REQUEST_GLOBAL: |
| 1485 | str_type = "request-global"; |
| 1486 | switch (subtype) { |
| 1487 | case SSH_GLOBAL_REQUEST_TCPIP_FORWARD: |
| 1488 | str_subtype = "tcpip-forward"; |
| 1489 | break; |
| 1490 | case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD: |
| 1491 | str_subtype = "cancel-tcpip-forward"; |
| 1492 | break; |
| 1493 | case SSH_GLOBAL_REQUEST_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1494 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1495 | default: |
| 1496 | str_subtype = "unknown"; |
| 1497 | break; |
| 1498 | } |
| 1499 | break; |
| 1500 | |
| 1501 | default: |
| 1502 | str_type = "unknown"; |
| 1503 | str_subtype = "unknown"; |
| 1504 | break; |
| 1505 | } |
| 1506 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1507 | VRB(session, "Received an SSH message \"%s\" of subtype \"%s\".", str_type, str_subtype); |
Michal Vasko | 5e0edd8 | 2020-07-29 15:26:13 +0200 | [diff] [blame] | 1508 | if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) { |
Michal Vasko | ce31916 | 2016-02-03 15:33:08 +0100 | [diff] [blame] | 1509 | /* "valid" situation if, for example, receiving some auth or channel request timeouted, |
| 1510 | * but we got it now, during session free */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1511 | VRB(session, "SSH message arrived on a %s session, the request will be denied.", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1512 | (session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid")); |
Michal Vasko | ce31916 | 2016-02-03 15:33:08 +0100 | [diff] [blame] | 1513 | ssh_message_reply_default(msg); |
| 1514 | return 0; |
| 1515 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1516 | session->flags |= NC_SESSION_SSH_NEW_MSG; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1517 | |
| 1518 | /* |
| 1519 | * process known messages |
| 1520 | */ |
| 1521 | if (type == SSH_REQUEST_AUTH) { |
| 1522 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1523 | ERR(session, "User \"%s\" authenticated, but requested another authentication.", session->username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1524 | ssh_message_reply_default(msg); |
| 1525 | return 0; |
| 1526 | } |
| 1527 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1528 | /* save the username, do not let the client change it */ |
| 1529 | username = ssh_message_auth_user(msg); |
| 1530 | if (!session->username) { |
| 1531 | if (!username) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1532 | ERR(session, "Denying an auth request without a username."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1533 | return 1; |
| 1534 | } |
| 1535 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1536 | session->username = strdup(username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1537 | } else if (username) { |
| 1538 | if (strcmp(username, session->username)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1539 | ERR(session, "User \"%s\" changed its username to \"%s\".", session->username, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1540 | session->status = NC_STATUS_INVALID; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1541 | session->term_reason = NC_SESSION_TERM_OTHER; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1542 | return 1; |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | if (subtype == SSH_AUTH_METHOD_NONE) { |
| 1547 | /* libssh will return the supported auth methods */ |
| 1548 | return 1; |
| 1549 | } else if (subtype == SSH_AUTH_METHOD_PASSWORD) { |
| 1550 | nc_sshcb_auth_password(session, msg); |
| 1551 | return 0; |
| 1552 | } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) { |
| 1553 | nc_sshcb_auth_pubkey(session, msg); |
| 1554 | return 0; |
| 1555 | } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) { |
| 1556 | nc_sshcb_auth_kbdint(session, msg); |
| 1557 | return 0; |
| 1558 | } |
| 1559 | } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1560 | 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] | 1561 | if (nc_sshcb_channel_open(session, msg)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1562 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1563 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1564 | return 0; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1565 | |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1566 | } 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] | 1567 | if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg), |
| 1568 | ssh_message_channel_request_subsystem(msg))) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1569 | ssh_message_reply_default(msg); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1570 | } else { |
| 1571 | ssh_message_channel_request_reply_success(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1572 | } |
| 1573 | return 0; |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | /* we did not process it */ |
| 1578 | return 1; |
| 1579 | } |
| 1580 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1581 | /* ret 1 on success, 0 on timeout, -1 on error */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1582 | static int |
| 1583 | nc_open_netconf_channel(struct nc_session *session, int timeout) |
| 1584 | { |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1585 | int ret; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1586 | struct timespec ts_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1587 | |
| 1588 | /* message callback is executed twice to give chance for the channel to be |
| 1589 | * created if timeout == 0 (it takes 2 messages, channel-open, subsystem-request) */ |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1590 | if (!timeout) { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1591 | if (!nc_session_is_connected(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1592 | ERR(session, "Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1593 | return -1; |
| 1594 | } |
| 1595 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1596 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1597 | if (ret != SSH_OK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1598 | ERR(session, "Failed to receive SSH messages on a session (%s).", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1599 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1600 | return -1; |
| 1601 | } |
| 1602 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1603 | if (!session->ti.libssh.channel) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1604 | return 0; |
| 1605 | } |
| 1606 | |
| 1607 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1608 | if (ret != SSH_OK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1609 | ERR(session, "Failed to receive SSH messages on a session (%s).", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1610 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1611 | return -1; |
| 1612 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1613 | |
| 1614 | if (!(session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1615 | /* we did not receive subsystem-request, timeout */ |
| 1616 | return 0; |
| 1617 | } |
| 1618 | |
| 1619 | return 1; |
| 1620 | } |
| 1621 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1622 | if (timeout > -1) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1623 | nc_gettimespec_mono_add(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1624 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1625 | while (1) { |
| 1626 | if (!nc_session_is_connected(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1627 | ERR(session, "Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1628 | return -1; |
| 1629 | } |
| 1630 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1631 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1632 | if (ret != SSH_OK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1633 | ERR(session, "Failed to receive SSH messages on a session (%s).", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1634 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1635 | return -1; |
| 1636 | } |
| 1637 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1638 | if (session->ti.libssh.channel && (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1639 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1640 | } |
| 1641 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1642 | usleep(NC_TIMEOUT_STEP); |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1643 | if ((timeout > -1) && (nc_difftimespec_cur(&ts_timeout) < 1)) { |
| 1644 | /* timeout */ |
| 1645 | ERR(session, "Failed to start \"netconf\" SSH subsystem for too long, disconnecting."); |
| 1646 | break; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1647 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1648 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1649 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1650 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1651 | } |
| 1652 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1653 | static int |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1654 | nc_ssh_bind_add_hostkeys(ssh_bind sbind, char **hostkeys, uint8_t hostkey_count) |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1655 | { |
| 1656 | uint8_t i; |
| 1657 | char *privkey_path, *privkey_data; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1658 | int ret; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1659 | NC_SSH_KEY_TYPE privkey_type; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1660 | |
| 1661 | if (!server_opts.hostkey_clb) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1662 | ERR(NULL, "Callback for retrieving SSH host keys not set."); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1663 | return -1; |
| 1664 | } |
| 1665 | |
| 1666 | for (i = 0; i < hostkey_count; ++i) { |
| 1667 | privkey_path = privkey_data = NULL; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1668 | if (server_opts.hostkey_clb(hostkeys[i], server_opts.hostkey_data, &privkey_path, &privkey_data, &privkey_type)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1669 | ERR(NULL, "Host key callback failed."); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1670 | return -1; |
| 1671 | } |
| 1672 | |
| 1673 | if (privkey_data) { |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1674 | 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] | 1675 | if (!privkey_path) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1676 | ERR(NULL, "Temporarily storing a host key into a file failed (%s).", strerror(errno)); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1677 | free(privkey_data); |
| 1678 | return -1; |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path); |
| 1683 | |
| 1684 | /* cleanup */ |
| 1685 | if (privkey_data && unlink(privkey_path)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1686 | WRN(NULL, "Removing a temporary host key file \"%s\" failed (%s).", privkey_path, strerror(errno)); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1687 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1688 | free(privkey_data); |
| 1689 | |
| 1690 | if (ret != SSH_OK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1691 | ERR(NULL, "Failed to set hostkey \"%s\" (%s).", hostkeys[i], privkey_path); |
Michal Vasko | 80075de | 2017-07-10 11:38:52 +0200 | [diff] [blame] | 1692 | } |
| 1693 | free(privkey_path); |
| 1694 | |
| 1695 | if (ret != SSH_OK) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1696 | return -1; |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | return 0; |
| 1701 | } |
| 1702 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1703 | int |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1704 | nc_accept_ssh_session(struct nc_session *session, int sock, int timeout) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1705 | { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1706 | ssh_bind sbind; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1707 | struct nc_server_ssh_opts *opts; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1708 | int libssh_auth_methods = 0, ret; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1709 | struct timespec ts_timeout; |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame^] | 1710 | ssh_message msg; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1711 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 1712 | opts = session->data; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1713 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1714 | /* other transport-specific data */ |
| 1715 | session->ti_type = NC_TI_LIBSSH; |
| 1716 | session->ti.libssh.session = ssh_new(); |
| 1717 | if (!session->ti.libssh.session) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1718 | ERR(NULL, "Failed to initialize a new SSH session."); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1719 | close(sock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1720 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1721 | } |
| 1722 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1723 | if (opts->auth_methods & NC_SSH_AUTH_PUBLICKEY) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1724 | libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY; |
| 1725 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1726 | if (opts->auth_methods & NC_SSH_AUTH_PASSWORD) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1727 | libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD; |
| 1728 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1729 | if (opts->auth_methods & NC_SSH_AUTH_INTERACTIVE) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1730 | libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE; |
| 1731 | } |
| 1732 | ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods); |
| 1733 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1734 | sbind = ssh_bind_new(); |
| 1735 | if (!sbind) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1736 | ERR(session, "Failed to create an SSH bind."); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1737 | close(sock); |
| 1738 | return -1; |
| 1739 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1740 | |
| 1741 | if (nc_ssh_bind_add_hostkeys(sbind, opts->hostkeys, opts->hostkey_count)) { |
| 1742 | close(sock); |
| 1743 | ssh_bind_free(sbind); |
| 1744 | return -1; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1745 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1746 | |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1747 | /* remember that this session was just set as nc_sshcb_msg() parameter */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1748 | session->flags |= NC_SESSION_SSH_MSG_CB; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1749 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1750 | if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1751 | ERR(session, "SSH failed to accept a new connection (%s).", ssh_get_error(sbind)); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1752 | close(sock); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1753 | ssh_bind_free(sbind); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1754 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1755 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1756 | ssh_bind_free(sbind); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1757 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1758 | ssh_set_blocking(session->ti.libssh.session, 0); |
| 1759 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1760 | if (timeout > -1) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1761 | nc_gettimespec_mono_add(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1762 | } |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1763 | while ((ret = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) { |
Michal Vasko | e65b449 | 2016-03-03 11:57:51 +0100 | [diff] [blame] | 1764 | /* this tends to take longer */ |
| 1765 | usleep(NC_TIMEOUT_STEP * 20); |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1766 | if ((timeout > -1) && (nc_difftimespec_cur(&ts_timeout) < 1)) { |
| 1767 | break; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1768 | } |
| 1769 | } |
| 1770 | if (ret == SSH_AGAIN) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1771 | ERR(session, "SSH key exchange timeout."); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1772 | return 0; |
| 1773 | } else if (ret != SSH_OK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1774 | ERR(session, "SSH key exchange error (%s).", ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1775 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | /* authenticate */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1779 | if (opts->auth_timeout) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1780 | nc_gettimespec_mono_add(&ts_timeout, opts->auth_timeout * 1000); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1781 | } |
| 1782 | while (1) { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1783 | if (!nc_session_is_connected(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1784 | ERR(session, "Communication SSH socket unexpectedly closed."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1785 | return -1; |
| 1786 | } |
| 1787 | |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame^] | 1788 | msg = ssh_message_get(session->ti.libssh.session); |
| 1789 | if (msg) { |
| 1790 | if (nc_sshcb_msg(session->ti.libssh.session, msg, (void *) session)) { |
| 1791 | ssh_message_reply_default(msg); |
| 1792 | } |
| 1793 | ssh_message_free(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1794 | } |
| 1795 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1796 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
| 1797 | break; |
| 1798 | } |
| 1799 | |
Michal Vasko | 145ae67 | 2017-02-07 10:57:27 +0100 | [diff] [blame] | 1800 | if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1801 | ERR(session, "Too many failed authentication attempts of user \"%s\".", session->username); |
Michal Vasko | 145ae67 | 2017-02-07 10:57:27 +0100 | [diff] [blame] | 1802 | return -1; |
| 1803 | } |
| 1804 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1805 | usleep(NC_TIMEOUT_STEP); |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1806 | if ((opts->auth_timeout) && (nc_difftimespec_cur(&ts_timeout) < 1)) { |
| 1807 | /* timeout */ |
| 1808 | break; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1809 | } |
| 1810 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1811 | |
| 1812 | if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) { |
| 1813 | /* timeout */ |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1814 | if (session->username) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1815 | ERR(session, "User \"%s\" failed to authenticate for too long, disconnecting.", session->username); |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1816 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1817 | ERR(session, "User failed to authenticate for too long, disconnecting."); |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1818 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1819 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1820 | } |
| 1821 | |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame^] | 1822 | /* set the message callback after a successful authentication */ |
| 1823 | ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, session); |
| 1824 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1825 | /* open channel */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1826 | ret = nc_open_netconf_channel(session, timeout); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1827 | if (ret < 1) { |
| 1828 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1829 | } |
| 1830 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1831 | session->flags &= ~NC_SESSION_SSH_NEW_MSG; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1832 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1833 | } |
| 1834 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1835 | API NC_MSG_TYPE |
| 1836 | nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session) |
| 1837 | { |
| 1838 | NC_MSG_TYPE msgtype; |
| 1839 | struct nc_session *new_session = NULL; |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1840 | struct timespec ts_cur; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1841 | |
| 1842 | if (!orig_session) { |
| 1843 | ERRARG("orig_session"); |
| 1844 | return NC_MSG_ERROR; |
| 1845 | } else if (!session) { |
| 1846 | ERRARG("session"); |
| 1847 | return NC_MSG_ERROR; |
| 1848 | } |
| 1849 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1850 | if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) && |
| 1851 | orig_session->ti.libssh.next) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1852 | for (new_session = orig_session->ti.libssh.next; |
| 1853 | new_session != orig_session; |
| 1854 | new_session = new_session->ti.libssh.next) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1855 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel && |
| 1856 | (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1857 | /* we found our session */ |
| 1858 | break; |
| 1859 | } |
| 1860 | } |
| 1861 | if (new_session == orig_session) { |
| 1862 | new_session = NULL; |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | if (!new_session) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1867 | ERR(orig_session, "Session does not have a NETCONF SSH channel ready."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1868 | return NC_MSG_ERROR; |
| 1869 | } |
| 1870 | |
| 1871 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 1872 | new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1873 | |
| 1874 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1875 | msgtype = nc_handshake_io(new_session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1876 | if (msgtype != NC_MSG_HELLO) { |
| 1877 | return msgtype; |
| 1878 | } |
| 1879 | |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1880 | nc_gettimespec_real_add(&ts_cur, 0); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1881 | new_session->opts.server.session_start = ts_cur.tv_sec; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1882 | nc_gettimespec_mono_add(&ts_cur, 0); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1883 | new_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1884 | new_session->status = NC_STATUS_RUNNING; |
| 1885 | *session = new_session; |
| 1886 | |
| 1887 | return msgtype; |
| 1888 | } |
| 1889 | |
| 1890 | API NC_MSG_TYPE |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1891 | 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] | 1892 | { |
Michal Vasko | bdcf236 | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1893 | uint8_t q_id; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1894 | NC_MSG_TYPE msgtype; |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1895 | struct nc_session *new_session = NULL, *cur_session; |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1896 | struct timespec ts_cur; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1897 | uint16_t i; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1898 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1899 | if (!ps) { |
| 1900 | ERRARG("ps"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1901 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1902 | } else if (!session) { |
| 1903 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1904 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1905 | } |
| 1906 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1907 | /* LOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1908 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1909 | return NC_MSG_ERROR; |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1910 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1911 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1912 | for (i = 0; i < ps->session_count; ++i) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1913 | cur_session = ps->sessions[i]->session; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1914 | if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH) && |
| 1915 | cur_session->ti.libssh.next) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1916 | /* an SSH session with more channels */ |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1917 | for (new_session = cur_session->ti.libssh.next; |
| 1918 | new_session != cur_session; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1919 | new_session = new_session->ti.libssh.next) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1920 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel && |
| 1921 | (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1922 | /* we found our session */ |
| 1923 | break; |
| 1924 | } |
| 1925 | } |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1926 | if (new_session != cur_session) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1927 | break; |
| 1928 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1929 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1930 | new_session = NULL; |
| 1931 | } |
| 1932 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1933 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1934 | /* UNLOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1935 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1936 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1937 | if (!new_session) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1938 | ERR(NULL, "No session with a NETCONF SSH channel ready was found."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1939 | return NC_MSG_ERROR; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 1943 | new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1944 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1945 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1946 | msgtype = nc_handshake_io(new_session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1947 | if (msgtype != NC_MSG_HELLO) { |
| 1948 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1949 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1950 | |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1951 | nc_gettimespec_real_add(&ts_cur, 0); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1952 | new_session->opts.server.session_start = ts_cur.tv_sec; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1953 | nc_gettimespec_mono_add(&ts_cur, 0); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1954 | new_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1955 | new_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1956 | *session = new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1957 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1958 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1959 | } |