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