blob: b69783f6fbf9711f2351c94eb047e9e32ff050ce [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 */
Michal Vaskoade892d2017-02-22 13:40:35 +010014#define _POSIX_SOUCE /* signals */
Michal Vasko086311b2016-01-08 09:53:11 +010015
16#include <stdint.h>
17#include <stdlib.h>
18#include <errno.h>
19#include <string.h>
20#include <poll.h>
21#include <sys/types.h>
22#include <sys/socket.h>
23#include <netinet/in.h>
24#include <arpa/inet.h>
25#include <unistd.h>
Michal Vasko0190bc32016-03-02 15:47:49 +010026#include <fcntl.h>
Michal Vaskob48aa812016-01-18 14:13:09 +010027#include <pthread.h>
Michal Vasko11d142a2016-01-19 15:58:24 +010028#include <time.h>
Michal Vaskoade892d2017-02-22 13:40:35 +010029#include <signal.h>
Michal Vasko086311b2016-01-08 09:53:11 +010030
Michal Vasko1a38c862016-01-15 15:50:07 +010031#include "libnetconf.h"
Michal Vasko086311b2016-01-08 09:53:11 +010032#include "session_server.h"
33
Michal Vaskob48aa812016-01-18 14:13:09 +010034struct nc_server_opts server_opts = {
Michal Vaskoade892d2017-02-22 13:40:35 +010035#ifdef NC_ENABLED_SSH
36 .authkey_lock = PTHREAD_MUTEX_INITIALIZER,
37#endif
38 .bind_lock = PTHREAD_MUTEX_INITIALIZER,
Michal Vasko2e6defd2016-10-07 15:48:15 +020039 .endpt_lock = PTHREAD_RWLOCK_INITIALIZER,
40 .ch_client_lock = PTHREAD_RWLOCK_INITIALIZER
Michal Vaskob48aa812016-01-18 14:13:09 +010041};
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010042
fanchanghu966f2de2016-07-21 02:28:57 -040043static nc_rpc_clb global_rpc_clb = NULL;
44
Michal Vasko3031aae2016-01-27 16:07:18 +010045struct nc_endpt *
Michal Vaskoade892d2017-02-22 13:40:35 +010046nc_server_endpt_lock_get(const char *name, NC_TRANSPORT_IMPL ti, uint16_t *idx)
Michal Vasko3031aae2016-01-27 16:07:18 +010047{
48 uint16_t i;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010049 struct nc_endpt *endpt = NULL;
50
Michal Vaskoade892d2017-02-22 13:40:35 +010051 /* WRITE LOCK */
52 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010053
54 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vasko2e6defd2016-10-07 15:48:15 +020055 if (!strcmp(server_opts.endpts[i].name, name) && (!ti || (server_opts.endpts[i].ti == ti))) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010056 endpt = &server_opts.endpts[i];
57 break;
Michal Vasko3031aae2016-01-27 16:07:18 +010058 }
59 }
60
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010061 if (!endpt) {
62 ERR("Endpoint \"%s\" was not found.", name);
Michal Vaskoade892d2017-02-22 13:40:35 +010063 /* UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +020064 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010065 return NULL;
66 }
67
Michal Vaskoe2713da2016-08-22 16:06:40 +020068 if (idx) {
69 *idx = i;
70 }
71
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010072 return endpt;
73}
74
Michal Vasko2e6defd2016-10-07 15:48:15 +020075struct nc_ch_client *
76nc_server_ch_client_lock(const char *name, NC_TRANSPORT_IMPL ti, uint16_t *idx)
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010077{
Michal Vasko2e6defd2016-10-07 15:48:15 +020078 uint16_t i;
79 struct nc_ch_client *client = NULL;
80
81 /* READ LOCK */
82 pthread_rwlock_rdlock(&server_opts.ch_client_lock);
83
84 for (i = 0; i < server_opts.ch_client_count; ++i) {
85 if (!strcmp(server_opts.ch_clients[i].name, name) && (!ti || (server_opts.ch_clients[i].ti == ti))) {
86 client = &server_opts.ch_clients[i];
87 break;
88 }
89 }
90
91 if (!client) {
92 ERR("Call Home client \"%s\" was not found.", name);
93 /* READ UNLOCK */
94 pthread_rwlock_unlock(&server_opts.ch_client_lock);
95 return NULL;
96 }
97
98 /* CH CLIENT LOCK */
99 pthread_mutex_lock(&client->lock);
100
101 if (idx) {
102 *idx = i;
103 }
104
105 return client;
106}
107
108void
109nc_server_ch_client_unlock(struct nc_ch_client *client)
110{
111 /* CH CLIENT UNLOCK */
112 pthread_mutex_unlock(&client->lock);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100113
114 /* READ UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200115 pthread_rwlock_unlock(&server_opts.ch_client_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100116}
Michal Vasko086311b2016-01-08 09:53:11 +0100117
Michal Vasko1a38c862016-01-15 15:50:07 +0100118API void
119nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason)
120{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200121 if (!session) {
122 ERRARG("session");
123 return;
124 } else if (!reason) {
125 ERRARG("reason");
Michal Vasko1a38c862016-01-15 15:50:07 +0100126 return;
127 }
128
129 session->term_reason = reason;
130}
131
Michal Vasko086311b2016-01-08 09:53:11 +0100132int
Michal Vaskof05562c2016-01-20 12:06:43 +0100133nc_sock_listen(const char *address, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100134{
135 const int optVal = 1;
136 const socklen_t optLen = sizeof(optVal);
137 int is_ipv4, sock;
138 struct sockaddr_storage saddr;
139
140 struct sockaddr_in *saddr4;
141 struct sockaddr_in6 *saddr6;
142
143
144 if (!strchr(address, ':')) {
145 is_ipv4 = 1;
146 } else {
147 is_ipv4 = 0;
148 }
149
150 sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0);
151 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100152 ERR("Failed to create socket (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100153 goto fail;
154 }
155
156 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, optLen)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100157 ERR("Could not set socket SO_REUSEADDR socket option (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100158 goto fail;
159 }
160
161 bzero(&saddr, sizeof(struct sockaddr_storage));
162 if (is_ipv4) {
163 saddr4 = (struct sockaddr_in *)&saddr;
164
165 saddr4->sin_family = AF_INET;
166 saddr4->sin_port = htons(port);
167
168 if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100169 ERR("Failed to convert IPv4 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100170 goto fail;
171 }
172
173 if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100174 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100175 goto fail;
176 }
177
178 } else {
179 saddr6 = (struct sockaddr_in6 *)&saddr;
180
181 saddr6->sin6_family = AF_INET6;
182 saddr6->sin6_port = htons(port);
183
184 if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100185 ERR("Failed to convert IPv6 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100186 goto fail;
187 }
188
189 if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100190 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100191 goto fail;
192 }
193 }
194
Michal Vaskofb89d772016-01-08 12:25:35 +0100195 if (listen(sock, NC_REVERSE_QUEUE) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100196 ERR("Unable to start listening on \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100197 goto fail;
198 }
199
200 return sock;
201
202fail:
203 if (sock > -1) {
204 close(sock);
205 }
206
207 return -1;
208}
209
210int
Michal Vasko3031aae2016-01-27 16:07:18 +0100211nc_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 +0100212{
Michal Vaskof54cd352017-02-22 13:42:02 +0100213 sigset_t sigmask, origmask;
Michal Vaskoac2f6182017-01-30 14:32:03 +0100214 uint16_t i, j, pfd_count;
Michal Vasko086311b2016-01-08 09:53:11 +0100215 struct pollfd *pfd;
216 struct sockaddr_storage saddr;
217 socklen_t saddr_len = sizeof(saddr);
Michal Vasko0190bc32016-03-02 15:47:49 +0100218 int ret, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100219
220 pfd = malloc(bind_count * sizeof *pfd);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100221 if (!pfd) {
222 ERRMEM;
223 return -1;
224 }
225
Michal Vaskoac2f6182017-01-30 14:32:03 +0100226 for (i = 0, pfd_count = 0; i < bind_count; ++i) {
Michal Vasko94acafc2016-09-23 13:40:10 +0200227 if (binds[i].sock < 0) {
228 /* invalid socket */
Michal Vasko94acafc2016-09-23 13:40:10 +0200229 continue;
230 }
Michal Vasko0a3f3752016-10-13 14:58:38 +0200231 if (binds[i].pollin) {
232 binds[i].pollin = 0;
233 /* leftover pollin */
234 sock = binds[i].sock;
Michal Vasko086311b2016-01-08 09:53:11 +0100235 break;
236 }
Michal Vaskoac2f6182017-01-30 14:32:03 +0100237 pfd[pfd_count].fd = binds[i].sock;
238 pfd[pfd_count].events = POLLIN;
239 pfd[pfd_count].revents = 0;
240
241 ++pfd_count;
Michal Vasko086311b2016-01-08 09:53:11 +0100242 }
243
Michal Vasko0a3f3752016-10-13 14:58:38 +0200244 if (sock == -1) {
245 /* poll for a new connection */
Michal Vaskof54cd352017-02-22 13:42:02 +0100246 sigfillset(&sigmask);
247 pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
Michal Vaskoac2f6182017-01-30 14:32:03 +0100248 ret = poll(pfd, pfd_count, timeout);
Michal Vaskof54cd352017-02-22 13:42:02 +0100249 pthread_sigmask(SIG_SETMASK, &origmask, NULL);
250
Michal Vasko0a3f3752016-10-13 14:58:38 +0200251 if (!ret) {
252 /* we timeouted */
253 free(pfd);
254 return 0;
255 } else if (ret == -1) {
256 ERR("Poll failed (%s).", strerror(errno));
257 free(pfd);
258 return -1;
259 }
Michal Vasko086311b2016-01-08 09:53:11 +0100260
Michal Vaskoac2f6182017-01-30 14:32:03 +0100261 for (i = 0, j = 0; j < pfd_count; ++i, ++j) {
262 /* adjust i so that indices in binds and pfd always match */
263 while (binds[i].sock != pfd[j].fd) {
264 ++i;
265 }
266
267 if (pfd[j].revents & POLLIN) {
Michal Vasko0a3f3752016-10-13 14:58:38 +0200268 --ret;
269
270 if (!ret) {
271 /* the last socket with an event, use it */
Michal Vaskoac2f6182017-01-30 14:32:03 +0100272 sock = pfd[j].fd;
Michal Vasko0a3f3752016-10-13 14:58:38 +0200273 break;
274 } else {
275 /* just remember the event for next time */
276 binds[i].pollin = 1;
277 }
278 }
Michal Vasko086311b2016-01-08 09:53:11 +0100279 }
280 }
281 free(pfd);
282
283 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100284 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100285 return -1;
286 }
287
288 ret = accept(sock, (struct sockaddr *)&saddr, &saddr_len);
Michal Vasko3f6cc4a2016-01-21 15:58:53 +0100289 if (ret < 0) {
Michal Vaskod083db62016-01-19 10:31:29 +0100290 ERR("Accept failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100291 return -1;
292 }
Michal Vasko6ccb29d2016-10-13 15:00:27 +0200293 VRB("Accepted a connection on %s:%u.", binds[i].address, binds[i].port);
Michal Vasko086311b2016-01-08 09:53:11 +0100294
Michal Vasko0190bc32016-03-02 15:47:49 +0100295 /* make the socket non-blocking */
296 if (((flags = fcntl(ret, F_GETFL)) == -1) || (fcntl(ret, F_SETFL, flags | O_NONBLOCK) == -1)) {
297 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100298 close(ret);
Michal Vasko0190bc32016-03-02 15:47:49 +0100299 return -1;
300 }
301
Michal Vasko3031aae2016-01-27 16:07:18 +0100302 if (idx) {
303 *idx = i;
Michal Vasko9e036d52016-01-08 10:49:26 +0100304 }
305
Michal Vasko086311b2016-01-08 09:53:11 +0100306 /* host was requested */
307 if (host) {
308 if (saddr.ss_family == AF_INET) {
309 *host = malloc(15);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100310 if (*host) {
311 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&saddr)->sin_addr.s_addr, *host, 15)) {
312 ERR("inet_ntop failed (%s).", strerror(errno));
313 free(*host);
314 *host = NULL;
315 }
Michal Vasko086311b2016-01-08 09:53:11 +0100316
Michal Vasko4eb3c312016-03-01 14:09:37 +0100317 if (port) {
318 *port = ntohs(((struct sockaddr_in *)&saddr)->sin_port);
319 }
320 } else {
321 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100322 }
323 } else if (saddr.ss_family == AF_INET6) {
324 *host = malloc(40);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100325 if (*host) {
326 if (!inet_ntop(AF_INET6, ((struct sockaddr_in6 *)&saddr)->sin6_addr.s6_addr, *host, 40)) {
327 ERR("inet_ntop failed (%s).", strerror(errno));
328 free(*host);
329 *host = NULL;
330 }
Michal Vasko086311b2016-01-08 09:53:11 +0100331
Michal Vasko4eb3c312016-03-01 14:09:37 +0100332 if (port) {
333 *port = ntohs(((struct sockaddr_in6 *)&saddr)->sin6_port);
334 }
335 } else {
336 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100337 }
338 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100339 ERR("Source host of an unknown protocol family.");
Michal Vasko086311b2016-01-08 09:53:11 +0100340 }
341 }
342
343 return ret;
344}
345
Michal Vasko05ba9df2016-01-13 14:40:27 +0100346static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100347nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *UNUSED(session))
Michal Vasko05ba9df2016-01-13 14:40:27 +0100348{
349 const char *identifier = NULL, *version = NULL, *format = NULL;
350 char *model_data = NULL;
351 const struct lys_module *module;
352 struct nc_server_error *err;
353 struct lyd_node *child, *data = NULL;
Michal Vasko11d142a2016-01-19 15:58:24 +0100354 const struct lys_node *sdata = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100355
356 LY_TREE_FOR(rpc->child, child) {
357 if (!strcmp(child->schema->name, "identifier")) {
358 identifier = ((struct lyd_node_leaf_list *)child)->value_str;
359 } else if (!strcmp(child->schema->name, "version")) {
360 version = ((struct lyd_node_leaf_list *)child)->value_str;
Radek Krejci1afa7792017-03-26 11:24:16 -0500361 if (version && version[0] == '\0') {
362 version = NULL;
363 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100364 } else if (!strcmp(child->schema->name, "format")) {
365 format = ((struct lyd_node_leaf_list *)child)->value_str;
366 }
367 }
368
369 /* check version */
370 if (version && (strlen(version) != 10) && strcmp(version, "1.0")) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100371 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
372 nc_err_set_msg(err, "The requested version is not supported.", "en");
373 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100374 }
375
376 /* check and get module with the name identifier */
377 module = ly_ctx_get_module(server_opts.ctx, identifier, version);
378 if (!module) {
Michal Vaskod91f6e62016-04-05 11:34:22 +0200379 module = (const struct lys_module *)ly_ctx_get_submodule(server_opts.ctx, NULL, NULL, identifier, version);
380 }
381 if (!module) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100382 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
383 nc_err_set_msg(err, "The requested schema was not found.", "en");
384 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100385 }
386
387 /* check format */
Radek Krejci90fba642016-12-07 15:59:45 +0100388 if (!format || !strcmp(format, "ietf-netconf-monitoring:yang")) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100389 lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL);
Radek Krejci90fba642016-12-07 15:59:45 +0100390 } else if (!strcmp(format, "ietf-netconf-monitoring:yin")) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100391 lys_print_mem(&model_data, module, LYS_OUT_YIN, NULL);
392 } else {
Michal Vasko1a38c862016-01-15 15:50:07 +0100393 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
394 nc_err_set_msg(err, "The requested format is not supported.", "en");
395 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100396 }
Michal Vaskod91f6e62016-04-05 11:34:22 +0200397 if (!model_data) {
398 ERRINT;
399 return NULL;
400 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100401
Michal Vasko303245c2016-03-24 15:20:03 +0100402 sdata = ly_ctx_get_node(server_opts.ctx, NULL, "/ietf-netconf-monitoring:get-schema/output/data");
Michal Vaskod91f6e62016-04-05 11:34:22 +0200403 if (!sdata) {
404 ERRINT;
405 free(model_data);
406 return NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100407 }
Michal Vaskod91f6e62016-04-05 11:34:22 +0200408
Radek Krejci539efb62016-08-24 15:05:16 +0200409 data = lyd_new_path(NULL, server_opts.ctx, "/ietf-netconf-monitoring:get-schema/data", model_data,
410 LYD_ANYDATA_STRING, LYD_PATH_OPT_OUTPUT);
Michal Vasko3cb0b132017-01-03 14:59:51 +0100411 if (!data || lyd_validate(&data, LYD_OPT_RPCREPLY, NULL)) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100412 ERRINT;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200413 free(model_data);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100414 return NULL;
415 }
416
Radek Krejci36dfdb32016-09-01 16:56:35 +0200417 return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100418}
419
420static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100421nc_clb_default_close_session(struct lyd_node *UNUSED(rpc), struct nc_session *session)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100422{
Michal Vasko428087d2016-01-14 16:04:28 +0100423 session->term_reason = NC_SESSION_TERM_CLOSED;
424 return nc_server_reply_ok();
Michal Vasko05ba9df2016-01-13 14:40:27 +0100425}
426
Michal Vasko086311b2016-01-08 09:53:11 +0100427API int
428nc_server_init(struct ly_ctx *ctx)
429{
Michal Vasko05ba9df2016-01-13 14:40:27 +0100430 const struct lys_node *rpc;
431
Michal Vasko086311b2016-01-08 09:53:11 +0100432 if (!ctx) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200433 ERRARG("ctx");
Michal Vasko086311b2016-01-08 09:53:11 +0100434 return -1;
435 }
436
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100437 nc_init();
438
Michal Vasko05ba9df2016-01-13 14:40:27 +0100439 /* set default <get-schema> callback if not specified */
Michal Vasko303245c2016-03-24 15:20:03 +0100440 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema");
Michal Vaskofd100c92016-03-01 15:23:46 +0100441 if (rpc && !rpc->priv) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100442 lys_set_private(rpc, nc_clb_default_get_schema);
443 }
444
445 /* set default <close-session> callback if not specififed */
Michal Vasko303245c2016-03-24 15:20:03 +0100446 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf:close-session");
Michal Vaskofd100c92016-03-01 15:23:46 +0100447 if (rpc && !rpc->priv) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100448 lys_set_private(rpc, nc_clb_default_close_session);
449 }
450
Michal Vasko086311b2016-01-08 09:53:11 +0100451 server_opts.ctx = ctx;
Michal Vaskob48aa812016-01-18 14:13:09 +0100452
453 server_opts.new_session_id = 1;
454 pthread_spin_init(&server_opts.sid_lock, PTHREAD_PROCESS_PRIVATE);
455
Michal Vasko086311b2016-01-08 09:53:11 +0100456 return 0;
457}
458
Michal Vaskob48aa812016-01-18 14:13:09 +0100459API void
460nc_server_destroy(void)
461{
Radek Krejci658782b2016-12-04 22:04:55 +0100462 unsigned int i;
463
464 for (i = 0; i < server_opts.capabilities_count; i++) {
465 lydict_remove(server_opts.ctx, server_opts.capabilities[i]);
466 }
467 free(server_opts.capabilities);
Michal Vaskob48aa812016-01-18 14:13:09 +0100468 pthread_spin_destroy(&server_opts.sid_lock);
469
Radek Krejci53691be2016-02-22 13:58:37 +0100470#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko59050372016-11-22 14:33:55 +0100471 nc_server_del_endpt(NULL, 0);
Michal Vaskob48aa812016-01-18 14:13:09 +0100472#endif
Michal Vasko17dfda92016-12-01 14:06:16 +0100473#ifdef NC_ENABLED_SSH
474 nc_server_ssh_del_authkey(NULL, NULL, 0, NULL);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100475
476 if (server_opts.hostkey_data && server_opts.hostkey_data_free) {
477 server_opts.hostkey_data_free(server_opts.hostkey_data);
478 }
479#endif
480#ifdef NC_ENABLED_TLS
481 if (server_opts.server_cert_data && server_opts.server_cert_data_free) {
482 server_opts.server_cert_data_free(server_opts.server_cert_data);
483 }
484 if (server_opts.trusted_cert_list_data && server_opts.trusted_cert_list_data_free) {
485 server_opts.trusted_cert_list_data_free(server_opts.trusted_cert_list_data);
486 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100487#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100488 nc_destroy();
Michal Vaskob48aa812016-01-18 14:13:09 +0100489}
490
Michal Vasko086311b2016-01-08 09:53:11 +0100491API int
492nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported)
493{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200494 if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)) {
495 ERRARG("basic_mode");
496 return -1;
497 } else if (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT))) {
498 ERRARG("also_supported");
Michal Vasko086311b2016-01-08 09:53:11 +0100499 return -1;
500 }
501
502 server_opts.wd_basic_mode = basic_mode;
503 server_opts.wd_also_supported = also_supported;
504 return 0;
505}
506
Michal Vasko1a38c862016-01-15 15:50:07 +0100507API void
Michal Vasko55f03972016-04-13 08:56:01 +0200508nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported)
509{
510 if (!basic_mode && !also_supported) {
511 ERRARG("basic_mode and also_supported");
512 return;
513 }
514
515 if (basic_mode) {
516 *basic_mode = server_opts.wd_basic_mode;
517 }
518 if (also_supported) {
519 *also_supported = server_opts.wd_also_supported;
520 }
521}
522
Michal Vasko55f03972016-04-13 08:56:01 +0200523API int
Radek Krejci658782b2016-12-04 22:04:55 +0100524nc_server_set_capability(const char *value)
Michal Vasko55f03972016-04-13 08:56:01 +0200525{
Radek Krejci658782b2016-12-04 22:04:55 +0100526 const char **new;
527
528 if (!value || !value[0]) {
529 ERRARG("value must not be empty");
530 return EXIT_FAILURE;
531 }
532
533 server_opts.capabilities_count++;
534 new = realloc(server_opts.capabilities, server_opts.capabilities_count * sizeof *server_opts.capabilities);
535 if (!new) {
536 ERRMEM;
537 return EXIT_FAILURE;
538 }
539 server_opts.capabilities = new;
540 server_opts.capabilities[server_opts.capabilities_count - 1] = lydict_insert(server_opts.ctx, value, 0);
541
542 return EXIT_SUCCESS;
Michal Vasko55f03972016-04-13 08:56:01 +0200543}
544
Michal Vasko1a38c862016-01-15 15:50:07 +0100545API void
Michal Vasko086311b2016-01-08 09:53:11 +0100546nc_server_set_hello_timeout(uint16_t hello_timeout)
547{
Michal Vasko086311b2016-01-08 09:53:11 +0100548 server_opts.hello_timeout = hello_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100549}
550
Michal Vasko55f03972016-04-13 08:56:01 +0200551API uint16_t
552nc_server_get_hello_timeout(void)
553{
554 return server_opts.hello_timeout;
555}
556
Michal Vasko1a38c862016-01-15 15:50:07 +0100557API void
Michal Vasko086311b2016-01-08 09:53:11 +0100558nc_server_set_idle_timeout(uint16_t idle_timeout)
559{
Michal Vasko086311b2016-01-08 09:53:11 +0100560 server_opts.idle_timeout = idle_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100561}
562
Michal Vasko55f03972016-04-13 08:56:01 +0200563API uint16_t
564nc_server_get_idle_timeout(void)
565{
566 return server_opts.idle_timeout;
567}
568
Michal Vasko71090fc2016-05-24 16:37:28 +0200569API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +0100570nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +0100571{
Michal Vasko71090fc2016-05-24 16:37:28 +0200572 NC_MSG_TYPE msgtype;
573
Michal Vasko45e53ae2016-04-07 11:46:03 +0200574 if (!server_opts.ctx) {
575 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +0200576 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200577 } else if (fdin < 0) {
578 ERRARG("fdin");
Michal Vasko71090fc2016-05-24 16:37:28 +0200579 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200580 } else if (fdout < 0) {
581 ERRARG("fdout");
Michal Vasko71090fc2016-05-24 16:37:28 +0200582 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200583 } else if (!username) {
584 ERRARG("username");
Michal Vasko71090fc2016-05-24 16:37:28 +0200585 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200586 } else if (!session) {
587 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200588 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100589 }
590
591 /* prepare session structure */
Michal Vaskoade892d2017-02-22 13:40:35 +0100592 *session = nc_new_session(0);
Michal Vasko1a38c862016-01-15 15:50:07 +0100593 if (!(*session)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100594 ERRMEM;
Michal Vasko71090fc2016-05-24 16:37:28 +0200595 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100596 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100597 (*session)->status = NC_STATUS_STARTING;
598 (*session)->side = NC_SERVER;
Michal Vasko086311b2016-01-08 09:53:11 +0100599
Michal Vaskoade892d2017-02-22 13:40:35 +0100600 /* transport lock */
601 pthread_mutex_init((*session)->ti_lock, NULL);
602 pthread_cond_init((*session)->ti_cond, NULL);
603 *(*session)->ti_inuse = 0;
604
Michal Vasko086311b2016-01-08 09:53:11 +0100605 /* transport specific data */
Michal Vasko1a38c862016-01-15 15:50:07 +0100606 (*session)->ti_type = NC_TI_FD;
607 (*session)->ti.fd.in = fdin;
608 (*session)->ti.fd.out = fdout;
Michal Vasko086311b2016-01-08 09:53:11 +0100609
610 /* assign context (dicionary needed for handshake) */
Michal Vasko1a38c862016-01-15 15:50:07 +0100611 (*session)->flags = NC_SESSION_SHAREDCTX;
612 (*session)->ctx = server_opts.ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100613
Michal Vaskob48aa812016-01-18 14:13:09 +0100614 /* assign new SID atomically */
615 pthread_spin_lock(&server_opts.sid_lock);
616 (*session)->id = server_opts.new_session_id++;
617 pthread_spin_unlock(&server_opts.sid_lock);
618
Michal Vasko086311b2016-01-08 09:53:11 +0100619 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200620 msgtype = nc_handshake(*session);
621 if (msgtype != NC_MSG_HELLO) {
622 nc_session_free(*session, NULL);
623 *session = NULL;
624 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100625 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200626 (*session)->opts.server.session_start = (*session)->opts.server.last_rpc = time(NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +0100627 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko086311b2016-01-08 09:53:11 +0100628
Michal Vasko71090fc2016-05-24 16:37:28 +0200629 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100630}
Michal Vasko9e036d52016-01-08 10:49:26 +0100631
Michal Vaskob30b99c2016-07-26 11:35:43 +0200632static void
633nc_ps_queue_remove_id(struct nc_pollsession *ps, uint8_t id)
634{
635 uint8_t i, found = 0;
636
637 for (i = 0; i < ps->queue_len; ++i) {
638 /* idx round buffer adjust */
639 if (ps->queue_begin + i == NC_PS_QUEUE_SIZE) {
640 i = -ps->queue_begin;
641 }
642
643 if (found) {
644 /* move the value back one place */
645 if (ps->queue[ps->queue_begin + i] == id) {
646 /* another equal value, simply cannot be */
647 ERRINT;
648 }
649
650 if (ps->queue_begin + i == 0) {
651 ps->queue[NC_PS_QUEUE_SIZE - 1] = ps->queue[ps->queue_begin + i];
652 } else {
653 ps->queue[ps->queue_begin + i - 1] = ps->queue[ps->queue_begin + i];
654 }
655 } else if (ps->queue[ps->queue_begin + i] == id) {
656 /* found our id, there can be no more equal valid values */
657 found = 1;
658 }
659 }
660
661 if (!found) {
662 ERRINT;
663 }
664 --ps->queue_len;
665}
666
Michal Vaskof04a52a2016-04-07 10:52:10 +0200667int
Michal Vasko26043172016-07-26 14:08:59 +0200668nc_ps_lock(struct nc_pollsession *ps, uint8_t *id, const char *func)
Michal Vaskobe86fe32016-04-07 10:43:03 +0200669{
670 int ret;
Michal Vaskob30b99c2016-07-26 11:35:43 +0200671 uint8_t queue_last;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200672 struct timespec ts;
673
Radek Krejci7ac16052016-07-15 11:48:18 +0200674 nc_gettimespec(&ts);
Michal Vasko81c5b302017-03-15 12:10:40 +0100675 nc_addtimespec(&ts, NC_PS_LOCK_TIMEOUT);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200676
677 /* LOCK */
678 ret = pthread_mutex_timedlock(&ps->lock, &ts);
679 if (ret) {
Michal Vasko26043172016-07-26 14:08:59 +0200680 ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200681 return -1;
682 }
683
684 /* get a unique queue value (by adding 1 to the last added value, if any) */
685 if (ps->queue_len) {
686 queue_last = ps->queue_begin + ps->queue_len - 1;
687 if (queue_last > NC_PS_QUEUE_SIZE - 1) {
688 queue_last -= NC_PS_QUEUE_SIZE;
689 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200690 *id = ps->queue[queue_last] + 1;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200691 } else {
Michal Vaskob30b99c2016-07-26 11:35:43 +0200692 *id = 0;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200693 }
694
695 /* add ourselves into the queue */
696 if (ps->queue_len == NC_PS_QUEUE_SIZE) {
Michal Vasko26043172016-07-26 14:08:59 +0200697 ERR("%s: pollsession queue too small.", func);
Michal Vasko0ea456b2016-07-26 12:23:24 +0200698 pthread_mutex_unlock(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200699 return -1;
700 }
701 ++ps->queue_len;
702 queue_last = ps->queue_begin + ps->queue_len - 1;
703 if (queue_last > NC_PS_QUEUE_SIZE - 1) {
704 queue_last -= NC_PS_QUEUE_SIZE;
705 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200706 ps->queue[queue_last] = *id;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200707
708 /* is it our turn? */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200709 while (ps->queue[ps->queue_begin] != *id) {
Radek Krejci7ac16052016-07-15 11:48:18 +0200710 nc_gettimespec(&ts);
Michal Vasko81c5b302017-03-15 12:10:40 +0100711 nc_addtimespec(&ts, NC_PS_LOCK_TIMEOUT);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200712
713 ret = pthread_cond_timedwait(&ps->cond, &ps->lock, &ts);
714 if (ret) {
Michal Vasko26043172016-07-26 14:08:59 +0200715 ERR("%s: failed to wait for a pollsession condition (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200716 /* remove ourselves from the queue */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200717 nc_ps_queue_remove_id(ps, *id);
718 pthread_mutex_unlock(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200719 return -1;
720 }
721 }
722
Michal Vaskobe86fe32016-04-07 10:43:03 +0200723 /* UNLOCK */
724 pthread_mutex_unlock(&ps->lock);
725
726 return 0;
727}
728
Michal Vaskof04a52a2016-04-07 10:52:10 +0200729int
Michal Vasko26043172016-07-26 14:08:59 +0200730nc_ps_unlock(struct nc_pollsession *ps, uint8_t id, const char *func)
Michal Vaskobe86fe32016-04-07 10:43:03 +0200731{
732 int ret;
733 struct timespec ts;
734
Radek Krejci7ac16052016-07-15 11:48:18 +0200735 nc_gettimespec(&ts);
Michal Vasko81c5b302017-03-15 12:10:40 +0100736 nc_addtimespec(&ts, NC_PS_LOCK_TIMEOUT);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200737
738 /* LOCK */
739 ret = pthread_mutex_timedlock(&ps->lock, &ts);
740 if (ret) {
Michal Vasko26043172016-07-26 14:08:59 +0200741 ERR("%s: failed to lock a pollsession (%s).", func, strerror(ret));
Michal Vaskobe86fe32016-04-07 10:43:03 +0200742 ret = -1;
743 }
744
Michal Vaskob30b99c2016-07-26 11:35:43 +0200745 /* we must be the first, it was our turn after all, right? */
746 if (ps->queue[ps->queue_begin] != id) {
747 ERRINT;
Michal Vaskob1a094b2016-10-05 14:04:52 +0200748 /* UNLOCK */
749 if (!ret) {
750 pthread_mutex_unlock(&ps->lock);
751 }
Michal Vaskob30b99c2016-07-26 11:35:43 +0200752 return -1;
753 }
754
Michal Vaskobe86fe32016-04-07 10:43:03 +0200755 /* remove ourselves from the queue */
Michal Vaskob30b99c2016-07-26 11:35:43 +0200756 nc_ps_queue_remove_id(ps, id);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200757
758 /* broadcast to all other threads that the queue moved */
759 pthread_cond_broadcast(&ps->cond);
760
Michal Vaskobe86fe32016-04-07 10:43:03 +0200761 /* UNLOCK */
762 if (!ret) {
763 pthread_mutex_unlock(&ps->lock);
764 }
765
766 return ret;
767}
768
Michal Vasko428087d2016-01-14 16:04:28 +0100769API struct nc_pollsession *
770nc_ps_new(void)
771{
Michal Vasko48a63ed2016-03-01 09:48:21 +0100772 struct nc_pollsession *ps;
773
774 ps = calloc(1, sizeof(struct nc_pollsession));
Michal Vasko4eb3c312016-03-01 14:09:37 +0100775 if (!ps) {
776 ERRMEM;
777 return NULL;
778 }
Michal Vaskobe86fe32016-04-07 10:43:03 +0200779 pthread_cond_init(&ps->cond, NULL);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100780 pthread_mutex_init(&ps->lock, NULL);
781
782 return ps;
Michal Vasko428087d2016-01-14 16:04:28 +0100783}
784
785API void
786nc_ps_free(struct nc_pollsession *ps)
787{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100788 if (!ps) {
789 return;
790 }
791
Michal Vaskobe86fe32016-04-07 10:43:03 +0200792 if (ps->queue_len) {
793 ERR("FATAL: Freeing a pollsession structure that is currently being worked with!");
794 }
795
Michal Vasko428087d2016-01-14 16:04:28 +0100796 free(ps->sessions);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100797 pthread_mutex_destroy(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200798 pthread_cond_destroy(&ps->cond);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100799
Michal Vasko428087d2016-01-14 16:04:28 +0100800 free(ps);
801}
802
803API int
804nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session)
805{
Michal Vaskob30b99c2016-07-26 11:35:43 +0200806 uint8_t q_id;
807
Michal Vasko45e53ae2016-04-07 11:46:03 +0200808 if (!ps) {
809 ERRARG("ps");
810 return -1;
811 } else if (!session) {
812 ERRARG("session");
Michal Vasko428087d2016-01-14 16:04:28 +0100813 return -1;
814 }
815
Michal Vasko48a63ed2016-03-01 09:48:21 +0100816 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200817 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200818 return -1;
819 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100820
Michal Vasko428087d2016-01-14 16:04:28 +0100821 ++ps->session_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100822 ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions);
Michal Vasko9a327362017-01-11 11:31:46 +0100823 if (!ps->sessions) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100824 ERRMEM;
825 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200826 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100827 return -1;
828 }
Michal Vasko3a715132016-01-21 15:40:31 +0100829 ps->sessions[ps->session_count - 1] = session;
Michal Vasko428087d2016-01-14 16:04:28 +0100830
Michal Vasko48a63ed2016-03-01 09:48:21 +0100831 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200832 return nc_ps_unlock(ps, q_id, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +0100833}
834
Michal Vasko48a63ed2016-03-01 09:48:21 +0100835static int
Radek Krejcid5f978f2016-03-03 13:14:45 +0100836_nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session, int index)
Michal Vasko428087d2016-01-14 16:04:28 +0100837{
838 uint16_t i;
839
Radek Krejcid5f978f2016-03-03 13:14:45 +0100840 if (index >= 0) {
841 i = (uint16_t)index;
842 goto remove;
843 }
Michal Vasko428087d2016-01-14 16:04:28 +0100844 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100845 if (ps->sessions[i] == session) {
Radek Krejcid5f978f2016-03-03 13:14:45 +0100846remove:
Michal Vasko428087d2016-01-14 16:04:28 +0100847 --ps->session_count;
Michal Vasko58005732016-02-02 15:50:52 +0100848 if (i < ps->session_count) {
849 ps->sessions[i] = ps->sessions[ps->session_count];
Michal Vasko58005732016-02-02 15:50:52 +0100850 } else if (!ps->session_count) {
851 free(ps->sessions);
852 ps->sessions = NULL;
Michal Vasko58005732016-02-02 15:50:52 +0100853 }
Michal Vasko11d2f6a2017-02-02 11:15:06 +0100854 ps->last_event_session = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100855 return 0;
856 }
857 }
858
Michal Vaskof0537d82016-01-29 14:42:38 +0100859 return -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100860}
861
Michal Vasko48a63ed2016-03-01 09:48:21 +0100862API int
863nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session)
864{
Michal Vaskob30b99c2016-07-26 11:35:43 +0200865 uint8_t q_id;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200866 int ret, ret2;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100867
Michal Vasko45e53ae2016-04-07 11:46:03 +0200868 if (!ps) {
869 ERRARG("ps");
870 return -1;
871 } else if (!session) {
872 ERRARG("session");
Michal Vasko48a63ed2016-03-01 09:48:21 +0100873 return -1;
874 }
875
876 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200877 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +0200878 return -1;
879 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100880
Radek Krejcid5f978f2016-03-03 13:14:45 +0100881 ret = _nc_ps_del_session(ps, session, -1);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100882
883 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +0200884 ret2 = nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100885
Michal Vaskobe86fe32016-04-07 10:43:03 +0200886 return (ret || ret2 ? -1 : 0);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100887}
888
Michal Vaskoe1ee05b2017-03-21 10:10:18 +0100889API struct nc_session *
890nc_ps_get_session_by_sid(const struct nc_pollsession *ps, uint32_t sid)
891{
892 uint8_t q_id;
893 uint16_t i;
894 struct nc_session *ret = NULL;
895
896 if (!ps) {
897 ERRARG("ps");
898 return NULL;
899 }
900
901 /* LOCK */
902 if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) {
903 return NULL;
904 }
905
906 for (i = 0; i < ps->session_count; ++i) {
907 if (ps->sessions[i]->id == sid) {
908 ret = ps->sessions[i];
909 break;
910 }
911 }
912
913 /* UNLOCK */
914 nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__);
915
916 return ret;
917}
918
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100919API uint16_t
920nc_ps_session_count(struct nc_pollsession *ps)
921{
922 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200923 ERRARG("ps");
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100924 return 0;
925 }
926
Michal Vaskof4462fd2017-02-15 14:29:05 +0100927 return ps->session_count;
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100928}
929
Michal Vasko71090fc2016-05-24 16:37:28 +0200930/* must be called holding the session lock!
931 * returns: NC_PSPOLL_ERROR,
932 * NC_PSPOLL_BAD_RPC,
933 * NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR,
934 * NC_PSPOLL_RPC
935 */
936static int
Radek Krejci93e80222016-10-03 13:34:25 +0200937nc_server_recv_rpc(struct nc_session *session, struct nc_server_rpc **rpc)
Michal Vasko428087d2016-01-14 16:04:28 +0100938{
939 struct lyxml_elem *xml = NULL;
940 NC_MSG_TYPE msgtype;
Radek Krejcif93c7d42016-04-06 13:41:15 +0200941 struct nc_server_reply *reply = NULL;
Radek Krejcif93c7d42016-04-06 13:41:15 +0200942 int ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100943
Michal Vasko45e53ae2016-04-07 11:46:03 +0200944 if (!session) {
945 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200946 return NC_PSPOLL_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200947 } else if (!rpc) {
948 ERRARG("rpc");
Michal Vasko71090fc2016-05-24 16:37:28 +0200949 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100950 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100951 ERR("Session %u: invalid session to receive RPCs.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200952 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100953 }
954
955 msgtype = nc_read_msg(session, &xml);
956
957 switch (msgtype) {
958 case NC_MSG_RPC:
Radek Krejcif93c7d42016-04-06 13:41:15 +0200959 *rpc = calloc(1, sizeof **rpc);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100960 if (!*rpc) {
961 ERRMEM;
962 goto error;
963 }
Michal Vaskoca4a2422016-02-02 12:17:14 +0100964
Radek Krejcif93c7d42016-04-06 13:41:15 +0200965 ly_errno = LY_SUCCESS;
Michal Vasko41adf392017-01-17 10:39:04 +0100966 (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child,
967 LYD_OPT_RPC | LYD_OPT_DESTRUCT | LYD_OPT_NOEXTDEPS | LYD_OPT_STRICT, NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +0100968 if (!(*rpc)->tree) {
Radek Krejcif93c7d42016-04-06 13:41:15 +0200969 /* parsing RPC failed */
Radek Krejci877e1822016-04-06 16:37:43 +0200970 reply = nc_server_reply_err(nc_err_libyang());
Radek Krejci844662e2016-04-13 16:54:43 +0200971 ret = nc_write_msg(session, NC_MSG_REPLY, xml, reply);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200972 nc_server_reply_free(reply);
973 if (ret == -1) {
974 ERR("Session %u: failed to write reply.", session->id);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200975 }
Michal Vasko71090fc2016-05-24 16:37:28 +0200976 ret = NC_PSPOLL_REPLY_ERROR | NC_PSPOLL_BAD_RPC;
977 } else {
978 ret = NC_PSPOLL_RPC;
Michal Vaskoca4a2422016-02-02 12:17:14 +0100979 }
Michal Vasko428087d2016-01-14 16:04:28 +0100980 (*rpc)->root = xml;
981 break;
982 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100983 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200984 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100985 goto error;
986 case NC_MSG_REPLY:
Michal Vasko81614ee2016-02-02 12:20:14 +0100987 ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200988 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100989 goto error;
990 case NC_MSG_NOTIF:
Michal Vasko81614ee2016-02-02 12:20:14 +0100991 ERR("Session %u: received <notification> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200992 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100993 goto error;
994 default:
Michal Vasko71090fc2016-05-24 16:37:28 +0200995 /* NC_MSG_ERROR,
Michal Vasko428087d2016-01-14 16:04:28 +0100996 * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg()
997 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200998 ret = NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100999 break;
1000 }
1001
Michal Vasko71090fc2016-05-24 16:37:28 +02001002 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001003
1004error:
1005 /* cleanup */
1006 lyxml_free(server_opts.ctx, xml);
1007
Michal Vasko71090fc2016-05-24 16:37:28 +02001008 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001009}
1010
fanchanghu966f2de2016-07-21 02:28:57 -04001011API void
1012nc_set_global_rpc_clb(nc_rpc_clb clb)
1013{
1014 global_rpc_clb = clb;
1015}
1016
Radek Krejci93e80222016-10-03 13:34:25 +02001017API NC_MSG_TYPE
1018nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout)
1019{
1020 NC_MSG_TYPE result = NC_MSG_NOTIF;
1021 int ret;
1022
1023 /* check parameters */
Michal Vasko3486a7c2017-03-03 13:28:07 +01001024 if (!session || (session->side != NC_SERVER) || !session->opts.server.ntf_status) {
Radek Krejci93e80222016-10-03 13:34:25 +02001025 ERRARG("session");
1026 return NC_MSG_ERROR;
1027 } else if (!notif || !notif->tree || !notif->eventtime) {
1028 ERRARG("notif");
1029 return NC_MSG_ERROR;
1030 }
1031
1032 /* reading an RPC and sending a reply must be atomic (no other RPC should be read) */
Michal Vaskoade892d2017-02-22 13:40:35 +01001033 ret = nc_session_lock(session, timeout, __func__);
Radek Krejci93e80222016-10-03 13:34:25 +02001034 if (ret < 0) {
1035 return NC_MSG_ERROR;
1036 } else if (!ret) {
1037 return NC_MSG_WOULDBLOCK;
1038 }
1039
1040 ret = nc_write_msg(session, NC_MSG_NOTIF, notif);
1041 if (ret == -1) {
1042 ERR("Session %u: failed to write notification.", session->id);
1043 result = NC_MSG_ERROR;
1044 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001045
1046 nc_session_unlock(session, timeout, __func__);
Radek Krejci93e80222016-10-03 13:34:25 +02001047
1048 return result;
1049}
1050
Michal Vasko71090fc2016-05-24 16:37:28 +02001051/* must be called holding the session lock!
1052 * returns: NC_PSPOLL_ERROR,
1053 * NC_PSPOLL_ERROR | NC_PSPOLL_REPLY_ERROR,
1054 * NC_PSPOLL_REPLY_ERROR,
1055 * 0
1056 */
1057static int
Radek Krejci93e80222016-10-03 13:34:25 +02001058nc_server_send_reply(struct nc_session *session, struct nc_server_rpc *rpc)
Michal Vasko428087d2016-01-14 16:04:28 +01001059{
1060 nc_rpc_clb clb;
1061 struct nc_server_reply *reply;
Michal Vasko90e8e692016-07-13 12:27:57 +02001062 struct lys_node *rpc_act = NULL;
1063 struct lyd_node *next, *elem;
Michal Vasko71090fc2016-05-24 16:37:28 +02001064 int ret = 0, r;
Michal Vasko428087d2016-01-14 16:04:28 +01001065
Michal Vasko4a827e52016-03-03 10:59:00 +01001066 if (!rpc) {
1067 ERRINT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001068 return NC_PSPOLL_ERROR;
Michal Vasko4a827e52016-03-03 10:59:00 +01001069 }
1070
Michal Vasko90e8e692016-07-13 12:27:57 +02001071 if (rpc->tree->schema->nodetype == LYS_RPC) {
1072 /* RPC */
1073 rpc_act = rpc->tree->schema;
fanchanghu966f2de2016-07-21 02:28:57 -04001074 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +02001075 /* action */
1076 LY_TREE_DFS_BEGIN(rpc->tree, next, elem) {
1077 if (elem->schema->nodetype == LYS_ACTION) {
1078 rpc_act = elem->schema;
1079 break;
1080 }
1081 LY_TREE_DFS_END(rpc->tree, next, elem);
fanchanghu966f2de2016-07-21 02:28:57 -04001082 }
Michal Vasko90e8e692016-07-13 12:27:57 +02001083 if (!rpc_act) {
1084 ERRINT;
1085 return NC_PSPOLL_ERROR;
1086 }
1087 }
1088
1089 if (!rpc_act->priv) {
1090 /* no callback, reply with a not-implemented error */
Michal Vasko1a38c862016-01-15 15:50:07 +01001091 reply = nc_server_reply_err(nc_err(NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT));
Michal Vasko428087d2016-01-14 16:04:28 +01001092 } else {
Michal Vasko90e8e692016-07-13 12:27:57 +02001093 clb = (nc_rpc_clb)rpc_act->priv;
Michal Vasko428087d2016-01-14 16:04:28 +01001094 reply = clb(rpc->tree, session);
1095 }
1096
1097 if (!reply) {
Michal Vasko1a38c862016-01-15 15:50:07 +01001098 reply = nc_server_reply_err(nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP));
Michal Vasko428087d2016-01-14 16:04:28 +01001099 }
Michal Vasko71090fc2016-05-24 16:37:28 +02001100 r = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply);
1101 if (reply->type == NC_RPL_ERROR) {
1102 ret |= NC_PSPOLL_REPLY_ERROR;
1103 }
1104 nc_server_reply_free(reply);
Michal Vasko428087d2016-01-14 16:04:28 +01001105
Michal Vasko71090fc2016-05-24 16:37:28 +02001106 if (r == -1) {
1107 ERR("Session %u: failed to write reply.", session->id);
1108 ret |= NC_PSPOLL_ERROR;
1109 }
Michal Vasko428087d2016-01-14 16:04:28 +01001110
1111 /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */
1112 if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) {
1113 session->status = NC_STATUS_INVALID;
1114 }
1115
Michal Vasko71090fc2016-05-24 16:37:28 +02001116 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001117}
1118
1119API int
Michal Vasko71090fc2016-05-24 16:37:28 +02001120nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session)
Michal Vasko428087d2016-01-14 16:04:28 +01001121{
Michal Vasko9a327362017-01-11 11:31:46 +01001122 int ret, r, poll_ret;
Michal Vaskob30b99c2016-07-26 11:35:43 +02001123 uint8_t q_id;
Michal Vasko9a327362017-01-11 11:31:46 +01001124 uint16_t i, j;
1125 char msg[256];
1126 NC_SESSION_TERM_REASON term_reason;
1127 struct pollfd pfd;
Michal Vasko36c7be82017-02-22 13:37:59 +01001128 struct timespec ts_timeout, ts_cur;
Michal Vasko71090fc2016-05-24 16:37:28 +02001129 struct nc_session *cur_session;
Michal Vasko4a827e52016-03-03 10:59:00 +01001130 struct nc_server_rpc *rpc = NULL;
Michal Vasko9a327362017-01-11 11:31:46 +01001131#ifdef NC_ENABLED_SSH
1132 struct nc_session *new;
1133#endif
Michal Vasko428087d2016-01-14 16:04:28 +01001134
Michal Vasko30a5d6b2017-02-15 14:29:39 +01001135 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001136 ERRARG("ps");
Michal Vasko71090fc2016-05-24 16:37:28 +02001137 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001138 }
1139
Michal Vaskoade892d2017-02-22 13:40:35 +01001140 /* PS LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001141 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001142 return NC_PSPOLL_ERROR;
Michal Vaskobe86fe32016-04-07 10:43:03 +02001143 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001144
Michal Vaskoade892d2017-02-22 13:40:35 +01001145 if (!ps->session_count) {
1146 ret = NC_PSPOLL_NOSESSIONS;
1147 goto ps_unlock_finish;
1148 }
Michal Vaskobd8ef262016-01-20 11:09:27 +01001149
Michal Vasko36c7be82017-02-22 13:37:59 +01001150 /* check timeout of all the sessions */
1151 nc_gettimespec(&ts_cur);
1152 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3486a7c2017-03-03 13:28:07 +01001153 if (!(ps->sessions[i]->flags & NC_SESSION_CALLHOME) && !ps->sessions[i]->opts.server.ntf_status
1154 && server_opts.idle_timeout
Michal Vasko36c7be82017-02-22 13:37:59 +01001155 && (ts_cur.tv_sec >= ps->sessions[i]->opts.server.last_rpc + server_opts.idle_timeout)) {
Michal Vasko3a715132016-01-21 15:40:31 +01001156 ERR("Session %u: session idle timeout elapsed.", ps->sessions[i]->id);
1157 ps->sessions[i]->status = NC_STATUS_INVALID;
1158 ps->sessions[i]->term_reason = NC_SESSION_TERM_TIMEOUT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001159 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1160 if (session) {
1161 *session = ps->sessions[i];
1162 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001163 goto ps_unlock_finish;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001164 }
Michal Vasko428087d2016-01-14 16:04:28 +01001165 }
1166
Michal Vasko36c7be82017-02-22 13:37:59 +01001167 if (timeout > -1) {
1168 nc_gettimespec(&ts_timeout);
1169 nc_addtimespec(&ts_timeout, timeout);
1170 }
1171
1172 /* poll all the sessions one-by-one */
Michal Vasko9a327362017-01-11 11:31:46 +01001173 do {
1174 /* loop from i to j */
1175 if (ps->last_event_session == ps->session_count - 1) {
1176 i = j = 0;
1177 } else {
1178 i = j = ps->last_event_session + 1;
Michal Vaskobd8ef262016-01-20 11:09:27 +01001179 }
Michal Vasko9a327362017-01-11 11:31:46 +01001180 do {
1181 cur_session = ps->sessions[i];
Michal Vasko428087d2016-01-14 16:04:28 +01001182
Michal Vaskoade892d2017-02-22 13:40:35 +01001183 /* SESSION LOCK */
1184 if ((cur_session->status == NC_STATUS_RUNNING)
1185 && !*cur_session->ti_inuse && ((r = nc_session_lock(cur_session, 0, __func__)))) {
1186 /* we go here if we successfully lock the session or there was an error, on timeout we simply skip it */
1187 if (r == -1) {
Michal Vasko9a327362017-01-11 11:31:46 +01001188 ret = NC_PSPOLL_ERROR;
Michal Vaskoade892d2017-02-22 13:40:35 +01001189 goto ps_unlock_finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001190 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001191 /* damn race condition */
1192 if (cur_session->status != NC_STATUS_RUNNING) {
1193 /* SESSION UNLOCK */
1194 nc_session_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__);
1195 goto next_iteration;
1196 }
Michal Vasko9a327362017-01-11 11:31:46 +01001197
Michal Vaskoade892d2017-02-22 13:40:35 +01001198 switch (cur_session->ti_type) {
Michal Vasko428087d2016-01-14 16:04:28 +01001199#ifdef NC_ENABLED_SSH
Michal Vaskoade892d2017-02-22 13:40:35 +01001200 case NC_TI_LIBSSH:
1201 r = ssh_channel_poll_timeout(cur_session->ti.libssh.channel, 0, 0);
1202 if (r < 1) {
1203 if (r == SSH_EOF) {
1204 sprintf(msg, "SSH channel unexpected EOF");
1205 term_reason = NC_SESSION_TERM_DROPPED;
1206 poll_ret = -2;
1207 } else if (r == SSH_ERROR) {
1208 sprintf(msg, "SSH channel poll error (%s)", ssh_get_error(cur_session->ti.libssh.session));
1209 term_reason = NC_SESSION_TERM_OTHER;
1210 poll_ret = -2;
1211 } else {
1212 poll_ret = 0;
1213 }
1214 break;
1215 }
1216
1217 /* we have some data, but it may be just an SSH message */
1218 r = ssh_execute_message_callbacks(cur_session->ti.libssh.session);
1219 if (r != SSH_OK) {
1220 sprintf(msg, "failed to receive SSH messages (%s)", ssh_get_error(cur_session->ti.libssh.session));
Michal Vasko76be6752017-01-19 12:14:48 +01001221 term_reason = NC_SESSION_TERM_OTHER;
1222 poll_ret = -2;
Michal Vaskoade892d2017-02-22 13:40:35 +01001223 } else if (cur_session->flags & NC_SESSION_SSH_NEW_MSG) {
1224 /* new SSH message */
1225 cur_session->flags &= ~NC_SESSION_SSH_NEW_MSG;
1226 if (cur_session->ti.libssh.next) {
1227 for (new = cur_session->ti.libssh.next; new != cur_session; new = new->ti.libssh.next) {
1228 if ((new->status == NC_STATUS_STARTING) && new->ti.libssh.channel
1229 && (new->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) {
1230 /* new NETCONF SSH channel */
1231 if (session) {
1232 *session = cur_session;
1233 }
1234 ret = NC_PSPOLL_SSH_CHANNEL;
1235 goto session_ps_unlock_finish;
Michal Vasko9a327362017-01-11 11:31:46 +01001236 }
Michal Vasko9a327362017-01-11 11:31:46 +01001237 }
1238 }
Michal Vasko9a327362017-01-11 11:31:46 +01001239
Michal Vaskoade892d2017-02-22 13:40:35 +01001240 /* just some SSH message */
1241 if (session) {
1242 *session = cur_session;
1243 }
1244 ret = NC_PSPOLL_SSH_MSG;
1245 goto session_ps_unlock_finish;
1246 } else {
1247 /* we have some application data */
1248 poll_ret = 1;
Michal Vasko9a327362017-01-11 11:31:46 +01001249 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001250 break;
Michal Vasko9a327362017-01-11 11:31:46 +01001251#endif
1252#ifdef NC_ENABLED_TLS
Michal Vaskoade892d2017-02-22 13:40:35 +01001253 case NC_TI_OPENSSL:
1254 r = SSL_pending(cur_session->ti.tls);
1255 if (!r) {
1256 /* no data pending in the SSL buffer, poll fd */
1257 pfd.fd = SSL_get_rfd(cur_session->ti.tls);
1258 if (pfd.fd < 0) {
1259 ERRINT;
1260 ret = NC_PSPOLL_ERROR;
1261 goto session_ps_unlock_finish;
1262 }
1263 pfd.events = POLLIN;
1264 pfd.revents = 0;
1265 r = poll(&pfd, 1, 0);
1266
1267 if ((r < 0) && (errno != EINTR)) {
1268 sprintf(msg, "poll failed (%s)", strerror(errno));
1269 poll_ret = -1;
1270 } else if (r > 0) {
1271 if (pfd.revents & (POLLHUP | POLLNVAL)) {
1272 sprintf(msg, "communication socket unexpectedly closed");
1273 term_reason = NC_SESSION_TERM_DROPPED;
1274 poll_ret = -2;
1275 } else if (pfd.revents & POLLERR) {
1276 sprintf(msg, "communication socket error");
1277 term_reason = NC_SESSION_TERM_OTHER;
1278 poll_ret = -2;
1279 } else {
1280 poll_ret = 1;
1281 }
1282 } else {
1283 poll_ret = 0;
1284 }
1285 } else {
1286 poll_ret = 1;
Michal Vasko9a327362017-01-11 11:31:46 +01001287 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001288 break;
1289#endif
1290 case NC_TI_FD:
1291 pfd.fd = cur_session->ti.fd.in;
Michal Vasko9a327362017-01-11 11:31:46 +01001292 pfd.events = POLLIN;
1293 pfd.revents = 0;
1294 r = poll(&pfd, 1, 0);
1295
Michal Vaskoade892d2017-02-22 13:40:35 +01001296 if ((r < 0) && (errno != EINTR)) {
Michal Vasko9a327362017-01-11 11:31:46 +01001297 sprintf(msg, "poll failed (%s)", strerror(errno));
1298 poll_ret = -1;
1299 } else if (r > 0) {
1300 if (pfd.revents & (POLLHUP | POLLNVAL)) {
1301 sprintf(msg, "communication socket unexpectedly closed");
1302 term_reason = NC_SESSION_TERM_DROPPED;
1303 poll_ret = -2;
1304 } else if (pfd.revents & POLLERR) {
1305 sprintf(msg, "communication socket error");
1306 term_reason = NC_SESSION_TERM_OTHER;
1307 poll_ret = -2;
1308 } else {
1309 poll_ret = 1;
1310 }
1311 } else {
1312 poll_ret = 0;
1313 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001314 break;
1315 case NC_TI_NONE:
1316 ERRINT;
1317 ret = NC_PSPOLL_ERROR;
1318 goto session_ps_unlock_finish;
Michal Vasko9a327362017-01-11 11:31:46 +01001319 }
Michal Vasko4d13eca2017-05-03 16:05:11 +02001320 /* we have some data, but it may be just an SSH message */
Michal Vaskoade892d2017-02-22 13:40:35 +01001321 /* here: poll_ret == -2 - session error, session terminated,
1322 * poll_ret == -1 - generic error,
1323 * poll_ret == 0 - nothing to read,
1324 * poll_ret > 0 - data available
1325 */
1326 if (poll_ret == -2) {
1327 ERR("Session %u: %s.", cur_session->id, msg);
1328 cur_session->status = NC_STATUS_INVALID;
1329 cur_session->term_reason = term_reason;
1330 if (session) {
1331 *session = cur_session;
Michal Vasko9a327362017-01-11 11:31:46 +01001332 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001333 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
1334 goto session_ps_unlock_finish;
1335 } else if (poll_ret == -1) {
1336 ERR("Session %u: %s.", cur_session->id, msg);
1337 ret = NC_PSPOLL_ERROR;
1338 goto session_ps_unlock_finish;
1339 } else if (poll_ret > 0) {
1340 break;
Michal Vasko9a327362017-01-11 11:31:46 +01001341 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001342
1343 /* SESSION UNLOCK */
1344 nc_session_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__);
1345 } else {
1346 /* timeout */
1347 poll_ret = 0;
Michal Vasko428087d2016-01-14 16:04:28 +01001348 }
Michal Vasko428087d2016-01-14 16:04:28 +01001349
Michal Vaskoade892d2017-02-22 13:40:35 +01001350next_iteration:
Michal Vasko9a327362017-01-11 11:31:46 +01001351 if (i == ps->session_count - 1) {
1352 i = 0;
1353 } else {
1354 ++i;
1355 }
1356 } while (i != j);
1357
Michal Vaskoade892d2017-02-22 13:40:35 +01001358 /* no event, no session remains locked */
Michal Vasko9a327362017-01-11 11:31:46 +01001359 if (!poll_ret && (timeout > -1)) {
1360 usleep(NC_TIMEOUT_STEP);
1361
Michal Vaskoade892d2017-02-22 13:40:35 +01001362 nc_gettimespec(&ts_cur);
Michal Vasko9a327362017-01-11 11:31:46 +01001363 /* final timeout */
Michal Vaskoade892d2017-02-22 13:40:35 +01001364 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
Michal Vasko9a327362017-01-11 11:31:46 +01001365 ret = NC_PSPOLL_TIMEOUT;
Michal Vaskoade892d2017-02-22 13:40:35 +01001366 goto ps_unlock_finish;
Michal Vasko9a327362017-01-11 11:31:46 +01001367 }
Michal Vasko428087d2016-01-14 16:04:28 +01001368 }
Michal Vasko9a327362017-01-11 11:31:46 +01001369 } while (!poll_ret);
Michal Vasko428087d2016-01-14 16:04:28 +01001370
Michal Vaskoade892d2017-02-22 13:40:35 +01001371 /* this is the session with some data available for reading, it is still locked */
Michal Vasko71090fc2016-05-24 16:37:28 +02001372 if (session) {
1373 *session = cur_session;
1374 }
Michal Vasko9a327362017-01-11 11:31:46 +01001375 ps->last_event_session = i;
Michal Vasko428087d2016-01-14 16:04:28 +01001376
Michal Vaskoade892d2017-02-22 13:40:35 +01001377 /* PS UNLOCK */
1378 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +01001379
Radek Krejci93e80222016-10-03 13:34:25 +02001380 ret = nc_server_recv_rpc(cur_session, &rpc);
Michal Vasko71090fc2016-05-24 16:37:28 +02001381 if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001382 if (cur_session->status != NC_STATUS_RUNNING) {
1383 ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001384 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001385 goto session_unlock_finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001386 }
1387
Michal Vasko2e6defd2016-10-07 15:48:15 +02001388 cur_session->opts.server.last_rpc = time(NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +01001389
Michal Vaskoade892d2017-02-22 13:40:35 +01001390 /* process RPC, not needed afterwards */
Radek Krejci93e80222016-10-03 13:34:25 +02001391 ret |= nc_server_send_reply(cur_session, rpc);
Michal Vaskoade892d2017-02-22 13:40:35 +01001392 nc_server_rpc_free(rpc, server_opts.ctx);
Michal Vasko428087d2016-01-14 16:04:28 +01001393
Michal Vasko71090fc2016-05-24 16:37:28 +02001394 if (cur_session->status != NC_STATUS_RUNNING) {
1395 ret |= NC_PSPOLL_SESSION_TERM;
1396 if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) {
1397 ret |= NC_PSPOLL_SESSION_ERROR;
1398 }
Michal Vasko428087d2016-01-14 16:04:28 +01001399 }
Radek Krejcif93c7d42016-04-06 13:41:15 +02001400
Michal Vaskoade892d2017-02-22 13:40:35 +01001401session_unlock_finish:
1402 /* SESSION UNLOCK */
1403 nc_session_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__);
1404 return ret;
Michal Vaskobd8ef262016-01-20 11:09:27 +01001405
Michal Vaskoade892d2017-02-22 13:40:35 +01001406session_ps_unlock_finish:
1407 /* SESSION UNLOCK */
1408 nc_session_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__);
1409
1410ps_unlock_finish:
1411 /* PS UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001412 nc_ps_unlock(ps, q_id, __func__);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001413 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001414}
1415
Michal Vaskod09eae62016-02-01 10:32:52 +01001416API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001417nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *))
Michal Vaskod09eae62016-02-01 10:32:52 +01001418{
Michal Vaskob30b99c2016-07-26 11:35:43 +02001419 uint8_t q_id;
Michal Vaskod09eae62016-02-01 10:32:52 +01001420 uint16_t i;
1421 struct nc_session *session;
1422
Michal Vasko9a25e932016-02-01 10:36:42 +01001423 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001424 ERRARG("ps");
Michal Vasko9a25e932016-02-01 10:36:42 +01001425 return;
1426 }
1427
Michal Vasko48a63ed2016-03-01 09:48:21 +01001428 /* LOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001429 if (nc_ps_lock(ps, &q_id, __func__)) {
Michal Vaskobe86fe32016-04-07 10:43:03 +02001430 return;
1431 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001432
Michal Vasko48a63ed2016-03-01 09:48:21 +01001433 if (all) {
Radek Krejci4f8042c2016-03-03 13:11:26 +01001434 for (i = 0; i < ps->session_count; i++) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001435 nc_session_free(ps->sessions[i], data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001436 }
1437 free(ps->sessions);
1438 ps->sessions = NULL;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001439 ps->session_count = 0;
Michal Vasko9a327362017-01-11 11:31:46 +01001440 ps->last_event_session = 0;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001441 } else {
1442 for (i = 0; i < ps->session_count; ) {
1443 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
1444 session = ps->sessions[i];
Radek Krejcid5f978f2016-03-03 13:14:45 +01001445 _nc_ps_del_session(ps, NULL, i);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001446 nc_session_free(session, data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001447 continue;
1448 }
1449
1450 ++i;
1451 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001452 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001453
1454 /* UNLOCK */
Michal Vasko26043172016-07-26 14:08:59 +02001455 nc_ps_unlock(ps, q_id, __func__);
Michal Vaskod09eae62016-02-01 10:32:52 +01001456}
1457
Radek Krejci53691be2016-02-22 13:58:37 +01001458#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +01001459
Michal Vaskoe2713da2016-08-22 16:06:40 +02001460API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001461nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01001462{
Michal Vasko3031aae2016-01-27 16:07:18 +01001463 uint16_t i;
Michal Vaskoade892d2017-02-22 13:40:35 +01001464 int ret = 0;
Michal Vasko9e036d52016-01-08 10:49:26 +01001465
Michal Vasko45e53ae2016-04-07 11:46:03 +02001466 if (!name) {
1467 ERRARG("name");
1468 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001469 }
1470
Michal Vaskoade892d2017-02-22 13:40:35 +01001471 /* BIND LOCK */
1472 pthread_mutex_lock(&server_opts.bind_lock);
1473
1474 /* ENDPT WRITE LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001475 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001476
1477 /* check name uniqueness */
1478 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001479 if (!strcmp(server_opts.endpts[i].name, name)) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001480 ERR("Endpoint \"%s\" already exists.", name);
Michal Vaskoade892d2017-02-22 13:40:35 +01001481 ret = -1;
1482 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +01001483 }
1484 }
1485
Michal Vasko3031aae2016-01-27 16:07:18 +01001486 ++server_opts.endpt_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001487 server_opts.endpts = nc_realloc(server_opts.endpts, server_opts.endpt_count * sizeof *server_opts.endpts);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001488 if (!server_opts.endpts) {
Michal Vasko4eb3c312016-03-01 14:09:37 +01001489 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001490 ret = -1;
1491 goto cleanup;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001492 }
1493 server_opts.endpts[server_opts.endpt_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001494 server_opts.endpts[server_opts.endpt_count - 1].ti = ti;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001495
Michal Vaskoe2713da2016-08-22 16:06:40 +02001496 server_opts.binds = nc_realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds);
Michal Vaskoe2713da2016-08-22 16:06:40 +02001497 if (!server_opts.binds) {
1498 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001499 ret = -1;
1500 goto cleanup;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001501 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001502
Michal Vasko2e6defd2016-10-07 15:48:15 +02001503 server_opts.binds[server_opts.endpt_count - 1].address = NULL;
1504 server_opts.binds[server_opts.endpt_count - 1].port = 0;
1505 server_opts.binds[server_opts.endpt_count - 1].sock = -1;
Michal Vasko0a3f3752016-10-13 14:58:38 +02001506 server_opts.binds[server_opts.endpt_count - 1].pollin = 0;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001507
1508 switch (ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001509#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001510 case NC_TI_LIBSSH:
1511 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts));
1512 if (!server_opts.endpts[server_opts.endpt_count - 1].opts.ssh) {
1513 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001514 ret = -1;
1515 goto cleanup;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001516 }
1517 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_methods =
1518 NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
1519 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_attempts = 3;
1520 server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_timeout = 10;
1521 break;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001522#endif
Michal Vaskoe2713da2016-08-22 16:06:40 +02001523#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001524 case NC_TI_OPENSSL:
1525 server_opts.endpts[server_opts.endpt_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts));
1526 if (!server_opts.endpts[server_opts.endpt_count - 1].opts.tls) {
1527 ERRMEM;
Michal Vaskoade892d2017-02-22 13:40:35 +01001528 ret = -1;
1529 goto cleanup;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001530 }
1531 break;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001532#endif
Michal Vasko2e6defd2016-10-07 15:48:15 +02001533 default:
1534 ERRINT;
Michal Vaskoade892d2017-02-22 13:40:35 +01001535 ret = -1;
1536 goto cleanup;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001537 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001538
Michal Vaskoade892d2017-02-22 13:40:35 +01001539cleanup:
1540 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001541 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko9e036d52016-01-08 10:49:26 +01001542
Michal Vaskoade892d2017-02-22 13:40:35 +01001543 /* BIND UNLOCK */
1544 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001545
Michal Vaskoade892d2017-02-22 13:40:35 +01001546 return ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001547}
1548
Michal Vasko3031aae2016-01-27 16:07:18 +01001549int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001550nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port)
Michal Vaskoda514772016-02-01 11:32:01 +01001551{
1552 struct nc_endpt *endpt;
1553 struct nc_bind *bind = NULL;
1554 uint16_t i;
Michal Vasko4c1fb492017-01-30 14:31:07 +01001555 int sock = -1, set_addr, ret = 0;
Michal Vaskoda514772016-02-01 11:32:01 +01001556
Michal Vasko45e53ae2016-04-07 11:46:03 +02001557 if (!endpt_name) {
1558 ERRARG("endpt_name");
1559 return -1;
1560 } else if ((!address && !port) || (address && port)) {
1561 ERRARG("address and port");
1562 return -1;
Michal Vaskoda514772016-02-01 11:32:01 +01001563 }
1564
Michal Vaskoe2713da2016-08-22 16:06:40 +02001565 if (address) {
1566 set_addr = 1;
1567 } else {
1568 set_addr = 0;
1569 }
1570
Michal Vaskoade892d2017-02-22 13:40:35 +01001571 /* BIND LOCK */
1572 pthread_mutex_lock(&server_opts.bind_lock);
1573
1574 /* ENDPT LOCK */
1575 endpt = nc_server_endpt_lock_get(endpt_name, 0, &i);
Michal Vaskoda514772016-02-01 11:32:01 +01001576 if (!endpt) {
Michal Vasko4e455dd2017-03-21 15:33:43 +01001577 /* BIND UNLOCK */
1578 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vaskoda514772016-02-01 11:32:01 +01001579 return -1;
1580 }
1581
Michal Vaskoe2713da2016-08-22 16:06:40 +02001582 bind = &server_opts.binds[i];
Michal Vaskoda514772016-02-01 11:32:01 +01001583
Michal Vaskoe2713da2016-08-22 16:06:40 +02001584 if (set_addr) {
1585 port = bind->port;
Michal Vaskoda514772016-02-01 11:32:01 +01001586 } else {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001587 address = bind->address;
Michal Vaskoda514772016-02-01 11:32:01 +01001588 }
1589
Michal Vaskoe2713da2016-08-22 16:06:40 +02001590 /* we have all the information we need to create a listening socket */
1591 if (address && port) {
1592 /* create new socket, close the old one */
1593 sock = nc_sock_listen(address, port);
1594 if (sock == -1) {
Michal Vasko4c1fb492017-01-30 14:31:07 +01001595 ret = -1;
1596 goto cleanup;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001597 }
1598
1599 if (bind->sock > -1) {
1600 close(bind->sock);
1601 }
1602 bind->sock = sock;
1603 } /* else we are just setting address or port */
1604
1605 if (set_addr) {
Michal Vaskoda514772016-02-01 11:32:01 +01001606 lydict_remove(server_opts.ctx, bind->address);
1607 bind->address = lydict_insert(server_opts.ctx, address, 0);
1608 } else {
1609 bind->port = port;
1610 }
1611
Michal Vaskoe2713da2016-08-22 16:06:40 +02001612 if (sock > -1) {
1613#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko2e6defd2016-10-07 15:48:15 +02001614 VRB("Listening on %s:%u for %s connections.", address, port, (endpt->ti == NC_TI_LIBSSH ? "SSH" : "TLS"));
Michal Vaskoe2713da2016-08-22 16:06:40 +02001615#elif defined(NC_ENABLED_SSH)
1616 VRB("Listening on %s:%u for SSH connections.", address, port);
1617#else
1618 VRB("Listening on %s:%u for TLS connections.", address, port);
1619#endif
1620 }
1621
Michal Vasko4c1fb492017-01-30 14:31:07 +01001622cleanup:
Michal Vaskoade892d2017-02-22 13:40:35 +01001623 /* ENDPT UNLOCK */
1624 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko51e514d2016-02-02 15:51:52 +01001625
Michal Vaskoade892d2017-02-22 13:40:35 +01001626 /* BIND UNLOCK */
1627 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vasko51e514d2016-02-02 15:51:52 +01001628
Michal Vasko4c1fb492017-01-30 14:31:07 +01001629 return ret;
Michal Vaskoda514772016-02-01 11:32:01 +01001630}
1631
Michal Vaskoe2713da2016-08-22 16:06:40 +02001632API int
Michal Vasko2e6defd2016-10-07 15:48:15 +02001633nc_server_endpt_set_address(const char *endpt_name, const char *address)
1634{
1635 return nc_server_endpt_set_address_port(endpt_name, address, 0);
1636}
1637
1638API int
1639nc_server_endpt_set_port(const char *endpt_name, uint16_t port)
1640{
1641 return nc_server_endpt_set_address_port(endpt_name, NULL, port);
1642}
1643
1644API int
Michal Vasko59050372016-11-22 14:33:55 +01001645nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01001646{
1647 uint32_t i;
1648 int ret = -1;
1649
Michal Vaskoade892d2017-02-22 13:40:35 +01001650 /* BIND LOCK */
1651 pthread_mutex_lock(&server_opts.bind_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001652
Michal Vaskoade892d2017-02-22 13:40:35 +01001653 /* ENDPT WRITE LOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001654 pthread_rwlock_wrlock(&server_opts.endpt_lock);
Michal Vasko9e036d52016-01-08 10:49:26 +01001655
Michal Vasko59050372016-11-22 14:33:55 +01001656 if (!name && !ti) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001657 /* remove all endpoints */
Michal Vasko3031aae2016-01-27 16:07:18 +01001658 for (i = 0; i < server_opts.endpt_count; ++i) {
1659 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001660 switch (server_opts.endpts[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001661#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001662 case NC_TI_LIBSSH:
1663 nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh);
1664 free(server_opts.endpts[i].opts.ssh);
1665 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001666#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001667#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001668 case NC_TI_OPENSSL:
1669 nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls);
1670 free(server_opts.endpts[i].opts.tls);
1671 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001672#endif
Michal Vasko2e6defd2016-10-07 15:48:15 +02001673 default:
1674 ERRINT;
1675 /* won't get here ...*/
1676 break;
1677 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001678 ret = 0;
1679 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001680 free(server_opts.endpts);
1681 server_opts.endpts = NULL;
Michal Vaskoe2713da2016-08-22 16:06:40 +02001682
1683 /* remove all binds */
1684 for (i = 0; i < server_opts.endpt_count; ++i) {
1685 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1686 if (server_opts.binds[i].sock > -1) {
1687 close(server_opts.binds[i].sock);
1688 }
1689 }
Michal Vaskoe2713da2016-08-22 16:06:40 +02001690 free(server_opts.binds);
1691 server_opts.binds = NULL;
1692
Michal Vasko3031aae2016-01-27 16:07:18 +01001693 server_opts.endpt_count = 0;
1694
Michal Vasko1a38c862016-01-15 15:50:07 +01001695 } else {
Michal Vasko59050372016-11-22 14:33:55 +01001696 /* remove one endpoint with bind(s) or all endpoints using one transport protocol */
Michal Vasko3031aae2016-01-27 16:07:18 +01001697 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vasko59050372016-11-22 14:33:55 +01001698 if ((name && !strcmp(server_opts.endpts[i].name, name)) || (!name && (server_opts.endpts[i].ti == ti))) {
Michal Vaskoe2713da2016-08-22 16:06:40 +02001699 /* remove endpt */
Michal Vasko3031aae2016-01-27 16:07:18 +01001700 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02001701 switch (server_opts.endpts[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001702#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001703 case NC_TI_LIBSSH:
1704 nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh);
1705 free(server_opts.endpts[i].opts.ssh);
1706 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001707#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001708#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001709 case NC_TI_OPENSSL:
1710 nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls);
1711 free(server_opts.endpts[i].opts.tls);
1712 break;
Michal Vasko3031aae2016-01-27 16:07:18 +01001713#endif
Michal Vasko2e6defd2016-10-07 15:48:15 +02001714 default:
1715 ERRINT;
1716 break;
1717 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001718
Michal Vaskoe2713da2016-08-22 16:06:40 +02001719 /* remove bind(s) */
Michal Vaskoe2713da2016-08-22 16:06:40 +02001720 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1721 if (server_opts.binds[i].sock > -1) {
1722 close(server_opts.binds[i].sock);
1723 }
1724
1725 /* move last endpt and bind(s) to the empty space */
Michal Vasko3031aae2016-01-27 16:07:18 +01001726 --server_opts.endpt_count;
Michal Vasko9ed67a72016-10-13 15:00:51 +02001727 if (!server_opts.endpt_count) {
Michal Vaskoc0256492016-02-02 12:19:06 +01001728 free(server_opts.binds);
1729 server_opts.binds = NULL;
1730 free(server_opts.endpts);
1731 server_opts.endpts = NULL;
Michal Vasko9ed67a72016-10-13 15:00:51 +02001732 } else if (i < server_opts.endpt_count) {
1733 memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
1734 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
Michal Vaskoc0256492016-02-02 12:19:06 +01001735 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001736
1737 ret = 0;
Michal Vasko59050372016-11-22 14:33:55 +01001738 if (name) {
1739 break;
1740 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001741 }
1742 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001743 }
1744
Michal Vaskoade892d2017-02-22 13:40:35 +01001745 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001746 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001747
Michal Vaskoade892d2017-02-22 13:40:35 +01001748 /* BIND UNLOCK */
1749 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vasko9e036d52016-01-08 10:49:26 +01001750
1751 return ret;
1752}
1753
Michal Vasko71090fc2016-05-24 16:37:28 +02001754API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +01001755nc_accept(int timeout, struct nc_session **session)
Michal Vasko9e036d52016-01-08 10:49:26 +01001756{
Michal Vasko71090fc2016-05-24 16:37:28 +02001757 NC_MSG_TYPE msgtype;
Michal Vasko1a38c862016-01-15 15:50:07 +01001758 int sock, ret;
Michal Vasko5c2f7952016-01-22 13:16:31 +01001759 char *host = NULL;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001760 uint16_t port, bind_idx;
Michal Vasko9e036d52016-01-08 10:49:26 +01001761
Michal Vasko45e53ae2016-04-07 11:46:03 +02001762 if (!server_opts.ctx) {
1763 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001764 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001765 } else if (!session) {
1766 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001767 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001768 }
1769
Michal Vaskoade892d2017-02-22 13:40:35 +01001770 /* BIND LOCK */
1771 pthread_mutex_lock(&server_opts.bind_lock);
Michal Vasko51e514d2016-02-02 15:51:52 +01001772
1773 if (!server_opts.endpt_count) {
Michal Vasko863a6e92016-07-28 14:27:41 +02001774 ERR("No endpoints to accept sessions on.");
Michal Vaskoade892d2017-02-22 13:40:35 +01001775 /* BIND UNLOCK */
1776 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001777 return NC_MSG_ERROR;
Michal Vasko51e514d2016-02-02 15:51:52 +01001778 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001779
Michal Vaskoe2713da2016-08-22 16:06:40 +02001780 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &bind_idx);
Michal Vasko50456e82016-02-02 12:16:08 +01001781 if (ret < 1) {
Michal Vaskoade892d2017-02-22 13:40:35 +01001782 /* BIND UNLOCK */
1783 pthread_mutex_unlock(&server_opts.bind_lock);
Michal Vaskob737d752016-02-09 09:01:27 +01001784 free(host);
Michal Vasko5e203472016-05-30 15:27:58 +02001785 if (!ret) {
1786 return NC_MSG_WOULDBLOCK;
1787 }
Michal Vasko71090fc2016-05-24 16:37:28 +02001788 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001789 }
Michal Vaskoade892d2017-02-22 13:40:35 +01001790
1791 /* switch bind_lock for endpt_lock, so that another thread can accept another session */
1792 /* ENDPT READ LOCK */
1793 pthread_rwlock_rdlock(&server_opts.endpt_lock);
1794
1795 /* BIND UNLOCK */
1796 pthread_mutex_unlock(&server_opts.bind_lock);
1797
Michal Vaskob48aa812016-01-18 14:13:09 +01001798 sock = ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001799
Michal Vaskoade892d2017-02-22 13:40:35 +01001800 *session = nc_new_session(0);
Michal Vasko686aa312016-01-21 15:58:18 +01001801 if (!(*session)) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001802 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001803 close(sock);
Michal Vasko5c2f7952016-01-22 13:16:31 +01001804 free(host);
Michal Vasko71090fc2016-05-24 16:37:28 +02001805 msgtype = NC_MSG_ERROR;
1806 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001807 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001808 (*session)->status = NC_STATUS_STARTING;
1809 (*session)->side = NC_SERVER;
1810 (*session)->ctx = server_opts.ctx;
1811 (*session)->flags = NC_SESSION_SHAREDCTX;
1812 (*session)->host = lydict_insert_zc(server_opts.ctx, host);
1813 (*session)->port = port;
Michal Vasko9e036d52016-01-08 10:49:26 +01001814
1815 /* transport lock */
Michal Vasko1a38c862016-01-15 15:50:07 +01001816 pthread_mutex_init((*session)->ti_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +01001817 pthread_cond_init((*session)->ti_cond, NULL);
1818 *(*session)->ti_inuse = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001819
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001820 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001821#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02001822 if (server_opts.endpts[bind_idx].ti == NC_TI_LIBSSH) {
1823 (*session)->data = server_opts.endpts[bind_idx].opts.ssh;
Michal Vaskob70c8b82017-03-17 09:09:29 +01001824 ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko71090fc2016-05-24 16:37:28 +02001825 if (ret < 0) {
1826 msgtype = NC_MSG_ERROR;
1827 goto cleanup;
1828 } else if (!ret) {
1829 msgtype = NC_MSG_WOULDBLOCK;
1830 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001831 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001832 } else
1833#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001834#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02001835 if (server_opts.endpts[bind_idx].ti == NC_TI_OPENSSL) {
1836 (*session)->data = server_opts.endpts[bind_idx].opts.tls;
Michal Vaskob70c8b82017-03-17 09:09:29 +01001837 ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko71090fc2016-05-24 16:37:28 +02001838 if (ret < 0) {
1839 msgtype = NC_MSG_ERROR;
1840 goto cleanup;
1841 } else if (!ret) {
1842 msgtype = NC_MSG_WOULDBLOCK;
1843 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001844 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001845 } else
1846#endif
1847 {
Michal Vasko9e036d52016-01-08 10:49:26 +01001848 ERRINT;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001849 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001850 msgtype = NC_MSG_ERROR;
1851 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001852 }
1853
Michal Vasko2cc4c682016-03-01 09:16:48 +01001854 (*session)->data = NULL;
1855
Michal Vaskoade892d2017-02-22 13:40:35 +01001856 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001857 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001858
Michal Vaskob48aa812016-01-18 14:13:09 +01001859 /* assign new SID atomically */
1860 /* LOCK */
1861 pthread_spin_lock(&server_opts.sid_lock);
1862 (*session)->id = server_opts.new_session_id++;
1863 /* UNLOCK */
1864 pthread_spin_unlock(&server_opts.sid_lock);
1865
Michal Vasko9e036d52016-01-08 10:49:26 +01001866 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001867 msgtype = nc_handshake(*session);
1868 if (msgtype != NC_MSG_HELLO) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001869 nc_session_free(*session, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001870 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001871 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001872 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001873 (*session)->opts.server.session_start = (*session)->opts.server.last_rpc = time(NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001874 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko9e036d52016-01-08 10:49:26 +01001875
Michal Vasko71090fc2016-05-24 16:37:28 +02001876 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001877
Michal Vasko71090fc2016-05-24 16:37:28 +02001878cleanup:
Michal Vaskoade892d2017-02-22 13:40:35 +01001879 /* ENDPT UNLOCK */
Michal Vasko2e6defd2016-10-07 15:48:15 +02001880 pthread_rwlock_unlock(&server_opts.endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001881
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001882 nc_session_free(*session, NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001883 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001884 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001885}
1886
Michal Vasko2e6defd2016-10-07 15:48:15 +02001887API int
1888nc_server_ch_add_client(const char *name, NC_TRANSPORT_IMPL ti)
1889{
1890 uint16_t i;
1891
1892 if (!name) {
1893 ERRARG("name");
1894 return -1;
1895 } else if (!ti) {
1896 ERRARG("ti");
1897 return -1;
1898 }
1899
1900 /* WRITE LOCK */
1901 pthread_rwlock_wrlock(&server_opts.ch_client_lock);
1902
1903 /* check name uniqueness */
1904 for (i = 0; i < server_opts.ch_client_count; ++i) {
1905 if (!strcmp(server_opts.ch_clients[i].name, name)) {
1906 ERR("Call Home client \"%s\" already exists.", name);
1907 /* WRITE UNLOCK */
1908 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1909 return -1;
1910 }
1911 }
1912
1913 ++server_opts.ch_client_count;
1914 server_opts.ch_clients = nc_realloc(server_opts.ch_clients, server_opts.ch_client_count * sizeof *server_opts.ch_clients);
1915 if (!server_opts.ch_clients) {
1916 ERRMEM;
1917 /* WRITE UNLOCK */
1918 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1919 return -1;
1920 }
1921 server_opts.ch_clients[server_opts.ch_client_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
1922 server_opts.ch_clients[server_opts.ch_client_count - 1].ti = ti;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001923 server_opts.ch_clients[server_opts.ch_client_count - 1].ch_endpts = NULL;
1924 server_opts.ch_clients[server_opts.ch_client_count - 1].ch_endpt_count = 0;
Michal Vasko2e6defd2016-10-07 15:48:15 +02001925
1926 switch (ti) {
1927#ifdef NC_ENABLED_SSH
1928 case NC_TI_LIBSSH:
1929 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts));
1930 if (!server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh) {
1931 ERRMEM;
1932 /* WRITE UNLOCK */
1933 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1934 return -1;
1935 }
1936 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_methods =
1937 NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
1938 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_attempts = 3;
1939 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.ssh->auth_timeout = 10;
1940 break;
1941#endif
1942#ifdef NC_ENABLED_TLS
1943 case NC_TI_OPENSSL:
1944 server_opts.ch_clients[server_opts.ch_client_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts));
1945 if (!server_opts.ch_clients[server_opts.ch_client_count - 1].opts.tls) {
1946 ERRMEM;
1947 /* WRITE UNLOCK */
1948 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1949 return -1;
1950 }
1951 break;
1952#endif
1953 default:
1954 ERRINT;
1955 /* WRITE UNLOCK */
1956 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1957 return -1;
1958 }
1959
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001960 server_opts.ch_clients[server_opts.ch_client_count - 1].conn_type = 0;
1961
Michal Vasko2e6defd2016-10-07 15:48:15 +02001962 /* set CH default options */
1963 server_opts.ch_clients[server_opts.ch_client_count - 1].start_with = NC_CH_FIRST_LISTED;
1964 server_opts.ch_clients[server_opts.ch_client_count - 1].max_attempts = 3;
1965
1966 pthread_mutex_init(&server_opts.ch_clients[server_opts.ch_client_count - 1].lock, NULL);
1967
1968 /* WRITE UNLOCK */
1969 pthread_rwlock_unlock(&server_opts.ch_client_lock);
1970
1971 return 0;
1972}
1973
1974API int
Michal Vasko59050372016-11-22 14:33:55 +01001975nc_server_ch_del_client(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko2e6defd2016-10-07 15:48:15 +02001976{
1977 uint16_t i, j;
1978 int ret = -1;
1979
1980 /* WRITE LOCK */
1981 pthread_rwlock_wrlock(&server_opts.ch_client_lock);
1982
Michal Vasko59050372016-11-22 14:33:55 +01001983 if (!name && !ti) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02001984 /* remove all CH clients */
1985 for (i = 0; i < server_opts.ch_client_count; ++i) {
1986 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name);
1987
1988 /* remove all endpoints */
1989 for (j = 0; j < server_opts.ch_clients[i].ch_endpt_count; ++j) {
1990 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].name);
1991 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].address);
1992 }
1993 free(server_opts.ch_clients[i].ch_endpts);
1994
1995 switch (server_opts.ch_clients[i].ti) {
1996#ifdef NC_ENABLED_SSH
1997 case NC_TI_LIBSSH:
1998 nc_server_ssh_clear_opts(server_opts.ch_clients[i].opts.ssh);
1999 free(server_opts.ch_clients[i].opts.ssh);
2000 break;
2001#endif
2002#ifdef NC_ENABLED_TLS
2003 case NC_TI_OPENSSL:
2004 nc_server_tls_clear_opts(server_opts.ch_clients[i].opts.tls);
2005 free(server_opts.ch_clients[i].opts.tls);
2006 break;
2007#endif
2008 default:
2009 ERRINT;
2010 /* won't get here ...*/
2011 break;
2012 }
2013
2014 pthread_mutex_destroy(&server_opts.ch_clients[i].lock);
2015
2016 ret = 0;
2017 }
2018 free(server_opts.ch_clients);
2019 server_opts.ch_clients = NULL;
2020
2021 server_opts.ch_client_count = 0;
2022
2023 } else {
Michal Vasko59050372016-11-22 14:33:55 +01002024 /* remove one client with endpoint(s) or all clients using one protocol */
Michal Vasko2e6defd2016-10-07 15:48:15 +02002025 for (i = 0; i < server_opts.ch_client_count; ++i) {
Michal Vasko59050372016-11-22 14:33:55 +01002026 if ((name && !strcmp(server_opts.ch_clients[i].name, name)) || (!name && (server_opts.ch_clients[i].ti == ti))) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02002027 /* remove endpt */
2028 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].name);
2029
2030 switch (server_opts.ch_clients[i].ti) {
2031#ifdef NC_ENABLED_SSH
2032 case NC_TI_LIBSSH:
2033 nc_server_ssh_clear_opts(server_opts.ch_clients[i].opts.ssh);
2034 free(server_opts.ch_clients[i].opts.ssh);
2035 break;
2036#endif
2037#ifdef NC_ENABLED_TLS
2038 case NC_TI_OPENSSL:
2039 nc_server_tls_clear_opts(server_opts.ch_clients[i].opts.tls);
2040 free(server_opts.ch_clients[i].opts.tls);
2041 break;
2042#endif
2043 default:
2044 ERRINT;
2045 break;
2046 }
2047
2048 /* remove all endpoints */
2049 for (j = 0; j < server_opts.ch_clients[i].ch_endpt_count; ++j) {
2050 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].name);
2051 lydict_remove(server_opts.ctx, server_opts.ch_clients[i].ch_endpts[j].address);
2052 }
2053 free(server_opts.ch_clients[i].ch_endpts);
2054
2055 pthread_mutex_destroy(&server_opts.ch_clients[i].lock);
2056
2057 /* move last client and endpoint(s) to the empty space */
2058 --server_opts.ch_client_count;
2059 if (i < server_opts.ch_client_count) {
2060 memcpy(&server_opts.ch_clients[i], &server_opts.ch_clients[server_opts.ch_client_count],
2061 sizeof *server_opts.ch_clients);
2062 } else if (!server_opts.ch_client_count) {
2063 free(server_opts.ch_clients);
2064 server_opts.ch_clients = NULL;
2065 }
2066
2067 ret = 0;
Michal Vasko59050372016-11-22 14:33:55 +01002068 if (name) {
2069 break;
2070 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002071 }
2072 }
2073 }
2074
2075 /* WRITE UNLOCK */
2076 pthread_rwlock_unlock(&server_opts.ch_client_lock);
2077
2078 return ret;
2079}
2080
2081API int
2082nc_server_ch_client_add_endpt(const char *client_name, const char *endpt_name)
2083{
2084 uint16_t i;
2085 struct nc_ch_client *client;
2086
2087 if (!client_name) {
2088 ERRARG("client_name");
2089 return -1;
2090 } else if (!endpt_name) {
2091 ERRARG("endpt_name");
2092 return -1;
2093 }
2094
2095 /* LOCK */
2096 client = nc_server_ch_client_lock(client_name, 0, NULL);
2097 if (!client) {
2098 return -1;
2099 }
2100
2101 for (i = 0; i < client->ch_endpt_count; ++i) {
2102 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2103 ERR("Call Home client \"%s\" endpoint \"%s\" already exists.", client_name, endpt_name);
2104 /* UNLOCK */
2105 nc_server_ch_client_unlock(client);
2106 return -1;
2107 }
2108 }
2109
2110 ++client->ch_endpt_count;
2111 client->ch_endpts = realloc(client->ch_endpts, client->ch_endpt_count * sizeof *client->ch_endpts);
2112 if (!client->ch_endpts) {
2113 ERRMEM;
2114 /* UNLOCK */
2115 nc_server_ch_client_unlock(client);
2116 return -1;
2117 }
2118
2119 client->ch_endpts[client->ch_endpt_count - 1].name = lydict_insert(server_opts.ctx, endpt_name, 0);
2120 client->ch_endpts[client->ch_endpt_count - 1].address = NULL;
2121 client->ch_endpts[client->ch_endpt_count - 1].port = 0;
2122
2123 /* UNLOCK */
2124 nc_server_ch_client_unlock(client);
2125
2126 return 0;
2127}
2128
2129API int
2130nc_server_ch_client_del_endpt(const char *client_name, const char *endpt_name)
2131{
2132 uint16_t i;
2133 int ret = -1;
2134 struct nc_ch_client *client;
2135
2136 if (!client_name) {
2137 ERRARG("client_name");
2138 return -1;
2139 }
2140
2141 /* LOCK */
2142 client = nc_server_ch_client_lock(client_name, 0, NULL);
2143 if (!client) {
2144 return -1;
2145 }
2146
2147 if (!endpt_name) {
2148 /* remove all endpoints */
2149 for (i = 0; i < client->ch_endpt_count; ++i) {
2150 lydict_remove(server_opts.ctx, client->ch_endpts[i].name);
2151 lydict_remove(server_opts.ctx, client->ch_endpts[i].address);
2152 }
2153 free(client->ch_endpts);
2154 client->ch_endpts = NULL;
2155 client->ch_endpt_count = 0;
2156
2157 ret = 0;
2158 } else {
2159 for (i = 0; i < client->ch_endpt_count; ++i) {
2160 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2161 lydict_remove(server_opts.ctx, client->ch_endpts[i].name);
2162 lydict_remove(server_opts.ctx, client->ch_endpts[i].address);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002163
Michal Vasko4f921012016-10-20 14:07:45 +02002164 /* move last endpoint to the empty space */
2165 --client->ch_endpt_count;
2166 if (i < client->ch_endpt_count) {
2167 memcpy(&client->ch_endpts[i], &client->ch_endpts[client->ch_endpt_count], sizeof *client->ch_endpts);
2168 } else if (!server_opts.ch_client_count) {
2169 free(server_opts.ch_clients);
2170 server_opts.ch_clients = NULL;
2171 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002172
Michal Vasko4f921012016-10-20 14:07:45 +02002173 ret = 0;
2174 break;
2175 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002176 }
2177 }
2178
2179 /* UNLOCK */
2180 nc_server_ch_client_unlock(client);
2181
2182 return ret;
2183}
2184
2185API int
2186nc_server_ch_client_endpt_set_address(const char *client_name, const char *endpt_name, const char *address)
2187{
2188 uint16_t i;
2189 int ret = -1;
2190 struct nc_ch_client *client;
2191
2192 if (!client_name) {
2193 ERRARG("client_name");
2194 return -1;
2195 } else if (!endpt_name) {
2196 ERRARG("endpt_name");
2197 return -1;
2198 } else if (!address) {
2199 ERRARG("address");
2200 return -1;
2201 }
2202
2203 /* LOCK */
2204 client = nc_server_ch_client_lock(client_name, 0, NULL);
2205 if (!client) {
2206 return -1;
2207 }
2208
2209 for (i = 0; i < client->ch_endpt_count; ++i) {
2210 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2211 lydict_remove(server_opts.ctx, client->ch_endpts[i].address);
2212 client->ch_endpts[i].address = lydict_insert(server_opts.ctx, address, 0);
2213
2214 ret = 0;
2215 break;
2216 }
2217 }
2218
2219 /* UNLOCK */
2220 nc_server_ch_client_unlock(client);
2221
2222 if (ret == -1) {
2223 ERR("Call Home client \"%s\" endpoint \"%s\" not found.", client_name, endpt_name);
2224 }
2225
2226 return ret;
2227}
2228
2229API int
2230nc_server_ch_client_endpt_set_port(const char *client_name, const char *endpt_name, uint16_t port)
2231{
2232 uint16_t i;
2233 int ret = -1;
2234 struct nc_ch_client *client;
2235
2236 if (!client_name) {
2237 ERRARG("client_name");
2238 return -1;
2239 } else if (!endpt_name) {
2240 ERRARG("endpt_name");
2241 return -1;
2242 } else if (!port) {
2243 ERRARG("port");
2244 return -1;
2245 }
2246
2247 /* LOCK */
2248 client = nc_server_ch_client_lock(client_name, 0, NULL);
2249 if (!client) {
2250 return -1;
2251 }
2252
2253 for (i = 0; i < client->ch_endpt_count; ++i) {
2254 if (!strcmp(client->ch_endpts[i].name, endpt_name)) {
2255 client->ch_endpts[i].port = port;
2256
2257 ret = 0;
2258 break;
2259 }
2260 }
2261
2262 /* UNLOCK */
2263 nc_server_ch_client_unlock(client);
2264
2265 if (ret == -1) {
2266 ERR("Call Home client \"%s\" endpoint \"%s\" not found.", client_name, endpt_name);
2267 }
2268
2269 return ret;
2270}
2271
2272API int
2273nc_server_ch_client_set_conn_type(const char *client_name, NC_CH_CONN_TYPE conn_type)
2274{
2275 struct nc_ch_client *client;
2276
2277 if (!client_name) {
2278 ERRARG("client_name");
2279 return -1;
2280 } else if (!conn_type) {
2281 ERRARG("conn_type");
2282 return -1;
2283 }
2284
2285 /* LOCK */
2286 client = nc_server_ch_client_lock(client_name, 0, NULL);
2287 if (!client) {
2288 return -1;
2289 }
2290
2291 if (client->conn_type != conn_type) {
2292 client->conn_type = conn_type;
2293
2294 /* set default options */
2295 switch (conn_type) {
2296 case NC_CH_PERSIST:
2297 client->conn.persist.idle_timeout = 86400;
2298 client->conn.persist.ka_max_wait = 30;
2299 client->conn.persist.ka_max_attempts = 3;
2300 break;
2301 case NC_CH_PERIOD:
2302 client->conn.period.idle_timeout = 300;
2303 client->conn.period.reconnect_timeout = 60;
2304 break;
2305 default:
2306 ERRINT;
2307 break;
2308 }
2309 }
2310
2311 /* UNLOCK */
2312 nc_server_ch_client_unlock(client);
2313
2314 return 0;
2315}
2316
2317API int
2318nc_server_ch_client_persist_set_idle_timeout(const char *client_name, uint32_t idle_timeout)
2319{
2320 struct nc_ch_client *client;
2321
2322 if (!client_name) {
2323 ERRARG("client_name");
2324 return -1;
2325 }
2326
2327 /* LOCK */
2328 client = nc_server_ch_client_lock(client_name, 0, NULL);
2329 if (!client) {
2330 return -1;
2331 }
2332
2333 if (client->conn_type != NC_CH_PERSIST) {
2334 ERR("Call Home client \"%s\" is not of persistent connection type.");
2335 /* UNLOCK */
2336 nc_server_ch_client_unlock(client);
2337 return -1;
2338 }
2339
2340 client->conn.persist.idle_timeout = idle_timeout;
2341
2342 /* UNLOCK */
2343 nc_server_ch_client_unlock(client);
2344
2345 return 0;
2346}
2347
2348API int
2349nc_server_ch_client_persist_set_keep_alive_max_wait(const char *client_name, uint16_t max_wait)
2350{
2351 struct nc_ch_client *client;
2352
2353 if (!client_name) {
2354 ERRARG("client_name");
2355 return -1;
2356 } else if (!max_wait) {
2357 ERRARG("max_wait");
2358 return -1;
2359 }
2360
2361 /* LOCK */
2362 client = nc_server_ch_client_lock(client_name, 0, NULL);
2363 if (!client) {
2364 return -1;
2365 }
2366
2367 if (client->conn_type != NC_CH_PERSIST) {
2368 ERR("Call Home client \"%s\" is not of persistent connection type.");
2369 /* UNLOCK */
2370 nc_server_ch_client_unlock(client);
2371 return -1;
2372 }
2373
2374 client->conn.persist.ka_max_wait = max_wait;
2375
2376 /* UNLOCK */
2377 nc_server_ch_client_unlock(client);
2378
2379 return 0;
2380}
2381
2382API int
2383nc_server_ch_client_persist_set_keep_alive_max_attempts(const char *client_name, uint8_t max_attempts)
2384{
2385 struct nc_ch_client *client;
2386
2387 if (!client_name) {
2388 ERRARG("client_name");
2389 return -1;
2390 }
2391
2392 /* LOCK */
2393 client = nc_server_ch_client_lock(client_name, 0, NULL);
2394 if (!client) {
2395 return -1;
2396 }
2397
2398 if (client->conn_type != NC_CH_PERSIST) {
2399 ERR("Call Home client \"%s\" is not of persistent connection type.");
2400 /* UNLOCK */
2401 nc_server_ch_client_unlock(client);
2402 return -1;
2403 }
2404
2405 client->conn.persist.ka_max_attempts = max_attempts;
2406
2407 /* UNLOCK */
2408 nc_server_ch_client_unlock(client);
2409
2410 return 0;
2411}
2412
2413API int
2414nc_server_ch_client_period_set_idle_timeout(const char *client_name, uint16_t idle_timeout)
2415{
2416 struct nc_ch_client *client;
2417
2418 if (!client_name) {
2419 ERRARG("client_name");
2420 return -1;
2421 }
2422
2423 /* LOCK */
2424 client = nc_server_ch_client_lock(client_name, 0, NULL);
2425 if (!client) {
2426 return -1;
2427 }
2428
2429 if (client->conn_type != NC_CH_PERIOD) {
2430 ERR("Call Home client \"%s\" is not of periodic connection type.");
2431 /* UNLOCK */
2432 nc_server_ch_client_unlock(client);
2433 return -1;
2434 }
2435
2436 client->conn.period.idle_timeout = idle_timeout;
2437
2438 /* UNLOCK */
2439 nc_server_ch_client_unlock(client);
2440
2441 return 0;
2442}
2443
2444API int
2445nc_server_ch_client_period_set_reconnect_timeout(const char *client_name, uint16_t reconnect_timeout)
2446{
2447 struct nc_ch_client *client;
2448
2449 if (!client_name) {
2450 ERRARG("client_name");
2451 return -1;
2452 } else if (!reconnect_timeout) {
2453 ERRARG("reconnect_timeout");
2454 return -1;
2455 }
2456
2457 /* LOCK */
2458 client = nc_server_ch_client_lock(client_name, 0, NULL);
2459 if (!client) {
2460 return -1;
2461 }
2462
2463 if (client->conn_type != NC_CH_PERIOD) {
2464 ERR("Call Home client \"%s\" is not of periodic connection type.");
2465 /* UNLOCK */
2466 nc_server_ch_client_unlock(client);
2467 return -1;
2468 }
2469
2470 client->conn.period.reconnect_timeout = reconnect_timeout;
2471
2472 /* UNLOCK */
2473 nc_server_ch_client_unlock(client);
2474
2475 return 0;
2476}
2477
2478API int
2479nc_server_ch_client_set_start_with(const char *client_name, NC_CH_START_WITH start_with)
2480{
2481 struct nc_ch_client *client;
2482
2483 if (!client_name) {
2484 ERRARG("client_name");
2485 return -1;
2486 }
2487
2488 /* LOCK */
2489 client = nc_server_ch_client_lock(client_name, 0, NULL);
2490 if (!client) {
2491 return -1;
2492 }
2493
2494 client->start_with = start_with;
2495
2496 /* UNLOCK */
2497 nc_server_ch_client_unlock(client);
2498
2499 return 0;
2500}
2501
2502API int
2503nc_server_ch_client_set_max_attempts(const char *client_name, uint8_t max_attempts)
2504{
2505 struct nc_ch_client *client;
2506
2507 if (!client_name) {
2508 ERRARG("client_name");
2509 return -1;
2510 } else if (!max_attempts) {
2511 ERRARG("max_attempts");
2512 return -1;
2513 }
2514
2515 /* LOCK */
2516 client = nc_server_ch_client_lock(client_name, 0, NULL);
2517 if (!client) {
2518 return -1;
2519 }
2520
2521 client->max_attempts = max_attempts;
2522
2523 /* UNLOCK */
2524 nc_server_ch_client_unlock(client);
2525
2526 return 0;
2527}
2528
2529/* client lock is expected to be held */
2530static NC_MSG_TYPE
2531nc_connect_ch_client_endpt(struct nc_ch_client *client, struct nc_ch_endpt *endpt, struct nc_session **session)
Michal Vaskob05053d2016-01-22 16:12:06 +01002532{
Michal Vasko71090fc2016-05-24 16:37:28 +02002533 NC_MSG_TYPE msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01002534 int sock, ret;
2535
Michal Vasko2e6defd2016-10-07 15:48:15 +02002536 sock = nc_sock_connect(endpt->address, endpt->port);
Michal Vaskoc61c4492016-01-25 11:13:34 +01002537 if (sock < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02002538 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01002539 }
2540
Michal Vaskoade892d2017-02-22 13:40:35 +01002541 *session = nc_new_session(0);
Michal Vaskob05053d2016-01-22 16:12:06 +01002542 if (!(*session)) {
2543 ERRMEM;
2544 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02002545 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01002546 }
2547 (*session)->status = NC_STATUS_STARTING;
2548 (*session)->side = NC_SERVER;
2549 (*session)->ctx = server_opts.ctx;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002550 (*session)->flags = NC_SESSION_SHAREDCTX;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002551 (*session)->host = lydict_insert(server_opts.ctx, endpt->address, 0);
2552 (*session)->port = endpt->port;
Michal Vaskob05053d2016-01-22 16:12:06 +01002553
2554 /* transport lock */
Michal Vaskob05053d2016-01-22 16:12:06 +01002555 pthread_mutex_init((*session)->ti_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +01002556 pthread_cond_init((*session)->ti_cond, NULL);
2557 *(*session)->ti_inuse = 0;
Michal Vaskob05053d2016-01-22 16:12:06 +01002558
2559 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01002560#ifdef NC_ENABLED_SSH
Michal Vasko2e6defd2016-10-07 15:48:15 +02002561 if (client->ti == NC_TI_LIBSSH) {
2562 (*session)->data = client->opts.ssh;
Michal Vasko0190bc32016-03-02 15:47:49 +01002563 ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01002564 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01002565
Michal Vasko71090fc2016-05-24 16:37:28 +02002566 if (ret < 0) {
2567 msgtype = NC_MSG_ERROR;
2568 goto fail;
2569 } else if (!ret) {
2570 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01002571 goto fail;
2572 }
Michal Vasko3d865d22016-01-28 16:00:53 +01002573 } else
2574#endif
Radek Krejci53691be2016-02-22 13:58:37 +01002575#ifdef NC_ENABLED_TLS
Michal Vasko2e6defd2016-10-07 15:48:15 +02002576 if (client->ti == NC_TI_OPENSSL) {
2577 (*session)->data = client->opts.tls;
Michal Vasko0190bc32016-03-02 15:47:49 +01002578 ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01002579 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01002580
Michal Vasko71090fc2016-05-24 16:37:28 +02002581 if (ret < 0) {
2582 msgtype = NC_MSG_ERROR;
2583 goto fail;
2584 } else if (!ret) {
2585 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01002586 goto fail;
2587 }
Michal Vasko3d865d22016-01-28 16:00:53 +01002588 } else
2589#endif
2590 {
Michal Vaskob05053d2016-01-22 16:12:06 +01002591 ERRINT;
2592 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02002593 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01002594 goto fail;
2595 }
2596
2597 /* assign new SID atomically */
2598 /* LOCK */
2599 pthread_spin_lock(&server_opts.sid_lock);
2600 (*session)->id = server_opts.new_session_id++;
2601 /* UNLOCK */
2602 pthread_spin_unlock(&server_opts.sid_lock);
2603
2604 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02002605 msgtype = nc_handshake(*session);
2606 if (msgtype != NC_MSG_HELLO) {
Michal Vaskob05053d2016-01-22 16:12:06 +01002607 goto fail;
2608 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002609 (*session)->opts.server.session_start = (*session)->opts.server.last_rpc = time(NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01002610 (*session)->status = NC_STATUS_RUNNING;
2611
Michal Vasko71090fc2016-05-24 16:37:28 +02002612 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01002613
2614fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01002615 nc_session_free(*session, NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01002616 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02002617 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01002618}
2619
Michal Vasko2e6defd2016-10-07 15:48:15 +02002620struct nc_ch_client_thread_arg {
2621 char *client_name;
2622 void (*session_clb)(const char *client_name, struct nc_session *new_session);
2623};
2624
2625static struct nc_ch_client *
2626nc_server_ch_client_with_endpt_lock(const char *name)
2627{
2628 struct nc_ch_client *client;
2629
2630 while (1) {
2631 /* LOCK */
2632 client = nc_server_ch_client_lock(name, 0, NULL);
2633 if (!client) {
2634 return NULL;
2635 }
2636 if (client->ch_endpt_count) {
2637 return client;
2638 }
2639 /* no endpoints defined yet */
2640
2641 /* UNLOCK */
2642 nc_server_ch_client_unlock(client);
2643
2644 usleep(NC_CH_NO_ENDPT_WAIT * 1000);
2645 }
2646
2647 return NULL;
2648}
2649
2650static int
2651nc_server_ch_client_thread_session_cond_wait(struct nc_session *session, struct nc_ch_client_thread_arg *data)
2652{
2653 int ret;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002654 uint32_t idle_timeout;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002655 struct timespec ts;
2656 struct nc_ch_client *client;
2657
2658 /* session created, initialize condition */
2659 session->opts.server.ch_lock = malloc(sizeof *session->opts.server.ch_lock);
2660 session->opts.server.ch_cond = malloc(sizeof *session->opts.server.ch_cond);
2661 if (!session->opts.server.ch_lock || !session->opts.server.ch_cond) {
2662 ERRMEM;
2663 nc_session_free(session, NULL);
2664 return -1;
2665 }
2666 pthread_mutex_init(session->opts.server.ch_lock, NULL);
2667 pthread_cond_init(session->opts.server.ch_cond, NULL);
2668
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002669 session->flags |= NC_SESSION_CALLHOME;
2670
Michal Vasko2e6defd2016-10-07 15:48:15 +02002671 /* CH LOCK */
2672 pthread_mutex_lock(session->opts.server.ch_lock);
2673
2674 /* give the session to the user */
2675 data->session_clb(data->client_name, session);
2676
2677 do {
2678 nc_gettimespec(&ts);
Michal Vaskoe39ae6b2017-03-14 09:03:46 +01002679 nc_addtimespec(&ts, NC_CH_NO_ENDPT_WAIT);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002680
2681 ret = pthread_cond_timedwait(session->opts.server.ch_cond, session->opts.server.ch_lock, &ts);
2682 if (ret && (ret != ETIMEDOUT)) {
2683 ERR("Pthread condition timedwait failed (%s).", strerror(ret));
2684 goto ch_client_remove;
2685 }
2686
2687 /* check whether the client was not removed */
2688 /* LOCK */
2689 client = nc_server_ch_client_lock(data->client_name, 0, NULL);
2690 if (!client) {
2691 /* client was removed, finish thread */
2692 VRB("Call Home client \"%s\" removed, but an established session will not be terminated.",
2693 data->client_name);
2694 goto ch_client_remove;
2695 }
2696
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002697 if (client->conn_type == NC_CH_PERSIST) {
2698 /* TODO keep-alives */
2699 idle_timeout = client->conn.persist.idle_timeout;
2700 } else {
2701 idle_timeout = client->conn.period.idle_timeout;
2702 }
2703
Michal Vasko3486a7c2017-03-03 13:28:07 +01002704 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 +02002705 VRB("Call Home client \"%s\" session %u: session idle timeout elapsed.", client->name, session->id);
2706 session->status = NC_STATUS_INVALID;
2707 session->term_reason = NC_SESSION_TERM_TIMEOUT;
2708 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02002709
2710 /* UNLOCK */
2711 nc_server_ch_client_unlock(client);
2712
2713 } while (session->status == NC_STATUS_RUNNING);
2714
2715 /* CH UNLOCK */
2716 pthread_mutex_unlock(session->opts.server.ch_lock);
2717
2718 return 0;
2719
2720ch_client_remove:
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002721 /* make the session a standard one */
2722 pthread_cond_destroy(session->opts.server.ch_cond);
2723 free(session->opts.server.ch_cond);
2724 session->opts.server.ch_cond = NULL;
2725
2726 session->flags &= ~NC_SESSION_CALLHOME;
2727
Michal Vasko2e6defd2016-10-07 15:48:15 +02002728 /* CH UNLOCK */
2729 pthread_mutex_unlock(session->opts.server.ch_lock);
2730
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002731 pthread_mutex_destroy(session->opts.server.ch_lock);
2732 free(session->opts.server.ch_lock);
2733 session->opts.server.ch_lock = NULL;
2734
Michal Vasko2e6defd2016-10-07 15:48:15 +02002735 return 1;
2736}
2737
2738static void *
2739nc_ch_client_thread(void *arg)
2740{
2741 struct nc_ch_client_thread_arg *data = (struct nc_ch_client_thread_arg *)arg;
2742 NC_MSG_TYPE msgtype;
2743 uint8_t cur_attempts = 0;
2744 uint16_t i;
Michal Vasko9550cf12017-03-21 15:33:58 +01002745 char *cur_endpt_name = NULL;
Michal Vasko2e6defd2016-10-07 15:48:15 +02002746 struct nc_ch_endpt *cur_endpt;
2747 struct nc_session *session;
2748 struct nc_ch_client *client;
2749
2750 /* LOCK */
2751 client = nc_server_ch_client_with_endpt_lock(data->client_name);
2752 if (!client) {
2753 goto cleanup;
2754 }
2755
2756 cur_endpt = &client->ch_endpts[0];
2757 cur_endpt_name = strdup(cur_endpt->name);
2758
Michal Vasko29af44b2016-10-13 10:59:55 +02002759 VRB("Call Home client \"%s\" connecting...", data->client_name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002760 while (1) {
2761 msgtype = nc_connect_ch_client_endpt(client, cur_endpt, &session);
2762
2763 if (msgtype == NC_MSG_HELLO) {
2764 /* UNLOCK */
2765 nc_server_ch_client_unlock(client);
2766
Michal Vasko29af44b2016-10-13 10:59:55 +02002767 VRB("Call Home client \"%s\" session %u established.", data->client_name, session->id);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002768 if (nc_server_ch_client_thread_session_cond_wait(session, data)) {
2769 goto cleanup;
2770 }
Michal Vasko29af44b2016-10-13 10:59:55 +02002771 VRB("Call Home client \"%s\" session terminated, reconnecting...", client->name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002772
2773 /* LOCK */
2774 client = nc_server_ch_client_with_endpt_lock(data->client_name);
2775 if (!client) {
2776 goto cleanup;
2777 }
2778
2779 /* session changed status -> it was disconnected for whatever reason,
2780 * persistent connection immediately tries to reconnect, periodic waits some first */
2781 if (client->conn_type == NC_CH_PERIOD) {
Michal Vasko2e6defd2016-10-07 15:48:15 +02002782 /* UNLOCK */
2783 nc_server_ch_client_unlock(client);
2784
2785 /* TODO wake up sometimes to check for new notifications */
2786 usleep(client->conn.period.reconnect_timeout * 60 * 1000000);
2787
2788 /* LOCK */
2789 client = nc_server_ch_client_with_endpt_lock(data->client_name);
2790 if (!client) {
2791 goto cleanup;
2792 }
2793 }
2794
2795 /* set next endpoint to try */
2796 if (client->start_with == NC_CH_FIRST_LISTED) {
2797 cur_endpt = &client->ch_endpts[0];
2798 free(cur_endpt_name);
2799 cur_endpt_name = strdup(cur_endpt->name);
2800 } /* else we keep the current one */
2801 } else {
Michal Vasko6bb116b2016-10-26 13:53:46 +02002802 /* UNLOCK */
2803 nc_server_ch_client_unlock(client);
2804
Michal Vasko2e6defd2016-10-07 15:48:15 +02002805 /* session was not created */
Michal Vaskoc4bc5812016-10-13 10:59:36 +02002806 usleep(NC_CH_ENDPT_FAIL_WAIT * 1000);
2807
Michal Vasko6bb116b2016-10-26 13:53:46 +02002808 /* LOCK */
2809 client = nc_server_ch_client_with_endpt_lock(data->client_name);
2810 if (!client) {
2811 goto cleanup;
2812 }
2813
Michal Vasko2e6defd2016-10-07 15:48:15 +02002814 ++cur_attempts;
2815 if (cur_attempts == client->max_attempts) {
2816 for (i = 0; i < client->ch_endpt_count; ++i) {
2817 if (!strcmp(client->ch_endpts[i].name, cur_endpt_name)) {
2818 break;
2819 }
2820 }
2821 if (i < client->ch_endpt_count - 1) {
2822 /* just go to the next endpoint */
2823 cur_endpt = &client->ch_endpts[i + 1];
2824 free(cur_endpt_name);
2825 cur_endpt_name = strdup(cur_endpt->name);
2826 } else {
2827 /* cur_endpoint was removed or is the last, either way start with the first one */
2828 cur_endpt = &client->ch_endpts[0];
2829 free(cur_endpt_name);
2830 cur_endpt_name = strdup(cur_endpt->name);
2831 }
2832
2833 cur_attempts = 0;
2834 } /* else we keep the current one */
2835 }
2836 }
2837
2838cleanup:
2839 VRB("Call Home client \"%s\" thread exit.", data->client_name);
Michal Vasko9550cf12017-03-21 15:33:58 +01002840 free(cur_endpt_name);
Michal Vasko2e6defd2016-10-07 15:48:15 +02002841 free(data->client_name);
2842 free(data);
2843 return NULL;
2844}
2845
2846API int
2847nc_connect_ch_client_dispatch(const char *client_name,
2848 void (*session_clb)(const char *client_name, struct nc_session *new_session)) {
2849 int ret;
2850 pthread_t tid;
2851 struct nc_ch_client_thread_arg *arg;
2852
2853 if (!client_name) {
2854 ERRARG("client_name");
2855 return -1;
2856 } else if (!session_clb) {
2857 ERRARG("session_clb");
2858 return -1;
2859 }
2860
2861 arg = malloc(sizeof *arg);
2862 if (!arg) {
2863 ERRMEM;
2864 return -1;
2865 }
2866 arg->client_name = strdup(client_name);
2867 if (!arg->client_name) {
2868 ERRMEM;
2869 free(arg);
2870 return -1;
2871 }
2872 arg->session_clb = session_clb;
2873
2874 ret = pthread_create(&tid, NULL, nc_ch_client_thread, arg);
2875 if (ret) {
2876 ERR("Creating a new thread failed (%s).", strerror(ret));
2877 free(arg->client_name);
2878 free(arg);
2879 return -1;
2880 }
2881 /* the thread now manages arg */
2882
2883 pthread_detach(tid);
2884
2885 return 0;
2886}
2887
Radek Krejci53691be2016-02-22 13:58:37 +01002888#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskof8352352016-05-24 09:11:36 +02002889
Michal Vaskoe8e07702017-03-15 10:19:30 +01002890API int
2891nc_server_endpt_count(void)
2892{
2893 return server_opts.endpt_count;
2894}
2895
Michal Vaskoc45ebd32016-05-25 11:17:36 +02002896API time_t
2897nc_session_get_start_time(const struct nc_session *session)
Michal Vaskof8352352016-05-24 09:11:36 +02002898{
Michal Vasko2e6defd2016-10-07 15:48:15 +02002899 if (!session || (session->side != NC_SERVER)) {
Michal Vaskof8352352016-05-24 09:11:36 +02002900 ERRARG("session");
Michal Vaskoc45ebd32016-05-25 11:17:36 +02002901 return 0;
Michal Vaskof8352352016-05-24 09:11:36 +02002902 }
2903
Michal Vasko2e6defd2016-10-07 15:48:15 +02002904 return session->opts.server.session_start;
Michal Vaskof8352352016-05-24 09:11:36 +02002905}
Michal Vasko3486a7c2017-03-03 13:28:07 +01002906
2907API void
2908nc_session_set_notif_status(struct nc_session *session, int notif_status)
2909{
2910 if (!session || (session->side != NC_SERVER)) {
2911 ERRARG("session");
2912 return;
2913 }
2914
2915 session->opts.server.ntf_status = (notif_status ? 1 : 0);
2916}
2917
2918API int
2919nc_session_get_notif_status(const struct nc_session *session)
2920{
2921 if (!session || (session->side != NC_SERVER)) {
2922 ERRARG("session");
2923 return 0;
2924 }
2925
2926 return session->opts.server.ntf_status;
Michal Vasko086311b2016-01-08 09:53:11 +01002927}