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; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 832 | |
| 833 | type = ssh_message_type(msg); |
| 834 | subtype = ssh_message_subtype(msg); |
| 835 | |
| 836 | switch (type) { |
| 837 | case SSH_REQUEST_AUTH: |
| 838 | str_type = "request-auth"; |
| 839 | switch (subtype) { |
| 840 | case SSH_AUTH_METHOD_NONE: |
| 841 | str_subtype = "none"; |
| 842 | break; |
| 843 | case SSH_AUTH_METHOD_PASSWORD: |
| 844 | str_subtype = "password"; |
| 845 | break; |
| 846 | case SSH_AUTH_METHOD_PUBLICKEY: |
| 847 | str_subtype = "publickey"; |
| 848 | break; |
| 849 | case SSH_AUTH_METHOD_HOSTBASED: |
| 850 | str_subtype = "hostbased"; |
| 851 | break; |
| 852 | case SSH_AUTH_METHOD_INTERACTIVE: |
| 853 | str_subtype = "interactive"; |
| 854 | break; |
| 855 | case SSH_AUTH_METHOD_GSSAPI_MIC: |
| 856 | str_subtype = "gssapi-mic"; |
| 857 | break; |
| 858 | } |
| 859 | break; |
| 860 | |
| 861 | case SSH_REQUEST_CHANNEL_OPEN: |
| 862 | str_type = "request-channel-open"; |
| 863 | switch (subtype) { |
| 864 | case SSH_CHANNEL_SESSION: |
| 865 | str_subtype = "session"; |
| 866 | break; |
| 867 | case SSH_CHANNEL_DIRECT_TCPIP: |
| 868 | str_subtype = "direct-tcpip"; |
| 869 | break; |
| 870 | case SSH_CHANNEL_FORWARDED_TCPIP: |
| 871 | str_subtype = "forwarded-tcpip"; |
| 872 | break; |
| 873 | case (int)SSH_CHANNEL_X11: |
| 874 | str_subtype = "channel-x11"; |
| 875 | break; |
| 876 | case SSH_CHANNEL_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 877 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 878 | default: |
| 879 | str_subtype = "unknown"; |
| 880 | break; |
| 881 | } |
| 882 | break; |
| 883 | |
| 884 | case SSH_REQUEST_CHANNEL: |
| 885 | str_type = "request-channel"; |
| 886 | switch (subtype) { |
| 887 | case SSH_CHANNEL_REQUEST_PTY: |
| 888 | str_subtype = "pty"; |
| 889 | break; |
| 890 | case SSH_CHANNEL_REQUEST_EXEC: |
| 891 | str_subtype = "exec"; |
| 892 | break; |
| 893 | case SSH_CHANNEL_REQUEST_SHELL: |
| 894 | str_subtype = "shell"; |
| 895 | break; |
| 896 | case SSH_CHANNEL_REQUEST_ENV: |
| 897 | str_subtype = "env"; |
| 898 | break; |
| 899 | case SSH_CHANNEL_REQUEST_SUBSYSTEM: |
| 900 | str_subtype = "subsystem"; |
| 901 | break; |
| 902 | case SSH_CHANNEL_REQUEST_WINDOW_CHANGE: |
| 903 | str_subtype = "window-change"; |
| 904 | break; |
| 905 | case SSH_CHANNEL_REQUEST_X11: |
| 906 | str_subtype = "x11"; |
| 907 | break; |
| 908 | case SSH_CHANNEL_REQUEST_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 909 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 910 | default: |
| 911 | str_subtype = "unknown"; |
| 912 | break; |
| 913 | } |
| 914 | break; |
| 915 | |
| 916 | case SSH_REQUEST_SERVICE: |
| 917 | str_type = "request-service"; |
| 918 | str_subtype = ssh_message_service_service(msg); |
| 919 | break; |
| 920 | |
| 921 | case SSH_REQUEST_GLOBAL: |
| 922 | str_type = "request-global"; |
| 923 | switch (subtype) { |
| 924 | case SSH_GLOBAL_REQUEST_TCPIP_FORWARD: |
| 925 | str_subtype = "tcpip-forward"; |
| 926 | break; |
| 927 | case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD: |
| 928 | str_subtype = "cancel-tcpip-forward"; |
| 929 | break; |
| 930 | case SSH_GLOBAL_REQUEST_UNKNOWN: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 931 | /* fallthrough */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 932 | default: |
| 933 | str_subtype = "unknown"; |
| 934 | break; |
| 935 | } |
| 936 | break; |
| 937 | |
| 938 | default: |
| 939 | str_type = "unknown"; |
| 940 | str_subtype = "unknown"; |
| 941 | break; |
| 942 | } |
| 943 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 944 | 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] | 945 | if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) { |
Michal Vasko | ce31916 | 2016-02-03 15:33:08 +0100 | [diff] [blame] | 946 | /* "valid" situation if, for example, receiving some auth or channel request timeouted, |
| 947 | * but we got it now, during session free */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 948 | 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] | 949 | (session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid")); |
Michal Vasko | ce31916 | 2016-02-03 15:33:08 +0100 | [diff] [blame] | 950 | ssh_message_reply_default(msg); |
| 951 | return 0; |
| 952 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 953 | |
| 954 | /* |
| 955 | * process known messages |
| 956 | */ |
| 957 | if (type == SSH_REQUEST_AUTH) { |
| 958 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 959 | ERR(session, "User \"%s\" authenticated, but requested another authentication.", session->username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 960 | ssh_message_reply_default(msg); |
| 961 | return 0; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 962 | } else if (!state || !opts) { |
| 963 | /* these two parameters should always be set during an authentication, |
| 964 | * however do a check just in case something goes really wrong, since they |
| 965 | * are not needed for other types of messages |
| 966 | */ |
| 967 | ERRINT; |
| 968 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 969 | } |
| 970 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 971 | /* save the username, do not let the client change it */ |
| 972 | username = ssh_message_auth_user(msg); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 973 | assert(username); |
| 974 | |
| 975 | for (i = 0; i < opts->client_count; i++) { |
| 976 | if (!strcmp(opts->auth_clients[i].username, username)) { |
| 977 | auth_client = &opts->auth_clients[i]; |
| 978 | break; |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | if (!auth_client) { |
| 983 | if (opts->endpt_client_ref) { |
| 984 | 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] | 985 | } |
| 986 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 987 | ERR(NULL, "User \"%s\" not known by the server.", username); |
| 988 | ssh_message_reply_default(msg); |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | if (!session->username) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 993 | session->username = strdup(username); |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 994 | |
| 995 | /* configure and count accepted auth methods */ |
| 996 | if (auth_client->store == NC_STORE_LOCAL) { |
| 997 | if (auth_client->pubkey_count) { |
| 998 | libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY; |
| 999 | } |
| 1000 | } else if (auth_client->ts_ref) { |
| 1001 | libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY; |
| 1002 | } |
| 1003 | if (auth_client->password) { |
| 1004 | state->auth_method_count++; |
| 1005 | libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD; |
| 1006 | } |
| 1007 | if (auth_client->pam_config_name) { |
| 1008 | state->auth_method_count++; |
| 1009 | libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE; |
| 1010 | } |
| 1011 | if (auth_client->supports_none) { |
| 1012 | libssh_auth_methods |= SSH_AUTH_METHOD_NONE; |
| 1013 | } |
| 1014 | |
| 1015 | if (libssh_auth_methods & SSH_AUTH_METHOD_PUBLICKEY) { |
| 1016 | state->auth_method_count++; |
| 1017 | } |
| 1018 | |
| 1019 | ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods); |
| 1020 | } else { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1021 | if (strcmp(username, session->username)) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1022 | /* changing username not allowed */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1023 | ERR(session, "User \"%s\" changed its username to \"%s\".", session->username, username); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1024 | session->status = NC_STATUS_INVALID; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1025 | session->term_reason = NC_SESSION_TERM_OTHER; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1026 | return 1; |
| 1027 | } |
| 1028 | } |
| 1029 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1030 | /* 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] | 1031 | if (subtype == SSH_AUTH_METHOD_NONE) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1032 | nc_sshcb_auth_none(session, auth_client, msg); |
| 1033 | ret = 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1034 | } else if (subtype == SSH_AUTH_METHOD_PASSWORD) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1035 | ret = nc_sshcb_auth_password(session, auth_client, msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1036 | } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1037 | ret = nc_sshcb_auth_pubkey(session, auth_client, msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1038 | } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1039 | ret = nc_sshcb_auth_kbdint(session, opts, msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1040 | } |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1041 | |
| 1042 | if (!ret) { |
| 1043 | state->auth_success_count++; |
| 1044 | } |
| 1045 | |
| 1046 | if (!ret && (state->auth_success_count < state->auth_method_count)) { |
| 1047 | /* success, but he needs to do another method */ |
| 1048 | VRB(session, "User \"%s\" partially authenticated, but still needs to authenticate via the rest of his configured methods.", username); |
| 1049 | ssh_message_auth_reply_success(msg, 1); |
| 1050 | } else if (!ret && (state->auth_success_count == state->auth_method_count)) { |
| 1051 | /* authenticated */ |
| 1052 | ssh_message_auth_reply_success(msg, 0); |
| 1053 | session->flags |= NC_SESSION_SSH_AUTHENTICATED; |
| 1054 | VRB(session, "User \"%s\" authenticated.", username); |
| 1055 | } |
| 1056 | |
| 1057 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1058 | } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1059 | 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] | 1060 | if (nc_sshcb_channel_open(session, msg)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1061 | ssh_message_reply_default(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1062 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1063 | return 0; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1064 | |
Michal Vasko | 0df6756 | 2016-01-21 15:50:11 +0100 | [diff] [blame] | 1065 | } 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] | 1066 | if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg), |
| 1067 | ssh_message_channel_request_subsystem(msg))) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1068 | ssh_message_reply_default(msg); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1069 | } else { |
| 1070 | ssh_message_channel_request_reply_success(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1071 | } |
| 1072 | return 0; |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | /* we did not process it */ |
| 1077 | return 1; |
| 1078 | } |
| 1079 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1080 | /* ret 1 on success, 0 on timeout, -1 on error */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1081 | static int |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1082 | 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] | 1083 | { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1084 | struct timespec ts_timeout; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1085 | ssh_message msg; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1086 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1087 | if (timeout) { |
| 1088 | nc_timeouttime_get(&ts_timeout, timeout * 1000); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1089 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1090 | while (1) { |
| 1091 | if (!nc_session_is_connected(session)) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1092 | ERR(session, "Communication SSH socket unexpectedly closed."); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1093 | return -1; |
| 1094 | } |
| 1095 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1096 | msg = ssh_message_get(session->ti.libssh.session); |
| 1097 | if (msg) { |
| 1098 | if (nc_session_ssh_msg(session, opts, msg, NULL)) { |
| 1099 | ssh_message_reply_default(msg); |
| 1100 | } |
| 1101 | ssh_message_free(msg); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1102 | } |
| 1103 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1104 | if (session->ti.libssh.channel && session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1105 | return 1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1106 | } |
| 1107 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1108 | usleep(NC_TIMEOUT_STEP); |
roman | ea0edaa | 2023-10-26 12:16:25 +0200 | [diff] [blame] | 1109 | if (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1110 | /* timeout */ |
| 1111 | ERR(session, "Failed to start \"netconf\" SSH subsystem for too long, disconnecting."); |
| 1112 | break; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1113 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1114 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1115 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1116 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1117 | } |
| 1118 | |
Michal Vasko | f3c41e3 | 2022-09-09 11:22:21 +0200 | [diff] [blame] | 1119 | /** |
| 1120 | * @brief Set hostkeys to be used for an SSH bind. |
| 1121 | * |
| 1122 | * @param[in] sbind SSH bind to use. |
| 1123 | * @param[in] hostkeys Array of hostkeys. |
| 1124 | * @param[in] hostkey_count Count of @p hostkeys. |
| 1125 | * @return 0 on success. |
| 1126 | * @return -1 on error. |
| 1127 | */ |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1128 | static int |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1129 | 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] | 1130 | { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1131 | uint16_t i; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1132 | char *privkey_path, *privkey_data; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 1133 | int ret; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1134 | struct nc_asymmetric_key *key = NULL; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1135 | |
| 1136 | for (i = 0; i < hostkey_count; ++i) { |
| 1137 | privkey_path = privkey_data = NULL; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1138 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1139 | /* get the asymmetric key */ |
| 1140 | if (opts->hostkeys[i].store == NC_STORE_LOCAL) { |
| 1141 | /* stored locally */ |
| 1142 | key = &opts->hostkeys[i].key; |
| 1143 | } else { |
| 1144 | /* keystore reference, need to get it */ |
| 1145 | 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] | 1146 | return -1; |
| 1147 | } |
| 1148 | } |
| 1149 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1150 | privkey_path = base64der_privkey_to_tmp_file(key->privkey_data, nc_privkey_format_to_str(key->privkey_type)); |
| 1151 | if (!privkey_path) { |
| 1152 | ERR(NULL, "Temporarily storing a host key into a file failed (%s).", strerror(errno)); |
| 1153 | return -1; |
| 1154 | } |
| 1155 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1156 | ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path); |
| 1157 | |
| 1158 | /* cleanup */ |
| 1159 | if (privkey_data && unlink(privkey_path)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1160 | 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] | 1161 | } |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1162 | |
| 1163 | if (ret != SSH_OK) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1164 | 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] | 1165 | } |
| 1166 | free(privkey_path); |
| 1167 | |
| 1168 | if (ret != SSH_OK) { |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 1169 | return -1; |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | return 0; |
| 1174 | } |
| 1175 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1176 | static int |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1177 | 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] | 1178 | { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1179 | struct timespec ts_timeout; |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1180 | ssh_message msg; |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1181 | struct nc_auth_state state = {0}; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1182 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1183 | /* authenticate */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1184 | if (opts->auth_timeout) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1185 | nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1186 | } |
| 1187 | while (1) { |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1188 | if (!nc_session_is_connected(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1189 | ERR(session, "Communication SSH socket unexpectedly closed."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 1190 | return -1; |
| 1191 | } |
| 1192 | |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1193 | msg = ssh_message_get(session->ti.libssh.session); |
| 1194 | if (msg) { |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1195 | if (nc_session_ssh_msg(session, opts, msg, &state)) { |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 1196 | ssh_message_reply_default(msg); |
| 1197 | } |
| 1198 | ssh_message_free(msg); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1199 | } |
| 1200 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1201 | if (session->flags & NC_SESSION_SSH_AUTHENTICATED) { |
| 1202 | break; |
| 1203 | } |
| 1204 | |
Michal Vasko | 145ae67 | 2017-02-07 10:57:27 +0100 | [diff] [blame] | 1205 | if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1206 | ERR(session, "Too many failed authentication attempts of user \"%s\".", session->username); |
Michal Vasko | 145ae67 | 2017-02-07 10:57:27 +0100 | [diff] [blame] | 1207 | return -1; |
| 1208 | } |
| 1209 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1210 | usleep(NC_TIMEOUT_STEP); |
roman | ea0edaa | 2023-10-26 12:16:25 +0200 | [diff] [blame] | 1211 | if (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 1212 | /* timeout */ |
| 1213 | break; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1214 | } |
| 1215 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1216 | |
| 1217 | if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) { |
| 1218 | /* timeout */ |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1219 | if (session->username) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1220 | 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] | 1221 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1222 | ERR(session, "User failed to authenticate for too long, disconnecting."); |
Michal Vasko | c13da70 | 2017-02-07 10:57:57 +0100 | [diff] [blame] | 1223 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 1224 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1225 | } |
| 1226 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1227 | return 1; |
| 1228 | } |
| 1229 | |
| 1230 | int |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1231 | 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] | 1232 | { |
| 1233 | ssh_bind sbind = NULL; |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1234 | int rc = 1, r; |
| 1235 | struct timespec ts_timeout; |
roman | 4cc0cd5 | 2023-04-14 09:12:29 +0200 | [diff] [blame] | 1236 | const char *err_msg; |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1237 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1238 | /* other transport-specific data */ |
| 1239 | session->ti_type = NC_TI_LIBSSH; |
| 1240 | session->ti.libssh.session = ssh_new(); |
| 1241 | if (!session->ti.libssh.session) { |
| 1242 | ERR(NULL, "Failed to initialize a new SSH session."); |
| 1243 | rc = -1; |
| 1244 | goto cleanup; |
| 1245 | } |
| 1246 | |
| 1247 | sbind = ssh_bind_new(); |
| 1248 | if (!sbind) { |
| 1249 | ERR(session, "Failed to create an SSH bind."); |
| 1250 | rc = -1; |
| 1251 | goto cleanup; |
| 1252 | } |
| 1253 | |
| 1254 | /* configure host keys */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1255 | if (nc_ssh_bind_add_hostkeys(sbind, opts, opts->hostkey_count)) { |
| 1256 | rc = -1; |
| 1257 | goto cleanup; |
| 1258 | } |
| 1259 | |
| 1260 | /* configure supported algorithms */ |
| 1261 | if (opts->hostkey_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, opts->hostkey_algs)) { |
| 1262 | rc = -1; |
| 1263 | goto cleanup; |
| 1264 | } |
| 1265 | if (opts->encryption_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_CIPHERS_S_C, opts->encryption_algs)) { |
| 1266 | rc = -1; |
| 1267 | goto cleanup; |
| 1268 | } |
| 1269 | if (opts->kex_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_KEY_EXCHANGE, opts->kex_algs)) { |
| 1270 | rc = -1; |
| 1271 | goto cleanup; |
| 1272 | } |
| 1273 | 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] | 1274 | rc = -1; |
| 1275 | goto cleanup; |
| 1276 | } |
| 1277 | |
| 1278 | /* accept new connection on the bind */ |
| 1279 | if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) { |
| 1280 | ERR(session, "SSH failed to accept a new connection (%s).", ssh_get_error(sbind)); |
| 1281 | rc = -1; |
| 1282 | goto cleanup; |
| 1283 | } |
| 1284 | sock = -1; |
| 1285 | |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1286 | /* set to non-blocking */ |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1287 | ssh_set_blocking(session->ti.libssh.session, 0); |
| 1288 | |
| 1289 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1290 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1291 | } |
| 1292 | while ((r = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) { |
| 1293 | /* this tends to take longer */ |
| 1294 | usleep(NC_TIMEOUT_STEP * 20); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1295 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1296 | break; |
| 1297 | } |
| 1298 | } |
| 1299 | if (r == SSH_AGAIN) { |
| 1300 | ERR(session, "SSH key exchange timeout."); |
| 1301 | rc = 0; |
| 1302 | goto cleanup; |
| 1303 | } else if (r != SSH_OK) { |
roman | 4cc0cd5 | 2023-04-14 09:12:29 +0200 | [diff] [blame] | 1304 | err_msg = ssh_get_error(session->ti.libssh.session); |
| 1305 | if (err_msg[0] == '\0') { |
| 1306 | err_msg = "hostkey algorithm generated from the hostkey most likely not found in the set of configured hostkey algorithms"; |
| 1307 | } |
| 1308 | ERR(session, "SSH key exchange error (%s).", err_msg); |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1309 | rc = -1; |
| 1310 | goto cleanup; |
| 1311 | } |
| 1312 | |
| 1313 | /* authenticate */ |
| 1314 | if ((rc = nc_accept_ssh_session_auth(session, opts)) != 1) { |
| 1315 | goto cleanup; |
| 1316 | } |
| 1317 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1318 | /* open channel and request 'netconf' subsystem */ |
roman | f578cd5 | 2023-10-19 09:47:40 +0200 | [diff] [blame] | 1319 | 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] | 1320 | goto cleanup; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1321 | } |
| 1322 | |
Michal Vasko | 09d700f | 2022-09-08 10:21:40 +0200 | [diff] [blame] | 1323 | cleanup: |
| 1324 | if (sock > -1) { |
| 1325 | close(sock); |
| 1326 | } |
| 1327 | ssh_bind_free(sbind); |
| 1328 | return rc; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1329 | } |
| 1330 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1331 | API NC_MSG_TYPE |
| 1332 | nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session) |
| 1333 | { |
| 1334 | NC_MSG_TYPE msgtype; |
| 1335 | struct nc_session *new_session = NULL; |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1336 | struct timespec ts_cur; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1337 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1338 | NC_CHECK_ARG_RET(orig_session, orig_session, session, NC_MSG_ERROR); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1339 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1340 | if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) && |
| 1341 | orig_session->ti.libssh.next) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1342 | for (new_session = orig_session->ti.libssh.next; |
| 1343 | new_session != orig_session; |
| 1344 | new_session = new_session->ti.libssh.next) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1345 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel && |
| 1346 | (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1347 | /* we found our session */ |
| 1348 | break; |
| 1349 | } |
| 1350 | } |
| 1351 | if (new_session == orig_session) { |
| 1352 | new_session = NULL; |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | if (!new_session) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1357 | ERR(orig_session, "Session does not have a NETCONF SSH channel ready."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1358 | return NC_MSG_ERROR; |
| 1359 | } |
| 1360 | |
| 1361 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 1362 | new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1363 | |
| 1364 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1365 | msgtype = nc_handshake_io(new_session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1366 | if (msgtype != NC_MSG_HELLO) { |
| 1367 | return msgtype; |
| 1368 | } |
| 1369 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1370 | nc_realtime_get(&ts_cur); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1371 | new_session->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1372 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1373 | new_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1374 | new_session->status = NC_STATUS_RUNNING; |
| 1375 | *session = new_session; |
| 1376 | |
| 1377 | return msgtype; |
| 1378 | } |
| 1379 | |
| 1380 | API NC_MSG_TYPE |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1381 | 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] | 1382 | { |
Michal Vasko | bdcf236 | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1383 | uint8_t q_id; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1384 | NC_MSG_TYPE msgtype; |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1385 | struct nc_session *new_session = NULL, *cur_session; |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1386 | struct timespec ts_cur; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 1387 | uint16_t i; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1388 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1389 | NC_CHECK_ARG_RET(NULL, ps, session, NC_MSG_ERROR); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1390 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1391 | /* LOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1392 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1393 | return NC_MSG_ERROR; |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1394 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1395 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1396 | for (i = 0; i < ps->session_count; ++i) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1397 | cur_session = ps->sessions[i]->session; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1398 | if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH) && |
| 1399 | cur_session->ti.libssh.next) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1400 | /* an SSH session with more channels */ |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1401 | for (new_session = cur_session->ti.libssh.next; |
| 1402 | new_session != cur_session; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1403 | new_session = new_session->ti.libssh.next) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1404 | if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel && |
| 1405 | (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1406 | /* we found our session */ |
| 1407 | break; |
| 1408 | } |
| 1409 | } |
Michal Vasko | e4300a8 | 2017-05-24 10:35:42 +0200 | [diff] [blame] | 1410 | if (new_session != cur_session) { |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1411 | break; |
| 1412 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1413 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1414 | new_session = NULL; |
| 1415 | } |
| 1416 | } |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1417 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1418 | /* UNLOCK */ |
Michal Vasko | 227f8ff | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1419 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1420 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1421 | if (!new_session) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1422 | ERR(NULL, "No session with a NETCONF SSH channel ready was found."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1423 | return NC_MSG_ERROR; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 1427 | new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 1428 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1429 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1430 | msgtype = nc_handshake_io(new_session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1431 | if (msgtype != NC_MSG_HELLO) { |
| 1432 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1433 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1434 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1435 | nc_realtime_get(&ts_cur); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1436 | new_session->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1437 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9f6275e | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1438 | new_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1439 | new_session->status = NC_STATUS_RUNNING; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 1440 | *session = new_session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1441 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1442 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1443 | } |