blob: c8fc072cf7cbb51db3f4dbc152aa915c7ede37e1 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_server_ssh.c
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 SSH server session manipulation functions
5 *
Michal Vasko4c1fb492017-01-30 14:31:07 +01006 * Copyright (c) 2017 CESNET, z.s.p.o.
Michal Vasko086311b2016-01-08 09:53:11 +01007 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
14
15#define _GNU_SOURCE
16
apropp-molex4e903c32020-04-20 03:06:58 -040017#include "config.h" /* Expose HAVE_SHADOW and HAVE_CRYPT */
18
19#ifdef HAVE_SHADOW
20 #include <shadow.h>
21#endif
22#ifdef HAVE_CRYPT
23 #include <crypt.h>
24#endif
25
Michal Vaskob83a3fa2021-05-26 09:53:42 +020026#include <errno.h>
27#include <fcntl.h>
28#include <pwd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010029#include <stdlib.h>
30#include <string.h>
Michal Vasko27252692017-03-21 15:34:13 +010031#include <sys/stat.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020032#include <sys/types.h>
Michal Vasko9f6275e2017-10-05 13:50:05 +020033#include <time.h>
Claus Klein22091912020-01-20 13:45:47 +010034#include <unistd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010035
Michal Vasko7a20d2e2021-05-19 16:40:23 +020036#include "compat.h"
37#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010038#include "session_server.h"
Michal Vaskoe22c6732016-01-29 11:03:02 +010039#include "session_server_ch.h"
Michal Vasko086311b2016-01-08 09:53:11 +010040
Michal Vaskob83a3fa2021-05-26 09:53:42 +020041#if !defined (HAVE_CRYPT_R)
Mislav Novakovicce9a7ef2017-08-08 13:45:52 +020042pthread_mutex_t crypt_lock = PTHREAD_MUTEX_INITIALIZER;
43#endif
44
Michal Vasko086311b2016-01-08 09:53:11 +010045extern struct nc_server_opts server_opts;
Michal Vaskob05053d2016-01-22 16:12:06 +010046
Michal Vasko4c1fb492017-01-30 14:31:07 +010047static char *
Michal Vaskoddce1212019-05-24 09:58:49 +020048base64der_key_to_tmp_file(const char *in, const char *key_str)
Michal Vasko086311b2016-01-08 09:53:11 +010049{
Michal Vasko4c1fb492017-01-30 14:31:07 +010050 char path[12] = "/tmp/XXXXXX";
51 int fd, written;
Michal Vasko27252692017-03-21 15:34:13 +010052 mode_t umode;
Michal Vasko4c1fb492017-01-30 14:31:07 +010053 FILE *file;
54
55 if (in == NULL) {
56 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +010057 }
58
mekleob31878b2019-09-09 14:10:47 +020059 umode = umask(0177);
Michal Vasko4c1fb492017-01-30 14:31:07 +010060 fd = mkstemp(path);
Michal Vasko27252692017-03-21 15:34:13 +010061 umask(umode);
Michal Vasko4c1fb492017-01-30 14:31:07 +010062 if (fd == -1) {
63 return NULL;
64 }
65
Michal Vasko3964a832018-09-18 14:37:39 +020066 file = fdopen(fd, "w");
Michal Vasko4c1fb492017-01-30 14:31:07 +010067 if (!file) {
68 close(fd);
69 return NULL;
70 }
71
72 /* write the key into the file */
Michal Vasko68177b72020-04-27 15:46:53 +020073 if (key_str) {
74 written = fwrite("-----BEGIN ", 1, 11, file);
75 written += fwrite(key_str, 1, strlen(key_str), file);
76 written += fwrite(" PRIVATE KEY-----\n", 1, 18, file);
77 written += fwrite(in, 1, strlen(in), file);
78 written += fwrite("\n-----END ", 1, 10, file);
79 written += fwrite(key_str, 1, strlen(key_str), file);
80 written += fwrite(" PRIVATE KEY-----", 1, 17, file);
Michal Vasko4c1fb492017-01-30 14:31:07 +010081
Michal Vasko68177b72020-04-27 15:46:53 +020082 fclose(file);
83 if ((unsigned)written != 11 + strlen(key_str) + 18 + strlen(in) + 10 + strlen(key_str) + 17) {
84 unlink(path);
85 return NULL;
86 }
87 } else {
88 written = fwrite("-----BEGIN PRIVATE KEY-----\n", 1, 28, file);
89 written += fwrite(in, 1, strlen(in), file);
90 written += fwrite("\n-----END PRIVATE KEY-----", 1, 26, file);
91
92 fclose(file);
93 if ((unsigned)written != 28 + strlen(in) + 26) {
94 unlink(path);
95 return NULL;
96 }
Michal Vasko4c1fb492017-01-30 14:31:07 +010097 }
98
99 return strdup(path);
100}
101
102static int
Michal Vasko7d255882017-02-09 13:35:08 +0100103nc_server_ssh_add_hostkey(const char *name, int16_t idx, struct nc_server_ssh_opts *opts)
Michal Vasko4c1fb492017-01-30 14:31:07 +0100104{
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100105 uint8_t i;
106
Michal Vasko4c1fb492017-01-30 14:31:07 +0100107 if (!name) {
108 ERRARG("name");
Michal Vasko5fcc7142016-02-02 12:21:10 +0100109 return -1;
Michal Vasko7d255882017-02-09 13:35:08 +0100110 } else if (idx > opts->hostkey_count) {
111 ERRARG("idx");
112 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100113 }
Michal Vaskod45e25a2016-01-08 15:48:44 +0100114
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100115 for (i = 0; i < opts->hostkey_count; ++i) {
116 if (!strcmp(opts->hostkeys[i], name)) {
117 ERRARG("name");
118 return -1;
119 }
120 }
121
Michal Vaskoe2713da2016-08-22 16:06:40 +0200122 ++opts->hostkey_count;
123 opts->hostkeys = nc_realloc(opts->hostkeys, opts->hostkey_count * sizeof *opts->hostkeys);
124 if (!opts->hostkeys) {
125 ERRMEM;
126 return -1;
127 }
Michal Vasko7d255882017-02-09 13:35:08 +0100128
129 if (idx < 0) {
130 idx = opts->hostkey_count - 1;
131 }
132 if (idx != opts->hostkey_count - 1) {
133 memmove(opts->hostkeys + idx + 1, opts->hostkeys + idx, opts->hostkey_count - idx);
134 }
Michal Vasko77367452021-02-16 16:32:18 +0100135 lydict_insert(server_opts.ctx, name, 0, &opts->hostkeys[idx]);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200136
Michal Vasko5fcc7142016-02-02 12:21:10 +0100137 return 0;
Michal Vaskob05053d2016-01-22 16:12:06 +0100138}
139
140API int
Michal Vasko7d255882017-02-09 13:35:08 +0100141nc_server_ssh_endpt_add_hostkey(const char *endpt_name, const char *name, int16_t idx)
Michal Vaskob05053d2016-01-22 16:12:06 +0100142{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100143 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100144 struct nc_endpt *endpt;
145
Michal Vasko51e514d2016-02-02 15:51:52 +0100146 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100147 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100148 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100149 return -1;
150 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200151
Michal Vasko7d255882017-02-09 13:35:08 +0100152 ret = nc_server_ssh_add_hostkey(name, idx, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200153
Michal Vasko51e514d2016-02-02 15:51:52 +0100154 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100155 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100156
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100157 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100158}
159
Michal Vasko974410a2018-04-03 09:36:57 +0200160API void
161nc_server_ssh_set_passwd_auth_clb(int (*passwd_auth_clb)(const struct nc_session *session, const char *password, void *user_data),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200162 void *user_data, void (*free_user_data)(void *user_data))
Michal Vasko974410a2018-04-03 09:36:57 +0200163{
164 server_opts.passwd_auth_clb = passwd_auth_clb;
165 server_opts.passwd_auth_data = user_data;
166 server_opts.passwd_auth_data_free = free_user_data;
167}
168
bhart1bb7cdb2018-07-02 15:03:30 -0500169API void
170nc_server_ssh_set_interactive_auth_clb(int (*interactive_auth_clb)(const struct nc_session *session, ssh_message msg, void *user_data),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200171 void *user_data, void (*free_user_data)(void *user_data))
bhart1bb7cdb2018-07-02 15:03:30 -0500172{
173 server_opts.interactive_auth_clb = interactive_auth_clb;
174 server_opts.interactive_auth_data = user_data;
175 server_opts.interactive_auth_data_free = free_user_data;
176}
Michal Vasko733c0bd2018-07-03 13:14:40 +0200177
bhart1bb7cdb2018-07-02 15:03:30 -0500178API void
179nc_server_ssh_set_pubkey_auth_clb(int (*pubkey_auth_clb)(const struct nc_session *session, ssh_key key, void *user_data),
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200180 void *user_data, void (*free_user_data)(void *user_data))
bhart1bb7cdb2018-07-02 15:03:30 -0500181{
182 server_opts.pubkey_auth_clb = pubkey_auth_clb;
183 server_opts.pubkey_auth_data = user_data;
184 server_opts.pubkey_auth_data_free = free_user_data;
185}
186
Michal Vaskob05053d2016-01-22 16:12:06 +0100187API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200188nc_server_ssh_ch_client_endpt_add_hostkey(const char *client_name, const char *endpt_name, const char *name, int16_t idx)
Michal Vaskob05053d2016-01-22 16:12:06 +0100189{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100190 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200191 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200192 struct nc_ch_endpt *endpt;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100193
Michal Vasko2e6defd2016-10-07 15:48:15 +0200194 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200195 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
196 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200197 return -1;
198 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200199
Michal Vaskoadf30f02019-06-24 09:34:47 +0200200 ret = nc_server_ssh_add_hostkey(name, idx, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200201
Michal Vasko2e6defd2016-10-07 15:48:15 +0200202 /* UNLOCK */
203 nc_server_ch_client_unlock(client);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200204
205 return ret;
206}
207
Michal Vasko4c1fb492017-01-30 14:31:07 +0100208API void
209nc_server_ssh_set_hostkey_clb(int (*hostkey_clb)(const char *name, void *user_data, char **privkey_path,
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200210 char **privkey_data, NC_SSH_KEY_TYPE *privkey_type), void *user_data, void (*free_user_data)(void *user_data))
Michal Vasko4c1fb492017-01-30 14:31:07 +0100211{
212 if (!hostkey_clb) {
213 ERRARG("hostkey_clb");
214 return;
215 }
216
217 server_opts.hostkey_clb = hostkey_clb;
218 server_opts.hostkey_data = user_data;
219 server_opts.hostkey_data_free = free_user_data;
220}
221
Michal Vaskoe2713da2016-08-22 16:06:40 +0200222static int
Michal Vasko7d255882017-02-09 13:35:08 +0100223nc_server_ssh_del_hostkey(const char *name, int16_t idx, struct nc_server_ssh_opts *opts)
Michal Vaskoe2713da2016-08-22 16:06:40 +0200224{
225 uint8_t i;
226
Michal Vasko7d255882017-02-09 13:35:08 +0100227 if (name && (idx > -1)) {
228 ERRARG("name and idx");
229 return -1;
230 } else if (idx >= opts->hostkey_count) {
231 ERRARG("idx");
232 }
233
234 if (!name && (idx < 0)) {
Michal Vaskoe2713da2016-08-22 16:06:40 +0200235 for (i = 0; i < opts->hostkey_count; ++i) {
236 lydict_remove(server_opts.ctx, opts->hostkeys[i]);
237 }
238 free(opts->hostkeys);
239 opts->hostkeys = NULL;
240 opts->hostkey_count = 0;
Michal Vasko7d255882017-02-09 13:35:08 +0100241 } else if (name) {
Michal Vaskoe2713da2016-08-22 16:06:40 +0200242 for (i = 0; i < opts->hostkey_count; ++i) {
Michal Vasko4c1fb492017-01-30 14:31:07 +0100243 if (!strcmp(opts->hostkeys[i], name)) {
Michal Vasko7d255882017-02-09 13:35:08 +0100244 idx = i;
245 goto remove_idx;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200246 }
247 }
248
Michal Vasko7d255882017-02-09 13:35:08 +0100249 ERRARG("name");
Michal Vaskoe2713da2016-08-22 16:06:40 +0200250 return -1;
Michal Vasko7d255882017-02-09 13:35:08 +0100251 } else {
252remove_idx:
253 --opts->hostkey_count;
254 lydict_remove(server_opts.ctx, opts->hostkeys[idx]);
255 if (idx < opts->hostkey_count - 1) {
256 memmove(opts->hostkeys + idx, opts->hostkeys + idx + 1, (opts->hostkey_count - idx) * sizeof *opts->hostkeys);
257 }
258 if (!opts->hostkey_count) {
259 free(opts->hostkeys);
260 opts->hostkeys = NULL;
261 }
Michal Vaskoe2713da2016-08-22 16:06:40 +0200262 }
263
264 return 0;
265}
266
267API int
Michal Vasko7d255882017-02-09 13:35:08 +0100268nc_server_ssh_endpt_del_hostkey(const char *endpt_name, const char *name, int16_t idx)
Michal Vaskoe2713da2016-08-22 16:06:40 +0200269{
270 int ret;
271 struct nc_endpt *endpt;
272
273 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100274 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200275 if (!endpt) {
276 return -1;
277 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200278
Michal Vasko7d255882017-02-09 13:35:08 +0100279 ret = nc_server_ssh_del_hostkey(name, idx, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200280
Michal Vaskoe2713da2016-08-22 16:06:40 +0200281 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100282 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200283
284 return ret;
285}
286
287API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200288nc_server_ssh_ch_client_endpt_del_hostkey(const char *client_name, const char *endpt_name, const char *name, int16_t idx)
Michal Vaskoe2713da2016-08-22 16:06:40 +0200289{
290 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200291 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200292 struct nc_ch_endpt *endpt;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200293
Michal Vasko2e6defd2016-10-07 15:48:15 +0200294 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200295 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
296 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200297 return -1;
298 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200299
Michal Vaskoadf30f02019-06-24 09:34:47 +0200300 ret = nc_server_ssh_del_hostkey(name, idx, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200301
Michal Vasko2e6defd2016-10-07 15:48:15 +0200302 /* UNLOCK */
303 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100304
305 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100306}
307
308static int
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100309nc_server_ssh_mov_hostkey(const char *key_mov, const char *key_after, struct nc_server_ssh_opts *opts)
310{
311 uint8_t i;
312 int16_t mov_idx = -1, after_idx = -1;
313 const char *bckup;
314
315 if (!key_mov) {
316 ERRARG("key_mov");
317 return -1;
318 }
319
320 for (i = 0; i < opts->hostkey_count; ++i) {
321 if (key_after && (after_idx == -1) && !strcmp(opts->hostkeys[i], key_after)) {
322 after_idx = i;
323 }
324 if ((mov_idx == -1) && !strcmp(opts->hostkeys[i], key_mov)) {
325 mov_idx = i;
326 }
327
328 if ((!key_after || (after_idx > -1)) && (mov_idx > -1)) {
329 break;
330 }
331 }
332
333 if (key_after && (after_idx == -1)) {
334 ERRARG("key_after");
335 return -1;
336 }
337 if (mov_idx == -1) {
338 ERRARG("key_mov");
339 return -1;
340 }
341 if ((mov_idx == after_idx) || (mov_idx == after_idx + 1)) {
342 /* nothing to do */
343 return 0;
344 }
345
346 /* finally move the key */
347 bckup = opts->hostkeys[mov_idx];
348 if (mov_idx > after_idx) {
349 memmove(opts->hostkeys + after_idx + 2, opts->hostkeys + after_idx + 1,
350 ((mov_idx - after_idx) - 1) * sizeof *opts->hostkeys);
351 opts->hostkeys[after_idx + 1] = bckup;
352 } else {
353 memmove(opts->hostkeys + mov_idx, opts->hostkeys + mov_idx + 1, (after_idx - mov_idx) * sizeof *opts->hostkeys);
354 opts->hostkeys[after_idx] = bckup;
355 }
356
357 return 0;
358}
359
360API int
361nc_server_ssh_endpt_mov_hostkey(const char *endpt_name, const char *key_mov, const char *key_after)
362{
363 int ret;
364 struct nc_endpt *endpt;
365
366 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100367 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100368 if (!endpt) {
369 return -1;
370 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200371
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100372 ret = nc_server_ssh_mov_hostkey(key_mov, key_after, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200373
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100374 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100375 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100376
377 return ret;
378}
379
380API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200381nc_server_ssh_ch_client_endpt_mov_hostkey(const char *client_name, const char *endpt_name, const char *key_mov,
382 const char *key_after)
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100383{
384 int ret;
385 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200386 struct nc_ch_endpt *endpt;
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100387
388 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200389 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100390 if (!endpt) {
391 return -1;
392 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200393
Michal Vaskoadf30f02019-06-24 09:34:47 +0200394 ret = nc_server_ssh_mov_hostkey(key_mov, key_after, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200395
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100396 /* UNLOCK */
397 nc_server_ch_client_unlock(client);
398
399 return ret;
400}
401
402static int
Michal Vasko3031aae2016-01-27 16:07:18 +0100403nc_server_ssh_set_auth_methods(int auth_methods, struct nc_server_ssh_opts *opts)
Michal Vaskob05053d2016-01-22 16:12:06 +0100404{
Michal Vaskob05053d2016-01-22 16:12:06 +0100405 opts->auth_methods = auth_methods;
406 return 0;
407}
408
409API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100410nc_server_ssh_endpt_set_auth_methods(const char *endpt_name, int auth_methods)
Michal Vaskob05053d2016-01-22 16:12:06 +0100411{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100412 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100413 struct nc_endpt *endpt;
414
Michal Vasko51e514d2016-02-02 15:51:52 +0100415 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100416 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100417 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100418 return -1;
419 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200420
Michal Vasko2e6defd2016-10-07 15:48:15 +0200421 ret = nc_server_ssh_set_auth_methods(auth_methods, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200422
Michal Vasko51e514d2016-02-02 15:51:52 +0100423 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100424 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100425
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100426 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100427}
428
429API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200430nc_server_ssh_ch_client_endpt_set_auth_methods(const char *client_name, const char *endpt_name, int auth_methods)
Michal Vaskob05053d2016-01-22 16:12:06 +0100431{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100432 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200433 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200434 struct nc_ch_endpt *endpt;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100435
Michal Vasko2e6defd2016-10-07 15:48:15 +0200436 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200437 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
438 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200439 return -1;
440 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200441
Michal Vaskoadf30f02019-06-24 09:34:47 +0200442 ret = nc_server_ssh_set_auth_methods(auth_methods, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200443
Michal Vasko2e6defd2016-10-07 15:48:15 +0200444 /* UNLOCK */
445 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100446
447 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100448}
449
Michal Vaskoddce1212019-05-24 09:58:49 +0200450API int
451nc_server_ssh_endpt_get_auth_methods(const char *endpt_name)
452{
453 int ret;
454 struct nc_endpt *endpt;
455
456 /* LOCK */
457 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
458 if (!endpt) {
459 return -1;
460 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200461
Michal Vaskoddce1212019-05-24 09:58:49 +0200462 ret = endpt->opts.ssh->auth_methods;
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200463
Michal Vaskoddce1212019-05-24 09:58:49 +0200464 /* UNLOCK */
465 pthread_rwlock_unlock(&server_opts.endpt_lock);
466
467 return ret;
468}
469
470API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200471nc_server_ssh_ch_client_endpt_get_auth_methods(const char *client_name, const char *endpt_name)
Michal Vaskoddce1212019-05-24 09:58:49 +0200472{
473 int ret;
474 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200475 struct nc_ch_endpt *endpt;
Michal Vaskoddce1212019-05-24 09:58:49 +0200476
477 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200478 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
479 if (!endpt) {
Michal Vaskoddce1212019-05-24 09:58:49 +0200480 return -1;
481 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200482
Michal Vaskoadf30f02019-06-24 09:34:47 +0200483 ret = endpt->opts.ssh->auth_methods;
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200484
Michal Vaskoddce1212019-05-24 09:58:49 +0200485 /* UNLOCK */
486 nc_server_ch_client_unlock(client);
487
488 return ret;
489}
490
Michal Vaskob05053d2016-01-22 16:12:06 +0100491static int
Michal Vasko3031aae2016-01-27 16:07:18 +0100492nc_server_ssh_set_auth_attempts(uint16_t auth_attempts, struct nc_server_ssh_opts *opts)
Michal Vaskob05053d2016-01-22 16:12:06 +0100493{
Michal Vaskob05053d2016-01-22 16:12:06 +0100494 if (!auth_attempts) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200495 ERRARG("auth_attempts");
Michal Vaskob05053d2016-01-22 16:12:06 +0100496 return -1;
497 }
498
Michal Vaskob05053d2016-01-22 16:12:06 +0100499 opts->auth_attempts = auth_attempts;
Michal Vasko086311b2016-01-08 09:53:11 +0100500 return 0;
501}
502
503API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100504nc_server_ssh_endpt_set_auth_attempts(const char *endpt_name, uint16_t auth_attempts)
Michal Vasko086311b2016-01-08 09:53:11 +0100505{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100506 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100507 struct nc_endpt *endpt;
508
Michal Vasko51e514d2016-02-02 15:51:52 +0100509 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100510 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100511 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100512 return -1;
513 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200514
Michal Vasko2e6defd2016-10-07 15:48:15 +0200515 ret = nc_server_ssh_set_auth_attempts(auth_attempts, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200516
Michal Vasko51e514d2016-02-02 15:51:52 +0100517 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100518 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100519
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100520 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100521}
522
523API int
Michal Vaskocbad4c52019-06-27 16:30:35 +0200524nc_server_ssh_ch_client_endpt_set_auth_attempts(const char *client_name, const char *endpt_name, uint16_t auth_attempts)
Michal Vaskob05053d2016-01-22 16:12:06 +0100525{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100526 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200527 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200528 struct nc_ch_endpt *endpt;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100529
Michal Vasko2e6defd2016-10-07 15:48:15 +0200530 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200531 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
532 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200533 return -1;
534 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200535
Michal Vaskoadf30f02019-06-24 09:34:47 +0200536 ret = nc_server_ssh_set_auth_attempts(auth_attempts, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200537
Michal Vasko2e6defd2016-10-07 15:48:15 +0200538 /* UNLOCK */
539 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100540
541 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100542}
543
544static int
Michal Vasko3031aae2016-01-27 16:07:18 +0100545nc_server_ssh_set_auth_timeout(uint16_t auth_timeout, struct nc_server_ssh_opts *opts)
Michal Vaskob05053d2016-01-22 16:12:06 +0100546{
Michal Vaskob05053d2016-01-22 16:12:06 +0100547 if (!auth_timeout) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200548 ERRARG("auth_timeout");
Michal Vasko086311b2016-01-08 09:53:11 +0100549 return -1;
550 }
551
Michal Vaskob05053d2016-01-22 16:12:06 +0100552 opts->auth_timeout = auth_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100553 return 0;
554}
555
556API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100557nc_server_ssh_endpt_set_auth_timeout(const char *endpt_name, uint16_t auth_timeout)
Michal Vasko086311b2016-01-08 09:53:11 +0100558{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100559 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100560 struct nc_endpt *endpt;
561
Michal Vasko51e514d2016-02-02 15:51:52 +0100562 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100563 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100564 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100565 return -1;
566 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200567
Michal Vasko2e6defd2016-10-07 15:48:15 +0200568 ret = nc_server_ssh_set_auth_timeout(auth_timeout, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200569
Michal Vasko51e514d2016-02-02 15:51:52 +0100570 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100571 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100572
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100573 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100574}
575
576API int
Michal Vaskocbad4c52019-06-27 16:30:35 +0200577nc_server_ssh_ch_client_endpt_set_auth_timeout(const char *client_name, const char *endpt_name, uint16_t auth_timeout)
Michal Vaskob05053d2016-01-22 16:12:06 +0100578{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100579 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200580 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200581 struct nc_ch_endpt *endpt;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100582
Michal Vasko2e6defd2016-10-07 15:48:15 +0200583 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200584 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
585 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200586 return -1;
587 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200588
Michal Vaskoadf30f02019-06-24 09:34:47 +0200589 ret = nc_server_ssh_set_auth_timeout(auth_timeout, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200590
Michal Vasko2e6defd2016-10-07 15:48:15 +0200591 /* UNLOCK */
592 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100593
594 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100595}
596
597static int
Michal Vasko77367452021-02-16 16:32:18 +0100598_nc_server_ssh_add_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type, const char *username)
Michal Vasko17dfda92016-12-01 14:06:16 +0100599{
Michal Vaskoa05c7b12017-01-30 14:33:08 +0100600 /* LOCK */
601 pthread_mutex_lock(&server_opts.authkey_lock);
602
Michal Vasko17dfda92016-12-01 14:06:16 +0100603 ++server_opts.authkey_count;
604 server_opts.authkeys = nc_realloc(server_opts.authkeys, server_opts.authkey_count * sizeof *server_opts.authkeys);
605 if (!server_opts.authkeys) {
606 ERRMEM;
607 return -1;
608 }
Michal Vasko77367452021-02-16 16:32:18 +0100609 lydict_insert(server_opts.ctx, pubkey_path, 0, &server_opts.authkeys[server_opts.authkey_count - 1].path);
610 lydict_insert(server_opts.ctx, pubkey_base64, 0, &server_opts.authkeys[server_opts.authkey_count - 1].base64);
Michal Vasko17dfda92016-12-01 14:06:16 +0100611 server_opts.authkeys[server_opts.authkey_count - 1].type = type;
Michal Vasko77367452021-02-16 16:32:18 +0100612 lydict_insert(server_opts.ctx, username, 0, &server_opts.authkeys[server_opts.authkey_count - 1].username);
Michal Vasko17dfda92016-12-01 14:06:16 +0100613
Michal Vaskoa05c7b12017-01-30 14:33:08 +0100614 /* UNLOCK */
615 pthread_mutex_unlock(&server_opts.authkey_lock);
616
Michal Vasko17dfda92016-12-01 14:06:16 +0100617 return 0;
618}
619
620API int
621nc_server_ssh_add_authkey_path(const char *pubkey_path, const char *username)
Michal Vaskob05053d2016-01-22 16:12:06 +0100622{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200623 if (!pubkey_path) {
624 ERRARG("pubkey_path");
625 return -1;
626 } else if (!username) {
627 ERRARG("username");
Michal Vasko086311b2016-01-08 09:53:11 +0100628 return -1;
629 }
630
Michal Vasko17dfda92016-12-01 14:06:16 +0100631 return _nc_server_ssh_add_authkey(pubkey_path, NULL, 0, username);
Michal Vasko086311b2016-01-08 09:53:11 +0100632}
633
634API int
Michal Vasko17dfda92016-12-01 14:06:16 +0100635nc_server_ssh_add_authkey(const char *pubkey_base64, NC_SSH_KEY_TYPE type, const char *username)
Michal Vasko086311b2016-01-08 09:53:11 +0100636{
Michal Vasko17dfda92016-12-01 14:06:16 +0100637 if (!pubkey_base64) {
638 ERRARG("pubkey_base64");
639 return -1;
640 } else if (!type) {
641 ERRARG("type");
642 return -1;
643 } else if (!username) {
644 ERRARG("username");
Michal Vasko3031aae2016-01-27 16:07:18 +0100645 return -1;
646 }
647
Michal Vasko17dfda92016-12-01 14:06:16 +0100648 return _nc_server_ssh_add_authkey(NULL, pubkey_base64, type, username);
Michal Vasko086311b2016-01-08 09:53:11 +0100649}
650
651API int
Michal Vasko17dfda92016-12-01 14:06:16 +0100652nc_server_ssh_del_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200653 const char *username)
Michal Vaskob05053d2016-01-22 16:12:06 +0100654{
Michal Vasko086311b2016-01-08 09:53:11 +0100655 uint32_t i;
656 int ret = -1;
657
Michal Vasko17dfda92016-12-01 14:06:16 +0100658 /* LOCK */
659 pthread_mutex_lock(&server_opts.authkey_lock);
660
661 if (!pubkey_path && !pubkey_base64 && !type && !username) {
662 for (i = 0; i < server_opts.authkey_count; ++i) {
663 lydict_remove(server_opts.ctx, server_opts.authkeys[i].path);
664 lydict_remove(server_opts.ctx, server_opts.authkeys[i].base64);
665 lydict_remove(server_opts.ctx, server_opts.authkeys[i].username);
Michal Vasko086311b2016-01-08 09:53:11 +0100666
Michal Vasko086311b2016-01-08 09:53:11 +0100667 ret = 0;
668 }
Michal Vasko17dfda92016-12-01 14:06:16 +0100669 free(server_opts.authkeys);
670 server_opts.authkeys = NULL;
671 server_opts.authkey_count = 0;
Michal Vasko1a38c862016-01-15 15:50:07 +0100672 } else {
Michal Vasko17dfda92016-12-01 14:06:16 +0100673 for (i = 0; i < server_opts.authkey_count; ++i) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200674 if ((!pubkey_path || !strcmp(server_opts.authkeys[i].path, pubkey_path)) &&
675 (!pubkey_base64 || !strcmp(server_opts.authkeys[i].base64, pubkey_base64)) &&
676 (!type || (server_opts.authkeys[i].type == type)) &&
677 (!username || !strcmp(server_opts.authkeys[i].username, username))) {
Michal Vasko17dfda92016-12-01 14:06:16 +0100678 lydict_remove(server_opts.ctx, server_opts.authkeys[i].path);
679 lydict_remove(server_opts.ctx, server_opts.authkeys[i].base64);
680 lydict_remove(server_opts.ctx, server_opts.authkeys[i].username);
Michal Vasko1a38c862016-01-15 15:50:07 +0100681
Michal Vasko17dfda92016-12-01 14:06:16 +0100682 --server_opts.authkey_count;
683 if (i < server_opts.authkey_count) {
684 memcpy(&server_opts.authkeys[i], &server_opts.authkeys[server_opts.authkey_count],
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200685 sizeof *server_opts.authkeys);
Michal Vasko17dfda92016-12-01 14:06:16 +0100686 } else if (!server_opts.authkey_count) {
687 free(server_opts.authkeys);
688 server_opts.authkeys = NULL;
Michal Vaskoc0256492016-02-02 12:19:06 +0100689 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100690
691 ret = 0;
692 }
693 }
Michal Vasko086311b2016-01-08 09:53:11 +0100694 }
695
Michal Vasko51e514d2016-02-02 15:51:52 +0100696 /* UNLOCK */
Michal Vasko17dfda92016-12-01 14:06:16 +0100697 pthread_mutex_unlock(&server_opts.authkey_lock);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100698
699 return ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100700}
701
702void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100703nc_server_ssh_clear_opts(struct nc_server_ssh_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +0100704{
Michal Vasko7d255882017-02-09 13:35:08 +0100705 nc_server_ssh_del_hostkey(NULL, -1, opts);
Michal Vaskob05053d2016-01-22 16:12:06 +0100706}
707
Michal Vasko7a866152021-07-22 11:01:13 +0200708#ifdef HAVE_SHADOW
709
710static struct passwd *
711auth_password_getpwnam(const char *username, struct passwd *pwd_buf, char **buf, size_t *buf_size)
712{
713 struct passwd *pwd = NULL;
714 char *mem;
715
716 do {
717 errno = 0;
718 getpwnam_r(username, pwd_buf, *buf, *buf_size, &pwd);
719 if (pwd) {
720 /* entry found */
721 break;
722 }
723
724 if (errno == ERANGE) {
725 /* small buffer, enlarge */
726 *buf_size <<= 2;
727 mem = realloc(*buf, *buf_size);
728 if (!mem) {
729 ERRMEM;
730 return NULL;
731 }
732 *buf = mem;
733 }
734 } while (errno == ERANGE);
735
736 return pwd;
737}
738
739static struct spwd *
740auth_password_getspnam(const char *username, struct spwd *spwd_buf, char **buf, size_t *buf_size)
741{
742 struct spwd *spwd = NULL;
743 char *mem;
744
745 do {
746 errno = 0;
747# ifndef __QNXNTO__
748 getspnam_r(username, spwd_buf, *buf, *buf_size, &spwd);
749# else
750 spwd = getspnam_r(username, spwd_buf, *buf, *buf_size);
751# endif
752 if (spwd) {
753 /* entry found */
754 break;
755 }
756
757 if (errno == ERANGE) {
758 /* small buffer, enlarge */
759 *buf_size <<= 2;
760 mem = realloc(*buf, *buf_size);
761 if (!mem) {
762 ERRMEM;
763 return NULL;
764 }
765 *buf = mem;
766 }
767 } while (errno == ERANGE);
768
769 return spwd;
770}
771
Michal Vasko086311b2016-01-08 09:53:11 +0100772static char *
773auth_password_get_pwd_hash(const char *username)
774{
775 struct passwd *pwd, pwd_buf;
776 struct spwd *spwd, spwd_buf;
Michal Vasko7a866152021-07-22 11:01:13 +0200777 char *pass_hash = NULL, *buf = NULL;
778 size_t buf_size = 256;
Michal Vasko086311b2016-01-08 09:53:11 +0100779
Michal Vasko7a866152021-07-22 11:01:13 +0200780 buf = malloc(buf_size);
781 if (!buf) {
782 ERRMEM;
783 goto error;
784 }
785
786 pwd = auth_password_getpwnam(username, &pwd_buf, &buf, &buf_size);
Michal Vasko086311b2016-01-08 09:53:11 +0100787 if (!pwd) {
Michal Vasko05532772021-06-03 12:12:38 +0200788 VRB(NULL, "User \"%s\" not found locally.", username);
Michal Vasko7a866152021-07-22 11:01:13 +0200789 goto error;
Michal Vasko086311b2016-01-08 09:53:11 +0100790 }
791
792 if (!strcmp(pwd->pw_passwd, "x")) {
Michal Vasko7a866152021-07-22 11:01:13 +0200793 spwd = auth_password_getspnam(username, &spwd_buf, &buf, &buf_size);
Michal Vasko086311b2016-01-08 09:53:11 +0100794 if (!spwd) {
Michal Vasko05532772021-06-03 12:12:38 +0200795 VRB(NULL, "Failed to retrieve the shadow entry for \"%s\".", username);
Michal Vasko7a866152021-07-22 11:01:13 +0200796 goto error;
Michal Vasko22b4fe72020-11-04 08:52:29 +0100797 } else if ((spwd->sp_expire > -1) && (spwd->sp_expire <= (time(NULL) / (60 * 60 * 24)))) {
Michal Vasko05532772021-06-03 12:12:38 +0200798 WRN(NULL, "User \"%s\" account has expired.", username);
Michal Vasko7a866152021-07-22 11:01:13 +0200799 goto error;
Michal Vasko086311b2016-01-08 09:53:11 +0100800 }
801
802 pass_hash = spwd->sp_pwdp;
803 } else {
804 pass_hash = pwd->pw_passwd;
805 }
806
807 if (!pass_hash) {
Michal Vasko05532772021-06-03 12:12:38 +0200808 ERR(NULL, "No password could be retrieved for \"%s\".", username);
Michal Vasko7a866152021-07-22 11:01:13 +0200809 goto error;
Michal Vasko086311b2016-01-08 09:53:11 +0100810 }
811
812 /* check the hash structure for special meaning */
813 if (!strcmp(pass_hash, "*") || !strcmp(pass_hash, "!")) {
Michal Vasko05532772021-06-03 12:12:38 +0200814 VRB(NULL, "User \"%s\" is not allowed to authenticate using a password.", username);
Michal Vasko7a866152021-07-22 11:01:13 +0200815 goto error;
Michal Vasko086311b2016-01-08 09:53:11 +0100816 }
817 if (!strcmp(pass_hash, "*NP*")) {
Michal Vasko05532772021-06-03 12:12:38 +0200818 VRB(NULL, "Retrieving password for \"%s\" from a NIS+ server not supported.", username);
Michal Vasko7a866152021-07-22 11:01:13 +0200819 goto error;
Michal Vasko086311b2016-01-08 09:53:11 +0100820 }
821
Michal Vasko7a866152021-07-22 11:01:13 +0200822 free(buf);
Michal Vasko086311b2016-01-08 09:53:11 +0100823 return strdup(pass_hash);
Michal Vasko7a866152021-07-22 11:01:13 +0200824
825error:
826 free(buf);
827 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100828}
829
Michal Vasko7a866152021-07-22 11:01:13 +0200830#else
831
832static char *
833auth_password_get_pwd_hash(const char *username)
834{
835 (void)username;
836 return strdup("");
837}
838
839#endif
840
Michal Vasko086311b2016-01-08 09:53:11 +0100841static int
842auth_password_compare_pwd(const char *pass_hash, const char *pass_clear)
843{
844 char *new_pass_hash;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200845
846#if defined (HAVE_CRYPT_R)
Michal Vasko086311b2016-01-08 09:53:11 +0100847 struct crypt_data cdata;
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200848#endif
Michal Vasko086311b2016-01-08 09:53:11 +0100849
850 if (!pass_hash[0]) {
851 if (!pass_clear[0]) {
Michal Vasko05532772021-06-03 12:12:38 +0200852 WRN(NULL, "User authentication successful with an empty password!");
Michal Vasko086311b2016-01-08 09:53:11 +0100853 return 0;
854 } else {
855 /* the user did now know he does not need any password,
856 * (which should not be used) so deny authentication */
857 return 1;
858 }
859 }
860
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200861#if defined (HAVE_CRYPT_R)
Michal Vasko086311b2016-01-08 09:53:11 +0100862 cdata.initialized = 0;
863 new_pass_hash = crypt_r(pass_clear, pass_hash, &cdata);
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200864#else
Mislav Novakovicce9a7ef2017-08-08 13:45:52 +0200865 pthread_mutex_lock(&crypt_lock);
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200866 new_pass_hash = crypt(pass_clear, pass_hash);
Mislav Novakovicce9a7ef2017-08-08 13:45:52 +0200867 pthread_mutex_unlock(&crypt_lock);
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200868#endif
Andrew Langefeld158d6fd2018-06-11 18:51:44 -0500869
870 if (!new_pass_hash) {
871 return 1;
872 }
873
Michal Vasko086311b2016-01-08 09:53:11 +0100874 return strcmp(new_pass_hash, pass_hash);
875}
876
877static void
878nc_sshcb_auth_password(struct nc_session *session, ssh_message msg)
879{
880 char *pass_hash;
Michal Vaskoebba7602018-03-23 13:14:08 +0100881 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100882
Michal Vaskoebba7602018-03-23 13:14:08 +0100883 if (server_opts.passwd_auth_clb) {
884 auth_ret = server_opts.passwd_auth_clb(session, ssh_message_auth_password(msg), server_opts.passwd_auth_data);
885 } else {
886 pass_hash = auth_password_get_pwd_hash(session->username);
887 if (pass_hash) {
888 auth_ret = auth_password_compare_pwd(pass_hash, ssh_message_auth_password(msg));
889 free(pass_hash);
890 }
Michal Vasko086311b2016-01-08 09:53:11 +0100891 }
892
Michal Vaskoebba7602018-03-23 13:14:08 +0100893 if (!auth_ret) {
894 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
Michal Vasko05532772021-06-03 12:12:38 +0200895 VRB(session, "User \"%s\" authenticated.", session->username);
Michal Vaskoebba7602018-03-23 13:14:08 +0100896 ssh_message_auth_reply_success(msg, 0);
897 } else {
898 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200899 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
900 session->opts.server.ssh_auth_attempts);
Michal Vaskoebba7602018-03-23 13:14:08 +0100901 ssh_message_reply_default(msg);
902 }
Michal Vasko086311b2016-01-08 09:53:11 +0100903}
904
905static void
906nc_sshcb_auth_kbdint(struct nc_session *session, ssh_message msg)
907{
bhart3bc2f582018-06-05 12:40:32 -0500908 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100909 char *pass_hash;
910
bhart1bb7cdb2018-07-02 15:03:30 -0500911 if (server_opts.interactive_auth_clb) {
Michal Vasko733c0bd2018-07-03 13:14:40 +0200912 auth_ret = server_opts.interactive_auth_clb(session, msg, server_opts.interactive_auth_data);
Michal Vasko086311b2016-01-08 09:53:11 +0100913 } else {
bhart1bb7cdb2018-07-02 15:03:30 -0500914 if (!ssh_message_auth_kbdint_is_response(msg)) {
915 const char *prompts[] = {"Password: "};
916 char echo[] = {0};
917
918 ssh_message_auth_interactive_request(msg, "Interactive SSH Authentication", "Type your password:", 1, prompts, echo);
Robert Vargaad7a5532018-08-10 20:40:54 +0200919 auth_ret = -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100920 } else {
bhart1bb7cdb2018-07-02 15:03:30 -0500921 if (ssh_userauth_kbdint_getnanswers(session->ti.libssh.session) != 1) {// failed session
922 ssh_message_reply_default(msg);
923 return;
924 }
bhart3bc2f582018-06-05 12:40:32 -0500925 pass_hash = auth_password_get_pwd_hash(session->username);// get hashed password
926 if (pass_hash) {
Robert Vargaad7a5532018-08-10 20:40:54 +0200927 /* Normalize auth_password_compare_pwd result to 0 or 1 */
928 auth_ret = !!auth_password_compare_pwd(pass_hash, ssh_userauth_kbdint_getanswer(session->ti.libssh.session, 0));
bhart3bc2f582018-06-05 12:40:32 -0500929 free(pass_hash);// free hashed password
930 }
Michal Vasko086311b2016-01-08 09:53:11 +0100931 }
bhart1bb7cdb2018-07-02 15:03:30 -0500932 }
933
Robert Vargaad7a5532018-08-10 20:40:54 +0200934 /* We have already sent a reply */
935 if (auth_ret == -1) {
936 return;
937 }
938
bhart1bb7cdb2018-07-02 15:03:30 -0500939 /* Authenticate message based on outcome */
940 if (!auth_ret) {
941 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
Michal Vasko05532772021-06-03 12:12:38 +0200942 VRB(session, "User \"%s\" authenticated.", session->username);
bhart1bb7cdb2018-07-02 15:03:30 -0500943 ssh_message_auth_reply_success(msg, 0);
944 } else {
945 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200946 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
947 session->opts.server.ssh_auth_attempts);
bhart1bb7cdb2018-07-02 15:03:30 -0500948 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +0100949 }
950}
951
952static const char *
Michal Vasko17dfda92016-12-01 14:06:16 +0100953auth_pubkey_compare_key(ssh_key key)
Michal Vasko086311b2016-01-08 09:53:11 +0100954{
955 uint32_t i;
956 ssh_key pub_key;
Michal Vasko76e3a352016-01-18 09:07:00 +0100957 const char *username = NULL;
Michal Vasko3e9d1682017-02-24 09:50:15 +0100958 int ret = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100959
Michal Vaskoa05c7b12017-01-30 14:33:08 +0100960 /* LOCK */
961 pthread_mutex_lock(&server_opts.authkey_lock);
962
Michal Vasko17dfda92016-12-01 14:06:16 +0100963 for (i = 0; i < server_opts.authkey_count; ++i) {
964 switch (server_opts.authkeys[i].type) {
965 case NC_SSH_KEY_UNKNOWN:
966 ret = ssh_pki_import_pubkey_file(server_opts.authkeys[i].path, &pub_key);
967 break;
968 case NC_SSH_KEY_DSA:
969 ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_DSS, &pub_key);
970 break;
971 case NC_SSH_KEY_RSA:
972 ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_RSA, &pub_key);
973 break;
974 case NC_SSH_KEY_ECDSA:
975 ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_ECDSA, &pub_key);
976 break;
977 }
978
Michal Vasko7abcdeb2016-05-30 15:27:00 +0200979 if (ret == SSH_EOF) {
Michal Vasko05532772021-06-03 12:12:38 +0200980 WRN(NULL, "Failed to import a public key of \"%s\" (File access problem).", server_opts.authkeys[i].username);
Michal Vasko7abcdeb2016-05-30 15:27:00 +0200981 continue;
982 } else if (ret == SSH_ERROR) {
Michal Vasko05532772021-06-03 12:12:38 +0200983 WRN(NULL, "Failed to import a public key of \"%s\" (SSH error).", server_opts.authkeys[i].username);
Michal Vasko086311b2016-01-08 09:53:11 +0100984 continue;
985 }
986
987 if (!ssh_key_cmp(key, pub_key, SSH_KEY_CMP_PUBLIC)) {
988 ssh_key_free(pub_key);
989 break;
990 }
991
992 ssh_key_free(pub_key);
993 }
994
Michal Vasko17dfda92016-12-01 14:06:16 +0100995 if (i < server_opts.authkey_count) {
996 username = server_opts.authkeys[i].username;
Michal Vasko086311b2016-01-08 09:53:11 +0100997 }
998
Michal Vaskoa05c7b12017-01-30 14:33:08 +0100999 /* UNLOCK */
1000 pthread_mutex_unlock(&server_opts.authkey_lock);
1001
Michal Vasko086311b2016-01-08 09:53:11 +01001002 return username;
1003}
1004
1005static void
1006nc_sshcb_auth_pubkey(struct nc_session *session, ssh_message msg)
1007{
1008 const char *username;
1009 int signature_state;
1010
Michal Vasko733c0bd2018-07-03 13:14:40 +02001011 if (server_opts.pubkey_auth_clb) {
1012 if (server_opts.pubkey_auth_clb(session, ssh_message_auth_pubkey(msg), server_opts.pubkey_auth_data)) {
bhart3bc2f582018-06-05 12:40:32 -05001013 goto fail;
1014 }
Michal Vasko733c0bd2018-07-03 13:14:40 +02001015 } else {
bhart3bc2f582018-06-05 12:40:32 -05001016 if ((username = auth_pubkey_compare_key(ssh_message_auth_pubkey(msg))) == NULL) {
Michal Vasko05532772021-06-03 12:12:38 +02001017 VRB(session, "User \"%s\" tried to use an unknown (unauthorized) public key.", session->username);
bhart3bc2f582018-06-05 12:40:32 -05001018 goto fail;
1019 } else if (strcmp(session->username, username)) {
Michal Vasko05532772021-06-03 12:12:38 +02001020 VRB(session, "User \"%s\" is not the username identified with the presented public key.", session->username);
bhart3bc2f582018-06-05 12:40:32 -05001021 goto fail;
1022 }
Michal Vaskobd13a932016-09-14 09:00:35 +02001023 }
Michal Vaskobd13a932016-09-14 09:00:35 +02001024
Michal Vasko086311b2016-01-08 09:53:11 +01001025 signature_state = ssh_message_auth_publickey_state(msg);
1026 if (signature_state == SSH_PUBLICKEY_STATE_VALID) {
Michal Vasko05532772021-06-03 12:12:38 +02001027 VRB(session, "User \"%s\" authenticated.", session->username);
Michal Vasko086311b2016-01-08 09:53:11 +01001028 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
1029 ssh_message_auth_reply_success(msg, 0);
Michal Vasko086311b2016-01-08 09:53:11 +01001030 } else if (signature_state == SSH_PUBLICKEY_STATE_NONE) {
Michal Vaskobd13a932016-09-14 09:00:35 +02001031 /* accepting only the use of a public key */
1032 ssh_message_auth_reply_pk_ok_simple(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001033 }
1034
Michal Vaskobd13a932016-09-14 09:00:35 +02001035 return;
1036
1037fail:
Michal Vasko2e6defd2016-10-07 15:48:15 +02001038 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +02001039 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
1040 session->opts.server.ssh_auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +01001041 ssh_message_reply_default(msg);
1042}
1043
1044static int
Michal Vasko96164bf2016-01-21 15:41:58 +01001045nc_sshcb_channel_open(struct nc_session *session, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001046{
Michal Vasko96164bf2016-01-21 15:41:58 +01001047 ssh_channel chan;
1048
1049 /* first channel request */
1050 if (!session->ti.libssh.channel) {
1051 if (session->status != NC_STATUS_STARTING) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001052 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001053 return -1;
1054 }
Michal Vasko96164bf2016-01-21 15:41:58 +01001055 chan = ssh_message_channel_request_open_reply_accept(msg);
1056 if (!chan) {
Michal Vasko05532772021-06-03 12:12:38 +02001057 ERR(session, "Failed to create a new SSH channel.");
Michal Vasko96164bf2016-01-21 15:41:58 +01001058 return -1;
1059 }
1060 session->ti.libssh.channel = chan;
Michal Vasko086311b2016-01-08 09:53:11 +01001061
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001062 /* additional channel request */
Michal Vasko96164bf2016-01-21 15:41:58 +01001063 } else {
1064 chan = ssh_message_channel_request_open_reply_accept(msg);
1065 if (!chan) {
Michal Vasko05532772021-06-03 12:12:38 +02001066 ERR(session, "Session %u: failed to create a new SSH channel.", session->id);
Michal Vasko96164bf2016-01-21 15:41:58 +01001067 return -1;
1068 }
1069 /* channel was created and libssh stored it internally in the ssh_session structure, good enough */
Michal Vasko086311b2016-01-08 09:53:11 +01001070 }
1071
Michal Vasko086311b2016-01-08 09:53:11 +01001072 return 0;
1073}
1074
1075static int
1076nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem)
1077{
Michal Vasko96164bf2016-01-21 15:41:58 +01001078 struct nc_session *new_session;
Michal Vasko086311b2016-01-08 09:53:11 +01001079
Michal Vasko96164bf2016-01-21 15:41:58 +01001080 if (strcmp(subsystem, "netconf")) {
Michal Vasko05532772021-06-03 12:12:38 +02001081 WRN(session, "Received an unknown subsystem \"%s\" request.", subsystem);
Michal Vasko086311b2016-01-08 09:53:11 +01001082 return -1;
1083 }
1084
Michal Vasko96164bf2016-01-21 15:41:58 +01001085 if (session->ti.libssh.channel == channel) {
1086 /* first channel requested */
1087 if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) {
1088 ERRINT;
1089 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001090 }
Michal Vasko96164bf2016-01-21 15:41:58 +01001091 if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) {
Michal Vasko05532772021-06-03 12:12:38 +02001092 ERR(session, "Subsystem \"netconf\" requested for the second time.");
Michal Vasko96164bf2016-01-21 15:41:58 +01001093 return -1;
1094 }
1095
1096 session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF;
Michal Vasko086311b2016-01-08 09:53:11 +01001097 } else {
Michal Vasko96164bf2016-01-21 15:41:58 +01001098 /* additional channel subsystem request, new session is ready as far as SSH is concerned */
Michal Vasko131120a2018-05-29 15:44:02 +02001099 new_session = nc_new_session(NC_SERVER, 1);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001100 if (!new_session) {
1101 ERRMEM;
1102 return -1;
1103 }
Michal Vasko96164bf2016-01-21 15:41:58 +01001104
1105 /* insert the new session */
1106 if (!session->ti.libssh.next) {
1107 new_session->ti.libssh.next = session;
1108 } else {
1109 new_session->ti.libssh.next = session->ti.libssh.next;
1110 }
1111 session->ti.libssh.next = new_session;
1112
1113 new_session->status = NC_STATUS_STARTING;
Michal Vasko96164bf2016-01-21 15:41:58 +01001114 new_session->ti_type = NC_TI_LIBSSH;
Michal Vasko131120a2018-05-29 15:44:02 +02001115 new_session->io_lock = session->io_lock;
Michal Vasko96164bf2016-01-21 15:41:58 +01001116 new_session->ti.libssh.channel = channel;
1117 new_session->ti.libssh.session = session->ti.libssh.session;
Michal Vasko77367452021-02-16 16:32:18 +01001118 lydict_insert(server_opts.ctx, session->username, 0, &new_session->username);
1119 lydict_insert(server_opts.ctx, session->host, 0, &new_session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +01001120 new_session->port = session->port;
1121 new_session->ctx = server_opts.ctx;
Michal Vasko83d15322018-09-27 09:44:02 +02001122 new_session->flags = NC_SESSION_SSH_AUTHENTICATED | NC_SESSION_SSH_SUBSYS_NETCONF | NC_SESSION_SHAREDCTX;
Michal Vasko086311b2016-01-08 09:53:11 +01001123 }
1124
1125 return 0;
1126}
1127
Michal Vasko96164bf2016-01-21 15:41:58 +01001128int
Michal Vaskob48aa812016-01-18 14:13:09 +01001129nc_sshcb_msg(ssh_session UNUSED(sshsession), ssh_message msg, void *data)
Michal Vasko086311b2016-01-08 09:53:11 +01001130{
1131 const char *str_type, *str_subtype = NULL, *username;
1132 int subtype, type;
1133 struct nc_session *session = (struct nc_session *)data;
Michal Vasko086311b2016-01-08 09:53:11 +01001134
1135 type = ssh_message_type(msg);
1136 subtype = ssh_message_subtype(msg);
1137
1138 switch (type) {
1139 case SSH_REQUEST_AUTH:
1140 str_type = "request-auth";
1141 switch (subtype) {
1142 case SSH_AUTH_METHOD_NONE:
1143 str_subtype = "none";
1144 break;
1145 case SSH_AUTH_METHOD_PASSWORD:
1146 str_subtype = "password";
1147 break;
1148 case SSH_AUTH_METHOD_PUBLICKEY:
1149 str_subtype = "publickey";
1150 break;
1151 case SSH_AUTH_METHOD_HOSTBASED:
1152 str_subtype = "hostbased";
1153 break;
1154 case SSH_AUTH_METHOD_INTERACTIVE:
1155 str_subtype = "interactive";
1156 break;
1157 case SSH_AUTH_METHOD_GSSAPI_MIC:
1158 str_subtype = "gssapi-mic";
1159 break;
1160 }
1161 break;
1162
1163 case SSH_REQUEST_CHANNEL_OPEN:
1164 str_type = "request-channel-open";
1165 switch (subtype) {
1166 case SSH_CHANNEL_SESSION:
1167 str_subtype = "session";
1168 break;
1169 case SSH_CHANNEL_DIRECT_TCPIP:
1170 str_subtype = "direct-tcpip";
1171 break;
1172 case SSH_CHANNEL_FORWARDED_TCPIP:
1173 str_subtype = "forwarded-tcpip";
1174 break;
1175 case (int)SSH_CHANNEL_X11:
1176 str_subtype = "channel-x11";
1177 break;
1178 case SSH_CHANNEL_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001179 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +01001180 default:
1181 str_subtype = "unknown";
1182 break;
1183 }
1184 break;
1185
1186 case SSH_REQUEST_CHANNEL:
1187 str_type = "request-channel";
1188 switch (subtype) {
1189 case SSH_CHANNEL_REQUEST_PTY:
1190 str_subtype = "pty";
1191 break;
1192 case SSH_CHANNEL_REQUEST_EXEC:
1193 str_subtype = "exec";
1194 break;
1195 case SSH_CHANNEL_REQUEST_SHELL:
1196 str_subtype = "shell";
1197 break;
1198 case SSH_CHANNEL_REQUEST_ENV:
1199 str_subtype = "env";
1200 break;
1201 case SSH_CHANNEL_REQUEST_SUBSYSTEM:
1202 str_subtype = "subsystem";
1203 break;
1204 case SSH_CHANNEL_REQUEST_WINDOW_CHANGE:
1205 str_subtype = "window-change";
1206 break;
1207 case SSH_CHANNEL_REQUEST_X11:
1208 str_subtype = "x11";
1209 break;
1210 case SSH_CHANNEL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001211 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +01001212 default:
1213 str_subtype = "unknown";
1214 break;
1215 }
1216 break;
1217
1218 case SSH_REQUEST_SERVICE:
1219 str_type = "request-service";
1220 str_subtype = ssh_message_service_service(msg);
1221 break;
1222
1223 case SSH_REQUEST_GLOBAL:
1224 str_type = "request-global";
1225 switch (subtype) {
1226 case SSH_GLOBAL_REQUEST_TCPIP_FORWARD:
1227 str_subtype = "tcpip-forward";
1228 break;
1229 case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD:
1230 str_subtype = "cancel-tcpip-forward";
1231 break;
1232 case SSH_GLOBAL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001233 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +01001234 default:
1235 str_subtype = "unknown";
1236 break;
1237 }
1238 break;
1239
1240 default:
1241 str_type = "unknown";
1242 str_subtype = "unknown";
1243 break;
1244 }
1245
Michal Vasko05532772021-06-03 12:12:38 +02001246 VRB(session, "Received an SSH message \"%s\" of subtype \"%s\".", str_type, str_subtype);
Michal Vasko5e0edd82020-07-29 15:26:13 +02001247 if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) {
Michal Vaskoce319162016-02-03 15:33:08 +01001248 /* "valid" situation if, for example, receiving some auth or channel request timeouted,
1249 * but we got it now, during session free */
Michal Vasko05532772021-06-03 12:12:38 +02001250 VRB(session, "SSH message arrived on a %s session, the request will be denied.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001251 (session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid"));
Michal Vaskoce319162016-02-03 15:33:08 +01001252 ssh_message_reply_default(msg);
1253 return 0;
1254 }
Michal Vasko96164bf2016-01-21 15:41:58 +01001255 session->flags |= NC_SESSION_SSH_NEW_MSG;
Michal Vasko086311b2016-01-08 09:53:11 +01001256
1257 /*
1258 * process known messages
1259 */
1260 if (type == SSH_REQUEST_AUTH) {
1261 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko05532772021-06-03 12:12:38 +02001262 ERR(session, "User \"%s\" authenticated, but requested another authentication.", session->username);
Michal Vasko086311b2016-01-08 09:53:11 +01001263 ssh_message_reply_default(msg);
1264 return 0;
1265 }
1266
Michal Vasko086311b2016-01-08 09:53:11 +01001267 /* save the username, do not let the client change it */
1268 username = ssh_message_auth_user(msg);
1269 if (!session->username) {
1270 if (!username) {
Michal Vasko05532772021-06-03 12:12:38 +02001271 ERR(session, "Denying an auth request without a username.");
Michal Vasko086311b2016-01-08 09:53:11 +01001272 return 1;
1273 }
1274
Michal Vasko77367452021-02-16 16:32:18 +01001275 lydict_insert(server_opts.ctx, username, 0, &session->username);
Michal Vasko086311b2016-01-08 09:53:11 +01001276 } else if (username) {
1277 if (strcmp(username, session->username)) {
Michal Vasko05532772021-06-03 12:12:38 +02001278 ERR(session, "User \"%s\" changed its username to \"%s\".", session->username, username);
Michal Vasko086311b2016-01-08 09:53:11 +01001279 session->status = NC_STATUS_INVALID;
Michal Vasko428087d2016-01-14 16:04:28 +01001280 session->term_reason = NC_SESSION_TERM_OTHER;
Michal Vasko086311b2016-01-08 09:53:11 +01001281 return 1;
1282 }
1283 }
1284
1285 if (subtype == SSH_AUTH_METHOD_NONE) {
1286 /* libssh will return the supported auth methods */
1287 return 1;
1288 } else if (subtype == SSH_AUTH_METHOD_PASSWORD) {
1289 nc_sshcb_auth_password(session, msg);
1290 return 0;
1291 } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) {
1292 nc_sshcb_auth_pubkey(session, msg);
1293 return 0;
1294 } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) {
1295 nc_sshcb_auth_kbdint(session, msg);
1296 return 0;
1297 }
1298 } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko0df67562016-01-21 15:50:11 +01001299 if ((type == SSH_REQUEST_CHANNEL_OPEN) && ((enum ssh_channel_type_e)subtype == SSH_CHANNEL_SESSION)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001300 if (nc_sshcb_channel_open(session, msg)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001301 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001302 }
Michal Vasko086311b2016-01-08 09:53:11 +01001303 return 0;
Michal Vasko96164bf2016-01-21 15:41:58 +01001304
Michal Vasko0df67562016-01-21 15:50:11 +01001305 } else if ((type == SSH_REQUEST_CHANNEL) && ((enum ssh_channel_requests_e)subtype == SSH_CHANNEL_REQUEST_SUBSYSTEM)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001306 if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg),
1307 ssh_message_channel_request_subsystem(msg))) {
Michal Vasko086311b2016-01-08 09:53:11 +01001308 ssh_message_reply_default(msg);
Michal Vasko96164bf2016-01-21 15:41:58 +01001309 } else {
1310 ssh_message_channel_request_reply_success(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001311 }
1312 return 0;
1313 }
1314 }
1315
1316 /* we did not process it */
1317 return 1;
1318}
1319
Michal Vasko1a38c862016-01-15 15:50:07 +01001320/* ret 1 on success, 0 on timeout, -1 on error */
Michal Vasko086311b2016-01-08 09:53:11 +01001321static int
1322nc_open_netconf_channel(struct nc_session *session, int timeout)
1323{
Michal Vasko36c7be82017-02-22 13:37:59 +01001324 int ret;
1325 struct timespec ts_timeout, ts_cur;
Michal Vasko086311b2016-01-08 09:53:11 +01001326
1327 /* message callback is executed twice to give chance for the channel to be
1328 * created if timeout == 0 (it takes 2 messages, channel-open, subsystem-request) */
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001329 if (!timeout) {
Michal Vasko2a7d4732016-01-15 09:24:46 +01001330 if (!nc_session_is_connected(session)) {
Michal Vasko05532772021-06-03 12:12:38 +02001331 ERR(session, "Communication socket unexpectedly closed (libssh).");
Michal Vasko2a7d4732016-01-15 09:24:46 +01001332 return -1;
1333 }
1334
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001335 ret = ssh_execute_message_callbacks(session->ti.libssh.session);
1336 if (ret != SSH_OK) {
Michal Vasko05532772021-06-03 12:12:38 +02001337 ERR(session, "Failed to receive SSH messages on a session (%s).",
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001338 ssh_get_error(session->ti.libssh.session));
Michal Vasko086311b2016-01-08 09:53:11 +01001339 return -1;
1340 }
1341
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001342 if (!session->ti.libssh.channel) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001343 return 0;
1344 }
1345
1346 ret = ssh_execute_message_callbacks(session->ti.libssh.session);
1347 if (ret != SSH_OK) {
Michal Vasko05532772021-06-03 12:12:38 +02001348 ERR(session, "Failed to receive SSH messages on a session (%s).",
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001349 ssh_get_error(session->ti.libssh.session));
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001350 return -1;
1351 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001352
1353 if (!(session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
1354 /* we did not receive subsystem-request, timeout */
1355 return 0;
1356 }
1357
1358 return 1;
1359 }
1360
Michal Vasko36c7be82017-02-22 13:37:59 +01001361 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001362 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001363 nc_addtimespec(&ts_timeout, timeout);
1364 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001365 while (1) {
1366 if (!nc_session_is_connected(session)) {
Michal Vasko05532772021-06-03 12:12:38 +02001367 ERR(session, "Communication socket unexpectedly closed (libssh).");
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001368 return -1;
1369 }
1370
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001371 ret = ssh_execute_message_callbacks(session->ti.libssh.session);
1372 if (ret != SSH_OK) {
Michal Vasko05532772021-06-03 12:12:38 +02001373 ERR(session, "Failed to receive SSH messages on a session (%s).",
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001374 ssh_get_error(session->ti.libssh.session));
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001375 return -1;
1376 }
1377
Michal Vasko086311b2016-01-08 09:53:11 +01001378 if (session->ti.libssh.channel && (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko1a38c862016-01-15 15:50:07 +01001379 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001380 }
1381
Michal Vasko086311b2016-01-08 09:53:11 +01001382 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001383 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001384 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001385 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1386 /* timeout */
Michal Vasko05532772021-06-03 12:12:38 +02001387 ERR(session, "Failed to start \"netconf\" SSH subsystem for too long, disconnecting.");
Michal Vasko36c7be82017-02-22 13:37:59 +01001388 break;
1389 }
1390 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001391 }
Michal Vasko086311b2016-01-08 09:53:11 +01001392
Michal Vasko1a38c862016-01-15 15:50:07 +01001393 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001394}
1395
Michal Vasko4c1fb492017-01-30 14:31:07 +01001396static int
1397nc_ssh_bind_add_hostkeys(ssh_bind sbind, const char **hostkeys, uint8_t hostkey_count)
1398{
1399 uint8_t i;
1400 char *privkey_path, *privkey_data;
Michal Vaskoddce1212019-05-24 09:58:49 +02001401 int ret;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001402 NC_SSH_KEY_TYPE privkey_type;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001403
1404 if (!server_opts.hostkey_clb) {
Michal Vasko05532772021-06-03 12:12:38 +02001405 ERR(NULL, "Callback for retrieving SSH host keys not set.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001406 return -1;
1407 }
1408
1409 for (i = 0; i < hostkey_count; ++i) {
1410 privkey_path = privkey_data = NULL;
Michal Vaskoddce1212019-05-24 09:58:49 +02001411 if (server_opts.hostkey_clb(hostkeys[i], server_opts.hostkey_data, &privkey_path, &privkey_data, &privkey_type)) {
Michal Vasko05532772021-06-03 12:12:38 +02001412 ERR(NULL, "Host key callback failed.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001413 return -1;
1414 }
1415
1416 if (privkey_data) {
Michal Vaskoddce1212019-05-24 09:58:49 +02001417 privkey_path = base64der_key_to_tmp_file(privkey_data, nc_keytype2str(privkey_type));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001418 if (!privkey_path) {
Michal Vasko05532772021-06-03 12:12:38 +02001419 ERR(NULL, "Temporarily storing a host key into a file failed (%s).", strerror(errno));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001420 free(privkey_data);
1421 return -1;
1422 }
1423 }
1424
1425 ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path);
1426
1427 /* cleanup */
1428 if (privkey_data && unlink(privkey_path)) {
Michal Vasko05532772021-06-03 12:12:38 +02001429 WRN(NULL, "Removing a temporary host key file \"%s\" failed (%s).", privkey_path, strerror(errno));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001430 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001431 free(privkey_data);
1432
1433 if (ret != SSH_OK) {
Michal Vasko05532772021-06-03 12:12:38 +02001434 ERR(NULL, "Failed to set hostkey \"%s\" (%s).", hostkeys[i], privkey_path);
Michal Vasko80075de2017-07-10 11:38:52 +02001435 }
1436 free(privkey_path);
1437
1438 if (ret != SSH_OK) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001439 return -1;
1440 }
1441 }
1442
1443 return 0;
1444}
1445
Michal Vasko96164bf2016-01-21 15:41:58 +01001446int
Michal Vasko0190bc32016-03-02 15:47:49 +01001447nc_accept_ssh_session(struct nc_session *session, int sock, int timeout)
Michal Vasko3031aae2016-01-27 16:07:18 +01001448{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001449 ssh_bind sbind;
Michal Vasko3031aae2016-01-27 16:07:18 +01001450 struct nc_server_ssh_opts *opts;
Michal Vasko36c7be82017-02-22 13:37:59 +01001451 int libssh_auth_methods = 0, ret;
1452 struct timespec ts_timeout, ts_cur;
Michal Vasko086311b2016-01-08 09:53:11 +01001453
Michal Vasko2cc4c682016-03-01 09:16:48 +01001454 opts = session->data;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001455
Michal Vasko086311b2016-01-08 09:53:11 +01001456 /* other transport-specific data */
1457 session->ti_type = NC_TI_LIBSSH;
1458 session->ti.libssh.session = ssh_new();
1459 if (!session->ti.libssh.session) {
Michal Vasko05532772021-06-03 12:12:38 +02001460 ERR(NULL, "Failed to initialize a new SSH session.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001461 close(sock);
Michal Vasko9e036d52016-01-08 10:49:26 +01001462 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001463 }
1464
Michal Vaskoc61c4492016-01-25 11:13:34 +01001465 if (opts->auth_methods & NC_SSH_AUTH_PUBLICKEY) {
Michal Vasko086311b2016-01-08 09:53:11 +01001466 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
1467 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001468 if (opts->auth_methods & NC_SSH_AUTH_PASSWORD) {
Michal Vasko086311b2016-01-08 09:53:11 +01001469 libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD;
1470 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001471 if (opts->auth_methods & NC_SSH_AUTH_INTERACTIVE) {
Michal Vasko086311b2016-01-08 09:53:11 +01001472 libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE;
1473 }
1474 ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods);
1475
Michal Vaskoe2713da2016-08-22 16:06:40 +02001476 sbind = ssh_bind_new();
1477 if (!sbind) {
Michal Vasko05532772021-06-03 12:12:38 +02001478 ERR(session, "Failed to create an SSH bind.");
Michal Vaskoe2713da2016-08-22 16:06:40 +02001479 close(sock);
1480 return -1;
1481 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001482
1483 if (nc_ssh_bind_add_hostkeys(sbind, opts->hostkeys, opts->hostkey_count)) {
1484 close(sock);
1485 ssh_bind_free(sbind);
1486 return -1;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001487 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001488
Michal Vasko086311b2016-01-08 09:53:11 +01001489 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, session);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001490 /* remember that this session was just set as nc_sshcb_msg() parameter */
Michal Vasko96164bf2016-01-21 15:41:58 +01001491 session->flags |= NC_SESSION_SSH_MSG_CB;
Michal Vasko086311b2016-01-08 09:53:11 +01001492
Michal Vaskoe2713da2016-08-22 16:06:40 +02001493 if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) {
Michal Vasko05532772021-06-03 12:12:38 +02001494 ERR(session, "SSH failed to accept a new connection (%s).", ssh_get_error(sbind));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001495 close(sock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001496 ssh_bind_free(sbind);
Michal Vasko9e036d52016-01-08 10:49:26 +01001497 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001498 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001499 ssh_bind_free(sbind);
Michal Vasko086311b2016-01-08 09:53:11 +01001500
Michal Vasko0190bc32016-03-02 15:47:49 +01001501 ssh_set_blocking(session->ti.libssh.session, 0);
1502
Michal Vasko36c7be82017-02-22 13:37:59 +01001503 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001504 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001505 nc_addtimespec(&ts_timeout, timeout);
1506 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001507 while ((ret = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) {
Michal Vaskoe65b4492016-03-03 11:57:51 +01001508 /* this tends to take longer */
1509 usleep(NC_TIMEOUT_STEP * 20);
Michal Vasko36c7be82017-02-22 13:37:59 +01001510 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001511 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001512 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1513 break;
1514 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001515 }
1516 }
1517 if (ret == SSH_AGAIN) {
Michal Vasko05532772021-06-03 12:12:38 +02001518 ERR(session, "SSH key exchange timeout.");
Michal Vasko0190bc32016-03-02 15:47:49 +01001519 return 0;
1520 } else if (ret != SSH_OK) {
Michal Vasko05532772021-06-03 12:12:38 +02001521 ERR(session, "SSH key exchange error (%s).", ssh_get_error(session->ti.libssh.session));
Michal Vasko9e036d52016-01-08 10:49:26 +01001522 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001523 }
1524
1525 /* authenticate */
Michal Vasko36c7be82017-02-22 13:37:59 +01001526 if (opts->auth_timeout) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001527 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001528 nc_addtimespec(&ts_timeout, opts->auth_timeout * 1000);
1529 }
1530 while (1) {
Michal Vasko2a7d4732016-01-15 09:24:46 +01001531 if (!nc_session_is_connected(session)) {
Michal Vasko05532772021-06-03 12:12:38 +02001532 ERR(session, "Communication SSH socket unexpectedly closed.");
Michal Vasko2a7d4732016-01-15 09:24:46 +01001533 return -1;
1534 }
1535
Michal Vasko086311b2016-01-08 09:53:11 +01001536 if (ssh_execute_message_callbacks(session->ti.libssh.session) != SSH_OK) {
Michal Vasko05532772021-06-03 12:12:38 +02001537 ERR(session, "Failed to receive SSH messages on a session (%s).",
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001538 ssh_get_error(session->ti.libssh.session));
Michal Vasko9e036d52016-01-08 10:49:26 +01001539 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001540 }
1541
Michal Vasko36c7be82017-02-22 13:37:59 +01001542 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
1543 break;
1544 }
1545
Michal Vasko145ae672017-02-07 10:57:27 +01001546 if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) {
Michal Vasko05532772021-06-03 12:12:38 +02001547 ERR(session, "Too many failed authentication attempts of user \"%s\".", session->username);
Michal Vasko145ae672017-02-07 10:57:27 +01001548 return -1;
1549 }
1550
Michal Vasko086311b2016-01-08 09:53:11 +01001551 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001552 if (opts->auth_timeout) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001553 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001554 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1555 /* timeout */
1556 break;
1557 }
1558 }
1559 }
Michal Vasko086311b2016-01-08 09:53:11 +01001560
1561 if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) {
1562 /* timeout */
Michal Vaskoc13da702017-02-07 10:57:57 +01001563 if (session->username) {
Michal Vasko05532772021-06-03 12:12:38 +02001564 ERR(session, "User \"%s\" failed to authenticate for too long, disconnecting.", session->username);
Michal Vaskoc13da702017-02-07 10:57:57 +01001565 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001566 ERR(session, "User failed to authenticate for too long, disconnecting.");
Michal Vaskoc13da702017-02-07 10:57:57 +01001567 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001568 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001569 }
1570
Michal Vasko086311b2016-01-08 09:53:11 +01001571 /* open channel */
Michal Vasko36c7be82017-02-22 13:37:59 +01001572 ret = nc_open_netconf_channel(session, timeout);
Michal Vasko1a38c862016-01-15 15:50:07 +01001573 if (ret < 1) {
1574 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001575 }
1576
Michal Vasko96164bf2016-01-21 15:41:58 +01001577 session->flags &= ~NC_SESSION_SSH_NEW_MSG;
Michal Vasko1a38c862016-01-15 15:50:07 +01001578 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001579}
1580
Michal Vasko71090fc2016-05-24 16:37:28 +02001581API NC_MSG_TYPE
1582nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session)
1583{
1584 NC_MSG_TYPE msgtype;
1585 struct nc_session *new_session = NULL;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001586 struct timespec ts_cur;
Michal Vasko71090fc2016-05-24 16:37:28 +02001587
1588 if (!orig_session) {
1589 ERRARG("orig_session");
1590 return NC_MSG_ERROR;
1591 } else if (!session) {
1592 ERRARG("session");
1593 return NC_MSG_ERROR;
1594 }
1595
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001596 if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) &&
1597 orig_session->ti.libssh.next) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001598 for (new_session = orig_session->ti.libssh.next;
1599 new_session != orig_session;
1600 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001601 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1602 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001603 /* we found our session */
1604 break;
1605 }
1606 }
1607 if (new_session == orig_session) {
1608 new_session = NULL;
1609 }
1610 }
1611
1612 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001613 ERR(orig_session, "Session does not have a NETCONF SSH channel ready.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001614 return NC_MSG_ERROR;
1615 }
1616
1617 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001618 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001619
1620 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001621 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001622 if (msgtype != NC_MSG_HELLO) {
1623 return msgtype;
1624 }
1625
Michal Vasko9f6275e2017-10-05 13:50:05 +02001626 nc_gettimespec_real(&ts_cur);
1627 new_session->opts.server.session_start = ts_cur.tv_sec;
1628 nc_gettimespec_mono(&ts_cur);
1629 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko71090fc2016-05-24 16:37:28 +02001630 new_session->status = NC_STATUS_RUNNING;
1631 *session = new_session;
1632
1633 return msgtype;
1634}
1635
1636API NC_MSG_TYPE
Michal Vasko96164bf2016-01-21 15:41:58 +01001637nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +01001638{
Michal Vaskobdcf2362016-07-26 11:35:43 +02001639 uint8_t q_id;
Michal Vasko71090fc2016-05-24 16:37:28 +02001640 NC_MSG_TYPE msgtype;
Michal Vaskoe4300a82017-05-24 10:35:42 +02001641 struct nc_session *new_session = NULL, *cur_session;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001642 struct timespec ts_cur;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001643 uint16_t i;
Michal Vasko086311b2016-01-08 09:53:11 +01001644
Michal Vasko45e53ae2016-04-07 11:46:03 +02001645 if (!ps) {
1646 ERRARG("ps");
Michal Vasko71090fc2016-05-24 16:37:28 +02001647 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001648 } else if (!session) {
1649 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001650 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001651 }
1652
Michal Vasko48a63ed2016-03-01 09:48:21 +01001653 /* LOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001654 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001655 return NC_MSG_ERROR;
Michal Vaskof04a52a2016-04-07 10:52:10 +02001656 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001657
Michal Vasko96164bf2016-01-21 15:41:58 +01001658 for (i = 0; i < ps->session_count; ++i) {
fanchanghu3d4e7212017-08-09 09:42:30 +08001659 cur_session = ps->sessions[i]->session;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001660 if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH) &&
1661 cur_session->ti.libssh.next) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001662 /* an SSH session with more channels */
Michal Vaskoe4300a82017-05-24 10:35:42 +02001663 for (new_session = cur_session->ti.libssh.next;
1664 new_session != cur_session;
Michal Vasko96164bf2016-01-21 15:41:58 +01001665 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001666 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1667 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001668 /* we found our session */
1669 break;
1670 }
1671 }
Michal Vaskoe4300a82017-05-24 10:35:42 +02001672 if (new_session != cur_session) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001673 break;
1674 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001675
Michal Vasko96164bf2016-01-21 15:41:58 +01001676 new_session = NULL;
1677 }
1678 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001679
Michal Vasko48a63ed2016-03-01 09:48:21 +01001680 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001681 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001682
Michal Vasko96164bf2016-01-21 15:41:58 +01001683 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001684 ERR(NULL, "No session with a NETCONF SSH channel ready was found.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001685 return NC_MSG_ERROR;
Michal Vasko96164bf2016-01-21 15:41:58 +01001686 }
1687
1688 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001689 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vaskofb89d772016-01-08 12:25:35 +01001690
Michal Vasko086311b2016-01-08 09:53:11 +01001691 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001692 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001693 if (msgtype != NC_MSG_HELLO) {
1694 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001695 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001696
Michal Vasko9f6275e2017-10-05 13:50:05 +02001697 nc_gettimespec_real(&ts_cur);
1698 new_session->opts.server.session_start = ts_cur.tv_sec;
1699 nc_gettimespec_mono(&ts_cur);
1700 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko086311b2016-01-08 09:53:11 +01001701 new_session->status = NC_STATUS_RUNNING;
Michal Vasko96164bf2016-01-21 15:41:58 +01001702 *session = new_session;
Michal Vasko086311b2016-01-08 09:53:11 +01001703
Michal Vasko71090fc2016-05-24 16:37:28 +02001704 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001705}