blob: 8f86fae9053c3bbd1ed0c7f2346d847545dcb7ae [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
romanc6518422023-11-30 16:39:00 +010018#include "config.h" /* Expose HAVE_LIBPAM and HAVE_SHADOW */
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
romanc6518422023-11-30 16:39:00 +010023#ifdef HAVE_SHADOW
24# include <shadow.h>
25#endif
apropp-molex4e903c32020-04-20 03:06:58 -040026
romanf578cd52023-10-19 09:47:40 +020027#include <arpa/inet.h>
28#include <assert.h>
romana9ec3362023-12-21 10:59:57 +010029#include <ctype.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020030#include <errno.h>
romanf578cd52023-10-19 09:47:40 +020031#include <libssh/libssh.h>
32#include <libssh/server.h>
33#include <libyang/libyang.h>
34#include <openssl/bio.h>
35#include <openssl/err.h>
36#include <openssl/evp.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020037#include <pwd.h>
romanf578cd52023-10-19 09:47:40 +020038#include <stdint.h>
Michal Vasko086311b2016-01-08 09:53:11 +010039#include <stdlib.h>
40#include <string.h>
Michal Vasko27252692017-03-21 15:34:13 +010041#include <sys/stat.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020042#include <sys/types.h>
Michal Vasko9f6275e2017-10-05 13:50:05 +020043#include <time.h>
Claus Klein22091912020-01-20 13:45:47 +010044#include <unistd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010045
Michal Vasko7a20d2e2021-05-19 16:40:23 +020046#include "compat.h"
romanf578cd52023-10-19 09:47:40 +020047#include "log_p.h"
48#include "session.h"
49#include "session_p.h"
Michal Vasko086311b2016-01-08 09:53:11 +010050
51extern struct nc_server_opts server_opts;
Michal Vaskob05053d2016-01-22 16:12:06 +010052
Michal Vasko4c1fb492017-01-30 14:31:07 +010053static char *
romanf578cd52023-10-19 09:47:40 +020054base64der_privkey_to_tmp_file(const char *in, const char *privkey_format)
Michal Vasko086311b2016-01-08 09:53:11 +010055{
Michal Vasko4c1fb492017-01-30 14:31:07 +010056 char path[12] = "/tmp/XXXXXX";
57 int fd, written;
romanf578cd52023-10-19 09:47:40 +020058 unsigned len;
Michal Vasko27252692017-03-21 15:34:13 +010059 mode_t umode;
Michal Vasko4c1fb492017-01-30 14:31:07 +010060 FILE *file;
61
romanf578cd52023-10-19 09:47:40 +020062 NC_CHECK_ARG_RET(NULL, in, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +010063
mekleob31878b2019-09-09 14:10:47 +020064 umode = umask(0177);
Michal Vasko4c1fb492017-01-30 14:31:07 +010065 fd = mkstemp(path);
Michal Vasko27252692017-03-21 15:34:13 +010066 umask(umode);
Michal Vasko4c1fb492017-01-30 14:31:07 +010067 if (fd == -1) {
68 return NULL;
69 }
70
Michal Vasko3964a832018-09-18 14:37:39 +020071 file = fdopen(fd, "w");
Michal Vasko4c1fb492017-01-30 14:31:07 +010072 if (!file) {
73 close(fd);
74 return NULL;
75 }
76
romanf578cd52023-10-19 09:47:40 +020077 /* write header */
78 written = fwrite("-----BEGIN ", 1, 11, file);
79 if (privkey_format) {
80 written += fwrite(privkey_format, 1, strlen(privkey_format), file);
Michal Vasko68177b72020-04-27 15:46:53 +020081 written += fwrite(" PRIVATE KEY-----\n", 1, 18, file);
Michal Vasko68177b72020-04-27 15:46:53 +020082 } else {
romanf578cd52023-10-19 09:47:40 +020083 written += fwrite("PRIVATE KEY-----\n", 1, 17, file);
84 }
Michal Vasko68177b72020-04-27 15:46:53 +020085
romanf578cd52023-10-19 09:47:40 +020086 /* write data */
87 written += fwrite(in, 1, strlen(in), file);
88
89 /* write footer */
90 written += fwrite("\n-----END ", 1, 10, file);
91 if (privkey_format) {
92 written += fwrite(privkey_format, 1, strlen(privkey_format), file);
93 written += fwrite(" PRIVATE KEY-----", 1, 17, file);
94 } else {
95 written += fwrite("PRIVATE KEY-----", 1, 16, file);
96 }
97
98 fclose(file);
99
100 /* checksum */
101 if (privkey_format) {
102 len = 11 + strlen(privkey_format) + 18 + strlen(in) + 10 + strlen(privkey_format) + 17;
103 } else {
104 len = 11 + 17 + strlen(in) + 10 + 16;
105 }
106
107 if ((unsigned)written != len) {
108 unlink(path);
109 return NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100110 }
111
112 return strdup(path);
113}
114
115static int
romanf578cd52023-10-19 09:47:40 +0200116nc_server_ssh_ks_ref_get_key(const char *referenced_name, struct nc_asymmetric_key **askey)
Michal Vasko4c1fb492017-01-30 14:31:07 +0100117{
romanf578cd52023-10-19 09:47:40 +0200118 uint16_t i;
119 struct nc_keystore *ks = &server_opts.keystore;
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100120
romanf578cd52023-10-19 09:47:40 +0200121 *askey = NULL;
Michal Vaskod45e25a2016-01-08 15:48:44 +0100122
romanf578cd52023-10-19 09:47:40 +0200123 /* lookup name */
124 for (i = 0; i < ks->asym_key_count; i++) {
125 if (!strcmp(referenced_name, ks->asym_keys[i].name)) {
126 break;
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100127 }
128 }
129
romanf578cd52023-10-19 09:47:40 +0200130 if (i == ks->asym_key_count) {
131 ERR(NULL, "Keystore entry \"%s\" not found.", referenced_name);
132 return 1;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200133 }
Michal Vasko7d255882017-02-09 13:35:08 +0100134
romanf578cd52023-10-19 09:47:40 +0200135 *askey = &ks->asym_keys[i];
136
137 /* check if the referenced public key is SubjectPublicKeyInfo */
138 if ((*askey)->pubkey_data && nc_is_pk_subject_public_key_info((*askey)->pubkey_data)) {
139 ERR(NULL, "The public key of the referenced hostkey \"%s\" is in the SubjectPublicKeyInfo format, "
140 "which is not allowed in the SSH!", referenced_name);
141 return 1;
Michal Vasko7d255882017-02-09 13:35:08 +0100142 }
Michal Vaskoe2713da2016-08-22 16:06:40 +0200143
Michal Vasko5fcc7142016-02-02 12:21:10 +0100144 return 0;
Michal Vaskob05053d2016-01-22 16:12:06 +0100145}
146
romanf578cd52023-10-19 09:47:40 +0200147static int
148nc_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 +0100149{
romanf578cd52023-10-19 09:47:40 +0200150 uint16_t i, j;
151 struct nc_truststore *ts = &server_opts.truststore;
Michal Vasko3031aae2016-01-27 16:07:18 +0100152
romanf578cd52023-10-19 09:47:40 +0200153 *pubkeys = NULL;
154 *pubkey_count = 0;
155
156 /* lookup name */
157 for (i = 0; i < ts->pub_bag_count; i++) {
158 if (!strcmp(referenced_name, ts->pub_bags[i].name)) {
159 break;
160 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100161 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200162
romanf578cd52023-10-19 09:47:40 +0200163 if (i == ts->pub_bag_count) {
164 ERR(NULL, "Truststore entry \"%s\" not found.", referenced_name);
165 return 1;
166 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200167
romanf578cd52023-10-19 09:47:40 +0200168 /* check if any of the referenced public keys is SubjectPublicKeyInfo */
169 for (j = 0; j < ts->pub_bags[i].pubkey_count; j++) {
170 if (nc_is_pk_subject_public_key_info(ts->pub_bags[i].pubkeys[j].data)) {
171 ERR(NULL, "A public key of the referenced public key bag \"%s\" is in the SubjectPublicKeyInfo format, "
172 "which is not allowed in the SSH!", referenced_name);
173 return 1;
174 }
175 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100176
romanf578cd52023-10-19 09:47:40 +0200177 *pubkeys = ts->pub_bags[i].pubkeys;
178 *pubkey_count = ts->pub_bags[i].pubkey_count;
179 return 0;
Michal Vaskob05053d2016-01-22 16:12:06 +0100180}
181
romana9ec3362023-12-21 10:59:57 +0100182static char *
183nc_server_ssh_uid_to_str(uid_t uid)
184{
185 int buf_len;
186 char *uid_str;
187
188 /* get the number of digits and alloc */
189 buf_len = snprintf(NULL, 0, "%u", uid);
190 uid_str = malloc(buf_len + 1);
191 NC_CHECK_ERRMEM_RET(!uid_str, NULL);
192
193 /* convert to string */
194 sprintf(uid_str, "%u", uid);
195 uid_str[buf_len] = '\0';
196 return uid_str;
197}
198
199static int
200nc_server_ssh_str_append(const char src_c, const char *src_str, int *size, int *idx, char **dst)
201{
202 int src_size, allocate = 0, ret;
203
204 /* get size of char/string we want to append */
205 if (src_str) {
206 src_size = strlen(src_str);
207 } else {
208 src_size = 1;
209 }
210
211 /* check if we have enough space, if not realloc */
212 while ((src_size + *idx) >= *size) {
213 (*size) += 16;
214 allocate = 1;
215 }
216 if (allocate) {
217 *dst = nc_realloc(*dst, *size);
218 NC_CHECK_ERRMEM_RET(!*dst, 1);
219 }
220
221 /* append the char/string */
222 if (src_str) {
223 ret = sprintf(*dst + *idx, "%s", src_str);
224 } else {
225 ret = sprintf(*dst + *idx, "%c", src_c);
226 }
227 if (ret < 0) {
228 return 1;
229 }
230
231 *idx += ret;
232 return 0;
233}
234
235static int
236nc_server_ssh_get_system_keys_path(const char *username, char **out_path)
237{
238 int ret = 0, i, have_percent = 0, size = 0, idx = 0;
239 const char *path_fmt = server_opts.authkey_path_fmt;
240 char *path = NULL, *buf = NULL, *uid = NULL;
241 struct passwd *pw, pw_buf;
242 size_t buf_len = 0;
243
244 /* check if the path format contains any tokens */
245 if (strstr(path_fmt, "%h") || strstr(path_fmt, "%U") || strstr(path_fmt, "%u") || strstr(path_fmt, "%%")) {
246 /* get pw */
247 pw = nc_getpw(0, username, &pw_buf, &buf, &buf_len);
248 if (!pw) {
249 ERR(NULL, "Unable to get passwd entry for user \"%s\".", username);
250 ret = 1;
251 goto cleanup;
252 }
253
254 /* convert UID to a string */
255 uid = nc_server_ssh_uid_to_str(pw->pw_uid);
256 if (!uid) {
257 ret = 1;
258 goto cleanup;
259 }
260 } else {
261 /* no tokens, just copy the path and return */
262 *out_path = strdup(path_fmt);
263 NC_CHECK_ERRMEM_RET(!*out_path, 1);
264 goto cleanup;
265 }
266
267 /* go over characters from format, copy them to path and interpret tokens correctly */
268 for (i = 0; path_fmt[i]; i++) {
269 if (have_percent) {
270 /* special token, need to convert it */
271 if (path_fmt[i] == '%') {
272 ret = nc_server_ssh_str_append('%', NULL, &size, &idx, &path);
273 } else if (path_fmt[i] == 'h') {
274 /* user home */
275 ret = nc_server_ssh_str_append(0, pw->pw_dir, &size, &idx, &path);
276 } else if (path_fmt[i] == 'u') {
277 /* username */
278 ret = nc_server_ssh_str_append(0, username, &size, &idx, &path);
279 } else if (path_fmt[i] == 'U') {
280 /* UID */
281 ret = nc_server_ssh_str_append(0, uid, &size, &idx, &path);
282 } else {
283 ERR(NULL, "Failed to parse system public keys path format \"%s\".", server_opts.authkey_path_fmt);
284 ret = 1;
285 }
286
287 have_percent = 0;
288 } else {
289 if (path_fmt[i] == '%') {
290 have_percent = 1;
291 } else {
292 /* ordinary character with no meaning */
293 ret = nc_server_ssh_str_append(path_fmt[i], NULL, &size, &idx, &path);
294 }
295 }
296
297 if (ret) {
298 free(path);
299 goto cleanup;
300 }
301 }
302
303 *out_path = path;
304cleanup:
305 free(uid);
306 free(buf);
307 return ret;
308}
309
310/* reads public keys from authorized_keys-like file */
311static int
312nc_server_ssh_read_authorized_keys_file(const char *path, struct nc_public_key **pubkeys, uint16_t *pubkey_count)
313{
314 int ret = 0, line_num = 0;
315 FILE *f = NULL;
316 char *line = NULL, *ptr, *ptr2;
317 size_t n;
318 enum ssh_keytypes_e ktype;
319
320 NC_CHECK_ARG_RET(NULL, path, pubkeys, 1);
321
322 *pubkeys = NULL;
323 *pubkey_count = 0;
324
325 f = fopen(path, "r");
326 if (!f) {
327 ERR(NULL, "Unable to open \"%s\" (%s).", path, strerror(errno));
328 ret = 1;
329 goto cleanup;
330 }
331
332 while (getline(&line, &n, f) > -1) {
333 ++line_num;
334 if ((line[0] == '#') || (line[0] == '\n')) {
335 /* comment or empty line */
336 continue;
337 }
338
339 /* separate key type */
340 ptr = line;
341 for (ptr2 = ptr; ptr2[0] && !isspace(ptr2[0]); ptr2++) {}
342 if (!ptr2[0]) {
343 ERR(NULL, "Invalid format of authorized keys file \"%s\" on line %d.", path, line_num);
344 ret = 1;
345 goto cleanup;
346 }
347 ptr2[0] = '\0';
348
349 /* detect key type */
350 ktype = ssh_key_type_from_name(ptr);
351 if ((ktype != SSH_KEYTYPE_RSA) && (ktype != SSH_KEYTYPE_ECDSA_P256) && (ktype != SSH_KEYTYPE_ECDSA_P384) &&
352 (ktype != SSH_KEYTYPE_ECDSA_P521) && (ktype != SSH_KEYTYPE_ED25519)) {
353 WRN(NULL, "Unsupported key type \"%s\" in authorized keys file \"%s\" on line %d.", ptr, path, line_num);
354 continue;
355 }
356
357 /* get key data */
358 ptr = ptr2 + 1;
359 for (ptr2 = ptr; ptr2[0] && !isspace(ptr2[0]); ptr2++) {}
360 ptr2[0] = '\0';
361
362 /* add the key */
363 *pubkeys = nc_realloc(*pubkeys, (*pubkey_count + 1) * sizeof **pubkeys);
364 NC_CHECK_ERRMEM_GOTO(!(*pubkeys), ret = 1, cleanup);
365 ret = asprintf(&(*pubkeys)[*pubkey_count].name, "authorized_key_%" PRIu16, *pubkey_count);
366 NC_CHECK_ERRMEM_GOTO(ret == -1, (*pubkeys)[*pubkey_count].name = NULL; ret = 1, cleanup);
367 (*pubkeys)[*pubkey_count].type = NC_PUBKEY_FORMAT_SSH;
368 (*pubkeys)[*pubkey_count].data = strdup(ptr);
369 NC_CHECK_ERRMEM_GOTO(!(*pubkeys)[*pubkey_count].data, ret = 1, cleanup);
370 (*pubkey_count)++;
371 }
372
373 /* ok */
374 ret = 0;
375cleanup:
376 if (f) {
377 fclose(f);
378 }
379 free(line);
380 return ret;
381}
382
383static int
384nc_server_ssh_get_system_keys(const char *username, struct nc_public_key **pubkeys, uint16_t *pubkey_count)
385{
386 int ret = 0;
387 char *path = NULL;
388
389 /* convert the path format to get the actual path */
390 ret = nc_server_ssh_get_system_keys_path(username, &path);
391 if (ret) {
392 ERR(NULL, "Getting system keys path failed.");
393 goto cleanup;
394 }
395
396 /* get the keys */
397 ret = nc_server_ssh_read_authorized_keys_file(path, pubkeys, pubkey_count);
398 if (ret) {
399 ERR(NULL, "Reading system keys failed.");
400 goto cleanup;
401 }
402
403cleanup:
404 free(path);
405 return ret;
406}
407
Michal Vaskof3c41e32022-09-09 11:22:21 +0200408/**
409 * @brief Compare hashed password with a cleartext password for a match.
410 *
411 * @param[in] pass_hash Hashed password.
412 * @param[in] pass_clear Cleartext password.
413 * @return 0 on match.
414 * @return non-zero if not a match.
415 */
Michal Vasko086311b2016-01-08 09:53:11 +0100416static int
roman814f5112023-10-19 15:51:16 +0200417auth_password_compare_pwd(const char *stored_pw, const char *received_pw)
Michal Vasko086311b2016-01-08 09:53:11 +0100418{
roman814f5112023-10-19 15:51:16 +0200419 char *received_pw_hash = NULL;
roman8b1a6c32023-10-26 13:35:22 +0200420 struct crypt_data cdata = {0};
Michal Vasko086311b2016-01-08 09:53:11 +0100421
roman814f5112023-10-19 15:51:16 +0200422 if (!stored_pw[0]) {
423 if (!received_pw[0]) {
Michal Vasko05532772021-06-03 12:12:38 +0200424 WRN(NULL, "User authentication successful with an empty password!");
Michal Vasko086311b2016-01-08 09:53:11 +0100425 return 0;
426 } else {
427 /* the user did now know he does not need any password,
428 * (which should not be used) so deny authentication */
429 return 1;
430 }
431 }
432
roman814f5112023-10-19 15:51:16 +0200433 if (!strncmp(stored_pw, "$0$", 3)) {
434 /* cleartext password, simply compare the values */
435 return strcmp(stored_pw + 3, received_pw);
436 }
437
roman814f5112023-10-19 15:51:16 +0200438 received_pw_hash = crypt_r(received_pw, stored_pw, &cdata);
roman814f5112023-10-19 15:51:16 +0200439 if (!received_pw_hash) {
roman8b1a6c32023-10-26 13:35:22 +0200440 ERR(NULL, "Hashing the password failed (%s).", strerror(errno));
Andrew Langefeld158d6fd2018-06-11 18:51:44 -0500441 return 1;
442 }
443
roman814f5112023-10-19 15:51:16 +0200444 return strcmp(received_pw_hash, stored_pw);
Michal Vasko086311b2016-01-08 09:53:11 +0100445}
446
romanf578cd52023-10-19 09:47:40 +0200447static int
448nc_sshcb_auth_password(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100449{
Michal Vaskoebba7602018-03-23 13:14:08 +0100450 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100451
roman9130fc12023-11-03 13:56:23 +0100452 auth_ret = auth_password_compare_pwd(auth_client->password, ssh_message_auth_password(msg));
Michal Vasko086311b2016-01-08 09:53:11 +0100453
romanf578cd52023-10-19 09:47:40 +0200454 if (auth_ret) {
Michal Vaskoebba7602018-03-23 13:14:08 +0100455 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200456 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
457 session->opts.server.ssh_auth_attempts);
Michal Vaskoebba7602018-03-23 13:14:08 +0100458 ssh_message_reply_default(msg);
459 }
romanf578cd52023-10-19 09:47:40 +0200460
461 return auth_ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100462}
463
romane5675b12024-03-05 14:26:23 +0100464API int
465nc_server_ssh_kbdint_get_nanswers(const struct nc_session *session, ssh_session libssh_session)
romanc6518422023-11-30 16:39:00 +0100466{
467 int ret = 0;
468 struct timespec ts_timeout = {0};
roman56c85c02023-12-07 13:09:28 +0100469 ssh_message reply = NULL;
romane5675b12024-03-05 14:26:23 +0100470 uint16_t auth_timeout = *((uint16_t *)session->data);
471
472 NC_CHECK_ARG_RET(NULL, session, libssh_session, -1);
romanc6518422023-11-30 16:39:00 +0100473
474 if (auth_timeout) {
475 nc_timeouttime_get(&ts_timeout, auth_timeout * 1000);
476 }
477
478 /* wait for answers from the client */
479 do {
480 if (!nc_session_is_connected(session)) {
481 ERR(NULL, "SSH communication socket unexpectedly closed.");
482 ret = -1;
483 goto cleanup;
484 }
485
486 reply = ssh_message_get(libssh_session);
487 if (reply) {
488 break;
489 }
490
491 usleep(NC_TIMEOUT_STEP);
492 } while (auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) >= 1));
493 if (!reply) {
494 ERR(NULL, "Authentication timeout.");
495 ret = -1;
496 goto cleanup;
497 }
498
499 ret = ssh_userauth_kbdint_getnanswers(libssh_session);
500
501cleanup:
502 ssh_message_free(reply);
503 return ret;
504}
505
Michal Vasko0d81c572022-09-26 10:39:31 +0200506#ifdef HAVE_LIBPAM
507
roman41a11e42022-06-22 09:27:08 +0200508/**
509 * @brief PAM conversation function, which serves as a callback for exchanging messages between the client and a PAM module.
510 *
511 * @param[in] n_messages Number of messages.
512 * @param[in] msg PAM module's messages.
513 * @param[out] resp User responses.
514 * @param[in] appdata_ptr Callback's data.
roman808f3f62023-11-23 16:01:04 +0100515 * @return PAM_SUCCESS on success, PAM_BUF_ERR on memory allocation error, PAM_CONV_ERR otherwise.
roman41a11e42022-06-22 09:27:08 +0200516 */
517static int
518nc_pam_conv_clb(int n_messages, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)
519{
520 int i, j, t, r = PAM_SUCCESS, n_answers, n_requests = n_messages;
521 const char **prompts = NULL;
522 char *echo = NULL;
523 const char *name = "Keyboard-Interactive Authentication";
524 const char *instruction = "Please enter your authentication token";
525 ssh_message reply = NULL;
526 struct nc_pam_thread_arg *clb_data = appdata_ptr;
527 ssh_session libssh_session;
roman41a11e42022-06-22 09:27:08 +0200528
529 libssh_session = clb_data->session->ti.libssh.session;
roman41a11e42022-06-22 09:27:08 +0200530
531 /* PAM_MAX_NUM_MSG == 32 by default */
532 if ((n_messages <= 0) || (n_messages >= PAM_MAX_NUM_MSG)) {
romanc6518422023-11-30 16:39:00 +0100533 ERR(clb_data->session, "Bad number of PAM messages (#%d).", n_messages);
roman41a11e42022-06-22 09:27:08 +0200534 r = PAM_CONV_ERR;
535 goto cleanup;
536 }
537
538 /* only accepting these 4 types of messages */
539 for (i = 0; i < n_messages; i++) {
540 t = msg[i]->msg_style;
541 if ((t != PAM_PROMPT_ECHO_OFF) && (t != PAM_PROMPT_ECHO_ON) && (t != PAM_TEXT_INFO) && (t != PAM_ERROR_MSG)) {
romanc6518422023-11-30 16:39:00 +0100542 ERR(clb_data->session, "PAM conversation callback received an unexpected type of message.");
roman41a11e42022-06-22 09:27:08 +0200543 r = PAM_CONV_ERR;
544 goto cleanup;
545 }
546 }
547
548 /* display messages with errors and/or some information and count the amount of actual authentication challenges */
549 for (i = 0; i < n_messages; i++) {
550 if (msg[i]->msg_style == PAM_TEXT_INFO) {
romanc6518422023-11-30 16:39:00 +0100551 VRB(clb_data->session, "PAM conversation callback received a message with some information for the client (%s).", msg[i]->msg);
roman41a11e42022-06-22 09:27:08 +0200552 n_requests--;
553 }
554 if (msg[i]->msg_style == PAM_ERROR_MSG) {
romanc6518422023-11-30 16:39:00 +0100555 ERR(clb_data->session, "PAM conversation callback received an error message (%s).", msg[i]->msg);
roman41a11e42022-06-22 09:27:08 +0200556 r = PAM_CONV_ERR;
557 goto cleanup;
558 }
559 }
560
561 /* there are no requests left for the user, only messages with some information for the client were sent */
562 if (n_requests <= 0) {
563 r = PAM_SUCCESS;
564 goto cleanup;
565 }
566
567 /* it is the PAM module's responsibility to release both, this array and the responses themselves */
568 *resp = calloc(n_requests, sizeof **resp);
569 prompts = calloc(n_requests, sizeof *prompts);
570 echo = calloc(n_requests, sizeof *echo);
roman3a95bb22023-10-26 11:07:17 +0200571 NC_CHECK_ERRMEM_GOTO(!(*resp) || !prompts || !echo, r = PAM_BUF_ERR, cleanup);
roman41a11e42022-06-22 09:27:08 +0200572
573 /* set the prompts for the user */
574 j = 0;
575 for (i = 0; i < n_messages; i++) {
576 if ((msg[i]->msg_style == PAM_PROMPT_ECHO_ON) || (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF)) {
577 prompts[j++] = msg[i]->msg;
578 }
579 }
580
581 /* iterate over all the messages and adjust the echo array accordingly */
582 j = 0;
583 for (i = 0; i < n_messages; i++) {
584 if (msg[i]->msg_style == PAM_PROMPT_ECHO_ON) {
585 echo[j++] = 1;
586 }
587 if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF) {
588 /* no need to set to 0 because of calloc */
589 j++;
590 }
591 }
592
593 /* print all the keyboard-interactive challenges to the user */
594 r = ssh_message_auth_interactive_request(clb_data->msg, name, instruction, n_requests, prompts, echo);
595 if (r != SSH_OK) {
romanc6518422023-11-30 16:39:00 +0100596 ERR(clb_data->session, "Failed to send an authentication request.");
roman41a11e42022-06-22 09:27:08 +0200597 r = PAM_CONV_ERR;
598 goto cleanup;
599 }
600
romane5675b12024-03-05 14:26:23 +0100601 n_answers = nc_server_ssh_kbdint_get_nanswers(clb_data->session, libssh_session);
romanc6518422023-11-30 16:39:00 +0100602 if (n_answers < 0) {
603 /* timeout or dc */
roman41a11e42022-06-22 09:27:08 +0200604 r = PAM_CONV_ERR;
605 goto cleanup;
romanc6518422023-11-30 16:39:00 +0100606 } else if (n_answers != n_requests) {
607 /* check if the number of answers and requests matches */
608 ERR(clb_data->session, "Expected %d response(s), got %d.", n_requests, n_answers);
roman41a11e42022-06-22 09:27:08 +0200609 r = PAM_CONV_ERR;
610 goto cleanup;
611 }
612
613 /* give the replies to a PAM module */
614 for (i = 0; i < n_answers; i++) {
615 (*resp)[i].resp = strdup(ssh_userauth_kbdint_getanswer(libssh_session, i));
616 /* it should be the caller's responsibility to free this, however if mem alloc fails,
617 * it is safer to free the responses here and set them to NULL */
618 if ((*resp)[i].resp == NULL) {
619 for (j = 0; j < i; j++) {
620 free((*resp)[j].resp);
621 (*resp)[j].resp = NULL;
622 }
623 ERRMEM;
624 r = PAM_BUF_ERR;
625 goto cleanup;
626 }
627 }
628
629cleanup:
630 ssh_message_free(reply);
631 free(prompts);
632 free(echo);
633 return r;
634}
635
636/**
637 * @brief Handles authentication via Linux PAM.
638 *
639 * @param[in] session NETCONF session.
640 * @param[in] ssh_msg SSH message with a keyboard-interactive authentication request.
641 * @return PAM_SUCCESS on success;
642 * @return PAM error otherwise.
643 */
644static int
romane5675b12024-03-05 14:26:23 +0100645nc_pam_auth(struct nc_session *session, struct nc_auth_client *client, ssh_message ssh_msg)
roman41a11e42022-06-22 09:27:08 +0200646{
647 pam_handle_t *pam_h = NULL;
648 int ret;
649 struct nc_pam_thread_arg clb_data;
650 struct pam_conv conv;
651
652 /* structure holding callback's data */
653 clb_data.msg = ssh_msg;
654 clb_data.session = session;
655
656 /* PAM conversation structure holding the callback and it's data */
657 conv.conv = nc_pam_conv_clb;
658 conv.appdata_ptr = &clb_data;
659
roman808f3f62023-11-23 16:01:04 +0100660 if (!server_opts.pam_config_name) {
romanc6518422023-11-30 16:39:00 +0100661 ERR(session, "PAM configuration filename not set.");
romanf578cd52023-10-19 09:47:40 +0200662 ret = 1;
663 goto cleanup;
664 }
665
roman41a11e42022-06-22 09:27:08 +0200666 /* initialize PAM and see if the given configuration file exists */
roman808f3f62023-11-23 16:01:04 +0100667 ret = pam_start(server_opts.pam_config_name, client->username, &conv, &pam_h);
roman41a11e42022-06-22 09:27:08 +0200668 if (ret != PAM_SUCCESS) {
romanc6518422023-11-30 16:39:00 +0100669 ERR(session, "PAM error occurred (%s).", pam_strerror(pam_h, ret));
roman41a11e42022-06-22 09:27:08 +0200670 goto cleanup;
671 }
672
673 /* authentication based on the modules listed in the configuration file */
674 ret = pam_authenticate(pam_h, 0);
675 if (ret != PAM_SUCCESS) {
676 if (ret == PAM_ABORT) {
romanc6518422023-11-30 16:39:00 +0100677 ERR(session, "PAM error occurred (%s).", pam_strerror(pam_h, ret));
roman41a11e42022-06-22 09:27:08 +0200678 goto cleanup;
679 } else {
romanc6518422023-11-30 16:39:00 +0100680 VRB(session, "PAM error occurred (%s).", pam_strerror(pam_h, ret));
roman41a11e42022-06-22 09:27:08 +0200681 goto cleanup;
682 }
683 }
684
685 /* correct token entered, check other requirements(the time of the day, expired token, ...) */
686 ret = pam_acct_mgmt(pam_h, 0);
687 if ((ret != PAM_SUCCESS) && (ret != PAM_NEW_AUTHTOK_REQD)) {
romanc6518422023-11-30 16:39:00 +0100688 VRB(session, "PAM error occurred (%s).", pam_strerror(pam_h, ret));
roman41a11e42022-06-22 09:27:08 +0200689 goto cleanup;
690 }
691
692 /* if a token has expired a new one will be generated */
693 if (ret == PAM_NEW_AUTHTOK_REQD) {
romanc6518422023-11-30 16:39:00 +0100694 VRB(session, "PAM warning occurred (%s).", pam_strerror(pam_h, ret));
roman41a11e42022-06-22 09:27:08 +0200695 ret = pam_chauthtok(pam_h, PAM_CHANGE_EXPIRED_AUTHTOK);
696 if (ret == PAM_SUCCESS) {
romanc6518422023-11-30 16:39:00 +0100697 VRB(session, "The authentication token of user \"%s\" updated successfully.", client->username);
roman41a11e42022-06-22 09:27:08 +0200698 } else {
romanc6518422023-11-30 16:39:00 +0100699 ERR(session, "PAM error occurred (%s).", pam_strerror(pam_h, ret));
roman41a11e42022-06-22 09:27:08 +0200700 goto cleanup;
701 }
702 }
703
704cleanup:
705 /* destroy the PAM context */
roman6dfdc0d2023-11-09 13:25:27 +0100706 if (pam_h && (pam_end(pam_h, ret) != PAM_SUCCESS)) {
romancab602e2023-11-24 11:30:32 +0100707 ERR(NULL, "PAM error occurred (%s).", pam_strerror(pam_h, ret));
roman41a11e42022-06-22 09:27:08 +0200708 }
709 return ret;
710}
711
romanc6518422023-11-30 16:39:00 +0100712#elif defined (HAVE_SHADOW)
713
714static struct passwd *
715nc_server_ssh_getpwnam(const char *username, struct passwd *pwd_buf, char **buf, size_t *buf_size)
716{
717 struct passwd *pwd = NULL;
718 char *mem;
719 int r = 0;
720
721 do {
722 r = getpwnam_r(username, pwd_buf, *buf, *buf_size, &pwd);
723 if (pwd) {
724 /* entry found */
725 break;
726 }
727
728 if (r == ERANGE) {
729 /* small buffer, enlarge */
730 *buf_size <<= 2;
731 mem = realloc(*buf, *buf_size);
732 if (!mem) {
733 ERRMEM;
734 return NULL;
735 }
736 *buf = mem;
737 }
738 } while (r == ERANGE);
739
740 return pwd;
741}
742
743static struct spwd *
744nc_server_ssh_getspnam(const char *username, struct spwd *spwd_buf, char **buf, size_t *buf_size)
745{
746 struct spwd *spwd = NULL;
747 char *mem;
748 int r = 0;
749
750 do {
751# ifndef __QNXNTO__
752 r = getspnam_r(username, spwd_buf, *buf, *buf_size, &spwd);
753# else
754 spwd = getspnam_r(username, spwd_buf, *buf, *buf_size);
755# endif
756 if (spwd) {
757 /* entry found */
758 break;
759 }
760
761 if (r == ERANGE) {
762 /* small buffer, enlarge */
763 *buf_size <<= 2;
764 mem = realloc(*buf, *buf_size);
765 if (!mem) {
766 ERRMEM;
767 return NULL;
768 }
769 *buf = mem;
770 }
771 } while (r == ERANGE);
772
773 return spwd;
774}
775
776static char *
777nc_server_ssh_get_pwd_hash(const char *username)
778{
779 struct passwd *pwd, pwd_buf;
780 struct spwd *spwd, spwd_buf;
781 char *pass_hash = NULL, *buf = NULL;
782 size_t buf_size = 256;
783
784 buf = malloc(buf_size);
785 NC_CHECK_ERRMEM_GOTO(!buf, , error);
786
787 pwd = nc_server_ssh_getpwnam(username, &pwd_buf, &buf, &buf_size);
788 if (!pwd) {
789 VRB(NULL, "User \"%s\" not found locally.", username);
790 goto error;
791 }
792
793 if (!strcmp(pwd->pw_passwd, "x")) {
794 spwd = nc_server_ssh_getspnam(username, &spwd_buf, &buf, &buf_size);
795 if (!spwd) {
796 VRB(NULL, "Failed to retrieve the shadow entry for \"%s\".", username);
797 goto error;
798 } else if ((spwd->sp_expire > -1) && (spwd->sp_expire <= (time(NULL) / (60 * 60 * 24)))) {
799 WRN(NULL, "User \"%s\" account has expired.", username);
800 goto error;
801 }
802
803 pass_hash = spwd->sp_pwdp;
804 } else {
805 pass_hash = pwd->pw_passwd;
806 }
807
808 if (!pass_hash) {
809 ERR(NULL, "No password could be retrieved for \"%s\".", username);
810 goto error;
811 }
812
813 /* check the hash structure for special meaning */
814 if (!strcmp(pass_hash, "*") || !strcmp(pass_hash, "!")) {
815 VRB(NULL, "User \"%s\" is not allowed to authenticate using a password.", username);
816 goto error;
817 }
818 if (!strcmp(pass_hash, "*NP*")) {
819 VRB(NULL, "Retrieving password for \"%s\" from a NIS+ server not supported.", username);
820 goto error;
821 }
822
823 pass_hash = strdup(pass_hash);
824 free(buf);
825 return pass_hash;
826
827error:
828 free(buf);
829 return NULL;
830}
831
832/**
833 * @brief Authenticate using locally stored credentials.
834 *
835 * @param[in] session Session to authenticate on.
836 * @param[in] client Client to authenticate.
romanc6518422023-11-30 16:39:00 +0100837 * @param[in] msg SSH message that originally requested kbdint authentication.
838 *
839 * @return 0 on success, non-zero otherwise.
840 */
841static int
romane5675b12024-03-05 14:26:23 +0100842nc_server_ssh_system_auth(struct nc_session *session, struct nc_auth_client *client, ssh_message msg)
romanc6518422023-11-30 16:39:00 +0100843{
844 int ret = 0, n_answers;
845 const char *name = "Keyboard-Interactive Authentication";
846 const char *instruction = "Please enter your authentication token";
847 char *prompt = NULL, *local_pw = NULL, *received_pw = NULL;
848 char echo[] = {0};
849
850 /* try to get the client's locally stored pw hash */
851 local_pw = nc_server_ssh_get_pwd_hash(client->username);
852 if (!local_pw) {
853 ERR(session, "Unable to get %s's credentials.", client->username);
854 ret = 1;
855 goto cleanup;
856 }
857
858 ret = asprintf(&prompt, "%s's password:", client->username);
859 NC_CHECK_ERRMEM_GOTO(ret == -1, prompt = NULL; ret = 1, cleanup);
860
861 /* send the password prompt to the client */
862 ret = ssh_message_auth_interactive_request(msg, name, instruction, 1, (const char **) &prompt, echo);
863 if (ret) {
864 ERR(session, "Failed to send an authentication request to client \"%s\".", client->username);
865 goto cleanup;
866 }
867
868 /* get the reply */
romane5675b12024-03-05 14:26:23 +0100869 n_answers = nc_server_ssh_kbdint_get_nanswers(session, session->ti.libssh.session);
romanc6518422023-11-30 16:39:00 +0100870 if (n_answers < 0) {
871 /* timeout or dc */
872 ret = 1;
873 goto cleanup;
874 } else if (n_answers != 1) {
875 /* only expecting a single answer */
876 ERR(session, "Unexpected amount of answers in system auth. Expected 1, got \"%d\".", n_answers);
877 ret = 1;
878 goto cleanup;
879 }
880 received_pw = strdup(ssh_userauth_kbdint_getanswer(session->ti.libssh.session, 0));
881 NC_CHECK_ERRMEM_GOTO(!received_pw, ret = 1, cleanup);
882
883 /* cmp the pw hashes */
884 ret = auth_password_compare_pwd(local_pw, received_pw);
885
886cleanup:
887 free(local_pw);
888 free(received_pw);
889 free(prompt);
890 return ret;
891}
892
893#endif
Michal Vasko0d81c572022-09-26 10:39:31 +0200894
romanf578cd52023-10-19 09:47:40 +0200895static int
romane5675b12024-03-05 14:26:23 +0100896nc_sshcb_auth_kbdint(struct nc_session *session, struct nc_auth_client *client, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100897{
bhart3bc2f582018-06-05 12:40:32 -0500898 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100899
romanf578cd52023-10-19 09:47:40 +0200900 if (server_opts.interactive_auth_clb) {
901 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 +0200902 } else {
903#ifdef HAVE_LIBPAM
romanc6518422023-11-30 16:39:00 +0100904 /* authenticate using PAM */
romane5675b12024-03-05 14:26:23 +0100905 if (!nc_pam_auth(session, client, msg)) {
romanc6518422023-11-30 16:39:00 +0100906 auth_ret = 0;
907 }
908#elif defined (HAVE_SHADOW)
909 /* authenticate using locally configured users */
romane5675b12024-03-05 14:26:23 +0100910 if (!nc_server_ssh_system_auth(session, client, msg)) {
Michal Vasko0d81c572022-09-26 10:39:31 +0200911 auth_ret = 0;
912 }
913#else
romanc6518422023-11-30 16:39:00 +0100914 ERR(NULL, "Keyboard-interactive method not supported.");
Michal Vasko0d81c572022-09-26 10:39:31 +0200915#endif
bhart1bb7cdb2018-07-02 15:03:30 -0500916 }
917
918 /* Authenticate message based on outcome */
romanf578cd52023-10-19 09:47:40 +0200919 if (auth_ret) {
bhart1bb7cdb2018-07-02 15:03:30 -0500920 ++session->opts.server.ssh_auth_attempts;
romanc6518422023-11-30 16:39:00 +0100921 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", client->username,
Michal Vasko05532772021-06-03 12:12:38 +0200922 session->opts.server.ssh_auth_attempts);
bhart1bb7cdb2018-07-02 15:03:30 -0500923 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +0100924 }
romanf578cd52023-10-19 09:47:40 +0200925
926 return auth_ret;
927}
928
roman808f3f62023-11-23 16:01:04 +0100929API void
930nc_server_ssh_set_interactive_auth_clb(int (*interactive_auth_clb)(const struct nc_session *session, ssh_session ssh_sess, ssh_message msg, void *user_data),
931 void *user_data, void (*free_user_data)(void *user_data))
932{
romanc6518422023-11-30 16:39:00 +0100933 /* CONFIG LOCK */
934 pthread_rwlock_wrlock(&server_opts.config_lock);
935
roman808f3f62023-11-23 16:01:04 +0100936 server_opts.interactive_auth_clb = interactive_auth_clb;
937 server_opts.interactive_auth_data = user_data;
938 server_opts.interactive_auth_data_free = free_user_data;
romanc6518422023-11-30 16:39:00 +0100939
940 /* CONFIG UNLOCK */
941 pthread_rwlock_unlock(&server_opts.config_lock);
roman808f3f62023-11-23 16:01:04 +0100942}
943
944#ifdef HAVE_LIBPAM
945
946API int
947nc_server_ssh_set_pam_conf_filename(const char *filename)
948{
romanc6518422023-11-30 16:39:00 +0100949 int ret = 0;
950
roman808f3f62023-11-23 16:01:04 +0100951 NC_CHECK_ARG_RET(NULL, filename, 1);
952
romanc6518422023-11-30 16:39:00 +0100953 /* CONFIG LOCK */
954 pthread_rwlock_wrlock(&server_opts.config_lock);
955
roman808f3f62023-11-23 16:01:04 +0100956 free(server_opts.pam_config_name);
957 server_opts.pam_config_name = strdup(filename);
romanc6518422023-11-30 16:39:00 +0100958 if (!server_opts.pam_config_name) {
959 ERRMEM;
960 ret = 1;
961 }
962
963 /* CONFIG UNLOCK */
964 pthread_rwlock_unlock(&server_opts.config_lock);
965 return ret;
roman808f3f62023-11-23 16:01:04 +0100966}
967
968#else
969
970API int
971nc_server_ssh_set_pam_conf_filename(const char *filename)
972{
romanf69fbcf2023-12-14 09:24:34 +0100973 /* LibPAM not supported */
roman808f3f62023-11-23 16:01:04 +0100974 (void) filename;
roman808f3f62023-11-23 16:01:04 +0100975 return 1;
976}
977
978#endif /* HAVE_LIBPAM */
979
romana9ec3362023-12-21 10:59:57 +0100980API int
981nc_server_ssh_set_authkey_path_format(const char *path)
982{
983 int ret = 0;
984
985 NC_CHECK_ARG_RET(NULL, path, 1);
986
987 /* CONFIG LOCK */
988 pthread_rwlock_wrlock(&server_opts.config_lock);
989
990 free(server_opts.authkey_path_fmt);
991 server_opts.authkey_path_fmt = strdup(path);
992 if (!server_opts.authkey_path_fmt) {
993 ERRMEM;
994 ret = 1;
995 }
996
997 /* CONFIG UNLOCK */
998 pthread_rwlock_unlock(&server_opts.config_lock);
999 return ret;
1000}
1001
romanf578cd52023-10-19 09:47:40 +02001002/*
1003 * Get the public key type from binary data stored in buffer.
1004 * The data is in the form of: 4 bytes = data length, then data of data length
1005 * and the data is in network byte order. The key has to be in the SSH2 format.
1006 */
1007static const char *
1008nc_server_ssh_get_pubkey_type(const char *buffer, uint32_t *len)
1009{
1010 uint32_t type_len;
1011
1012 /* copy the 4 bytes */
1013 memcpy(&type_len, buffer, sizeof type_len);
1014 /* type_len now stores the length of the key type */
1015 type_len = ntohl(type_len);
1016 *len = type_len;
1017
1018 /* move 4 bytes in the buffer, this is where the type should be */
1019 buffer += sizeof type_len;
1020 return buffer;
1021}
1022
1023/**
1024 * @brief Create ssh key from base64 pubkey data.
1025 *
1026 * @param[in] base64 base64 encoded public key.
1027 * @param[out] key created ssh key.
1028 * @return 0 on success, 1 otherwise.
1029 */
1030static int
1031nc_server_ssh_create_ssh_pubkey(const char *base64, ssh_key *key)
1032{
1033 int ret = 0;
1034 char *bin = NULL;
1035 const char *pub_type = NULL;
1036 uint32_t pub_type_len = 0;
1037
roman6dfdc0d2023-11-09 13:25:27 +01001038 NC_CHECK_ARG_RET(NULL, base64, key, 1);
romanf578cd52023-10-19 09:47:40 +02001039
1040 *key = NULL;
1041
1042 /* convert base64 to binary */
1043 if (nc_base64_to_bin(base64, &bin) == -1) {
1044 ERR(NULL, "Unable to decode base64.");
1045 ret = 1;
1046 goto cleanup;
1047 }
1048
1049 /* get the key type and try to import it if possible */
1050 pub_type = nc_server_ssh_get_pubkey_type(bin, &pub_type_len);
1051 if (!pub_type) {
1052 ret = 1;
1053 goto cleanup;
1054 } else if (!strncmp(pub_type, "ssh-dss", pub_type_len)) {
1055 ERR(NULL, "DSA keys are not supported.");
1056 ret = 1;
1057 goto cleanup;
1058 } else if (!strncmp(pub_type, "ssh-rsa", pub_type_len)) {
1059 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_RSA, key);
1060 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp256", pub_type_len)) {
1061 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P256, key);
1062 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp384", pub_type_len)) {
1063 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P384, key);
1064 } else if (!strncmp(pub_type, "ecdsa-sha2-nistp521", pub_type_len)) {
1065 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ECDSA_P521, key);
1066 } else if (!strncmp(pub_type, "ssh-ed25519", pub_type_len)) {
1067 ret = ssh_pki_import_pubkey_base64(base64, SSH_KEYTYPE_ED25519, key);
1068 } else {
1069 ERR(NULL, "Public key type not recognised.");
1070 ret = 1;
1071 goto cleanup;
1072 }
1073
1074cleanup:
1075 if (ret != SSH_OK) {
1076 ERR(NULL, "Error importing public key.");
1077 }
1078 free(bin);
1079 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001080}
1081
Michal Vaskof3c41e32022-09-09 11:22:21 +02001082/**
1083 * @brief Compare SSH key with configured authorized keys and return the username of the matching one, if any.
1084 *
1085 * @param[in] key Presented SSH key to compare.
1086 * @return Authorized key username, NULL if no match was found.
1087 */
romanf578cd52023-10-19 09:47:40 +02001088static int
1089auth_pubkey_compare_key(ssh_key key, struct nc_auth_client *auth_client)
Michal Vasko086311b2016-01-08 09:53:11 +01001090{
romanf578cd52023-10-19 09:47:40 +02001091 uint16_t i, pubkey_count;
Michal Vasko3e9d1682017-02-24 09:50:15 +01001092 int ret = 0;
romanf578cd52023-10-19 09:47:40 +02001093 ssh_key new_key = NULL;
romana9ec3362023-12-21 10:59:57 +01001094 struct nc_public_key *pubkeys = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001095
romanf578cd52023-10-19 09:47:40 +02001096 /* get the correct public key storage */
1097 if (auth_client->store == NC_STORE_LOCAL) {
1098 pubkeys = auth_client->pubkeys;
1099 pubkey_count = auth_client->pubkey_count;
romana9ec3362023-12-21 10:59:57 +01001100 } else if (auth_client->store == NC_STORE_TRUSTSTORE) {
romanf578cd52023-10-19 09:47:40 +02001101 ret = nc_server_ssh_ts_ref_get_keys(auth_client->ts_ref, &pubkeys, &pubkey_count);
1102 if (ret) {
1103 ERR(NULL, "Error getting \"%s\"'s public keys from the truststore.", auth_client->username);
romana9ec3362023-12-21 10:59:57 +01001104 goto cleanup;
Michal Vasko17dfda92016-12-01 14:06:16 +01001105 }
romana9ec3362023-12-21 10:59:57 +01001106 } else if (auth_client->store == NC_STORE_SYSTEM) {
1107 ret = nc_server_ssh_get_system_keys(auth_client->username, &pubkeys, &pubkey_count);
1108 if (ret) {
1109 ERR(NULL, "Failed to retrieve public keys of user \"%s\" from the system.", auth_client->username);
1110 goto cleanup;
1111 }
1112 } else {
1113 ERRINT;
1114 return 1;
romanf578cd52023-10-19 09:47:40 +02001115 }
Michal Vasko17dfda92016-12-01 14:06:16 +01001116
romanf578cd52023-10-19 09:47:40 +02001117 /* try to compare all of the client's keys with the key received in the SSH message */
1118 for (i = 0; i < pubkey_count; i++) {
1119 /* create the SSH key from the data */
1120 if (nc_server_ssh_create_ssh_pubkey(pubkeys[i].data, &new_key)) {
1121 ssh_key_free(new_key);
Michal Vasko086311b2016-01-08 09:53:11 +01001122 continue;
1123 }
1124
romanf578cd52023-10-19 09:47:40 +02001125 /* compare the keys */
1126 ret = ssh_key_cmp(key, new_key, SSH_KEY_CMP_PUBLIC);
1127 if (!ret) {
Michal Vasko086311b2016-01-08 09:53:11 +01001128 break;
romanf578cd52023-10-19 09:47:40 +02001129 } else {
1130 WRN(NULL, "User's \"%s\" public key doesn't match, trying another.", auth_client->username);
1131 ssh_key_free(new_key);
Michal Vasko086311b2016-01-08 09:53:11 +01001132 }
Michal Vasko086311b2016-01-08 09:53:11 +01001133 }
romanf578cd52023-10-19 09:47:40 +02001134 if (i == pubkey_count) {
1135 ret = 1;
romana9ec3362023-12-21 10:59:57 +01001136 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001137 }
1138
romana9ec3362023-12-21 10:59:57 +01001139cleanup:
romanf578cd52023-10-19 09:47:40 +02001140 if (!ret) {
1141 /* only free a key if everything was ok, it would have already been freed otherwise */
1142 ssh_key_free(new_key);
1143 }
Michal Vaskoa05c7b12017-01-30 14:33:08 +01001144
romana9ec3362023-12-21 10:59:57 +01001145 if ((auth_client->store == NC_STORE_SYSTEM) && pubkeys) {
1146 for (i = 0; i < pubkey_count; i++) {
1147 free(pubkeys[i].name);
1148 free(pubkeys[i].data);
1149 }
1150 free(pubkeys);
1151 }
romanf578cd52023-10-19 09:47:40 +02001152 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001153}
1154
1155static void
romanf578cd52023-10-19 09:47:40 +02001156nc_sshcb_auth_none(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001157{
roman808f3f62023-11-23 16:01:04 +01001158 if (auth_client->none_enabled && !auth_client->password && !auth_client->pubkey_count && !auth_client->kb_int_enabled) {
romanf578cd52023-10-19 09:47:40 +02001159 /* only authenticate the client if he supports none and no other method */
1160 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
1161 VRB(session, "User \"%s\" authenticated.", session->username);
1162 ssh_message_auth_reply_success(msg, 0);
1163 }
1164
1165 ssh_message_reply_default(msg);
1166}
1167
1168static int
1169nc_sshcb_auth_pubkey(struct nc_session *session, struct nc_auth_client *auth_client, ssh_message msg)
1170{
1171 int signature_state, ret = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001172
roman9130fc12023-11-03 13:56:23 +01001173 if (auth_pubkey_compare_key(ssh_message_auth_pubkey(msg), auth_client)) {
1174 VRB(session, "User \"%s\" tried to use an unknown (unauthorized) public key.", session->username);
1175 ret = 1;
1176 goto fail;
Michal Vaskobd13a932016-09-14 09:00:35 +02001177 }
Michal Vaskobd13a932016-09-14 09:00:35 +02001178
Michal Vasko086311b2016-01-08 09:53:11 +01001179 signature_state = ssh_message_auth_publickey_state(msg);
romanf578cd52023-10-19 09:47:40 +02001180 if (signature_state == SSH_PUBLICKEY_STATE_NONE) {
Michal Vaskobd13a932016-09-14 09:00:35 +02001181 /* accepting only the use of a public key */
1182 ssh_message_auth_reply_pk_ok_simple(msg);
romanf578cd52023-10-19 09:47:40 +02001183 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001184 }
1185
romanf578cd52023-10-19 09:47:40 +02001186 return ret;
Michal Vaskobd13a932016-09-14 09:00:35 +02001187
1188fail:
Michal Vasko2e6defd2016-10-07 15:48:15 +02001189 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +02001190 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
1191 session->opts.server.ssh_auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +01001192 ssh_message_reply_default(msg);
romanf578cd52023-10-19 09:47:40 +02001193
1194 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001195}
1196
1197static int
Michal Vasko96164bf2016-01-21 15:41:58 +01001198nc_sshcb_channel_open(struct nc_session *session, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001199{
Michal Vasko96164bf2016-01-21 15:41:58 +01001200 ssh_channel chan;
1201
1202 /* first channel request */
1203 if (!session->ti.libssh.channel) {
1204 if (session->status != NC_STATUS_STARTING) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001205 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001206 return -1;
1207 }
Michal Vasko96164bf2016-01-21 15:41:58 +01001208 chan = ssh_message_channel_request_open_reply_accept(msg);
1209 if (!chan) {
Michal Vasko05532772021-06-03 12:12:38 +02001210 ERR(session, "Failed to create a new SSH channel.");
Michal Vasko96164bf2016-01-21 15:41:58 +01001211 return -1;
1212 }
1213 session->ti.libssh.channel = chan;
Michal Vasko086311b2016-01-08 09:53:11 +01001214
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001215 /* additional channel request */
Michal Vasko96164bf2016-01-21 15:41:58 +01001216 } else {
1217 chan = ssh_message_channel_request_open_reply_accept(msg);
1218 if (!chan) {
Michal Vasko05532772021-06-03 12:12:38 +02001219 ERR(session, "Session %u: failed to create a new SSH channel.", session->id);
Michal Vasko96164bf2016-01-21 15:41:58 +01001220 return -1;
1221 }
1222 /* channel was created and libssh stored it internally in the ssh_session structure, good enough */
Michal Vasko086311b2016-01-08 09:53:11 +01001223 }
1224
Michal Vasko086311b2016-01-08 09:53:11 +01001225 return 0;
1226}
1227
1228static int
1229nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem)
1230{
Michal Vasko96164bf2016-01-21 15:41:58 +01001231 struct nc_session *new_session;
Michal Vasko086311b2016-01-08 09:53:11 +01001232
Michal Vasko96164bf2016-01-21 15:41:58 +01001233 if (strcmp(subsystem, "netconf")) {
Michal Vasko05532772021-06-03 12:12:38 +02001234 WRN(session, "Received an unknown subsystem \"%s\" request.", subsystem);
Michal Vasko086311b2016-01-08 09:53:11 +01001235 return -1;
1236 }
1237
Michal Vasko96164bf2016-01-21 15:41:58 +01001238 if (session->ti.libssh.channel == channel) {
1239 /* first channel requested */
1240 if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) {
1241 ERRINT;
1242 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001243 }
Michal Vasko96164bf2016-01-21 15:41:58 +01001244 if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) {
Michal Vasko05532772021-06-03 12:12:38 +02001245 ERR(session, "Subsystem \"netconf\" requested for the second time.");
Michal Vasko96164bf2016-01-21 15:41:58 +01001246 return -1;
1247 }
1248
1249 session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF;
Michal Vasko086311b2016-01-08 09:53:11 +01001250 } else {
Michal Vasko96164bf2016-01-21 15:41:58 +01001251 /* additional channel subsystem request, new session is ready as far as SSH is concerned */
Michal Vasko131120a2018-05-29 15:44:02 +02001252 new_session = nc_new_session(NC_SERVER, 1);
roman3a95bb22023-10-26 11:07:17 +02001253 NC_CHECK_ERRMEM_RET(!new_session, -1);
Michal Vasko96164bf2016-01-21 15:41:58 +01001254
1255 /* insert the new session */
1256 if (!session->ti.libssh.next) {
1257 new_session->ti.libssh.next = session;
1258 } else {
1259 new_session->ti.libssh.next = session->ti.libssh.next;
1260 }
1261 session->ti.libssh.next = new_session;
1262
1263 new_session->status = NC_STATUS_STARTING;
Michal Vasko96164bf2016-01-21 15:41:58 +01001264 new_session->ti_type = NC_TI_LIBSSH;
Michal Vasko131120a2018-05-29 15:44:02 +02001265 new_session->io_lock = session->io_lock;
Michal Vasko96164bf2016-01-21 15:41:58 +01001266 new_session->ti.libssh.channel = channel;
1267 new_session->ti.libssh.session = session->ti.libssh.session;
Michal Vasko93224072021-11-09 12:14:28 +01001268 new_session->username = strdup(session->username);
1269 new_session->host = strdup(session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +01001270 new_session->port = session->port;
Michal Vasko93224072021-11-09 12:14:28 +01001271 new_session->ctx = (struct ly_ctx *)session->ctx;
Michal Vasko83d15322018-09-27 09:44:02 +02001272 new_session->flags = NC_SESSION_SSH_AUTHENTICATED | NC_SESSION_SSH_SUBSYS_NETCONF | NC_SESSION_SHAREDCTX;
Michal Vasko086311b2016-01-08 09:53:11 +01001273 }
1274
1275 return 0;
1276}
1277
Michal Vasko96164bf2016-01-21 15:41:58 +01001278int
romanf578cd52023-10-19 09:47:40 +02001279nc_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 +01001280{
1281 const char *str_type, *str_subtype = NULL, *username;
romanf578cd52023-10-19 09:47:40 +02001282 int subtype, type, libssh_auth_methods = 0, ret = 0;
1283 uint16_t i;
1284 struct nc_auth_client *auth_client = NULL;
roman78df0fa2023-11-02 10:33:57 +01001285 struct nc_endpt *referenced_endpt;
Michal Vasko086311b2016-01-08 09:53:11 +01001286
1287 type = ssh_message_type(msg);
1288 subtype = ssh_message_subtype(msg);
1289
1290 switch (type) {
1291 case SSH_REQUEST_AUTH:
1292 str_type = "request-auth";
1293 switch (subtype) {
1294 case SSH_AUTH_METHOD_NONE:
1295 str_subtype = "none";
1296 break;
1297 case SSH_AUTH_METHOD_PASSWORD:
1298 str_subtype = "password";
1299 break;
1300 case SSH_AUTH_METHOD_PUBLICKEY:
1301 str_subtype = "publickey";
1302 break;
1303 case SSH_AUTH_METHOD_HOSTBASED:
1304 str_subtype = "hostbased";
1305 break;
1306 case SSH_AUTH_METHOD_INTERACTIVE:
1307 str_subtype = "interactive";
1308 break;
1309 case SSH_AUTH_METHOD_GSSAPI_MIC:
1310 str_subtype = "gssapi-mic";
1311 break;
1312 }
1313 break;
1314
1315 case SSH_REQUEST_CHANNEL_OPEN:
1316 str_type = "request-channel-open";
1317 switch (subtype) {
1318 case SSH_CHANNEL_SESSION:
1319 str_subtype = "session";
1320 break;
1321 case SSH_CHANNEL_DIRECT_TCPIP:
1322 str_subtype = "direct-tcpip";
1323 break;
1324 case SSH_CHANNEL_FORWARDED_TCPIP:
1325 str_subtype = "forwarded-tcpip";
1326 break;
1327 case (int)SSH_CHANNEL_X11:
1328 str_subtype = "channel-x11";
1329 break;
1330 case SSH_CHANNEL_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001331 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +01001332 default:
1333 str_subtype = "unknown";
1334 break;
1335 }
1336 break;
1337
1338 case SSH_REQUEST_CHANNEL:
1339 str_type = "request-channel";
1340 switch (subtype) {
1341 case SSH_CHANNEL_REQUEST_PTY:
1342 str_subtype = "pty";
1343 break;
1344 case SSH_CHANNEL_REQUEST_EXEC:
1345 str_subtype = "exec";
1346 break;
1347 case SSH_CHANNEL_REQUEST_SHELL:
1348 str_subtype = "shell";
1349 break;
1350 case SSH_CHANNEL_REQUEST_ENV:
1351 str_subtype = "env";
1352 break;
1353 case SSH_CHANNEL_REQUEST_SUBSYSTEM:
1354 str_subtype = "subsystem";
1355 break;
1356 case SSH_CHANNEL_REQUEST_WINDOW_CHANGE:
1357 str_subtype = "window-change";
1358 break;
1359 case SSH_CHANNEL_REQUEST_X11:
1360 str_subtype = "x11";
1361 break;
1362 case SSH_CHANNEL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001363 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +01001364 default:
1365 str_subtype = "unknown";
1366 break;
1367 }
1368 break;
1369
1370 case SSH_REQUEST_SERVICE:
1371 str_type = "request-service";
1372 str_subtype = ssh_message_service_service(msg);
1373 break;
1374
1375 case SSH_REQUEST_GLOBAL:
1376 str_type = "request-global";
1377 switch (subtype) {
1378 case SSH_GLOBAL_REQUEST_TCPIP_FORWARD:
1379 str_subtype = "tcpip-forward";
1380 break;
1381 case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD:
1382 str_subtype = "cancel-tcpip-forward";
1383 break;
1384 case SSH_GLOBAL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001385 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +01001386 default:
1387 str_subtype = "unknown";
1388 break;
1389 }
1390 break;
1391
1392 default:
1393 str_type = "unknown";
1394 str_subtype = "unknown";
1395 break;
1396 }
1397
Michal Vasko05532772021-06-03 12:12:38 +02001398 VRB(session, "Received an SSH message \"%s\" of subtype \"%s\".", str_type, str_subtype);
Michal Vasko5e0edd82020-07-29 15:26:13 +02001399 if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) {
Michal Vaskoce319162016-02-03 15:33:08 +01001400 /* "valid" situation if, for example, receiving some auth or channel request timeouted,
1401 * but we got it now, during session free */
Michal Vasko05532772021-06-03 12:12:38 +02001402 VRB(session, "SSH message arrived on a %s session, the request will be denied.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001403 (session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid"));
Michal Vaskoce319162016-02-03 15:33:08 +01001404 ssh_message_reply_default(msg);
1405 return 0;
1406 }
Michal Vasko086311b2016-01-08 09:53:11 +01001407
1408 /*
1409 * process known messages
1410 */
1411 if (type == SSH_REQUEST_AUTH) {
1412 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko05532772021-06-03 12:12:38 +02001413 ERR(session, "User \"%s\" authenticated, but requested another authentication.", session->username);
Michal Vasko086311b2016-01-08 09:53:11 +01001414 ssh_message_reply_default(msg);
1415 return 0;
romanf578cd52023-10-19 09:47:40 +02001416 } else if (!state || !opts) {
1417 /* these two parameters should always be set during an authentication,
1418 * however do a check just in case something goes really wrong, since they
1419 * are not needed for other types of messages
1420 */
1421 ERRINT;
1422 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001423 }
1424
Michal Vasko086311b2016-01-08 09:53:11 +01001425 /* save the username, do not let the client change it */
1426 username = ssh_message_auth_user(msg);
romanf578cd52023-10-19 09:47:40 +02001427 assert(username);
1428
1429 for (i = 0; i < opts->client_count; i++) {
1430 if (!strcmp(opts->auth_clients[i].username, username)) {
1431 auth_client = &opts->auth_clients[i];
1432 break;
1433 }
1434 }
1435
1436 if (!auth_client) {
roman78df0fa2023-11-02 10:33:57 +01001437 if (opts->referenced_endpt_name) {
1438 /* client not known by the endpt, but it references another one so try it */
1439 if (nc_server_get_referenced_endpt(opts->referenced_endpt_name, &referenced_endpt)) {
1440 ERRINT;
1441 return 1;
1442 }
1443
1444 return nc_session_ssh_msg(session, referenced_endpt->opts.ssh, msg, state);
Michal Vasko086311b2016-01-08 09:53:11 +01001445 }
1446
roman545879e2024-01-19 14:43:46 +01001447 /* user not known, set his authentication methods to public key only so that
1448 * there is no interaction and it will simply be denied */
romanf578cd52023-10-19 09:47:40 +02001449 ERR(NULL, "User \"%s\" not known by the server.", username);
roman545879e2024-01-19 14:43:46 +01001450 ssh_set_auth_methods(session->ti.libssh.session, SSH_AUTH_METHOD_PUBLICKEY);
romanf578cd52023-10-19 09:47:40 +02001451 ssh_message_reply_default(msg);
1452 return 0;
1453 }
1454
1455 if (!session->username) {
Michal Vasko93224072021-11-09 12:14:28 +01001456 session->username = strdup(username);
romanf578cd52023-10-19 09:47:40 +02001457
1458 /* configure and count accepted auth methods */
1459 if (auth_client->store == NC_STORE_LOCAL) {
1460 if (auth_client->pubkey_count) {
1461 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
1462 }
romana9ec3362023-12-21 10:59:57 +01001463 } else if (auth_client->store == NC_STORE_TRUSTSTORE) {
1464 if (auth_client->ts_ref) {
1465 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
1466 }
1467 } else if (auth_client->store == NC_STORE_SYSTEM) {
romanf578cd52023-10-19 09:47:40 +02001468 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
1469 }
1470 if (auth_client->password) {
1471 state->auth_method_count++;
1472 libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD;
1473 }
roman808f3f62023-11-23 16:01:04 +01001474 if (auth_client->kb_int_enabled) {
romanf578cd52023-10-19 09:47:40 +02001475 state->auth_method_count++;
1476 libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE;
1477 }
roman808f3f62023-11-23 16:01:04 +01001478 if (auth_client->none_enabled) {
romanf578cd52023-10-19 09:47:40 +02001479 libssh_auth_methods |= SSH_AUTH_METHOD_NONE;
1480 }
1481
1482 if (libssh_auth_methods & SSH_AUTH_METHOD_PUBLICKEY) {
1483 state->auth_method_count++;
1484 }
1485
1486 ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods);
1487 } else {
Michal Vasko086311b2016-01-08 09:53:11 +01001488 if (strcmp(username, session->username)) {
romanf578cd52023-10-19 09:47:40 +02001489 /* changing username not allowed */
Michal Vasko05532772021-06-03 12:12:38 +02001490 ERR(session, "User \"%s\" changed its username to \"%s\".", session->username, username);
Michal Vasko086311b2016-01-08 09:53:11 +01001491 session->status = NC_STATUS_INVALID;
Michal Vasko428087d2016-01-14 16:04:28 +01001492 session->term_reason = NC_SESSION_TERM_OTHER;
Michal Vasko086311b2016-01-08 09:53:11 +01001493 return 1;
1494 }
1495 }
1496
romanf578cd52023-10-19 09:47:40 +02001497 /* try authenticating, the user must authenticate via all of his configured auth methods */
Michal Vasko086311b2016-01-08 09:53:11 +01001498 if (subtype == SSH_AUTH_METHOD_NONE) {
romanf578cd52023-10-19 09:47:40 +02001499 nc_sshcb_auth_none(session, auth_client, msg);
1500 ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001501 } else if (subtype == SSH_AUTH_METHOD_PASSWORD) {
romanf578cd52023-10-19 09:47:40 +02001502 ret = nc_sshcb_auth_password(session, auth_client, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001503 } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) {
romanf578cd52023-10-19 09:47:40 +02001504 ret = nc_sshcb_auth_pubkey(session, auth_client, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001505 } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) {
romane5675b12024-03-05 14:26:23 +01001506 ret = nc_sshcb_auth_kbdint(session, auth_client, msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001507 }
romanf578cd52023-10-19 09:47:40 +02001508
1509 if (!ret) {
1510 state->auth_success_count++;
1511 }
1512
1513 if (!ret && (state->auth_success_count < state->auth_method_count)) {
1514 /* success, but he needs to do another method */
1515 VRB(session, "User \"%s\" partially authenticated, but still needs to authenticate via the rest of his configured methods.", username);
1516 ssh_message_auth_reply_success(msg, 1);
1517 } else if (!ret && (state->auth_success_count == state->auth_method_count)) {
1518 /* authenticated */
1519 ssh_message_auth_reply_success(msg, 0);
1520 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
1521 VRB(session, "User \"%s\" authenticated.", username);
1522 }
1523
1524 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001525 } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko0df67562016-01-21 15:50:11 +01001526 if ((type == SSH_REQUEST_CHANNEL_OPEN) && ((enum ssh_channel_type_e)subtype == SSH_CHANNEL_SESSION)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001527 if (nc_sshcb_channel_open(session, msg)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001528 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001529 }
Michal Vasko086311b2016-01-08 09:53:11 +01001530 return 0;
Michal Vasko96164bf2016-01-21 15:41:58 +01001531
Michal Vasko0df67562016-01-21 15:50:11 +01001532 } else if ((type == SSH_REQUEST_CHANNEL) && ((enum ssh_channel_requests_e)subtype == SSH_CHANNEL_REQUEST_SUBSYSTEM)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001533 if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg),
1534 ssh_message_channel_request_subsystem(msg))) {
Michal Vasko086311b2016-01-08 09:53:11 +01001535 ssh_message_reply_default(msg);
Michal Vasko96164bf2016-01-21 15:41:58 +01001536 } else {
1537 ssh_message_channel_request_reply_success(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001538 }
1539 return 0;
1540 }
1541 }
1542
1543 /* we did not process it */
1544 return 1;
1545}
1546
Michal Vasko1a38c862016-01-15 15:50:07 +01001547/* ret 1 on success, 0 on timeout, -1 on error */
Michal Vasko086311b2016-01-08 09:53:11 +01001548static int
romanf578cd52023-10-19 09:47:40 +02001549nc_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 +01001550{
roman6ece9c52022-06-22 09:29:17 +02001551 struct timespec ts_timeout;
romanf578cd52023-10-19 09:47:40 +02001552 ssh_message msg;
Michal Vasko086311b2016-01-08 09:53:11 +01001553
romanf578cd52023-10-19 09:47:40 +02001554 if (timeout) {
1555 nc_timeouttime_get(&ts_timeout, timeout * 1000);
Michal Vasko36c7be82017-02-22 13:37:59 +01001556 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001557 while (1) {
1558 if (!nc_session_is_connected(session)) {
romanf578cd52023-10-19 09:47:40 +02001559 ERR(session, "Communication SSH socket unexpectedly closed.");
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001560 return -1;
1561 }
1562
romanf578cd52023-10-19 09:47:40 +02001563 msg = ssh_message_get(session->ti.libssh.session);
1564 if (msg) {
1565 if (nc_session_ssh_msg(session, opts, msg, NULL)) {
1566 ssh_message_reply_default(msg);
1567 }
1568 ssh_message_free(msg);
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001569 }
1570
romanf578cd52023-10-19 09:47:40 +02001571 if (session->ti.libssh.channel && session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) {
Michal Vasko1a38c862016-01-15 15:50:07 +01001572 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001573 }
1574
Michal Vasko086311b2016-01-08 09:53:11 +01001575 usleep(NC_TIMEOUT_STEP);
romanea0edaa2023-10-26 12:16:25 +02001576 if (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
roman6ece9c52022-06-22 09:29:17 +02001577 /* timeout */
1578 ERR(session, "Failed to start \"netconf\" SSH subsystem for too long, disconnecting.");
1579 break;
Michal Vasko36c7be82017-02-22 13:37:59 +01001580 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001581 }
Michal Vasko086311b2016-01-08 09:53:11 +01001582
Michal Vasko1a38c862016-01-15 15:50:07 +01001583 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001584}
1585
Michal Vaskof3c41e32022-09-09 11:22:21 +02001586/**
1587 * @brief Set hostkeys to be used for an SSH bind.
1588 *
1589 * @param[in] sbind SSH bind to use.
1590 * @param[in] hostkeys Array of hostkeys.
1591 * @param[in] hostkey_count Count of @p hostkeys.
1592 * @return 0 on success.
1593 * @return -1 on error.
1594 */
Michal Vasko4c1fb492017-01-30 14:31:07 +01001595static int
romanf578cd52023-10-19 09:47:40 +02001596nc_ssh_bind_add_hostkeys(ssh_bind sbind, struct nc_server_ssh_opts *opts, uint16_t hostkey_count)
Michal Vasko4c1fb492017-01-30 14:31:07 +01001597{
romanf578cd52023-10-19 09:47:40 +02001598 uint16_t i;
roman6dfdc0d2023-11-09 13:25:27 +01001599 char *privkey_path;
Michal Vaskoddce1212019-05-24 09:58:49 +02001600 int ret;
romanf578cd52023-10-19 09:47:40 +02001601 struct nc_asymmetric_key *key = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001602
1603 for (i = 0; i < hostkey_count; ++i) {
roman6dfdc0d2023-11-09 13:25:27 +01001604 privkey_path = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001605
romanf578cd52023-10-19 09:47:40 +02001606 /* get the asymmetric key */
1607 if (opts->hostkeys[i].store == NC_STORE_LOCAL) {
1608 /* stored locally */
1609 key = &opts->hostkeys[i].key;
1610 } else {
1611 /* keystore reference, need to get it */
1612 if (nc_server_ssh_ks_ref_get_key(opts->hostkeys[i].ks_ref, &key)) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001613 return -1;
1614 }
1615 }
1616
romanf578cd52023-10-19 09:47:40 +02001617 privkey_path = base64der_privkey_to_tmp_file(key->privkey_data, nc_privkey_format_to_str(key->privkey_type));
1618 if (!privkey_path) {
1619 ERR(NULL, "Temporarily storing a host key into a file failed (%s).", strerror(errno));
1620 return -1;
1621 }
1622
Michal Vasko4c1fb492017-01-30 14:31:07 +01001623 ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path);
1624
1625 /* cleanup */
roman6dfdc0d2023-11-09 13:25:27 +01001626 if (unlink(privkey_path)) {
Michal Vasko05532772021-06-03 12:12:38 +02001627 WRN(NULL, "Removing a temporary host key file \"%s\" failed (%s).", privkey_path, strerror(errno));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001628 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001629
1630 if (ret != SSH_OK) {
romanf578cd52023-10-19 09:47:40 +02001631 ERR(NULL, "Failed to set hostkey \"%s\" (%s).", opts->hostkeys[i].name, privkey_path);
Michal Vasko80075de2017-07-10 11:38:52 +02001632 }
1633 free(privkey_path);
1634
1635 if (ret != SSH_OK) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001636 return -1;
1637 }
1638 }
1639
1640 return 0;
1641}
1642
Michal Vasko09d700f2022-09-08 10:21:40 +02001643static int
romanf578cd52023-10-19 09:47:40 +02001644nc_accept_ssh_session_auth(struct nc_session *session, struct nc_server_ssh_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001645{
roman6ece9c52022-06-22 09:29:17 +02001646 struct timespec ts_timeout;
roman41a11e42022-06-22 09:27:08 +02001647 ssh_message msg;
romanf578cd52023-10-19 09:47:40 +02001648 struct nc_auth_state state = {0};
Michal Vasko086311b2016-01-08 09:53:11 +01001649
Michal Vasko086311b2016-01-08 09:53:11 +01001650 /* authenticate */
Michal Vasko36c7be82017-02-22 13:37:59 +01001651 if (opts->auth_timeout) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001652 nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000);
Michal Vasko36c7be82017-02-22 13:37:59 +01001653 }
1654 while (1) {
Michal Vasko2a7d4732016-01-15 09:24:46 +01001655 if (!nc_session_is_connected(session)) {
Michal Vasko05532772021-06-03 12:12:38 +02001656 ERR(session, "Communication SSH socket unexpectedly closed.");
Michal Vasko2a7d4732016-01-15 09:24:46 +01001657 return -1;
1658 }
1659
roman41a11e42022-06-22 09:27:08 +02001660 msg = ssh_message_get(session->ti.libssh.session);
1661 if (msg) {
romanf578cd52023-10-19 09:47:40 +02001662 if (nc_session_ssh_msg(session, opts, msg, &state)) {
roman41a11e42022-06-22 09:27:08 +02001663 ssh_message_reply_default(msg);
1664 }
1665 ssh_message_free(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001666 }
1667
Michal Vasko36c7be82017-02-22 13:37:59 +01001668 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
1669 break;
1670 }
1671
Michal Vasko145ae672017-02-07 10:57:27 +01001672 if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) {
Michal Vasko05532772021-06-03 12:12:38 +02001673 ERR(session, "Too many failed authentication attempts of user \"%s\".", session->username);
Michal Vasko145ae672017-02-07 10:57:27 +01001674 return -1;
1675 }
1676
Michal Vasko086311b2016-01-08 09:53:11 +01001677 usleep(NC_TIMEOUT_STEP);
romanea0edaa2023-10-26 12:16:25 +02001678 if (opts->auth_timeout && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
roman6ece9c52022-06-22 09:29:17 +02001679 /* timeout */
1680 break;
Michal Vasko36c7be82017-02-22 13:37:59 +01001681 }
1682 }
Michal Vasko086311b2016-01-08 09:53:11 +01001683
1684 if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) {
1685 /* timeout */
Michal Vaskoc13da702017-02-07 10:57:57 +01001686 if (session->username) {
Michal Vasko05532772021-06-03 12:12:38 +02001687 ERR(session, "User \"%s\" failed to authenticate for too long, disconnecting.", session->username);
Michal Vaskoc13da702017-02-07 10:57:57 +01001688 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001689 ERR(session, "User failed to authenticate for too long, disconnecting.");
Michal Vaskoc13da702017-02-07 10:57:57 +01001690 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001691 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001692 }
1693
Michal Vasko09d700f2022-09-08 10:21:40 +02001694 return 1;
1695}
1696
1697int
romanf578cd52023-10-19 09:47:40 +02001698nc_accept_ssh_session(struct nc_session *session, struct nc_server_ssh_opts *opts, int sock, int timeout)
Michal Vasko09d700f2022-09-08 10:21:40 +02001699{
1700 ssh_bind sbind = NULL;
Michal Vasko09d700f2022-09-08 10:21:40 +02001701 int rc = 1, r;
1702 struct timespec ts_timeout;
roman4cc0cd52023-04-14 09:12:29 +02001703 const char *err_msg;
Michal Vasko09d700f2022-09-08 10:21:40 +02001704
Michal Vasko09d700f2022-09-08 10:21:40 +02001705 /* other transport-specific data */
1706 session->ti_type = NC_TI_LIBSSH;
1707 session->ti.libssh.session = ssh_new();
1708 if (!session->ti.libssh.session) {
1709 ERR(NULL, "Failed to initialize a new SSH session.");
1710 rc = -1;
1711 goto cleanup;
1712 }
1713
1714 sbind = ssh_bind_new();
1715 if (!sbind) {
1716 ERR(session, "Failed to create an SSH bind.");
1717 rc = -1;
1718 goto cleanup;
1719 }
1720
1721 /* configure host keys */
romanf578cd52023-10-19 09:47:40 +02001722 if (nc_ssh_bind_add_hostkeys(sbind, opts, opts->hostkey_count)) {
1723 rc = -1;
1724 goto cleanup;
1725 }
1726
1727 /* configure supported algorithms */
1728 if (opts->hostkey_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, opts->hostkey_algs)) {
1729 rc = -1;
1730 goto cleanup;
1731 }
1732 if (opts->encryption_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_CIPHERS_S_C, opts->encryption_algs)) {
1733 rc = -1;
1734 goto cleanup;
1735 }
1736 if (opts->kex_algs && ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_KEY_EXCHANGE, opts->kex_algs)) {
1737 rc = -1;
1738 goto cleanup;
1739 }
1740 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 +02001741 rc = -1;
1742 goto cleanup;
1743 }
1744
1745 /* accept new connection on the bind */
1746 if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) {
1747 ERR(session, "SSH failed to accept a new connection (%s).", ssh_get_error(sbind));
1748 rc = -1;
1749 goto cleanup;
1750 }
1751 sock = -1;
1752
romanf578cd52023-10-19 09:47:40 +02001753 /* set to non-blocking */
Michal Vasko09d700f2022-09-08 10:21:40 +02001754 ssh_set_blocking(session->ti.libssh.session, 0);
1755
1756 if (timeout > -1) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001757 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko09d700f2022-09-08 10:21:40 +02001758 }
1759 while ((r = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) {
1760 /* this tends to take longer */
1761 usleep(NC_TIMEOUT_STEP * 20);
Michal Vaskod8a74192023-02-06 15:51:50 +01001762 if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001763 break;
1764 }
1765 }
1766 if (r == SSH_AGAIN) {
1767 ERR(session, "SSH key exchange timeout.");
1768 rc = 0;
1769 goto cleanup;
1770 } else if (r != SSH_OK) {
roman4cc0cd52023-04-14 09:12:29 +02001771 err_msg = ssh_get_error(session->ti.libssh.session);
1772 if (err_msg[0] == '\0') {
1773 err_msg = "hostkey algorithm generated from the hostkey most likely not found in the set of configured hostkey algorithms";
1774 }
1775 ERR(session, "SSH key exchange error (%s).", err_msg);
Michal Vasko09d700f2022-09-08 10:21:40 +02001776 rc = -1;
1777 goto cleanup;
1778 }
1779
romane5675b12024-03-05 14:26:23 +01001780 /* authenticate, store auth_timeout in session so we can retrieve it in kb interactive API */
1781 session->data = &opts->auth_timeout;
1782 rc = nc_accept_ssh_session_auth(session, opts);
1783 session->data = NULL;
1784 if (rc != 1) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001785 goto cleanup;
1786 }
1787
Michal Vasko09d700f2022-09-08 10:21:40 +02001788 /* open channel and request 'netconf' subsystem */
romanf578cd52023-10-19 09:47:40 +02001789 if ((rc = nc_accept_ssh_session_open_netconf_channel(session, opts, timeout)) != 1) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001790 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001791 }
1792
Michal Vasko09d700f2022-09-08 10:21:40 +02001793cleanup:
1794 if (sock > -1) {
1795 close(sock);
1796 }
1797 ssh_bind_free(sbind);
1798 return rc;
Michal Vasko086311b2016-01-08 09:53:11 +01001799}
1800
Michal Vasko71090fc2016-05-24 16:37:28 +02001801API NC_MSG_TYPE
1802nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session)
1803{
1804 NC_MSG_TYPE msgtype;
1805 struct nc_session *new_session = NULL;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001806 struct timespec ts_cur;
Michal Vasko71090fc2016-05-24 16:37:28 +02001807
roman40672412023-05-04 11:10:22 +02001808 NC_CHECK_ARG_RET(orig_session, orig_session, session, NC_MSG_ERROR);
Michal Vasko71090fc2016-05-24 16:37:28 +02001809
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001810 if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) &&
1811 orig_session->ti.libssh.next) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001812 for (new_session = orig_session->ti.libssh.next;
1813 new_session != orig_session;
1814 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001815 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1816 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001817 /* we found our session */
1818 break;
1819 }
1820 }
1821 if (new_session == orig_session) {
1822 new_session = NULL;
1823 }
1824 }
1825
1826 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001827 ERR(orig_session, "Session does not have a NETCONF SSH channel ready.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001828 return NC_MSG_ERROR;
1829 }
1830
1831 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001832 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001833
1834 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001835 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001836 if (msgtype != NC_MSG_HELLO) {
1837 return msgtype;
1838 }
1839
Michal Vaskod8a74192023-02-06 15:51:50 +01001840 nc_realtime_get(&ts_cur);
roman44efa322023-11-03 13:57:25 +01001841 new_session->opts.server.session_start = ts_cur;
Michal Vaskod8a74192023-02-06 15:51:50 +01001842 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001843 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko71090fc2016-05-24 16:37:28 +02001844 new_session->status = NC_STATUS_RUNNING;
1845 *session = new_session;
1846
1847 return msgtype;
1848}
1849
1850API NC_MSG_TYPE
Michal Vasko96164bf2016-01-21 15:41:58 +01001851nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +01001852{
Michal Vaskobdcf2362016-07-26 11:35:43 +02001853 uint8_t q_id;
Michal Vasko71090fc2016-05-24 16:37:28 +02001854 NC_MSG_TYPE msgtype;
Michal Vaskoe4300a82017-05-24 10:35:42 +02001855 struct nc_session *new_session = NULL, *cur_session;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001856 struct timespec ts_cur;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001857 uint16_t i;
Michal Vasko086311b2016-01-08 09:53:11 +01001858
roman40672412023-05-04 11:10:22 +02001859 NC_CHECK_ARG_RET(NULL, ps, session, NC_MSG_ERROR);
Michal Vasko086311b2016-01-08 09:53:11 +01001860
Michal Vasko48a63ed2016-03-01 09:48:21 +01001861 /* LOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001862 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001863 return NC_MSG_ERROR;
Michal Vaskof04a52a2016-04-07 10:52:10 +02001864 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001865
Michal Vasko96164bf2016-01-21 15:41:58 +01001866 for (i = 0; i < ps->session_count; ++i) {
fanchanghu3d4e7212017-08-09 09:42:30 +08001867 cur_session = ps->sessions[i]->session;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001868 if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH) &&
1869 cur_session->ti.libssh.next) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001870 /* an SSH session with more channels */
Michal Vaskoe4300a82017-05-24 10:35:42 +02001871 for (new_session = cur_session->ti.libssh.next;
1872 new_session != cur_session;
Michal Vasko96164bf2016-01-21 15:41:58 +01001873 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001874 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1875 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001876 /* we found our session */
1877 break;
1878 }
1879 }
Michal Vaskoe4300a82017-05-24 10:35:42 +02001880 if (new_session != cur_session) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001881 break;
1882 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001883
Michal Vasko96164bf2016-01-21 15:41:58 +01001884 new_session = NULL;
1885 }
1886 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001887
Michal Vasko48a63ed2016-03-01 09:48:21 +01001888 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001889 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001890
Michal Vasko96164bf2016-01-21 15:41:58 +01001891 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001892 ERR(NULL, "No session with a NETCONF SSH channel ready was found.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001893 return NC_MSG_ERROR;
Michal Vasko96164bf2016-01-21 15:41:58 +01001894 }
1895
1896 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001897 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vaskofb89d772016-01-08 12:25:35 +01001898
Michal Vasko086311b2016-01-08 09:53:11 +01001899 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001900 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001901 if (msgtype != NC_MSG_HELLO) {
1902 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001903 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001904
Michal Vaskod8a74192023-02-06 15:51:50 +01001905 nc_realtime_get(&ts_cur);
roman44efa322023-11-03 13:57:25 +01001906 new_session->opts.server.session_start = ts_cur;
Michal Vaskod8a74192023-02-06 15:51:50 +01001907 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001908 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko086311b2016-01-08 09:53:11 +01001909 new_session->status = NC_STATUS_RUNNING;
Michal Vasko96164bf2016-01-21 15:41:58 +01001910 *session = new_session;
Michal Vasko086311b2016-01-08 09:53:11 +01001911
Michal Vasko71090fc2016-05-24 16:37:28 +02001912 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001913}