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