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 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 554 | NC_CHECK_ARG_RET(NULL, auth_attempts, -1); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 555 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 556 | opts->auth_attempts = auth_attempts; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 561 | 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] | 562 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 563 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 564 | struct nc_endpt *endpt; |
| 565 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 566 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 567 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 568 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 569 | return -1; |
| 570 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 571 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 572 | ret = nc_server_ssh_set_auth_attempts(auth_attempts, endpt->opts.ssh); |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 573 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 574 | /* UNLOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 575 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 576 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 577 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | API int |
Michal Vasko | cbad4c5 | 2019-06-27 16:30:35 +0200 | [diff] [blame] | 581 | 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] | 582 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 583 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 584 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 585 | struct nc_ch_endpt *endpt; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 586 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 587 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 588 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client); |
| 589 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 590 | return -1; |
| 591 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 592 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 593 | ret = nc_server_ssh_set_auth_attempts(auth_attempts, endpt->opts.ssh); |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 594 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 595 | /* UNLOCK */ |
| 596 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 597 | |
| 598 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | static int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 602 | 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] | 603 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 604 | NC_CHECK_ARG_RET(NULL, auth_timeout, -1); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 605 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 606 | opts->auth_timeout = auth_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | API int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 611 | 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] | 612 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 613 | int ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 614 | struct nc_endpt *endpt; |
| 615 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 616 | /* LOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 617 | endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 618 | if (!endpt) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 619 | return -1; |
| 620 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 621 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 622 | ret = nc_server_ssh_set_auth_timeout(auth_timeout, endpt->opts.ssh); |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 623 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 624 | /* UNLOCK */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 625 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 626 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 627 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | API int |
Michal Vasko | cbad4c5 | 2019-06-27 16:30:35 +0200 | [diff] [blame] | 631 | 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] | 632 | { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 633 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 634 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 635 | struct nc_ch_endpt *endpt; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 636 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 637 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 638 | endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client); |
| 639 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 640 | return -1; |
| 641 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 642 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 643 | ret = nc_server_ssh_set_auth_timeout(auth_timeout, endpt->opts.ssh); |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 644 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 645 | /* UNLOCK */ |
| 646 | nc_server_ch_client_unlock(client); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 647 | |
| 648 | return ret; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | static int |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 652 | _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] | 653 | { |
Michal Vasko | 0911189 | 2021-07-26 09:30:20 +0200 | [diff] [blame] | 654 | int ret = 0; |
| 655 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 656 | /* LOCK */ |
| 657 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 658 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 659 | ++server_opts.authkey_count; |
| 660 | server_opts.authkeys = nc_realloc(server_opts.authkeys, server_opts.authkey_count * sizeof *server_opts.authkeys); |
| 661 | if (!server_opts.authkeys) { |
| 662 | ERRMEM; |
Michal Vasko | 0911189 | 2021-07-26 09:30:20 +0200 | [diff] [blame] | 663 | ret = -1; |
| 664 | goto cleanup; |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 665 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 666 | server_opts.authkeys[server_opts.authkey_count - 1].path = pubkey_path ? strdup(pubkey_path) : NULL; |
| 667 | 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] | 668 | server_opts.authkeys[server_opts.authkey_count - 1].type = type; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 669 | server_opts.authkeys[server_opts.authkey_count - 1].username = strdup(username); |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 670 | |
Michal Vasko | 0911189 | 2021-07-26 09:30:20 +0200 | [diff] [blame] | 671 | cleanup: |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 672 | /* UNLOCK */ |
| 673 | pthread_mutex_unlock(&server_opts.authkey_lock); |
Michal Vasko | 0911189 | 2021-07-26 09:30:20 +0200 | [diff] [blame] | 674 | return ret; |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | API int |
| 678 | 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] | 679 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 680 | if (!pubkey_path) { |
| 681 | ERRARG("pubkey_path"); |
| 682 | return -1; |
| 683 | } else if (!username) { |
| 684 | ERRARG("username"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 685 | return -1; |
| 686 | } |
| 687 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 688 | return _nc_server_ssh_add_authkey(pubkey_path, NULL, 0, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | API int |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 692 | 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] | 693 | { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 694 | if (!pubkey_base64) { |
| 695 | ERRARG("pubkey_base64"); |
| 696 | return -1; |
| 697 | } else if (!type) { |
| 698 | ERRARG("type"); |
| 699 | return -1; |
| 700 | } else if (!username) { |
| 701 | ERRARG("username"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 702 | return -1; |
| 703 | } |
| 704 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 705 | return _nc_server_ssh_add_authkey(NULL, pubkey_base64, type, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | API int |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 709 | 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] | 710 | const char *username) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 711 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 712 | uint32_t i; |
| 713 | int ret = -1; |
| 714 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 715 | /* LOCK */ |
| 716 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 717 | |
| 718 | if (!pubkey_path && !pubkey_base64 && !type && !username) { |
| 719 | for (i = 0; i < server_opts.authkey_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 720 | free(server_opts.authkeys[i].path); |
| 721 | free(server_opts.authkeys[i].base64); |
| 722 | free(server_opts.authkeys[i].username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 723 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 724 | ret = 0; |
| 725 | } |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 726 | free(server_opts.authkeys); |
| 727 | server_opts.authkeys = NULL; |
| 728 | server_opts.authkey_count = 0; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 729 | } else { |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 730 | for (i = 0; i < server_opts.authkey_count; ++i) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 731 | if ((!pubkey_path || !strcmp(server_opts.authkeys[i].path, pubkey_path)) && |
| 732 | (!pubkey_base64 || !strcmp(server_opts.authkeys[i].base64, pubkey_base64)) && |
| 733 | (!type || (server_opts.authkeys[i].type == type)) && |
| 734 | (!username || !strcmp(server_opts.authkeys[i].username, username))) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 735 | free(server_opts.authkeys[i].path); |
| 736 | free(server_opts.authkeys[i].base64); |
| 737 | free(server_opts.authkeys[i].username); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 738 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 739 | --server_opts.authkey_count; |
| 740 | if (i < server_opts.authkey_count) { |
| 741 | memcpy(&server_opts.authkeys[i], &server_opts.authkeys[server_opts.authkey_count], |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 742 | sizeof *server_opts.authkeys); |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 743 | } else if (!server_opts.authkey_count) { |
| 744 | free(server_opts.authkeys); |
| 745 | server_opts.authkeys = NULL; |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 746 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 747 | |
| 748 | ret = 0; |
| 749 | } |
| 750 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 751 | } |
| 752 | |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 753 | /* UNLOCK */ |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 754 | pthread_mutex_unlock(&server_opts.authkey_lock); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 755 | |
| 756 | return ret; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | void |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 760 | nc_server_ssh_clear_opts(struct nc_server_ssh_opts *opts) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 761 | { |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 762 | nc_server_ssh_del_hostkey(NULL, -1, opts); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 763 | } |
| 764 | |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 765 | #ifdef HAVE_SHADOW |
| 766 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 767 | /** |
| 768 | * @brief Get passwd entry for a user. |
| 769 | * |
| 770 | * @param[in] username Name of the user. |
| 771 | * @param[in] pwd_buf Passwd entry buffer. |
| 772 | * @param[in,out] buf Passwd entry string buffer. |
| 773 | * @param[in,out] buf_size Current @p buf size. |
| 774 | * @return Found passwd entry for the user, NULL if none found. |
| 775 | */ |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 776 | static struct passwd * |
| 777 | auth_password_getpwnam(const char *username, struct passwd *pwd_buf, char **buf, size_t *buf_size) |
| 778 | { |
| 779 | struct passwd *pwd = NULL; |
| 780 | char *mem; |
Jan Kundrát | aa32310 | 2021-10-08 20:10:50 +0200 | [diff] [blame] | 781 | int r = 0; |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 782 | |
| 783 | do { |
Jan Kundrát | aa32310 | 2021-10-08 20:10:50 +0200 | [diff] [blame] | 784 | r = getpwnam_r(username, pwd_buf, *buf, *buf_size, &pwd); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 785 | if (pwd) { |
| 786 | /* entry found */ |
| 787 | break; |
| 788 | } |
| 789 | |
Jan Kundrát | aa32310 | 2021-10-08 20:10:50 +0200 | [diff] [blame] | 790 | if (r == ERANGE) { |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 791 | /* small buffer, enlarge */ |
| 792 | *buf_size <<= 2; |
| 793 | mem = realloc(*buf, *buf_size); |
| 794 | if (!mem) { |
| 795 | ERRMEM; |
| 796 | return NULL; |
| 797 | } |
| 798 | *buf = mem; |
| 799 | } |
Jan Kundrát | aa32310 | 2021-10-08 20:10:50 +0200 | [diff] [blame] | 800 | } while (r == ERANGE); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 801 | |
| 802 | return pwd; |
| 803 | } |
| 804 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 805 | /** |
| 806 | * @brief Get shadow entry for a user. |
| 807 | * |
| 808 | * @param[in] username Name of the user. |
| 809 | * @param[in] spwd_buf Shadow entry buffer. |
| 810 | * @param[in,out] buf Shadow entry string buffer. |
| 811 | * @param[in,out] buf_size Current @p buf size. |
| 812 | * @return Found shadow entry for the user, NULL if none found. |
| 813 | */ |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 814 | static struct spwd * |
| 815 | auth_password_getspnam(const char *username, struct spwd *spwd_buf, char **buf, size_t *buf_size) |
| 816 | { |
| 817 | struct spwd *spwd = NULL; |
| 818 | char *mem; |
Jan Kundrát | aa32310 | 2021-10-08 20:10:50 +0200 | [diff] [blame] | 819 | int r = 0; |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 820 | |
| 821 | do { |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 822 | # ifndef __QNXNTO__ |
Jan Kundrát | aa32310 | 2021-10-08 20:10:50 +0200 | [diff] [blame] | 823 | r = getspnam_r(username, spwd_buf, *buf, *buf_size, &spwd); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 824 | # else |
| 825 | spwd = getspnam_r(username, spwd_buf, *buf, *buf_size); |
| 826 | # endif |
| 827 | if (spwd) { |
| 828 | /* entry found */ |
| 829 | break; |
| 830 | } |
| 831 | |
Jan Kundrát | aa32310 | 2021-10-08 20:10:50 +0200 | [diff] [blame] | 832 | if (r == ERANGE) { |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 833 | /* small buffer, enlarge */ |
| 834 | *buf_size <<= 2; |
| 835 | mem = realloc(*buf, *buf_size); |
| 836 | if (!mem) { |
| 837 | ERRMEM; |
| 838 | return NULL; |
| 839 | } |
| 840 | *buf = mem; |
| 841 | } |
Jan Kundrát | aa32310 | 2021-10-08 20:10:50 +0200 | [diff] [blame] | 842 | } while (r == ERANGE); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 843 | |
| 844 | return spwd; |
| 845 | } |
| 846 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 847 | /** |
| 848 | * @brief Get hashed system apssword for a user. |
| 849 | * |
| 850 | * @param[in] username Name of the user. |
| 851 | * @return Hashed password of @p username. |
| 852 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 853 | static char * |
| 854 | auth_password_get_pwd_hash(const char *username) |
| 855 | { |
| 856 | struct passwd *pwd, pwd_buf; |
| 857 | struct spwd *spwd, spwd_buf; |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 858 | char *pass_hash = NULL, *buf = NULL; |
| 859 | size_t buf_size = 256; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 860 | |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 861 | buf = malloc(buf_size); |
| 862 | if (!buf) { |
| 863 | ERRMEM; |
| 864 | goto error; |
| 865 | } |
| 866 | |
| 867 | pwd = auth_password_getpwnam(username, &pwd_buf, &buf, &buf_size); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 868 | if (!pwd) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 869 | VRB(NULL, "User \"%s\" not found locally.", username); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 870 | goto error; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | if (!strcmp(pwd->pw_passwd, "x")) { |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 874 | spwd = auth_password_getspnam(username, &spwd_buf, &buf, &buf_size); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 875 | if (!spwd) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 876 | VRB(NULL, "Failed to retrieve the shadow entry for \"%s\".", username); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 877 | goto error; |
Michal Vasko | 22b4fe7 | 2020-11-04 08:52:29 +0100 | [diff] [blame] | 878 | } 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] | 879 | WRN(NULL, "User \"%s\" account has expired.", username); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 880 | goto error; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | pass_hash = spwd->sp_pwdp; |
| 884 | } else { |
| 885 | pass_hash = pwd->pw_passwd; |
| 886 | } |
| 887 | |
| 888 | if (!pass_hash) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 889 | ERR(NULL, "No password could be retrieved for \"%s\".", username); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 890 | goto error; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | /* check the hash structure for special meaning */ |
| 894 | if (!strcmp(pass_hash, "*") || !strcmp(pass_hash, "!")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 895 | 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] | 896 | goto error; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 897 | } |
| 898 | if (!strcmp(pass_hash, "*NP*")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 899 | 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] | 900 | goto error; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 901 | } |
| 902 | |
Michal Vasko | 963f6c0 | 2021-07-26 15:55:44 +0200 | [diff] [blame] | 903 | pass_hash = strdup(pass_hash); |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 904 | free(buf); |
Michal Vasko | 963f6c0 | 2021-07-26 15:55:44 +0200 | [diff] [blame] | 905 | return pass_hash; |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 906 | |
| 907 | error: |
| 908 | free(buf); |
| 909 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 910 | } |
| 911 | |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 912 | #else |
| 913 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 914 | /** |
| 915 | * @brief Get hashed system password for a user. |
| 916 | * |
| 917 | * @param[in] username Name of the user. |
| 918 | * @return Hashed password of @p username. |
| 919 | */ |
Michal Vasko | 7a86615 | 2021-07-22 11:01:13 +0200 | [diff] [blame] | 920 | static char * |
| 921 | auth_password_get_pwd_hash(const char *username) |
| 922 | { |
| 923 | (void)username; |
| 924 | return strdup(""); |
| 925 | } |
| 926 | |
| 927 | #endif |
| 928 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 929 | /** |
| 930 | * @brief Compare hashed password with a cleartext password for a match. |
| 931 | * |
| 932 | * @param[in] pass_hash Hashed password. |
| 933 | * @param[in] pass_clear Cleartext password. |
| 934 | * @return 0 on match. |
| 935 | * @return non-zero if not a match. |
| 936 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 937 | static int |
| 938 | auth_password_compare_pwd(const char *pass_hash, const char *pass_clear) |
| 939 | { |
| 940 | char *new_pass_hash; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 941 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 942 | #ifdef HAVE_CRYPT_R |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 943 | struct crypt_data cdata; |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 944 | #endif |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 945 | |
| 946 | if (!pass_hash[0]) { |
| 947 | if (!pass_clear[0]) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 948 | WRN(NULL, "User authentication successful with an empty password!"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 949 | return 0; |
| 950 | } else { |
| 951 | /* the user did now know he does not need any password, |
| 952 | * (which should not be used) so deny authentication */ |
| 953 | return 1; |
| 954 | } |
| 955 | } |
| 956 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 957 | #ifdef HAVE_CRYPT_R |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 958 | cdata.initialized = 0; |
| 959 | new_pass_hash = crypt_r(pass_clear, pass_hash, &cdata); |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 960 | #else |
Mislav Novakovic | ce9a7ef | 2017-08-08 13:45:52 +0200 | [diff] [blame] | 961 | pthread_mutex_lock(&crypt_lock); |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 962 | new_pass_hash = crypt(pass_clear, pass_hash); |
Mislav Novakovic | ce9a7ef | 2017-08-08 13:45:52 +0200 | [diff] [blame] | 963 | pthread_mutex_unlock(&crypt_lock); |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 964 | #endif |
Andrew Langefeld | 158d6fd | 2018-06-11 18:51:44 -0500 | [diff] [blame] | 965 | |
| 966 | if (!new_pass_hash) { |
| 967 | return 1; |
| 968 | } |
| 969 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 970 | return strcmp(new_pass_hash, pass_hash); |
| 971 | } |
| 972 | |
| 973 | static void |
| 974 | nc_sshcb_auth_password(struct nc_session *session, ssh_message msg) |
| 975 | { |
| 976 | char *pass_hash; |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 977 | int auth_ret = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 978 | |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 979 | if (server_opts.passwd_auth_clb) { |
| 980 | auth_ret = server_opts.passwd_auth_clb(session, ssh_message_auth_password(msg), server_opts.passwd_auth_data); |
| 981 | } else { |
| 982 | pass_hash = auth_password_get_pwd_hash(session->username); |
| 983 | if (pass_hash) { |
| 984 | auth_ret = auth_password_compare_pwd(pass_hash, ssh_message_auth_password(msg)); |
| 985 | free(pass_hash); |
| 986 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 987 | } |
| 988 | |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 989 | if (!auth_ret) { |
| 990 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 991 | VRB(session, "User \"%s\" authenticated.", session->username); |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 992 | ssh_message_auth_reply_success(msg, 0); |
| 993 | } else { |
| 994 | ++session->opts.server.ssh_auth_attempts; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 995 | VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username, |
| 996 | session->opts.server.ssh_auth_attempts); |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 997 | ssh_message_reply_default(msg); |
| 998 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
Michal Vasko | 0d81c57 | 2022-09-26 10:39:31 +0200 | [diff] [blame] | 1001 | #ifdef HAVE_LIBPAM |
| 1002 | |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1003 | /** |
| 1004 | * @brief PAM conversation function, which serves as a callback for exchanging messages between the client and a PAM module. |
| 1005 | * |
| 1006 | * @param[in] n_messages Number of messages. |
| 1007 | * @param[in] msg PAM module's messages. |
| 1008 | * @param[out] resp User responses. |
| 1009 | * @param[in] appdata_ptr Callback's data. |
| 1010 | * @return PAM_SUCCESS on success; |
| 1011 | * @return PAM_BUF_ERR on memory allocation error; |
| 1012 | * @return PAM_CONV_ERR otherwise. |
| 1013 | */ |
| 1014 | static int |
| 1015 | nc_pam_conv_clb(int n_messages, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr) |
| 1016 | { |
| 1017 | int i, j, t, r = PAM_SUCCESS, n_answers, n_requests = n_messages; |
| 1018 | const char **prompts = NULL; |
| 1019 | char *echo = NULL; |
| 1020 | const char *name = "Keyboard-Interactive Authentication"; |
| 1021 | const char *instruction = "Please enter your authentication token"; |
| 1022 | ssh_message reply = NULL; |
| 1023 | struct nc_pam_thread_arg *clb_data = appdata_ptr; |
| 1024 | ssh_session libssh_session; |
| 1025 | struct timespec ts_timeout; |
| 1026 | struct nc_server_ssh_opts *opts; |
| 1027 | |
| 1028 | libssh_session = clb_data->session->ti.libssh.session; |
| 1029 | opts = clb_data->session->data; |
| 1030 | |
| 1031 | /* PAM_MAX_NUM_MSG == 32 by default */ |
| 1032 | if ((n_messages <= 0) || (n_messages >= PAM_MAX_NUM_MSG)) { |
| 1033 | ERR(NULL, "Bad number of PAM messages (#%d).", n_messages); |
| 1034 | r = PAM_CONV_ERR; |
| 1035 | goto cleanup; |
| 1036 | } |
| 1037 | |
| 1038 | /* only accepting these 4 types of messages */ |
| 1039 | for (i = 0; i < n_messages; i++) { |
| 1040 | t = msg[i]->msg_style; |
| 1041 | if ((t != PAM_PROMPT_ECHO_OFF) && (t != PAM_PROMPT_ECHO_ON) && (t != PAM_TEXT_INFO) && (t != PAM_ERROR_MSG)) { |
| 1042 | ERR(NULL, "PAM conversation callback received an unexpected type of message."); |
| 1043 | r = PAM_CONV_ERR; |
| 1044 | goto cleanup; |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | /* display messages with errors and/or some information and count the amount of actual authentication challenges */ |
| 1049 | for (i = 0; i < n_messages; i++) { |
| 1050 | if (msg[i]->msg_style == PAM_TEXT_INFO) { |
| 1051 | VRB(NULL, "PAM conversation callback received a message with some information for the client (%s).", msg[i]->msg); |
| 1052 | n_requests--; |
| 1053 | } |
| 1054 | if (msg[i]->msg_style == PAM_ERROR_MSG) { |
| 1055 | ERR(NULL, "PAM conversation callback received an error message (%s).", msg[i]->msg); |
| 1056 | r = PAM_CONV_ERR; |
| 1057 | goto cleanup; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | /* there are no requests left for the user, only messages with some information for the client were sent */ |
| 1062 | if (n_requests <= 0) { |
| 1063 | r = PAM_SUCCESS; |
| 1064 | goto cleanup; |
| 1065 | } |
| 1066 | |
| 1067 | /* it is the PAM module's responsibility to release both, this array and the responses themselves */ |
| 1068 | *resp = calloc(n_requests, sizeof **resp); |
| 1069 | prompts = calloc(n_requests, sizeof *prompts); |
| 1070 | echo = calloc(n_requests, sizeof *echo); |
| 1071 | if (!(*resp) || !prompts || !echo) { |
| 1072 | ERRMEM; |
| 1073 | r = PAM_BUF_ERR; |
| 1074 | goto cleanup; |
| 1075 | } |
| 1076 | |
| 1077 | /* set the prompts for the user */ |
| 1078 | j = 0; |
| 1079 | for (i = 0; i < n_messages; i++) { |
| 1080 | if ((msg[i]->msg_style == PAM_PROMPT_ECHO_ON) || (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF)) { |
| 1081 | prompts[j++] = msg[i]->msg; |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | /* iterate over all the messages and adjust the echo array accordingly */ |
| 1086 | j = 0; |
| 1087 | for (i = 0; i < n_messages; i++) { |
| 1088 | if (msg[i]->msg_style == PAM_PROMPT_ECHO_ON) { |
| 1089 | echo[j++] = 1; |
| 1090 | } |
| 1091 | if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF) { |
| 1092 | /* no need to set to 0 because of calloc */ |
| 1093 | j++; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | /* print all the keyboard-interactive challenges to the user */ |
| 1098 | r = ssh_message_auth_interactive_request(clb_data->msg, name, instruction, n_requests, prompts, echo); |
| 1099 | if (r != SSH_OK) { |
| 1100 | ERR(NULL, "Failed to send an authentication request."); |
| 1101 | r = PAM_CONV_ERR; |
| 1102 | goto cleanup; |
| 1103 | } |
| 1104 | |
| 1105 | if (opts->auth_timeout) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1106 | nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000); |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /* get user's replies */ |
| 1110 | do { |
| 1111 | if (!nc_session_is_connected(clb_data->session)) { |
| 1112 | ERR(NULL, "Communication SSH socket unexpectedly closed."); |
| 1113 | r = PAM_CONV_ERR; |
| 1114 | goto cleanup; |
| 1115 | } |
| 1116 | |
| 1117 | reply = ssh_message_get(libssh_session); |
| 1118 | if (reply) { |
| 1119 | break; |
| 1120 | } |
| 1121 | |
| 1122 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1123 | } while ((opts->auth_timeout) && (nc_timeouttime_cur_diff(&ts_timeout) >= 1)); |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1124 | |
| 1125 | if (!reply) { |
| 1126 | ERR(NULL, "Authentication timeout."); |
| 1127 | r = PAM_CONV_ERR; |
| 1128 | goto cleanup; |
| 1129 | } |
| 1130 | |
| 1131 | /* check if the amount of replies matches the amount of requests */ |
| 1132 | n_answers = ssh_userauth_kbdint_getnanswers(libssh_session); |
| 1133 | if (n_answers != n_requests) { |
| 1134 | ERR(NULL, "Expected %d response(s), got %d.", n_requests, n_answers); |
| 1135 | r = PAM_CONV_ERR; |
| 1136 | goto cleanup; |
| 1137 | } |
| 1138 | |
| 1139 | /* give the replies to a PAM module */ |
| 1140 | for (i = 0; i < n_answers; i++) { |
| 1141 | (*resp)[i].resp = strdup(ssh_userauth_kbdint_getanswer(libssh_session, i)); |
| 1142 | /* it should be the caller's responsibility to free this, however if mem alloc fails, |
| 1143 | * it is safer to free the responses here and set them to NULL */ |
| 1144 | if ((*resp)[i].resp == NULL) { |
| 1145 | for (j = 0; j < i; j++) { |
| 1146 | free((*resp)[j].resp); |
| 1147 | (*resp)[j].resp = NULL; |
| 1148 | } |
| 1149 | ERRMEM; |
| 1150 | r = PAM_BUF_ERR; |
| 1151 | goto cleanup; |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | cleanup: |
| 1156 | ssh_message_free(reply); |
| 1157 | free(prompts); |
| 1158 | free(echo); |
| 1159 | return r; |
| 1160 | } |
| 1161 | |
| 1162 | /** |
| 1163 | * @brief Handles authentication via Linux PAM. |
| 1164 | * |
| 1165 | * @param[in] session NETCONF session. |
| 1166 | * @param[in] ssh_msg SSH message with a keyboard-interactive authentication request. |
| 1167 | * @return PAM_SUCCESS on success; |
| 1168 | * @return PAM error otherwise. |
| 1169 | */ |
| 1170 | static int |
| 1171 | nc_pam_auth(struct nc_session *session, ssh_message ssh_msg) |
| 1172 | { |
| 1173 | pam_handle_t *pam_h = NULL; |
| 1174 | int ret; |
| 1175 | struct nc_pam_thread_arg clb_data; |
| 1176 | struct pam_conv conv; |
| 1177 | |
| 1178 | /* structure holding callback's data */ |
| 1179 | clb_data.msg = ssh_msg; |
| 1180 | clb_data.session = session; |
| 1181 | |
| 1182 | /* PAM conversation structure holding the callback and it's data */ |
| 1183 | conv.conv = nc_pam_conv_clb; |
| 1184 | conv.appdata_ptr = &clb_data; |
| 1185 | |
| 1186 | /* initialize PAM and see if the given configuration file exists */ |
Michal Vasko | 0d81c57 | 2022-09-26 10:39:31 +0200 | [diff] [blame] | 1187 | # ifdef LIBPAM_HAVE_CONFDIR |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1188 | /* PAM version >= 1.4 */ |
| 1189 | 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] | 1190 | # else |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1191 | /* PAM version < 1.4 */ |
| 1192 | ret = pam_start(server_opts.conf_name, session->username, &conv, &pam_h); |
Michal Vasko | 0d81c57 | 2022-09-26 10:39:31 +0200 | [diff] [blame] | 1193 | # endif |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1194 | if (ret != PAM_SUCCESS) { |
| 1195 | ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 1196 | goto cleanup; |
| 1197 | } |
| 1198 | |
| 1199 | /* authentication based on the modules listed in the configuration file */ |
| 1200 | ret = pam_authenticate(pam_h, 0); |
| 1201 | if (ret != PAM_SUCCESS) { |
| 1202 | if (ret == PAM_ABORT) { |
| 1203 | ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 1204 | goto cleanup; |
| 1205 | } else { |
| 1206 | VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 1207 | goto cleanup; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | /* correct token entered, check other requirements(the time of the day, expired token, ...) */ |
| 1212 | ret = pam_acct_mgmt(pam_h, 0); |
| 1213 | if ((ret != PAM_SUCCESS) && (ret != PAM_NEW_AUTHTOK_REQD)) { |
| 1214 | VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 1215 | goto cleanup; |
| 1216 | } |
| 1217 | |
| 1218 | /* if a token has expired a new one will be generated */ |
| 1219 | if (ret == PAM_NEW_AUTHTOK_REQD) { |
| 1220 | VRB(NULL, "PAM warning occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 1221 | ret = pam_chauthtok(pam_h, PAM_CHANGE_EXPIRED_AUTHTOK); |
| 1222 | if (ret == PAM_SUCCESS) { |
| 1223 | VRB(NULL, "The authentication token of user \"%s\" updated successfully.", session->username); |
| 1224 | } else { |
| 1225 | ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 1226 | goto cleanup; |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | cleanup: |
| 1231 | /* destroy the PAM context */ |
| 1232 | if (pam_end(pam_h, ret) != PAM_SUCCESS) { |
| 1233 | ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 1234 | } |
| 1235 | return ret; |
| 1236 | } |
| 1237 | |
Michal Vasko | 0d81c57 | 2022-09-26 10:39:31 +0200 | [diff] [blame] | 1238 | #endif |
| 1239 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1240 | static void |
| 1241 | nc_sshcb_auth_kbdint(struct nc_session *session, ssh_message msg) |
| 1242 | { |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 1243 | int auth_ret = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1244 | |
Michal Vasko | 1c2d265 | 2023-10-17 08:53:36 +0200 | [diff] [blame] | 1245 | if (server_opts.interactive_auth_sess_clb) { |
| 1246 | auth_ret = server_opts.interactive_auth_sess_clb(session, session->ti.libssh.session, msg, |
| 1247 | server_opts.interactive_auth_data); |
| 1248 | } else if (server_opts.interactive_auth_clb) { |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 1249 | 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] | 1250 | } else { |
| 1251 | #ifdef HAVE_LIBPAM |
| 1252 | if (nc_pam_auth(session, msg) == PAM_SUCCESS) { |
| 1253 | auth_ret = 0; |
| 1254 | } |
| 1255 | #else |
| 1256 | ERR(session, "PAM-based SSH authentication is not supported."); |
| 1257 | #endif |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 1258 | } |
| 1259 | |
Robert Varga | ad7a553 | 2018-08-10 20:40:54 +0200 | [diff] [blame] | 1260 | /* We have already sent a reply */ |
| 1261 | if (auth_ret == -1) { |
| 1262 | return; |
| 1263 | } |
| 1264 | |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 1265 | /* Authenticate message based on outcome */ |
| 1266 | if (!auth_ret) { |
| 1267 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1268 | VRB(session, "User \"%s\" authenticated.", session->username); |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 1269 | ssh_message_auth_reply_success(msg, 0); |
| 1270 | } else { |
| 1271 | ++session->opts.server.ssh_auth_attempts; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1272 | VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username, |
| 1273 | session->opts.server.ssh_auth_attempts); |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 1274 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1275 | } |
| 1276 | } |
| 1277 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 1278 | /** |
| 1279 | * @brief Compare SSH key with configured authorized keys and return the username of the matching one, if any. |
| 1280 | * |
| 1281 | * @param[in] key Presented SSH key to compare. |
| 1282 | * @return Authorized key username, NULL if no match was found. |
| 1283 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1284 | static const char * |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 1285 | auth_pubkey_compare_key(ssh_key key) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1286 | { |
| 1287 | uint32_t i; |
| 1288 | ssh_key pub_key; |
Michal Vasko | 76e3a35 | 2016-01-18 09:07:00 +0100 | [diff] [blame] | 1289 | const char *username = NULL; |
Michal Vasko | 3e9d168 | 2017-02-24 09:50:15 +0100 | [diff] [blame] | 1290 | int ret = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1291 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 1292 | /* LOCK */ |
| 1293 | pthread_mutex_lock(&server_opts.authkey_lock); |
| 1294 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 1295 | for (i = 0; i < server_opts.authkey_count; ++i) { |
| 1296 | switch (server_opts.authkeys[i].type) { |
| 1297 | case NC_SSH_KEY_UNKNOWN: |
| 1298 | ret = ssh_pki_import_pubkey_file(server_opts.authkeys[i].path, &pub_key); |
| 1299 | break; |
| 1300 | case NC_SSH_KEY_DSA: |
| 1301 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_DSS, &pub_key); |
| 1302 | break; |
| 1303 | case NC_SSH_KEY_RSA: |
| 1304 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_RSA, &pub_key); |
| 1305 | break; |
| 1306 | case NC_SSH_KEY_ECDSA: |
| 1307 | ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_ECDSA, &pub_key); |
| 1308 | break; |
| 1309 | } |
| 1310 | |
Michal Vasko | 7abcdeb | 2016-05-30 15:27:00 +0200 | [diff] [blame] | 1311 | if (ret == SSH_EOF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1312 | 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] | 1313 | continue; |
| 1314 | } else if (ret == SSH_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1315 | 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] | 1316 | continue; |
| 1317 | } |
| 1318 | |
| 1319 | if (!ssh_key_cmp(key, pub_key, SSH_KEY_CMP_PUBLIC)) { |
| 1320 | ssh_key_free(pub_key); |
| 1321 | break; |
| 1322 | } |
| 1323 | |
| 1324 | ssh_key_free(pub_key); |
| 1325 | } |
| 1326 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 1327 | if (i < server_opts.authkey_count) { |
| 1328 | username = server_opts.authkeys[i].username; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1329 | } |
| 1330 | |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 1331 | /* UNLOCK */ |
| 1332 | pthread_mutex_unlock(&server_opts.authkey_lock); |
| 1333 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1334 | return username; |
| 1335 | } |
| 1336 | |
| 1337 | static void |
| 1338 | nc_sshcb_auth_pubkey(struct nc_session *session, ssh_message msg) |
| 1339 | { |
| 1340 | const char *username; |
| 1341 | int signature_state; |
| 1342 | |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 1343 | if (server_opts.pubkey_auth_clb) { |
| 1344 | 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] | 1345 | goto fail; |
| 1346 | } |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 1347 | } else { |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 1348 | if ((username = auth_pubkey_compare_key(ssh_message_auth_pubkey(msg))) == NULL) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1349 | 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] | 1350 | goto fail; |
| 1351 | } else if (strcmp(session->username, username)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1352 | 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] | 1353 | goto fail; |
| 1354 | } |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 1355 | } |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 1356 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1357 | signature_state = ssh_message_auth_publickey_state(msg); |
| 1358 | if (signature_state == SSH_PUBLICKEY_STATE_VALID) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1359 | VRB(session, "User \"%s\" authenticated.", session->username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1360 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 1361 | ssh_message_auth_reply_success(msg, 0); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1362 | } else if (signature_state == SSH_PUBLICKEY_STATE_NONE) { |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 1363 | /* accepting only the use of a public key */ |
| 1364 | ssh_message_auth_reply_pk_ok_simple(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1365 | } |
| 1366 | |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 1367 | return; |
| 1368 | |
| 1369 | fail: |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1370 | ++session->opts.server.ssh_auth_attempts; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1371 | VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username, |
| 1372 | session->opts.server.ssh_auth_attempts); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1373 | ssh_message_reply_default(msg); |
| 1374 | } |
| 1375 | |
| 1376 | static int |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1377 | nc_sshcb_channel_open(struct nc_session *session, ssh_message msg) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1378 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1379 | ssh_channel chan; |
| 1380 | |
| 1381 | /* first channel request */ |
| 1382 | if (!session->ti.libssh.channel) { |
| 1383 | if (session->status != NC_STATUS_STARTING) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1384 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1385 | return -1; |
| 1386 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1387 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 1388 | if (!chan) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1389 | ERR(session, "Failed to create a new SSH channel."); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1390 | return -1; |
| 1391 | } |
| 1392 | session->ti.libssh.channel = chan; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1393 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1394 | /* additional channel request */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1395 | } else { |
| 1396 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 1397 | if (!chan) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1398 | 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] | 1399 | return -1; |
| 1400 | } |
| 1401 | /* 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] | 1402 | } |
| 1403 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1404 | return 0; |
| 1405 | } |
| 1406 | |
| 1407 | static int |
| 1408 | nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem) |
| 1409 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1410 | struct nc_session *new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1411 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1412 | if (strcmp(subsystem, "netconf")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1413 | WRN(session, "Received an unknown subsystem \"%s\" request.", subsystem); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1414 | return -1; |
| 1415 | } |
| 1416 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1417 | if (session->ti.libssh.channel == channel) { |
| 1418 | /* first channel requested */ |
| 1419 | if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) { |
| 1420 | ERRINT; |
| 1421 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1422 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1423 | if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1424 | ERR(session, "Subsystem \"netconf\" requested for the second time."); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1425 | return -1; |
| 1426 | } |
| 1427 | |
| 1428 | session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1429 | } else { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1430 | /* 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] | 1431 | new_session = nc_new_session(NC_SERVER, 1); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1432 | if (!new_session) { |
| 1433 | ERRMEM; |
| 1434 | return -1; |
| 1435 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1436 | |
| 1437 | /* insert the new session */ |
| 1438 | if (!session->ti.libssh.next) { |
| 1439 | new_session->ti.libssh.next = session; |
| 1440 | } else { |
| 1441 | new_session->ti.libssh.next = session->ti.libssh.next; |
| 1442 | } |
| 1443 | session->ti.libssh.next = new_session; |
| 1444 | |
| 1445 | new_session->status = NC_STATUS_STARTING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1446 | new_session->ti_type = NC_TI_LIBSSH; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1447 | new_session->io_lock = session->io_lock; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1448 | new_session->ti.libssh.channel = channel; |
| 1449 | new_session->ti.libssh.session = session->ti.libssh.session; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1450 | new_session->username = strdup(session->username); |
| 1451 | new_session->host = strdup(session->host); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1452 | new_session->port = session->port; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1453 | new_session->ctx = (struct ly_ctx *)session->ctx; |
Michal Vasko | 83d1532 | 2018-09-27 09:44:02 +0200 | [diff] [blame] | 1454 | 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] | 1455 | } |
| 1456 | |
| 1457 | return 0; |
| 1458 | } |
| 1459 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1460 | int |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 1461 | nc_sshcb_msg(ssh_session UNUSED(sshsession), ssh_message msg, void *data) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1462 | { |
| 1463 | const char *str_type, *str_subtype = NULL, *username; |
| 1464 | int subtype, type; |
| 1465 | struct nc_session *session = (struct nc_session *)data; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1466 | |
| 1467 | type = ssh_message_type(msg); |
| 1468 | subtype = ssh_message_subtype(msg); |
| 1469 | |
| 1470 | switch (type) { |
| 1471 | case SSH_REQUEST_AUTH: |
| 1472 | str_type = "request-auth"; |
| 1473 | switch (subtype) { |
| 1474 | case SSH_AUTH_METHOD_NONE: |
| 1475 | str_subtype = "none"; |
| 1476 | break; |
| 1477 | case SSH_AUTH_METHOD_PASSWORD: |
| 1478 | str_subtype = "password"; |
| 1479 | break; |
| 1480 | case SSH_AUTH_METHOD_PUBLICKEY: |
| 1481 | str_subtype = "publickey"; |
| 1482 | break; |
| 1483 | case SSH_AUTH_METHOD_HOSTBASED: |
| 1484 | str_subtype = "hostbased"; |
| 1485 | break; |
| 1486 | case SSH_AUTH_METHOD_INTERACTIVE: |
| 1487 | str_subtype = "interactive"; |
| 1488 | break; |
| 1489 | case SSH_AUTH_METHOD_GSSAPI_MIC: |
| 1490 | str_subtype = "gssapi-mic"; |
| 1491 | break; |
| 1492 | } |
| 1493 | break; |
| 1494 | |
| 1495 | case SSH_REQUEST_CHANNEL_OPEN: |
| 1496 | str_type = "request-channel-open"; |
| 1497 | switch (subtype) { |
| 1498 | case SSH_CHANNEL_SESSION: |
| 1499 | str_subtype = "session"; |
| 1500 | break; |
| 1501 | case SSH_CHANNEL_DIRECT_TCPIP: |
| 1502 | str_subtype = "direct-tcpip"; |
| 1503 | break; |
| 1504 | case SSH_CHANNEL_FORWARDED_TCPIP: |
| 1505 | str_subtype = "forwarded-tcpip"; |
| 1506 | break; |
| 1507 | case (int)SSH_CHANNEL_X11: |
| 1508 | str_subtype = "channel-x11"; |
| 1509 | break; |
| 1510 | case SSH_CHANNEL_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1511 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1512 | default: |
| 1513 | str_subtype = "unknown"; |
| 1514 | break; |
| 1515 | } |
| 1516 | break; |
| 1517 | |
| 1518 | case SSH_REQUEST_CHANNEL: |
| 1519 | str_type = "request-channel"; |
| 1520 | switch (subtype) { |
| 1521 | case SSH_CHANNEL_REQUEST_PTY: |
| 1522 | str_subtype = "pty"; |
| 1523 | break; |
| 1524 | case SSH_CHANNEL_REQUEST_EXEC: |
| 1525 | str_subtype = "exec"; |
| 1526 | break; |
| 1527 | case SSH_CHANNEL_REQUEST_SHELL: |
| 1528 | str_subtype = "shell"; |
| 1529 | break; |
| 1530 | case SSH_CHANNEL_REQUEST_ENV: |
| 1531 | str_subtype = "env"; |
| 1532 | break; |
| 1533 | case SSH_CHANNEL_REQUEST_SUBSYSTEM: |
| 1534 | str_subtype = "subsystem"; |
| 1535 | break; |
| 1536 | case SSH_CHANNEL_REQUEST_WINDOW_CHANGE: |
| 1537 | str_subtype = "window-change"; |
| 1538 | break; |
| 1539 | case SSH_CHANNEL_REQUEST_X11: |
| 1540 | str_subtype = "x11"; |
| 1541 | break; |
| 1542 | case SSH_CHANNEL_REQUEST_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1543 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1544 | default: |
| 1545 | str_subtype = "unknown"; |
| 1546 | break; |
| 1547 | } |
| 1548 | break; |
| 1549 | |
| 1550 | case SSH_REQUEST_SERVICE: |
| 1551 | str_type = "request-service"; |
| 1552 | str_subtype = ssh_message_service_service(msg); |
| 1553 | break; |
| 1554 | |
| 1555 | case SSH_REQUEST_GLOBAL: |
| 1556 | str_type = "request-global"; |
| 1557 | switch (subtype) { |
| 1558 | case SSH_GLOBAL_REQUEST_TCPIP_FORWARD: |
| 1559 | str_subtype = "tcpip-forward"; |
| 1560 | break; |
| 1561 | case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD: |
| 1562 | str_subtype = "cancel-tcpip-forward"; |
| 1563 | break; |
| 1564 | case SSH_GLOBAL_REQUEST_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1565 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1566 | default: |
| 1567 | str_subtype = "unknown"; |
| 1568 | break; |
| 1569 | } |
| 1570 | break; |
| 1571 | |
| 1572 | default: |
| 1573 | str_type = "unknown"; |
| 1574 | str_subtype = "unknown"; |
| 1575 | break; |
| 1576 | } |
| 1577 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1578 | 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] | 1579 | if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) { |
Michal Vasko | ce31916 | 2016-02-03 15:33:08 +0100 | [diff] [blame] | 1580 | /* "valid" situation if, for example, receiving some auth or channel request timeouted, |
| 1581 | * but we got it now, during session free */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1582 | 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] | 1583 | (session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid")); |
Michal Vasko | ce31916 | 2016-02-03 15:33:08 +0100 | [diff] [blame] | 1584 | ssh_message_reply_default(msg); |
| 1585 | return 0; |
| 1586 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1587 | session->flags |= NC_SESSION_SSH_NEW_MSG; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1588 | |
| 1589 | /* |
| 1590 | * process known messages |
| 1591 | */ |
| 1592 | if (type == SSH_REQUEST_AUTH) { |
| 1593 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1594 | ERR(session, "User \"%s\" authenticated, but requested another authentication.", session->username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1595 | ssh_message_reply_default(msg); |
| 1596 | return 0; |
| 1597 | } |
| 1598 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1599 | /* save the username, do not let the client change it */ |
| 1600 | username = ssh_message_auth_user(msg); |
| 1601 | if (!session->username) { |
| 1602 | if (!username) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1603 | ERR(session, "Denying an auth request without a username."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1604 | return 1; |
| 1605 | } |
| 1606 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1607 | session->username = strdup(username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1608 | } else if (username) { |
| 1609 | if (strcmp(username, session->username)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1610 | ERR(session, "User \"%s\" changed its username to \"%s\".", session->username, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1611 | session->status = NC_STATUS_INVALID; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1612 | session->term_reason = NC_SESSION_TERM_OTHER; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1613 | return 1; |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | if (subtype == SSH_AUTH_METHOD_NONE) { |
| 1618 | /* libssh will return the supported auth methods */ |
| 1619 | return 1; |
| 1620 | } else if (subtype == SSH_AUTH_METHOD_PASSWORD) { |
| 1621 | nc_sshcb_auth_password(session, msg); |
| 1622 | return 0; |
| 1623 | } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) { |
| 1624 | nc_sshcb_auth_pubkey(session, msg); |
| 1625 | return 0; |
| 1626 | } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) { |
| 1627 | nc_sshcb_auth_kbdint(session, msg); |
| 1628 | return 0; |
| 1629 | } |
| 1630 | } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1631 | 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] | 1632 | if (nc_sshcb_channel_open(session, msg)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1633 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1634 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1635 | return 0; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1636 | |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1637 | } 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] | 1638 | if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg), |
| 1639 | ssh_message_channel_request_subsystem(msg))) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1640 | ssh_message_reply_default(msg); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1641 | } else { |
| 1642 | ssh_message_channel_request_reply_success(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1643 | } |
| 1644 | return 0; |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | /* we did not process it */ |
| 1649 | return 1; |
| 1650 | } |
| 1651 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1652 | /* ret 1 on success, 0 on timeout, -1 on error */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1653 | static int |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1654 | 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] | 1655 | { |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1656 | int ret; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1657 | struct timespec ts_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1658 | |
| 1659 | /* message callback is executed twice to give chance for the channel to be |
| 1660 | * created if timeout == 0 (it takes 2 messages, channel-open, subsystem-request) */ |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1661 | if (!timeout) { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1662 | if (!nc_session_is_connected(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1663 | ERR(session, "Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1664 | return -1; |
| 1665 | } |
| 1666 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1667 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1668 | if (ret != SSH_OK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1669 | ERR(session, "Failed to receive SSH messages on a session (%s).", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1670 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1671 | return -1; |
| 1672 | } |
| 1673 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1674 | if (!session->ti.libssh.channel) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1675 | return 0; |
| 1676 | } |
| 1677 | |
| 1678 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1679 | if (ret != SSH_OK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1680 | ERR(session, "Failed to receive SSH messages on a session (%s).", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1681 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1682 | return -1; |
| 1683 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1684 | |
| 1685 | if (!(session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
| 1686 | /* we did not receive subsystem-request, timeout */ |
| 1687 | return 0; |
| 1688 | } |
| 1689 | |
| 1690 | return 1; |
| 1691 | } |
| 1692 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1693 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1694 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1695 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1696 | while (1) { |
| 1697 | if (!nc_session_is_connected(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1698 | ERR(session, "Communication socket unexpectedly closed (libssh)."); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1699 | return -1; |
| 1700 | } |
| 1701 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1702 | ret = ssh_execute_message_callbacks(session->ti.libssh.session); |
| 1703 | if (ret != SSH_OK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1704 | ERR(session, "Failed to receive SSH messages on a session (%s).", |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1705 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1706 | return -1; |
| 1707 | } |
| 1708 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1709 | if (session->ti.libssh.channel && (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1710 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1711 | } |
| 1712 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1713 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1714 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1715 | /* timeout */ |
| 1716 | ERR(session, "Failed to start \"netconf\" SSH subsystem for too long, disconnecting."); |
| 1717 | break; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1718 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1719 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1720 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1721 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1722 | } |
| 1723 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 1724 | /** |
| 1725 | * @brief Set hostkeys to be used for an SSH bind. |
| 1726 | * |
| 1727 | * @param[in] sbind SSH bind to use. |
| 1728 | * @param[in] hostkeys Array of hostkeys. |
| 1729 | * @param[in] hostkey_count Count of @p hostkeys. |
| 1730 | * @return 0 on success. |
| 1731 | * @return -1 on error. |
| 1732 | */ |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1733 | static int |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1734 | 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] | 1735 | { |
| 1736 | uint8_t i; |
| 1737 | char *privkey_path, *privkey_data; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1738 | int ret; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1739 | NC_SSH_KEY_TYPE privkey_type; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1740 | |
| 1741 | if (!server_opts.hostkey_clb) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1742 | ERR(NULL, "Callback for retrieving SSH host keys not set."); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1743 | return -1; |
| 1744 | } |
| 1745 | |
| 1746 | for (i = 0; i < hostkey_count; ++i) { |
| 1747 | privkey_path = privkey_data = NULL; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1748 | 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] | 1749 | ERR(NULL, "Host key callback failed."); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1750 | return -1; |
| 1751 | } |
| 1752 | |
| 1753 | if (privkey_data) { |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1754 | 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] | 1755 | if (!privkey_path) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1756 | 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] | 1757 | free(privkey_data); |
| 1758 | return -1; |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path); |
| 1763 | |
| 1764 | /* cleanup */ |
| 1765 | if (privkey_data && unlink(privkey_path)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1766 | 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] | 1767 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1768 | free(privkey_data); |
| 1769 | |
| 1770 | if (ret != SSH_OK) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1771 | ERR(NULL, "Failed to set hostkey \"%s\" (%s).", hostkeys[i], privkey_path); |
Michal Vasko | 80075de | 2017-07-10 11:38:52 +0200 | [diff] [blame] | 1772 | } |
| 1773 | free(privkey_path); |
| 1774 | |
| 1775 | if (ret != SSH_OK) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1776 | return -1; |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | return 0; |
| 1781 | } |
| 1782 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1783 | static int |
| 1784 | 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] | 1785 | { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1786 | struct timespec ts_timeout; |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1787 | ssh_message msg; |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1788 | int libssh_auth_methods = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1789 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1790 | /* configure accepted auth methods */ |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1791 | if (opts->auth_methods & NC_SSH_AUTH_PUBLICKEY) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1792 | libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY; |
| 1793 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1794 | if (opts->auth_methods & NC_SSH_AUTH_PASSWORD) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1795 | libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD; |
| 1796 | } |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1797 | if (opts->auth_methods & NC_SSH_AUTH_INTERACTIVE) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1798 | libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE; |
| 1799 | } |
| 1800 | ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods); |
| 1801 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1802 | /* authenticate */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1803 | if (opts->auth_timeout) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1804 | nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1805 | } |
| 1806 | while (1) { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1807 | if (!nc_session_is_connected(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1808 | ERR(session, "Communication SSH socket unexpectedly closed."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1809 | return -1; |
| 1810 | } |
| 1811 | |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1812 | msg = ssh_message_get(session->ti.libssh.session); |
| 1813 | if (msg) { |
| 1814 | if (nc_sshcb_msg(session->ti.libssh.session, msg, (void *) session)) { |
| 1815 | ssh_message_reply_default(msg); |
| 1816 | } |
| 1817 | ssh_message_free(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1818 | } |
| 1819 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1820 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
| 1821 | break; |
| 1822 | } |
| 1823 | |
Michal Vasko | 145ae67 | 2017-02-07 10:57:27 +0100 | [diff] [blame] | 1824 | if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1825 | ERR(session, "Too many failed authentication attempts of user \"%s\".", session->username); |
Michal Vasko | 145ae67 | 2017-02-07 10:57:27 +0100 | [diff] [blame] | 1826 | return -1; |
| 1827 | } |
| 1828 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1829 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1830 | if ((opts->auth_timeout) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1831 | /* timeout */ |
| 1832 | break; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1833 | } |
| 1834 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1835 | |
| 1836 | if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) { |
| 1837 | /* timeout */ |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1838 | if (session->username) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1839 | 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] | 1840 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1841 | ERR(session, "User failed to authenticate for too long, disconnecting."); |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1842 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1843 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1844 | } |
| 1845 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1846 | return 1; |
| 1847 | } |
| 1848 | |
| 1849 | int |
| 1850 | nc_accept_ssh_session(struct nc_session *session, int sock, int timeout) |
| 1851 | { |
| 1852 | ssh_bind sbind = NULL; |
| 1853 | struct nc_server_ssh_opts *opts; |
| 1854 | int rc = 1, r; |
| 1855 | struct timespec ts_timeout; |
roman | 4cc0cd5 | 2023-04-14 09:12:29 +0200 | [diff] [blame] | 1856 | const char *err_msg; |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1857 | |
| 1858 | opts = session->data; |
| 1859 | |
| 1860 | /* other transport-specific data */ |
| 1861 | session->ti_type = NC_TI_LIBSSH; |
| 1862 | session->ti.libssh.session = ssh_new(); |
| 1863 | if (!session->ti.libssh.session) { |
| 1864 | ERR(NULL, "Failed to initialize a new SSH session."); |
| 1865 | rc = -1; |
| 1866 | goto cleanup; |
| 1867 | } |
| 1868 | |
| 1869 | sbind = ssh_bind_new(); |
| 1870 | if (!sbind) { |
| 1871 | ERR(session, "Failed to create an SSH bind."); |
| 1872 | rc = -1; |
| 1873 | goto cleanup; |
| 1874 | } |
| 1875 | |
| 1876 | /* configure host keys */ |
| 1877 | if (nc_ssh_bind_add_hostkeys(sbind, opts->hostkeys, opts->hostkey_count)) { |
| 1878 | rc = -1; |
| 1879 | goto cleanup; |
| 1880 | } |
| 1881 | |
| 1882 | /* accept new connection on the bind */ |
| 1883 | if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) { |
| 1884 | ERR(session, "SSH failed to accept a new connection (%s).", ssh_get_error(sbind)); |
| 1885 | rc = -1; |
| 1886 | goto cleanup; |
| 1887 | } |
| 1888 | sock = -1; |
| 1889 | |
| 1890 | ssh_set_blocking(session->ti.libssh.session, 0); |
| 1891 | |
| 1892 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1893 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1894 | } |
| 1895 | while ((r = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) { |
| 1896 | /* this tends to take longer */ |
| 1897 | usleep(NC_TIMEOUT_STEP * 20); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1898 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1899 | break; |
| 1900 | } |
| 1901 | } |
| 1902 | if (r == SSH_AGAIN) { |
| 1903 | ERR(session, "SSH key exchange timeout."); |
| 1904 | rc = 0; |
| 1905 | goto cleanup; |
| 1906 | } else if (r != SSH_OK) { |
roman | 4cc0cd5 | 2023-04-14 09:12:29 +0200 | [diff] [blame] | 1907 | err_msg = ssh_get_error(session->ti.libssh.session); |
| 1908 | if (err_msg[0] == '\0') { |
| 1909 | err_msg = "hostkey algorithm generated from the hostkey most likely not found in the set of configured hostkey algorithms"; |
| 1910 | } |
| 1911 | ERR(session, "SSH key exchange error (%s).", err_msg); |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1912 | rc = -1; |
| 1913 | goto cleanup; |
| 1914 | } |
| 1915 | |
| 1916 | /* authenticate */ |
| 1917 | if ((rc = nc_accept_ssh_session_auth(session, opts)) != 1) { |
| 1918 | goto cleanup; |
| 1919 | } |
| 1920 | |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1921 | /* set the message callback after a successful authentication */ |
| 1922 | ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, session); |
| 1923 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1924 | /* remember that this session was just set as nc_sshcb_msg() parameter */ |
| 1925 | session->flags |= NC_SESSION_SSH_MSG_CB; |
| 1926 | |
| 1927 | /* open channel and request 'netconf' subsystem */ |
| 1928 | if ((rc = nc_accept_ssh_session_open_netconf_channel(session, timeout)) != 1) { |
| 1929 | goto cleanup; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1930 | } |
| 1931 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1932 | /* all SSH messages were processed */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1933 | session->flags &= ~NC_SESSION_SSH_NEW_MSG; |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1934 | |
| 1935 | cleanup: |
| 1936 | if (sock > -1) { |
| 1937 | close(sock); |
| 1938 | } |
| 1939 | ssh_bind_free(sbind); |
| 1940 | return rc; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1941 | } |
| 1942 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1943 | API NC_MSG_TYPE |
| 1944 | nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session) |
| 1945 | { |
| 1946 | NC_MSG_TYPE msgtype; |
| 1947 | struct nc_session *new_session = NULL; |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1948 | struct timespec ts_cur; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1949 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1950 | NC_CHECK_ARG_RET(orig_session, orig_session, session, NC_MSG_ERROR); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1951 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1952 | if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) && |
| 1953 | orig_session->ti.libssh.next) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1954 | for (new_session = orig_session->ti.libssh.next; |
| 1955 | new_session != orig_session; |
| 1956 | new_session = new_session->ti.libssh.next) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1957 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel && |
| 1958 | (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1959 | /* we found our session */ |
| 1960 | break; |
| 1961 | } |
| 1962 | } |
| 1963 | if (new_session == orig_session) { |
| 1964 | new_session = NULL; |
| 1965 | } |
| 1966 | } |
| 1967 | |
| 1968 | if (!new_session) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1969 | ERR(orig_session, "Session does not have a NETCONF SSH channel ready."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1970 | return NC_MSG_ERROR; |
| 1971 | } |
| 1972 | |
| 1973 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 1974 | new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1975 | |
| 1976 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1977 | msgtype = nc_handshake_io(new_session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1978 | if (msgtype != NC_MSG_HELLO) { |
| 1979 | return msgtype; |
| 1980 | } |
| 1981 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1982 | nc_realtime_get(&ts_cur); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1983 | new_session->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1984 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1985 | new_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1986 | new_session->status = NC_STATUS_RUNNING; |
| 1987 | *session = new_session; |
| 1988 | |
| 1989 | return msgtype; |
| 1990 | } |
| 1991 | |
| 1992 | API NC_MSG_TYPE |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1993 | 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] | 1994 | { |
Michal Vasko | bdcf236 | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1995 | uint8_t q_id; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1996 | NC_MSG_TYPE msgtype; |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1997 | struct nc_session *new_session = NULL, *cur_session; |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1998 | struct timespec ts_cur; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1999 | uint16_t i; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2000 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 2001 | NC_CHECK_ARG_RET(NULL, ps, session, NC_MSG_ERROR); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2002 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 2003 | /* LOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 2004 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2005 | return NC_MSG_ERROR; |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 2006 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 2007 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 2008 | for (i = 0; i < ps->session_count; ++i) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 2009 | cur_session = ps->sessions[i]->session; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2010 | if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH) && |
| 2011 | cur_session->ti.libssh.next) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 2012 | /* an SSH session with more channels */ |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 2013 | for (new_session = cur_session->ti.libssh.next; |
| 2014 | new_session != cur_session; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 2015 | new_session = new_session->ti.libssh.next) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2016 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel && |
| 2017 | (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 2018 | /* we found our session */ |
| 2019 | break; |
| 2020 | } |
| 2021 | } |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 2022 | if (new_session != cur_session) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 2023 | break; |
| 2024 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 2025 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 2026 | new_session = NULL; |
| 2027 | } |
| 2028 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 2029 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 2030 | /* UNLOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 2031 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 2032 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 2033 | if (!new_session) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2034 | ERR(NULL, "No session with a NETCONF SSH channel ready was found."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2035 | return NC_MSG_ERROR; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2039 | new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 2040 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2041 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2042 | msgtype = nc_handshake_io(new_session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2043 | if (msgtype != NC_MSG_HELLO) { |
| 2044 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2045 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 2046 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 2047 | nc_realtime_get(&ts_cur); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2048 | new_session->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 2049 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2050 | new_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2051 | new_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 2052 | *session = new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2053 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2054 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2055 | } |