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 | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 18 | #include "config.h" /* Expose HAVE_SHADOW, HAVE_CRYPT and HAVE_LIBPAM */ |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 19 | |
| 20 | #ifdef HAVE_SHADOW |
| 21 | #include <shadow.h> |
| 22 | #endif |
| 23 | #ifdef HAVE_CRYPT |
| 24 | #include <crypt.h> |
| 25 | #endif |
Michal Vasko | 0d81c57 | 2022-09-26 10:39:31 +0200 | [diff] [blame] | 26 | #ifdef HAVE_LIBPAM |
| 27 | #include <security/pam_appl.h> |
| 28 | #endif |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 29 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 30 | #include <arpa/inet.h> |
| 31 | #include <assert.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 32 | #include <errno.h> |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 33 | #include <libssh/libssh.h> |
| 34 | #include <libssh/server.h> |
| 35 | #include <libyang/libyang.h> |
| 36 | #include <openssl/bio.h> |
| 37 | #include <openssl/err.h> |
| 38 | #include <openssl/evp.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 39 | #include <pwd.h> |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 40 | #include <stdint.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
Michal Vasko | 2725269 | 2017-03-21 15:34:13 +0100 | [diff] [blame] | 43 | #include <sys/stat.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 44 | #include <sys/types.h> |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 45 | #include <time.h> |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 46 | #include <unistd.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 47 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 48 | #include "compat.h" |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 49 | #include "log_p.h" |
| 50 | #include "session.h" |
| 51 | #include "session_p.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 52 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 53 | #if !defined (HAVE_CRYPT_R) |
Mislav Novakovic | ce9a7ef | 2017-08-08 13:45:52 +0200 | [diff] [blame] | 54 | pthread_mutex_t crypt_lock = PTHREAD_MUTEX_INITIALIZER; |
| 55 | #endif |
| 56 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 57 | extern struct nc_server_opts server_opts; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 58 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 59 | static char * |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 60 | base64der_privkey_to_tmp_file(const char *in, const char *privkey_format) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 61 | { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 62 | char path[12] = "/tmp/XXXXXX"; |
| 63 | int fd, written; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 64 | unsigned len; |
Michal Vasko | 2725269 | 2017-03-21 15:34:13 +0100 | [diff] [blame] | 65 | mode_t umode; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 66 | FILE *file; |
| 67 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 68 | NC_CHECK_ARG_RET(NULL, in, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 69 | |
mekleo | b31878b | 2019-09-09 14:10:47 +0200 | [diff] [blame] | 70 | umode = umask(0177); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 71 | fd = mkstemp(path); |
Michal Vasko | 2725269 | 2017-03-21 15:34:13 +0100 | [diff] [blame] | 72 | umask(umode); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 73 | if (fd == -1) { |
| 74 | return NULL; |
| 75 | } |
| 76 | |
Michal Vasko | 3964a83 | 2018-09-18 14:37:39 +0200 | [diff] [blame] | 77 | file = fdopen(fd, "w"); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 78 | if (!file) { |
| 79 | close(fd); |
| 80 | return NULL; |
| 81 | } |
| 82 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 83 | /* write header */ |
| 84 | written = fwrite("-----BEGIN ", 1, 11, file); |
| 85 | if (privkey_format) { |
| 86 | written += fwrite(privkey_format, 1, strlen(privkey_format), file); |
Michal Vasko | 68177b7 | 2020-04-27 15:46:53 +0200 | [diff] [blame] | 87 | written += fwrite(" PRIVATE KEY-----\n", 1, 18, file); |
Michal Vasko | 68177b7 | 2020-04-27 15:46:53 +0200 | [diff] [blame] | 88 | } else { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 89 | written += fwrite("PRIVATE KEY-----\n", 1, 17, file); |
| 90 | } |
Michal Vasko | 68177b7 | 2020-04-27 15:46:53 +0200 | [diff] [blame] | 91 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 92 | /* write data */ |
| 93 | written += fwrite(in, 1, strlen(in), file); |
| 94 | |
| 95 | /* write footer */ |
| 96 | written += fwrite("\n-----END ", 1, 10, file); |
| 97 | if (privkey_format) { |
| 98 | written += fwrite(privkey_format, 1, strlen(privkey_format), file); |
| 99 | written += fwrite(" PRIVATE KEY-----", 1, 17, file); |
| 100 | } else { |
| 101 | written += fwrite("PRIVATE KEY-----", 1, 16, file); |
| 102 | } |
| 103 | |
| 104 | fclose(file); |
| 105 | |
| 106 | /* checksum */ |
| 107 | if (privkey_format) { |
| 108 | len = 11 + strlen(privkey_format) + 18 + strlen(in) + 10 + strlen(privkey_format) + 17; |
| 109 | } else { |
| 110 | len = 11 + 17 + strlen(in) + 10 + 16; |
| 111 | } |
| 112 | |
| 113 | if ((unsigned)written != len) { |
| 114 | unlink(path); |
| 115 | return NULL; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return strdup(path); |
| 119 | } |
| 120 | |
| 121 | static int |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 122 | 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] | 123 | { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 124 | uint16_t i; |
| 125 | struct nc_keystore *ks = &server_opts.keystore; |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 126 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 127 | *askey = NULL; |
Michal Vasko | d45e25a | 2016-01-08 15:48:44 +0100 | [diff] [blame] | 128 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 129 | /* lookup name */ |
| 130 | for (i = 0; i < ks->asym_key_count; i++) { |
| 131 | if (!strcmp(referenced_name, ks->asym_keys[i].name)) { |
| 132 | break; |
Michal Vasko | fbfe8b6 | 2017-02-14 10:22:30 +0100 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 136 | if (i == ks->asym_key_count) { |
| 137 | ERR(NULL, "Keystore entry \"%s\" not found.", referenced_name); |
| 138 | return 1; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 139 | } |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 140 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 141 | *askey = &ks->asym_keys[i]; |
| 142 | |
| 143 | /* check if the referenced public key is SubjectPublicKeyInfo */ |
| 144 | if ((*askey)->pubkey_data && nc_is_pk_subject_public_key_info((*askey)->pubkey_data)) { |
| 145 | ERR(NULL, "The public key of the referenced hostkey \"%s\" is in the SubjectPublicKeyInfo format, " |
| 146 | "which is not allowed in the SSH!", referenced_name); |
| 147 | return 1; |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 148 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 149 | |
Michal Vasko | 5fcc714 | 2016-02-02 12:21:10 +0100 | [diff] [blame] | 150 | return 0; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 151 | } |
| 152 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 153 | static int |
| 154 | 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] | 155 | { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 156 | uint16_t i, j; |
| 157 | struct nc_truststore *ts = &server_opts.truststore; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 158 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 159 | *pubkeys = NULL; |
| 160 | *pubkey_count = 0; |
| 161 | |
| 162 | /* lookup name */ |
| 163 | for (i = 0; i < ts->pub_bag_count; i++) { |
| 164 | if (!strcmp(referenced_name, ts->pub_bags[i].name)) { |
| 165 | break; |
| 166 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 167 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 168 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 169 | if (i == ts->pub_bag_count) { |
| 170 | ERR(NULL, "Truststore entry \"%s\" not found.", referenced_name); |
| 171 | return 1; |
| 172 | } |
Michal Vasko | c46b3df | 2021-07-26 09:30:05 +0200 | [diff] [blame] | 173 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 174 | /* check if any of the referenced public keys is SubjectPublicKeyInfo */ |
| 175 | for (j = 0; j < ts->pub_bags[i].pubkey_count; j++) { |
| 176 | if (nc_is_pk_subject_public_key_info(ts->pub_bags[i].pubkeys[j].data)) { |
| 177 | ERR(NULL, "A public key of the referenced public key bag \"%s\" is in the SubjectPublicKeyInfo format, " |
| 178 | "which is not allowed in the SSH!", referenced_name); |
| 179 | return 1; |
| 180 | } |
| 181 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 182 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 183 | *pubkeys = ts->pub_bags[i].pubkeys; |
| 184 | *pubkey_count = ts->pub_bags[i].pubkey_count; |
| 185 | return 0; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Michal Vasko | 974410a | 2018-04-03 09:36:57 +0200 | [diff] [blame] | 188 | API void |
| 189 | nc_server_ssh_set_passwd_auth_clb(int (*passwd_auth_clb)(const struct nc_session *session, const char *password, void *user_data), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 190 | void *user_data, void (*free_user_data)(void *user_data)) |
Michal Vasko | 974410a | 2018-04-03 09:36:57 +0200 | [diff] [blame] | 191 | { |
| 192 | server_opts.passwd_auth_clb = passwd_auth_clb; |
| 193 | server_opts.passwd_auth_data = user_data; |
| 194 | server_opts.passwd_auth_data_free = free_user_data; |
| 195 | } |
| 196 | |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 197 | API void |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 198 | nc_server_ssh_set_interactive_auth_clb(int (*interactive_auth_clb)(const struct nc_session *session, ssh_session ssh_sess, |
| 199 | ssh_message msg, void *user_data), void *user_data, void (*free_user_data)(void *user_data)) |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 200 | { |
| 201 | server_opts.interactive_auth_clb = interactive_auth_clb; |
| 202 | server_opts.interactive_auth_data = user_data; |
| 203 | server_opts.interactive_auth_data_free = free_user_data; |
| 204 | } |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 205 | |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 206 | API void |
| 207 | nc_server_ssh_set_pubkey_auth_clb(int (*pubkey_auth_clb)(const struct nc_session *session, ssh_key key, void *user_data), |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 208 | void *user_data, void (*free_user_data)(void *user_data)) |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 209 | { |
| 210 | server_opts.pubkey_auth_clb = pubkey_auth_clb; |
| 211 | server_opts.pubkey_auth_data = user_data; |
| 212 | server_opts.pubkey_auth_data_free = free_user_data; |
| 213 | } |
| 214 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 215 | /** |
| 216 | * @brief Compare hashed password with a cleartext password for a match. |
| 217 | * |
| 218 | * @param[in] pass_hash Hashed password. |
| 219 | * @param[in] pass_clear Cleartext password. |
| 220 | * @return 0 on match. |
| 221 | * @return non-zero if not a match. |
| 222 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 223 | static int |
roman | 814f511 | 2023-10-19 15:51:16 +0200 | [diff] [blame] | 224 | auth_password_compare_pwd(const char *stored_pw, const char *received_pw) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 225 | { |
roman | 814f511 | 2023-10-19 15:51:16 +0200 | [diff] [blame] | 226 | char *received_pw_hash = NULL; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 227 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 228 | #ifdef HAVE_CRYPT_R |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 229 | struct crypt_data cdata; |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 230 | #endif |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 231 | |
roman | 814f511 | 2023-10-19 15:51:16 +0200 | [diff] [blame] | 232 | if (!stored_pw[0]) { |
| 233 | if (!received_pw[0]) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 234 | WRN(NULL, "User authentication successful with an empty password!"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 235 | return 0; |
| 236 | } else { |
| 237 | /* the user did now know he does not need any password, |
| 238 | * (which should not be used) so deny authentication */ |
| 239 | return 1; |
| 240 | } |
| 241 | } |
| 242 | |
roman | 814f511 | 2023-10-19 15:51:16 +0200 | [diff] [blame] | 243 | if (!strncmp(stored_pw, "$0$", 3)) { |
| 244 | /* cleartext password, simply compare the values */ |
| 245 | return strcmp(stored_pw + 3, received_pw); |
| 246 | } |
| 247 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 248 | #ifdef HAVE_CRYPT_R |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 249 | cdata.initialized = 0; |
roman | 814f511 | 2023-10-19 15:51:16 +0200 | [diff] [blame] | 250 | received_pw_hash = crypt_r(received_pw, stored_pw, &cdata); |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 251 | #else |
Mislav Novakovic | ce9a7ef | 2017-08-08 13:45:52 +0200 | [diff] [blame] | 252 | pthread_mutex_lock(&crypt_lock); |
roman | 814f511 | 2023-10-19 15:51:16 +0200 | [diff] [blame] | 253 | received_pw_hash = crypt(received_pw, stored_pw); |
Mislav Novakovic | ce9a7ef | 2017-08-08 13:45:52 +0200 | [diff] [blame] | 254 | pthread_mutex_unlock(&crypt_lock); |
Mislav Novakovic | ebf4bd7 | 2017-08-02 10:43:41 +0200 | [diff] [blame] | 255 | #endif |
roman | 814f511 | 2023-10-19 15:51:16 +0200 | [diff] [blame] | 256 | if (!received_pw_hash) { |
Andrew Langefeld | 158d6fd | 2018-06-11 18:51:44 -0500 | [diff] [blame] | 257 | return 1; |
| 258 | } |
| 259 | |
roman | 814f511 | 2023-10-19 15:51:16 +0200 | [diff] [blame] | 260 | return strcmp(received_pw_hash, stored_pw); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 261 | } |
| 262 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 263 | static int |
| 264 | 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] | 265 | { |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 266 | int auth_ret = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 267 | |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 268 | if (server_opts.passwd_auth_clb) { |
| 269 | auth_ret = server_opts.passwd_auth_clb(session, ssh_message_auth_password(msg), server_opts.passwd_auth_data); |
| 270 | } else { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 271 | auth_ret = auth_password_compare_pwd(auth_client->password, ssh_message_auth_password(msg)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 272 | } |
| 273 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 274 | if (auth_ret) { |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 275 | ++session->opts.server.ssh_auth_attempts; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 276 | VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username, |
| 277 | session->opts.server.ssh_auth_attempts); |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 278 | ssh_message_reply_default(msg); |
| 279 | } |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 280 | |
| 281 | return auth_ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Michal Vasko | 0d81c57 | 2022-09-26 10:39:31 +0200 | [diff] [blame] | 284 | #ifdef HAVE_LIBPAM |
| 285 | |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 286 | /** |
| 287 | * @brief PAM conversation function, which serves as a callback for exchanging messages between the client and a PAM module. |
| 288 | * |
| 289 | * @param[in] n_messages Number of messages. |
| 290 | * @param[in] msg PAM module's messages. |
| 291 | * @param[out] resp User responses. |
| 292 | * @param[in] appdata_ptr Callback's data. |
| 293 | * @return PAM_SUCCESS on success; |
| 294 | * @return PAM_BUF_ERR on memory allocation error; |
| 295 | * @return PAM_CONV_ERR otherwise. |
| 296 | */ |
| 297 | static int |
| 298 | nc_pam_conv_clb(int n_messages, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr) |
| 299 | { |
| 300 | int i, j, t, r = PAM_SUCCESS, n_answers, n_requests = n_messages; |
| 301 | const char **prompts = NULL; |
| 302 | char *echo = NULL; |
| 303 | const char *name = "Keyboard-Interactive Authentication"; |
| 304 | const char *instruction = "Please enter your authentication token"; |
| 305 | ssh_message reply = NULL; |
| 306 | struct nc_pam_thread_arg *clb_data = appdata_ptr; |
| 307 | ssh_session libssh_session; |
| 308 | struct timespec ts_timeout; |
| 309 | struct nc_server_ssh_opts *opts; |
| 310 | |
| 311 | libssh_session = clb_data->session->ti.libssh.session; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 312 | opts = clb_data->opts; |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 313 | |
| 314 | /* PAM_MAX_NUM_MSG == 32 by default */ |
| 315 | if ((n_messages <= 0) || (n_messages >= PAM_MAX_NUM_MSG)) { |
| 316 | ERR(NULL, "Bad number of PAM messages (#%d).", n_messages); |
| 317 | r = PAM_CONV_ERR; |
| 318 | goto cleanup; |
| 319 | } |
| 320 | |
| 321 | /* only accepting these 4 types of messages */ |
| 322 | for (i = 0; i < n_messages; i++) { |
| 323 | t = msg[i]->msg_style; |
| 324 | if ((t != PAM_PROMPT_ECHO_OFF) && (t != PAM_PROMPT_ECHO_ON) && (t != PAM_TEXT_INFO) && (t != PAM_ERROR_MSG)) { |
| 325 | ERR(NULL, "PAM conversation callback received an unexpected type of message."); |
| 326 | r = PAM_CONV_ERR; |
| 327 | goto cleanup; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /* display messages with errors and/or some information and count the amount of actual authentication challenges */ |
| 332 | for (i = 0; i < n_messages; i++) { |
| 333 | if (msg[i]->msg_style == PAM_TEXT_INFO) { |
| 334 | VRB(NULL, "PAM conversation callback received a message with some information for the client (%s).", msg[i]->msg); |
| 335 | n_requests--; |
| 336 | } |
| 337 | if (msg[i]->msg_style == PAM_ERROR_MSG) { |
| 338 | ERR(NULL, "PAM conversation callback received an error message (%s).", msg[i]->msg); |
| 339 | r = PAM_CONV_ERR; |
| 340 | goto cleanup; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /* there are no requests left for the user, only messages with some information for the client were sent */ |
| 345 | if (n_requests <= 0) { |
| 346 | r = PAM_SUCCESS; |
| 347 | goto cleanup; |
| 348 | } |
| 349 | |
| 350 | /* it is the PAM module's responsibility to release both, this array and the responses themselves */ |
| 351 | *resp = calloc(n_requests, sizeof **resp); |
| 352 | prompts = calloc(n_requests, sizeof *prompts); |
| 353 | echo = calloc(n_requests, sizeof *echo); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 354 | NC_CHECK_ERRMEM_GOTO(!(*resp) || !prompts || !echo, r = PAM_BUF_ERR, cleanup); |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 355 | |
| 356 | /* set the prompts for the user */ |
| 357 | j = 0; |
| 358 | for (i = 0; i < n_messages; i++) { |
| 359 | if ((msg[i]->msg_style == PAM_PROMPT_ECHO_ON) || (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF)) { |
| 360 | prompts[j++] = msg[i]->msg; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /* iterate over all the messages and adjust the echo array accordingly */ |
| 365 | j = 0; |
| 366 | for (i = 0; i < n_messages; i++) { |
| 367 | if (msg[i]->msg_style == PAM_PROMPT_ECHO_ON) { |
| 368 | echo[j++] = 1; |
| 369 | } |
| 370 | if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF) { |
| 371 | /* no need to set to 0 because of calloc */ |
| 372 | j++; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /* print all the keyboard-interactive challenges to the user */ |
| 377 | r = ssh_message_auth_interactive_request(clb_data->msg, name, instruction, n_requests, prompts, echo); |
| 378 | if (r != SSH_OK) { |
| 379 | ERR(NULL, "Failed to send an authentication request."); |
| 380 | r = PAM_CONV_ERR; |
| 381 | goto cleanup; |
| 382 | } |
| 383 | |
| 384 | if (opts->auth_timeout) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 385 | nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000); |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /* get user's replies */ |
| 389 | do { |
| 390 | if (!nc_session_is_connected(clb_data->session)) { |
| 391 | ERR(NULL, "Communication SSH socket unexpectedly closed."); |
| 392 | r = PAM_CONV_ERR; |
| 393 | goto cleanup; |
| 394 | } |
| 395 | |
| 396 | reply = ssh_message_get(libssh_session); |
| 397 | if (reply) { |
| 398 | break; |
| 399 | } |
| 400 | |
| 401 | usleep(NC_TIMEOUT_STEP); |
roman | ea0edaa | 2023-10-26 12:16:25 +0200 | [diff] [blame^] | 402 | } while (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) >= 1)); |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 403 | |
| 404 | if (!reply) { |
| 405 | ERR(NULL, "Authentication timeout."); |
| 406 | r = PAM_CONV_ERR; |
| 407 | goto cleanup; |
| 408 | } |
| 409 | |
| 410 | /* check if the amount of replies matches the amount of requests */ |
| 411 | n_answers = ssh_userauth_kbdint_getnanswers(libssh_session); |
| 412 | if (n_answers != n_requests) { |
| 413 | ERR(NULL, "Expected %d response(s), got %d.", n_requests, n_answers); |
| 414 | r = PAM_CONV_ERR; |
| 415 | goto cleanup; |
| 416 | } |
| 417 | |
| 418 | /* give the replies to a PAM module */ |
| 419 | for (i = 0; i < n_answers; i++) { |
| 420 | (*resp)[i].resp = strdup(ssh_userauth_kbdint_getanswer(libssh_session, i)); |
| 421 | /* it should be the caller's responsibility to free this, however if mem alloc fails, |
| 422 | * it is safer to free the responses here and set them to NULL */ |
| 423 | if ((*resp)[i].resp == NULL) { |
| 424 | for (j = 0; j < i; j++) { |
| 425 | free((*resp)[j].resp); |
| 426 | (*resp)[j].resp = NULL; |
| 427 | } |
| 428 | ERRMEM; |
| 429 | r = PAM_BUF_ERR; |
| 430 | goto cleanup; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | cleanup: |
| 435 | ssh_message_free(reply); |
| 436 | free(prompts); |
| 437 | free(echo); |
| 438 | return r; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * @brief Handles authentication via Linux PAM. |
| 443 | * |
| 444 | * @param[in] session NETCONF session. |
| 445 | * @param[in] ssh_msg SSH message with a keyboard-interactive authentication request. |
| 446 | * @return PAM_SUCCESS on success; |
| 447 | * @return PAM error otherwise. |
| 448 | */ |
| 449 | static int |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 450 | nc_pam_auth(struct nc_session *session, struct nc_server_ssh_opts *opts, ssh_message ssh_msg) |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 451 | { |
| 452 | pam_handle_t *pam_h = NULL; |
| 453 | int ret; |
| 454 | struct nc_pam_thread_arg clb_data; |
| 455 | struct pam_conv conv; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 456 | uint16_t i; |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 457 | |
| 458 | /* structure holding callback's data */ |
| 459 | clb_data.msg = ssh_msg; |
| 460 | clb_data.session = session; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 461 | clb_data.opts = opts; |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 462 | |
| 463 | /* PAM conversation structure holding the callback and it's data */ |
| 464 | conv.conv = nc_pam_conv_clb; |
| 465 | conv.appdata_ptr = &clb_data; |
| 466 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 467 | /* get the current client's configuration file */ |
| 468 | for (i = 0; i < opts->client_count; i++) { |
| 469 | if (!strcmp(opts->auth_clients[i].username, session->username)) { |
| 470 | break; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | if (i == opts->client_count) { |
| 475 | ERR(NULL, "User \"%s\" not found.", session->username); |
| 476 | ret = 1; |
| 477 | goto cleanup; |
| 478 | } |
| 479 | |
| 480 | if (!opts->auth_clients[i].pam_config_name) { |
| 481 | ERR(NULL, "User's \"%s\" PAM configuration filename not set."); |
| 482 | ret = 1; |
| 483 | goto cleanup; |
| 484 | } |
| 485 | |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 486 | /* initialize PAM and see if the given configuration file exists */ |
Michal Vasko | 0d81c57 | 2022-09-26 10:39:31 +0200 | [diff] [blame] | 487 | # ifdef LIBPAM_HAVE_CONFDIR |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 488 | /* PAM version >= 1.4 */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 489 | ret = pam_start_confdir(opts->auth_clients[i].pam_config_name, session->username, &conv, opts->auth_clients[i].pam_config_dir, &pam_h); |
Michal Vasko | 0d81c57 | 2022-09-26 10:39:31 +0200 | [diff] [blame] | 490 | # else |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 491 | /* PAM version < 1.4 */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 492 | ret = pam_start(opts->auth_clients[i].pam_config_name, session->username, &conv, &pam_h); |
Michal Vasko | 0d81c57 | 2022-09-26 10:39:31 +0200 | [diff] [blame] | 493 | # endif |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 494 | if (ret != PAM_SUCCESS) { |
| 495 | ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 496 | goto cleanup; |
| 497 | } |
| 498 | |
| 499 | /* authentication based on the modules listed in the configuration file */ |
| 500 | ret = pam_authenticate(pam_h, 0); |
| 501 | if (ret != PAM_SUCCESS) { |
| 502 | if (ret == PAM_ABORT) { |
| 503 | ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 504 | goto cleanup; |
| 505 | } else { |
| 506 | VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 507 | goto cleanup; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /* correct token entered, check other requirements(the time of the day, expired token, ...) */ |
| 512 | ret = pam_acct_mgmt(pam_h, 0); |
| 513 | if ((ret != PAM_SUCCESS) && (ret != PAM_NEW_AUTHTOK_REQD)) { |
| 514 | VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 515 | goto cleanup; |
| 516 | } |
| 517 | |
| 518 | /* if a token has expired a new one will be generated */ |
| 519 | if (ret == PAM_NEW_AUTHTOK_REQD) { |
| 520 | VRB(NULL, "PAM warning occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 521 | ret = pam_chauthtok(pam_h, PAM_CHANGE_EXPIRED_AUTHTOK); |
| 522 | if (ret == PAM_SUCCESS) { |
| 523 | VRB(NULL, "The authentication token of user \"%s\" updated successfully.", session->username); |
| 524 | } else { |
| 525 | ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 526 | goto cleanup; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | cleanup: |
| 531 | /* destroy the PAM context */ |
| 532 | if (pam_end(pam_h, ret) != PAM_SUCCESS) { |
| 533 | ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret)); |
| 534 | } |
| 535 | return ret; |
| 536 | } |
| 537 | |
Michal Vasko | 0d81c57 | 2022-09-26 10:39:31 +0200 | [diff] [blame] | 538 | #endif |
| 539 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 540 | static int |
| 541 | nc_sshcb_auth_kbdint(struct nc_session *session, struct nc_server_ssh_opts *opts, ssh_message msg) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 542 | { |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 543 | int auth_ret = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 544 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 545 | if (server_opts.interactive_auth_clb) { |
| 546 | 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] | 547 | } else { |
| 548 | #ifdef HAVE_LIBPAM |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 549 | if (nc_pam_auth(session, opts, msg) == PAM_SUCCESS) { |
Michal Vasko | 0d81c57 | 2022-09-26 10:39:31 +0200 | [diff] [blame] | 550 | auth_ret = 0; |
| 551 | } |
| 552 | #else |
| 553 | ERR(session, "PAM-based SSH authentication is not supported."); |
| 554 | #endif |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | /* Authenticate message based on outcome */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 558 | if (auth_ret) { |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 559 | ++session->opts.server.ssh_auth_attempts; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 560 | VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username, |
| 561 | session->opts.server.ssh_auth_attempts); |
bhart | 1bb7cdb | 2018-07-02 15:03:30 -0500 | [diff] [blame] | 562 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 563 | } |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 564 | |
| 565 | return auth_ret; |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | * Get the public key type from binary data stored in buffer. |
| 570 | * The data is in the form of: 4 bytes = data length, then data of data length |
| 571 | * and the data is in network byte order. The key has to be in the SSH2 format. |
| 572 | */ |
| 573 | static const char * |
| 574 | nc_server_ssh_get_pubkey_type(const char *buffer, uint32_t *len) |
| 575 | { |
| 576 | uint32_t type_len; |
| 577 | |
| 578 | /* copy the 4 bytes */ |
| 579 | memcpy(&type_len, buffer, sizeof type_len); |
| 580 | /* type_len now stores the length of the key type */ |
| 581 | type_len = ntohl(type_len); |
| 582 | *len = type_len; |
| 583 | |
| 584 | /* move 4 bytes in the buffer, this is where the type should be */ |
| 585 | buffer += sizeof type_len; |
| 586 | return buffer; |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * @brief Create ssh key from base64 pubkey data. |
| 591 | * |
| 592 | * @param[in] base64 base64 encoded public key. |
| 593 | * @param[out] key created ssh key. |
| 594 | * @return 0 on success, 1 otherwise. |
| 595 | */ |
| 596 | static int |
| 597 | nc_server_ssh_create_ssh_pubkey(const char *base64, ssh_key *key) |
| 598 | { |
| 599 | int ret = 0; |
| 600 | char *bin = NULL; |
| 601 | const char *pub_type = NULL; |
| 602 | uint32_t pub_type_len = 0; |
| 603 | |
| 604 | if (!key && !base64) { |
| 605 | ERRINT; |
| 606 | ret = 1; |
| 607 | goto cleanup; |
| 608 | } |
| 609 | |
| 610 | *key = NULL; |
| 611 | |
| 612 | /* convert base64 to binary */ |
| 613 | if (nc_base64_to_bin(base64, &bin) == -1) { |
| 614 | ERR(NULL, "Unable to decode base64."); |
| 615 | ret = 1; |
| 616 | goto cleanup; |
| 617 | } |
| 618 | |
| 619 | /* get the key type and try to import it if possible */ |
| 620 | pub_type = nc_server_ssh_get_pubkey_type(bin, &pub_type_len); |
| 621 | if (!pub_type) { |
| 622 | ret = 1; |
| 623 | goto cleanup; |
| 624 | } else if (!strncmp(pub_type, "ssh-dss", pub_type_len)) { |
| 625 | ERR(NULL, "DSA keys are not supported."); |
| 626 | ret = 1; |
| 627 | goto cleanup; |
| 628 | } else if (!strncmp(pub_type, "ssh-rsa", pub_type_len)) { |
| 629 | ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_RSA, key); |
| 630 | } else if (!strncmp(pub_type, "ecdsa-sha2-nistp256", pub_type_len)) { |
| 631 | ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P256, key); |
| 632 | } else if (!strncmp(pub_type, "ecdsa-sha2-nistp384", pub_type_len)) { |
| 633 | ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P384, key); |
| 634 | } else if (!strncmp(pub_type, "ecdsa-sha2-nistp521", pub_type_len)) { |
| 635 | ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P521, key); |
| 636 | } else if (!strncmp(pub_type, "ssh-ed25519", pub_type_len)) { |
| 637 | ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ED25519, key); |
| 638 | } else { |
| 639 | ERR(NULL, "Public key type not recognised."); |
| 640 | ret = 1; |
| 641 | goto cleanup; |
| 642 | } |
| 643 | |
| 644 | cleanup: |
| 645 | if (ret != SSH_OK) { |
| 646 | ERR(NULL, "Error importing public key."); |
| 647 | } |
| 648 | free(bin); |
| 649 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 650 | } |
| 651 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 652 | /** |
| 653 | * @brief Compare SSH key with configured authorized keys and return the username of the matching one, if any. |
| 654 | * |
| 655 | * @param[in] key Presented SSH key to compare. |
| 656 | * @return Authorized key username, NULL if no match was found. |
| 657 | */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 658 | static int |
| 659 | 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] | 660 | { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 661 | uint16_t i, pubkey_count; |
Michal Vasko | 3e9d168 | 2017-02-24 09:50:15 +0100 | [diff] [blame] | 662 | int ret = 0; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 663 | ssh_key new_key = NULL; |
| 664 | struct nc_public_key *pubkeys; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 665 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 666 | /* get the correct public key storage */ |
| 667 | if (auth_client->store == NC_STORE_LOCAL) { |
| 668 | pubkeys = auth_client->pubkeys; |
| 669 | pubkey_count = auth_client->pubkey_count; |
| 670 | } else { |
| 671 | ret = nc_server_ssh_ts_ref_get_keys(auth_client->ts_ref, &pubkeys, &pubkey_count); |
| 672 | if (ret) { |
| 673 | ERR(NULL, "Error getting \"%s\"'s public keys from the truststore.", auth_client->username); |
| 674 | return ret; |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 675 | } |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 676 | } |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 677 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 678 | /* try to compare all of the client's keys with the key received in the SSH message */ |
| 679 | for (i = 0; i < pubkey_count; i++) { |
| 680 | /* create the SSH key from the data */ |
| 681 | if (nc_server_ssh_create_ssh_pubkey(pubkeys[i].data, &new_key)) { |
| 682 | ssh_key_free(new_key); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 683 | continue; |
| 684 | } |
| 685 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 686 | /* compare the keys */ |
| 687 | ret = ssh_key_cmp(key, new_key, SSH_KEY_CMP_PUBLIC); |
| 688 | if (!ret) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 689 | break; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 690 | } else { |
| 691 | WRN(NULL, "User's \"%s\" public key doesn't match, trying another.", auth_client->username); |
| 692 | ssh_key_free(new_key); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 693 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 694 | } |
| 695 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 696 | if (i == pubkey_count) { |
| 697 | ret = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 698 | } |
| 699 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 700 | if (!ret) { |
| 701 | /* only free a key if everything was ok, it would have already been freed otherwise */ |
| 702 | ssh_key_free(new_key); |
| 703 | } |
Michal Vasko | a05c7b1 | 2017-01-30 14:33:08 +0100 | [diff] [blame] | 704 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 705 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | static void |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 709 | 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] | 710 | { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 711 | if (auth_client->supports_none && !auth_client->password && !auth_client->pubkey_count && !auth_client->pam_config_name) { |
| 712 | /* only authenticate the client if he supports none and no other method */ |
| 713 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 714 | VRB(session, "User \"%s\" authenticated.", session->username); |
| 715 | ssh_message_auth_reply_success(msg, 0); |
| 716 | } |
| 717 | |
| 718 | ssh_message_reply_default(msg); |
| 719 | } |
| 720 | |
| 721 | static int |
| 722 | nc_sshcb_auth_pubkey(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg) |
| 723 | { |
| 724 | int signature_state, ret = 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 725 | |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 726 | if (server_opts.pubkey_auth_clb) { |
| 727 | if (server_opts.pubkey_auth_clb(session, ssh_message_auth_pubkey(msg), server_opts.pubkey_auth_data)) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 728 | ret = 1; |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 729 | goto fail; |
| 730 | } |
Michal Vasko | 733c0bd | 2018-07-03 13:14:40 +0200 | [diff] [blame] | 731 | } else { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 732 | if (auth_pubkey_compare_key(ssh_message_auth_pubkey(msg), auth_client)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 733 | VRB(session, "User \"%s\" tried to use an unknown (unauthorized) public key.", session->username); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 734 | ret = 1; |
bhart | 3bc2f58 | 2018-06-05 12:40:32 -0500 | [diff] [blame] | 735 | goto fail; |
| 736 | } |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 737 | } |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 738 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 739 | signature_state = ssh_message_auth_publickey_state(msg); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 740 | if (signature_state == SSH_PUBLICKEY_STATE_NONE) { |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 741 | /* accepting only the use of a public key */ |
| 742 | ssh_message_auth_reply_pk_ok_simple(msg); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 743 | ret = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 744 | } |
| 745 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 746 | return ret; |
Michal Vasko | bd13a93 | 2016-09-14 09:00:35 +0200 | [diff] [blame] | 747 | |
| 748 | fail: |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 749 | ++session->opts.server.ssh_auth_attempts; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 750 | VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username, |
| 751 | session->opts.server.ssh_auth_attempts); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 752 | ssh_message_reply_default(msg); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 753 | |
| 754 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | static int |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 758 | nc_sshcb_channel_open(struct nc_session *session, ssh_message msg) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 759 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 760 | ssh_channel chan; |
| 761 | |
| 762 | /* first channel request */ |
| 763 | if (!session->ti.libssh.channel) { |
| 764 | if (session->status != NC_STATUS_STARTING) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 765 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 766 | return -1; |
| 767 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 768 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 769 | if (!chan) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 770 | ERR(session, "Failed to create a new SSH channel."); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 771 | return -1; |
| 772 | } |
| 773 | session->ti.libssh.channel = chan; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 774 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 775 | /* additional channel request */ |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 776 | } else { |
| 777 | chan = ssh_message_channel_request_open_reply_accept(msg); |
| 778 | if (!chan) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 779 | 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] | 780 | return -1; |
| 781 | } |
| 782 | /* 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] | 783 | } |
| 784 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | static int |
| 789 | nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem) |
| 790 | { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 791 | struct nc_session *new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 792 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 793 | if (strcmp(subsystem, "netconf")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 794 | WRN(session, "Received an unknown subsystem \"%s\" request.", subsystem); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 795 | return -1; |
| 796 | } |
| 797 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 798 | if (session->ti.libssh.channel == channel) { |
| 799 | /* first channel requested */ |
| 800 | if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) { |
| 801 | ERRINT; |
| 802 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 803 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 804 | if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 805 | ERR(session, "Subsystem \"netconf\" requested for the second time."); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 806 | return -1; |
| 807 | } |
| 808 | |
| 809 | session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 810 | } else { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 811 | /* 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] | 812 | new_session = nc_new_session(NC_SERVER, 1); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 813 | NC_CHECK_ERRMEM_RET(!new_session, -1); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 814 | |
| 815 | /* insert the new session */ |
| 816 | if (!session->ti.libssh.next) { |
| 817 | new_session->ti.libssh.next = session; |
| 818 | } else { |
| 819 | new_session->ti.libssh.next = session->ti.libssh.next; |
| 820 | } |
| 821 | session->ti.libssh.next = new_session; |
| 822 | |
| 823 | new_session->status = NC_STATUS_STARTING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 824 | new_session->ti_type = NC_TI_LIBSSH; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 825 | new_session->io_lock = session->io_lock; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 826 | new_session->ti.libssh.channel = channel; |
| 827 | new_session->ti.libssh.session = session->ti.libssh.session; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 828 | new_session->username = strdup(session->username); |
| 829 | new_session->host = strdup(session->host); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 830 | new_session->port = session->port; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 831 | new_session->ctx = (struct ly_ctx *)session->ctx; |
Michal Vasko | 83d1532 | 2018-09-27 09:44:02 +0200 | [diff] [blame] | 832 | 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] | 833 | } |
| 834 | |
| 835 | return 0; |
| 836 | } |
| 837 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 838 | int |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 839 | 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] | 840 | { |
| 841 | const char *str_type, *str_subtype = NULL, *username; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 842 | int subtype, type, libssh_auth_methods = 0, ret = 0; |
| 843 | uint16_t i; |
| 844 | struct nc_auth_client *auth_client = NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 845 | |
| 846 | type = ssh_message_type(msg); |
| 847 | subtype = ssh_message_subtype(msg); |
| 848 | |
| 849 | switch (type) { |
| 850 | case SSH_REQUEST_AUTH: |
| 851 | str_type = "request-auth"; |
| 852 | switch (subtype) { |
| 853 | case SSH_AUTH_METHOD_NONE: |
| 854 | str_subtype = "none"; |
| 855 | break; |
| 856 | case SSH_AUTH_METHOD_PASSWORD: |
| 857 | str_subtype = "password"; |
| 858 | break; |
| 859 | case SSH_AUTH_METHOD_PUBLICKEY: |
| 860 | str_subtype = "publickey"; |
| 861 | break; |
| 862 | case SSH_AUTH_METHOD_HOSTBASED: |
| 863 | str_subtype = "hostbased"; |
| 864 | break; |
| 865 | case SSH_AUTH_METHOD_INTERACTIVE: |
| 866 | str_subtype = "interactive"; |
| 867 | break; |
| 868 | case SSH_AUTH_METHOD_GSSAPI_MIC: |
| 869 | str_subtype = "gssapi-mic"; |
| 870 | break; |
| 871 | } |
| 872 | break; |
| 873 | |
| 874 | case SSH_REQUEST_CHANNEL_OPEN: |
| 875 | str_type = "request-channel-open"; |
| 876 | switch (subtype) { |
| 877 | case SSH_CHANNEL_SESSION: |
| 878 | str_subtype = "session"; |
| 879 | break; |
| 880 | case SSH_CHANNEL_DIRECT_TCPIP: |
| 881 | str_subtype = "direct-tcpip"; |
| 882 | break; |
| 883 | case SSH_CHANNEL_FORWARDED_TCPIP: |
| 884 | str_subtype = "forwarded-tcpip"; |
| 885 | break; |
| 886 | case (int)SSH_CHANNEL_X11: |
| 887 | str_subtype = "channel-x11"; |
| 888 | break; |
| 889 | case SSH_CHANNEL_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 890 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 891 | default: |
| 892 | str_subtype = "unknown"; |
| 893 | break; |
| 894 | } |
| 895 | break; |
| 896 | |
| 897 | case SSH_REQUEST_CHANNEL: |
| 898 | str_type = "request-channel"; |
| 899 | switch (subtype) { |
| 900 | case SSH_CHANNEL_REQUEST_PTY: |
| 901 | str_subtype = "pty"; |
| 902 | break; |
| 903 | case SSH_CHANNEL_REQUEST_EXEC: |
| 904 | str_subtype = "exec"; |
| 905 | break; |
| 906 | case SSH_CHANNEL_REQUEST_SHELL: |
| 907 | str_subtype = "shell"; |
| 908 | break; |
| 909 | case SSH_CHANNEL_REQUEST_ENV: |
| 910 | str_subtype = "env"; |
| 911 | break; |
| 912 | case SSH_CHANNEL_REQUEST_SUBSYSTEM: |
| 913 | str_subtype = "subsystem"; |
| 914 | break; |
| 915 | case SSH_CHANNEL_REQUEST_WINDOW_CHANGE: |
| 916 | str_subtype = "window-change"; |
| 917 | break; |
| 918 | case SSH_CHANNEL_REQUEST_X11: |
| 919 | str_subtype = "x11"; |
| 920 | break; |
| 921 | case SSH_CHANNEL_REQUEST_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 922 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 923 | default: |
| 924 | str_subtype = "unknown"; |
| 925 | break; |
| 926 | } |
| 927 | break; |
| 928 | |
| 929 | case SSH_REQUEST_SERVICE: |
| 930 | str_type = "request-service"; |
| 931 | str_subtype = ssh_message_service_service(msg); |
| 932 | break; |
| 933 | |
| 934 | case SSH_REQUEST_GLOBAL: |
| 935 | str_type = "request-global"; |
| 936 | switch (subtype) { |
| 937 | case SSH_GLOBAL_REQUEST_TCPIP_FORWARD: |
| 938 | str_subtype = "tcpip-forward"; |
| 939 | break; |
| 940 | case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD: |
| 941 | str_subtype = "cancel-tcpip-forward"; |
| 942 | break; |
| 943 | case SSH_GLOBAL_REQUEST_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 944 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 945 | default: |
| 946 | str_subtype = "unknown"; |
| 947 | break; |
| 948 | } |
| 949 | break; |
| 950 | |
| 951 | default: |
| 952 | str_type = "unknown"; |
| 953 | str_subtype = "unknown"; |
| 954 | break; |
| 955 | } |
| 956 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 957 | 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] | 958 | if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) { |
Michal Vasko | ce31916 | 2016-02-03 15:33:08 +0100 | [diff] [blame] | 959 | /* "valid" situation if, for example, receiving some auth or channel request timeouted, |
| 960 | * but we got it now, during session free */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 961 | 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] | 962 | (session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid")); |
Michal Vasko | ce31916 | 2016-02-03 15:33:08 +0100 | [diff] [blame] | 963 | ssh_message_reply_default(msg); |
| 964 | return 0; |
| 965 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 966 | |
| 967 | /* |
| 968 | * process known messages |
| 969 | */ |
| 970 | if (type == SSH_REQUEST_AUTH) { |
| 971 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 972 | ERR(session, "User \"%s\" authenticated, but requested another authentication.", session->username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 973 | ssh_message_reply_default(msg); |
| 974 | return 0; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 975 | } else if (!state || !opts) { |
| 976 | /* these two parameters should always be set during an authentication, |
| 977 | * however do a check just in case something goes really wrong, since they |
| 978 | * are not needed for other types of messages |
| 979 | */ |
| 980 | ERRINT; |
| 981 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 982 | } |
| 983 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 984 | /* save the username, do not let the client change it */ |
| 985 | username = ssh_message_auth_user(msg); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 986 | assert(username); |
| 987 | |
| 988 | for (i = 0; i < opts->client_count; i++) { |
| 989 | if (!strcmp(opts->auth_clients[i].username, username)) { |
| 990 | auth_client = &opts->auth_clients[i]; |
| 991 | break; |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | if (!auth_client) { |
| 996 | if (opts->endpt_client_ref) { |
| 997 | return nc_session_ssh_msg(session, opts->endpt_client_ref->opts.ssh, msg, state); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 998 | } |
| 999 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1000 | ERR(NULL, "User \"%s\" not known by the server.", username); |
| 1001 | ssh_message_reply_default(msg); |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
| 1005 | if (!session->username) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1006 | session->username = strdup(username); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1007 | |
| 1008 | /* configure and count accepted auth methods */ |
| 1009 | if (auth_client->store == NC_STORE_LOCAL) { |
| 1010 | if (auth_client->pubkey_count) { |
| 1011 | libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY; |
| 1012 | } |
| 1013 | } else if (auth_client->ts_ref) { |
| 1014 | libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY; |
| 1015 | } |
| 1016 | if (auth_client->password) { |
| 1017 | state->auth_method_count++; |
| 1018 | libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD; |
| 1019 | } |
| 1020 | if (auth_client->pam_config_name) { |
| 1021 | state->auth_method_count++; |
| 1022 | libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE; |
| 1023 | } |
| 1024 | if (auth_client->supports_none) { |
| 1025 | libssh_auth_methods |= SSH_AUTH_METHOD_NONE; |
| 1026 | } |
| 1027 | |
| 1028 | if (libssh_auth_methods & SSH_AUTH_METHOD_PUBLICKEY) { |
| 1029 | state->auth_method_count++; |
| 1030 | } |
| 1031 | |
| 1032 | ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods); |
| 1033 | } else { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1034 | if (strcmp(username, session->username)) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1035 | /* changing username not allowed */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1036 | ERR(session, "User \"%s\" changed its username to \"%s\".", session->username, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1037 | session->status = NC_STATUS_INVALID; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1038 | session->term_reason = NC_SESSION_TERM_OTHER; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1039 | return 1; |
| 1040 | } |
| 1041 | } |
| 1042 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1043 | /* 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] | 1044 | if (subtype == SSH_AUTH_METHOD_NONE) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1045 | nc_sshcb_auth_none(session, auth_client, msg); |
| 1046 | ret = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1047 | } else if (subtype == SSH_AUTH_METHOD_PASSWORD) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1048 | ret = nc_sshcb_auth_password(session, auth_client, msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1049 | } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1050 | ret = nc_sshcb_auth_pubkey(session, auth_client, msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1051 | } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1052 | ret = nc_sshcb_auth_kbdint(session, opts, msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1053 | } |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1054 | |
| 1055 | if (!ret) { |
| 1056 | state->auth_success_count++; |
| 1057 | } |
| 1058 | |
| 1059 | if (!ret && (state->auth_success_count < state->auth_method_count)) { |
| 1060 | /* success, but he needs to do another method */ |
| 1061 | VRB(session, "User \"%s\" partially authenticated, but still needs to authenticate via the rest of his configured methods.", username); |
| 1062 | ssh_message_auth_reply_success(msg, 1); |
| 1063 | } else if (!ret && (state->auth_success_count == state->auth_method_count)) { |
| 1064 | /* authenticated */ |
| 1065 | ssh_message_auth_reply_success(msg, 0); |
| 1066 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 1067 | VRB(session, "User \"%s\" authenticated.", username); |
| 1068 | } |
| 1069 | |
| 1070 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1071 | } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1072 | 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] | 1073 | if (nc_sshcb_channel_open(session, msg)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1074 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1075 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1076 | return 0; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1077 | |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1078 | } 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] | 1079 | if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg), |
| 1080 | ssh_message_channel_request_subsystem(msg))) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1081 | ssh_message_reply_default(msg); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1082 | } else { |
| 1083 | ssh_message_channel_request_reply_success(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1084 | } |
| 1085 | return 0; |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | /* we did not process it */ |
| 1090 | return 1; |
| 1091 | } |
| 1092 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1093 | /* ret 1 on success, 0 on timeout, -1 on error */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1094 | static int |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1095 | 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] | 1096 | { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1097 | struct timespec ts_timeout; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1098 | ssh_message msg; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1099 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1100 | if (timeout) { |
| 1101 | nc_timeouttime_get(&ts_timeout, timeout * 1000); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1102 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1103 | while (1) { |
| 1104 | if (!nc_session_is_connected(session)) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1105 | ERR(session, "Communication SSH socket unexpectedly closed."); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1106 | return -1; |
| 1107 | } |
| 1108 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1109 | msg = ssh_message_get(session->ti.libssh.session); |
| 1110 | if (msg) { |
| 1111 | if (nc_session_ssh_msg(session, opts, msg, NULL)) { |
| 1112 | ssh_message_reply_default(msg); |
| 1113 | } |
| 1114 | ssh_message_free(msg); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1115 | } |
| 1116 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1117 | if (session->ti.libssh.channel && session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1118 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1119 | } |
| 1120 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1121 | usleep(NC_TIMEOUT_STEP); |
roman | ea0edaa | 2023-10-26 12:16:25 +0200 | [diff] [blame^] | 1122 | if (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1123 | /* timeout */ |
| 1124 | ERR(session, "Failed to start \"netconf\" SSH subsystem for too long, disconnecting."); |
| 1125 | break; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1126 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1127 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1128 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1129 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1130 | } |
| 1131 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 1132 | /** |
| 1133 | * @brief Set hostkeys to be used for an SSH bind. |
| 1134 | * |
| 1135 | * @param[in] sbind SSH bind to use. |
| 1136 | * @param[in] hostkeys Array of hostkeys. |
| 1137 | * @param[in] hostkey_count Count of @p hostkeys. |
| 1138 | * @return 0 on success. |
| 1139 | * @return -1 on error. |
| 1140 | */ |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1141 | static int |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1142 | 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] | 1143 | { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1144 | uint16_t i; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1145 | char *privkey_path, *privkey_data; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1146 | int ret; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1147 | struct nc_asymmetric_key *key = NULL; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1148 | |
| 1149 | for (i = 0; i < hostkey_count; ++i) { |
| 1150 | privkey_path = privkey_data = NULL; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1151 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1152 | /* get the asymmetric key */ |
| 1153 | if (opts->hostkeys[i].store == NC_STORE_LOCAL) { |
| 1154 | /* stored locally */ |
| 1155 | key = &opts->hostkeys[i].key; |
| 1156 | } else { |
| 1157 | /* keystore reference, need to get it */ |
| 1158 | 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] | 1159 | return -1; |
| 1160 | } |
| 1161 | } |
| 1162 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1163 | privkey_path = base64der_privkey_to_tmp_file(key->privkey_data, nc_privkey_format_to_str(key->privkey_type)); |
| 1164 | if (!privkey_path) { |
| 1165 | ERR(NULL, "Temporarily storing a host key into a file failed (%s).", strerror(errno)); |
| 1166 | return -1; |
| 1167 | } |
| 1168 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1169 | ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path); |
| 1170 | |
| 1171 | /* cleanup */ |
| 1172 | if (privkey_data && unlink(privkey_path)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1173 | 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] | 1174 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1175 | |
| 1176 | if (ret != SSH_OK) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1177 | 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] | 1178 | } |
| 1179 | free(privkey_path); |
| 1180 | |
| 1181 | if (ret != SSH_OK) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1182 | return -1; |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1189 | static int |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1190 | 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] | 1191 | { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1192 | struct timespec ts_timeout; |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1193 | ssh_message msg; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1194 | struct nc_auth_state state = {0}; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1195 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1196 | /* authenticate */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1197 | if (opts->auth_timeout) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1198 | nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1199 | } |
| 1200 | while (1) { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1201 | if (!nc_session_is_connected(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1202 | ERR(session, "Communication SSH socket unexpectedly closed."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1203 | return -1; |
| 1204 | } |
| 1205 | |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1206 | msg = ssh_message_get(session->ti.libssh.session); |
| 1207 | if (msg) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1208 | if (nc_session_ssh_msg(session, opts, msg, &state)) { |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1209 | ssh_message_reply_default(msg); |
| 1210 | } |
| 1211 | ssh_message_free(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1212 | } |
| 1213 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1214 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
| 1215 | break; |
| 1216 | } |
| 1217 | |
Michal Vasko | 145ae67 | 2017-02-07 10:57:27 +0100 | [diff] [blame] | 1218 | if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1219 | ERR(session, "Too many failed authentication attempts of user \"%s\".", session->username); |
Michal Vasko | 145ae67 | 2017-02-07 10:57:27 +0100 | [diff] [blame] | 1220 | return -1; |
| 1221 | } |
| 1222 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1223 | usleep(NC_TIMEOUT_STEP); |
roman | ea0edaa | 2023-10-26 12:16:25 +0200 | [diff] [blame^] | 1224 | if (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1225 | /* timeout */ |
| 1226 | break; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1227 | } |
| 1228 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1229 | |
| 1230 | if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) { |
| 1231 | /* timeout */ |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1232 | if (session->username) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1233 | 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] | 1234 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1235 | ERR(session, "User failed to authenticate for too long, disconnecting."); |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1236 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1237 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1238 | } |
| 1239 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1240 | return 1; |
| 1241 | } |
| 1242 | |
| 1243 | int |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1244 | 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] | 1245 | { |
| 1246 | ssh_bind sbind = NULL; |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1247 | int rc = 1, r; |
| 1248 | struct timespec ts_timeout; |
roman | 4cc0cd5 | 2023-04-14 09:12:29 +0200 | [diff] [blame] | 1249 | const char *err_msg; |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1250 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1251 | /* other transport-specific data */ |
| 1252 | session->ti_type = NC_TI_LIBSSH; |
| 1253 | session->ti.libssh.session = ssh_new(); |
| 1254 | if (!session->ti.libssh.session) { |
| 1255 | ERR(NULL, "Failed to initialize a new SSH session."); |
| 1256 | rc = -1; |
| 1257 | goto cleanup; |
| 1258 | } |
| 1259 | |
| 1260 | sbind = ssh_bind_new(); |
| 1261 | if (!sbind) { |
| 1262 | ERR(session, "Failed to create an SSH bind."); |
| 1263 | rc = -1; |
| 1264 | goto cleanup; |
| 1265 | } |
| 1266 | |
| 1267 | /* configure host keys */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1268 | if (nc_ssh_bind_add_hostkeys(sbind, opts, opts->hostkey_count)) { |
| 1269 | rc = -1; |
| 1270 | goto cleanup; |
| 1271 | } |
| 1272 | |
| 1273 | /* configure supported algorithms */ |
| 1274 | if (opts->hostkey_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, opts->hostkey_algs)) { |
| 1275 | rc = -1; |
| 1276 | goto cleanup; |
| 1277 | } |
| 1278 | if (opts->encryption_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_CIPHERS_S_C, opts->encryption_algs)) { |
| 1279 | rc = -1; |
| 1280 | goto cleanup; |
| 1281 | } |
| 1282 | if (opts->kex_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_KEY_EXCHANGE, opts->kex_algs)) { |
| 1283 | rc = -1; |
| 1284 | goto cleanup; |
| 1285 | } |
| 1286 | 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] | 1287 | rc = -1; |
| 1288 | goto cleanup; |
| 1289 | } |
| 1290 | |
| 1291 | /* accept new connection on the bind */ |
| 1292 | if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) { |
| 1293 | ERR(session, "SSH failed to accept a new connection (%s).", ssh_get_error(sbind)); |
| 1294 | rc = -1; |
| 1295 | goto cleanup; |
| 1296 | } |
| 1297 | sock = -1; |
| 1298 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1299 | /* set to non-blocking */ |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1300 | ssh_set_blocking(session->ti.libssh.session, 0); |
| 1301 | |
| 1302 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1303 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1304 | } |
| 1305 | while ((r = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) { |
| 1306 | /* this tends to take longer */ |
| 1307 | usleep(NC_TIMEOUT_STEP * 20); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1308 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1309 | break; |
| 1310 | } |
| 1311 | } |
| 1312 | if (r == SSH_AGAIN) { |
| 1313 | ERR(session, "SSH key exchange timeout."); |
| 1314 | rc = 0; |
| 1315 | goto cleanup; |
| 1316 | } else if (r != SSH_OK) { |
roman | 4cc0cd5 | 2023-04-14 09:12:29 +0200 | [diff] [blame] | 1317 | err_msg = ssh_get_error(session->ti.libssh.session); |
| 1318 | if (err_msg[0] == '\0') { |
| 1319 | err_msg = "hostkey algorithm generated from the hostkey most likely not found in the set of configured hostkey algorithms"; |
| 1320 | } |
| 1321 | ERR(session, "SSH key exchange error (%s).", err_msg); |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1322 | rc = -1; |
| 1323 | goto cleanup; |
| 1324 | } |
| 1325 | |
| 1326 | /* authenticate */ |
| 1327 | if ((rc = nc_accept_ssh_session_auth(session, opts)) != 1) { |
| 1328 | goto cleanup; |
| 1329 | } |
| 1330 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1331 | /* open channel and request 'netconf' subsystem */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1332 | 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] | 1333 | goto cleanup; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1334 | } |
| 1335 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1336 | cleanup: |
| 1337 | if (sock > -1) { |
| 1338 | close(sock); |
| 1339 | } |
| 1340 | ssh_bind_free(sbind); |
| 1341 | return rc; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1342 | } |
| 1343 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1344 | API NC_MSG_TYPE |
| 1345 | nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session) |
| 1346 | { |
| 1347 | NC_MSG_TYPE msgtype; |
| 1348 | struct nc_session *new_session = NULL; |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1349 | struct timespec ts_cur; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1350 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1351 | NC_CHECK_ARG_RET(orig_session, orig_session, session, NC_MSG_ERROR); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1352 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1353 | if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) && |
| 1354 | orig_session->ti.libssh.next) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1355 | for (new_session = orig_session->ti.libssh.next; |
| 1356 | new_session != orig_session; |
| 1357 | new_session = new_session->ti.libssh.next) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1358 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel && |
| 1359 | (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1360 | /* we found our session */ |
| 1361 | break; |
| 1362 | } |
| 1363 | } |
| 1364 | if (new_session == orig_session) { |
| 1365 | new_session = NULL; |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | if (!new_session) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1370 | ERR(orig_session, "Session does not have a NETCONF SSH channel ready."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1371 | return NC_MSG_ERROR; |
| 1372 | } |
| 1373 | |
| 1374 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 1375 | new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1376 | |
| 1377 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1378 | msgtype = nc_handshake_io(new_session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1379 | if (msgtype != NC_MSG_HELLO) { |
| 1380 | return msgtype; |
| 1381 | } |
| 1382 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1383 | nc_realtime_get(&ts_cur); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1384 | new_session->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1385 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1386 | new_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1387 | new_session->status = NC_STATUS_RUNNING; |
| 1388 | *session = new_session; |
| 1389 | |
| 1390 | return msgtype; |
| 1391 | } |
| 1392 | |
| 1393 | API NC_MSG_TYPE |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1394 | 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] | 1395 | { |
Michal Vasko | bdcf236 | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1396 | uint8_t q_id; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1397 | NC_MSG_TYPE msgtype; |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1398 | struct nc_session *new_session = NULL, *cur_session; |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1399 | struct timespec ts_cur; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1400 | uint16_t i; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1401 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1402 | NC_CHECK_ARG_RET(NULL, ps, session, NC_MSG_ERROR); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1403 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1404 | /* LOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1405 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1406 | return NC_MSG_ERROR; |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1407 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1408 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1409 | for (i = 0; i < ps->session_count; ++i) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1410 | cur_session = ps->sessions[i]->session; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1411 | if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH) && |
| 1412 | cur_session->ti.libssh.next) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1413 | /* an SSH session with more channels */ |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1414 | for (new_session = cur_session->ti.libssh.next; |
| 1415 | new_session != cur_session; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1416 | new_session = new_session->ti.libssh.next) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1417 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel && |
| 1418 | (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1419 | /* we found our session */ |
| 1420 | break; |
| 1421 | } |
| 1422 | } |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1423 | if (new_session != cur_session) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1424 | break; |
| 1425 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1426 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1427 | new_session = NULL; |
| 1428 | } |
| 1429 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1430 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1431 | /* UNLOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1432 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1433 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1434 | if (!new_session) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1435 | ERR(NULL, "No session with a NETCONF SSH channel ready was found."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1436 | return NC_MSG_ERROR; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 1440 | new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1441 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1442 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1443 | msgtype = nc_handshake_io(new_session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1444 | if (msgtype != NC_MSG_HELLO) { |
| 1445 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1446 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1447 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1448 | nc_realtime_get(&ts_cur); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1449 | new_session->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1450 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1451 | new_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1452 | new_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1453 | *session = new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1454 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1455 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1456 | } |