blob: d49edb146cbf2b4a30093cf316d229f44924985a [file] [log] [blame]
Michal Vasko086311b2016-01-08 09:53:11 +01001/**
2 * \file session_server.c
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 server session manipulation functions
5 *
Michal Vasko18aeb5d2017-02-17 09:23:56 +01006 * Copyright (c) 2015 - 2017 CESNET, z.s.p.o.
Michal Vasko086311b2016-01-08 09:53:11 +01007 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko086311b2016-01-08 09:53:11 +010013 */
apropp-molex4e903c32020-04-20 03:06:58 -040014#define _QNX_SOURCE /* getpeereid */
Olivier Matzac7fa2f2018-10-11 10:02:04 +020015#define _GNU_SOURCE /* signals, threads, SO_PEERCRED */
Michal Vasko086311b2016-01-08 09:53:11 +010016
17#include <stdint.h>
18#include <stdlib.h>
19#include <errno.h>
20#include <string.h>
21#include <poll.h>
22#include <sys/types.h>
23#include <sys/socket.h>
Olivier Matzac7fa2f2018-10-11 10:02:04 +020024#include <sys/un.h>
25#include <netinet/in.h>
26#include <netinet/tcp.h>
Michal Vasko086311b2016-01-08 09:53:11 +010027#include <arpa/inet.h>
28#include <unistd.h>
Michal Vasko0190bc32016-03-02 15:47:49 +010029#include <fcntl.h>
Michal Vaskob48aa812016-01-18 14:13:09 +010030#include <pthread.h>
Michal Vasko11d142a2016-01-19 15:58:24 +010031#include <time.h>
Michal Vaskoade892d2017-02-22 13:40:35 +010032#include <signal.h>
Olivier Matzac7fa2f2018-10-11 10:02:04 +020033#include <pwd.h>
Michal Vasko086311b2016-01-08 09:53:11 +010034
Michal Vasko1a38c862016-01-15 15:50:07 +010035#include "libnetconf.h"
Michal Vasko086311b2016-01-08 09:53:11 +010036#include "session_server.h"
Michal Vasko0bdf70b2019-06-24 19:20:20 +020037#include "session_server_ch.h"
Michal Vasko086311b2016-01-08 09:53:11 +010038
Michal Vaskob48aa812016-01-18 14:13:09 +010039struct nc_server_opts server_opts = {
Michal Vaskoade892d2017-02-22 13:40:35 +010040#ifdef NC_ENABLED_SSH
41 .authkey_lock = PTHREAD_MUTEX_INITIALIZER,
42#endif
43 .bind_lock = PTHREAD_MUTEX_INITIALIZER,
Michal Vasko2e6defd2016-10-07 15:48:15 +020044 .endpt_lock = PTHREAD_RWLOCK_INITIALIZER,
45 .ch_client_lock = PTHREAD_RWLOCK_INITIALIZER
Michal Vaskob48aa812016-01-18 14:13:09 +010046};
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010047
fanchanghu966f2de2016-07-21 02:28:57 -040048static nc_rpc_clb global_rpc_clb = NULL;
49
Michal Vasko3031aae2016-01-27 16:07:18 +010050struct nc_endpt *
Michal Vaskoade892d2017-02-22 13:40:35 +010051nc_server_endpt_lock_get(const char *name, NC_TRANSPORT_IMPL ti, uint16_t *idx)
Michal Vasko3031aae2016-01-27 16:07:18 +010052{
53 uint16_t i;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010054 struct nc_endpt *endpt = NULL;
55
Michal Vaskoddce1212019-05-24 09:58:49 +020056 if (!name) {
57 ERRARG("endpt_name");
58 return NULL;
59 }
60
Michal Vaskoade892d2017-02-22 13:40:35 +010061 /* WRITE LOCK */
62 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010063
64 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vasko2e6defd2016-10-07 15:48:15 +020065 if (!strcmp(server_opts.endpts[i].name, name) && (!ti || (server_opts.endpts[i].ti == ti))) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010066 endpt = &server_opts.endpts[i];
67 break;
Michal Vasko3031aae2016-01-27 16:07:18 +010068 }
69 }
70
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010071 if (!endpt) {
72 ERR("Endpoint \"%s\" was not found.", name);
Michal Vaskoade892d2017-02-22 13:40:35 +010073 /* UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +020074 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010075 return NULL;
76 }
77
Michal Vaskoe2713da2016-08-22 16:06:40 +020078 if (idx) {
79 *idx = i;
80 }
81
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010082 return endpt;
83}
84
Michal Vaskoadf30f02019-06-24 09:34:47 +020085struct nc_ch_endpt *
86nc_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 +010087{
Michal Vaskoadf30f02019-06-24 09:34:47 +020088 uint16_t i, j;
Michal Vasko2e6defd2016-10-07 15:48:15 +020089 struct nc_ch_client *client = NULL;
Michal Vaskoadf30f02019-06-24 09:34:47 +020090 struct nc_ch_endpt *endpt = NULL;
91
92 *client_p = NULL;
Michal Vasko2e6defd2016-10-07 15:48:15 +020093
Michal Vaskoddce1212019-05-24 09:58:49 +020094 if (!name) {
95 ERRARG("client_name");
96 return NULL;
97 }
98
Michal Vasko2e6defd2016-10-07 15:48:15 +020099 /* READ LOCK */
100 pthread_rwlock_rdlock(&server_opts.ch_client_lock);
101
102 for (i = 0; i < server_opts.ch_client_count; ++i) {
Michal Vaskoadf30f02019-06-24 09:34:47 +0200103 if (!strcmp(server_opts.ch_clients[i].name, name)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +0200104 client = &server_opts.ch_clients[i];
Michal Vaskoadf30f02019-06-24 09:34:47 +0200105 if (!endpt_name && !ti) {
106 /* return only client */
107 break;
108 }
109 for (j = 0; j < client->ch_endpt_count; ++j) {
110 if (!strcmp(client->ch_endpts[j].name, endpt_name) && (!ti || (ti == client->ch_endpts[j].ti))) {
111 endpt = &client->ch_endpts[j];
112 break;
113 }
114 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200115 break;
116 }
117 }
118
119 if (!client) {
120 ERR("Call Home client \"%s\" was not found.", name);
Michal Vaskoadf30f02019-06-24 09:34:47 +0200121
Michal Vasko2e6defd2016-10-07 15:48:15 +0200122 /* READ UNLOCK */
123 pthread_rwlock_unlock(&server_opts.ch_client_lock);
Michal Vaskoadf30f02019-06-24 09:34:47 +0200124 } else if (endpt_name && ti && !endpt) {
125 ERR("Call Home client \"%s\" endpoint \"%s\" was not found.", name, endpt_name);
126
127 /* READ UNLOCK */
128 pthread_rwlock_unlock(&server_opts.ch_client_lock);
129 } else {
130 /* CH CLIENT LOCK */
131 pthread_mutex_lock(&client->lock);
132
133 *client_p = client;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200134 }
135
Michal Vaskoadf30f02019-06-24 09:34:47 +0200136 return endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +0200137}
138
139void
140nc_server_ch_client_unlock(struct nc_ch_client *client)
141{
142 /* CH CLIENT UNLOCK */
143 pthread_mutex_unlock(&client->lock);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100144
145 /* READ UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200146 pthread_rwlock_unlock(&server_opts.ch_client_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100147}
Michal Vasko086311b2016-01-08 09:53:11 +0100148
Michal Vasko1a38c862016-01-15 15:50:07 +0100149API void
150nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason)
151{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200152 if (!session) {
153 ERRARG("session");
154 return;
155 } else if (!reason) {
156 ERRARG("reason");
Michal Vasko1a38c862016-01-15 15:50:07 +0100157 return;
158 }
159
Michal Vasko142cfea2017-08-07 10:12:11 +0200160 if ((reason != NC_SESSION_TERM_KILLED) && (session->term_reason == NC_SESSION_TERM_KILLED)) {
161 session->killed_by = 0;
162 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100163 session->term_reason = reason;
164}
165
Michal Vasko142cfea2017-08-07 10:12:11 +0200166API void
167nc_session_set_killed_by(struct nc_session *session, uint32_t sid)
168{
169 if (!session || (session->term_reason != NC_SESSION_TERM_KILLED)) {
170 ERRARG("session");
171 return;
172 } else if (!sid) {
173 ERRARG("sid");
174 return;
175 }
176
177 session->killed_by = sid;
178}
179
180API void
181nc_session_set_status(struct nc_session *session, NC_STATUS status)
182{
183 if (!session) {
184 ERRARG("session");
185 return;
186 } else if (!status) {
187 ERRARG("status");
188 return;
189 }
190
191 session->status = status;
192}
193
Michal Vasko086311b2016-01-08 09:53:11 +0100194int
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200195nc_sock_listen_inet(const char *address, uint16_t port, struct nc_keepalives *ka)
Michal Vasko086311b2016-01-08 09:53:11 +0100196{
Michal Vasko06c860d2018-07-09 16:08:52 +0200197 int opt;
Michal Vasko086311b2016-01-08 09:53:11 +0100198 int is_ipv4, sock;
199 struct sockaddr_storage saddr;
200
201 struct sockaddr_in *saddr4;
202 struct sockaddr_in6 *saddr6;
203
204
205 if (!strchr(address, ':')) {
206 is_ipv4 = 1;
207 } else {
208 is_ipv4 = 0;
209 }
210
211 sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0);
212 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100213 ERR("Failed to create socket (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100214 goto fail;
215 }
216
Michal Vaskobe52dc22018-10-17 09:28:17 +0200217 /* these options will be inherited by accepted sockets */
Michal Vasko06c860d2018-07-09 16:08:52 +0200218 opt = 1;
219 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) == -1) {
220 ERR("Could not set SO_REUSEADDR socket option (%s).", strerror(errno));
221 goto fail;
222 }
Michal Vasko83ad17e2019-01-30 10:11:37 +0100223 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) {
224 ERR("Could not set TCP_NODELAY socket option (%s).", strerror(errno));
225 goto fail;
226 }
Michal Vaskobe52dc22018-10-17 09:28:17 +0200227
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200228 if (nc_sock_enable_keepalive(sock, ka)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100229 goto fail;
230 }
231
Michal Vaskof22d5ff2020-04-15 11:10:27 +0200232 memset(&saddr, 0, sizeof(struct sockaddr_storage));
Michal Vasko086311b2016-01-08 09:53:11 +0100233 if (is_ipv4) {
234 saddr4 = (struct sockaddr_in *)&saddr;
235
236 saddr4->sin_family = AF_INET;
237 saddr4->sin_port = htons(port);
238
239 if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100240 ERR("Failed to convert IPv4 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100241 goto fail;
242 }
243
244 if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100245 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100246 goto fail;
247 }
248
249 } else {
250 saddr6 = (struct sockaddr_in6 *)&saddr;
251
252 saddr6->sin6_family = AF_INET6;
253 saddr6->sin6_port = htons(port);
254
255 if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100256 ERR("Failed to convert IPv6 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100257 goto fail;
258 }
259
260 if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100261 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100262 goto fail;
263 }
264 }
265
Michal Vaskofb89d772016-01-08 12:25:35 +0100266 if (listen(sock, NC_REVERSE_QUEUE) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100267 ERR("Unable to start listening on \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100268 goto fail;
269 }
270
271 return sock;
272
273fail:
274 if (sock > -1) {
275 close(sock);
276 }
277
278 return -1;
279}
280
281int
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200282nc_sock_listen_unix(const char *address, const struct nc_server_unix_opts *opts)
283{
284 struct sockaddr_un sun;
285 int sock = -1;
286
287 sock = socket(AF_UNIX, SOCK_STREAM, 0);
288 if (sock == -1) {
289 ERR("Failed to create socket (%s).", strerror(errno));
290 goto fail;
291 }
292
293 memset(&sun, 0, sizeof(sun));
294 sun.sun_family = AF_UNIX;
295 snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", address);
296
297 unlink(sun.sun_path);
298 if (bind(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
299 ERR("Could not bind \"%s\" (%s).", address, strerror(errno));
300 goto fail;
301 }
302
303 if (opts->mode != (mode_t)-1) {
304 if (chmod(sun.sun_path, opts->mode) < 0) {
305 ERR("Failed to set unix socket permissions (%s).", strerror(errno));
306 goto fail;
307 }
308 }
309
310 if (opts->uid != (uid_t)-1 || opts->gid != (gid_t)-1) {
311 if (chown(sun.sun_path, opts->uid, opts->gid) < 0) {
312 ERR("Failed to set unix socket uid/gid (%s).", strerror(errno));
313 goto fail;
314 }
315 }
316
317 if (listen(sock, NC_REVERSE_QUEUE) == -1) {
318 ERR("Unable to start listening on \"%s\" (%s).", address, strerror(errno));
319 goto fail;
320 }
321
322 return sock;
323
324fail:
325 if (sock > -1) {
326 close(sock);
327 }
328
329 return -1;
330}
331
332int
Michal Vasko3031aae2016-01-27 16:07:18 +0100333nc_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 +0100334{
Michal Vaskof54cd352017-02-22 13:42:02 +0100335 sigset_t sigmask, origmask;
Michal Vaskoac2f6182017-01-30 14:32:03 +0100336 uint16_t i, j, pfd_count;
Michal Vasko086311b2016-01-08 09:53:11 +0100337 struct pollfd *pfd;
338 struct sockaddr_storage saddr;
339 socklen_t saddr_len = sizeof(saddr);
Michal Vasko0190bc32016-03-02 15:47:49 +0100340 int ret, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100341
342 pfd = malloc(bind_count * sizeof *pfd);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100343 if (!pfd) {
344 ERRMEM;
345 return -1;
346 }
347
Michal Vaskoac2f6182017-01-30 14:32:03 +0100348 for (i = 0, pfd_count = 0; i < bind_count; ++i) {
Michal Vasko94acafc2016-09-23 13:40:10 +0200349 if (binds[i].sock < 0) {
350 /* invalid socket */
Michal Vasko94acafc2016-09-23 13:40:10 +0200351 continue;
352 }
Michal Vasko0a3f3752016-10-13 14:58:38 +0200353 if (binds[i].pollin) {
354 binds[i].pollin = 0;
355 /* leftover pollin */
356 sock = binds[i].sock;
Michal Vasko086311b2016-01-08 09:53:11 +0100357 break;
358 }
Michal Vaskoac2f6182017-01-30 14:32:03 +0100359 pfd[pfd_count].fd = binds[i].sock;
360 pfd[pfd_count].events = POLLIN;
361 pfd[pfd_count].revents = 0;
362
363 ++pfd_count;
Michal Vasko086311b2016-01-08 09:53:11 +0100364 }
365
Michal Vasko0a3f3752016-10-13 14:58:38 +0200366 if (sock == -1) {
367 /* poll for a new connection */
Michal Vaskof54cd352017-02-22 13:42:02 +0100368 sigfillset(&sigmask);
369 pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
Michal Vaskoac2f6182017-01-30 14:32:03 +0100370 ret = poll(pfd, pfd_count, timeout);
Michal Vaskof54cd352017-02-22 13:42:02 +0100371 pthread_sigmask(SIG_SETMASK, &origmask, NULL);
372
Michal Vasko0a3f3752016-10-13 14:58:38 +0200373 if (!ret) {
374 /* we timeouted */
375 free(pfd);
376 return 0;
377 } else if (ret == -1) {
378 ERR("Poll failed (%s).", strerror(errno));
379 free(pfd);
380 return -1;
381 }
Michal Vasko086311b2016-01-08 09:53:11 +0100382
Michal Vaskoac2f6182017-01-30 14:32:03 +0100383 for (i = 0, j = 0; j < pfd_count; ++i, ++j) {
384 /* adjust i so that indices in binds and pfd always match */
385 while (binds[i].sock != pfd[j].fd) {
386 ++i;
387 }
388
389 if (pfd[j].revents & POLLIN) {
Michal Vasko0a3f3752016-10-13 14:58:38 +0200390 --ret;
391
392 if (!ret) {
393 /* the last socket with an event, use it */
Michal Vaskoac2f6182017-01-30 14:32:03 +0100394 sock = pfd[j].fd;
Michal Vasko0a3f3752016-10-13 14:58:38 +0200395 break;
396 } else {
397 /* just remember the event for next time */
398 binds[i].pollin = 1;
399 }
400 }
Michal Vasko086311b2016-01-08 09:53:11 +0100401 }
402 }
403 free(pfd);
404
405 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100406 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100407 return -1;
408 }
409
410 ret = accept(sock, (struct sockaddr *)&saddr, &saddr_len);
Michal Vasko3f6cc4a2016-01-21 15:58:53 +0100411 if (ret < 0) {
Michal Vaskod083db62016-01-19 10:31:29 +0100412 ERR("Accept failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100413 return -1;
414 }
Michal Vasko6ccb29d2016-10-13 15:00:27 +0200415 VRB("Accepted a connection on %s:%u.", binds[i].address, binds[i].port);
Michal Vasko086311b2016-01-08 09:53:11 +0100416
Michal Vasko0190bc32016-03-02 15:47:49 +0100417 /* make the socket non-blocking */
418 if (((flags = fcntl(ret, F_GETFL)) == -1) || (fcntl(ret, F_SETFL, flags | O_NONBLOCK) == -1)) {
419 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100420 close(ret);
Michal Vasko0190bc32016-03-02 15:47:49 +0100421 return -1;
422 }
423
Michal Vasko3031aae2016-01-27 16:07:18 +0100424 if (idx) {
425 *idx = i;
Michal Vasko9e036d52016-01-08 10:49:26 +0100426 }
427
Michal Vasko086311b2016-01-08 09:53:11 +0100428 /* host was requested */
429 if (host) {
430 if (saddr.ss_family == AF_INET) {
Frank Rimpler740c22f2018-08-06 13:55:16 +0000431 *host = malloc(INET_ADDRSTRLEN);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100432 if (*host) {
Frank Rimpler740c22f2018-08-06 13:55:16 +0000433 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&saddr)->sin_addr.s_addr, *host, INET_ADDRSTRLEN)) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100434 ERR("inet_ntop failed (%s).", strerror(errno));
435 free(*host);
436 *host = NULL;
437 }
Michal Vasko086311b2016-01-08 09:53:11 +0100438
Michal Vasko4eb3c312016-03-01 14:09:37 +0100439 if (port) {
440 *port = ntohs(((struct sockaddr_in *)&saddr)->sin_port);
441 }
442 } else {
443 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100444 }
445 } else if (saddr.ss_family == AF_INET6) {
Jan Kundrát0f942e82018-02-14 14:52:00 +0100446 *host = malloc(INET6_ADDRSTRLEN);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100447 if (*host) {
Frank Rimpler740c22f2018-08-06 13:55:16 +0000448 if (!inet_ntop(AF_INET6, ((struct sockaddr_in6 *)&saddr)->sin6_addr.s6_addr, *host, INET6_ADDRSTRLEN)) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100449 ERR("inet_ntop failed (%s).", strerror(errno));
450 free(*host);
451 *host = NULL;
452 }
Michal Vasko086311b2016-01-08 09:53:11 +0100453
Michal Vasko4eb3c312016-03-01 14:09:37 +0100454 if (port) {
455 *port = ntohs(((struct sockaddr_in6 *)&saddr)->sin6_port);
456 }
457 } else {
458 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100459 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200460 } else if (saddr.ss_family == AF_UNIX) {
461 *host = strdup(((struct sockaddr_un *)&saddr)->sun_path);
462 if (*host) {
463 if (port) {
464 *port = 0;
465 }
466 } else {
467 ERRMEM;
468 }
Michal Vasko086311b2016-01-08 09:53:11 +0100469 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100470 ERR("Source host of an unknown protocol family.");
Michal Vasko086311b2016-01-08 09:53:11 +0100471 }
472 }
473
474 return ret;
475}
476
Michal Vasko05ba9df2016-01-13 14:40:27 +0100477static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100478nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *UNUSED(session))
Michal Vasko05ba9df2016-01-13 14:40:27 +0100479{
480 const char *identifier = NULL, *version = NULL, *format = NULL;
481 char *model_data = NULL;
482 const struct lys_module *module;
483 struct nc_server_error *err;
484 struct lyd_node *child, *data = NULL;
Michal Vasko88639e92017-08-03 14:38:10 +0200485 const struct lys_node *sdata = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100486
487 LY_TREE_FOR(rpc->child, child) {
488 if (!strcmp(child->schema->name, "identifier")) {
489 identifier = ((struct lyd_node_leaf_list *)child)->value_str;
490 } else if (!strcmp(child->schema->name, "version")) {
491 version = ((struct lyd_node_leaf_list *)child)->value_str;
Radek Krejci1afa7792017-03-26 11:24:16 -0500492 if (version && version[0] == '\0') {
493 version = NULL;
494 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100495 } else if (!strcmp(child->schema->name, "format")) {
496 format = ((struct lyd_node_leaf_list *)child)->value_str;
497 }
498 }
Michal Vaskoddce1212019-05-24 09:58:49 +0200499 VRB("Schema \"%s\" was requested.", identifier);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100500
501 /* check version */
502 if (version && (strlen(version) != 10) && strcmp(version, "1.0")) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100503 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
504 nc_err_set_msg(err, "The requested version is not supported.", "en");
505 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100506 }
507
508 /* check and get module with the name identifier */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200509 module = ly_ctx_get_module(server_opts.ctx, identifier, version, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100510 if (!module) {
Michal Vaskod91f6e62016-04-05 11:34:22 +0200511 module = (const struct lys_module *)ly_ctx_get_submodule(server_opts.ctx, NULL, NULL, identifier, version);
512 }
513 if (!module) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100514 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
515 nc_err_set_msg(err, "The requested schema was not found.", "en");
516 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100517 }
518
519 /* check format */
Radek Krejci90fba642016-12-07 15:59:45 +0100520 if (!format || !strcmp(format, "ietf-netconf-monitoring:yang")) {
Michal Vaskof8aa9972018-01-31 13:19:08 +0100521 lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL, 0, 0);
Radek Krejci90fba642016-12-07 15:59:45 +0100522 } else if (!strcmp(format, "ietf-netconf-monitoring:yin")) {
Michal Vaskof8aa9972018-01-31 13:19:08 +0100523 lys_print_mem(&model_data, module, LYS_OUT_YIN, NULL, 0, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100524 } else {
Michal Vasko1a38c862016-01-15 15:50:07 +0100525 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
526 nc_err_set_msg(err, "The requested format is not supported.", "en");
527 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100528 }
Michal Vaskod91f6e62016-04-05 11:34:22 +0200529 if (!model_data) {
530 ERRINT;
531 return NULL;
532 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100533
Michal Vasko88639e92017-08-03 14:38:10 +0200534 sdata = ly_ctx_get_node(server_opts.ctx, NULL, "/ietf-netconf-monitoring:get-schema/data", 1);
535 if (!sdata) {
536 ERRINT;
537 free(model_data);
538 return NULL;
539 }
540
Radek Krejci539efb62016-08-24 15:05:16 +0200541 data = lyd_new_path(NULL, server_opts.ctx, "/ietf-netconf-monitoring:get-schema/data", model_data,
542 LYD_ANYDATA_STRING, LYD_PATH_OPT_OUTPUT);
Michal Vasko3cb0b132017-01-03 14:59:51 +0100543 if (!data || lyd_validate(&data, LYD_OPT_RPCREPLY, NULL)) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100544 ERRINT;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200545 free(model_data);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100546 return NULL;
547 }
548
Radek Krejci36dfdb32016-09-01 16:56:35 +0200549 return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100550}
551
552static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100553nc_clb_default_close_session(struct lyd_node *UNUSED(rpc), struct nc_session *session)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100554{
Michal Vasko428087d2016-01-14 16:04:28 +0100555 session->term_reason = NC_SESSION_TERM_CLOSED;
556 return nc_server_reply_ok();
Michal Vasko05ba9df2016-01-13 14:40:27 +0100557}
558
Michal Vasko086311b2016-01-08 09:53:11 +0100559API int
560nc_server_init(struct ly_ctx *ctx)
561{
Michal Vasko88639e92017-08-03 14:38:10 +0200562 const struct lys_node *rpc;
Frank Rimpler9f838b02018-07-25 06:44:03 +0000563 pthread_rwlockattr_t attr;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100564
Michal Vasko086311b2016-01-08 09:53:11 +0100565 if (!ctx) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200566 ERRARG("ctx");
Michal Vasko086311b2016-01-08 09:53:11 +0100567 return -1;
568 }
569
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100570 nc_init();
571
Michal Vasko05ba9df2016-01-13 14:40:27 +0100572 /* set default <get-schema> callback if not specified */
Michal Vasko88639e92017-08-03 14:38:10 +0200573 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema", 0);
574 if (rpc && !rpc->priv) {
575 lys_set_private(rpc, nc_clb_default_get_schema);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100576 }
577
578 /* set default <close-session> callback if not specififed */
Michal Vasko88639e92017-08-03 14:38:10 +0200579 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf:close-session", 0);
580 if (rpc && !rpc->priv) {
581 lys_set_private(rpc, nc_clb_default_close_session);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100582 }
583
Michal Vasko086311b2016-01-08 09:53:11 +0100584 server_opts.ctx = ctx;
Michal Vaskob48aa812016-01-18 14:13:09 +0100585
586 server_opts.new_session_id = 1;
Andrew Langefeld6ed922d2018-09-12 14:08:32 -0500587 server_opts.new_client_id = 1;
Michal Vaskob48aa812016-01-18 14:13:09 +0100588
Frank Rimpler9f838b02018-07-25 06:44:03 +0000589 errno=0;
590
591 if (pthread_rwlockattr_init(&attr) == 0) {
Rosen Penevef2f3ac2019-07-15 18:15:28 -0700592#if defined(HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP)
Frank Rimpler9f838b02018-07-25 06:44:03 +0000593 if (pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP) == 0) {
594 if (pthread_rwlock_init(&server_opts.endpt_lock, &attr) != 0) {
595 ERR("%s: failed to init rwlock(%s).", __FUNCTION__, strerror(errno));
596 }
597 if (pthread_rwlock_init(&server_opts.ch_client_lock, &attr) != 0) {
598 ERR("%s: failed to init rwlock(%s).", __FUNCTION__, strerror(errno));
599 }
600 } else {
601 ERR("%s: failed set attribute (%s).", __FUNCTION__, strerror(errno));
602 }
Rosen Penevef2f3ac2019-07-15 18:15:28 -0700603#endif
Frank Rimpler9f838b02018-07-25 06:44:03 +0000604 pthread_rwlockattr_destroy(&attr);
605 } else {
606 ERR("%s: failed init attribute (%s).", __FUNCTION__, strerror(errno));
607 }
Michal Vasko086311b2016-01-08 09:53:11 +0100608 return 0;
609}
610
Michal Vaskob48aa812016-01-18 14:13:09 +0100611API void
612nc_server_destroy(void)
613{
Radek Krejci658782b2016-12-04 22:04:55 +0100614 unsigned int i;
615
616 for (i = 0; i < server_opts.capabilities_count; i++) {
617 lydict_remove(server_opts.ctx, server_opts.capabilities[i]);
618 }
619 free(server_opts.capabilities);
Michal Vaskodd6e4f72018-06-01 10:21:27 +0200620 server_opts.capabilities = NULL;
621 server_opts.capabilities_count = 0;
622
Radek Krejci53691be2016-02-22 13:58:37 +0100623#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko59050372016-11-22 14:33:55 +0100624 nc_server_del_endpt(NULL, 0);
Michal Vasko0bdf70b2019-06-24 19:20:20 +0200625 nc_server_ch_del_client(NULL);
Michal Vaskob48aa812016-01-18 14:13:09 +0100626#endif
Michal Vasko17dfda92016-12-01 14:06:16 +0100627#ifdef NC_ENABLED_SSH
Michal Vaskoebba7602018-03-23 13:14:08 +0100628 if (server_opts.passwd_auth_data && server_opts.passwd_auth_data_free) {
629 server_opts.passwd_auth_data_free(server_opts.passwd_auth_data);
630 }
Michal Vaskodd6e4f72018-06-01 10:21:27 +0200631 server_opts.passwd_auth_data = NULL;
632 server_opts.passwd_auth_data_free = NULL;
Michal Vaskoebba7602018-03-23 13:14:08 +0100633
Michal Vasko17dfda92016-12-01 14:06:16 +0100634 nc_server_ssh_del_authkey(NULL, NULL, 0, NULL);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100635
636 if (server_opts.hostkey_data && server_opts.hostkey_data_free) {
637 server_opts.hostkey_data_free(server_opts.hostkey_data);
638 }
Michal Vaskodd6e4f72018-06-01 10:21:27 +0200639 server_opts.hostkey_data = NULL;
640 server_opts.hostkey_data_free = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100641#endif
642#ifdef NC_ENABLED_TLS
643 if (server_opts.server_cert_data && server_opts.server_cert_data_free) {
644 server_opts.server_cert_data_free(server_opts.server_cert_data);
645 }
Michal Vaskodd6e4f72018-06-01 10:21:27 +0200646 server_opts.server_cert_data = NULL;
647 server_opts.server_cert_data_free = NULL;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100648 if (server_opts.trusted_cert_list_data && server_opts.trusted_cert_list_data_free) {
649 server_opts.trusted_cert_list_data_free(server_opts.trusted_cert_list_data);
650 }
Michal Vaskodd6e4f72018-06-01 10:21:27 +0200651 server_opts.trusted_cert_list_data = NULL;
652 server_opts.trusted_cert_list_data_free = NULL;
Michal Vaskob48aa812016-01-18 14:13:09 +0100653#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100654 nc_destroy();
Michal Vaskob48aa812016-01-18 14:13:09 +0100655}
656
Michal Vasko086311b2016-01-08 09:53:11 +0100657API int
658nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported)
659{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200660 if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)) {
661 ERRARG("basic_mode");
662 return -1;
663 } else if (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT))) {
664 ERRARG("also_supported");
Michal Vasko086311b2016-01-08 09:53:11 +0100665 return -1;
666 }
667
668 server_opts.wd_basic_mode = basic_mode;
669 server_opts.wd_also_supported = also_supported;
670 return 0;
671}
672
Michal Vasko1a38c862016-01-15 15:50:07 +0100673API void
Michal Vasko55f03972016-04-13 08:56:01 +0200674nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported)
675{
676 if (!basic_mode && !also_supported) {
677 ERRARG("basic_mode and also_supported");
678 return;
679 }
680
681 if (basic_mode) {
682 *basic_mode = server_opts.wd_basic_mode;
683 }
684 if (also_supported) {
685 *also_supported = server_opts.wd_also_supported;
686 }
687}
688
Michal Vasko55f03972016-04-13 08:56:01 +0200689API int
Radek Krejci658782b2016-12-04 22:04:55 +0100690nc_server_set_capability(const char *value)
Michal Vasko55f03972016-04-13 08:56:01 +0200691{
Radek Krejci658782b2016-12-04 22:04:55 +0100692 const char **new;
693
694 if (!value || !value[0]) {
695 ERRARG("value must not be empty");
696 return EXIT_FAILURE;
697 }
698
699 server_opts.capabilities_count++;
700 new = realloc(server_opts.capabilities, server_opts.capabilities_count * sizeof *server_opts.capabilities);
701 if (!new) {
702 ERRMEM;
703 return EXIT_FAILURE;
704 }
705 server_opts.capabilities = new;
706 server_opts.capabilities[server_opts.capabilities_count - 1] = lydict_insert(server_opts.ctx, value, 0);
707
708 return EXIT_SUCCESS;
Michal Vasko55f03972016-04-13 08:56:01 +0200709}
710
Michal Vasko1a38c862016-01-15 15:50:07 +0100711API void
Michal Vasko086311b2016-01-08 09:53:11 +0100712nc_server_set_hello_timeout(uint16_t hello_timeout)
713{
Michal Vasko086311b2016-01-08 09:53:11 +0100714 server_opts.hello_timeout = hello_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100715}
716
Michal Vasko55f03972016-04-13 08:56:01 +0200717API uint16_t
718nc_server_get_hello_timeout(void)
719{
720 return server_opts.hello_timeout;
721}
722
Michal Vasko1a38c862016-01-15 15:50:07 +0100723API void
Michal Vasko086311b2016-01-08 09:53:11 +0100724nc_server_set_idle_timeout(uint16_t idle_timeout)
725{
Michal Vasko086311b2016-01-08 09:53:11 +0100726 server_opts.idle_timeout = idle_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100727}
728
Michal Vasko55f03972016-04-13 08:56:01 +0200729API uint16_t
730nc_server_get_idle_timeout(void)
731{
732 return server_opts.idle_timeout;
733}
734
Michal Vasko71090fc2016-05-24 16:37:28 +0200735API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +0100736nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +0100737{
Michal Vasko71090fc2016-05-24 16:37:28 +0200738 NC_MSG_TYPE msgtype;
Michal Vasko9fb42272017-10-05 13:50:05 +0200739 struct timespec ts_cur;
Michal Vasko71090fc2016-05-24 16:37:28 +0200740
Michal Vasko45e53ae2016-04-07 11:46:03 +0200741 if (!server_opts.ctx) {
742 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +0200743 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200744 } else if (fdin < 0) {
745 ERRARG("fdin");
Michal Vasko71090fc2016-05-24 16:37:28 +0200746 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200747 } else if (fdout < 0) {
748 ERRARG("fdout");
Michal Vasko71090fc2016-05-24 16:37:28 +0200749 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200750 } else if (!username) {
751 ERRARG("username");
Michal Vasko71090fc2016-05-24 16:37:28 +0200752 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200753 } else if (!session) {
754 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200755 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100756 }
757
758 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +0200759 *session = nc_new_session(NC_SERVER, 0);
Michal Vasko1a38c862016-01-15 15:50:07 +0100760 if (!(*session)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100761 ERRMEM;
Michal Vasko71090fc2016-05-24 16:37:28 +0200762 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100763 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100764 (*session)->status = NC_STATUS_STARTING;
Michal Vaskoade892d2017-02-22 13:40:35 +0100765
Michal Vasko086311b2016-01-08 09:53:11 +0100766 /* transport specific data */
Michal Vasko1a38c862016-01-15 15:50:07 +0100767 (*session)->ti_type = NC_TI_FD;
768 (*session)->ti.fd.in = fdin;
769 (*session)->ti.fd.out = fdout;
Michal Vasko086311b2016-01-08 09:53:11 +0100770
771 /* assign context (dicionary needed for handshake) */
Michal Vasko1a38c862016-01-15 15:50:07 +0100772 (*session)->flags = NC_SESSION_SHAREDCTX;
773 (*session)->ctx = server_opts.ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100774
Michal Vaskob48aa812016-01-18 14:13:09 +0100775 /* assign new SID atomically */
Michal Vasko69a3ff62018-11-09 09:31:48 +0100776 (*session)->id = ATOMIC_INC(&server_opts.new_session_id);
Michal Vaskob48aa812016-01-18 14:13:09 +0100777
Michal Vasko086311b2016-01-08 09:53:11 +0100778 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +0200779 msgtype = nc_handshake_io(*session);
Michal Vasko71090fc2016-05-24 16:37:28 +0200780 if (msgtype != NC_MSG_HELLO) {
781 nc_session_free(*session, NULL);
782 *session = NULL;
783 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100784 }
Michal Vasko9fb42272017-10-05 13:50:05 +0200785
786 nc_gettimespec_mono(&ts_cur);
787 (*session)->opts.server.last_rpc = ts_cur.tv_sec;
788 nc_gettimespec_real(&ts_cur);
789 (*session)->opts.server.session_start = ts_cur.tv_sec;
790
Michal Vasko1a38c862016-01-15 15:50:07 +0100791 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko086311b2016-01-08 09:53:11 +0100792
Michal Vasko71090fc2016-05-24 16:37:28 +0200793 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100794}
Michal Vasko9e036d52016-01-08 10:49:26 +0100795
Michal Vaskob30b99c2016-07-26 11:35:43 +0200796static void
Michal Vasko74c345f2018-02-07 10:37:11 +0100797nc_ps_queue_add_id(struct nc_pollsession *ps, uint8_t *id)
798{
799 uint8_t q_last;
800
801 if (ps->queue_len == NC_PS_QUEUE_SIZE) {
802 ERRINT;
803 return;
804 }
805
806 /* get a unique queue value (by adding 1 to the last added value, if any) */
807 if (ps->queue_len) {
808 q_last = (ps->queue_begin + ps->queue_len - 1) % NC_PS_QUEUE_SIZE;
809 *id = ps->queue[q_last] + 1;
810 } else {
811 *id = 0;
812 }
813
814 /* add the id into the queue */
815 ++ps->queue_len;
816 q_last = (ps->queue_begin + ps->queue_len - 1) % NC_PS_QUEUE_SIZE;
817 ps->queue[q_last] = *id;
818}
819
820static void
Michal Vaskob30b99c2016-07-26 11:35:43 +0200821nc_ps_queue_remove_id(struct nc_pollsession *ps, uint8_t id)
822{
Michal Vasko74c345f2018-02-07 10:37:11 +0100823 uint8_t i, q_idx, found = 0;
Michal Vaskob30b99c2016-07-26 11:35:43 +0200824
825 for (i = 0; i < ps->queue_len; ++i) {
Michal Vasko74c345f2018-02-07 10:37:11 +0100826 /* get the actual queue idx */
827 q_idx = (ps->queue_begin + i) % NC_PS_QUEUE_SIZE;
Michal Vaskob30b99c2016-07-26 11:35:43 +0200828
829 if (found) {
Michal Vasko74c345f2018-02-07 10:37:11 +0100830 if (ps->queue[q_idx] == id) {
Michal Vaskob30b99c2016-07-26 11:35:43 +0200831 /* another equal value, simply cannot be */
832 ERRINT;
833 }
Michal Vaskod8340032018-02-12 14:41:00 +0100834 if (found == 2) {
835 /* move the following values */
836 ps->queue[q_idx ? q_idx - 1 : NC_PS_QUEUE_SIZE - 1] = ps->queue[q_idx];
837 }
Michal Vasko74c345f2018-02-07 10:37:11 +0100838 } else if (ps->queue[q_idx] == id) {
Michal Vaskob30b99c2016-07-26 11:35:43 +0200839 /* found our id, there can be no more equal valid values */
Michal Vaskod8340032018-02-12 14:41:00 +0100840 if (i == 0) {
841 found = 1;
842 } else {
843 /* this is not okay, our id is in the middle of the queue */
844 found = 2;
845 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200846 }
847 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200848 if (!found) {
849 ERRINT;
Michal Vasko103fe632018-02-12 16:37:45 +0100850 return;
Michal Vaskob30b99c2016-07-26 11:35:43 +0200851 }
Michal Vasko74c345f2018-02-07 10:37:11 +0100852
Michal Vasko103fe632018-02-12 16:37:45 +0100853 --ps->queue_len;
Michal Vaskod8340032018-02-12 14:41:00 +0100854 if (found == 1) {
Michal Vasko103fe632018-02-12 16:37:45 +0100855 /* remove the id by moving the queue, otherwise all the values in the queue were moved */
Michal Vaskod8340032018-02-12 14:41:00 +0100856 ps->queue_begin = (ps->queue_begin + 1) % NC_PS_QUEUE_SIZE;
857 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200858}
859
Michal Vaskof04a52a2016-04-07 10:52:10 +0200860int
Michal Vasko26043172016-07-26 14:08:59 +0200861nc_ps_lock(struct nc_pollsession *ps, uint8_t *id, const char *func)
Michal Vaskobe86fe32016-04-07 10:43:03 +0200862{
863 int ret;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200864 struct timespec ts;
865
Michal Vasko77a6abe2017-10-05 10:02:20 +0200866 nc_gettimespec_real(&ts);
Michal Vasko81c5b302017-03-15 12:10:40 +0100867 nc_addtimespec(&ts, NC_PS_LOCK_TIMEOUT);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200868
869 /* LOCK */
870 ret = pthread_mutex_timedlock(&ps->lock, &ts);
871 if (ret) {
Michal Vasko26043172016-07-26 14:08:59 +0200872 ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200873 return -1;
874 }
875
Michal Vasko74c345f2018-02-07 10:37:11 +0100876 /* check that the queue is long enough */
Michal Vasko8bc747c2018-02-09 16:37:04 +0100877 if (ps->queue_len == NC_PS_QUEUE_SIZE) {
Michal Vasko74c345f2018-02-07 10:37:11 +0100878 ERR("%s: pollsession queue size (%d) too small.", func, NC_PS_QUEUE_SIZE);
Michal Vasko8bc747c2018-02-09 16:37:04 +0100879 pthread_mutex_unlock(&ps->lock);
880 return -1;
881 }
Michal Vasko74c345f2018-02-07 10:37:11 +0100882
883 /* add ourselves into the queue */
884 nc_ps_queue_add_id(ps, id);
Michal Vasko2733aad2020-04-16 09:01:52 +0200885 DBL("PS 0x%p TID %lu queue: added %u, head %u, lenght %u", ps, (long unsigned int)pthread_self(), *id,
Michal Vasko91290952019-09-27 11:30:55 +0200886 ps->queue[ps->queue_begin], ps->queue_len);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200887
888 /* is it our turn? */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200889 while (ps->queue[ps->queue_begin] != *id) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200890 nc_gettimespec_real(&ts);
Michal Vasko2b768092018-02-12 16:37:12 +0100891 nc_addtimespec(&ts, NC_PS_QUEUE_TIMEOUT);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200892
893 ret = pthread_cond_timedwait(&ps->cond, &ps->lock, &ts);
894 if (ret) {
preetbhansalif3edf492018-12-14 17:53:32 +0530895 /**
896 * This may happen when another thread releases the lock and broadcasts the condition
897 * and this thread had already timed out. When this thread is scheduled, it returns timed out error
898 * but when actually this thread was ready for condition.
899 */
preetbhansali629dfc42018-12-17 16:04:40 +0530900 if ((ETIMEDOUT == ret) && (ps->queue[ps->queue_begin] == *id)) {
preetbhansalif3edf492018-12-14 17:53:32 +0530901 break;
902 }
Michal Vasko66032bc2019-01-22 15:03:12 +0100903
Michal Vasko26043172016-07-26 14:08:59 +0200904 ERR("%s: failed to wait for a pollsession condition (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200905 /* remove ourselves from the queue */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200906 nc_ps_queue_remove_id(ps, *id);
907 pthread_mutex_unlock(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200908 return -1;
909 }
910 }
911
Michal Vaskobe86fe32016-04-07 10:43:03 +0200912 /* UNLOCK */
913 pthread_mutex_unlock(&ps->lock);
914
915 return 0;
916}
917
Michal Vaskof04a52a2016-04-07 10:52:10 +0200918int
Michal Vasko26043172016-07-26 14:08:59 +0200919nc_ps_unlock(struct nc_pollsession *ps, uint8_t id, const char *func)
Michal Vaskobe86fe32016-04-07 10:43:03 +0200920{
921 int ret;
922 struct timespec ts;
923
Michal Vasko77a6abe2017-10-05 10:02:20 +0200924 nc_gettimespec_real(&ts);
Michal Vasko81c5b302017-03-15 12:10:40 +0100925 nc_addtimespec(&ts, NC_PS_LOCK_TIMEOUT);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200926
927 /* LOCK */
928 ret = pthread_mutex_timedlock(&ps->lock, &ts);
929 if (ret) {
Michal Vasko26043172016-07-26 14:08:59 +0200930 ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200931 ret = -1;
932 }
933
Michal Vaskob30b99c2016-07-26 11:35:43 +0200934 /* we must be the first, it was our turn after all, right? */
935 if (ps->queue[ps->queue_begin] != id) {
936 ERRINT;
Michal Vaskob1a094b2016-10-05 14:04:52 +0200937 /* UNLOCK */
938 if (!ret) {
939 pthread_mutex_unlock(&ps->lock);
940 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200941 return -1;
942 }
943
Michal Vaskobe86fe32016-04-07 10:43:03 +0200944 /* remove ourselves from the queue */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200945 nc_ps_queue_remove_id(ps, id);
Michal Vasko2733aad2020-04-16 09:01:52 +0200946 DBL("PS 0x%p TID %lu queue: removed %u, head %u, lenght %u", ps, (long unsigned int)pthread_self(), id,
Michal Vasko91290952019-09-27 11:30:55 +0200947 ps->queue[ps->queue_begin], ps->queue_len);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200948
949 /* broadcast to all other threads that the queue moved */
950 pthread_cond_broadcast(&ps->cond);
951
Michal Vaskobe86fe32016-04-07 10:43:03 +0200952 /* UNLOCK */
953 if (!ret) {
954 pthread_mutex_unlock(&ps->lock);
955 }
956
957 return ret;
958}
959
Michal Vasko428087d2016-01-14 16:04:28 +0100960API struct nc_pollsession *
961nc_ps_new(void)
962{
Michal Vasko48a63ed2016-03-01 09:48:21 +0100963 struct nc_pollsession *ps;
964
965 ps = calloc(1, sizeof(struct nc_pollsession));
Michal Vasko4eb3c312016-03-01 14:09:37 +0100966 if (!ps) {
967 ERRMEM;
968 return NULL;
969 }
Michal Vaskobe86fe32016-04-07 10:43:03 +0200970 pthread_cond_init(&ps->cond, NULL);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100971 pthread_mutex_init(&ps->lock, NULL);
972
973 return ps;
Michal Vasko428087d2016-01-14 16:04:28 +0100974}
975
976API void
977nc_ps_free(struct nc_pollsession *ps)
978{
fanchanghu3d4e7212017-08-09 09:42:30 +0800979 uint16_t i;
980
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100981 if (!ps) {
982 return;
983 }
984
Michal Vaskobe86fe32016-04-07 10:43:03 +0200985 if (ps->queue_len) {
986 ERR("FATAL: Freeing a pollsession structure that is currently being worked with!");
987 }
988
fanchanghu3d4e7212017-08-09 09:42:30 +0800989 for (i = 0; i < ps->session_count; i++) {
990 free(ps->sessions[i]);
991 }
992
Michal Vasko428087d2016-01-14 16:04:28 +0100993 free(ps->sessions);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100994 pthread_mutex_destroy(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200995 pthread_cond_destroy(&ps->cond);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100996
Michal Vasko428087d2016-01-14 16:04:28 +0100997 free(ps);
998}
999
1000API int
1001nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session)
1002{
Michal Vaskob30b99c2016-07-26 11:35:43 +02001003 uint8_t q_id;
1004
Michal Vasko45e53ae2016-04-07 11:46:03 +02001005 if (!ps) {
1006 ERRARG("ps");
1007 return -1;
1008 } else if (!session) {
1009 ERRARG("session");
Michal Vasko428087d2016-01-14 16:04:28 +01001010 return -1;
1011 }
1012
Michal Vasko48a63ed2016-03-01 09:48:21 +01001013 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001014 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +02001015 return -1;
1016 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001017
Michal Vasko428087d2016-01-14 16:04:28 +01001018 ++ps->session_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001019 ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions);
Michal Vasko9a327362017-01-11 11:31:46 +01001020 if (!ps->sessions) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001021 ERRMEM;
1022 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001023 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001024 return -1;
1025 }
fanchanghu3d4e7212017-08-09 09:42:30 +08001026 ps->sessions[ps->session_count - 1] = calloc(1, sizeof **ps->sessions);
1027 if (!ps->sessions[ps->session_count - 1]) {
1028 ERRMEM;
1029 --ps->session_count;
1030 /* UNLOCK */
1031 nc_ps_unlock(ps, q_id, __func__);
1032 return -1;
1033 }
1034 ps->sessions[ps->session_count - 1]->session = session;
1035 ps->sessions[ps->session_count - 1]->state = NC_PS_STATE_NONE;
Michal Vasko428087d2016-01-14 16:04:28 +01001036
Michal Vasko48a63ed2016-03-01 09:48:21 +01001037 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001038 return nc_ps_unlock(ps, q_id, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +01001039}
1040
Michal Vasko48a63ed2016-03-01 09:48:21 +01001041static int
Radek Krejcid5f978f2016-03-03 13:14:45 +01001042_nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session, int index)
Michal Vasko428087d2016-01-14 16:04:28 +01001043{
1044 uint16_t i;
1045
Radek Krejcid5f978f2016-03-03 13:14:45 +01001046 if (index >= 0) {
1047 i = (uint16_t)index;
1048 goto remove;
1049 }
Michal Vasko428087d2016-01-14 16:04:28 +01001050 for (i = 0; i < ps->session_count; ++i) {
fanchanghu3d4e7212017-08-09 09:42:30 +08001051 if (ps->sessions[i]->session == session) {
Radek Krejcid5f978f2016-03-03 13:14:45 +01001052remove:
Michal Vasko428087d2016-01-14 16:04:28 +01001053 --ps->session_count;
fanchanghu3d4e7212017-08-09 09:42:30 +08001054 if (i <= ps->session_count) {
1055 free(ps->sessions[i]);
Michal Vasko58005732016-02-02 15:50:52 +01001056 ps->sessions[i] = ps->sessions[ps->session_count];
fanchanghu3d4e7212017-08-09 09:42:30 +08001057 }
1058 if (!ps->session_count) {
Michal Vasko58005732016-02-02 15:50:52 +01001059 free(ps->sessions);
1060 ps->sessions = NULL;
Michal Vasko58005732016-02-02 15:50:52 +01001061 }
Michal Vasko11d2f6a2017-02-02 11:15:06 +01001062 ps->last_event_session = 0;
Michal Vasko428087d2016-01-14 16:04:28 +01001063 return 0;
1064 }
1065 }
1066
Michal Vaskof0537d82016-01-29 14:42:38 +01001067 return -1;
Michal Vasko428087d2016-01-14 16:04:28 +01001068}
1069
Michal Vasko48a63ed2016-03-01 09:48:21 +01001070API int
1071nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session)
1072{
Michal Vaskob30b99c2016-07-26 11:35:43 +02001073 uint8_t q_id;
Michal Vaskobe86fe32016-04-07 10:43:03 +02001074 int ret, ret2;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001075
Michal Vasko45e53ae2016-04-07 11:46:03 +02001076 if (!ps) {
1077 ERRARG("ps");
1078 return -1;
1079 } else if (!session) {
1080 ERRARG("session");
Michal Vasko48a63ed2016-03-01 09:48:21 +01001081 return -1;
1082 }
1083
1084 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001085 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +02001086 return -1;
1087 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001088
Radek Krejcid5f978f2016-03-03 13:14:45 +01001089 ret = _nc_ps_del_session(ps, session, -1);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001090
1091 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001092 ret2 = nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001093
Michal Vaskobe86fe32016-04-07 10:43:03 +02001094 return (ret || ret2 ? -1 : 0);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001095}
1096
Michal Vaskoe1ee05b2017-03-21 10:10:18 +01001097API struct nc_session *
Michal Vasko4871c9d2017-10-09 14:48:39 +02001098nc_ps_get_session(const struct nc_pollsession *ps, uint16_t idx)
Michal Vaskoe1ee05b2017-03-21 10:10:18 +01001099{
1100 uint8_t q_id;
Michal Vaskoe1ee05b2017-03-21 10:10:18 +01001101 struct nc_session *ret = NULL;
1102
1103 if (!ps) {
1104 ERRARG("ps");
1105 return NULL;
1106 }
1107
1108 /* LOCK */
1109 if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) {
1110 return NULL;
1111 }
1112
Michal Vasko4871c9d2017-10-09 14:48:39 +02001113 if (idx < ps->session_count) {
1114 ret = ps->sessions[idx]->session;
Michal Vaskoe1ee05b2017-03-21 10:10:18 +01001115 }
1116
1117 /* UNLOCK */
1118 nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__);
1119
1120 return ret;
1121}
1122
Michal Vasko0fdb7ac2016-03-01 09:03:12 +01001123API uint16_t
1124nc_ps_session_count(struct nc_pollsession *ps)
1125{
Michal Vasko47003942019-03-14 12:25:23 +01001126 uint8_t q_id;
1127 uint16_t session_count;
1128
Michal Vasko0fdb7ac2016-03-01 09:03:12 +01001129 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001130 ERRARG("ps");
Michal Vasko0fdb7ac2016-03-01 09:03:12 +01001131 return 0;
1132 }
1133
Michal Vasko47003942019-03-14 12:25:23 +01001134 /* LOCK (just for memory barrier so that we read the current value) */
1135 if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) {
1136 return 0;
1137 }
1138
1139 session_count = ps->session_count;
1140
1141 /* UNLOCK */
1142 nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__);
1143
1144 return session_count;
Michal Vasko0fdb7ac2016-03-01 09:03:12 +01001145}
1146
Michal Vasko131120a2018-05-29 15:44:02 +02001147/* should be called holding the session RPC lock! IO lock will be acquired as needed
Michal Vasko71090fc2016-05-24 16:37:28 +02001148 * returns: NC_PSPOLL_ERROR,
1149 * NC_PSPOLL_BAD_RPC,
1150 * NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR,
1151 * NC_PSPOLL_RPC
1152 */
1153static int
Michal Vasko131120a2018-05-29 15:44:02 +02001154nc_server_recv_rpc_io(struct nc_session *session, int io_timeout, struct nc_server_rpc **rpc)
Michal Vasko428087d2016-01-14 16:04:28 +01001155{
1156 struct lyxml_elem *xml = NULL;
1157 NC_MSG_TYPE msgtype;
Radek Krejcif93c7d42016-04-06 13:41:15 +02001158 struct nc_server_reply *reply = NULL;
Radek Krejcif93c7d42016-04-06 13:41:15 +02001159 int ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001160
Michal Vasko45e53ae2016-04-07 11:46:03 +02001161 if (!session) {
1162 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001163 return NC_PSPOLL_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001164 } else if (!rpc) {
1165 ERRARG("rpc");
Michal Vasko71090fc2016-05-24 16:37:28 +02001166 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001167 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001168 ERR("Session %u: invalid session to receive RPCs.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001169 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001170 }
1171
Michal Vasko131120a2018-05-29 15:44:02 +02001172 msgtype = nc_read_msg_io(session, io_timeout, &xml, 0);
Michal Vasko428087d2016-01-14 16:04:28 +01001173
1174 switch (msgtype) {
1175 case NC_MSG_RPC:
Radek Krejcif93c7d42016-04-06 13:41:15 +02001176 *rpc = calloc(1, sizeof **rpc);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001177 if (!*rpc) {
1178 ERRMEM;
1179 goto error;
1180 }
Michal Vaskoca4a2422016-02-02 12:17:14 +01001181
Radek Krejcif93c7d42016-04-06 13:41:15 +02001182 ly_errno = LY_SUCCESS;
Michal Vasko41adf392017-01-17 10:39:04 +01001183 (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child,
1184 LYD_OPT_RPC | LYD_OPT_DESTRUCT | LYD_OPT_NOEXTDEPS | LYD_OPT_STRICT, NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +01001185 if (!(*rpc)->tree) {
Radek Krejcif93c7d42016-04-06 13:41:15 +02001186 /* parsing RPC failed */
Michal Vaskoc9970242018-02-14 16:03:35 +01001187 reply = nc_server_reply_err(nc_err_libyang(server_opts.ctx));
Michal Vasko131120a2018-05-29 15:44:02 +02001188 ret = nc_write_msg_io(session, io_timeout, NC_MSG_REPLY, xml, reply);
Radek Krejcif93c7d42016-04-06 13:41:15 +02001189 nc_server_reply_free(reply);
Michal Vaskod057b272020-02-07 10:49:05 +01001190 if (ret != NC_MSG_REPLY) {
Michal Vasko8fe604c2020-02-10 15:25:04 +01001191 ERR("Session %u: failed to write reply (%s).", session->id, nc_msgtype2str[ret]);
Radek Krejcif93c7d42016-04-06 13:41:15 +02001192 }
Michal Vasko71090fc2016-05-24 16:37:28 +02001193 ret = NC_PSPOLL_REPLY_ERROR | NC_PSPOLL_BAD_RPC;
1194 } else {
1195 ret = NC_PSPOLL_RPC;
Michal Vaskoca4a2422016-02-02 12:17:14 +01001196 }
Michal Vasko428087d2016-01-14 16:04:28 +01001197 (*rpc)->root = xml;
1198 break;
1199 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +01001200 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001201 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +01001202 goto error;
1203 case NC_MSG_REPLY:
Michal Vasko81614ee2016-02-02 12:20:14 +01001204 ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001205 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +01001206 goto error;
1207 case NC_MSG_NOTIF:
Michal Vasko81614ee2016-02-02 12:20:14 +01001208 ERR("Session %u: received <notification> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +02001209 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +01001210 goto error;
1211 default:
Michal Vasko71090fc2016-05-24 16:37:28 +02001212 /* NC_MSG_ERROR,
Michal Vasko131120a2018-05-29 15:44:02 +02001213 * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg_io()
Michal Vasko428087d2016-01-14 16:04:28 +01001214 */
Michal Vasko71090fc2016-05-24 16:37:28 +02001215 ret = NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001216 break;
1217 }
1218
Michal Vasko71090fc2016-05-24 16:37:28 +02001219 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001220
1221error:
1222 /* cleanup */
1223 lyxml_free(server_opts.ctx, xml);
1224
Michal Vasko71090fc2016-05-24 16:37:28 +02001225 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001226}
1227
fanchanghu966f2de2016-07-21 02:28:57 -04001228API void
1229nc_set_global_rpc_clb(nc_rpc_clb clb)
1230{
1231 global_rpc_clb = clb;
1232}
1233
Radek Krejci93e80222016-10-03 13:34:25 +02001234API NC_MSG_TYPE
1235nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout)
1236{
Michal Vasko131120a2018-05-29 15:44:02 +02001237 NC_MSG_TYPE ret;
Radek Krejci93e80222016-10-03 13:34:25 +02001238
1239 /* check parameters */
Michal Vasko3486a7c2017-03-03 13:28:07 +01001240 if (!session || (session->side != NC_SERVER) || !session->opts.server.ntf_status) {
Radek Krejci93e80222016-10-03 13:34:25 +02001241 ERRARG("session");
1242 return NC_MSG_ERROR;
1243 } else if (!notif || !notif->tree || !notif->eventtime) {
1244 ERRARG("notif");
1245 return NC_MSG_ERROR;
1246 }
1247
Michal Vasko131120a2018-05-29 15:44:02 +02001248 /* we do not need RPC lock for this, IO lock will be acquired properly */
1249 ret = nc_write_msg_io(session, timeout, NC_MSG_NOTIF, notif);
Michal Vasko8fe604c2020-02-10 15:25:04 +01001250 if (ret != NC_MSG_NOTIF) {
1251 ERR("Session %u: failed to write notification (%s).", session->id, nc_msgtype2str[ret]);
Radek Krejci93e80222016-10-03 13:34:25 +02001252 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001253
Michal Vasko131120a2018-05-29 15:44:02 +02001254 return ret;
Radek Krejci93e80222016-10-03 13:34:25 +02001255}
1256
Michal Vasko131120a2018-05-29 15:44:02 +02001257/* must be called holding the session RPC lock! IO lock will be acquired as needed
Michal Vasko71090fc2016-05-24 16:37:28 +02001258 * returns: NC_PSPOLL_ERROR,
1259 * NC_PSPOLL_ERROR | NC_PSPOLL_REPLY_ERROR,
1260 * NC_PSPOLL_REPLY_ERROR,
1261 * 0
1262 */
1263static int
Michal Vasko131120a2018-05-29 15:44:02 +02001264nc_server_send_reply_io(struct nc_session *session, int io_timeout, struct nc_server_rpc *rpc)
Michal Vasko428087d2016-01-14 16:04:28 +01001265{
1266 nc_rpc_clb clb;
1267 struct nc_server_reply *reply;
Michal Vasko90e8e692016-07-13 12:27:57 +02001268 struct lys_node *rpc_act = NULL;
1269 struct lyd_node *next, *elem;
Michal Vasko131120a2018-05-29 15:44:02 +02001270 int ret = 0;
1271 NC_MSG_TYPE r;
Michal Vasko428087d2016-01-14 16:04:28 +01001272
Michal Vasko4a827e52016-03-03 10:59:00 +01001273 if (!rpc) {
1274 ERRINT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001275 return NC_PSPOLL_ERROR;
Michal Vasko4a827e52016-03-03 10:59:00 +01001276 }
1277
Michal Vasko90e8e692016-07-13 12:27:57 +02001278 if (rpc->tree->schema->nodetype == LYS_RPC) {
1279 /* RPC */
1280 rpc_act = rpc->tree->schema;
fanchanghu966f2de2016-07-21 02:28:57 -04001281 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +02001282 /* action */
1283 LY_TREE_DFS_BEGIN(rpc->tree, next, elem) {
1284 if (elem->schema->nodetype == LYS_ACTION) {
1285 rpc_act = elem->schema;
1286 break;
1287 }
1288 LY_TREE_DFS_END(rpc->tree, next, elem);
fanchanghu966f2de2016-07-21 02:28:57 -04001289 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001290 if (!rpc_act) {
1291 ERRINT;
1292 return NC_PSPOLL_ERROR;
1293 }
1294 }
1295
1296 if (!rpc_act->priv) {
Petr A. Sokolov16284ca2019-02-04 09:02:40 +03001297 if (!global_rpc_clb) {
1298 /* no callback, reply with a not-implemented error */
1299 reply = nc_server_reply_err(nc_err(NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT));
1300 } else {
1301 reply = global_rpc_clb(rpc->tree, session);
1302 }
Michal Vasko428087d2016-01-14 16:04:28 +01001303 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +02001304 clb = (nc_rpc_clb)rpc_act->priv;
Michal Vasko428087d2016-01-14 16:04:28 +01001305 reply = clb(rpc->tree, session);
1306 }
1307
1308 if (!reply) {
Michal Vasko1a38c862016-01-15 15:50:07 +01001309 reply = nc_server_reply_err(nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP));
Michal Vasko428087d2016-01-14 16:04:28 +01001310 }
Michal Vasko131120a2018-05-29 15:44:02 +02001311 r = nc_write_msg_io(session, io_timeout, NC_MSG_REPLY, rpc->root, reply);
Michal Vasko71090fc2016-05-24 16:37:28 +02001312 if (reply->type == NC_RPL_ERROR) {
1313 ret |= NC_PSPOLL_REPLY_ERROR;
1314 }
1315 nc_server_reply_free(reply);
Michal Vasko428087d2016-01-14 16:04:28 +01001316
Michal Vasko131120a2018-05-29 15:44:02 +02001317 if (r != NC_MSG_REPLY) {
Michal Vasko8fe604c2020-02-10 15:25:04 +01001318 ERR("Session %u: failed to write reply (%s).", session->id, nc_msgtype2str[r]);
Michal Vasko71090fc2016-05-24 16:37:28 +02001319 ret |= NC_PSPOLL_ERROR;
1320 }
Michal Vasko428087d2016-01-14 16:04:28 +01001321
1322 /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */
1323 if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) {
1324 session->status = NC_STATUS_INVALID;
1325 }
1326
Michal Vasko71090fc2016-05-24 16:37:28 +02001327 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001328}
1329
Michal Vasko131120a2018-05-29 15:44:02 +02001330/* session must be running and session RPC lock held!
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001331 * returns: NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR, (msg filled)
1332 * NC_PSPOLL_ERROR, (msg filled)
1333 * NC_PSPOLL_TIMEOUT,
1334 * NC_PSPOLL_RPC (some application data available),
1335 * NC_PSPOLL_SSH_CHANNEL,
1336 * NC_PSPOLL_SSH_MSG
1337 */
1338static int
Michal Vasko131120a2018-05-29 15:44:02 +02001339nc_ps_poll_session_io(struct nc_session *session, int io_timeout, time_t now_mono, char *msg)
Michal Vasko428087d2016-01-14 16:04:28 +01001340{
Michal Vasko9a327362017-01-11 11:31:46 +01001341 struct pollfd pfd;
Michal Vasko2260f552018-06-06 10:06:12 +02001342 int r, ret = 0;
Michal Vasko9a327362017-01-11 11:31:46 +01001343#ifdef NC_ENABLED_SSH
1344 struct nc_session *new;
1345#endif
Michal Vasko428087d2016-01-14 16:04:28 +01001346
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001347 /* check timeout first */
1348 if (!(session->flags & NC_SESSION_CALLHOME) && !session->opts.server.ntf_status && server_opts.idle_timeout
Michal Vasko9fb42272017-10-05 13:50:05 +02001349 && (now_mono >= session->opts.server.last_rpc + server_opts.idle_timeout)) {
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001350 sprintf(msg, "session idle timeout elapsed");
1351 session->status = NC_STATUS_INVALID;
1352 session->term_reason = NC_SESSION_TERM_TIMEOUT;
1353 return NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1354 }
1355
Michal Vasko131120a2018-05-29 15:44:02 +02001356 r = nc_session_io_lock(session, io_timeout, __func__);
1357 if (r < 0) {
1358 sprintf(msg, "session IO lock failed to be acquired");
1359 return NC_PSPOLL_ERROR;
1360 } else if (!r) {
1361 return NC_PSPOLL_TIMEOUT;
1362 }
1363
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001364 switch (session->ti_type) {
1365#ifdef NC_ENABLED_SSH
1366 case NC_TI_LIBSSH:
1367 r = ssh_channel_poll_timeout(session->ti.libssh.channel, 0, 0);
Michal Vasko8dcaa882017-10-19 14:28:42 +02001368 if (r == SSH_EOF) {
1369 sprintf(msg, "SSH channel unexpected EOF");
1370 session->status = NC_STATUS_INVALID;
1371 session->term_reason = NC_SESSION_TERM_DROPPED;
1372 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1373 } else if (r == SSH_ERROR) {
1374 sprintf(msg, "SSH channel poll error (%s)", ssh_get_error(session->ti.libssh.session));
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001375 session->status = NC_STATUS_INVALID;
1376 session->term_reason = NC_SESSION_TERM_OTHER;
1377 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
Michal Vasko8dcaa882017-10-19 14:28:42 +02001378 } else if (!r) {
1379 if (session->flags & NC_SESSION_SSH_NEW_MSG) {
1380 /* new SSH message */
1381 session->flags &= ~NC_SESSION_SSH_NEW_MSG;
1382 if (session->ti.libssh.next) {
1383 for (new = session->ti.libssh.next; new != session; new = new->ti.libssh.next) {
1384 if ((new->status == NC_STATUS_STARTING) && new->ti.libssh.channel
1385 && (new->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
1386 /* new NETCONF SSH channel */
1387 ret = NC_PSPOLL_SSH_CHANNEL;
1388 break;
1389 }
1390 }
1391 if (new != session) {
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001392 break;
1393 }
1394 }
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001395
Michal Vasko8dcaa882017-10-19 14:28:42 +02001396 /* just some SSH message */
1397 ret = NC_PSPOLL_SSH_MSG;
1398 } else {
1399 ret = NC_PSPOLL_TIMEOUT;
1400 }
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001401 } else {
1402 /* we have some application data */
1403 ret = NC_PSPOLL_RPC;
1404 }
1405 break;
1406#endif
1407#ifdef NC_ENABLED_TLS
1408 case NC_TI_OPENSSL:
1409 r = SSL_pending(session->ti.tls);
1410 if (!r) {
1411 /* no data pending in the SSL buffer, poll fd */
1412 pfd.fd = SSL_get_rfd(session->ti.tls);
1413 if (pfd.fd < 0) {
1414 sprintf(msg, "internal error (%s:%d)", __FILE__, __LINE__);
1415 ret = NC_PSPOLL_ERROR;
1416 break;
1417 }
1418 pfd.events = POLLIN;
1419 pfd.revents = 0;
1420 r = poll(&pfd, 1, 0);
1421
1422 if ((r < 0) && (errno != EINTR)) {
1423 sprintf(msg, "poll failed (%s)", strerror(errno));
1424 session->status = NC_STATUS_INVALID;
1425 ret = NC_PSPOLL_ERROR;
1426 } else if (r > 0) {
1427 if (pfd.revents & (POLLHUP | POLLNVAL)) {
1428 sprintf(msg, "communication socket unexpectedly closed");
1429 session->status = NC_STATUS_INVALID;
1430 session->term_reason = NC_SESSION_TERM_DROPPED;
1431 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1432 } else if (pfd.revents & POLLERR) {
1433 sprintf(msg, "communication socket error");
1434 session->status = NC_STATUS_INVALID;
1435 session->term_reason = NC_SESSION_TERM_OTHER;
1436 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1437 } else {
1438 ret = NC_PSPOLL_RPC;
1439 }
1440 } else {
1441 ret = NC_PSPOLL_TIMEOUT;
1442 }
1443 } else {
1444 ret = NC_PSPOLL_RPC;
1445 }
1446 break;
1447#endif
1448 case NC_TI_FD:
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001449 case NC_TI_UNIX:
1450 pfd.fd = (session->ti_type == NC_TI_FD) ? session->ti.fd.in : session->ti.unixsock.sock;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001451 pfd.events = POLLIN;
1452 pfd.revents = 0;
1453 r = poll(&pfd, 1, 0);
1454
1455 if ((r < 0) && (errno != EINTR)) {
1456 sprintf(msg, "poll failed (%s)", strerror(errno));
1457 session->status = NC_STATUS_INVALID;
1458 ret = NC_PSPOLL_ERROR;
1459 } else if (r > 0) {
1460 if (pfd.revents & (POLLHUP | POLLNVAL)) {
1461 sprintf(msg, "communication socket unexpectedly closed");
1462 session->status = NC_STATUS_INVALID;
1463 session->term_reason = NC_SESSION_TERM_DROPPED;
1464 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1465 } else if (pfd.revents & POLLERR) {
1466 sprintf(msg, "communication socket error");
1467 session->status = NC_STATUS_INVALID;
1468 session->term_reason = NC_SESSION_TERM_OTHER;
1469 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1470 } else {
1471 ret = NC_PSPOLL_RPC;
1472 }
1473 } else {
1474 ret = NC_PSPOLL_TIMEOUT;
1475 }
1476 break;
1477 case NC_TI_NONE:
1478 sprintf(msg, "internal error (%s:%d)", __FILE__, __LINE__);
1479 ret = NC_PSPOLL_ERROR;
1480 break;
1481 }
1482
Michal Vasko131120a2018-05-29 15:44:02 +02001483 nc_session_io_unlock(session, __func__);
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001484 return ret;
1485}
1486
1487API int
1488nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session)
1489{
1490 int ret, r;
1491 uint8_t q_id;
1492 uint16_t i, j;
1493 char msg[256];
1494 struct timespec ts_timeout, ts_cur;
1495 struct nc_session *cur_session;
fanchanghu3d4e7212017-08-09 09:42:30 +08001496 struct nc_ps_session *cur_ps_session;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001497 struct nc_server_rpc *rpc = NULL;
1498
Michal Vasko30a5d6b2017-02-15 14:29:39 +01001499 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001500 ERRARG("ps");
Michal Vasko71090fc2016-05-24 16:37:28 +02001501 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001502 }
1503
Michal Vaskoade892d2017-02-22 13:40:35 +01001504 /* PS LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001505 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001506 return NC_PSPOLL_ERROR;
Michal Vaskobe86fe32016-04-07 10:43:03 +02001507 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001508
Michal Vaskoade892d2017-02-22 13:40:35 +01001509 if (!ps->session_count) {
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001510 nc_ps_unlock(ps, q_id, __func__);
1511 return NC_PSPOLL_NOSESSIONS;
Michal Vaskoade892d2017-02-22 13:40:35 +01001512 }
Michal Vaskobd8ef262016-01-20 11:09:27 +01001513
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001514 /* fill timespecs */
Michal Vasko77a6abe2017-10-05 10:02:20 +02001515 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +01001516 if (timeout > -1) {
Michal Vasko77a6abe2017-10-05 10:02:20 +02001517 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +01001518 nc_addtimespec(&ts_timeout, timeout);
1519 }
1520
1521 /* poll all the sessions one-by-one */
Michal Vasko9a327362017-01-11 11:31:46 +01001522 do {
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001523 /* loop from i to j once (all sessions) */
Michal Vasko9a327362017-01-11 11:31:46 +01001524 if (ps->last_event_session == ps->session_count - 1) {
1525 i = j = 0;
1526 } else {
1527 i = j = ps->last_event_session + 1;
Michal Vaskobd8ef262016-01-20 11:09:27 +01001528 }
Michal Vasko9a327362017-01-11 11:31:46 +01001529 do {
fanchanghu3d4e7212017-08-09 09:42:30 +08001530 cur_ps_session = ps->sessions[i];
1531 cur_session = cur_ps_session->session;
Michal Vasko428087d2016-01-14 16:04:28 +01001532
Michal Vasko131120a2018-05-29 15:44:02 +02001533 /* SESSION RPC LOCK */
1534 r = nc_session_rpc_lock(cur_session, 0, __func__);
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001535 if (r == -1) {
1536 ret = NC_PSPOLL_ERROR;
1537 } else if (r == 1) {
1538 /* no one else is currently working with the session, so we can, otherwise skip it */
Michal Vaskoc97cb162017-10-16 12:10:23 +02001539 switch (cur_ps_session->state) {
1540 case NC_PS_STATE_NONE:
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001541 if (cur_session->status == NC_STATUS_RUNNING) {
1542 /* session is fine, work with it */
fanchanghu3d4e7212017-08-09 09:42:30 +08001543 cur_ps_session->state = NC_PS_STATE_BUSY;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001544
Michal Vasko131120a2018-05-29 15:44:02 +02001545 ret = nc_ps_poll_session_io(cur_session, NC_SESSION_LOCK_TIMEOUT, ts_cur.tv_sec, msg);
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001546 switch (ret) {
1547 case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR:
1548 ERR("Session %u: %s.", cur_session->id, msg);
fanchanghu3d4e7212017-08-09 09:42:30 +08001549 cur_ps_session->state = NC_PS_STATE_INVALID;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001550 break;
1551 case NC_PSPOLL_ERROR:
1552 ERR("Session %u: %s.", cur_session->id, msg);
fanchanghu3d4e7212017-08-09 09:42:30 +08001553 cur_ps_session->state = NC_PS_STATE_NONE;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001554 break;
1555 case NC_PSPOLL_TIMEOUT:
1556#ifdef NC_ENABLED_SSH
1557 case NC_PSPOLL_SSH_CHANNEL:
1558 case NC_PSPOLL_SSH_MSG:
1559#endif
fanchanghu3d4e7212017-08-09 09:42:30 +08001560 cur_ps_session->state = NC_PS_STATE_NONE;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001561 break;
1562 case NC_PSPOLL_RPC:
1563 /* let's keep the state busy, we are not done with this session */
1564 break;
1565 }
1566 } else {
1567 /* session is not fine, let the caller know */
1568 ret = NC_PSPOLL_SESSION_TERM;
1569 if (cur_session->term_reason != NC_SESSION_TERM_CLOSED) {
1570 ret |= NC_PSPOLL_SESSION_ERROR;
1571 }
fanchanghu3d4e7212017-08-09 09:42:30 +08001572 cur_ps_session->state = NC_PS_STATE_INVALID;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001573 }
Michal Vaskoc97cb162017-10-16 12:10:23 +02001574 break;
1575 case NC_PS_STATE_BUSY:
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001576 /* it definitely should not be busy because we have the lock */
1577 ERRINT;
Michal Vasko4992d802017-08-09 12:20:01 +02001578 ret = NC_PSPOLL_ERROR;
Michal Vaskoc97cb162017-10-16 12:10:23 +02001579 break;
1580 case NC_PS_STATE_INVALID:
1581 /* we got it locked, but it will be freed, let it be */
1582 ret = NC_PSPOLL_TIMEOUT;
1583 break;
Michal Vasko428087d2016-01-14 16:04:28 +01001584 }
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001585
Michal Vasko131120a2018-05-29 15:44:02 +02001586 /* keep RPC lock in this one case */
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001587 if (ret != NC_PSPOLL_RPC) {
Michal Vasko131120a2018-05-29 15:44:02 +02001588 /* SESSION RPC UNLOCK */
1589 nc_session_rpc_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vaskoade892d2017-02-22 13:40:35 +01001590 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001591 } else {
1592 /* timeout */
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001593 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko428087d2016-01-14 16:04:28 +01001594 }
Michal Vasko428087d2016-01-14 16:04:28 +01001595
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001596 /* something happened */
1597 if (ret != NC_PSPOLL_TIMEOUT) {
1598 break;
1599 }
1600
Michal Vasko9a327362017-01-11 11:31:46 +01001601 if (i == ps->session_count - 1) {
1602 i = 0;
1603 } else {
1604 ++i;
1605 }
1606 } while (i != j);
1607
Michal Vaskoade892d2017-02-22 13:40:35 +01001608 /* no event, no session remains locked */
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001609 if (ret == NC_PSPOLL_TIMEOUT) {
Michal Vasko9a327362017-01-11 11:31:46 +01001610 usleep(NC_TIMEOUT_STEP);
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001611 /* update current time */
Michal Vasko77a6abe2017-10-05 10:02:20 +02001612 nc_gettimespec_mono(&ts_cur);
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001613
1614 if ((timeout > -1) && (nc_difftimespec(&ts_cur, &ts_timeout) < 1)) {
1615 /* final timeout */
1616 break;
Michal Vasko9a327362017-01-11 11:31:46 +01001617 }
Michal Vasko428087d2016-01-14 16:04:28 +01001618 }
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001619 } while (ret == NC_PSPOLL_TIMEOUT);
Michal Vasko428087d2016-01-14 16:04:28 +01001620
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001621 /* do we want to return the session? */
1622 switch (ret) {
1623 case NC_PSPOLL_RPC:
1624 case NC_PSPOLL_SESSION_TERM:
1625 case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR:
1626#ifdef NC_ENABLED_SSH
1627 case NC_PSPOLL_SSH_CHANNEL:
1628 case NC_PSPOLL_SSH_MSG:
1629#endif
1630 if (session) {
1631 *session = cur_session;
1632 }
1633 ps->last_event_session = i;
1634 break;
1635 default:
1636 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001637 }
Michal Vasko428087d2016-01-14 16:04:28 +01001638
Michal Vaskoade892d2017-02-22 13:40:35 +01001639 /* PS UNLOCK */
1640 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +01001641
Michal Vasko131120a2018-05-29 15:44:02 +02001642 /* we have some data available and the session is RPC locked (but not IO locked) */
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001643 if (ret == NC_PSPOLL_RPC) {
Michal Vasko131120a2018-05-29 15:44:02 +02001644 ret = nc_server_recv_rpc_io(cur_session, timeout, &rpc);
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001645 if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) {
1646 if (cur_session->status != NC_STATUS_RUNNING) {
1647 ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
fanchanghu3d4e7212017-08-09 09:42:30 +08001648 cur_ps_session->state = NC_PS_STATE_INVALID;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001649 } else {
fanchanghu3d4e7212017-08-09 09:42:30 +08001650 cur_ps_session->state = NC_PS_STATE_NONE;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001651 }
1652 } else {
Michal Vasko9fb42272017-10-05 13:50:05 +02001653 cur_session->opts.server.last_rpc = ts_cur.tv_sec;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001654
Michal Vasko7f1ee932018-10-11 09:41:42 +02001655 /* process RPC */
Michal Vasko131120a2018-05-29 15:44:02 +02001656 ret |= nc_server_send_reply_io(cur_session, timeout, rpc);
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001657 if (cur_session->status != NC_STATUS_RUNNING) {
1658 ret |= NC_PSPOLL_SESSION_TERM;
1659 if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) {
1660 ret |= NC_PSPOLL_SESSION_ERROR;
1661 }
fanchanghu3d4e7212017-08-09 09:42:30 +08001662 cur_ps_session->state = NC_PS_STATE_INVALID;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001663 } else {
fanchanghu3d4e7212017-08-09 09:42:30 +08001664 cur_ps_session->state = NC_PS_STATE_NONE;
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001665 }
Michal Vasko428087d2016-01-14 16:04:28 +01001666 }
Michal Vasko7f1ee932018-10-11 09:41:42 +02001667 nc_server_rpc_free(rpc, server_opts.ctx);
Michal Vaskoefbc5e32017-05-26 14:02:16 +02001668
Michal Vasko131120a2018-05-29 15:44:02 +02001669 /* SESSION RPC UNLOCK */
1670 nc_session_rpc_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +01001671 }
1672
Michal Vasko48a63ed2016-03-01 09:48:21 +01001673 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001674}
1675
Michal Vaskod09eae62016-02-01 10:32:52 +01001676API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001677nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *))
Michal Vaskod09eae62016-02-01 10:32:52 +01001678{
Michal Vaskob30b99c2016-07-26 11:35:43 +02001679 uint8_t q_id;
Michal Vaskod09eae62016-02-01 10:32:52 +01001680 uint16_t i;
1681 struct nc_session *session;
1682
Michal Vasko9a25e932016-02-01 10:36:42 +01001683 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001684 ERRARG("ps");
Michal Vasko9a25e932016-02-01 10:36:42 +01001685 return;
1686 }
1687
Michal Vasko48a63ed2016-03-01 09:48:21 +01001688 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001689 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +02001690 return;
1691 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001692
Michal Vasko48a63ed2016-03-01 09:48:21 +01001693 if (all) {
Radek Krejci4f8042c2016-03-03 13:11:26 +01001694 for (i = 0; i < ps->session_count; i++) {
fanchanghu3d4e7212017-08-09 09:42:30 +08001695 nc_session_free(ps->sessions[i]->session, data_free);
1696 free(ps->sessions[i]);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001697 }
1698 free(ps->sessions);
1699 ps->sessions = NULL;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001700 ps->session_count = 0;
Michal Vasko9a327362017-01-11 11:31:46 +01001701 ps->last_event_session = 0;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001702 } else {
1703 for (i = 0; i < ps->session_count; ) {
fanchanghu3d4e7212017-08-09 09:42:30 +08001704 if (ps->sessions[i]->session->status != NC_STATUS_RUNNING) {
1705 session = ps->sessions[i]->session;
Radek Krejcid5f978f2016-03-03 13:14:45 +01001706 _nc_ps_del_session(ps, NULL, i);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001707 nc_session_free(session, data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001708 continue;
1709 }
1710
1711 ++i;
1712 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001713 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001714
1715 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001716 nc_ps_unlock(ps, q_id, __func__);
Michal Vaskod09eae62016-02-01 10:32:52 +01001717}
1718
Radek Krejci53691be2016-02-22 13:58:37 +01001719#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +01001720
Michal Vasko5f352c52019-07-10 16:12:06 +02001721static int
apropp-molex4e903c32020-04-20 03:06:58 -04001722nc_get_uid(int sock, uid_t *uid)
1723{
Michal Vaskod3910912020-04-20 09:12:49 +02001724 int ret;
apropp-molex4e903c32020-04-20 03:06:58 -04001725
Michal Vaskod3910912020-04-20 09:12:49 +02001726#ifdef SO_PEERCRED
1727 struct ucred ucred;
1728 socklen_t len;
1729 len = sizeof(ucred);
1730 ret = getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len);
1731 if (!ret) {
1732 *uid = ucred.uid;
1733 }
1734#else
1735 ret = getpeereid(sock, uid, NULL);
1736#endif
1737
1738 if (ret < 0) {
1739 ERR("Failed to get credentials from unix socket (%s).", strerror(errno));
1740 return -1;
1741 }
apropp-molex4e903c32020-04-20 03:06:58 -04001742 return 0;
1743}
1744
1745static int
Michal Vasko5f352c52019-07-10 16:12:06 +02001746nc_accept_unix(struct nc_session *session, int sock)
1747{
apropp-molex4e903c32020-04-20 03:06:58 -04001748#if defined(SO_PEERCRED) || defined(HAVE_GETPEEREID)
Michal Vasko5f352c52019-07-10 16:12:06 +02001749 const struct passwd *pw;
Michal Vasko5f352c52019-07-10 16:12:06 +02001750 char *username;
Michal Vasko5f352c52019-07-10 16:12:06 +02001751 session->ti_type = NC_TI_UNIX;
apropp-molex4e903c32020-04-20 03:06:58 -04001752 uid_t uid;
Michal Vasko5f352c52019-07-10 16:12:06 +02001753
Michal Vaskod3910912020-04-20 09:12:49 +02001754 if (nc_get_uid(sock, &uid)) {
1755 close(sock);
Michal Vasko5f352c52019-07-10 16:12:06 +02001756 return -1;
1757 }
1758
apropp-molex4e903c32020-04-20 03:06:58 -04001759 pw = getpwuid(uid);
Michal Vasko5f352c52019-07-10 16:12:06 +02001760 if (pw == NULL) {
apropp-molex4e903c32020-04-20 03:06:58 -04001761 ERR("Failed to find username for uid=%u (%s).\n", uid,
Michal Vasko5f352c52019-07-10 16:12:06 +02001762 strerror(errno));
1763 close(sock);
1764 return -1;
1765 }
1766
1767 username = strdup(pw->pw_name);
1768 if (username == NULL) {
1769 ERRMEM;
1770 close(sock);
1771 return -1;
1772 }
1773 session->username = lydict_insert_zc(server_opts.ctx, username);
1774
1775 session->ti.unixsock.sock = sock;
1776
1777 return 1;
Claus Klein22091912020-01-20 13:45:47 +01001778#else
1779 return -1;
1780#endif
Michal Vasko5f352c52019-07-10 16:12:06 +02001781}
1782
Michal Vaskoe2713da2016-08-22 16:06:40 +02001783API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001784nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01001785{
Michal Vasko3031aae2016-01-27 16:07:18 +01001786 uint16_t i;
Michal Vaskoade892d2017-02-22 13:40:35 +01001787 int ret = 0;
Michal Vasko9e036d52016-01-08 10:49:26 +01001788
Michal Vasko45e53ae2016-04-07 11:46:03 +02001789 if (!name) {
1790 ERRARG("name");
1791 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001792 }
1793
Michal Vaskoade892d2017-02-22 13:40:35 +01001794 /* BIND LOCK */
1795 pthread_mutex_lock(&server_opts.bind_lock);
1796
1797 /* ENDPT WRITE LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001798 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001799
1800 /* check name uniqueness */
1801 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001802 if (!strcmp(server_opts.endpts[i].name, name)) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001803 ERR("Endpoint \"%s\" already exists.", name);
Michal Vaskoade892d2017-02-22 13:40:35 +01001804 ret = -1;
1805 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +01001806 }
1807 }
1808
Michal Vasko3031aae2016-01-27 16:07:18 +01001809 ++server_opts.endpt_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001810 server_opts.endpts = nc_realloc(server_opts.endpts, server_opts.endpt_count * sizeof *server_opts.endpts);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001811 if (!server_opts.endpts) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001812 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001813 ret = -1;
1814 goto cleanup;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001815 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001816 memset(&server_opts.endpts[server_opts.endpt_count - 1], 0, sizeof *server_opts.endpts);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001817 server_opts.endpts[server_opts.endpt_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001818 server_opts.endpts[server_opts.endpt_count - 1].ti = ti;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001819 server_opts.endpts[server_opts.endpt_count - 1].ka.idle_time = 1;
1820 server_opts.endpts[server_opts.endpt_count - 1].ka.max_probes = 10;
1821 server_opts.endpts[server_opts.endpt_count - 1].ka.probe_interval = 5;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001822
Michal Vaskoe2713da2016-08-22 16:06:40 +02001823 server_opts.binds = nc_realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001824 if (!server_opts.binds) {
1825 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001826 ret = -1;
1827 goto cleanup;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001828 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001829
Michal Vaskoe49a15f2019-05-27 14:18:36 +02001830 memset(&server_opts.binds[server_opts.endpt_count - 1], 0, sizeof *server_opts.binds);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001831 server_opts.binds[server_opts.endpt_count - 1].sock = -1;
1832
1833 switch (ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001834#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001835 case NC_TI_LIBSSH:
1836 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts));
1837 if (!server_opts.endpts[server_opts.endpt_count - 1].opts.ssh) {
1838 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001839 ret = -1;
1840 goto cleanup;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001841 }
1842 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_methods =
1843 NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
1844 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_attempts = 3;
Michal Vaskocbad4c52019-06-27 16:30:35 +02001845 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_timeout = 30;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001846 break;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001847#endif
Michal Vaskoe2713da2016-08-22 16:06:40 +02001848#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001849 case NC_TI_OPENSSL:
1850 server_opts.endpts[server_opts.endpt_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts));
1851 if (!server_opts.endpts[server_opts.endpt_count - 1].opts.tls) {
1852 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001853 ret = -1;
1854 goto cleanup;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001855 }
1856 break;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001857#endif
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001858 case NC_TI_UNIX:
1859 server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock = calloc(1, sizeof(struct nc_server_unix_opts));
1860 if (!server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock) {
1861 ERRMEM;
1862 ret = -1;
1863 goto cleanup;
1864 }
1865 server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->mode = (mode_t)-1;
1866 server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->uid = (uid_t)-1;
1867 server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->gid = (gid_t)-1;
1868 break;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001869 default:
1870 ERRINT;
Michal Vaskoade892d2017-02-22 13:40:35 +01001871 ret = -1;
1872 goto cleanup;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001873 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001874
Michal Vaskoade892d2017-02-22 13:40:35 +01001875cleanup:
1876 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001877 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko9e036d52016-01-08 10:49:26 +01001878
Michal Vaskoade892d2017-02-22 13:40:35 +01001879 /* BIND UNLOCK */
1880 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001881
Michal Vaskoade892d2017-02-22 13:40:35 +01001882 return ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001883}
1884
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001885API int
Michal Vasko59050372016-11-22 14:33:55 +01001886nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01001887{
1888 uint32_t i;
1889 int ret = -1;
1890
Michal Vaskoade892d2017-02-22 13:40:35 +01001891 /* BIND LOCK */
1892 pthread_mutex_lock(&server_opts.bind_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001893
Michal Vaskoade892d2017-02-22 13:40:35 +01001894 /* ENDPT WRITE LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001895 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vasko9e036d52016-01-08 10:49:26 +01001896
Michal Vasko59050372016-11-22 14:33:55 +01001897 if (!name && !ti) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001898 /* remove all endpoints */
Michal Vasko3031aae2016-01-27 16:07:18 +01001899 for (i = 0; i < server_opts.endpt_count; ++i) {
1900 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001901 switch (server_opts.endpts[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001902#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001903 case NC_TI_LIBSSH:
1904 nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh);
1905 free(server_opts.endpts[i].opts.ssh);
1906 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001907#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001908#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001909 case NC_TI_OPENSSL:
1910 nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls);
1911 free(server_opts.endpts[i].opts.tls);
1912 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001913#endif
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001914 case NC_TI_UNIX:
1915 free(server_opts.endpts[i].opts.unixsock);
1916 break;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001917 default:
1918 ERRINT;
1919 /* won't get here ...*/
1920 break;
1921 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001922 ret = 0;
1923 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001924 free(server_opts.endpts);
1925 server_opts.endpts = NULL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001926
1927 /* remove all binds */
1928 for (i = 0; i < server_opts.endpt_count; ++i) {
1929 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1930 if (server_opts.binds[i].sock > -1) {
1931 close(server_opts.binds[i].sock);
1932 }
1933 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001934 free(server_opts.binds);
1935 server_opts.binds = NULL;
1936
Michal Vasko3031aae2016-01-27 16:07:18 +01001937 server_opts.endpt_count = 0;
1938
Michal Vasko1a38c862016-01-15 15:50:07 +01001939 } else {
Michal Vasko59050372016-11-22 14:33:55 +01001940 /* remove one endpoint with bind(s) or all endpoints using one transport protocol */
Michal Vasko3031aae2016-01-27 16:07:18 +01001941 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vasko59050372016-11-22 14:33:55 +01001942 if ((name && !strcmp(server_opts.endpts[i].name, name)) || (!name && (server_opts.endpts[i].ti == ti))) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001943 /* remove endpt */
Michal Vasko3031aae2016-01-27 16:07:18 +01001944 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001945 switch (server_opts.endpts[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001946#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001947 case NC_TI_LIBSSH:
1948 nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh);
1949 free(server_opts.endpts[i].opts.ssh);
1950 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001951#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001952#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001953 case NC_TI_OPENSSL:
1954 nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls);
1955 free(server_opts.endpts[i].opts.tls);
1956 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001957#endif
Olivier Matzac7fa2f2018-10-11 10:02:04 +02001958 case NC_TI_UNIX:
1959 free(server_opts.endpts[i].opts.unixsock);
1960 break;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001961 default:
1962 ERRINT;
1963 break;
1964 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001965
Michal Vaskoe2713da2016-08-22 16:06:40 +02001966 /* remove bind(s) */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001967 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1968 if (server_opts.binds[i].sock > -1) {
1969 close(server_opts.binds[i].sock);
1970 }
1971
1972 /* move last endpt and bind(s) to the empty space */
Michal Vasko3031aae2016-01-27 16:07:18 +01001973 --server_opts.endpt_count;
Michal Vasko9ed67a72016-10-13 15:00:51 +02001974 if (!server_opts.endpt_count) {
Michal Vaskoc0256492016-02-02 12:19:06 +01001975 free(server_opts.binds);
1976 server_opts.binds = NULL;
1977 free(server_opts.endpts);
1978 server_opts.endpts = NULL;
Michal Vasko9ed67a72016-10-13 15:00:51 +02001979 } else if (i < server_opts.endpt_count) {
1980 memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
1981 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
Michal Vaskoc0256492016-02-02 12:19:06 +01001982 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001983
1984 ret = 0;
Michal Vasko59050372016-11-22 14:33:55 +01001985 if (name) {
1986 break;
1987 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001988 }
1989 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001990 }
1991
Michal Vaskoade892d2017-02-22 13:40:35 +01001992 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001993 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001994
Michal Vaskoade892d2017-02-22 13:40:35 +01001995 /* BIND UNLOCK */
1996 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vasko9e036d52016-01-08 10:49:26 +01001997
1998 return ret;
1999}
2000
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002001API int
2002nc_server_endpt_count(void)
2003{
2004 return server_opts.endpt_count;
2005}
2006
Michal Vasko1b5973e2020-01-30 16:05:46 +01002007API int
2008nc_server_is_endpt(const char *name)
2009{
2010 uint16_t i;
2011 int found = 0;
2012
Michal Vaskofb1724b2020-01-31 11:02:00 +01002013 if (!name) {
2014 return found;
2015 }
2016
Michal Vasko1b5973e2020-01-30 16:05:46 +01002017 /* ENDPT READ LOCK */
2018 pthread_rwlock_rdlock(&server_opts.endpt_lock);
2019
2020 /* check name uniqueness */
2021 for (i = 0; i < server_opts.endpt_count; ++i) {
2022 if (!strcmp(server_opts.endpts[i].name, name)) {
2023 found = 1;
2024 break;
2025 }
2026 }
2027
2028 /* ENDPT UNLOCK */
2029 pthread_rwlock_unlock(&server_opts.endpt_lock);
2030
2031 return found;
2032}
2033
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002034int
2035nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port)
2036{
2037 struct nc_endpt *endpt;
2038 struct nc_bind *bind = NULL;
2039 uint16_t i;
2040 int sock = -1, set_addr, ret = 0;
2041
2042 if (!endpt_name) {
2043 ERRARG("endpt_name");
2044 return -1;
2045 } else if ((!address && !port) || (address && port)) {
2046 ERRARG("address and port");
2047 return -1;
2048 }
2049
2050 if (address) {
2051 set_addr = 1;
2052 } else {
2053 set_addr = 0;
2054 }
2055
2056 /* BIND LOCK */
2057 pthread_mutex_lock(&server_opts.bind_lock);
2058
2059 /* ENDPT LOCK */
2060 endpt = nc_server_endpt_lock_get(endpt_name, 0, &i);
2061 if (!endpt) {
2062 /* BIND UNLOCK */
2063 pthread_mutex_unlock(&server_opts.bind_lock);
2064 return -1;
2065 }
2066
2067 bind = &server_opts.binds[i];
2068
2069 if (set_addr) {
2070 port = bind->port;
2071 } else {
2072 address = bind->address;
2073 }
2074
2075 if (!set_addr && endpt->ti == NC_TI_UNIX) {
2076 ret = -1;
2077 goto cleanup;
2078 }
2079
2080 /* we have all the information we need to create a listening socket */
2081 if (address && (port || endpt->ti == NC_TI_UNIX)) {
2082 /* create new socket, close the old one */
2083 if (endpt->ti == NC_TI_UNIX)
2084 sock = nc_sock_listen_unix(address, endpt->opts.unixsock);
2085 else
2086 sock = nc_sock_listen_inet(address, port, &endpt->ka);
2087 if (sock == -1) {
2088 ret = -1;
2089 goto cleanup;
2090 }
2091
2092 if (bind->sock > -1) {
2093 close(bind->sock);
2094 }
2095 bind->sock = sock;
2096 } /* else we are just setting address or port */
2097
2098 if (set_addr) {
2099 lydict_remove(server_opts.ctx, bind->address);
2100 bind->address = lydict_insert(server_opts.ctx, address, 0);
2101 } else {
2102 bind->port = port;
2103 }
2104
2105 if (sock > -1) {
2106#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
2107 VRB("Listening on %s:%u for %s connections.", address, port, (endpt->ti == NC_TI_LIBSSH ? "SSH" : "TLS"));
2108#elif defined(NC_ENABLED_SSH)
2109 VRB("Listening on %s:%u for SSH connections.", address, port);
2110#else
2111 VRB("Listening on %s:%u for TLS connections.", address, port);
2112#endif
2113 }
2114
2115cleanup:
2116 /* ENDPT UNLOCK */
2117 pthread_rwlock_unlock(&server_opts.endpt_lock);
2118
2119 /* BIND UNLOCK */
2120 pthread_mutex_unlock(&server_opts.bind_lock);
2121
2122 return ret;
2123}
2124
2125API int
2126nc_server_endpt_set_address(const char *endpt_name, const char *address)
2127{
2128 return nc_server_endpt_set_address_port(endpt_name, address, 0);
2129}
2130
2131API int
2132nc_server_endpt_set_port(const char *endpt_name, uint16_t port)
2133{
2134 return nc_server_endpt_set_address_port(endpt_name, NULL, port);
2135}
2136
2137API int
2138nc_server_endpt_set_perms(const char *endpt_name, mode_t mode, uid_t uid, gid_t gid)
2139{
2140 struct nc_endpt *endpt;
2141 uint16_t i;
2142 int ret = 0;
2143
2144 if (!endpt_name) {
2145 ERRARG("endpt_name");
2146 return -1;
2147 } else if (mode == 0) {
2148 ERRARG("mode");
2149 return -1;
2150 }
2151
2152 /* ENDPT LOCK */
2153 endpt = nc_server_endpt_lock_get(endpt_name, 0, &i);
2154 if (!endpt)
2155 return -1;
2156
2157 if (endpt->ti != NC_TI_UNIX) {
2158 ret = -1;
2159 goto cleanup;
2160 }
2161
2162 endpt->opts.unixsock->mode = mode;
2163 endpt->opts.unixsock->uid = uid;
2164 endpt->opts.unixsock->gid = gid;
2165
2166cleanup:
2167 /* ENDPT UNLOCK */
2168 pthread_rwlock_unlock(&server_opts.endpt_lock);
2169
2170 return ret;
2171}
2172
2173API int
2174nc_server_endpt_enable_keepalives(const char *endpt_name, int enable)
2175{
2176 struct nc_endpt *endpt;
2177 int ret = 0;
2178
2179 if (!endpt_name) {
2180 ERRARG("endpt_name");
2181 return -1;
2182 }
2183
2184 /* ENDPT LOCK */
2185 endpt = nc_server_endpt_lock_get(endpt_name, 0, NULL);
2186 if (!endpt) {
2187 return -1;
2188 }
2189
2190 endpt->ka.enabled = (enable ? 1 : 0);
2191
2192 /* ENDPT UNLOCK */
2193 pthread_rwlock_unlock(&server_opts.endpt_lock);
2194
2195 return ret;
2196}
2197
2198API int
2199nc_server_endpt_set_keepalives(const char *endpt_name, int idle_time, int max_probes, int probe_interval)
2200{
2201 struct nc_endpt *endpt;
2202 int ret = 0;
2203
2204 if (!endpt_name) {
2205 ERRARG("endpt_name");
2206 return -1;
2207 }
2208
2209 /* ENDPT LOCK */
2210 endpt = nc_server_endpt_lock_get(endpt_name, 0, NULL);
2211 if (!endpt) {
2212 return -1;
2213 }
2214
2215 if (idle_time > -1) {
2216 endpt->ka.idle_time = idle_time;
2217 }
2218 if (max_probes > -1) {
2219 endpt->ka.max_probes = max_probes;
2220 }
2221 if (probe_interval > -1) {
2222 endpt->ka.probe_interval = probe_interval;
2223 }
2224
2225 /* ENDPT UNLOCK */
2226 pthread_rwlock_unlock(&server_opts.endpt_lock);
2227
2228 return ret;
2229}
2230
Michal Vasko71090fc2016-05-24 16:37:28 +02002231API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +01002232nc_accept(int timeout, struct nc_session **session)
Michal Vasko9e036d52016-01-08 10:49:26 +01002233{
Michal Vasko71090fc2016-05-24 16:37:28 +02002234 NC_MSG_TYPE msgtype;
Michal Vasko1a38c862016-01-15 15:50:07 +01002235 int sock, ret;
Michal Vasko5c2f7952016-01-22 13:16:31 +01002236 char *host = NULL;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002237 uint16_t port, bind_idx;
Michal Vasko9fb42272017-10-05 13:50:05 +02002238 struct timespec ts_cur;
Michal Vasko9e036d52016-01-08 10:49:26 +01002239
Michal Vasko45e53ae2016-04-07 11:46:03 +02002240 if (!server_opts.ctx) {
2241 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +02002242 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02002243 } else if (!session) {
2244 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02002245 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01002246 }
2247
Michal Vaskoade892d2017-02-22 13:40:35 +01002248 /* BIND LOCK */
2249 pthread_mutex_lock(&server_opts.bind_lock);
Michal Vasko51e514d2016-02-02 15:51:52 +01002250
2251 if (!server_opts.endpt_count) {
Michal Vasko863a6e92016-07-28 14:27:41 +02002252 ERR("No endpoints to accept sessions on.");
Michal Vaskoade892d2017-02-22 13:40:35 +01002253 /* BIND UNLOCK */
2254 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vasko71090fc2016-05-24 16:37:28 +02002255 return NC_MSG_ERROR;
Michal Vasko51e514d2016-02-02 15:51:52 +01002256 }
Michal Vaskob48aa812016-01-18 14:13:09 +01002257
Michal Vaskoe2713da2016-08-22 16:06:40 +02002258 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &bind_idx);
Michal Vasko50456e82016-02-02 12:16:08 +01002259 if (ret < 1) {
Michal Vaskoade892d2017-02-22 13:40:35 +01002260 /* BIND UNLOCK */
2261 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vaskob737d752016-02-09 09:01:27 +01002262 free(host);
Michal Vasko5e203472016-05-30 15:27:58 +02002263 if (!ret) {
2264 return NC_MSG_WOULDBLOCK;
2265 }
Michal Vasko71090fc2016-05-24 16:37:28 +02002266 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01002267 }
Michal Vaskoade892d2017-02-22 13:40:35 +01002268
2269 /* switch bind_lock for endpt_lock, so that another thread can accept another session */
2270 /* ENDPT READ LOCK */
2271 pthread_rwlock_rdlock(&server_opts.endpt_lock);
2272
2273 /* BIND UNLOCK */
2274 pthread_mutex_unlock(&server_opts.bind_lock);
2275
Michal Vaskob48aa812016-01-18 14:13:09 +01002276 sock = ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01002277
Michal Vasko131120a2018-05-29 15:44:02 +02002278 *session = nc_new_session(NC_SERVER, 0);
Michal Vasko686aa312016-01-21 15:58:18 +01002279 if (!(*session)) {
Michal Vasko9e036d52016-01-08 10:49:26 +01002280 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01002281 close(sock);
Michal Vasko5c2f7952016-01-22 13:16:31 +01002282 free(host);
Michal Vasko71090fc2016-05-24 16:37:28 +02002283 msgtype = NC_MSG_ERROR;
2284 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01002285 }
Michal Vasko1a38c862016-01-15 15:50:07 +01002286 (*session)->status = NC_STATUS_STARTING;
Michal Vasko1a38c862016-01-15 15:50:07 +01002287 (*session)->ctx = server_opts.ctx;
2288 (*session)->flags = NC_SESSION_SHAREDCTX;
2289 (*session)->host = lydict_insert_zc(server_opts.ctx, host);
2290 (*session)->port = port;
Michal Vasko9e036d52016-01-08 10:49:26 +01002291
Michal Vaskoc14e3c82016-01-11 16:14:30 +01002292 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01002293#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02002294 if (server_opts.endpts[bind_idx].ti == NC_TI_LIBSSH) {
2295 (*session)->data = server_opts.endpts[bind_idx].opts.ssh;
Michal Vaskob70c8b82017-03-17 09:09:29 +01002296 ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko71090fc2016-05-24 16:37:28 +02002297 if (ret < 0) {
2298 msgtype = NC_MSG_ERROR;
2299 goto cleanup;
2300 } else if (!ret) {
2301 msgtype = NC_MSG_WOULDBLOCK;
2302 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01002303 }
Michal Vasko3d865d22016-01-28 16:00:53 +01002304 } else
2305#endif
Radek Krejci53691be2016-02-22 13:58:37 +01002306#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02002307 if (server_opts.endpts[bind_idx].ti == NC_TI_OPENSSL) {
2308 (*session)->data = server_opts.endpts[bind_idx].opts.tls;
Michal Vaskob70c8b82017-03-17 09:09:29 +01002309 ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko71090fc2016-05-24 16:37:28 +02002310 if (ret < 0) {
2311 msgtype = NC_MSG_ERROR;
2312 goto cleanup;
2313 } else if (!ret) {
2314 msgtype = NC_MSG_WOULDBLOCK;
2315 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01002316 }
Michal Vasko3d865d22016-01-28 16:00:53 +01002317 } else
2318#endif
Olivier Matzac7fa2f2018-10-11 10:02:04 +02002319 if (server_opts.endpts[bind_idx].ti == NC_TI_UNIX) {
2320 (*session)->data = server_opts.endpts[bind_idx].opts.unixsock;
2321 ret = nc_accept_unix(*session, sock);
2322 if (ret < 0) {
2323 msgtype = NC_MSG_ERROR;
2324 goto cleanup;
2325 }
2326 } else {
Michal Vasko9e036d52016-01-08 10:49:26 +01002327 ERRINT;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01002328 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02002329 msgtype = NC_MSG_ERROR;
2330 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01002331 }
2332
Michal Vasko2cc4c682016-03-01 09:16:48 +01002333 (*session)->data = NULL;
2334
Michal Vaskoade892d2017-02-22 13:40:35 +01002335 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002336 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01002337
Michal Vaskob48aa812016-01-18 14:13:09 +01002338 /* assign new SID atomically */
Michal Vasko69a3ff62018-11-09 09:31:48 +01002339 (*session)->id = ATOMIC_INC(&server_opts.new_session_id);
Michal Vaskob48aa812016-01-18 14:13:09 +01002340
Michal Vasko9e036d52016-01-08 10:49:26 +01002341 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02002342 msgtype = nc_handshake_io(*session);
Michal Vasko71090fc2016-05-24 16:37:28 +02002343 if (msgtype != NC_MSG_HELLO) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01002344 nc_session_free(*session, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01002345 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02002346 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01002347 }
Michal Vasko9fb42272017-10-05 13:50:05 +02002348
2349 nc_gettimespec_mono(&ts_cur);
2350 (*session)->opts.server.last_rpc = ts_cur.tv_sec;
2351 nc_gettimespec_real(&ts_cur);
2352 (*session)->opts.server.session_start = ts_cur.tv_sec;
Michal Vasko1a38c862016-01-15 15:50:07 +01002353 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko9e036d52016-01-08 10:49:26 +01002354
Michal Vasko71090fc2016-05-24 16:37:28 +02002355 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01002356
Michal Vasko71090fc2016-05-24 16:37:28 +02002357cleanup:
Michal Vaskoade892d2017-02-22 13:40:35 +01002358 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002359 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01002360
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01002361 nc_session_free(*session, NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01002362 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02002363 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01002364}
2365
Michal Vaskoadf30f02019-06-24 09:34:47 +02002366/* client is expected to be locked */
2367static int
2368_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 +02002369{
2370 uint16_t i;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002371 int ret = -1;
2372
2373 if (!endpt_name) {
2374 /* remove all endpoints */
2375 for (i = 0; i < client->ch_endpt_count; ++i) {
2376 lydict_remove(server_opts.ctx, client->ch_endpts[i].name);
2377 lydict_remove(server_opts.ctx, client->ch_endpts[i].address);
2378 if (client->ch_endpts[i].sock_pending != -1) {
2379 close(client->ch_endpts[i].sock_pending);
2380 }
2381 switch (client->ch_endpts[i].ti) {
2382#ifdef NC_ENABLED_SSH
2383 case NC_TI_LIBSSH:
2384 nc_server_ssh_clear_opts(client->ch_endpts[i].opts.ssh);
2385 free(client->ch_endpts[i].opts.ssh);
2386 break;
2387#endif
2388#ifdef NC_ENABLED_TLS
2389 case NC_TI_OPENSSL:
2390 nc_server_tls_clear_opts(client->ch_endpts[i].opts.tls);
2391 free(client->ch_endpts[i].opts.tls);
2392 break;
2393#endif
2394 default:
2395 ERRINT;
2396 /* won't get here ...*/
2397 break;
2398 }
2399 }
2400 free(client->ch_endpts);
2401 client->ch_endpts = NULL;
2402 client->ch_endpt_count = 0;
2403
2404 ret = 0;
2405 } else {
2406 for (i = 0; i < client->ch_endpt_count; ++i) {
2407 if (!strcmp(client->ch_endpts[i].name, endpt_name) && (!ti || (ti == client->ch_endpts[i].ti))) {
2408 lydict_remove(server_opts.ctx, client->ch_endpts[i].name);
2409 lydict_remove(server_opts.ctx, client->ch_endpts[i].address);
2410 if (client->ch_endpts[i].sock_pending != -1) {
2411 close(client->ch_endpts[i].sock_pending);
2412 }
2413 switch (client->ch_endpts[i].ti) {
2414#ifdef NC_ENABLED_SSH
2415 case NC_TI_LIBSSH:
2416 nc_server_ssh_clear_opts(client->ch_endpts[i].opts.ssh);
2417 free(client->ch_endpts[i].opts.ssh);
2418 break;
2419#endif
2420#ifdef NC_ENABLED_TLS
2421 case NC_TI_OPENSSL:
2422 nc_server_tls_clear_opts(client->ch_endpts[i].opts.tls);
2423 free(client->ch_endpts[i].opts.tls);
2424 break;
2425#endif
2426 default:
2427 ERRINT;
2428 /* won't get here ...*/
2429 break;
2430 }
2431
2432 /* move last endpoint to the empty space */
2433 --client->ch_endpt_count;
2434 if (i < client->ch_endpt_count) {
2435 memcpy(&client->ch_endpts[i], &client->ch_endpts[client->ch_endpt_count], sizeof *client->ch_endpts);
2436 } else if (!server_opts.ch_client_count) {
2437 free(server_opts.ch_clients);
2438 server_opts.ch_clients = NULL;
2439 }
2440
2441 ret = 0;
2442 break;
2443 }
2444 }
2445 }
2446
2447 return ret;
2448}
2449
2450API int
2451nc_server_ch_add_client(const char *name)
2452{
2453 uint16_t i;
2454 struct nc_ch_client *client;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002455
2456 if (!name) {
2457 ERRARG("name");
2458 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002459 }
2460
2461 /* WRITE LOCK */
2462 pthread_rwlock_wrlock(&server_opts.ch_client_lock);
2463
2464 /* check name uniqueness */
2465 for (i = 0; i < server_opts.ch_client_count; ++i) {
2466 if (!strcmp(server_opts.ch_clients[i].name, name)) {
2467 ERR("Call Home client \"%s\" already exists.", name);
2468 /* WRITE UNLOCK */
2469 pthread_rwlock_unlock(&server_opts.ch_client_lock);
2470 return -1;
2471 }
2472 }
2473
2474 ++server_opts.ch_client_count;
2475 server_opts.ch_clients = nc_realloc(server_opts.ch_clients, server_opts.ch_client_count * sizeof *server_opts.ch_clients);
2476 if (!server_opts.ch_clients) {
2477 ERRMEM;
2478 /* WRITE UNLOCK */
2479 pthread_rwlock_unlock(&server_opts.ch_client_lock);
2480 return -1;
2481 }
Michal Vaskoadf30f02019-06-24 09:34:47 +02002482 client = &server_opts.ch_clients[server_opts.ch_client_count - 1];
Michal Vasko2e6defd2016-10-07 15:48:15 +02002483
Michal Vaskoadf30f02019-06-24 09:34:47 +02002484 client->name = lydict_insert(server_opts.ctx, name, 0);
2485 client->id = ATOMIC_INC(&server_opts.new_client_id);
2486 client->ch_endpts = NULL;
2487 client->ch_endpt_count = 0;
2488 client->conn_type = 0;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002489
Michal Vasko2e6defd2016-10-07 15:48:15 +02002490 /* set CH default options */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002491 client->start_with = NC_CH_FIRST_LISTED;
2492 client->max_attempts = 3;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002493
Michal Vaskoadf30f02019-06-24 09:34:47 +02002494 pthread_mutex_init(&client->lock, NULL);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002495
2496 /* WRITE UNLOCK */
2497 pthread_rwlock_unlock(&server_opts.ch_client_lock);
2498
2499 return 0;
2500}
2501
2502API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02002503nc_server_ch_del_client(const char *name)
Michal Vasko2e6defd2016-10-07 15:48:15 +02002504{
Michal Vaskoadf30f02019-06-24 09:34:47 +02002505 uint16_t i;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002506 int ret = -1;
2507
2508 /* WRITE LOCK */
2509 pthread_rwlock_wrlock(&server_opts.ch_client_lock);
2510
Michal Vaskoadf30f02019-06-24 09:34:47 +02002511 if (!name) {
2512 /* remove all CH clients with endpoints */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002513 for (i = 0; i < server_opts.ch_client_count; ++i) {
2514 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name);
2515
2516 /* remove all endpoints */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002517 _nc_server_ch_client_del_endpt(&server_opts.ch_clients[i], NULL, 0);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002518
2519 pthread_mutex_destroy(&server_opts.ch_clients[i].lock);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002520 ret = 0;
2521 }
2522 free(server_opts.ch_clients);
2523 server_opts.ch_clients = NULL;
2524
2525 server_opts.ch_client_count = 0;
2526
2527 } else {
Michal Vaskoadf30f02019-06-24 09:34:47 +02002528 /* remove one client with endpoints */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002529 for (i = 0; i < server_opts.ch_client_count; ++i) {
Michal Vaskoadf30f02019-06-24 09:34:47 +02002530 if (!strcmp(server_opts.ch_clients[i].name, name)) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02002531 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name);
2532
Michal Vasko2e6defd2016-10-07 15:48:15 +02002533 /* remove all endpoints */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002534 _nc_server_ch_client_del_endpt(&server_opts.ch_clients[i], NULL, 0);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002535
2536 pthread_mutex_destroy(&server_opts.ch_clients[i].lock);
2537
2538 /* move last client and endpoint(s) to the empty space */
2539 --server_opts.ch_client_count;
2540 if (i < server_opts.ch_client_count) {
2541 memcpy(&server_opts.ch_clients[i], &server_opts.ch_clients[server_opts.ch_client_count],
Michal Vaskoadf30f02019-06-24 09:34:47 +02002542 sizeof *server_opts.ch_clients);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002543 } else if (!server_opts.ch_client_count) {
2544 free(server_opts.ch_clients);
2545 server_opts.ch_clients = NULL;
2546 }
2547
2548 ret = 0;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002549 break;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002550 }
2551 }
2552 }
2553
2554 /* WRITE UNLOCK */
2555 pthread_rwlock_unlock(&server_opts.ch_client_lock);
2556
2557 return ret;
2558}
2559
2560API int
Michal Vaskofb1724b2020-01-31 11:02:00 +01002561nc_server_ch_is_client(const char *name)
2562{
2563 uint16_t i;
2564 int found = 0;
2565
2566 if (!name) {
2567 return found;
2568 }
2569
2570 /* READ LOCK */
2571 pthread_rwlock_rdlock(&server_opts.ch_client_lock);
2572
2573 /* check name uniqueness */
2574 for (i = 0; i < server_opts.ch_client_count; ++i) {
2575 if (!strcmp(server_opts.ch_clients[i].name, name)) {
2576 found = 1;
2577 break;
2578 }
2579 }
2580
2581 /* UNLOCK */
2582 pthread_rwlock_unlock(&server_opts.ch_client_lock);
2583
2584 return found;
2585}
2586
2587API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02002588nc_server_ch_client_add_endpt(const char *client_name, const char *endpt_name, NC_TRANSPORT_IMPL ti)
Michal Vasko2e6defd2016-10-07 15:48:15 +02002589{
2590 uint16_t i;
2591 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002592 struct nc_ch_endpt *endpt;
2593 int ret = -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002594
2595 if (!client_name) {
2596 ERRARG("client_name");
2597 return -1;
2598 } else if (!endpt_name) {
2599 ERRARG("endpt_name");
2600 return -1;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002601 } else if (!ti) {
2602 ERRARG("ti");
2603 return -1;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002604 }
2605
2606 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002607 nc_server_ch_client_lock(client_name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002608 if (!client) {
2609 return -1;
2610 }
2611
2612 for (i = 0; i < client->ch_endpt_count; ++i) {
2613 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2614 ERR("Call Home client \"%s\" endpoint \"%s\" already exists.", client_name, endpt_name);
Michal Vaskoadf30f02019-06-24 09:34:47 +02002615 goto cleanup;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002616 }
2617 }
2618
2619 ++client->ch_endpt_count;
2620 client->ch_endpts = realloc(client->ch_endpts, client->ch_endpt_count * sizeof *client->ch_endpts);
2621 if (!client->ch_endpts) {
2622 ERRMEM;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002623 goto cleanup;
2624 }
2625 endpt = &client->ch_endpts[client->ch_endpt_count - 1];
2626
2627 memset(endpt, 0, sizeof *client->ch_endpts);
2628 endpt->name = lydict_insert(server_opts.ctx, endpt_name, 0);
2629 endpt->ti = ti;
2630 endpt->sock_pending = -1;
2631 endpt->ka.idle_time = 1;
2632 endpt->ka.max_probes = 10;
2633 endpt->ka.probe_interval = 5;
2634
2635 switch (ti) {
2636#ifdef NC_ENABLED_SSH
2637 case NC_TI_LIBSSH:
2638 endpt->opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts));
2639 if (!endpt->opts.ssh) {
2640 ERRMEM;
2641 goto cleanup;
2642 }
2643 endpt->opts.ssh->auth_methods = NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
2644 endpt->opts.ssh->auth_attempts = 3;
Michal Vaskocbad4c52019-06-27 16:30:35 +02002645 endpt->opts.ssh->auth_timeout = 30;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002646 break;
2647#endif
2648#ifdef NC_ENABLED_TLS
2649 case NC_TI_OPENSSL:
2650 endpt->opts.tls = calloc(1, sizeof(struct nc_server_tls_opts));
2651 if (!endpt->opts.tls) {
2652 ERRMEM;
2653 goto cleanup;
2654 }
2655 break;
2656#endif
2657 default:
2658 ERRINT;
2659 goto cleanup;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002660 }
2661
Michal Vaskoadf30f02019-06-24 09:34:47 +02002662 /* success */
2663 ret = 0;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002664
Michal Vaskoadf30f02019-06-24 09:34:47 +02002665cleanup:
Michal Vasko2e6defd2016-10-07 15:48:15 +02002666 /* UNLOCK */
2667 nc_server_ch_client_unlock(client);
2668
Michal Vaskoadf30f02019-06-24 09:34:47 +02002669 return ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002670}
2671
2672API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02002673nc_server_ch_client_del_endpt(const char *client_name, const char *endpt_name, NC_TRANSPORT_IMPL ti)
Michal Vasko2e6defd2016-10-07 15:48:15 +02002674{
Michal Vaskoadf30f02019-06-24 09:34:47 +02002675 int ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002676 struct nc_ch_client *client;
2677
2678 if (!client_name) {
2679 ERRARG("client_name");
2680 return -1;
2681 }
2682
2683 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002684 nc_server_ch_client_lock(client_name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002685 if (!client) {
2686 return -1;
2687 }
2688
Michal Vaskoadf30f02019-06-24 09:34:47 +02002689 ret = _nc_server_ch_client_del_endpt(client, endpt_name, ti);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002690
2691 /* UNLOCK */
2692 nc_server_ch_client_unlock(client);
2693
2694 return ret;
2695}
2696
2697API int
Michal Vaskofb1724b2020-01-31 11:02:00 +01002698nc_server_ch_client_is_endpt(const char *client_name, const char *endpt_name)
2699{
2700 uint16_t i;
2701 struct nc_ch_client *client = NULL;
2702 int found = 0;
2703
2704 if (!client_name || !endpt_name) {
2705 return found;
2706 }
2707
2708 /* READ LOCK */
2709 pthread_rwlock_rdlock(&server_opts.ch_client_lock);
2710
2711 for (i = 0; i < server_opts.ch_client_count; ++i) {
2712 if (!strcmp(server_opts.ch_clients[i].name, client_name)) {
2713 client = &server_opts.ch_clients[i];
2714 break;
2715 }
2716 }
2717
2718 if (!client) {
2719 goto cleanup;
2720 }
2721
2722 for (i = 0; i < client->ch_endpt_count; ++i) {
2723 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2724 found = 1;
2725 break;
2726 }
2727 }
2728
2729cleanup:
2730 /* UNLOCK */
2731 pthread_rwlock_unlock(&server_opts.ch_client_lock);
2732 return found;
2733}
2734
2735API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02002736nc_server_ch_client_endpt_set_address(const char *client_name, const char *endpt_name, const char *address)
2737{
Michal Vasko2e6defd2016-10-07 15:48:15 +02002738 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002739 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002740
2741 if (!client_name) {
2742 ERRARG("client_name");
2743 return -1;
2744 } else if (!endpt_name) {
2745 ERRARG("endpt_name");
2746 return -1;
2747 } else if (!address) {
2748 ERRARG("address");
2749 return -1;
2750 }
2751
2752 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002753 endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client);
2754 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02002755 return -1;
2756 }
2757
Michal Vaskoadf30f02019-06-24 09:34:47 +02002758 lydict_remove(server_opts.ctx, endpt->address);
2759 endpt->address = lydict_insert(server_opts.ctx, address, 0);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002760
2761 /* UNLOCK */
2762 nc_server_ch_client_unlock(client);
2763
Michal Vaskoadf30f02019-06-24 09:34:47 +02002764 return 0;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002765}
2766
2767API int
2768nc_server_ch_client_endpt_set_port(const char *client_name, const char *endpt_name, uint16_t port)
2769{
Michal Vasko2e6defd2016-10-07 15:48:15 +02002770 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002771 struct nc_ch_endpt *endpt;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002772
2773 if (!client_name) {
2774 ERRARG("client_name");
2775 return -1;
2776 } else if (!endpt_name) {
2777 ERRARG("endpt_name");
2778 return -1;
2779 } else if (!port) {
2780 ERRARG("port");
2781 return -1;
2782 }
2783
2784 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002785 endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client);
2786 if (!endpt) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02002787 return -1;
2788 }
2789
Michal Vaskoadf30f02019-06-24 09:34:47 +02002790 endpt->port = port;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002791
2792 /* UNLOCK */
2793 nc_server_ch_client_unlock(client);
2794
ravsz5c5a4422020-03-31 15:53:21 +02002795 return 0;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002796}
2797
2798API int
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002799nc_server_ch_client_endpt_enable_keepalives(const char *client_name, const char *endpt_name, int enable)
2800{
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002801 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002802 struct nc_ch_endpt *endpt;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002803
2804 if (!client_name) {
2805 ERRARG("client_name");
2806 return -1;
2807 } else if (!endpt_name) {
2808 ERRARG("endpt_name");
2809 return -1;
2810 }
2811
2812 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002813 endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client);
2814 if (!endpt) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002815 return -1;
2816 }
2817
Michal Vaskoadf30f02019-06-24 09:34:47 +02002818 endpt->ka.enabled = (enable ? 1 : 0);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002819
2820 /* UNLOCK */
2821 nc_server_ch_client_unlock(client);
2822
Michal Vasko9af829a2019-09-12 13:50:00 +02002823 return 0;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002824}
2825
2826API int
2827nc_server_ch_client_endpt_set_keepalives(const char *client_name, const char *endpt_name, int idle_time, int max_probes,
2828 int probe_interval)
2829{
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002830 struct nc_ch_client *client;
Michal Vaskoadf30f02019-06-24 09:34:47 +02002831 struct nc_ch_endpt *endpt;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002832
2833 if (!client_name) {
2834 ERRARG("client_name");
2835 return -1;
2836 } else if (!endpt_name) {
2837 ERRARG("endpt_name");
2838 return -1;
2839 }
2840
2841 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002842 endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client);
2843 if (!endpt) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002844 return -1;
2845 }
2846
Michal Vaskoadf30f02019-06-24 09:34:47 +02002847 if (idle_time > -1) {
2848 endpt->ka.idle_time = idle_time;
2849 }
2850 if (max_probes > -1) {
2851 endpt->ka.max_probes = max_probes;
2852 }
2853 if (probe_interval > -1) {
2854 endpt->ka.probe_interval = probe_interval;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002855 }
2856
2857 /* UNLOCK */
2858 nc_server_ch_client_unlock(client);
2859
Michal Vasko9af829a2019-09-12 13:50:00 +02002860 return 0;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002861}
2862
2863API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02002864nc_server_ch_client_set_conn_type(const char *client_name, NC_CH_CONN_TYPE conn_type)
2865{
2866 struct nc_ch_client *client;
2867
2868 if (!client_name) {
2869 ERRARG("client_name");
2870 return -1;
2871 } else if (!conn_type) {
2872 ERRARG("conn_type");
2873 return -1;
2874 }
2875
2876 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002877 nc_server_ch_client_lock(client_name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002878 if (!client) {
2879 return -1;
2880 }
2881
2882 if (client->conn_type != conn_type) {
2883 client->conn_type = conn_type;
2884
2885 /* set default options */
2886 switch (conn_type) {
2887 case NC_CH_PERSIST:
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002888 /* no options */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002889 break;
2890 case NC_CH_PERIOD:
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002891 client->conn.period.period = 60;
2892 client->conn.period.anchor_time = 0;
2893 client->conn.period.idle_timeout = 120;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002894 break;
2895 default:
2896 ERRINT;
2897 break;
2898 }
2899 }
2900
2901 /* UNLOCK */
2902 nc_server_ch_client_unlock(client);
2903
2904 return 0;
2905}
2906
2907API int
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002908nc_server_ch_client_periodic_set_period(const char *client_name, uint16_t period)
2909{
2910 struct nc_ch_client *client;
2911
2912 if (!client_name) {
2913 ERRARG("client_name");
2914 return -1;
2915 } else if (!period) {
2916 ERRARG("period");
2917 return -1;
2918 }
2919
2920 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002921 nc_server_ch_client_lock(client_name, NULL, 0, &client);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002922 if (!client) {
2923 return -1;
2924 }
2925
2926 if (client->conn_type != NC_CH_PERIOD) {
ravsz20789e12020-03-31 14:43:05 +02002927 ERR("Call Home client \"%s\" is not of periodic connection type.", client_name);
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002928 /* UNLOCK */
2929 nc_server_ch_client_unlock(client);
2930 return -1;
2931 }
2932
2933 client->conn.period.period = period;
2934
2935 /* UNLOCK */
2936 nc_server_ch_client_unlock(client);
2937
2938 return 0;
2939}
2940
2941API int
2942nc_server_ch_client_periodic_set_anchor_time(const char *client_name, time_t anchor_time)
Michal Vasko2e6defd2016-10-07 15:48:15 +02002943{
2944 struct nc_ch_client *client;
2945
2946 if (!client_name) {
2947 ERRARG("client_name");
2948 return -1;
2949 }
2950
2951 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002952 nc_server_ch_client_lock(client_name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002953 if (!client) {
2954 return -1;
2955 }
2956
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002957 if (client->conn_type != NC_CH_PERIOD) {
ravsz20789e12020-03-31 14:43:05 +02002958 ERR("Call Home client \"%s\" is not of periodic connection type.", client_name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002959 /* UNLOCK */
2960 nc_server_ch_client_unlock(client);
2961 return -1;
2962 }
2963
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002964 client->conn.period.anchor_time = anchor_time;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002965
2966 /* UNLOCK */
2967 nc_server_ch_client_unlock(client);
2968
2969 return 0;
2970}
2971
2972API int
Michal Vaskoe49a15f2019-05-27 14:18:36 +02002973nc_server_ch_client_periodic_set_idle_timeout(const char *client_name, uint16_t idle_timeout)
Michal Vasko2e6defd2016-10-07 15:48:15 +02002974{
2975 struct nc_ch_client *client;
2976
2977 if (!client_name) {
2978 ERRARG("client_name");
2979 return -1;
2980 }
2981
2982 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02002983 nc_server_ch_client_lock(client_name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002984 if (!client) {
2985 return -1;
2986 }
2987
2988 if (client->conn_type != NC_CH_PERIOD) {
Darshanajk2d2bf4d2017-09-15 21:34:47 +10002989 ERR("Call Home client \"%s\" is not of periodic connection type.", client_name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002990 /* UNLOCK */
2991 nc_server_ch_client_unlock(client);
2992 return -1;
2993 }
2994
2995 client->conn.period.idle_timeout = idle_timeout;
2996
2997 /* UNLOCK */
2998 nc_server_ch_client_unlock(client);
2999
3000 return 0;
3001}
3002
3003API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02003004nc_server_ch_client_set_start_with(const char *client_name, NC_CH_START_WITH start_with)
3005{
3006 struct nc_ch_client *client;
3007
3008 if (!client_name) {
3009 ERRARG("client_name");
3010 return -1;
3011 }
3012
3013 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02003014 nc_server_ch_client_lock(client_name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003015 if (!client) {
3016 return -1;
3017 }
3018
3019 client->start_with = start_with;
3020
3021 /* UNLOCK */
3022 nc_server_ch_client_unlock(client);
3023
3024 return 0;
3025}
3026
3027API int
3028nc_server_ch_client_set_max_attempts(const char *client_name, uint8_t max_attempts)
3029{
3030 struct nc_ch_client *client;
3031
3032 if (!client_name) {
3033 ERRARG("client_name");
3034 return -1;
3035 } else if (!max_attempts) {
3036 ERRARG("max_attempts");
3037 return -1;
3038 }
3039
3040 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02003041 nc_server_ch_client_lock(client_name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003042 if (!client) {
3043 return -1;
3044 }
3045
3046 client->max_attempts = max_attempts;
3047
3048 /* UNLOCK */
3049 nc_server_ch_client_unlock(client);
3050
3051 return 0;
3052}
3053
3054/* client lock is expected to be held */
3055static NC_MSG_TYPE
Michal Vaskoadf30f02019-06-24 09:34:47 +02003056nc_connect_ch_endpt(struct nc_ch_endpt *endpt, struct nc_session **session)
Michal Vaskob05053d2016-01-22 16:12:06 +01003057{
Michal Vasko71090fc2016-05-24 16:37:28 +02003058 NC_MSG_TYPE msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01003059 int sock, ret;
Michal Vasko9fb42272017-10-05 13:50:05 +02003060 struct timespec ts_cur;
Michal Vasko66032bc2019-01-22 15:03:12 +01003061 char *ip_host;
Michal Vaskob05053d2016-01-22 16:12:06 +01003062
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003063 sock = nc_sock_connect(endpt->address, endpt->port, 5, &endpt->ka, &endpt->sock_pending, &ip_host);
Michal Vaskoc61c4492016-01-25 11:13:34 +01003064 if (sock < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02003065 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01003066 }
Frank Rimpler9f838b02018-07-25 06:44:03 +00003067 /* no need to store the socket as pending any longer */
3068 endpt->sock_pending = -1;
Michal Vaskob05053d2016-01-22 16:12:06 +01003069
Michal Vasko131120a2018-05-29 15:44:02 +02003070 *session = nc_new_session(NC_SERVER, 0);
Michal Vaskob05053d2016-01-22 16:12:06 +01003071 if (!(*session)) {
3072 ERRMEM;
3073 close(sock);
Michal Vasko66032bc2019-01-22 15:03:12 +01003074 free(ip_host);
Michal Vasko71090fc2016-05-24 16:37:28 +02003075 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01003076 }
3077 (*session)->status = NC_STATUS_STARTING;
Michal Vaskob05053d2016-01-22 16:12:06 +01003078 (*session)->ctx = server_opts.ctx;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02003079 (*session)->flags = NC_SESSION_SHAREDCTX;
Michal Vasko66032bc2019-01-22 15:03:12 +01003080 (*session)->host = lydict_insert_zc(server_opts.ctx, ip_host);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003081 (*session)->port = endpt->port;
Michal Vaskob05053d2016-01-22 16:12:06 +01003082
Michal Vaskob05053d2016-01-22 16:12:06 +01003083 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01003084#ifdef NC_ENABLED_SSH
Michal Vaskoadf30f02019-06-24 09:34:47 +02003085 if (endpt->ti == NC_TI_LIBSSH) {
3086 (*session)->data = endpt->opts.ssh;
Michal Vasko0190bc32016-03-02 15:47:49 +01003087 ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01003088 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01003089
Michal Vasko71090fc2016-05-24 16:37:28 +02003090 if (ret < 0) {
3091 msgtype = NC_MSG_ERROR;
3092 goto fail;
3093 } else if (!ret) {
3094 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01003095 goto fail;
3096 }
Michal Vasko3d865d22016-01-28 16:00:53 +01003097 } else
3098#endif
Radek Krejci53691be2016-02-22 13:58:37 +01003099#ifdef NC_ENABLED_TLS
Michal Vaskoadf30f02019-06-24 09:34:47 +02003100 if (endpt->ti == NC_TI_OPENSSL) {
3101 (*session)->data = endpt->opts.tls;
Michal Vasko0190bc32016-03-02 15:47:49 +01003102 ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01003103 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01003104
Michal Vasko71090fc2016-05-24 16:37:28 +02003105 if (ret < 0) {
3106 msgtype = NC_MSG_ERROR;
3107 goto fail;
3108 } else if (!ret) {
3109 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01003110 goto fail;
3111 }
Michal Vasko3d865d22016-01-28 16:00:53 +01003112 } else
3113#endif
3114 {
Michal Vaskob05053d2016-01-22 16:12:06 +01003115 ERRINT;
3116 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02003117 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01003118 goto fail;
3119 }
3120
3121 /* assign new SID atomically */
Michal Vasko69a3ff62018-11-09 09:31:48 +01003122 (*session)->id = ATOMIC_INC(&server_opts.new_session_id);
Michal Vaskob05053d2016-01-22 16:12:06 +01003123
3124 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +02003125 msgtype = nc_handshake_io(*session);
Michal Vasko71090fc2016-05-24 16:37:28 +02003126 if (msgtype != NC_MSG_HELLO) {
Michal Vaskob05053d2016-01-22 16:12:06 +01003127 goto fail;
3128 }
Michal Vasko9fb42272017-10-05 13:50:05 +02003129
3130 nc_gettimespec_mono(&ts_cur);
3131 (*session)->opts.server.last_rpc = ts_cur.tv_sec;
3132 nc_gettimespec_real(&ts_cur);
3133 (*session)->opts.server.session_start = ts_cur.tv_sec;
Michal Vaskob05053d2016-01-22 16:12:06 +01003134 (*session)->status = NC_STATUS_RUNNING;
3135
Michal Vasko71090fc2016-05-24 16:37:28 +02003136 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01003137
3138fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01003139 nc_session_free(*session, NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01003140 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02003141 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01003142}
3143
Michal Vasko2e6defd2016-10-07 15:48:15 +02003144struct nc_ch_client_thread_arg {
3145 char *client_name;
3146 void (*session_clb)(const char *client_name, struct nc_session *new_session);
3147};
3148
3149static struct nc_ch_client *
3150nc_server_ch_client_with_endpt_lock(const char *name)
3151{
3152 struct nc_ch_client *client;
3153
3154 while (1) {
3155 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02003156 nc_server_ch_client_lock(name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003157 if (!client) {
3158 return NULL;
3159 }
3160 if (client->ch_endpt_count) {
3161 return client;
3162 }
3163 /* no endpoints defined yet */
3164
3165 /* UNLOCK */
3166 nc_server_ch_client_unlock(client);
3167
3168 usleep(NC_CH_NO_ENDPT_WAIT * 1000);
3169 }
3170
3171 return NULL;
3172}
3173
3174static int
3175nc_server_ch_client_thread_session_cond_wait(struct nc_session *session, struct nc_ch_client_thread_arg *data)
3176{
Michal Vasko3f05a092018-03-13 10:39:49 +01003177 int ret = 0, r;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02003178 uint32_t idle_timeout;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003179 struct timespec ts;
3180 struct nc_ch_client *client;
3181
3182 /* session created, initialize condition */
Michal Vasko27377422018-03-15 08:59:35 +01003183 session->opts.server.ch_lock = calloc(1, sizeof *session->opts.server.ch_lock);
3184 session->opts.server.ch_cond = calloc(1, sizeof *session->opts.server.ch_cond);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003185 if (!session->opts.server.ch_lock || !session->opts.server.ch_cond) {
3186 ERRMEM;
3187 nc_session_free(session, NULL);
3188 return -1;
3189 }
3190 pthread_mutex_init(session->opts.server.ch_lock, NULL);
3191 pthread_cond_init(session->opts.server.ch_cond, NULL);
3192
Michal Vaskoc4bc5812016-10-13 10:59:36 +02003193 session->flags |= NC_SESSION_CALLHOME;
3194
Michal Vasko2e6defd2016-10-07 15:48:15 +02003195 /* CH LOCK */
3196 pthread_mutex_lock(session->opts.server.ch_lock);
3197
3198 /* give the session to the user */
3199 data->session_clb(data->client_name, session);
3200
3201 do {
Michal Vasko77a6abe2017-10-05 10:02:20 +02003202 nc_gettimespec_real(&ts);
Michal Vaskoe39ae6b2017-03-14 09:03:46 +01003203 nc_addtimespec(&ts, NC_CH_NO_ENDPT_WAIT);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003204
Michal Vasko3f05a092018-03-13 10:39:49 +01003205 r = pthread_cond_timedwait(session->opts.server.ch_cond, session->opts.server.ch_lock, &ts);
3206 if (!r) {
3207 /* we were woken up, something probably happened */
3208 if (session->status != NC_STATUS_RUNNING) {
3209 break;
3210 }
3211 } else if (r != ETIMEDOUT) {
3212 ERR("Pthread condition timedwait failed (%s).", strerror(r));
3213 ret = -1;
3214 break;
Michal Vasko2e39ed92018-03-12 13:51:44 +01003215 }
3216
Michal Vasko2e6defd2016-10-07 15:48:15 +02003217 /* check whether the client was not removed */
3218 /* LOCK */
Michal Vaskoadf30f02019-06-24 09:34:47 +02003219 nc_server_ch_client_lock(data->client_name, NULL, 0, &client);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003220 if (!client) {
3221 /* client was removed, finish thread */
3222 VRB("Call Home client \"%s\" removed, but an established session will not be terminated.",
Michal Vaskoadf30f02019-06-24 09:34:47 +02003223 data->client_name);
Michal Vasko3f05a092018-03-13 10:39:49 +01003224 ret = 1;
3225 break;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003226 }
3227
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003228 if (client->conn_type == NC_CH_PERIOD) {
Michal Vaskoc4bc5812016-10-13 10:59:36 +02003229 idle_timeout = client->conn.period.idle_timeout;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003230 } else {
3231 idle_timeout = 0;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02003232 }
3233
Michal Vasko9fb42272017-10-05 13:50:05 +02003234 nc_gettimespec_mono(&ts);
Michal Vasko3486a7c2017-03-03 13:28:07 +01003235 if (!session->opts.server.ntf_status && idle_timeout && (ts.tv_sec >= session->opts.server.last_rpc + idle_timeout)) {
Michal Vaskoc4bc5812016-10-13 10:59:36 +02003236 VRB("Call Home client \"%s\" session %u: session idle timeout elapsed.", client->name, session->id);
3237 session->status = NC_STATUS_INVALID;
3238 session->term_reason = NC_SESSION_TERM_TIMEOUT;
3239 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02003240
3241 /* UNLOCK */
3242 nc_server_ch_client_unlock(client);
3243
3244 } while (session->status == NC_STATUS_RUNNING);
3245
Michal Vasko27377422018-03-15 08:59:35 +01003246 /* CH UNLOCK */
3247 pthread_mutex_unlock(session->opts.server.ch_lock);
3248
Michal Vasko3f05a092018-03-13 10:39:49 +01003249 if (session->status == NC_STATUS_CLOSING) {
3250 /* signal to nc_session_free() that we registered session being freed, otherwise it matters not */
3251 session->flags &= ~NC_SESSION_CALLHOME;
3252 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +02003253
Michal Vasko3f05a092018-03-13 10:39:49 +01003254 return ret;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003255}
3256
3257static void *
3258nc_ch_client_thread(void *arg)
3259{
3260 struct nc_ch_client_thread_arg *data = (struct nc_ch_client_thread_arg *)arg;
3261 NC_MSG_TYPE msgtype;
3262 uint8_t cur_attempts = 0;
Peter Feiged05f2252018-09-03 08:09:47 +00003263 uint16_t next_endpt_index;
Michal Vasko9550cf12017-03-21 15:33:58 +01003264 char *cur_endpt_name = NULL;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003265 struct nc_ch_endpt *cur_endpt;
3266 struct nc_session *session;
3267 struct nc_ch_client *client;
Andrew Langefeld6ed922d2018-09-12 14:08:32 -05003268 uint32_t client_id;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003269 time_t reconnect_in;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003270
3271 /* LOCK */
3272 client = nc_server_ch_client_with_endpt_lock(data->client_name);
3273 if (!client) {
3274 goto cleanup;
3275 }
Andrew Langefeld6ed922d2018-09-12 14:08:32 -05003276 client_id = client->id;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003277
3278 cur_endpt = &client->ch_endpts[0];
3279 cur_endpt_name = strdup(cur_endpt->name);
3280
Michal Vasko29af44b2016-10-13 10:59:55 +02003281 VRB("Call Home client \"%s\" connecting...", data->client_name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003282 while (1) {
Michal Vaskoadf30f02019-06-24 09:34:47 +02003283 msgtype = nc_connect_ch_endpt(cur_endpt, &session);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003284
3285 if (msgtype == NC_MSG_HELLO) {
3286 /* UNLOCK */
3287 nc_server_ch_client_unlock(client);
3288
Michal Vasko29af44b2016-10-13 10:59:55 +02003289 VRB("Call Home client \"%s\" session %u established.", data->client_name, session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003290 if (nc_server_ch_client_thread_session_cond_wait(session, data)) {
3291 goto cleanup;
3292 }
Michal Vasko4ec210b2020-04-16 09:32:01 +02003293 VRB("Call Home client \"%s\" session terminated, reconnecting...", data->client_name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003294
3295 /* LOCK */
3296 client = nc_server_ch_client_with_endpt_lock(data->client_name);
3297 if (!client) {
3298 goto cleanup;
3299 }
Andrew Langefeld6ed922d2018-09-12 14:08:32 -05003300 if (client->id != client_id) {
3301 nc_server_ch_client_unlock(client);
3302 goto cleanup;
3303 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02003304
3305 /* session changed status -> it was disconnected for whatever reason,
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003306 * persistent connection immediately tries to reconnect, periodic connects at specific times */
Michal Vasko2e6defd2016-10-07 15:48:15 +02003307 if (client->conn_type == NC_CH_PERIOD) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02003308 /* UNLOCK */
3309 nc_server_ch_client_unlock(client);
3310
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003311 /* sleep until we should reconnect TODO wake up sometimes to check for new notifications */
3312 reconnect_in = (time(NULL) - client->conn.period.anchor_time) % (client->conn.period.period * 60);
3313 sleep(reconnect_in);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003314
3315 /* LOCK */
3316 client = nc_server_ch_client_with_endpt_lock(data->client_name);
3317 if (!client) {
3318 goto cleanup;
3319 }
Andrew Langefeld6ed922d2018-09-12 14:08:32 -05003320 if (client->id != client_id) {
3321 nc_server_ch_client_unlock(client);
3322 goto cleanup;
3323 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02003324 }
3325
3326 /* set next endpoint to try */
3327 if (client->start_with == NC_CH_FIRST_LISTED) {
Peter Feiged05f2252018-09-03 08:09:47 +00003328 next_endpt_index = 0;
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003329 } else if (client->start_with == NC_CH_LAST_CONNECTED) {
Peter Feiged05f2252018-09-03 08:09:47 +00003330 /* we keep the current one but due to unlock/lock we have to find it again */
3331 for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) {
3332 if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) {
3333 break;
3334 }
3335 }
3336 if (next_endpt_index >= client->ch_endpt_count) {
3337 /* endpoint was removed, start with the first one */
3338 next_endpt_index = 0;
3339 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +02003340 } else {
3341 /* just get a random index */
3342 next_endpt_index = rand() % client->ch_endpt_count;
Peter Feiged05f2252018-09-03 08:09:47 +00003343 }
3344
Michal Vasko2e6defd2016-10-07 15:48:15 +02003345 } else {
Michal Vasko6bb116b2016-10-26 13:53:46 +02003346 /* UNLOCK */
3347 nc_server_ch_client_unlock(client);
3348
Michal Vasko2e6defd2016-10-07 15:48:15 +02003349 /* session was not created */
Michal Vaskoc4bc5812016-10-13 10:59:36 +02003350 usleep(NC_CH_ENDPT_FAIL_WAIT * 1000);
3351
Michal Vasko6bb116b2016-10-26 13:53:46 +02003352 /* LOCK */
3353 client = nc_server_ch_client_with_endpt_lock(data->client_name);
3354 if (!client) {
3355 goto cleanup;
3356 }
Andrew Langefeld6ed922d2018-09-12 14:08:32 -05003357 if (client->id != client_id) {
3358 nc_server_ch_client_unlock(client);
3359 goto cleanup;
3360 }
Michal Vasko6bb116b2016-10-26 13:53:46 +02003361
Michal Vasko2e6defd2016-10-07 15:48:15 +02003362 ++cur_attempts;
Michal Vasko4cb8de52018-04-23 14:38:07 +02003363
3364 /* try to find our endpoint again */
Peter Feiged05f2252018-09-03 08:09:47 +00003365 for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) {
3366 if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) {
Michal Vasko4cb8de52018-04-23 14:38:07 +02003367 break;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003368 }
Michal Vasko4cb8de52018-04-23 14:38:07 +02003369 }
3370
Peter Feiged05f2252018-09-03 08:09:47 +00003371 if (next_endpt_index >= client->ch_endpt_count) {
Michal Vasko4cb8de52018-04-23 14:38:07 +02003372 /* endpoint was removed, start with the first one */
Peter Feiged05f2252018-09-03 08:09:47 +00003373 next_endpt_index = 0;
Michal Vasko4cb8de52018-04-23 14:38:07 +02003374 cur_attempts = 0;
3375 } else if (cur_attempts == client->max_attempts) {
3376 /* we have tried to connect to this endpoint enough times */
Peter Feiged05f2252018-09-03 08:09:47 +00003377 if (next_endpt_index < client->ch_endpt_count - 1) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02003378 /* just go to the next endpoint */
Peter Feiged05f2252018-09-03 08:09:47 +00003379 ++next_endpt_index;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003380 } else {
Michal Vasko4cb8de52018-04-23 14:38:07 +02003381 /* cur_endpoint is the last, start with the first one */
Michal Vasko2a225342018-09-05 08:38:34 +02003382 next_endpt_index = 0;
Michal Vasko2e6defd2016-10-07 15:48:15 +02003383 }
3384
3385 cur_attempts = 0;
3386 } /* else we keep the current one */
3387 }
Peter Feiged05f2252018-09-03 08:09:47 +00003388
3389 cur_endpt = &client->ch_endpts[next_endpt_index];
3390 free(cur_endpt_name);
3391 cur_endpt_name = strdup(cur_endpt->name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003392 }
3393
3394cleanup:
3395 VRB("Call Home client \"%s\" thread exit.", data->client_name);
Michal Vasko9550cf12017-03-21 15:33:58 +01003396 free(cur_endpt_name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02003397 free(data->client_name);
3398 free(data);
3399 return NULL;
3400}
3401
3402API int
Michal Vaskoadf30f02019-06-24 09:34:47 +02003403nc_connect_ch_client_dispatch(const char *client_name, void (*session_clb)(const char *client_name,
3404 struct nc_session *new_session))
Michal Vasko3f05a092018-03-13 10:39:49 +01003405{
Michal Vasko2e6defd2016-10-07 15:48:15 +02003406 int ret;
3407 pthread_t tid;
3408 struct nc_ch_client_thread_arg *arg;
3409
3410 if (!client_name) {
3411 ERRARG("client_name");
3412 return -1;
3413 } else if (!session_clb) {
3414 ERRARG("session_clb");
3415 return -1;
3416 }
3417
3418 arg = malloc(sizeof *arg);
3419 if (!arg) {
3420 ERRMEM;
3421 return -1;
3422 }
3423 arg->client_name = strdup(client_name);
3424 if (!arg->client_name) {
3425 ERRMEM;
3426 free(arg);
3427 return -1;
3428 }
3429 arg->session_clb = session_clb;
3430
3431 ret = pthread_create(&tid, NULL, nc_ch_client_thread, arg);
3432 if (ret) {
3433 ERR("Creating a new thread failed (%s).", strerror(ret));
3434 free(arg->client_name);
3435 free(arg);
3436 return -1;
3437 }
3438 /* the thread now manages arg */
3439
3440 pthread_detach(tid);
3441
3442 return 0;
3443}
3444
Radek Krejci53691be2016-02-22 13:58:37 +01003445#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskof8352352016-05-24 09:11:36 +02003446
Michal Vaskoc45ebd32016-05-25 11:17:36 +02003447API time_t
3448nc_session_get_start_time(const struct nc_session *session)
Michal Vaskof8352352016-05-24 09:11:36 +02003449{
Michal Vasko2e6defd2016-10-07 15:48:15 +02003450 if (!session || (session->side != NC_SERVER)) {
Michal Vaskof8352352016-05-24 09:11:36 +02003451 ERRARG("session");
Michal Vaskoc45ebd32016-05-25 11:17:36 +02003452 return 0;
Michal Vaskof8352352016-05-24 09:11:36 +02003453 }
3454
Michal Vasko2e6defd2016-10-07 15:48:15 +02003455 return session->opts.server.session_start;
Michal Vaskof8352352016-05-24 09:11:36 +02003456}
Michal Vasko3486a7c2017-03-03 13:28:07 +01003457
3458API void
3459nc_session_set_notif_status(struct nc_session *session, int notif_status)
3460{
3461 if (!session || (session->side != NC_SERVER)) {
3462 ERRARG("session");
3463 return;
3464 }
3465
3466 session->opts.server.ntf_status = (notif_status ? 1 : 0);
3467}
3468
3469API int
3470nc_session_get_notif_status(const struct nc_session *session)
3471{
3472 if (!session || (session->side != NC_SERVER)) {
3473 ERRARG("session");
3474 return 0;
3475 }
3476
3477 return session->opts.server.ntf_status;
Michal Vasko086311b2016-01-08 09:53:11 +01003478}
Michal Vasko8f430592019-02-26 08:32:54 +01003479
3480API int
3481nc_session_is_callhome(const struct nc_session *session)
3482{
3483 if (!session || (session->side != NC_SERVER)) {
3484 ERRARG("session");
3485 return 0;
3486 }
3487
3488 if (session->flags & NC_SESSION_CALLHOME) {
3489 return 1;
3490 }
3491
3492 return 0;
3493}