blob: 77953c6144650d13ac125b175ba658ab36b1ca43 [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
roman068eb402023-11-02 15:27:04 +010018#include "config.h" /* Expose HAVE_LIBPAM */
apropp-molex4e903c32020-04-20 03:06:58 -040019
Michal Vasko0d81c572022-09-26 10:39:31 +020020#ifdef HAVE_LIBPAM
roman068eb402023-11-02 15:27:04 +010021# include <security/pam_appl.h>
Michal Vasko0d81c572022-09-26 10:39:31 +020022#endif
apropp-molex4e903c32020-04-20 03:06:58 -040023
romanf578cd52023-10-19 09:47:40 +020024#include <arpa/inet.h>
25#include <assert.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020026#include <errno.h>
romanf578cd52023-10-19 09:47:40 +020027#include <libssh/libssh.h>
28#include <libssh/server.h>
29#include <libyang/libyang.h>
30#include <openssl/bio.h>
31#include <openssl/err.h>
32#include <openssl/evp.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020033#include <pwd.h>
romanf578cd52023-10-19 09:47:40 +020034#include <stdint.h>
Michal Vasko086311b2016-01-08 09:53:11 +010035#include <stdlib.h>
36#include <string.h>
Michal Vasko27252692017-03-21 15:34:13 +010037#include <sys/stat.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020038#include <sys/types.h>
Michal Vasko9f6275e2017-10-05 13:50:05 +020039#include <time.h>
Claus Klein22091912020-01-20 13:45:47 +010040#include <unistd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010041
Michal Vasko7a20d2e2021-05-19 16:40:23 +020042#include "compat.h"
romanf578cd52023-10-19 09:47:40 +020043#include "log_p.h"
44#include "session.h"
45#include "session_p.h"
Michal Vasko086311b2016-01-08 09:53:11 +010046
47extern struct nc_server_opts server_opts;
Michal Vaskob05053d2016-01-22 16:12:06 +010048
Michal Vasko4c1fb492017-01-30 14:31:07 +010049static char *
romanf578cd52023-10-19 09:47:40 +020050base64der_privkey_to_tmp_file(const char *in, const char *privkey_format)
Michal Vasko086311b2016-01-08 09:53:11 +010051{
Michal Vasko4c1fb492017-01-30 14:31:07 +010052 char path[12] = "/tmp/XXXXXX";
53 int fd, written;
romanf578cd52023-10-19 09:47:40 +020054 unsigned len;
Michal Vasko27252692017-03-21 15:34:13 +010055 mode_t umode;
Michal Vasko4c1fb492017-01-30 14:31:07 +010056 FILE *file;
57
romanf578cd52023-10-19 09:47:40 +020058 NC_CHECK_ARG_RET(NULL, in, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +010059
mekleob31878b2019-09-09 14:10:47 +020060 umode = umask(0177);
Michal Vasko4c1fb492017-01-30 14:31:07 +010061 fd = mkstemp(path);
Michal Vasko27252692017-03-21 15:34:13 +010062 umask(umode);
Michal Vasko4c1fb492017-01-30 14:31:07 +010063 if (fd == -1) {
64 return NULL;
65 }
66
Michal Vasko3964a832018-09-18 14:37:39 +020067 file = fdopen(fd, "w");
Michal Vasko4c1fb492017-01-30 14:31:07 +010068 if (!file) {
69 close(fd);
70 return NULL;
71 }
72
romanf578cd52023-10-19 09:47:40 +020073 /* write header */
74 written = fwrite("-----BEGIN ", 1, 11, file);
75 if (privkey_format) {
76 written += fwrite(privkey_format, 1, strlen(privkey_format), file);
Michal Vasko68177b72020-04-27 15:46:53 +020077 written += fwrite(" PRIVATE KEY-----\n", 1, 18, file);
Michal Vasko68177b72020-04-27 15:46:53 +020078 } else {
romanf578cd52023-10-19 09:47:40 +020079 written += fwrite("PRIVATE KEY-----\n", 1, 17, file);
80 }
Michal Vasko68177b72020-04-27 15:46:53 +020081
romanf578cd52023-10-19 09:47:40 +020082 /* write data */
83 written += fwrite(in, 1, strlen(in), file);
84
85 /* write footer */
86 written += fwrite("\n-----END ", 1, 10, file);
87 if (privkey_format) {
88 written += fwrite(privkey_format, 1, strlen(privkey_format), file);
89 written += fwrite(" PRIVATE KEY-----", 1, 17, file);
90 } else {
91 written += fwrite("PRIVATE KEY-----", 1, 16, file);
92 }
93
94 fclose(file);
95
96 /* checksum */
97 if (privkey_format) {
98 len = 11 + strlen(privkey_format) + 18 + strlen(in) + 10 + strlen(privkey_format) + 17;
99 } else {
100 len = 11 + 17 + strlen(in) + 10 + 16;
101 }
102
103 if ((unsigned)written != len) {
104 unlink(path);
105 return NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100106 }
107
108 return strdup(path);
109}
110
111static int
romanf578cd52023-10-19 09:47:40 +0200112nc_server_ssh_ks_ref_get_key(const char *referenced_name, struct nc_asymmetric_key **askey)
Michal Vasko4c1fb492017-01-30 14:31:07 +0100113{
romanf578cd52023-10-19 09:47:40 +0200114 uint16_t i;
115 struct nc_keystore *ks = &server_opts.keystore;
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100116
romanf578cd52023-10-19 09:47:40 +0200117 *askey = NULL;
Michal Vaskod45e25a2016-01-08 15:48:44 +0100118
romanf578cd52023-10-19 09:47:40 +0200119 /* lookup name */
120 for (i = 0; i < ks->asym_key_count; i++) {
121 if (!strcmp(referenced_name, ks->asym_keys[i].name)) {
122 break;
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100123 }
124 }
125
romanf578cd52023-10-19 09:47:40 +0200126 if (i == ks->asym_key_count) {
127 ERR(NULL, "Keystore entry \"%s\" not found.", referenced_name);
128 return 1;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200129 }
Michal Vasko7d255882017-02-09 13:35:08 +0100130
romanf578cd52023-10-19 09:47:40 +0200131 *askey = &ks->asym_keys[i];
132
133 /* check if the referenced public key is SubjectPublicKeyInfo */
134 if ((*askey)->pubkey_data && nc_is_pk_subject_public_key_info((*askey)->pubkey_data)) {
135 ERR(NULL, "The public key of the referenced hostkey \"%s\" is in the SubjectPublicKeyInfo format, "
136 "which is not allowed in the SSH!", referenced_name);
137 return 1;
Michal Vasko7d255882017-02-09 13:35:08 +0100138 }
Michal Vaskoe2713da2016-08-22 16:06:40 +0200139
Michal Vasko5fcc7142016-02-02 12:21:10 +0100140 return 0;
Michal Vaskob05053d2016-01-22 16:12:06 +0100141}
142
romanf578cd52023-10-19 09:47:40 +0200143static int
144nc_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 +0100145{
romanf578cd52023-10-19 09:47:40 +0200146 uint16_t i, j;
147 struct nc_truststore *ts = &server_opts.truststore;
Michal Vasko3031aae2016-01-27 16:07:18 +0100148
romanf578cd52023-10-19 09:47:40 +0200149 *pubkeys = NULL;
150 *pubkey_count = 0;
151
152 /* lookup name */
153 for (i = 0; i < ts->pub_bag_count; i++) {
154 if (!strcmp(referenced_name, ts->pub_bags[i].name)) {
155 break;
156 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100157 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200158
romanf578cd52023-10-19 09:47:40 +0200159 if (i == ts->pub_bag_count) {
160 ERR(NULL, "Truststore entry \"%s\" not found.", referenced_name);
161 return 1;
162 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200163
romanf578cd52023-10-19 09:47:40 +0200164 /* check if any of the referenced public keys is SubjectPublicKeyInfo */
165 for (j = 0; j < ts->pub_bags[i].pubkey_count; j++) {
166 if (nc_is_pk_subject_public_key_info(ts->pub_bags[i].pubkeys[j].data)) {
167 ERR(NULL, "A public key of the referenced public key bag \"%s\" is in the SubjectPublicKeyInfo format, "
168 "which is not allowed in the SSH!", referenced_name);
169 return 1;
170 }
171 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100172
romanf578cd52023-10-19 09:47:40 +0200173 *pubkeys = ts->pub_bags[i].pubkeys;
174 *pubkey_count = ts->pub_bags[i].pubkey_count;
175 return 0;
Michal Vaskob05053d2016-01-22 16:12:06 +0100176}
177
Michal Vaskof3c41e32022-09-09 11:22:21 +0200178/**
179 * @brief Compare hashed password with a cleartext password for a match.
180 *
181 * @param[in] pass_hash Hashed password.
182 * @param[in] pass_clear Cleartext password.
183 * @return 0 on match.
184 * @return non-zero if not a match.
185 */
Michal Vasko086311b2016-01-08 09:53:11 +0100186static int
roman814f5112023-10-19 15:51:16 +0200187auth_password_compare_pwd(const char *stored_pw, const char *received_pw)
Michal Vasko086311b2016-01-08 09:53:11 +0100188{
roman814f5112023-10-19 15:51:16 +0200189 char *received_pw_hash = NULL;
roman8b1a6c32023-10-26 13:35:22 +0200190 struct crypt_data cdata = {0};
Michal Vasko086311b2016-01-08 09:53:11 +0100191
roman814f5112023-10-19 15:51:16 +0200192 if (!stored_pw[0]) {
193 if (!received_pw[0]) {
Michal Vasko05532772021-06-03 12:12:38 +0200194 WRN(NULL, "User authentication successful with an empty password!");
Michal Vasko086311b2016-01-08 09:53:11 +0100195 return 0;
196 } else {
197 /* the user did now know he does not need any password,
198 * (which should not be used) so deny authentication */
199 return 1;
200 }
201 }
202
roman814f5112023-10-19 15:51:16 +0200203 if (!strncmp(stored_pw, "$0$", 3)) {
204 /* cleartext password, simply compare the values */
205 return strcmp(stored_pw + 3, received_pw);
206 }
207
roman814f5112023-10-19 15:51:16 +0200208 received_pw_hash = crypt_r(received_pw, stored_pw, &cdata);
roman814f5112023-10-19 15:51:16 +0200209 if (!received_pw_hash) {
roman8b1a6c32023-10-26 13:35:22 +0200210 ERR(NULL, "Hashing the password failed (%s).", strerror(errno));
Andrew Langefeld158d6fd2018-06-11 18:51:44 -0500211 return 1;
212 }
213
roman814f5112023-10-19 15:51:16 +0200214 return strcmp(received_pw_hash, stored_pw);
Michal Vasko086311b2016-01-08 09:53:11 +0100215}
216
romanf578cd52023-10-19 09:47:40 +0200217static int
218nc_sshcb_auth_password(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100219{
Michal Vaskoebba7602018-03-23 13:14:08 +0100220 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100221
roman9130fc12023-11-03 13:56:23 +0100222 auth_ret = auth_password_compare_pwd(auth_client->password, ssh_message_auth_password(msg));
Michal Vasko086311b2016-01-08 09:53:11 +0100223
romanf578cd52023-10-19 09:47:40 +0200224 if (auth_ret) {
Michal Vaskoebba7602018-03-23 13:14:08 +0100225 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200226 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
227 session->opts.server.ssh_auth_attempts);
Michal Vaskoebba7602018-03-23 13:14:08 +0100228 ssh_message_reply_default(msg);
229 }
romanf578cd52023-10-19 09:47:40 +0200230
231 return auth_ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100232}
233
Michal Vasko0d81c572022-09-26 10:39:31 +0200234#ifdef HAVE_LIBPAM
235
roman41a11e42022-06-22 09:27:08 +0200236/**
237 * @brief PAM conversation function, which serves as a callback for exchanging messages between the client and a PAM module.
238 *
239 * @param[in] n_messages Number of messages.
240 * @param[in] msg PAM module's messages.
241 * @param[out] resp User responses.
242 * @param[in] appdata_ptr Callback's data.
243 * @return PAM_SUCCESS on success;
244 * @return PAM_BUF_ERR on memory allocation error;
245 * @return PAM_CONV_ERR otherwise.
246 */
247static int
248nc_pam_conv_clb(int n_messages, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)
249{
250 int i, j, t, r = PAM_SUCCESS, n_answers, n_requests = n_messages;
251 const char **prompts = NULL;
252 char *echo = NULL;
253 const char *name = "Keyboard-Interactive Authentication";
254 const char *instruction = "Please enter your authentication token";
255 ssh_message reply = NULL;
256 struct nc_pam_thread_arg *clb_data = appdata_ptr;
257 ssh_session libssh_session;
258 struct timespec ts_timeout;
259 struct nc_server_ssh_opts *opts;
260
261 libssh_session = clb_data->session->ti.libssh.session;
romanf578cd52023-10-19 09:47:40 +0200262 opts = clb_data->opts;
roman41a11e42022-06-22 09:27:08 +0200263
264 /* PAM_MAX_NUM_MSG == 32 by default */
265 if ((n_messages <= 0) || (n_messages >= PAM_MAX_NUM_MSG)) {
266 ERR(NULL, "Bad number of PAM messages (#%d).", n_messages);
267 r = PAM_CONV_ERR;
268 goto cleanup;
269 }
270
271 /* only accepting these 4 types of messages */
272 for (i = 0; i < n_messages; i++) {
273 t = msg[i]->msg_style;
274 if ((t != PAM_PROMPT_ECHO_OFF) && (t != PAM_PROMPT_ECHO_ON) && (t != PAM_TEXT_INFO) && (t != PAM_ERROR_MSG)) {
275 ERR(NULL, "PAM conversation callback received an unexpected type of message.");
276 r = PAM_CONV_ERR;
277 goto cleanup;
278 }
279 }
280
281 /* display messages with errors and/or some information and count the amount of actual authentication challenges */
282 for (i = 0; i < n_messages; i++) {
283 if (msg[i]->msg_style == PAM_TEXT_INFO) {
284 VRB(NULL, "PAM conversation callback received a message with some information for the client (%s).", msg[i]->msg);
285 n_requests--;
286 }
287 if (msg[i]->msg_style == PAM_ERROR_MSG) {
288 ERR(NULL, "PAM conversation callback received an error message (%s).", msg[i]->msg);
289 r = PAM_CONV_ERR;
290 goto cleanup;
291 }
292 }
293
294 /* there are no requests left for the user, only messages with some information for the client were sent */
295 if (n_requests <= 0) {
296 r = PAM_SUCCESS;
297 goto cleanup;
298 }
299
300 /* it is the PAM module's responsibility to release both, this array and the responses themselves */
301 *resp = calloc(n_requests, sizeof **resp);
302 prompts = calloc(n_requests, sizeof *prompts);
303 echo = calloc(n_requests, sizeof *echo);
roman3a95bb22023-10-26 11:07:17 +0200304 NC_CHECK_ERRMEM_GOTO(!(*resp) || !prompts || !echo, r = PAM_BUF_ERR, cleanup);
roman41a11e42022-06-22 09:27:08 +0200305
306 /* set the prompts for the user */
307 j = 0;
308 for (i = 0; i < n_messages; i++) {
309 if ((msg[i]->msg_style == PAM_PROMPT_ECHO_ON) || (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF)) {
310 prompts[j++] = msg[i]->msg;
311 }
312 }
313
314 /* iterate over all the messages and adjust the echo array accordingly */
315 j = 0;
316 for (i = 0; i < n_messages; i++) {
317 if (msg[i]->msg_style == PAM_PROMPT_ECHO_ON) {
318 echo[j++] = 1;
319 }
320 if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF) {
321 /* no need to set to 0 because of calloc */
322 j++;
323 }
324 }
325
326 /* print all the keyboard-interactive challenges to the user */
327 r = ssh_message_auth_interactive_request(clb_data->msg, name, instruction, n_requests, prompts, echo);
328 if (r != SSH_OK) {
329 ERR(NULL, "Failed to send an authentication request.");
330 r = PAM_CONV_ERR;
331 goto cleanup;
332 }
333
334 if (opts->auth_timeout) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100335 nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000);
roman41a11e42022-06-22 09:27:08 +0200336 }
337
338 /* get user's replies */
339 do {
340 if (!nc_session_is_connected(clb_data->session)) {
341 ERR(NULL, "Communication SSH socket unexpectedly closed.");
342 r = PAM_CONV_ERR;
343 goto cleanup;
344 }
345
346 reply = ssh_message_get(libssh_session);
347 if (reply) {
348 break;
349 }
350
351 usleep(NC_TIMEOUT_STEP);
romanea0edaa2023-10-26 12:16:25 +0200352 } while (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) >= 1));
roman41a11e42022-06-22 09:27:08 +0200353
354 if (!reply) {
355 ERR(NULL, "Authentication timeout.");
356 r = PAM_CONV_ERR;
357 goto cleanup;
358 }
359
360 /* check if the amount of replies matches the amount of requests */
361 n_answers = ssh_userauth_kbdint_getnanswers(libssh_session);
362 if (n_answers != n_requests) {
363 ERR(NULL, "Expected %d response(s), got %d.", n_requests, n_answers);
364 r = PAM_CONV_ERR;
365 goto cleanup;
366 }
367
368 /* give the replies to a PAM module */
369 for (i = 0; i < n_answers; i++) {
370 (*resp)[i].resp = strdup(ssh_userauth_kbdint_getanswer(libssh_session, i));
371 /* it should be the caller's responsibility to free this, however if mem alloc fails,
372 * it is safer to free the responses here and set them to NULL */
373 if ((*resp)[i].resp == NULL) {
374 for (j = 0; j < i; j++) {
375 free((*resp)[j].resp);
376 (*resp)[j].resp = NULL;
377 }
378 ERRMEM;
379 r = PAM_BUF_ERR;
380 goto cleanup;
381 }
382 }
383
384cleanup:
385 ssh_message_free(reply);
386 free(prompts);
387 free(echo);
388 return r;
389}
390
391/**
392 * @brief Handles authentication via Linux PAM.
393 *
394 * @param[in] session NETCONF session.
395 * @param[in] ssh_msg SSH message with a keyboard-interactive authentication request.
396 * @return PAM_SUCCESS on success;
397 * @return PAM error otherwise.
398 */
399static int
romanf578cd52023-10-19 09:47:40 +0200400nc_pam_auth(struct nc_session *session, struct nc_server_ssh_opts *opts, ssh_message ssh_msg)
roman41a11e42022-06-22 09:27:08 +0200401{
402 pam_handle_t *pam_h = NULL;
403 int ret;
404 struct nc_pam_thread_arg clb_data;
405 struct pam_conv conv;
romanf578cd52023-10-19 09:47:40 +0200406 uint16_t i;
roman41a11e42022-06-22 09:27:08 +0200407
408 /* structure holding callback's data */
409 clb_data.msg = ssh_msg;
410 clb_data.session = session;
romanf578cd52023-10-19 09:47:40 +0200411 clb_data.opts = opts;
roman41a11e42022-06-22 09:27:08 +0200412
413 /* PAM conversation structure holding the callback and it's data */
414 conv.conv = nc_pam_conv_clb;
415 conv.appdata_ptr = &clb_data;
416
romanf578cd52023-10-19 09:47:40 +0200417 /* get the current client's configuration file */
418 for (i = 0; i < opts->client_count; i++) {
419 if (!strcmp(opts->auth_clients[i].username, session->username)) {
420 break;
421 }
422 }
423
424 if (i == opts->client_count) {
425 ERR(NULL, "User \"%s\" not found.", session->username);
426 ret = 1;
427 goto cleanup;
428 }
429
430 if (!opts->auth_clients[i].pam_config_name) {
431 ERR(NULL, "User's \"%s\" PAM configuration filename not set.");
432 ret = 1;
433 goto cleanup;
434 }
435
roman41a11e42022-06-22 09:27:08 +0200436 /* initialize PAM and see if the given configuration file exists */
Michal Vasko0d81c572022-09-26 10:39:31 +0200437# ifdef LIBPAM_HAVE_CONFDIR
roman41a11e42022-06-22 09:27:08 +0200438 /* PAM version >= 1.4 */
romanf578cd52023-10-19 09:47:40 +0200439 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 +0200440# else
roman41a11e42022-06-22 09:27:08 +0200441 /* PAM version < 1.4 */
romanf578cd52023-10-19 09:47:40 +0200442 ret = pam_start(opts->auth_clients[i].pam_config_name, session->username, &conv, &pam_h);
Michal Vasko0d81c572022-09-26 10:39:31 +0200443# endif
roman41a11e42022-06-22 09:27:08 +0200444 if (ret != PAM_SUCCESS) {
445 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
446 goto cleanup;
447 }
448
449 /* authentication based on the modules listed in the configuration file */
450 ret = pam_authenticate(pam_h, 0);
451 if (ret != PAM_SUCCESS) {
452 if (ret == PAM_ABORT) {
453 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
454 goto cleanup;
455 } else {
456 VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
457 goto cleanup;
458 }
459 }
460
461 /* correct token entered, check other requirements(the time of the day, expired token, ...) */
462 ret = pam_acct_mgmt(pam_h, 0);
463 if ((ret != PAM_SUCCESS) && (ret != PAM_NEW_AUTHTOK_REQD)) {
464 VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
465 goto cleanup;
466 }
467
468 /* if a token has expired a new one will be generated */
469 if (ret == PAM_NEW_AUTHTOK_REQD) {
470 VRB(NULL, "PAM warning occurred (%s).\n", pam_strerror(pam_h, ret));
471 ret = pam_chauthtok(pam_h, PAM_CHANGE_EXPIRED_AUTHTOK);
472 if (ret == PAM_SUCCESS) {
473 VRB(NULL, "The authentication token of user \"%s\" updated successfully.", session->username);
474 } else {
475 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
476 goto cleanup;
477 }
478 }
479
480cleanup:
481 /* destroy the PAM context */
482 if (pam_end(pam_h, ret) != PAM_SUCCESS) {
483 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
484 }
485 return ret;
486}
487
Michal Vasko0d81c572022-09-26 10:39:31 +0200488#endif
489
romanf578cd52023-10-19 09:47:40 +0200490static int
491nc_sshcb_auth_kbdint(struct nc_session *session, struct nc_server_ssh_opts *opts, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100492{
bhart3bc2f582018-06-05 12:40:32 -0500493 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100494
romanf578cd52023-10-19 09:47:40 +0200495 if (server_opts.interactive_auth_clb) {
496 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 +0200497 } else {
498#ifdef HAVE_LIBPAM
romanf578cd52023-10-19 09:47:40 +0200499 if (nc_pam_auth(session, opts, msg) == PAM_SUCCESS) {
Michal Vasko0d81c572022-09-26 10:39:31 +0200500 auth_ret = 0;
501 }
502#else
503 ERR(session, "PAM-based SSH authentication is not supported.");
504#endif
bhart1bb7cdb2018-07-02 15:03:30 -0500505 }
506
507 /* Authenticate message based on outcome */
romanf578cd52023-10-19 09:47:40 +0200508 if (auth_ret) {
bhart1bb7cdb2018-07-02 15:03:30 -0500509 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200510 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
511 session->opts.server.ssh_auth_attempts);
bhart1bb7cdb2018-07-02 15:03:30 -0500512 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +0100513 }
romanf578cd52023-10-19 09:47:40 +0200514
515 return auth_ret;
516}
517
518/*
519 * Get the public key type from binary data stored in buffer.
520 * The data is in the form of: 4 bytes = data length, then data of data length
521 * and the data is in network byte order. The key has to be in the SSH2 format.
522 */
523static const char *
524nc_server_ssh_get_pubkey_type(const char *buffer, uint32_t *len)
525{
526 uint32_t type_len;
527
528 /* copy the 4 bytes */
529 memcpy(&type_len, buffer, sizeof type_len);
530 /* type_len now stores the length of the key type */
531 type_len = ntohl(type_len);
532 *len = type_len;
533
534 /* move 4 bytes in the buffer, this is where the type should be */
535 buffer += sizeof type_len;
536 return buffer;
537}
538
539/**
540 * @brief Create ssh key from base64 pubkey data.
541 *
542 * @param[in] base64 base64 encoded public key.
543 * @param[out] key created ssh key.
544 * @return 0 on success, 1 otherwise.
545 */
546static int
547nc_server_ssh_create_ssh_pubkey(const char *base64, ssh_key *key)
548{
549 int ret = 0;
550 char *bin = NULL;
551 const char *pub_type = NULL;
552 uint32_t pub_type_len = 0;
553
554 if (!key && !base64) {
555 ERRINT;
556 ret = 1;
557 goto cleanup;
558 }
559
560 *key = NULL;
561
562 /* convert base64 to binary */
563 if (nc_base64_to_bin(base64, &bin) == -1) {
564 ERR(NULL, "Unable to decode base64.");
565 ret = 1;
566 goto cleanup;
567 }
568
569 /* get the key type and try to import it if possible */
570 pub_type = nc_server_ssh_get_pubkey_type(bin, &pub_type_len);
571 if (!pub_type) {
572 ret = 1;
573 goto cleanup;
574 } else if (!strncmp(pub_type, "ssh-dss", pub_type_len)) {
575 ERR(NULL, "DSA keys are not supported.");
576 ret = 1;
577 goto cleanup;
578 } else if (!strncmp(pub_type, "ssh-rsa", pub_type_len)) {
579 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_RSA, key);
580 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp256", pub_type_len)) {
581 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P256, key);
582 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp384", pub_type_len)) {
583 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P384, key);
584 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp521", pub_type_len)) {
585 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P521, key);
586 } else if (!strncmp(pub_type, "ssh-ed25519", pub_type_len)) {
587 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ED25519, key);
588 } else {
589 ERR(NULL, "Public key type not recognised.");
590 ret = 1;
591 goto cleanup;
592 }
593
594cleanup:
595 if (ret != SSH_OK) {
596 ERR(NULL, "Error importing public key.");
597 }
598 free(bin);
599 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100600}
601
Michal Vaskof3c41e32022-09-09 11:22:21 +0200602/**
603 * @brief Compare SSH key with configured authorized keys and return the username of the matching one, if any.
604 *
605 * @param[in] key Presented SSH key to compare.
606 * @return Authorized key username, NULL if no match was found.
607 */
romanf578cd52023-10-19 09:47:40 +0200608static int
609auth_pubkey_compare_key(ssh_key key, struct nc_auth_client *auth_client)
Michal Vasko086311b2016-01-08 09:53:11 +0100610{
romanf578cd52023-10-19 09:47:40 +0200611 uint16_t i, pubkey_count;
Michal Vasko3e9d1682017-02-24 09:50:15 +0100612 int ret = 0;
romanf578cd52023-10-19 09:47:40 +0200613 ssh_key new_key = NULL;
614 struct nc_public_key *pubkeys;
Michal Vasko086311b2016-01-08 09:53:11 +0100615
romanf578cd52023-10-19 09:47:40 +0200616 /* get the correct public key storage */
617 if (auth_client->store == NC_STORE_LOCAL) {
618 pubkeys = auth_client->pubkeys;
619 pubkey_count = auth_client->pubkey_count;
620 } else {
621 ret = nc_server_ssh_ts_ref_get_keys(auth_client->ts_ref, &pubkeys, &pubkey_count);
622 if (ret) {
623 ERR(NULL, "Error getting \"%s\"'s public keys from the truststore.", auth_client->username);
624 return ret;
Michal Vasko17dfda92016-12-01 14:06:16 +0100625 }
romanf578cd52023-10-19 09:47:40 +0200626 }
Michal Vasko17dfda92016-12-01 14:06:16 +0100627
romanf578cd52023-10-19 09:47:40 +0200628 /* try to compare all of the client's keys with the key received in the SSH message */
629 for (i = 0; i < pubkey_count; i++) {
630 /* create the SSH key from the data */
631 if (nc_server_ssh_create_ssh_pubkey(pubkeys[i].data, &new_key)) {
632 ssh_key_free(new_key);
Michal Vasko086311b2016-01-08 09:53:11 +0100633 continue;
634 }
635
romanf578cd52023-10-19 09:47:40 +0200636 /* compare the keys */
637 ret = ssh_key_cmp(key, new_key, SSH_KEY_CMP_PUBLIC);
638 if (!ret) {
Michal Vasko086311b2016-01-08 09:53:11 +0100639 break;
romanf578cd52023-10-19 09:47:40 +0200640 } else {
641 WRN(NULL, "User's \"%s\" public key doesn't match, trying another.", auth_client->username);
642 ssh_key_free(new_key);
Michal Vasko086311b2016-01-08 09:53:11 +0100643 }
Michal Vasko086311b2016-01-08 09:53:11 +0100644 }
645
romanf578cd52023-10-19 09:47:40 +0200646 if (i == pubkey_count) {
647 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100648 }
649
romanf578cd52023-10-19 09:47:40 +0200650 if (!ret) {
651 /* only free a key if everything was ok, it would have already been freed otherwise */
652 ssh_key_free(new_key);
653 }
Michal Vaskoa05c7b12017-01-30 14:33:08 +0100654
romanf578cd52023-10-19 09:47:40 +0200655 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100656}
657
658static void
romanf578cd52023-10-19 09:47:40 +0200659nc_sshcb_auth_none(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100660{
romanf578cd52023-10-19 09:47:40 +0200661 if (auth_client->supports_none && !auth_client->password && !auth_client->pubkey_count && !auth_client->pam_config_name) {
662 /* only authenticate the client if he supports none and no other method */
663 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
664 VRB(session, "User \"%s\" authenticated.", session->username);
665 ssh_message_auth_reply_success(msg, 0);
666 }
667
668 ssh_message_reply_default(msg);
669}
670
671static int
672nc_sshcb_auth_pubkey(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
673{
674 int signature_state, ret = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100675
roman9130fc12023-11-03 13:56:23 +0100676 if (auth_pubkey_compare_key(ssh_message_auth_pubkey(msg), auth_client)) {
677 VRB(session, "User \"%s\" tried to use an unknown (unauthorized) public key.", session->username);
678 ret = 1;
679 goto fail;
Michal Vaskobd13a932016-09-14 09:00:35 +0200680 }
Michal Vaskobd13a932016-09-14 09:00:35 +0200681
Michal Vasko086311b2016-01-08 09:53:11 +0100682 signature_state = ssh_message_auth_publickey_state(msg);
romanf578cd52023-10-19 09:47:40 +0200683 if (signature_state == SSH_PUBLICKEY_STATE_NONE) {
Michal Vaskobd13a932016-09-14 09:00:35 +0200684 /* accepting only the use of a public key */
685 ssh_message_auth_reply_pk_ok_simple(msg);
romanf578cd52023-10-19 09:47:40 +0200686 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100687 }
688
romanf578cd52023-10-19 09:47:40 +0200689 return ret;
Michal Vaskobd13a932016-09-14 09:00:35 +0200690
691fail:
Michal Vasko2e6defd2016-10-07 15:48:15 +0200692 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200693 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
694 session->opts.server.ssh_auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +0100695 ssh_message_reply_default(msg);
romanf578cd52023-10-19 09:47:40 +0200696
697 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100698}
699
700static int
Michal Vasko96164bf2016-01-21 15:41:58 +0100701nc_sshcb_channel_open(struct nc_session *session, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100702{
Michal Vasko96164bf2016-01-21 15:41:58 +0100703 ssh_channel chan;
704
705 /* first channel request */
706 if (!session->ti.libssh.channel) {
707 if (session->status != NC_STATUS_STARTING) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100708 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100709 return -1;
710 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100711 chan = ssh_message_channel_request_open_reply_accept(msg);
712 if (!chan) {
Michal Vasko05532772021-06-03 12:12:38 +0200713 ERR(session, "Failed to create a new SSH channel.");
Michal Vasko96164bf2016-01-21 15:41:58 +0100714 return -1;
715 }
716 session->ti.libssh.channel = chan;
Michal Vasko086311b2016-01-08 09:53:11 +0100717
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200718 /* additional channel request */
Michal Vasko96164bf2016-01-21 15:41:58 +0100719 } else {
720 chan = ssh_message_channel_request_open_reply_accept(msg);
721 if (!chan) {
Michal Vasko05532772021-06-03 12:12:38 +0200722 ERR(session, "Session %u: failed to create a new SSH channel.", session->id);
Michal Vasko96164bf2016-01-21 15:41:58 +0100723 return -1;
724 }
725 /* channel was created and libssh stored it internally in the ssh_session structure, good enough */
Michal Vasko086311b2016-01-08 09:53:11 +0100726 }
727
Michal Vasko086311b2016-01-08 09:53:11 +0100728 return 0;
729}
730
731static int
732nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem)
733{
Michal Vasko96164bf2016-01-21 15:41:58 +0100734 struct nc_session *new_session;
Michal Vasko086311b2016-01-08 09:53:11 +0100735
Michal Vasko96164bf2016-01-21 15:41:58 +0100736 if (strcmp(subsystem, "netconf")) {
Michal Vasko05532772021-06-03 12:12:38 +0200737 WRN(session, "Received an unknown subsystem \"%s\" request.", subsystem);
Michal Vasko086311b2016-01-08 09:53:11 +0100738 return -1;
739 }
740
Michal Vasko96164bf2016-01-21 15:41:58 +0100741 if (session->ti.libssh.channel == channel) {
742 /* first channel requested */
743 if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) {
744 ERRINT;
745 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100746 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100747 if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) {
Michal Vasko05532772021-06-03 12:12:38 +0200748 ERR(session, "Subsystem \"netconf\" requested for the second time.");
Michal Vasko96164bf2016-01-21 15:41:58 +0100749 return -1;
750 }
751
752 session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF;
Michal Vasko086311b2016-01-08 09:53:11 +0100753 } else {
Michal Vasko96164bf2016-01-21 15:41:58 +0100754 /* additional channel subsystem request, new session is ready as far as SSH is concerned */
Michal Vasko131120a2018-05-29 15:44:02 +0200755 new_session = nc_new_session(NC_SERVER, 1);
roman3a95bb22023-10-26 11:07:17 +0200756 NC_CHECK_ERRMEM_RET(!new_session, -1);
Michal Vasko96164bf2016-01-21 15:41:58 +0100757
758 /* insert the new session */
759 if (!session->ti.libssh.next) {
760 new_session->ti.libssh.next = session;
761 } else {
762 new_session->ti.libssh.next = session->ti.libssh.next;
763 }
764 session->ti.libssh.next = new_session;
765
766 new_session->status = NC_STATUS_STARTING;
Michal Vasko96164bf2016-01-21 15:41:58 +0100767 new_session->ti_type = NC_TI_LIBSSH;
Michal Vasko131120a2018-05-29 15:44:02 +0200768 new_session->io_lock = session->io_lock;
Michal Vasko96164bf2016-01-21 15:41:58 +0100769 new_session->ti.libssh.channel = channel;
770 new_session->ti.libssh.session = session->ti.libssh.session;
Michal Vasko93224072021-11-09 12:14:28 +0100771 new_session->username = strdup(session->username);
772 new_session->host = strdup(session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100773 new_session->port = session->port;
Michal Vasko93224072021-11-09 12:14:28 +0100774 new_session->ctx = (struct ly_ctx *)session->ctx;
Michal Vasko83d15322018-09-27 09:44:02 +0200775 new_session->flags = NC_SESSION_SSH_AUTHENTICATED | NC_SESSION_SSH_SUBSYS_NETCONF | NC_SESSION_SHAREDCTX;
Michal Vasko086311b2016-01-08 09:53:11 +0100776 }
777
778 return 0;
779}
780
Michal Vasko96164bf2016-01-21 15:41:58 +0100781int
romanf578cd52023-10-19 09:47:40 +0200782nc_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 +0100783{
784 const char *str_type, *str_subtype = NULL, *username;
romanf578cd52023-10-19 09:47:40 +0200785 int subtype, type, libssh_auth_methods = 0, ret = 0;
786 uint16_t i;
787 struct nc_auth_client *auth_client = NULL;
roman78df0fa2023-11-02 10:33:57 +0100788 struct nc_endpt *referenced_endpt;
Michal Vasko086311b2016-01-08 09:53:11 +0100789
790 type = ssh_message_type(msg);
791 subtype = ssh_message_subtype(msg);
792
793 switch (type) {
794 case SSH_REQUEST_AUTH:
795 str_type = "request-auth";
796 switch (subtype) {
797 case SSH_AUTH_METHOD_NONE:
798 str_subtype = "none";
799 break;
800 case SSH_AUTH_METHOD_PASSWORD:
801 str_subtype = "password";
802 break;
803 case SSH_AUTH_METHOD_PUBLICKEY:
804 str_subtype = "publickey";
805 break;
806 case SSH_AUTH_METHOD_HOSTBASED:
807 str_subtype = "hostbased";
808 break;
809 case SSH_AUTH_METHOD_INTERACTIVE:
810 str_subtype = "interactive";
811 break;
812 case SSH_AUTH_METHOD_GSSAPI_MIC:
813 str_subtype = "gssapi-mic";
814 break;
815 }
816 break;
817
818 case SSH_REQUEST_CHANNEL_OPEN:
819 str_type = "request-channel-open";
820 switch (subtype) {
821 case SSH_CHANNEL_SESSION:
822 str_subtype = "session";
823 break;
824 case SSH_CHANNEL_DIRECT_TCPIP:
825 str_subtype = "direct-tcpip";
826 break;
827 case SSH_CHANNEL_FORWARDED_TCPIP:
828 str_subtype = "forwarded-tcpip";
829 break;
830 case (int)SSH_CHANNEL_X11:
831 str_subtype = "channel-x11";
832 break;
833 case SSH_CHANNEL_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200834 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100835 default:
836 str_subtype = "unknown";
837 break;
838 }
839 break;
840
841 case SSH_REQUEST_CHANNEL:
842 str_type = "request-channel";
843 switch (subtype) {
844 case SSH_CHANNEL_REQUEST_PTY:
845 str_subtype = "pty";
846 break;
847 case SSH_CHANNEL_REQUEST_EXEC:
848 str_subtype = "exec";
849 break;
850 case SSH_CHANNEL_REQUEST_SHELL:
851 str_subtype = "shell";
852 break;
853 case SSH_CHANNEL_REQUEST_ENV:
854 str_subtype = "env";
855 break;
856 case SSH_CHANNEL_REQUEST_SUBSYSTEM:
857 str_subtype = "subsystem";
858 break;
859 case SSH_CHANNEL_REQUEST_WINDOW_CHANGE:
860 str_subtype = "window-change";
861 break;
862 case SSH_CHANNEL_REQUEST_X11:
863 str_subtype = "x11";
864 break;
865 case SSH_CHANNEL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200866 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100867 default:
868 str_subtype = "unknown";
869 break;
870 }
871 break;
872
873 case SSH_REQUEST_SERVICE:
874 str_type = "request-service";
875 str_subtype = ssh_message_service_service(msg);
876 break;
877
878 case SSH_REQUEST_GLOBAL:
879 str_type = "request-global";
880 switch (subtype) {
881 case SSH_GLOBAL_REQUEST_TCPIP_FORWARD:
882 str_subtype = "tcpip-forward";
883 break;
884 case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD:
885 str_subtype = "cancel-tcpip-forward";
886 break;
887 case SSH_GLOBAL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200888 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +0100889 default:
890 str_subtype = "unknown";
891 break;
892 }
893 break;
894
895 default:
896 str_type = "unknown";
897 str_subtype = "unknown";
898 break;
899 }
900
Michal Vasko05532772021-06-03 12:12:38 +0200901 VRB(session, "Received an SSH message \"%s\" of subtype \"%s\".", str_type, str_subtype);
Michal Vasko5e0edd82020-07-29 15:26:13 +0200902 if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) {
Michal Vaskoce319162016-02-03 15:33:08 +0100903 /* "valid" situation if, for example, receiving some auth or channel request timeouted,
904 * but we got it now, during session free */
Michal Vasko05532772021-06-03 12:12:38 +0200905 VRB(session, "SSH message arrived on a %s session, the request will be denied.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200906 (session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid"));
Michal Vaskoce319162016-02-03 15:33:08 +0100907 ssh_message_reply_default(msg);
908 return 0;
909 }
Michal Vasko086311b2016-01-08 09:53:11 +0100910
911 /*
912 * process known messages
913 */
914 if (type == SSH_REQUEST_AUTH) {
915 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko05532772021-06-03 12:12:38 +0200916 ERR(session, "User \"%s\" authenticated, but requested another authentication.", session->username);
Michal Vasko086311b2016-01-08 09:53:11 +0100917 ssh_message_reply_default(msg);
918 return 0;
romanf578cd52023-10-19 09:47:40 +0200919 } else if (!state || !opts) {
920 /* these two parameters should always be set during an authentication,
921 * however do a check just in case something goes really wrong, since they
922 * are not needed for other types of messages
923 */
924 ERRINT;
925 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100926 }
927
Michal Vasko086311b2016-01-08 09:53:11 +0100928 /* save the username, do not let the client change it */
929 username = ssh_message_auth_user(msg);
romanf578cd52023-10-19 09:47:40 +0200930 assert(username);
931
932 for (i = 0; i < opts->client_count; i++) {
933 if (!strcmp(opts->auth_clients[i].username, username)) {
934 auth_client = &opts->auth_clients[i];
935 break;
936 }
937 }
938
939 if (!auth_client) {
roman78df0fa2023-11-02 10:33:57 +0100940 if (opts->referenced_endpt_name) {
941 /* client not known by the endpt, but it references another one so try it */
942 if (nc_server_get_referenced_endpt(opts->referenced_endpt_name, &referenced_endpt)) {
943 ERRINT;
944 return 1;
945 }
946
947 return nc_session_ssh_msg(session, referenced_endpt->opts.ssh, msg, state);
Michal Vasko086311b2016-01-08 09:53:11 +0100948 }
949
romanf578cd52023-10-19 09:47:40 +0200950 ERR(NULL, "User \"%s\" not known by the server.", username);
951 ssh_message_reply_default(msg);
952 return 0;
953 }
954
955 if (!session->username) {
Michal Vasko93224072021-11-09 12:14:28 +0100956 session->username = strdup(username);
romanf578cd52023-10-19 09:47:40 +0200957
958 /* configure and count accepted auth methods */
959 if (auth_client->store == NC_STORE_LOCAL) {
960 if (auth_client->pubkey_count) {
961 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
962 }
963 } else if (auth_client->ts_ref) {
964 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
965 }
966 if (auth_client->password) {
967 state->auth_method_count++;
968 libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD;
969 }
970 if (auth_client->pam_config_name) {
971 state->auth_method_count++;
972 libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE;
973 }
974 if (auth_client->supports_none) {
975 libssh_auth_methods |= SSH_AUTH_METHOD_NONE;
976 }
977
978 if (libssh_auth_methods & SSH_AUTH_METHOD_PUBLICKEY) {
979 state->auth_method_count++;
980 }
981
982 ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods);
983 } else {
Michal Vasko086311b2016-01-08 09:53:11 +0100984 if (strcmp(username, session->username)) {
romanf578cd52023-10-19 09:47:40 +0200985 /* changing username not allowed */
Michal Vasko05532772021-06-03 12:12:38 +0200986 ERR(session, "User \"%s\" changed its username to \"%s\".", session->username, username);
Michal Vasko086311b2016-01-08 09:53:11 +0100987 session->status = NC_STATUS_INVALID;
Michal Vasko428087d2016-01-14 16:04:28 +0100988 session->term_reason = NC_SESSION_TERM_OTHER;
Michal Vasko086311b2016-01-08 09:53:11 +0100989 return 1;
990 }
991 }
992
romanf578cd52023-10-19 09:47:40 +0200993 /* try authenticating, the user must authenticate via all of his configured auth methods */
Michal Vasko086311b2016-01-08 09:53:11 +0100994 if (subtype == SSH_AUTH_METHOD_NONE) {
romanf578cd52023-10-19 09:47:40 +0200995 nc_sshcb_auth_none(session, auth_client, msg);
996 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100997 } else if (subtype == SSH_AUTH_METHOD_PASSWORD) {
romanf578cd52023-10-19 09:47:40 +0200998 ret = nc_sshcb_auth_password(session, auth_client, msg);
Michal Vasko086311b2016-01-08 09:53:11 +0100999 } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) {
romanf578cd52023-10-19 09:47:40 +02001000 ret = nc_sshcb_auth_pubkey(session, auth_client, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001001 } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) {
romanf578cd52023-10-19 09:47:40 +02001002 ret = nc_sshcb_auth_kbdint(session, opts, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001003 }
romanf578cd52023-10-19 09:47:40 +02001004
1005 if (!ret) {
1006 state->auth_success_count++;
1007 }
1008
1009 if (!ret && (state->auth_success_count < state->auth_method_count)) {
1010 /* success, but he needs to do another method */
1011 VRB(session, "User \"%s\" partially authenticated, but still needs to authenticate via the rest of his configured methods.", username);
1012 ssh_message_auth_reply_success(msg, 1);
1013 } else if (!ret && (state->auth_success_count == state->auth_method_count)) {
1014 /* authenticated */
1015 ssh_message_auth_reply_success(msg, 0);
1016 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
1017 VRB(session, "User \"%s\" authenticated.", username);
1018 }
1019
1020 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001021 } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko0df67562016-01-21 15:50:11 +01001022 if ((type == SSH_REQUEST_CHANNEL_OPEN) && ((enum ssh_channel_type_e)subtype == SSH_CHANNEL_SESSION)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001023 if (nc_sshcb_channel_open(session, msg)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001024 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001025 }
Michal Vasko086311b2016-01-08 09:53:11 +01001026 return 0;
Michal Vasko96164bf2016-01-21 15:41:58 +01001027
Michal Vasko0df67562016-01-21 15:50:11 +01001028 } else if ((type == SSH_REQUEST_CHANNEL) && ((enum ssh_channel_requests_e)subtype == SSH_CHANNEL_REQUEST_SUBSYSTEM)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001029 if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg),
1030 ssh_message_channel_request_subsystem(msg))) {
Michal Vasko086311b2016-01-08 09:53:11 +01001031 ssh_message_reply_default(msg);
Michal Vasko96164bf2016-01-21 15:41:58 +01001032 } else {
1033 ssh_message_channel_request_reply_success(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001034 }
1035 return 0;
1036 }
1037 }
1038
1039 /* we did not process it */
1040 return 1;
1041}
1042
Michal Vasko1a38c862016-01-15 15:50:07 +01001043/* ret 1 on success, 0 on timeout, -1 on error */
Michal Vasko086311b2016-01-08 09:53:11 +01001044static int
romanf578cd52023-10-19 09:47:40 +02001045nc_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 +01001046{
roman6ece9c52022-06-22 09:29:17 +02001047 struct timespec ts_timeout;
romanf578cd52023-10-19 09:47:40 +02001048 ssh_message msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001049
romanf578cd52023-10-19 09:47:40 +02001050 if (timeout) {
1051 nc_timeouttime_get(&ts_timeout, timeout * 1000);
Michal Vasko36c7be82017-02-22 13:37:59 +01001052 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001053 while (1) {
1054 if (!nc_session_is_connected(session)) {
romanf578cd52023-10-19 09:47:40 +02001055 ERR(session, "Communication SSH socket unexpectedly closed.");
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001056 return -1;
1057 }
1058
romanf578cd52023-10-19 09:47:40 +02001059 msg = ssh_message_get(session->ti.libssh.session);
1060 if (msg) {
1061 if (nc_session_ssh_msg(session, opts, msg, NULL)) {
1062 ssh_message_reply_default(msg);
1063 }
1064 ssh_message_free(msg);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001065 }
1066
romanf578cd52023-10-19 09:47:40 +02001067 if (session->ti.libssh.channel && session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) {
Michal Vasko1a38c862016-01-15 15:50:07 +01001068 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001069 }
1070
Michal Vasko086311b2016-01-08 09:53:11 +01001071 usleep(NC_TIMEOUT_STEP);
romanea0edaa2023-10-26 12:16:25 +02001072 if (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
roman6ece9c52022-06-22 09:29:17 +02001073 /* timeout */
1074 ERR(session, "Failed to start \"netconf\" SSH subsystem for too long, disconnecting.");
1075 break;
Michal Vasko36c7be82017-02-22 13:37:59 +01001076 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001077 }
Michal Vasko086311b2016-01-08 09:53:11 +01001078
Michal Vasko1a38c862016-01-15 15:50:07 +01001079 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001080}
1081
Michal Vaskof3c41e32022-09-09 11:22:21 +02001082/**
1083 * @brief Set hostkeys to be used for an SSH bind.
1084 *
1085 * @param[in] sbind SSH bind to use.
1086 * @param[in] hostkeys Array of hostkeys.
1087 * @param[in] hostkey_count Count of @p hostkeys.
1088 * @return 0 on success.
1089 * @return -1 on error.
1090 */
Michal Vasko4c1fb492017-01-30 14:31:07 +01001091static int
romanf578cd52023-10-19 09:47:40 +02001092nc_ssh_bind_add_hostkeys(ssh_bind sbind, struct nc_server_ssh_opts *opts, uint16_t hostkey_count)
Michal Vasko4c1fb492017-01-30 14:31:07 +01001093{
romanf578cd52023-10-19 09:47:40 +02001094 uint16_t i;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001095 char *privkey_path, *privkey_data;
Michal Vaskoddce1212019-05-24 09:58:49 +02001096 int ret;
romanf578cd52023-10-19 09:47:40 +02001097 struct nc_asymmetric_key *key = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001098
1099 for (i = 0; i < hostkey_count; ++i) {
1100 privkey_path = privkey_data = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001101
romanf578cd52023-10-19 09:47:40 +02001102 /* get the asymmetric key */
1103 if (opts->hostkeys[i].store == NC_STORE_LOCAL) {
1104 /* stored locally */
1105 key = &opts->hostkeys[i].key;
1106 } else {
1107 /* keystore reference, need to get it */
1108 if (nc_server_ssh_ks_ref_get_key(opts->hostkeys[i].ks_ref, &key)) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001109 return -1;
1110 }
1111 }
1112
romanf578cd52023-10-19 09:47:40 +02001113 privkey_path = base64der_privkey_to_tmp_file(key->privkey_data, nc_privkey_format_to_str(key->privkey_type));
1114 if (!privkey_path) {
1115 ERR(NULL, "Temporarily storing a host key into a file failed (%s).", strerror(errno));
1116 return -1;
1117 }
1118
Michal Vasko4c1fb492017-01-30 14:31:07 +01001119 ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path);
1120
1121 /* cleanup */
1122 if (privkey_data && unlink(privkey_path)) {
Michal Vasko05532772021-06-03 12:12:38 +02001123 WRN(NULL, "Removing a temporary host key file \"%s\" failed (%s).", privkey_path, strerror(errno));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001124 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001125
1126 if (ret != SSH_OK) {
romanf578cd52023-10-19 09:47:40 +02001127 ERR(NULL, "Failed to set hostkey \"%s\" (%s).", opts->hostkeys[i].name, privkey_path);
Michal Vasko80075de2017-07-10 11:38:52 +02001128 }
1129 free(privkey_path);
1130
1131 if (ret != SSH_OK) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001132 return -1;
1133 }
1134 }
1135
1136 return 0;
1137}
1138
Michal Vasko09d700f2022-09-08 10:21:40 +02001139static int
romanf578cd52023-10-19 09:47:40 +02001140nc_accept_ssh_session_auth(struct nc_session *session, struct nc_server_ssh_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001141{
roman6ece9c52022-06-22 09:29:17 +02001142 struct timespec ts_timeout;
roman41a11e42022-06-22 09:27:08 +02001143 ssh_message msg;
romanf578cd52023-10-19 09:47:40 +02001144 struct nc_auth_state state = {0};
Michal Vasko086311b2016-01-08 09:53:11 +01001145
Michal Vasko086311b2016-01-08 09:53:11 +01001146 /* authenticate */
Michal Vasko36c7be82017-02-22 13:37:59 +01001147 if (opts->auth_timeout) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001148 nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000);
Michal Vasko36c7be82017-02-22 13:37:59 +01001149 }
1150 while (1) {
Michal Vasko2a7d4732016-01-15 09:24:46 +01001151 if (!nc_session_is_connected(session)) {
Michal Vasko05532772021-06-03 12:12:38 +02001152 ERR(session, "Communication SSH socket unexpectedly closed.");
Michal Vasko2a7d4732016-01-15 09:24:46 +01001153 return -1;
1154 }
1155
roman41a11e42022-06-22 09:27:08 +02001156 msg = ssh_message_get(session->ti.libssh.session);
1157 if (msg) {
romanf578cd52023-10-19 09:47:40 +02001158 if (nc_session_ssh_msg(session, opts, msg, &state)) {
roman41a11e42022-06-22 09:27:08 +02001159 ssh_message_reply_default(msg);
1160 }
1161 ssh_message_free(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001162 }
1163
Michal Vasko36c7be82017-02-22 13:37:59 +01001164 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
1165 break;
1166 }
1167
Michal Vasko145ae672017-02-07 10:57:27 +01001168 if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) {
Michal Vasko05532772021-06-03 12:12:38 +02001169 ERR(session, "Too many failed authentication attempts of user \"%s\".", session->username);
Michal Vasko145ae672017-02-07 10:57:27 +01001170 return -1;
1171 }
1172
Michal Vasko086311b2016-01-08 09:53:11 +01001173 usleep(NC_TIMEOUT_STEP);
romanea0edaa2023-10-26 12:16:25 +02001174 if (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
roman6ece9c52022-06-22 09:29:17 +02001175 /* timeout */
1176 break;
Michal Vasko36c7be82017-02-22 13:37:59 +01001177 }
1178 }
Michal Vasko086311b2016-01-08 09:53:11 +01001179
1180 if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) {
1181 /* timeout */
Michal Vaskoc13da702017-02-07 10:57:57 +01001182 if (session->username) {
Michal Vasko05532772021-06-03 12:12:38 +02001183 ERR(session, "User \"%s\" failed to authenticate for too long, disconnecting.", session->username);
Michal Vaskoc13da702017-02-07 10:57:57 +01001184 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001185 ERR(session, "User failed to authenticate for too long, disconnecting.");
Michal Vaskoc13da702017-02-07 10:57:57 +01001186 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001187 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001188 }
1189
Michal Vasko09d700f2022-09-08 10:21:40 +02001190 return 1;
1191}
1192
1193int
romanf578cd52023-10-19 09:47:40 +02001194nc_accept_ssh_session(struct nc_session *session, struct nc_server_ssh_opts *opts, int sock, int timeout)
Michal Vasko09d700f2022-09-08 10:21:40 +02001195{
1196 ssh_bind sbind = NULL;
Michal Vasko09d700f2022-09-08 10:21:40 +02001197 int rc = 1, r;
1198 struct timespec ts_timeout;
roman4cc0cd52023-04-14 09:12:29 +02001199 const char *err_msg;
Michal Vasko09d700f2022-09-08 10:21:40 +02001200
Michal Vasko09d700f2022-09-08 10:21:40 +02001201 /* other transport-specific data */
1202 session->ti_type = NC_TI_LIBSSH;
1203 session->ti.libssh.session = ssh_new();
1204 if (!session->ti.libssh.session) {
1205 ERR(NULL, "Failed to initialize a new SSH session.");
1206 rc = -1;
1207 goto cleanup;
1208 }
1209
1210 sbind = ssh_bind_new();
1211 if (!sbind) {
1212 ERR(session, "Failed to create an SSH bind.");
1213 rc = -1;
1214 goto cleanup;
1215 }
1216
1217 /* configure host keys */
romanf578cd52023-10-19 09:47:40 +02001218 if (nc_ssh_bind_add_hostkeys(sbind, opts, opts->hostkey_count)) {
1219 rc = -1;
1220 goto cleanup;
1221 }
1222
1223 /* configure supported algorithms */
1224 if (opts->hostkey_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, opts->hostkey_algs)) {
1225 rc = -1;
1226 goto cleanup;
1227 }
1228 if (opts->encryption_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_CIPHERS_S_C, opts->encryption_algs)) {
1229 rc = -1;
1230 goto cleanup;
1231 }
1232 if (opts->kex_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_KEY_EXCHANGE, opts->kex_algs)) {
1233 rc = -1;
1234 goto cleanup;
1235 }
1236 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 +02001237 rc = -1;
1238 goto cleanup;
1239 }
1240
1241 /* accept new connection on the bind */
1242 if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) {
1243 ERR(session, "SSH failed to accept a new connection (%s).", ssh_get_error(sbind));
1244 rc = -1;
1245 goto cleanup;
1246 }
1247 sock = -1;
1248
romanf578cd52023-10-19 09:47:40 +02001249 /* set to non-blocking */
Michal Vasko09d700f2022-09-08 10:21:40 +02001250 ssh_set_blocking(session->ti.libssh.session, 0);
1251
1252 if (timeout > -1) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001253 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko09d700f2022-09-08 10:21:40 +02001254 }
1255 while ((r = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) {
1256 /* this tends to take longer */
1257 usleep(NC_TIMEOUT_STEP * 20);
Michal Vaskod8a74192023-02-06 15:51:50 +01001258 if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001259 break;
1260 }
1261 }
1262 if (r == SSH_AGAIN) {
1263 ERR(session, "SSH key exchange timeout.");
1264 rc = 0;
1265 goto cleanup;
1266 } else if (r != SSH_OK) {
roman4cc0cd52023-04-14 09:12:29 +02001267 err_msg = ssh_get_error(session->ti.libssh.session);
1268 if (err_msg[0] == '\0') {
1269 err_msg = "hostkey algorithm generated from the hostkey most likely not found in the set of configured hostkey algorithms";
1270 }
1271 ERR(session, "SSH key exchange error (%s).", err_msg);
Michal Vasko09d700f2022-09-08 10:21:40 +02001272 rc = -1;
1273 goto cleanup;
1274 }
1275
1276 /* authenticate */
1277 if ((rc = nc_accept_ssh_session_auth(session, opts)) != 1) {
1278 goto cleanup;
1279 }
1280
Michal Vasko09d700f2022-09-08 10:21:40 +02001281 /* open channel and request 'netconf' subsystem */
romanf578cd52023-10-19 09:47:40 +02001282 if ((rc = nc_accept_ssh_session_open_netconf_channel(session, opts, timeout)) != 1) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001283 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001284 }
1285
Michal Vasko09d700f2022-09-08 10:21:40 +02001286cleanup:
1287 if (sock > -1) {
1288 close(sock);
1289 }
1290 ssh_bind_free(sbind);
1291 return rc;
Michal Vasko086311b2016-01-08 09:53:11 +01001292}
1293
Michal Vasko71090fc2016-05-24 16:37:28 +02001294API NC_MSG_TYPE
1295nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session)
1296{
1297 NC_MSG_TYPE msgtype;
1298 struct nc_session *new_session = NULL;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001299 struct timespec ts_cur;
Michal Vasko71090fc2016-05-24 16:37:28 +02001300
roman40672412023-05-04 11:10:22 +02001301 NC_CHECK_ARG_RET(orig_session, orig_session, session, NC_MSG_ERROR);
Michal Vasko71090fc2016-05-24 16:37:28 +02001302
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001303 if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) &&
1304 orig_session->ti.libssh.next) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001305 for (new_session = orig_session->ti.libssh.next;
1306 new_session != orig_session;
1307 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001308 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1309 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001310 /* we found our session */
1311 break;
1312 }
1313 }
1314 if (new_session == orig_session) {
1315 new_session = NULL;
1316 }
1317 }
1318
1319 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001320 ERR(orig_session, "Session does not have a NETCONF SSH channel ready.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001321 return NC_MSG_ERROR;
1322 }
1323
1324 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001325 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001326
1327 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001328 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001329 if (msgtype != NC_MSG_HELLO) {
1330 return msgtype;
1331 }
1332
Michal Vaskod8a74192023-02-06 15:51:50 +01001333 nc_realtime_get(&ts_cur);
roman44efa322023-11-03 13:57:25 +01001334 new_session->opts.server.session_start = ts_cur;
Michal Vaskod8a74192023-02-06 15:51:50 +01001335 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001336 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko71090fc2016-05-24 16:37:28 +02001337 new_session->status = NC_STATUS_RUNNING;
1338 *session = new_session;
1339
1340 return msgtype;
1341}
1342
1343API NC_MSG_TYPE
Michal Vasko96164bf2016-01-21 15:41:58 +01001344nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +01001345{
Michal Vaskobdcf2362016-07-26 11:35:43 +02001346 uint8_t q_id;
Michal Vasko71090fc2016-05-24 16:37:28 +02001347 NC_MSG_TYPE msgtype;
Michal Vaskoe4300a82017-05-24 10:35:42 +02001348 struct nc_session *new_session = NULL, *cur_session;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001349 struct timespec ts_cur;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001350 uint16_t i;
Michal Vasko086311b2016-01-08 09:53:11 +01001351
roman40672412023-05-04 11:10:22 +02001352 NC_CHECK_ARG_RET(NULL, ps, session, NC_MSG_ERROR);
Michal Vasko086311b2016-01-08 09:53:11 +01001353
Michal Vasko48a63ed2016-03-01 09:48:21 +01001354 /* LOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001355 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001356 return NC_MSG_ERROR;
Michal Vaskof04a52a2016-04-07 10:52:10 +02001357 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001358
Michal Vasko96164bf2016-01-21 15:41:58 +01001359 for (i = 0; i < ps->session_count; ++i) {
fanchanghu3d4e7212017-08-09 09:42:30 +08001360 cur_session = ps->sessions[i]->session;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001361 if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH) &&
1362 cur_session->ti.libssh.next) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001363 /* an SSH session with more channels */
Michal Vaskoe4300a82017-05-24 10:35:42 +02001364 for (new_session = cur_session->ti.libssh.next;
1365 new_session != cur_session;
Michal Vasko96164bf2016-01-21 15:41:58 +01001366 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001367 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1368 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001369 /* we found our session */
1370 break;
1371 }
1372 }
Michal Vaskoe4300a82017-05-24 10:35:42 +02001373 if (new_session != cur_session) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001374 break;
1375 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001376
Michal Vasko96164bf2016-01-21 15:41:58 +01001377 new_session = NULL;
1378 }
1379 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001380
Michal Vasko48a63ed2016-03-01 09:48:21 +01001381 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001382 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001383
Michal Vasko96164bf2016-01-21 15:41:58 +01001384 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001385 ERR(NULL, "No session with a NETCONF SSH channel ready was found.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001386 return NC_MSG_ERROR;
Michal Vasko96164bf2016-01-21 15:41:58 +01001387 }
1388
1389 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001390 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vaskofb89d772016-01-08 12:25:35 +01001391
Michal Vasko086311b2016-01-08 09:53:11 +01001392 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001393 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001394 if (msgtype != NC_MSG_HELLO) {
1395 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001396 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001397
Michal Vaskod8a74192023-02-06 15:51:50 +01001398 nc_realtime_get(&ts_cur);
roman44efa322023-11-03 13:57:25 +01001399 new_session->opts.server.session_start = ts_cur;
Michal Vaskod8a74192023-02-06 15:51:50 +01001400 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001401 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko086311b2016-01-08 09:53:11 +01001402 new_session->status = NC_STATUS_RUNNING;
Michal Vasko96164bf2016-01-21 15:41:58 +01001403 *session = new_session;
Michal Vasko086311b2016-01-08 09:53:11 +01001404
Michal Vasko71090fc2016-05-24 16:37:28 +02001405 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001406}