blob: 8b7d1f4e59c376070bb4cbd7556ed19fff8cd0c6 [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
Michal Vasko95ea9ff2021-11-09 12:29:14 +01002 * @file session_server.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libnetconf2 server session manipulation functions
Michal Vasko086311b2016-01-08 09:53:11 +01005 *
Michal Vasko95ea9ff2021-11-09 12:29:14 +01006 * @copyright
7 * Copyright (c) 2015 - 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 */
apropp-molex4e903c32020-04-20 03:06:58 -040015#define _QNX_SOURCE /* getpeereid */
Olivier Matzac7fa2f2018-10-11 10:02:04 +020016#define _GNU_SOURCE /* signals, threads, SO_PEERCRED */
Michal Vasko086311b2016-01-08 09:53:11 +010017
Michal Vaskob83a3fa2021-05-26 09:53:42 +020018#include <arpa/inet.h>
Michal Vasko77e83572022-07-21 15:31:15 +020019#include <assert.h>
Michal Vasko086311b2016-01-08 09:53:11 +010020#include <errno.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020021#include <fcntl.h>
Olivier Matzac7fa2f2018-10-11 10:02:04 +020022#include <netinet/in.h>
23#include <netinet/tcp.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020024#include <poll.h>
Michal Vaskob48aa812016-01-18 14:13:09 +010025#include <pthread.h>
Olivier Matzac7fa2f2018-10-11 10:02:04 +020026#include <pwd.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020027#include <signal.h>
28#include <stdint.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sys/socket.h>
32#include <sys/types.h>
33#include <sys/un.h>
34#include <time.h>
35#include <unistd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010036
Michal Vasko7a20d2e2021-05-19 16:40:23 +020037#include "compat.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010038#include "libnetconf.h"
Michal Vasko086311b2016-01-08 09:53:11 +010039#include "session_server.h"
Michal Vasko0bdf70b2019-06-24 19:20:20 +020040#include "session_server_ch.h"
Michal Vasko086311b2016-01-08 09:53:11 +010041
Michal Vaskob48aa812016-01-18 14:13:09 +010042struct nc_server_opts server_opts = {
Michal Vaskoade892d2017-02-22 13:40:35 +010043#ifdef NC_ENABLED_SSH
44 .authkey_lock = PTHREAD_MUTEX_INITIALIZER,
45#endif
46 .bind_lock = PTHREAD_MUTEX_INITIALIZER,
Michal Vasko2e6defd2016-10-07 15:48:15 +020047 .endpt_lock = PTHREAD_RWLOCK_INITIALIZER,
48 .ch_client_lock = PTHREAD_RWLOCK_INITIALIZER
Michal Vaskob48aa812016-01-18 14:13:09 +010049};
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010050
fanchanghu966f2de2016-07-21 02:28:57 -040051static nc_rpc_clb global_rpc_clb = NULL;
52
Michal Vasko3031aae2016-01-27 16:07:18 +010053struct nc_endpt *
Michal Vaskoade892d2017-02-22 13:40:35 +010054nc_server_endpt_lock_get(const char *name, NC_TRANSPORT_IMPL ti, uint16_t *idx)
Michal Vasko3031aae2016-01-27 16:07:18 +010055{
56 uint16_t i;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010057 struct nc_endpt *endpt = NULL;
58
Michal Vaskoddce1212019-05-24 09:58:49 +020059 if (!name) {
60 ERRARG("endpt_name");
61 return NULL;
62 }
63
Michal Vaskoade892d2017-02-22 13:40:35 +010064 /* WRITE LOCK */
65 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010066
67 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vasko2e6defd2016-10-07 15:48:15 +020068 if (!strcmp(server_opts.endpts[i].name, name) && (!ti || (server_opts.endpts[i].ti == ti))) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010069 endpt = &server_opts.endpts[i];
70 break;
Michal Vasko3031aae2016-01-27 16:07:18 +010071 }
72 }
73
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010074 if (!endpt) {
Michal Vasko05532772021-06-03 12:12:38 +020075 ERR(NULL, "Endpoint \"%s\" was not found.", name);
Michal Vaskoade892d2017-02-22 13:40:35 +010076 /* UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +020077 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010078 return NULL;
79 }
80
Michal Vaskoe2713da2016-08-22 16:06:40 +020081 if (idx) {
82 *idx = i;
83 }
84
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010085 return endpt;
86}
87
Michal Vaskoadf30f02019-06-24 09:34:47 +020088struct nc_ch_endpt *
89nc_server_ch_client_lock(const char *name, const char *endpt_name, NC_TRANSPORT_IMPL ti, struct nc_ch_client **client_p)
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010090{
Michal Vaskoadf30f02019-06-24 09:34:47 +020091 uint16_t i, j;
Michal Vasko2e6defd2016-10-07 15:48:15 +020092 struct nc_ch_client *client = NULL;
Michal Vaskoadf30f02019-06-24 09:34:47 +020093 struct nc_ch_endpt *endpt = NULL;
94
95 *client_p = NULL;
Michal Vasko2e6defd2016-10-07 15:48:15 +020096
Michal Vaskoddce1212019-05-24 09:58:49 +020097 if (!name) {
98 ERRARG("client_name");
99 return NULL;
100 }
101
Michal Vasko2e6defd2016-10-07 15:48:15 +0200102 /* READ LOCK */
103 pthread_rwlock_rdlock(&server_opts.ch_client_lock);
104
105 for (i = 0; i < server_opts.ch_client_count; ++i) {
Michal Vaskoadf30f02019-06-24 09:34:47 +0200106 if (!strcmp(server_opts.ch_clients[i].name, name)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200107 client = &server_opts.ch_clients[i];
Michal Vaskoadf30f02019-06-24 09:34:47 +0200108 if (!endpt_name && !ti) {
109 /* return only client */
110 break;
111 }
112 for (j = 0; j < client->ch_endpt_count; ++j) {
Michal Vasko530d95c2021-05-28 13:32:02 +0200113 if ((!endpt_name || !strcmp(client->ch_endpts[j].name, endpt_name)) &&
114 (!ti || (ti == client->ch_endpts[j].ti))) {
Michal Vaskoadf30f02019-06-24 09:34:47 +0200115 endpt = &client->ch_endpts[j];
116 break;
117 }
118 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200119 break;
120 }
121 }
122
123 if (!client) {
Michal Vasko05532772021-06-03 12:12:38 +0200124 ERR(NULL, "Call Home client \"%s\" was not found.", name);
Michal Vaskoadf30f02019-06-24 09:34:47 +0200125
Michal Vasko2e6defd2016-10-07 15:48:15 +0200126 /* READ UNLOCK */
127 pthread_rwlock_unlock(&server_opts.ch_client_lock);
Michal Vaskoadf30f02019-06-24 09:34:47 +0200128 } else if (endpt_name && ti && !endpt) {
Michal Vasko05532772021-06-03 12:12:38 +0200129 ERR(NULL, "Call Home client \"%s\" endpoint \"%s\" was not found.", name, endpt_name);
Michal Vaskoadf30f02019-06-24 09:34:47 +0200130
131 /* READ UNLOCK */
132 pthread_rwlock_unlock(&server_opts.ch_client_lock);
133 } else {
134 /* CH CLIENT LOCK */
135 pthread_mutex_lock(&client->lock);
136
137 *client_p = client;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200138 }
139
Michal Vaskoadf30f02019-06-24 09:34:47 +0200140 return endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200141}
142
143void
144nc_server_ch_client_unlock(struct nc_ch_client *client)
145{
146 /* CH CLIENT UNLOCK */
147 pthread_mutex_unlock(&client->lock);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100148
149 /* READ UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200150 pthread_rwlock_unlock(&server_opts.ch_client_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100151}
Michal Vasko086311b2016-01-08 09:53:11 +0100152
Michal Vasko1a38c862016-01-15 15:50:07 +0100153API void
154nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason)
155{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200156 if (!session) {
157 ERRARG("session");
158 return;
159 } else if (!reason) {
160 ERRARG("reason");
Michal Vasko1a38c862016-01-15 15:50:07 +0100161 return;
162 }
163
Michal Vasko142cfea2017-08-07 10:12:11 +0200164 if ((reason != NC_SESSION_TERM_KILLED) && (session->term_reason == NC_SESSION_TERM_KILLED)) {
165 session->killed_by = 0;
166 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100167 session->term_reason = reason;
168}
169
Michal Vasko142cfea2017-08-07 10:12:11 +0200170API void
171nc_session_set_killed_by(struct nc_session *session, uint32_t sid)
172{
173 if (!session || (session->term_reason != NC_SESSION_TERM_KILLED)) {
174 ERRARG("session");
175 return;
176 } else if (!sid) {
177 ERRARG("sid");
178 return;
179 }
180
181 session->killed_by = sid;
182}
183
184API void
185nc_session_set_status(struct nc_session *session, NC_STATUS status)
186{
187 if (!session) {
188 ERRARG("session");
189 return;
190 } else if (!status) {
191 ERRARG("status");
192 return;
193 }
194
195 session->status = status;
196}
197
Michal Vasko086311b2016-01-08 09:53:11 +0100198int
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200199nc_sock_listen_inet(const char *address, uint16_t port, struct nc_keepalives *ka)
Michal Vasko086311b2016-01-08 09:53:11 +0100200{
Michal Vasko06c860d2018-07-09 16:08:52 +0200201 int opt;
Michal Vasko086311b2016-01-08 09:53:11 +0100202 int is_ipv4, sock;
203 struct sockaddr_storage saddr;
204
205 struct sockaddr_in *saddr4;
206 struct sockaddr_in6 *saddr6;
207
Michal Vasko086311b2016-01-08 09:53:11 +0100208 if (!strchr(address, ':')) {
209 is_ipv4 = 1;
210 } else {
211 is_ipv4 = 0;
212 }
213
214 sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0);
215 if (sock == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200216 ERR(NULL, "Failed to create socket (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100217 goto fail;
218 }
219
Michal Vaskobe52dc22018-10-17 09:28:17 +0200220 /* these options will be inherited by accepted sockets */
Michal Vasko06c860d2018-07-09 16:08:52 +0200221 opt = 1;
222 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200223 ERR(NULL, "Could not set SO_REUSEADDR socket option (%s).", strerror(errno));
Michal Vasko06c860d2018-07-09 16:08:52 +0200224 goto fail;
225 }
Michal Vasko83ad17e2019-01-30 10:11:37 +0100226 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200227 ERR(NULL, "Could not set TCP_NODELAY socket option (%s).", strerror(errno));
Michal Vasko83ad17e2019-01-30 10:11:37 +0100228 goto fail;
229 }
Michal Vaskobe52dc22018-10-17 09:28:17 +0200230
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200231 if (nc_sock_enable_keepalive(sock, ka)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100232 goto fail;
233 }
234
Michal Vaskof22d5ff2020-04-15 11:10:27 +0200235 memset(&saddr, 0, sizeof(struct sockaddr_storage));
Michal Vasko086311b2016-01-08 09:53:11 +0100236 if (is_ipv4) {
237 saddr4 = (struct sockaddr_in *)&saddr;
238
239 saddr4->sin_family = AF_INET;
240 saddr4->sin_port = htons(port);
241
242 if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200243 ERR(NULL, "Failed to convert IPv4 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100244 goto fail;
245 }
246
247 if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200248 ERR(NULL, "Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100249 goto fail;
250 }
251
252 } else {
253 saddr6 = (struct sockaddr_in6 *)&saddr;
254
255 saddr6->sin6_family = AF_INET6;
256 saddr6->sin6_port = htons(port);
257
258 if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200259 ERR(NULL, "Failed to convert IPv6 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100260 goto fail;
261 }
262
263 if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200264 ERR(NULL, "Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100265 goto fail;
266 }
267 }
268
Michal Vaskofb89d772016-01-08 12:25:35 +0100269 if (listen(sock, NC_REVERSE_QUEUE) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200270 ERR(NULL, "Unable to start listening on \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100271 goto fail;
272 }
273
274 return sock;
275
276fail:
277 if (sock > -1) {
278 close(sock);
279 }
280
281 return -1;
282}
283
284int
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200285nc_sock_listen_unix(const char *address, const struct nc_server_unix_opts *opts)
286{
287 struct sockaddr_un sun;
288 int sock = -1;
289
Michal Vasko93e96f12021-09-30 10:02:09 +0200290 if (strlen(address) > sizeof(sun.sun_path) - 1) {
291 ERR(NULL, "Socket path \"%s\" is longer than maximum length %d.", address, (int)(sizeof(sun.sun_path) - 1));
292 goto fail;
293 }
294
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200295 sock = socket(AF_UNIX, SOCK_STREAM, 0);
296 if (sock == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200297 ERR(NULL, "Failed to create socket (%s).", strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200298 goto fail;
299 }
300
301 memset(&sun, 0, sizeof(sun));
302 sun.sun_family = AF_UNIX;
Michal Vasko93e96f12021-09-30 10:02:09 +0200303 snprintf(sun.sun_path, sizeof(sun.sun_path) - 1, "%s", address);
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200304
305 unlink(sun.sun_path);
306 if (bind(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200307 ERR(NULL, "Could not bind \"%s\" (%s).", address, strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200308 goto fail;
309 }
310
311 if (opts->mode != (mode_t)-1) {
312 if (chmod(sun.sun_path, opts->mode) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200313 ERR(NULL, "Failed to set unix socket permissions (%s).", strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200314 goto fail;
315 }
316 }
317
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200318 if ((opts->uid != (uid_t)-1) || (opts->gid != (gid_t)-1)) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200319 if (chown(sun.sun_path, opts->uid, opts->gid) < 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200320 ERR(NULL, "Failed to set unix socket uid/gid (%s).", strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200321 goto fail;
322 }
323 }
324
325 if (listen(sock, NC_REVERSE_QUEUE) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200326 ERR(NULL, "Unable to start listening on \"%s\" (%s).", address, strerror(errno));
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200327 goto fail;
328 }
329
330 return sock;
331
332fail:
333 if (sock > -1) {
334 close(sock);
335 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200336 return -1;
337}
338
aPiecek90ff0242021-02-14 14:58:01 +0100339/**
340 * @brief Evaluate socket name for AF_UNIX socket.
341 * @param[in] acc_sock_fd is file descriptor for the accepted socket (a nonnegative).
342 * @param[out] host is pointer to char* to which the socket name will be set. It must not be NULL.
343 * @return 0 in case of success. Call free function for parameter host to avoid a memory leak.
344 * @return 0 if the stream socket is unnamed. Parameter host is set to NULL.
345 * @return -1 in case of error. Parameter host is set to NULL.
346 */
347static int
348sock_host_unix(int acc_sock_fd, char **host)
349{
350 char *sun_path;
351 struct sockaddr_storage saddr;
352 socklen_t addr_len;
353
354 *host = NULL;
355 saddr.ss_family = AF_UNIX;
356 addr_len = sizeof(saddr);
357
358 if (getsockname(acc_sock_fd, (struct sockaddr *)&saddr, &addr_len)) {
Michal Vasko05532772021-06-03 12:12:38 +0200359 ERR(NULL, "getsockname failed (%s).", strerror(errno));
aPiecek90ff0242021-02-14 14:58:01 +0100360 return -1;
361 }
362
363 sun_path = ((struct sockaddr_un *)&saddr)->sun_path;
364 if (!sun_path) {
365 /* stream socket is unnamed */
366 return 0;
367 }
368
369 if (!(*host = strdup(sun_path))) {
370 ERRMEM;
371 return -1;
372 }
373
374 return 0;
375}
376
377/**
378 * @brief Evaluate socket name and port number for AF_INET socket.
379 * @param[in] addr is pointing to structure filled by accept function which was successful.
380 * @param[out] host is pointer to char* to which the socket name will be set. It must not be NULL.
381 * @param[out] port is pointer to uint16_t to which the port number will be set. It must not be NULL.
382 * @return 0 in case of success. Call free function for parameter host to avoid a memory leak.
383 * @return -1 in case of error. Parameter host is set to NULL and port is unchanged.
384 */
385static int
386sock_host_inet(const struct sockaddr_in *addr, char **host, uint16_t *port)
387{
388 *host = malloc(INET_ADDRSTRLEN);
389 if (!(*host)) {
390 ERRMEM;
391 return -1;
392 }
393
aPiecek3da9b342021-02-18 15:00:03 +0100394 if (!inet_ntop(AF_INET, &addr->sin_addr, *host, INET_ADDRSTRLEN)) {
Michal Vasko69e98752022-12-14 14:20:17 +0100395 ERR(NULL, "inet_ntop failed (%s).", strerror(errno));
aPiecek90ff0242021-02-14 14:58:01 +0100396 free(*host);
397 *host = NULL;
398 return -1;
399 }
400
Michal Vasko89ffa8a2021-06-25 08:40:08 +0200401 *port = ntohs(addr->sin_port);
aPiecek90ff0242021-02-14 14:58:01 +0100402
403 return 0;
404}
405
406/**
407 * @brief Evaluate socket name and port number for AF_INET6 socket.
408 * @param[in] addr is pointing to structure filled by accept function which was successful.
409 * @param[out] host is pointer to char* to which the socket name will be set. It must not be NULL.
410 * @param[out] port is pointer to uint16_t to which the port number will be set. It must not be NULL.
411 * @return 0 in case of success. Call free function for parameter host to avoid a memory leak.
412 * @return -1 in case of error. Parameter host is set to the NULL and port is unchanged.
413 */
414static int
415sock_host_inet6(const struct sockaddr_in6 *addr, char **host, uint16_t *port)
416{
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200417 *host = malloc(INET6_ADDRSTRLEN);
aPiecek90ff0242021-02-14 14:58:01 +0100418 if (!(*host)) {
419 ERRMEM;
420 return -1;
421 }
422
aPiecek3da9b342021-02-18 15:00:03 +0100423 if (!inet_ntop(AF_INET6, &addr->sin6_addr, *host, INET6_ADDRSTRLEN)) {
Michal Vasko69e98752022-12-14 14:20:17 +0100424 ERR(NULL, "inet_ntop failed (%s).", strerror(errno));
aPiecek90ff0242021-02-14 14:58:01 +0100425 free(*host);
426 *host = NULL;
427 return -1;
428 }
429
Michal Vasko89ffa8a2021-06-25 08:40:08 +0200430 *port = ntohs(addr->sin6_port);
aPiecek90ff0242021-02-14 14:58:01 +0100431
432 return 0;
433}
434
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200435int
Michal Vasko3031aae2016-01-27 16:07:18 +0100436nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, int timeout, char **host, uint16_t *port, uint16_t *idx)
Michal Vasko086311b2016-01-08 09:53:11 +0100437{
Michal Vaskof54cd352017-02-22 13:42:02 +0100438 sigset_t sigmask, origmask;
Michal Vasko89ffa8a2021-06-25 08:40:08 +0200439 uint16_t i, j, pfd_count, client_port;
440 char *client_address;
Michal Vasko086311b2016-01-08 09:53:11 +0100441 struct pollfd *pfd;
442 struct sockaddr_storage saddr;
443 socklen_t saddr_len = sizeof(saddr);
Michal Vasko89ffa8a2021-06-25 08:40:08 +0200444 int ret, client_sock, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100445
446 pfd = malloc(bind_count * sizeof *pfd);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100447 if (!pfd) {
448 ERRMEM;
449 return -1;
450 }
451
Michal Vaskoac2f6182017-01-30 14:32:03 +0100452 for (i = 0, pfd_count = 0; i < bind_count; ++i) {
Michal Vasko94acafc2016-09-23 13:40:10 +0200453 if (binds[i].sock < 0) {
454 /* invalid socket */
Michal Vasko94acafc2016-09-23 13:40:10 +0200455 continue;
456 }
Michal Vasko0a3f3752016-10-13 14:58:38 +0200457 if (binds[i].pollin) {
458 binds[i].pollin = 0;
459 /* leftover pollin */
460 sock = binds[i].sock;
Michal Vasko086311b2016-01-08 09:53:11 +0100461 break;
462 }
Michal Vaskoac2f6182017-01-30 14:32:03 +0100463 pfd[pfd_count].fd = binds[i].sock;
464 pfd[pfd_count].events = POLLIN;
465 pfd[pfd_count].revents = 0;
466
467 ++pfd_count;
Michal Vasko086311b2016-01-08 09:53:11 +0100468 }
469
Michal Vasko0a3f3752016-10-13 14:58:38 +0200470 if (sock == -1) {
471 /* poll for a new connection */
Michal Vaskof54cd352017-02-22 13:42:02 +0100472 sigfillset(&sigmask);
473 pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
Michal Vaskoac2f6182017-01-30 14:32:03 +0100474 ret = poll(pfd, pfd_count, timeout);
Michal Vaskof54cd352017-02-22 13:42:02 +0100475 pthread_sigmask(SIG_SETMASK, &origmask, NULL);
476
Michal Vasko0a3f3752016-10-13 14:58:38 +0200477 if (!ret) {
478 /* we timeouted */
479 free(pfd);
480 return 0;
481 } else if (ret == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200482 ERR(NULL, "Poll failed (%s).", strerror(errno));
Michal Vasko0a3f3752016-10-13 14:58:38 +0200483 free(pfd);
484 return -1;
485 }
Michal Vasko086311b2016-01-08 09:53:11 +0100486
Michal Vaskoac2f6182017-01-30 14:32:03 +0100487 for (i = 0, j = 0; j < pfd_count; ++i, ++j) {
488 /* adjust i so that indices in binds and pfd always match */
489 while (binds[i].sock != pfd[j].fd) {
490 ++i;
491 }
492
493 if (pfd[j].revents & POLLIN) {
Michal Vasko0a3f3752016-10-13 14:58:38 +0200494 --ret;
495
496 if (!ret) {
497 /* the last socket with an event, use it */
Michal Vaskoac2f6182017-01-30 14:32:03 +0100498 sock = pfd[j].fd;
Michal Vasko0a3f3752016-10-13 14:58:38 +0200499 break;
500 } else {
501 /* just remember the event for next time */
502 binds[i].pollin = 1;
503 }
504 }
Michal Vasko086311b2016-01-08 09:53:11 +0100505 }
506 }
507 free(pfd);
508
509 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100510 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100511 return -1;
512 }
513
Michal Vasko89ffa8a2021-06-25 08:40:08 +0200514 /* accept connection */
515 client_sock = accept(sock, (struct sockaddr *)&saddr, &saddr_len);
516 if (client_sock < 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200517 ERR(NULL, "Accept failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100518 return -1;
519 }
520
Michal Vasko0190bc32016-03-02 15:47:49 +0100521 /* make the socket non-blocking */
Michal Vasko89ffa8a2021-06-25 08:40:08 +0200522 if (((flags = fcntl(client_sock, F_GETFL)) == -1) || (fcntl(client_sock, F_SETFL, flags | O_NONBLOCK) == -1)) {
Michal Vasko05532772021-06-03 12:12:38 +0200523 ERR(NULL, "Fcntl failed (%s).", strerror(errno));
Michal Vasko89ffa8a2021-06-25 08:40:08 +0200524 goto fail;
Michal Vasko0190bc32016-03-02 15:47:49 +0100525 }
526
Michal Vasko89ffa8a2021-06-25 08:40:08 +0200527 /* learn information about the client end */
528 if (saddr.ss_family == AF_UNIX) {
529 if (sock_host_unix(client_sock, &client_address)) {
530 goto fail;
531 }
532 client_port = 0;
533 } else if (saddr.ss_family == AF_INET) {
534 if (sock_host_inet((struct sockaddr_in *)&saddr, &client_address, &client_port)) {
535 goto fail;
536 }
537 } else if (saddr.ss_family == AF_INET6) {
538 if (sock_host_inet6((struct sockaddr_in6 *)&saddr, &client_address, &client_port)) {
539 goto fail;
540 }
541 } else {
542 ERR(NULL, "Source host of an unknown protocol family.");
543 goto fail;
aPiecek90ff0242021-02-14 14:58:01 +0100544 }
Michal Vasko086311b2016-01-08 09:53:11 +0100545
aPiecek90ff0242021-02-14 14:58:01 +0100546 if (saddr.ss_family == AF_UNIX) {
Michal Vasko89ffa8a2021-06-25 08:40:08 +0200547 VRB(NULL, "Accepted a connection on %s.", binds[i].address);
aPiecek90ff0242021-02-14 14:58:01 +0100548 } else {
Michal Vasko89ffa8a2021-06-25 08:40:08 +0200549 VRB(NULL, "Accepted a connection on %s:%u from %s:%u.", binds[i].address, binds[i].port, client_address, client_port);
Michal Vasko086311b2016-01-08 09:53:11 +0100550 }
551
Michal Vasko89ffa8a2021-06-25 08:40:08 +0200552 if (host) {
553 *host = client_address;
554 } else {
555 free(client_address);
556 }
557 if (port) {
558 *port = client_port;
559 }
560 if (idx) {
561 *idx = i;
562 }
563 return client_sock;
564
565fail:
566 close(client_sock);
567 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100568}
569
Michal Vasko238b6c12021-12-14 15:14:09 +0100570API struct nc_server_reply *
Michal Vasko05532772021-06-03 12:12:38 +0200571nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *session)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100572{
Michal Vasko77367452021-02-16 16:32:18 +0100573 const char *identifier = NULL, *revision = NULL, *format = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100574 char *model_data = NULL;
Michal Vasko77367452021-02-16 16:32:18 +0100575 struct ly_out *out;
Michal Vasko9b1a9522021-03-15 16:24:26 +0100576 const struct lys_module *module = NULL, *mod;
Michal Vasko77367452021-02-16 16:32:18 +0100577 const struct lysp_submodule *submodule = NULL;
578 struct lyd_node *child, *err, *data = NULL;
579 LYS_OUTFORMAT outformat = 0;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100580
Michal Vasko77367452021-02-16 16:32:18 +0100581 LY_LIST_FOR(lyd_child(rpc), child) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100582 if (!strcmp(child->schema->name, "identifier")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200583 identifier = lyd_get_value(child);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100584 } else if (!strcmp(child->schema->name, "version")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200585 revision = lyd_get_value(child);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200586 if (revision && (revision[0] == '\0')) {
Michal Vasko77367452021-02-16 16:32:18 +0100587 revision = NULL;
Radek Krejci1afa7792017-03-26 11:24:16 -0500588 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100589 } else if (!strcmp(child->schema->name, "format")) {
Michal Vaskoe97b10a2021-04-28 08:52:52 +0200590 format = lyd_get_value(child);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100591 }
592 }
Michal Vasko5ca5d972022-09-14 13:51:31 +0200593 VRB(session, "Module \"%s@%s\" was requested.", identifier, revision ? revision : "<any>");
Michal Vasko05ba9df2016-01-13 14:40:27 +0100594
Michal Vasko77367452021-02-16 16:32:18 +0100595 /* check revision */
596 if (revision && (strlen(revision) != 10) && strcmp(revision, "1.0")) {
Michal Vasko93224072021-11-09 12:14:28 +0100597 err = nc_err(session->ctx, NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
Michal Vasko1a38c862016-01-15 15:50:07 +0100598 nc_err_set_msg(err, "The requested version is not supported.", "en");
599 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100600 }
601
Michal Vasko77367452021-02-16 16:32:18 +0100602 if (revision) {
603 /* get specific module */
Michal Vasko93224072021-11-09 12:14:28 +0100604 module = ly_ctx_get_module(session->ctx, identifier, revision);
Michal Vasko77367452021-02-16 16:32:18 +0100605 if (!module) {
Michal Vasko93224072021-11-09 12:14:28 +0100606 submodule = ly_ctx_get_submodule(session->ctx, identifier, revision);
Michal Vasko77367452021-02-16 16:32:18 +0100607 }
608 } else {
609 /* try to get implemented, then latest module */
Michal Vasko93224072021-11-09 12:14:28 +0100610 module = ly_ctx_get_module_implemented(session->ctx, identifier);
Michal Vasko77367452021-02-16 16:32:18 +0100611 if (!module) {
Michal Vasko93224072021-11-09 12:14:28 +0100612 module = ly_ctx_get_module_latest(session->ctx, identifier);
Michal Vasko77367452021-02-16 16:32:18 +0100613 }
614 if (!module) {
Michal Vasko93224072021-11-09 12:14:28 +0100615 submodule = ly_ctx_get_submodule_latest(session->ctx, identifier);
Michal Vasko77367452021-02-16 16:32:18 +0100616 }
Michal Vaskod91f6e62016-04-05 11:34:22 +0200617 }
Michal Vasko77367452021-02-16 16:32:18 +0100618 if (!module && !submodule) {
Michal Vasko93224072021-11-09 12:14:28 +0100619 err = nc_err(session->ctx, NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
Michal Vasko5ca5d972022-09-14 13:51:31 +0200620 nc_err_set_msg(err, "The requested module was not found.", "en");
Michal Vasko1a38c862016-01-15 15:50:07 +0100621 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100622 }
623
624 /* check format */
Radek Krejci90fba642016-12-07 15:59:45 +0100625 if (!format || !strcmp(format, "ietf-netconf-monitoring:yang")) {
Michal Vasko77367452021-02-16 16:32:18 +0100626 outformat = LYS_OUT_YANG;
Radek Krejci90fba642016-12-07 15:59:45 +0100627 } else if (!strcmp(format, "ietf-netconf-monitoring:yin")) {
Michal Vasko77367452021-02-16 16:32:18 +0100628 outformat = LYS_OUT_YIN;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100629 } else {
Michal Vasko93224072021-11-09 12:14:28 +0100630 err = nc_err(session->ctx, NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
Michal Vasko1a38c862016-01-15 15:50:07 +0100631 nc_err_set_msg(err, "The requested format is not supported.", "en");
632 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100633 }
Michal Vasko77367452021-02-16 16:32:18 +0100634
635 /* print */
636 ly_out_new_memory(&model_data, 0, &out);
637 if (module) {
638 lys_print_module(out, module, outformat, 0, 0);
639 } else {
640 lys_print_submodule(out, submodule, outformat, 0, 0);
641 }
642 ly_out_free(out, NULL, 0);
Michal Vaskod91f6e62016-04-05 11:34:22 +0200643 if (!model_data) {
644 ERRINT;
645 return NULL;
646 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100647
Michal Vasko9b1a9522021-03-15 16:24:26 +0100648 /* create reply */
Michal Vasko93224072021-11-09 12:14:28 +0100649 mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-monitoring");
Michal Vasko9b1a9522021-03-15 16:24:26 +0100650 if (!mod || lyd_new_inner(NULL, mod, "get-schema", 0, &data)) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100651 ERRINT;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200652 free(model_data);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100653 return NULL;
654 }
Michal Vasko9b1a9522021-03-15 16:24:26 +0100655 if (lyd_new_any(data, NULL, "data", model_data, 1, LYD_ANYDATA_STRING, 1, NULL)) {
656 ERRINT;
Michal Vaskoa50f68e2022-02-24 16:10:54 +0100657 free(model_data);
Michal Vasko9b1a9522021-03-15 16:24:26 +0100658 lyd_free_tree(data);
659 return NULL;
660 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100661
Radek Krejci36dfdb32016-09-01 16:56:35 +0200662 return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100663}
664
Michal Vasko238b6c12021-12-14 15:14:09 +0100665API struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100666nc_clb_default_close_session(struct lyd_node *UNUSED(rpc), struct nc_session *session)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100667{
Michal Vasko428087d2016-01-14 16:04:28 +0100668 session->term_reason = NC_SESSION_TERM_CLOSED;
669 return nc_server_reply_ok();
Michal Vasko05ba9df2016-01-13 14:40:27 +0100670}
671
Michal Vasko93224072021-11-09 12:14:28 +0100672/**
673 * @brief Initialize a context with default RPC callbacks if none are set.
674 *
675 * @param[in] ctx Context to initialize.
676 */
677static void
678nc_server_init_ctx(const struct ly_ctx *ctx)
Michal Vasko086311b2016-01-08 09:53:11 +0100679{
Michal Vasko77367452021-02-16 16:32:18 +0100680 struct lysc_node *rpc;
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100681
Michal Vasko238b6c12021-12-14 15:14:09 +0100682 if (global_rpc_clb) {
683 /* expect it to handle these RPCs as well */
684 return;
685 }
686
Michal Vasko05ba9df2016-01-13 14:40:27 +0100687 /* set default <get-schema> callback if not specified */
Michal Vasko77367452021-02-16 16:32:18 +0100688 rpc = NULL;
689 if (ly_ctx_get_module_implemented(ctx, "ietf-netconf-monitoring")) {
690 rpc = (struct lysc_node *)lys_find_path(ctx, NULL, "/ietf-netconf-monitoring:get-schema", 0);
691 }
Michal Vasko88639e92017-08-03 14:38:10 +0200692 if (rpc && !rpc->priv) {
Michal Vasko77367452021-02-16 16:32:18 +0100693 rpc->priv = nc_clb_default_get_schema;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100694 }
695
Michal Vasko93224072021-11-09 12:14:28 +0100696 /* set default <close-session> callback if not specified */
Michal Vasko77367452021-02-16 16:32:18 +0100697 rpc = (struct lysc_node *)lys_find_path(ctx, NULL, "/ietf-netconf:close-session", 0);
Michal Vasko88639e92017-08-03 14:38:10 +0200698 if (rpc && !rpc->priv) {
Michal Vasko77367452021-02-16 16:32:18 +0100699 rpc->priv = nc_clb_default_close_session;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100700 }
Michal Vasko93224072021-11-09 12:14:28 +0100701}
Michal Vasko05ba9df2016-01-13 14:40:27 +0100702
Michal Vasko93224072021-11-09 12:14:28 +0100703API int
704nc_server_init(void)
705{
706 pthread_rwlockattr_t attr, *attr_p = NULL;
707 int r;
708
709 nc_init();
Michal Vaskob48aa812016-01-18 14:13:09 +0100710
711 server_opts.new_session_id = 1;
Andrew Langefeld6ed922d2018-09-12 14:08:32 -0500712 server_opts.new_client_id = 1;
Michal Vaskob48aa812016-01-18 14:13:09 +0100713
Michal Vasko93224072021-11-09 12:14:28 +0100714#ifdef HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP
715 if ((r = pthread_rwlockattr_init(&attr))) {
716 ERR(NULL, "%s: failed init attribute (%s).", __func__, strerror(r));
717 goto error;
718 }
719 attr_p = &attr;
720 if ((r = pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP))) {
721 ERR(NULL, "%s: failed set attribute (%s).", __func__, strerror(r));
722 goto error;
723 }
Rosen Penevef2f3ac2019-07-15 18:15:28 -0700724#endif
Michal Vasko93224072021-11-09 12:14:28 +0100725
726 if ((r = pthread_rwlock_init(&server_opts.endpt_lock, attr_p))) {
727 ERR(NULL, "%s: failed to init rwlock(%s).", __func__, strerror(r));
728 goto error;
729 }
730 if ((r = pthread_rwlock_init(&server_opts.ch_client_lock, attr_p))) {
731 ERR(NULL, "%s: failed to init rwlock(%s).", __func__, strerror(r));
732 goto error;
733 }
734
735 if (attr_p) {
736 pthread_rwlockattr_destroy(attr_p);
Frank Rimpler9f838b02018-07-25 06:44:03 +0000737 }
Michal Vasko086311b2016-01-08 09:53:11 +0100738 return 0;
Michal Vasko93224072021-11-09 12:14:28 +0100739
740error:
741 if (attr_p) {
742 pthread_rwlockattr_destroy(attr_p);
743 }
744 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100745}
746
Michal Vaskob48aa812016-01-18 14:13:09 +0100747API void
748nc_server_destroy(void)
749{
Michal Vasko1440a742021-03-31 11:11:03 +0200750 uint32_t i;
Radek Krejci658782b2016-12-04 22:04:55 +0100751
752 for (i = 0; i < server_opts.capabilities_count; i++) {
Michal Vasko93224072021-11-09 12:14:28 +0100753 free(server_opts.capabilities[i]);
Radek Krejci658782b2016-12-04 22:04:55 +0100754 }
755 free(server_opts.capabilities);
Michal Vaskodd6e4f72018-06-01 10:21:27 +0200756 server_opts.capabilities = NULL;
757 server_opts.capabilities_count = 0;
Michal Vasko1440a742021-03-31 11:11:03 +0200758 if (server_opts.content_id_data && server_opts.content_id_data_free) {
759 server_opts.content_id_data_free(server_opts.content_id_data);
760 }
Michal Vaskodd6e4f72018-06-01 10:21:27 +0200761
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200762#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko59050372016-11-22 14:33:55 +0100763 nc_server_del_endpt(NULL, 0);
Michal Vasko0bdf70b2019-06-24 19:20:20 +0200764 nc_server_ch_del_client(NULL);
Michal Vaskob48aa812016-01-18 14:13:09 +0100765#endif
Michal Vasko17dfda92016-12-01 14:06:16 +0100766#ifdef NC_ENABLED_SSH
Michal Vaskoebba7602018-03-23 13:14:08 +0100767 if (server_opts.passwd_auth_data && server_opts.passwd_auth_data_free) {
768 server_opts.passwd_auth_data_free(server_opts.passwd_auth_data);
769 }
Michal Vaskodd6e4f72018-06-01 10:21:27 +0200770 server_opts.passwd_auth_data = NULL;
771 server_opts.passwd_auth_data_free = NULL;
Michal Vaskoebba7602018-03-23 13:14:08 +0100772
Michal Vasko17dfda92016-12-01 14:06:16 +0100773 nc_server_ssh_del_authkey(NULL, NULL, 0, NULL);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100774
775 if (server_opts.hostkey_data && server_opts.hostkey_data_free) {
776 server_opts.hostkey_data_free(server_opts.hostkey_data);
777 }
Michal Vaskodd6e4f72018-06-01 10:21:27 +0200778 server_opts.hostkey_data = NULL;
779 server_opts.hostkey_data_free = NULL;
roman41a11e42022-06-22 09:27:08 +0200780
781 /* PAM */
782 free(server_opts.conf_name);
783 free(server_opts.conf_dir);
784 server_opts.conf_name = NULL;
785 server_opts.conf_dir = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100786#endif
787#ifdef NC_ENABLED_TLS
788 if (server_opts.server_cert_data && server_opts.server_cert_data_free) {
789 server_opts.server_cert_data_free(server_opts.server_cert_data);
790 }
Michal Vaskodd6e4f72018-06-01 10:21:27 +0200791 server_opts.server_cert_data = NULL;
792 server_opts.server_cert_data_free = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100793 if (server_opts.trusted_cert_list_data && server_opts.trusted_cert_list_data_free) {
794 server_opts.trusted_cert_list_data_free(server_opts.trusted_cert_list_data);
795 }
Michal Vaskodd6e4f72018-06-01 10:21:27 +0200796 server_opts.trusted_cert_list_data = NULL;
797 server_opts.trusted_cert_list_data_free = NULL;
Michal Vaskob48aa812016-01-18 14:13:09 +0100798#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100799 nc_destroy();
Michal Vaskob48aa812016-01-18 14:13:09 +0100800}
801
Michal Vasko086311b2016-01-08 09:53:11 +0100802API int
803nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported)
804{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200805 if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)) {
806 ERRARG("basic_mode");
807 return -1;
808 } else if (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT))) {
809 ERRARG("also_supported");
Michal Vasko086311b2016-01-08 09:53:11 +0100810 return -1;
811 }
812
813 server_opts.wd_basic_mode = basic_mode;
814 server_opts.wd_also_supported = also_supported;
815 return 0;
816}
817
Michal Vasko1a38c862016-01-15 15:50:07 +0100818API void
Michal Vasko55f03972016-04-13 08:56:01 +0200819nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported)
820{
821 if (!basic_mode && !also_supported) {
822 ERRARG("basic_mode and also_supported");
823 return;
824 }
825
826 if (basic_mode) {
827 *basic_mode = server_opts.wd_basic_mode;
828 }
829 if (also_supported) {
830 *also_supported = server_opts.wd_also_supported;
831 }
832}
833
Michal Vasko55f03972016-04-13 08:56:01 +0200834API int
Radek Krejci658782b2016-12-04 22:04:55 +0100835nc_server_set_capability(const char *value)
Michal Vasko55f03972016-04-13 08:56:01 +0200836{
Michal Vasko93224072021-11-09 12:14:28 +0100837 void *mem;
Radek Krejci658782b2016-12-04 22:04:55 +0100838
839 if (!value || !value[0]) {
840 ERRARG("value must not be empty");
841 return EXIT_FAILURE;
842 }
843
Michal Vasko93224072021-11-09 12:14:28 +0100844 mem = realloc(server_opts.capabilities, (server_opts.capabilities_count + 1) * sizeof *server_opts.capabilities);
845 if (!mem) {
Radek Krejci658782b2016-12-04 22:04:55 +0100846 ERRMEM;
847 return EXIT_FAILURE;
848 }
Michal Vasko93224072021-11-09 12:14:28 +0100849 server_opts.capabilities = mem;
850
851 server_opts.capabilities[server_opts.capabilities_count] = strdup(value);
852 server_opts.capabilities_count++;
Radek Krejci658782b2016-12-04 22:04:55 +0100853
854 return EXIT_SUCCESS;
Michal Vasko55f03972016-04-13 08:56:01 +0200855}
856
Michal Vasko1a38c862016-01-15 15:50:07 +0100857API void
Michal Vasko1440a742021-03-31 11:11:03 +0200858nc_server_set_content_id_clb(char *(*content_id_clb)(void *user_data), void *user_data,
859 void (*free_user_data)(void *user_data))
860{
861 server_opts.content_id_clb = content_id_clb;
862 server_opts.content_id_data = user_data;
863 server_opts.content_id_data_free = free_user_data;
864}
865
866API void
Michal Vasko086311b2016-01-08 09:53:11 +0100867nc_server_set_hello_timeout(uint16_t hello_timeout)
868{
Michal Vasko086311b2016-01-08 09:53:11 +0100869 server_opts.hello_timeout = hello_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100870}
871
Michal Vasko55f03972016-04-13 08:56:01 +0200872API uint16_t
873nc_server_get_hello_timeout(void)
874{
875 return server_opts.hello_timeout;
876}
877
Michal Vasko1a38c862016-01-15 15:50:07 +0100878API void
Michal Vasko086311b2016-01-08 09:53:11 +0100879nc_server_set_idle_timeout(uint16_t idle_timeout)
880{
Michal Vasko086311b2016-01-08 09:53:11 +0100881 server_opts.idle_timeout = idle_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100882}
883
Michal Vasko55f03972016-04-13 08:56:01 +0200884API uint16_t
885nc_server_get_idle_timeout(void)
886{
887 return server_opts.idle_timeout;
888}
889
Michal Vasko71090fc2016-05-24 16:37:28 +0200890API NC_MSG_TYPE
Michal Vasko93224072021-11-09 12:14:28 +0100891nc_accept_inout(int fdin, int fdout, const char *username, const struct ly_ctx *ctx, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +0100892{
Michal Vasko71090fc2016-05-24 16:37:28 +0200893 NC_MSG_TYPE msgtype;
Michal Vasko9fb42272017-10-05 13:50:05 +0200894 struct timespec ts_cur;
Michal Vasko71090fc2016-05-24 16:37:28 +0200895
Michal Vasko93224072021-11-09 12:14:28 +0100896 if (!ctx) {
897 ERRARG("ctx");
Michal Vasko71090fc2016-05-24 16:37:28 +0200898 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200899 } else if (fdin < 0) {
900 ERRARG("fdin");
Michal Vasko71090fc2016-05-24 16:37:28 +0200901 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200902 } else if (fdout < 0) {
903 ERRARG("fdout");
Michal Vasko71090fc2016-05-24 16:37:28 +0200904 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200905 } else if (!username) {
906 ERRARG("username");
Michal Vasko71090fc2016-05-24 16:37:28 +0200907 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200908 } else if (!session) {
909 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200910 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100911 }
912
Michal Vasko93224072021-11-09 12:14:28 +0100913 /* init ctx as needed */
914 nc_server_init_ctx(ctx);
915
Michal Vasko086311b2016-01-08 09:53:11 +0100916 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +0200917 *session = nc_new_session(NC_SERVER, 0);
Michal Vasko1a38c862016-01-15 15:50:07 +0100918 if (!(*session)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100919 ERRMEM;
Michal Vasko71090fc2016-05-24 16:37:28 +0200920 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100921 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100922 (*session)->status = NC_STATUS_STARTING;
Michal Vaskoade892d2017-02-22 13:40:35 +0100923
Michal Vasko086311b2016-01-08 09:53:11 +0100924 /* transport specific data */
Michal Vasko1a38c862016-01-15 15:50:07 +0100925 (*session)->ti_type = NC_TI_FD;
926 (*session)->ti.fd.in = fdin;
927 (*session)->ti.fd.out = fdout;
Michal Vasko086311b2016-01-08 09:53:11 +0100928
Michal Vasko93224072021-11-09 12:14:28 +0100929 /* assign context */
Michal Vasko1a38c862016-01-15 15:50:07 +0100930 (*session)->flags = NC_SESSION_SHAREDCTX;
Michal Vasko93224072021-11-09 12:14:28 +0100931 (*session)->ctx = (struct ly_ctx *)ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100932
Michal Vaskob48aa812016-01-18 14:13:09 +0100933 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200934 (*session)->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vaskob48aa812016-01-18 14:13:09 +0100935
Michal Vasko086311b2016-01-08 09:53:11 +0100936 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +0200937 msgtype = nc_handshake_io(*session);
Michal Vasko71090fc2016-05-24 16:37:28 +0200938 if (msgtype != NC_MSG_HELLO) {
939 nc_session_free(*session, NULL);
940 *session = NULL;
941 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100942 }
Michal Vasko9fb42272017-10-05 13:50:05 +0200943
Michal Vaskod8a74192023-02-06 15:51:50 +0100944 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9fb42272017-10-05 13:50:05 +0200945 (*session)->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vaskod8a74192023-02-06 15:51:50 +0100946 nc_realtime_get(&ts_cur);
Michal Vasko9fb42272017-10-05 13:50:05 +0200947 (*session)->opts.server.session_start = ts_cur.tv_sec;
948
Michal Vasko1a38c862016-01-15 15:50:07 +0100949 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko086311b2016-01-08 09:53:11 +0100950
Michal Vasko71090fc2016-05-24 16:37:28 +0200951 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100952}
Michal Vasko9e036d52016-01-08 10:49:26 +0100953
Michal Vaskob30b99c2016-07-26 11:35:43 +0200954static void
Michal Vasko74c345f2018-02-07 10:37:11 +0100955nc_ps_queue_add_id(struct nc_pollsession *ps, uint8_t *id)
956{
957 uint8_t q_last;
958
959 if (ps->queue_len == NC_PS_QUEUE_SIZE) {
960 ERRINT;
961 return;
962 }
963
964 /* get a unique queue value (by adding 1 to the last added value, if any) */
965 if (ps->queue_len) {
966 q_last = (ps->queue_begin + ps->queue_len - 1) % NC_PS_QUEUE_SIZE;
967 *id = ps->queue[q_last] + 1;
968 } else {
969 *id = 0;
970 }
971
972 /* add the id into the queue */
973 ++ps->queue_len;
974 q_last = (ps->queue_begin + ps->queue_len - 1) % NC_PS_QUEUE_SIZE;
975 ps->queue[q_last] = *id;
976}
977
978static void
Michal Vaskob30b99c2016-07-26 11:35:43 +0200979nc_ps_queue_remove_id(struct nc_pollsession *ps, uint8_t id)
980{
Michal Vasko74c345f2018-02-07 10:37:11 +0100981 uint8_t i, q_idx, found = 0;
Michal Vaskob30b99c2016-07-26 11:35:43 +0200982
983 for (i = 0; i < ps->queue_len; ++i) {
Michal Vasko74c345f2018-02-07 10:37:11 +0100984 /* get the actual queue idx */
985 q_idx = (ps->queue_begin + i) % NC_PS_QUEUE_SIZE;
Michal Vaskob30b99c2016-07-26 11:35:43 +0200986
987 if (found) {
Michal Vasko74c345f2018-02-07 10:37:11 +0100988 if (ps->queue[q_idx] == id) {
Michal Vaskob30b99c2016-07-26 11:35:43 +0200989 /* another equal value, simply cannot be */
990 ERRINT;
991 }
Michal Vaskod8340032018-02-12 14:41:00 +0100992 if (found == 2) {
993 /* move the following values */
994 ps->queue[q_idx ? q_idx - 1 : NC_PS_QUEUE_SIZE - 1] = ps->queue[q_idx];
995 }
Michal Vasko74c345f2018-02-07 10:37:11 +0100996 } else if (ps->queue[q_idx] == id) {
Michal Vaskob30b99c2016-07-26 11:35:43 +0200997 /* found our id, there can be no more equal valid values */
Michal Vaskod8340032018-02-12 14:41:00 +0100998 if (i == 0) {
999 found = 1;
1000 } else {
1001 /* this is not okay, our id is in the middle of the queue */
1002 found = 2;
1003 }
Michal Vaskob30b99c2016-07-26 11:35:43 +02001004 }
1005 }
Michal Vaskob30b99c2016-07-26 11:35:43 +02001006 if (!found) {
1007 ERRINT;
Michal Vasko103fe632018-02-12 16:37:45 +01001008 return;
Michal Vaskob30b99c2016-07-26 11:35:43 +02001009 }
Michal Vasko74c345f2018-02-07 10:37:11 +01001010
Michal Vasko103fe632018-02-12 16:37:45 +01001011 --ps->queue_len;
Michal Vaskod8340032018-02-12 14:41:00 +01001012 if (found == 1) {
Michal Vasko103fe632018-02-12 16:37:45 +01001013 /* remove the id by moving the queue, otherwise all the values in the queue were moved */
Michal Vaskod8340032018-02-12 14:41:00 +01001014 ps->queue_begin = (ps->queue_begin + 1) % NC_PS_QUEUE_SIZE;
1015 }
Michal Vaskob30b99c2016-07-26 11:35:43 +02001016}
1017
Michal Vaskof04a52a2016-04-07 10:52:10 +02001018int
Michal Vasko26043172016-07-26 14:08:59 +02001019nc_ps_lock(struct nc_pollsession *ps, uint8_t *id, const char *func)
Michal Vaskobe86fe32016-04-07 10:43:03 +02001020{
1021 int ret;
Michal Vaskobe86fe32016-04-07 10:43:03 +02001022 struct timespec ts;
1023
Michal Vaskod8a74192023-02-06 15:51:50 +01001024 nc_timeouttime_get(&ts, NC_PS_LOCK_TIMEOUT);
Michal Vaskobe86fe32016-04-07 10:43:03 +02001025
1026 /* LOCK */
Michal Vaskod8a74192023-02-06 15:51:50 +01001027 ret = pthread_mutex_clocklock(&ps->lock, COMPAT_CLOCK_ID, &ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +02001028 if (ret) {
Michal Vasko05532772021-06-03 12:12:38 +02001029 ERR(NULL, "%s: failed to lock a pollsession (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +02001030 return -1;
1031 }
1032
Michal Vasko74c345f2018-02-07 10:37:11 +01001033 /* check that the queue is long enough */
Michal Vasko8bc747c2018-02-09 16:37:04 +01001034 if (ps->queue_len == NC_PS_QUEUE_SIZE) {
Michal Vasko05532772021-06-03 12:12:38 +02001035 ERR(NULL, "%s: pollsession queue size (%d) too small.", func, NC_PS_QUEUE_SIZE);
Michal Vasko8bc747c2018-02-09 16:37:04 +01001036 pthread_mutex_unlock(&ps->lock);
1037 return -1;
1038 }
Michal Vasko74c345f2018-02-07 10:37:11 +01001039
1040 /* add ourselves into the queue */
1041 nc_ps_queue_add_id(ps, id);
Michal Vaskofdba4a32022-01-05 12:13:53 +01001042 DBL(NULL, "PS 0x%p TID %lu queue: added %u, head %u, length %u", ps, (long unsigned int)pthread_self(), *id,
Michal Vasko91290952019-09-27 11:30:55 +02001043 ps->queue[ps->queue_begin], ps->queue_len);
Michal Vaskobe86fe32016-04-07 10:43:03 +02001044
1045 /* is it our turn? */
Michal Vaskob30b99c2016-07-26 11:35:43 +02001046 while (ps->queue[ps->queue_begin] != *id) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001047 nc_timeouttime_get(&ts, NC_PS_QUEUE_TIMEOUT);
Michal Vaskobe86fe32016-04-07 10:43:03 +02001048
Michal Vaskod8a74192023-02-06 15:51:50 +01001049 ret = pthread_cond_clockwait(&ps->cond, &ps->lock, COMPAT_CLOCK_ID, &ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +02001050 if (ret) {
preetbhansalif3edf492018-12-14 17:53:32 +05301051 /**
1052 * This may happen when another thread releases the lock and broadcasts the condition
1053 * and this thread had already timed out. When this thread is scheduled, it returns timed out error
1054 * but when actually this thread was ready for condition.
1055 */
preetbhansali629dfc42018-12-17 16:04:40 +05301056 if ((ETIMEDOUT == ret) && (ps->queue[ps->queue_begin] == *id)) {
preetbhansalif3edf492018-12-14 17:53:32 +05301057 break;
1058 }
Michal Vasko66032bc2019-01-22 15:03:12 +01001059
Michal Vasko05532772021-06-03 12:12:38 +02001060 ERR(NULL, "%s: failed to wait for a pollsession condition (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +02001061 /* remove ourselves from the queue */
Michal Vaskob30b99c2016-07-26 11:35:43 +02001062 nc_ps_queue_remove_id(ps, *id);
1063 pthread_mutex_unlock(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +02001064 return -1;
1065 }
1066 }
1067
Michal Vaskobe86fe32016-04-07 10:43:03 +02001068 /* UNLOCK */
1069 pthread_mutex_unlock(&ps->lock);
1070
1071 return 0;
1072}
1073
Michal Vaskof04a52a2016-04-07 10:52:10 +02001074int
Michal Vasko26043172016-07-26 14:08:59 +02001075nc_ps_unlock(struct nc_pollsession *ps, uint8_t id, const char *func)
Michal Vaskobe86fe32016-04-07 10:43:03 +02001076{
1077 int ret;
1078 struct timespec ts;
1079
Michal Vaskod8a74192023-02-06 15:51:50 +01001080 nc_timeouttime_get(&ts, NC_PS_LOCK_TIMEOUT);
Michal Vaskobe86fe32016-04-07 10:43:03 +02001081
1082 /* LOCK */
Michal Vaskod8a74192023-02-06 15:51:50 +01001083 ret = pthread_mutex_clocklock(&ps->lock, COMPAT_CLOCK_ID, &ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +02001084 if (ret) {
Michal Vasko05532772021-06-03 12:12:38 +02001085 ERR(NULL, "%s: failed to lock a pollsession (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +02001086 ret = -1;
1087 }
1088
Michal Vaskob30b99c2016-07-26 11:35:43 +02001089 /* we must be the first, it was our turn after all, right? */
1090 if (ps->queue[ps->queue_begin] != id) {
1091 ERRINT;
Michal Vaskob1a094b2016-10-05 14:04:52 +02001092 /* UNLOCK */
1093 if (!ret) {
1094 pthread_mutex_unlock(&ps->lock);
1095 }
Michal Vaskob30b99c2016-07-26 11:35:43 +02001096 return -1;
1097 }
1098
Michal Vaskobe86fe32016-04-07 10:43:03 +02001099 /* remove ourselves from the queue */
Michal Vaskob30b99c2016-07-26 11:35:43 +02001100 nc_ps_queue_remove_id(ps, id);
Michal Vaskofdba4a32022-01-05 12:13:53 +01001101 DBL(NULL, "PS 0x%p TID %lu queue: removed %u, head %u, length %u", ps, (long unsigned int)pthread_self(), id,
Michal Vasko91290952019-09-27 11:30:55 +02001102 ps->queue[ps->queue_begin], ps->queue_len);
Michal Vaskobe86fe32016-04-07 10:43:03 +02001103
1104 /* broadcast to all other threads that the queue moved */
1105 pthread_cond_broadcast(&ps->cond);
1106
Michal Vaskobe86fe32016-04-07 10:43:03 +02001107 /* UNLOCK */
1108 if (!ret) {
1109 pthread_mutex_unlock(&ps->lock);
1110 }
1111
1112 return ret;
1113}
1114
Michal Vasko428087d2016-01-14 16:04:28 +01001115API struct nc_pollsession *
1116nc_ps_new(void)
1117{
Michal Vasko48a63ed2016-03-01 09:48:21 +01001118 struct nc_pollsession *ps;
1119
1120 ps = calloc(1, sizeof(struct nc_pollsession));
Michal Vasko4eb3c312016-03-01 14:09:37 +01001121 if (!ps) {
1122 ERRMEM;
1123 return NULL;
1124 }
Michal Vaskobe86fe32016-04-07 10:43:03 +02001125 pthread_cond_init(&ps->cond, NULL);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001126 pthread_mutex_init(&ps->lock, NULL);
1127
1128 return ps;
Michal Vasko428087d2016-01-14 16:04:28 +01001129}
1130
1131API void
1132nc_ps_free(struct nc_pollsession *ps)
1133{
fanchanghu3d4e7212017-08-09 09:42:30 +08001134 uint16_t i;
1135
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001136 if (!ps) {
1137 return;
1138 }
1139
Michal Vaskobe86fe32016-04-07 10:43:03 +02001140 if (ps->queue_len) {
Michal Vasko05532772021-06-03 12:12:38 +02001141 ERR(NULL, "FATAL: Freeing a pollsession structure that is currently being worked with!");
Michal Vaskobe86fe32016-04-07 10:43:03 +02001142 }
1143
fanchanghu3d4e7212017-08-09 09:42:30 +08001144 for (i = 0; i < ps->session_count; i++) {
1145 free(ps->sessions[i]);
1146 }
1147
Michal Vasko428087d2016-01-14 16:04:28 +01001148 free(ps->sessions);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001149 pthread_mutex_destroy(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +02001150 pthread_cond_destroy(&ps->cond);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001151
Michal Vasko428087d2016-01-14 16:04:28 +01001152 free(ps);
1153}
1154
1155API int
1156nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session)
1157{
Michal Vaskob30b99c2016-07-26 11:35:43 +02001158 uint8_t q_id;
1159
Michal Vasko45e53ae2016-04-07 11:46:03 +02001160 if (!ps) {
1161 ERRARG("ps");
1162 return -1;
1163 } else if (!session) {
1164 ERRARG("session");
Michal Vasko428087d2016-01-14 16:04:28 +01001165 return -1;
1166 }
1167
Michal Vasko48a63ed2016-03-01 09:48:21 +01001168 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001169 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +02001170 return -1;
1171 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001172
Michal Vasko428087d2016-01-14 16:04:28 +01001173 ++ps->session_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001174 ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions);
Michal Vasko9a327362017-01-11 11:31:46 +01001175 if (!ps->sessions) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001176 ERRMEM;
1177 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001178 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001179 return -1;
1180 }
fanchanghu3d4e7212017-08-09 09:42:30 +08001181 ps->sessions[ps->session_count - 1] = calloc(1, sizeof **ps->sessions);
1182 if (!ps->sessions[ps->session_count - 1]) {
1183 ERRMEM;
1184 --ps->session_count;
1185 /* UNLOCK */
1186 nc_ps_unlock(ps, q_id, __func__);
1187 return -1;
1188 }
1189 ps->sessions[ps->session_count - 1]->session = session;
1190 ps->sessions[ps->session_count - 1]->state = NC_PS_STATE_NONE;
Michal Vasko428087d2016-01-14 16:04:28 +01001191
Michal Vasko48a63ed2016-03-01 09:48:21 +01001192 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001193 return nc_ps_unlock(ps, q_id, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +01001194}
1195
Michal Vasko48a63ed2016-03-01 09:48:21 +01001196static int
Radek Krejcid5f978f2016-03-03 13:14:45 +01001197_nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session, int index)
Michal Vasko428087d2016-01-14 16:04:28 +01001198{
1199 uint16_t i;
1200
Radek Krejcid5f978f2016-03-03 13:14:45 +01001201 if (index >= 0) {
1202 i = (uint16_t)index;
1203 goto remove;
1204 }
Michal Vasko428087d2016-01-14 16:04:28 +01001205 for (i = 0; i < ps->session_count; ++i) {
fanchanghu3d4e7212017-08-09 09:42:30 +08001206 if (ps->sessions[i]->session == session) {
Radek Krejcid5f978f2016-03-03 13:14:45 +01001207remove:
Michal Vasko428087d2016-01-14 16:04:28 +01001208 --ps->session_count;
fanchanghu3d4e7212017-08-09 09:42:30 +08001209 if (i <= ps->session_count) {
1210 free(ps->sessions[i]);
Michal Vasko58005732016-02-02 15:50:52 +01001211 ps->sessions[i] = ps->sessions[ps->session_count];
fanchanghu3d4e7212017-08-09 09:42:30 +08001212 }
1213 if (!ps->session_count) {
Michal Vasko58005732016-02-02 15:50:52 +01001214 free(ps->sessions);
1215 ps->sessions = NULL;
Michal Vasko58005732016-02-02 15:50:52 +01001216 }
Michal Vasko11d2f6a2017-02-02 11:15:06 +01001217 ps->last_event_session = 0;
Michal Vasko428087d2016-01-14 16:04:28 +01001218 return 0;
1219 }
1220 }
1221
Michal Vaskof0537d82016-01-29 14:42:38 +01001222 return -1;
Michal Vasko428087d2016-01-14 16:04:28 +01001223}
1224
Michal Vasko48a63ed2016-03-01 09:48:21 +01001225API int
1226nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session)
1227{
Michal Vaskob30b99c2016-07-26 11:35:43 +02001228 uint8_t q_id;
Michal Vaskobe86fe32016-04-07 10:43:03 +02001229 int ret, ret2;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001230
Michal Vasko45e53ae2016-04-07 11:46:03 +02001231 if (!ps) {
1232 ERRARG("ps");
1233 return -1;
1234 } else if (!session) {
1235 ERRARG("session");
Michal Vasko48a63ed2016-03-01 09:48:21 +01001236 return -1;
1237 }
1238
1239 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001240 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +02001241 return -1;
1242 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001243
Radek Krejcid5f978f2016-03-03 13:14:45 +01001244 ret = _nc_ps_del_session(ps, session, -1);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001245
1246 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001247 ret2 = nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001248
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001249 return ret || ret2 ? -1 : 0;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001250}
1251
Michal Vaskoe1ee05b2017-03-21 10:10:18 +01001252API struct nc_session *
Michal Vasko4871c9d2017-10-09 14:48:39 +02001253nc_ps_get_session(const struct nc_pollsession *ps, uint16_t idx)
Michal Vaskoe1ee05b2017-03-21 10:10:18 +01001254{
1255 uint8_t q_id;
Michal Vaskoe1ee05b2017-03-21 10:10:18 +01001256 struct nc_session *ret = NULL;
1257
1258 if (!ps) {
1259 ERRARG("ps");
1260 return NULL;
1261 }
1262
1263 /* LOCK */
1264 if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) {
1265 return NULL;
1266 }
1267
Michal Vasko4871c9d2017-10-09 14:48:39 +02001268 if (idx < ps->session_count) {
1269 ret = ps->sessions[idx]->session;
Michal Vaskoe1ee05b2017-03-21 10:10:18 +01001270 }
1271
1272 /* UNLOCK */
1273 nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__);
1274
1275 return ret;
1276}
1277
Michal Vasko3ec3b112022-07-21 12:32:33 +02001278API struct nc_session *
1279nc_ps_find_session(const struct nc_pollsession *ps, nc_ps_session_match_cb match_cb, void *cb_data)
1280{
1281 uint8_t q_id;
1282 uint16_t i;
1283 struct nc_session *ret = NULL;
1284
1285 if (!ps) {
1286 ERRARG("ps");
1287 return NULL;
1288 }
1289
1290 /* LOCK */
1291 if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) {
1292 return NULL;
1293 }
1294
1295 for (i = 0; i < ps->session_count; ++i) {
1296 if (match_cb(ps->sessions[i]->session, cb_data)) {
1297 ret = ps->sessions[i]->session;
1298 break;
1299 }
1300 }
1301
1302 /* UNLOCK */
1303 nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__);
1304
1305 return ret;
1306}
1307
Michal Vasko0fdb7ac2016-03-01 09:03:12 +01001308API uint16_t
1309nc_ps_session_count(struct nc_pollsession *ps)
1310{
Michal Vasko47003942019-03-14 12:25:23 +01001311 uint8_t q_id;
1312 uint16_t session_count;
1313
Michal Vasko0fdb7ac2016-03-01 09:03:12 +01001314 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001315 ERRARG("ps");
Michal Vasko0fdb7ac2016-03-01 09:03:12 +01001316 return 0;
1317 }
1318
Michal Vasko47003942019-03-14 12:25:23 +01001319 /* LOCK (just for memory barrier so that we read the current value) */
1320 if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) {
1321 return 0;
1322 }
1323
1324 session_count = ps->session_count;
1325
1326 /* UNLOCK */
1327 nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__);
1328
1329 return session_count;
Michal Vasko0fdb7ac2016-03-01 09:03:12 +01001330}
1331
Michal Vasko77e83572022-07-21 15:31:15 +02001332static NC_MSG_TYPE
1333recv_rpc_check_msgid(struct nc_session *session, const struct lyd_node *envp)
1334{
1335 struct lyd_attr *attr;
1336
1337 assert(envp && !envp->schema);
1338
1339 /* find the message-id attribute */
1340 LY_LIST_FOR(((struct lyd_node_opaq *)envp)->attr, attr) {
1341 if (!strcmp(attr->name.name, "message-id")) {
1342 break;
1343 }
1344 }
1345
1346 if (!attr) {
1347 ERR(session, "Received an <rpc> without a message-id.");
1348 return NC_MSG_REPLY_ERR_MSGID;
1349 }
1350
1351 return NC_MSG_RPC;
1352}
1353
Michal Vasko131120a2018-05-29 15:44:02 +02001354/* should be called holding the session RPC lock! IO lock will be acquired as needed
Michal Vasko71090fc2016-05-24 16:37:28 +02001355 * returns: NC_PSPOLL_ERROR,
Michal Vasko77367452021-02-16 16:32:18 +01001356 * NC_PSPOLL_TIMEOUT,
Michal Vasko77e83572022-07-21 15:31:15 +02001357 * NC_PSPOLL_BAD_RPC,
Michal Vasko71090fc2016-05-24 16:37:28 +02001358 * NC_PSPOLL_RPC
1359 */
1360static int
Michal Vasko131120a2018-05-29 15:44:02 +02001361nc_server_recv_rpc_io(struct nc_session *session, int io_timeout, struct nc_server_rpc **rpc)
Michal Vasko428087d2016-01-14 16:04:28 +01001362{
Michal Vasko77367452021-02-16 16:32:18 +01001363 struct ly_in *msg;
Radek Krejcif93c7d42016-04-06 13:41:15 +02001364 struct nc_server_reply *reply = NULL;
Michal Vasko939ffce2021-04-12 13:02:01 +02001365 struct lyd_node *e;
Michal Vasko77367452021-02-16 16:32:18 +01001366 int r, ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001367
Michal Vasko45e53ae2016-04-07 11:46:03 +02001368 if (!session) {
1369 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001370 return NC_PSPOLL_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001371 } else if (!rpc) {
1372 ERRARG("rpc");
Michal Vasko71090fc2016-05-24 16:37:28 +02001373 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001374 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) {
Michal Vasko05532772021-06-03 12:12:38 +02001375 ERR(session, "Invalid session to receive RPCs.");
Michal Vasko71090fc2016-05-24 16:37:28 +02001376 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001377 }
1378
Michal Vasko93224072021-11-09 12:14:28 +01001379 *rpc = NULL;
1380
Michal Vasko77367452021-02-16 16:32:18 +01001381 /* get a message */
1382 r = nc_read_msg_io(session, io_timeout, &msg, 0);
1383 if (r == -2) {
1384 /* malformed message */
Michal Vasko77e83572022-07-21 15:31:15 +02001385 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko93224072021-11-09 12:14:28 +01001386 reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_MALFORMED_MSG));
Michal Vasko77e83572022-07-21 15:31:15 +02001387 goto cleanup;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001388 }
1389 if (r == -1) {
Michal Vasko77367452021-02-16 16:32:18 +01001390 return NC_PSPOLL_ERROR;
1391 } else if (!r) {
1392 return NC_PSPOLL_TIMEOUT;
Michal Vasko428087d2016-01-14 16:04:28 +01001393 }
1394
Michal Vasko77367452021-02-16 16:32:18 +01001395 *rpc = calloc(1, sizeof **rpc);
1396 if (!*rpc) {
1397 ERRMEM;
Michal Vasko77e83572022-07-21 15:31:15 +02001398 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko77367452021-02-16 16:32:18 +01001399 goto cleanup;
1400 }
1401
1402 /* parse the RPC */
Michal Vasko77e83572022-07-21 15:31:15 +02001403 if (!lyd_parse_op(session->ctx, NULL, msg, LYD_XML, LYD_TYPE_RPC_NETCONF, &(*rpc)->envp, &(*rpc)->rpc)) {
1404 /* check message-id */
1405 if (recv_rpc_check_msgid(session, (*rpc)->envp) == NC_MSG_RPC) {
1406 /* valid RPC */
1407 ret = NC_PSPOLL_RPC;
1408 } else {
1409 /* no message-id */
1410 ret = NC_PSPOLL_BAD_RPC;
1411 reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_MISSING_ATTR, NC_ERR_TYPE_RPC, "message-id", "rpc"));
1412 }
1413 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001414 /* bad RPC received */
Michal Vasko77e83572022-07-21 15:31:15 +02001415 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko77367452021-02-16 16:32:18 +01001416
1417 if ((*rpc)->envp) {
1418 /* at least the envelopes were parsed */
Michal Vasko93224072021-11-09 12:14:28 +01001419 e = nc_err(session->ctx, NC_ERR_OP_FAILED, NC_ERR_TYPE_APP);
1420 nc_err_set_msg(e, ly_errmsg(session->ctx), "en");
Michal Vasko939ffce2021-04-12 13:02:01 +02001421 reply = nc_server_reply_err(e);
Michal Vasko77367452021-02-16 16:32:18 +01001422 } else if (session->version == NC_VERSION_11) {
Michal Vasko93224072021-11-09 12:14:28 +01001423 /* completely malformed message, NETCONF version 1.1 defines sending error reply from
1424 * the server (RFC 6241 sec. 3) */
1425 reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_MALFORMED_MSG));
Michal Vasko77367452021-02-16 16:32:18 +01001426 }
Michal Vasko77367452021-02-16 16:32:18 +01001427 }
1428
1429cleanup:
Michal Vasko77e83572022-07-21 15:31:15 +02001430 if (reply) {
1431 /* send error reply */
1432 r = nc_write_msg_io(session, io_timeout, NC_MSG_REPLY, *rpc ? (*rpc)->envp : NULL, reply);
1433 nc_server_reply_free(reply);
1434 if (r != NC_MSG_REPLY) {
1435 ERR(session, "Failed to write reply (%s), terminating session.", nc_msgtype2str[r]);
1436 if (session->status != NC_STATUS_INVALID) {
1437 session->status = NC_STATUS_INVALID;
1438 session->term_reason = NC_SESSION_TERM_OTHER;
1439 }
1440 }
1441 }
1442
Michal Vasko77367452021-02-16 16:32:18 +01001443 ly_in_free(msg, 1);
1444 if (ret != NC_PSPOLL_RPC) {
1445 nc_server_rpc_free(*rpc);
1446 *rpc = NULL;
1447 }
Michal Vasko71090fc2016-05-24 16:37:28 +02001448 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001449}
1450
fanchanghu966f2de2016-07-21 02:28:57 -04001451API void
1452nc_set_global_rpc_clb(nc_rpc_clb clb)
1453{
1454 global_rpc_clb = clb;
1455}
1456
Radek Krejci93e80222016-10-03 13:34:25 +02001457API NC_MSG_TYPE
1458nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout)
1459{
Michal Vasko131120a2018-05-29 15:44:02 +02001460 NC_MSG_TYPE ret;
Radek Krejci93e80222016-10-03 13:34:25 +02001461
1462 /* check parameters */
Michal Vaskodf68e7e2022-04-21 11:04:00 +02001463 if (!session || (session->side != NC_SERVER) || !nc_session_get_notif_status(session)) {
Radek Krejci93e80222016-10-03 13:34:25 +02001464 ERRARG("session");
1465 return NC_MSG_ERROR;
Michal Vasko77367452021-02-16 16:32:18 +01001466 } else if (!notif || !notif->ntf || !notif->eventtime) {
Radek Krejci93e80222016-10-03 13:34:25 +02001467 ERRARG("notif");
1468 return NC_MSG_ERROR;
1469 }
1470
Michal Vasko131120a2018-05-29 15:44:02 +02001471 /* we do not need RPC lock for this, IO lock will be acquired properly */
1472 ret = nc_write_msg_io(session, timeout, NC_MSG_NOTIF, notif);
Michal Vasko8fe604c2020-02-10 15:25:04 +01001473 if (ret != NC_MSG_NOTIF) {
Michal Vasko05532772021-06-03 12:12:38 +02001474 ERR(session, "Failed to write notification (%s).", nc_msgtype2str[ret]);
Radek Krejci93e80222016-10-03 13:34:25 +02001475 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001476
Michal Vasko131120a2018-05-29 15:44:02 +02001477 return ret;
Radek Krejci93e80222016-10-03 13:34:25 +02001478}
1479
Michal Vaskof9467762023-03-28 09:02:08 +02001480/**
1481 * @brief Send a reply acquiring IO lock as needed.
1482 * Session RPC lock must be held!
1483 *
1484 * @param[in] session Session to use.
1485 * @param[in] io_timeout Timeout to use for acquiring IO lock.
1486 * @param[in] rpc RPC to sent.
1487 * @return 0 on success.
1488 * @return Bitmask of NC_PSPOLL_ERROR (any fatal error) and NC_PSPOLL_REPLY_ERROR (reply failed to be sent).
1489 * @return NC_PSPOLL_ERROR on other errors.
Michal Vasko71090fc2016-05-24 16:37:28 +02001490 */
1491static int
Michal Vasko93224072021-11-09 12:14:28 +01001492nc_server_send_reply_io(struct nc_session *session, int io_timeout, const struct nc_server_rpc *rpc)
Michal Vasko428087d2016-01-14 16:04:28 +01001493{
1494 nc_rpc_clb clb;
1495 struct nc_server_reply *reply;
Michal Vasko77367452021-02-16 16:32:18 +01001496 const struct lysc_node *rpc_act = NULL;
1497 struct lyd_node *elem;
Michal Vasko131120a2018-05-29 15:44:02 +02001498 int ret = 0;
1499 NC_MSG_TYPE r;
Michal Vasko428087d2016-01-14 16:04:28 +01001500
Michal Vasko4a827e52016-03-03 10:59:00 +01001501 if (!rpc) {
1502 ERRINT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001503 return NC_PSPOLL_ERROR;
Michal Vasko4a827e52016-03-03 10:59:00 +01001504 }
1505
Michal Vasko77367452021-02-16 16:32:18 +01001506 if (rpc->rpc->schema->nodetype == LYS_RPC) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001507 /* RPC */
Michal Vasko77367452021-02-16 16:32:18 +01001508 rpc_act = rpc->rpc->schema;
fanchanghu966f2de2016-07-21 02:28:57 -04001509 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +02001510 /* action */
Michal Vasko77367452021-02-16 16:32:18 +01001511 LYD_TREE_DFS_BEGIN(rpc->rpc, elem) {
Michal Vasko90e8e692016-07-13 12:27:57 +02001512 if (elem->schema->nodetype == LYS_ACTION) {
1513 rpc_act = elem->schema;
1514 break;
1515 }
Michal Vasko77367452021-02-16 16:32:18 +01001516 LYD_TREE_DFS_END(rpc->rpc, elem);
fanchanghu966f2de2016-07-21 02:28:57 -04001517 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001518 if (!rpc_act) {
1519 ERRINT;
1520 return NC_PSPOLL_ERROR;
1521 }
1522 }
1523
1524 if (!rpc_act->priv) {
Petr A. Sokolov16284ca2019-02-04 09:02:40 +03001525 if (!global_rpc_clb) {
1526 /* no callback, reply with a not-implemented error */
Michal Vasko93224072021-11-09 12:14:28 +01001527 reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT));
Petr A. Sokolov16284ca2019-02-04 09:02:40 +03001528 } else {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001529 reply = global_rpc_clb(rpc->rpc, session);
Petr A. Sokolov16284ca2019-02-04 09:02:40 +03001530 }
Michal Vasko428087d2016-01-14 16:04:28 +01001531 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +02001532 clb = (nc_rpc_clb)rpc_act->priv;
Michal Vasko77367452021-02-16 16:32:18 +01001533 reply = clb(rpc->rpc, session);
Michal Vasko428087d2016-01-14 16:04:28 +01001534 }
1535
1536 if (!reply) {
Michal Vasko93224072021-11-09 12:14:28 +01001537 reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_OP_FAILED, NC_ERR_TYPE_APP));
Michal Vasko428087d2016-01-14 16:04:28 +01001538 }
Michal Vasko77367452021-02-16 16:32:18 +01001539 r = nc_write_msg_io(session, io_timeout, NC_MSG_REPLY, rpc->envp, reply);
Michal Vasko71090fc2016-05-24 16:37:28 +02001540 if (reply->type == NC_RPL_ERROR) {
1541 ret |= NC_PSPOLL_REPLY_ERROR;
1542 }
1543 nc_server_reply_free(reply);
Michal Vasko428087d2016-01-14 16:04:28 +01001544
Michal Vasko131120a2018-05-29 15:44:02 +02001545 if (r != NC_MSG_REPLY) {
Michal Vasko15469492021-06-09 08:40:48 +02001546 ERR(session, "Failed to write reply (%s).", nc_msgtype2str[r]);
Michal Vasko71090fc2016-05-24 16:37:28 +02001547 ret |= NC_PSPOLL_ERROR;
1548 }
Michal Vasko428087d2016-01-14 16:04:28 +01001549
1550 /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */
1551 if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) {
1552 session->status = NC_STATUS_INVALID;
1553 }
1554
Michal Vasko71090fc2016-05-24 16:37:28 +02001555 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001556}
1557
Michal Vaskof9467762023-03-28 09:02:08 +02001558/**
1559 * @brief Poll a session from pspoll acquiring IO lock as needed.
1560 * Session must be running and session RPC lock held!
1561 *
1562 * @param[in] session Session to use.
1563 * @param[in] io_timeout Timeout to use for acquiring IO lock.
1564 * @param[in] now_mono Current monotonic timestamp.
1565 * @param[in,out] msg Message to fill in case of an error.
1566 * @return NC_PSPOLL_RPC if some application data are available.
1567 * @return NC_PSPOLL_TIMEOUT if a timeout elapsed.
1568 * @return NC_PSPOLL_SSH_CHANNEL if a new SSH channel has been created.
1569 * @return NC_PSPOLL_SSH_MSG if just an SSH message has been processed.
1570 * @return NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR if session has been terminated (@p msg filled).
1571 * @return NC_PSPOLL_ERROR on other fatal errors (@p msg filled).
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001572 */
1573static int
Michal Vasko131120a2018-05-29 15:44:02 +02001574nc_ps_poll_session_io(struct nc_session *session, int io_timeout, time_t now_mono, char *msg)
Michal Vasko428087d2016-01-14 16:04:28 +01001575{
Michal Vasko9a327362017-01-11 11:31:46 +01001576 struct pollfd pfd;
Michal Vasko2260f552018-06-06 10:06:12 +02001577 int r, ret = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001578
Michal Vasko9a327362017-01-11 11:31:46 +01001579#ifdef NC_ENABLED_SSH
1580 struct nc_session *new;
1581#endif
Michal Vasko428087d2016-01-14 16:04:28 +01001582
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001583 /* check timeout first */
Michal Vaskodf68e7e2022-04-21 11:04:00 +02001584 if (!(session->flags & NC_SESSION_CALLHOME) && !nc_session_get_notif_status(session) && server_opts.idle_timeout &&
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001585 (now_mono >= session->opts.server.last_rpc + server_opts.idle_timeout)) {
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001586 sprintf(msg, "session idle timeout elapsed");
1587 session->status = NC_STATUS_INVALID;
1588 session->term_reason = NC_SESSION_TERM_TIMEOUT;
1589 return NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1590 }
1591
Michal Vasko131120a2018-05-29 15:44:02 +02001592 r = nc_session_io_lock(session, io_timeout, __func__);
1593 if (r < 0) {
1594 sprintf(msg, "session IO lock failed to be acquired");
1595 return NC_PSPOLL_ERROR;
1596 } else if (!r) {
1597 return NC_PSPOLL_TIMEOUT;
1598 }
1599
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001600 switch (session->ti_type) {
1601#ifdef NC_ENABLED_SSH
1602 case NC_TI_LIBSSH:
1603 r = ssh_channel_poll_timeout(session->ti.libssh.channel, 0, 0);
Michal Vasko8dcaa882017-10-19 14:28:42 +02001604 if (r == SSH_EOF) {
1605 sprintf(msg, "SSH channel unexpected EOF");
1606 session->status = NC_STATUS_INVALID;
1607 session->term_reason = NC_SESSION_TERM_DROPPED;
1608 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1609 } else if (r == SSH_ERROR) {
1610 sprintf(msg, "SSH channel poll error (%s)", ssh_get_error(session->ti.libssh.session));
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001611 session->status = NC_STATUS_INVALID;
1612 session->term_reason = NC_SESSION_TERM_OTHER;
1613 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
Michal Vasko8dcaa882017-10-19 14:28:42 +02001614 } else if (!r) {
1615 if (session->flags & NC_SESSION_SSH_NEW_MSG) {
1616 /* new SSH message */
1617 session->flags &= ~NC_SESSION_SSH_NEW_MSG;
1618 if (session->ti.libssh.next) {
1619 for (new = session->ti.libssh.next; new != session; new = new->ti.libssh.next) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001620 if ((new->status == NC_STATUS_STARTING) && new->ti.libssh.channel &&
1621 (new->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
Michal Vasko8dcaa882017-10-19 14:28:42 +02001622 /* new NETCONF SSH channel */
1623 ret = NC_PSPOLL_SSH_CHANNEL;
1624 break;
1625 }
1626 }
1627 if (new != session) {
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001628 break;
1629 }
1630 }
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001631
Michal Vasko8dcaa882017-10-19 14:28:42 +02001632 /* just some SSH message */
1633 ret = NC_PSPOLL_SSH_MSG;
1634 } else {
1635 ret = NC_PSPOLL_TIMEOUT;
1636 }
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001637 } else {
1638 /* we have some application data */
1639 ret = NC_PSPOLL_RPC;
1640 }
1641 break;
1642#endif
1643#ifdef NC_ENABLED_TLS
1644 case NC_TI_OPENSSL:
1645 r = SSL_pending(session->ti.tls);
1646 if (!r) {
1647 /* no data pending in the SSL buffer, poll fd */
1648 pfd.fd = SSL_get_rfd(session->ti.tls);
1649 if (pfd.fd < 0) {
1650 sprintf(msg, "internal error (%s:%d)", __FILE__, __LINE__);
1651 ret = NC_PSPOLL_ERROR;
1652 break;
1653 }
1654 pfd.events = POLLIN;
1655 pfd.revents = 0;
1656 r = poll(&pfd, 1, 0);
1657
1658 if ((r < 0) && (errno != EINTR)) {
1659 sprintf(msg, "poll failed (%s)", strerror(errno));
1660 session->status = NC_STATUS_INVALID;
1661 ret = NC_PSPOLL_ERROR;
1662 } else if (r > 0) {
1663 if (pfd.revents & (POLLHUP | POLLNVAL)) {
1664 sprintf(msg, "communication socket unexpectedly closed");
1665 session->status = NC_STATUS_INVALID;
1666 session->term_reason = NC_SESSION_TERM_DROPPED;
1667 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1668 } else if (pfd.revents & POLLERR) {
1669 sprintf(msg, "communication socket error");
1670 session->status = NC_STATUS_INVALID;
1671 session->term_reason = NC_SESSION_TERM_OTHER;
1672 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1673 } else {
1674 ret = NC_PSPOLL_RPC;
1675 }
1676 } else {
1677 ret = NC_PSPOLL_TIMEOUT;
1678 }
1679 } else {
1680 ret = NC_PSPOLL_RPC;
1681 }
1682 break;
1683#endif
1684 case NC_TI_FD:
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001685 case NC_TI_UNIX:
1686 pfd.fd = (session->ti_type == NC_TI_FD) ? session->ti.fd.in : session->ti.unixsock.sock;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001687 pfd.events = POLLIN;
1688 pfd.revents = 0;
1689 r = poll(&pfd, 1, 0);
1690
1691 if ((r < 0) && (errno != EINTR)) {
1692 sprintf(msg, "poll failed (%s)", strerror(errno));
1693 session->status = NC_STATUS_INVALID;
1694 ret = NC_PSPOLL_ERROR;
1695 } else if (r > 0) {
1696 if (pfd.revents & (POLLHUP | POLLNVAL)) {
1697 sprintf(msg, "communication socket unexpectedly closed");
1698 session->status = NC_STATUS_INVALID;
1699 session->term_reason = NC_SESSION_TERM_DROPPED;
1700 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1701 } else if (pfd.revents & POLLERR) {
1702 sprintf(msg, "communication socket error");
1703 session->status = NC_STATUS_INVALID;
1704 session->term_reason = NC_SESSION_TERM_OTHER;
1705 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1706 } else {
1707 ret = NC_PSPOLL_RPC;
1708 }
1709 } else {
1710 ret = NC_PSPOLL_TIMEOUT;
1711 }
1712 break;
1713 case NC_TI_NONE:
1714 sprintf(msg, "internal error (%s:%d)", __FILE__, __LINE__);
1715 ret = NC_PSPOLL_ERROR;
1716 break;
1717 }
1718
Michal Vasko131120a2018-05-29 15:44:02 +02001719 nc_session_io_unlock(session, __func__);
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001720 return ret;
1721}
1722
1723API int
1724nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session)
1725{
Michal Vasko443faa02022-10-20 09:09:03 +02001726 int ret = NC_PSPOLL_ERROR, r;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001727 uint8_t q_id;
1728 uint16_t i, j;
1729 char msg[256];
1730 struct timespec ts_timeout, ts_cur;
1731 struct nc_session *cur_session;
fanchanghu3d4e7212017-08-09 09:42:30 +08001732 struct nc_ps_session *cur_ps_session;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001733 struct nc_server_rpc *rpc = NULL;
1734
Michal Vasko30a5d6b2017-02-15 14:29:39 +01001735 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001736 ERRARG("ps");
Michal Vasko71090fc2016-05-24 16:37:28 +02001737 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001738 }
1739
Michal Vaskoade892d2017-02-22 13:40:35 +01001740 /* PS LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001741 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001742 return NC_PSPOLL_ERROR;
Michal Vaskobe86fe32016-04-07 10:43:03 +02001743 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001744
Michal Vaskoade892d2017-02-22 13:40:35 +01001745 if (!ps->session_count) {
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001746 nc_ps_unlock(ps, q_id, __func__);
1747 return NC_PSPOLL_NOSESSIONS;
Michal Vaskoade892d2017-02-22 13:40:35 +01001748 }
Michal Vaskobd8ef262016-01-20 11:09:27 +01001749
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001750 /* fill timespecs */
Michal Vaskod8a74192023-02-06 15:51:50 +01001751 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko36c7be82017-02-22 13:37:59 +01001752 if (timeout > -1) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001753 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001754 }
1755
1756 /* poll all the sessions one-by-one */
Michal Vasko9a327362017-01-11 11:31:46 +01001757 do {
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001758 /* loop from i to j once (all sessions) */
Michal Vasko9a327362017-01-11 11:31:46 +01001759 if (ps->last_event_session == ps->session_count - 1) {
1760 i = j = 0;
1761 } else {
1762 i = j = ps->last_event_session + 1;
Michal Vaskobd8ef262016-01-20 11:09:27 +01001763 }
Michal Vasko9a327362017-01-11 11:31:46 +01001764 do {
fanchanghu3d4e7212017-08-09 09:42:30 +08001765 cur_ps_session = ps->sessions[i];
1766 cur_session = cur_ps_session->session;
Michal Vasko428087d2016-01-14 16:04:28 +01001767
Michal Vasko131120a2018-05-29 15:44:02 +02001768 /* SESSION RPC LOCK */
1769 r = nc_session_rpc_lock(cur_session, 0, __func__);
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001770 if (r == -1) {
1771 ret = NC_PSPOLL_ERROR;
1772 } else if (r == 1) {
1773 /* no one else is currently working with the session, so we can, otherwise skip it */
Michal Vaskoc97cb162017-10-16 12:10:23 +02001774 switch (cur_ps_session->state) {
1775 case NC_PS_STATE_NONE:
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001776 if (cur_session->status == NC_STATUS_RUNNING) {
1777 /* session is fine, work with it */
fanchanghu3d4e7212017-08-09 09:42:30 +08001778 cur_ps_session->state = NC_PS_STATE_BUSY;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001779
Michal Vasko131120a2018-05-29 15:44:02 +02001780 ret = nc_ps_poll_session_io(cur_session, NC_SESSION_LOCK_TIMEOUT, ts_cur.tv_sec, msg);
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001781 switch (ret) {
1782 case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR:
Michal Vasko05532772021-06-03 12:12:38 +02001783 ERR(cur_session, "%s.", msg);
fanchanghu3d4e7212017-08-09 09:42:30 +08001784 cur_ps_session->state = NC_PS_STATE_INVALID;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001785 break;
1786 case NC_PSPOLL_ERROR:
Michal Vasko05532772021-06-03 12:12:38 +02001787 ERR(cur_session, "%s.", msg);
fanchanghu3d4e7212017-08-09 09:42:30 +08001788 cur_ps_session->state = NC_PS_STATE_NONE;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001789 break;
1790 case NC_PSPOLL_TIMEOUT:
1791#ifdef NC_ENABLED_SSH
1792 case NC_PSPOLL_SSH_CHANNEL:
1793 case NC_PSPOLL_SSH_MSG:
1794#endif
fanchanghu3d4e7212017-08-09 09:42:30 +08001795 cur_ps_session->state = NC_PS_STATE_NONE;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001796 break;
1797 case NC_PSPOLL_RPC:
1798 /* let's keep the state busy, we are not done with this session */
1799 break;
1800 }
1801 } else {
1802 /* session is not fine, let the caller know */
1803 ret = NC_PSPOLL_SESSION_TERM;
1804 if (cur_session->term_reason != NC_SESSION_TERM_CLOSED) {
1805 ret |= NC_PSPOLL_SESSION_ERROR;
1806 }
fanchanghu3d4e7212017-08-09 09:42:30 +08001807 cur_ps_session->state = NC_PS_STATE_INVALID;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001808 }
Michal Vaskoc97cb162017-10-16 12:10:23 +02001809 break;
1810 case NC_PS_STATE_BUSY:
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001811 /* it definitely should not be busy because we have the lock */
1812 ERRINT;
Michal Vasko4992d802017-08-09 12:20:01 +02001813 ret = NC_PSPOLL_ERROR;
Michal Vaskoc97cb162017-10-16 12:10:23 +02001814 break;
1815 case NC_PS_STATE_INVALID:
1816 /* we got it locked, but it will be freed, let it be */
1817 ret = NC_PSPOLL_TIMEOUT;
1818 break;
Michal Vasko428087d2016-01-14 16:04:28 +01001819 }
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001820
Michal Vasko131120a2018-05-29 15:44:02 +02001821 /* keep RPC lock in this one case */
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001822 if (ret != NC_PSPOLL_RPC) {
Michal Vasko131120a2018-05-29 15:44:02 +02001823 /* SESSION RPC UNLOCK */
1824 nc_session_rpc_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vaskoade892d2017-02-22 13:40:35 +01001825 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001826 } else {
1827 /* timeout */
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001828 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko428087d2016-01-14 16:04:28 +01001829 }
Michal Vasko428087d2016-01-14 16:04:28 +01001830
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001831 /* something happened */
1832 if (ret != NC_PSPOLL_TIMEOUT) {
1833 break;
1834 }
1835
Michal Vasko9a327362017-01-11 11:31:46 +01001836 if (i == ps->session_count - 1) {
1837 i = 0;
1838 } else {
1839 ++i;
1840 }
1841 } while (i != j);
1842
Michal Vaskoade892d2017-02-22 13:40:35 +01001843 /* no event, no session remains locked */
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001844 if (ret == NC_PSPOLL_TIMEOUT) {
Michal Vasko9a327362017-01-11 11:31:46 +01001845 usleep(NC_TIMEOUT_STEP);
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001846
Michal Vaskod8a74192023-02-06 15:51:50 +01001847 if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001848 /* final timeout */
1849 break;
Michal Vasko9a327362017-01-11 11:31:46 +01001850 }
Michal Vasko428087d2016-01-14 16:04:28 +01001851 }
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001852 } while (ret == NC_PSPOLL_TIMEOUT);
Michal Vasko428087d2016-01-14 16:04:28 +01001853
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001854 /* do we want to return the session? */
1855 switch (ret) {
1856 case NC_PSPOLL_RPC:
1857 case NC_PSPOLL_SESSION_TERM:
1858 case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR:
1859#ifdef NC_ENABLED_SSH
1860 case NC_PSPOLL_SSH_CHANNEL:
1861 case NC_PSPOLL_SSH_MSG:
1862#endif
1863 if (session) {
1864 *session = cur_session;
1865 }
1866 ps->last_event_session = i;
1867 break;
1868 default:
1869 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001870 }
Michal Vasko428087d2016-01-14 16:04:28 +01001871
Michal Vaskoade892d2017-02-22 13:40:35 +01001872 /* PS UNLOCK */
1873 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +01001874
Michal Vasko131120a2018-05-29 15:44:02 +02001875 /* we have some data available and the session is RPC locked (but not IO locked) */
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001876 if (ret == NC_PSPOLL_RPC) {
Michal Vasko131120a2018-05-29 15:44:02 +02001877 ret = nc_server_recv_rpc_io(cur_session, timeout, &rpc);
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001878 if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) {
1879 if (cur_session->status != NC_STATUS_RUNNING) {
1880 ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
fanchanghu3d4e7212017-08-09 09:42:30 +08001881 cur_ps_session->state = NC_PS_STATE_INVALID;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001882 } else {
fanchanghu3d4e7212017-08-09 09:42:30 +08001883 cur_ps_session->state = NC_PS_STATE_NONE;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001884 }
1885 } else {
Michal Vasko9fb42272017-10-05 13:50:05 +02001886 cur_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001887
Michal Vasko7f1ee932018-10-11 09:41:42 +02001888 /* process RPC */
Michal Vasko131120a2018-05-29 15:44:02 +02001889 ret |= nc_server_send_reply_io(cur_session, timeout, rpc);
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001890 if (cur_session->status != NC_STATUS_RUNNING) {
1891 ret |= NC_PSPOLL_SESSION_TERM;
1892 if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) {
1893 ret |= NC_PSPOLL_SESSION_ERROR;
1894 }
fanchanghu3d4e7212017-08-09 09:42:30 +08001895 cur_ps_session->state = NC_PS_STATE_INVALID;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001896 } else {
fanchanghu3d4e7212017-08-09 09:42:30 +08001897 cur_ps_session->state = NC_PS_STATE_NONE;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001898 }
Michal Vasko428087d2016-01-14 16:04:28 +01001899 }
Michal Vasko77367452021-02-16 16:32:18 +01001900 nc_server_rpc_free(rpc);
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001901
Michal Vasko131120a2018-05-29 15:44:02 +02001902 /* SESSION RPC UNLOCK */
1903 nc_session_rpc_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +01001904 }
1905
Michal Vasko48a63ed2016-03-01 09:48:21 +01001906 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001907}
1908
Michal Vaskod09eae62016-02-01 10:32:52 +01001909API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001910nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *))
Michal Vaskod09eae62016-02-01 10:32:52 +01001911{
Michal Vaskob30b99c2016-07-26 11:35:43 +02001912 uint8_t q_id;
Michal Vaskod09eae62016-02-01 10:32:52 +01001913 uint16_t i;
1914 struct nc_session *session;
1915
Michal Vasko9a25e932016-02-01 10:36:42 +01001916 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001917 ERRARG("ps");
Michal Vasko9a25e932016-02-01 10:36:42 +01001918 return;
1919 }
1920
Michal Vasko48a63ed2016-03-01 09:48:21 +01001921 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001922 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +02001923 return;
1924 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001925
Michal Vasko48a63ed2016-03-01 09:48:21 +01001926 if (all) {
Radek Krejci4f8042c2016-03-03 13:11:26 +01001927 for (i = 0; i < ps->session_count; i++) {
fanchanghu3d4e7212017-08-09 09:42:30 +08001928 nc_session_free(ps->sessions[i]->session, data_free);
1929 free(ps->sessions[i]);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001930 }
1931 free(ps->sessions);
1932 ps->sessions = NULL;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001933 ps->session_count = 0;
Michal Vasko9a327362017-01-11 11:31:46 +01001934 ps->last_event_session = 0;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001935 } else {
1936 for (i = 0; i < ps->session_count; ) {
fanchanghu3d4e7212017-08-09 09:42:30 +08001937 if (ps->sessions[i]->session->status != NC_STATUS_RUNNING) {
1938 session = ps->sessions[i]->session;
Radek Krejcid5f978f2016-03-03 13:14:45 +01001939 _nc_ps_del_session(ps, NULL, i);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001940 nc_session_free(session, data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001941 continue;
1942 }
1943
1944 ++i;
1945 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001946 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001947
1948 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001949 nc_ps_unlock(ps, q_id, __func__);
Michal Vaskod09eae62016-02-01 10:32:52 +01001950}
1951
Michal Vasko5f352c52019-07-10 16:12:06 +02001952static int
apropp-molex4e903c32020-04-20 03:06:58 -04001953nc_get_uid(int sock, uid_t *uid)
1954{
Michal Vaskod3910912020-04-20 09:12:49 +02001955 int ret;
apropp-molex4e903c32020-04-20 03:06:58 -04001956
Michal Vaskod3910912020-04-20 09:12:49 +02001957#ifdef SO_PEERCRED
1958 struct ucred ucred;
1959 socklen_t len;
Michal Vasko292c5542023-02-01 14:33:17 +01001960
Michal Vaskod3910912020-04-20 09:12:49 +02001961 len = sizeof(ucred);
1962 ret = getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len);
1963 if (!ret) {
1964 *uid = ucred.uid;
1965 }
1966#else
1967 ret = getpeereid(sock, uid, NULL);
1968#endif
1969
1970 if (ret < 0) {
Michal Vasko05532772021-06-03 12:12:38 +02001971 ERR(NULL, "Failed to get credentials from unix socket (%s).", strerror(errno));
Michal Vaskod3910912020-04-20 09:12:49 +02001972 return -1;
1973 }
apropp-molex4e903c32020-04-20 03:06:58 -04001974 return 0;
1975}
1976
1977static int
Michal Vasko5f352c52019-07-10 16:12:06 +02001978nc_accept_unix(struct nc_session *session, int sock)
1979{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001980#if defined (SO_PEERCRED) || defined (HAVE_GETPEEREID)
Jan Kundrát6aa0eeb2021-10-08 21:10:05 +02001981 struct passwd *pw, pw_buf;
Michal Vasko5f352c52019-07-10 16:12:06 +02001982 char *username;
Michal Vasko292c5542023-02-01 14:33:17 +01001983
Michal Vasko5f352c52019-07-10 16:12:06 +02001984 session->ti_type = NC_TI_UNIX;
Michal Vasko143aa142021-10-01 15:31:48 +02001985 uid_t uid = 0;
Jan Kundrát6aa0eeb2021-10-08 21:10:05 +02001986 char *buf = NULL;
1987 size_t buf_len = 0;
Michal Vasko5f352c52019-07-10 16:12:06 +02001988
Michal Vaskod3910912020-04-20 09:12:49 +02001989 if (nc_get_uid(sock, &uid)) {
1990 close(sock);
Michal Vasko5f352c52019-07-10 16:12:06 +02001991 return -1;
1992 }
1993
Jan Kundrát6aa0eeb2021-10-08 21:10:05 +02001994 pw = nc_getpwuid(uid, &pw_buf, &buf, &buf_len);
Michal Vasko5f352c52019-07-10 16:12:06 +02001995 if (pw == NULL) {
Michal Vasko05532772021-06-03 12:12:38 +02001996 ERR(NULL, "Failed to find username for uid=%u (%s).\n", uid, strerror(errno));
Michal Vasko5f352c52019-07-10 16:12:06 +02001997 close(sock);
1998 return -1;
1999 }
2000
2001 username = strdup(pw->pw_name);
Michal Vaskoccd2dd02021-10-11 09:13:01 +02002002 free(buf);
Michal Vasko5f352c52019-07-10 16:12:06 +02002003 if (username == NULL) {
2004 ERRMEM;
2005 close(sock);
2006 return -1;
2007 }
Michal Vasko93224072021-11-09 12:14:28 +01002008 session->username = username;
Michal Vasko5f352c52019-07-10 16:12:06 +02002009
2010 session->ti.unixsock.sock = sock;
2011
2012 return 1;
Claus Klein22091912020-01-20 13:45:47 +01002013#else
2014 return -1;
2015#endif
Michal Vasko5f352c52019-07-10 16:12:06 +02002016}
2017
Michal Vaskoe2713da2016-08-22 16:06:40 +02002018API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02002019nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01002020{
Michal Vasko3031aae2016-01-27 16:07:18 +01002021 uint16_t i;
Michal Vaskoade892d2017-02-22 13:40:35 +01002022 int ret = 0;
Michal Vasko9e036d52016-01-08 10:49:26 +01002023
Michal Vasko45e53ae2016-04-07 11:46:03 +02002024 if (!name) {
2025 ERRARG("name");
2026 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01002027 }
2028
Michal Vaskoade892d2017-02-22 13:40:35 +01002029 /* BIND LOCK */
2030 pthread_mutex_lock(&server_opts.bind_lock);
2031
2032 /* ENDPT WRITE LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002033 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01002034
2035 /* check name uniqueness */
2036 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02002037 if (!strcmp(server_opts.endpts[i].name, name)) {
Michal Vasko05532772021-06-03 12:12:38 +02002038 ERR(NULL, "Endpoint \"%s\" already exists.", name);
Michal Vaskoade892d2017-02-22 13:40:35 +01002039 ret = -1;
2040 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +01002041 }
2042 }
2043
Michal Vasko93224072021-11-09 12:14:28 +01002044 server_opts.endpts = nc_realloc(server_opts.endpts, (server_opts.endpt_count + 1) * sizeof *server_opts.endpts);
Michal Vaskoe2713da2016-08-22 16:06:40 +02002045 if (!server_opts.endpts) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01002046 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01002047 ret = -1;
2048 goto cleanup;
Michal Vaskoe2713da2016-08-22 16:06:40 +02002049 }
Michal Vasko93224072021-11-09 12:14:28 +01002050 memset(&server_opts.endpts[server_opts.endpt_count], 0, sizeof *server_opts.endpts);
2051 ++server_opts.endpt_count;
2052
2053 server_opts.endpts[server_opts.endpt_count - 1].name = strdup(name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002054 server_opts.endpts[server_opts.endpt_count - 1].ti = ti;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002055 server_opts.endpts[server_opts.endpt_count - 1].ka.idle_time = 1;
2056 server_opts.endpts[server_opts.endpt_count - 1].ka.max_probes = 10;
2057 server_opts.endpts[server_opts.endpt_count - 1].ka.probe_interval = 5;
Michal Vaskoe2713da2016-08-22 16:06:40 +02002058
Michal Vaskoe2713da2016-08-22 16:06:40 +02002059 server_opts.binds = nc_realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds);
Michal Vaskoe2713da2016-08-22 16:06:40 +02002060 if (!server_opts.binds) {
2061 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01002062 ret = -1;
2063 goto cleanup;
Michal Vasko4eb3c312016-03-01 14:09:37 +01002064 }
Michal Vasko9e036d52016-01-08 10:49:26 +01002065
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002066 memset(&server_opts.binds[server_opts.endpt_count - 1], 0, sizeof *server_opts.binds);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002067 server_opts.binds[server_opts.endpt_count - 1].sock = -1;
2068
2069 switch (ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01002070#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02002071 case NC_TI_LIBSSH:
2072 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts));
2073 if (!server_opts.endpts[server_opts.endpt_count - 1].opts.ssh) {
2074 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01002075 ret = -1;
2076 goto cleanup;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002077 }
2078 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_methods =
roman41a11e42022-06-22 09:27:08 +02002079 NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002080 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_attempts = 3;
Michal Vaskocbad4c52019-06-27 16:30:35 +02002081 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_timeout = 30;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002082 break;
Michal Vaskoe2713da2016-08-22 16:06:40 +02002083#endif
Michal Vaskoe2713da2016-08-22 16:06:40 +02002084#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02002085 case NC_TI_OPENSSL:
2086 server_opts.endpts[server_opts.endpt_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts));
2087 if (!server_opts.endpts[server_opts.endpt_count - 1].opts.tls) {
2088 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01002089 ret = -1;
2090 goto cleanup;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002091 }
2092 break;
Michal Vaskoe2713da2016-08-22 16:06:40 +02002093#endif
Olivier Matzac7fa2f2018-10-11 10:02:04 +02002094 case NC_TI_UNIX:
2095 server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock = calloc(1, sizeof(struct nc_server_unix_opts));
2096 if (!server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock) {
2097 ERRMEM;
2098 ret = -1;
2099 goto cleanup;
2100 }
2101 server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->mode = (mode_t)-1;
2102 server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->uid = (uid_t)-1;
2103 server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->gid = (gid_t)-1;
2104 break;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002105 default:
2106 ERRINT;
Michal Vaskoade892d2017-02-22 13:40:35 +01002107 ret = -1;
2108 goto cleanup;
Michal Vaskoe2713da2016-08-22 16:06:40 +02002109 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02002110
Michal Vaskoade892d2017-02-22 13:40:35 +01002111cleanup:
2112 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002113 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko9e036d52016-01-08 10:49:26 +01002114
Michal Vaskoade892d2017-02-22 13:40:35 +01002115 /* BIND UNLOCK */
2116 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01002117
Michal Vaskoade892d2017-02-22 13:40:35 +01002118 return ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01002119}
2120
Olivier Matzac7fa2f2018-10-11 10:02:04 +02002121API int
Michal Vasko59050372016-11-22 14:33:55 +01002122nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01002123{
2124 uint32_t i;
2125 int ret = -1;
2126
Michal Vaskoade892d2017-02-22 13:40:35 +01002127 /* BIND LOCK */
2128 pthread_mutex_lock(&server_opts.bind_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01002129
Michal Vaskoade892d2017-02-22 13:40:35 +01002130 /* ENDPT WRITE LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002131 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vasko9e036d52016-01-08 10:49:26 +01002132
Michal Vasko59050372016-11-22 14:33:55 +01002133 if (!name && !ti) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02002134 /* remove all endpoints */
Michal Vasko3031aae2016-01-27 16:07:18 +01002135 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01002136 free(server_opts.endpts[i].name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002137 switch (server_opts.endpts[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01002138#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02002139 case NC_TI_LIBSSH:
2140 nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh);
2141 free(server_opts.endpts[i].opts.ssh);
2142 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01002143#endif
Radek Krejci53691be2016-02-22 13:58:37 +01002144#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02002145 case NC_TI_OPENSSL:
2146 nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls);
2147 free(server_opts.endpts[i].opts.tls);
2148 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01002149#endif
Olivier Matzac7fa2f2018-10-11 10:02:04 +02002150 case NC_TI_UNIX:
2151 free(server_opts.endpts[i].opts.unixsock);
2152 break;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002153 default:
2154 ERRINT;
2155 /* won't get here ...*/
2156 break;
2157 }
Michal Vasko9e036d52016-01-08 10:49:26 +01002158 ret = 0;
2159 }
Michal Vasko3031aae2016-01-27 16:07:18 +01002160 free(server_opts.endpts);
2161 server_opts.endpts = NULL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02002162
2163 /* remove all binds */
2164 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01002165 free(server_opts.binds[i].address);
Michal Vaskoe2713da2016-08-22 16:06:40 +02002166 if (server_opts.binds[i].sock > -1) {
2167 close(server_opts.binds[i].sock);
2168 }
2169 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02002170 free(server_opts.binds);
2171 server_opts.binds = NULL;
2172
Michal Vasko3031aae2016-01-27 16:07:18 +01002173 server_opts.endpt_count = 0;
2174
Michal Vasko1a38c862016-01-15 15:50:07 +01002175 } else {
Michal Vasko59050372016-11-22 14:33:55 +01002176 /* remove one endpoint with bind(s) or all endpoints using one transport protocol */
Michal Vasko3031aae2016-01-27 16:07:18 +01002177 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vasko59050372016-11-22 14:33:55 +01002178 if ((name && !strcmp(server_opts.endpts[i].name, name)) || (!name && (server_opts.endpts[i].ti == ti))) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02002179 /* remove endpt */
Michal Vasko93224072021-11-09 12:14:28 +01002180 free(server_opts.endpts[i].name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002181 switch (server_opts.endpts[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01002182#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02002183 case NC_TI_LIBSSH:
2184 nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh);
2185 free(server_opts.endpts[i].opts.ssh);
2186 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01002187#endif
Radek Krejci53691be2016-02-22 13:58:37 +01002188#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02002189 case NC_TI_OPENSSL:
2190 nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls);
2191 free(server_opts.endpts[i].opts.tls);
2192 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01002193#endif
Olivier Matzac7fa2f2018-10-11 10:02:04 +02002194 case NC_TI_UNIX:
2195 free(server_opts.endpts[i].opts.unixsock);
2196 break;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002197 default:
2198 ERRINT;
2199 break;
2200 }
Michal Vasko1a38c862016-01-15 15:50:07 +01002201
Michal Vaskoe2713da2016-08-22 16:06:40 +02002202 /* remove bind(s) */
Michal Vasko93224072021-11-09 12:14:28 +01002203 free(server_opts.binds[i].address);
Michal Vaskoe2713da2016-08-22 16:06:40 +02002204 if (server_opts.binds[i].sock > -1) {
2205 close(server_opts.binds[i].sock);
2206 }
2207
2208 /* move last endpt and bind(s) to the empty space */
Michal Vasko3031aae2016-01-27 16:07:18 +01002209 --server_opts.endpt_count;
Michal Vasko9ed67a72016-10-13 15:00:51 +02002210 if (!server_opts.endpt_count) {
Michal Vaskoc0256492016-02-02 12:19:06 +01002211 free(server_opts.binds);
2212 server_opts.binds = NULL;
2213 free(server_opts.endpts);
2214 server_opts.endpts = NULL;
Michal Vasko9ed67a72016-10-13 15:00:51 +02002215 } else if (i < server_opts.endpt_count) {
2216 memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
2217 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
Michal Vaskoc0256492016-02-02 12:19:06 +01002218 }
Michal Vasko1a38c862016-01-15 15:50:07 +01002219
2220 ret = 0;
Michal Vasko59050372016-11-22 14:33:55 +01002221 if (name) {
2222 break;
2223 }
Michal Vasko1a38c862016-01-15 15:50:07 +01002224 }
2225 }
Michal Vasko9e036d52016-01-08 10:49:26 +01002226 }
2227
Michal Vaskoade892d2017-02-22 13:40:35 +01002228 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002229 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01002230
Michal Vaskoade892d2017-02-22 13:40:35 +01002231 /* BIND UNLOCK */
2232 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vasko9e036d52016-01-08 10:49:26 +01002233
2234 return ret;
2235}
2236
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002237API int
2238nc_server_endpt_count(void)
2239{
2240 return server_opts.endpt_count;
2241}
2242
Michal Vasko1b5973e2020-01-30 16:05:46 +01002243API int
2244nc_server_is_endpt(const char *name)
2245{
2246 uint16_t i;
2247 int found = 0;
2248
Michal Vaskofb1724b2020-01-31 11:02:00 +01002249 if (!name) {
2250 return found;
2251 }
2252
Michal Vasko1b5973e2020-01-30 16:05:46 +01002253 /* ENDPT READ LOCK */
2254 pthread_rwlock_rdlock(&server_opts.endpt_lock);
2255
2256 /* check name uniqueness */
2257 for (i = 0; i < server_opts.endpt_count; ++i) {
2258 if (!strcmp(server_opts.endpts[i].name, name)) {
2259 found = 1;
2260 break;
2261 }
2262 }
2263
2264 /* ENDPT UNLOCK */
2265 pthread_rwlock_unlock(&server_opts.endpt_lock);
2266
2267 return found;
2268}
2269
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002270int
2271nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port)
2272{
2273 struct nc_endpt *endpt;
2274 struct nc_bind *bind = NULL;
2275 uint16_t i;
2276 int sock = -1, set_addr, ret = 0;
2277
2278 if (!endpt_name) {
2279 ERRARG("endpt_name");
2280 return -1;
2281 } else if ((!address && !port) || (address && port)) {
2282 ERRARG("address and port");
2283 return -1;
2284 }
2285
2286 if (address) {
2287 set_addr = 1;
2288 } else {
2289 set_addr = 0;
2290 }
2291
2292 /* BIND LOCK */
2293 pthread_mutex_lock(&server_opts.bind_lock);
2294
2295 /* ENDPT LOCK */
2296 endpt = nc_server_endpt_lock_get(endpt_name, 0, &i);
2297 if (!endpt) {
2298 /* BIND UNLOCK */
2299 pthread_mutex_unlock(&server_opts.bind_lock);
2300 return -1;
2301 }
2302
2303 bind = &server_opts.binds[i];
2304
2305 if (set_addr) {
2306 port = bind->port;
2307 } else {
2308 address = bind->address;
2309 }
2310
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002311 if (!set_addr && (endpt->ti == NC_TI_UNIX)) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002312 ret = -1;
2313 goto cleanup;
2314 }
2315
2316 /* we have all the information we need to create a listening socket */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002317 if (address && (port || (endpt->ti == NC_TI_UNIX))) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002318 /* create new socket, close the old one */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002319 if (endpt->ti == NC_TI_UNIX) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002320 sock = nc_sock_listen_unix(address, endpt->opts.unixsock);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002321 } else {
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002322 sock = nc_sock_listen_inet(address, port, &endpt->ka);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002323 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002324 if (sock == -1) {
2325 ret = -1;
2326 goto cleanup;
2327 }
2328
2329 if (bind->sock > -1) {
2330 close(bind->sock);
2331 }
2332 bind->sock = sock;
2333 } /* else we are just setting address or port */
2334
2335 if (set_addr) {
Michal Vasko93224072021-11-09 12:14:28 +01002336 free(bind->address);
2337 bind->address = strdup(address);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002338 } else {
2339 bind->port = port;
2340 }
2341
2342 if (sock > -1) {
Michal Vasko946cacb2020-08-12 11:18:08 +02002343 switch (endpt->ti) {
2344 case NC_TI_UNIX:
Michal Vasko05532772021-06-03 12:12:38 +02002345 VRB(NULL, "Listening on %s for UNIX connections.", address);
Michal Vasko946cacb2020-08-12 11:18:08 +02002346 break;
2347#ifdef NC_ENABLED_SSH
2348 case NC_TI_LIBSSH:
Michal Vasko05532772021-06-03 12:12:38 +02002349 VRB(NULL, "Listening on %s:%u for SSH connections.", address, port);
Michal Vasko946cacb2020-08-12 11:18:08 +02002350 break;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002351#endif
Michal Vasko946cacb2020-08-12 11:18:08 +02002352#ifdef NC_ENABLED_TLS
2353 case NC_TI_OPENSSL:
Michal Vasko05532772021-06-03 12:12:38 +02002354 VRB(NULL, "Listening on %s:%u for TLS connections.", address, port);
Michal Vasko946cacb2020-08-12 11:18:08 +02002355 break;
2356#endif
2357 default:
2358 ERRINT;
2359 break;
2360 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002361 }
2362
2363cleanup:
2364 /* ENDPT UNLOCK */
2365 pthread_rwlock_unlock(&server_opts.endpt_lock);
2366
2367 /* BIND UNLOCK */
2368 pthread_mutex_unlock(&server_opts.bind_lock);
2369
2370 return ret;
2371}
2372
2373API int
2374nc_server_endpt_set_address(const char *endpt_name, const char *address)
2375{
2376 return nc_server_endpt_set_address_port(endpt_name, address, 0);
2377}
2378
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002379#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko946cacb2020-08-12 11:18:08 +02002380
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002381API int
2382nc_server_endpt_set_port(const char *endpt_name, uint16_t port)
2383{
2384 return nc_server_endpt_set_address_port(endpt_name, NULL, port);
2385}
2386
Michal Vasko946cacb2020-08-12 11:18:08 +02002387#endif
2388
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002389API int
2390nc_server_endpt_set_perms(const char *endpt_name, mode_t mode, uid_t uid, gid_t gid)
2391{
2392 struct nc_endpt *endpt;
2393 uint16_t i;
2394 int ret = 0;
2395
2396 if (!endpt_name) {
2397 ERRARG("endpt_name");
2398 return -1;
2399 } else if (mode == 0) {
2400 ERRARG("mode");
2401 return -1;
2402 }
2403
2404 /* ENDPT LOCK */
2405 endpt = nc_server_endpt_lock_get(endpt_name, 0, &i);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002406 if (!endpt) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002407 return -1;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002408 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002409
2410 if (endpt->ti != NC_TI_UNIX) {
2411 ret = -1;
2412 goto cleanup;
2413 }
2414
2415 endpt->opts.unixsock->mode = mode;
2416 endpt->opts.unixsock->uid = uid;
2417 endpt->opts.unixsock->gid = gid;
2418
2419cleanup:
2420 /* ENDPT UNLOCK */
2421 pthread_rwlock_unlock(&server_opts.endpt_lock);
2422
2423 return ret;
2424}
2425
2426API int
2427nc_server_endpt_enable_keepalives(const char *endpt_name, int enable)
2428{
2429 struct nc_endpt *endpt;
2430 int ret = 0;
2431
2432 if (!endpt_name) {
2433 ERRARG("endpt_name");
2434 return -1;
2435 }
2436
2437 /* ENDPT LOCK */
2438 endpt = nc_server_endpt_lock_get(endpt_name, 0, NULL);
2439 if (!endpt) {
2440 return -1;
2441 }
2442
2443 endpt->ka.enabled = (enable ? 1 : 0);
2444
2445 /* ENDPT UNLOCK */
2446 pthread_rwlock_unlock(&server_opts.endpt_lock);
2447
2448 return ret;
2449}
2450
2451API int
2452nc_server_endpt_set_keepalives(const char *endpt_name, int idle_time, int max_probes, int probe_interval)
2453{
2454 struct nc_endpt *endpt;
2455 int ret = 0;
2456
2457 if (!endpt_name) {
2458 ERRARG("endpt_name");
2459 return -1;
2460 }
2461
2462 /* ENDPT LOCK */
2463 endpt = nc_server_endpt_lock_get(endpt_name, 0, NULL);
2464 if (!endpt) {
2465 return -1;
2466 }
2467
2468 if (idle_time > -1) {
2469 endpt->ka.idle_time = idle_time;
2470 }
2471 if (max_probes > -1) {
2472 endpt->ka.max_probes = max_probes;
2473 }
2474 if (probe_interval > -1) {
2475 endpt->ka.probe_interval = probe_interval;
2476 }
2477
2478 /* ENDPT UNLOCK */
2479 pthread_rwlock_unlock(&server_opts.endpt_lock);
2480
2481 return ret;
2482}
2483
Michal Vasko71090fc2016-05-24 16:37:28 +02002484API NC_MSG_TYPE
Michal Vasko93224072021-11-09 12:14:28 +01002485nc_accept(int timeout, const struct ly_ctx *ctx, struct nc_session **session)
Michal Vasko9e036d52016-01-08 10:49:26 +01002486{
Michal Vasko71090fc2016-05-24 16:37:28 +02002487 NC_MSG_TYPE msgtype;
Michal Vasko1a38c862016-01-15 15:50:07 +01002488 int sock, ret;
Michal Vasko5c2f7952016-01-22 13:16:31 +01002489 char *host = NULL;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002490 uint16_t port, bind_idx;
Michal Vasko9fb42272017-10-05 13:50:05 +02002491 struct timespec ts_cur;
Michal Vasko9e036d52016-01-08 10:49:26 +01002492
Michal Vasko93224072021-11-09 12:14:28 +01002493 if (!ctx) {
2494 ERRARG("ctx");
Michal Vasko71090fc2016-05-24 16:37:28 +02002495 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02002496 } else if (!session) {
2497 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02002498 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01002499 }
2500
Michal Vasko93224072021-11-09 12:14:28 +01002501 /* init ctx as needed */
2502 nc_server_init_ctx(ctx);
2503
Michal Vaskoade892d2017-02-22 13:40:35 +01002504 /* BIND LOCK */
2505 pthread_mutex_lock(&server_opts.bind_lock);
Michal Vasko51e514d2016-02-02 15:51:52 +01002506
2507 if (!server_opts.endpt_count) {
Michal Vasko05532772021-06-03 12:12:38 +02002508 ERR(NULL, "No endpoints to accept sessions on.");
Michal Vaskoade892d2017-02-22 13:40:35 +01002509 /* BIND UNLOCK */
2510 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vasko71090fc2016-05-24 16:37:28 +02002511 return NC_MSG_ERROR;
Michal Vasko51e514d2016-02-02 15:51:52 +01002512 }
Michal Vaskob48aa812016-01-18 14:13:09 +01002513
Michal Vaskoe2713da2016-08-22 16:06:40 +02002514 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &bind_idx);
Michal Vasko50456e82016-02-02 12:16:08 +01002515 if (ret < 1) {
Michal Vaskoade892d2017-02-22 13:40:35 +01002516 /* BIND UNLOCK */
2517 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vaskob737d752016-02-09 09:01:27 +01002518 free(host);
Michal Vasko5e203472016-05-30 15:27:58 +02002519 if (!ret) {
2520 return NC_MSG_WOULDBLOCK;
2521 }
Michal Vasko71090fc2016-05-24 16:37:28 +02002522 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01002523 }
Michal Vaskoade892d2017-02-22 13:40:35 +01002524
2525 /* switch bind_lock for endpt_lock, so that another thread can accept another session */
2526 /* ENDPT READ LOCK */
2527 pthread_rwlock_rdlock(&server_opts.endpt_lock);
2528
2529 /* BIND UNLOCK */
2530 pthread_mutex_unlock(&server_opts.bind_lock);
2531
Michal Vaskob48aa812016-01-18 14:13:09 +01002532 sock = ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01002533
Michal Vasko131120a2018-05-29 15:44:02 +02002534 *session = nc_new_session(NC_SERVER, 0);
Michal Vasko686aa312016-01-21 15:58:18 +01002535 if (!(*session)) {
Michal Vasko9e036d52016-01-08 10:49:26 +01002536 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01002537 close(sock);
Michal Vasko5c2f7952016-01-22 13:16:31 +01002538 free(host);
Michal Vasko71090fc2016-05-24 16:37:28 +02002539 msgtype = NC_MSG_ERROR;
2540 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01002541 }
Michal Vasko1a38c862016-01-15 15:50:07 +01002542 (*session)->status = NC_STATUS_STARTING;
Michal Vasko93224072021-11-09 12:14:28 +01002543 (*session)->ctx = (struct ly_ctx *)ctx;
Michal Vasko1a38c862016-01-15 15:50:07 +01002544 (*session)->flags = NC_SESSION_SHAREDCTX;
Michal Vasko93224072021-11-09 12:14:28 +01002545 (*session)->host = host;
Michal Vasko1a38c862016-01-15 15:50:07 +01002546 (*session)->port = port;
Michal Vasko9e036d52016-01-08 10:49:26 +01002547
Michal Vaskoc14e3c82016-01-11 16:14:30 +01002548 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01002549#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02002550 if (server_opts.endpts[bind_idx].ti == NC_TI_LIBSSH) {
2551 (*session)->data = server_opts.endpts[bind_idx].opts.ssh;
Michal Vaskob70c8b82017-03-17 09:09:29 +01002552 ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko71090fc2016-05-24 16:37:28 +02002553 if (ret < 0) {
2554 msgtype = NC_MSG_ERROR;
2555 goto cleanup;
2556 } else if (!ret) {
2557 msgtype = NC_MSG_WOULDBLOCK;
2558 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01002559 }
Michal Vasko3d865d22016-01-28 16:00:53 +01002560 } else
2561#endif
Radek Krejci53691be2016-02-22 13:58:37 +01002562#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02002563 if (server_opts.endpts[bind_idx].ti == NC_TI_OPENSSL) {
2564 (*session)->data = server_opts.endpts[bind_idx].opts.tls;
Michal Vaskob70c8b82017-03-17 09:09:29 +01002565 ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko71090fc2016-05-24 16:37:28 +02002566 if (ret < 0) {
2567 msgtype = NC_MSG_ERROR;
2568 goto cleanup;
2569 } else if (!ret) {
2570 msgtype = NC_MSG_WOULDBLOCK;
2571 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01002572 }
Michal Vasko3d865d22016-01-28 16:00:53 +01002573 } else
2574#endif
Olivier Matzac7fa2f2018-10-11 10:02:04 +02002575 if (server_opts.endpts[bind_idx].ti == NC_TI_UNIX) {
2576 (*session)->data = server_opts.endpts[bind_idx].opts.unixsock;
2577 ret = nc_accept_unix(*session, sock);
2578 if (ret < 0) {
2579 msgtype = NC_MSG_ERROR;
2580 goto cleanup;
2581 }
2582 } else {
Michal Vasko9e036d52016-01-08 10:49:26 +01002583 ERRINT;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01002584 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02002585 msgtype = NC_MSG_ERROR;
2586 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01002587 }
2588
Michal Vasko2cc4c682016-03-01 09:16:48 +01002589 (*session)->data = NULL;
2590
Michal Vaskoade892d2017-02-22 13:40:35 +01002591 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002592 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01002593
Michal Vaskob48aa812016-01-18 14:13:09 +01002594 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002595 (*session)->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vaskob48aa812016-01-18 14:13:09 +01002596
Michal Vasko9e036d52016-01-08 10:49:26 +01002597 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02002598 msgtype = nc_handshake_io(*session);
Michal Vasko71090fc2016-05-24 16:37:28 +02002599 if (msgtype != NC_MSG_HELLO) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01002600 nc_session_free(*session, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01002601 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02002602 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01002603 }
Michal Vasko9fb42272017-10-05 13:50:05 +02002604
Michal Vaskod8a74192023-02-06 15:51:50 +01002605 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9fb42272017-10-05 13:50:05 +02002606 (*session)->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vaskod8a74192023-02-06 15:51:50 +01002607 nc_realtime_get(&ts_cur);
Michal Vasko9fb42272017-10-05 13:50:05 +02002608 (*session)->opts.server.session_start = ts_cur.tv_sec;
Michal Vasko1a38c862016-01-15 15:50:07 +01002609 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko9e036d52016-01-08 10:49:26 +01002610
Michal Vasko71090fc2016-05-24 16:37:28 +02002611 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01002612
Michal Vasko71090fc2016-05-24 16:37:28 +02002613cleanup:
Michal Vaskoade892d2017-02-22 13:40:35 +01002614 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002615 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01002616
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01002617 nc_session_free(*session, NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01002618 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02002619 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01002620}
2621
Michal Vaskob83a3fa2021-05-26 09:53:42 +02002622#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko946cacb2020-08-12 11:18:08 +02002623
Michal Vaskoadf30f02019-06-24 09:34:47 +02002624/* client is expected to be locked */
2625static int
2626_nc_server_ch_client_del_endpt(struct nc_ch_client *client, const char *endpt_name, NC_TRANSPORT_IMPL ti)
Michal Vasko2e6defd2016-10-07 15:48:15 +02002627{
2628 uint16_t i;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002629 int ret = -1;
2630
2631 if (!endpt_name) {
2632 /* remove all endpoints */
2633 for (i = 0; i < client->ch_endpt_count; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01002634 free(client->ch_endpts[i].name);
2635 free(client->ch_endpts[i].address);
Michal Vaskoadf30f02019-06-24 09:34:47 +02002636 if (client->ch_endpts[i].sock_pending != -1) {
2637 close(client->ch_endpts[i].sock_pending);
2638 }
2639 switch (client->ch_endpts[i].ti) {
2640#ifdef NC_ENABLED_SSH
2641 case NC_TI_LIBSSH:
2642 nc_server_ssh_clear_opts(client->ch_endpts[i].opts.ssh);
2643 free(client->ch_endpts[i].opts.ssh);
2644 break;
2645#endif
2646#ifdef NC_ENABLED_TLS
2647 case NC_TI_OPENSSL:
2648 nc_server_tls_clear_opts(client->ch_endpts[i].opts.tls);
2649 free(client->ch_endpts[i].opts.tls);
2650 break;
2651#endif
2652 default:
2653 ERRINT;
2654 /* won't get here ...*/
2655 break;
2656 }
2657 }
2658 free(client->ch_endpts);
2659 client->ch_endpts = NULL;
2660 client->ch_endpt_count = 0;
2661
2662 ret = 0;
2663 } else {
2664 for (i = 0; i < client->ch_endpt_count; ++i) {
2665 if (!strcmp(client->ch_endpts[i].name, endpt_name) && (!ti || (ti == client->ch_endpts[i].ti))) {
Michal Vasko93224072021-11-09 12:14:28 +01002666 free(client->ch_endpts[i].name);
2667 free(client->ch_endpts[i].address);
Michal Vaskoadf30f02019-06-24 09:34:47 +02002668 if (client->ch_endpts[i].sock_pending != -1) {
2669 close(client->ch_endpts[i].sock_pending);
2670 }
2671 switch (client->ch_endpts[i].ti) {
2672#ifdef NC_ENABLED_SSH
2673 case NC_TI_LIBSSH:
2674 nc_server_ssh_clear_opts(client->ch_endpts[i].opts.ssh);
2675 free(client->ch_endpts[i].opts.ssh);
2676 break;
2677#endif
2678#ifdef NC_ENABLED_TLS
2679 case NC_TI_OPENSSL:
2680 nc_server_tls_clear_opts(client->ch_endpts[i].opts.tls);
2681 free(client->ch_endpts[i].opts.tls);
2682 break;
2683#endif
2684 default:
2685 ERRINT;
2686 /* won't get here ...*/
2687 break;
2688 }
2689
2690 /* move last endpoint to the empty space */
2691 --client->ch_endpt_count;
2692 if (i < client->ch_endpt_count) {
2693 memcpy(&client->ch_endpts[i], &client->ch_endpts[client->ch_endpt_count], sizeof *client->ch_endpts);
2694 } else if (!server_opts.ch_client_count) {
2695 free(server_opts.ch_clients);
2696 server_opts.ch_clients = NULL;
2697 }
2698
2699 ret = 0;
2700 break;
2701 }
2702 }
2703 }
2704
2705 return ret;
2706}
2707
2708API int
2709nc_server_ch_add_client(const char *name)
2710{
2711 uint16_t i;
2712 struct nc_ch_client *client;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002713
2714 if (!name) {
2715 ERRARG("name");
2716 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002717 }
2718
2719 /* WRITE LOCK */
2720 pthread_rwlock_wrlock(&server_opts.ch_client_lock);
2721
2722 /* check name uniqueness */
2723 for (i = 0; i < server_opts.ch_client_count; ++i) {
2724 if (!strcmp(server_opts.ch_clients[i].name, name)) {
Michal Vasko05532772021-06-03 12:12:38 +02002725 ERR(NULL, "Call Home client \"%s\" already exists.", name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002726 /* WRITE UNLOCK */
2727 pthread_rwlock_unlock(&server_opts.ch_client_lock);
2728 return -1;
2729 }
2730 }
2731
2732 ++server_opts.ch_client_count;
2733 server_opts.ch_clients = nc_realloc(server_opts.ch_clients, server_opts.ch_client_count * sizeof *server_opts.ch_clients);
2734 if (!server_opts.ch_clients) {
2735 ERRMEM;
2736 /* WRITE UNLOCK */
2737 pthread_rwlock_unlock(&server_opts.ch_client_lock);
2738 return -1;
2739 }
Michal Vaskoadf30f02019-06-24 09:34:47 +02002740 client = &server_opts.ch_clients[server_opts.ch_client_count - 1];
Michal Vasko2e6defd2016-10-07 15:48:15 +02002741
Michal Vasko93224072021-11-09 12:14:28 +01002742 client->name = strdup(name);
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02002743 client->id = ATOMIC_INC_RELAXED(server_opts.new_client_id);
Michal Vaskoadf30f02019-06-24 09:34:47 +02002744 client->ch_endpts = NULL;
2745 client->ch_endpt_count = 0;
2746 client->conn_type = 0;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002747
Michal Vasko2e6defd2016-10-07 15:48:15 +02002748 /* set CH default options */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002749 client->start_with = NC_CH_FIRST_LISTED;
2750 client->max_attempts = 3;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002751
Michal Vaskoadf30f02019-06-24 09:34:47 +02002752 pthread_mutex_init(&client->lock, NULL);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002753
2754 /* WRITE UNLOCK */
2755 pthread_rwlock_unlock(&server_opts.ch_client_lock);
2756
2757 return 0;
2758}
2759
2760API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02002761nc_server_ch_del_client(const char *name)
Michal Vasko2e6defd2016-10-07 15:48:15 +02002762{
Michal Vaskoadf30f02019-06-24 09:34:47 +02002763 uint16_t i;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002764 int ret = -1;
2765
2766 /* WRITE LOCK */
2767 pthread_rwlock_wrlock(&server_opts.ch_client_lock);
2768
Michal Vaskoadf30f02019-06-24 09:34:47 +02002769 if (!name) {
2770 /* remove all CH clients with endpoints */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002771 for (i = 0; i < server_opts.ch_client_count; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01002772 free(server_opts.ch_clients[i].name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002773
2774 /* remove all endpoints */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002775 _nc_server_ch_client_del_endpt(&server_opts.ch_clients[i], NULL, 0);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002776
2777 pthread_mutex_destroy(&server_opts.ch_clients[i].lock);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002778 ret = 0;
2779 }
2780 free(server_opts.ch_clients);
2781 server_opts.ch_clients = NULL;
2782
2783 server_opts.ch_client_count = 0;
2784
2785 } else {
Michal Vaskoadf30f02019-06-24 09:34:47 +02002786 /* remove one client with endpoints */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002787 for (i = 0; i < server_opts.ch_client_count; ++i) {
Michal Vaskoadf30f02019-06-24 09:34:47 +02002788 if (!strcmp(server_opts.ch_clients[i].name, name)) {
Michal Vasko93224072021-11-09 12:14:28 +01002789 free(server_opts.ch_clients[i].name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002790
Michal Vasko2e6defd2016-10-07 15:48:15 +02002791 /* remove all endpoints */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002792 _nc_server_ch_client_del_endpt(&server_opts.ch_clients[i], NULL, 0);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002793
2794 pthread_mutex_destroy(&server_opts.ch_clients[i].lock);
2795
2796 /* move last client and endpoint(s) to the empty space */
2797 --server_opts.ch_client_count;
2798 if (i < server_opts.ch_client_count) {
2799 memcpy(&server_opts.ch_clients[i], &server_opts.ch_clients[server_opts.ch_client_count],
Michal Vaskoadf30f02019-06-24 09:34:47 +02002800 sizeof *server_opts.ch_clients);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002801 } else if (!server_opts.ch_client_count) {
2802 free(server_opts.ch_clients);
2803 server_opts.ch_clients = NULL;
2804 }
2805
2806 ret = 0;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002807 break;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002808 }
2809 }
2810 }
2811
2812 /* WRITE UNLOCK */
2813 pthread_rwlock_unlock(&server_opts.ch_client_lock);
2814
2815 return ret;
2816}
2817
2818API int
Michal Vaskofb1724b2020-01-31 11:02:00 +01002819nc_server_ch_is_client(const char *name)
2820{
2821 uint16_t i;
2822 int found = 0;
2823
2824 if (!name) {
2825 return found;
2826 }
2827
2828 /* READ LOCK */
2829 pthread_rwlock_rdlock(&server_opts.ch_client_lock);
2830
2831 /* check name uniqueness */
2832 for (i = 0; i < server_opts.ch_client_count; ++i) {
2833 if (!strcmp(server_opts.ch_clients[i].name, name)) {
2834 found = 1;
2835 break;
2836 }
2837 }
2838
2839 /* UNLOCK */
2840 pthread_rwlock_unlock(&server_opts.ch_client_lock);
2841
2842 return found;
2843}
2844
2845API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02002846nc_server_ch_client_add_endpt(const char *client_name, const char *endpt_name, NC_TRANSPORT_IMPL ti)
Michal Vasko2e6defd2016-10-07 15:48:15 +02002847{
2848 uint16_t i;
2849 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002850 struct nc_ch_endpt *endpt;
2851 int ret = -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002852
2853 if (!client_name) {
2854 ERRARG("client_name");
2855 return -1;
2856 } else if (!endpt_name) {
2857 ERRARG("endpt_name");
2858 return -1;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002859 } else if (!ti) {
2860 ERRARG("ti");
2861 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002862 }
2863
2864 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002865 nc_server_ch_client_lock(client_name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002866 if (!client) {
2867 return -1;
2868 }
2869
2870 for (i = 0; i < client->ch_endpt_count; ++i) {
2871 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
Michal Vasko05532772021-06-03 12:12:38 +02002872 ERR(NULL, "Call Home client \"%s\" endpoint \"%s\" already exists.", client_name, endpt_name);
Michal Vaskoadf30f02019-06-24 09:34:47 +02002873 goto cleanup;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002874 }
2875 }
2876
2877 ++client->ch_endpt_count;
2878 client->ch_endpts = realloc(client->ch_endpts, client->ch_endpt_count * sizeof *client->ch_endpts);
2879 if (!client->ch_endpts) {
2880 ERRMEM;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002881 goto cleanup;
2882 }
2883 endpt = &client->ch_endpts[client->ch_endpt_count - 1];
2884
2885 memset(endpt, 0, sizeof *client->ch_endpts);
Michal Vasko93224072021-11-09 12:14:28 +01002886 endpt->name = strdup(endpt_name);
Michal Vaskoadf30f02019-06-24 09:34:47 +02002887 endpt->ti = ti;
2888 endpt->sock_pending = -1;
2889 endpt->ka.idle_time = 1;
2890 endpt->ka.max_probes = 10;
2891 endpt->ka.probe_interval = 5;
2892
2893 switch (ti) {
2894#ifdef NC_ENABLED_SSH
2895 case NC_TI_LIBSSH:
2896 endpt->opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts));
2897 if (!endpt->opts.ssh) {
2898 ERRMEM;
2899 goto cleanup;
2900 }
roman41a11e42022-06-22 09:27:08 +02002901 endpt->opts.ssh->auth_methods = NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002902 endpt->opts.ssh->auth_attempts = 3;
Michal Vaskocbad4c52019-06-27 16:30:35 +02002903 endpt->opts.ssh->auth_timeout = 30;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002904 break;
2905#endif
2906#ifdef NC_ENABLED_TLS
2907 case NC_TI_OPENSSL:
2908 endpt->opts.tls = calloc(1, sizeof(struct nc_server_tls_opts));
2909 if (!endpt->opts.tls) {
2910 ERRMEM;
2911 goto cleanup;
2912 }
2913 break;
2914#endif
2915 default:
2916 ERRINT;
2917 goto cleanup;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002918 }
2919
Michal Vaskoadf30f02019-06-24 09:34:47 +02002920 /* success */
2921 ret = 0;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002922
Michal Vaskoadf30f02019-06-24 09:34:47 +02002923cleanup:
Michal Vasko2e6defd2016-10-07 15:48:15 +02002924 /* UNLOCK */
2925 nc_server_ch_client_unlock(client);
2926
Michal Vaskoadf30f02019-06-24 09:34:47 +02002927 return ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002928}
2929
2930API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02002931nc_server_ch_client_del_endpt(const char *client_name, const char *endpt_name, NC_TRANSPORT_IMPL ti)
Michal Vasko2e6defd2016-10-07 15:48:15 +02002932{
Michal Vaskoadf30f02019-06-24 09:34:47 +02002933 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002934 struct nc_ch_client *client;
2935
2936 if (!client_name) {
2937 ERRARG("client_name");
2938 return -1;
2939 }
2940
2941 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002942 nc_server_ch_client_lock(client_name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002943 if (!client) {
2944 return -1;
2945 }
2946
Michal Vaskoadf30f02019-06-24 09:34:47 +02002947 ret = _nc_server_ch_client_del_endpt(client, endpt_name, ti);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002948
2949 /* UNLOCK */
2950 nc_server_ch_client_unlock(client);
2951
2952 return ret;
2953}
2954
2955API int
Michal Vaskofb1724b2020-01-31 11:02:00 +01002956nc_server_ch_client_is_endpt(const char *client_name, const char *endpt_name)
2957{
2958 uint16_t i;
2959 struct nc_ch_client *client = NULL;
2960 int found = 0;
2961
2962 if (!client_name || !endpt_name) {
2963 return found;
2964 }
2965
2966 /* READ LOCK */
2967 pthread_rwlock_rdlock(&server_opts.ch_client_lock);
2968
2969 for (i = 0; i < server_opts.ch_client_count; ++i) {
2970 if (!strcmp(server_opts.ch_clients[i].name, client_name)) {
2971 client = &server_opts.ch_clients[i];
2972 break;
2973 }
2974 }
2975
2976 if (!client) {
2977 goto cleanup;
2978 }
2979
2980 for (i = 0; i < client->ch_endpt_count; ++i) {
2981 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2982 found = 1;
2983 break;
2984 }
2985 }
2986
2987cleanup:
2988 /* UNLOCK */
2989 pthread_rwlock_unlock(&server_opts.ch_client_lock);
2990 return found;
2991}
2992
2993API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02002994nc_server_ch_client_endpt_set_address(const char *client_name, const char *endpt_name, const char *address)
2995{
Michal Vasko2e6defd2016-10-07 15:48:15 +02002996 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002997 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002998
2999 if (!client_name) {
3000 ERRARG("client_name");
3001 return -1;
3002 } else if (!endpt_name) {
3003 ERRARG("endpt_name");
3004 return -1;
3005 } else if (!address) {
3006 ERRARG("address");
3007 return -1;
3008 }
3009
3010 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02003011 endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client);
3012 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02003013 return -1;
3014 }
3015
Michal Vasko93224072021-11-09 12:14:28 +01003016 free(endpt->address);
3017 endpt->address = strdup(address);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003018
3019 /* UNLOCK */
3020 nc_server_ch_client_unlock(client);
3021
Michal Vaskoadf30f02019-06-24 09:34:47 +02003022 return 0;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003023}
3024
3025API int
3026nc_server_ch_client_endpt_set_port(const char *client_name, const char *endpt_name, uint16_t port)
3027{
Michal Vasko2e6defd2016-10-07 15:48:15 +02003028 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02003029 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003030
3031 if (!client_name) {
3032 ERRARG("client_name");
3033 return -1;
3034 } else if (!endpt_name) {
3035 ERRARG("endpt_name");
3036 return -1;
3037 } else if (!port) {
3038 ERRARG("port");
3039 return -1;
3040 }
3041
3042 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02003043 endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client);
3044 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02003045 return -1;
3046 }
3047
Michal Vaskoadf30f02019-06-24 09:34:47 +02003048 endpt->port = port;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003049
3050 /* UNLOCK */
3051 nc_server_ch_client_unlock(client);
3052
ravsz5c5a4422020-03-31 15:53:21 +02003053 return 0;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003054}
3055
3056API int
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003057nc_server_ch_client_endpt_enable_keepalives(const char *client_name, const char *endpt_name, int enable)
3058{
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003059 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02003060 struct nc_ch_endpt *endpt;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003061
3062 if (!client_name) {
3063 ERRARG("client_name");
3064 return -1;
3065 } else if (!endpt_name) {
3066 ERRARG("endpt_name");
3067 return -1;
3068 }
3069
3070 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02003071 endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client);
3072 if (!endpt) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003073 return -1;
3074 }
3075
Michal Vaskoadf30f02019-06-24 09:34:47 +02003076 endpt->ka.enabled = (enable ? 1 : 0);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003077
3078 /* UNLOCK */
3079 nc_server_ch_client_unlock(client);
3080
Michal Vasko9af829a2019-09-12 13:50:00 +02003081 return 0;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003082}
3083
3084API int
3085nc_server_ch_client_endpt_set_keepalives(const char *client_name, const char *endpt_name, int idle_time, int max_probes,
3086 int probe_interval)
3087{
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003088 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02003089 struct nc_ch_endpt *endpt;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003090
3091 if (!client_name) {
3092 ERRARG("client_name");
3093 return -1;
3094 } else if (!endpt_name) {
3095 ERRARG("endpt_name");
3096 return -1;
3097 }
3098
3099 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02003100 endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client);
3101 if (!endpt) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003102 return -1;
3103 }
3104
Michal Vaskoadf30f02019-06-24 09:34:47 +02003105 if (idle_time > -1) {
3106 endpt->ka.idle_time = idle_time;
3107 }
3108 if (max_probes > -1) {
3109 endpt->ka.max_probes = max_probes;
3110 }
3111 if (probe_interval > -1) {
3112 endpt->ka.probe_interval = probe_interval;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003113 }
3114
3115 /* UNLOCK */
3116 nc_server_ch_client_unlock(client);
3117
Michal Vasko9af829a2019-09-12 13:50:00 +02003118 return 0;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003119}
3120
3121API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02003122nc_server_ch_client_set_conn_type(const char *client_name, NC_CH_CONN_TYPE conn_type)
3123{
3124 struct nc_ch_client *client;
3125
3126 if (!client_name) {
3127 ERRARG("client_name");
3128 return -1;
3129 } else if (!conn_type) {
3130 ERRARG("conn_type");
3131 return -1;
3132 }
3133
3134 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02003135 nc_server_ch_client_lock(client_name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003136 if (!client) {
3137 return -1;
3138 }
3139
3140 if (client->conn_type != conn_type) {
3141 client->conn_type = conn_type;
3142
3143 /* set default options */
3144 switch (conn_type) {
3145 case NC_CH_PERSIST:
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003146 /* no options */
Michal Vasko2e6defd2016-10-07 15:48:15 +02003147 break;
3148 case NC_CH_PERIOD:
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003149 client->conn.period.period = 60;
3150 client->conn.period.anchor_time = 0;
3151 client->conn.period.idle_timeout = 120;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003152 break;
3153 default:
3154 ERRINT;
3155 break;
3156 }
3157 }
3158
3159 /* UNLOCK */
3160 nc_server_ch_client_unlock(client);
3161
3162 return 0;
3163}
3164
3165API int
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003166nc_server_ch_client_periodic_set_period(const char *client_name, uint16_t period)
3167{
3168 struct nc_ch_client *client;
3169
3170 if (!client_name) {
3171 ERRARG("client_name");
3172 return -1;
3173 } else if (!period) {
3174 ERRARG("period");
3175 return -1;
3176 }
3177
3178 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02003179 nc_server_ch_client_lock(client_name, NULL, 0, &client);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003180 if (!client) {
3181 return -1;
3182 }
3183
3184 if (client->conn_type != NC_CH_PERIOD) {
Michal Vasko05532772021-06-03 12:12:38 +02003185 ERR(NULL, "Call Home client \"%s\" is not of periodic connection type.", client_name);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003186 /* UNLOCK */
3187 nc_server_ch_client_unlock(client);
3188 return -1;
3189 }
3190
3191 client->conn.period.period = period;
3192
3193 /* UNLOCK */
3194 nc_server_ch_client_unlock(client);
3195
3196 return 0;
3197}
3198
3199API int
3200nc_server_ch_client_periodic_set_anchor_time(const char *client_name, time_t anchor_time)
Michal Vasko2e6defd2016-10-07 15:48:15 +02003201{
3202 struct nc_ch_client *client;
3203
3204 if (!client_name) {
3205 ERRARG("client_name");
3206 return -1;
3207 }
3208
3209 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02003210 nc_server_ch_client_lock(client_name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003211 if (!client) {
3212 return -1;
3213 }
3214
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003215 if (client->conn_type != NC_CH_PERIOD) {
Michal Vasko05532772021-06-03 12:12:38 +02003216 ERR(NULL, "Call Home client \"%s\" is not of periodic connection type.", client_name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003217 /* UNLOCK */
3218 nc_server_ch_client_unlock(client);
3219 return -1;
3220 }
3221
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003222 client->conn.period.anchor_time = anchor_time;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003223
3224 /* UNLOCK */
3225 nc_server_ch_client_unlock(client);
3226
3227 return 0;
3228}
3229
3230API int
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003231nc_server_ch_client_periodic_set_idle_timeout(const char *client_name, uint16_t idle_timeout)
Michal Vasko2e6defd2016-10-07 15:48:15 +02003232{
3233 struct nc_ch_client *client;
3234
3235 if (!client_name) {
3236 ERRARG("client_name");
3237 return -1;
3238 }
3239
3240 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02003241 nc_server_ch_client_lock(client_name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003242 if (!client) {
3243 return -1;
3244 }
3245
3246 if (client->conn_type != NC_CH_PERIOD) {
Michal Vasko05532772021-06-03 12:12:38 +02003247 ERR(NULL, "Call Home client \"%s\" is not of periodic connection type.", client_name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003248 /* UNLOCK */
3249 nc_server_ch_client_unlock(client);
3250 return -1;
3251 }
3252
3253 client->conn.period.idle_timeout = idle_timeout;
3254
3255 /* UNLOCK */
3256 nc_server_ch_client_unlock(client);
3257
3258 return 0;
3259}
3260
3261API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02003262nc_server_ch_client_set_start_with(const char *client_name, NC_CH_START_WITH start_with)
3263{
3264 struct nc_ch_client *client;
3265
3266 if (!client_name) {
3267 ERRARG("client_name");
3268 return -1;
3269 }
3270
3271 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02003272 nc_server_ch_client_lock(client_name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003273 if (!client) {
3274 return -1;
3275 }
3276
3277 client->start_with = start_with;
3278
3279 /* UNLOCK */
3280 nc_server_ch_client_unlock(client);
3281
3282 return 0;
3283}
3284
3285API int
3286nc_server_ch_client_set_max_attempts(const char *client_name, uint8_t max_attempts)
3287{
3288 struct nc_ch_client *client;
3289
3290 if (!client_name) {
3291 ERRARG("client_name");
3292 return -1;
3293 } else if (!max_attempts) {
3294 ERRARG("max_attempts");
3295 return -1;
3296 }
3297
3298 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02003299 nc_server_ch_client_lock(client_name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003300 if (!client) {
3301 return -1;
3302 }
3303
3304 client->max_attempts = max_attempts;
3305
3306 /* UNLOCK */
3307 nc_server_ch_client_unlock(client);
3308
3309 return 0;
3310}
3311
Michal Vasko056f53c2022-10-21 13:38:15 +02003312/**
3313 * @brief Create a connection for an endpoint.
3314 *
3315 * Client lock is expected to be held.
3316 *
3317 * @param[in] endpt Endpoint to use.
3318 * @param[in] acquire_ctx_cb Callback for acquiring the libyang context.
3319 * @param[in] release_ctx_cb Callback for releasing the libyang context.
3320 * @param[in] ctx_cb_data Context callbacks data.
3321 * @param[out] session Created NC session.
3322 * @return NC_MSG values.
3323 */
Michal Vasko2e6defd2016-10-07 15:48:15 +02003324static NC_MSG_TYPE
Michal Vasko58bac1c2022-03-24 15:25:26 +01003325nc_connect_ch_endpt(struct nc_ch_endpt *endpt, nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb,
3326 nc_server_ch_session_release_ctx_cb release_ctx_cb, void *ctx_cb_data, struct nc_session **session)
Michal Vaskob05053d2016-01-22 16:12:06 +01003327{
Michal Vasko71090fc2016-05-24 16:37:28 +02003328 NC_MSG_TYPE msgtype;
Michal Vasko58bac1c2022-03-24 15:25:26 +01003329 const struct ly_ctx *ctx = NULL;
Michal Vaskob05053d2016-01-22 16:12:06 +01003330 int sock, ret;
Michal Vasko9fb42272017-10-05 13:50:05 +02003331 struct timespec ts_cur;
Michal Vasko66032bc2019-01-22 15:03:12 +01003332 char *ip_host;
Michal Vaskob05053d2016-01-22 16:12:06 +01003333
Michal Vasko056f53c2022-10-21 13:38:15 +02003334 sock = nc_sock_connect(endpt->address, endpt->port, NC_CH_CONNECT_TIMEOUT, &endpt->ka, &endpt->sock_pending, &ip_host);
Michal Vaskoc61c4492016-01-25 11:13:34 +01003335 if (sock < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02003336 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01003337 }
3338
Michal Vasko93224072021-11-09 12:14:28 +01003339 /* acquire context */
3340 ctx = acquire_ctx_cb(ctx_cb_data);
3341 if (!ctx) {
3342 ERR(NULL, "Failed to acquire context for a new Call Home session.");
3343 close(sock);
3344 free(ip_host);
3345 return NC_MSG_ERROR;
3346 }
3347
3348 /* create session */
Michal Vasko131120a2018-05-29 15:44:02 +02003349 *session = nc_new_session(NC_SERVER, 0);
Michal Vaskob05053d2016-01-22 16:12:06 +01003350 if (!(*session)) {
3351 ERRMEM;
3352 close(sock);
Michal Vasko66032bc2019-01-22 15:03:12 +01003353 free(ip_host);
Michal Vaskod530d362022-09-07 10:07:57 +02003354 msgtype = NC_MSG_ERROR;
3355 goto fail;
Michal Vaskob05053d2016-01-22 16:12:06 +01003356 }
3357 (*session)->status = NC_STATUS_STARTING;
Michal Vasko93224072021-11-09 12:14:28 +01003358 (*session)->ctx = (struct ly_ctx *)ctx;
Michal Vaskodc96bb92023-03-28 08:52:48 +02003359 (*session)->flags = NC_SESSION_SHAREDCTX | NC_SESSION_CALLHOME;
Michal Vasko93224072021-11-09 12:14:28 +01003360 (*session)->host = ip_host;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003361 (*session)->port = endpt->port;
Michal Vaskob05053d2016-01-22 16:12:06 +01003362
Michal Vaskob05053d2016-01-22 16:12:06 +01003363 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01003364#ifdef NC_ENABLED_SSH
Michal Vaskoadf30f02019-06-24 09:34:47 +02003365 if (endpt->ti == NC_TI_LIBSSH) {
3366 (*session)->data = endpt->opts.ssh;
Michal Vasko0190bc32016-03-02 15:47:49 +01003367 ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01003368 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01003369
Michal Vasko71090fc2016-05-24 16:37:28 +02003370 if (ret < 0) {
3371 msgtype = NC_MSG_ERROR;
3372 goto fail;
3373 } else if (!ret) {
3374 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01003375 goto fail;
3376 }
Michal Vasko3d865d22016-01-28 16:00:53 +01003377 } else
3378#endif
Radek Krejci53691be2016-02-22 13:58:37 +01003379#ifdef NC_ENABLED_TLS
Michal Vaskoadf30f02019-06-24 09:34:47 +02003380 if (endpt->ti == NC_TI_OPENSSL) {
3381 (*session)->data = endpt->opts.tls;
Michal Vasko0190bc32016-03-02 15:47:49 +01003382 ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01003383 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01003384
Michal Vasko71090fc2016-05-24 16:37:28 +02003385 if (ret < 0) {
3386 msgtype = NC_MSG_ERROR;
3387 goto fail;
3388 } else if (!ret) {
3389 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01003390 goto fail;
3391 }
Michal Vasko3d865d22016-01-28 16:00:53 +01003392 } else
3393#endif
3394 {
Michal Vaskob05053d2016-01-22 16:12:06 +01003395 ERRINT;
3396 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02003397 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01003398 goto fail;
3399 }
3400
3401 /* assign new SID atomically */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +02003402 (*session)->id = ATOMIC_INC_RELAXED(server_opts.new_session_id);
Michal Vaskob05053d2016-01-22 16:12:06 +01003403
3404 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02003405 msgtype = nc_handshake_io(*session);
Michal Vasko71090fc2016-05-24 16:37:28 +02003406 if (msgtype != NC_MSG_HELLO) {
Michal Vaskob05053d2016-01-22 16:12:06 +01003407 goto fail;
3408 }
Michal Vasko9fb42272017-10-05 13:50:05 +02003409
Michal Vaskod8a74192023-02-06 15:51:50 +01003410 nc_timeouttime_get(&ts_cur, 0);
Michal Vasko9fb42272017-10-05 13:50:05 +02003411 (*session)->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vaskod8a74192023-02-06 15:51:50 +01003412 nc_realtime_get(&ts_cur);
Michal Vasko9fb42272017-10-05 13:50:05 +02003413 (*session)->opts.server.session_start = ts_cur.tv_sec;
Michal Vaskob05053d2016-01-22 16:12:06 +01003414 (*session)->status = NC_STATUS_RUNNING;
3415
Michal Vasko71090fc2016-05-24 16:37:28 +02003416 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01003417
3418fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01003419 nc_session_free(*session, NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01003420 *session = NULL;
Michal Vasko58bac1c2022-03-24 15:25:26 +01003421 if (ctx) {
3422 release_ctx_cb(ctx_cb_data);
3423 }
Michal Vasko71090fc2016-05-24 16:37:28 +02003424 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01003425}
3426
Michal Vasko2e6defd2016-10-07 15:48:15 +02003427struct nc_ch_client_thread_arg {
3428 char *client_name;
Michal Vasko93224072021-11-09 12:14:28 +01003429 nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb;
3430 nc_server_ch_session_release_ctx_cb release_ctx_cb;
3431 void *ctx_cb_data;
3432 nc_server_ch_new_session_cb new_session_cb;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003433};
3434
3435static struct nc_ch_client *
3436nc_server_ch_client_with_endpt_lock(const char *name)
3437{
3438 struct nc_ch_client *client;
3439
3440 while (1) {
3441 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02003442 nc_server_ch_client_lock(name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003443 if (!client) {
3444 return NULL;
3445 }
3446 if (client->ch_endpt_count) {
3447 return client;
3448 }
3449 /* no endpoints defined yet */
3450
3451 /* UNLOCK */
3452 nc_server_ch_client_unlock(client);
3453
3454 usleep(NC_CH_NO_ENDPT_WAIT * 1000);
3455 }
3456
3457 return NULL;
3458}
3459
3460static int
3461nc_server_ch_client_thread_session_cond_wait(struct nc_session *session, struct nc_ch_client_thread_arg *data)
3462{
Michal Vasko3f05a092018-03-13 10:39:49 +01003463 int ret = 0, r;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02003464 uint32_t idle_timeout;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003465 struct timespec ts;
3466 struct nc_ch_client *client;
3467
Michal Vasko2e6defd2016-10-07 15:48:15 +02003468 /* CH LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +01003469 pthread_mutex_lock(&session->opts.server.ch_lock);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003470
Michal Vaskofeccb312022-03-24 15:24:59 +01003471 session->flags |= NC_SESSION_CH_THREAD;
Michal Vasko0db3db52021-03-03 10:45:42 +01003472
Michal Vasko2e6defd2016-10-07 15:48:15 +02003473 /* give the session to the user */
Michal Vasko93224072021-11-09 12:14:28 +01003474 if (data->new_session_cb(data->client_name, session)) {
Michal Vaskof1c26c22021-04-12 16:34:33 +02003475 /* something is wrong, free the session */
Michal Vaskofeccb312022-03-24 15:24:59 +01003476 session->flags &= ~NC_SESSION_CH_THREAD;
Michal Vaskof1c26c22021-04-12 16:34:33 +02003477
3478 /* CH UNLOCK */
3479 pthread_mutex_unlock(&session->opts.server.ch_lock);
3480
Michal Vasko77d56d72022-09-07 10:30:48 +02003481 /* session terminated, free it and release its context */
Michal Vaskof1c26c22021-04-12 16:34:33 +02003482 nc_session_free(session, NULL);
Michal Vasko58bac1c2022-03-24 15:25:26 +01003483 data->release_ctx_cb(data->ctx_cb_data);
3484 return ret;
Michal Vaskof1c26c22021-04-12 16:34:33 +02003485 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02003486
3487 do {
Michal Vaskod8a74192023-02-06 15:51:50 +01003488 nc_timeouttime_get(&ts, NC_CH_NO_ENDPT_WAIT);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003489
Michal Vasko0db3db52021-03-03 10:45:42 +01003490 /* CH COND WAIT */
Michal Vaskod8a74192023-02-06 15:51:50 +01003491 r = pthread_cond_clockwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, COMPAT_CLOCK_ID, &ts);
Michal Vasko3f05a092018-03-13 10:39:49 +01003492 if (!r) {
3493 /* we were woken up, something probably happened */
3494 if (session->status != NC_STATUS_RUNNING) {
3495 break;
3496 }
3497 } else if (r != ETIMEDOUT) {
Michal Vasko05532772021-06-03 12:12:38 +02003498 ERR(session, "Pthread condition timedwait failed (%s).", strerror(r));
Michal Vasko3f05a092018-03-13 10:39:49 +01003499 ret = -1;
3500 break;
Michal Vasko2e39ed92018-03-12 13:51:44 +01003501 }
3502
Michal Vasko2e6defd2016-10-07 15:48:15 +02003503 /* check whether the client was not removed */
3504 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02003505 nc_server_ch_client_lock(data->client_name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003506 if (!client) {
3507 /* client was removed, finish thread */
Michal Vasko05532772021-06-03 12:12:38 +02003508 VRB(session, "Call Home client \"%s\" removed, but an established session will not be terminated.",
Michal Vaskoadf30f02019-06-24 09:34:47 +02003509 data->client_name);
Michal Vasko3f05a092018-03-13 10:39:49 +01003510 ret = 1;
3511 break;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003512 }
3513
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003514 if (client->conn_type == NC_CH_PERIOD) {
Michal Vaskoc4bc5812016-10-13 10:59:36 +02003515 idle_timeout = client->conn.period.idle_timeout;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003516 } else {
3517 idle_timeout = 0;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02003518 }
3519
Michal Vaskod8a74192023-02-06 15:51:50 +01003520 nc_timeouttime_get(&ts, 0);
Michal Vaskodf68e7e2022-04-21 11:04:00 +02003521 if (!nc_session_get_notif_status(session) && idle_timeout && (ts.tv_sec >= session->opts.server.last_rpc + idle_timeout)) {
Michal Vasko05532772021-06-03 12:12:38 +02003522 VRB(session, "Call Home client \"%s\": session idle timeout elapsed.", client->name);
Michal Vaskoc4bc5812016-10-13 10:59:36 +02003523 session->status = NC_STATUS_INVALID;
3524 session->term_reason = NC_SESSION_TERM_TIMEOUT;
3525 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02003526
3527 /* UNLOCK */
3528 nc_server_ch_client_unlock(client);
3529
3530 } while (session->status == NC_STATUS_RUNNING);
3531
Michal Vaskofeccb312022-03-24 15:24:59 +01003532 /* signal to nc_session_free() that CH thread is terminating */
3533 session->flags &= ~NC_SESSION_CH_THREAD;
3534 pthread_cond_signal(&session->opts.server.ch_cond);
Michal Vasko0db3db52021-03-03 10:45:42 +01003535
Michal Vasko27377422018-03-15 08:59:35 +01003536 /* CH UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +01003537 pthread_mutex_unlock(&session->opts.server.ch_lock);
Michal Vasko27377422018-03-15 08:59:35 +01003538
Michal Vasko3f05a092018-03-13 10:39:49 +01003539 return ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003540}
3541
3542static void *
3543nc_ch_client_thread(void *arg)
3544{
3545 struct nc_ch_client_thread_arg *data = (struct nc_ch_client_thread_arg *)arg;
3546 NC_MSG_TYPE msgtype;
3547 uint8_t cur_attempts = 0;
Peter Feiged05f2252018-09-03 08:09:47 +00003548 uint16_t next_endpt_index;
Michal Vasko9550cf12017-03-21 15:33:58 +01003549 char *cur_endpt_name = NULL;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003550 struct nc_ch_endpt *cur_endpt;
3551 struct nc_session *session;
3552 struct nc_ch_client *client;
Michal Vasko5d0a1e32022-09-07 07:46:36 +02003553 uint32_t client_id, reconnect_in;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003554
3555 /* LOCK */
3556 client = nc_server_ch_client_with_endpt_lock(data->client_name);
3557 if (!client) {
3558 goto cleanup;
3559 }
Andrew Langefeld6ed922d2018-09-12 14:08:32 -05003560 client_id = client->id;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003561
3562 cur_endpt = &client->ch_endpts[0];
3563 cur_endpt_name = strdup(cur_endpt->name);
3564
3565 while (1) {
Michal Vasko056f53c2022-10-21 13:38:15 +02003566 if (!cur_attempts) {
3567 VRB(NULL, "Call Home client \"%s\" endpoint \"%s\" connecting...", data->client_name, cur_endpt_name);
3568 }
Michal Vasko58bac1c2022-03-24 15:25:26 +01003569 msgtype = nc_connect_ch_endpt(cur_endpt, data->acquire_ctx_cb, data->release_ctx_cb, data->ctx_cb_data, &session);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003570
3571 if (msgtype == NC_MSG_HELLO) {
3572 /* UNLOCK */
3573 nc_server_ch_client_unlock(client);
3574
Michal Vasko05532772021-06-03 12:12:38 +02003575 VRB(NULL, "Call Home client \"%s\" session %u established.", data->client_name, session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003576 if (nc_server_ch_client_thread_session_cond_wait(session, data)) {
3577 goto cleanup;
3578 }
Michal Vasko05532772021-06-03 12:12:38 +02003579 VRB(NULL, "Call Home client \"%s\" session terminated.", data->client_name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003580
3581 /* LOCK */
3582 client = nc_server_ch_client_with_endpt_lock(data->client_name);
3583 if (!client) {
3584 goto cleanup;
3585 }
Andrew Langefeld6ed922d2018-09-12 14:08:32 -05003586 if (client->id != client_id) {
3587 nc_server_ch_client_unlock(client);
3588 goto cleanup;
3589 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02003590
3591 /* session changed status -> it was disconnected for whatever reason,
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003592 * persistent connection immediately tries to reconnect, periodic connects at specific times */
Michal Vasko2e6defd2016-10-07 15:48:15 +02003593 if (client->conn_type == NC_CH_PERIOD) {
Michal Vasko18e1fa02021-11-29 09:02:05 +01003594 if (client->conn.period.anchor_time) {
3595 /* anchored */
3596 reconnect_in = (time(NULL) - client->conn.period.anchor_time) % (client->conn.period.period * 60);
3597 } else {
3598 /* fixed timeout */
3599 reconnect_in = client->conn.period.period * 60;
3600 }
3601
Michal Vasko2e6defd2016-10-07 15:48:15 +02003602 /* UNLOCK */
3603 nc_server_ch_client_unlock(client);
3604
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003605 /* sleep until we should reconnect TODO wake up sometimes to check for new notifications */
Michal Vasko75b836e2022-09-07 07:51:45 +02003606 VRB(NULL, "Call Home client \"%s\" reconnecting in %" PRIu32 " seconds.", data->client_name, reconnect_in);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003607 sleep(reconnect_in);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003608
3609 /* LOCK */
3610 client = nc_server_ch_client_with_endpt_lock(data->client_name);
3611 if (!client) {
3612 goto cleanup;
3613 }
Andrew Langefeld6ed922d2018-09-12 14:08:32 -05003614 if (client->id != client_id) {
3615 nc_server_ch_client_unlock(client);
3616 goto cleanup;
3617 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02003618 }
3619
3620 /* set next endpoint to try */
3621 if (client->start_with == NC_CH_FIRST_LISTED) {
Peter Feiged05f2252018-09-03 08:09:47 +00003622 next_endpt_index = 0;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003623 } else if (client->start_with == NC_CH_LAST_CONNECTED) {
Peter Feiged05f2252018-09-03 08:09:47 +00003624 /* we keep the current one but due to unlock/lock we have to find it again */
3625 for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) {
3626 if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) {
3627 break;
3628 }
3629 }
3630 if (next_endpt_index >= client->ch_endpt_count) {
3631 /* endpoint was removed, start with the first one */
3632 next_endpt_index = 0;
3633 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003634 } else {
3635 /* just get a random index */
3636 next_endpt_index = rand() % client->ch_endpt_count;
Peter Feiged05f2252018-09-03 08:09:47 +00003637 }
3638
Michal Vasko2e6defd2016-10-07 15:48:15 +02003639 } else {
Michal Vasko6bb116b2016-10-26 13:53:46 +02003640 /* UNLOCK */
3641 nc_server_ch_client_unlock(client);
3642
Michal Vasko2e6defd2016-10-07 15:48:15 +02003643 /* session was not created */
Michal Vaskod8951c72022-09-26 09:39:29 +02003644 sleep(NC_CH_ENDPT_BACKOFF_WAIT);
Michal Vaskoc4bc5812016-10-13 10:59:36 +02003645
Michal Vasko6bb116b2016-10-26 13:53:46 +02003646 /* LOCK */
3647 client = nc_server_ch_client_with_endpt_lock(data->client_name);
3648 if (!client) {
3649 goto cleanup;
3650 }
Andrew Langefeld6ed922d2018-09-12 14:08:32 -05003651 if (client->id != client_id) {
3652 nc_server_ch_client_unlock(client);
3653 goto cleanup;
3654 }
Michal Vasko6bb116b2016-10-26 13:53:46 +02003655
Michal Vasko2e6defd2016-10-07 15:48:15 +02003656 ++cur_attempts;
Michal Vasko4cb8de52018-04-23 14:38:07 +02003657
3658 /* try to find our endpoint again */
Peter Feiged05f2252018-09-03 08:09:47 +00003659 for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) {
3660 if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) {
Michal Vasko4cb8de52018-04-23 14:38:07 +02003661 break;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003662 }
Michal Vasko4cb8de52018-04-23 14:38:07 +02003663 }
3664
Peter Feiged05f2252018-09-03 08:09:47 +00003665 if (next_endpt_index >= client->ch_endpt_count) {
Michal Vasko4cb8de52018-04-23 14:38:07 +02003666 /* endpoint was removed, start with the first one */
Michal Vasko056f53c2022-10-21 13:38:15 +02003667 VRB(NULL, "Call Home client \"%s\" endpoint \"%s\" removed.", data->client_name, cur_endpt_name);
Peter Feiged05f2252018-09-03 08:09:47 +00003668 next_endpt_index = 0;
Michal Vasko4cb8de52018-04-23 14:38:07 +02003669 cur_attempts = 0;
3670 } else if (cur_attempts == client->max_attempts) {
3671 /* we have tried to connect to this endpoint enough times */
Michal Vasko056f53c2022-10-21 13:38:15 +02003672 VRB(NULL, "Call Home client \"%s\" endpoint \"%s\" failed connection attempt limit %" PRIu8 " reached.",
3673 data->client_name, cur_endpt_name, client->max_attempts);
3674
3675 /* clear a pending socket, if any */
3676 cur_endpt = &client->ch_endpts[next_endpt_index];
3677 if (cur_endpt->sock_pending > -1) {
3678 close(cur_endpt->sock_pending);
3679 cur_endpt->sock_pending = -1;
3680 }
3681
Peter Feiged05f2252018-09-03 08:09:47 +00003682 if (next_endpt_index < client->ch_endpt_count - 1) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02003683 /* just go to the next endpoint */
Peter Feiged05f2252018-09-03 08:09:47 +00003684 ++next_endpt_index;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003685 } else {
Michal Vasko4cb8de52018-04-23 14:38:07 +02003686 /* cur_endpoint is the last, start with the first one */
Michal Vasko2a225342018-09-05 08:38:34 +02003687 next_endpt_index = 0;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003688 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02003689 cur_attempts = 0;
3690 } /* else we keep the current one */
3691 }
Peter Feiged05f2252018-09-03 08:09:47 +00003692
3693 cur_endpt = &client->ch_endpts[next_endpt_index];
3694 free(cur_endpt_name);
3695 cur_endpt_name = strdup(cur_endpt->name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003696 }
3697
3698cleanup:
Michal Vasko05532772021-06-03 12:12:38 +02003699 VRB(NULL, "Call Home client \"%s\" thread exit.", data->client_name);
Michal Vasko9550cf12017-03-21 15:33:58 +01003700 free(cur_endpt_name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003701 free(data->client_name);
3702 free(data);
3703 return NULL;
3704}
3705
3706API int
Michal Vasko93224072021-11-09 12:14:28 +01003707nc_connect_ch_client_dispatch(const char *client_name, nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb,
3708 nc_server_ch_session_release_ctx_cb release_ctx_cb, void *ctx_cb_data, nc_server_ch_new_session_cb new_session_cb)
Michal Vasko3f05a092018-03-13 10:39:49 +01003709{
Michal Vasko2e6defd2016-10-07 15:48:15 +02003710 int ret;
3711 pthread_t tid;
3712 struct nc_ch_client_thread_arg *arg;
3713
3714 if (!client_name) {
3715 ERRARG("client_name");
3716 return -1;
Michal Vasko93224072021-11-09 12:14:28 +01003717 } else if (!acquire_ctx_cb) {
3718 ERRARG("acquire_ctx_cb");
3719 return -1;
3720 } else if (!release_ctx_cb) {
3721 ERRARG("release_ctx_cb");
3722 return -1;
3723 } else if (!new_session_cb) {
3724 ERRARG("new_session_cb");
Michal Vasko2e6defd2016-10-07 15:48:15 +02003725 return -1;
3726 }
3727
3728 arg = malloc(sizeof *arg);
3729 if (!arg) {
3730 ERRMEM;
3731 return -1;
3732 }
3733 arg->client_name = strdup(client_name);
3734 if (!arg->client_name) {
3735 ERRMEM;
3736 free(arg);
3737 return -1;
3738 }
Michal Vasko93224072021-11-09 12:14:28 +01003739 arg->acquire_ctx_cb = acquire_ctx_cb;
3740 arg->release_ctx_cb = release_ctx_cb;
3741 arg->ctx_cb_data = ctx_cb_data;
3742 arg->new_session_cb = new_session_cb;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003743
3744 ret = pthread_create(&tid, NULL, nc_ch_client_thread, arg);
3745 if (ret) {
Michal Vasko05532772021-06-03 12:12:38 +02003746 ERR(NULL, "Creating a new thread failed (%s).", strerror(ret));
Michal Vasko2e6defd2016-10-07 15:48:15 +02003747 free(arg->client_name);
3748 free(arg);
3749 return -1;
3750 }
3751 /* the thread now manages arg */
3752
3753 pthread_detach(tid);
3754
3755 return 0;
3756}
3757
Radek Krejci53691be2016-02-22 13:58:37 +01003758#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskof8352352016-05-24 09:11:36 +02003759
Michal Vaskoc45ebd32016-05-25 11:17:36 +02003760API time_t
3761nc_session_get_start_time(const struct nc_session *session)
Michal Vaskof8352352016-05-24 09:11:36 +02003762{
Michal Vasko2e6defd2016-10-07 15:48:15 +02003763 if (!session || (session->side != NC_SERVER)) {
Michal Vaskof8352352016-05-24 09:11:36 +02003764 ERRARG("session");
Michal Vaskoc45ebd32016-05-25 11:17:36 +02003765 return 0;
Michal Vaskof8352352016-05-24 09:11:36 +02003766 }
3767
Michal Vasko2e6defd2016-10-07 15:48:15 +02003768 return session->opts.server.session_start;
Michal Vaskof8352352016-05-24 09:11:36 +02003769}
Michal Vasko3486a7c2017-03-03 13:28:07 +01003770
3771API void
Michal Vasko71dbd772021-03-23 14:08:37 +01003772nc_session_inc_notif_status(struct nc_session *session)
Michal Vasko3486a7c2017-03-03 13:28:07 +01003773{
3774 if (!session || (session->side != NC_SERVER)) {
3775 ERRARG("session");
3776 return;
3777 }
3778
Michal Vaskodf68e7e2022-04-21 11:04:00 +02003779 /* NTF STATUS LOCK */
3780 pthread_mutex_lock(&session->opts.server.ntf_status_lock);
3781
Michal Vasko71dbd772021-03-23 14:08:37 +01003782 ++session->opts.server.ntf_status;
Michal Vaskodf68e7e2022-04-21 11:04:00 +02003783
3784 /* NTF STATUS UNLOCK */
3785 pthread_mutex_unlock(&session->opts.server.ntf_status_lock);
Michal Vasko71dbd772021-03-23 14:08:37 +01003786}
3787
3788API void
3789nc_session_dec_notif_status(struct nc_session *session)
3790{
3791 if (!session || (session->side != NC_SERVER)) {
3792 ERRARG("session");
3793 return;
3794 }
3795
Michal Vaskodf68e7e2022-04-21 11:04:00 +02003796 /* NTF STATUS LOCK */
3797 pthread_mutex_lock(&session->opts.server.ntf_status_lock);
3798
Michal Vasko71dbd772021-03-23 14:08:37 +01003799 if (session->opts.server.ntf_status) {
3800 --session->opts.server.ntf_status;
3801 }
Michal Vaskodf68e7e2022-04-21 11:04:00 +02003802
3803 /* NTF STATUS UNLOCK */
3804 pthread_mutex_unlock(&session->opts.server.ntf_status_lock);
Michal Vasko3486a7c2017-03-03 13:28:07 +01003805}
3806
3807API int
3808nc_session_get_notif_status(const struct nc_session *session)
3809{
Michal Vaskodf68e7e2022-04-21 11:04:00 +02003810 uint32_t ntf_status;
3811
Michal Vasko3486a7c2017-03-03 13:28:07 +01003812 if (!session || (session->side != NC_SERVER)) {
3813 ERRARG("session");
3814 return 0;
3815 }
3816
Michal Vaskodf68e7e2022-04-21 11:04:00 +02003817 /* NTF STATUS LOCK */
3818 pthread_mutex_lock(&((struct nc_session *)session)->opts.server.ntf_status_lock);
3819
3820 ntf_status = session->opts.server.ntf_status;
3821
3822 /* NTF STATUS UNLOCK */
3823 pthread_mutex_unlock(&((struct nc_session *)session)->opts.server.ntf_status_lock);
3824
3825 return ntf_status;
Michal Vasko086311b2016-01-08 09:53:11 +01003826}