blob: ec3a37df3b155dea8053f8740c0a979e2177aa9f [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 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 */
22
23#include <stdint.h>
24#include <stdlib.h>
25#include <errno.h>
26#include <string.h>
27#include <poll.h>
28#include <sys/types.h>
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <arpa/inet.h>
32#include <unistd.h>
Michal Vaskob48aa812016-01-18 14:13:09 +010033#include <pthread.h>
Michal Vasko11d142a2016-01-19 15:58:24 +010034#include <time.h>
Michal Vasko086311b2016-01-08 09:53:11 +010035
Michal Vasko1a38c862016-01-15 15:50:07 +010036#include "libnetconf.h"
Michal Vasko086311b2016-01-08 09:53:11 +010037#include "session_server.h"
38
Michal Vaskob48aa812016-01-18 14:13:09 +010039struct nc_server_opts server_opts = {
Michal Vasko3031aae2016-01-27 16:07:18 +010040 .endpt_array_lock = PTHREAD_RWLOCK_INITIALIZER
Michal Vaskob48aa812016-01-18 14:13:09 +010041};
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010042
Michal Vasko3031aae2016-01-27 16:07:18 +010043extern struct nc_server_ssh_opts ssh_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010044extern pthread_mutex_t ssh_ch_opts_lock;
45
Michal Vasko3031aae2016-01-27 16:07:18 +010046extern struct nc_server_tls_opts tls_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010047extern pthread_mutex_t tls_ch_opts_lock;
Michal Vasko3031aae2016-01-27 16:07:18 +010048
49struct nc_endpt *
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010050nc_server_endpt_lock(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko3031aae2016-01-27 16:07:18 +010051{
52 uint16_t i;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010053 struct nc_endpt *endpt = NULL;
54
55 /* READ LOCK */
56 pthread_rwlock_rdlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010057
58 for (i = 0; i < server_opts.endpt_count; ++i) {
59 if ((server_opts.binds[i].ti == ti) && !strcmp(server_opts.endpts[i].name, name)) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010060 endpt = &server_opts.endpts[i];
61 break;
Michal Vasko3031aae2016-01-27 16:07:18 +010062 }
63 }
64
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010065 if (!endpt) {
66 ERR("Endpoint \"%s\" was not found.", name);
67 /* READ UNLOCK */
68 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
69 return NULL;
70 }
71
72 /* ENDPT LOCK */
73 pthread_mutex_lock(&endpt->endpt_lock);
74
75 return endpt;
76}
77
78void
79nc_server_endpt_unlock(struct nc_endpt *endpt)
80{
81 /* ENDPT UNLOCK */
82 pthread_mutex_unlock(&endpt->endpt_lock);
83
84 /* READ UNLOCK */
85 pthread_rwlock_rdlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010086}
Michal Vasko086311b2016-01-08 09:53:11 +010087
Michal Vasko1a38c862016-01-15 15:50:07 +010088API void
89nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason)
90{
91 if (!session || !reason) {
92 ERRARG;
93 return;
94 }
95
96 session->term_reason = reason;
97}
98
Michal Vasko086311b2016-01-08 09:53:11 +010099int
Michal Vaskof05562c2016-01-20 12:06:43 +0100100nc_sock_listen(const char *address, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +0100101{
102 const int optVal = 1;
103 const socklen_t optLen = sizeof(optVal);
104 int is_ipv4, sock;
105 struct sockaddr_storage saddr;
106
107 struct sockaddr_in *saddr4;
108 struct sockaddr_in6 *saddr6;
109
110
111 if (!strchr(address, ':')) {
112 is_ipv4 = 1;
113 } else {
114 is_ipv4 = 0;
115 }
116
117 sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0);
118 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100119 ERR("Failed to create socket (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100120 goto fail;
121 }
122
123 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, optLen)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100124 ERR("Could not set socket SO_REUSEADDR socket option (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100125 goto fail;
126 }
127
128 bzero(&saddr, sizeof(struct sockaddr_storage));
129 if (is_ipv4) {
130 saddr4 = (struct sockaddr_in *)&saddr;
131
132 saddr4->sin_family = AF_INET;
133 saddr4->sin_port = htons(port);
134
135 if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100136 ERR("Failed to convert IPv4 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100137 goto fail;
138 }
139
140 if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100141 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100142 goto fail;
143 }
144
145 } else {
146 saddr6 = (struct sockaddr_in6 *)&saddr;
147
148 saddr6->sin6_family = AF_INET6;
149 saddr6->sin6_port = htons(port);
150
151 if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100152 ERR("Failed to convert IPv6 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100153 goto fail;
154 }
155
156 if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100157 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100158 goto fail;
159 }
160 }
161
Michal Vaskofb89d772016-01-08 12:25:35 +0100162 if (listen(sock, NC_REVERSE_QUEUE) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100163 ERR("Unable to start listening on \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100164 goto fail;
165 }
166
167 return sock;
168
169fail:
170 if (sock > -1) {
171 close(sock);
172 }
173
174 return -1;
175}
176
177int
Michal Vasko3031aae2016-01-27 16:07:18 +0100178nc_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 +0100179{
180 uint16_t i;
181 struct pollfd *pfd;
182 struct sockaddr_storage saddr;
183 socklen_t saddr_len = sizeof(saddr);
184 int ret, sock = -1;
185
186 pfd = malloc(bind_count * sizeof *pfd);
187 for (i = 0; i < bind_count; ++i) {
188 pfd[i].fd = binds[i].sock;
189 pfd[i].events = POLLIN;
190 pfd[i].revents = 0;
191 }
192
193 /* poll for a new connection */
194 errno = 0;
195 ret = poll(pfd, bind_count, timeout);
196 if (!ret) {
197 /* we timeouted */
198 free(pfd);
199 return 0;
200 } else if (ret == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100201 ERR("Poll failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100202 free(pfd);
203 return -1;
204 }
205
206 for (i = 0; i < bind_count; ++i) {
207 if (pfd[i].revents & POLLIN) {
208 sock = pfd[i].fd;
209 break;
210 }
211 }
212 free(pfd);
213
214 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100215 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100216 return -1;
217 }
218
219 ret = accept(sock, (struct sockaddr *)&saddr, &saddr_len);
Michal Vasko3f6cc4a2016-01-21 15:58:53 +0100220 if (ret < 0) {
Michal Vaskod083db62016-01-19 10:31:29 +0100221 ERR("Accept failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100222 return -1;
223 }
224
Michal Vasko3031aae2016-01-27 16:07:18 +0100225 if (idx) {
226 *idx = i;
Michal Vasko9e036d52016-01-08 10:49:26 +0100227 }
228
Michal Vasko086311b2016-01-08 09:53:11 +0100229 /* host was requested */
230 if (host) {
231 if (saddr.ss_family == AF_INET) {
232 *host = malloc(15);
233 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&saddr)->sin_addr.s_addr, *host, 15)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100234 ERR("inet_ntop failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100235 free(*host);
236 *host = NULL;
237 }
238
239 if (port) {
240 *port = ntohs(((struct sockaddr_in *)&saddr)->sin_port);
241 }
242 } else if (saddr.ss_family == AF_INET6) {
243 *host = malloc(40);
244 if (!inet_ntop(AF_INET6, ((struct sockaddr_in6 *)&saddr)->sin6_addr.s6_addr, *host, 40)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100245 ERR("inet_ntop failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100246 free(*host);
247 *host = NULL;
248 }
249
250 if (port) {
251 *port = ntohs(((struct sockaddr_in6 *)&saddr)->sin6_port);
252 }
253 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100254 ERR("Source host of an unknown protocol family.");
Michal Vasko086311b2016-01-08 09:53:11 +0100255 }
256 }
257
258 return ret;
259}
260
Michal Vasko05ba9df2016-01-13 14:40:27 +0100261static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100262nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *UNUSED(session))
Michal Vasko05ba9df2016-01-13 14:40:27 +0100263{
264 const char *identifier = NULL, *version = NULL, *format = NULL;
265 char *model_data = NULL;
266 const struct lys_module *module;
267 struct nc_server_error *err;
268 struct lyd_node *child, *data = NULL;
Michal Vasko11d142a2016-01-19 15:58:24 +0100269 const struct lys_node *sdata = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100270
271 LY_TREE_FOR(rpc->child, child) {
272 if (!strcmp(child->schema->name, "identifier")) {
273 identifier = ((struct lyd_node_leaf_list *)child)->value_str;
274 } else if (!strcmp(child->schema->name, "version")) {
275 version = ((struct lyd_node_leaf_list *)child)->value_str;
276 } else if (!strcmp(child->schema->name, "format")) {
277 format = ((struct lyd_node_leaf_list *)child)->value_str;
278 }
279 }
280
281 /* check version */
282 if (version && (strlen(version) != 10) && strcmp(version, "1.0")) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100283 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
284 nc_err_set_msg(err, "The requested version is not supported.", "en");
285 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100286 }
287
288 /* check and get module with the name identifier */
289 module = ly_ctx_get_module(server_opts.ctx, identifier, version);
290 if (!module) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100291 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
292 nc_err_set_msg(err, "The requested schema was not found.", "en");
293 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100294 }
295
296 /* check format */
297 if (!format || !strcmp(format, "yang")) {
298 lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL);
299 } else if (!strcmp(format, "yin")) {
300 lys_print_mem(&model_data, module, LYS_OUT_YIN, NULL);
301 } else {
Michal Vasko1a38c862016-01-15 15:50:07 +0100302 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
303 nc_err_set_msg(err, "The requested format is not supported.", "en");
304 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100305 }
306
Michal Vasko0473c4c2016-01-19 10:40:06 +0100307 module = ly_ctx_get_module(server_opts.ctx, "ietf-netconf-monitoring", NULL);
308 if (module) {
309 sdata = lys_get_node(module, "/get-schema/output/data");
310 }
311 if (model_data && sdata) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100312 nc_ctx_lock(-1, NULL);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100313 data = lyd_output_new_anyxml(sdata, model_data);
Michal Vasko11d142a2016-01-19 15:58:24 +0100314 nc_ctx_unlock();
Michal Vasko05ba9df2016-01-13 14:40:27 +0100315 }
316 free(model_data);
317 if (!data) {
318 ERRINT;
319 return NULL;
320 }
321
322 return nc_server_reply_data(data, NC_PARAMTYPE_FREE);
323}
324
325static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100326nc_clb_default_close_session(struct lyd_node *UNUSED(rpc), struct nc_session *session)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100327{
Michal Vasko428087d2016-01-14 16:04:28 +0100328 session->term_reason = NC_SESSION_TERM_CLOSED;
329 return nc_server_reply_ok();
Michal Vasko05ba9df2016-01-13 14:40:27 +0100330}
331
Michal Vasko086311b2016-01-08 09:53:11 +0100332API int
333nc_server_init(struct ly_ctx *ctx)
334{
Michal Vasko05ba9df2016-01-13 14:40:27 +0100335 const struct lys_node *rpc;
Michal Vasko0473c4c2016-01-19 10:40:06 +0100336 const struct lys_module *mod;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100337
Michal Vasko086311b2016-01-08 09:53:11 +0100338 if (!ctx) {
339 ERRARG;
340 return -1;
341 }
342
Michal Vasko05ba9df2016-01-13 14:40:27 +0100343 /* set default <get-schema> callback if not specified */
Michal Vasko0473c4c2016-01-19 10:40:06 +0100344 rpc = NULL;
345 mod = ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL);
346 if (mod) {
347 rpc = lys_get_node(mod, "/get-schema");
348 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100349 if (rpc && !rpc->private) {
350 lys_set_private(rpc, nc_clb_default_get_schema);
351 }
352
353 /* set default <close-session> callback if not specififed */
Michal Vasko0473c4c2016-01-19 10:40:06 +0100354 rpc = NULL;
355 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
356 if (mod) {
357 rpc = lys_get_node(mod, "/close-session");
358 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100359 if (rpc && !rpc->private) {
360 lys_set_private(rpc, nc_clb_default_close_session);
361 }
362
Michal Vasko086311b2016-01-08 09:53:11 +0100363 server_opts.ctx = ctx;
Michal Vaskob48aa812016-01-18 14:13:09 +0100364
365 server_opts.new_session_id = 1;
366 pthread_spin_init(&server_opts.sid_lock, PTHREAD_PROCESS_PRIVATE);
367
Michal Vasko086311b2016-01-08 09:53:11 +0100368 return 0;
369}
370
Michal Vaskob48aa812016-01-18 14:13:09 +0100371API void
372nc_server_destroy(void)
373{
374 pthread_spin_destroy(&server_opts.sid_lock);
375
376#if defined(ENABLE_SSH) || defined(ENABLE_TLS)
Michal Vasko3031aae2016-01-27 16:07:18 +0100377 nc_server_del_endpt(NULL, 0);
Michal Vaskob48aa812016-01-18 14:13:09 +0100378#endif
379}
380
Michal Vasko086311b2016-01-08 09:53:11 +0100381API int
382nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported)
383{
384 if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)
385 || (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT)))) {
386 ERRARG;
387 return -1;
388 }
389
390 server_opts.wd_basic_mode = basic_mode;
391 server_opts.wd_also_supported = also_supported;
392 return 0;
393}
394
Michal Vasko1a38c862016-01-15 15:50:07 +0100395API void
Michal Vasko086311b2016-01-08 09:53:11 +0100396nc_server_set_capab_interleave(int interleave_support)
397{
398 if (interleave_support) {
399 server_opts.interleave_capab = 1;
400 } else {
401 server_opts.interleave_capab = 0;
402 }
Michal Vasko086311b2016-01-08 09:53:11 +0100403}
404
Michal Vasko1a38c862016-01-15 15:50:07 +0100405API void
Michal Vasko086311b2016-01-08 09:53:11 +0100406nc_server_set_hello_timeout(uint16_t hello_timeout)
407{
Michal Vasko086311b2016-01-08 09:53:11 +0100408 server_opts.hello_timeout = hello_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100409}
410
Michal Vasko1a38c862016-01-15 15:50:07 +0100411API void
Michal Vasko086311b2016-01-08 09:53:11 +0100412nc_server_set_idle_timeout(uint16_t idle_timeout)
413{
Michal Vasko086311b2016-01-08 09:53:11 +0100414 server_opts.idle_timeout = idle_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100415}
416
417API int
Michal Vasko1a38c862016-01-15 15:50:07 +0100418nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +0100419{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100420 if (!server_opts.ctx || (fdin < 0) || (fdout < 0) || !username || !session) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100421 ERRARG;
422 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100423 }
424
425 /* prepare session structure */
Michal Vasko1a38c862016-01-15 15:50:07 +0100426 *session = calloc(1, sizeof **session);
427 if (!(*session)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100428 ERRMEM;
Michal Vasko1a38c862016-01-15 15:50:07 +0100429 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100430 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100431 (*session)->status = NC_STATUS_STARTING;
432 (*session)->side = NC_SERVER;
Michal Vasko086311b2016-01-08 09:53:11 +0100433
434 /* transport specific data */
Michal Vasko1a38c862016-01-15 15:50:07 +0100435 (*session)->ti_type = NC_TI_FD;
436 (*session)->ti.fd.in = fdin;
437 (*session)->ti.fd.out = fdout;
Michal Vasko086311b2016-01-08 09:53:11 +0100438
439 /* assign context (dicionary needed for handshake) */
Michal Vasko1a38c862016-01-15 15:50:07 +0100440 (*session)->flags = NC_SESSION_SHAREDCTX;
441 (*session)->ctx = server_opts.ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100442
Michal Vaskob48aa812016-01-18 14:13:09 +0100443 /* assign new SID atomically */
444 pthread_spin_lock(&server_opts.sid_lock);
445 (*session)->id = server_opts.new_session_id++;
446 pthread_spin_unlock(&server_opts.sid_lock);
447
Michal Vasko086311b2016-01-08 09:53:11 +0100448 /* NETCONF handshake */
Michal Vasko1a38c862016-01-15 15:50:07 +0100449 if (nc_handshake(*session)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100450 goto fail;
451 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100452 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100453 (*session)->last_rpc = time(NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100454
Michal Vasko1a38c862016-01-15 15:50:07 +0100455 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100456
457fail:
Michal Vasko1a38c862016-01-15 15:50:07 +0100458 nc_session_free(*session);
459 *session = NULL;
460 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100461}
Michal Vasko9e036d52016-01-08 10:49:26 +0100462
Michal Vasko428087d2016-01-14 16:04:28 +0100463API struct nc_pollsession *
464nc_ps_new(void)
465{
466 return calloc(1, sizeof(struct nc_pollsession));
467}
468
469API void
470nc_ps_free(struct nc_pollsession *ps)
471{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100472 if (!ps) {
473 return;
474 }
475
Michal Vasko3a715132016-01-21 15:40:31 +0100476 free(ps->pfds);
Michal Vasko428087d2016-01-14 16:04:28 +0100477 free(ps->sessions);
478 free(ps);
479}
480
481API int
482nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session)
483{
484 if (!ps || !session) {
485 ERRARG;
486 return -1;
487 }
488
489 ++ps->session_count;
Michal Vasko3a715132016-01-21 15:40:31 +0100490 ps->pfds = realloc(ps->pfds, ps->session_count * sizeof *ps->pfds);
Michal Vasko428087d2016-01-14 16:04:28 +0100491 ps->sessions = realloc(ps->sessions, ps->session_count * sizeof *ps->sessions);
492
493 switch (session->ti_type) {
494 case NC_TI_FD:
Michal Vasko3a715132016-01-21 15:40:31 +0100495 ps->pfds[ps->session_count - 1].fd = session->ti.fd.in;
Michal Vasko428087d2016-01-14 16:04:28 +0100496 break;
497
498#ifdef ENABLE_SSH
499 case NC_TI_LIBSSH:
Michal Vasko3a715132016-01-21 15:40:31 +0100500 ps->pfds[ps->session_count - 1].fd = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100501 break;
502#endif
503
504#ifdef ENABLE_TLS
505 case NC_TI_OPENSSL:
Michal Vasko3a715132016-01-21 15:40:31 +0100506 ps->pfds[ps->session_count - 1].fd = SSL_get_rfd(session->ti.tls);
Michal Vasko428087d2016-01-14 16:04:28 +0100507 break;
508#endif
509
510 default:
511 ERRINT;
512 return -1;
513 }
Michal Vasko3a715132016-01-21 15:40:31 +0100514 ps->pfds[ps->session_count - 1].events = POLLIN;
515 ps->pfds[ps->session_count - 1].revents = 0;
516 ps->sessions[ps->session_count - 1] = session;
Michal Vasko428087d2016-01-14 16:04:28 +0100517
518 return 0;
519}
520
521API int
522nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session)
523{
524 uint16_t i;
525
526 if (!ps || !session) {
527 ERRARG;
528 return -1;
529 }
530
531 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100532 if (ps->sessions[i] == session) {
Michal Vasko428087d2016-01-14 16:04:28 +0100533 --ps->session_count;
Michal Vasko3a715132016-01-21 15:40:31 +0100534 ps->sessions[i] = ps->sessions[ps->session_count];
535 memcpy(&ps->pfds[i], &ps->pfds[ps->session_count], sizeof *ps->pfds);
Michal Vasko428087d2016-01-14 16:04:28 +0100536 return 0;
537 }
538 }
539
Michal Vaskof0537d82016-01-29 14:42:38 +0100540 return -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100541}
542
543/* must be called holding the session lock! */
544static NC_MSG_TYPE
545nc_recv_rpc(struct nc_session *session, struct nc_server_rpc **rpc)
546{
547 struct lyxml_elem *xml = NULL;
548 NC_MSG_TYPE msgtype;
549
550 if (!session || !rpc) {
551 ERRARG;
552 return NC_MSG_ERROR;
553 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100554 ERR("Session %u: invalid session to receive RPCs.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100555 return NC_MSG_ERROR;
556 }
557
558 msgtype = nc_read_msg(session, &xml);
559
560 switch (msgtype) {
561 case NC_MSG_RPC:
562 *rpc = malloc(sizeof **rpc);
Michal Vaskoca4a2422016-02-02 12:17:14 +0100563
Michal Vasko11d142a2016-01-19 15:58:24 +0100564 nc_ctx_lock(-1, NULL);
Michal Vasko428087d2016-01-14 16:04:28 +0100565 (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPC);
Michal Vaskoca4a2422016-02-02 12:17:14 +0100566 if (!(*rpc)->tree) {
567 ERR("Session %u: received message failed to be parsed into a known RPC.", session->id);
568 msgtype = NC_MSG_NONE;
569 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100570 nc_ctx_unlock();
Michal Vaskoca4a2422016-02-02 12:17:14 +0100571
Michal Vasko428087d2016-01-14 16:04:28 +0100572 (*rpc)->root = xml;
573 break;
574 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100575 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100576 goto error;
577 case NC_MSG_REPLY:
Michal Vasko81614ee2016-02-02 12:20:14 +0100578 ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100579 goto error;
580 case NC_MSG_NOTIF:
Michal Vasko81614ee2016-02-02 12:20:14 +0100581 ERR("Session %u: received <notification> from a NETCONF client.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100582 goto error;
583 default:
584 /* NC_MSG_ERROR - pass it out;
585 * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg()
586 */
587 break;
588 }
589
590 return msgtype;
591
592error:
593 /* cleanup */
594 lyxml_free(server_opts.ctx, xml);
595
596 return NC_MSG_ERROR;
597}
598
599/* must be called holding the session lock! */
600static NC_MSG_TYPE
601nc_send_reply(struct nc_session *session, struct nc_server_rpc *rpc)
602{
603 nc_rpc_clb clb;
604 struct nc_server_reply *reply;
605 int ret;
606
607 /* no callback, reply with a not-implemented error */
Michal Vaskoca4a2422016-02-02 12:17:14 +0100608 if (!rpc->tree || !rpc->tree->schema->private) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100609 reply = nc_server_reply_err(nc_err(NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT));
Michal Vasko428087d2016-01-14 16:04:28 +0100610 } else {
611 clb = (nc_rpc_clb)rpc->tree->schema->private;
612 reply = clb(rpc->tree, session);
613 }
614
615 if (!reply) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100616 reply = nc_server_reply_err(nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP));
Michal Vasko428087d2016-01-14 16:04:28 +0100617 }
618
619 ret = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply);
620
621 /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */
622 if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) {
623 session->status = NC_STATUS_INVALID;
624 }
625
626 if (ret == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100627 ERR("Session %u: failed to write reply.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100628 nc_server_reply_free(reply);
629 return NC_MSG_ERROR;
630 }
631 nc_server_reply_free(reply);
632
633 return NC_MSG_REPLY;
634}
635
636API int
637nc_ps_poll(struct nc_pollsession *ps, int timeout)
638{
639 int ret;
Michal Vasko3512e402016-01-28 16:22:34 +0100640 uint16_t i;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100641 time_t cur_time;
Michal Vasko428087d2016-01-14 16:04:28 +0100642 NC_MSG_TYPE msgtype;
643 struct nc_session *session;
644 struct nc_server_rpc *rpc;
Michal Vasko96164bf2016-01-21 15:41:58 +0100645 struct timespec old_ts;
Michal Vasko428087d2016-01-14 16:04:28 +0100646
647 if (!ps || !ps->session_count) {
648 ERRARG;
649 return -1;
650 }
651
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100652 cur_time = time(NULL);
653
Michal Vasko428087d2016-01-14 16:04:28 +0100654 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100655 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
656 ERR("Session %u: session not running.", ps->sessions[i]->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100657 return -1;
658 }
Michal Vaskobd8ef262016-01-20 11:09:27 +0100659
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100660 /* TODO invalidate only sessions without subscription */
Michal Vasko3a715132016-01-21 15:40:31 +0100661 if (server_opts.idle_timeout && (ps->sessions[i]->last_rpc + server_opts.idle_timeout >= cur_time)) {
662 ERR("Session %u: session idle timeout elapsed.", ps->sessions[i]->id);
663 ps->sessions[i]->status = NC_STATUS_INVALID;
664 ps->sessions[i]->term_reason = NC_SESSION_TERM_TIMEOUT;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100665 return 3;
666 }
667
Michal Vasko3a715132016-01-21 15:40:31 +0100668 if (ps->pfds[i].revents) {
Michal Vaskobd8ef262016-01-20 11:09:27 +0100669 break;
670 }
Michal Vasko428087d2016-01-14 16:04:28 +0100671 }
672
673 if (timeout > 0) {
674 clock_gettime(CLOCK_MONOTONIC_RAW, &old_ts);
675 }
676
Michal Vaskobd8ef262016-01-20 11:09:27 +0100677 if (i == ps->session_count) {
Michal Vasko3512e402016-01-28 16:22:34 +0100678#ifdef ENABLE_SSH
Michal Vasko3a715132016-01-21 15:40:31 +0100679retry_poll:
Michal Vasko3512e402016-01-28 16:22:34 +0100680#endif
Michal Vaskobd8ef262016-01-20 11:09:27 +0100681 /* no leftover event */
682 i = 0;
Michal Vasko3a715132016-01-21 15:40:31 +0100683 ret = poll(ps->pfds, ps->session_count, timeout);
Michal Vaskobd8ef262016-01-20 11:09:27 +0100684 if (ret < 1) {
685 return ret;
686 }
Michal Vasko428087d2016-01-14 16:04:28 +0100687 }
688
Michal Vaskobd8ef262016-01-20 11:09:27 +0100689 /* find the first fd with POLLIN, we don't care if there are more now */
690 for (; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100691 if (ps->pfds[i].revents & POLLHUP) {
692 ERR("Session %u: communication socket unexpectedly closed.", ps->sessions[i]->id);
693 ps->sessions[i]->status = NC_STATUS_INVALID;
694 ps->sessions[i]->term_reason = NC_SESSION_TERM_DROPPED;
Michal Vaskobd8ef262016-01-20 11:09:27 +0100695 return 3;
Michal Vasko3a715132016-01-21 15:40:31 +0100696 } else if (ps->pfds[i].revents & POLLERR) {
697 ERR("Session %u: communication socket error.", ps->sessions[i]->id);
698 ps->sessions[i]->status = NC_STATUS_INVALID;
699 ps->sessions[i]->term_reason = NC_SESSION_TERM_OTHER;
Michal Vaskobd8ef262016-01-20 11:09:27 +0100700 return 3;
Michal Vasko3a715132016-01-21 15:40:31 +0100701 } else if (ps->pfds[i].revents & POLLIN) {
Michal Vasko428087d2016-01-14 16:04:28 +0100702#ifdef ENABLE_SSH
Michal Vasko96164bf2016-01-21 15:41:58 +0100703 if (ps->sessions[i]->ti_type == NC_TI_LIBSSH) {
Michal Vasko3512e402016-01-28 16:22:34 +0100704 uint16_t j;
705
Michal Vasko96164bf2016-01-21 15:41:58 +0100706 /* things are not that simple with SSH... */
707 ret = nc_ssh_pollin(ps->sessions[i], &timeout);
708
709 /* clear POLLIN on sessions sharing this session's SSH session */
710 if ((ret == 1) || (ret >= 4)) {
711 for (j = i + 1; j < ps->session_count; ++j) {
712 if (ps->pfds[j].fd == ps->pfds[i].fd) {
713 ps->pfds[j].revents = 0;
714 }
715 }
716 }
717
718 /* actual event happened */
719 if ((ret <= 0) || (ret >= 3)) {
720 ps->pfds[i].revents = 0;
721 return ret;
722
723 /* event occurred on some other channel */
724 } else if (ret == 2) {
725 ps->pfds[i].revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100726 if (i == ps->session_count - 1) {
727 /* last session and it is not the right channel, ... */
728 if (timeout > 0) {
729 /* ... decrease timeout, wait it all out and try again, last time */
Michal Vasko96164bf2016-01-21 15:41:58 +0100730 nc_subtract_elapsed(&timeout, &old_ts);
731 usleep(timeout * 1000);
732 timeout = 0;
733 goto retry_poll;
Michal Vasko428087d2016-01-14 16:04:28 +0100734 } else if (!timeout) {
735 /* ... timeout is 0, so that is it */
736 return 0;
737 } else {
738 /* ... retry polling reasonable time apart */
739 usleep(NC_TIMEOUT_STEP);
740 goto retry_poll;
741 }
742 }
743 /* check other sessions */
744 continue;
Michal Vasko428087d2016-01-14 16:04:28 +0100745 }
746 }
747#endif /* ENABLE_SSH */
748
Michal Vaskobd8ef262016-01-20 11:09:27 +0100749 /* we are going to process it now */
Michal Vasko3a715132016-01-21 15:40:31 +0100750 ps->pfds[i].revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100751 break;
752 }
753 }
754
755 if (i == ps->session_count) {
756 ERRINT;
757 return -1;
758 }
759
760 /* this is the session with some data available for reading */
Michal Vasko3a715132016-01-21 15:40:31 +0100761 session = ps->sessions[i];
Michal Vasko428087d2016-01-14 16:04:28 +0100762
763 if (timeout > 0) {
Michal Vasko96164bf2016-01-21 15:41:58 +0100764 nc_subtract_elapsed(&timeout, &old_ts);
Michal Vasko428087d2016-01-14 16:04:28 +0100765 }
766
Michal Vaskobd8ef262016-01-20 11:09:27 +0100767 /* reading an RPC and sending a reply must be atomic (no other RPC should be read) */
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100768 ret = nc_timedlock(session->ti_lock, timeout, NULL);
769 if (ret != 1) {
770 /* error or timeout */
771 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100772 }
773
774 msgtype = nc_recv_rpc(session, &rpc);
775 if (msgtype == NC_MSG_ERROR) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100776 pthread_mutex_unlock(session->ti_lock);
Michal Vasko428087d2016-01-14 16:04:28 +0100777 if (session->status != NC_STATUS_RUNNING) {
Michal Vaskobd8ef262016-01-20 11:09:27 +0100778 return 3;
Michal Vasko428087d2016-01-14 16:04:28 +0100779 }
780 return -1;
781 }
782
Michal Vaskoca4a2422016-02-02 12:17:14 +0100783 /* NC_MSG_NONE is not a real (known) RPC */
784 if (msgtype == NC_MSG_RPC) {
785 session->last_rpc = time(NULL);
786 }
787
Michal Vasko428087d2016-01-14 16:04:28 +0100788 /* process RPC */
789 msgtype = nc_send_reply(session, rpc);
790
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100791 pthread_mutex_unlock(session->ti_lock);
Michal Vasko428087d2016-01-14 16:04:28 +0100792
793 if (msgtype == NC_MSG_ERROR) {
Michal Vaskoca4a2422016-02-02 12:17:14 +0100794 nc_server_rpc_free(rpc, server_opts.ctx);
Michal Vasko428087d2016-01-14 16:04:28 +0100795 return -1;
796 }
Michal Vaskoca4a2422016-02-02 12:17:14 +0100797 nc_server_rpc_free(rpc, server_opts.ctx);
Michal Vaskobd8ef262016-01-20 11:09:27 +0100798
Michal Vaskobd8b4e12016-01-22 16:11:20 +0100799 /* status change takes precedence over leftover events (return 2) */
800 if (session->status != NC_STATUS_RUNNING) {
801 return 3;
802 }
803
Michal Vaskobd8ef262016-01-20 11:09:27 +0100804 /* is there some other socket waiting? */
805 for (++i; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100806 if (ps->pfds[i].revents) {
Michal Vaskobd8ef262016-01-20 11:09:27 +0100807 return 2;
808 }
809 }
810
Michal Vasko428087d2016-01-14 16:04:28 +0100811 return 1;
812}
813
Michal Vaskod09eae62016-02-01 10:32:52 +0100814API void
815nc_ps_clear(struct nc_pollsession *ps)
816{
817 uint16_t i;
818 struct nc_session *session;
819
Michal Vasko9a25e932016-02-01 10:36:42 +0100820 if (!ps) {
821 ERRARG;
822 return;
823 }
824
Michal Vaskod09eae62016-02-01 10:32:52 +0100825 for (i = 0; i < ps->session_count; ) {
826 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
827 session = ps->sessions[i];
828 nc_ps_del_session(ps, session);
829 nc_session_free(session);
830 continue;
831 }
832
833 ++i;
834 }
835}
836
Michal Vasko11d142a2016-01-19 15:58:24 +0100837API int
838nc_ctx_lock(int timeout, int *elapsed)
839{
840 return nc_timedlock(&server_opts.ctx_lock, timeout, elapsed);
841}
842
843API int
844nc_ctx_unlock(void)
845{
846 int ret;
847
848 ret = pthread_mutex_unlock(&server_opts.ctx_lock);
849
850 if (ret) {
851 ERR("Mutex unlock failed (%s).", strerror(ret));
852 return -1;
853 }
854
855 return 0;
856}
857
Michal Vasko9e036d52016-01-08 10:49:26 +0100858#if defined(ENABLE_SSH) || defined(ENABLE_TLS)
859
Michal Vasko3031aae2016-01-27 16:07:18 +0100860int
861nc_server_add_endpt_listen(const char *name, const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +0100862{
863 int sock;
Michal Vasko3031aae2016-01-27 16:07:18 +0100864 uint16_t i;
Michal Vasko08a629a2016-02-02 12:20:47 +0100865#ifdef ENABLE_SSH
866 struct nc_server_ssh_opts *ssh_opts;
867#endif
Michal Vasko9e036d52016-01-08 10:49:26 +0100868
Michal Vasko3031aae2016-01-27 16:07:18 +0100869 if (!name || !address || !port) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100870 ERRARG;
871 return -1;
872 }
873
Michal Vasko3031aae2016-01-27 16:07:18 +0100874 /* READ LOCK */
875 pthread_rwlock_rdlock(&server_opts.endpt_array_lock);
876
877 /* check name uniqueness */
878 for (i = 0; i < server_opts.endpt_count; ++i) {
879 if (!strcmp(server_opts.endpts[i].name, name)) {
880 ERR("Endpoint \"%s\" already exists.", name);
Michal Vasko9faf1c82016-02-01 13:26:19 +0100881 /* READ UNLOCK */
882 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100883 return -1;
884 }
885 }
886
887 /* READ UNLOCK */
888 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
889
Michal Vasko9e036d52016-01-08 10:49:26 +0100890 sock = nc_sock_listen(address, port);
891 if (sock == -1) {
892 return -1;
893 }
894
Michal Vasko3031aae2016-01-27 16:07:18 +0100895 /* WRITE LOCK */
896 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +0100897
Michal Vasko3031aae2016-01-27 16:07:18 +0100898 ++server_opts.endpt_count;
899 server_opts.binds = realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds);
900 server_opts.endpts = realloc(server_opts.endpts, server_opts.endpt_count * sizeof *server_opts.endpts);
Michal Vasko9e036d52016-01-08 10:49:26 +0100901
Michal Vasko11d142a2016-01-19 15:58:24 +0100902 nc_ctx_lock(-1, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100903 server_opts.endpts[server_opts.endpt_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
904 server_opts.binds[server_opts.endpt_count - 1].address = lydict_insert(server_opts.ctx, address, 0);
Michal Vasko11d142a2016-01-19 15:58:24 +0100905 nc_ctx_unlock();
Michal Vasko3031aae2016-01-27 16:07:18 +0100906 server_opts.binds[server_opts.endpt_count - 1].port = port;
907 server_opts.binds[server_opts.endpt_count - 1].sock = sock;
908 server_opts.binds[server_opts.endpt_count - 1].ti = ti;
909 switch (ti) {
910#ifdef ENABLE_SSH
911 case NC_TI_LIBSSH:
Michal Vasko08a629a2016-02-02 12:20:47 +0100912 ssh_opts = calloc(1, sizeof *ssh_opts);
913 /* set default values */
914 ssh_opts->auth_methods = NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
915 ssh_opts->auth_attempts = 3;
916 ssh_opts->auth_timeout = 10;
917
918 server_opts.endpts[server_opts.endpt_count - 1].ti_opts = ssh_opts;
Michal Vasko3031aae2016-01-27 16:07:18 +0100919 break;
920#endif
921#ifdef ENABLE_TLS
922 case NC_TI_OPENSSL:
923 server_opts.endpts[server_opts.endpt_count - 1].ti_opts = calloc(1, sizeof(struct nc_server_tls_opts));
924 break;
925#endif
926 default:
927 ERRINT;
928 server_opts.endpts[server_opts.endpt_count - 1].ti_opts = NULL;
929 break;
930 }
931 pthread_mutex_init(&server_opts.endpts[server_opts.endpt_count - 1].endpt_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +0100932
Michal Vasko3031aae2016-01-27 16:07:18 +0100933 /* WRITE UNLOCK */
934 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +0100935
Michal Vasko9e036d52016-01-08 10:49:26 +0100936 return 0;
937}
938
Michal Vasko3031aae2016-01-27 16:07:18 +0100939int
Michal Vaskoda514772016-02-01 11:32:01 +0100940nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
941{
942 struct nc_endpt *endpt;
943 struct nc_bind *bind = NULL;
944 uint16_t i;
945 int sock;
946
947 if (!endpt_name || (!address && !port) || (address && port) || !ti) {
948 ERRARG;
949 return -1;
950 }
951
952 endpt = nc_server_endpt_lock(endpt_name, ti);
953 if (!endpt) {
954 return -1;
955 }
956
957 /* we need to learn the index, to get the bind :-/ */
958 for (i = 0; i < server_opts.endpt_count; ++i) {
959 if (&server_opts.endpts[i] == endpt) {
960 bind = &server_opts.binds[i];
961 }
962 }
963 if (!bind) {
964 ERRINT;
Michal Vasko7a93af72016-02-01 16:00:15 +0100965 nc_server_endpt_unlock(endpt);
Michal Vaskoda514772016-02-01 11:32:01 +0100966 return -1;
967 }
968
969 if (address) {
970 sock = nc_sock_listen(address, bind->port);
971 } else {
972 sock = nc_sock_listen(bind->address, port);
973 }
974 if (sock == -1) {
Michal Vasko7a93af72016-02-01 16:00:15 +0100975 nc_server_endpt_unlock(endpt);
Michal Vaskoda514772016-02-01 11:32:01 +0100976 return -1;
977 }
978
979 /* close old socket, update parameters */
980 close(bind->sock);
981 bind->sock = sock;
982 if (address) {
983 lydict_remove(server_opts.ctx, bind->address);
984 bind->address = lydict_insert(server_opts.ctx, address, 0);
985 } else {
986 bind->port = port;
987 }
988
Michal Vasko7a93af72016-02-01 16:00:15 +0100989 nc_server_endpt_unlock(endpt);
Michal Vaskoda514772016-02-01 11:32:01 +0100990 return 0;
991}
992
993int
Michal Vasko3031aae2016-01-27 16:07:18 +0100994nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +0100995{
996 uint32_t i;
997 int ret = -1;
998
Michal Vasko3031aae2016-01-27 16:07:18 +0100999 /* WRITE LOCK */
1000 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001001
Michal Vasko3031aae2016-01-27 16:07:18 +01001002 if (!name && !ti) {
1003 /* remove all */
Michal Vasko11d142a2016-01-19 15:58:24 +01001004 nc_ctx_lock(-1, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001005 for (i = 0; i < server_opts.endpt_count; ++i) {
1006 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko11d142a2016-01-19 15:58:24 +01001007 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
Michal Vasko3031aae2016-01-27 16:07:18 +01001008 close(server_opts.binds[i].sock);
1009 pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock);
1010 switch (server_opts.binds[i].ti) {
1011#ifdef ENABLE_SSH
1012 case NC_TI_LIBSSH:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001013 nc_server_ssh_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001014 break;
1015#endif
1016#ifdef ENABLE_TLS
1017 case NC_TI_OPENSSL:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001018 nc_server_tls_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001019 break;
1020#endif
1021 default:
1022 ERRINT;
1023 break;
1024 }
1025 free(server_opts.endpts[i].ti_opts);
Michal Vasko9e036d52016-01-08 10:49:26 +01001026
Michal Vasko9e036d52016-01-08 10:49:26 +01001027 ret = 0;
1028 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001029 nc_ctx_unlock();
Michal Vasko3031aae2016-01-27 16:07:18 +01001030 free(server_opts.endpts);
1031 server_opts.endpts = NULL;
1032 server_opts.endpt_count = 0;
1033
Michal Vasko1a38c862016-01-15 15:50:07 +01001034 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001035 /* remove one name endpoint or all ti endpoints */
1036 for (i = 0; i < server_opts.endpt_count; ++i) {
1037 if ((server_opts.binds[i].ti == ti) &&
1038 (!name || !strcmp(server_opts.endpts[i].name, name))) {
1039
Michal Vasko11d142a2016-01-19 15:58:24 +01001040 nc_ctx_lock(-1, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001041 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko11d142a2016-01-19 15:58:24 +01001042 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
1043 nc_ctx_unlock();
Michal Vasko3031aae2016-01-27 16:07:18 +01001044 close(server_opts.binds[i].sock);
1045 pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock);
1046 switch (server_opts.binds[i].ti) {
1047#ifdef ENABLE_SSH
1048 case NC_TI_LIBSSH:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001049 nc_server_ssh_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001050 break;
1051#endif
1052#ifdef ENABLE_TLS
1053 case NC_TI_OPENSSL:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001054 nc_server_tls_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001055 break;
1056#endif
1057 default:
1058 ERRINT;
1059 break;
1060 }
1061 free(server_opts.endpts[i].ti_opts);
Michal Vasko1a38c862016-01-15 15:50:07 +01001062
Michal Vasko3031aae2016-01-27 16:07:18 +01001063 --server_opts.endpt_count;
Michal Vaskoc0256492016-02-02 12:19:06 +01001064 if (i < server_opts.endpt_count) {
1065 memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
1066 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
1067 } else if (!server_opts.endpt_count) {
1068 free(server_opts.binds);
1069 server_opts.binds = NULL;
1070 free(server_opts.endpts);
1071 server_opts.endpts = NULL;
1072 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001073
1074 ret = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001075
1076 if (name) {
1077 /* one name endpoint removed, they are unique, we're done */
1078 break;
1079 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001080 }
1081 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001082 }
1083
Michal Vasko3031aae2016-01-27 16:07:18 +01001084 /* WRITE UNLOCK */
1085 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001086
Michal Vasko9e036d52016-01-08 10:49:26 +01001087 return ret;
1088}
1089
Michal Vasko1a38c862016-01-15 15:50:07 +01001090API int
1091nc_accept(int timeout, struct nc_session **session)
Michal Vasko9e036d52016-01-08 10:49:26 +01001092{
Michal Vasko1a38c862016-01-15 15:50:07 +01001093 int sock, ret;
Michal Vasko5c2f7952016-01-22 13:16:31 +01001094 char *host = NULL;
Michal Vasko3031aae2016-01-27 16:07:18 +01001095 uint16_t port, idx;
Michal Vasko9e036d52016-01-08 10:49:26 +01001096
Michal Vasko3031aae2016-01-27 16:07:18 +01001097 if (!server_opts.ctx || !server_opts.endpt_count || !session) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001098 ERRARG;
Michal Vasko1a38c862016-01-15 15:50:07 +01001099 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001100 }
1101
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001102 /* READ LOCK */
1103 pthread_rwlock_rdlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001104
Michal Vasko3031aae2016-01-27 16:07:18 +01001105 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &idx);
Michal Vaskob48aa812016-01-18 14:13:09 +01001106
Michal Vasko50456e82016-02-02 12:16:08 +01001107 if (ret < 1) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001108 /* READ UNLOCK */
Michal Vasko3031aae2016-01-27 16:07:18 +01001109 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001110 return ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001111 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001112 sock = ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001113
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001114 /* ENDPT LOCK */
1115 pthread_mutex_lock(&server_opts.endpts[idx].endpt_lock);
1116
Michal Vasko1a38c862016-01-15 15:50:07 +01001117 *session = calloc(1, sizeof **session);
Michal Vasko686aa312016-01-21 15:58:18 +01001118 if (!(*session)) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001119 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001120 close(sock);
Michal Vasko5c2f7952016-01-22 13:16:31 +01001121 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001122 ret = -1;
1123 goto fail;
Michal Vasko9e036d52016-01-08 10:49:26 +01001124 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001125 (*session)->status = NC_STATUS_STARTING;
1126 (*session)->side = NC_SERVER;
1127 (*session)->ctx = server_opts.ctx;
1128 (*session)->flags = NC_SESSION_SHAREDCTX;
Michal Vasko11d142a2016-01-19 15:58:24 +01001129 nc_ctx_lock(-1, NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001130 (*session)->host = lydict_insert_zc(server_opts.ctx, host);
Michal Vasko11d142a2016-01-19 15:58:24 +01001131 nc_ctx_unlock();
Michal Vasko1a38c862016-01-15 15:50:07 +01001132 (*session)->port = port;
Michal Vasko9e036d52016-01-08 10:49:26 +01001133
1134 /* transport lock */
Michal Vasko1a38c862016-01-15 15:50:07 +01001135 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1136 if (!(*session)->ti_lock) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001137 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001138 close(sock);
Michal Vasko1a38c862016-01-15 15:50:07 +01001139 ret = -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001140 goto fail;
1141 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001142 pthread_mutex_init((*session)->ti_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001143
Michal Vasko3031aae2016-01-27 16:07:18 +01001144 (*session)->ti_opts = server_opts.endpts[idx].ti_opts;
1145
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001146 /* sock gets assigned to session or closed */
Michal Vasko3d865d22016-01-28 16:00:53 +01001147#ifdef ENABLE_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001148 if (server_opts.binds[idx].ti == NC_TI_LIBSSH) {
1149 ret = nc_accept_ssh_session(*session, sock, timeout);
Michal Vasko1a38c862016-01-15 15:50:07 +01001150 if (ret < 1) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001151 goto fail;
1152 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001153 } else
1154#endif
1155#ifdef ENABLE_TLS
1156 if (server_opts.binds[idx].ti == NC_TI_OPENSSL) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001157 ret = nc_accept_tls_session(*session, sock, timeout);
Michal Vasko1a38c862016-01-15 15:50:07 +01001158 if (ret < 1) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001159 goto fail;
1160 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001161 } else
1162#endif
1163 {
Michal Vasko9e036d52016-01-08 10:49:26 +01001164 ERRINT;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001165 close(sock);
Michal Vasko1a38c862016-01-15 15:50:07 +01001166 ret = -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001167 goto fail;
1168 }
1169
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001170 /* ENDPT UNLOCK */
Michal Vaskod8e271a2016-02-01 16:00:28 +01001171 pthread_mutex_unlock(&server_opts.endpts[idx].endpt_lock);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001172
1173 /* READ UNLOCK */
Michal Vasko3031aae2016-01-27 16:07:18 +01001174 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1175
Michal Vaskob48aa812016-01-18 14:13:09 +01001176 /* assign new SID atomically */
1177 /* LOCK */
1178 pthread_spin_lock(&server_opts.sid_lock);
1179 (*session)->id = server_opts.new_session_id++;
1180 /* UNLOCK */
1181 pthread_spin_unlock(&server_opts.sid_lock);
1182
Michal Vasko9e036d52016-01-08 10:49:26 +01001183 /* NETCONF handshake */
Michal Vasko1a38c862016-01-15 15:50:07 +01001184 if (nc_handshake(*session)) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001185 nc_session_free(*session);
1186 *session = NULL;
1187 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001188 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001189 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko9e036d52016-01-08 10:49:26 +01001190
Michal Vasko1a38c862016-01-15 15:50:07 +01001191 return 1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001192
1193fail:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001194 /* ENDPT UNLOCK */
Michal Vasko9faf1c82016-02-01 13:26:19 +01001195 pthread_mutex_unlock(&server_opts.endpts[idx].endpt_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001196 /* WRITE UNLOCK */
1197 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1198
Michal Vasko1a38c862016-01-15 15:50:07 +01001199 nc_session_free(*session);
1200 *session = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001201 return ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001202}
1203
Michal Vasko3031aae2016-01-27 16:07:18 +01001204int
Michal Vaskob05053d2016-01-22 16:12:06 +01001205nc_connect_callhome(const char *host, uint16_t port, NC_TRANSPORT_IMPL ti, int timeout, struct nc_session **session)
1206{
1207 int sock, ret;
1208
Michal Vaskoc61c4492016-01-25 11:13:34 +01001209 if (!host || !port || !ti || !session) {
1210 ERRARG;
1211 return -1;
1212 }
1213
Michal Vaskob05053d2016-01-22 16:12:06 +01001214 sock = nc_sock_connect(host, port);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001215 if (sock < 0) {
1216 return -1;
Michal Vaskob05053d2016-01-22 16:12:06 +01001217 }
1218
1219 *session = calloc(1, sizeof **session);
1220 if (!(*session)) {
1221 ERRMEM;
1222 close(sock);
1223 return -1;
1224 }
1225 (*session)->status = NC_STATUS_STARTING;
1226 (*session)->side = NC_SERVER;
1227 (*session)->ctx = server_opts.ctx;
1228 (*session)->flags = NC_SESSION_SHAREDCTX | NC_SESSION_CALLHOME;
1229 nc_ctx_lock(-1, NULL);
1230 (*session)->host = lydict_insert(server_opts.ctx, host, 0);
1231 nc_ctx_unlock();
1232 (*session)->port = port;
1233
1234 /* transport lock */
1235 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1236 if (!(*session)->ti_lock) {
1237 ERRMEM;
1238 close(sock);
1239 ret = -1;
1240 goto fail;
1241 }
1242 pthread_mutex_init((*session)->ti_lock, NULL);
1243
1244 /* sock gets assigned to session or closed */
Michal Vasko3d865d22016-01-28 16:00:53 +01001245#ifdef ENABLE_SSH
Michal Vaskob05053d2016-01-22 16:12:06 +01001246 if (ti == NC_TI_LIBSSH) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001247 /* OPTS LOCK */
1248 pthread_mutex_lock(&ssh_ch_opts_lock);
1249
Michal Vasko3031aae2016-01-27 16:07:18 +01001250 (*session)->ti_opts = &ssh_ch_opts;
1251 ret = nc_accept_ssh_session(*session, sock, timeout);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001252 (*session)->ti_opts = NULL;
1253
1254 /* OPTS UNLOCK */
1255 pthread_mutex_unlock(&ssh_ch_opts_lock);
1256
Michal Vaskob05053d2016-01-22 16:12:06 +01001257 if (ret < 1) {
1258 goto fail;
1259 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001260 } else
1261#endif
1262#ifdef ENABLE_TLS
1263 if (ti == NC_TI_OPENSSL) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001264 /* OPTS LOCK */
1265 pthread_mutex_lock(&tls_ch_opts_lock);
1266
Michal Vasko3031aae2016-01-27 16:07:18 +01001267 (*session)->ti_opts = &tls_ch_opts;
1268 ret = nc_accept_tls_session(*session, sock, timeout);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001269 (*session)->ti_opts = NULL;
1270
1271 /* OPTS UNLOCK */
1272 pthread_mutex_unlock(&tls_ch_opts_lock);
1273
Michal Vaskob05053d2016-01-22 16:12:06 +01001274 if (ret < 1) {
1275 goto fail;
1276 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001277 } else
1278#endif
1279 {
Michal Vaskob05053d2016-01-22 16:12:06 +01001280 ERRINT;
1281 close(sock);
1282 ret = -1;
1283 goto fail;
1284 }
1285
1286 /* assign new SID atomically */
1287 /* LOCK */
1288 pthread_spin_lock(&server_opts.sid_lock);
1289 (*session)->id = server_opts.new_session_id++;
1290 /* UNLOCK */
1291 pthread_spin_unlock(&server_opts.sid_lock);
1292
1293 /* NETCONF handshake */
1294 if (nc_handshake(*session)) {
1295 ret = -1;
1296 goto fail;
1297 }
1298 (*session)->status = NC_STATUS_RUNNING;
1299
1300 return 1;
1301
1302fail:
1303 nc_session_free(*session);
1304 *session = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001305 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +01001306}
1307
Michal Vasko9e036d52016-01-08 10:49:26 +01001308#endif /* ENABLE_SSH || ENABLE_TLS */