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