blob: 0809d58a5a76d9bb812e639a815ec66975bea74c [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 Vasko086311b2016-01-08 09:53:11 +010026#include <stdlib.h>
27#include <string.h>
28#include <sys/types.h>
Michal Vasko27252692017-03-21 15:34:13 +010029#include <sys/stat.h>
Michal Vasko086311b2016-01-08 09:53:11 +010030#include <pwd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010031#include <errno.h>
Michal Vasko9f6275e2017-10-05 13:50:05 +020032#include <time.h>
Claus Klein22091912020-01-20 13:45:47 +010033#include <fcntl.h>
34#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
Mislav Novakovicce9a7ef2017-08-08 13:45:52 +020041#if !defined(HAVE_CRYPT_R)
42pthread_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 Vasko7d255882017-02-09 13:35:08 +0100151 ret = nc_server_ssh_add_hostkey(name, idx, endpt->opts.ssh);
Michal Vasko51e514d2016-02-02 15:51:52 +0100152 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100153 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100154
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100155 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100156}
157
Michal Vasko974410a2018-04-03 09:36:57 +0200158API void
159nc_server_ssh_set_passwd_auth_clb(int (*passwd_auth_clb)(const struct nc_session *session, const char *password, void *user_data),
160 void *user_data, void (*free_user_data)(void *user_data))
161{
162 server_opts.passwd_auth_clb = passwd_auth_clb;
163 server_opts.passwd_auth_data = user_data;
164 server_opts.passwd_auth_data_free = free_user_data;
165}
166
bhart1bb7cdb2018-07-02 15:03:30 -0500167API void
168nc_server_ssh_set_interactive_auth_clb(int (*interactive_auth_clb)(const struct nc_session *session, ssh_message msg, void *user_data),
169 void *user_data, void (*free_user_data)(void *user_data))
170{
171 server_opts.interactive_auth_clb = interactive_auth_clb;
172 server_opts.interactive_auth_data = user_data;
173 server_opts.interactive_auth_data_free = free_user_data;
174}
Michal Vasko733c0bd2018-07-03 13:14:40 +0200175
bhart1bb7cdb2018-07-02 15:03:30 -0500176API void
177nc_server_ssh_set_pubkey_auth_clb(int (*pubkey_auth_clb)(const struct nc_session *session, ssh_key key, void *user_data),
178 void *user_data, void (*free_user_data)(void *user_data))
179{
180 server_opts.pubkey_auth_clb = pubkey_auth_clb;
181 server_opts.pubkey_auth_data = user_data;
182 server_opts.pubkey_auth_data_free = free_user_data;
183}
184
Michal Vaskob05053d2016-01-22 16:12:06 +0100185API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200186nc_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 +0100187{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100188 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200189 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200190 struct nc_ch_endpt *endpt;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100191
Michal Vasko2e6defd2016-10-07 15:48:15 +0200192 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200193 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
194 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200195 return -1;
196 }
Michal Vaskoadf30f02019-06-24 09:34:47 +0200197 ret = nc_server_ssh_add_hostkey(name, idx, endpt->opts.ssh);
Michal Vasko2e6defd2016-10-07 15:48:15 +0200198 /* UNLOCK */
199 nc_server_ch_client_unlock(client);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200200
201 return ret;
202}
203
Michal Vasko4c1fb492017-01-30 14:31:07 +0100204API void
205nc_server_ssh_set_hostkey_clb(int (*hostkey_clb)(const char *name, void *user_data, char **privkey_path,
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200206 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 +0100207{
208 if (!hostkey_clb) {
209 ERRARG("hostkey_clb");
210 return;
211 }
212
213 server_opts.hostkey_clb = hostkey_clb;
214 server_opts.hostkey_data = user_data;
215 server_opts.hostkey_data_free = free_user_data;
216}
217
Michal Vaskoe2713da2016-08-22 16:06:40 +0200218static int
Michal Vasko7d255882017-02-09 13:35:08 +0100219nc_server_ssh_del_hostkey(const char *name, int16_t idx, struct nc_server_ssh_opts *opts)
Michal Vaskoe2713da2016-08-22 16:06:40 +0200220{
221 uint8_t i;
222
Michal Vasko7d255882017-02-09 13:35:08 +0100223 if (name && (idx > -1)) {
224 ERRARG("name and idx");
225 return -1;
226 } else if (idx >= opts->hostkey_count) {
227 ERRARG("idx");
228 }
229
230 if (!name && (idx < 0)) {
Michal Vaskoe2713da2016-08-22 16:06:40 +0200231 for (i = 0; i < opts->hostkey_count; ++i) {
232 lydict_remove(server_opts.ctx, opts->hostkeys[i]);
233 }
234 free(opts->hostkeys);
235 opts->hostkeys = NULL;
236 opts->hostkey_count = 0;
Michal Vasko7d255882017-02-09 13:35:08 +0100237 } else if (name) {
Michal Vaskoe2713da2016-08-22 16:06:40 +0200238 for (i = 0; i < opts->hostkey_count; ++i) {
Michal Vasko4c1fb492017-01-30 14:31:07 +0100239 if (!strcmp(opts->hostkeys[i], name)) {
Michal Vasko7d255882017-02-09 13:35:08 +0100240 idx = i;
241 goto remove_idx;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200242 }
243 }
244
Michal Vasko7d255882017-02-09 13:35:08 +0100245 ERRARG("name");
Michal Vaskoe2713da2016-08-22 16:06:40 +0200246 return -1;
Michal Vasko7d255882017-02-09 13:35:08 +0100247 } else {
248remove_idx:
249 --opts->hostkey_count;
250 lydict_remove(server_opts.ctx, opts->hostkeys[idx]);
251 if (idx < opts->hostkey_count - 1) {
252 memmove(opts->hostkeys + idx, opts->hostkeys + idx + 1, (opts->hostkey_count - idx) * sizeof *opts->hostkeys);
253 }
254 if (!opts->hostkey_count) {
255 free(opts->hostkeys);
256 opts->hostkeys = NULL;
257 }
Michal Vaskoe2713da2016-08-22 16:06:40 +0200258 }
259
260 return 0;
261}
262
263API int
Michal Vasko7d255882017-02-09 13:35:08 +0100264nc_server_ssh_endpt_del_hostkey(const char *endpt_name, const char *name, int16_t idx)
Michal Vaskoe2713da2016-08-22 16:06:40 +0200265{
266 int ret;
267 struct nc_endpt *endpt;
268
269 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100270 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200271 if (!endpt) {
272 return -1;
273 }
Michal Vasko7d255882017-02-09 13:35:08 +0100274 ret = nc_server_ssh_del_hostkey(name, idx, endpt->opts.ssh);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200275 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100276 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200277
278 return ret;
279}
280
281API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200282nc_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 +0200283{
284 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200285 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200286 struct nc_ch_endpt *endpt;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200287
Michal Vasko2e6defd2016-10-07 15:48:15 +0200288 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200289 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
290 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200291 return -1;
292 }
Michal Vaskoadf30f02019-06-24 09:34:47 +0200293 ret = nc_server_ssh_del_hostkey(name, idx, endpt->opts.ssh);
Michal Vasko2e6defd2016-10-07 15:48:15 +0200294 /* UNLOCK */
295 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100296
297 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100298}
299
300static int
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100301nc_server_ssh_mov_hostkey(const char *key_mov, const char *key_after, struct nc_server_ssh_opts *opts)
302{
303 uint8_t i;
304 int16_t mov_idx = -1, after_idx = -1;
305 const char *bckup;
306
307 if (!key_mov) {
308 ERRARG("key_mov");
309 return -1;
310 }
311
312 for (i = 0; i < opts->hostkey_count; ++i) {
313 if (key_after && (after_idx == -1) && !strcmp(opts->hostkeys[i], key_after)) {
314 after_idx = i;
315 }
316 if ((mov_idx == -1) && !strcmp(opts->hostkeys[i], key_mov)) {
317 mov_idx = i;
318 }
319
320 if ((!key_after || (after_idx > -1)) && (mov_idx > -1)) {
321 break;
322 }
323 }
324
325 if (key_after && (after_idx == -1)) {
326 ERRARG("key_after");
327 return -1;
328 }
329 if (mov_idx == -1) {
330 ERRARG("key_mov");
331 return -1;
332 }
333 if ((mov_idx == after_idx) || (mov_idx == after_idx + 1)) {
334 /* nothing to do */
335 return 0;
336 }
337
338 /* finally move the key */
339 bckup = opts->hostkeys[mov_idx];
340 if (mov_idx > after_idx) {
341 memmove(opts->hostkeys + after_idx + 2, opts->hostkeys + after_idx + 1,
342 ((mov_idx - after_idx) - 1) * sizeof *opts->hostkeys);
343 opts->hostkeys[after_idx + 1] = bckup;
344 } else {
345 memmove(opts->hostkeys + mov_idx, opts->hostkeys + mov_idx + 1, (after_idx - mov_idx) * sizeof *opts->hostkeys);
346 opts->hostkeys[after_idx] = bckup;
347 }
348
349 return 0;
350}
351
352API int
353nc_server_ssh_endpt_mov_hostkey(const char *endpt_name, const char *key_mov, const char *key_after)
354{
355 int ret;
356 struct nc_endpt *endpt;
357
358 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100359 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100360 if (!endpt) {
361 return -1;
362 }
363 ret = nc_server_ssh_mov_hostkey(key_mov, key_after, endpt->opts.ssh);
364 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100365 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100366
367 return ret;
368}
369
370API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200371nc_server_ssh_ch_client_endpt_mov_hostkey(const char *client_name, const char *endpt_name, const char *key_mov,
372 const char *key_after)
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100373{
374 int ret;
375 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200376 struct nc_ch_endpt *endpt;
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100377
378 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200379 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100380 if (!endpt) {
381 return -1;
382 }
Michal Vaskoadf30f02019-06-24 09:34:47 +0200383 ret = nc_server_ssh_mov_hostkey(key_mov, key_after, endpt->opts.ssh);
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100384 /* UNLOCK */
385 nc_server_ch_client_unlock(client);
386
387 return ret;
388}
389
390static int
Michal Vasko3031aae2016-01-27 16:07:18 +0100391nc_server_ssh_set_auth_methods(int auth_methods, struct nc_server_ssh_opts *opts)
Michal Vaskob05053d2016-01-22 16:12:06 +0100392{
Michal Vaskob05053d2016-01-22 16:12:06 +0100393 opts->auth_methods = auth_methods;
394 return 0;
395}
396
397API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100398nc_server_ssh_endpt_set_auth_methods(const char *endpt_name, int auth_methods)
Michal Vaskob05053d2016-01-22 16:12:06 +0100399{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100400 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100401 struct nc_endpt *endpt;
402
Michal Vasko51e514d2016-02-02 15:51:52 +0100403 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100404 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100405 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100406 return -1;
407 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200408 ret = nc_server_ssh_set_auth_methods(auth_methods, endpt->opts.ssh);
Michal Vasko51e514d2016-02-02 15:51:52 +0100409 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100410 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100411
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100412 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100413}
414
415API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200416nc_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 +0100417{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100418 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200419 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200420 struct nc_ch_endpt *endpt;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100421
Michal Vasko2e6defd2016-10-07 15:48:15 +0200422 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200423 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
424 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200425 return -1;
426 }
Michal Vaskoadf30f02019-06-24 09:34:47 +0200427 ret = nc_server_ssh_set_auth_methods(auth_methods, endpt->opts.ssh);
Michal Vasko2e6defd2016-10-07 15:48:15 +0200428 /* UNLOCK */
429 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100430
431 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100432}
433
Michal Vaskoddce1212019-05-24 09:58:49 +0200434API int
435nc_server_ssh_endpt_get_auth_methods(const char *endpt_name)
436{
437 int ret;
438 struct nc_endpt *endpt;
439
440 /* LOCK */
441 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
442 if (!endpt) {
443 return -1;
444 }
445 ret = endpt->opts.ssh->auth_methods;
446 /* UNLOCK */
447 pthread_rwlock_unlock(&server_opts.endpt_lock);
448
449 return ret;
450}
451
452API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200453nc_server_ssh_ch_client_endpt_get_auth_methods(const char *client_name, const char *endpt_name)
Michal Vaskoddce1212019-05-24 09:58:49 +0200454{
455 int ret;
456 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200457 struct nc_ch_endpt *endpt;
Michal Vaskoddce1212019-05-24 09:58:49 +0200458
459 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200460 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
461 if (!endpt) {
Michal Vaskoddce1212019-05-24 09:58:49 +0200462 return -1;
463 }
Michal Vaskoadf30f02019-06-24 09:34:47 +0200464 ret = endpt->opts.ssh->auth_methods;
Michal Vaskoddce1212019-05-24 09:58:49 +0200465 /* UNLOCK */
466 nc_server_ch_client_unlock(client);
467
468 return ret;
469}
470
Michal Vaskob05053d2016-01-22 16:12:06 +0100471static int
Michal Vasko3031aae2016-01-27 16:07:18 +0100472nc_server_ssh_set_auth_attempts(uint16_t auth_attempts, struct nc_server_ssh_opts *opts)
Michal Vaskob05053d2016-01-22 16:12:06 +0100473{
Michal Vaskob05053d2016-01-22 16:12:06 +0100474 if (!auth_attempts) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200475 ERRARG("auth_attempts");
Michal Vaskob05053d2016-01-22 16:12:06 +0100476 return -1;
477 }
478
Michal Vaskob05053d2016-01-22 16:12:06 +0100479 opts->auth_attempts = auth_attempts;
Michal Vasko086311b2016-01-08 09:53:11 +0100480 return 0;
481}
482
483API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100484nc_server_ssh_endpt_set_auth_attempts(const char *endpt_name, uint16_t auth_attempts)
Michal Vasko086311b2016-01-08 09:53:11 +0100485{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100486 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100487 struct nc_endpt *endpt;
488
Michal Vasko51e514d2016-02-02 15:51:52 +0100489 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100490 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100491 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100492 return -1;
493 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200494 ret = nc_server_ssh_set_auth_attempts(auth_attempts, endpt->opts.ssh);
Michal Vasko51e514d2016-02-02 15:51:52 +0100495 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100496 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100497
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100498 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100499}
500
501API int
Michal Vaskocbad4c52019-06-27 16:30:35 +0200502nc_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 +0100503{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100504 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200505 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200506 struct nc_ch_endpt *endpt;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100507
Michal Vasko2e6defd2016-10-07 15:48:15 +0200508 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200509 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
510 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200511 return -1;
512 }
Michal Vaskoadf30f02019-06-24 09:34:47 +0200513 ret = nc_server_ssh_set_auth_attempts(auth_attempts, endpt->opts.ssh);
Michal Vasko2e6defd2016-10-07 15:48:15 +0200514 /* UNLOCK */
515 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100516
517 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100518}
519
520static int
Michal Vasko3031aae2016-01-27 16:07:18 +0100521nc_server_ssh_set_auth_timeout(uint16_t auth_timeout, struct nc_server_ssh_opts *opts)
Michal Vaskob05053d2016-01-22 16:12:06 +0100522{
Michal Vaskob05053d2016-01-22 16:12:06 +0100523 if (!auth_timeout) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200524 ERRARG("auth_timeout");
Michal Vasko086311b2016-01-08 09:53:11 +0100525 return -1;
526 }
527
Michal Vaskob05053d2016-01-22 16:12:06 +0100528 opts->auth_timeout = auth_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100529 return 0;
530}
531
532API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100533nc_server_ssh_endpt_set_auth_timeout(const char *endpt_name, uint16_t auth_timeout)
Michal Vasko086311b2016-01-08 09:53:11 +0100534{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100535 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100536 struct nc_endpt *endpt;
537
Michal Vasko51e514d2016-02-02 15:51:52 +0100538 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100539 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100540 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100541 return -1;
542 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200543 ret = nc_server_ssh_set_auth_timeout(auth_timeout, endpt->opts.ssh);
Michal Vasko51e514d2016-02-02 15:51:52 +0100544 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100545 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100546
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100547 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100548}
549
550API int
Michal Vaskocbad4c52019-06-27 16:30:35 +0200551nc_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 +0100552{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100553 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200554 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200555 struct nc_ch_endpt *endpt;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100556
Michal Vasko2e6defd2016-10-07 15:48:15 +0200557 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200558 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
559 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200560 return -1;
561 }
Michal Vaskoadf30f02019-06-24 09:34:47 +0200562 ret = nc_server_ssh_set_auth_timeout(auth_timeout, endpt->opts.ssh);
Michal Vasko2e6defd2016-10-07 15:48:15 +0200563 /* UNLOCK */
564 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100565
566 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100567}
568
569static int
Michal Vasko77367452021-02-16 16:32:18 +0100570_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 +0100571{
Michal Vaskoa05c7b12017-01-30 14:33:08 +0100572 /* LOCK */
573 pthread_mutex_lock(&server_opts.authkey_lock);
574
Michal Vasko17dfda92016-12-01 14:06:16 +0100575 ++server_opts.authkey_count;
576 server_opts.authkeys = nc_realloc(server_opts.authkeys, server_opts.authkey_count * sizeof *server_opts.authkeys);
577 if (!server_opts.authkeys) {
578 ERRMEM;
579 return -1;
580 }
Michal Vasko77367452021-02-16 16:32:18 +0100581 lydict_insert(server_opts.ctx, pubkey_path, 0, &server_opts.authkeys[server_opts.authkey_count - 1].path);
582 lydict_insert(server_opts.ctx, pubkey_base64, 0, &server_opts.authkeys[server_opts.authkey_count - 1].base64);
Michal Vasko17dfda92016-12-01 14:06:16 +0100583 server_opts.authkeys[server_opts.authkey_count - 1].type = type;
Michal Vasko77367452021-02-16 16:32:18 +0100584 lydict_insert(server_opts.ctx, username, 0, &server_opts.authkeys[server_opts.authkey_count - 1].username);
Michal Vasko17dfda92016-12-01 14:06:16 +0100585
Michal Vaskoa05c7b12017-01-30 14:33:08 +0100586 /* UNLOCK */
587 pthread_mutex_unlock(&server_opts.authkey_lock);
588
Michal Vasko17dfda92016-12-01 14:06:16 +0100589 return 0;
590}
591
592API int
593nc_server_ssh_add_authkey_path(const char *pubkey_path, const char *username)
Michal Vaskob05053d2016-01-22 16:12:06 +0100594{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200595 if (!pubkey_path) {
596 ERRARG("pubkey_path");
597 return -1;
598 } else if (!username) {
599 ERRARG("username");
Michal Vasko086311b2016-01-08 09:53:11 +0100600 return -1;
601 }
602
Michal Vasko17dfda92016-12-01 14:06:16 +0100603 return _nc_server_ssh_add_authkey(pubkey_path, NULL, 0, username);
Michal Vasko086311b2016-01-08 09:53:11 +0100604}
605
606API int
Michal Vasko17dfda92016-12-01 14:06:16 +0100607nc_server_ssh_add_authkey(const char *pubkey_base64, NC_SSH_KEY_TYPE type, const char *username)
Michal Vasko086311b2016-01-08 09:53:11 +0100608{
Michal Vasko17dfda92016-12-01 14:06:16 +0100609 if (!pubkey_base64) {
610 ERRARG("pubkey_base64");
611 return -1;
612 } else if (!type) {
613 ERRARG("type");
614 return -1;
615 } else if (!username) {
616 ERRARG("username");
Michal Vasko3031aae2016-01-27 16:07:18 +0100617 return -1;
618 }
619
Michal Vasko17dfda92016-12-01 14:06:16 +0100620 return _nc_server_ssh_add_authkey(NULL, pubkey_base64, type, username);
Michal Vasko086311b2016-01-08 09:53:11 +0100621}
622
623API int
Michal Vasko17dfda92016-12-01 14:06:16 +0100624nc_server_ssh_del_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type,
625 const char *username)
Michal Vaskob05053d2016-01-22 16:12:06 +0100626{
Michal Vasko086311b2016-01-08 09:53:11 +0100627 uint32_t i;
628 int ret = -1;
629
Michal Vasko17dfda92016-12-01 14:06:16 +0100630 /* LOCK */
631 pthread_mutex_lock(&server_opts.authkey_lock);
632
633 if (!pubkey_path && !pubkey_base64 && !type && !username) {
634 for (i = 0; i < server_opts.authkey_count; ++i) {
635 lydict_remove(server_opts.ctx, server_opts.authkeys[i].path);
636 lydict_remove(server_opts.ctx, server_opts.authkeys[i].base64);
637 lydict_remove(server_opts.ctx, server_opts.authkeys[i].username);
Michal Vasko086311b2016-01-08 09:53:11 +0100638
Michal Vasko086311b2016-01-08 09:53:11 +0100639 ret = 0;
640 }
Michal Vasko17dfda92016-12-01 14:06:16 +0100641 free(server_opts.authkeys);
642 server_opts.authkeys = NULL;
643 server_opts.authkey_count = 0;
Michal Vasko1a38c862016-01-15 15:50:07 +0100644 } else {
Michal Vasko17dfda92016-12-01 14:06:16 +0100645 for (i = 0; i < server_opts.authkey_count; ++i) {
646 if ((!pubkey_path || !strcmp(server_opts.authkeys[i].path, pubkey_path))
Michal Vaskoe846aee2019-03-28 08:38:41 +0100647 && (!pubkey_base64 || !strcmp(server_opts.authkeys[i].base64, pubkey_base64))
Michal Vasko17dfda92016-12-01 14:06:16 +0100648 && (!type || (server_opts.authkeys[i].type == type))
649 && (!username || !strcmp(server_opts.authkeys[i].username, username))) {
650 lydict_remove(server_opts.ctx, server_opts.authkeys[i].path);
651 lydict_remove(server_opts.ctx, server_opts.authkeys[i].base64);
652 lydict_remove(server_opts.ctx, server_opts.authkeys[i].username);
Michal Vasko1a38c862016-01-15 15:50:07 +0100653
Michal Vasko17dfda92016-12-01 14:06:16 +0100654 --server_opts.authkey_count;
655 if (i < server_opts.authkey_count) {
656 memcpy(&server_opts.authkeys[i], &server_opts.authkeys[server_opts.authkey_count],
657 sizeof *server_opts.authkeys);
658 } else if (!server_opts.authkey_count) {
659 free(server_opts.authkeys);
660 server_opts.authkeys = NULL;
Michal Vaskoc0256492016-02-02 12:19:06 +0100661 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100662
663 ret = 0;
664 }
665 }
Michal Vasko086311b2016-01-08 09:53:11 +0100666 }
667
Michal Vasko51e514d2016-02-02 15:51:52 +0100668 /* UNLOCK */
Michal Vasko17dfda92016-12-01 14:06:16 +0100669 pthread_mutex_unlock(&server_opts.authkey_lock);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100670
671 return ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100672}
673
674void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100675nc_server_ssh_clear_opts(struct nc_server_ssh_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +0100676{
Michal Vasko7d255882017-02-09 13:35:08 +0100677 nc_server_ssh_del_hostkey(NULL, -1, opts);
Michal Vaskob05053d2016-01-22 16:12:06 +0100678}
679
Michal Vasko086311b2016-01-08 09:53:11 +0100680static char *
681auth_password_get_pwd_hash(const char *username)
682{
apropp-molex4e903c32020-04-20 03:06:58 -0400683#ifdef HAVE_SHADOW
Michal Vasko086311b2016-01-08 09:53:11 +0100684 struct passwd *pwd, pwd_buf;
685 struct spwd *spwd, spwd_buf;
686 char *pass_hash = NULL, buf[256];
687
688 getpwnam_r(username, &pwd_buf, buf, 256, &pwd);
689 if (!pwd) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100690 VRB("User \"%s\" not found locally.", username);
Michal Vasko086311b2016-01-08 09:53:11 +0100691 return NULL;
692 }
693
694 if (!strcmp(pwd->pw_passwd, "x")) {
Michal Vaskob4ff2fd2020-11-03 14:18:13 +0100695# ifndef __QNXNTO__
696 getspnam_r(username, &spwd_buf, buf, 256, &spwd);
697# else
698 spwd = getspnam_r(username, &spwd_buf, buf, 256);
699# endif
Michal Vasko086311b2016-01-08 09:53:11 +0100700 if (!spwd) {
701 VRB("Failed to retrieve the shadow entry for \"%s\".", username);
702 return NULL;
Michal Vasko22b4fe72020-11-04 08:52:29 +0100703 } else if ((spwd->sp_expire > -1) && (spwd->sp_expire <= (time(NULL) / (60 * 60 * 24)))) {
704 WRN("User \"%s\" account has expired.", username);
705 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100706 }
707
708 pass_hash = spwd->sp_pwdp;
709 } else {
710 pass_hash = pwd->pw_passwd;
711 }
712
713 if (!pass_hash) {
Michal Vaskod083db62016-01-19 10:31:29 +0100714 ERR("No password could be retrieved for \"%s\".", username);
Michal Vasko086311b2016-01-08 09:53:11 +0100715 return NULL;
716 }
717
718 /* check the hash structure for special meaning */
719 if (!strcmp(pass_hash, "*") || !strcmp(pass_hash, "!")) {
720 VRB("User \"%s\" is not allowed to authenticate using a password.", username);
721 return NULL;
722 }
723 if (!strcmp(pass_hash, "*NP*")) {
724 VRB("Retrieving password for \"%s\" from a NIS+ server not supported.", username);
725 return NULL;
726 }
727
728 return strdup(pass_hash);
Claus Klein22091912020-01-20 13:45:47 +0100729#else
730 return strdup("");
731#endif
Michal Vasko086311b2016-01-08 09:53:11 +0100732}
733
734static int
735auth_password_compare_pwd(const char *pass_hash, const char *pass_clear)
736{
737 char *new_pass_hash;
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200738#if defined(HAVE_CRYPT_R)
Michal Vasko086311b2016-01-08 09:53:11 +0100739 struct crypt_data cdata;
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200740#endif
Michal Vasko086311b2016-01-08 09:53:11 +0100741
742 if (!pass_hash[0]) {
743 if (!pass_clear[0]) {
744 WRN("User authentication successful with an empty password!");
745 return 0;
746 } else {
747 /* the user did now know he does not need any password,
748 * (which should not be used) so deny authentication */
749 return 1;
750 }
751 }
752
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200753#if defined(HAVE_CRYPT_R)
Michal Vasko086311b2016-01-08 09:53:11 +0100754 cdata.initialized = 0;
755 new_pass_hash = crypt_r(pass_clear, pass_hash, &cdata);
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200756#else
Mislav Novakovicce9a7ef2017-08-08 13:45:52 +0200757 pthread_mutex_lock(&crypt_lock);
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200758 new_pass_hash = crypt(pass_clear, pass_hash);
Mislav Novakovicce9a7ef2017-08-08 13:45:52 +0200759 pthread_mutex_unlock(&crypt_lock);
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200760#endif
Andrew Langefeld158d6fd2018-06-11 18:51:44 -0500761
762 if (!new_pass_hash) {
763 return 1;
764 }
765
Michal Vasko086311b2016-01-08 09:53:11 +0100766 return strcmp(new_pass_hash, pass_hash);
767}
768
769static void
770nc_sshcb_auth_password(struct nc_session *session, ssh_message msg)
771{
772 char *pass_hash;
Michal Vaskoebba7602018-03-23 13:14:08 +0100773 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100774
Michal Vaskoebba7602018-03-23 13:14:08 +0100775 if (server_opts.passwd_auth_clb) {
776 auth_ret = server_opts.passwd_auth_clb(session, ssh_message_auth_password(msg), server_opts.passwd_auth_data);
777 } else {
778 pass_hash = auth_password_get_pwd_hash(session->username);
779 if (pass_hash) {
780 auth_ret = auth_password_compare_pwd(pass_hash, ssh_message_auth_password(msg));
781 free(pass_hash);
782 }
Michal Vasko086311b2016-01-08 09:53:11 +0100783 }
784
Michal Vaskoebba7602018-03-23 13:14:08 +0100785 if (!auth_ret) {
786 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
787 VRB("User \"%s\" authenticated.", session->username);
788 ssh_message_auth_reply_success(msg, 0);
789 } else {
790 ++session->opts.server.ssh_auth_attempts;
791 VRB("Failed user \"%s\" authentication attempt (#%d).", session->username, session->opts.server.ssh_auth_attempts);
792 ssh_message_reply_default(msg);
793 }
Michal Vasko086311b2016-01-08 09:53:11 +0100794}
795
796static void
797nc_sshcb_auth_kbdint(struct nc_session *session, ssh_message msg)
798{
bhart3bc2f582018-06-05 12:40:32 -0500799 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100800 char *pass_hash;
801
bhart1bb7cdb2018-07-02 15:03:30 -0500802 if (server_opts.interactive_auth_clb) {
Michal Vasko733c0bd2018-07-03 13:14:40 +0200803 auth_ret = server_opts.interactive_auth_clb(session, msg, server_opts.interactive_auth_data);
Michal Vasko086311b2016-01-08 09:53:11 +0100804 } else {
bhart1bb7cdb2018-07-02 15:03:30 -0500805 if (!ssh_message_auth_kbdint_is_response(msg)) {
806 const char *prompts[] = {"Password: "};
807 char echo[] = {0};
808
809 ssh_message_auth_interactive_request(msg, "Interactive SSH Authentication", "Type your password:", 1, prompts, echo);
Robert Vargaad7a5532018-08-10 20:40:54 +0200810 auth_ret = -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100811 } else {
bhart1bb7cdb2018-07-02 15:03:30 -0500812 if (ssh_userauth_kbdint_getnanswers(session->ti.libssh.session) != 1) {// failed session
813 ssh_message_reply_default(msg);
814 return;
815 }
bhart3bc2f582018-06-05 12:40:32 -0500816 pass_hash = auth_password_get_pwd_hash(session->username);// get hashed password
817 if (pass_hash) {
Robert Vargaad7a5532018-08-10 20:40:54 +0200818 /* Normalize auth_password_compare_pwd result to 0 or 1 */
819 auth_ret = !!auth_password_compare_pwd(pass_hash, ssh_userauth_kbdint_getanswer(session->ti.libssh.session, 0));
bhart3bc2f582018-06-05 12:40:32 -0500820 free(pass_hash);// free hashed password
821 }
Michal Vasko086311b2016-01-08 09:53:11 +0100822 }
bhart1bb7cdb2018-07-02 15:03:30 -0500823 }
824
Robert Vargaad7a5532018-08-10 20:40:54 +0200825 /* We have already sent a reply */
826 if (auth_ret == -1) {
827 return;
828 }
829
bhart1bb7cdb2018-07-02 15:03:30 -0500830 /* Authenticate message based on outcome */
831 if (!auth_ret) {
832 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
833 VRB("User \"%s\" authenticated.", session->username);
834 ssh_message_auth_reply_success(msg, 0);
835 } else {
836 ++session->opts.server.ssh_auth_attempts;
837 VRB("Failed user \"%s\" authentication attempt (#%d).", session->username, session->opts.server.ssh_auth_attempts);
838 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +0100839 }
840}
841
842static const char *
Michal Vasko17dfda92016-12-01 14:06:16 +0100843auth_pubkey_compare_key(ssh_key key)
Michal Vasko086311b2016-01-08 09:53:11 +0100844{
845 uint32_t i;
846 ssh_key pub_key;
Michal Vasko76e3a352016-01-18 09:07:00 +0100847 const char *username = NULL;
Michal Vasko3e9d1682017-02-24 09:50:15 +0100848 int ret = 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100849
Michal Vaskoa05c7b12017-01-30 14:33:08 +0100850 /* LOCK */
851 pthread_mutex_lock(&server_opts.authkey_lock);
852
Michal Vasko17dfda92016-12-01 14:06:16 +0100853 for (i = 0; i < server_opts.authkey_count; ++i) {
854 switch (server_opts.authkeys[i].type) {
855 case NC_SSH_KEY_UNKNOWN:
856 ret = ssh_pki_import_pubkey_file(server_opts.authkeys[i].path, &pub_key);
857 break;
858 case NC_SSH_KEY_DSA:
859 ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_DSS, &pub_key);
860 break;
861 case NC_SSH_KEY_RSA:
862 ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_RSA, &pub_key);
863 break;
864 case NC_SSH_KEY_ECDSA:
865 ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_ECDSA, &pub_key);
866 break;
867 }
868
Michal Vasko7abcdeb2016-05-30 15:27:00 +0200869 if (ret == SSH_EOF) {
Michal Vasko17dfda92016-12-01 14:06:16 +0100870 WRN("Failed to import a public key of \"%s\" (File access problem).", server_opts.authkeys[i].username);
Michal Vasko7abcdeb2016-05-30 15:27:00 +0200871 continue;
872 } else if (ret == SSH_ERROR) {
Michal Vasko17dfda92016-12-01 14:06:16 +0100873 WRN("Failed to import a public key of \"%s\" (SSH error).", server_opts.authkeys[i].username);
Michal Vasko086311b2016-01-08 09:53:11 +0100874 continue;
875 }
876
877 if (!ssh_key_cmp(key, pub_key, SSH_KEY_CMP_PUBLIC)) {
878 ssh_key_free(pub_key);
879 break;
880 }
881
882 ssh_key_free(pub_key);
883 }
884
Michal Vasko17dfda92016-12-01 14:06:16 +0100885 if (i < server_opts.authkey_count) {
886 username = server_opts.authkeys[i].username;
Michal Vasko086311b2016-01-08 09:53:11 +0100887 }
888
Michal Vaskoa05c7b12017-01-30 14:33:08 +0100889 /* UNLOCK */
890 pthread_mutex_unlock(&server_opts.authkey_lock);
891
Michal Vasko086311b2016-01-08 09:53:11 +0100892 return username;
893}
894
895static void
896nc_sshcb_auth_pubkey(struct nc_session *session, ssh_message msg)
897{
898 const char *username;
899 int signature_state;
900
Michal Vasko733c0bd2018-07-03 13:14:40 +0200901 if (server_opts.pubkey_auth_clb) {
902 if (server_opts.pubkey_auth_clb(session, ssh_message_auth_pubkey(msg), server_opts.pubkey_auth_data)) {
bhart3bc2f582018-06-05 12:40:32 -0500903 goto fail;
904 }
Michal Vasko733c0bd2018-07-03 13:14:40 +0200905 } else {
bhart3bc2f582018-06-05 12:40:32 -0500906 if ((username = auth_pubkey_compare_key(ssh_message_auth_pubkey(msg))) == NULL) {
907 VRB("User \"%s\" tried to use an unknown (unauthorized) public key.", session->username);
908 goto fail;
909 } else if (strcmp(session->username, username)) {
910 VRB("User \"%s\" is not the username identified with the presented public key.", session->username);
911 goto fail;
912 }
Michal Vaskobd13a932016-09-14 09:00:35 +0200913 }
Michal Vaskobd13a932016-09-14 09:00:35 +0200914
Michal Vasko086311b2016-01-08 09:53:11 +0100915 signature_state = ssh_message_auth_publickey_state(msg);
916 if (signature_state == SSH_PUBLICKEY_STATE_VALID) {
917 VRB("User \"%s\" authenticated.", session->username);
918 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
919 ssh_message_auth_reply_success(msg, 0);
Michal Vasko086311b2016-01-08 09:53:11 +0100920 } else if (signature_state == SSH_PUBLICKEY_STATE_NONE) {
Michal Vaskobd13a932016-09-14 09:00:35 +0200921 /* accepting only the use of a public key */
922 ssh_message_auth_reply_pk_ok_simple(msg);
Michal Vasko086311b2016-01-08 09:53:11 +0100923 }
924
Michal Vaskobd13a932016-09-14 09:00:35 +0200925 return;
926
927fail:
Michal Vasko2e6defd2016-10-07 15:48:15 +0200928 ++session->opts.server.ssh_auth_attempts;
929 VRB("Failed user \"%s\" authentication attempt (#%d).", session->username, session->opts.server.ssh_auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +0100930 ssh_message_reply_default(msg);
931}
932
933static int
Michal Vasko96164bf2016-01-21 15:41:58 +0100934nc_sshcb_channel_open(struct nc_session *session, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +0100935{
Michal Vasko96164bf2016-01-21 15:41:58 +0100936 ssh_channel chan;
937
938 /* first channel request */
939 if (!session->ti.libssh.channel) {
940 if (session->status != NC_STATUS_STARTING) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100941 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100942 return -1;
943 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100944 chan = ssh_message_channel_request_open_reply_accept(msg);
945 if (!chan) {
946 ERR("Failed to create a new SSH channel.");
947 return -1;
948 }
949 session->ti.libssh.channel = chan;
Michal Vasko086311b2016-01-08 09:53:11 +0100950
Michal Vasko96164bf2016-01-21 15:41:58 +0100951 /* additional channel request */
952 } else {
953 chan = ssh_message_channel_request_open_reply_accept(msg);
954 if (!chan) {
955 ERR("Session %u: failed to create a new SSH channel.", session->id);
956 return -1;
957 }
958 /* channel was created and libssh stored it internally in the ssh_session structure, good enough */
Michal Vasko086311b2016-01-08 09:53:11 +0100959 }
960
Michal Vasko086311b2016-01-08 09:53:11 +0100961 return 0;
962}
963
964static int
965nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem)
966{
Michal Vasko96164bf2016-01-21 15:41:58 +0100967 struct nc_session *new_session;
Michal Vasko086311b2016-01-08 09:53:11 +0100968
Michal Vasko96164bf2016-01-21 15:41:58 +0100969 if (strcmp(subsystem, "netconf")) {
970 WRN("Received an unknown subsystem \"%s\" request.", subsystem);
Michal Vasko086311b2016-01-08 09:53:11 +0100971 return -1;
972 }
973
Michal Vasko96164bf2016-01-21 15:41:58 +0100974 if (session->ti.libssh.channel == channel) {
975 /* first channel requested */
976 if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) {
977 ERRINT;
978 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100979 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100980 if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) {
981 ERR("Session %u: subsystem \"netconf\" requested for the second time.", session->id);
982 return -1;
983 }
984
985 session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF;
Michal Vasko086311b2016-01-08 09:53:11 +0100986 } else {
Michal Vasko96164bf2016-01-21 15:41:58 +0100987 /* additional channel subsystem request, new session is ready as far as SSH is concerned */
Michal Vasko131120a2018-05-29 15:44:02 +0200988 new_session = nc_new_session(NC_SERVER, 1);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100989 if (!new_session) {
990 ERRMEM;
991 return -1;
992 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100993
994 /* insert the new session */
995 if (!session->ti.libssh.next) {
996 new_session->ti.libssh.next = session;
997 } else {
998 new_session->ti.libssh.next = session->ti.libssh.next;
999 }
1000 session->ti.libssh.next = new_session;
1001
1002 new_session->status = NC_STATUS_STARTING;
Michal Vasko96164bf2016-01-21 15:41:58 +01001003 new_session->ti_type = NC_TI_LIBSSH;
Michal Vasko131120a2018-05-29 15:44:02 +02001004 new_session->io_lock = session->io_lock;
Michal Vasko96164bf2016-01-21 15:41:58 +01001005 new_session->ti.libssh.channel = channel;
1006 new_session->ti.libssh.session = session->ti.libssh.session;
Michal Vasko77367452021-02-16 16:32:18 +01001007 lydict_insert(server_opts.ctx, session->username, 0, &new_session->username);
1008 lydict_insert(server_opts.ctx, session->host, 0, &new_session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +01001009 new_session->port = session->port;
1010 new_session->ctx = server_opts.ctx;
Michal Vasko83d15322018-09-27 09:44:02 +02001011 new_session->flags = NC_SESSION_SSH_AUTHENTICATED | NC_SESSION_SSH_SUBSYS_NETCONF | NC_SESSION_SHAREDCTX;
Michal Vasko086311b2016-01-08 09:53:11 +01001012 }
1013
1014 return 0;
1015}
1016
Michal Vasko96164bf2016-01-21 15:41:58 +01001017int
Michal Vaskob48aa812016-01-18 14:13:09 +01001018nc_sshcb_msg(ssh_session UNUSED(sshsession), ssh_message msg, void *data)
Michal Vasko086311b2016-01-08 09:53:11 +01001019{
1020 const char *str_type, *str_subtype = NULL, *username;
1021 int subtype, type;
1022 struct nc_session *session = (struct nc_session *)data;
Michal Vasko086311b2016-01-08 09:53:11 +01001023
1024 type = ssh_message_type(msg);
1025 subtype = ssh_message_subtype(msg);
1026
1027 switch (type) {
1028 case SSH_REQUEST_AUTH:
1029 str_type = "request-auth";
1030 switch (subtype) {
1031 case SSH_AUTH_METHOD_NONE:
1032 str_subtype = "none";
1033 break;
1034 case SSH_AUTH_METHOD_PASSWORD:
1035 str_subtype = "password";
1036 break;
1037 case SSH_AUTH_METHOD_PUBLICKEY:
1038 str_subtype = "publickey";
1039 break;
1040 case SSH_AUTH_METHOD_HOSTBASED:
1041 str_subtype = "hostbased";
1042 break;
1043 case SSH_AUTH_METHOD_INTERACTIVE:
1044 str_subtype = "interactive";
1045 break;
1046 case SSH_AUTH_METHOD_GSSAPI_MIC:
1047 str_subtype = "gssapi-mic";
1048 break;
1049 }
1050 break;
1051
1052 case SSH_REQUEST_CHANNEL_OPEN:
1053 str_type = "request-channel-open";
1054 switch (subtype) {
1055 case SSH_CHANNEL_SESSION:
1056 str_subtype = "session";
1057 break;
1058 case SSH_CHANNEL_DIRECT_TCPIP:
1059 str_subtype = "direct-tcpip";
1060 break;
1061 case SSH_CHANNEL_FORWARDED_TCPIP:
1062 str_subtype = "forwarded-tcpip";
1063 break;
1064 case (int)SSH_CHANNEL_X11:
1065 str_subtype = "channel-x11";
1066 break;
1067 case SSH_CHANNEL_UNKNOWN:
1068 /* fallthrough */
1069 default:
1070 str_subtype = "unknown";
1071 break;
1072 }
1073 break;
1074
1075 case SSH_REQUEST_CHANNEL:
1076 str_type = "request-channel";
1077 switch (subtype) {
1078 case SSH_CHANNEL_REQUEST_PTY:
1079 str_subtype = "pty";
1080 break;
1081 case SSH_CHANNEL_REQUEST_EXEC:
1082 str_subtype = "exec";
1083 break;
1084 case SSH_CHANNEL_REQUEST_SHELL:
1085 str_subtype = "shell";
1086 break;
1087 case SSH_CHANNEL_REQUEST_ENV:
1088 str_subtype = "env";
1089 break;
1090 case SSH_CHANNEL_REQUEST_SUBSYSTEM:
1091 str_subtype = "subsystem";
1092 break;
1093 case SSH_CHANNEL_REQUEST_WINDOW_CHANGE:
1094 str_subtype = "window-change";
1095 break;
1096 case SSH_CHANNEL_REQUEST_X11:
1097 str_subtype = "x11";
1098 break;
1099 case SSH_CHANNEL_REQUEST_UNKNOWN:
1100 /* fallthrough */
1101 default:
1102 str_subtype = "unknown";
1103 break;
1104 }
1105 break;
1106
1107 case SSH_REQUEST_SERVICE:
1108 str_type = "request-service";
1109 str_subtype = ssh_message_service_service(msg);
1110 break;
1111
1112 case SSH_REQUEST_GLOBAL:
1113 str_type = "request-global";
1114 switch (subtype) {
1115 case SSH_GLOBAL_REQUEST_TCPIP_FORWARD:
1116 str_subtype = "tcpip-forward";
1117 break;
1118 case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD:
1119 str_subtype = "cancel-tcpip-forward";
1120 break;
1121 case SSH_GLOBAL_REQUEST_UNKNOWN:
1122 /* fallthrough */
1123 default:
1124 str_subtype = "unknown";
1125 break;
1126 }
1127 break;
1128
1129 default:
1130 str_type = "unknown";
1131 str_subtype = "unknown";
1132 break;
1133 }
1134
1135 VRB("Received an SSH message \"%s\" of subtype \"%s\".", str_type, str_subtype);
Michal Vasko5e0edd82020-07-29 15:26:13 +02001136 if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) {
Michal Vaskoce319162016-02-03 15:33:08 +01001137 /* "valid" situation if, for example, receiving some auth or channel request timeouted,
1138 * but we got it now, during session free */
1139 VRB("SSH message arrived on a %s session, the request will be denied.",
Michal Vasko5e0edd82020-07-29 15:26:13 +02001140 (session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid"));
Michal Vaskoce319162016-02-03 15:33:08 +01001141 ssh_message_reply_default(msg);
1142 return 0;
1143 }
Michal Vasko96164bf2016-01-21 15:41:58 +01001144 session->flags |= NC_SESSION_SSH_NEW_MSG;
Michal Vasko086311b2016-01-08 09:53:11 +01001145
1146 /*
1147 * process known messages
1148 */
1149 if (type == SSH_REQUEST_AUTH) {
1150 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
1151 ERR("User \"%s\" authenticated, but requested another authentication.", session->username);
1152 ssh_message_reply_default(msg);
1153 return 0;
1154 }
1155
Michal Vasko086311b2016-01-08 09:53:11 +01001156 /* save the username, do not let the client change it */
1157 username = ssh_message_auth_user(msg);
1158 if (!session->username) {
1159 if (!username) {
1160 ERR("Denying an auth request without a username.");
1161 return 1;
1162 }
1163
Michal Vasko77367452021-02-16 16:32:18 +01001164 lydict_insert(server_opts.ctx, username, 0, &session->username);
Michal Vasko086311b2016-01-08 09:53:11 +01001165 } else if (username) {
1166 if (strcmp(username, session->username)) {
1167 ERR("User \"%s\" changed its username to \"%s\".", session->username, username);
1168 session->status = NC_STATUS_INVALID;
Michal Vasko428087d2016-01-14 16:04:28 +01001169 session->term_reason = NC_SESSION_TERM_OTHER;
Michal Vasko086311b2016-01-08 09:53:11 +01001170 return 1;
1171 }
1172 }
1173
1174 if (subtype == SSH_AUTH_METHOD_NONE) {
1175 /* libssh will return the supported auth methods */
1176 return 1;
1177 } else if (subtype == SSH_AUTH_METHOD_PASSWORD) {
1178 nc_sshcb_auth_password(session, msg);
1179 return 0;
1180 } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) {
1181 nc_sshcb_auth_pubkey(session, msg);
1182 return 0;
1183 } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) {
1184 nc_sshcb_auth_kbdint(session, msg);
1185 return 0;
1186 }
1187 } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko0df67562016-01-21 15:50:11 +01001188 if ((type == SSH_REQUEST_CHANNEL_OPEN) && ((enum ssh_channel_type_e)subtype == SSH_CHANNEL_SESSION)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001189 if (nc_sshcb_channel_open(session, msg)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001190 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001191 }
Michal Vasko086311b2016-01-08 09:53:11 +01001192 return 0;
Michal Vasko96164bf2016-01-21 15:41:58 +01001193
Michal Vasko0df67562016-01-21 15:50:11 +01001194 } else if ((type == SSH_REQUEST_CHANNEL) && ((enum ssh_channel_requests_e)subtype == SSH_CHANNEL_REQUEST_SUBSYSTEM)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001195 if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg),
1196 ssh_message_channel_request_subsystem(msg))) {
Michal Vasko086311b2016-01-08 09:53:11 +01001197 ssh_message_reply_default(msg);
Michal Vasko96164bf2016-01-21 15:41:58 +01001198 } else {
1199 ssh_message_channel_request_reply_success(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001200 }
1201 return 0;
1202 }
1203 }
1204
1205 /* we did not process it */
1206 return 1;
1207}
1208
Michal Vasko1a38c862016-01-15 15:50:07 +01001209/* ret 1 on success, 0 on timeout, -1 on error */
Michal Vasko086311b2016-01-08 09:53:11 +01001210static int
1211nc_open_netconf_channel(struct nc_session *session, int timeout)
1212{
Michal Vasko36c7be82017-02-22 13:37:59 +01001213 int ret;
1214 struct timespec ts_timeout, ts_cur;
Michal Vasko086311b2016-01-08 09:53:11 +01001215
1216 /* message callback is executed twice to give chance for the channel to be
1217 * created if timeout == 0 (it takes 2 messages, channel-open, subsystem-request) */
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001218 if (!timeout) {
Michal Vasko2a7d4732016-01-15 09:24:46 +01001219 if (!nc_session_is_connected(session)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001220 ERR("Communication socket unexpectedly closed (libssh).");
Michal Vasko2a7d4732016-01-15 09:24:46 +01001221 return -1;
1222 }
1223
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001224 ret = ssh_execute_message_callbacks(session->ti.libssh.session);
1225 if (ret != SSH_OK) {
Michal Vaskod083db62016-01-19 10:31:29 +01001226 ERR("Failed to receive SSH messages on a session (%s).",
1227 ssh_get_error(session->ti.libssh.session));
Michal Vasko086311b2016-01-08 09:53:11 +01001228 return -1;
1229 }
1230
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001231 if (!session->ti.libssh.channel) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001232 return 0;
1233 }
1234
1235 ret = ssh_execute_message_callbacks(session->ti.libssh.session);
1236 if (ret != SSH_OK) {
Michal Vaskod083db62016-01-19 10:31:29 +01001237 ERR("Failed to receive SSH messages on a session (%s).",
1238 ssh_get_error(session->ti.libssh.session));
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001239 return -1;
1240 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001241
1242 if (!(session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
1243 /* we did not receive subsystem-request, timeout */
1244 return 0;
1245 }
1246
1247 return 1;
1248 }
1249
Michal Vasko36c7be82017-02-22 13:37:59 +01001250 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001251 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001252 nc_addtimespec(&ts_timeout, timeout);
1253 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001254 while (1) {
1255 if (!nc_session_is_connected(session)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001256 ERR("Communication socket unexpectedly closed (libssh).");
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001257 return -1;
1258 }
1259
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001260 ret = ssh_execute_message_callbacks(session->ti.libssh.session);
1261 if (ret != SSH_OK) {
Michal Vaskod083db62016-01-19 10:31:29 +01001262 ERR("Failed to receive SSH messages on a session (%s).",
1263 ssh_get_error(session->ti.libssh.session));
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001264 return -1;
1265 }
1266
Michal Vasko086311b2016-01-08 09:53:11 +01001267 if (session->ti.libssh.channel && (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko1a38c862016-01-15 15:50:07 +01001268 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001269 }
1270
Michal Vasko086311b2016-01-08 09:53:11 +01001271 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001272 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001273 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001274 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1275 /* timeout */
1276 ERR("Failed to start \"netconf\" SSH subsystem for too long, disconnecting.");
1277 break;
1278 }
1279 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001280 }
Michal Vasko086311b2016-01-08 09:53:11 +01001281
Michal Vasko1a38c862016-01-15 15:50:07 +01001282 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001283}
1284
Michal Vasko4c1fb492017-01-30 14:31:07 +01001285static int
1286nc_ssh_bind_add_hostkeys(ssh_bind sbind, const char **hostkeys, uint8_t hostkey_count)
1287{
1288 uint8_t i;
1289 char *privkey_path, *privkey_data;
Michal Vaskoddce1212019-05-24 09:58:49 +02001290 int ret;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001291 NC_SSH_KEY_TYPE privkey_type;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001292
1293 if (!server_opts.hostkey_clb) {
1294 ERR("Callback for retrieving SSH host keys not set.");
1295 return -1;
1296 }
1297
1298 for (i = 0; i < hostkey_count; ++i) {
1299 privkey_path = privkey_data = NULL;
Michal Vaskoddce1212019-05-24 09:58:49 +02001300 if (server_opts.hostkey_clb(hostkeys[i], server_opts.hostkey_data, &privkey_path, &privkey_data, &privkey_type)) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001301 ERR("Host key callback failed.");
1302 return -1;
1303 }
1304
1305 if (privkey_data) {
Michal Vaskoddce1212019-05-24 09:58:49 +02001306 privkey_path = base64der_key_to_tmp_file(privkey_data, nc_keytype2str(privkey_type));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001307 if (!privkey_path) {
1308 ERR("Temporarily storing a host key into a file failed (%s).", strerror(errno));
1309 free(privkey_data);
1310 return -1;
1311 }
1312 }
1313
1314 ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path);
1315
1316 /* cleanup */
1317 if (privkey_data && unlink(privkey_path)) {
1318 WRN("Removing a temporary host key file \"%s\" failed (%s).", privkey_path, strerror(errno));
1319 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001320 free(privkey_data);
1321
1322 if (ret != SSH_OK) {
Michal Vasko80075de2017-07-10 11:38:52 +02001323 ERR("Failed to set hostkey \"%s\" (%s).", hostkeys[i], privkey_path);
1324 }
1325 free(privkey_path);
1326
1327 if (ret != SSH_OK) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001328 return -1;
1329 }
1330 }
1331
1332 return 0;
1333}
1334
Michal Vasko96164bf2016-01-21 15:41:58 +01001335int
Michal Vasko0190bc32016-03-02 15:47:49 +01001336nc_accept_ssh_session(struct nc_session *session, int sock, int timeout)
Michal Vasko3031aae2016-01-27 16:07:18 +01001337{
Michal Vaskoe2713da2016-08-22 16:06:40 +02001338 ssh_bind sbind;
Michal Vasko3031aae2016-01-27 16:07:18 +01001339 struct nc_server_ssh_opts *opts;
Michal Vasko36c7be82017-02-22 13:37:59 +01001340 int libssh_auth_methods = 0, ret;
1341 struct timespec ts_timeout, ts_cur;
Michal Vasko086311b2016-01-08 09:53:11 +01001342
Michal Vasko2cc4c682016-03-01 09:16:48 +01001343 opts = session->data;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001344
Michal Vasko086311b2016-01-08 09:53:11 +01001345 /* other transport-specific data */
1346 session->ti_type = NC_TI_LIBSSH;
1347 session->ti.libssh.session = ssh_new();
1348 if (!session->ti.libssh.session) {
Michal Vaskod083db62016-01-19 10:31:29 +01001349 ERR("Failed to initialize a new SSH session.");
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001350 close(sock);
Michal Vasko9e036d52016-01-08 10:49:26 +01001351 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001352 }
1353
Michal Vaskoc61c4492016-01-25 11:13:34 +01001354 if (opts->auth_methods & NC_SSH_AUTH_PUBLICKEY) {
Michal Vasko086311b2016-01-08 09:53:11 +01001355 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
1356 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001357 if (opts->auth_methods & NC_SSH_AUTH_PASSWORD) {
Michal Vasko086311b2016-01-08 09:53:11 +01001358 libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD;
1359 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001360 if (opts->auth_methods & NC_SSH_AUTH_INTERACTIVE) {
Michal Vasko086311b2016-01-08 09:53:11 +01001361 libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE;
1362 }
1363 ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods);
1364
Michal Vaskoe2713da2016-08-22 16:06:40 +02001365 sbind = ssh_bind_new();
1366 if (!sbind) {
1367 ERR("Failed to create an SSH bind.");
1368 close(sock);
1369 return -1;
1370 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001371
1372 if (nc_ssh_bind_add_hostkeys(sbind, opts->hostkeys, opts->hostkey_count)) {
1373 close(sock);
1374 ssh_bind_free(sbind);
1375 return -1;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001376 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001377
Michal Vasko086311b2016-01-08 09:53:11 +01001378 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, session);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001379 /* remember that this session was just set as nc_sshcb_msg() parameter */
Michal Vasko96164bf2016-01-21 15:41:58 +01001380 session->flags |= NC_SESSION_SSH_MSG_CB;
Michal Vasko086311b2016-01-08 09:53:11 +01001381
Michal Vaskoe2713da2016-08-22 16:06:40 +02001382 if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) {
1383 ERR("SSH failed to accept a new connection (%s).", ssh_get_error(sbind));
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001384 close(sock);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001385 ssh_bind_free(sbind);
Michal Vasko9e036d52016-01-08 10:49:26 +01001386 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001387 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001388 ssh_bind_free(sbind);
Michal Vasko086311b2016-01-08 09:53:11 +01001389
Michal Vasko0190bc32016-03-02 15:47:49 +01001390 ssh_set_blocking(session->ti.libssh.session, 0);
1391
Michal Vasko36c7be82017-02-22 13:37:59 +01001392 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001393 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001394 nc_addtimespec(&ts_timeout, timeout);
1395 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001396 while ((ret = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) {
Michal Vaskoe65b4492016-03-03 11:57:51 +01001397 /* this tends to take longer */
1398 usleep(NC_TIMEOUT_STEP * 20);
Michal Vasko36c7be82017-02-22 13:37:59 +01001399 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001400 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001401 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1402 break;
1403 }
Michal Vasko0190bc32016-03-02 15:47:49 +01001404 }
1405 }
1406 if (ret == SSH_AGAIN) {
1407 ERR("SSH key exchange timeout.");
1408 return 0;
1409 } else if (ret != SSH_OK) {
Michal Vaskod083db62016-01-19 10:31:29 +01001410 ERR("SSH key exchange error (%s).", ssh_get_error(session->ti.libssh.session));
Michal Vasko9e036d52016-01-08 10:49:26 +01001411 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001412 }
1413
1414 /* authenticate */
Michal Vasko36c7be82017-02-22 13:37:59 +01001415 if (opts->auth_timeout) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001416 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001417 nc_addtimespec(&ts_timeout, opts->auth_timeout * 1000);
1418 }
1419 while (1) {
Michal Vasko2a7d4732016-01-15 09:24:46 +01001420 if (!nc_session_is_connected(session)) {
Michal Vaskoc13da702017-02-07 10:57:57 +01001421 ERR("Communication SSH socket unexpectedly closed.");
Michal Vasko2a7d4732016-01-15 09:24:46 +01001422 return -1;
1423 }
1424
Michal Vasko086311b2016-01-08 09:53:11 +01001425 if (ssh_execute_message_callbacks(session->ti.libssh.session) != SSH_OK) {
Michal Vaskod083db62016-01-19 10:31:29 +01001426 ERR("Failed to receive SSH messages on a session (%s).",
1427 ssh_get_error(session->ti.libssh.session));
Michal Vasko9e036d52016-01-08 10:49:26 +01001428 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001429 }
1430
Michal Vasko36c7be82017-02-22 13:37:59 +01001431 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
1432 break;
1433 }
1434
Michal Vasko145ae672017-02-07 10:57:27 +01001435 if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) {
1436 ERR("Too many failed authentication attempts of user \"%s\".", session->username);
1437 return -1;
1438 }
1439
Michal Vasko086311b2016-01-08 09:53:11 +01001440 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +01001441 if (opts->auth_timeout) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +02001442 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001443 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
1444 /* timeout */
1445 break;
1446 }
1447 }
1448 }
Michal Vasko086311b2016-01-08 09:53:11 +01001449
1450 if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) {
1451 /* timeout */
Michal Vaskoc13da702017-02-07 10:57:57 +01001452 if (session->username) {
1453 ERR("User \"%s\" failed to authenticate for too long, disconnecting.", session->username);
1454 } else {
1455 ERR("User failed to authenticate for too long, disconnecting.");
1456 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001457 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001458 }
1459
Michal Vasko086311b2016-01-08 09:53:11 +01001460 /* open channel */
Michal Vasko36c7be82017-02-22 13:37:59 +01001461 ret = nc_open_netconf_channel(session, timeout);
Michal Vasko1a38c862016-01-15 15:50:07 +01001462 if (ret < 1) {
1463 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001464 }
1465
Michal Vasko96164bf2016-01-21 15:41:58 +01001466 session->flags &= ~NC_SESSION_SSH_NEW_MSG;
Michal Vasko1a38c862016-01-15 15:50:07 +01001467 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001468}
1469
Michal Vasko71090fc2016-05-24 16:37:28 +02001470API NC_MSG_TYPE
1471nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session)
1472{
1473 NC_MSG_TYPE msgtype;
1474 struct nc_session *new_session = NULL;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001475 struct timespec ts_cur;
Michal Vasko71090fc2016-05-24 16:37:28 +02001476
1477 if (!orig_session) {
1478 ERRARG("orig_session");
1479 return NC_MSG_ERROR;
1480 } else if (!session) {
1481 ERRARG("session");
1482 return NC_MSG_ERROR;
1483 }
1484
1485 if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH)
1486 && orig_session->ti.libssh.next) {
1487 for (new_session = orig_session->ti.libssh.next;
1488 new_session != orig_session;
1489 new_session = new_session->ti.libssh.next) {
1490 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel
1491 && (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
1492 /* we found our session */
1493 break;
1494 }
1495 }
1496 if (new_session == orig_session) {
1497 new_session = NULL;
1498 }
1499 }
1500
1501 if (!new_session) {
1502 ERR("Session does not have a NETCONF SSH channel ready.");
1503 return NC_MSG_ERROR;
1504 }
1505
1506 /* assign new SID atomically */
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02001507 new_session->id = ATOMIC_INC(server_opts.new_session_id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001508
1509 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001510 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001511 if (msgtype != NC_MSG_HELLO) {
1512 return msgtype;
1513 }
1514
Michal Vasko9f6275e2017-10-05 13:50:05 +02001515 nc_gettimespec_real(&ts_cur);
1516 new_session->opts.server.session_start = ts_cur.tv_sec;
1517 nc_gettimespec_mono(&ts_cur);
1518 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko71090fc2016-05-24 16:37:28 +02001519 new_session->status = NC_STATUS_RUNNING;
1520 *session = new_session;
1521
1522 return msgtype;
1523}
1524
1525API NC_MSG_TYPE
Michal Vasko96164bf2016-01-21 15:41:58 +01001526nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +01001527{
Michal Vaskobdcf2362016-07-26 11:35:43 +02001528 uint8_t q_id;
Michal Vasko71090fc2016-05-24 16:37:28 +02001529 NC_MSG_TYPE msgtype;
Michal Vaskoe4300a82017-05-24 10:35:42 +02001530 struct nc_session *new_session = NULL, *cur_session;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001531 struct timespec ts_cur;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001532 uint16_t i;
Michal Vasko086311b2016-01-08 09:53:11 +01001533
Michal Vasko45e53ae2016-04-07 11:46:03 +02001534 if (!ps) {
1535 ERRARG("ps");
Michal Vasko71090fc2016-05-24 16:37:28 +02001536 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001537 } else if (!session) {
1538 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001539 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001540 }
1541
Michal Vasko48a63ed2016-03-01 09:48:21 +01001542 /* LOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001543 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001544 return NC_MSG_ERROR;
Michal Vaskof04a52a2016-04-07 10:52:10 +02001545 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001546
Michal Vasko96164bf2016-01-21 15:41:58 +01001547 for (i = 0; i < ps->session_count; ++i) {
fanchanghu3d4e7212017-08-09 09:42:30 +08001548 cur_session = ps->sessions[i]->session;
Michal Vaskoe4300a82017-05-24 10:35:42 +02001549 if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH)
1550 && cur_session->ti.libssh.next) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001551 /* an SSH session with more channels */
Michal Vaskoe4300a82017-05-24 10:35:42 +02001552 for (new_session = cur_session->ti.libssh.next;
1553 new_session != cur_session;
Michal Vasko96164bf2016-01-21 15:41:58 +01001554 new_session = new_session->ti.libssh.next) {
1555 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel
1556 && (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
1557 /* we found our session */
1558 break;
1559 }
1560 }
Michal Vaskoe4300a82017-05-24 10:35:42 +02001561 if (new_session != cur_session) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001562 break;
1563 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001564
Michal Vasko96164bf2016-01-21 15:41:58 +01001565 new_session = NULL;
1566 }
1567 }
Michal Vaskofb89d772016-01-08 12:25:35 +01001568
Michal Vasko48a63ed2016-03-01 09:48:21 +01001569 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02001570 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001571
Michal Vasko96164bf2016-01-21 15:41:58 +01001572 if (!new_session) {
1573 ERR("No session with a NETCONF SSH channel ready was found.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001574 return NC_MSG_ERROR;
Michal Vasko96164bf2016-01-21 15:41:58 +01001575 }
1576
1577 /* assign new SID atomically */
Michal Vasko7f1fa3c2020-09-08 16:30:41 +02001578 new_session->id = ATOMIC_INC(server_opts.new_session_id);
Michal Vaskofb89d772016-01-08 12:25:35 +01001579
Michal Vasko086311b2016-01-08 09:53:11 +01001580 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001581 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001582 if (msgtype != NC_MSG_HELLO) {
1583 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001584 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001585
Michal Vasko9f6275e2017-10-05 13:50:05 +02001586 nc_gettimespec_real(&ts_cur);
1587 new_session->opts.server.session_start = ts_cur.tv_sec;
1588 nc_gettimespec_mono(&ts_cur);
1589 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko086311b2016-01-08 09:53:11 +01001590 new_session->status = NC_STATUS_RUNNING;
Michal Vasko96164bf2016-01-21 15:41:58 +01001591 *session = new_session;
Michal Vasko086311b2016-01-08 09:53:11 +01001592
Michal Vasko71090fc2016-05-24 16:37:28 +02001593 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01001594}