blob: cc1f3de7aa6e123c8edc4e942b356ad8dc5d1c7e [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;
roman78df0fa2023-11-02 10:33:57 +0100832 struct nc_endpt *referenced_endpt;
Michal Vasko086311b2016-01-08 09:53:11 +0100833
834 type = ssh_message_type(msg);
835 subtype = ssh_message_subtype(msg);
836
837 switch (type) {
838 case SSH_REQUEST_AUTH:
839 str_type = "request-auth";
840 switch (subtype) {
841 case SSH_AUTH_METHOD_NONE:
842 str_subtype = "none";
843 break;
844 case SSH_AUTH_METHOD_PASSWORD:
845 str_subtype = "password";
846 break;
847 case SSH_AUTH_METHOD_PUBLICKEY:
848 str_subtype = "publickey";
849 break;
850 case SSH_AUTH_METHOD_HOSTBASED:
851 str_subtype = "hostbased";
852 break;
853 case SSH_AUTH_METHOD_INTERACTIVE:
854 str_subtype = "interactive";
855 break;
856 case SSH_AUTH_METHOD_GSSAPI_MIC:
857 str_subtype = "gssapi-mic";
858 break;
859 }
860 break;
861
862 case SSH_REQUEST_CHANNEL_OPEN:
863 str_type = "request-channel-open";
864 switch (subtype) {
865 case SSH_CHANNEL_SESSION:
866 str_subtype = "session";
867 break;
868 case SSH_CHANNEL_DIRECT_TCPIP:
869 str_subtype = "direct-tcpip";
870 break;
871 case SSH_CHANNEL_FORWARDED_TCPIP:
872 str_subtype = "forwarded-tcpip";
873 break;
874 case (int)SSH_CHANNEL_X11:
875 str_subtype = "channel-x11";
876 break;
877 case SSH_CHANNEL_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200878 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100879 default:
880 str_subtype = "unknown";
881 break;
882 }
883 break;
884
885 case SSH_REQUEST_CHANNEL:
886 str_type = "request-channel";
887 switch (subtype) {
888 case SSH_CHANNEL_REQUEST_PTY:
889 str_subtype = "pty";
890 break;
891 case SSH_CHANNEL_REQUEST_EXEC:
892 str_subtype = "exec";
893 break;
894 case SSH_CHANNEL_REQUEST_SHELL:
895 str_subtype = "shell";
896 break;
897 case SSH_CHANNEL_REQUEST_ENV:
898 str_subtype = "env";
899 break;
900 case SSH_CHANNEL_REQUEST_SUBSYSTEM:
901 str_subtype = "subsystem";
902 break;
903 case SSH_CHANNEL_REQUEST_WINDOW_CHANGE:
904 str_subtype = "window-change";
905 break;
906 case SSH_CHANNEL_REQUEST_X11:
907 str_subtype = "x11";
908 break;
909 case SSH_CHANNEL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200910 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100911 default:
912 str_subtype = "unknown";
913 break;
914 }
915 break;
916
917 case SSH_REQUEST_SERVICE:
918 str_type = "request-service";
919 str_subtype = ssh_message_service_service(msg);
920 break;
921
922 case SSH_REQUEST_GLOBAL:
923 str_type = "request-global";
924 switch (subtype) {
925 case SSH_GLOBAL_REQUEST_TCPIP_FORWARD:
926 str_subtype = "tcpip-forward";
927 break;
928 case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD:
929 str_subtype = "cancel-tcpip-forward";
930 break;
931 case SSH_GLOBAL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200932 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100933 default:
934 str_subtype = "unknown";
935 break;
936 }
937 break;
938
939 default:
940 str_type = "unknown";
941 str_subtype = "unknown";
942 break;
943 }
944
Michal Vasko05532772021-06-03 12:12:38 +0200945 VRB(session, "Received an SSH message \"%s\" of subtype \"%s\".", str_type, str_subtype);
Michal Vasko5e0edd82020-07-29 15:26:13 +0200946 if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) {
Michal Vaskoce319162016-02-03 15:33:08 +0100947 /* "valid" situation if, for example, receiving some auth or channel request timeouted,
948 * but we got it now, during session free */
Michal Vasko05532772021-06-03 12:12:38 +0200949 VRB(session, "SSH message arrived on a %s session, the request will be denied.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200950 (session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid"));
Michal Vaskoce319162016-02-03 15:33:08 +0100951 ssh_message_reply_default(msg);
952 return 0;
953 }
Michal Vasko086311b2016-01-08 09:53:11 +0100954
955 /*
956 * process known messages
957 */
958 if (type == SSH_REQUEST_AUTH) {
959 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko05532772021-06-03 12:12:38 +0200960 ERR(session, "User \"%s\" authenticated, but requested another authentication.", session->username);
Michal Vasko086311b2016-01-08 09:53:11 +0100961 ssh_message_reply_default(msg);
962 return 0;
romanf578cd52023-10-19 09:47:40 +0200963 } else if (!state || !opts) {
964 /* these two parameters should always be set during an authentication,
965 * however do a check just in case something goes really wrong, since they
966 * are not needed for other types of messages
967 */
968 ERRINT;
969 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100970 }
971
Michal Vasko086311b2016-01-08 09:53:11 +0100972 /* save the username, do not let the client change it */
973 username = ssh_message_auth_user(msg);
romanf578cd52023-10-19 09:47:40 +0200974 assert(username);
975
976 for (i = 0; i < opts->client_count; i++) {
977 if (!strcmp(opts->auth_clients[i].username, username)) {
978 auth_client = &opts->auth_clients[i];
979 break;
980 }
981 }
982
983 if (!auth_client) {
roman78df0fa2023-11-02 10:33:57 +0100984 if (opts->referenced_endpt_name) {
985 /* client not known by the endpt, but it references another one so try it */
986 if (nc_server_get_referenced_endpt(opts->referenced_endpt_name, &referenced_endpt)) {
987 ERRINT;
988 return 1;
989 }
990
991 return nc_session_ssh_msg(session, referenced_endpt->opts.ssh, msg, state);
Michal Vasko086311b2016-01-08 09:53:11 +0100992 }
993
romanf578cd52023-10-19 09:47:40 +0200994 ERR(NULL, "User \"%s\" not known by the server.", username);
995 ssh_message_reply_default(msg);
996 return 0;
997 }
998
999 if (!session->username) {
Michal Vasko93224072021-11-09 12:14:28 +01001000 session->username = strdup(username);
romanf578cd52023-10-19 09:47:40 +02001001
1002 /* configure and count accepted auth methods */
1003 if (auth_client->store == NC_STORE_LOCAL) {
1004 if (auth_client->pubkey_count) {
1005 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
1006 }
1007 } else if (auth_client->ts_ref) {
1008 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
1009 }
1010 if (auth_client->password) {
1011 state->auth_method_count++;
1012 libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD;
1013 }
1014 if (auth_client->pam_config_name) {
1015 state->auth_method_count++;
1016 libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE;
1017 }
1018 if (auth_client->supports_none) {
1019 libssh_auth_methods |= SSH_AUTH_METHOD_NONE;
1020 }
1021
1022 if (libssh_auth_methods & SSH_AUTH_METHOD_PUBLICKEY) {
1023 state->auth_method_count++;
1024 }
1025
1026 ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods);
1027 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001028 if (strcmp(username, session->username)) {
romanf578cd52023-10-19 09:47:40 +02001029 /* changing username not allowed */
Michal Vasko05532772021-06-03 12:12:38 +02001030 ERR(session, "User \"%s\" changed its username to \"%s\".", session->username, username);
Michal Vasko086311b2016-01-08 09:53:11 +01001031 session->status = NC_STATUS_INVALID;
Michal Vasko428087d2016-01-14 16:04:28 +01001032 session->term_reason = NC_SESSION_TERM_OTHER;
Michal Vasko086311b2016-01-08 09:53:11 +01001033 return 1;
1034 }
1035 }
1036
romanf578cd52023-10-19 09:47:40 +02001037 /* try authenticating, the user must authenticate via all of his configured auth methods */
Michal Vasko086311b2016-01-08 09:53:11 +01001038 if (subtype == SSH_AUTH_METHOD_NONE) {
romanf578cd52023-10-19 09:47:40 +02001039 nc_sshcb_auth_none(session, auth_client, msg);
1040 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001041 } else if (subtype == SSH_AUTH_METHOD_PASSWORD) {
romanf578cd52023-10-19 09:47:40 +02001042 ret = nc_sshcb_auth_password(session, auth_client, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001043 } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) {
romanf578cd52023-10-19 09:47:40 +02001044 ret = nc_sshcb_auth_pubkey(session, auth_client, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001045 } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) {
romanf578cd52023-10-19 09:47:40 +02001046 ret = nc_sshcb_auth_kbdint(session, opts, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001047 }
romanf578cd52023-10-19 09:47:40 +02001048
1049 if (!ret) {
1050 state->auth_success_count++;
1051 }
1052
1053 if (!ret && (state->auth_success_count < state->auth_method_count)) {
1054 /* success, but he needs to do another method */
1055 VRB(session, "User \"%s\" partially authenticated, but still needs to authenticate via the rest of his configured methods.", username);
1056 ssh_message_auth_reply_success(msg, 1);
1057 } else if (!ret && (state->auth_success_count == state->auth_method_count)) {
1058 /* authenticated */
1059 ssh_message_auth_reply_success(msg, 0);
1060 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
1061 VRB(session, "User \"%s\" authenticated.", username);
1062 }
1063
1064 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001065 } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko0df67562016-01-21 15:50:11 +01001066 if ((type == SSH_REQUEST_CHANNEL_OPEN) && ((enum ssh_channel_type_e)subtype == SSH_CHANNEL_SESSION)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001067 if (nc_sshcb_channel_open(session, msg)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001068 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001069 }
Michal Vasko086311b2016-01-08 09:53:11 +01001070 return 0;
Michal Vasko96164bf2016-01-21 15:41:58 +01001071
Michal Vasko0df67562016-01-21 15:50:11 +01001072 } else if ((type == SSH_REQUEST_CHANNEL) && ((enum ssh_channel_requests_e)subtype == SSH_CHANNEL_REQUEST_SUBSYSTEM)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001073 if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg),
1074 ssh_message_channel_request_subsystem(msg))) {
Michal Vasko086311b2016-01-08 09:53:11 +01001075 ssh_message_reply_default(msg);
Michal Vasko96164bf2016-01-21 15:41:58 +01001076 } else {
1077 ssh_message_channel_request_reply_success(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001078 }
1079 return 0;
1080 }
1081 }
1082
1083 /* we did not process it */
1084 return 1;
1085}
1086
Michal Vasko1a38c862016-01-15 15:50:07 +01001087/* ret 1 on success, 0 on timeout, -1 on error */
Michal Vasko086311b2016-01-08 09:53:11 +01001088static int
romanf578cd52023-10-19 09:47:40 +02001089nc_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 +01001090{
roman6ece9c52022-06-22 09:29:17 +02001091 struct timespec ts_timeout;
romanf578cd52023-10-19 09:47:40 +02001092 ssh_message msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001093
romanf578cd52023-10-19 09:47:40 +02001094 if (timeout) {
1095 nc_timeouttime_get(&ts_timeout, timeout * 1000);
Michal Vasko36c7be82017-02-22 13:37:59 +01001096 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001097 while (1) {
1098 if (!nc_session_is_connected(session)) {
romanf578cd52023-10-19 09:47:40 +02001099 ERR(session, "Communication SSH socket unexpectedly closed.");
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001100 return -1;
1101 }
1102
romanf578cd52023-10-19 09:47:40 +02001103 msg = ssh_message_get(session->ti.libssh.session);
1104 if (msg) {
1105 if (nc_session_ssh_msg(session, opts, msg, NULL)) {
1106 ssh_message_reply_default(msg);
1107 }
1108 ssh_message_free(msg);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001109 }
1110
romanf578cd52023-10-19 09:47:40 +02001111 if (session->ti.libssh.channel && session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) {
Michal Vasko1a38c862016-01-15 15:50:07 +01001112 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001113 }
1114
Michal Vasko086311b2016-01-08 09:53:11 +01001115 usleep(NC_TIMEOUT_STEP);
romanea0edaa2023-10-26 12:16:25 +02001116 if (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
roman6ece9c52022-06-22 09:29:17 +02001117 /* timeout */
1118 ERR(session, "Failed to start \"netconf\" SSH subsystem for too long, disconnecting.");
1119 break;
Michal Vasko36c7be82017-02-22 13:37:59 +01001120 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001121 }
Michal Vasko086311b2016-01-08 09:53:11 +01001122
Michal Vasko1a38c862016-01-15 15:50:07 +01001123 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001124}
1125
Michal Vaskof3c41e32022-09-09 11:22:21 +02001126/**
1127 * @brief Set hostkeys to be used for an SSH bind.
1128 *
1129 * @param[in] sbind SSH bind to use.
1130 * @param[in] hostkeys Array of hostkeys.
1131 * @param[in] hostkey_count Count of @p hostkeys.
1132 * @return 0 on success.
1133 * @return -1 on error.
1134 */
Michal Vasko4c1fb492017-01-30 14:31:07 +01001135static int
romanf578cd52023-10-19 09:47:40 +02001136nc_ssh_bind_add_hostkeys(ssh_bind sbind, struct nc_server_ssh_opts *opts, uint16_t hostkey_count)
Michal Vasko4c1fb492017-01-30 14:31:07 +01001137{
romanf578cd52023-10-19 09:47:40 +02001138 uint16_t i;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001139 char *privkey_path, *privkey_data;
Michal Vaskoddce1212019-05-24 09:58:49 +02001140 int ret;
romanf578cd52023-10-19 09:47:40 +02001141 struct nc_asymmetric_key *key = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001142
1143 for (i = 0; i < hostkey_count; ++i) {
1144 privkey_path = privkey_data = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001145
romanf578cd52023-10-19 09:47:40 +02001146 /* get the asymmetric key */
1147 if (opts->hostkeys[i].store == NC_STORE_LOCAL) {
1148 /* stored locally */
1149 key = &opts->hostkeys[i].key;
1150 } else {
1151 /* keystore reference, need to get it */
1152 if (nc_server_ssh_ks_ref_get_key(opts->hostkeys[i].ks_ref, &key)) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001153 return -1;
1154 }
1155 }
1156
romanf578cd52023-10-19 09:47:40 +02001157 privkey_path = base64der_privkey_to_tmp_file(key->privkey_data, nc_privkey_format_to_str(key->privkey_type));
1158 if (!privkey_path) {
1159 ERR(NULL, "Temporarily storing a host key into a file failed (%s).", strerror(errno));
1160 return -1;
1161 }
1162
Michal Vasko4c1fb492017-01-30 14:31:07 +01001163 ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path);
1164
1165 /* cleanup */
1166 if (privkey_data && unlink(privkey_path)) {
Michal Vasko05532772021-06-03 12:12:38 +02001167 WRN(NULL, "Removing a temporary host key file \"%s\" failed (%s).", privkey_path, strerror(errno));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001168 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001169
1170 if (ret != SSH_OK) {
romanf578cd52023-10-19 09:47:40 +02001171 ERR(NULL, "Failed to set hostkey \"%s\" (%s).", opts->hostkeys[i].name, privkey_path);
Michal Vasko80075de2017-07-10 11:38:52 +02001172 }
1173 free(privkey_path);
1174
1175 if (ret != SSH_OK) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001176 return -1;
1177 }
1178 }
1179
1180 return 0;
1181}
1182
Michal Vasko09d700f2022-09-08 10:21:40 +02001183static int
romanf578cd52023-10-19 09:47:40 +02001184nc_accept_ssh_session_auth(struct nc_session *session, struct nc_server_ssh_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001185{
roman6ece9c52022-06-22 09:29:17 +02001186 struct timespec ts_timeout;
roman41a11e42022-06-22 09:27:08 +02001187 ssh_message msg;
romanf578cd52023-10-19 09:47:40 +02001188 struct nc_auth_state state = {0};
Michal Vasko086311b2016-01-08 09:53:11 +01001189
Michal Vasko086311b2016-01-08 09:53:11 +01001190 /* authenticate */
Michal Vasko36c7be82017-02-22 13:37:59 +01001191 if (opts->auth_timeout) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001192 nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000);
Michal Vasko36c7be82017-02-22 13:37:59 +01001193 }
1194 while (1) {
Michal Vasko2a7d4732016-01-15 09:24:46 +01001195 if (!nc_session_is_connected(session)) {
Michal Vasko05532772021-06-03 12:12:38 +02001196 ERR(session, "Communication SSH socket unexpectedly closed.");
Michal Vasko2a7d4732016-01-15 09:24:46 +01001197 return -1;
1198 }
1199
roman41a11e42022-06-22 09:27:08 +02001200 msg = ssh_message_get(session->ti.libssh.session);
1201 if (msg) {
romanf578cd52023-10-19 09:47:40 +02001202 if (nc_session_ssh_msg(session, opts, msg, &state)) {
roman41a11e42022-06-22 09:27:08 +02001203 ssh_message_reply_default(msg);
1204 }
1205 ssh_message_free(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001206 }
1207
Michal Vasko36c7be82017-02-22 13:37:59 +01001208 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
1209 break;
1210 }
1211
Michal Vasko145ae672017-02-07 10:57:27 +01001212 if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) {
Michal Vasko05532772021-06-03 12:12:38 +02001213 ERR(session, "Too many failed authentication attempts of user \"%s\".", session->username);
Michal Vasko145ae672017-02-07 10:57:27 +01001214 return -1;
1215 }
1216
Michal Vasko086311b2016-01-08 09:53:11 +01001217 usleep(NC_TIMEOUT_STEP);
romanea0edaa2023-10-26 12:16:25 +02001218 if (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
roman6ece9c52022-06-22 09:29:17 +02001219 /* timeout */
1220 break;
Michal Vasko36c7be82017-02-22 13:37:59 +01001221 }
1222 }
Michal Vasko086311b2016-01-08 09:53:11 +01001223
1224 if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) {
1225 /* timeout */
Michal Vaskoc13da702017-02-07 10:57:57 +01001226 if (session->username) {
Michal Vasko05532772021-06-03 12:12:38 +02001227 ERR(session, "User \"%s\" failed to authenticate for too long, disconnecting.", session->username);
Michal Vaskoc13da702017-02-07 10:57:57 +01001228 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001229 ERR(session, "User failed to authenticate for too long, disconnecting.");
Michal Vaskoc13da702017-02-07 10:57:57 +01001230 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001231 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001232 }
1233
Michal Vasko09d700f2022-09-08 10:21:40 +02001234 return 1;
1235}
1236
1237int
romanf578cd52023-10-19 09:47:40 +02001238nc_accept_ssh_session(struct nc_session *session, struct nc_server_ssh_opts *opts, int sock, int timeout)
Michal Vasko09d700f2022-09-08 10:21:40 +02001239{
1240 ssh_bind sbind = NULL;
Michal Vasko09d700f2022-09-08 10:21:40 +02001241 int rc = 1, r;
1242 struct timespec ts_timeout;
roman4cc0cd52023-04-14 09:12:29 +02001243 const char *err_msg;
Michal Vasko09d700f2022-09-08 10:21:40 +02001244
Michal Vasko09d700f2022-09-08 10:21:40 +02001245 /* other transport-specific data */
1246 session->ti_type = NC_TI_LIBSSH;
1247 session->ti.libssh.session = ssh_new();
1248 if (!session->ti.libssh.session) {
1249 ERR(NULL, "Failed to initialize a new SSH session.");
1250 rc = -1;
1251 goto cleanup;
1252 }
1253
1254 sbind = ssh_bind_new();
1255 if (!sbind) {
1256 ERR(session, "Failed to create an SSH bind.");
1257 rc = -1;
1258 goto cleanup;
1259 }
1260
1261 /* configure host keys */
romanf578cd52023-10-19 09:47:40 +02001262 if (nc_ssh_bind_add_hostkeys(sbind, opts, opts->hostkey_count)) {
1263 rc = -1;
1264 goto cleanup;
1265 }
1266
1267 /* configure supported algorithms */
1268 if (opts->hostkey_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, opts->hostkey_algs)) {
1269 rc = -1;
1270 goto cleanup;
1271 }
1272 if (opts->encryption_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_CIPHERS_S_C, opts->encryption_algs)) {
1273 rc = -1;
1274 goto cleanup;
1275 }
1276 if (opts->kex_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_KEY_EXCHANGE, opts->kex_algs)) {
1277 rc = -1;
1278 goto cleanup;
1279 }
1280 if (opts->mac_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HMAC_S_C, opts->mac_algs)) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001281 rc = -1;
1282 goto cleanup;
1283 }
1284
1285 /* accept new connection on the bind */
1286 if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) {
1287 ERR(session, "SSH failed to accept a new connection (%s).", ssh_get_error(sbind));
1288 rc = -1;
1289 goto cleanup;
1290 }
1291 sock = -1;
1292
romanf578cd52023-10-19 09:47:40 +02001293 /* set to non-blocking */
Michal Vasko09d700f2022-09-08 10:21:40 +02001294 ssh_set_blocking(session->ti.libssh.session, 0);
1295
1296 if (timeout > -1) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001297 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko09d700f2022-09-08 10:21:40 +02001298 }
1299 while ((r = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) {
1300 /* this tends to take longer */
1301 usleep(NC_TIMEOUT_STEP * 20);
Michal Vaskod8a74192023-02-06 15:51:50 +01001302 if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001303 break;
1304 }
1305 }
1306 if (r == SSH_AGAIN) {
1307 ERR(session, "SSH key exchange timeout.");
1308 rc = 0;
1309 goto cleanup;
1310 } else if (r != SSH_OK) {
roman4cc0cd52023-04-14 09:12:29 +02001311 err_msg = ssh_get_error(session->ti.libssh.session);
1312 if (err_msg[0] == '\0') {
1313 err_msg = "hostkey algorithm generated from the hostkey most likely not found in the set of configured hostkey algorithms";
1314 }
1315 ERR(session, "SSH key exchange error (%s).", err_msg);
Michal Vasko09d700f2022-09-08 10:21:40 +02001316 rc = -1;
1317 goto cleanup;
1318 }
1319
1320 /* authenticate */
1321 if ((rc = nc_accept_ssh_session_auth(session, opts)) != 1) {
1322 goto cleanup;
1323 }
1324
Michal Vasko09d700f2022-09-08 10:21:40 +02001325 /* open channel and request 'netconf' subsystem */
romanf578cd52023-10-19 09:47:40 +02001326 if ((rc = nc_accept_ssh_session_open_netconf_channel(session, opts, timeout)) != 1) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001327 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001328 }
1329
Michal Vasko09d700f2022-09-08 10:21:40 +02001330cleanup:
1331 if (sock > -1) {
1332 close(sock);
1333 }
1334 ssh_bind_free(sbind);
1335 return rc;
Michal Vasko086311b2016-01-08 09:53:11 +01001336}
1337
Michal Vasko71090fc2016-05-24 16:37:28 +02001338API NC_MSG_TYPE
1339nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session)
1340{
1341 NC_MSG_TYPE msgtype;
1342 struct nc_session *new_session = NULL;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001343 struct timespec ts_cur;
Michal Vasko71090fc2016-05-24 16:37:28 +02001344
roman40672412023-05-04 11:10:22 +02001345 NC_CHECK_ARG_RET(orig_session, orig_session, session, NC_MSG_ERROR);
Michal Vasko71090fc2016-05-24 16:37:28 +02001346
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001347 if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) &&
1348 orig_session->ti.libssh.next) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001349 for (new_session = orig_session->ti.libssh.next;
1350 new_session != orig_session;
1351 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001352 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1353 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001354 /* we found our session */
1355 break;
1356 }
1357 }
1358 if (new_session == orig_session) {
1359 new_session = NULL;
1360 }
1361 }
1362
1363 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001364 ERR(orig_session, "Session does not have a NETCONF SSH channel ready.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001365 return NC_MSG_ERROR;
1366 }
1367
1368 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001369 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001370
1371 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001372 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001373 if (msgtype != NC_MSG_HELLO) {
1374 return msgtype;
1375 }
1376
Michal Vaskod8a74192023-02-06 15:51:50 +01001377 nc_realtime_get(&ts_cur);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001378 new_session->opts.server.session_start = ts_cur.tv_sec;
Michal Vaskod8a74192023-02-06 15:51:50 +01001379 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001380 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko71090fc2016-05-24 16:37:28 +02001381 new_session->status = NC_STATUS_RUNNING;
1382 *session = new_session;
1383
1384 return msgtype;
1385}
1386
1387API NC_MSG_TYPE
Michal Vasko96164bf2016-01-21 15:41:58 +01001388nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +01001389{
Michal Vaskobdcf2362016-07-26 11:35:43 +02001390 uint8_t q_id;
Michal Vasko71090fc2016-05-24 16:37:28 +02001391 NC_MSG_TYPE msgtype;
Michal Vaskoe4300a82017-05-24 10:35:42 +02001392 struct nc_session *new_session = NULL, *cur_session;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001393 struct timespec ts_cur;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001394 uint16_t i;
Michal Vasko086311b2016-01-08 09:53:11 +01001395
roman40672412023-05-04 11:10:22 +02001396 NC_CHECK_ARG_RET(NULL, ps, session, NC_MSG_ERROR);
Michal Vasko086311b2016-01-08 09:53:11 +01001397
Michal Vasko48a63ed2016-03-01 09:48:21 +01001398 /* LOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001399 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001400 return NC_MSG_ERROR;
Michal Vaskof04a52a2016-04-07 10:52:10 +02001401 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001402
Michal Vasko96164bf2016-01-21 15:41:58 +01001403 for (i = 0; i < ps->session_count; ++i) {
fanchanghu3d4e7212017-08-09 09:42:30 +08001404 cur_session = ps->sessions[i]->session;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001405 if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH) &&
1406 cur_session->ti.libssh.next) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001407 /* an SSH session with more channels */
Michal Vaskoe4300a82017-05-24 10:35:42 +02001408 for (new_session = cur_session->ti.libssh.next;
1409 new_session != cur_session;
Michal Vasko96164bf2016-01-21 15:41:58 +01001410 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001411 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1412 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001413 /* we found our session */
1414 break;
1415 }
1416 }
Michal Vaskoe4300a82017-05-24 10:35:42 +02001417 if (new_session != cur_session) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001418 break;
1419 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001420
Michal Vasko96164bf2016-01-21 15:41:58 +01001421 new_session = NULL;
1422 }
1423 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001424
Michal Vasko48a63ed2016-03-01 09:48:21 +01001425 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001426 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001427
Michal Vasko96164bf2016-01-21 15:41:58 +01001428 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001429 ERR(NULL, "No session with a NETCONF SSH channel ready was found.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001430 return NC_MSG_ERROR;
Michal Vasko96164bf2016-01-21 15:41:58 +01001431 }
1432
1433 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001434 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vaskofb89d772016-01-08 12:25:35 +01001435
Michal Vasko086311b2016-01-08 09:53:11 +01001436 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001437 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001438 if (msgtype != NC_MSG_HELLO) {
1439 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001440 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001441
Michal Vaskod8a74192023-02-06 15:51:50 +01001442 nc_realtime_get(&ts_cur);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001443 new_session->opts.server.session_start = ts_cur.tv_sec;
Michal Vaskod8a74192023-02-06 15:51:50 +01001444 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001445 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko086311b2016-01-08 09:53:11 +01001446 new_session->status = NC_STATUS_RUNNING;
Michal Vasko96164bf2016-01-21 15:41:58 +01001447 *session = new_session;
Michal Vasko086311b2016-01-08 09:53:11 +01001448
Michal Vasko71090fc2016-05-24 16:37:28 +02001449 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001450}