blob: c4c3085f0f47cb1c55d1c1ac32d8606b81663361 [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
53extern struct nc_server_opts server_opts;
Michal Vaskob05053d2016-01-22 16:12:06 +010054
Michal Vasko4c1fb492017-01-30 14:31:07 +010055static char *
romanf578cd52023-10-19 09:47:40 +020056base64der_privkey_to_tmp_file(const char *in, const char *privkey_format)
Michal Vasko086311b2016-01-08 09:53:11 +010057{
Michal Vasko4c1fb492017-01-30 14:31:07 +010058 char path[12] = "/tmp/XXXXXX";
59 int fd, written;
romanf578cd52023-10-19 09:47:40 +020060 unsigned len;
Michal Vasko27252692017-03-21 15:34:13 +010061 mode_t umode;
Michal Vasko4c1fb492017-01-30 14:31:07 +010062 FILE *file;
63
romanf578cd52023-10-19 09:47:40 +020064 NC_CHECK_ARG_RET(NULL, in, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +010065
mekleob31878b2019-09-09 14:10:47 +020066 umode = umask(0177);
Michal Vasko4c1fb492017-01-30 14:31:07 +010067 fd = mkstemp(path);
Michal Vasko27252692017-03-21 15:34:13 +010068 umask(umode);
Michal Vasko4c1fb492017-01-30 14:31:07 +010069 if (fd == -1) {
70 return NULL;
71 }
72
Michal Vasko3964a832018-09-18 14:37:39 +020073 file = fdopen(fd, "w");
Michal Vasko4c1fb492017-01-30 14:31:07 +010074 if (!file) {
75 close(fd);
76 return NULL;
77 }
78
romanf578cd52023-10-19 09:47:40 +020079 /* write header */
80 written = fwrite("-----BEGIN ", 1, 11, file);
81 if (privkey_format) {
82 written += fwrite(privkey_format, 1, strlen(privkey_format), file);
Michal Vasko68177b72020-04-27 15:46:53 +020083 written += fwrite(" PRIVATE KEY-----\n", 1, 18, file);
Michal Vasko68177b72020-04-27 15:46:53 +020084 } else {
romanf578cd52023-10-19 09:47:40 +020085 written += fwrite("PRIVATE KEY-----\n", 1, 17, file);
86 }
Michal Vasko68177b72020-04-27 15:46:53 +020087
romanf578cd52023-10-19 09:47:40 +020088 /* write data */
89 written += fwrite(in, 1, strlen(in), file);
90
91 /* write footer */
92 written += fwrite("\n-----END ", 1, 10, file);
93 if (privkey_format) {
94 written += fwrite(privkey_format, 1, strlen(privkey_format), file);
95 written += fwrite(" PRIVATE KEY-----", 1, 17, file);
96 } else {
97 written += fwrite("PRIVATE KEY-----", 1, 16, file);
98 }
99
100 fclose(file);
101
102 /* checksum */
103 if (privkey_format) {
104 len = 11 + strlen(privkey_format) + 18 + strlen(in) + 10 + strlen(privkey_format) + 17;
105 } else {
106 len = 11 + 17 + strlen(in) + 10 + 16;
107 }
108
109 if ((unsigned)written != len) {
110 unlink(path);
111 return NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100112 }
113
114 return strdup(path);
115}
116
117static int
romanf578cd52023-10-19 09:47:40 +0200118nc_server_ssh_ks_ref_get_key(const char *referenced_name, struct nc_asymmetric_key **askey)
Michal Vasko4c1fb492017-01-30 14:31:07 +0100119{
romanf578cd52023-10-19 09:47:40 +0200120 uint16_t i;
121 struct nc_keystore *ks = &server_opts.keystore;
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100122
romanf578cd52023-10-19 09:47:40 +0200123 *askey = NULL;
Michal Vaskod45e25a2016-01-08 15:48:44 +0100124
romanf578cd52023-10-19 09:47:40 +0200125 /* lookup name */
126 for (i = 0; i < ks->asym_key_count; i++) {
127 if (!strcmp(referenced_name, ks->asym_keys[i].name)) {
128 break;
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100129 }
130 }
131
romanf578cd52023-10-19 09:47:40 +0200132 if (i == ks->asym_key_count) {
133 ERR(NULL, "Keystore entry \"%s\" not found.", referenced_name);
134 return 1;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200135 }
Michal Vasko7d255882017-02-09 13:35:08 +0100136
romanf578cd52023-10-19 09:47:40 +0200137 *askey = &ks->asym_keys[i];
138
139 /* check if the referenced public key is SubjectPublicKeyInfo */
140 if ((*askey)->pubkey_data && nc_is_pk_subject_public_key_info((*askey)->pubkey_data)) {
141 ERR(NULL, "The public key of the referenced hostkey \"%s\" is in the SubjectPublicKeyInfo format, "
142 "which is not allowed in the SSH!", referenced_name);
143 return 1;
Michal Vasko7d255882017-02-09 13:35:08 +0100144 }
Michal Vaskoe2713da2016-08-22 16:06:40 +0200145
Michal Vasko5fcc7142016-02-02 12:21:10 +0100146 return 0;
Michal Vaskob05053d2016-01-22 16:12:06 +0100147}
148
romanf578cd52023-10-19 09:47:40 +0200149static int
150nc_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 +0100151{
romanf578cd52023-10-19 09:47:40 +0200152 uint16_t i, j;
153 struct nc_truststore *ts = &server_opts.truststore;
Michal Vasko3031aae2016-01-27 16:07:18 +0100154
romanf578cd52023-10-19 09:47:40 +0200155 *pubkeys = NULL;
156 *pubkey_count = 0;
157
158 /* lookup name */
159 for (i = 0; i < ts->pub_bag_count; i++) {
160 if (!strcmp(referenced_name, ts->pub_bags[i].name)) {
161 break;
162 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100163 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200164
romanf578cd52023-10-19 09:47:40 +0200165 if (i == ts->pub_bag_count) {
166 ERR(NULL, "Truststore entry \"%s\" not found.", referenced_name);
167 return 1;
168 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200169
romanf578cd52023-10-19 09:47:40 +0200170 /* check if any of the referenced public keys is SubjectPublicKeyInfo */
171 for (j = 0; j < ts->pub_bags[i].pubkey_count; j++) {
172 if (nc_is_pk_subject_public_key_info(ts->pub_bags[i].pubkeys[j].data)) {
173 ERR(NULL, "A public key of the referenced public key bag \"%s\" is in the SubjectPublicKeyInfo format, "
174 "which is not allowed in the SSH!", referenced_name);
175 return 1;
176 }
177 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100178
romanf578cd52023-10-19 09:47:40 +0200179 *pubkeys = ts->pub_bags[i].pubkeys;
180 *pubkey_count = ts->pub_bags[i].pubkey_count;
181 return 0;
Michal Vaskob05053d2016-01-22 16:12:06 +0100182}
183
Michal Vasko974410a2018-04-03 09:36:57 +0200184API void
185nc_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 +0200186 void *user_data, void (*free_user_data)(void *user_data))
Michal Vasko974410a2018-04-03 09:36:57 +0200187{
188 server_opts.passwd_auth_clb = passwd_auth_clb;
189 server_opts.passwd_auth_data = user_data;
190 server_opts.passwd_auth_data_free = free_user_data;
191}
192
bhart1bb7cdb2018-07-02 15:03:30 -0500193API void
romanf578cd52023-10-19 09:47:40 +0200194nc_server_ssh_set_interactive_auth_clb(int (*interactive_auth_clb)(const struct nc_session *session, ssh_session ssh_sess,
195 ssh_message msg, void *user_data), void *user_data, void (*free_user_data)(void *user_data))
bhart1bb7cdb2018-07-02 15:03:30 -0500196{
197 server_opts.interactive_auth_clb = interactive_auth_clb;
198 server_opts.interactive_auth_data = user_data;
199 server_opts.interactive_auth_data_free = free_user_data;
200}
Michal Vasko733c0bd2018-07-03 13:14:40 +0200201
bhart1bb7cdb2018-07-02 15:03:30 -0500202API void
203nc_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 +0200204 void *user_data, void (*free_user_data)(void *user_data))
bhart1bb7cdb2018-07-02 15:03:30 -0500205{
206 server_opts.pubkey_auth_clb = pubkey_auth_clb;
207 server_opts.pubkey_auth_data = user_data;
208 server_opts.pubkey_auth_data_free = free_user_data;
209}
210
Michal Vaskof3c41e32022-09-09 11:22:21 +0200211/**
212 * @brief Compare hashed password with a cleartext password for a match.
213 *
214 * @param[in] pass_hash Hashed password.
215 * @param[in] pass_clear Cleartext password.
216 * @return 0 on match.
217 * @return non-zero if not a match.
218 */
Michal Vasko086311b2016-01-08 09:53:11 +0100219static int
roman814f5112023-10-19 15:51:16 +0200220auth_password_compare_pwd(const char *stored_pw, const char *received_pw)
Michal Vasko086311b2016-01-08 09:53:11 +0100221{
roman814f5112023-10-19 15:51:16 +0200222 char *received_pw_hash = NULL;
roman8b1a6c32023-10-26 13:35:22 +0200223 struct crypt_data cdata = {0};
Michal Vasko086311b2016-01-08 09:53:11 +0100224
roman814f5112023-10-19 15:51:16 +0200225 if (!stored_pw[0]) {
226 if (!received_pw[0]) {
Michal Vasko05532772021-06-03 12:12:38 +0200227 WRN(NULL, "User authentication successful with an empty password!");
Michal Vasko086311b2016-01-08 09:53:11 +0100228 return 0;
229 } else {
230 /* the user did now know he does not need any password,
231 * (which should not be used) so deny authentication */
232 return 1;
233 }
234 }
235
roman814f5112023-10-19 15:51:16 +0200236 if (!strncmp(stored_pw, "$0$", 3)) {
237 /* cleartext password, simply compare the values */
238 return strcmp(stored_pw + 3, received_pw);
239 }
240
roman814f5112023-10-19 15:51:16 +0200241 received_pw_hash = crypt_r(received_pw, stored_pw, &cdata);
roman814f5112023-10-19 15:51:16 +0200242 if (!received_pw_hash) {
roman8b1a6c32023-10-26 13:35:22 +0200243 ERR(NULL, "Hashing the password failed (%s).", strerror(errno));
Andrew Langefeld158d6fd2018-06-11 18:51:44 -0500244 return 1;
245 }
246
roman814f5112023-10-19 15:51:16 +0200247 return strcmp(received_pw_hash, stored_pw);
Michal Vasko086311b2016-01-08 09:53:11 +0100248}
249
romanf578cd52023-10-19 09:47:40 +0200250static int
251nc_sshcb_auth_password(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100252{
Michal Vaskoebba7602018-03-23 13:14:08 +0100253 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100254
Michal Vaskoebba7602018-03-23 13:14:08 +0100255 if (server_opts.passwd_auth_clb) {
256 auth_ret = server_opts.passwd_auth_clb(session, ssh_message_auth_password(msg), server_opts.passwd_auth_data);
257 } else {
romanf578cd52023-10-19 09:47:40 +0200258 auth_ret = auth_password_compare_pwd(auth_client->password, ssh_message_auth_password(msg));
Michal Vasko086311b2016-01-08 09:53:11 +0100259 }
260
romanf578cd52023-10-19 09:47:40 +0200261 if (auth_ret) {
Michal Vaskoebba7602018-03-23 13:14:08 +0100262 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200263 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
264 session->opts.server.ssh_auth_attempts);
Michal Vaskoebba7602018-03-23 13:14:08 +0100265 ssh_message_reply_default(msg);
266 }
romanf578cd52023-10-19 09:47:40 +0200267
268 return auth_ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100269}
270
Michal Vasko0d81c572022-09-26 10:39:31 +0200271#ifdef HAVE_LIBPAM
272
roman41a11e42022-06-22 09:27:08 +0200273/**
274 * @brief PAM conversation function, which serves as a callback for exchanging messages between the client and a PAM module.
275 *
276 * @param[in] n_messages Number of messages.
277 * @param[in] msg PAM module's messages.
278 * @param[out] resp User responses.
279 * @param[in] appdata_ptr Callback's data.
280 * @return PAM_SUCCESS on success;
281 * @return PAM_BUF_ERR on memory allocation error;
282 * @return PAM_CONV_ERR otherwise.
283 */
284static int
285nc_pam_conv_clb(int n_messages, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)
286{
287 int i, j, t, r = PAM_SUCCESS, n_answers, n_requests = n_messages;
288 const char **prompts = NULL;
289 char *echo = NULL;
290 const char *name = "Keyboard-Interactive Authentication";
291 const char *instruction = "Please enter your authentication token";
292 ssh_message reply = NULL;
293 struct nc_pam_thread_arg *clb_data = appdata_ptr;
294 ssh_session libssh_session;
295 struct timespec ts_timeout;
296 struct nc_server_ssh_opts *opts;
297
298 libssh_session = clb_data->session->ti.libssh.session;
romanf578cd52023-10-19 09:47:40 +0200299 opts = clb_data->opts;
roman41a11e42022-06-22 09:27:08 +0200300
301 /* PAM_MAX_NUM_MSG == 32 by default */
302 if ((n_messages <= 0) || (n_messages >= PAM_MAX_NUM_MSG)) {
303 ERR(NULL, "Bad number of PAM messages (#%d).", n_messages);
304 r = PAM_CONV_ERR;
305 goto cleanup;
306 }
307
308 /* only accepting these 4 types of messages */
309 for (i = 0; i < n_messages; i++) {
310 t = msg[i]->msg_style;
311 if ((t != PAM_PROMPT_ECHO_OFF) && (t != PAM_PROMPT_ECHO_ON) && (t != PAM_TEXT_INFO) && (t != PAM_ERROR_MSG)) {
312 ERR(NULL, "PAM conversation callback received an unexpected type of message.");
313 r = PAM_CONV_ERR;
314 goto cleanup;
315 }
316 }
317
318 /* display messages with errors and/or some information and count the amount of actual authentication challenges */
319 for (i = 0; i < n_messages; i++) {
320 if (msg[i]->msg_style == PAM_TEXT_INFO) {
321 VRB(NULL, "PAM conversation callback received a message with some information for the client (%s).", msg[i]->msg);
322 n_requests--;
323 }
324 if (msg[i]->msg_style == PAM_ERROR_MSG) {
325 ERR(NULL, "PAM conversation callback received an error message (%s).", msg[i]->msg);
326 r = PAM_CONV_ERR;
327 goto cleanup;
328 }
329 }
330
331 /* there are no requests left for the user, only messages with some information for the client were sent */
332 if (n_requests <= 0) {
333 r = PAM_SUCCESS;
334 goto cleanup;
335 }
336
337 /* it is the PAM module's responsibility to release both, this array and the responses themselves */
338 *resp = calloc(n_requests, sizeof **resp);
339 prompts = calloc(n_requests, sizeof *prompts);
340 echo = calloc(n_requests, sizeof *echo);
roman3a95bb22023-10-26 11:07:17 +0200341 NC_CHECK_ERRMEM_GOTO(!(*resp) || !prompts || !echo, r = PAM_BUF_ERR, cleanup);
roman41a11e42022-06-22 09:27:08 +0200342
343 /* set the prompts for the user */
344 j = 0;
345 for (i = 0; i < n_messages; i++) {
346 if ((msg[i]->msg_style == PAM_PROMPT_ECHO_ON) || (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF)) {
347 prompts[j++] = msg[i]->msg;
348 }
349 }
350
351 /* iterate over all the messages and adjust the echo array accordingly */
352 j = 0;
353 for (i = 0; i < n_messages; i++) {
354 if (msg[i]->msg_style == PAM_PROMPT_ECHO_ON) {
355 echo[j++] = 1;
356 }
357 if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF) {
358 /* no need to set to 0 because of calloc */
359 j++;
360 }
361 }
362
363 /* print all the keyboard-interactive challenges to the user */
364 r = ssh_message_auth_interactive_request(clb_data->msg, name, instruction, n_requests, prompts, echo);
365 if (r != SSH_OK) {
366 ERR(NULL, "Failed to send an authentication request.");
367 r = PAM_CONV_ERR;
368 goto cleanup;
369 }
370
371 if (opts->auth_timeout) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100372 nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000);
roman41a11e42022-06-22 09:27:08 +0200373 }
374
375 /* get user's replies */
376 do {
377 if (!nc_session_is_connected(clb_data->session)) {
378 ERR(NULL, "Communication SSH socket unexpectedly closed.");
379 r = PAM_CONV_ERR;
380 goto cleanup;
381 }
382
383 reply = ssh_message_get(libssh_session);
384 if (reply) {
385 break;
386 }
387
388 usleep(NC_TIMEOUT_STEP);
romanea0edaa2023-10-26 12:16:25 +0200389 } while (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) >= 1));
roman41a11e42022-06-22 09:27:08 +0200390
391 if (!reply) {
392 ERR(NULL, "Authentication timeout.");
393 r = PAM_CONV_ERR;
394 goto cleanup;
395 }
396
397 /* check if the amount of replies matches the amount of requests */
398 n_answers = ssh_userauth_kbdint_getnanswers(libssh_session);
399 if (n_answers != n_requests) {
400 ERR(NULL, "Expected %d response(s), got %d.", n_requests, n_answers);
401 r = PAM_CONV_ERR;
402 goto cleanup;
403 }
404
405 /* give the replies to a PAM module */
406 for (i = 0; i < n_answers; i++) {
407 (*resp)[i].resp = strdup(ssh_userauth_kbdint_getanswer(libssh_session, i));
408 /* it should be the caller's responsibility to free this, however if mem alloc fails,
409 * it is safer to free the responses here and set them to NULL */
410 if ((*resp)[i].resp == NULL) {
411 for (j = 0; j < i; j++) {
412 free((*resp)[j].resp);
413 (*resp)[j].resp = NULL;
414 }
415 ERRMEM;
416 r = PAM_BUF_ERR;
417 goto cleanup;
418 }
419 }
420
421cleanup:
422 ssh_message_free(reply);
423 free(prompts);
424 free(echo);
425 return r;
426}
427
428/**
429 * @brief Handles authentication via Linux PAM.
430 *
431 * @param[in] session NETCONF session.
432 * @param[in] ssh_msg SSH message with a keyboard-interactive authentication request.
433 * @return PAM_SUCCESS on success;
434 * @return PAM error otherwise.
435 */
436static int
romanf578cd52023-10-19 09:47:40 +0200437nc_pam_auth(struct nc_session *session, struct nc_server_ssh_opts *opts, ssh_message ssh_msg)
roman41a11e42022-06-22 09:27:08 +0200438{
439 pam_handle_t *pam_h = NULL;
440 int ret;
441 struct nc_pam_thread_arg clb_data;
442 struct pam_conv conv;
romanf578cd52023-10-19 09:47:40 +0200443 uint16_t i;
roman41a11e42022-06-22 09:27:08 +0200444
445 /* structure holding callback's data */
446 clb_data.msg = ssh_msg;
447 clb_data.session = session;
romanf578cd52023-10-19 09:47:40 +0200448 clb_data.opts = opts;
roman41a11e42022-06-22 09:27:08 +0200449
450 /* PAM conversation structure holding the callback and it's data */
451 conv.conv = nc_pam_conv_clb;
452 conv.appdata_ptr = &clb_data;
453
romanf578cd52023-10-19 09:47:40 +0200454 /* get the current client's configuration file */
455 for (i = 0; i < opts->client_count; i++) {
456 if (!strcmp(opts->auth_clients[i].username, session->username)) {
457 break;
458 }
459 }
460
461 if (i == opts->client_count) {
462 ERR(NULL, "User \"%s\" not found.", session->username);
463 ret = 1;
464 goto cleanup;
465 }
466
467 if (!opts->auth_clients[i].pam_config_name) {
468 ERR(NULL, "User's \"%s\" PAM configuration filename not set.");
469 ret = 1;
470 goto cleanup;
471 }
472
roman41a11e42022-06-22 09:27:08 +0200473 /* initialize PAM and see if the given configuration file exists */
Michal Vasko0d81c572022-09-26 10:39:31 +0200474# ifdef LIBPAM_HAVE_CONFDIR
roman41a11e42022-06-22 09:27:08 +0200475 /* PAM version >= 1.4 */
romanf578cd52023-10-19 09:47:40 +0200476 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 +0200477# else
roman41a11e42022-06-22 09:27:08 +0200478 /* PAM version < 1.4 */
romanf578cd52023-10-19 09:47:40 +0200479 ret = pam_start(opts->auth_clients[i].pam_config_name, session->username, &conv, &pam_h);
Michal Vasko0d81c572022-09-26 10:39:31 +0200480# endif
roman41a11e42022-06-22 09:27:08 +0200481 if (ret != PAM_SUCCESS) {
482 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
483 goto cleanup;
484 }
485
486 /* authentication based on the modules listed in the configuration file */
487 ret = pam_authenticate(pam_h, 0);
488 if (ret != PAM_SUCCESS) {
489 if (ret == PAM_ABORT) {
490 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
491 goto cleanup;
492 } else {
493 VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
494 goto cleanup;
495 }
496 }
497
498 /* correct token entered, check other requirements(the time of the day, expired token, ...) */
499 ret = pam_acct_mgmt(pam_h, 0);
500 if ((ret != PAM_SUCCESS) && (ret != PAM_NEW_AUTHTOK_REQD)) {
501 VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
502 goto cleanup;
503 }
504
505 /* if a token has expired a new one will be generated */
506 if (ret == PAM_NEW_AUTHTOK_REQD) {
507 VRB(NULL, "PAM warning occurred (%s).\n", pam_strerror(pam_h, ret));
508 ret = pam_chauthtok(pam_h, PAM_CHANGE_EXPIRED_AUTHTOK);
509 if (ret == PAM_SUCCESS) {
510 VRB(NULL, "The authentication token of user \"%s\" updated successfully.", session->username);
511 } else {
512 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
513 goto cleanup;
514 }
515 }
516
517cleanup:
518 /* destroy the PAM context */
519 if (pam_end(pam_h, ret) != PAM_SUCCESS) {
520 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
521 }
522 return ret;
523}
524
Michal Vasko0d81c572022-09-26 10:39:31 +0200525#endif
526
romanf578cd52023-10-19 09:47:40 +0200527static int
528nc_sshcb_auth_kbdint(struct nc_session *session, struct nc_server_ssh_opts *opts, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100529{
bhart3bc2f582018-06-05 12:40:32 -0500530 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100531
romanf578cd52023-10-19 09:47:40 +0200532 if (server_opts.interactive_auth_clb) {
533 auth_ret = server_opts.interactive_auth_clb(session, session->ti.libssh.session, msg, server_opts.interactive_auth_data);
Michal Vasko0d81c572022-09-26 10:39:31 +0200534 } else {
535#ifdef HAVE_LIBPAM
romanf578cd52023-10-19 09:47:40 +0200536 if (nc_pam_auth(session, opts, msg) == PAM_SUCCESS) {
Michal Vasko0d81c572022-09-26 10:39:31 +0200537 auth_ret = 0;
538 }
539#else
540 ERR(session, "PAM-based SSH authentication is not supported.");
541#endif
bhart1bb7cdb2018-07-02 15:03:30 -0500542 }
543
544 /* Authenticate message based on outcome */
romanf578cd52023-10-19 09:47:40 +0200545 if (auth_ret) {
bhart1bb7cdb2018-07-02 15:03:30 -0500546 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200547 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
548 session->opts.server.ssh_auth_attempts);
bhart1bb7cdb2018-07-02 15:03:30 -0500549 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +0100550 }
romanf578cd52023-10-19 09:47:40 +0200551
552 return auth_ret;
553}
554
555/*
556 * Get the public key type from binary data stored in buffer.
557 * The data is in the form of: 4 bytes = data length, then data of data length
558 * and the data is in network byte order. The key has to be in the SSH2 format.
559 */
560static const char *
561nc_server_ssh_get_pubkey_type(const char *buffer, uint32_t *len)
562{
563 uint32_t type_len;
564
565 /* copy the 4 bytes */
566 memcpy(&type_len, buffer, sizeof type_len);
567 /* type_len now stores the length of the key type */
568 type_len = ntohl(type_len);
569 *len = type_len;
570
571 /* move 4 bytes in the buffer, this is where the type should be */
572 buffer += sizeof type_len;
573 return buffer;
574}
575
576/**
577 * @brief Create ssh key from base64 pubkey data.
578 *
579 * @param[in] base64 base64 encoded public key.
580 * @param[out] key created ssh key.
581 * @return 0 on success, 1 otherwise.
582 */
583static int
584nc_server_ssh_create_ssh_pubkey(const char *base64, ssh_key *key)
585{
586 int ret = 0;
587 char *bin = NULL;
588 const char *pub_type = NULL;
589 uint32_t pub_type_len = 0;
590
591 if (!key && !base64) {
592 ERRINT;
593 ret = 1;
594 goto cleanup;
595 }
596
597 *key = NULL;
598
599 /* convert base64 to binary */
600 if (nc_base64_to_bin(base64, &bin) == -1) {
601 ERR(NULL, "Unable to decode base64.");
602 ret = 1;
603 goto cleanup;
604 }
605
606 /* get the key type and try to import it if possible */
607 pub_type = nc_server_ssh_get_pubkey_type(bin, &pub_type_len);
608 if (!pub_type) {
609 ret = 1;
610 goto cleanup;
611 } else if (!strncmp(pub_type, "ssh-dss", pub_type_len)) {
612 ERR(NULL, "DSA keys are not supported.");
613 ret = 1;
614 goto cleanup;
615 } else if (!strncmp(pub_type, "ssh-rsa", pub_type_len)) {
616 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_RSA, key);
617 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp256", pub_type_len)) {
618 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P256, key);
619 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp384", pub_type_len)) {
620 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P384, key);
621 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp521", pub_type_len)) {
622 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P521, key);
623 } else if (!strncmp(pub_type, "ssh-ed25519", pub_type_len)) {
624 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ED25519, key);
625 } else {
626 ERR(NULL, "Public key type not recognised.");
627 ret = 1;
628 goto cleanup;
629 }
630
631cleanup:
632 if (ret != SSH_OK) {
633 ERR(NULL, "Error importing public key.");
634 }
635 free(bin);
636 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100637}
638
Michal Vaskof3c41e32022-09-09 11:22:21 +0200639/**
640 * @brief Compare SSH key with configured authorized keys and return the username of the matching one, if any.
641 *
642 * @param[in] key Presented SSH key to compare.
643 * @return Authorized key username, NULL if no match was found.
644 */
romanf578cd52023-10-19 09:47:40 +0200645static int
646auth_pubkey_compare_key(ssh_key key, struct nc_auth_client *auth_client)
Michal Vasko086311b2016-01-08 09:53:11 +0100647{
romanf578cd52023-10-19 09:47:40 +0200648 uint16_t i, pubkey_count;
Michal Vasko3e9d1682017-02-24 09:50:15 +0100649 int ret = 0;
romanf578cd52023-10-19 09:47:40 +0200650 ssh_key new_key = NULL;
651 struct nc_public_key *pubkeys;
Michal Vasko086311b2016-01-08 09:53:11 +0100652
romanf578cd52023-10-19 09:47:40 +0200653 /* get the correct public key storage */
654 if (auth_client->store == NC_STORE_LOCAL) {
655 pubkeys = auth_client->pubkeys;
656 pubkey_count = auth_client->pubkey_count;
657 } else {
658 ret = nc_server_ssh_ts_ref_get_keys(auth_client->ts_ref, &pubkeys, &pubkey_count);
659 if (ret) {
660 ERR(NULL, "Error getting \"%s\"'s public keys from the truststore.", auth_client->username);
661 return ret;
Michal Vasko17dfda92016-12-01 14:06:16 +0100662 }
romanf578cd52023-10-19 09:47:40 +0200663 }
Michal Vasko17dfda92016-12-01 14:06:16 +0100664
romanf578cd52023-10-19 09:47:40 +0200665 /* try to compare all of the client's keys with the key received in the SSH message */
666 for (i = 0; i < pubkey_count; i++) {
667 /* create the SSH key from the data */
668 if (nc_server_ssh_create_ssh_pubkey(pubkeys[i].data, &new_key)) {
669 ssh_key_free(new_key);
Michal Vasko086311b2016-01-08 09:53:11 +0100670 continue;
671 }
672
romanf578cd52023-10-19 09:47:40 +0200673 /* compare the keys */
674 ret = ssh_key_cmp(key, new_key, SSH_KEY_CMP_PUBLIC);
675 if (!ret) {
Michal Vasko086311b2016-01-08 09:53:11 +0100676 break;
romanf578cd52023-10-19 09:47:40 +0200677 } else {
678 WRN(NULL, "User's \"%s\" public key doesn't match, trying another.", auth_client->username);
679 ssh_key_free(new_key);
Michal Vasko086311b2016-01-08 09:53:11 +0100680 }
Michal Vasko086311b2016-01-08 09:53:11 +0100681 }
682
romanf578cd52023-10-19 09:47:40 +0200683 if (i == pubkey_count) {
684 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100685 }
686
romanf578cd52023-10-19 09:47:40 +0200687 if (!ret) {
688 /* only free a key if everything was ok, it would have already been freed otherwise */
689 ssh_key_free(new_key);
690 }
Michal Vaskoa05c7b12017-01-30 14:33:08 +0100691
romanf578cd52023-10-19 09:47:40 +0200692 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100693}
694
695static void
romanf578cd52023-10-19 09:47:40 +0200696nc_sshcb_auth_none(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100697{
romanf578cd52023-10-19 09:47:40 +0200698 if (auth_client->supports_none && !auth_client->password && !auth_client->pubkey_count && !auth_client->pam_config_name) {
699 /* only authenticate the client if he supports none and no other method */
700 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
701 VRB(session, "User \"%s\" authenticated.", session->username);
702 ssh_message_auth_reply_success(msg, 0);
703 }
704
705 ssh_message_reply_default(msg);
706}
707
708static int
709nc_sshcb_auth_pubkey(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
710{
711 int signature_state, ret = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100712
Michal Vasko733c0bd2018-07-03 13:14:40 +0200713 if (server_opts.pubkey_auth_clb) {
714 if (server_opts.pubkey_auth_clb(session, ssh_message_auth_pubkey(msg), server_opts.pubkey_auth_data)) {
romanf578cd52023-10-19 09:47:40 +0200715 ret = 1;
bhart3bc2f582018-06-05 12:40:32 -0500716 goto fail;
717 }
Michal Vasko733c0bd2018-07-03 13:14:40 +0200718 } else {
romanf578cd52023-10-19 09:47:40 +0200719 if (auth_pubkey_compare_key(ssh_message_auth_pubkey(msg), auth_client)) {
Michal Vasko05532772021-06-03 12:12:38 +0200720 VRB(session, "User \"%s\" tried to use an unknown (unauthorized) public key.", session->username);
romanf578cd52023-10-19 09:47:40 +0200721 ret = 1;
bhart3bc2f582018-06-05 12:40:32 -0500722 goto fail;
723 }
Michal Vaskobd13a932016-09-14 09:00:35 +0200724 }
Michal Vaskobd13a932016-09-14 09:00:35 +0200725
Michal Vasko086311b2016-01-08 09:53:11 +0100726 signature_state = ssh_message_auth_publickey_state(msg);
romanf578cd52023-10-19 09:47:40 +0200727 if (signature_state == SSH_PUBLICKEY_STATE_NONE) {
Michal Vaskobd13a932016-09-14 09:00:35 +0200728 /* accepting only the use of a public key */
729 ssh_message_auth_reply_pk_ok_simple(msg);
romanf578cd52023-10-19 09:47:40 +0200730 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100731 }
732
romanf578cd52023-10-19 09:47:40 +0200733 return ret;
Michal Vaskobd13a932016-09-14 09:00:35 +0200734
735fail:
Michal Vasko2e6defd2016-10-07 15:48:15 +0200736 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200737 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
738 session->opts.server.ssh_auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +0100739 ssh_message_reply_default(msg);
romanf578cd52023-10-19 09:47:40 +0200740
741 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100742}
743
744static int
Michal Vasko96164bf2016-01-21 15:41:58 +0100745nc_sshcb_channel_open(struct nc_session *session, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100746{
Michal Vasko96164bf2016-01-21 15:41:58 +0100747 ssh_channel chan;
748
749 /* first channel request */
750 if (!session->ti.libssh.channel) {
751 if (session->status != NC_STATUS_STARTING) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100752 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100753 return -1;
754 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100755 chan = ssh_message_channel_request_open_reply_accept(msg);
756 if (!chan) {
Michal Vasko05532772021-06-03 12:12:38 +0200757 ERR(session, "Failed to create a new SSH channel.");
Michal Vasko96164bf2016-01-21 15:41:58 +0100758 return -1;
759 }
760 session->ti.libssh.channel = chan;
Michal Vasko086311b2016-01-08 09:53:11 +0100761
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200762 /* additional channel request */
Michal Vasko96164bf2016-01-21 15:41:58 +0100763 } else {
764 chan = ssh_message_channel_request_open_reply_accept(msg);
765 if (!chan) {
Michal Vasko05532772021-06-03 12:12:38 +0200766 ERR(session, "Session %u: failed to create a new SSH channel.", session->id);
Michal Vasko96164bf2016-01-21 15:41:58 +0100767 return -1;
768 }
769 /* channel was created and libssh stored it internally in the ssh_session structure, good enough */
Michal Vasko086311b2016-01-08 09:53:11 +0100770 }
771
Michal Vasko086311b2016-01-08 09:53:11 +0100772 return 0;
773}
774
775static int
776nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem)
777{
Michal Vasko96164bf2016-01-21 15:41:58 +0100778 struct nc_session *new_session;
Michal Vasko086311b2016-01-08 09:53:11 +0100779
Michal Vasko96164bf2016-01-21 15:41:58 +0100780 if (strcmp(subsystem, "netconf")) {
Michal Vasko05532772021-06-03 12:12:38 +0200781 WRN(session, "Received an unknown subsystem \"%s\" request.", subsystem);
Michal Vasko086311b2016-01-08 09:53:11 +0100782 return -1;
783 }
784
Michal Vasko96164bf2016-01-21 15:41:58 +0100785 if (session->ti.libssh.channel == channel) {
786 /* first channel requested */
787 if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) {
788 ERRINT;
789 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100790 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100791 if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) {
Michal Vasko05532772021-06-03 12:12:38 +0200792 ERR(session, "Subsystem \"netconf\" requested for the second time.");
Michal Vasko96164bf2016-01-21 15:41:58 +0100793 return -1;
794 }
795
796 session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF;
Michal Vasko086311b2016-01-08 09:53:11 +0100797 } else {
Michal Vasko96164bf2016-01-21 15:41:58 +0100798 /* additional channel subsystem request, new session is ready as far as SSH is concerned */
Michal Vasko131120a2018-05-29 15:44:02 +0200799 new_session = nc_new_session(NC_SERVER, 1);
roman3a95bb22023-10-26 11:07:17 +0200800 NC_CHECK_ERRMEM_RET(!new_session, -1);
Michal Vasko96164bf2016-01-21 15:41:58 +0100801
802 /* insert the new session */
803 if (!session->ti.libssh.next) {
804 new_session->ti.libssh.next = session;
805 } else {
806 new_session->ti.libssh.next = session->ti.libssh.next;
807 }
808 session->ti.libssh.next = new_session;
809
810 new_session->status = NC_STATUS_STARTING;
Michal Vasko96164bf2016-01-21 15:41:58 +0100811 new_session->ti_type = NC_TI_LIBSSH;
Michal Vasko131120a2018-05-29 15:44:02 +0200812 new_session->io_lock = session->io_lock;
Michal Vasko96164bf2016-01-21 15:41:58 +0100813 new_session->ti.libssh.channel = channel;
814 new_session->ti.libssh.session = session->ti.libssh.session;
Michal Vasko93224072021-11-09 12:14:28 +0100815 new_session->username = strdup(session->username);
816 new_session->host = strdup(session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100817 new_session->port = session->port;
Michal Vasko93224072021-11-09 12:14:28 +0100818 new_session->ctx = (struct ly_ctx *)session->ctx;
Michal Vasko83d15322018-09-27 09:44:02 +0200819 new_session->flags = NC_SESSION_SSH_AUTHENTICATED | NC_SESSION_SSH_SUBSYS_NETCONF | NC_SESSION_SHAREDCTX;
Michal Vasko086311b2016-01-08 09:53:11 +0100820 }
821
822 return 0;
823}
824
Michal Vasko96164bf2016-01-21 15:41:58 +0100825int
romanf578cd52023-10-19 09:47:40 +0200826nc_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 +0100827{
828 const char *str_type, *str_subtype = NULL, *username;
romanf578cd52023-10-19 09:47:40 +0200829 int subtype, type, libssh_auth_methods = 0, ret = 0;
830 uint16_t i;
831 struct nc_auth_client *auth_client = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100832
833 type = ssh_message_type(msg);
834 subtype = ssh_message_subtype(msg);
835
836 switch (type) {
837 case SSH_REQUEST_AUTH:
838 str_type = "request-auth";
839 switch (subtype) {
840 case SSH_AUTH_METHOD_NONE:
841 str_subtype = "none";
842 break;
843 case SSH_AUTH_METHOD_PASSWORD:
844 str_subtype = "password";
845 break;
846 case SSH_AUTH_METHOD_PUBLICKEY:
847 str_subtype = "publickey";
848 break;
849 case SSH_AUTH_METHOD_HOSTBASED:
850 str_subtype = "hostbased";
851 break;
852 case SSH_AUTH_METHOD_INTERACTIVE:
853 str_subtype = "interactive";
854 break;
855 case SSH_AUTH_METHOD_GSSAPI_MIC:
856 str_subtype = "gssapi-mic";
857 break;
858 }
859 break;
860
861 case SSH_REQUEST_CHANNEL_OPEN:
862 str_type = "request-channel-open";
863 switch (subtype) {
864 case SSH_CHANNEL_SESSION:
865 str_subtype = "session";
866 break;
867 case SSH_CHANNEL_DIRECT_TCPIP:
868 str_subtype = "direct-tcpip";
869 break;
870 case SSH_CHANNEL_FORWARDED_TCPIP:
871 str_subtype = "forwarded-tcpip";
872 break;
873 case (int)SSH_CHANNEL_X11:
874 str_subtype = "channel-x11";
875 break;
876 case SSH_CHANNEL_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200877 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100878 default:
879 str_subtype = "unknown";
880 break;
881 }
882 break;
883
884 case SSH_REQUEST_CHANNEL:
885 str_type = "request-channel";
886 switch (subtype) {
887 case SSH_CHANNEL_REQUEST_PTY:
888 str_subtype = "pty";
889 break;
890 case SSH_CHANNEL_REQUEST_EXEC:
891 str_subtype = "exec";
892 break;
893 case SSH_CHANNEL_REQUEST_SHELL:
894 str_subtype = "shell";
895 break;
896 case SSH_CHANNEL_REQUEST_ENV:
897 str_subtype = "env";
898 break;
899 case SSH_CHANNEL_REQUEST_SUBSYSTEM:
900 str_subtype = "subsystem";
901 break;
902 case SSH_CHANNEL_REQUEST_WINDOW_CHANGE:
903 str_subtype = "window-change";
904 break;
905 case SSH_CHANNEL_REQUEST_X11:
906 str_subtype = "x11";
907 break;
908 case SSH_CHANNEL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200909 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100910 default:
911 str_subtype = "unknown";
912 break;
913 }
914 break;
915
916 case SSH_REQUEST_SERVICE:
917 str_type = "request-service";
918 str_subtype = ssh_message_service_service(msg);
919 break;
920
921 case SSH_REQUEST_GLOBAL:
922 str_type = "request-global";
923 switch (subtype) {
924 case SSH_GLOBAL_REQUEST_TCPIP_FORWARD:
925 str_subtype = "tcpip-forward";
926 break;
927 case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD:
928 str_subtype = "cancel-tcpip-forward";
929 break;
930 case SSH_GLOBAL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200931 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100932 default:
933 str_subtype = "unknown";
934 break;
935 }
936 break;
937
938 default:
939 str_type = "unknown";
940 str_subtype = "unknown";
941 break;
942 }
943
Michal Vasko05532772021-06-03 12:12:38 +0200944 VRB(session, "Received an SSH message \"%s\" of subtype \"%s\".", str_type, str_subtype);
Michal Vasko5e0edd82020-07-29 15:26:13 +0200945 if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) {
Michal Vaskoce319162016-02-03 15:33:08 +0100946 /* "valid" situation if, for example, receiving some auth or channel request timeouted,
947 * but we got it now, during session free */
Michal Vasko05532772021-06-03 12:12:38 +0200948 VRB(session, "SSH message arrived on a %s session, the request will be denied.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200949 (session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid"));
Michal Vaskoce319162016-02-03 15:33:08 +0100950 ssh_message_reply_default(msg);
951 return 0;
952 }
Michal Vasko086311b2016-01-08 09:53:11 +0100953
954 /*
955 * process known messages
956 */
957 if (type == SSH_REQUEST_AUTH) {
958 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko05532772021-06-03 12:12:38 +0200959 ERR(session, "User \"%s\" authenticated, but requested another authentication.", session->username);
Michal Vasko086311b2016-01-08 09:53:11 +0100960 ssh_message_reply_default(msg);
961 return 0;
romanf578cd52023-10-19 09:47:40 +0200962 } else if (!state || !opts) {
963 /* these two parameters should always be set during an authentication,
964 * however do a check just in case something goes really wrong, since they
965 * are not needed for other types of messages
966 */
967 ERRINT;
968 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100969 }
970
Michal Vasko086311b2016-01-08 09:53:11 +0100971 /* save the username, do not let the client change it */
972 username = ssh_message_auth_user(msg);
romanf578cd52023-10-19 09:47:40 +0200973 assert(username);
974
975 for (i = 0; i < opts->client_count; i++) {
976 if (!strcmp(opts->auth_clients[i].username, username)) {
977 auth_client = &opts->auth_clients[i];
978 break;
979 }
980 }
981
982 if (!auth_client) {
983 if (opts->endpt_client_ref) {
984 return nc_session_ssh_msg(session, opts->endpt_client_ref->opts.ssh, msg, state);
Michal Vasko086311b2016-01-08 09:53:11 +0100985 }
986
romanf578cd52023-10-19 09:47:40 +0200987 ERR(NULL, "User \"%s\" not known by the server.", username);
988 ssh_message_reply_default(msg);
989 return 0;
990 }
991
992 if (!session->username) {
Michal Vasko93224072021-11-09 12:14:28 +0100993 session->username = strdup(username);
romanf578cd52023-10-19 09:47:40 +0200994
995 /* configure and count accepted auth methods */
996 if (auth_client->store == NC_STORE_LOCAL) {
997 if (auth_client->pubkey_count) {
998 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
999 }
1000 } else if (auth_client->ts_ref) {
1001 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
1002 }
1003 if (auth_client->password) {
1004 state->auth_method_count++;
1005 libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD;
1006 }
1007 if (auth_client->pam_config_name) {
1008 state->auth_method_count++;
1009 libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE;
1010 }
1011 if (auth_client->supports_none) {
1012 libssh_auth_methods |= SSH_AUTH_METHOD_NONE;
1013 }
1014
1015 if (libssh_auth_methods & SSH_AUTH_METHOD_PUBLICKEY) {
1016 state->auth_method_count++;
1017 }
1018
1019 ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods);
1020 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001021 if (strcmp(username, session->username)) {
romanf578cd52023-10-19 09:47:40 +02001022 /* changing username not allowed */
Michal Vasko05532772021-06-03 12:12:38 +02001023 ERR(session, "User \"%s\" changed its username to \"%s\".", session->username, username);
Michal Vasko086311b2016-01-08 09:53:11 +01001024 session->status = NC_STATUS_INVALID;
Michal Vasko428087d2016-01-14 16:04:28 +01001025 session->term_reason = NC_SESSION_TERM_OTHER;
Michal Vasko086311b2016-01-08 09:53:11 +01001026 return 1;
1027 }
1028 }
1029
romanf578cd52023-10-19 09:47:40 +02001030 /* try authenticating, the user must authenticate via all of his configured auth methods */
Michal Vasko086311b2016-01-08 09:53:11 +01001031 if (subtype == SSH_AUTH_METHOD_NONE) {
romanf578cd52023-10-19 09:47:40 +02001032 nc_sshcb_auth_none(session, auth_client, msg);
1033 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001034 } else if (subtype == SSH_AUTH_METHOD_PASSWORD) {
romanf578cd52023-10-19 09:47:40 +02001035 ret = nc_sshcb_auth_password(session, auth_client, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001036 } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) {
romanf578cd52023-10-19 09:47:40 +02001037 ret = nc_sshcb_auth_pubkey(session, auth_client, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001038 } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) {
romanf578cd52023-10-19 09:47:40 +02001039 ret = nc_sshcb_auth_kbdint(session, opts, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001040 }
romanf578cd52023-10-19 09:47:40 +02001041
1042 if (!ret) {
1043 state->auth_success_count++;
1044 }
1045
1046 if (!ret && (state->auth_success_count < state->auth_method_count)) {
1047 /* success, but he needs to do another method */
1048 VRB(session, "User \"%s\" partially authenticated, but still needs to authenticate via the rest of his configured methods.", username);
1049 ssh_message_auth_reply_success(msg, 1);
1050 } else if (!ret && (state->auth_success_count == state->auth_method_count)) {
1051 /* authenticated */
1052 ssh_message_auth_reply_success(msg, 0);
1053 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
1054 VRB(session, "User \"%s\" authenticated.", username);
1055 }
1056
1057 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001058 } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko0df67562016-01-21 15:50:11 +01001059 if ((type == SSH_REQUEST_CHANNEL_OPEN) && ((enum ssh_channel_type_e)subtype == SSH_CHANNEL_SESSION)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001060 if (nc_sshcb_channel_open(session, msg)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001061 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001062 }
Michal Vasko086311b2016-01-08 09:53:11 +01001063 return 0;
Michal Vasko96164bf2016-01-21 15:41:58 +01001064
Michal Vasko0df67562016-01-21 15:50:11 +01001065 } else if ((type == SSH_REQUEST_CHANNEL) && ((enum ssh_channel_requests_e)subtype == SSH_CHANNEL_REQUEST_SUBSYSTEM)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001066 if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg),
1067 ssh_message_channel_request_subsystem(msg))) {
Michal Vasko086311b2016-01-08 09:53:11 +01001068 ssh_message_reply_default(msg);
Michal Vasko96164bf2016-01-21 15:41:58 +01001069 } else {
1070 ssh_message_channel_request_reply_success(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001071 }
1072 return 0;
1073 }
1074 }
1075
1076 /* we did not process it */
1077 return 1;
1078}
1079
Michal Vasko1a38c862016-01-15 15:50:07 +01001080/* ret 1 on success, 0 on timeout, -1 on error */
Michal Vasko086311b2016-01-08 09:53:11 +01001081static int
romanf578cd52023-10-19 09:47:40 +02001082nc_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 +01001083{
roman6ece9c52022-06-22 09:29:17 +02001084 struct timespec ts_timeout;
romanf578cd52023-10-19 09:47:40 +02001085 ssh_message msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001086
romanf578cd52023-10-19 09:47:40 +02001087 if (timeout) {
1088 nc_timeouttime_get(&ts_timeout, timeout * 1000);
Michal Vasko36c7be82017-02-22 13:37:59 +01001089 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001090 while (1) {
1091 if (!nc_session_is_connected(session)) {
romanf578cd52023-10-19 09:47:40 +02001092 ERR(session, "Communication SSH socket unexpectedly closed.");
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001093 return -1;
1094 }
1095
romanf578cd52023-10-19 09:47:40 +02001096 msg = ssh_message_get(session->ti.libssh.session);
1097 if (msg) {
1098 if (nc_session_ssh_msg(session, opts, msg, NULL)) {
1099 ssh_message_reply_default(msg);
1100 }
1101 ssh_message_free(msg);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001102 }
1103
romanf578cd52023-10-19 09:47:40 +02001104 if (session->ti.libssh.channel && session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) {
Michal Vasko1a38c862016-01-15 15:50:07 +01001105 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001106 }
1107
Michal Vasko086311b2016-01-08 09:53:11 +01001108 usleep(NC_TIMEOUT_STEP);
romanea0edaa2023-10-26 12:16:25 +02001109 if (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
roman6ece9c52022-06-22 09:29:17 +02001110 /* timeout */
1111 ERR(session, "Failed to start \"netconf\" SSH subsystem for too long, disconnecting.");
1112 break;
Michal Vasko36c7be82017-02-22 13:37:59 +01001113 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001114 }
Michal Vasko086311b2016-01-08 09:53:11 +01001115
Michal Vasko1a38c862016-01-15 15:50:07 +01001116 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001117}
1118
Michal Vaskof3c41e32022-09-09 11:22:21 +02001119/**
1120 * @brief Set hostkeys to be used for an SSH bind.
1121 *
1122 * @param[in] sbind SSH bind to use.
1123 * @param[in] hostkeys Array of hostkeys.
1124 * @param[in] hostkey_count Count of @p hostkeys.
1125 * @return 0 on success.
1126 * @return -1 on error.
1127 */
Michal Vasko4c1fb492017-01-30 14:31:07 +01001128static int
romanf578cd52023-10-19 09:47:40 +02001129nc_ssh_bind_add_hostkeys(ssh_bind sbind, struct nc_server_ssh_opts *opts, uint16_t hostkey_count)
Michal Vasko4c1fb492017-01-30 14:31:07 +01001130{
romanf578cd52023-10-19 09:47:40 +02001131 uint16_t i;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001132 char *privkey_path, *privkey_data;
Michal Vaskoddce1212019-05-24 09:58:49 +02001133 int ret;
romanf578cd52023-10-19 09:47:40 +02001134 struct nc_asymmetric_key *key = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001135
1136 for (i = 0; i < hostkey_count; ++i) {
1137 privkey_path = privkey_data = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001138
romanf578cd52023-10-19 09:47:40 +02001139 /* get the asymmetric key */
1140 if (opts->hostkeys[i].store == NC_STORE_LOCAL) {
1141 /* stored locally */
1142 key = &opts->hostkeys[i].key;
1143 } else {
1144 /* keystore reference, need to get it */
1145 if (nc_server_ssh_ks_ref_get_key(opts->hostkeys[i].ks_ref, &key)) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001146 return -1;
1147 }
1148 }
1149
romanf578cd52023-10-19 09:47:40 +02001150 privkey_path = base64der_privkey_to_tmp_file(key->privkey_data, nc_privkey_format_to_str(key->privkey_type));
1151 if (!privkey_path) {
1152 ERR(NULL, "Temporarily storing a host key into a file failed (%s).", strerror(errno));
1153 return -1;
1154 }
1155
Michal Vasko4c1fb492017-01-30 14:31:07 +01001156 ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path);
1157
1158 /* cleanup */
1159 if (privkey_data && unlink(privkey_path)) {
Michal Vasko05532772021-06-03 12:12:38 +02001160 WRN(NULL, "Removing a temporary host key file \"%s\" failed (%s).", privkey_path, strerror(errno));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001161 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001162
1163 if (ret != SSH_OK) {
romanf578cd52023-10-19 09:47:40 +02001164 ERR(NULL, "Failed to set hostkey \"%s\" (%s).", opts->hostkeys[i].name, privkey_path);
Michal Vasko80075de2017-07-10 11:38:52 +02001165 }
1166 free(privkey_path);
1167
1168 if (ret != SSH_OK) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001169 return -1;
1170 }
1171 }
1172
1173 return 0;
1174}
1175
Michal Vasko09d700f2022-09-08 10:21:40 +02001176static int
romanf578cd52023-10-19 09:47:40 +02001177nc_accept_ssh_session_auth(struct nc_session *session, struct nc_server_ssh_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001178{
roman6ece9c52022-06-22 09:29:17 +02001179 struct timespec ts_timeout;
roman41a11e42022-06-22 09:27:08 +02001180 ssh_message msg;
romanf578cd52023-10-19 09:47:40 +02001181 struct nc_auth_state state = {0};
Michal Vasko086311b2016-01-08 09:53:11 +01001182
Michal Vasko086311b2016-01-08 09:53:11 +01001183 /* authenticate */
Michal Vasko36c7be82017-02-22 13:37:59 +01001184 if (opts->auth_timeout) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001185 nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000);
Michal Vasko36c7be82017-02-22 13:37:59 +01001186 }
1187 while (1) {
Michal Vasko2a7d4732016-01-15 09:24:46 +01001188 if (!nc_session_is_connected(session)) {
Michal Vasko05532772021-06-03 12:12:38 +02001189 ERR(session, "Communication SSH socket unexpectedly closed.");
Michal Vasko2a7d4732016-01-15 09:24:46 +01001190 return -1;
1191 }
1192
roman41a11e42022-06-22 09:27:08 +02001193 msg = ssh_message_get(session->ti.libssh.session);
1194 if (msg) {
romanf578cd52023-10-19 09:47:40 +02001195 if (nc_session_ssh_msg(session, opts, msg, &state)) {
roman41a11e42022-06-22 09:27:08 +02001196 ssh_message_reply_default(msg);
1197 }
1198 ssh_message_free(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001199 }
1200
Michal Vasko36c7be82017-02-22 13:37:59 +01001201 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
1202 break;
1203 }
1204
Michal Vasko145ae672017-02-07 10:57:27 +01001205 if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) {
Michal Vasko05532772021-06-03 12:12:38 +02001206 ERR(session, "Too many failed authentication attempts of user \"%s\".", session->username);
Michal Vasko145ae672017-02-07 10:57:27 +01001207 return -1;
1208 }
1209
Michal Vasko086311b2016-01-08 09:53:11 +01001210 usleep(NC_TIMEOUT_STEP);
romanea0edaa2023-10-26 12:16:25 +02001211 if (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
roman6ece9c52022-06-22 09:29:17 +02001212 /* timeout */
1213 break;
Michal Vasko36c7be82017-02-22 13:37:59 +01001214 }
1215 }
Michal Vasko086311b2016-01-08 09:53:11 +01001216
1217 if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) {
1218 /* timeout */
Michal Vaskoc13da702017-02-07 10:57:57 +01001219 if (session->username) {
Michal Vasko05532772021-06-03 12:12:38 +02001220 ERR(session, "User \"%s\" failed to authenticate for too long, disconnecting.", session->username);
Michal Vaskoc13da702017-02-07 10:57:57 +01001221 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001222 ERR(session, "User failed to authenticate for too long, disconnecting.");
Michal Vaskoc13da702017-02-07 10:57:57 +01001223 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001224 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001225 }
1226
Michal Vasko09d700f2022-09-08 10:21:40 +02001227 return 1;
1228}
1229
1230int
romanf578cd52023-10-19 09:47:40 +02001231nc_accept_ssh_session(struct nc_session *session, struct nc_server_ssh_opts *opts, int sock, int timeout)
Michal Vasko09d700f2022-09-08 10:21:40 +02001232{
1233 ssh_bind sbind = NULL;
Michal Vasko09d700f2022-09-08 10:21:40 +02001234 int rc = 1, r;
1235 struct timespec ts_timeout;
roman4cc0cd52023-04-14 09:12:29 +02001236 const char *err_msg;
Michal Vasko09d700f2022-09-08 10:21:40 +02001237
Michal Vasko09d700f2022-09-08 10:21:40 +02001238 /* other transport-specific data */
1239 session->ti_type = NC_TI_LIBSSH;
1240 session->ti.libssh.session = ssh_new();
1241 if (!session->ti.libssh.session) {
1242 ERR(NULL, "Failed to initialize a new SSH session.");
1243 rc = -1;
1244 goto cleanup;
1245 }
1246
1247 sbind = ssh_bind_new();
1248 if (!sbind) {
1249 ERR(session, "Failed to create an SSH bind.");
1250 rc = -1;
1251 goto cleanup;
1252 }
1253
1254 /* configure host keys */
romanf578cd52023-10-19 09:47:40 +02001255 if (nc_ssh_bind_add_hostkeys(sbind, opts, opts->hostkey_count)) {
1256 rc = -1;
1257 goto cleanup;
1258 }
1259
1260 /* configure supported algorithms */
1261 if (opts->hostkey_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, opts->hostkey_algs)) {
1262 rc = -1;
1263 goto cleanup;
1264 }
1265 if (opts->encryption_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_CIPHERS_S_C, opts->encryption_algs)) {
1266 rc = -1;
1267 goto cleanup;
1268 }
1269 if (opts->kex_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_KEY_EXCHANGE, opts->kex_algs)) {
1270 rc = -1;
1271 goto cleanup;
1272 }
1273 if (opts->mac_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HMAC_S_C, opts->mac_algs)) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001274 rc = -1;
1275 goto cleanup;
1276 }
1277
1278 /* accept new connection on the bind */
1279 if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) {
1280 ERR(session, "SSH failed to accept a new connection (%s).", ssh_get_error(sbind));
1281 rc = -1;
1282 goto cleanup;
1283 }
1284 sock = -1;
1285
romanf578cd52023-10-19 09:47:40 +02001286 /* set to non-blocking */
Michal Vasko09d700f2022-09-08 10:21:40 +02001287 ssh_set_blocking(session->ti.libssh.session, 0);
1288
1289 if (timeout > -1) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001290 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko09d700f2022-09-08 10:21:40 +02001291 }
1292 while ((r = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) {
1293 /* this tends to take longer */
1294 usleep(NC_TIMEOUT_STEP * 20);
Michal Vaskod8a74192023-02-06 15:51:50 +01001295 if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001296 break;
1297 }
1298 }
1299 if (r == SSH_AGAIN) {
1300 ERR(session, "SSH key exchange timeout.");
1301 rc = 0;
1302 goto cleanup;
1303 } else if (r != SSH_OK) {
roman4cc0cd52023-04-14 09:12:29 +02001304 err_msg = ssh_get_error(session->ti.libssh.session);
1305 if (err_msg[0] == '\0') {
1306 err_msg = "hostkey algorithm generated from the hostkey most likely not found in the set of configured hostkey algorithms";
1307 }
1308 ERR(session, "SSH key exchange error (%s).", err_msg);
Michal Vasko09d700f2022-09-08 10:21:40 +02001309 rc = -1;
1310 goto cleanup;
1311 }
1312
1313 /* authenticate */
1314 if ((rc = nc_accept_ssh_session_auth(session, opts)) != 1) {
1315 goto cleanup;
1316 }
1317
Michal Vasko09d700f2022-09-08 10:21:40 +02001318 /* open channel and request 'netconf' subsystem */
romanf578cd52023-10-19 09:47:40 +02001319 if ((rc = nc_accept_ssh_session_open_netconf_channel(session, opts, timeout)) != 1) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001320 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001321 }
1322
Michal Vasko09d700f2022-09-08 10:21:40 +02001323cleanup:
1324 if (sock > -1) {
1325 close(sock);
1326 }
1327 ssh_bind_free(sbind);
1328 return rc;
Michal Vasko086311b2016-01-08 09:53:11 +01001329}
1330
Michal Vasko71090fc2016-05-24 16:37:28 +02001331API NC_MSG_TYPE
1332nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session)
1333{
1334 NC_MSG_TYPE msgtype;
1335 struct nc_session *new_session = NULL;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001336 struct timespec ts_cur;
Michal Vasko71090fc2016-05-24 16:37:28 +02001337
roman40672412023-05-04 11:10:22 +02001338 NC_CHECK_ARG_RET(orig_session, orig_session, session, NC_MSG_ERROR);
Michal Vasko71090fc2016-05-24 16:37:28 +02001339
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001340 if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) &&
1341 orig_session->ti.libssh.next) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001342 for (new_session = orig_session->ti.libssh.next;
1343 new_session != orig_session;
1344 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001345 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1346 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001347 /* we found our session */
1348 break;
1349 }
1350 }
1351 if (new_session == orig_session) {
1352 new_session = NULL;
1353 }
1354 }
1355
1356 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001357 ERR(orig_session, "Session does not have a NETCONF SSH channel ready.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001358 return NC_MSG_ERROR;
1359 }
1360
1361 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001362 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001363
1364 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001365 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001366 if (msgtype != NC_MSG_HELLO) {
1367 return msgtype;
1368 }
1369
Michal Vaskod8a74192023-02-06 15:51:50 +01001370 nc_realtime_get(&ts_cur);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001371 new_session->opts.server.session_start = ts_cur.tv_sec;
Michal Vaskod8a74192023-02-06 15:51:50 +01001372 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001373 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko71090fc2016-05-24 16:37:28 +02001374 new_session->status = NC_STATUS_RUNNING;
1375 *session = new_session;
1376
1377 return msgtype;
1378}
1379
1380API NC_MSG_TYPE
Michal Vasko96164bf2016-01-21 15:41:58 +01001381nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +01001382{
Michal Vaskobdcf2362016-07-26 11:35:43 +02001383 uint8_t q_id;
Michal Vasko71090fc2016-05-24 16:37:28 +02001384 NC_MSG_TYPE msgtype;
Michal Vaskoe4300a82017-05-24 10:35:42 +02001385 struct nc_session *new_session = NULL, *cur_session;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001386 struct timespec ts_cur;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001387 uint16_t i;
Michal Vasko086311b2016-01-08 09:53:11 +01001388
roman40672412023-05-04 11:10:22 +02001389 NC_CHECK_ARG_RET(NULL, ps, session, NC_MSG_ERROR);
Michal Vasko086311b2016-01-08 09:53:11 +01001390
Michal Vasko48a63ed2016-03-01 09:48:21 +01001391 /* LOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001392 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001393 return NC_MSG_ERROR;
Michal Vaskof04a52a2016-04-07 10:52:10 +02001394 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001395
Michal Vasko96164bf2016-01-21 15:41:58 +01001396 for (i = 0; i < ps->session_count; ++i) {
fanchanghu3d4e7212017-08-09 09:42:30 +08001397 cur_session = ps->sessions[i]->session;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001398 if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH) &&
1399 cur_session->ti.libssh.next) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001400 /* an SSH session with more channels */
Michal Vaskoe4300a82017-05-24 10:35:42 +02001401 for (new_session = cur_session->ti.libssh.next;
1402 new_session != cur_session;
Michal Vasko96164bf2016-01-21 15:41:58 +01001403 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001404 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1405 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001406 /* we found our session */
1407 break;
1408 }
1409 }
Michal Vaskoe4300a82017-05-24 10:35:42 +02001410 if (new_session != cur_session) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001411 break;
1412 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001413
Michal Vasko96164bf2016-01-21 15:41:58 +01001414 new_session = NULL;
1415 }
1416 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001417
Michal Vasko48a63ed2016-03-01 09:48:21 +01001418 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001419 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001420
Michal Vasko96164bf2016-01-21 15:41:58 +01001421 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001422 ERR(NULL, "No session with a NETCONF SSH channel ready was found.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001423 return NC_MSG_ERROR;
Michal Vasko96164bf2016-01-21 15:41:58 +01001424 }
1425
1426 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001427 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vaskofb89d772016-01-08 12:25:35 +01001428
Michal Vasko086311b2016-01-08 09:53:11 +01001429 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001430 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001431 if (msgtype != NC_MSG_HELLO) {
1432 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001433 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001434
Michal Vaskod8a74192023-02-06 15:51:50 +01001435 nc_realtime_get(&ts_cur);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001436 new_session->opts.server.session_start = ts_cur.tv_sec;
Michal Vaskod8a74192023-02-06 15:51:50 +01001437 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001438 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko086311b2016-01-08 09:53:11 +01001439 new_session->status = NC_STATUS_RUNNING;
Michal Vasko96164bf2016-01-21 15:41:58 +01001440 *session = new_session;
Michal Vasko086311b2016-01-08 09:53:11 +01001441
Michal Vasko71090fc2016-05-24 16:37:28 +02001442 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001443}