blob: 0b7aad8541bb90658642998f4cd1a42aa07424a2 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
Michal Vasko95ea9ff2021-11-09 12:29:14 +01002 * @file session_server_ssh.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libnetconf2 SSH server session manipulation functions
Michal Vasko086311b2016-01-08 09:53:11 +01005 *
Michal Vasko95ea9ff2021-11-09 12:29:14 +01006 * @copyright
7 * Copyright (c) 2017 - 2021 CESNET, z.s.p.o.
Michal Vasko086311b2016-01-08 09:53:11 +01008 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01009 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010012 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010013 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010014 */
15
16#define _GNU_SOURCE
17
apropp-molex4e903c32020-04-20 03:06:58 -040018#include "config.h" /* Expose HAVE_SHADOW and HAVE_CRYPT */
19
20#ifdef HAVE_SHADOW
21 #include <shadow.h>
22#endif
23#ifdef HAVE_CRYPT
24 #include <crypt.h>
25#endif
Michal Vasko0d81c572022-09-26 10:39:31 +020026#ifdef HAVE_LIBPAM
27 #include <security/pam_appl.h>
28#endif
apropp-molex4e903c32020-04-20 03:06:58 -040029
Michal Vaskob83a3fa2021-05-26 09:53:42 +020030#include <errno.h>
31#include <fcntl.h>
32#include <pwd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010033#include <stdlib.h>
34#include <string.h>
Michal Vasko27252692017-03-21 15:34:13 +010035#include <sys/stat.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020036#include <sys/types.h>
Michal Vasko9f6275e2017-10-05 13:50:05 +020037#include <time.h>
Claus Klein22091912020-01-20 13:45:47 +010038#include <unistd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010039
Michal Vasko7a20d2e2021-05-19 16:40:23 +020040#include "compat.h"
41#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010042#include "session_server.h"
Michal Vaskoe22c6732016-01-29 11:03:02 +010043#include "session_server_ch.h"
Michal Vasko086311b2016-01-08 09:53:11 +010044
Michal Vaskob83a3fa2021-05-26 09:53:42 +020045#if !defined (HAVE_CRYPT_R)
Mislav Novakovicce9a7ef2017-08-08 13:45:52 +020046pthread_mutex_t crypt_lock = PTHREAD_MUTEX_INITIALIZER;
47#endif
48
Michal Vasko086311b2016-01-08 09:53:11 +010049extern struct nc_server_opts server_opts;
Michal Vaskob05053d2016-01-22 16:12:06 +010050
Michal Vasko4c1fb492017-01-30 14:31:07 +010051static char *
Michal Vaskoddce1212019-05-24 09:58:49 +020052base64der_key_to_tmp_file(const char *in, const char *key_str)
Michal Vasko086311b2016-01-08 09:53:11 +010053{
Michal Vasko4c1fb492017-01-30 14:31:07 +010054 char path[12] = "/tmp/XXXXXX";
55 int fd, written;
Michal Vasko27252692017-03-21 15:34:13 +010056 mode_t umode;
Michal Vasko4c1fb492017-01-30 14:31:07 +010057 FILE *file;
58
59 if (in == NULL) {
60 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +010061 }
62
mekleob31878b2019-09-09 14:10:47 +020063 umode = umask(0177);
Michal Vasko4c1fb492017-01-30 14:31:07 +010064 fd = mkstemp(path);
Michal Vasko27252692017-03-21 15:34:13 +010065 umask(umode);
Michal Vasko4c1fb492017-01-30 14:31:07 +010066 if (fd == -1) {
67 return NULL;
68 }
69
Michal Vasko3964a832018-09-18 14:37:39 +020070 file = fdopen(fd, "w");
Michal Vasko4c1fb492017-01-30 14:31:07 +010071 if (!file) {
72 close(fd);
73 return NULL;
74 }
75
76 /* write the key into the file */
Michal Vasko68177b72020-04-27 15:46:53 +020077 if (key_str) {
78 written = fwrite("-----BEGIN ", 1, 11, file);
79 written += fwrite(key_str, 1, strlen(key_str), file);
80 written += fwrite(" PRIVATE KEY-----\n", 1, 18, file);
81 written += fwrite(in, 1, strlen(in), file);
82 written += fwrite("\n-----END ", 1, 10, file);
83 written += fwrite(key_str, 1, strlen(key_str), file);
84 written += fwrite(" PRIVATE KEY-----", 1, 17, file);
Michal Vasko4c1fb492017-01-30 14:31:07 +010085
Michal Vasko68177b72020-04-27 15:46:53 +020086 fclose(file);
87 if ((unsigned)written != 11 + strlen(key_str) + 18 + strlen(in) + 10 + strlen(key_str) + 17) {
88 unlink(path);
89 return NULL;
90 }
91 } else {
92 written = fwrite("-----BEGIN PRIVATE KEY-----\n", 1, 28, file);
93 written += fwrite(in, 1, strlen(in), file);
94 written += fwrite("\n-----END PRIVATE KEY-----", 1, 26, file);
95
96 fclose(file);
97 if ((unsigned)written != 28 + strlen(in) + 26) {
98 unlink(path);
99 return NULL;
100 }
Michal Vasko4c1fb492017-01-30 14:31:07 +0100101 }
102
103 return strdup(path);
104}
105
106static int
Michal Vasko7d255882017-02-09 13:35:08 +0100107nc_server_ssh_add_hostkey(const char *name, int16_t idx, struct nc_server_ssh_opts *opts)
Michal Vasko4c1fb492017-01-30 14:31:07 +0100108{
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100109 uint8_t i;
110
Michal Vasko4c1fb492017-01-30 14:31:07 +0100111 if (!name) {
112 ERRARG("name");
Michal Vasko5fcc7142016-02-02 12:21:10 +0100113 return -1;
Michal Vasko7d255882017-02-09 13:35:08 +0100114 } else if (idx > opts->hostkey_count) {
115 ERRARG("idx");
116 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100117 }
Michal Vaskod45e25a2016-01-08 15:48:44 +0100118
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100119 for (i = 0; i < opts->hostkey_count; ++i) {
120 if (!strcmp(opts->hostkeys[i], name)) {
121 ERRARG("name");
122 return -1;
123 }
124 }
125
Michal Vaskoe2713da2016-08-22 16:06:40 +0200126 ++opts->hostkey_count;
127 opts->hostkeys = nc_realloc(opts->hostkeys, opts->hostkey_count * sizeof *opts->hostkeys);
128 if (!opts->hostkeys) {
129 ERRMEM;
130 return -1;
131 }
Michal Vasko7d255882017-02-09 13:35:08 +0100132
133 if (idx < 0) {
134 idx = opts->hostkey_count - 1;
135 }
136 if (idx != opts->hostkey_count - 1) {
137 memmove(opts->hostkeys + idx + 1, opts->hostkeys + idx, opts->hostkey_count - idx);
138 }
Michal Vasko93224072021-11-09 12:14:28 +0100139 opts->hostkeys[idx] = strdup(name);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200140
Michal Vasko5fcc7142016-02-02 12:21:10 +0100141 return 0;
Michal Vaskob05053d2016-01-22 16:12:06 +0100142}
143
144API int
Michal Vasko7d255882017-02-09 13:35:08 +0100145nc_server_ssh_endpt_add_hostkey(const char *endpt_name, const char *name, int16_t idx)
Michal Vaskob05053d2016-01-22 16:12:06 +0100146{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100147 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100148 struct nc_endpt *endpt;
149
Michal Vasko51e514d2016-02-02 15:51:52 +0100150 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100151 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100152 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100153 return -1;
154 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200155
Michal Vasko7d255882017-02-09 13:35:08 +0100156 ret = nc_server_ssh_add_hostkey(name, idx, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200157
Michal Vasko51e514d2016-02-02 15:51:52 +0100158 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100159 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100160
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100161 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100162}
163
Michal Vasko974410a2018-04-03 09:36:57 +0200164API void
165nc_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 +0200166 void *user_data, void (*free_user_data)(void *user_data))
Michal Vasko974410a2018-04-03 09:36:57 +0200167{
168 server_opts.passwd_auth_clb = passwd_auth_clb;
169 server_opts.passwd_auth_data = user_data;
170 server_opts.passwd_auth_data_free = free_user_data;
171}
172
bhart1bb7cdb2018-07-02 15:03:30 -0500173API void
Michal Vasko1c2d2652023-10-17 08:53:36 +0200174nc_server_ssh_set_interactive_auth_sess_clb(int (*interactive_auth_sess_clb)(const struct nc_session *session,
175 ssh_session ssh_sess, ssh_message msg, void *user_data), void *user_data, void (*free_user_data)(void *user_data))
176{
177 server_opts.interactive_auth_sess_clb = interactive_auth_sess_clb;
178 server_opts.interactive_auth_sess_data = user_data;
179 server_opts.interactive_auth_sess_data_free = free_user_data;
180}
181
182API void
bhart1bb7cdb2018-07-02 15:03:30 -0500183nc_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 +0200184 void *user_data, void (*free_user_data)(void *user_data))
bhart1bb7cdb2018-07-02 15:03:30 -0500185{
186 server_opts.interactive_auth_clb = interactive_auth_clb;
187 server_opts.interactive_auth_data = user_data;
188 server_opts.interactive_auth_data_free = free_user_data;
189}
Michal Vasko733c0bd2018-07-03 13:14:40 +0200190
roman41a11e42022-06-22 09:27:08 +0200191API int
192nc_server_ssh_set_pam_conf_path(const char *conf_name, const char *conf_dir)
193{
Michal Vasko0d81c572022-09-26 10:39:31 +0200194#ifdef HAVE_LIBPAM
roman41a11e42022-06-22 09:27:08 +0200195 free(server_opts.conf_name);
196 free(server_opts.conf_dir);
197 server_opts.conf_name = NULL;
198 server_opts.conf_dir = NULL;
199
200 if (conf_dir) {
Michal Vasko0d81c572022-09-26 10:39:31 +0200201# ifdef LIBPAM_HAVE_CONFDIR
roman41a11e42022-06-22 09:27:08 +0200202 server_opts.conf_dir = strdup(conf_dir);
203 if (!(server_opts.conf_dir)) {
204 ERRMEM;
205 return -1;
206 }
Michal Vasko0d81c572022-09-26 10:39:31 +0200207# else
roman41a11e42022-06-22 09:27:08 +0200208 ERR(NULL, "Failed to set PAM config directory because of old version of PAM. "
209 "Put the config file in the system directory (usually /etc/pam.d/).");
210 return -1;
Michal Vasko0d81c572022-09-26 10:39:31 +0200211# endif
roman41a11e42022-06-22 09:27:08 +0200212 }
213
214 if (conf_name) {
215 server_opts.conf_name = strdup(conf_name);
216 if (!(server_opts.conf_name)) {
217 ERRMEM;
218 return -1;
219 }
220 }
221
222 return 0;
Michal Vasko0d81c572022-09-26 10:39:31 +0200223#else
224 (void)conf_name;
225 (void)conf_dir;
226
227 ERR(NULL, "PAM-based SSH authentication is not supported.");
228 return -1;
229#endif
roman41a11e42022-06-22 09:27:08 +0200230}
231
bhart1bb7cdb2018-07-02 15:03:30 -0500232API void
233nc_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 +0200234 void *user_data, void (*free_user_data)(void *user_data))
bhart1bb7cdb2018-07-02 15:03:30 -0500235{
236 server_opts.pubkey_auth_clb = pubkey_auth_clb;
237 server_opts.pubkey_auth_data = user_data;
238 server_opts.pubkey_auth_data_free = free_user_data;
239}
240
Michal Vaskob05053d2016-01-22 16:12:06 +0100241API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200242nc_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 +0100243{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100244 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200245 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200246 struct nc_ch_endpt *endpt;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100247
Michal Vasko2e6defd2016-10-07 15:48:15 +0200248 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200249 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
250 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200251 return -1;
252 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200253
Michal Vaskoadf30f02019-06-24 09:34:47 +0200254 ret = nc_server_ssh_add_hostkey(name, idx, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200255
Michal Vasko2e6defd2016-10-07 15:48:15 +0200256 /* UNLOCK */
257 nc_server_ch_client_unlock(client);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200258
259 return ret;
260}
261
Michal Vasko4c1fb492017-01-30 14:31:07 +0100262API void
263nc_server_ssh_set_hostkey_clb(int (*hostkey_clb)(const char *name, void *user_data, char **privkey_path,
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200264 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 +0100265{
266 if (!hostkey_clb) {
267 ERRARG("hostkey_clb");
268 return;
269 }
270
271 server_opts.hostkey_clb = hostkey_clb;
272 server_opts.hostkey_data = user_data;
273 server_opts.hostkey_data_free = free_user_data;
274}
275
Michal Vaskoe2713da2016-08-22 16:06:40 +0200276static int
Michal Vasko7d255882017-02-09 13:35:08 +0100277nc_server_ssh_del_hostkey(const char *name, int16_t idx, struct nc_server_ssh_opts *opts)
Michal Vaskoe2713da2016-08-22 16:06:40 +0200278{
279 uint8_t i;
280
Michal Vasko7d255882017-02-09 13:35:08 +0100281 if (name && (idx > -1)) {
282 ERRARG("name and idx");
283 return -1;
284 } else if (idx >= opts->hostkey_count) {
285 ERRARG("idx");
286 }
287
288 if (!name && (idx < 0)) {
Michal Vaskoe2713da2016-08-22 16:06:40 +0200289 for (i = 0; i < opts->hostkey_count; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +0100290 free(opts->hostkeys[i]);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200291 }
292 free(opts->hostkeys);
293 opts->hostkeys = NULL;
294 opts->hostkey_count = 0;
Michal Vasko7d255882017-02-09 13:35:08 +0100295 } else if (name) {
Michal Vaskoe2713da2016-08-22 16:06:40 +0200296 for (i = 0; i < opts->hostkey_count; ++i) {
Michal Vasko4c1fb492017-01-30 14:31:07 +0100297 if (!strcmp(opts->hostkeys[i], name)) {
Michal Vasko7d255882017-02-09 13:35:08 +0100298 idx = i;
299 goto remove_idx;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200300 }
301 }
302
Michal Vasko7d255882017-02-09 13:35:08 +0100303 ERRARG("name");
Michal Vaskoe2713da2016-08-22 16:06:40 +0200304 return -1;
Michal Vasko7d255882017-02-09 13:35:08 +0100305 } else {
306remove_idx:
307 --opts->hostkey_count;
Michal Vasko93224072021-11-09 12:14:28 +0100308 free(opts->hostkeys[idx]);
Michal Vasko7d255882017-02-09 13:35:08 +0100309 if (idx < opts->hostkey_count - 1) {
310 memmove(opts->hostkeys + idx, opts->hostkeys + idx + 1, (opts->hostkey_count - idx) * sizeof *opts->hostkeys);
311 }
312 if (!opts->hostkey_count) {
313 free(opts->hostkeys);
314 opts->hostkeys = NULL;
315 }
Michal Vaskoe2713da2016-08-22 16:06:40 +0200316 }
317
318 return 0;
319}
320
321API int
Michal Vasko7d255882017-02-09 13:35:08 +0100322nc_server_ssh_endpt_del_hostkey(const char *endpt_name, const char *name, int16_t idx)
Michal Vaskoe2713da2016-08-22 16:06:40 +0200323{
324 int ret;
325 struct nc_endpt *endpt;
326
327 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100328 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200329 if (!endpt) {
330 return -1;
331 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200332
Michal Vasko7d255882017-02-09 13:35:08 +0100333 ret = nc_server_ssh_del_hostkey(name, idx, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200334
Michal Vaskoe2713da2016-08-22 16:06:40 +0200335 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100336 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200337
338 return ret;
339}
340
341API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200342nc_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 +0200343{
344 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200345 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200346 struct nc_ch_endpt *endpt;
Michal Vaskoe2713da2016-08-22 16:06:40 +0200347
Michal Vasko2e6defd2016-10-07 15:48:15 +0200348 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200349 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
350 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200351 return -1;
352 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200353
Michal Vaskoadf30f02019-06-24 09:34:47 +0200354 ret = nc_server_ssh_del_hostkey(name, idx, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200355
Michal Vasko2e6defd2016-10-07 15:48:15 +0200356 /* UNLOCK */
357 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100358
359 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100360}
361
362static int
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100363nc_server_ssh_mov_hostkey(const char *key_mov, const char *key_after, struct nc_server_ssh_opts *opts)
364{
365 uint8_t i;
366 int16_t mov_idx = -1, after_idx = -1;
Michal Vasko93224072021-11-09 12:14:28 +0100367 char *bckup;
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100368
369 if (!key_mov) {
370 ERRARG("key_mov");
371 return -1;
372 }
373
374 for (i = 0; i < opts->hostkey_count; ++i) {
375 if (key_after && (after_idx == -1) && !strcmp(opts->hostkeys[i], key_after)) {
376 after_idx = i;
377 }
378 if ((mov_idx == -1) && !strcmp(opts->hostkeys[i], key_mov)) {
379 mov_idx = i;
380 }
381
382 if ((!key_after || (after_idx > -1)) && (mov_idx > -1)) {
383 break;
384 }
385 }
386
387 if (key_after && (after_idx == -1)) {
388 ERRARG("key_after");
389 return -1;
390 }
391 if (mov_idx == -1) {
392 ERRARG("key_mov");
393 return -1;
394 }
395 if ((mov_idx == after_idx) || (mov_idx == after_idx + 1)) {
396 /* nothing to do */
397 return 0;
398 }
399
400 /* finally move the key */
401 bckup = opts->hostkeys[mov_idx];
402 if (mov_idx > after_idx) {
403 memmove(opts->hostkeys + after_idx + 2, opts->hostkeys + after_idx + 1,
404 ((mov_idx - after_idx) - 1) * sizeof *opts->hostkeys);
405 opts->hostkeys[after_idx + 1] = bckup;
406 } else {
407 memmove(opts->hostkeys + mov_idx, opts->hostkeys + mov_idx + 1, (after_idx - mov_idx) * sizeof *opts->hostkeys);
408 opts->hostkeys[after_idx] = bckup;
409 }
410
411 return 0;
412}
413
414API int
415nc_server_ssh_endpt_mov_hostkey(const char *endpt_name, const char *key_mov, const char *key_after)
416{
417 int ret;
418 struct nc_endpt *endpt;
419
420 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100421 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100422 if (!endpt) {
423 return -1;
424 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200425
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100426 ret = nc_server_ssh_mov_hostkey(key_mov, key_after, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200427
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100428 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100429 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100430
431 return ret;
432}
433
434API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200435nc_server_ssh_ch_client_endpt_mov_hostkey(const char *client_name, const char *endpt_name, const char *key_mov,
436 const char *key_after)
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100437{
438 int ret;
439 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200440 struct nc_ch_endpt *endpt;
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100441
442 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200443 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100444 if (!endpt) {
445 return -1;
446 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200447
Michal Vaskoadf30f02019-06-24 09:34:47 +0200448 ret = nc_server_ssh_mov_hostkey(key_mov, key_after, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200449
Michal Vaskofbfe8b62017-02-14 10:22:30 +0100450 /* UNLOCK */
451 nc_server_ch_client_unlock(client);
452
453 return ret;
454}
455
456static int
Michal Vasko3031aae2016-01-27 16:07:18 +0100457nc_server_ssh_set_auth_methods(int auth_methods, struct nc_server_ssh_opts *opts)
Michal Vaskob05053d2016-01-22 16:12:06 +0100458{
Michal Vasko1c2d2652023-10-17 08:53:36 +0200459 if ((auth_methods & NC_SSH_AUTH_INTERACTIVE) && !server_opts.conf_name && !server_opts.interactive_auth_clb &&
460 !server_opts.interactive_auth_sess_clb) {
roman41a11e42022-06-22 09:27:08 +0200461 /* path to a configuration file not set */
Michal Vasko1c2d2652023-10-17 08:53:36 +0200462 ERR(NULL, "To use Keyboard-Interactive authentication method, set the PAM configuration file or a callback.");
roman41a11e42022-06-22 09:27:08 +0200463 return 1;
464 }
Michal Vaskob05053d2016-01-22 16:12:06 +0100465 opts->auth_methods = auth_methods;
466 return 0;
467}
468
469API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100470nc_server_ssh_endpt_set_auth_methods(const char *endpt_name, int auth_methods)
Michal Vaskob05053d2016-01-22 16:12:06 +0100471{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100472 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100473 struct nc_endpt *endpt;
474
Michal Vasko51e514d2016-02-02 15:51:52 +0100475 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100476 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100477 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100478 return -1;
479 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200480
Michal Vasko2e6defd2016-10-07 15:48:15 +0200481 ret = nc_server_ssh_set_auth_methods(auth_methods, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200482
Michal Vasko51e514d2016-02-02 15:51:52 +0100483 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100484 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100485
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100486 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100487}
488
489API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200490nc_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 +0100491{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100492 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200493 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200494 struct nc_ch_endpt *endpt;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100495
Michal Vasko2e6defd2016-10-07 15:48:15 +0200496 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200497 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
498 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200499 return -1;
500 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200501
Michal Vaskoadf30f02019-06-24 09:34:47 +0200502 ret = nc_server_ssh_set_auth_methods(auth_methods, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200503
Michal Vasko2e6defd2016-10-07 15:48:15 +0200504 /* UNLOCK */
505 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100506
507 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100508}
509
Michal Vaskoddce1212019-05-24 09:58:49 +0200510API int
511nc_server_ssh_endpt_get_auth_methods(const char *endpt_name)
512{
513 int ret;
514 struct nc_endpt *endpt;
515
516 /* LOCK */
517 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
518 if (!endpt) {
519 return -1;
520 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200521
Michal Vaskoddce1212019-05-24 09:58:49 +0200522 ret = endpt->opts.ssh->auth_methods;
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200523
Michal Vaskoddce1212019-05-24 09:58:49 +0200524 /* UNLOCK */
525 pthread_rwlock_unlock(&server_opts.endpt_lock);
526
527 return ret;
528}
529
530API int
Michal Vaskoadf30f02019-06-24 09:34:47 +0200531nc_server_ssh_ch_client_endpt_get_auth_methods(const char *client_name, const char *endpt_name)
Michal Vaskoddce1212019-05-24 09:58:49 +0200532{
533 int ret;
534 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200535 struct nc_ch_endpt *endpt;
Michal Vaskoddce1212019-05-24 09:58:49 +0200536
537 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200538 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
539 if (!endpt) {
Michal Vaskoddce1212019-05-24 09:58:49 +0200540 return -1;
541 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200542
Michal Vaskoadf30f02019-06-24 09:34:47 +0200543 ret = endpt->opts.ssh->auth_methods;
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200544
Michal Vaskoddce1212019-05-24 09:58:49 +0200545 /* UNLOCK */
546 nc_server_ch_client_unlock(client);
547
548 return ret;
549}
550
Michal Vaskob05053d2016-01-22 16:12:06 +0100551static int
Michal Vasko3031aae2016-01-27 16:07:18 +0100552nc_server_ssh_set_auth_attempts(uint16_t auth_attempts, struct nc_server_ssh_opts *opts)
Michal Vaskob05053d2016-01-22 16:12:06 +0100553{
roman40672412023-05-04 11:10:22 +0200554 NC_CHECK_ARG_RET(NULL, auth_attempts, -1);
Michal Vaskob05053d2016-01-22 16:12:06 +0100555
Michal Vaskob05053d2016-01-22 16:12:06 +0100556 opts->auth_attempts = auth_attempts;
Michal Vasko086311b2016-01-08 09:53:11 +0100557 return 0;
558}
559
560API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100561nc_server_ssh_endpt_set_auth_attempts(const char *endpt_name, uint16_t auth_attempts)
Michal Vasko086311b2016-01-08 09:53:11 +0100562{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100563 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100564 struct nc_endpt *endpt;
565
Michal Vasko51e514d2016-02-02 15:51:52 +0100566 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100567 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100568 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100569 return -1;
570 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200571
Michal Vasko2e6defd2016-10-07 15:48:15 +0200572 ret = nc_server_ssh_set_auth_attempts(auth_attempts, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200573
Michal Vasko51e514d2016-02-02 15:51:52 +0100574 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100575 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100576
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100577 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100578}
579
580API int
Michal Vaskocbad4c52019-06-27 16:30:35 +0200581nc_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 +0100582{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100583 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200584 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200585 struct nc_ch_endpt *endpt;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100586
Michal Vasko2e6defd2016-10-07 15:48:15 +0200587 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200588 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
589 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200590 return -1;
591 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200592
Michal Vaskoadf30f02019-06-24 09:34:47 +0200593 ret = nc_server_ssh_set_auth_attempts(auth_attempts, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200594
Michal Vasko2e6defd2016-10-07 15:48:15 +0200595 /* UNLOCK */
596 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100597
598 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100599}
600
601static int
Michal Vasko3031aae2016-01-27 16:07:18 +0100602nc_server_ssh_set_auth_timeout(uint16_t auth_timeout, struct nc_server_ssh_opts *opts)
Michal Vaskob05053d2016-01-22 16:12:06 +0100603{
roman40672412023-05-04 11:10:22 +0200604 NC_CHECK_ARG_RET(NULL, auth_timeout, -1);
Michal Vasko086311b2016-01-08 09:53:11 +0100605
Michal Vaskob05053d2016-01-22 16:12:06 +0100606 opts->auth_timeout = auth_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100607 return 0;
608}
609
610API int
Michal Vasko3031aae2016-01-27 16:07:18 +0100611nc_server_ssh_endpt_set_auth_timeout(const char *endpt_name, uint16_t auth_timeout)
Michal Vasko086311b2016-01-08 09:53:11 +0100612{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100613 int ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100614 struct nc_endpt *endpt;
615
Michal Vasko51e514d2016-02-02 15:51:52 +0100616 /* LOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100617 endpt = nc_server_endpt_lock_get(endpt_name, NC_TI_LIBSSH, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100618 if (!endpt) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100619 return -1;
620 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200621
Michal Vasko2e6defd2016-10-07 15:48:15 +0200622 ret = nc_server_ssh_set_auth_timeout(auth_timeout, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200623
Michal Vasko51e514d2016-02-02 15:51:52 +0100624 /* UNLOCK */
Michal Vaskoade892d2017-02-22 13:40:35 +0100625 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100626
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100627 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100628}
629
630API int
Michal Vaskocbad4c52019-06-27 16:30:35 +0200631nc_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 +0100632{
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100633 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200634 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +0200635 struct nc_ch_endpt *endpt;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100636
Michal Vasko2e6defd2016-10-07 15:48:15 +0200637 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +0200638 endpt = nc_server_ch_client_lock(client_name, endpt_name, NC_TI_LIBSSH, &client);
639 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200640 return -1;
641 }
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200642
Michal Vaskoadf30f02019-06-24 09:34:47 +0200643 ret = nc_server_ssh_set_auth_timeout(auth_timeout, endpt->opts.ssh);
Michal Vaskoc46b3df2021-07-26 09:30:05 +0200644
Michal Vasko2e6defd2016-10-07 15:48:15 +0200645 /* UNLOCK */
646 nc_server_ch_client_unlock(client);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100647
648 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +0100649}
650
651static int
Michal Vasko77367452021-02-16 16:32:18 +0100652_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 +0100653{
Michal Vasko09111892021-07-26 09:30:20 +0200654 int ret = 0;
655
Michal Vaskoa05c7b12017-01-30 14:33:08 +0100656 /* LOCK */
657 pthread_mutex_lock(&server_opts.authkey_lock);
658
Michal Vasko17dfda92016-12-01 14:06:16 +0100659 ++server_opts.authkey_count;
660 server_opts.authkeys = nc_realloc(server_opts.authkeys, server_opts.authkey_count * sizeof *server_opts.authkeys);
661 if (!server_opts.authkeys) {
662 ERRMEM;
Michal Vasko09111892021-07-26 09:30:20 +0200663 ret = -1;
664 goto cleanup;
Michal Vasko17dfda92016-12-01 14:06:16 +0100665 }
Michal Vasko93224072021-11-09 12:14:28 +0100666 server_opts.authkeys[server_opts.authkey_count - 1].path = pubkey_path ? strdup(pubkey_path) : NULL;
667 server_opts.authkeys[server_opts.authkey_count - 1].base64 = pubkey_base64 ? strdup(pubkey_base64) : NULL;
Michal Vasko17dfda92016-12-01 14:06:16 +0100668 server_opts.authkeys[server_opts.authkey_count - 1].type = type;
Michal Vasko93224072021-11-09 12:14:28 +0100669 server_opts.authkeys[server_opts.authkey_count - 1].username = strdup(username);
Michal Vasko17dfda92016-12-01 14:06:16 +0100670
Michal Vasko09111892021-07-26 09:30:20 +0200671cleanup:
Michal Vaskoa05c7b12017-01-30 14:33:08 +0100672 /* UNLOCK */
673 pthread_mutex_unlock(&server_opts.authkey_lock);
Michal Vasko09111892021-07-26 09:30:20 +0200674 return ret;
Michal Vasko17dfda92016-12-01 14:06:16 +0100675}
676
677API int
678nc_server_ssh_add_authkey_path(const char *pubkey_path, const char *username)
Michal Vaskob05053d2016-01-22 16:12:06 +0100679{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200680 if (!pubkey_path) {
681 ERRARG("pubkey_path");
682 return -1;
683 } else if (!username) {
684 ERRARG("username");
Michal Vasko086311b2016-01-08 09:53:11 +0100685 return -1;
686 }
687
Michal Vasko17dfda92016-12-01 14:06:16 +0100688 return _nc_server_ssh_add_authkey(pubkey_path, NULL, 0, username);
Michal Vasko086311b2016-01-08 09:53:11 +0100689}
690
691API int
Michal Vasko17dfda92016-12-01 14:06:16 +0100692nc_server_ssh_add_authkey(const char *pubkey_base64, NC_SSH_KEY_TYPE type, const char *username)
Michal Vasko086311b2016-01-08 09:53:11 +0100693{
Michal Vasko17dfda92016-12-01 14:06:16 +0100694 if (!pubkey_base64) {
695 ERRARG("pubkey_base64");
696 return -1;
697 } else if (!type) {
698 ERRARG("type");
699 return -1;
700 } else if (!username) {
701 ERRARG("username");
Michal Vasko3031aae2016-01-27 16:07:18 +0100702 return -1;
703 }
704
Michal Vasko17dfda92016-12-01 14:06:16 +0100705 return _nc_server_ssh_add_authkey(NULL, pubkey_base64, type, username);
Michal Vasko086311b2016-01-08 09:53:11 +0100706}
707
708API int
Michal Vasko17dfda92016-12-01 14:06:16 +0100709nc_server_ssh_del_authkey(const char *pubkey_path, const char *pubkey_base64, NC_SSH_KEY_TYPE type,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200710 const char *username)
Michal Vaskob05053d2016-01-22 16:12:06 +0100711{
Michal Vasko086311b2016-01-08 09:53:11 +0100712 uint32_t i;
713 int ret = -1;
714
Michal Vasko17dfda92016-12-01 14:06:16 +0100715 /* LOCK */
716 pthread_mutex_lock(&server_opts.authkey_lock);
717
718 if (!pubkey_path && !pubkey_base64 && !type && !username) {
719 for (i = 0; i < server_opts.authkey_count; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +0100720 free(server_opts.authkeys[i].path);
721 free(server_opts.authkeys[i].base64);
722 free(server_opts.authkeys[i].username);
Michal Vasko086311b2016-01-08 09:53:11 +0100723
Michal Vasko086311b2016-01-08 09:53:11 +0100724 ret = 0;
725 }
Michal Vasko17dfda92016-12-01 14:06:16 +0100726 free(server_opts.authkeys);
727 server_opts.authkeys = NULL;
728 server_opts.authkey_count = 0;
Michal Vasko1a38c862016-01-15 15:50:07 +0100729 } else {
Michal Vasko17dfda92016-12-01 14:06:16 +0100730 for (i = 0; i < server_opts.authkey_count; ++i) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200731 if ((!pubkey_path || !strcmp(server_opts.authkeys[i].path, pubkey_path)) &&
732 (!pubkey_base64 || !strcmp(server_opts.authkeys[i].base64, pubkey_base64)) &&
733 (!type || (server_opts.authkeys[i].type == type)) &&
734 (!username || !strcmp(server_opts.authkeys[i].username, username))) {
Michal Vasko93224072021-11-09 12:14:28 +0100735 free(server_opts.authkeys[i].path);
736 free(server_opts.authkeys[i].base64);
737 free(server_opts.authkeys[i].username);
Michal Vasko1a38c862016-01-15 15:50:07 +0100738
Michal Vasko17dfda92016-12-01 14:06:16 +0100739 --server_opts.authkey_count;
740 if (i < server_opts.authkey_count) {
741 memcpy(&server_opts.authkeys[i], &server_opts.authkeys[server_opts.authkey_count],
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200742 sizeof *server_opts.authkeys);
Michal Vasko17dfda92016-12-01 14:06:16 +0100743 } else if (!server_opts.authkey_count) {
744 free(server_opts.authkeys);
745 server_opts.authkeys = NULL;
Michal Vaskoc0256492016-02-02 12:19:06 +0100746 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100747
748 ret = 0;
749 }
750 }
Michal Vasko086311b2016-01-08 09:53:11 +0100751 }
752
Michal Vasko51e514d2016-02-02 15:51:52 +0100753 /* UNLOCK */
Michal Vasko17dfda92016-12-01 14:06:16 +0100754 pthread_mutex_unlock(&server_opts.authkey_lock);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100755
756 return ret;
Michal Vasko3031aae2016-01-27 16:07:18 +0100757}
758
759void
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100760nc_server_ssh_clear_opts(struct nc_server_ssh_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +0100761{
Michal Vasko7d255882017-02-09 13:35:08 +0100762 nc_server_ssh_del_hostkey(NULL, -1, opts);
Michal Vaskob05053d2016-01-22 16:12:06 +0100763}
764
Michal Vasko7a866152021-07-22 11:01:13 +0200765#ifdef HAVE_SHADOW
766
Michal Vaskof3c41e32022-09-09 11:22:21 +0200767/**
768 * @brief Get passwd entry for a user.
769 *
770 * @param[in] username Name of the user.
771 * @param[in] pwd_buf Passwd entry buffer.
772 * @param[in,out] buf Passwd entry string buffer.
773 * @param[in,out] buf_size Current @p buf size.
774 * @return Found passwd entry for the user, NULL if none found.
775 */
Michal Vasko7a866152021-07-22 11:01:13 +0200776static struct passwd *
777auth_password_getpwnam(const char *username, struct passwd *pwd_buf, char **buf, size_t *buf_size)
778{
779 struct passwd *pwd = NULL;
780 char *mem;
Jan Kundrátaa323102021-10-08 20:10:50 +0200781 int r = 0;
Michal Vasko7a866152021-07-22 11:01:13 +0200782
783 do {
Jan Kundrátaa323102021-10-08 20:10:50 +0200784 r = getpwnam_r(username, pwd_buf, *buf, *buf_size, &pwd);
Michal Vasko7a866152021-07-22 11:01:13 +0200785 if (pwd) {
786 /* entry found */
787 break;
788 }
789
Jan Kundrátaa323102021-10-08 20:10:50 +0200790 if (r == ERANGE) {
Michal Vasko7a866152021-07-22 11:01:13 +0200791 /* small buffer, enlarge */
792 *buf_size <<= 2;
793 mem = realloc(*buf, *buf_size);
794 if (!mem) {
795 ERRMEM;
796 return NULL;
797 }
798 *buf = mem;
799 }
Jan Kundrátaa323102021-10-08 20:10:50 +0200800 } while (r == ERANGE);
Michal Vasko7a866152021-07-22 11:01:13 +0200801
802 return pwd;
803}
804
Michal Vaskof3c41e32022-09-09 11:22:21 +0200805/**
806 * @brief Get shadow entry for a user.
807 *
808 * @param[in] username Name of the user.
809 * @param[in] spwd_buf Shadow entry buffer.
810 * @param[in,out] buf Shadow entry string buffer.
811 * @param[in,out] buf_size Current @p buf size.
812 * @return Found shadow entry for the user, NULL if none found.
813 */
Michal Vasko7a866152021-07-22 11:01:13 +0200814static struct spwd *
815auth_password_getspnam(const char *username, struct spwd *spwd_buf, char **buf, size_t *buf_size)
816{
817 struct spwd *spwd = NULL;
818 char *mem;
Jan Kundrátaa323102021-10-08 20:10:50 +0200819 int r = 0;
Michal Vasko7a866152021-07-22 11:01:13 +0200820
821 do {
Michal Vasko7a866152021-07-22 11:01:13 +0200822# ifndef __QNXNTO__
Jan Kundrátaa323102021-10-08 20:10:50 +0200823 r = getspnam_r(username, spwd_buf, *buf, *buf_size, &spwd);
Michal Vasko7a866152021-07-22 11:01:13 +0200824# else
825 spwd = getspnam_r(username, spwd_buf, *buf, *buf_size);
826# endif
827 if (spwd) {
828 /* entry found */
829 break;
830 }
831
Jan Kundrátaa323102021-10-08 20:10:50 +0200832 if (r == ERANGE) {
Michal Vasko7a866152021-07-22 11:01:13 +0200833 /* small buffer, enlarge */
834 *buf_size <<= 2;
835 mem = realloc(*buf, *buf_size);
836 if (!mem) {
837 ERRMEM;
838 return NULL;
839 }
840 *buf = mem;
841 }
Jan Kundrátaa323102021-10-08 20:10:50 +0200842 } while (r == ERANGE);
Michal Vasko7a866152021-07-22 11:01:13 +0200843
844 return spwd;
845}
846
Michal Vaskof3c41e32022-09-09 11:22:21 +0200847/**
848 * @brief Get hashed system apssword for a user.
849 *
850 * @param[in] username Name of the user.
851 * @return Hashed password of @p username.
852 */
Michal Vasko086311b2016-01-08 09:53:11 +0100853static char *
854auth_password_get_pwd_hash(const char *username)
855{
856 struct passwd *pwd, pwd_buf;
857 struct spwd *spwd, spwd_buf;
Michal Vasko7a866152021-07-22 11:01:13 +0200858 char *pass_hash = NULL, *buf = NULL;
859 size_t buf_size = 256;
Michal Vasko086311b2016-01-08 09:53:11 +0100860
Michal Vasko7a866152021-07-22 11:01:13 +0200861 buf = malloc(buf_size);
862 if (!buf) {
863 ERRMEM;
864 goto error;
865 }
866
867 pwd = auth_password_getpwnam(username, &pwd_buf, &buf, &buf_size);
Michal Vasko086311b2016-01-08 09:53:11 +0100868 if (!pwd) {
Michal Vasko05532772021-06-03 12:12:38 +0200869 VRB(NULL, "User \"%s\" not found locally.", username);
Michal Vasko7a866152021-07-22 11:01:13 +0200870 goto error;
Michal Vasko086311b2016-01-08 09:53:11 +0100871 }
872
873 if (!strcmp(pwd->pw_passwd, "x")) {
Michal Vasko7a866152021-07-22 11:01:13 +0200874 spwd = auth_password_getspnam(username, &spwd_buf, &buf, &buf_size);
Michal Vasko086311b2016-01-08 09:53:11 +0100875 if (!spwd) {
Michal Vasko05532772021-06-03 12:12:38 +0200876 VRB(NULL, "Failed to retrieve the shadow entry for \"%s\".", username);
Michal Vasko7a866152021-07-22 11:01:13 +0200877 goto error;
Michal Vasko22b4fe72020-11-04 08:52:29 +0100878 } else if ((spwd->sp_expire > -1) && (spwd->sp_expire <= (time(NULL) / (60 * 60 * 24)))) {
Michal Vasko05532772021-06-03 12:12:38 +0200879 WRN(NULL, "User \"%s\" account has expired.", username);
Michal Vasko7a866152021-07-22 11:01:13 +0200880 goto error;
Michal Vasko086311b2016-01-08 09:53:11 +0100881 }
882
883 pass_hash = spwd->sp_pwdp;
884 } else {
885 pass_hash = pwd->pw_passwd;
886 }
887
888 if (!pass_hash) {
Michal Vasko05532772021-06-03 12:12:38 +0200889 ERR(NULL, "No password could be retrieved for \"%s\".", username);
Michal Vasko7a866152021-07-22 11:01:13 +0200890 goto error;
Michal Vasko086311b2016-01-08 09:53:11 +0100891 }
892
893 /* check the hash structure for special meaning */
894 if (!strcmp(pass_hash, "*") || !strcmp(pass_hash, "!")) {
Michal Vasko05532772021-06-03 12:12:38 +0200895 VRB(NULL, "User \"%s\" is not allowed to authenticate using a password.", username);
Michal Vasko7a866152021-07-22 11:01:13 +0200896 goto error;
Michal Vasko086311b2016-01-08 09:53:11 +0100897 }
898 if (!strcmp(pass_hash, "*NP*")) {
Michal Vasko05532772021-06-03 12:12:38 +0200899 VRB(NULL, "Retrieving password for \"%s\" from a NIS+ server not supported.", username);
Michal Vasko7a866152021-07-22 11:01:13 +0200900 goto error;
Michal Vasko086311b2016-01-08 09:53:11 +0100901 }
902
Michal Vasko963f6c02021-07-26 15:55:44 +0200903 pass_hash = strdup(pass_hash);
Michal Vasko7a866152021-07-22 11:01:13 +0200904 free(buf);
Michal Vasko963f6c02021-07-26 15:55:44 +0200905 return pass_hash;
Michal Vasko7a866152021-07-22 11:01:13 +0200906
907error:
908 free(buf);
909 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100910}
911
Michal Vasko7a866152021-07-22 11:01:13 +0200912#else
913
Michal Vaskof3c41e32022-09-09 11:22:21 +0200914/**
915 * @brief Get hashed system password for a user.
916 *
917 * @param[in] username Name of the user.
918 * @return Hashed password of @p username.
919 */
Michal Vasko7a866152021-07-22 11:01:13 +0200920static char *
921auth_password_get_pwd_hash(const char *username)
922{
923 (void)username;
924 return strdup("");
925}
926
927#endif
928
Michal Vaskof3c41e32022-09-09 11:22:21 +0200929/**
930 * @brief Compare hashed password with a cleartext password for a match.
931 *
932 * @param[in] pass_hash Hashed password.
933 * @param[in] pass_clear Cleartext password.
934 * @return 0 on match.
935 * @return non-zero if not a match.
936 */
Michal Vasko086311b2016-01-08 09:53:11 +0100937static int
938auth_password_compare_pwd(const char *pass_hash, const char *pass_clear)
939{
940 char *new_pass_hash;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200941
Michal Vaskof3c41e32022-09-09 11:22:21 +0200942#ifdef HAVE_CRYPT_R
Michal Vasko086311b2016-01-08 09:53:11 +0100943 struct crypt_data cdata;
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200944#endif
Michal Vasko086311b2016-01-08 09:53:11 +0100945
946 if (!pass_hash[0]) {
947 if (!pass_clear[0]) {
Michal Vasko05532772021-06-03 12:12:38 +0200948 WRN(NULL, "User authentication successful with an empty password!");
Michal Vasko086311b2016-01-08 09:53:11 +0100949 return 0;
950 } else {
951 /* the user did now know he does not need any password,
952 * (which should not be used) so deny authentication */
953 return 1;
954 }
955 }
956
Michal Vaskof3c41e32022-09-09 11:22:21 +0200957#ifdef HAVE_CRYPT_R
Michal Vasko086311b2016-01-08 09:53:11 +0100958 cdata.initialized = 0;
959 new_pass_hash = crypt_r(pass_clear, pass_hash, &cdata);
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200960#else
Mislav Novakovicce9a7ef2017-08-08 13:45:52 +0200961 pthread_mutex_lock(&crypt_lock);
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200962 new_pass_hash = crypt(pass_clear, pass_hash);
Mislav Novakovicce9a7ef2017-08-08 13:45:52 +0200963 pthread_mutex_unlock(&crypt_lock);
Mislav Novakovicebf4bd72017-08-02 10:43:41 +0200964#endif
Andrew Langefeld158d6fd2018-06-11 18:51:44 -0500965
966 if (!new_pass_hash) {
967 return 1;
968 }
969
Michal Vasko086311b2016-01-08 09:53:11 +0100970 return strcmp(new_pass_hash, pass_hash);
971}
972
973static void
974nc_sshcb_auth_password(struct nc_session *session, ssh_message msg)
975{
976 char *pass_hash;
Michal Vaskoebba7602018-03-23 13:14:08 +0100977 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +0100978
Michal Vaskoebba7602018-03-23 13:14:08 +0100979 if (server_opts.passwd_auth_clb) {
980 auth_ret = server_opts.passwd_auth_clb(session, ssh_message_auth_password(msg), server_opts.passwd_auth_data);
981 } else {
982 pass_hash = auth_password_get_pwd_hash(session->username);
983 if (pass_hash) {
984 auth_ret = auth_password_compare_pwd(pass_hash, ssh_message_auth_password(msg));
985 free(pass_hash);
986 }
Michal Vasko086311b2016-01-08 09:53:11 +0100987 }
988
Michal Vaskoebba7602018-03-23 13:14:08 +0100989 if (!auth_ret) {
990 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
Michal Vasko05532772021-06-03 12:12:38 +0200991 VRB(session, "User \"%s\" authenticated.", session->username);
Michal Vaskoebba7602018-03-23 13:14:08 +0100992 ssh_message_auth_reply_success(msg, 0);
993 } else {
994 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +0200995 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
996 session->opts.server.ssh_auth_attempts);
Michal Vaskoebba7602018-03-23 13:14:08 +0100997 ssh_message_reply_default(msg);
998 }
Michal Vasko086311b2016-01-08 09:53:11 +0100999}
1000
Michal Vasko0d81c572022-09-26 10:39:31 +02001001#ifdef HAVE_LIBPAM
1002
roman41a11e42022-06-22 09:27:08 +02001003/**
1004 * @brief PAM conversation function, which serves as a callback for exchanging messages between the client and a PAM module.
1005 *
1006 * @param[in] n_messages Number of messages.
1007 * @param[in] msg PAM module's messages.
1008 * @param[out] resp User responses.
1009 * @param[in] appdata_ptr Callback's data.
1010 * @return PAM_SUCCESS on success;
1011 * @return PAM_BUF_ERR on memory allocation error;
1012 * @return PAM_CONV_ERR otherwise.
1013 */
1014static int
1015nc_pam_conv_clb(int n_messages, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)
1016{
1017 int i, j, t, r = PAM_SUCCESS, n_answers, n_requests = n_messages;
1018 const char **prompts = NULL;
1019 char *echo = NULL;
1020 const char *name = "Keyboard-Interactive Authentication";
1021 const char *instruction = "Please enter your authentication token";
1022 ssh_message reply = NULL;
1023 struct nc_pam_thread_arg *clb_data = appdata_ptr;
1024 ssh_session libssh_session;
1025 struct timespec ts_timeout;
1026 struct nc_server_ssh_opts *opts;
1027
1028 libssh_session = clb_data->session->ti.libssh.session;
1029 opts = clb_data->session->data;
1030
1031 /* PAM_MAX_NUM_MSG == 32 by default */
1032 if ((n_messages <= 0) || (n_messages >= PAM_MAX_NUM_MSG)) {
1033 ERR(NULL, "Bad number of PAM messages (#%d).", n_messages);
1034 r = PAM_CONV_ERR;
1035 goto cleanup;
1036 }
1037
1038 /* only accepting these 4 types of messages */
1039 for (i = 0; i < n_messages; i++) {
1040 t = msg[i]->msg_style;
1041 if ((t != PAM_PROMPT_ECHO_OFF) && (t != PAM_PROMPT_ECHO_ON) && (t != PAM_TEXT_INFO) && (t != PAM_ERROR_MSG)) {
1042 ERR(NULL, "PAM conversation callback received an unexpected type of message.");
1043 r = PAM_CONV_ERR;
1044 goto cleanup;
1045 }
1046 }
1047
1048 /* display messages with errors and/or some information and count the amount of actual authentication challenges */
1049 for (i = 0; i < n_messages; i++) {
1050 if (msg[i]->msg_style == PAM_TEXT_INFO) {
1051 VRB(NULL, "PAM conversation callback received a message with some information for the client (%s).", msg[i]->msg);
1052 n_requests--;
1053 }
1054 if (msg[i]->msg_style == PAM_ERROR_MSG) {
1055 ERR(NULL, "PAM conversation callback received an error message (%s).", msg[i]->msg);
1056 r = PAM_CONV_ERR;
1057 goto cleanup;
1058 }
1059 }
1060
1061 /* there are no requests left for the user, only messages with some information for the client were sent */
1062 if (n_requests <= 0) {
1063 r = PAM_SUCCESS;
1064 goto cleanup;
1065 }
1066
1067 /* it is the PAM module's responsibility to release both, this array and the responses themselves */
1068 *resp = calloc(n_requests, sizeof **resp);
1069 prompts = calloc(n_requests, sizeof *prompts);
1070 echo = calloc(n_requests, sizeof *echo);
1071 if (!(*resp) || !prompts || !echo) {
1072 ERRMEM;
1073 r = PAM_BUF_ERR;
1074 goto cleanup;
1075 }
1076
1077 /* set the prompts for the user */
1078 j = 0;
1079 for (i = 0; i < n_messages; i++) {
1080 if ((msg[i]->msg_style == PAM_PROMPT_ECHO_ON) || (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF)) {
1081 prompts[j++] = msg[i]->msg;
1082 }
1083 }
1084
1085 /* iterate over all the messages and adjust the echo array accordingly */
1086 j = 0;
1087 for (i = 0; i < n_messages; i++) {
1088 if (msg[i]->msg_style == PAM_PROMPT_ECHO_ON) {
1089 echo[j++] = 1;
1090 }
1091 if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF) {
1092 /* no need to set to 0 because of calloc */
1093 j++;
1094 }
1095 }
1096
1097 /* print all the keyboard-interactive challenges to the user */
1098 r = ssh_message_auth_interactive_request(clb_data->msg, name, instruction, n_requests, prompts, echo);
1099 if (r != SSH_OK) {
1100 ERR(NULL, "Failed to send an authentication request.");
1101 r = PAM_CONV_ERR;
1102 goto cleanup;
1103 }
1104
1105 if (opts->auth_timeout) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001106 nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000);
roman41a11e42022-06-22 09:27:08 +02001107 }
1108
1109 /* get user's replies */
1110 do {
1111 if (!nc_session_is_connected(clb_data->session)) {
1112 ERR(NULL, "Communication SSH socket unexpectedly closed.");
1113 r = PAM_CONV_ERR;
1114 goto cleanup;
1115 }
1116
1117 reply = ssh_message_get(libssh_session);
1118 if (reply) {
1119 break;
1120 }
1121
1122 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +01001123 } while ((opts->auth_timeout) && (nc_timeouttime_cur_diff(&ts_timeout) >= 1));
roman41a11e42022-06-22 09:27:08 +02001124
1125 if (!reply) {
1126 ERR(NULL, "Authentication timeout.");
1127 r = PAM_CONV_ERR;
1128 goto cleanup;
1129 }
1130
1131 /* check if the amount of replies matches the amount of requests */
1132 n_answers = ssh_userauth_kbdint_getnanswers(libssh_session);
1133 if (n_answers != n_requests) {
1134 ERR(NULL, "Expected %d response(s), got %d.", n_requests, n_answers);
1135 r = PAM_CONV_ERR;
1136 goto cleanup;
1137 }
1138
1139 /* give the replies to a PAM module */
1140 for (i = 0; i < n_answers; i++) {
1141 (*resp)[i].resp = strdup(ssh_userauth_kbdint_getanswer(libssh_session, i));
1142 /* it should be the caller's responsibility to free this, however if mem alloc fails,
1143 * it is safer to free the responses here and set them to NULL */
1144 if ((*resp)[i].resp == NULL) {
1145 for (j = 0; j < i; j++) {
1146 free((*resp)[j].resp);
1147 (*resp)[j].resp = NULL;
1148 }
1149 ERRMEM;
1150 r = PAM_BUF_ERR;
1151 goto cleanup;
1152 }
1153 }
1154
1155cleanup:
1156 ssh_message_free(reply);
1157 free(prompts);
1158 free(echo);
1159 return r;
1160}
1161
1162/**
1163 * @brief Handles authentication via Linux PAM.
1164 *
1165 * @param[in] session NETCONF session.
1166 * @param[in] ssh_msg SSH message with a keyboard-interactive authentication request.
1167 * @return PAM_SUCCESS on success;
1168 * @return PAM error otherwise.
1169 */
1170static int
1171nc_pam_auth(struct nc_session *session, ssh_message ssh_msg)
1172{
1173 pam_handle_t *pam_h = NULL;
1174 int ret;
1175 struct nc_pam_thread_arg clb_data;
1176 struct pam_conv conv;
1177
1178 /* structure holding callback's data */
1179 clb_data.msg = ssh_msg;
1180 clb_data.session = session;
1181
1182 /* PAM conversation structure holding the callback and it's data */
1183 conv.conv = nc_pam_conv_clb;
1184 conv.appdata_ptr = &clb_data;
1185
1186 /* initialize PAM and see if the given configuration file exists */
Michal Vasko0d81c572022-09-26 10:39:31 +02001187# ifdef LIBPAM_HAVE_CONFDIR
roman41a11e42022-06-22 09:27:08 +02001188 /* PAM version >= 1.4 */
1189 ret = pam_start_confdir(server_opts.conf_name, session->username, &conv, server_opts.conf_dir, &pam_h);
Michal Vasko0d81c572022-09-26 10:39:31 +02001190# else
roman41a11e42022-06-22 09:27:08 +02001191 /* PAM version < 1.4 */
1192 ret = pam_start(server_opts.conf_name, session->username, &conv, &pam_h);
Michal Vasko0d81c572022-09-26 10:39:31 +02001193# endif
roman41a11e42022-06-22 09:27:08 +02001194 if (ret != PAM_SUCCESS) {
1195 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
1196 goto cleanup;
1197 }
1198
1199 /* authentication based on the modules listed in the configuration file */
1200 ret = pam_authenticate(pam_h, 0);
1201 if (ret != PAM_SUCCESS) {
1202 if (ret == PAM_ABORT) {
1203 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
1204 goto cleanup;
1205 } else {
1206 VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
1207 goto cleanup;
1208 }
1209 }
1210
1211 /* correct token entered, check other requirements(the time of the day, expired token, ...) */
1212 ret = pam_acct_mgmt(pam_h, 0);
1213 if ((ret != PAM_SUCCESS) && (ret != PAM_NEW_AUTHTOK_REQD)) {
1214 VRB(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
1215 goto cleanup;
1216 }
1217
1218 /* if a token has expired a new one will be generated */
1219 if (ret == PAM_NEW_AUTHTOK_REQD) {
1220 VRB(NULL, "PAM warning occurred (%s).\n", pam_strerror(pam_h, ret));
1221 ret = pam_chauthtok(pam_h, PAM_CHANGE_EXPIRED_AUTHTOK);
1222 if (ret == PAM_SUCCESS) {
1223 VRB(NULL, "The authentication token of user \"%s\" updated successfully.", session->username);
1224 } else {
1225 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
1226 goto cleanup;
1227 }
1228 }
1229
1230cleanup:
1231 /* destroy the PAM context */
1232 if (pam_end(pam_h, ret) != PAM_SUCCESS) {
1233 ERR(NULL, "PAM error occurred (%s).\n", pam_strerror(pam_h, ret));
1234 }
1235 return ret;
1236}
1237
Michal Vasko0d81c572022-09-26 10:39:31 +02001238#endif
1239
Michal Vasko086311b2016-01-08 09:53:11 +01001240static void
1241nc_sshcb_auth_kbdint(struct nc_session *session, ssh_message msg)
1242{
bhart3bc2f582018-06-05 12:40:32 -05001243 int auth_ret = 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001244
Michal Vasko1c2d2652023-10-17 08:53:36 +02001245 if (server_opts.interactive_auth_sess_clb) {
1246 auth_ret = server_opts.interactive_auth_sess_clb(session, session->ti.libssh.session, msg,
1247 server_opts.interactive_auth_data);
1248 } else if (server_opts.interactive_auth_clb) {
Michal Vasko733c0bd2018-07-03 13:14:40 +02001249 auth_ret = server_opts.interactive_auth_clb(session, msg, server_opts.interactive_auth_data);
Michal Vasko0d81c572022-09-26 10:39:31 +02001250 } else {
1251#ifdef HAVE_LIBPAM
1252 if (nc_pam_auth(session, msg) == PAM_SUCCESS) {
1253 auth_ret = 0;
1254 }
1255#else
1256 ERR(session, "PAM-based SSH authentication is not supported.");
1257#endif
bhart1bb7cdb2018-07-02 15:03:30 -05001258 }
1259
Robert Vargaad7a5532018-08-10 20:40:54 +02001260 /* We have already sent a reply */
1261 if (auth_ret == -1) {
1262 return;
1263 }
1264
bhart1bb7cdb2018-07-02 15:03:30 -05001265 /* Authenticate message based on outcome */
1266 if (!auth_ret) {
1267 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
Michal Vasko05532772021-06-03 12:12:38 +02001268 VRB(session, "User \"%s\" authenticated.", session->username);
bhart1bb7cdb2018-07-02 15:03:30 -05001269 ssh_message_auth_reply_success(msg, 0);
1270 } else {
1271 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +02001272 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
1273 session->opts.server.ssh_auth_attempts);
bhart1bb7cdb2018-07-02 15:03:30 -05001274 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001275 }
1276}
1277
Michal Vaskof3c41e32022-09-09 11:22:21 +02001278/**
1279 * @brief Compare SSH key with configured authorized keys and return the username of the matching one, if any.
1280 *
1281 * @param[in] key Presented SSH key to compare.
1282 * @return Authorized key username, NULL if no match was found.
1283 */
Michal Vasko086311b2016-01-08 09:53:11 +01001284static const char *
Michal Vasko17dfda92016-12-01 14:06:16 +01001285auth_pubkey_compare_key(ssh_key key)
Michal Vasko086311b2016-01-08 09:53:11 +01001286{
1287 uint32_t i;
1288 ssh_key pub_key;
Michal Vasko76e3a352016-01-18 09:07:00 +01001289 const char *username = NULL;
Michal Vasko3e9d1682017-02-24 09:50:15 +01001290 int ret = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001291
Michal Vaskoa05c7b12017-01-30 14:33:08 +01001292 /* LOCK */
1293 pthread_mutex_lock(&server_opts.authkey_lock);
1294
Michal Vasko17dfda92016-12-01 14:06:16 +01001295 for (i = 0; i < server_opts.authkey_count; ++i) {
1296 switch (server_opts.authkeys[i].type) {
1297 case NC_SSH_KEY_UNKNOWN:
1298 ret = ssh_pki_import_pubkey_file(server_opts.authkeys[i].path, &pub_key);
1299 break;
1300 case NC_SSH_KEY_DSA:
1301 ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_DSS, &pub_key);
1302 break;
1303 case NC_SSH_KEY_RSA:
1304 ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_RSA, &pub_key);
1305 break;
1306 case NC_SSH_KEY_ECDSA:
1307 ret = ssh_pki_import_pubkey_base64(server_opts.authkeys[i].base64, SSH_KEYTYPE_ECDSA, &pub_key);
1308 break;
1309 }
1310
Michal Vasko7abcdeb2016-05-30 15:27:00 +02001311 if (ret == SSH_EOF) {
Michal Vasko05532772021-06-03 12:12:38 +02001312 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 +02001313 continue;
1314 } else if (ret == SSH_ERROR) {
Michal Vasko05532772021-06-03 12:12:38 +02001315 WRN(NULL, "Failed to import a public key of \"%s\" (SSH error).", server_opts.authkeys[i].username);
Michal Vasko086311b2016-01-08 09:53:11 +01001316 continue;
1317 }
1318
1319 if (!ssh_key_cmp(key, pub_key, SSH_KEY_CMP_PUBLIC)) {
1320 ssh_key_free(pub_key);
1321 break;
1322 }
1323
1324 ssh_key_free(pub_key);
1325 }
1326
Michal Vasko17dfda92016-12-01 14:06:16 +01001327 if (i < server_opts.authkey_count) {
1328 username = server_opts.authkeys[i].username;
Michal Vasko086311b2016-01-08 09:53:11 +01001329 }
1330
Michal Vaskoa05c7b12017-01-30 14:33:08 +01001331 /* UNLOCK */
1332 pthread_mutex_unlock(&server_opts.authkey_lock);
1333
Michal Vasko086311b2016-01-08 09:53:11 +01001334 return username;
1335}
1336
1337static void
1338nc_sshcb_auth_pubkey(struct nc_session *session, ssh_message msg)
1339{
1340 const char *username;
1341 int signature_state;
1342
Michal Vasko733c0bd2018-07-03 13:14:40 +02001343 if (server_opts.pubkey_auth_clb) {
1344 if (server_opts.pubkey_auth_clb(session, ssh_message_auth_pubkey(msg), server_opts.pubkey_auth_data)) {
bhart3bc2f582018-06-05 12:40:32 -05001345 goto fail;
1346 }
Michal Vasko733c0bd2018-07-03 13:14:40 +02001347 } else {
bhart3bc2f582018-06-05 12:40:32 -05001348 if ((username = auth_pubkey_compare_key(ssh_message_auth_pubkey(msg))) == NULL) {
Michal Vasko05532772021-06-03 12:12:38 +02001349 VRB(session, "User \"%s\" tried to use an unknown (unauthorized) public key.", session->username);
bhart3bc2f582018-06-05 12:40:32 -05001350 goto fail;
1351 } else if (strcmp(session->username, username)) {
Michal Vasko05532772021-06-03 12:12:38 +02001352 VRB(session, "User \"%s\" is not the username identified with the presented public key.", session->username);
bhart3bc2f582018-06-05 12:40:32 -05001353 goto fail;
1354 }
Michal Vaskobd13a932016-09-14 09:00:35 +02001355 }
Michal Vaskobd13a932016-09-14 09:00:35 +02001356
Michal Vasko086311b2016-01-08 09:53:11 +01001357 signature_state = ssh_message_auth_publickey_state(msg);
1358 if (signature_state == SSH_PUBLICKEY_STATE_VALID) {
Michal Vasko05532772021-06-03 12:12:38 +02001359 VRB(session, "User \"%s\" authenticated.", session->username);
Michal Vasko086311b2016-01-08 09:53:11 +01001360 session->flags |= NC_SESSION_SSH_AUTHENTICATED;
1361 ssh_message_auth_reply_success(msg, 0);
Michal Vasko086311b2016-01-08 09:53:11 +01001362 } else if (signature_state == SSH_PUBLICKEY_STATE_NONE) {
Michal Vaskobd13a932016-09-14 09:00:35 +02001363 /* accepting only the use of a public key */
1364 ssh_message_auth_reply_pk_ok_simple(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001365 }
1366
Michal Vaskobd13a932016-09-14 09:00:35 +02001367 return;
1368
1369fail:
Michal Vasko2e6defd2016-10-07 15:48:15 +02001370 ++session->opts.server.ssh_auth_attempts;
Michal Vasko05532772021-06-03 12:12:38 +02001371 VRB(session, "Failed user \"%s\" authentication attempt (#%d).", session->username,
1372 session->opts.server.ssh_auth_attempts);
Michal Vasko086311b2016-01-08 09:53:11 +01001373 ssh_message_reply_default(msg);
1374}
1375
1376static int
Michal Vasko96164bf2016-01-21 15:41:58 +01001377nc_sshcb_channel_open(struct nc_session *session, ssh_message msg)
Michal Vasko086311b2016-01-08 09:53:11 +01001378{
Michal Vasko96164bf2016-01-21 15:41:58 +01001379 ssh_channel chan;
1380
1381 /* first channel request */
1382 if (!session->ti.libssh.channel) {
1383 if (session->status != NC_STATUS_STARTING) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001384 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001385 return -1;
1386 }
Michal Vasko96164bf2016-01-21 15:41:58 +01001387 chan = ssh_message_channel_request_open_reply_accept(msg);
1388 if (!chan) {
Michal Vasko05532772021-06-03 12:12:38 +02001389 ERR(session, "Failed to create a new SSH channel.");
Michal Vasko96164bf2016-01-21 15:41:58 +01001390 return -1;
1391 }
1392 session->ti.libssh.channel = chan;
Michal Vasko086311b2016-01-08 09:53:11 +01001393
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001394 /* additional channel request */
Michal Vasko96164bf2016-01-21 15:41:58 +01001395 } else {
1396 chan = ssh_message_channel_request_open_reply_accept(msg);
1397 if (!chan) {
Michal Vasko05532772021-06-03 12:12:38 +02001398 ERR(session, "Session %u: failed to create a new SSH channel.", session->id);
Michal Vasko96164bf2016-01-21 15:41:58 +01001399 return -1;
1400 }
1401 /* channel was created and libssh stored it internally in the ssh_session structure, good enough */
Michal Vasko086311b2016-01-08 09:53:11 +01001402 }
1403
Michal Vasko086311b2016-01-08 09:53:11 +01001404 return 0;
1405}
1406
1407static int
1408nc_sshcb_channel_subsystem(struct nc_session *session, ssh_channel channel, const char *subsystem)
1409{
Michal Vasko96164bf2016-01-21 15:41:58 +01001410 struct nc_session *new_session;
Michal Vasko086311b2016-01-08 09:53:11 +01001411
Michal Vasko96164bf2016-01-21 15:41:58 +01001412 if (strcmp(subsystem, "netconf")) {
Michal Vasko05532772021-06-03 12:12:38 +02001413 WRN(session, "Received an unknown subsystem \"%s\" request.", subsystem);
Michal Vasko086311b2016-01-08 09:53:11 +01001414 return -1;
1415 }
1416
Michal Vasko96164bf2016-01-21 15:41:58 +01001417 if (session->ti.libssh.channel == channel) {
1418 /* first channel requested */
1419 if (session->ti.libssh.next || (session->status != NC_STATUS_STARTING)) {
1420 ERRINT;
1421 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +01001422 }
Michal Vasko96164bf2016-01-21 15:41:58 +01001423 if (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF) {
Michal Vasko05532772021-06-03 12:12:38 +02001424 ERR(session, "Subsystem \"netconf\" requested for the second time.");
Michal Vasko96164bf2016-01-21 15:41:58 +01001425 return -1;
1426 }
1427
1428 session->flags |= NC_SESSION_SSH_SUBSYS_NETCONF;
Michal Vasko086311b2016-01-08 09:53:11 +01001429 } else {
Michal Vasko96164bf2016-01-21 15:41:58 +01001430 /* additional channel subsystem request, new session is ready as far as SSH is concerned */
Michal Vasko131120a2018-05-29 15:44:02 +02001431 new_session = nc_new_session(NC_SERVER, 1);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001432 if (!new_session) {
1433 ERRMEM;
1434 return -1;
1435 }
Michal Vasko96164bf2016-01-21 15:41:58 +01001436
1437 /* insert the new session */
1438 if (!session->ti.libssh.next) {
1439 new_session->ti.libssh.next = session;
1440 } else {
1441 new_session->ti.libssh.next = session->ti.libssh.next;
1442 }
1443 session->ti.libssh.next = new_session;
1444
1445 new_session->status = NC_STATUS_STARTING;
Michal Vasko96164bf2016-01-21 15:41:58 +01001446 new_session->ti_type = NC_TI_LIBSSH;
Michal Vasko131120a2018-05-29 15:44:02 +02001447 new_session->io_lock = session->io_lock;
Michal Vasko96164bf2016-01-21 15:41:58 +01001448 new_session->ti.libssh.channel = channel;
1449 new_session->ti.libssh.session = session->ti.libssh.session;
Michal Vasko93224072021-11-09 12:14:28 +01001450 new_session->username = strdup(session->username);
1451 new_session->host = strdup(session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +01001452 new_session->port = session->port;
Michal Vasko93224072021-11-09 12:14:28 +01001453 new_session->ctx = (struct ly_ctx *)session->ctx;
Michal Vasko83d15322018-09-27 09:44:02 +02001454 new_session->flags = NC_SESSION_SSH_AUTHENTICATED | NC_SESSION_SSH_SUBSYS_NETCONF | NC_SESSION_SHAREDCTX;
Michal Vasko086311b2016-01-08 09:53:11 +01001455 }
1456
1457 return 0;
1458}
1459
Michal Vasko96164bf2016-01-21 15:41:58 +01001460int
Michal Vaskob48aa812016-01-18 14:13:09 +01001461nc_sshcb_msg(ssh_session UNUSED(sshsession), ssh_message msg, void *data)
Michal Vasko086311b2016-01-08 09:53:11 +01001462{
1463 const char *str_type, *str_subtype = NULL, *username;
1464 int subtype, type;
1465 struct nc_session *session = (struct nc_session *)data;
Michal Vasko086311b2016-01-08 09:53:11 +01001466
1467 type = ssh_message_type(msg);
1468 subtype = ssh_message_subtype(msg);
1469
1470 switch (type) {
1471 case SSH_REQUEST_AUTH:
1472 str_type = "request-auth";
1473 switch (subtype) {
1474 case SSH_AUTH_METHOD_NONE:
1475 str_subtype = "none";
1476 break;
1477 case SSH_AUTH_METHOD_PASSWORD:
1478 str_subtype = "password";
1479 break;
1480 case SSH_AUTH_METHOD_PUBLICKEY:
1481 str_subtype = "publickey";
1482 break;
1483 case SSH_AUTH_METHOD_HOSTBASED:
1484 str_subtype = "hostbased";
1485 break;
1486 case SSH_AUTH_METHOD_INTERACTIVE:
1487 str_subtype = "interactive";
1488 break;
1489 case SSH_AUTH_METHOD_GSSAPI_MIC:
1490 str_subtype = "gssapi-mic";
1491 break;
1492 }
1493 break;
1494
1495 case SSH_REQUEST_CHANNEL_OPEN:
1496 str_type = "request-channel-open";
1497 switch (subtype) {
1498 case SSH_CHANNEL_SESSION:
1499 str_subtype = "session";
1500 break;
1501 case SSH_CHANNEL_DIRECT_TCPIP:
1502 str_subtype = "direct-tcpip";
1503 break;
1504 case SSH_CHANNEL_FORWARDED_TCPIP:
1505 str_subtype = "forwarded-tcpip";
1506 break;
1507 case (int)SSH_CHANNEL_X11:
1508 str_subtype = "channel-x11";
1509 break;
1510 case SSH_CHANNEL_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001511 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +01001512 default:
1513 str_subtype = "unknown";
1514 break;
1515 }
1516 break;
1517
1518 case SSH_REQUEST_CHANNEL:
1519 str_type = "request-channel";
1520 switch (subtype) {
1521 case SSH_CHANNEL_REQUEST_PTY:
1522 str_subtype = "pty";
1523 break;
1524 case SSH_CHANNEL_REQUEST_EXEC:
1525 str_subtype = "exec";
1526 break;
1527 case SSH_CHANNEL_REQUEST_SHELL:
1528 str_subtype = "shell";
1529 break;
1530 case SSH_CHANNEL_REQUEST_ENV:
1531 str_subtype = "env";
1532 break;
1533 case SSH_CHANNEL_REQUEST_SUBSYSTEM:
1534 str_subtype = "subsystem";
1535 break;
1536 case SSH_CHANNEL_REQUEST_WINDOW_CHANGE:
1537 str_subtype = "window-change";
1538 break;
1539 case SSH_CHANNEL_REQUEST_X11:
1540 str_subtype = "x11";
1541 break;
1542 case SSH_CHANNEL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001543 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +01001544 default:
1545 str_subtype = "unknown";
1546 break;
1547 }
1548 break;
1549
1550 case SSH_REQUEST_SERVICE:
1551 str_type = "request-service";
1552 str_subtype = ssh_message_service_service(msg);
1553 break;
1554
1555 case SSH_REQUEST_GLOBAL:
1556 str_type = "request-global";
1557 switch (subtype) {
1558 case SSH_GLOBAL_REQUEST_TCPIP_FORWARD:
1559 str_subtype = "tcpip-forward";
1560 break;
1561 case SSH_GLOBAL_REQUEST_CANCEL_TCPIP_FORWARD:
1562 str_subtype = "cancel-tcpip-forward";
1563 break;
1564 case SSH_GLOBAL_REQUEST_UNKNOWN:
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001565 /* fallthrough */
Michal Vasko086311b2016-01-08 09:53:11 +01001566 default:
1567 str_subtype = "unknown";
1568 break;
1569 }
1570 break;
1571
1572 default:
1573 str_type = "unknown";
1574 str_subtype = "unknown";
1575 break;
1576 }
1577
Michal Vasko05532772021-06-03 12:12:38 +02001578 VRB(session, "Received an SSH message \"%s\" of subtype \"%s\".", str_type, str_subtype);
Michal Vasko5e0edd82020-07-29 15:26:13 +02001579 if (!session || (session->status == NC_STATUS_CLOSING) || (session->status == NC_STATUS_INVALID)) {
Michal Vaskoce319162016-02-03 15:33:08 +01001580 /* "valid" situation if, for example, receiving some auth or channel request timeouted,
1581 * but we got it now, during session free */
Michal Vasko05532772021-06-03 12:12:38 +02001582 VRB(session, "SSH message arrived on a %s session, the request will be denied.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001583 (session && session->status == NC_STATUS_CLOSING ? "closing" : "invalid"));
Michal Vaskoce319162016-02-03 15:33:08 +01001584 ssh_message_reply_default(msg);
1585 return 0;
1586 }
Michal Vasko96164bf2016-01-21 15:41:58 +01001587 session->flags |= NC_SESSION_SSH_NEW_MSG;
Michal Vasko086311b2016-01-08 09:53:11 +01001588
1589 /*
1590 * process known messages
1591 */
1592 if (type == SSH_REQUEST_AUTH) {
1593 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko05532772021-06-03 12:12:38 +02001594 ERR(session, "User \"%s\" authenticated, but requested another authentication.", session->username);
Michal Vasko086311b2016-01-08 09:53:11 +01001595 ssh_message_reply_default(msg);
1596 return 0;
1597 }
1598
Michal Vasko086311b2016-01-08 09:53:11 +01001599 /* save the username, do not let the client change it */
1600 username = ssh_message_auth_user(msg);
1601 if (!session->username) {
1602 if (!username) {
Michal Vasko05532772021-06-03 12:12:38 +02001603 ERR(session, "Denying an auth request without a username.");
Michal Vasko086311b2016-01-08 09:53:11 +01001604 return 1;
1605 }
1606
Michal Vasko93224072021-11-09 12:14:28 +01001607 session->username = strdup(username);
Michal Vasko086311b2016-01-08 09:53:11 +01001608 } else if (username) {
1609 if (strcmp(username, session->username)) {
Michal Vasko05532772021-06-03 12:12:38 +02001610 ERR(session, "User \"%s\" changed its username to \"%s\".", session->username, username);
Michal Vasko086311b2016-01-08 09:53:11 +01001611 session->status = NC_STATUS_INVALID;
Michal Vasko428087d2016-01-14 16:04:28 +01001612 session->term_reason = NC_SESSION_TERM_OTHER;
Michal Vasko086311b2016-01-08 09:53:11 +01001613 return 1;
1614 }
1615 }
1616
1617 if (subtype == SSH_AUTH_METHOD_NONE) {
1618 /* libssh will return the supported auth methods */
1619 return 1;
1620 } else if (subtype == SSH_AUTH_METHOD_PASSWORD) {
1621 nc_sshcb_auth_password(session, msg);
1622 return 0;
1623 } else if (subtype == SSH_AUTH_METHOD_PUBLICKEY) {
1624 nc_sshcb_auth_pubkey(session, msg);
1625 return 0;
1626 } else if (subtype == SSH_AUTH_METHOD_INTERACTIVE) {
1627 nc_sshcb_auth_kbdint(session, msg);
1628 return 0;
1629 }
1630 } else if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
Michal Vasko0df67562016-01-21 15:50:11 +01001631 if ((type == SSH_REQUEST_CHANNEL_OPEN) && ((enum ssh_channel_type_e)subtype == SSH_CHANNEL_SESSION)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001632 if (nc_sshcb_channel_open(session, msg)) {
Michal Vasko086311b2016-01-08 09:53:11 +01001633 ssh_message_reply_default(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001634 }
Michal Vasko086311b2016-01-08 09:53:11 +01001635 return 0;
Michal Vasko96164bf2016-01-21 15:41:58 +01001636
Michal Vasko0df67562016-01-21 15:50:11 +01001637 } else if ((type == SSH_REQUEST_CHANNEL) && ((enum ssh_channel_requests_e)subtype == SSH_CHANNEL_REQUEST_SUBSYSTEM)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001638 if (nc_sshcb_channel_subsystem(session, ssh_message_channel_request_channel(msg),
1639 ssh_message_channel_request_subsystem(msg))) {
Michal Vasko086311b2016-01-08 09:53:11 +01001640 ssh_message_reply_default(msg);
Michal Vasko96164bf2016-01-21 15:41:58 +01001641 } else {
1642 ssh_message_channel_request_reply_success(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001643 }
1644 return 0;
1645 }
1646 }
1647
1648 /* we did not process it */
1649 return 1;
1650}
1651
Michal Vasko1a38c862016-01-15 15:50:07 +01001652/* ret 1 on success, 0 on timeout, -1 on error */
Michal Vasko086311b2016-01-08 09:53:11 +01001653static int
Michal Vasko09d700f2022-09-08 10:21:40 +02001654nc_accept_ssh_session_open_netconf_channel(struct nc_session *session, int timeout)
Michal Vasko086311b2016-01-08 09:53:11 +01001655{
Michal Vasko36c7be82017-02-22 13:37:59 +01001656 int ret;
roman6ece9c52022-06-22 09:29:17 +02001657 struct timespec ts_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +01001658
1659 /* message callback is executed twice to give chance for the channel to be
1660 * created if timeout == 0 (it takes 2 messages, channel-open, subsystem-request) */
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001661 if (!timeout) {
Michal Vasko2a7d4732016-01-15 09:24:46 +01001662 if (!nc_session_is_connected(session)) {
Michal Vasko05532772021-06-03 12:12:38 +02001663 ERR(session, "Communication socket unexpectedly closed (libssh).");
Michal Vasko2a7d4732016-01-15 09:24:46 +01001664 return -1;
1665 }
1666
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001667 ret = ssh_execute_message_callbacks(session->ti.libssh.session);
1668 if (ret != SSH_OK) {
Michal Vasko05532772021-06-03 12:12:38 +02001669 ERR(session, "Failed to receive SSH messages on a session (%s).",
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001670 ssh_get_error(session->ti.libssh.session));
Michal Vasko086311b2016-01-08 09:53:11 +01001671 return -1;
1672 }
1673
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001674 if (!session->ti.libssh.channel) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001675 return 0;
1676 }
1677
1678 ret = ssh_execute_message_callbacks(session->ti.libssh.session);
1679 if (ret != SSH_OK) {
Michal Vasko05532772021-06-03 12:12:38 +02001680 ERR(session, "Failed to receive SSH messages on a session (%s).",
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001681 ssh_get_error(session->ti.libssh.session));
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001682 return -1;
1683 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001684
1685 if (!(session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
1686 /* we did not receive subsystem-request, timeout */
1687 return 0;
1688 }
1689
1690 return 1;
1691 }
1692
Michal Vasko36c7be82017-02-22 13:37:59 +01001693 if (timeout > -1) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001694 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001695 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001696 while (1) {
1697 if (!nc_session_is_connected(session)) {
Michal Vasko05532772021-06-03 12:12:38 +02001698 ERR(session, "Communication socket unexpectedly closed (libssh).");
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001699 return -1;
1700 }
1701
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001702 ret = ssh_execute_message_callbacks(session->ti.libssh.session);
1703 if (ret != SSH_OK) {
Michal Vasko05532772021-06-03 12:12:38 +02001704 ERR(session, "Failed to receive SSH messages on a session (%s).",
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001705 ssh_get_error(session->ti.libssh.session));
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001706 return -1;
1707 }
1708
Michal Vasko086311b2016-01-08 09:53:11 +01001709 if (session->ti.libssh.channel && (session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko1a38c862016-01-15 15:50:07 +01001710 return 1;
Michal Vasko086311b2016-01-08 09:53:11 +01001711 }
1712
Michal Vasko086311b2016-01-08 09:53:11 +01001713 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +01001714 if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
roman6ece9c52022-06-22 09:29:17 +02001715 /* timeout */
1716 ERR(session, "Failed to start \"netconf\" SSH subsystem for too long, disconnecting.");
1717 break;
Michal Vasko36c7be82017-02-22 13:37:59 +01001718 }
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001719 }
Michal Vasko086311b2016-01-08 09:53:11 +01001720
Michal Vasko1a38c862016-01-15 15:50:07 +01001721 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001722}
1723
Michal Vaskof3c41e32022-09-09 11:22:21 +02001724/**
1725 * @brief Set hostkeys to be used for an SSH bind.
1726 *
1727 * @param[in] sbind SSH bind to use.
1728 * @param[in] hostkeys Array of hostkeys.
1729 * @param[in] hostkey_count Count of @p hostkeys.
1730 * @return 0 on success.
1731 * @return -1 on error.
1732 */
Michal Vasko4c1fb492017-01-30 14:31:07 +01001733static int
Michal Vasko93224072021-11-09 12:14:28 +01001734nc_ssh_bind_add_hostkeys(ssh_bind sbind, char **hostkeys, uint8_t hostkey_count)
Michal Vasko4c1fb492017-01-30 14:31:07 +01001735{
1736 uint8_t i;
1737 char *privkey_path, *privkey_data;
Michal Vaskoddce1212019-05-24 09:58:49 +02001738 int ret;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001739 NC_SSH_KEY_TYPE privkey_type;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001740
1741 if (!server_opts.hostkey_clb) {
Michal Vasko05532772021-06-03 12:12:38 +02001742 ERR(NULL, "Callback for retrieving SSH host keys not set.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001743 return -1;
1744 }
1745
1746 for (i = 0; i < hostkey_count; ++i) {
1747 privkey_path = privkey_data = NULL;
Michal Vaskoddce1212019-05-24 09:58:49 +02001748 if (server_opts.hostkey_clb(hostkeys[i], server_opts.hostkey_data, &privkey_path, &privkey_data, &privkey_type)) {
Michal Vasko05532772021-06-03 12:12:38 +02001749 ERR(NULL, "Host key callback failed.");
Michal Vasko4c1fb492017-01-30 14:31:07 +01001750 return -1;
1751 }
1752
1753 if (privkey_data) {
Michal Vaskoddce1212019-05-24 09:58:49 +02001754 privkey_path = base64der_key_to_tmp_file(privkey_data, nc_keytype2str(privkey_type));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001755 if (!privkey_path) {
Michal Vasko05532772021-06-03 12:12:38 +02001756 ERR(NULL, "Temporarily storing a host key into a file failed (%s).", strerror(errno));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001757 free(privkey_data);
1758 return -1;
1759 }
1760 }
1761
1762 ret = ssh_bind_options_set(sbind, SSH_BIND_OPTIONS_HOSTKEY, privkey_path);
1763
1764 /* cleanup */
1765 if (privkey_data && unlink(privkey_path)) {
Michal Vasko05532772021-06-03 12:12:38 +02001766 WRN(NULL, "Removing a temporary host key file \"%s\" failed (%s).", privkey_path, strerror(errno));
Michal Vasko4c1fb492017-01-30 14:31:07 +01001767 }
Michal Vasko4c1fb492017-01-30 14:31:07 +01001768 free(privkey_data);
1769
1770 if (ret != SSH_OK) {
Michal Vasko05532772021-06-03 12:12:38 +02001771 ERR(NULL, "Failed to set hostkey \"%s\" (%s).", hostkeys[i], privkey_path);
Michal Vasko80075de2017-07-10 11:38:52 +02001772 }
1773 free(privkey_path);
1774
1775 if (ret != SSH_OK) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001776 return -1;
1777 }
1778 }
1779
1780 return 0;
1781}
1782
Michal Vasko09d700f2022-09-08 10:21:40 +02001783static int
1784nc_accept_ssh_session_auth(struct nc_session *session, const struct nc_server_ssh_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +01001785{
roman6ece9c52022-06-22 09:29:17 +02001786 struct timespec ts_timeout;
roman41a11e42022-06-22 09:27:08 +02001787 ssh_message msg;
Michal Vasko09d700f2022-09-08 10:21:40 +02001788 int libssh_auth_methods = 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001789
Michal Vasko09d700f2022-09-08 10:21:40 +02001790 /* configure accepted auth methods */
Michal Vaskoc61c4492016-01-25 11:13:34 +01001791 if (opts->auth_methods & NC_SSH_AUTH_PUBLICKEY) {
Michal Vasko086311b2016-01-08 09:53:11 +01001792 libssh_auth_methods |= SSH_AUTH_METHOD_PUBLICKEY;
1793 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001794 if (opts->auth_methods & NC_SSH_AUTH_PASSWORD) {
Michal Vasko086311b2016-01-08 09:53:11 +01001795 libssh_auth_methods |= SSH_AUTH_METHOD_PASSWORD;
1796 }
Michal Vaskoc61c4492016-01-25 11:13:34 +01001797 if (opts->auth_methods & NC_SSH_AUTH_INTERACTIVE) {
Michal Vasko086311b2016-01-08 09:53:11 +01001798 libssh_auth_methods |= SSH_AUTH_METHOD_INTERACTIVE;
1799 }
1800 ssh_set_auth_methods(session->ti.libssh.session, libssh_auth_methods);
1801
Michal Vasko086311b2016-01-08 09:53:11 +01001802 /* authenticate */
Michal Vasko36c7be82017-02-22 13:37:59 +01001803 if (opts->auth_timeout) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001804 nc_timeouttime_get(&ts_timeout, opts->auth_timeout * 1000);
Michal Vasko36c7be82017-02-22 13:37:59 +01001805 }
1806 while (1) {
Michal Vasko2a7d4732016-01-15 09:24:46 +01001807 if (!nc_session_is_connected(session)) {
Michal Vasko05532772021-06-03 12:12:38 +02001808 ERR(session, "Communication SSH socket unexpectedly closed.");
Michal Vasko2a7d4732016-01-15 09:24:46 +01001809 return -1;
1810 }
1811
roman41a11e42022-06-22 09:27:08 +02001812 msg = ssh_message_get(session->ti.libssh.session);
1813 if (msg) {
1814 if (nc_sshcb_msg(session->ti.libssh.session, msg, (void *) session)) {
1815 ssh_message_reply_default(msg);
1816 }
1817 ssh_message_free(msg);
Michal Vasko086311b2016-01-08 09:53:11 +01001818 }
1819
Michal Vasko36c7be82017-02-22 13:37:59 +01001820 if (session->flags & NC_SESSION_SSH_AUTHENTICATED) {
1821 break;
1822 }
1823
Michal Vasko145ae672017-02-07 10:57:27 +01001824 if (session->opts.server.ssh_auth_attempts >= opts->auth_attempts) {
Michal Vasko05532772021-06-03 12:12:38 +02001825 ERR(session, "Too many failed authentication attempts of user \"%s\".", session->username);
Michal Vasko145ae672017-02-07 10:57:27 +01001826 return -1;
1827 }
1828
Michal Vasko086311b2016-01-08 09:53:11 +01001829 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +01001830 if ((opts->auth_timeout) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
roman6ece9c52022-06-22 09:29:17 +02001831 /* timeout */
1832 break;
Michal Vasko36c7be82017-02-22 13:37:59 +01001833 }
1834 }
Michal Vasko086311b2016-01-08 09:53:11 +01001835
1836 if (!(session->flags & NC_SESSION_SSH_AUTHENTICATED)) {
1837 /* timeout */
Michal Vaskoc13da702017-02-07 10:57:57 +01001838 if (session->username) {
Michal Vasko05532772021-06-03 12:12:38 +02001839 ERR(session, "User \"%s\" failed to authenticate for too long, disconnecting.", session->username);
Michal Vaskoc13da702017-02-07 10:57:57 +01001840 } else {
Michal Vasko05532772021-06-03 12:12:38 +02001841 ERR(session, "User failed to authenticate for too long, disconnecting.");
Michal Vaskoc13da702017-02-07 10:57:57 +01001842 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001843 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +01001844 }
1845
Michal Vasko09d700f2022-09-08 10:21:40 +02001846 return 1;
1847}
1848
1849int
1850nc_accept_ssh_session(struct nc_session *session, int sock, int timeout)
1851{
1852 ssh_bind sbind = NULL;
1853 struct nc_server_ssh_opts *opts;
1854 int rc = 1, r;
1855 struct timespec ts_timeout;
roman4cc0cd52023-04-14 09:12:29 +02001856 const char *err_msg;
Michal Vasko09d700f2022-09-08 10:21:40 +02001857
1858 opts = session->data;
1859
1860 /* other transport-specific data */
1861 session->ti_type = NC_TI_LIBSSH;
1862 session->ti.libssh.session = ssh_new();
1863 if (!session->ti.libssh.session) {
1864 ERR(NULL, "Failed to initialize a new SSH session.");
1865 rc = -1;
1866 goto cleanup;
1867 }
1868
1869 sbind = ssh_bind_new();
1870 if (!sbind) {
1871 ERR(session, "Failed to create an SSH bind.");
1872 rc = -1;
1873 goto cleanup;
1874 }
1875
1876 /* configure host keys */
1877 if (nc_ssh_bind_add_hostkeys(sbind, opts->hostkeys, opts->hostkey_count)) {
1878 rc = -1;
1879 goto cleanup;
1880 }
1881
1882 /* accept new connection on the bind */
1883 if (ssh_bind_accept_fd(sbind, session->ti.libssh.session, sock) == SSH_ERROR) {
1884 ERR(session, "SSH failed to accept a new connection (%s).", ssh_get_error(sbind));
1885 rc = -1;
1886 goto cleanup;
1887 }
1888 sock = -1;
1889
1890 ssh_set_blocking(session->ti.libssh.session, 0);
1891
1892 if (timeout > -1) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001893 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko09d700f2022-09-08 10:21:40 +02001894 }
1895 while ((r = ssh_handle_key_exchange(session->ti.libssh.session)) == SSH_AGAIN) {
1896 /* this tends to take longer */
1897 usleep(NC_TIMEOUT_STEP * 20);
Michal Vaskod8a74192023-02-06 15:51:50 +01001898 if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
Michal Vasko09d700f2022-09-08 10:21:40 +02001899 break;
1900 }
1901 }
1902 if (r == SSH_AGAIN) {
1903 ERR(session, "SSH key exchange timeout.");
1904 rc = 0;
1905 goto cleanup;
1906 } else if (r != SSH_OK) {
roman4cc0cd52023-04-14 09:12:29 +02001907 err_msg = ssh_get_error(session->ti.libssh.session);
1908 if (err_msg[0] == '\0') {
1909 err_msg = "hostkey algorithm generated from the hostkey most likely not found in the set of configured hostkey algorithms";
1910 }
1911 ERR(session, "SSH key exchange error (%s).", err_msg);
Michal Vasko09d700f2022-09-08 10:21:40 +02001912 rc = -1;
1913 goto cleanup;
1914 }
1915
1916 /* authenticate */
1917 if ((rc = nc_accept_ssh_session_auth(session, opts)) != 1) {
1918 goto cleanup;
1919 }
1920
roman41a11e42022-06-22 09:27:08 +02001921 /* set the message callback after a successful authentication */
1922 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, session);
1923
Michal Vasko09d700f2022-09-08 10:21:40 +02001924 /* remember that this session was just set as nc_sshcb_msg() parameter */
1925 session->flags |= NC_SESSION_SSH_MSG_CB;
1926
1927 /* open channel and request 'netconf' subsystem */
1928 if ((rc = nc_accept_ssh_session_open_netconf_channel(session, timeout)) != 1) {
1929 goto cleanup;
Michal Vasko086311b2016-01-08 09:53:11 +01001930 }
1931
Michal Vasko09d700f2022-09-08 10:21:40 +02001932 /* all SSH messages were processed */
Michal Vasko96164bf2016-01-21 15:41:58 +01001933 session->flags &= ~NC_SESSION_SSH_NEW_MSG;
Michal Vasko09d700f2022-09-08 10:21:40 +02001934
1935cleanup:
1936 if (sock > -1) {
1937 close(sock);
1938 }
1939 ssh_bind_free(sbind);
1940 return rc;
Michal Vasko086311b2016-01-08 09:53:11 +01001941}
1942
Michal Vasko71090fc2016-05-24 16:37:28 +02001943API NC_MSG_TYPE
1944nc_session_accept_ssh_channel(struct nc_session *orig_session, struct nc_session **session)
1945{
1946 NC_MSG_TYPE msgtype;
1947 struct nc_session *new_session = NULL;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001948 struct timespec ts_cur;
Michal Vasko71090fc2016-05-24 16:37:28 +02001949
roman40672412023-05-04 11:10:22 +02001950 NC_CHECK_ARG_RET(orig_session, orig_session, session, NC_MSG_ERROR);
Michal Vasko71090fc2016-05-24 16:37:28 +02001951
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001952 if ((orig_session->status == NC_STATUS_RUNNING) && (orig_session->ti_type == NC_TI_LIBSSH) &&
1953 orig_session->ti.libssh.next) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001954 for (new_session = orig_session->ti.libssh.next;
1955 new_session != orig_session;
1956 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001957 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
1958 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001959 /* we found our session */
1960 break;
1961 }
1962 }
1963 if (new_session == orig_session) {
1964 new_session = NULL;
1965 }
1966 }
1967
1968 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02001969 ERR(orig_session, "Session does not have a NETCONF SSH channel ready.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001970 return NC_MSG_ERROR;
1971 }
1972
1973 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02001974 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001975
1976 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02001977 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02001978 if (msgtype != NC_MSG_HELLO) {
1979 return msgtype;
1980 }
1981
Michal Vaskod8a74192023-02-06 15:51:50 +01001982 nc_realtime_get(&ts_cur);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001983 new_session->opts.server.session_start = ts_cur.tv_sec;
Michal Vaskod8a74192023-02-06 15:51:50 +01001984 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9f6275e2017-10-05 13:50:05 +02001985 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko71090fc2016-05-24 16:37:28 +02001986 new_session->status = NC_STATUS_RUNNING;
1987 *session = new_session;
1988
1989 return msgtype;
1990}
1991
1992API NC_MSG_TYPE
Michal Vasko96164bf2016-01-21 15:41:58 +01001993nc_ps_accept_ssh_channel(struct nc_pollsession *ps, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +01001994{
Michal Vaskobdcf2362016-07-26 11:35:43 +02001995 uint8_t q_id;
Michal Vasko71090fc2016-05-24 16:37:28 +02001996 NC_MSG_TYPE msgtype;
Michal Vaskoe4300a82017-05-24 10:35:42 +02001997 struct nc_session *new_session = NULL, *cur_session;
Michal Vasko9f6275e2017-10-05 13:50:05 +02001998 struct timespec ts_cur;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001999 uint16_t i;
Michal Vasko086311b2016-01-08 09:53:11 +01002000
roman40672412023-05-04 11:10:22 +02002001 NC_CHECK_ARG_RET(NULL, ps, session, NC_MSG_ERROR);
Michal Vasko086311b2016-01-08 09:53:11 +01002002
Michal Vasko48a63ed2016-03-01 09:48:21 +01002003 /* LOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02002004 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02002005 return NC_MSG_ERROR;
Michal Vaskof04a52a2016-04-07 10:52:10 +02002006 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01002007
Michal Vasko96164bf2016-01-21 15:41:58 +01002008 for (i = 0; i < ps->session_count; ++i) {
fanchanghu3d4e7212017-08-09 09:42:30 +08002009 cur_session = ps->sessions[i]->session;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002010 if ((cur_session->status == NC_STATUS_RUNNING) && (cur_session->ti_type == NC_TI_LIBSSH) &&
2011 cur_session->ti.libssh.next) {
Michal Vasko96164bf2016-01-21 15:41:58 +01002012 /* an SSH session with more channels */
Michal Vaskoe4300a82017-05-24 10:35:42 +02002013 for (new_session = cur_session->ti.libssh.next;
2014 new_session != cur_session;
Michal Vasko96164bf2016-01-21 15:41:58 +01002015 new_session = new_session->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002016 if ((new_session->status == NC_STATUS_STARTING) && new_session->ti.libssh.channel &&
2017 (new_session->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01002018 /* we found our session */
2019 break;
2020 }
2021 }
Michal Vaskoe4300a82017-05-24 10:35:42 +02002022 if (new_session != cur_session) {
Michal Vasko96164bf2016-01-21 15:41:58 +01002023 break;
2024 }
Michal Vaskofb89d772016-01-08 12:25:35 +01002025
Michal Vasko96164bf2016-01-21 15:41:58 +01002026 new_session = NULL;
2027 }
2028 }
Michal Vaskofb89d772016-01-08 12:25:35 +01002029
Michal Vasko48a63ed2016-03-01 09:48:21 +01002030 /* UNLOCK */
Michal Vasko227f8ff2016-07-26 14:08:59 +02002031 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01002032
Michal Vasko96164bf2016-01-21 15:41:58 +01002033 if (!new_session) {
Michal Vasko05532772021-06-03 12:12:38 +02002034 ERR(NULL, "No session with a NETCONF SSH channel ready was found.");
Michal Vasko71090fc2016-05-24 16:37:28 +02002035 return NC_MSG_ERROR;
Michal Vasko96164bf2016-01-21 15:41:58 +01002036 }
2037
2038 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002039 new_session->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vaskofb89d772016-01-08 12:25:35 +01002040
Michal Vasko086311b2016-01-08 09:53:11 +01002041 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02002042 msgtype = nc_handshake_io(new_session);
Michal Vasko71090fc2016-05-24 16:37:28 +02002043 if (msgtype != NC_MSG_HELLO) {
2044 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01002045 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01002046
Michal Vaskod8a74192023-02-06 15:51:50 +01002047 nc_realtime_get(&ts_cur);
Michal Vasko9f6275e2017-10-05 13:50:05 +02002048 new_session->opts.server.session_start = ts_cur.tv_sec;
Michal Vaskod8a74192023-02-06 15:51:50 +01002049 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9f6275e2017-10-05 13:50:05 +02002050 new_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vasko086311b2016-01-08 09:53:11 +01002051 new_session->status = NC_STATUS_RUNNING;
Michal Vasko96164bf2016-01-21 15:41:58 +01002052 *session = new_session;
Michal Vasko086311b2016-01-08 09:53:11 +01002053
Michal Vasko71090fc2016-05-24 16:37:28 +02002054 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +01002055}