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