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 * |
roman | ce43511 | 2024-04-23 15:12:09 +0200 | [diff] [blame^] | 1012 | nc_server_ssh_get_pubkey_type(const unsigned char *buffer, uint32_t *len) |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 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; |
roman | ce43511 | 2024-04-23 15:12:09 +0200 | [diff] [blame^] | 1024 | return (const char *)buffer; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 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; |
roman | ce43511 | 2024-04-23 15:12:09 +0200 | [diff] [blame^] | 1038 | unsigned char *bin = NULL; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 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 | ret = 1; |
| 1049 | goto cleanup; |
| 1050 | } |
| 1051 | |
| 1052 | /* get the key type and try to import it if possible */ |
| 1053 | pub_type = nc_server_ssh_get_pubkey_type(bin, &pub_type_len); |
| 1054 | if (!pub_type) { |
| 1055 | ret = 1; |
| 1056 | goto cleanup; |
| 1057 | } else if (!strncmp(pub_type, "ssh-dss", pub_type_len)) { |
| 1058 | ERR(NULL, "DSA keys are not supported."); |
| 1059 | ret = 1; |
| 1060 | goto cleanup; |
| 1061 | } else if (!strncmp(pub_type, "ssh-rsa", pub_type_len)) { |
| 1062 | ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_RSA, key); |
| 1063 | } else if (!strncmp(pub_type, "ecdsa-sha2-nistp256", pub_type_len)) { |
| 1064 | ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P256, key); |
| 1065 | } else if (!strncmp(pub_type, "ecdsa-sha2-nistp384", pub_type_len)) { |
| 1066 | ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P384, key); |
| 1067 | } else if (!strncmp(pub_type, "ecdsa-sha2-nistp521", pub_type_len)) { |
| 1068 | ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P521, key); |
| 1069 | } else if (!strncmp(pub_type, "ssh-ed25519", pub_type_len)) { |
| 1070 | ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ED25519, key); |
| 1071 | } else { |
| 1072 | ERR(NULL, "Public key type not recognised."); |
| 1073 | ret = 1; |
| 1074 | goto cleanup; |
| 1075 | } |
| 1076 | |
| 1077 | cleanup: |
| 1078 | if (ret != SSH_OK) { |
| 1079 | ERR(NULL, "Error importing public key."); |
| 1080 | } |
| 1081 | free(bin); |
| 1082 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1083 | } |
| 1084 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 1085 | /** |
| 1086 | * @brief Compare SSH key with configured authorized keys and return the username of the matching one, if any. |
| 1087 | * |
| 1088 | * @param[in] key Presented SSH key to compare. |
| 1089 | * @return Authorized key username, NULL if no match was found. |
| 1090 | */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1091 | static int |
| 1092 | 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] | 1093 | { |
Michal Vasko | e2bfcd6 | 2024-03-28 07:58:49 +0100 | [diff] [blame] | 1094 | uint16_t i, pubkey_count = 0; |
Michal Vasko | 3e9d168 | 2017-02-24 09:50:15 +0100 | [diff] [blame] | 1095 | int ret = 0; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1096 | ssh_key new_key = NULL; |
roman | a9ec336 | 2023-12-21 10:59:57 +0100 | [diff] [blame] | 1097 | struct nc_public_key *pubkeys = NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1098 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1099 | /* get the correct public key storage */ |
| 1100 | if (auth_client->store == NC_STORE_LOCAL) { |
| 1101 | pubkeys = auth_client->pubkeys; |
| 1102 | pubkey_count = auth_client->pubkey_count; |
roman | a9ec336 | 2023-12-21 10:59:57 +0100 | [diff] [blame] | 1103 | } else if (auth_client->store == NC_STORE_TRUSTSTORE) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1104 | ret = nc_server_ssh_ts_ref_get_keys(auth_client->ts_ref, &pubkeys, &pubkey_count); |
| 1105 | if (ret) { |
| 1106 | 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] | 1107 | goto cleanup; |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 1108 | } |
roman | a9ec336 | 2023-12-21 10:59:57 +0100 | [diff] [blame] | 1109 | } else if (auth_client->store == NC_STORE_SYSTEM) { |
| 1110 | ret = nc_server_ssh_get_system_keys(auth_client->username, &pubkeys, &pubkey_count); |
| 1111 | if (ret) { |
| 1112 | ERR(NULL, "Failed to retrieve public keys of user \"%s\" from the system.", auth_client->username); |
| 1113 | goto cleanup; |
| 1114 | } |
| 1115 | } else { |
| 1116 | ERRINT; |
| 1117 | return 1; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1118 | } |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 1119 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1120 | /* try to compare all of the client's keys with the key received in the SSH message */ |
| 1121 | for (i = 0; i < pubkey_count; i++) { |
| 1122 | /* create the SSH key from the data */ |
| 1123 | if (nc_server_ssh_create_ssh_pubkey(pubkeys[i].data, &new_key)) { |
| 1124 | ssh_key_free(new_key); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1125 | continue; |
| 1126 | } |
| 1127 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1128 | /* compare the keys */ |
| 1129 | ret = ssh_key_cmp(key, new_key, SSH_KEY_CMP_PUBLIC); |
| 1130 | if (!ret) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1131 | break; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1132 | } else { |
| 1133 | WRN(NULL, "User's \"%s\" public key doesn't match, trying another.", auth_client->username); |
| 1134 | ssh_key_free(new_key); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1135 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1136 | } |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1137 | if (i == pubkey_count) { |
| 1138 | ret = 1; |
roman | a9ec336 | 2023-12-21 10:59:57 +0100 | [diff] [blame] | 1139 | goto cleanup; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1140 | } |
| 1141 | |
roman | a9ec336 | 2023-12-21 10:59:57 +0100 | [diff] [blame] | 1142 | cleanup: |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1143 | if (!ret) { |
| 1144 | /* only free a key if everything was ok, it would have already been freed otherwise */ |
| 1145 | ssh_key_free(new_key); |
| 1146 | } |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 1147 | |
roman | a9ec336 | 2023-12-21 10:59:57 +0100 | [diff] [blame] | 1148 | if ((auth_client->store == NC_STORE_SYSTEM) && pubkeys) { |
| 1149 | for (i = 0; i < pubkey_count; i++) { |
| 1150 | free(pubkeys[i].name); |
| 1151 | free(pubkeys[i].data); |
| 1152 | } |
| 1153 | free(pubkeys); |
| 1154 | } |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1155 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | static void |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1159 | 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] | 1160 | { |
roman | 808f3f6 | 2023-11-23 16:01:04 +0100 | [diff] [blame] | 1161 | 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] | 1162 | /* only authenticate the client if he supports none and no other method */ |
| 1163 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 1164 | VRB(session, "User \"%s\" authenticated.", session->username); |
| 1165 | ssh_message_auth_reply_success(msg, 0); |
| 1166 | } |
| 1167 | |
| 1168 | ssh_message_reply_default(msg); |
| 1169 | } |
| 1170 | |
| 1171 | static int |
| 1172 | nc_sshcb_auth_pubkey(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg) |
| 1173 | { |
| 1174 | int signature_state, ret = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1175 | |
roman | 9130fc1 | 2023-11-03 13:56:23 +0100 | [diff] [blame] | 1176 | if (auth_pubkey_compare_key(ssh_message_auth_pubkey(msg), auth_client)) { |
| 1177 | VRB(session, "User \"%s\" tried to use an unknown (unauthorized) public key.", session->username); |
| 1178 | ret = 1; |
| 1179 | goto fail; |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 1180 | } |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 1181 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1182 | signature_state = ssh_message_auth_publickey_state(msg); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1183 | if (signature_state == SSH_PUBLICKEY_STATE_NONE) { |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 1184 | /* accepting only the use of a public key */ |
| 1185 | ssh_message_auth_reply_pk_ok_simple(msg); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1186 | ret = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1187 | } |
| 1188 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1189 | return ret; |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 1190 | |
| 1191 | fail: |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1192 | ++session->opts.server.ssh_auth_attempts; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1193 | VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username, |
| 1194 | session->opts.server.ssh_auth_attempts); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1195 | ssh_message_reply_default(msg); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1196 | |
| 1197 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | static int |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1201 | nc_sshcb_channel_open(struct nc_session *session, ssh_message msg) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1202 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1203 | ssh_channel chan; |
| 1204 | |
| 1205 | /* first channel request */ |
| 1206 | if (!session->ti.libssh.channel) { |
| 1207 | if (session->status != NC_STATUS_STARTING) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1208 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1209 | return -1; |
| 1210 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1211 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 1212 | if (!chan) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1213 | ERR(session, "Failed to create a new SSH channel."); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1214 | return -1; |
| 1215 | } |
| 1216 | session->ti.libssh.channel = chan; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1217 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1218 | /* additional channel request */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1219 | } else { |
| 1220 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 1221 | if (!chan) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1222 | 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] | 1223 | return -1; |
| 1224 | } |
| 1225 | /* 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] | 1226 | } |
| 1227 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1228 | return 0; |
| 1229 | } |
| 1230 | |
| 1231 | static int |
| 1232 | nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem) |
| 1233 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1234 | struct nc_session *new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1235 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1236 | if (strcmp(subsystem, "netconf")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1237 | WRN(session, "Received an unknown subsystem \"%s\" request.", subsystem); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1238 | return -1; |
| 1239 | } |
| 1240 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1241 | if (session->ti.libssh.channel == channel) { |
| 1242 | /* first channel requested */ |
| 1243 | if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) { |
| 1244 | ERRINT; |
| 1245 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1246 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1247 | if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1248 | ERR(session, "Subsystem \"netconf\" requested for the second time."); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1249 | return -1; |
| 1250 | } |
| 1251 | |
| 1252 | session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1253 | } else { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1254 | /* 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] | 1255 | new_session = nc_new_session(NC_SERVER, 1); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1256 | NC_CHECK_ERRMEM_RET(!new_session, -1); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1257 | |
| 1258 | /* insert the new session */ |
| 1259 | if (!session->ti.libssh.next) { |
| 1260 | new_session->ti.libssh.next = session; |
| 1261 | } else { |
| 1262 | new_session->ti.libssh.next = session->ti.libssh.next; |
| 1263 | } |
| 1264 | session->ti.libssh.next = new_session; |
| 1265 | |
| 1266 | new_session->status = NC_STATUS_STARTING; |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 1267 | new_session->ti_type = NC_TI_SSH; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1268 | new_session->io_lock = session->io_lock; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1269 | new_session->ti.libssh.channel = channel; |
| 1270 | new_session->ti.libssh.session = session->ti.libssh.session; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1271 | new_session->username = strdup(session->username); |
| 1272 | new_session->host = strdup(session->host); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1273 | new_session->port = session->port; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1274 | new_session->ctx = (struct ly_ctx *)session->ctx; |
Michal Vasko | 83d1532 | 2018-09-27 09:44:02 +0200 | [diff] [blame] | 1275 | 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] | 1276 | } |
| 1277 | |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1281 | int |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1282 | 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] | 1283 | { |
| 1284 | const char *str_type, *str_subtype = NULL, *username; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1285 | int subtype, type, libssh_auth_methods = 0, ret = 0; |
| 1286 | uint16_t i; |
| 1287 | struct nc_auth_client *auth_client = NULL; |
roman | 78df0fa | 2023-11-02 10:33:57 +0100 | [diff] [blame] | 1288 | struct nc_endpt *referenced_endpt; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1289 | |
| 1290 | type = ssh_message_type(msg); |
| 1291 | subtype = ssh_message_subtype(msg); |
| 1292 | |
| 1293 | switch (type) { |
| 1294 | case SSH_REQUEST_AUTH: |
| 1295 | str_type = "request-auth"; |
| 1296 | switch (subtype) { |
| 1297 | case SSH_AUTH_METHOD_NONE: |
| 1298 | str_subtype = "none"; |
| 1299 | break; |
| 1300 | case SSH_AUTH_METHOD_PASSWORD: |
| 1301 | str_subtype = "password"; |
| 1302 | break; |
| 1303 | case SSH_AUTH_METHOD_PUBLICKEY: |
| 1304 | str_subtype = "publickey"; |
| 1305 | break; |
| 1306 | case SSH_AUTH_METHOD_HOSTBASED: |
| 1307 | str_subtype = "hostbased"; |
| 1308 | break; |
| 1309 | case SSH_AUTH_METHOD_INTERACTIVE: |
| 1310 | str_subtype = "interactive"; |
| 1311 | break; |
| 1312 | case SSH_AUTH_METHOD_GSSAPI_MIC: |
| 1313 | str_subtype = "gssapi-mic"; |
| 1314 | break; |
| 1315 | } |
| 1316 | break; |
| 1317 | |
| 1318 | case SSH_REQUEST_CHANNEL_OPEN: |
| 1319 | str_type = "request-channel-open"; |
| 1320 | switch (subtype) { |
| 1321 | case SSH_CHANNEL_SESSION: |
| 1322 | str_subtype = "session"; |
| 1323 | break; |
| 1324 | case SSH_CHANNEL_DIRECT_TCPIP: |
| 1325 | str_subtype = "direct-tcpip"; |
| 1326 | break; |
| 1327 | case SSH_CHANNEL_FORWARDED_TCPIP: |
| 1328 | str_subtype = "forwarded-tcpip"; |
| 1329 | break; |
| 1330 | case (int)SSH_CHANNEL_X11: |
| 1331 | str_subtype = "channel-x11"; |
| 1332 | break; |
| 1333 | case SSH_CHANNEL_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1334 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1335 | default: |
| 1336 | str_subtype = "unknown"; |
| 1337 | break; |
| 1338 | } |
| 1339 | break; |
| 1340 | |
| 1341 | case SSH_REQUEST_CHANNEL: |
| 1342 | str_type = "request-channel"; |
| 1343 | switch (subtype) { |
| 1344 | case SSH_CHANNEL_REQUEST_PTY: |
| 1345 | str_subtype = "pty"; |
| 1346 | break; |
| 1347 | case SSH_CHANNEL_REQUEST_EXEC: |
| 1348 | str_subtype = "exec"; |
| 1349 | break; |
| 1350 | case SSH_CHANNEL_REQUEST_SHELL: |
| 1351 | str_subtype = "shell"; |
| 1352 | break; |
| 1353 | case SSH_CHANNEL_REQUEST_ENV: |
| 1354 | str_subtype = "env"; |
| 1355 | break; |
| 1356 | case SSH_CHANNEL_REQUEST_SUBSYSTEM: |
| 1357 | str_subtype = "subsystem"; |
| 1358 | break; |
| 1359 | case SSH_CHANNEL_REQUEST_WINDOW_CHANGE: |
| 1360 | str_subtype = "window-change"; |
| 1361 | break; |
| 1362 | case SSH_CHANNEL_REQUEST_X11: |
| 1363 | str_subtype = "x11"; |
| 1364 | break; |
| 1365 | case SSH_CHANNEL_REQUEST_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1366 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1367 | default: |
| 1368 | str_subtype = "unknown"; |
| 1369 | break; |
| 1370 | } |
| 1371 | break; |
| 1372 | |
| 1373 | case SSH_REQUEST_SERVICE: |
| 1374 | str_type = "request-service"; |
| 1375 | str_subtype = ssh_message_service_service(msg); |
| 1376 | break; |
| 1377 | |
| 1378 | case SSH_REQUEST_GLOBAL: |
| 1379 | str_type = "request-global"; |
| 1380 | switch (subtype) { |
| 1381 | case SSH_GLOBAL_REQUEST_TCPIP_FORWARD: |
| 1382 | str_subtype = "tcpip-forward"; |
| 1383 | break; |
| 1384 | case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD: |
| 1385 | str_subtype = "cancel-tcpip-forward"; |
| 1386 | break; |
| 1387 | case SSH_GLOBAL_REQUEST_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1388 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1389 | default: |
| 1390 | str_subtype = "unknown"; |
| 1391 | break; |
| 1392 | } |
| 1393 | break; |
| 1394 | |
| 1395 | default: |
| 1396 | str_type = "unknown"; |
| 1397 | str_subtype = "unknown"; |
| 1398 | break; |
| 1399 | } |
| 1400 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1401 | 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] | 1402 | if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) { |
Michal Vasko | ce31916 | 2016-02-03 15:33:08 +0100 | [diff] [blame] | 1403 | /* "valid" situation if, for example, receiving some auth or channel request timeouted, |
| 1404 | * but we got it now, during session free */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1405 | 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] | 1406 | (session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid")); |
Michal Vasko | ce31916 | 2016-02-03 15:33:08 +0100 | [diff] [blame] | 1407 | ssh_message_reply_default(msg); |
| 1408 | return 0; |
| 1409 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1410 | |
| 1411 | /* |
| 1412 | * process known messages |
| 1413 | */ |
| 1414 | if (type == SSH_REQUEST_AUTH) { |
| 1415 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1416 | ERR(session, "User \"%s\" authenticated, but requested another authentication.", session->username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1417 | ssh_message_reply_default(msg); |
| 1418 | return 0; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1419 | } else if (!state || !opts) { |
| 1420 | /* these two parameters should always be set during an authentication, |
| 1421 | * however do a check just in case something goes really wrong, since they |
| 1422 | * are not needed for other types of messages |
| 1423 | */ |
| 1424 | ERRINT; |
| 1425 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1426 | } |
| 1427 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1428 | /* save the username, do not let the client change it */ |
| 1429 | username = ssh_message_auth_user(msg); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1430 | assert(username); |
| 1431 | |
| 1432 | for (i = 0; i < opts->client_count; i++) { |
| 1433 | if (!strcmp(opts->auth_clients[i].username, username)) { |
| 1434 | auth_client = &opts->auth_clients[i]; |
| 1435 | break; |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | if (!auth_client) { |
roman | 78df0fa | 2023-11-02 10:33:57 +0100 | [diff] [blame] | 1440 | if (opts->referenced_endpt_name) { |
| 1441 | /* client not known by the endpt, but it references another one so try it */ |
| 1442 | if (nc_server_get_referenced_endpt(opts->referenced_endpt_name, &referenced_endpt)) { |
| 1443 | ERRINT; |
| 1444 | return 1; |
| 1445 | } |
| 1446 | |
| 1447 | return nc_session_ssh_msg(session, referenced_endpt->opts.ssh, msg, state); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1448 | } |
| 1449 | |
roman | 545879e | 2024-01-19 14:43:46 +0100 | [diff] [blame] | 1450 | /* user not known, set his authentication methods to public key only so that |
| 1451 | * there is no interaction and it will simply be denied */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1452 | ERR(NULL, "User \"%s\" not known by the server.", username); |
roman | 545879e | 2024-01-19 14:43:46 +0100 | [diff] [blame] | 1453 | ssh_set_auth_methods(session->ti.libssh.session, SSH_AUTH_METHOD_PUBLICKEY); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1454 | ssh_message_reply_default(msg); |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
| 1458 | if (!session->username) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1459 | session->username = strdup(username); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1460 | |
| 1461 | /* configure and count accepted auth methods */ |
| 1462 | if (auth_client->store == NC_STORE_LOCAL) { |
| 1463 | if (auth_client->pubkey_count) { |
| 1464 | libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY; |
| 1465 | } |
roman | a9ec336 | 2023-12-21 10:59:57 +0100 | [diff] [blame] | 1466 | } else if (auth_client->store == NC_STORE_TRUSTSTORE) { |
| 1467 | if (auth_client->ts_ref) { |
| 1468 | libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY; |
| 1469 | } |
| 1470 | } else if (auth_client->store == NC_STORE_SYSTEM) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1471 | libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY; |
| 1472 | } |
| 1473 | if (auth_client->password) { |
| 1474 | state->auth_method_count++; |
| 1475 | libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD; |
| 1476 | } |
roman | 808f3f6 | 2023-11-23 16:01:04 +0100 | [diff] [blame] | 1477 | if (auth_client->kb_int_enabled) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1478 | state->auth_method_count++; |
| 1479 | libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE; |
| 1480 | } |
roman | 808f3f6 | 2023-11-23 16:01:04 +0100 | [diff] [blame] | 1481 | if (auth_client->none_enabled) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1482 | libssh_auth_methods |= SSH_AUTH_METHOD_NONE; |
| 1483 | } |
| 1484 | |
| 1485 | if (libssh_auth_methods & SSH_AUTH_METHOD_PUBLICKEY) { |
| 1486 | state->auth_method_count++; |
| 1487 | } |
| 1488 | |
| 1489 | ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods); |
| 1490 | } else { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1491 | if (strcmp(username, session->username)) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1492 | /* changing username not allowed */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1493 | ERR(session, "User \"%s\" changed its username to \"%s\".", session->username, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1494 | session->status = NC_STATUS_INVALID; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1495 | session->term_reason = NC_SESSION_TERM_OTHER; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1496 | return 1; |
| 1497 | } |
| 1498 | } |
| 1499 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1500 | /* 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] | 1501 | if (subtype == SSH_AUTH_METHOD_NONE) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1502 | nc_sshcb_auth_none(session, auth_client, msg); |
| 1503 | ret = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1504 | } else if (subtype == SSH_AUTH_METHOD_PASSWORD) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1505 | ret = nc_sshcb_auth_password(session, auth_client, msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1506 | } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1507 | ret = nc_sshcb_auth_pubkey(session, auth_client, msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1508 | } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) { |
roman | e5675b1 | 2024-03-05 14:26:23 +0100 | [diff] [blame] | 1509 | ret = nc_sshcb_auth_kbdint(session, auth_client, msg); |
roman | 4f9e442 | 2024-03-21 10:58:41 +0100 | [diff] [blame] | 1510 | } else { |
| 1511 | VRB(session, "Authentication method \"%s\" not supported.", str_subtype); |
| 1512 | ssh_message_reply_default(msg); |
| 1513 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1514 | } |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1515 | |
| 1516 | if (!ret) { |
| 1517 | state->auth_success_count++; |
| 1518 | } |
| 1519 | |
| 1520 | if (!ret && (state->auth_success_count < state->auth_method_count)) { |
| 1521 | /* success, but he needs to do another method */ |
| 1522 | VRB(session, "User \"%s\" partially authenticated, but still needs to authenticate via the rest of his configured methods.", username); |
| 1523 | ssh_message_auth_reply_success(msg, 1); |
| 1524 | } else if (!ret && (state->auth_success_count == state->auth_method_count)) { |
| 1525 | /* authenticated */ |
| 1526 | ssh_message_auth_reply_success(msg, 0); |
| 1527 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 1528 | VRB(session, "User \"%s\" authenticated.", username); |
| 1529 | } |
| 1530 | |
| 1531 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1532 | } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1533 | 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] | 1534 | if (nc_sshcb_channel_open(session, msg)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1535 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1536 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1537 | return 0; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1538 | |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1539 | } 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] | 1540 | if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg), |
| 1541 | ssh_message_channel_request_subsystem(msg))) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1542 | ssh_message_reply_default(msg); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1543 | } else { |
| 1544 | ssh_message_channel_request_reply_success(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1545 | } |
| 1546 | return 0; |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | /* we did not process it */ |
| 1551 | return 1; |
| 1552 | } |
| 1553 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1554 | /* ret 1 on success, 0 on timeout, -1 on error */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1555 | static int |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1556 | 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] | 1557 | { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1558 | struct timespec ts_timeout; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1559 | ssh_message msg; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1560 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1561 | if (timeout) { |
| 1562 | nc_timeouttime_get(&ts_timeout, timeout * 1000); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1563 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1564 | while (1) { |
| 1565 | if (!nc_session_is_connected(session)) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1566 | ERR(session, "Communication SSH socket unexpectedly closed."); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1567 | return -1; |
| 1568 | } |
| 1569 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1570 | msg = ssh_message_get(session->ti.libssh.session); |
| 1571 | if (msg) { |
| 1572 | if (nc_session_ssh_msg(session, opts, msg, NULL)) { |
| 1573 | ssh_message_reply_default(msg); |
| 1574 | } |
| 1575 | ssh_message_free(msg); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1576 | } |
| 1577 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1578 | if (session->ti.libssh.channel && session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1579 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1580 | } |
| 1581 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1582 | usleep(NC_TIMEOUT_STEP); |
roman | 0b7e698 | 2024-04-29 11:11:52 +0200 | [diff] [blame] | 1583 | if (timeout && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1584 | /* timeout */ |
| 1585 | ERR(session, "Failed to start \"netconf\" SSH subsystem for too long, disconnecting."); |
| 1586 | break; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1587 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1588 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1589 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1590 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1591 | } |
| 1592 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 1593 | /** |
| 1594 | * @brief Set hostkeys to be used for an SSH bind. |
| 1595 | * |
| 1596 | * @param[in] sbind SSH bind to use. |
| 1597 | * @param[in] hostkeys Array of hostkeys. |
| 1598 | * @param[in] hostkey_count Count of @p hostkeys. |
| 1599 | * @return 0 on success. |
| 1600 | * @return -1 on error. |
| 1601 | */ |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1602 | static int |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1603 | 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] | 1604 | { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1605 | uint16_t i; |
roman | 6dfdc0d | 2023-11-09 13:25:27 +0100 | [diff] [blame] | 1606 | char *privkey_path; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1607 | int ret; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1608 | struct nc_asymmetric_key *key = NULL; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1609 | |
| 1610 | for (i = 0; i < hostkey_count; ++i) { |
roman | 6dfdc0d | 2023-11-09 13:25:27 +0100 | [diff] [blame] | 1611 | privkey_path = NULL; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1612 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1613 | /* get the asymmetric key */ |
| 1614 | if (opts->hostkeys[i].store == NC_STORE_LOCAL) { |
| 1615 | /* stored locally */ |
| 1616 | key = &opts->hostkeys[i].key; |
| 1617 | } else { |
| 1618 | /* keystore reference, need to get it */ |
| 1619 | 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] | 1620 | return -1; |
| 1621 | } |
| 1622 | } |
| 1623 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1624 | privkey_path = base64der_privkey_to_tmp_file(key->privkey_data, nc_privkey_format_to_str(key->privkey_type)); |
| 1625 | if (!privkey_path) { |
| 1626 | ERR(NULL, "Temporarily storing a host key into a file failed (%s).", strerror(errno)); |
| 1627 | return -1; |
| 1628 | } |
| 1629 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1630 | ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path); |
| 1631 | |
| 1632 | /* cleanup */ |
roman | 6dfdc0d | 2023-11-09 13:25:27 +0100 | [diff] [blame] | 1633 | if (unlink(privkey_path)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1634 | 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] | 1635 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1636 | |
| 1637 | if (ret != SSH_OK) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1638 | 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] | 1639 | } |
| 1640 | free(privkey_path); |
| 1641 | |
| 1642 | if (ret != SSH_OK) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1643 | return -1; |
| 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | return 0; |
| 1648 | } |
| 1649 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1650 | static int |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1651 | 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] | 1652 | { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1653 | struct timespec ts_timeout; |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1654 | ssh_message msg; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1655 | struct nc_auth_state state = {0}; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1656 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1657 | /* authenticate */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1658 | if (opts->auth_timeout) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1659 | nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1660 | } |
| 1661 | while (1) { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1662 | if (!nc_session_is_connected(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1663 | ERR(session, "Communication SSH socket unexpectedly closed."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1664 | return -1; |
| 1665 | } |
| 1666 | |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1667 | msg = ssh_message_get(session->ti.libssh.session); |
| 1668 | if (msg) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1669 | if (nc_session_ssh_msg(session, opts, msg, &state)) { |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1670 | ssh_message_reply_default(msg); |
| 1671 | } |
| 1672 | ssh_message_free(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1673 | } |
| 1674 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1675 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
| 1676 | break; |
| 1677 | } |
| 1678 | |
Michal Vasko | 145ae67 | 2017-02-07 10:57:27 +0100 | [diff] [blame] | 1679 | if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1680 | ERR(session, "Too many failed authentication attempts of user \"%s\".", session->username); |
Michal Vasko | 145ae67 | 2017-02-07 10:57:27 +0100 | [diff] [blame] | 1681 | return -1; |
| 1682 | } |
| 1683 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1684 | usleep(NC_TIMEOUT_STEP); |
roman | ea0edaa | 2023-10-26 12:16:25 +0200 | [diff] [blame] | 1685 | if (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1686 | /* timeout */ |
| 1687 | break; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1688 | } |
| 1689 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1690 | |
| 1691 | if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) { |
| 1692 | /* timeout */ |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1693 | if (session->username) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1694 | 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] | 1695 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1696 | ERR(session, "User failed to authenticate for too long, disconnecting."); |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1697 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1698 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1699 | } |
| 1700 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1701 | return 1; |
| 1702 | } |
| 1703 | |
| 1704 | int |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1705 | 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] | 1706 | { |
| 1707 | ssh_bind sbind = NULL; |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1708 | int rc = 1, r; |
| 1709 | struct timespec ts_timeout; |
roman | 4cc0cd5 | 2023-04-14 09:12:29 +0200 | [diff] [blame] | 1710 | const char *err_msg; |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1711 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1712 | /* other transport-specific data */ |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 1713 | session->ti_type = NC_TI_SSH; |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1714 | session->ti.libssh.session = ssh_new(); |
| 1715 | if (!session->ti.libssh.session) { |
| 1716 | ERR(NULL, "Failed to initialize a new SSH session."); |
| 1717 | rc = -1; |
| 1718 | goto cleanup; |
| 1719 | } |
| 1720 | |
| 1721 | sbind = ssh_bind_new(); |
| 1722 | if (!sbind) { |
| 1723 | ERR(session, "Failed to create an SSH bind."); |
| 1724 | rc = -1; |
| 1725 | goto cleanup; |
| 1726 | } |
| 1727 | |
| 1728 | /* configure host keys */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1729 | if (nc_ssh_bind_add_hostkeys(sbind, opts, opts->hostkey_count)) { |
| 1730 | rc = -1; |
| 1731 | goto cleanup; |
| 1732 | } |
| 1733 | |
| 1734 | /* configure supported algorithms */ |
| 1735 | if (opts->hostkey_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, opts->hostkey_algs)) { |
| 1736 | rc = -1; |
| 1737 | goto cleanup; |
| 1738 | } |
| 1739 | if (opts->encryption_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_CIPHERS_S_C, opts->encryption_algs)) { |
| 1740 | rc = -1; |
| 1741 | goto cleanup; |
| 1742 | } |
| 1743 | if (opts->kex_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_KEY_EXCHANGE, opts->kex_algs)) { |
| 1744 | rc = -1; |
| 1745 | goto cleanup; |
| 1746 | } |
| 1747 | 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] | 1748 | rc = -1; |
| 1749 | goto cleanup; |
| 1750 | } |
| 1751 | |
| 1752 | /* accept new connection on the bind */ |
| 1753 | if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) { |
| 1754 | ERR(session, "SSH failed to accept a new connection (%s).", ssh_get_error(sbind)); |
| 1755 | rc = -1; |
| 1756 | goto cleanup; |
| 1757 | } |
| 1758 | sock = -1; |
| 1759 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1760 | /* set to non-blocking */ |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1761 | ssh_set_blocking(session->ti.libssh.session, 0); |
| 1762 | |
| 1763 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1764 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1765 | } |
| 1766 | while ((r = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) { |
| 1767 | /* this tends to take longer */ |
| 1768 | usleep(NC_TIMEOUT_STEP * 20); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1769 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1770 | break; |
| 1771 | } |
| 1772 | } |
| 1773 | if (r == SSH_AGAIN) { |
| 1774 | ERR(session, "SSH key exchange timeout."); |
| 1775 | rc = 0; |
| 1776 | goto cleanup; |
| 1777 | } else if (r != SSH_OK) { |
roman | 4cc0cd5 | 2023-04-14 09:12:29 +0200 | [diff] [blame] | 1778 | err_msg = ssh_get_error(session->ti.libssh.session); |
| 1779 | if (err_msg[0] == '\0') { |
| 1780 | err_msg = "hostkey algorithm generated from the hostkey most likely not found in the set of configured hostkey algorithms"; |
| 1781 | } |
| 1782 | ERR(session, "SSH key exchange error (%s).", err_msg); |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1783 | rc = -1; |
| 1784 | goto cleanup; |
| 1785 | } |
| 1786 | |
roman | e5675b1 | 2024-03-05 14:26:23 +0100 | [diff] [blame] | 1787 | /* authenticate, store auth_timeout in session so we can retrieve it in kb interactive API */ |
| 1788 | session->data = &opts->auth_timeout; |
| 1789 | rc = nc_accept_ssh_session_auth(session, opts); |
| 1790 | session->data = NULL; |
| 1791 | if (rc != 1) { |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1792 | goto cleanup; |
| 1793 | } |
| 1794 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1795 | /* open channel and request 'netconf' subsystem */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1796 | 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] | 1797 | goto cleanup; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1798 | } |
| 1799 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1800 | cleanup: |
| 1801 | if (sock > -1) { |
| 1802 | close(sock); |
| 1803 | } |
| 1804 | ssh_bind_free(sbind); |
| 1805 | return rc; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1806 | } |
| 1807 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1808 | API NC_MSG_TYPE |
| 1809 | nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session) |
| 1810 | { |
| 1811 | NC_MSG_TYPE msgtype; |
| 1812 | struct nc_session *new_session = NULL; |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1813 | struct timespec ts_cur; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1814 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1815 | NC_CHECK_ARG_RET(orig_session, orig_session, session, NC_MSG_ERROR); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1816 | |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 1817 | 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] | 1818 | orig_session->ti.libssh.next) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1819 | for (new_session = orig_session->ti.libssh.next; |
| 1820 | new_session != orig_session; |
| 1821 | new_session = new_session->ti.libssh.next) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1822 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel && |
| 1823 | (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1824 | /* we found our session */ |
| 1825 | break; |
| 1826 | } |
| 1827 | } |
| 1828 | if (new_session == orig_session) { |
| 1829 | new_session = NULL; |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | if (!new_session) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1834 | ERR(orig_session, "Session does not have a NETCONF SSH channel ready."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1835 | return NC_MSG_ERROR; |
| 1836 | } |
| 1837 | |
| 1838 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 1839 | new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1840 | |
| 1841 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1842 | msgtype = nc_handshake_io(new_session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1843 | if (msgtype != NC_MSG_HELLO) { |
| 1844 | return msgtype; |
| 1845 | } |
| 1846 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1847 | nc_realtime_get(&ts_cur); |
roman | 44efa32 | 2023-11-03 13:57:25 +0100 | [diff] [blame] | 1848 | new_session->opts.server.session_start = ts_cur; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1849 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1850 | new_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1851 | new_session->status = NC_STATUS_RUNNING; |
| 1852 | *session = new_session; |
| 1853 | |
| 1854 | return msgtype; |
| 1855 | } |
| 1856 | |
| 1857 | API NC_MSG_TYPE |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1858 | 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] | 1859 | { |
Michal Vasko | bdcf236 | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1860 | uint8_t q_id; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1861 | NC_MSG_TYPE msgtype; |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1862 | struct nc_session *new_session = NULL, *cur_session; |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1863 | struct timespec ts_cur; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1864 | uint16_t i; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1865 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1866 | NC_CHECK_ARG_RET(NULL, ps, session, NC_MSG_ERROR); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1867 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1868 | /* LOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1869 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1870 | return NC_MSG_ERROR; |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1871 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1872 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1873 | for (i = 0; i < ps->session_count; ++i) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1874 | cur_session = ps->sessions[i]->session; |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 1875 | 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] | 1876 | cur_session->ti.libssh.next) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1877 | /* an SSH session with more channels */ |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1878 | for (new_session = cur_session->ti.libssh.next; |
| 1879 | new_session != cur_session; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1880 | new_session = new_session->ti.libssh.next) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1881 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel && |
| 1882 | (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1883 | /* we found our session */ |
| 1884 | break; |
| 1885 | } |
| 1886 | } |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1887 | if (new_session != cur_session) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1888 | break; |
| 1889 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1890 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1891 | new_session = NULL; |
| 1892 | } |
| 1893 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1894 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1895 | /* UNLOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1896 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1897 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1898 | if (!new_session) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1899 | ERR(NULL, "No session with a NETCONF SSH channel ready was found."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1900 | return NC_MSG_ERROR; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 1904 | new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1905 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1906 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1907 | msgtype = nc_handshake_io(new_session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1908 | if (msgtype != NC_MSG_HELLO) { |
| 1909 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1910 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1911 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1912 | nc_realtime_get(&ts_cur); |
roman | 44efa32 | 2023-11-03 13:57:25 +0100 | [diff] [blame] | 1913 | new_session->opts.server.session_start = ts_cur; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1914 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1915 | new_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1916 | new_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1917 | *session = new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1918 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1919 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1920 | } |