blob: d4bb528f166095c8fb317e3b9327bb69d56f45be [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
Michal Vasko95ea9ff2021-11-09 12:29:14 +01002 * @file session_server_ssh.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libnetconf2 SSH server session manipulation functions
Michal Vasko086311b2016-01-08 09:53:11 +01005 *
Michal Vasko95ea9ff2021-11-09 12:29:14 +01006 * @copyright
7 * Copyright (c) 2017 - 2021 CESNET, z.s.p.o.
Michal Vasko086311b2016-01-08 09:53:11 +01008 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01009 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010012 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010013 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010014 */
15
16#define _GNU_SOURCE
17
romanf578cd52023-10-19 09:47:40 +020018#include "config.h" /* Expose HAVE_SHADOW, HAVE_CRYPT and HAVE_LIBPAM */
apropp-molex4e903c32020-04-20 03:06:58 -040019
20#ifdef HAVE_SHADOW
21 #include <shadow.h>
22#endif
23#ifdef HAVE_CRYPT
24 #include <crypt.h>
25#endif
Michal Vasko0d81c572022-09-26 10:39:31 +020026#ifdef HAVE_LIBPAM
27 #include <security/pam_appl.h>
28#endif
apropp-molex4e903c32020-04-20 03:06:58 -040029
romanf578cd52023-10-19 09:47:40 +020030#include <arpa/inet.h>
31#include <assert.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020032#include <errno.h>
romanf578cd52023-10-19 09:47:40 +020033#include <libssh/libssh.h>
34#include <libssh/server.h>
35#include <libyang/libyang.h>
36#include <openssl/bio.h>
37#include <openssl/err.h>
38#include <openssl/evp.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020039#include <pwd.h>
romanf578cd52023-10-19 09:47:40 +020040#include <stdint.h>
Michal Vasko086311b2016-01-08 09:53:11 +010041#include <stdlib.h>
42#include <string.h>
Michal Vasko27252692017-03-21 15:34:13 +010043#include <sys/stat.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020044#include <sys/types.h>
Michal Vasko9f6275e2017-10-05 13:50:05 +020045#include <time.h>
Claus Klein22091912020-01-20 13:45:47 +010046#include <unistd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010047
Michal Vasko7a20d2e2021-05-19 16:40:23 +020048#include "compat.h"
romanf578cd52023-10-19 09:47:40 +020049#include "log_p.h"
50#include "session.h"
51#include "session_p.h"
Michal Vasko086311b2016-01-08 09:53:11 +010052
Michal Vaskob83a3fa2021-05-26 09:53:42 +020053#if !defined (HAVE_CRYPT_R)
Mislav Novakovicce9a7ef2017-08-08 13:45:52 +020054pthread_mutex_t crypt_lock = PTHREAD_MUTEX_INITIALIZER;
55#endif
56
Michal Vasko086311b2016-01-08 09:53:11 +010057extern struct nc_server_opts server_opts;
Michal Vaskob05053d2016-01-22 16:12:06 +010058
Michal Vasko4c1fb492017-01-30 14:31:07 +010059static char *
romanf578cd52023-10-19 09:47:40 +020060base64der_privkey_to_tmp_file(const char *in, const char *privkey_format)
Michal Vasko086311b2016-01-08 09:53:11 +010061{
Michal Vasko4c1fb492017-01-30 14:31:07 +010062 char path[12] = "/tmp/XXXXXX";
63 int fd, written;
romanf578cd52023-10-19 09:47:40 +020064 unsigned len;
Michal Vasko27252692017-03-21 15:34:13 +010065 mode_t umode;
Michal Vasko4c1fb492017-01-30 14:31:07 +010066 FILE *file;
67
romanf578cd52023-10-19 09:47:40 +020068 NC_CHECK_ARG_RET(NULL, in, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +010069
mekleob31878b2019-09-09 14:10:47 +020070 umode = umask(0177);
Michal Vasko4c1fb492017-01-30 14:31:07 +010071 fd = mkstemp(path);
Michal Vasko27252692017-03-21 15:34:13 +010072 umask(umode);
Michal Vasko4c1fb492017-01-30 14:31:07 +010073 if (fd == -1) {
74 return NULL;
75 }
76
Michal Vasko3964a832018-09-18 14:37:39 +020077 file = fdopen(fd, "w");
Michal Vasko4c1fb492017-01-30 14:31:07 +010078 if (!file) {
79 close(fd);
80 return NULL;
81 }
82
romanf578cd52023-10-19 09:47:40 +020083 /* write header */
84 written = fwrite("-----BEGIN ", 1, 11, file);
85 if (privkey_format) {
86 written += fwrite(privkey_format, 1, strlen(privkey_format), file);
Michal Vasko68177b72020-04-27 15:46:53 +020087 written += fwrite(" PRIVATE KEY-----\n", 1, 18, file);
Michal Vasko68177b72020-04-27 15:46:53 +020088 } else {
romanf578cd52023-10-19 09:47:40 +020089 written += fwrite("PRIVATE KEY-----\n", 1, 17, file);
90 }
Michal Vasko68177b72020-04-27 15:46:53 +020091
romanf578cd52023-10-19 09:47:40 +020092 /* write data */
93 written += fwrite(in, 1, strlen(in), file);
94
95 /* write footer */
96 written += fwrite("\n-----END ", 1, 10, file);
97 if (privkey_format) {
98 written += fwrite(privkey_format, 1, strlen(privkey_format), file);
99 written += fwrite(" PRIVATE KEY-----", 1, 17, file);
100 } else {
101 written += fwrite("PRIVATE KEY-----", 1, 16, file);
102 }
103
104 fclose(file);
105
106 /* checksum */
107 if (privkey_format) {
108 len = 11 + strlen(privkey_format) + 18 + strlen(in) + 10 + strlen(privkey_format) + 17;
109 } else {
110 len = 11 + 17 + strlen(in) + 10 + 16;
111 }
112
113 if ((unsigned)written != len) {
114 unlink(path);
115 return NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100116 }
117
118 return strdup(path);
119}
120
121static int
romanf578cd52023-10-19 09:47:40 +0200122nc_server_ssh_ks_ref_get_key(const char *referenced_name, struct nc_asymmetric_key **askey)
Michal Vasko4c1fb492017-01-30 14:31:07 +0100123{
romanf578cd52023-10-19 09:47:40 +0200124 uint16_t i;
125 struct nc_keystore *ks = &server_opts.keystore;
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100126
romanf578cd52023-10-19 09:47:40 +0200127 *askey = NULL;
Michal Vaskod45e25a2016-01-08 15:48:44 +0100128
romanf578cd52023-10-19 09:47:40 +0200129 /* lookup name */
130 for (i = 0; i < ks->asym_key_count; i++) {
131 if (!strcmp(referenced_name, ks->asym_keys[i].name)) {
132 break;
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100133 }
134 }
135
romanf578cd52023-10-19 09:47:40 +0200136 if (i == ks->asym_key_count) {
137 ERR(NULL, "Keystore entry \"%s\" not found.", referenced_name);
138 return 1;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200139 }
Michal Vasko7d255882017-02-09 13:35:08 +0100140
romanf578cd52023-10-19 09:47:40 +0200141 *askey = &ks->asym_keys[i];
142
143 /* check if the referenced public key is SubjectPublicKeyInfo */
144 if ((*askey)->pubkey_data && nc_is_pk_subject_public_key_info((*askey)->pubkey_data)) {
145 ERR(NULL, "The public key of the referenced hostkey \"%s\" is in the SubjectPublicKeyInfo format, "
146 "which is not allowed in the SSH!", referenced_name);
147 return 1;
Michal Vasko7d255882017-02-09 13:35:08 +0100148 }
Michal Vaskoe2713da2016-08-22 16:06:40 +0200149
Michal Vasko5fcc7142016-02-02 12:21:10 +0100150 return 0;
Michal Vaskob05053d2016-01-22 16:12:06 +0100151}
152
romanf578cd52023-10-19 09:47:40 +0200153static int
154nc_server_ssh_ts_ref_get_keys(const char *referenced_name, struct nc_public_key **pubkeys, uint16_t *pubkey_count)
Michal Vaskob05053d2016-01-22 16:12:06 +0100155{
romanf578cd52023-10-19 09:47:40 +0200156 uint16_t i, j;
157 struct nc_truststore *ts = &server_opts.truststore;
Michal Vasko3031aae2016-01-27 16:07:18 +0100158
romanf578cd52023-10-19 09:47:40 +0200159 *pubkeys = NULL;
160 *pubkey_count = 0;
161
162 /* lookup name */
163 for (i = 0; i < ts->pub_bag_count; i++) {
164 if (!strcmp(referenced_name, ts->pub_bags[i].name)) {
165 break;
166 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100167 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200168
romanf578cd52023-10-19 09:47:40 +0200169 if (i == ts->pub_bag_count) {
170 ERR(NULL, "Truststore entry \"%s\" not found.", referenced_name);
171 return 1;
172 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200173
romanf578cd52023-10-19 09:47:40 +0200174 /* check if any of the referenced public keys is SubjectPublicKeyInfo */
175 for (j = 0; j < ts->pub_bags[i].pubkey_count; j++) {
176 if (nc_is_pk_subject_public_key_info(ts->pub_bags[i].pubkeys[j].data)) {
177 ERR(NULL, "A public key of the referenced public key bag \"%s\" is in the SubjectPublicKeyInfo format, "
178 "which is not allowed in the SSH!", referenced_name);
179 return 1;
180 }
181 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100182
romanf578cd52023-10-19 09:47:40 +0200183 *pubkeys = ts->pub_bags[i].pubkeys;
184 *pubkey_count = ts->pub_bags[i].pubkey_count;
185 return 0;
Michal Vaskob05053d2016-01-22 16:12:06 +0100186}
187
Michal Vasko974410a2018-04-03 09:36:57 +0200188API void
189nc_server_ssh_set_passwd_auth_clb(int (*passwd_auth_clb)(const struct nc_session *session, const char *password, void *user_data),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200190 void *user_data, void (*free_user_data)(void *user_data))
Michal Vasko974410a2018-04-03 09:36:57 +0200191{
192 server_opts.passwd_auth_clb = passwd_auth_clb;
193 server_opts.passwd_auth_data = user_data;
194 server_opts.passwd_auth_data_free = free_user_data;
195}
196
bhart1bb7cdb2018-07-02 15:03:30 -0500197API void
romanf578cd52023-10-19 09:47:40 +0200198nc_server_ssh_set_interactive_auth_clb(int (*interactive_auth_clb)(const struct nc_session *session, ssh_session ssh_sess,
199 ssh_message msg, void *user_data), void *user_data, void (*free_user_data)(void *user_data))
bhart1bb7cdb2018-07-02 15:03:30 -0500200{
201 server_opts.interactive_auth_clb = interactive_auth_clb;
202 server_opts.interactive_auth_data = user_data;
203 server_opts.interactive_auth_data_free = free_user_data;
204}
Michal Vasko733c0bd2018-07-03 13:14:40 +0200205
bhart1bb7cdb2018-07-02 15:03:30 -0500206API void
207nc_server_ssh_set_pubkey_auth_clb(int (*pubkey_auth_clb)(const struct nc_session *session, ssh_key key, void *user_data),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200208 void *user_data, void (*free_user_data)(void *user_data))
bhart1bb7cdb2018-07-02 15:03:30 -0500209{
210 server_opts.pubkey_auth_clb = pubkey_auth_clb;
211 server_opts.pubkey_auth_data = user_data;
212 server_opts.pubkey_auth_data_free = free_user_data;
213}
214
Michal Vaskof3c41e32022-09-09 11:22:21 +0200215/**
216 * @brief Compare hashed password with a cleartext password for a match.
217 *
218 * @param[in] pass_hash Hashed password.
219 * @param[in] pass_clear Cleartext password.
220 * @return 0 on match.
221 * @return non-zero if not a match.
222 */
Michal Vasko086311b2016-01-08 09:53:11 +0100223static int
roman814f5112023-10-19 15:51:16 +0200224auth_password_compare_pwd(const char *stored_pw, const char *received_pw)
Michal Vasko086311b2016-01-08 09:53:11 +0100225{
roman814f5112023-10-19 15:51:16 +0200226 char *received_pw_hash = NULL;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200227
Michal Vaskof3c41e32022-09-09 11:22:21 +0200228#ifdef HAVE_CRYPT_R
Michal Vasko086311b2016-01-08 09:53:11 +0100229 struct crypt_data cdata;
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200230#endif
Michal Vasko086311b2016-01-08 09:53:11 +0100231
roman814f5112023-10-19 15:51:16 +0200232 if (!stored_pw[0]) {
233 if (!received_pw[0]) {
Michal Vasko05532772021-06-03 12:12:38 +0200234 WRN(NULL, "User authentication successful with an empty password!");
Michal Vasko086311b2016-01-08 09:53:11 +0100235 return 0;
236 } else {
237 /* the user did now know he does not need any password,
238 * (which should not be used) so deny authentication */
239 return 1;
240 }
241 }
242
roman814f5112023-10-19 15:51:16 +0200243 if (!strncmp(stored_pw, "$0$", 3)) {
244 /* cleartext password, simply compare the values */
245 return strcmp(stored_pw + 3, received_pw);
246 }
247
Michal Vaskof3c41e32022-09-09 11:22:21 +0200248#ifdef HAVE_CRYPT_R
Michal Vasko086311b2016-01-08 09:53:11 +0100249 cdata.initialized = 0;
roman814f5112023-10-19 15:51:16 +0200250 received_pw_hash = crypt_r(received_pw, stored_pw, &cdata);
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200251#else
Mislav Novakovicce9a7ef2017-08-08 13:45:52 +0200252 pthread_mutex_lock(&crypt_lock);
roman814f5112023-10-19 15:51:16 +0200253 received_pw_hash = crypt(received_pw, stored_pw);
Mislav Novakovicce9a7ef2017-08-08 13:45:52 +0200254 pthread_mutex_unlock(&crypt_lock);
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200255#endif
roman814f5112023-10-19 15:51:16 +0200256 if (!received_pw_hash) {
Andrew Langefeld158d6fd2018-06-11 18:51:44 -0500257 return 1;
258 }
259
roman814f5112023-10-19 15:51:16 +0200260 return strcmp(received_pw_hash, stored_pw);
Michal Vasko086311b2016-01-08 09:53:11 +0100261}
262
romanf578cd52023-10-19 09:47:40 +0200263static int
264nc_sshcb_auth_password(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100265{
Michal Vaskoebba7602018-03-23 13:14:08 +0100266 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100267
Michal Vaskoebba7602018-03-23 13:14:08 +0100268 if (server_opts.passwd_auth_clb) {
269 auth_ret = server_opts.passwd_auth_clb(session, ssh_message_auth_password(msg), server_opts.passwd_auth_data);
270 } else {
romanf578cd52023-10-19 09:47:40 +0200271 auth_ret = auth_password_compare_pwd(auth_client->password, ssh_message_auth_password(msg));
Michal Vasko086311b2016-01-08 09:53:11 +0100272 }
273
romanf578cd52023-10-19 09:47:40 +0200274 if (auth_ret) {
Michal Vaskoebba7602018-03-23 13:14:08 +0100275 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200276 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
277 session->opts.server.ssh_auth_attempts);
Michal Vaskoebba7602018-03-23 13:14:08 +0100278 ssh_message_reply_default(msg);
279 }
romanf578cd52023-10-19 09:47:40 +0200280
281 return auth_ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100282}
283
Michal Vasko0d81c572022-09-26 10:39:31 +0200284#ifdef HAVE_LIBPAM
285
roman41a11e42022-06-22 09:27:08 +0200286/**
287 * @brief PAM conversation function, which serves as a callback for exchanging messages between the client and a PAM module.
288 *
289 * @param[in] n_messages Number of messages.
290 * @param[in] msg PAM module's messages.
291 * @param[out] resp User responses.
292 * @param[in] appdata_ptr Callback's data.
293 * @return PAM_SUCCESS on success;
294 * @return PAM_BUF_ERR on memory allocation error;
295 * @return PAM_CONV_ERR otherwise.
296 */
297static int
298nc_pam_conv_clb(int n_messages, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)
299{
300 int i, j, t, r = PAM_SUCCESS, n_answers, n_requests = n_messages;
301 const char **prompts = NULL;
302 char *echo = NULL;
303 const char *name = "Keyboard-Interactive Authentication";
304 const char *instruction = "Please enter your authentication token";
305 ssh_message reply = NULL;
306 struct nc_pam_thread_arg *clb_data = appdata_ptr;
307 ssh_session libssh_session;
308 struct timespec ts_timeout;
309 struct nc_server_ssh_opts *opts;
310
311 libssh_session = clb_data->session->ti.libssh.session;
romanf578cd52023-10-19 09:47:40 +0200312 opts = clb_data->opts;
roman41a11e42022-06-22 09:27:08 +0200313
314 /* PAM_MAX_NUM_MSG == 32 by default */
315 if ((n_messages <= 0) || (n_messages >= PAM_MAX_NUM_MSG)) {
316 ERR(NULL, "Bad number of PAM messages (#%d).", n_messages);
317 r = PAM_CONV_ERR;
318 goto cleanup;
319 }
320
321 /* only accepting these 4 types of messages */
322 for (i = 0; i < n_messages; i++) {
323 t = msg[i]->msg_style;
324 if ((t != PAM_PROMPT_ECHO_OFF) && (t != PAM_PROMPT_ECHO_ON) && (t != PAM_TEXT_INFO) && (t != PAM_ERROR_MSG)) {
325 ERR(NULL, "PAM conversation callback received an unexpected type of message.");
326 r = PAM_CONV_ERR;
327 goto cleanup;
328 }
329 }
330
331 /* display messages with errors and/or some information and count the amount of actual authentication challenges */
332 for (i = 0; i < n_messages; i++) {
333 if (msg[i]->msg_style == PAM_TEXT_INFO) {
334 VRB(NULL, "PAM conversation callback received a message with some information for the client (%s).", msg[i]->msg);
335 n_requests--;
336 }
337 if (msg[i]->msg_style == PAM_ERROR_MSG) {
338 ERR(NULL, "PAM conversation callback received an error message (%s).", msg[i]->msg);
339 r = PAM_CONV_ERR;
340 goto cleanup;
341 }
342 }
343
344 /* there are no requests left for the user, only messages with some information for the client were sent */
345 if (n_requests <= 0) {
346 r = PAM_SUCCESS;
347 goto cleanup;
348 }
349
350 /* it is the PAM module's responsibility to release both, this array and the responses themselves */
351 *resp = calloc(n_requests, sizeof **resp);
352 prompts = calloc(n_requests, sizeof *prompts);
353 echo = calloc(n_requests, sizeof *echo);
354 if (!(*resp) || !prompts || !echo) {
355 ERRMEM;
356 r = PAM_BUF_ERR;
357 goto cleanup;
358 }
359
360 /* set the prompts for the user */
361 j = 0;
362 for (i = 0; i < n_messages; i++) {
363 if ((msg[i]->msg_style == PAM_PROMPT_ECHO_ON) || (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF)) {
364 prompts[j++] = msg[i]->msg;
365 }
366 }
367
368 /* iterate over all the messages and adjust the echo array accordingly */
369 j = 0;
370 for (i = 0; i < n_messages; i++) {
371 if (msg[i]->msg_style == PAM_PROMPT_ECHO_ON) {
372 echo[j++] = 1;
373 }
374 if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF) {
375 /* no need to set to 0 because of calloc */
376 j++;
377 }
378 }
379
380 /* print all the keyboard-interactive challenges to the user */
381 r = ssh_message_auth_interactive_request(clb_data->msg, name, instruction, n_requests, prompts, echo);
382 if (r != SSH_OK) {
383 ERR(NULL, "Failed to send an authentication request.");
384 r = PAM_CONV_ERR;
385 goto cleanup;
386 }
387
388 if (opts->auth_timeout) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100389 nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000);
roman41a11e42022-06-22 09:27:08 +0200390 }
391
392 /* get user's replies */
393 do {
394 if (!nc_session_is_connected(clb_data->session)) {
395 ERR(NULL, "Communication SSH socket unexpectedly closed.");
396 r = PAM_CONV_ERR;
397 goto cleanup;
398 }
399
400 reply = ssh_message_get(libssh_session);
401 if (reply) {
402 break;
403 }
404
405 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +0100406 } while ((opts->auth_timeout) && (nc_timeouttime_cur_diff(&ts_timeout) >= 1));
roman41a11e42022-06-22 09:27:08 +0200407
408 if (!reply) {
409 ERR(NULL, "Authentication timeout.");
410 r = PAM_CONV_ERR;
411 goto cleanup;
412 }
413
414 /* check if the amount of replies matches the amount of requests */
415 n_answers = ssh_userauth_kbdint_getnanswers(libssh_session);
416 if (n_answers != n_requests) {
417 ERR(NULL, "Expected %d response(s), got %d.", n_requests, n_answers);
418 r = PAM_CONV_ERR;
419 goto cleanup;
420 }
421
422 /* give the replies to a PAM module */
423 for (i = 0; i < n_answers; i++) {
424 (*resp)[i].resp = strdup(ssh_userauth_kbdint_getanswer(libssh_session, i));
425 /* it should be the caller's responsibility to free this, however if mem alloc fails,
426 * it is safer to free the responses here and set them to NULL */
427 if ((*resp)[i].resp == NULL) {
428 for (j = 0; j < i; j++) {
429 free((*resp)[j].resp);
430 (*resp)[j].resp = NULL;
431 }
432 ERRMEM;
433 r = PAM_BUF_ERR;
434 goto cleanup;
435 }
436 }
437
438cleanup:
439 ssh_message_free(reply);
440 free(prompts);
441 free(echo);
442 return r;
443}
444
445/**
446 * @brief Handles authentication via Linux PAM.
447 *
448 * @param[in] session NETCONF session.
449 * @param[in] ssh_msg SSH message with a keyboard-interactive authentication request.
450 * @return PAM_SUCCESS on success;
451 * @return PAM error otherwise.
452 */
453static int
romanf578cd52023-10-19 09:47:40 +0200454nc_pam_auth(struct nc_session *session, struct nc_server_ssh_opts *opts, ssh_message ssh_msg)
roman41a11e42022-06-22 09:27:08 +0200455{
456 pam_handle_t *pam_h = NULL;
457 int ret;
458 struct nc_pam_thread_arg clb_data;
459 struct pam_conv conv;
romanf578cd52023-10-19 09:47:40 +0200460 uint16_t i;
roman41a11e42022-06-22 09:27:08 +0200461
462 /* structure holding callback's data */
463 clb_data.msg = ssh_msg;
464 clb_data.session = session;
romanf578cd52023-10-19 09:47:40 +0200465 clb_data.opts = opts;
roman41a11e42022-06-22 09:27:08 +0200466
467 /* PAM conversation structure holding the callback and it's data */
468 conv.conv = nc_pam_conv_clb;
469 conv.appdata_ptr = &clb_data;
470
romanf578cd52023-10-19 09:47:40 +0200471 /* get the current client's configuration file */
472 for (i = 0; i < opts->client_count; i++) {
473 if (!strcmp(opts->auth_clients[i].username, session->username)) {
474 break;
475 }
476 }
477
478 if (i == opts->client_count) {
479 ERR(NULL, "User \"%s\" not found.", session->username);
480 ret = 1;
481 goto cleanup;
482 }
483
484 if (!opts->auth_clients[i].pam_config_name) {
485 ERR(NULL, "User's \"%s\" PAM configuration filename not set.");
486 ret = 1;
487 goto cleanup;
488 }
489
roman41a11e42022-06-22 09:27:08 +0200490 /* initialize PAM and see if the given configuration file exists */
Michal Vasko0d81c572022-09-26 10:39:31 +0200491# ifdef LIBPAM_HAVE_CONFDIR
roman41a11e42022-06-22 09:27:08 +0200492 /* PAM version >= 1.4 */
romanf578cd52023-10-19 09:47:40 +0200493 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 +0200494# else
roman41a11e42022-06-22 09:27:08 +0200495 /* PAM version < 1.4 */
romanf578cd52023-10-19 09:47:40 +0200496 ret = pam_start(opts->auth_clients[i].pam_config_name, session->username, &conv, &pam_h);
Michal Vasko0d81c572022-09-26 10:39:31 +0200497# endif
roman41a11e42022-06-22 09:27:08 +0200498 if (ret != PAM_SUCCESS) {
499 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
500 goto cleanup;
501 }
502
503 /* authentication based on the modules listed in the configuration file */
504 ret = pam_authenticate(pam_h, 0);
505 if (ret != PAM_SUCCESS) {
506 if (ret == PAM_ABORT) {
507 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
508 goto cleanup;
509 } else {
510 VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
511 goto cleanup;
512 }
513 }
514
515 /* correct token entered, check other requirements(the time of the day, expired token, ...) */
516 ret = pam_acct_mgmt(pam_h, 0);
517 if ((ret != PAM_SUCCESS) && (ret != PAM_NEW_AUTHTOK_REQD)) {
518 VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
519 goto cleanup;
520 }
521
522 /* if a token has expired a new one will be generated */
523 if (ret == PAM_NEW_AUTHTOK_REQD) {
524 VRB(NULL, "PAM warning occurred (%s).\n", pam_strerror(pam_h, ret));
525 ret = pam_chauthtok(pam_h, PAM_CHANGE_EXPIRED_AUTHTOK);
526 if (ret == PAM_SUCCESS) {
527 VRB(NULL, "The authentication token of user \"%s\" updated successfully.", session->username);
528 } else {
529 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
530 goto cleanup;
531 }
532 }
533
534cleanup:
535 /* destroy the PAM context */
536 if (pam_end(pam_h, ret) != PAM_SUCCESS) {
537 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
538 }
539 return ret;
540}
541
Michal Vasko0d81c572022-09-26 10:39:31 +0200542#endif
543
romanf578cd52023-10-19 09:47:40 +0200544static int
545nc_sshcb_auth_kbdint(struct nc_session *session, struct nc_server_ssh_opts *opts, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100546{
bhart3bc2f582018-06-05 12:40:32 -0500547 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100548
romanf578cd52023-10-19 09:47:40 +0200549 if (server_opts.interactive_auth_clb) {
550 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 +0200551 } else {
552#ifdef HAVE_LIBPAM
romanf578cd52023-10-19 09:47:40 +0200553 if (nc_pam_auth(session, opts, msg) == PAM_SUCCESS) {
Michal Vasko0d81c572022-09-26 10:39:31 +0200554 auth_ret = 0;
555 }
556#else
557 ERR(session, "PAM-based SSH authentication is not supported.");
558#endif
bhart1bb7cdb2018-07-02 15:03:30 -0500559 }
560
561 /* Authenticate message based on outcome */
romanf578cd52023-10-19 09:47:40 +0200562 if (auth_ret) {
bhart1bb7cdb2018-07-02 15:03:30 -0500563 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200564 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
565 session->opts.server.ssh_auth_attempts);
bhart1bb7cdb2018-07-02 15:03:30 -0500566 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +0100567 }
romanf578cd52023-10-19 09:47:40 +0200568
569 return auth_ret;
570}
571
572/*
573 * Get the public key type from binary data stored in buffer.
574 * The data is in the form of: 4 bytes = data length, then data of data length
575 * and the data is in network byte order. The key has to be in the SSH2 format.
576 */
577static const char *
578nc_server_ssh_get_pubkey_type(const char *buffer, uint32_t *len)
579{
580 uint32_t type_len;
581
582 /* copy the 4 bytes */
583 memcpy(&type_len, buffer, sizeof type_len);
584 /* type_len now stores the length of the key type */
585 type_len = ntohl(type_len);
586 *len = type_len;
587
588 /* move 4 bytes in the buffer, this is where the type should be */
589 buffer += sizeof type_len;
590 return buffer;
591}
592
593/**
594 * @brief Create ssh key from base64 pubkey data.
595 *
596 * @param[in] base64 base64 encoded public key.
597 * @param[out] key created ssh key.
598 * @return 0 on success, 1 otherwise.
599 */
600static int
601nc_server_ssh_create_ssh_pubkey(const char *base64, ssh_key *key)
602{
603 int ret = 0;
604 char *bin = NULL;
605 const char *pub_type = NULL;
606 uint32_t pub_type_len = 0;
607
608 if (!key && !base64) {
609 ERRINT;
610 ret = 1;
611 goto cleanup;
612 }
613
614 *key = NULL;
615
616 /* convert base64 to binary */
617 if (nc_base64_to_bin(base64, &bin) == -1) {
618 ERR(NULL, "Unable to decode base64.");
619 ret = 1;
620 goto cleanup;
621 }
622
623 /* get the key type and try to import it if possible */
624 pub_type = nc_server_ssh_get_pubkey_type(bin, &pub_type_len);
625 if (!pub_type) {
626 ret = 1;
627 goto cleanup;
628 } else if (!strncmp(pub_type, "ssh-dss", pub_type_len)) {
629 ERR(NULL, "DSA keys are not supported.");
630 ret = 1;
631 goto cleanup;
632 } else if (!strncmp(pub_type, "ssh-rsa", pub_type_len)) {
633 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_RSA, key);
634 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp256", pub_type_len)) {
635 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P256, key);
636 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp384", pub_type_len)) {
637 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P384, key);
638 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp521", pub_type_len)) {
639 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P521, key);
640 } else if (!strncmp(pub_type, "ssh-ed25519", pub_type_len)) {
641 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ED25519, key);
642 } else {
643 ERR(NULL, "Public key type not recognised.");
644 ret = 1;
645 goto cleanup;
646 }
647
648cleanup:
649 if (ret != SSH_OK) {
650 ERR(NULL, "Error importing public key.");
651 }
652 free(bin);
653 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100654}
655
Michal Vaskof3c41e32022-09-09 11:22:21 +0200656/**
657 * @brief Compare SSH key with configured authorized keys and return the username of the matching one, if any.
658 *
659 * @param[in] key Presented SSH key to compare.
660 * @return Authorized key username, NULL if no match was found.
661 */
romanf578cd52023-10-19 09:47:40 +0200662static int
663auth_pubkey_compare_key(ssh_key key, struct nc_auth_client *auth_client)
Michal Vasko086311b2016-01-08 09:53:11 +0100664{
romanf578cd52023-10-19 09:47:40 +0200665 uint16_t i, pubkey_count;
Michal Vasko3e9d1682017-02-24 09:50:15 +0100666 int ret = 0;
romanf578cd52023-10-19 09:47:40 +0200667 ssh_key new_key = NULL;
668 struct nc_public_key *pubkeys;
Michal Vasko086311b2016-01-08 09:53:11 +0100669
romanf578cd52023-10-19 09:47:40 +0200670 /* get the correct public key storage */
671 if (auth_client->store == NC_STORE_LOCAL) {
672 pubkeys = auth_client->pubkeys;
673 pubkey_count = auth_client->pubkey_count;
674 } else {
675 ret = nc_server_ssh_ts_ref_get_keys(auth_client->ts_ref, &pubkeys, &pubkey_count);
676 if (ret) {
677 ERR(NULL, "Error getting \"%s\"'s public keys from the truststore.", auth_client->username);
678 return ret;
Michal Vasko17dfda92016-12-01 14:06:16 +0100679 }
romanf578cd52023-10-19 09:47:40 +0200680 }
Michal Vasko17dfda92016-12-01 14:06:16 +0100681
romanf578cd52023-10-19 09:47:40 +0200682 /* try to compare all of the client's keys with the key received in the SSH message */
683 for (i = 0; i < pubkey_count; i++) {
684 /* create the SSH key from the data */
685 if (nc_server_ssh_create_ssh_pubkey(pubkeys[i].data, &new_key)) {
686 ssh_key_free(new_key);
Michal Vasko086311b2016-01-08 09:53:11 +0100687 continue;
688 }
689
romanf578cd52023-10-19 09:47:40 +0200690 /* compare the keys */
691 ret = ssh_key_cmp(key, new_key, SSH_KEY_CMP_PUBLIC);
692 if (!ret) {
Michal Vasko086311b2016-01-08 09:53:11 +0100693 break;
romanf578cd52023-10-19 09:47:40 +0200694 } else {
695 WRN(NULL, "User's \"%s\" public key doesn't match, trying another.", auth_client->username);
696 ssh_key_free(new_key);
Michal Vasko086311b2016-01-08 09:53:11 +0100697 }
Michal Vasko086311b2016-01-08 09:53:11 +0100698 }
699
romanf578cd52023-10-19 09:47:40 +0200700 if (i == pubkey_count) {
701 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100702 }
703
romanf578cd52023-10-19 09:47:40 +0200704 if (!ret) {
705 /* only free a key if everything was ok, it would have already been freed otherwise */
706 ssh_key_free(new_key);
707 }
Michal Vaskoa05c7b12017-01-30 14:33:08 +0100708
romanf578cd52023-10-19 09:47:40 +0200709 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100710}
711
712static void
romanf578cd52023-10-19 09:47:40 +0200713nc_sshcb_auth_none(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100714{
romanf578cd52023-10-19 09:47:40 +0200715 if (auth_client->supports_none && !auth_client->password && !auth_client->pubkey_count && !auth_client->pam_config_name) {
716 /* only authenticate the client if he supports none and no other method */
717 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
718 VRB(session, "User \"%s\" authenticated.", session->username);
719 ssh_message_auth_reply_success(msg, 0);
720 }
721
722 ssh_message_reply_default(msg);
723}
724
725static int
726nc_sshcb_auth_pubkey(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
727{
728 int signature_state, ret = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100729
Michal Vasko733c0bd2018-07-03 13:14:40 +0200730 if (server_opts.pubkey_auth_clb) {
731 if (server_opts.pubkey_auth_clb(session, ssh_message_auth_pubkey(msg), server_opts.pubkey_auth_data)) {
romanf578cd52023-10-19 09:47:40 +0200732 ret = 1;
bhart3bc2f582018-06-05 12:40:32 -0500733 goto fail;
734 }
Michal Vasko733c0bd2018-07-03 13:14:40 +0200735 } else {
romanf578cd52023-10-19 09:47:40 +0200736 if (auth_pubkey_compare_key(ssh_message_auth_pubkey(msg), auth_client)) {
Michal Vasko05532772021-06-03 12:12:38 +0200737 VRB(session, "User \"%s\" tried to use an unknown (unauthorized) public key.", session->username);
romanf578cd52023-10-19 09:47:40 +0200738 ret = 1;
bhart3bc2f582018-06-05 12:40:32 -0500739 goto fail;
740 }
Michal Vaskobd13a932016-09-14 09:00:35 +0200741 }
Michal Vaskobd13a932016-09-14 09:00:35 +0200742
Michal Vasko086311b2016-01-08 09:53:11 +0100743 signature_state = ssh_message_auth_publickey_state(msg);
romanf578cd52023-10-19 09:47:40 +0200744 if (signature_state == SSH_PUBLICKEY_STATE_NONE) {
Michal Vaskobd13a932016-09-14 09:00:35 +0200745 /* accepting only the use of a public key */
746 ssh_message_auth_reply_pk_ok_simple(msg);
romanf578cd52023-10-19 09:47:40 +0200747 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100748 }
749
romanf578cd52023-10-19 09:47:40 +0200750 return ret;
Michal Vaskobd13a932016-09-14 09:00:35 +0200751
752fail:
Michal Vasko2e6defd2016-10-07 15:48:15 +0200753 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200754 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
755 session->opts.server.ssh_auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +0100756 ssh_message_reply_default(msg);
romanf578cd52023-10-19 09:47:40 +0200757
758 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100759}
760
761static int
Michal Vasko96164bf2016-01-21 15:41:58 +0100762nc_sshcb_channel_open(struct nc_session *session, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100763{
Michal Vasko96164bf2016-01-21 15:41:58 +0100764 ssh_channel chan;
765
766 /* first channel request */
767 if (!session->ti.libssh.channel) {
768 if (session->status != NC_STATUS_STARTING) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100769 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100770 return -1;
771 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100772 chan = ssh_message_channel_request_open_reply_accept(msg);
773 if (!chan) {
Michal Vasko05532772021-06-03 12:12:38 +0200774 ERR(session, "Failed to create a new SSH channel.");
Michal Vasko96164bf2016-01-21 15:41:58 +0100775 return -1;
776 }
777 session->ti.libssh.channel = chan;
Michal Vasko086311b2016-01-08 09:53:11 +0100778
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200779 /* additional channel request */
Michal Vasko96164bf2016-01-21 15:41:58 +0100780 } else {
781 chan = ssh_message_channel_request_open_reply_accept(msg);
782 if (!chan) {
Michal Vasko05532772021-06-03 12:12:38 +0200783 ERR(session, "Session %u: failed to create a new SSH channel.", session->id);
Michal Vasko96164bf2016-01-21 15:41:58 +0100784 return -1;
785 }
786 /* channel was created and libssh stored it internally in the ssh_session structure, good enough */
Michal Vasko086311b2016-01-08 09:53:11 +0100787 }
788
Michal Vasko086311b2016-01-08 09:53:11 +0100789 return 0;
790}
791
792static int
793nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem)
794{
Michal Vasko96164bf2016-01-21 15:41:58 +0100795 struct nc_session *new_session;
Michal Vasko086311b2016-01-08 09:53:11 +0100796
Michal Vasko96164bf2016-01-21 15:41:58 +0100797 if (strcmp(subsystem, "netconf")) {
Michal Vasko05532772021-06-03 12:12:38 +0200798 WRN(session, "Received an unknown subsystem \"%s\" request.", subsystem);
Michal Vasko086311b2016-01-08 09:53:11 +0100799 return -1;
800 }
801
Michal Vasko96164bf2016-01-21 15:41:58 +0100802 if (session->ti.libssh.channel == channel) {
803 /* first channel requested */
804 if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) {
805 ERRINT;
806 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100807 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100808 if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) {
Michal Vasko05532772021-06-03 12:12:38 +0200809 ERR(session, "Subsystem \"netconf\" requested for the second time.");
Michal Vasko96164bf2016-01-21 15:41:58 +0100810 return -1;
811 }
812
813 session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF;
Michal Vasko086311b2016-01-08 09:53:11 +0100814 } else {
Michal Vasko96164bf2016-01-21 15:41:58 +0100815 /* additional channel subsystem request, new session is ready as far as SSH is concerned */
Michal Vasko131120a2018-05-29 15:44:02 +0200816 new_session = nc_new_session(NC_SERVER, 1);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100817 if (!new_session) {
818 ERRMEM;
819 return -1;
820 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100821
822 /* insert the new session */
823 if (!session->ti.libssh.next) {
824 new_session->ti.libssh.next = session;
825 } else {
826 new_session->ti.libssh.next = session->ti.libssh.next;
827 }
828 session->ti.libssh.next = new_session;
829
830 new_session->status = NC_STATUS_STARTING;
Michal Vasko96164bf2016-01-21 15:41:58 +0100831 new_session->ti_type = NC_TI_LIBSSH;
Michal Vasko131120a2018-05-29 15:44:02 +0200832 new_session->io_lock = session->io_lock;
Michal Vasko96164bf2016-01-21 15:41:58 +0100833 new_session->ti.libssh.channel = channel;
834 new_session->ti.libssh.session = session->ti.libssh.session;
Michal Vasko93224072021-11-09 12:14:28 +0100835 new_session->username = strdup(session->username);
836 new_session->host = strdup(session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100837 new_session->port = session->port;
Michal Vasko93224072021-11-09 12:14:28 +0100838 new_session->ctx = (struct ly_ctx *)session->ctx;
Michal Vasko83d15322018-09-27 09:44:02 +0200839 new_session->flags = NC_SESSION_SSH_AUTHENTICATED | NC_SESSION_SSH_SUBSYS_NETCONF | NC_SESSION_SHAREDCTX;
Michal Vasko086311b2016-01-08 09:53:11 +0100840 }
841
842 return 0;
843}
844
Michal Vasko96164bf2016-01-21 15:41:58 +0100845int
romanf578cd52023-10-19 09:47:40 +0200846nc_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 +0100847{
848 const char *str_type, *str_subtype = NULL, *username;
romanf578cd52023-10-19 09:47:40 +0200849 int subtype, type, libssh_auth_methods = 0, ret = 0;
850 uint16_t i;
851 struct nc_auth_client *auth_client = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100852
853 type = ssh_message_type(msg);
854 subtype = ssh_message_subtype(msg);
855
856 switch (type) {
857 case SSH_REQUEST_AUTH:
858 str_type = "request-auth";
859 switch (subtype) {
860 case SSH_AUTH_METHOD_NONE:
861 str_subtype = "none";
862 break;
863 case SSH_AUTH_METHOD_PASSWORD:
864 str_subtype = "password";
865 break;
866 case SSH_AUTH_METHOD_PUBLICKEY:
867 str_subtype = "publickey";
868 break;
869 case SSH_AUTH_METHOD_HOSTBASED:
870 str_subtype = "hostbased";
871 break;
872 case SSH_AUTH_METHOD_INTERACTIVE:
873 str_subtype = "interactive";
874 break;
875 case SSH_AUTH_METHOD_GSSAPI_MIC:
876 str_subtype = "gssapi-mic";
877 break;
878 }
879 break;
880
881 case SSH_REQUEST_CHANNEL_OPEN:
882 str_type = "request-channel-open";
883 switch (subtype) {
884 case SSH_CHANNEL_SESSION:
885 str_subtype = "session";
886 break;
887 case SSH_CHANNEL_DIRECT_TCPIP:
888 str_subtype = "direct-tcpip";
889 break;
890 case SSH_CHANNEL_FORWARDED_TCPIP:
891 str_subtype = "forwarded-tcpip";
892 break;
893 case (int)SSH_CHANNEL_X11:
894 str_subtype = "channel-x11";
895 break;
896 case SSH_CHANNEL_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200897 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100898 default:
899 str_subtype = "unknown";
900 break;
901 }
902 break;
903
904 case SSH_REQUEST_CHANNEL:
905 str_type = "request-channel";
906 switch (subtype) {
907 case SSH_CHANNEL_REQUEST_PTY:
908 str_subtype = "pty";
909 break;
910 case SSH_CHANNEL_REQUEST_EXEC:
911 str_subtype = "exec";
912 break;
913 case SSH_CHANNEL_REQUEST_SHELL:
914 str_subtype = "shell";
915 break;
916 case SSH_CHANNEL_REQUEST_ENV:
917 str_subtype = "env";
918 break;
919 case SSH_CHANNEL_REQUEST_SUBSYSTEM:
920 str_subtype = "subsystem";
921 break;
922 case SSH_CHANNEL_REQUEST_WINDOW_CHANGE:
923 str_subtype = "window-change";
924 break;
925 case SSH_CHANNEL_REQUEST_X11:
926 str_subtype = "x11";
927 break;
928 case SSH_CHANNEL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200929 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100930 default:
931 str_subtype = "unknown";
932 break;
933 }
934 break;
935
936 case SSH_REQUEST_SERVICE:
937 str_type = "request-service";
938 str_subtype = ssh_message_service_service(msg);
939 break;
940
941 case SSH_REQUEST_GLOBAL:
942 str_type = "request-global";
943 switch (subtype) {
944 case SSH_GLOBAL_REQUEST_TCPIP_FORWARD:
945 str_subtype = "tcpip-forward";
946 break;
947 case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD:
948 str_subtype = "cancel-tcpip-forward";
949 break;
950 case SSH_GLOBAL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200951 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100952 default:
953 str_subtype = "unknown";
954 break;
955 }
956 break;
957
958 default:
959 str_type = "unknown";
960 str_subtype = "unknown";
961 break;
962 }
963
Michal Vasko05532772021-06-03 12:12:38 +0200964 VRB(session, "Received an SSH message \"%s\" of subtype \"%s\".", str_type, str_subtype);
Michal Vasko5e0edd82020-07-29 15:26:13 +0200965 if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) {
Michal Vaskoce319162016-02-03 15:33:08 +0100966 /* "valid" situation if, for example, receiving some auth or channel request timeouted,
967 * but we got it now, during session free */
Michal Vasko05532772021-06-03 12:12:38 +0200968 VRB(session, "SSH message arrived on a %s session, the request will be denied.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200969 (session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid"));
Michal Vaskoce319162016-02-03 15:33:08 +0100970 ssh_message_reply_default(msg);
971 return 0;
972 }
Michal Vasko086311b2016-01-08 09:53:11 +0100973
974 /*
975 * process known messages
976 */
977 if (type == SSH_REQUEST_AUTH) {
978 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko05532772021-06-03 12:12:38 +0200979 ERR(session, "User \"%s\" authenticated, but requested another authentication.", session->username);
Michal Vasko086311b2016-01-08 09:53:11 +0100980 ssh_message_reply_default(msg);
981 return 0;
romanf578cd52023-10-19 09:47:40 +0200982 } else if (!state || !opts) {
983 /* these two parameters should always be set during an authentication,
984 * however do a check just in case something goes really wrong, since they
985 * are not needed for other types of messages
986 */
987 ERRINT;
988 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100989 }
990
Michal Vasko086311b2016-01-08 09:53:11 +0100991 /* save the username, do not let the client change it */
992 username = ssh_message_auth_user(msg);
romanf578cd52023-10-19 09:47:40 +0200993 assert(username);
994
995 for (i = 0; i < opts->client_count; i++) {
996 if (!strcmp(opts->auth_clients[i].username, username)) {
997 auth_client = &opts->auth_clients[i];
998 break;
999 }
1000 }
1001
1002 if (!auth_client) {
1003 if (opts->endpt_client_ref) {
1004 return nc_session_ssh_msg(session, opts->endpt_client_ref->opts.ssh, msg, state);
Michal Vasko086311b2016-01-08 09:53:11 +01001005 }
1006
romanf578cd52023-10-19 09:47:40 +02001007 ERR(NULL, "User \"%s\" not known by the server.", username);
1008 ssh_message_reply_default(msg);
1009 return 0;
1010 }
1011
1012 if (!session->username) {
Michal Vasko93224072021-11-09 12:14:28 +01001013 session->username = strdup(username);
romanf578cd52023-10-19 09:47:40 +02001014
1015 /* configure and count accepted auth methods */
1016 if (auth_client->store == NC_STORE_LOCAL) {
1017 if (auth_client->pubkey_count) {
1018 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
1019 }
1020 } else if (auth_client->ts_ref) {
1021 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
1022 }
1023 if (auth_client->password) {
1024 state->auth_method_count++;
1025 libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD;
1026 }
1027 if (auth_client->pam_config_name) {
1028 state->auth_method_count++;
1029 libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE;
1030 }
1031 if (auth_client->supports_none) {
1032 libssh_auth_methods |= SSH_AUTH_METHOD_NONE;
1033 }
1034
1035 if (libssh_auth_methods & SSH_AUTH_METHOD_PUBLICKEY) {
1036 state->auth_method_count++;
1037 }
1038
1039 ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods);
1040 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001041 if (strcmp(username, session->username)) {
romanf578cd52023-10-19 09:47:40 +02001042 /* changing username not allowed */
Michal Vasko05532772021-06-03 12:12:38 +02001043 ERR(session, "User \"%s\" changed its username to \"%s\".", session->username, username);
Michal Vasko086311b2016-01-08 09:53:11 +01001044 session->status = NC_STATUS_INVALID;
Michal Vasko428087d2016-01-14 16:04:28 +01001045 session->term_reason = NC_SESSION_TERM_OTHER;
Michal Vasko086311b2016-01-08 09:53:11 +01001046 return 1;
1047 }
1048 }
1049
romanf578cd52023-10-19 09:47:40 +02001050 /* try authenticating, the user must authenticate via all of his configured auth methods */
Michal Vasko086311b2016-01-08 09:53:11 +01001051 if (subtype == SSH_AUTH_METHOD_NONE) {
romanf578cd52023-10-19 09:47:40 +02001052 nc_sshcb_auth_none(session, auth_client, msg);
1053 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001054 } else if (subtype == SSH_AUTH_METHOD_PASSWORD) {
romanf578cd52023-10-19 09:47:40 +02001055 ret = nc_sshcb_auth_password(session, auth_client, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001056 } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) {
romanf578cd52023-10-19 09:47:40 +02001057 ret = nc_sshcb_auth_pubkey(session, auth_client, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001058 } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) {
romanf578cd52023-10-19 09:47:40 +02001059 ret = nc_sshcb_auth_kbdint(session, opts, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001060 }
romanf578cd52023-10-19 09:47:40 +02001061
1062 if (!ret) {
1063 state->auth_success_count++;
1064 }
1065
1066 if (!ret && (state->auth_success_count < state->auth_method_count)) {
1067 /* success, but he needs to do another method */
1068 VRB(session, "User \"%s\" partially authenticated, but still needs to authenticate via the rest of his configured methods.", username);
1069 ssh_message_auth_reply_success(msg, 1);
1070 } else if (!ret && (state->auth_success_count == state->auth_method_count)) {
1071 /* authenticated */
1072 ssh_message_auth_reply_success(msg, 0);
1073 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
1074 VRB(session, "User \"%s\" authenticated.", username);
1075 }
1076
1077 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001078 } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko0df67562016-01-21 15:50:11 +01001079 if ((type == SSH_REQUEST_CHANNEL_OPEN) && ((enum ssh_channel_type_e)subtype == SSH_CHANNEL_SESSION)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001080 if (nc_sshcb_channel_open(session, msg)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001081 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001082 }
Michal Vasko086311b2016-01-08 09:53:11 +01001083 return 0;
Michal Vasko96164bf2016-01-21 15:41:58 +01001084
Michal Vasko0df67562016-01-21 15:50:11 +01001085 } else if ((type == SSH_REQUEST_CHANNEL) && ((enum ssh_channel_requests_e)subtype == SSH_CHANNEL_REQUEST_SUBSYSTEM)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001086 if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg),
1087 ssh_message_channel_request_subsystem(msg))) {
Michal Vasko086311b2016-01-08 09:53:11 +01001088 ssh_message_reply_default(msg);
Michal Vasko96164bf2016-01-21 15:41:58 +01001089 } else {
1090 ssh_message_channel_request_reply_success(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001091 }
1092 return 0;
1093 }
1094 }
1095
1096 /* we did not process it */
1097 return 1;
1098}
1099
Michal Vasko1a38c862016-01-15 15:50:07 +01001100/* ret 1 on success, 0 on timeout, -1 on error */
Michal Vasko086311b2016-01-08 09:53:11 +01001101static int
romanf578cd52023-10-19 09:47:40 +02001102nc_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 +01001103{
roman6ece9c52022-06-22 09:29:17 +02001104 struct timespec ts_timeout;
romanf578cd52023-10-19 09:47:40 +02001105 ssh_message msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001106
romanf578cd52023-10-19 09:47:40 +02001107 if (timeout) {
1108 nc_timeouttime_get(&ts_timeout, timeout * 1000);
Michal Vasko36c7be82017-02-22 13:37:59 +01001109 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001110 while (1) {
1111 if (!nc_session_is_connected(session)) {
romanf578cd52023-10-19 09:47:40 +02001112 ERR(session, "Communication SSH socket unexpectedly closed.");
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001113 return -1;
1114 }
1115
romanf578cd52023-10-19 09:47:40 +02001116 msg = ssh_message_get(session->ti.libssh.session);
1117 if (msg) {
1118 if (nc_session_ssh_msg(session, opts, msg, NULL)) {
1119 ssh_message_reply_default(msg);
1120 }
1121 ssh_message_free(msg);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001122 }
1123
romanf578cd52023-10-19 09:47:40 +02001124 if (session->ti.libssh.channel && session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) {
Michal Vasko1a38c862016-01-15 15:50:07 +01001125 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001126 }
1127
Michal Vasko086311b2016-01-08 09:53:11 +01001128 usleep(NC_TIMEOUT_STEP);
romanf578cd52023-10-19 09:47:40 +02001129 if ((opts->auth_timeout) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
roman6ece9c52022-06-22 09:29:17 +02001130 /* timeout */
1131 ERR(session, "Failed to start \"netconf\" SSH subsystem for too long, disconnecting.");
1132 break;
Michal Vasko36c7be82017-02-22 13:37:59 +01001133 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001134 }
Michal Vasko086311b2016-01-08 09:53:11 +01001135
Michal Vasko1a38c862016-01-15 15:50:07 +01001136 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001137}
1138
Michal Vaskof3c41e32022-09-09 11:22:21 +02001139/**
1140 * @brief Set hostkeys to be used for an SSH bind.
1141 *
1142 * @param[in] sbind SSH bind to use.
1143 * @param[in] hostkeys Array of hostkeys.
1144 * @param[in] hostkey_count Count of @p hostkeys.
1145 * @return 0 on success.
1146 * @return -1 on error.
1147 */
Michal Vasko4c1fb492017-01-30 14:31:07 +01001148static int
romanf578cd52023-10-19 09:47:40 +02001149nc_ssh_bind_add_hostkeys(ssh_bind sbind, struct nc_server_ssh_opts *opts, uint16_t hostkey_count)
Michal Vasko4c1fb492017-01-30 14:31:07 +01001150{
romanf578cd52023-10-19 09:47:40 +02001151 uint16_t i;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001152 char *privkey_path, *privkey_data;
Michal Vaskoddce1212019-05-24 09:58:49 +02001153 int ret;
romanf578cd52023-10-19 09:47:40 +02001154 struct nc_asymmetric_key *key = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001155
1156 for (i = 0; i < hostkey_count; ++i) {
1157 privkey_path = privkey_data = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001158
romanf578cd52023-10-19 09:47:40 +02001159 /* get the asymmetric key */
1160 if (opts->hostkeys[i].store == NC_STORE_LOCAL) {
1161 /* stored locally */
1162 key = &opts->hostkeys[i].key;
1163 } else {
1164 /* keystore reference, need to get it */
1165 if (nc_server_ssh_ks_ref_get_key(opts->hostkeys[i].ks_ref, &key)) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001166 return -1;
1167 }
1168 }
1169
romanf578cd52023-10-19 09:47:40 +02001170 privkey_path = base64der_privkey_to_tmp_file(key->privkey_data, nc_privkey_format_to_str(key->privkey_type));
1171 if (!privkey_path) {
1172 ERR(NULL, "Temporarily storing a host key into a file failed (%s).", strerror(errno));
1173 return -1;
1174 }
1175
Michal Vasko4c1fb492017-01-30 14:31:07 +01001176 ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path);
1177
1178 /* cleanup */
1179 if (privkey_data && unlink(privkey_path)) {
Michal Vasko05532772021-06-03 12:12:38 +02001180 WRN(NULL, "Removing a temporary host key file \"%s\" failed (%s).", privkey_path, strerror(errno));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001181 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001182
1183 if (ret != SSH_OK) {
romanf578cd52023-10-19 09:47:40 +02001184 ERR(NULL, "Failed to set hostkey \"%s\" (%s).", opts->hostkeys[i].name, privkey_path);
Michal Vasko80075de2017-07-10 11:38:52 +02001185 }
1186 free(privkey_path);
1187
1188 if (ret != SSH_OK) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001189 return -1;
1190 }
1191 }
1192
1193 return 0;
1194}
1195
Michal Vasko09d700f2022-09-08 10:21:40 +02001196static int
romanf578cd52023-10-19 09:47:40 +02001197nc_accept_ssh_session_auth(struct nc_session *session, struct nc_server_ssh_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001198{
roman6ece9c52022-06-22 09:29:17 +02001199 struct timespec ts_timeout;
roman41a11e42022-06-22 09:27:08 +02001200 ssh_message msg;
romanf578cd52023-10-19 09:47:40 +02001201 struct nc_auth_state state = {0};
Michal Vasko086311b2016-01-08 09:53:11 +01001202
Michal Vasko086311b2016-01-08 09:53:11 +01001203 /* authenticate */
Michal Vasko36c7be82017-02-22 13:37:59 +01001204 if (opts->auth_timeout) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001205 nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000);
Michal Vasko36c7be82017-02-22 13:37:59 +01001206 }
1207 while (1) {
Michal Vasko2a7d4732016-01-15 09:24:46 +01001208 if (!nc_session_is_connected(session)) {
Michal Vasko05532772021-06-03 12:12:38 +02001209 ERR(session, "Communication SSH socket unexpectedly closed.");
Michal Vasko2a7d4732016-01-15 09:24:46 +01001210 return -1;
1211 }
1212
roman41a11e42022-06-22 09:27:08 +02001213 msg = ssh_message_get(session->ti.libssh.session);
1214 if (msg) {
romanf578cd52023-10-19 09:47:40 +02001215 if (nc_session_ssh_msg(session, opts, msg, &state)) {
roman41a11e42022-06-22 09:27:08 +02001216 ssh_message_reply_default(msg);
1217 }
1218 ssh_message_free(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001219 }
1220
Michal Vasko36c7be82017-02-22 13:37:59 +01001221 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
1222 break;
1223 }
1224
Michal Vasko145ae672017-02-07 10:57:27 +01001225 if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) {
Michal Vasko05532772021-06-03 12:12:38 +02001226 ERR(session, "Too many failed authentication attempts of user \"%s\".", session->username);
Michal Vasko145ae672017-02-07 10:57:27 +01001227 return -1;
1228 }
1229
Michal Vasko086311b2016-01-08 09:53:11 +01001230 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +01001231 if ((opts->auth_timeout) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
roman6ece9c52022-06-22 09:29:17 +02001232 /* timeout */
1233 break;
Michal Vasko36c7be82017-02-22 13:37:59 +01001234 }
1235 }
Michal Vasko086311b2016-01-08 09:53:11 +01001236
1237 if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) {
1238 /* timeout */
Michal Vaskoc13da702017-02-07 10:57:57 +01001239 if (session->username) {
Michal Vasko05532772021-06-03 12:12:38 +02001240 ERR(session, "User \"%s\" failed to authenticate for too long, disconnecting.", session->username);
Michal Vaskoc13da702017-02-07 10:57:57 +01001241 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001242 ERR(session, "User failed to authenticate for too long, disconnecting.");
Michal Vaskoc13da702017-02-07 10:57:57 +01001243 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001244 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001245 }
1246
Michal Vasko09d700f2022-09-08 10:21:40 +02001247 return 1;
1248}
1249
1250int
romanf578cd52023-10-19 09:47:40 +02001251nc_accept_ssh_session(struct nc_session *session, struct nc_server_ssh_opts *opts, int sock, int timeout)
Michal Vasko09d700f2022-09-08 10:21:40 +02001252{
1253 ssh_bind sbind = NULL;
Michal Vasko09d700f2022-09-08 10:21:40 +02001254 int rc = 1, r;
1255 struct timespec ts_timeout;
roman4cc0cd52023-04-14 09:12:29 +02001256 const char *err_msg;
Michal Vasko09d700f2022-09-08 10:21:40 +02001257
Michal Vasko09d700f2022-09-08 10:21:40 +02001258 /* other transport-specific data */
1259 session->ti_type = NC_TI_LIBSSH;
1260 session->ti.libssh.session = ssh_new();
1261 if (!session->ti.libssh.session) {
1262 ERR(NULL, "Failed to initialize a new SSH session.");
1263 rc = -1;
1264 goto cleanup;
1265 }
1266
1267 sbind = ssh_bind_new();
1268 if (!sbind) {
1269 ERR(session, "Failed to create an SSH bind.");
1270 rc = -1;
1271 goto cleanup;
1272 }
1273
1274 /* configure host keys */
romanf578cd52023-10-19 09:47:40 +02001275 if (nc_ssh_bind_add_hostkeys(sbind, opts, opts->hostkey_count)) {
1276 rc = -1;
1277 goto cleanup;
1278 }
1279
1280 /* configure supported algorithms */
1281 if (opts->hostkey_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, opts->hostkey_algs)) {
1282 rc = -1;
1283 goto cleanup;
1284 }
1285 if (opts->encryption_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_CIPHERS_S_C, opts->encryption_algs)) {
1286 rc = -1;
1287 goto cleanup;
1288 }
1289 if (opts->kex_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_KEY_EXCHANGE, opts->kex_algs)) {
1290 rc = -1;
1291 goto cleanup;
1292 }
1293 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 +02001294 rc = -1;
1295 goto cleanup;
1296 }
1297
1298 /* accept new connection on the bind */
1299 if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) {
1300 ERR(session, "SSH failed to accept a new connection (%s).", ssh_get_error(sbind));
1301 rc = -1;
1302 goto cleanup;
1303 }
1304 sock = -1;
1305
romanf578cd52023-10-19 09:47:40 +02001306 /* set to non-blocking */
Michal Vasko09d700f2022-09-08 10:21:40 +02001307 ssh_set_blocking(session->ti.libssh.session, 0);
1308
1309 if (timeout > -1) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001310 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko09d700f2022-09-08 10:21:40 +02001311 }
1312 while ((r = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) {
1313 /* this tends to take longer */
1314 usleep(NC_TIMEOUT_STEP * 20);
Michal Vaskod8a74192023-02-06 15:51:50 +01001315 if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001316 break;
1317 }
1318 }
1319 if (r == SSH_AGAIN) {
1320 ERR(session, "SSH key exchange timeout.");
1321 rc = 0;
1322 goto cleanup;
1323 } else if (r != SSH_OK) {
roman4cc0cd52023-04-14 09:12:29 +02001324 err_msg = ssh_get_error(session->ti.libssh.session);
1325 if (err_msg[0] == '\0') {
1326 err_msg = "hostkey algorithm generated from the hostkey most likely not found in the set of configured hostkey algorithms";
1327 }
1328 ERR(session, "SSH key exchange error (%s).", err_msg);
Michal Vasko09d700f2022-09-08 10:21:40 +02001329 rc = -1;
1330 goto cleanup;
1331 }
1332
1333 /* authenticate */
1334 if ((rc = nc_accept_ssh_session_auth(session, opts)) != 1) {
1335 goto cleanup;
1336 }
1337
Michal Vasko09d700f2022-09-08 10:21:40 +02001338 /* open channel and request 'netconf' subsystem */
romanf578cd52023-10-19 09:47:40 +02001339 if ((rc = nc_accept_ssh_session_open_netconf_channel(session, opts, timeout)) != 1) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001340 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001341 }
1342
Michal Vasko09d700f2022-09-08 10:21:40 +02001343cleanup:
1344 if (sock > -1) {
1345 close(sock);
1346 }
1347 ssh_bind_free(sbind);
1348 return rc;
Michal Vasko086311b2016-01-08 09:53:11 +01001349}
1350
Michal Vasko71090fc2016-05-24 16:37:28 +02001351API NC_MSG_TYPE
1352nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session)
1353{
1354 NC_MSG_TYPE msgtype;
1355 struct nc_session *new_session = NULL;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001356 struct timespec ts_cur;
Michal Vasko71090fc2016-05-24 16:37:28 +02001357
roman40672412023-05-04 11:10:22 +02001358 NC_CHECK_ARG_RET(orig_session, orig_session, session, NC_MSG_ERROR);
Michal Vasko71090fc2016-05-24 16:37:28 +02001359
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001360 if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) &&
1361 orig_session->ti.libssh.next) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001362 for (new_session = orig_session->ti.libssh.next;
1363 new_session != orig_session;
1364 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001365 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1366 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001367 /* we found our session */
1368 break;
1369 }
1370 }
1371 if (new_session == orig_session) {
1372 new_session = NULL;
1373 }
1374 }
1375
1376 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001377 ERR(orig_session, "Session does not have a NETCONF SSH channel ready.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001378 return NC_MSG_ERROR;
1379 }
1380
1381 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001382 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001383
1384 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001385 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001386 if (msgtype != NC_MSG_HELLO) {
1387 return msgtype;
1388 }
1389
Michal Vaskod8a74192023-02-06 15:51:50 +01001390 nc_realtime_get(&ts_cur);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001391 new_session->opts.server.session_start = ts_cur.tv_sec;
Michal Vaskod8a74192023-02-06 15:51:50 +01001392 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001393 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko71090fc2016-05-24 16:37:28 +02001394 new_session->status = NC_STATUS_RUNNING;
1395 *session = new_session;
1396
1397 return msgtype;
1398}
1399
1400API NC_MSG_TYPE
Michal Vasko96164bf2016-01-21 15:41:58 +01001401nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +01001402{
Michal Vaskobdcf2362016-07-26 11:35:43 +02001403 uint8_t q_id;
Michal Vasko71090fc2016-05-24 16:37:28 +02001404 NC_MSG_TYPE msgtype;
Michal Vaskoe4300a82017-05-24 10:35:42 +02001405 struct nc_session *new_session = NULL, *cur_session;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001406 struct timespec ts_cur;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001407 uint16_t i;
Michal Vasko086311b2016-01-08 09:53:11 +01001408
roman40672412023-05-04 11:10:22 +02001409 NC_CHECK_ARG_RET(NULL, ps, session, NC_MSG_ERROR);
Michal Vasko086311b2016-01-08 09:53:11 +01001410
Michal Vasko48a63ed2016-03-01 09:48:21 +01001411 /* LOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001412 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001413 return NC_MSG_ERROR;
Michal Vaskof04a52a2016-04-07 10:52:10 +02001414 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001415
Michal Vasko96164bf2016-01-21 15:41:58 +01001416 for (i = 0; i < ps->session_count; ++i) {
fanchanghu3d4e7212017-08-09 09:42:30 +08001417 cur_session = ps->sessions[i]->session;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001418 if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH) &&
1419 cur_session->ti.libssh.next) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001420 /* an SSH session with more channels */
Michal Vaskoe4300a82017-05-24 10:35:42 +02001421 for (new_session = cur_session->ti.libssh.next;
1422 new_session != cur_session;
Michal Vasko96164bf2016-01-21 15:41:58 +01001423 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001424 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1425 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001426 /* we found our session */
1427 break;
1428 }
1429 }
Michal Vaskoe4300a82017-05-24 10:35:42 +02001430 if (new_session != cur_session) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001431 break;
1432 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001433
Michal Vasko96164bf2016-01-21 15:41:58 +01001434 new_session = NULL;
1435 }
1436 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001437
Michal Vasko48a63ed2016-03-01 09:48:21 +01001438 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001439 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001440
Michal Vasko96164bf2016-01-21 15:41:58 +01001441 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001442 ERR(NULL, "No session with a NETCONF SSH channel ready was found.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001443 return NC_MSG_ERROR;
Michal Vasko96164bf2016-01-21 15:41:58 +01001444 }
1445
1446 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001447 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vaskofb89d772016-01-08 12:25:35 +01001448
Michal Vasko086311b2016-01-08 09:53:11 +01001449 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001450 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001451 if (msgtype != NC_MSG_HELLO) {
1452 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001453 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001454
Michal Vaskod8a74192023-02-06 15:51:50 +01001455 nc_realtime_get(&ts_cur);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001456 new_session->opts.server.session_start = ts_cur.tv_sec;
Michal Vaskod8a74192023-02-06 15:51:50 +01001457 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001458 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko086311b2016-01-08 09:53:11 +01001459 new_session->status = NC_STATUS_RUNNING;
Michal Vasko96164bf2016-01-21 15:41:58 +01001460 *session = new_session;
Michal Vasko086311b2016-01-08 09:53:11 +01001461
Michal Vasko71090fc2016-05-24 16:37:28 +02001462 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001463}