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