blob: a65e77dc0265d2fa1eb21eb97cbfa0712af94dc4 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
Michal Vasko95ea9ff2021-11-09 12:29:14 +01002 * @file session_server_ssh.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libnetconf2 SSH server session manipulation functions
Michal Vasko086311b2016-01-08 09:53:11 +01005 *
Michal Vasko95ea9ff2021-11-09 12:29:14 +01006 * @copyright
7 * Copyright (c) 2017 - 2021 CESNET, z.s.p.o.
Michal Vasko086311b2016-01-08 09:53:11 +01008 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01009 * 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 Vaskoafd416b2016-02-25 14:51:46 +010012 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010013 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010014 */
15
16#define _GNU_SOURCE
17
romanf578cd52023-10-19 09:47:40 +020018#include "config.h" /* Expose HAVE_SHADOW, HAVE_CRYPT and HAVE_LIBPAM */
apropp-molex4e903c32020-04-20 03:06:58 -040019
20#ifdef HAVE_SHADOW
21 #include <shadow.h>
22#endif
23#ifdef HAVE_CRYPT
24 #include <crypt.h>
25#endif
Michal Vasko0d81c572022-09-26 10:39:31 +020026#ifdef HAVE_LIBPAM
27 #include <security/pam_appl.h>
28#endif
apropp-molex4e903c32020-04-20 03:06:58 -040029
romanf578cd52023-10-19 09:47:40 +020030#include <arpa/inet.h>
31#include <assert.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020032#include <errno.h>
romanf578cd52023-10-19 09:47:40 +020033#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 Vaskob83a3fa2021-05-26 09:53:42 +020039#include <pwd.h>
romanf578cd52023-10-19 09:47:40 +020040#include <stdint.h>
Michal Vasko086311b2016-01-08 09:53:11 +010041#include <stdlib.h>
42#include <string.h>
Michal Vasko27252692017-03-21 15:34:13 +010043#include <sys/stat.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020044#include <sys/types.h>
Michal Vasko9f6275e2017-10-05 13:50:05 +020045#include <time.h>
Claus Klein22091912020-01-20 13:45:47 +010046#include <unistd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010047
Michal Vasko7a20d2e2021-05-19 16:40:23 +020048#include "compat.h"
romanf578cd52023-10-19 09:47:40 +020049#include "log_p.h"
50#include "session.h"
51#include "session_p.h"
Michal Vasko086311b2016-01-08 09:53:11 +010052
Michal Vaskob83a3fa2021-05-26 09:53:42 +020053#if !defined (HAVE_CRYPT_R)
Mislav Novakovicce9a7ef2017-08-08 13:45:52 +020054pthread_mutex_t crypt_lock = PTHREAD_MUTEX_INITIALIZER;
55#endif
56
Michal Vasko086311b2016-01-08 09:53:11 +010057extern struct nc_server_opts server_opts;
Michal Vaskob05053d2016-01-22 16:12:06 +010058
Michal Vasko4c1fb492017-01-30 14:31:07 +010059static char *
romanf578cd52023-10-19 09:47:40 +020060base64der_privkey_to_tmp_file(const char *in, const char *privkey_format)
Michal Vasko086311b2016-01-08 09:53:11 +010061{
Michal Vasko4c1fb492017-01-30 14:31:07 +010062 char path[12] = "/tmp/XXXXXX";
63 int fd, written;
romanf578cd52023-10-19 09:47:40 +020064 unsigned len;
Michal Vasko27252692017-03-21 15:34:13 +010065 mode_t umode;
Michal Vasko4c1fb492017-01-30 14:31:07 +010066 FILE *file;
67
romanf578cd52023-10-19 09:47:40 +020068 NC_CHECK_ARG_RET(NULL, in, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +010069
mekleob31878b2019-09-09 14:10:47 +020070 umode = umask(0177);
Michal Vasko4c1fb492017-01-30 14:31:07 +010071 fd = mkstemp(path);
Michal Vasko27252692017-03-21 15:34:13 +010072 umask(umode);
Michal Vasko4c1fb492017-01-30 14:31:07 +010073 if (fd == -1) {
74 return NULL;
75 }
76
Michal Vasko3964a832018-09-18 14:37:39 +020077 file = fdopen(fd, "w");
Michal Vasko4c1fb492017-01-30 14:31:07 +010078 if (!file) {
79 close(fd);
80 return NULL;
81 }
82
romanf578cd52023-10-19 09:47:40 +020083 /* write header */
84 written = fwrite("-----BEGIN ", 1, 11, file);
85 if (privkey_format) {
86 written += fwrite(privkey_format, 1, strlen(privkey_format), file);
Michal Vasko68177b72020-04-27 15:46:53 +020087 written += fwrite(" PRIVATE KEY-----\n", 1, 18, file);
Michal Vasko68177b72020-04-27 15:46:53 +020088 } else {
romanf578cd52023-10-19 09:47:40 +020089 written += fwrite("PRIVATE KEY-----\n", 1, 17, file);
90 }
Michal Vasko68177b72020-04-27 15:46:53 +020091
romanf578cd52023-10-19 09:47:40 +020092 /* write data */
93 written += fwrite(in, 1, strlen(in), file);
94
95 /* write footer */
96 written += fwrite("\n-----END ", 1, 10, file);
97 if (privkey_format) {
98 written += fwrite(privkey_format, 1, strlen(privkey_format), file);
99 written += fwrite(" PRIVATE KEY-----", 1, 17, file);
100 } else {
101 written += fwrite("PRIVATE KEY-----", 1, 16, file);
102 }
103
104 fclose(file);
105
106 /* checksum */
107 if (privkey_format) {
108 len = 11 + strlen(privkey_format) + 18 + strlen(in) + 10 + strlen(privkey_format) + 17;
109 } else {
110 len = 11 + 17 + strlen(in) + 10 + 16;
111 }
112
113 if ((unsigned)written != len) {
114 unlink(path);
115 return NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100116 }
117
118 return strdup(path);
119}
120
121static int
romanf578cd52023-10-19 09:47:40 +0200122nc_server_ssh_ks_ref_get_key(const char *referenced_name, struct nc_asymmetric_key **askey)
Michal Vasko4c1fb492017-01-30 14:31:07 +0100123{
romanf578cd52023-10-19 09:47:40 +0200124 uint16_t i;
125 struct nc_keystore *ks = &server_opts.keystore;
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100126
romanf578cd52023-10-19 09:47:40 +0200127 *askey = NULL;
Michal Vaskod45e25a2016-01-08 15:48:44 +0100128
romanf578cd52023-10-19 09:47:40 +0200129 /* lookup name */
130 for (i = 0; i < ks->asym_key_count; i++) {
131 if (!strcmp(referenced_name, ks->asym_keys[i].name)) {
132 break;
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100133 }
134 }
135
romanf578cd52023-10-19 09:47:40 +0200136 if (i == ks->asym_key_count) {
137 ERR(NULL, "Keystore entry \"%s\" not found.", referenced_name);
138 return 1;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200139 }
Michal Vasko7d255882017-02-09 13:35:08 +0100140
romanf578cd52023-10-19 09:47:40 +0200141 *askey = &ks->asym_keys[i];
142
143 /* check if the referenced public key is SubjectPublicKeyInfo */
144 if ((*askey)->pubkey_data && nc_is_pk_subject_public_key_info((*askey)->pubkey_data)) {
145 ERR(NULL, "The public key of the referenced hostkey \"%s\" is in the SubjectPublicKeyInfo format, "
146 "which is not allowed in the SSH!", referenced_name);
147 return 1;
Michal Vasko7d255882017-02-09 13:35:08 +0100148 }
Michal Vaskoe2713da2016-08-22 16:06:40 +0200149
Michal Vasko5fcc7142016-02-02 12:21:10 +0100150 return 0;
Michal Vaskob05053d2016-01-22 16:12:06 +0100151}
152
romanf578cd52023-10-19 09:47:40 +0200153static int
154nc_server_ssh_ts_ref_get_keys(const char *referenced_name, struct nc_public_key **pubkeys, uint16_t *pubkey_count)
Michal Vaskob05053d2016-01-22 16:12:06 +0100155{
romanf578cd52023-10-19 09:47:40 +0200156 uint16_t i, j;
157 struct nc_truststore *ts = &server_opts.truststore;
Michal Vasko3031aae2016-01-27 16:07:18 +0100158
romanf578cd52023-10-19 09:47:40 +0200159 *pubkeys = NULL;
160 *pubkey_count = 0;
161
162 /* lookup name */
163 for (i = 0; i < ts->pub_bag_count; i++) {
164 if (!strcmp(referenced_name, ts->pub_bags[i].name)) {
165 break;
166 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100167 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200168
romanf578cd52023-10-19 09:47:40 +0200169 if (i == ts->pub_bag_count) {
170 ERR(NULL, "Truststore entry \"%s\" not found.", referenced_name);
171 return 1;
172 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200173
romanf578cd52023-10-19 09:47:40 +0200174 /* check if any of the referenced public keys is SubjectPublicKeyInfo */
175 for (j = 0; j < ts->pub_bags[i].pubkey_count; j++) {
176 if (nc_is_pk_subject_public_key_info(ts->pub_bags[i].pubkeys[j].data)) {
177 ERR(NULL, "A public key of the referenced public key bag \"%s\" is in the SubjectPublicKeyInfo format, "
178 "which is not allowed in the SSH!", referenced_name);
179 return 1;
180 }
181 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100182
romanf578cd52023-10-19 09:47:40 +0200183 *pubkeys = ts->pub_bags[i].pubkeys;
184 *pubkey_count = ts->pub_bags[i].pubkey_count;
185 return 0;
Michal Vaskob05053d2016-01-22 16:12:06 +0100186}
187
Michal Vasko974410a2018-04-03 09:36:57 +0200188API void
189nc_server_ssh_set_passwd_auth_clb(int (*passwd_auth_clb)(const struct nc_session *session, const char *password, void *user_data),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200190 void *user_data, void (*free_user_data)(void *user_data))
Michal Vasko974410a2018-04-03 09:36:57 +0200191{
192 server_opts.passwd_auth_clb = passwd_auth_clb;
193 server_opts.passwd_auth_data = user_data;
194 server_opts.passwd_auth_data_free = free_user_data;
195}
196
bhart1bb7cdb2018-07-02 15:03:30 -0500197API void
romanf578cd52023-10-19 09:47:40 +0200198nc_server_ssh_set_interactive_auth_clb(int (*interactive_auth_clb)(const struct nc_session *session, ssh_session ssh_sess,
199 ssh_message msg, void *user_data), void *user_data, void (*free_user_data)(void *user_data))
bhart1bb7cdb2018-07-02 15:03:30 -0500200{
201 server_opts.interactive_auth_clb = interactive_auth_clb;
202 server_opts.interactive_auth_data = user_data;
203 server_opts.interactive_auth_data_free = free_user_data;
204}
Michal Vasko733c0bd2018-07-03 13:14:40 +0200205
bhart1bb7cdb2018-07-02 15:03:30 -0500206API void
207nc_server_ssh_set_pubkey_auth_clb(int (*pubkey_auth_clb)(const struct nc_session *session, ssh_key key, void *user_data),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200208 void *user_data, void (*free_user_data)(void *user_data))
bhart1bb7cdb2018-07-02 15:03:30 -0500209{
210 server_opts.pubkey_auth_clb = pubkey_auth_clb;
211 server_opts.pubkey_auth_data = user_data;
212 server_opts.pubkey_auth_data_free = free_user_data;
213}
214
Michal Vaskof3c41e32022-09-09 11:22:21 +0200215/**
216 * @brief Compare hashed password with a cleartext password for a match.
217 *
218 * @param[in] pass_hash Hashed password.
219 * @param[in] pass_clear Cleartext password.
220 * @return 0 on match.
221 * @return non-zero if not a match.
222 */
Michal Vasko086311b2016-01-08 09:53:11 +0100223static int
224auth_password_compare_pwd(const char *pass_hash, const char *pass_clear)
225{
226 char *new_pass_hash;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200227
Michal Vaskof3c41e32022-09-09 11:22:21 +0200228#ifdef HAVE_CRYPT_R
Michal Vasko086311b2016-01-08 09:53:11 +0100229 struct crypt_data cdata;
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200230#endif
Michal Vasko086311b2016-01-08 09:53:11 +0100231
232 if (!pass_hash[0]) {
233 if (!pass_clear[0]) {
Michal Vasko05532772021-06-03 12:12:38 +0200234 WRN(NULL, "User authentication successful with an empty password!");
Michal Vasko086311b2016-01-08 09:53:11 +0100235 return 0;
236 } else {
237 /* the user did now know he does not need any password,
238 * (which should not be used) so deny authentication */
239 return 1;
240 }
241 }
242
Michal Vaskof3c41e32022-09-09 11:22:21 +0200243#ifdef HAVE_CRYPT_R
Michal Vasko086311b2016-01-08 09:53:11 +0100244 cdata.initialized = 0;
245 new_pass_hash = crypt_r(pass_clear, pass_hash, &cdata);
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200246#else
Mislav Novakovicce9a7ef2017-08-08 13:45:52 +0200247 pthread_mutex_lock(&crypt_lock);
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200248 new_pass_hash = crypt(pass_clear, pass_hash);
Mislav Novakovicce9a7ef2017-08-08 13:45:52 +0200249 pthread_mutex_unlock(&crypt_lock);
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200250#endif
Andrew Langefeld158d6fd2018-06-11 18:51:44 -0500251
252 if (!new_pass_hash) {
253 return 1;
254 }
255
Michal Vasko086311b2016-01-08 09:53:11 +0100256 return strcmp(new_pass_hash, pass_hash);
257}
258
romanf578cd52023-10-19 09:47:40 +0200259static int
260nc_sshcb_auth_password(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100261{
Michal Vaskoebba7602018-03-23 13:14:08 +0100262 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100263
Michal Vaskoebba7602018-03-23 13:14:08 +0100264 if (server_opts.passwd_auth_clb) {
265 auth_ret = server_opts.passwd_auth_clb(session, ssh_message_auth_password(msg), server_opts.passwd_auth_data);
266 } else {
romanf578cd52023-10-19 09:47:40 +0200267 auth_ret = auth_password_compare_pwd(auth_client->password, ssh_message_auth_password(msg));
Michal Vasko086311b2016-01-08 09:53:11 +0100268 }
269
romanf578cd52023-10-19 09:47:40 +0200270 if (auth_ret) {
Michal Vaskoebba7602018-03-23 13:14:08 +0100271 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200272 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
273 session->opts.server.ssh_auth_attempts);
Michal Vaskoebba7602018-03-23 13:14:08 +0100274 ssh_message_reply_default(msg);
275 }
romanf578cd52023-10-19 09:47:40 +0200276
277 return auth_ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100278}
279
Michal Vasko0d81c572022-09-26 10:39:31 +0200280#ifdef HAVE_LIBPAM
281
roman41a11e42022-06-22 09:27:08 +0200282/**
283 * @brief PAM conversation function, which serves as a callback for exchanging messages between the client and a PAM module.
284 *
285 * @param[in] n_messages Number of messages.
286 * @param[in] msg PAM module's messages.
287 * @param[out] resp User responses.
288 * @param[in] appdata_ptr Callback's data.
289 * @return PAM_SUCCESS on success;
290 * @return PAM_BUF_ERR on memory allocation error;
291 * @return PAM_CONV_ERR otherwise.
292 */
293static int
294nc_pam_conv_clb(int n_messages, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)
295{
296 int i, j, t, r = PAM_SUCCESS, n_answers, n_requests = n_messages;
297 const char **prompts = NULL;
298 char *echo = NULL;
299 const char *name = "Keyboard-Interactive Authentication";
300 const char *instruction = "Please enter your authentication token";
301 ssh_message reply = NULL;
302 struct nc_pam_thread_arg *clb_data = appdata_ptr;
303 ssh_session libssh_session;
304 struct timespec ts_timeout;
305 struct nc_server_ssh_opts *opts;
306
307 libssh_session = clb_data->session->ti.libssh.session;
romanf578cd52023-10-19 09:47:40 +0200308 opts = clb_data->opts;
roman41a11e42022-06-22 09:27:08 +0200309
310 /* PAM_MAX_NUM_MSG == 32 by default */
311 if ((n_messages <= 0) || (n_messages >= PAM_MAX_NUM_MSG)) {
312 ERR(NULL, "Bad number of PAM messages (#%d).", n_messages);
313 r = PAM_CONV_ERR;
314 goto cleanup;
315 }
316
317 /* only accepting these 4 types of messages */
318 for (i = 0; i < n_messages; i++) {
319 t = msg[i]->msg_style;
320 if ((t != PAM_PROMPT_ECHO_OFF) && (t != PAM_PROMPT_ECHO_ON) && (t != PAM_TEXT_INFO) && (t != PAM_ERROR_MSG)) {
321 ERR(NULL, "PAM conversation callback received an unexpected type of message.");
322 r = PAM_CONV_ERR;
323 goto cleanup;
324 }
325 }
326
327 /* display messages with errors and/or some information and count the amount of actual authentication challenges */
328 for (i = 0; i < n_messages; i++) {
329 if (msg[i]->msg_style == PAM_TEXT_INFO) {
330 VRB(NULL, "PAM conversation callback received a message with some information for the client (%s).", msg[i]->msg);
331 n_requests--;
332 }
333 if (msg[i]->msg_style == PAM_ERROR_MSG) {
334 ERR(NULL, "PAM conversation callback received an error message (%s).", msg[i]->msg);
335 r = PAM_CONV_ERR;
336 goto cleanup;
337 }
338 }
339
340 /* there are no requests left for the user, only messages with some information for the client were sent */
341 if (n_requests <= 0) {
342 r = PAM_SUCCESS;
343 goto cleanup;
344 }
345
346 /* it is the PAM module's responsibility to release both, this array and the responses themselves */
347 *resp = calloc(n_requests, sizeof **resp);
348 prompts = calloc(n_requests, sizeof *prompts);
349 echo = calloc(n_requests, sizeof *echo);
350 if (!(*resp) || !prompts || !echo) {
351 ERRMEM;
352 r = PAM_BUF_ERR;
353 goto cleanup;
354 }
355
356 /* set the prompts for the user */
357 j = 0;
358 for (i = 0; i < n_messages; i++) {
359 if ((msg[i]->msg_style == PAM_PROMPT_ECHO_ON) || (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF)) {
360 prompts[j++] = msg[i]->msg;
361 }
362 }
363
364 /* iterate over all the messages and adjust the echo array accordingly */
365 j = 0;
366 for (i = 0; i < n_messages; i++) {
367 if (msg[i]->msg_style == PAM_PROMPT_ECHO_ON) {
368 echo[j++] = 1;
369 }
370 if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF) {
371 /* no need to set to 0 because of calloc */
372 j++;
373 }
374 }
375
376 /* print all the keyboard-interactive challenges to the user */
377 r = ssh_message_auth_interactive_request(clb_data->msg, name, instruction, n_requests, prompts, echo);
378 if (r != SSH_OK) {
379 ERR(NULL, "Failed to send an authentication request.");
380 r = PAM_CONV_ERR;
381 goto cleanup;
382 }
383
384 if (opts->auth_timeout) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100385 nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000);
roman41a11e42022-06-22 09:27:08 +0200386 }
387
388 /* get user's replies */
389 do {
390 if (!nc_session_is_connected(clb_data->session)) {
391 ERR(NULL, "Communication SSH socket unexpectedly closed.");
392 r = PAM_CONV_ERR;
393 goto cleanup;
394 }
395
396 reply = ssh_message_get(libssh_session);
397 if (reply) {
398 break;
399 }
400
401 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +0100402 } while ((opts->auth_timeout) && (nc_timeouttime_cur_diff(&ts_timeout) >= 1));
roman41a11e42022-06-22 09:27:08 +0200403
404 if (!reply) {
405 ERR(NULL, "Authentication timeout.");
406 r = PAM_CONV_ERR;
407 goto cleanup;
408 }
409
410 /* check if the amount of replies matches the amount of requests */
411 n_answers = ssh_userauth_kbdint_getnanswers(libssh_session);
412 if (n_answers != n_requests) {
413 ERR(NULL, "Expected %d response(s), got %d.", n_requests, n_answers);
414 r = PAM_CONV_ERR;
415 goto cleanup;
416 }
417
418 /* give the replies to a PAM module */
419 for (i = 0; i < n_answers; i++) {
420 (*resp)[i].resp = strdup(ssh_userauth_kbdint_getanswer(libssh_session, i));
421 /* it should be the caller's responsibility to free this, however if mem alloc fails,
422 * it is safer to free the responses here and set them to NULL */
423 if ((*resp)[i].resp == NULL) {
424 for (j = 0; j < i; j++) {
425 free((*resp)[j].resp);
426 (*resp)[j].resp = NULL;
427 }
428 ERRMEM;
429 r = PAM_BUF_ERR;
430 goto cleanup;
431 }
432 }
433
434cleanup:
435 ssh_message_free(reply);
436 free(prompts);
437 free(echo);
438 return r;
439}
440
441/**
442 * @brief Handles authentication via Linux PAM.
443 *
444 * @param[in] session NETCONF session.
445 * @param[in] ssh_msg SSH message with a keyboard-interactive authentication request.
446 * @return PAM_SUCCESS on success;
447 * @return PAM error otherwise.
448 */
449static int
romanf578cd52023-10-19 09:47:40 +0200450nc_pam_auth(struct nc_session *session, struct nc_server_ssh_opts *opts, ssh_message ssh_msg)
roman41a11e42022-06-22 09:27:08 +0200451{
452 pam_handle_t *pam_h = NULL;
453 int ret;
454 struct nc_pam_thread_arg clb_data;
455 struct pam_conv conv;
romanf578cd52023-10-19 09:47:40 +0200456 uint16_t i;
roman41a11e42022-06-22 09:27:08 +0200457
458 /* structure holding callback's data */
459 clb_data.msg = ssh_msg;
460 clb_data.session = session;
romanf578cd52023-10-19 09:47:40 +0200461 clb_data.opts = opts;
roman41a11e42022-06-22 09:27:08 +0200462
463 /* PAM conversation structure holding the callback and it's data */
464 conv.conv = nc_pam_conv_clb;
465 conv.appdata_ptr = &clb_data;
466
romanf578cd52023-10-19 09:47:40 +0200467 /* get the current client's configuration file */
468 for (i = 0; i < opts->client_count; i++) {
469 if (!strcmp(opts->auth_clients[i].username, session->username)) {
470 break;
471 }
472 }
473
474 if (i == opts->client_count) {
475 ERR(NULL, "User \"%s\" not found.", session->username);
476 ret = 1;
477 goto cleanup;
478 }
479
480 if (!opts->auth_clients[i].pam_config_name) {
481 ERR(NULL, "User's \"%s\" PAM configuration filename not set.");
482 ret = 1;
483 goto cleanup;
484 }
485
roman41a11e42022-06-22 09:27:08 +0200486 /* initialize PAM and see if the given configuration file exists */
Michal Vasko0d81c572022-09-26 10:39:31 +0200487# ifdef LIBPAM_HAVE_CONFDIR
roman41a11e42022-06-22 09:27:08 +0200488 /* PAM version >= 1.4 */
romanf578cd52023-10-19 09:47:40 +0200489 ret = pam_start_confdir(opts->auth_clients[i].pam_config_name, session->username, &conv, opts->auth_clients[i].pam_config_dir, &pam_h);
Michal Vasko0d81c572022-09-26 10:39:31 +0200490# else
roman41a11e42022-06-22 09:27:08 +0200491 /* PAM version < 1.4 */
romanf578cd52023-10-19 09:47:40 +0200492 ret = pam_start(opts->auth_clients[i].pam_config_name, session->username, &conv, &pam_h);
Michal Vasko0d81c572022-09-26 10:39:31 +0200493# endif
roman41a11e42022-06-22 09:27:08 +0200494 if (ret != PAM_SUCCESS) {
495 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
496 goto cleanup;
497 }
498
499 /* authentication based on the modules listed in the configuration file */
500 ret = pam_authenticate(pam_h, 0);
501 if (ret != PAM_SUCCESS) {
502 if (ret == PAM_ABORT) {
503 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
504 goto cleanup;
505 } else {
506 VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
507 goto cleanup;
508 }
509 }
510
511 /* correct token entered, check other requirements(the time of the day, expired token, ...) */
512 ret = pam_acct_mgmt(pam_h, 0);
513 if ((ret != PAM_SUCCESS) && (ret != PAM_NEW_AUTHTOK_REQD)) {
514 VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
515 goto cleanup;
516 }
517
518 /* if a token has expired a new one will be generated */
519 if (ret == PAM_NEW_AUTHTOK_REQD) {
520 VRB(NULL, "PAM warning occurred (%s).\n", pam_strerror(pam_h, ret));
521 ret = pam_chauthtok(pam_h, PAM_CHANGE_EXPIRED_AUTHTOK);
522 if (ret == PAM_SUCCESS) {
523 VRB(NULL, "The authentication token of user \"%s\" updated successfully.", session->username);
524 } else {
525 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
526 goto cleanup;
527 }
528 }
529
530cleanup:
531 /* destroy the PAM context */
532 if (pam_end(pam_h, ret) != PAM_SUCCESS) {
533 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
534 }
535 return ret;
536}
537
Michal Vasko0d81c572022-09-26 10:39:31 +0200538#endif
539
romanf578cd52023-10-19 09:47:40 +0200540static int
541nc_sshcb_auth_kbdint(struct nc_session *session, struct nc_server_ssh_opts *opts, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100542{
bhart3bc2f582018-06-05 12:40:32 -0500543 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100544
romanf578cd52023-10-19 09:47:40 +0200545 if (server_opts.interactive_auth_clb) {
546 auth_ret = server_opts.interactive_auth_clb(session, session->ti.libssh.session, msg, server_opts.interactive_auth_data);
Michal Vasko0d81c572022-09-26 10:39:31 +0200547 } else {
548#ifdef HAVE_LIBPAM
romanf578cd52023-10-19 09:47:40 +0200549 if (nc_pam_auth(session, opts, msg) == PAM_SUCCESS) {
Michal Vasko0d81c572022-09-26 10:39:31 +0200550 auth_ret = 0;
551 }
552#else
553 ERR(session, "PAM-based SSH authentication is not supported.");
554#endif
bhart1bb7cdb2018-07-02 15:03:30 -0500555 }
556
557 /* Authenticate message based on outcome */
romanf578cd52023-10-19 09:47:40 +0200558 if (auth_ret) {
bhart1bb7cdb2018-07-02 15:03:30 -0500559 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200560 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
561 session->opts.server.ssh_auth_attempts);
bhart1bb7cdb2018-07-02 15:03:30 -0500562 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +0100563 }
romanf578cd52023-10-19 09:47:40 +0200564
565 return auth_ret;
566}
567
568/*
569 * Get the public key type from binary data stored in buffer.
570 * The data is in the form of: 4 bytes = data length, then data of data length
571 * and the data is in network byte order. The key has to be in the SSH2 format.
572 */
573static const char *
574nc_server_ssh_get_pubkey_type(const char *buffer, uint32_t *len)
575{
576 uint32_t type_len;
577
578 /* copy the 4 bytes */
579 memcpy(&type_len, buffer, sizeof type_len);
580 /* type_len now stores the length of the key type */
581 type_len = ntohl(type_len);
582 *len = type_len;
583
584 /* move 4 bytes in the buffer, this is where the type should be */
585 buffer += sizeof type_len;
586 return buffer;
587}
588
589/**
590 * @brief Create ssh key from base64 pubkey data.
591 *
592 * @param[in] base64 base64 encoded public key.
593 * @param[out] key created ssh key.
594 * @return 0 on success, 1 otherwise.
595 */
596static int
597nc_server_ssh_create_ssh_pubkey(const char *base64, ssh_key *key)
598{
599 int ret = 0;
600 char *bin = NULL;
601 const char *pub_type = NULL;
602 uint32_t pub_type_len = 0;
603
604 if (!key && !base64) {
605 ERRINT;
606 ret = 1;
607 goto cleanup;
608 }
609
610 *key = NULL;
611
612 /* convert base64 to binary */
613 if (nc_base64_to_bin(base64, &bin) == -1) {
614 ERR(NULL, "Unable to decode base64.");
615 ret = 1;
616 goto cleanup;
617 }
618
619 /* get the key type and try to import it if possible */
620 pub_type = nc_server_ssh_get_pubkey_type(bin, &pub_type_len);
621 if (!pub_type) {
622 ret = 1;
623 goto cleanup;
624 } else if (!strncmp(pub_type, "ssh-dss", pub_type_len)) {
625 ERR(NULL, "DSA keys are not supported.");
626 ret = 1;
627 goto cleanup;
628 } else if (!strncmp(pub_type, "ssh-rsa", pub_type_len)) {
629 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_RSA, key);
630 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp256", pub_type_len)) {
631 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P256, key);
632 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp384", pub_type_len)) {
633 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P384, key);
634 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp521", pub_type_len)) {
635 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P521, key);
636 } else if (!strncmp(pub_type, "ssh-ed25519", pub_type_len)) {
637 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ED25519, key);
638 } else {
639 ERR(NULL, "Public key type not recognised.");
640 ret = 1;
641 goto cleanup;
642 }
643
644cleanup:
645 if (ret != SSH_OK) {
646 ERR(NULL, "Error importing public key.");
647 }
648 free(bin);
649 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100650}
651
Michal Vaskof3c41e32022-09-09 11:22:21 +0200652/**
653 * @brief Compare SSH key with configured authorized keys and return the username of the matching one, if any.
654 *
655 * @param[in] key Presented SSH key to compare.
656 * @return Authorized key username, NULL if no match was found.
657 */
romanf578cd52023-10-19 09:47:40 +0200658static int
659auth_pubkey_compare_key(ssh_key key, struct nc_auth_client *auth_client)
Michal Vasko086311b2016-01-08 09:53:11 +0100660{
romanf578cd52023-10-19 09:47:40 +0200661 uint16_t i, pubkey_count;
Michal Vasko3e9d1682017-02-24 09:50:15 +0100662 int ret = 0;
romanf578cd52023-10-19 09:47:40 +0200663 ssh_key new_key = NULL;
664 struct nc_public_key *pubkeys;
Michal Vasko086311b2016-01-08 09:53:11 +0100665
romanf578cd52023-10-19 09:47:40 +0200666 /* get the correct public key storage */
667 if (auth_client->store == NC_STORE_LOCAL) {
668 pubkeys = auth_client->pubkeys;
669 pubkey_count = auth_client->pubkey_count;
670 } else {
671 ret = nc_server_ssh_ts_ref_get_keys(auth_client->ts_ref, &pubkeys, &pubkey_count);
672 if (ret) {
673 ERR(NULL, "Error getting \"%s\"'s public keys from the truststore.", auth_client->username);
674 return ret;
Michal Vasko17dfda92016-12-01 14:06:16 +0100675 }
romanf578cd52023-10-19 09:47:40 +0200676 }
Michal Vasko17dfda92016-12-01 14:06:16 +0100677
romanf578cd52023-10-19 09:47:40 +0200678 /* try to compare all of the client's keys with the key received in the SSH message */
679 for (i = 0; i < pubkey_count; i++) {
680 /* create the SSH key from the data */
681 if (nc_server_ssh_create_ssh_pubkey(pubkeys[i].data, &new_key)) {
682 ssh_key_free(new_key);
Michal Vasko086311b2016-01-08 09:53:11 +0100683 continue;
684 }
685
romanf578cd52023-10-19 09:47:40 +0200686 /* compare the keys */
687 ret = ssh_key_cmp(key, new_key, SSH_KEY_CMP_PUBLIC);
688 if (!ret) {
Michal Vasko086311b2016-01-08 09:53:11 +0100689 break;
romanf578cd52023-10-19 09:47:40 +0200690 } else {
691 WRN(NULL, "User's \"%s\" public key doesn't match, trying another.", auth_client->username);
692 ssh_key_free(new_key);
Michal Vasko086311b2016-01-08 09:53:11 +0100693 }
Michal Vasko086311b2016-01-08 09:53:11 +0100694 }
695
romanf578cd52023-10-19 09:47:40 +0200696 if (i == pubkey_count) {
697 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100698 }
699
romanf578cd52023-10-19 09:47:40 +0200700 if (!ret) {
701 /* only free a key if everything was ok, it would have already been freed otherwise */
702 ssh_key_free(new_key);
703 }
Michal Vaskoa05c7b12017-01-30 14:33:08 +0100704
romanf578cd52023-10-19 09:47:40 +0200705 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100706}
707
708static void
romanf578cd52023-10-19 09:47:40 +0200709nc_sshcb_auth_none(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100710{
romanf578cd52023-10-19 09:47:40 +0200711 if (auth_client->supports_none && !auth_client->password && !auth_client->pubkey_count && !auth_client->pam_config_name) {
712 /* only authenticate the client if he supports none and no other method */
713 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
714 VRB(session, "User \"%s\" authenticated.", session->username);
715 ssh_message_auth_reply_success(msg, 0);
716 }
717
718 ssh_message_reply_default(msg);
719}
720
721static int
722nc_sshcb_auth_pubkey(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
723{
724 int signature_state, ret = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100725
Michal Vasko733c0bd2018-07-03 13:14:40 +0200726 if (server_opts.pubkey_auth_clb) {
727 if (server_opts.pubkey_auth_clb(session, ssh_message_auth_pubkey(msg), server_opts.pubkey_auth_data)) {
romanf578cd52023-10-19 09:47:40 +0200728 ret = 1;
bhart3bc2f582018-06-05 12:40:32 -0500729 goto fail;
730 }
Michal Vasko733c0bd2018-07-03 13:14:40 +0200731 } else {
romanf578cd52023-10-19 09:47:40 +0200732 if (auth_pubkey_compare_key(ssh_message_auth_pubkey(msg), auth_client)) {
Michal Vasko05532772021-06-03 12:12:38 +0200733 VRB(session, "User \"%s\" tried to use an unknown (unauthorized) public key.", session->username);
romanf578cd52023-10-19 09:47:40 +0200734 ret = 1;
bhart3bc2f582018-06-05 12:40:32 -0500735 goto fail;
736 }
Michal Vaskobd13a932016-09-14 09:00:35 +0200737 }
Michal Vaskobd13a932016-09-14 09:00:35 +0200738
Michal Vasko086311b2016-01-08 09:53:11 +0100739 signature_state = ssh_message_auth_publickey_state(msg);
romanf578cd52023-10-19 09:47:40 +0200740 if (signature_state == SSH_PUBLICKEY_STATE_NONE) {
Michal Vaskobd13a932016-09-14 09:00:35 +0200741 /* accepting only the use of a public key */
742 ssh_message_auth_reply_pk_ok_simple(msg);
romanf578cd52023-10-19 09:47:40 +0200743 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100744 }
745
romanf578cd52023-10-19 09:47:40 +0200746 return ret;
Michal Vaskobd13a932016-09-14 09:00:35 +0200747
748fail:
Michal Vasko2e6defd2016-10-07 15:48:15 +0200749 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200750 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
751 session->opts.server.ssh_auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +0100752 ssh_message_reply_default(msg);
romanf578cd52023-10-19 09:47:40 +0200753
754 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100755}
756
757static int
Michal Vasko96164bf2016-01-21 15:41:58 +0100758nc_sshcb_channel_open(struct nc_session *session, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100759{
Michal Vasko96164bf2016-01-21 15:41:58 +0100760 ssh_channel chan;
761
762 /* first channel request */
763 if (!session->ti.libssh.channel) {
764 if (session->status != NC_STATUS_STARTING) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100765 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100766 return -1;
767 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100768 chan = ssh_message_channel_request_open_reply_accept(msg);
769 if (!chan) {
Michal Vasko05532772021-06-03 12:12:38 +0200770 ERR(session, "Failed to create a new SSH channel.");
Michal Vasko96164bf2016-01-21 15:41:58 +0100771 return -1;
772 }
773 session->ti.libssh.channel = chan;
Michal Vasko086311b2016-01-08 09:53:11 +0100774
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200775 /* additional channel request */
Michal Vasko96164bf2016-01-21 15:41:58 +0100776 } else {
777 chan = ssh_message_channel_request_open_reply_accept(msg);
778 if (!chan) {
Michal Vasko05532772021-06-03 12:12:38 +0200779 ERR(session, "Session %u: failed to create a new SSH channel.", session->id);
Michal Vasko96164bf2016-01-21 15:41:58 +0100780 return -1;
781 }
782 /* channel was created and libssh stored it internally in the ssh_session structure, good enough */
Michal Vasko086311b2016-01-08 09:53:11 +0100783 }
784
Michal Vasko086311b2016-01-08 09:53:11 +0100785 return 0;
786}
787
788static int
789nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem)
790{
Michal Vasko96164bf2016-01-21 15:41:58 +0100791 struct nc_session *new_session;
Michal Vasko086311b2016-01-08 09:53:11 +0100792
Michal Vasko96164bf2016-01-21 15:41:58 +0100793 if (strcmp(subsystem, "netconf")) {
Michal Vasko05532772021-06-03 12:12:38 +0200794 WRN(session, "Received an unknown subsystem \"%s\" request.", subsystem);
Michal Vasko086311b2016-01-08 09:53:11 +0100795 return -1;
796 }
797
Michal Vasko96164bf2016-01-21 15:41:58 +0100798 if (session->ti.libssh.channel == channel) {
799 /* first channel requested */
800 if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) {
801 ERRINT;
802 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100803 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100804 if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) {
Michal Vasko05532772021-06-03 12:12:38 +0200805 ERR(session, "Subsystem \"netconf\" requested for the second time.");
Michal Vasko96164bf2016-01-21 15:41:58 +0100806 return -1;
807 }
808
809 session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF;
Michal Vasko086311b2016-01-08 09:53:11 +0100810 } else {
Michal Vasko96164bf2016-01-21 15:41:58 +0100811 /* additional channel subsystem request, new session is ready as far as SSH is concerned */
Michal Vasko131120a2018-05-29 15:44:02 +0200812 new_session = nc_new_session(NC_SERVER, 1);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100813 if (!new_session) {
814 ERRMEM;
815 return -1;
816 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100817
818 /* insert the new session */
819 if (!session->ti.libssh.next) {
820 new_session->ti.libssh.next = session;
821 } else {
822 new_session->ti.libssh.next = session->ti.libssh.next;
823 }
824 session->ti.libssh.next = new_session;
825
826 new_session->status = NC_STATUS_STARTING;
Michal Vasko96164bf2016-01-21 15:41:58 +0100827 new_session->ti_type = NC_TI_LIBSSH;
Michal Vasko131120a2018-05-29 15:44:02 +0200828 new_session->io_lock = session->io_lock;
Michal Vasko96164bf2016-01-21 15:41:58 +0100829 new_session->ti.libssh.channel = channel;
830 new_session->ti.libssh.session = session->ti.libssh.session;
Michal Vasko93224072021-11-09 12:14:28 +0100831 new_session->username = strdup(session->username);
832 new_session->host = strdup(session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100833 new_session->port = session->port;
Michal Vasko93224072021-11-09 12:14:28 +0100834 new_session->ctx = (struct ly_ctx *)session->ctx;
Michal Vasko83d15322018-09-27 09:44:02 +0200835 new_session->flags = NC_SESSION_SSH_AUTHENTICATED | NC_SESSION_SSH_SUBSYS_NETCONF | NC_SESSION_SHAREDCTX;
Michal Vasko086311b2016-01-08 09:53:11 +0100836 }
837
838 return 0;
839}
840
Michal Vasko96164bf2016-01-21 15:41:58 +0100841int
romanf578cd52023-10-19 09:47:40 +0200842nc_session_ssh_msg(struct nc_session *session, struct nc_server_ssh_opts *opts, ssh_message msg, struct nc_auth_state *state)
Michal Vasko086311b2016-01-08 09:53:11 +0100843{
844 const char *str_type, *str_subtype = NULL, *username;
romanf578cd52023-10-19 09:47:40 +0200845 int subtype, type, libssh_auth_methods = 0, ret = 0;
846 uint16_t i;
847 struct nc_auth_client *auth_client = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100848
849 type = ssh_message_type(msg);
850 subtype = ssh_message_subtype(msg);
851
852 switch (type) {
853 case SSH_REQUEST_AUTH:
854 str_type = "request-auth";
855 switch (subtype) {
856 case SSH_AUTH_METHOD_NONE:
857 str_subtype = "none";
858 break;
859 case SSH_AUTH_METHOD_PASSWORD:
860 str_subtype = "password";
861 break;
862 case SSH_AUTH_METHOD_PUBLICKEY:
863 str_subtype = "publickey";
864 break;
865 case SSH_AUTH_METHOD_HOSTBASED:
866 str_subtype = "hostbased";
867 break;
868 case SSH_AUTH_METHOD_INTERACTIVE:
869 str_subtype = "interactive";
870 break;
871 case SSH_AUTH_METHOD_GSSAPI_MIC:
872 str_subtype = "gssapi-mic";
873 break;
874 }
875 break;
876
877 case SSH_REQUEST_CHANNEL_OPEN:
878 str_type = "request-channel-open";
879 switch (subtype) {
880 case SSH_CHANNEL_SESSION:
881 str_subtype = "session";
882 break;
883 case SSH_CHANNEL_DIRECT_TCPIP:
884 str_subtype = "direct-tcpip";
885 break;
886 case SSH_CHANNEL_FORWARDED_TCPIP:
887 str_subtype = "forwarded-tcpip";
888 break;
889 case (int)SSH_CHANNEL_X11:
890 str_subtype = "channel-x11";
891 break;
892 case SSH_CHANNEL_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200893 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100894 default:
895 str_subtype = "unknown";
896 break;
897 }
898 break;
899
900 case SSH_REQUEST_CHANNEL:
901 str_type = "request-channel";
902 switch (subtype) {
903 case SSH_CHANNEL_REQUEST_PTY:
904 str_subtype = "pty";
905 break;
906 case SSH_CHANNEL_REQUEST_EXEC:
907 str_subtype = "exec";
908 break;
909 case SSH_CHANNEL_REQUEST_SHELL:
910 str_subtype = "shell";
911 break;
912 case SSH_CHANNEL_REQUEST_ENV:
913 str_subtype = "env";
914 break;
915 case SSH_CHANNEL_REQUEST_SUBSYSTEM:
916 str_subtype = "subsystem";
917 break;
918 case SSH_CHANNEL_REQUEST_WINDOW_CHANGE:
919 str_subtype = "window-change";
920 break;
921 case SSH_CHANNEL_REQUEST_X11:
922 str_subtype = "x11";
923 break;
924 case SSH_CHANNEL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200925 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100926 default:
927 str_subtype = "unknown";
928 break;
929 }
930 break;
931
932 case SSH_REQUEST_SERVICE:
933 str_type = "request-service";
934 str_subtype = ssh_message_service_service(msg);
935 break;
936
937 case SSH_REQUEST_GLOBAL:
938 str_type = "request-global";
939 switch (subtype) {
940 case SSH_GLOBAL_REQUEST_TCPIP_FORWARD:
941 str_subtype = "tcpip-forward";
942 break;
943 case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD:
944 str_subtype = "cancel-tcpip-forward";
945 break;
946 case SSH_GLOBAL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200947 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100948 default:
949 str_subtype = "unknown";
950 break;
951 }
952 break;
953
954 default:
955 str_type = "unknown";
956 str_subtype = "unknown";
957 break;
958 }
959
Michal Vasko05532772021-06-03 12:12:38 +0200960 VRB(session, "Received an SSH message \"%s\" of subtype \"%s\".", str_type, str_subtype);
Michal Vasko5e0edd82020-07-29 15:26:13 +0200961 if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) {
Michal Vaskoce319162016-02-03 15:33:08 +0100962 /* "valid" situation if, for example, receiving some auth or channel request timeouted,
963 * but we got it now, during session free */
Michal Vasko05532772021-06-03 12:12:38 +0200964 VRB(session, "SSH message arrived on a %s session, the request will be denied.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200965 (session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid"));
Michal Vaskoce319162016-02-03 15:33:08 +0100966 ssh_message_reply_default(msg);
967 return 0;
968 }
Michal Vasko086311b2016-01-08 09:53:11 +0100969
970 /*
971 * process known messages
972 */
973 if (type == SSH_REQUEST_AUTH) {
974 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko05532772021-06-03 12:12:38 +0200975 ERR(session, "User \"%s\" authenticated, but requested another authentication.", session->username);
Michal Vasko086311b2016-01-08 09:53:11 +0100976 ssh_message_reply_default(msg);
977 return 0;
romanf578cd52023-10-19 09:47:40 +0200978 } else if (!state || !opts) {
979 /* these two parameters should always be set during an authentication,
980 * however do a check just in case something goes really wrong, since they
981 * are not needed for other types of messages
982 */
983 ERRINT;
984 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100985 }
986
Michal Vasko086311b2016-01-08 09:53:11 +0100987 /* save the username, do not let the client change it */
988 username = ssh_message_auth_user(msg);
romanf578cd52023-10-19 09:47:40 +0200989 assert(username);
990
991 for (i = 0; i < opts->client_count; i++) {
992 if (!strcmp(opts->auth_clients[i].username, username)) {
993 auth_client = &opts->auth_clients[i];
994 break;
995 }
996 }
997
998 if (!auth_client) {
999 if (opts->endpt_client_ref) {
1000 return nc_session_ssh_msg(session, opts->endpt_client_ref->opts.ssh, msg, state);
Michal Vasko086311b2016-01-08 09:53:11 +01001001 }
1002
romanf578cd52023-10-19 09:47:40 +02001003 ERR(NULL, "User \"%s\" not known by the server.", username);
1004 ssh_message_reply_default(msg);
1005 return 0;
1006 }
1007
1008 if (!session->username) {
Michal Vasko93224072021-11-09 12:14:28 +01001009 session->username = strdup(username);
romanf578cd52023-10-19 09:47:40 +02001010
1011 /* configure and count accepted auth methods */
1012 if (auth_client->store == NC_STORE_LOCAL) {
1013 if (auth_client->pubkey_count) {
1014 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
1015 }
1016 } else if (auth_client->ts_ref) {
1017 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
1018 }
1019 if (auth_client->password) {
1020 state->auth_method_count++;
1021 libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD;
1022 }
1023 if (auth_client->pam_config_name) {
1024 state->auth_method_count++;
1025 libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE;
1026 }
1027 if (auth_client->supports_none) {
1028 libssh_auth_methods |= SSH_AUTH_METHOD_NONE;
1029 }
1030
1031 if (libssh_auth_methods & SSH_AUTH_METHOD_PUBLICKEY) {
1032 state->auth_method_count++;
1033 }
1034
1035 ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods);
1036 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001037 if (strcmp(username, session->username)) {
romanf578cd52023-10-19 09:47:40 +02001038 /* changing username not allowed */
Michal Vasko05532772021-06-03 12:12:38 +02001039 ERR(session, "User \"%s\" changed its username to \"%s\".", session->username, username);
Michal Vasko086311b2016-01-08 09:53:11 +01001040 session->status = NC_STATUS_INVALID;
Michal Vasko428087d2016-01-14 16:04:28 +01001041 session->term_reason = NC_SESSION_TERM_OTHER;
Michal Vasko086311b2016-01-08 09:53:11 +01001042 return 1;
1043 }
1044 }
1045
romanf578cd52023-10-19 09:47:40 +02001046 /* try authenticating, the user must authenticate via all of his configured auth methods */
Michal Vasko086311b2016-01-08 09:53:11 +01001047 if (subtype == SSH_AUTH_METHOD_NONE) {
romanf578cd52023-10-19 09:47:40 +02001048 nc_sshcb_auth_none(session, auth_client, msg);
1049 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001050 } else if (subtype == SSH_AUTH_METHOD_PASSWORD) {
romanf578cd52023-10-19 09:47:40 +02001051 ret = nc_sshcb_auth_password(session, auth_client, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001052 } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) {
romanf578cd52023-10-19 09:47:40 +02001053 ret = nc_sshcb_auth_pubkey(session, auth_client, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001054 } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) {
romanf578cd52023-10-19 09:47:40 +02001055 ret = nc_sshcb_auth_kbdint(session, opts, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001056 }
romanf578cd52023-10-19 09:47:40 +02001057
1058 if (!ret) {
1059 state->auth_success_count++;
1060 }
1061
1062 if (!ret && (state->auth_success_count < state->auth_method_count)) {
1063 /* success, but he needs to do another method */
1064 VRB(session, "User \"%s\" partially authenticated, but still needs to authenticate via the rest of his configured methods.", username);
1065 ssh_message_auth_reply_success(msg, 1);
1066 } else if (!ret && (state->auth_success_count == state->auth_method_count)) {
1067 /* authenticated */
1068 ssh_message_auth_reply_success(msg, 0);
1069 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
1070 VRB(session, "User \"%s\" authenticated.", username);
1071 }
1072
1073 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001074 } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko0df67562016-01-21 15:50:11 +01001075 if ((type == SSH_REQUEST_CHANNEL_OPEN) && ((enum ssh_channel_type_e)subtype == SSH_CHANNEL_SESSION)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001076 if (nc_sshcb_channel_open(session, msg)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001077 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001078 }
Michal Vasko086311b2016-01-08 09:53:11 +01001079 return 0;
Michal Vasko96164bf2016-01-21 15:41:58 +01001080
Michal Vasko0df67562016-01-21 15:50:11 +01001081 } else if ((type == SSH_REQUEST_CHANNEL) && ((enum ssh_channel_requests_e)subtype == SSH_CHANNEL_REQUEST_SUBSYSTEM)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001082 if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg),
1083 ssh_message_channel_request_subsystem(msg))) {
Michal Vasko086311b2016-01-08 09:53:11 +01001084 ssh_message_reply_default(msg);
Michal Vasko96164bf2016-01-21 15:41:58 +01001085 } else {
1086 ssh_message_channel_request_reply_success(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001087 }
1088 return 0;
1089 }
1090 }
1091
1092 /* we did not process it */
1093 return 1;
1094}
1095
Michal Vasko1a38c862016-01-15 15:50:07 +01001096/* ret 1 on success, 0 on timeout, -1 on error */
Michal Vasko086311b2016-01-08 09:53:11 +01001097static int
romanf578cd52023-10-19 09:47:40 +02001098nc_accept_ssh_session_open_netconf_channel(struct nc_session *session, struct nc_server_ssh_opts *opts, int timeout)
Michal Vasko086311b2016-01-08 09:53:11 +01001099{
roman6ece9c52022-06-22 09:29:17 +02001100 struct timespec ts_timeout;
romanf578cd52023-10-19 09:47:40 +02001101 ssh_message msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001102
romanf578cd52023-10-19 09:47:40 +02001103 if (timeout) {
1104 nc_timeouttime_get(&ts_timeout, timeout * 1000);
Michal Vasko36c7be82017-02-22 13:37:59 +01001105 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001106 while (1) {
1107 if (!nc_session_is_connected(session)) {
romanf578cd52023-10-19 09:47:40 +02001108 ERR(session, "Communication SSH socket unexpectedly closed.");
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001109 return -1;
1110 }
1111
romanf578cd52023-10-19 09:47:40 +02001112 msg = ssh_message_get(session->ti.libssh.session);
1113 if (msg) {
1114 if (nc_session_ssh_msg(session, opts, msg, NULL)) {
1115 ssh_message_reply_default(msg);
1116 }
1117 ssh_message_free(msg);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001118 }
1119
romanf578cd52023-10-19 09:47:40 +02001120 if (session->ti.libssh.channel && session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) {
Michal Vasko1a38c862016-01-15 15:50:07 +01001121 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001122 }
1123
Michal Vasko086311b2016-01-08 09:53:11 +01001124 usleep(NC_TIMEOUT_STEP);
romanf578cd52023-10-19 09:47:40 +02001125 if ((opts->auth_timeout) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
roman6ece9c52022-06-22 09:29:17 +02001126 /* timeout */
1127 ERR(session, "Failed to start \"netconf\" SSH subsystem for too long, disconnecting.");
1128 break;
Michal Vasko36c7be82017-02-22 13:37:59 +01001129 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001130 }
Michal Vasko086311b2016-01-08 09:53:11 +01001131
Michal Vasko1a38c862016-01-15 15:50:07 +01001132 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001133}
1134
Michal Vaskof3c41e32022-09-09 11:22:21 +02001135/**
1136 * @brief Set hostkeys to be used for an SSH bind.
1137 *
1138 * @param[in] sbind SSH bind to use.
1139 * @param[in] hostkeys Array of hostkeys.
1140 * @param[in] hostkey_count Count of @p hostkeys.
1141 * @return 0 on success.
1142 * @return -1 on error.
1143 */
Michal Vasko4c1fb492017-01-30 14:31:07 +01001144static int
romanf578cd52023-10-19 09:47:40 +02001145nc_ssh_bind_add_hostkeys(ssh_bind sbind, struct nc_server_ssh_opts *opts, uint16_t hostkey_count)
Michal Vasko4c1fb492017-01-30 14:31:07 +01001146{
romanf578cd52023-10-19 09:47:40 +02001147 uint16_t i;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001148 char *privkey_path, *privkey_data;
Michal Vaskoddce1212019-05-24 09:58:49 +02001149 int ret;
romanf578cd52023-10-19 09:47:40 +02001150 struct nc_asymmetric_key *key = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001151
1152 for (i = 0; i < hostkey_count; ++i) {
1153 privkey_path = privkey_data = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001154
romanf578cd52023-10-19 09:47:40 +02001155 /* get the asymmetric key */
1156 if (opts->hostkeys[i].store == NC_STORE_LOCAL) {
1157 /* stored locally */
1158 key = &opts->hostkeys[i].key;
1159 } else {
1160 /* keystore reference, need to get it */
1161 if (nc_server_ssh_ks_ref_get_key(opts->hostkeys[i].ks_ref, &key)) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001162 return -1;
1163 }
1164 }
1165
romanf578cd52023-10-19 09:47:40 +02001166 privkey_path = base64der_privkey_to_tmp_file(key->privkey_data, nc_privkey_format_to_str(key->privkey_type));
1167 if (!privkey_path) {
1168 ERR(NULL, "Temporarily storing a host key into a file failed (%s).", strerror(errno));
1169 return -1;
1170 }
1171
Michal Vasko4c1fb492017-01-30 14:31:07 +01001172 ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path);
1173
1174 /* cleanup */
1175 if (privkey_data && unlink(privkey_path)) {
Michal Vasko05532772021-06-03 12:12:38 +02001176 WRN(NULL, "Removing a temporary host key file \"%s\" failed (%s).", privkey_path, strerror(errno));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001177 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001178
1179 if (ret != SSH_OK) {
romanf578cd52023-10-19 09:47:40 +02001180 ERR(NULL, "Failed to set hostkey \"%s\" (%s).", opts->hostkeys[i].name, privkey_path);
Michal Vasko80075de2017-07-10 11:38:52 +02001181 }
1182 free(privkey_path);
1183
1184 if (ret != SSH_OK) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001185 return -1;
1186 }
1187 }
1188
1189 return 0;
1190}
1191
Michal Vasko09d700f2022-09-08 10:21:40 +02001192static int
romanf578cd52023-10-19 09:47:40 +02001193nc_accept_ssh_session_auth(struct nc_session *session, struct nc_server_ssh_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001194{
roman6ece9c52022-06-22 09:29:17 +02001195 struct timespec ts_timeout;
roman41a11e42022-06-22 09:27:08 +02001196 ssh_message msg;
romanf578cd52023-10-19 09:47:40 +02001197 struct nc_auth_state state = {0};
Michal Vasko086311b2016-01-08 09:53:11 +01001198
Michal Vasko086311b2016-01-08 09:53:11 +01001199 /* authenticate */
Michal Vasko36c7be82017-02-22 13:37:59 +01001200 if (opts->auth_timeout) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001201 nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000);
Michal Vasko36c7be82017-02-22 13:37:59 +01001202 }
1203 while (1) {
Michal Vasko2a7d4732016-01-15 09:24:46 +01001204 if (!nc_session_is_connected(session)) {
Michal Vasko05532772021-06-03 12:12:38 +02001205 ERR(session, "Communication SSH socket unexpectedly closed.");
Michal Vasko2a7d4732016-01-15 09:24:46 +01001206 return -1;
1207 }
1208
roman41a11e42022-06-22 09:27:08 +02001209 msg = ssh_message_get(session->ti.libssh.session);
1210 if (msg) {
romanf578cd52023-10-19 09:47:40 +02001211 if (nc_session_ssh_msg(session, opts, msg, &state)) {
roman41a11e42022-06-22 09:27:08 +02001212 ssh_message_reply_default(msg);
1213 }
1214 ssh_message_free(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001215 }
1216
Michal Vasko36c7be82017-02-22 13:37:59 +01001217 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
1218 break;
1219 }
1220
Michal Vasko145ae672017-02-07 10:57:27 +01001221 if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) {
Michal Vasko05532772021-06-03 12:12:38 +02001222 ERR(session, "Too many failed authentication attempts of user \"%s\".", session->username);
Michal Vasko145ae672017-02-07 10:57:27 +01001223 return -1;
1224 }
1225
Michal Vasko086311b2016-01-08 09:53:11 +01001226 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +01001227 if ((opts->auth_timeout) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
roman6ece9c52022-06-22 09:29:17 +02001228 /* timeout */
1229 break;
Michal Vasko36c7be82017-02-22 13:37:59 +01001230 }
1231 }
Michal Vasko086311b2016-01-08 09:53:11 +01001232
1233 if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) {
1234 /* timeout */
Michal Vaskoc13da702017-02-07 10:57:57 +01001235 if (session->username) {
Michal Vasko05532772021-06-03 12:12:38 +02001236 ERR(session, "User \"%s\" failed to authenticate for too long, disconnecting.", session->username);
Michal Vaskoc13da702017-02-07 10:57:57 +01001237 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001238 ERR(session, "User failed to authenticate for too long, disconnecting.");
Michal Vaskoc13da702017-02-07 10:57:57 +01001239 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001240 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001241 }
1242
Michal Vasko09d700f2022-09-08 10:21:40 +02001243 return 1;
1244}
1245
1246int
romanf578cd52023-10-19 09:47:40 +02001247nc_accept_ssh_session(struct nc_session *session, struct nc_server_ssh_opts *opts, int sock, int timeout)
Michal Vasko09d700f2022-09-08 10:21:40 +02001248{
1249 ssh_bind sbind = NULL;
Michal Vasko09d700f2022-09-08 10:21:40 +02001250 int rc = 1, r;
1251 struct timespec ts_timeout;
roman4cc0cd52023-04-14 09:12:29 +02001252 const char *err_msg;
Michal Vasko09d700f2022-09-08 10:21:40 +02001253
Michal Vasko09d700f2022-09-08 10:21:40 +02001254 /* other transport-specific data */
1255 session->ti_type = NC_TI_LIBSSH;
1256 session->ti.libssh.session = ssh_new();
1257 if (!session->ti.libssh.session) {
1258 ERR(NULL, "Failed to initialize a new SSH session.");
1259 rc = -1;
1260 goto cleanup;
1261 }
1262
1263 sbind = ssh_bind_new();
1264 if (!sbind) {
1265 ERR(session, "Failed to create an SSH bind.");
1266 rc = -1;
1267 goto cleanup;
1268 }
1269
1270 /* configure host keys */
romanf578cd52023-10-19 09:47:40 +02001271 if (nc_ssh_bind_add_hostkeys(sbind, opts, opts->hostkey_count)) {
1272 rc = -1;
1273 goto cleanup;
1274 }
1275
1276 /* configure supported algorithms */
1277 if (opts->hostkey_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, opts->hostkey_algs)) {
1278 rc = -1;
1279 goto cleanup;
1280 }
1281 if (opts->encryption_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_CIPHERS_S_C, opts->encryption_algs)) {
1282 rc = -1;
1283 goto cleanup;
1284 }
1285 if (opts->kex_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_KEY_EXCHANGE, opts->kex_algs)) {
1286 rc = -1;
1287 goto cleanup;
1288 }
1289 if (opts->mac_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HMAC_S_C, opts->mac_algs)) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001290 rc = -1;
1291 goto cleanup;
1292 }
1293
1294 /* accept new connection on the bind */
1295 if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) {
1296 ERR(session, "SSH failed to accept a new connection (%s).", ssh_get_error(sbind));
1297 rc = -1;
1298 goto cleanup;
1299 }
1300 sock = -1;
1301
romanf578cd52023-10-19 09:47:40 +02001302 /* set to non-blocking */
Michal Vasko09d700f2022-09-08 10:21:40 +02001303 ssh_set_blocking(session->ti.libssh.session, 0);
1304
1305 if (timeout > -1) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001306 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko09d700f2022-09-08 10:21:40 +02001307 }
1308 while ((r = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) {
1309 /* this tends to take longer */
1310 usleep(NC_TIMEOUT_STEP * 20);
Michal Vaskod8a74192023-02-06 15:51:50 +01001311 if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001312 break;
1313 }
1314 }
1315 if (r == SSH_AGAIN) {
1316 ERR(session, "SSH key exchange timeout.");
1317 rc = 0;
1318 goto cleanup;
1319 } else if (r != SSH_OK) {
roman4cc0cd52023-04-14 09:12:29 +02001320 err_msg = ssh_get_error(session->ti.libssh.session);
1321 if (err_msg[0] == '\0') {
1322 err_msg = "hostkey algorithm generated from the hostkey most likely not found in the set of configured hostkey algorithms";
1323 }
1324 ERR(session, "SSH key exchange error (%s).", err_msg);
Michal Vasko09d700f2022-09-08 10:21:40 +02001325 rc = -1;
1326 goto cleanup;
1327 }
1328
1329 /* authenticate */
1330 if ((rc = nc_accept_ssh_session_auth(session, opts)) != 1) {
1331 goto cleanup;
1332 }
1333
Michal Vasko09d700f2022-09-08 10:21:40 +02001334 /* open channel and request 'netconf' subsystem */
romanf578cd52023-10-19 09:47:40 +02001335 if ((rc = nc_accept_ssh_session_open_netconf_channel(session, opts, timeout)) != 1) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001336 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001337 }
1338
Michal Vasko09d700f2022-09-08 10:21:40 +02001339cleanup:
1340 if (sock > -1) {
1341 close(sock);
1342 }
1343 ssh_bind_free(sbind);
1344 return rc;
Michal Vasko086311b2016-01-08 09:53:11 +01001345}
1346
Michal Vasko71090fc2016-05-24 16:37:28 +02001347API NC_MSG_TYPE
1348nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session)
1349{
1350 NC_MSG_TYPE msgtype;
1351 struct nc_session *new_session = NULL;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001352 struct timespec ts_cur;
Michal Vasko71090fc2016-05-24 16:37:28 +02001353
roman40672412023-05-04 11:10:22 +02001354 NC_CHECK_ARG_RET(orig_session, orig_session, session, NC_MSG_ERROR);
Michal Vasko71090fc2016-05-24 16:37:28 +02001355
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001356 if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) &&
1357 orig_session->ti.libssh.next) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001358 for (new_session = orig_session->ti.libssh.next;
1359 new_session != orig_session;
1360 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001361 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1362 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001363 /* we found our session */
1364 break;
1365 }
1366 }
1367 if (new_session == orig_session) {
1368 new_session = NULL;
1369 }
1370 }
1371
1372 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001373 ERR(orig_session, "Session does not have a NETCONF SSH channel ready.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001374 return NC_MSG_ERROR;
1375 }
1376
1377 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001378 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001379
1380 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001381 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001382 if (msgtype != NC_MSG_HELLO) {
1383 return msgtype;
1384 }
1385
Michal Vaskod8a74192023-02-06 15:51:50 +01001386 nc_realtime_get(&ts_cur);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001387 new_session->opts.server.session_start = ts_cur.tv_sec;
Michal Vaskod8a74192023-02-06 15:51:50 +01001388 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001389 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko71090fc2016-05-24 16:37:28 +02001390 new_session->status = NC_STATUS_RUNNING;
1391 *session = new_session;
1392
1393 return msgtype;
1394}
1395
1396API NC_MSG_TYPE
Michal Vasko96164bf2016-01-21 15:41:58 +01001397nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +01001398{
Michal Vaskobdcf2362016-07-26 11:35:43 +02001399 uint8_t q_id;
Michal Vasko71090fc2016-05-24 16:37:28 +02001400 NC_MSG_TYPE msgtype;
Michal Vaskoe4300a82017-05-24 10:35:42 +02001401 struct nc_session *new_session = NULL, *cur_session;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001402 struct timespec ts_cur;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001403 uint16_t i;
Michal Vasko086311b2016-01-08 09:53:11 +01001404
roman40672412023-05-04 11:10:22 +02001405 NC_CHECK_ARG_RET(NULL, ps, session, NC_MSG_ERROR);
Michal Vasko086311b2016-01-08 09:53:11 +01001406
Michal Vasko48a63ed2016-03-01 09:48:21 +01001407 /* LOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001408 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001409 return NC_MSG_ERROR;
Michal Vaskof04a52a2016-04-07 10:52:10 +02001410 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001411
Michal Vasko96164bf2016-01-21 15:41:58 +01001412 for (i = 0; i < ps->session_count; ++i) {
fanchanghu3d4e7212017-08-09 09:42:30 +08001413 cur_session = ps->sessions[i]->session;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001414 if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH) &&
1415 cur_session->ti.libssh.next) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001416 /* an SSH session with more channels */
Michal Vaskoe4300a82017-05-24 10:35:42 +02001417 for (new_session = cur_session->ti.libssh.next;
1418 new_session != cur_session;
Michal Vasko96164bf2016-01-21 15:41:58 +01001419 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001420 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1421 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001422 /* we found our session */
1423 break;
1424 }
1425 }
Michal Vaskoe4300a82017-05-24 10:35:42 +02001426 if (new_session != cur_session) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001427 break;
1428 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001429
Michal Vasko96164bf2016-01-21 15:41:58 +01001430 new_session = NULL;
1431 }
1432 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001433
Michal Vasko48a63ed2016-03-01 09:48:21 +01001434 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001435 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001436
Michal Vasko96164bf2016-01-21 15:41:58 +01001437 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001438 ERR(NULL, "No session with a NETCONF SSH channel ready was found.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001439 return NC_MSG_ERROR;
Michal Vasko96164bf2016-01-21 15:41:58 +01001440 }
1441
1442 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001443 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vaskofb89d772016-01-08 12:25:35 +01001444
Michal Vasko086311b2016-01-08 09:53:11 +01001445 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001446 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001447 if (msgtype != NC_MSG_HELLO) {
1448 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001449 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001450
Michal Vaskod8a74192023-02-06 15:51:50 +01001451 nc_realtime_get(&ts_cur);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001452 new_session->opts.server.session_start = ts_cur.tv_sec;
Michal Vaskod8a74192023-02-06 15:51:50 +01001453 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001454 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko086311b2016-01-08 09:53:11 +01001455 new_session->status = NC_STATUS_RUNNING;
Michal Vasko96164bf2016-01-21 15:41:58 +01001456 *session = new_session;
Michal Vasko086311b2016-01-08 09:53:11 +01001457
Michal Vasko71090fc2016-05-24 16:37:28 +02001458 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001459}