blob: fa0dad3bc18df55f36249e126eb3fb26dc77e4b3 [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);
roman3a95bb22023-10-26 11:07:17 +0200354 NC_CHECK_ERRMEM_GOTO(!(*resp) || !prompts || !echo, r = PAM_BUF_ERR, cleanup);
roman41a11e42022-06-22 09:27:08 +0200355
356 /* set the prompts for the user */
357 j = 0;
358 for (i = 0; i < n_messages; i++) {
359 if ((msg[i]->msg_style == PAM_PROMPT_ECHO_ON) || (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF)) {
360 prompts[j++] = msg[i]->msg;
361 }
362 }
363
364 /* iterate over all the messages and adjust the echo array accordingly */
365 j = 0;
366 for (i = 0; i < n_messages; i++) {
367 if (msg[i]->msg_style == PAM_PROMPT_ECHO_ON) {
368 echo[j++] = 1;
369 }
370 if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF) {
371 /* no need to set to 0 because of calloc */
372 j++;
373 }
374 }
375
376 /* print all the keyboard-interactive challenges to the user */
377 r = ssh_message_auth_interactive_request(clb_data->msg, name, instruction, n_requests, prompts, echo);
378 if (r != SSH_OK) {
379 ERR(NULL, "Failed to send an authentication request.");
380 r = PAM_CONV_ERR;
381 goto cleanup;
382 }
383
384 if (opts->auth_timeout) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100385 nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000);
roman41a11e42022-06-22 09:27:08 +0200386 }
387
388 /* get user's replies */
389 do {
390 if (!nc_session_is_connected(clb_data->session)) {
391 ERR(NULL, "Communication SSH socket unexpectedly closed.");
392 r = PAM_CONV_ERR;
393 goto cleanup;
394 }
395
396 reply = ssh_message_get(libssh_session);
397 if (reply) {
398 break;
399 }
400
401 usleep(NC_TIMEOUT_STEP);
romanea0edaa2023-10-26 12:16:25 +0200402 } while (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) >= 1));
roman41a11e42022-06-22 09:27:08 +0200403
404 if (!reply) {
405 ERR(NULL, "Authentication timeout.");
406 r = PAM_CONV_ERR;
407 goto cleanup;
408 }
409
410 /* check if the amount of replies matches the amount of requests */
411 n_answers = ssh_userauth_kbdint_getnanswers(libssh_session);
412 if (n_answers != n_requests) {
413 ERR(NULL, "Expected %d response(s), got %d.", n_requests, n_answers);
414 r = PAM_CONV_ERR;
415 goto cleanup;
416 }
417
418 /* give the replies to a PAM module */
419 for (i = 0; i < n_answers; i++) {
420 (*resp)[i].resp = strdup(ssh_userauth_kbdint_getanswer(libssh_session, i));
421 /* it should be the caller's responsibility to free this, however if mem alloc fails,
422 * it is safer to free the responses here and set them to NULL */
423 if ((*resp)[i].resp == NULL) {
424 for (j = 0; j < i; j++) {
425 free((*resp)[j].resp);
426 (*resp)[j].resp = NULL;
427 }
428 ERRMEM;
429 r = PAM_BUF_ERR;
430 goto cleanup;
431 }
432 }
433
434cleanup:
435 ssh_message_free(reply);
436 free(prompts);
437 free(echo);
438 return r;
439}
440
441/**
442 * @brief Handles authentication via Linux PAM.
443 *
444 * @param[in] session NETCONF session.
445 * @param[in] ssh_msg SSH message with a keyboard-interactive authentication request.
446 * @return PAM_SUCCESS on success;
447 * @return PAM error otherwise.
448 */
449static int
romanf578cd52023-10-19 09:47:40 +0200450nc_pam_auth(struct nc_session *session, struct nc_server_ssh_opts *opts, ssh_message ssh_msg)
roman41a11e42022-06-22 09:27:08 +0200451{
452 pam_handle_t *pam_h = NULL;
453 int ret;
454 struct nc_pam_thread_arg clb_data;
455 struct pam_conv conv;
romanf578cd52023-10-19 09:47:40 +0200456 uint16_t i;
roman41a11e42022-06-22 09:27:08 +0200457
458 /* structure holding callback's data */
459 clb_data.msg = ssh_msg;
460 clb_data.session = session;
romanf578cd52023-10-19 09:47:40 +0200461 clb_data.opts = opts;
roman41a11e42022-06-22 09:27:08 +0200462
463 /* PAM conversation structure holding the callback and it's data */
464 conv.conv = nc_pam_conv_clb;
465 conv.appdata_ptr = &clb_data;
466
romanf578cd52023-10-19 09:47:40 +0200467 /* get the current client's configuration file */
468 for (i = 0; i < opts->client_count; i++) {
469 if (!strcmp(opts->auth_clients[i].username, session->username)) {
470 break;
471 }
472 }
473
474 if (i == opts->client_count) {
475 ERR(NULL, "User \"%s\" not found.", session->username);
476 ret = 1;
477 goto cleanup;
478 }
479
480 if (!opts->auth_clients[i].pam_config_name) {
481 ERR(NULL, "User's \"%s\" PAM configuration filename not set.");
482 ret = 1;
483 goto cleanup;
484 }
485
roman41a11e42022-06-22 09:27:08 +0200486 /* initialize PAM and see if the given configuration file exists */
Michal Vasko0d81c572022-09-26 10:39:31 +0200487# ifdef LIBPAM_HAVE_CONFDIR
roman41a11e42022-06-22 09:27:08 +0200488 /* PAM version >= 1.4 */
romanf578cd52023-10-19 09:47:40 +0200489 ret = pam_start_confdir(opts->auth_clients[i].pam_config_name, session->username, &conv, opts->auth_clients[i].pam_config_dir, &pam_h);
Michal Vasko0d81c572022-09-26 10:39:31 +0200490# else
roman41a11e42022-06-22 09:27:08 +0200491 /* PAM version < 1.4 */
romanf578cd52023-10-19 09:47:40 +0200492 ret = pam_start(opts->auth_clients[i].pam_config_name, session->username, &conv, &pam_h);
Michal Vasko0d81c572022-09-26 10:39:31 +0200493# endif
roman41a11e42022-06-22 09:27:08 +0200494 if (ret != PAM_SUCCESS) {
495 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
496 goto cleanup;
497 }
498
499 /* authentication based on the modules listed in the configuration file */
500 ret = pam_authenticate(pam_h, 0);
501 if (ret != PAM_SUCCESS) {
502 if (ret == PAM_ABORT) {
503 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
504 goto cleanup;
505 } else {
506 VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
507 goto cleanup;
508 }
509 }
510
511 /* correct token entered, check other requirements(the time of the day, expired token, ...) */
512 ret = pam_acct_mgmt(pam_h, 0);
513 if ((ret != PAM_SUCCESS) && (ret != PAM_NEW_AUTHTOK_REQD)) {
514 VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
515 goto cleanup;
516 }
517
518 /* if a token has expired a new one will be generated */
519 if (ret == PAM_NEW_AUTHTOK_REQD) {
520 VRB(NULL, "PAM warning occurred (%s).\n", pam_strerror(pam_h, ret));
521 ret = pam_chauthtok(pam_h, PAM_CHANGE_EXPIRED_AUTHTOK);
522 if (ret == PAM_SUCCESS) {
523 VRB(NULL, "The authentication token of user \"%s\" updated successfully.", session->username);
524 } else {
525 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
526 goto cleanup;
527 }
528 }
529
530cleanup:
531 /* destroy the PAM context */
532 if (pam_end(pam_h, ret) != PAM_SUCCESS) {
533 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
534 }
535 return ret;
536}
537
Michal Vasko0d81c572022-09-26 10:39:31 +0200538#endif
539
romanf578cd52023-10-19 09:47:40 +0200540static int
541nc_sshcb_auth_kbdint(struct nc_session *session, struct nc_server_ssh_opts *opts, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100542{
bhart3bc2f582018-06-05 12:40:32 -0500543 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100544
romanf578cd52023-10-19 09:47:40 +0200545 if (server_opts.interactive_auth_clb) {
546 auth_ret = server_opts.interactive_auth_clb(session, session->ti.libssh.session, msg, server_opts.interactive_auth_data);
Michal Vasko0d81c572022-09-26 10:39:31 +0200547 } else {
548#ifdef HAVE_LIBPAM
romanf578cd52023-10-19 09:47:40 +0200549 if (nc_pam_auth(session, opts, msg) == PAM_SUCCESS) {
Michal Vasko0d81c572022-09-26 10:39:31 +0200550 auth_ret = 0;
551 }
552#else
553 ERR(session, "PAM-based SSH authentication is not supported.");
554#endif
bhart1bb7cdb2018-07-02 15:03:30 -0500555 }
556
557 /* Authenticate message based on outcome */
romanf578cd52023-10-19 09:47:40 +0200558 if (auth_ret) {
bhart1bb7cdb2018-07-02 15:03:30 -0500559 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200560 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
561 session->opts.server.ssh_auth_attempts);
bhart1bb7cdb2018-07-02 15:03:30 -0500562 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +0100563 }
romanf578cd52023-10-19 09:47:40 +0200564
565 return auth_ret;
566}
567
568/*
569 * Get the public key type from binary data stored in buffer.
570 * The data is in the form of: 4 bytes = data length, then data of data length
571 * and the data is in network byte order. The key has to be in the SSH2 format.
572 */
573static const char *
574nc_server_ssh_get_pubkey_type(const char *buffer, uint32_t *len)
575{
576 uint32_t type_len;
577
578 /* copy the 4 bytes */
579 memcpy(&type_len, buffer, sizeof type_len);
580 /* type_len now stores the length of the key type */
581 type_len = ntohl(type_len);
582 *len = type_len;
583
584 /* move 4 bytes in the buffer, this is where the type should be */
585 buffer += sizeof type_len;
586 return buffer;
587}
588
589/**
590 * @brief Create ssh key from base64 pubkey data.
591 *
592 * @param[in] base64 base64 encoded public key.
593 * @param[out] key created ssh key.
594 * @return 0 on success, 1 otherwise.
595 */
596static int
597nc_server_ssh_create_ssh_pubkey(const char *base64, ssh_key *key)
598{
599 int ret = 0;
600 char *bin = NULL;
601 const char *pub_type = NULL;
602 uint32_t pub_type_len = 0;
603
604 if (!key && !base64) {
605 ERRINT;
606 ret = 1;
607 goto cleanup;
608 }
609
610 *key = NULL;
611
612 /* convert base64 to binary */
613 if (nc_base64_to_bin(base64, &bin) == -1) {
614 ERR(NULL, "Unable to decode base64.");
615 ret = 1;
616 goto cleanup;
617 }
618
619 /* get the key type and try to import it if possible */
620 pub_type = nc_server_ssh_get_pubkey_type(bin, &pub_type_len);
621 if (!pub_type) {
622 ret = 1;
623 goto cleanup;
624 } else if (!strncmp(pub_type, "ssh-dss", pub_type_len)) {
625 ERR(NULL, "DSA keys are not supported.");
626 ret = 1;
627 goto cleanup;
628 } else if (!strncmp(pub_type, "ssh-rsa", pub_type_len)) {
629 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_RSA, key);
630 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp256", pub_type_len)) {
631 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P256, key);
632 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp384", pub_type_len)) {
633 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P384, key);
634 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp521", pub_type_len)) {
635 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P521, key);
636 } else if (!strncmp(pub_type, "ssh-ed25519", pub_type_len)) {
637 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ED25519, key);
638 } else {
639 ERR(NULL, "Public key type not recognised.");
640 ret = 1;
641 goto cleanup;
642 }
643
644cleanup:
645 if (ret != SSH_OK) {
646 ERR(NULL, "Error importing public key.");
647 }
648 free(bin);
649 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100650}
651
Michal Vaskof3c41e32022-09-09 11:22:21 +0200652/**
653 * @brief Compare SSH key with configured authorized keys and return the username of the matching one, if any.
654 *
655 * @param[in] key Presented SSH key to compare.
656 * @return Authorized key username, NULL if no match was found.
657 */
romanf578cd52023-10-19 09:47:40 +0200658static int
659auth_pubkey_compare_key(ssh_key key, struct nc_auth_client *auth_client)
Michal Vasko086311b2016-01-08 09:53:11 +0100660{
romanf578cd52023-10-19 09:47:40 +0200661 uint16_t i, pubkey_count;
Michal Vasko3e9d1682017-02-24 09:50:15 +0100662 int ret = 0;
romanf578cd52023-10-19 09:47:40 +0200663 ssh_key new_key = NULL;
664 struct nc_public_key *pubkeys;
Michal Vasko086311b2016-01-08 09:53:11 +0100665
romanf578cd52023-10-19 09:47:40 +0200666 /* get the correct public key storage */
667 if (auth_client->store == NC_STORE_LOCAL) {
668 pubkeys = auth_client->pubkeys;
669 pubkey_count = auth_client->pubkey_count;
670 } else {
671 ret = nc_server_ssh_ts_ref_get_keys(auth_client->ts_ref, &pubkeys, &pubkey_count);
672 if (ret) {
673 ERR(NULL, "Error getting \"%s\"'s public keys from the truststore.", auth_client->username);
674 return ret;
Michal Vasko17dfda92016-12-01 14:06:16 +0100675 }
romanf578cd52023-10-19 09:47:40 +0200676 }
Michal Vasko17dfda92016-12-01 14:06:16 +0100677
romanf578cd52023-10-19 09:47:40 +0200678 /* try to compare all of the client's keys with the key received in the SSH message */
679 for (i = 0; i < pubkey_count; i++) {
680 /* create the SSH key from the data */
681 if (nc_server_ssh_create_ssh_pubkey(pubkeys[i].data, &new_key)) {
682 ssh_key_free(new_key);
Michal Vasko086311b2016-01-08 09:53:11 +0100683 continue;
684 }
685
romanf578cd52023-10-19 09:47:40 +0200686 /* compare the keys */
687 ret = ssh_key_cmp(key, new_key, SSH_KEY_CMP_PUBLIC);
688 if (!ret) {
Michal Vasko086311b2016-01-08 09:53:11 +0100689 break;
romanf578cd52023-10-19 09:47:40 +0200690 } else {
691 WRN(NULL, "User's \"%s\" public key doesn't match, trying another.", auth_client->username);
692 ssh_key_free(new_key);
Michal Vasko086311b2016-01-08 09:53:11 +0100693 }
Michal Vasko086311b2016-01-08 09:53:11 +0100694 }
695
romanf578cd52023-10-19 09:47:40 +0200696 if (i == pubkey_count) {
697 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100698 }
699
romanf578cd52023-10-19 09:47:40 +0200700 if (!ret) {
701 /* only free a key if everything was ok, it would have already been freed otherwise */
702 ssh_key_free(new_key);
703 }
Michal Vaskoa05c7b12017-01-30 14:33:08 +0100704
romanf578cd52023-10-19 09:47:40 +0200705 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100706}
707
708static void
romanf578cd52023-10-19 09:47:40 +0200709nc_sshcb_auth_none(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100710{
romanf578cd52023-10-19 09:47:40 +0200711 if (auth_client->supports_none && !auth_client->password && !auth_client->pubkey_count && !auth_client->pam_config_name) {
712 /* only authenticate the client if he supports none and no other method */
713 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
714 VRB(session, "User \"%s\" authenticated.", session->username);
715 ssh_message_auth_reply_success(msg, 0);
716 }
717
718 ssh_message_reply_default(msg);
719}
720
721static int
722nc_sshcb_auth_pubkey(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
723{
724 int signature_state, ret = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100725
Michal Vasko733c0bd2018-07-03 13:14:40 +0200726 if (server_opts.pubkey_auth_clb) {
727 if (server_opts.pubkey_auth_clb(session, ssh_message_auth_pubkey(msg), server_opts.pubkey_auth_data)) {
romanf578cd52023-10-19 09:47:40 +0200728 ret = 1;
bhart3bc2f582018-06-05 12:40:32 -0500729 goto fail;
730 }
Michal Vasko733c0bd2018-07-03 13:14:40 +0200731 } else {
romanf578cd52023-10-19 09:47:40 +0200732 if (auth_pubkey_compare_key(ssh_message_auth_pubkey(msg), auth_client)) {
Michal Vasko05532772021-06-03 12:12:38 +0200733 VRB(session, "User \"%s\" tried to use an unknown (unauthorized) public key.", session->username);
romanf578cd52023-10-19 09:47:40 +0200734 ret = 1;
bhart3bc2f582018-06-05 12:40:32 -0500735 goto fail;
736 }
Michal Vaskobd13a932016-09-14 09:00:35 +0200737 }
Michal Vaskobd13a932016-09-14 09:00:35 +0200738
Michal Vasko086311b2016-01-08 09:53:11 +0100739 signature_state = ssh_message_auth_publickey_state(msg);
romanf578cd52023-10-19 09:47:40 +0200740 if (signature_state == SSH_PUBLICKEY_STATE_NONE) {
Michal Vaskobd13a932016-09-14 09:00:35 +0200741 /* accepting only the use of a public key */
742 ssh_message_auth_reply_pk_ok_simple(msg);
romanf578cd52023-10-19 09:47:40 +0200743 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100744 }
745
romanf578cd52023-10-19 09:47:40 +0200746 return ret;
Michal Vaskobd13a932016-09-14 09:00:35 +0200747
748fail:
Michal Vasko2e6defd2016-10-07 15:48:15 +0200749 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200750 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
751 session->opts.server.ssh_auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +0100752 ssh_message_reply_default(msg);
romanf578cd52023-10-19 09:47:40 +0200753
754 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100755}
756
757static int
Michal Vasko96164bf2016-01-21 15:41:58 +0100758nc_sshcb_channel_open(struct nc_session *session, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100759{
Michal Vasko96164bf2016-01-21 15:41:58 +0100760 ssh_channel chan;
761
762 /* first channel request */
763 if (!session->ti.libssh.channel) {
764 if (session->status != NC_STATUS_STARTING) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100765 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100766 return -1;
767 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100768 chan = ssh_message_channel_request_open_reply_accept(msg);
769 if (!chan) {
Michal Vasko05532772021-06-03 12:12:38 +0200770 ERR(session, "Failed to create a new SSH channel.");
Michal Vasko96164bf2016-01-21 15:41:58 +0100771 return -1;
772 }
773 session->ti.libssh.channel = chan;
Michal Vasko086311b2016-01-08 09:53:11 +0100774
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200775 /* additional channel request */
Michal Vasko96164bf2016-01-21 15:41:58 +0100776 } else {
777 chan = ssh_message_channel_request_open_reply_accept(msg);
778 if (!chan) {
Michal Vasko05532772021-06-03 12:12:38 +0200779 ERR(session, "Session %u: failed to create a new SSH channel.", session->id);
Michal Vasko96164bf2016-01-21 15:41:58 +0100780 return -1;
781 }
782 /* channel was created and libssh stored it internally in the ssh_session structure, good enough */
Michal Vasko086311b2016-01-08 09:53:11 +0100783 }
784
Michal Vasko086311b2016-01-08 09:53:11 +0100785 return 0;
786}
787
788static int
789nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem)
790{
Michal Vasko96164bf2016-01-21 15:41:58 +0100791 struct nc_session *new_session;
Michal Vasko086311b2016-01-08 09:53:11 +0100792
Michal Vasko96164bf2016-01-21 15:41:58 +0100793 if (strcmp(subsystem, "netconf")) {
Michal Vasko05532772021-06-03 12:12:38 +0200794 WRN(session, "Received an unknown subsystem \"%s\" request.", subsystem);
Michal Vasko086311b2016-01-08 09:53:11 +0100795 return -1;
796 }
797
Michal Vasko96164bf2016-01-21 15:41:58 +0100798 if (session->ti.libssh.channel == channel) {
799 /* first channel requested */
800 if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) {
801 ERRINT;
802 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100803 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100804 if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) {
Michal Vasko05532772021-06-03 12:12:38 +0200805 ERR(session, "Subsystem \"netconf\" requested for the second time.");
Michal Vasko96164bf2016-01-21 15:41:58 +0100806 return -1;
807 }
808
809 session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF;
Michal Vasko086311b2016-01-08 09:53:11 +0100810 } else {
Michal Vasko96164bf2016-01-21 15:41:58 +0100811 /* additional channel subsystem request, new session is ready as far as SSH is concerned */
Michal Vasko131120a2018-05-29 15:44:02 +0200812 new_session = nc_new_session(NC_SERVER, 1);
roman3a95bb22023-10-26 11:07:17 +0200813 NC_CHECK_ERRMEM_RET(!new_session, -1);
Michal Vasko96164bf2016-01-21 15:41:58 +0100814
815 /* insert the new session */
816 if (!session->ti.libssh.next) {
817 new_session->ti.libssh.next = session;
818 } else {
819 new_session->ti.libssh.next = session->ti.libssh.next;
820 }
821 session->ti.libssh.next = new_session;
822
823 new_session->status = NC_STATUS_STARTING;
Michal Vasko96164bf2016-01-21 15:41:58 +0100824 new_session->ti_type = NC_TI_LIBSSH;
Michal Vasko131120a2018-05-29 15:44:02 +0200825 new_session->io_lock = session->io_lock;
Michal Vasko96164bf2016-01-21 15:41:58 +0100826 new_session->ti.libssh.channel = channel;
827 new_session->ti.libssh.session = session->ti.libssh.session;
Michal Vasko93224072021-11-09 12:14:28 +0100828 new_session->username = strdup(session->username);
829 new_session->host = strdup(session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100830 new_session->port = session->port;
Michal Vasko93224072021-11-09 12:14:28 +0100831 new_session->ctx = (struct ly_ctx *)session->ctx;
Michal Vasko83d15322018-09-27 09:44:02 +0200832 new_session->flags = NC_SESSION_SSH_AUTHENTICATED | NC_SESSION_SSH_SUBSYS_NETCONF | NC_SESSION_SHAREDCTX;
Michal Vasko086311b2016-01-08 09:53:11 +0100833 }
834
835 return 0;
836}
837
Michal Vasko96164bf2016-01-21 15:41:58 +0100838int
romanf578cd52023-10-19 09:47:40 +0200839nc_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 +0100840{
841 const char *str_type, *str_subtype = NULL, *username;
romanf578cd52023-10-19 09:47:40 +0200842 int subtype, type, libssh_auth_methods = 0, ret = 0;
843 uint16_t i;
844 struct nc_auth_client *auth_client = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100845
846 type = ssh_message_type(msg);
847 subtype = ssh_message_subtype(msg);
848
849 switch (type) {
850 case SSH_REQUEST_AUTH:
851 str_type = "request-auth";
852 switch (subtype) {
853 case SSH_AUTH_METHOD_NONE:
854 str_subtype = "none";
855 break;
856 case SSH_AUTH_METHOD_PASSWORD:
857 str_subtype = "password";
858 break;
859 case SSH_AUTH_METHOD_PUBLICKEY:
860 str_subtype = "publickey";
861 break;
862 case SSH_AUTH_METHOD_HOSTBASED:
863 str_subtype = "hostbased";
864 break;
865 case SSH_AUTH_METHOD_INTERACTIVE:
866 str_subtype = "interactive";
867 break;
868 case SSH_AUTH_METHOD_GSSAPI_MIC:
869 str_subtype = "gssapi-mic";
870 break;
871 }
872 break;
873
874 case SSH_REQUEST_CHANNEL_OPEN:
875 str_type = "request-channel-open";
876 switch (subtype) {
877 case SSH_CHANNEL_SESSION:
878 str_subtype = "session";
879 break;
880 case SSH_CHANNEL_DIRECT_TCPIP:
881 str_subtype = "direct-tcpip";
882 break;
883 case SSH_CHANNEL_FORWARDED_TCPIP:
884 str_subtype = "forwarded-tcpip";
885 break;
886 case (int)SSH_CHANNEL_X11:
887 str_subtype = "channel-x11";
888 break;
889 case SSH_CHANNEL_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200890 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100891 default:
892 str_subtype = "unknown";
893 break;
894 }
895 break;
896
897 case SSH_REQUEST_CHANNEL:
898 str_type = "request-channel";
899 switch (subtype) {
900 case SSH_CHANNEL_REQUEST_PTY:
901 str_subtype = "pty";
902 break;
903 case SSH_CHANNEL_REQUEST_EXEC:
904 str_subtype = "exec";
905 break;
906 case SSH_CHANNEL_REQUEST_SHELL:
907 str_subtype = "shell";
908 break;
909 case SSH_CHANNEL_REQUEST_ENV:
910 str_subtype = "env";
911 break;
912 case SSH_CHANNEL_REQUEST_SUBSYSTEM:
913 str_subtype = "subsystem";
914 break;
915 case SSH_CHANNEL_REQUEST_WINDOW_CHANGE:
916 str_subtype = "window-change";
917 break;
918 case SSH_CHANNEL_REQUEST_X11:
919 str_subtype = "x11";
920 break;
921 case SSH_CHANNEL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200922 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100923 default:
924 str_subtype = "unknown";
925 break;
926 }
927 break;
928
929 case SSH_REQUEST_SERVICE:
930 str_type = "request-service";
931 str_subtype = ssh_message_service_service(msg);
932 break;
933
934 case SSH_REQUEST_GLOBAL:
935 str_type = "request-global";
936 switch (subtype) {
937 case SSH_GLOBAL_REQUEST_TCPIP_FORWARD:
938 str_subtype = "tcpip-forward";
939 break;
940 case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD:
941 str_subtype = "cancel-tcpip-forward";
942 break;
943 case SSH_GLOBAL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200944 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100945 default:
946 str_subtype = "unknown";
947 break;
948 }
949 break;
950
951 default:
952 str_type = "unknown";
953 str_subtype = "unknown";
954 break;
955 }
956
Michal Vasko05532772021-06-03 12:12:38 +0200957 VRB(session, "Received an SSH message \"%s\" of subtype \"%s\".", str_type, str_subtype);
Michal Vasko5e0edd82020-07-29 15:26:13 +0200958 if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) {
Michal Vaskoce319162016-02-03 15:33:08 +0100959 /* "valid" situation if, for example, receiving some auth or channel request timeouted,
960 * but we got it now, during session free */
Michal Vasko05532772021-06-03 12:12:38 +0200961 VRB(session, "SSH message arrived on a %s session, the request will be denied.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200962 (session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid"));
Michal Vaskoce319162016-02-03 15:33:08 +0100963 ssh_message_reply_default(msg);
964 return 0;
965 }
Michal Vasko086311b2016-01-08 09:53:11 +0100966
967 /*
968 * process known messages
969 */
970 if (type == SSH_REQUEST_AUTH) {
971 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko05532772021-06-03 12:12:38 +0200972 ERR(session, "User \"%s\" authenticated, but requested another authentication.", session->username);
Michal Vasko086311b2016-01-08 09:53:11 +0100973 ssh_message_reply_default(msg);
974 return 0;
romanf578cd52023-10-19 09:47:40 +0200975 } else if (!state || !opts) {
976 /* these two parameters should always be set during an authentication,
977 * however do a check just in case something goes really wrong, since they
978 * are not needed for other types of messages
979 */
980 ERRINT;
981 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100982 }
983
Michal Vasko086311b2016-01-08 09:53:11 +0100984 /* save the username, do not let the client change it */
985 username = ssh_message_auth_user(msg);
romanf578cd52023-10-19 09:47:40 +0200986 assert(username);
987
988 for (i = 0; i < opts->client_count; i++) {
989 if (!strcmp(opts->auth_clients[i].username, username)) {
990 auth_client = &opts->auth_clients[i];
991 break;
992 }
993 }
994
995 if (!auth_client) {
996 if (opts->endpt_client_ref) {
997 return nc_session_ssh_msg(session, opts->endpt_client_ref->opts.ssh, msg, state);
Michal Vasko086311b2016-01-08 09:53:11 +0100998 }
999
romanf578cd52023-10-19 09:47:40 +02001000 ERR(NULL, "User \"%s\" not known by the server.", username);
1001 ssh_message_reply_default(msg);
1002 return 0;
1003 }
1004
1005 if (!session->username) {
Michal Vasko93224072021-11-09 12:14:28 +01001006 session->username = strdup(username);
romanf578cd52023-10-19 09:47:40 +02001007
1008 /* configure and count accepted auth methods */
1009 if (auth_client->store == NC_STORE_LOCAL) {
1010 if (auth_client->pubkey_count) {
1011 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
1012 }
1013 } else if (auth_client->ts_ref) {
1014 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
1015 }
1016 if (auth_client->password) {
1017 state->auth_method_count++;
1018 libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD;
1019 }
1020 if (auth_client->pam_config_name) {
1021 state->auth_method_count++;
1022 libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE;
1023 }
1024 if (auth_client->supports_none) {
1025 libssh_auth_methods |= SSH_AUTH_METHOD_NONE;
1026 }
1027
1028 if (libssh_auth_methods & SSH_AUTH_METHOD_PUBLICKEY) {
1029 state->auth_method_count++;
1030 }
1031
1032 ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods);
1033 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001034 if (strcmp(username, session->username)) {
romanf578cd52023-10-19 09:47:40 +02001035 /* changing username not allowed */
Michal Vasko05532772021-06-03 12:12:38 +02001036 ERR(session, "User \"%s\" changed its username to \"%s\".", session->username, username);
Michal Vasko086311b2016-01-08 09:53:11 +01001037 session->status = NC_STATUS_INVALID;
Michal Vasko428087d2016-01-14 16:04:28 +01001038 session->term_reason = NC_SESSION_TERM_OTHER;
Michal Vasko086311b2016-01-08 09:53:11 +01001039 return 1;
1040 }
1041 }
1042
romanf578cd52023-10-19 09:47:40 +02001043 /* try authenticating, the user must authenticate via all of his configured auth methods */
Michal Vasko086311b2016-01-08 09:53:11 +01001044 if (subtype == SSH_AUTH_METHOD_NONE) {
romanf578cd52023-10-19 09:47:40 +02001045 nc_sshcb_auth_none(session, auth_client, msg);
1046 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001047 } else if (subtype == SSH_AUTH_METHOD_PASSWORD) {
romanf578cd52023-10-19 09:47:40 +02001048 ret = nc_sshcb_auth_password(session, auth_client, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001049 } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) {
romanf578cd52023-10-19 09:47:40 +02001050 ret = nc_sshcb_auth_pubkey(session, auth_client, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001051 } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) {
romanf578cd52023-10-19 09:47:40 +02001052 ret = nc_sshcb_auth_kbdint(session, opts, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001053 }
romanf578cd52023-10-19 09:47:40 +02001054
1055 if (!ret) {
1056 state->auth_success_count++;
1057 }
1058
1059 if (!ret && (state->auth_success_count < state->auth_method_count)) {
1060 /* success, but he needs to do another method */
1061 VRB(session, "User \"%s\" partially authenticated, but still needs to authenticate via the rest of his configured methods.", username);
1062 ssh_message_auth_reply_success(msg, 1);
1063 } else if (!ret && (state->auth_success_count == state->auth_method_count)) {
1064 /* authenticated */
1065 ssh_message_auth_reply_success(msg, 0);
1066 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
1067 VRB(session, "User \"%s\" authenticated.", username);
1068 }
1069
1070 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001071 } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko0df67562016-01-21 15:50:11 +01001072 if ((type == SSH_REQUEST_CHANNEL_OPEN) && ((enum ssh_channel_type_e)subtype == SSH_CHANNEL_SESSION)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001073 if (nc_sshcb_channel_open(session, msg)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001074 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001075 }
Michal Vasko086311b2016-01-08 09:53:11 +01001076 return 0;
Michal Vasko96164bf2016-01-21 15:41:58 +01001077
Michal Vasko0df67562016-01-21 15:50:11 +01001078 } else if ((type == SSH_REQUEST_CHANNEL) && ((enum ssh_channel_requests_e)subtype == SSH_CHANNEL_REQUEST_SUBSYSTEM)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001079 if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg),
1080 ssh_message_channel_request_subsystem(msg))) {
Michal Vasko086311b2016-01-08 09:53:11 +01001081 ssh_message_reply_default(msg);
Michal Vasko96164bf2016-01-21 15:41:58 +01001082 } else {
1083 ssh_message_channel_request_reply_success(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001084 }
1085 return 0;
1086 }
1087 }
1088
1089 /* we did not process it */
1090 return 1;
1091}
1092
Michal Vasko1a38c862016-01-15 15:50:07 +01001093/* ret 1 on success, 0 on timeout, -1 on error */
Michal Vasko086311b2016-01-08 09:53:11 +01001094static int
romanf578cd52023-10-19 09:47:40 +02001095nc_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 +01001096{
roman6ece9c52022-06-22 09:29:17 +02001097 struct timespec ts_timeout;
romanf578cd52023-10-19 09:47:40 +02001098 ssh_message msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001099
romanf578cd52023-10-19 09:47:40 +02001100 if (timeout) {
1101 nc_timeouttime_get(&ts_timeout, timeout * 1000);
Michal Vasko36c7be82017-02-22 13:37:59 +01001102 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001103 while (1) {
1104 if (!nc_session_is_connected(session)) {
romanf578cd52023-10-19 09:47:40 +02001105 ERR(session, "Communication SSH socket unexpectedly closed.");
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001106 return -1;
1107 }
1108
romanf578cd52023-10-19 09:47:40 +02001109 msg = ssh_message_get(session->ti.libssh.session);
1110 if (msg) {
1111 if (nc_session_ssh_msg(session, opts, msg, NULL)) {
1112 ssh_message_reply_default(msg);
1113 }
1114 ssh_message_free(msg);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001115 }
1116
romanf578cd52023-10-19 09:47:40 +02001117 if (session->ti.libssh.channel && session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) {
Michal Vasko1a38c862016-01-15 15:50:07 +01001118 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001119 }
1120
Michal Vasko086311b2016-01-08 09:53:11 +01001121 usleep(NC_TIMEOUT_STEP);
romanea0edaa2023-10-26 12:16:25 +02001122 if (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
roman6ece9c52022-06-22 09:29:17 +02001123 /* timeout */
1124 ERR(session, "Failed to start \"netconf\" SSH subsystem for too long, disconnecting.");
1125 break;
Michal Vasko36c7be82017-02-22 13:37:59 +01001126 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001127 }
Michal Vasko086311b2016-01-08 09:53:11 +01001128
Michal Vasko1a38c862016-01-15 15:50:07 +01001129 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001130}
1131
Michal Vaskof3c41e32022-09-09 11:22:21 +02001132/**
1133 * @brief Set hostkeys to be used for an SSH bind.
1134 *
1135 * @param[in] sbind SSH bind to use.
1136 * @param[in] hostkeys Array of hostkeys.
1137 * @param[in] hostkey_count Count of @p hostkeys.
1138 * @return 0 on success.
1139 * @return -1 on error.
1140 */
Michal Vasko4c1fb492017-01-30 14:31:07 +01001141static int
romanf578cd52023-10-19 09:47:40 +02001142nc_ssh_bind_add_hostkeys(ssh_bind sbind, struct nc_server_ssh_opts *opts, uint16_t hostkey_count)
Michal Vasko4c1fb492017-01-30 14:31:07 +01001143{
romanf578cd52023-10-19 09:47:40 +02001144 uint16_t i;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001145 char *privkey_path, *privkey_data;
Michal Vaskoddce1212019-05-24 09:58:49 +02001146 int ret;
romanf578cd52023-10-19 09:47:40 +02001147 struct nc_asymmetric_key *key = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001148
1149 for (i = 0; i < hostkey_count; ++i) {
1150 privkey_path = privkey_data = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001151
romanf578cd52023-10-19 09:47:40 +02001152 /* get the asymmetric key */
1153 if (opts->hostkeys[i].store == NC_STORE_LOCAL) {
1154 /* stored locally */
1155 key = &opts->hostkeys[i].key;
1156 } else {
1157 /* keystore reference, need to get it */
1158 if (nc_server_ssh_ks_ref_get_key(opts->hostkeys[i].ks_ref, &key)) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001159 return -1;
1160 }
1161 }
1162
romanf578cd52023-10-19 09:47:40 +02001163 privkey_path = base64der_privkey_to_tmp_file(key->privkey_data, nc_privkey_format_to_str(key->privkey_type));
1164 if (!privkey_path) {
1165 ERR(NULL, "Temporarily storing a host key into a file failed (%s).", strerror(errno));
1166 return -1;
1167 }
1168
Michal Vasko4c1fb492017-01-30 14:31:07 +01001169 ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path);
1170
1171 /* cleanup */
1172 if (privkey_data && unlink(privkey_path)) {
Michal Vasko05532772021-06-03 12:12:38 +02001173 WRN(NULL, "Removing a temporary host key file \"%s\" failed (%s).", privkey_path, strerror(errno));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001174 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001175
1176 if (ret != SSH_OK) {
romanf578cd52023-10-19 09:47:40 +02001177 ERR(NULL, "Failed to set hostkey \"%s\" (%s).", opts->hostkeys[i].name, privkey_path);
Michal Vasko80075de2017-07-10 11:38:52 +02001178 }
1179 free(privkey_path);
1180
1181 if (ret != SSH_OK) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001182 return -1;
1183 }
1184 }
1185
1186 return 0;
1187}
1188
Michal Vasko09d700f2022-09-08 10:21:40 +02001189static int
romanf578cd52023-10-19 09:47:40 +02001190nc_accept_ssh_session_auth(struct nc_session *session, struct nc_server_ssh_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001191{
roman6ece9c52022-06-22 09:29:17 +02001192 struct timespec ts_timeout;
roman41a11e42022-06-22 09:27:08 +02001193 ssh_message msg;
romanf578cd52023-10-19 09:47:40 +02001194 struct nc_auth_state state = {0};
Michal Vasko086311b2016-01-08 09:53:11 +01001195
Michal Vasko086311b2016-01-08 09:53:11 +01001196 /* authenticate */
Michal Vasko36c7be82017-02-22 13:37:59 +01001197 if (opts->auth_timeout) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001198 nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000);
Michal Vasko36c7be82017-02-22 13:37:59 +01001199 }
1200 while (1) {
Michal Vasko2a7d4732016-01-15 09:24:46 +01001201 if (!nc_session_is_connected(session)) {
Michal Vasko05532772021-06-03 12:12:38 +02001202 ERR(session, "Communication SSH socket unexpectedly closed.");
Michal Vasko2a7d4732016-01-15 09:24:46 +01001203 return -1;
1204 }
1205
roman41a11e42022-06-22 09:27:08 +02001206 msg = ssh_message_get(session->ti.libssh.session);
1207 if (msg) {
romanf578cd52023-10-19 09:47:40 +02001208 if (nc_session_ssh_msg(session, opts, msg, &state)) {
roman41a11e42022-06-22 09:27:08 +02001209 ssh_message_reply_default(msg);
1210 }
1211 ssh_message_free(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001212 }
1213
Michal Vasko36c7be82017-02-22 13:37:59 +01001214 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
1215 break;
1216 }
1217
Michal Vasko145ae672017-02-07 10:57:27 +01001218 if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) {
Michal Vasko05532772021-06-03 12:12:38 +02001219 ERR(session, "Too many failed authentication attempts of user \"%s\".", session->username);
Michal Vasko145ae672017-02-07 10:57:27 +01001220 return -1;
1221 }
1222
Michal Vasko086311b2016-01-08 09:53:11 +01001223 usleep(NC_TIMEOUT_STEP);
romanea0edaa2023-10-26 12:16:25 +02001224 if (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
roman6ece9c52022-06-22 09:29:17 +02001225 /* timeout */
1226 break;
Michal Vasko36c7be82017-02-22 13:37:59 +01001227 }
1228 }
Michal Vasko086311b2016-01-08 09:53:11 +01001229
1230 if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) {
1231 /* timeout */
Michal Vaskoc13da702017-02-07 10:57:57 +01001232 if (session->username) {
Michal Vasko05532772021-06-03 12:12:38 +02001233 ERR(session, "User \"%s\" failed to authenticate for too long, disconnecting.", session->username);
Michal Vaskoc13da702017-02-07 10:57:57 +01001234 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001235 ERR(session, "User failed to authenticate for too long, disconnecting.");
Michal Vaskoc13da702017-02-07 10:57:57 +01001236 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001237 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001238 }
1239
Michal Vasko09d700f2022-09-08 10:21:40 +02001240 return 1;
1241}
1242
1243int
romanf578cd52023-10-19 09:47:40 +02001244nc_accept_ssh_session(struct nc_session *session, struct nc_server_ssh_opts *opts, int sock, int timeout)
Michal Vasko09d700f2022-09-08 10:21:40 +02001245{
1246 ssh_bind sbind = NULL;
Michal Vasko09d700f2022-09-08 10:21:40 +02001247 int rc = 1, r;
1248 struct timespec ts_timeout;
roman4cc0cd52023-04-14 09:12:29 +02001249 const char *err_msg;
Michal Vasko09d700f2022-09-08 10:21:40 +02001250
Michal Vasko09d700f2022-09-08 10:21:40 +02001251 /* other transport-specific data */
1252 session->ti_type = NC_TI_LIBSSH;
1253 session->ti.libssh.session = ssh_new();
1254 if (!session->ti.libssh.session) {
1255 ERR(NULL, "Failed to initialize a new SSH session.");
1256 rc = -1;
1257 goto cleanup;
1258 }
1259
1260 sbind = ssh_bind_new();
1261 if (!sbind) {
1262 ERR(session, "Failed to create an SSH bind.");
1263 rc = -1;
1264 goto cleanup;
1265 }
1266
1267 /* configure host keys */
romanf578cd52023-10-19 09:47:40 +02001268 if (nc_ssh_bind_add_hostkeys(sbind, opts, opts->hostkey_count)) {
1269 rc = -1;
1270 goto cleanup;
1271 }
1272
1273 /* configure supported algorithms */
1274 if (opts->hostkey_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, opts->hostkey_algs)) {
1275 rc = -1;
1276 goto cleanup;
1277 }
1278 if (opts->encryption_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_CIPHERS_S_C, opts->encryption_algs)) {
1279 rc = -1;
1280 goto cleanup;
1281 }
1282 if (opts->kex_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_KEY_EXCHANGE, opts->kex_algs)) {
1283 rc = -1;
1284 goto cleanup;
1285 }
1286 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 +02001287 rc = -1;
1288 goto cleanup;
1289 }
1290
1291 /* accept new connection on the bind */
1292 if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) {
1293 ERR(session, "SSH failed to accept a new connection (%s).", ssh_get_error(sbind));
1294 rc = -1;
1295 goto cleanup;
1296 }
1297 sock = -1;
1298
romanf578cd52023-10-19 09:47:40 +02001299 /* set to non-blocking */
Michal Vasko09d700f2022-09-08 10:21:40 +02001300 ssh_set_blocking(session->ti.libssh.session, 0);
1301
1302 if (timeout > -1) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001303 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko09d700f2022-09-08 10:21:40 +02001304 }
1305 while ((r = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) {
1306 /* this tends to take longer */
1307 usleep(NC_TIMEOUT_STEP * 20);
Michal Vaskod8a74192023-02-06 15:51:50 +01001308 if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001309 break;
1310 }
1311 }
1312 if (r == SSH_AGAIN) {
1313 ERR(session, "SSH key exchange timeout.");
1314 rc = 0;
1315 goto cleanup;
1316 } else if (r != SSH_OK) {
roman4cc0cd52023-04-14 09:12:29 +02001317 err_msg = ssh_get_error(session->ti.libssh.session);
1318 if (err_msg[0] == '\0') {
1319 err_msg = "hostkey algorithm generated from the hostkey most likely not found in the set of configured hostkey algorithms";
1320 }
1321 ERR(session, "SSH key exchange error (%s).", err_msg);
Michal Vasko09d700f2022-09-08 10:21:40 +02001322 rc = -1;
1323 goto cleanup;
1324 }
1325
1326 /* authenticate */
1327 if ((rc = nc_accept_ssh_session_auth(session, opts)) != 1) {
1328 goto cleanup;
1329 }
1330
Michal Vasko09d700f2022-09-08 10:21:40 +02001331 /* open channel and request 'netconf' subsystem */
romanf578cd52023-10-19 09:47:40 +02001332 if ((rc = nc_accept_ssh_session_open_netconf_channel(session, opts, timeout)) != 1) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001333 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001334 }
1335
Michal Vasko09d700f2022-09-08 10:21:40 +02001336cleanup:
1337 if (sock > -1) {
1338 close(sock);
1339 }
1340 ssh_bind_free(sbind);
1341 return rc;
Michal Vasko086311b2016-01-08 09:53:11 +01001342}
1343
Michal Vasko71090fc2016-05-24 16:37:28 +02001344API NC_MSG_TYPE
1345nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session)
1346{
1347 NC_MSG_TYPE msgtype;
1348 struct nc_session *new_session = NULL;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001349 struct timespec ts_cur;
Michal Vasko71090fc2016-05-24 16:37:28 +02001350
roman40672412023-05-04 11:10:22 +02001351 NC_CHECK_ARG_RET(orig_session, orig_session, session, NC_MSG_ERROR);
Michal Vasko71090fc2016-05-24 16:37:28 +02001352
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001353 if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) &&
1354 orig_session->ti.libssh.next) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001355 for (new_session = orig_session->ti.libssh.next;
1356 new_session != orig_session;
1357 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001358 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1359 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001360 /* we found our session */
1361 break;
1362 }
1363 }
1364 if (new_session == orig_session) {
1365 new_session = NULL;
1366 }
1367 }
1368
1369 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001370 ERR(orig_session, "Session does not have a NETCONF SSH channel ready.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001371 return NC_MSG_ERROR;
1372 }
1373
1374 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001375 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001376
1377 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001378 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001379 if (msgtype != NC_MSG_HELLO) {
1380 return msgtype;
1381 }
1382
Michal Vaskod8a74192023-02-06 15:51:50 +01001383 nc_realtime_get(&ts_cur);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001384 new_session->opts.server.session_start = ts_cur.tv_sec;
Michal Vaskod8a74192023-02-06 15:51:50 +01001385 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001386 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko71090fc2016-05-24 16:37:28 +02001387 new_session->status = NC_STATUS_RUNNING;
1388 *session = new_session;
1389
1390 return msgtype;
1391}
1392
1393API NC_MSG_TYPE
Michal Vasko96164bf2016-01-21 15:41:58 +01001394nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +01001395{
Michal Vaskobdcf2362016-07-26 11:35:43 +02001396 uint8_t q_id;
Michal Vasko71090fc2016-05-24 16:37:28 +02001397 NC_MSG_TYPE msgtype;
Michal Vaskoe4300a82017-05-24 10:35:42 +02001398 struct nc_session *new_session = NULL, *cur_session;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001399 struct timespec ts_cur;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001400 uint16_t i;
Michal Vasko086311b2016-01-08 09:53:11 +01001401
roman40672412023-05-04 11:10:22 +02001402 NC_CHECK_ARG_RET(NULL, ps, session, NC_MSG_ERROR);
Michal Vasko086311b2016-01-08 09:53:11 +01001403
Michal Vasko48a63ed2016-03-01 09:48:21 +01001404 /* LOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001405 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001406 return NC_MSG_ERROR;
Michal Vaskof04a52a2016-04-07 10:52:10 +02001407 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001408
Michal Vasko96164bf2016-01-21 15:41:58 +01001409 for (i = 0; i < ps->session_count; ++i) {
fanchanghu3d4e7212017-08-09 09:42:30 +08001410 cur_session = ps->sessions[i]->session;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001411 if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH) &&
1412 cur_session->ti.libssh.next) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001413 /* an SSH session with more channels */
Michal Vaskoe4300a82017-05-24 10:35:42 +02001414 for (new_session = cur_session->ti.libssh.next;
1415 new_session != cur_session;
Michal Vasko96164bf2016-01-21 15:41:58 +01001416 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001417 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1418 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001419 /* we found our session */
1420 break;
1421 }
1422 }
Michal Vaskoe4300a82017-05-24 10:35:42 +02001423 if (new_session != cur_session) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001424 break;
1425 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001426
Michal Vasko96164bf2016-01-21 15:41:58 +01001427 new_session = NULL;
1428 }
1429 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001430
Michal Vasko48a63ed2016-03-01 09:48:21 +01001431 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001432 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001433
Michal Vasko96164bf2016-01-21 15:41:58 +01001434 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001435 ERR(NULL, "No session with a NETCONF SSH channel ready was found.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001436 return NC_MSG_ERROR;
Michal Vasko96164bf2016-01-21 15:41:58 +01001437 }
1438
1439 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001440 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vaskofb89d772016-01-08 12:25:35 +01001441
Michal Vasko086311b2016-01-08 09:53:11 +01001442 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001443 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001444 if (msgtype != NC_MSG_HELLO) {
1445 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001446 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001447
Michal Vaskod8a74192023-02-06 15:51:50 +01001448 nc_realtime_get(&ts_cur);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001449 new_session->opts.server.session_start = ts_cur.tv_sec;
Michal Vaskod8a74192023-02-06 15:51:50 +01001450 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001451 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko086311b2016-01-08 09:53:11 +01001452 new_session->status = NC_STATUS_RUNNING;
Michal Vasko96164bf2016-01-21 15:41:58 +01001453 *session = new_session;
Michal Vasko086311b2016-01-08 09:53:11 +01001454
Michal Vasko71090fc2016-05-24 16:37:28 +02001455 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001456}