blob: b1e677016db01ca1a3b59855e4751498fed2b967 [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 *
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 */
14
15#include <stdint.h>
16#include <stdlib.h>
17#include <errno.h>
18#include <string.h>
19#include <poll.h>
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24#include <unistd.h>
Michal Vaskob48aa812016-01-18 14:13:09 +010025#include <pthread.h>
Michal Vasko11d142a2016-01-19 15:58:24 +010026#include <time.h>
Michal Vasko086311b2016-01-08 09:53:11 +010027
Michal Vasko1a38c862016-01-15 15:50:07 +010028#include "libnetconf.h"
Michal Vasko086311b2016-01-08 09:53:11 +010029#include "session_server.h"
30
Michal Vaskob48aa812016-01-18 14:13:09 +010031struct nc_server_opts server_opts = {
Michal Vasko3031aae2016-01-27 16:07:18 +010032 .endpt_array_lock = PTHREAD_RWLOCK_INITIALIZER
Michal Vaskob48aa812016-01-18 14:13:09 +010033};
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010034
Michal Vasko3031aae2016-01-27 16:07:18 +010035extern struct nc_server_ssh_opts ssh_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010036extern pthread_mutex_t ssh_ch_opts_lock;
37
Michal Vasko3031aae2016-01-27 16:07:18 +010038extern struct nc_server_tls_opts tls_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010039extern pthread_mutex_t tls_ch_opts_lock;
Michal Vasko3031aae2016-01-27 16:07:18 +010040
41struct nc_endpt *
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010042nc_server_endpt_lock(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko3031aae2016-01-27 16:07:18 +010043{
44 uint16_t i;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010045 struct nc_endpt *endpt = NULL;
46
47 /* READ LOCK */
48 pthread_rwlock_rdlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010049
50 for (i = 0; i < server_opts.endpt_count; ++i) {
51 if ((server_opts.binds[i].ti == ti) && !strcmp(server_opts.endpts[i].name, name)) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010052 endpt = &server_opts.endpts[i];
53 break;
Michal Vasko3031aae2016-01-27 16:07:18 +010054 }
55 }
56
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010057 if (!endpt) {
58 ERR("Endpoint \"%s\" was not found.", name);
59 /* READ UNLOCK */
60 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
61 return NULL;
62 }
63
64 /* ENDPT LOCK */
65 pthread_mutex_lock(&endpt->endpt_lock);
66
67 return endpt;
68}
69
70void
71nc_server_endpt_unlock(struct nc_endpt *endpt)
72{
73 /* ENDPT UNLOCK */
74 pthread_mutex_unlock(&endpt->endpt_lock);
75
76 /* READ UNLOCK */
Michal Vasko27562ad2016-02-02 15:50:39 +010077 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010078}
Michal Vasko086311b2016-01-08 09:53:11 +010079
Michal Vasko1a38c862016-01-15 15:50:07 +010080API void
81nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason)
82{
83 if (!session || !reason) {
84 ERRARG;
85 return;
86 }
87
88 session->term_reason = reason;
89}
90
Michal Vasko086311b2016-01-08 09:53:11 +010091int
Michal Vaskof05562c2016-01-20 12:06:43 +010092nc_sock_listen(const char *address, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +010093{
94 const int optVal = 1;
95 const socklen_t optLen = sizeof(optVal);
96 int is_ipv4, sock;
97 struct sockaddr_storage saddr;
98
99 struct sockaddr_in *saddr4;
100 struct sockaddr_in6 *saddr6;
101
102
103 if (!strchr(address, ':')) {
104 is_ipv4 = 1;
105 } else {
106 is_ipv4 = 0;
107 }
108
109 sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0);
110 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100111 ERR("Failed to create socket (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100112 goto fail;
113 }
114
115 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, optLen)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100116 ERR("Could not set socket SO_REUSEADDR socket option (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100117 goto fail;
118 }
119
120 bzero(&saddr, sizeof(struct sockaddr_storage));
121 if (is_ipv4) {
122 saddr4 = (struct sockaddr_in *)&saddr;
123
124 saddr4->sin_family = AF_INET;
125 saddr4->sin_port = htons(port);
126
127 if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100128 ERR("Failed to convert IPv4 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100129 goto fail;
130 }
131
132 if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100133 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100134 goto fail;
135 }
136
137 } else {
138 saddr6 = (struct sockaddr_in6 *)&saddr;
139
140 saddr6->sin6_family = AF_INET6;
141 saddr6->sin6_port = htons(port);
142
143 if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100144 ERR("Failed to convert IPv6 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100145 goto fail;
146 }
147
148 if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100149 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100150 goto fail;
151 }
152 }
153
Michal Vaskofb89d772016-01-08 12:25:35 +0100154 if (listen(sock, NC_REVERSE_QUEUE) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100155 ERR("Unable to start listening on \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100156 goto fail;
157 }
158
159 return sock;
160
161fail:
162 if (sock > -1) {
163 close(sock);
164 }
165
166 return -1;
167}
168
169int
Michal Vasko3031aae2016-01-27 16:07:18 +0100170nc_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 +0100171{
172 uint16_t i;
173 struct pollfd *pfd;
174 struct sockaddr_storage saddr;
175 socklen_t saddr_len = sizeof(saddr);
176 int ret, sock = -1;
177
178 pfd = malloc(bind_count * sizeof *pfd);
179 for (i = 0; i < bind_count; ++i) {
180 pfd[i].fd = binds[i].sock;
181 pfd[i].events = POLLIN;
182 pfd[i].revents = 0;
183 }
184
185 /* poll for a new connection */
186 errno = 0;
187 ret = poll(pfd, bind_count, timeout);
188 if (!ret) {
189 /* we timeouted */
190 free(pfd);
191 return 0;
192 } else if (ret == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100193 ERR("Poll failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100194 free(pfd);
195 return -1;
196 }
197
198 for (i = 0; i < bind_count; ++i) {
199 if (pfd[i].revents & POLLIN) {
200 sock = pfd[i].fd;
201 break;
202 }
203 }
204 free(pfd);
205
206 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100207 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100208 return -1;
209 }
210
211 ret = accept(sock, (struct sockaddr *)&saddr, &saddr_len);
Michal Vasko3f6cc4a2016-01-21 15:58:53 +0100212 if (ret < 0) {
Michal Vaskod083db62016-01-19 10:31:29 +0100213 ERR("Accept failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100214 return -1;
215 }
216
Michal Vasko3031aae2016-01-27 16:07:18 +0100217 if (idx) {
218 *idx = i;
Michal Vasko9e036d52016-01-08 10:49:26 +0100219 }
220
Michal Vasko086311b2016-01-08 09:53:11 +0100221 /* host was requested */
222 if (host) {
223 if (saddr.ss_family == AF_INET) {
224 *host = malloc(15);
225 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&saddr)->sin_addr.s_addr, *host, 15)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100226 ERR("inet_ntop failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100227 free(*host);
228 *host = NULL;
229 }
230
231 if (port) {
232 *port = ntohs(((struct sockaddr_in *)&saddr)->sin_port);
233 }
234 } else if (saddr.ss_family == AF_INET6) {
235 *host = malloc(40);
236 if (!inet_ntop(AF_INET6, ((struct sockaddr_in6 *)&saddr)->sin6_addr.s6_addr, *host, 40)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100237 ERR("inet_ntop failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100238 free(*host);
239 *host = NULL;
240 }
241
242 if (port) {
243 *port = ntohs(((struct sockaddr_in6 *)&saddr)->sin6_port);
244 }
245 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100246 ERR("Source host of an unknown protocol family.");
Michal Vasko086311b2016-01-08 09:53:11 +0100247 }
248 }
249
250 return ret;
251}
252
Michal Vasko05ba9df2016-01-13 14:40:27 +0100253static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100254nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *UNUSED(session))
Michal Vasko05ba9df2016-01-13 14:40:27 +0100255{
256 const char *identifier = NULL, *version = NULL, *format = NULL;
257 char *model_data = NULL;
258 const struct lys_module *module;
259 struct nc_server_error *err;
260 struct lyd_node *child, *data = NULL;
Michal Vasko11d142a2016-01-19 15:58:24 +0100261 const struct lys_node *sdata = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100262
263 LY_TREE_FOR(rpc->child, child) {
264 if (!strcmp(child->schema->name, "identifier")) {
265 identifier = ((struct lyd_node_leaf_list *)child)->value_str;
266 } else if (!strcmp(child->schema->name, "version")) {
267 version = ((struct lyd_node_leaf_list *)child)->value_str;
268 } else if (!strcmp(child->schema->name, "format")) {
269 format = ((struct lyd_node_leaf_list *)child)->value_str;
270 }
271 }
272
273 /* check version */
274 if (version && (strlen(version) != 10) && strcmp(version, "1.0")) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100275 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
276 nc_err_set_msg(err, "The requested version is not supported.", "en");
277 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100278 }
279
280 /* check and get module with the name identifier */
281 module = ly_ctx_get_module(server_opts.ctx, identifier, version);
282 if (!module) {
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 schema was not found.", "en");
285 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100286 }
287
288 /* check format */
289 if (!format || !strcmp(format, "yang")) {
290 lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL);
291 } else if (!strcmp(format, "yin")) {
292 lys_print_mem(&model_data, module, LYS_OUT_YIN, NULL);
293 } else {
Michal Vasko1a38c862016-01-15 15:50:07 +0100294 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
295 nc_err_set_msg(err, "The requested format is not supported.", "en");
296 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100297 }
298
Michal Vaskofea54dc2016-02-17 13:12:16 +0100299 sdata = ly_ctx_get_node(server_opts.ctx, "/ietf-netconf-monitoring:get-schema/output/data");
Michal Vasko0473c4c2016-01-19 10:40:06 +0100300 if (model_data && sdata) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100301 data = lyd_output_new_anyxml(sdata, model_data);
302 }
303 free(model_data);
304 if (!data) {
305 ERRINT;
306 return NULL;
307 }
308
309 return nc_server_reply_data(data, NC_PARAMTYPE_FREE);
310}
311
312static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100313nc_clb_default_close_session(struct lyd_node *UNUSED(rpc), struct nc_session *session)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100314{
Michal Vasko428087d2016-01-14 16:04:28 +0100315 session->term_reason = NC_SESSION_TERM_CLOSED;
316 return nc_server_reply_ok();
Michal Vasko05ba9df2016-01-13 14:40:27 +0100317}
318
Michal Vasko086311b2016-01-08 09:53:11 +0100319API int
320nc_server_init(struct ly_ctx *ctx)
321{
Michal Vasko05ba9df2016-01-13 14:40:27 +0100322 const struct lys_node *rpc;
323
Michal Vasko086311b2016-01-08 09:53:11 +0100324 if (!ctx) {
325 ERRARG;
326 return -1;
327 }
328
Michal Vasko05ba9df2016-01-13 14:40:27 +0100329 /* set default <get-schema> callback if not specified */
Michal Vaskofea54dc2016-02-17 13:12:16 +0100330 rpc = ly_ctx_get_node(ctx, "/ietf-netconf-monitoring:get-schema");
Michal Vasko05ba9df2016-01-13 14:40:27 +0100331 if (rpc && !rpc->private) {
332 lys_set_private(rpc, nc_clb_default_get_schema);
333 }
334
335 /* set default <close-session> callback if not specififed */
Michal Vaskofea54dc2016-02-17 13:12:16 +0100336 rpc = ly_ctx_get_node(ctx, "/ietf-netconf:close-session");
Michal Vasko05ba9df2016-01-13 14:40:27 +0100337 if (rpc && !rpc->private) {
338 lys_set_private(rpc, nc_clb_default_close_session);
339 }
340
Michal Vasko086311b2016-01-08 09:53:11 +0100341 server_opts.ctx = ctx;
Michal Vaskob48aa812016-01-18 14:13:09 +0100342
343 server_opts.new_session_id = 1;
344 pthread_spin_init(&server_opts.sid_lock, PTHREAD_PROCESS_PRIVATE);
345
Michal Vasko086311b2016-01-08 09:53:11 +0100346 return 0;
347}
348
Michal Vaskob48aa812016-01-18 14:13:09 +0100349API void
350nc_server_destroy(void)
351{
352 pthread_spin_destroy(&server_opts.sid_lock);
353
Radek Krejci53691be2016-02-22 13:58:37 +0100354#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3031aae2016-01-27 16:07:18 +0100355 nc_server_del_endpt(NULL, 0);
Michal Vaskob48aa812016-01-18 14:13:09 +0100356#endif
357}
358
Michal Vasko086311b2016-01-08 09:53:11 +0100359API int
360nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported)
361{
362 if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)
363 || (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT)))) {
364 ERRARG;
365 return -1;
366 }
367
368 server_opts.wd_basic_mode = basic_mode;
369 server_opts.wd_also_supported = also_supported;
370 return 0;
371}
372
Michal Vasko1a38c862016-01-15 15:50:07 +0100373API void
Michal Vasko086311b2016-01-08 09:53:11 +0100374nc_server_set_capab_interleave(int interleave_support)
375{
376 if (interleave_support) {
377 server_opts.interleave_capab = 1;
378 } else {
379 server_opts.interleave_capab = 0;
380 }
Michal Vasko086311b2016-01-08 09:53:11 +0100381}
382
Michal Vasko1a38c862016-01-15 15:50:07 +0100383API void
Michal Vasko086311b2016-01-08 09:53:11 +0100384nc_server_set_hello_timeout(uint16_t hello_timeout)
385{
Michal Vasko086311b2016-01-08 09:53:11 +0100386 server_opts.hello_timeout = hello_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100387}
388
Michal Vasko1a38c862016-01-15 15:50:07 +0100389API void
Michal Vasko086311b2016-01-08 09:53:11 +0100390nc_server_set_idle_timeout(uint16_t idle_timeout)
391{
Michal Vasko086311b2016-01-08 09:53:11 +0100392 server_opts.idle_timeout = idle_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100393}
394
395API int
Michal Vasko1a38c862016-01-15 15:50:07 +0100396nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +0100397{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100398 if (!server_opts.ctx || (fdin < 0) || (fdout < 0) || !username || !session) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100399 ERRARG;
400 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100401 }
402
403 /* prepare session structure */
Michal Vasko1a38c862016-01-15 15:50:07 +0100404 *session = calloc(1, sizeof **session);
405 if (!(*session)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100406 ERRMEM;
Michal Vasko1a38c862016-01-15 15:50:07 +0100407 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100408 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100409 (*session)->status = NC_STATUS_STARTING;
410 (*session)->side = NC_SERVER;
Michal Vasko086311b2016-01-08 09:53:11 +0100411
412 /* transport specific data */
Michal Vasko1a38c862016-01-15 15:50:07 +0100413 (*session)->ti_type = NC_TI_FD;
414 (*session)->ti.fd.in = fdin;
415 (*session)->ti.fd.out = fdout;
Michal Vasko086311b2016-01-08 09:53:11 +0100416
417 /* assign context (dicionary needed for handshake) */
Michal Vasko1a38c862016-01-15 15:50:07 +0100418 (*session)->flags = NC_SESSION_SHAREDCTX;
419 (*session)->ctx = server_opts.ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100420
Michal Vaskob48aa812016-01-18 14:13:09 +0100421 /* assign new SID atomically */
422 pthread_spin_lock(&server_opts.sid_lock);
423 (*session)->id = server_opts.new_session_id++;
424 pthread_spin_unlock(&server_opts.sid_lock);
425
Michal Vasko086311b2016-01-08 09:53:11 +0100426 /* NETCONF handshake */
Michal Vasko1a38c862016-01-15 15:50:07 +0100427 if (nc_handshake(*session)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100428 goto fail;
429 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100430 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100431 (*session)->last_rpc = time(NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100432
Michal Vasko1a38c862016-01-15 15:50:07 +0100433 return 0;
Michal Vasko086311b2016-01-08 09:53:11 +0100434
435fail:
Michal Vasko1a38c862016-01-15 15:50:07 +0100436 nc_session_free(*session);
437 *session = NULL;
438 return -1;
Michal Vasko086311b2016-01-08 09:53:11 +0100439}
Michal Vasko9e036d52016-01-08 10:49:26 +0100440
Michal Vasko428087d2016-01-14 16:04:28 +0100441API struct nc_pollsession *
442nc_ps_new(void)
443{
444 return calloc(1, sizeof(struct nc_pollsession));
445}
446
447API void
448nc_ps_free(struct nc_pollsession *ps)
449{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100450 if (!ps) {
451 return;
452 }
453
Michal Vasko3a715132016-01-21 15:40:31 +0100454 free(ps->pfds);
Michal Vasko428087d2016-01-14 16:04:28 +0100455 free(ps->sessions);
456 free(ps);
457}
458
459API int
460nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session)
461{
462 if (!ps || !session) {
463 ERRARG;
464 return -1;
465 }
466
467 ++ps->session_count;
Michal Vasko3a715132016-01-21 15:40:31 +0100468 ps->pfds = realloc(ps->pfds, ps->session_count * sizeof *ps->pfds);
Michal Vasko428087d2016-01-14 16:04:28 +0100469 ps->sessions = realloc(ps->sessions, ps->session_count * sizeof *ps->sessions);
470
471 switch (session->ti_type) {
472 case NC_TI_FD:
Michal Vasko3a715132016-01-21 15:40:31 +0100473 ps->pfds[ps->session_count - 1].fd = session->ti.fd.in;
Michal Vasko428087d2016-01-14 16:04:28 +0100474 break;
475
Radek Krejci53691be2016-02-22 13:58:37 +0100476#ifdef NC_ENABLED_SSH
Michal Vasko428087d2016-01-14 16:04:28 +0100477 case NC_TI_LIBSSH:
Michal Vasko3a715132016-01-21 15:40:31 +0100478 ps->pfds[ps->session_count - 1].fd = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100479 break;
480#endif
481
Radek Krejci53691be2016-02-22 13:58:37 +0100482#ifdef NC_ENABLED_TLS
Michal Vasko428087d2016-01-14 16:04:28 +0100483 case NC_TI_OPENSSL:
Michal Vasko3a715132016-01-21 15:40:31 +0100484 ps->pfds[ps->session_count - 1].fd = SSL_get_rfd(session->ti.tls);
Michal Vasko428087d2016-01-14 16:04:28 +0100485 break;
486#endif
487
488 default:
489 ERRINT;
490 return -1;
491 }
Michal Vasko3a715132016-01-21 15:40:31 +0100492 ps->pfds[ps->session_count - 1].events = POLLIN;
493 ps->pfds[ps->session_count - 1].revents = 0;
494 ps->sessions[ps->session_count - 1] = session;
Michal Vasko428087d2016-01-14 16:04:28 +0100495
496 return 0;
497}
498
499API int
500nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session)
501{
502 uint16_t i;
503
504 if (!ps || !session) {
505 ERRARG;
506 return -1;
507 }
508
509 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100510 if (ps->sessions[i] == session) {
Michal Vasko428087d2016-01-14 16:04:28 +0100511 --ps->session_count;
Michal Vasko58005732016-02-02 15:50:52 +0100512 if (i < ps->session_count) {
513 ps->sessions[i] = ps->sessions[ps->session_count];
514 memcpy(&ps->pfds[i], &ps->pfds[ps->session_count], sizeof *ps->pfds);
515 } else if (!ps->session_count) {
516 free(ps->sessions);
517 ps->sessions = NULL;
518 free(ps->pfds);
519 ps->pfds = NULL;
520 }
Michal Vasko428087d2016-01-14 16:04:28 +0100521 return 0;
522 }
523 }
524
Michal Vaskof0537d82016-01-29 14:42:38 +0100525 return -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100526}
527
528/* must be called holding the session lock! */
529static NC_MSG_TYPE
530nc_recv_rpc(struct nc_session *session, struct nc_server_rpc **rpc)
531{
532 struct lyxml_elem *xml = NULL;
533 NC_MSG_TYPE msgtype;
534
535 if (!session || !rpc) {
536 ERRARG;
537 return NC_MSG_ERROR;
538 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100539 ERR("Session %u: invalid session to receive RPCs.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100540 return NC_MSG_ERROR;
541 }
542
543 msgtype = nc_read_msg(session, &xml);
544
545 switch (msgtype) {
546 case NC_MSG_RPC:
547 *rpc = malloc(sizeof **rpc);
Michal Vaskoca4a2422016-02-02 12:17:14 +0100548
Michal Vasko428087d2016-01-14 16:04:28 +0100549 (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPC);
Michal Vaskoca4a2422016-02-02 12:17:14 +0100550 if (!(*rpc)->tree) {
551 ERR("Session %u: received message failed to be parsed into a known RPC.", session->id);
552 msgtype = NC_MSG_NONE;
553 }
Michal Vasko428087d2016-01-14 16:04:28 +0100554 (*rpc)->root = xml;
555 break;
556 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100557 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100558 goto error;
559 case NC_MSG_REPLY:
Michal Vasko81614ee2016-02-02 12:20:14 +0100560 ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100561 goto error;
562 case NC_MSG_NOTIF:
Michal Vasko81614ee2016-02-02 12:20:14 +0100563 ERR("Session %u: received <notification> from a NETCONF client.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100564 goto error;
565 default:
566 /* NC_MSG_ERROR - pass it out;
567 * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg()
568 */
569 break;
570 }
571
572 return msgtype;
573
574error:
575 /* cleanup */
576 lyxml_free(server_opts.ctx, xml);
577
578 return NC_MSG_ERROR;
579}
580
581/* must be called holding the session lock! */
582static NC_MSG_TYPE
583nc_send_reply(struct nc_session *session, struct nc_server_rpc *rpc)
584{
585 nc_rpc_clb clb;
586 struct nc_server_reply *reply;
587 int ret;
588
589 /* no callback, reply with a not-implemented error */
Michal Vaskoca4a2422016-02-02 12:17:14 +0100590 if (!rpc->tree || !rpc->tree->schema->private) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100591 reply = nc_server_reply_err(nc_err(NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT));
Michal Vasko428087d2016-01-14 16:04:28 +0100592 } else {
593 clb = (nc_rpc_clb)rpc->tree->schema->private;
594 reply = clb(rpc->tree, session);
595 }
596
597 if (!reply) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100598 reply = nc_server_reply_err(nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP));
Michal Vasko428087d2016-01-14 16:04:28 +0100599 }
600
601 ret = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply);
602
603 /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */
604 if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) {
605 session->status = NC_STATUS_INVALID;
606 }
607
608 if (ret == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100609 ERR("Session %u: failed to write reply.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100610 nc_server_reply_free(reply);
611 return NC_MSG_ERROR;
612 }
613 nc_server_reply_free(reply);
614
615 return NC_MSG_REPLY;
616}
617
618API int
619nc_ps_poll(struct nc_pollsession *ps, int timeout)
620{
621 int ret;
Michal Vasko3512e402016-01-28 16:22:34 +0100622 uint16_t i;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100623 time_t cur_time;
Michal Vasko428087d2016-01-14 16:04:28 +0100624 NC_MSG_TYPE msgtype;
625 struct nc_session *session;
626 struct nc_server_rpc *rpc;
Michal Vasko96164bf2016-01-21 15:41:58 +0100627 struct timespec old_ts;
Michal Vasko428087d2016-01-14 16:04:28 +0100628
629 if (!ps || !ps->session_count) {
630 ERRARG;
631 return -1;
632 }
633
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100634 cur_time = time(NULL);
635
Michal Vasko428087d2016-01-14 16:04:28 +0100636 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100637 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
638 ERR("Session %u: session not running.", ps->sessions[i]->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100639 return -1;
640 }
Michal Vaskobd8ef262016-01-20 11:09:27 +0100641
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100642 /* TODO invalidate only sessions without subscription */
Michal Vasko3a715132016-01-21 15:40:31 +0100643 if (server_opts.idle_timeout && (ps->sessions[i]->last_rpc + server_opts.idle_timeout >= cur_time)) {
644 ERR("Session %u: session idle timeout elapsed.", ps->sessions[i]->id);
645 ps->sessions[i]->status = NC_STATUS_INVALID;
646 ps->sessions[i]->term_reason = NC_SESSION_TERM_TIMEOUT;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100647 return 3;
648 }
649
Michal Vasko3a715132016-01-21 15:40:31 +0100650 if (ps->pfds[i].revents) {
Michal Vaskobd8ef262016-01-20 11:09:27 +0100651 break;
652 }
Michal Vasko428087d2016-01-14 16:04:28 +0100653 }
654
655 if (timeout > 0) {
656 clock_gettime(CLOCK_MONOTONIC_RAW, &old_ts);
657 }
658
Michal Vaskobd8ef262016-01-20 11:09:27 +0100659 if (i == ps->session_count) {
Radek Krejci53691be2016-02-22 13:58:37 +0100660#ifdef NC_ENABLED_SSH
Michal Vasko3a715132016-01-21 15:40:31 +0100661retry_poll:
Michal Vasko3512e402016-01-28 16:22:34 +0100662#endif
Michal Vaskobd8ef262016-01-20 11:09:27 +0100663 /* no leftover event */
664 i = 0;
Michal Vasko3a715132016-01-21 15:40:31 +0100665 ret = poll(ps->pfds, ps->session_count, timeout);
Michal Vaskobd8ef262016-01-20 11:09:27 +0100666 if (ret < 1) {
667 return ret;
668 }
Michal Vasko428087d2016-01-14 16:04:28 +0100669 }
670
Michal Vaskobd8ef262016-01-20 11:09:27 +0100671 /* find the first fd with POLLIN, we don't care if there are more now */
672 for (; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100673 if (ps->pfds[i].revents & POLLHUP) {
674 ERR("Session %u: communication socket unexpectedly closed.", ps->sessions[i]->id);
675 ps->sessions[i]->status = NC_STATUS_INVALID;
676 ps->sessions[i]->term_reason = NC_SESSION_TERM_DROPPED;
Michal Vaskobd8ef262016-01-20 11:09:27 +0100677 return 3;
Michal Vasko3a715132016-01-21 15:40:31 +0100678 } else if (ps->pfds[i].revents & POLLERR) {
679 ERR("Session %u: communication socket error.", ps->sessions[i]->id);
680 ps->sessions[i]->status = NC_STATUS_INVALID;
681 ps->sessions[i]->term_reason = NC_SESSION_TERM_OTHER;
Michal Vaskobd8ef262016-01-20 11:09:27 +0100682 return 3;
Michal Vasko3a715132016-01-21 15:40:31 +0100683 } else if (ps->pfds[i].revents & POLLIN) {
Radek Krejci53691be2016-02-22 13:58:37 +0100684#ifdef NC_ENABLED_SSH
Michal Vasko96164bf2016-01-21 15:41:58 +0100685 if (ps->sessions[i]->ti_type == NC_TI_LIBSSH) {
Michal Vasko3512e402016-01-28 16:22:34 +0100686 uint16_t j;
687
Michal Vasko96164bf2016-01-21 15:41:58 +0100688 /* things are not that simple with SSH... */
689 ret = nc_ssh_pollin(ps->sessions[i], &timeout);
690
691 /* clear POLLIN on sessions sharing this session's SSH session */
692 if ((ret == 1) || (ret >= 4)) {
693 for (j = i + 1; j < ps->session_count; ++j) {
694 if (ps->pfds[j].fd == ps->pfds[i].fd) {
695 ps->pfds[j].revents = 0;
696 }
697 }
698 }
699
700 /* actual event happened */
701 if ((ret <= 0) || (ret >= 3)) {
702 ps->pfds[i].revents = 0;
703 return ret;
704
705 /* event occurred on some other channel */
706 } else if (ret == 2) {
707 ps->pfds[i].revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100708 if (i == ps->session_count - 1) {
709 /* last session and it is not the right channel, ... */
Michal Vasko8c748832016-02-03 15:32:16 +0100710 if (!timeout) {
Michal Vasko428087d2016-01-14 16:04:28 +0100711 /* ... timeout is 0, so that is it */
712 return 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100713 }
Michal Vasko8c748832016-02-03 15:32:16 +0100714 /* ... retry polling reasonable time apart ... */
715 usleep(NC_TIMEOUT_STEP);
716 if (timeout > 0) {
717 /* ... and decrease timeout, if not -1 */
718 nc_subtract_elapsed(&timeout, &old_ts);
719 }
720 goto retry_poll;
Michal Vasko428087d2016-01-14 16:04:28 +0100721 }
722 /* check other sessions */
723 continue;
Michal Vasko428087d2016-01-14 16:04:28 +0100724 }
725 }
Radek Krejci53691be2016-02-22 13:58:37 +0100726#endif /* NC_ENABLED_SSH */
Michal Vasko428087d2016-01-14 16:04:28 +0100727
Michal Vaskobd8ef262016-01-20 11:09:27 +0100728 /* we are going to process it now */
Michal Vasko3a715132016-01-21 15:40:31 +0100729 ps->pfds[i].revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100730 break;
731 }
732 }
733
734 if (i == ps->session_count) {
735 ERRINT;
736 return -1;
737 }
738
739 /* this is the session with some data available for reading */
Michal Vasko3a715132016-01-21 15:40:31 +0100740 session = ps->sessions[i];
Michal Vasko428087d2016-01-14 16:04:28 +0100741
742 if (timeout > 0) {
Michal Vasko96164bf2016-01-21 15:41:58 +0100743 nc_subtract_elapsed(&timeout, &old_ts);
Michal Vasko428087d2016-01-14 16:04:28 +0100744 }
745
Michal Vaskobd8ef262016-01-20 11:09:27 +0100746 /* reading an RPC and sending a reply must be atomic (no other RPC should be read) */
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100747 ret = nc_timedlock(session->ti_lock, timeout, NULL);
748 if (ret != 1) {
749 /* error or timeout */
750 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100751 }
752
753 msgtype = nc_recv_rpc(session, &rpc);
754 if (msgtype == NC_MSG_ERROR) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100755 pthread_mutex_unlock(session->ti_lock);
Michal Vasko428087d2016-01-14 16:04:28 +0100756 if (session->status != NC_STATUS_RUNNING) {
Michal Vaskobd8ef262016-01-20 11:09:27 +0100757 return 3;
Michal Vasko428087d2016-01-14 16:04:28 +0100758 }
759 return -1;
760 }
761
Michal Vaskoca4a2422016-02-02 12:17:14 +0100762 /* NC_MSG_NONE is not a real (known) RPC */
763 if (msgtype == NC_MSG_RPC) {
764 session->last_rpc = time(NULL);
765 }
766
Michal Vasko428087d2016-01-14 16:04:28 +0100767 /* process RPC */
768 msgtype = nc_send_reply(session, rpc);
769
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100770 pthread_mutex_unlock(session->ti_lock);
Michal Vasko428087d2016-01-14 16:04:28 +0100771
772 if (msgtype == NC_MSG_ERROR) {
Michal Vaskoca4a2422016-02-02 12:17:14 +0100773 nc_server_rpc_free(rpc, server_opts.ctx);
Michal Vasko428087d2016-01-14 16:04:28 +0100774 return -1;
775 }
Michal Vaskoca4a2422016-02-02 12:17:14 +0100776 nc_server_rpc_free(rpc, server_opts.ctx);
Michal Vaskobd8ef262016-01-20 11:09:27 +0100777
Michal Vaskobd8b4e12016-01-22 16:11:20 +0100778 /* status change takes precedence over leftover events (return 2) */
779 if (session->status != NC_STATUS_RUNNING) {
780 return 3;
781 }
782
Michal Vaskobd8ef262016-01-20 11:09:27 +0100783 /* is there some other socket waiting? */
784 for (++i; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100785 if (ps->pfds[i].revents) {
Michal Vaskobd8ef262016-01-20 11:09:27 +0100786 return 2;
787 }
788 }
789
Michal Vasko428087d2016-01-14 16:04:28 +0100790 return 1;
791}
792
Michal Vaskod09eae62016-02-01 10:32:52 +0100793API void
794nc_ps_clear(struct nc_pollsession *ps)
795{
796 uint16_t i;
797 struct nc_session *session;
798
Michal Vasko9a25e932016-02-01 10:36:42 +0100799 if (!ps) {
800 ERRARG;
801 return;
802 }
803
Michal Vaskod09eae62016-02-01 10:32:52 +0100804 for (i = 0; i < ps->session_count; ) {
805 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
806 session = ps->sessions[i];
807 nc_ps_del_session(ps, session);
808 nc_session_free(session);
809 continue;
810 }
811
812 ++i;
813 }
814}
815
Radek Krejci53691be2016-02-22 13:58:37 +0100816#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +0100817
Michal Vasko3031aae2016-01-27 16:07:18 +0100818int
819nc_server_add_endpt_listen(const char *name, const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +0100820{
821 int sock;
Michal Vasko3031aae2016-01-27 16:07:18 +0100822 uint16_t i;
Radek Krejci53691be2016-02-22 13:58:37 +0100823#ifdef NC_ENABLED_SSH
Michal Vasko08a629a2016-02-02 12:20:47 +0100824 struct nc_server_ssh_opts *ssh_opts;
825#endif
Michal Vasko9e036d52016-01-08 10:49:26 +0100826
Michal Vasko3031aae2016-01-27 16:07:18 +0100827 if (!name || !address || !port) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100828 ERRARG;
829 return -1;
830 }
831
Michal Vasko51e514d2016-02-02 15:51:52 +0100832 /* WRITE LOCK */
833 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100834
835 /* check name uniqueness */
836 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskod4c03a82016-02-08 15:27:26 +0100837 if ((server_opts.binds[i].ti == ti) && !strcmp(server_opts.endpts[i].name, name)) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100838 ERR("Endpoint \"%s\" already exists.", name);
Michal Vasko51e514d2016-02-02 15:51:52 +0100839 /* WRITE UNLOCK */
Michal Vasko9faf1c82016-02-01 13:26:19 +0100840 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +0100841 return -1;
842 }
843 }
844
Michal Vasko9e036d52016-01-08 10:49:26 +0100845 sock = nc_sock_listen(address, port);
846 if (sock == -1) {
Michal Vasko51e514d2016-02-02 15:51:52 +0100847 /* WRITE UNLOCK */
848 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko9e036d52016-01-08 10:49:26 +0100849 return -1;
850 }
851
Michal Vasko3031aae2016-01-27 16:07:18 +0100852 ++server_opts.endpt_count;
853 server_opts.binds = realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds);
854 server_opts.endpts = realloc(server_opts.endpts, server_opts.endpt_count * sizeof *server_opts.endpts);
Michal Vasko9e036d52016-01-08 10:49:26 +0100855
Michal Vasko3031aae2016-01-27 16:07:18 +0100856 server_opts.endpts[server_opts.endpt_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
857 server_opts.binds[server_opts.endpt_count - 1].address = lydict_insert(server_opts.ctx, address, 0);
Michal Vasko3031aae2016-01-27 16:07:18 +0100858 server_opts.binds[server_opts.endpt_count - 1].port = port;
859 server_opts.binds[server_opts.endpt_count - 1].sock = sock;
860 server_opts.binds[server_opts.endpt_count - 1].ti = ti;
861 switch (ti) {
Radek Krejci53691be2016-02-22 13:58:37 +0100862#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +0100863 case NC_TI_LIBSSH:
Michal Vasko08a629a2016-02-02 12:20:47 +0100864 ssh_opts = calloc(1, sizeof *ssh_opts);
865 /* set default values */
866 ssh_opts->auth_methods = NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
867 ssh_opts->auth_attempts = 3;
868 ssh_opts->auth_timeout = 10;
869
870 server_opts.endpts[server_opts.endpt_count - 1].ti_opts = ssh_opts;
Michal Vasko3031aae2016-01-27 16:07:18 +0100871 break;
872#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100873#ifdef NC_ENABLED_TLS
Michal Vasko3031aae2016-01-27 16:07:18 +0100874 case NC_TI_OPENSSL:
875 server_opts.endpts[server_opts.endpt_count - 1].ti_opts = calloc(1, sizeof(struct nc_server_tls_opts));
876 break;
877#endif
878 default:
879 ERRINT;
880 server_opts.endpts[server_opts.endpt_count - 1].ti_opts = NULL;
881 break;
882 }
883 pthread_mutex_init(&server_opts.endpts[server_opts.endpt_count - 1].endpt_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +0100884
Michal Vasko3031aae2016-01-27 16:07:18 +0100885 /* WRITE UNLOCK */
886 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +0100887
Michal Vasko9e036d52016-01-08 10:49:26 +0100888 return 0;
889}
890
Michal Vasko3031aae2016-01-27 16:07:18 +0100891int
Michal Vaskoda514772016-02-01 11:32:01 +0100892nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
893{
894 struct nc_endpt *endpt;
895 struct nc_bind *bind = NULL;
896 uint16_t i;
897 int sock;
898
899 if (!endpt_name || (!address && !port) || (address && port) || !ti) {
900 ERRARG;
901 return -1;
902 }
903
Michal Vasko51e514d2016-02-02 15:51:52 +0100904 /* LOCK */
Michal Vaskoda514772016-02-01 11:32:01 +0100905 endpt = nc_server_endpt_lock(endpt_name, ti);
906 if (!endpt) {
907 return -1;
908 }
909
910 /* we need to learn the index, to get the bind :-/ */
911 for (i = 0; i < server_opts.endpt_count; ++i) {
912 if (&server_opts.endpts[i] == endpt) {
913 bind = &server_opts.binds[i];
914 }
915 }
916 if (!bind) {
917 ERRINT;
Michal Vasko51e514d2016-02-02 15:51:52 +0100918 goto fail;
Michal Vaskoda514772016-02-01 11:32:01 +0100919 }
920
921 if (address) {
922 sock = nc_sock_listen(address, bind->port);
923 } else {
924 sock = nc_sock_listen(bind->address, port);
925 }
926 if (sock == -1) {
Michal Vasko51e514d2016-02-02 15:51:52 +0100927 goto fail;
Michal Vaskoda514772016-02-01 11:32:01 +0100928 }
929
930 /* close old socket, update parameters */
931 close(bind->sock);
932 bind->sock = sock;
933 if (address) {
934 lydict_remove(server_opts.ctx, bind->address);
935 bind->address = lydict_insert(server_opts.ctx, address, 0);
936 } else {
937 bind->port = port;
938 }
939
Michal Vasko51e514d2016-02-02 15:51:52 +0100940 /* UNLOCK */
Michal Vasko7a93af72016-02-01 16:00:15 +0100941 nc_server_endpt_unlock(endpt);
Michal Vaskoda514772016-02-01 11:32:01 +0100942 return 0;
Michal Vasko51e514d2016-02-02 15:51:52 +0100943
944fail:
945 /* UNLOCK */
946 nc_server_endpt_unlock(endpt);
947 return -1;
Michal Vaskoda514772016-02-01 11:32:01 +0100948}
949
950int
Michal Vasko3031aae2016-01-27 16:07:18 +0100951nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +0100952{
953 uint32_t i;
954 int ret = -1;
955
Michal Vasko3031aae2016-01-27 16:07:18 +0100956 /* WRITE LOCK */
957 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +0100958
Michal Vasko3031aae2016-01-27 16:07:18 +0100959 if (!name && !ti) {
960 /* remove all */
Michal Vasko3031aae2016-01-27 16:07:18 +0100961 for (i = 0; i < server_opts.endpt_count; ++i) {
962 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko11d142a2016-01-19 15:58:24 +0100963 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
Michal Vasko51e514d2016-02-02 15:51:52 +0100964
Michal Vasko3031aae2016-01-27 16:07:18 +0100965 close(server_opts.binds[i].sock);
966 pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock);
967 switch (server_opts.binds[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +0100968#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +0100969 case NC_TI_LIBSSH:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100970 nc_server_ssh_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100971 break;
972#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100973#ifdef NC_ENABLED_TLS
Michal Vasko3031aae2016-01-27 16:07:18 +0100974 case NC_TI_OPENSSL:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +0100975 nc_server_tls_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100976 break;
977#endif
978 default:
979 ERRINT;
980 break;
981 }
982 free(server_opts.endpts[i].ti_opts);
Michal Vasko9e036d52016-01-08 10:49:26 +0100983
Michal Vasko9e036d52016-01-08 10:49:26 +0100984 ret = 0;
985 }
Michal Vasko7ddc5702016-02-08 15:29:39 +0100986 free(server_opts.binds);
987 server_opts.binds = NULL;
Michal Vasko3031aae2016-01-27 16:07:18 +0100988 free(server_opts.endpts);
989 server_opts.endpts = NULL;
990 server_opts.endpt_count = 0;
991
Michal Vasko1a38c862016-01-15 15:50:07 +0100992 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100993 /* remove one name endpoint or all ti endpoints */
994 for (i = 0; i < server_opts.endpt_count; ++i) {
995 if ((server_opts.binds[i].ti == ti) &&
996 (!name || !strcmp(server_opts.endpts[i].name, name))) {
997
Michal Vasko3031aae2016-01-27 16:07:18 +0100998 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko11d142a2016-01-19 15:58:24 +0100999 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
Michal Vasko3031aae2016-01-27 16:07:18 +01001000 close(server_opts.binds[i].sock);
1001 pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock);
1002 switch (server_opts.binds[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001003#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001004 case NC_TI_LIBSSH:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001005 nc_server_ssh_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001006 break;
1007#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001008#ifdef NC_ENABLED_TLS
Michal Vasko3031aae2016-01-27 16:07:18 +01001009 case NC_TI_OPENSSL:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001010 nc_server_tls_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001011 break;
1012#endif
1013 default:
1014 ERRINT;
1015 break;
1016 }
1017 free(server_opts.endpts[i].ti_opts);
Michal Vasko1a38c862016-01-15 15:50:07 +01001018
Michal Vasko3031aae2016-01-27 16:07:18 +01001019 --server_opts.endpt_count;
Michal Vaskoc0256492016-02-02 12:19:06 +01001020 if (i < server_opts.endpt_count) {
1021 memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
1022 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
1023 } else if (!server_opts.endpt_count) {
1024 free(server_opts.binds);
1025 server_opts.binds = NULL;
1026 free(server_opts.endpts);
1027 server_opts.endpts = NULL;
1028 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001029
1030 ret = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001031
1032 if (name) {
1033 /* one name endpoint removed, they are unique, we're done */
1034 break;
1035 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001036 }
1037 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001038 }
1039
Michal Vasko3031aae2016-01-27 16:07:18 +01001040 /* WRITE UNLOCK */
1041 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001042
Michal Vasko9e036d52016-01-08 10:49:26 +01001043 return ret;
1044}
1045
Michal Vasko1a38c862016-01-15 15:50:07 +01001046API int
1047nc_accept(int timeout, struct nc_session **session)
Michal Vasko9e036d52016-01-08 10:49:26 +01001048{
Michal Vasko1a38c862016-01-15 15:50:07 +01001049 int sock, ret;
Michal Vasko5c2f7952016-01-22 13:16:31 +01001050 char *host = NULL;
Michal Vasko3031aae2016-01-27 16:07:18 +01001051 uint16_t port, idx;
Michal Vasko9e036d52016-01-08 10:49:26 +01001052
Michal Vasko51e514d2016-02-02 15:51:52 +01001053 if (!server_opts.ctx || !session) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001054 ERRARG;
Michal Vasko1a38c862016-01-15 15:50:07 +01001055 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001056 }
1057
Michal Vasko51e514d2016-02-02 15:51:52 +01001058 /* we have to hold WRITE for the whole time, since there is not
1059 * a way of downgrading the lock to READ */
1060 /* WRITE LOCK */
1061 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
1062
1063 if (!server_opts.endpt_count) {
1064 ERRARG;
1065 /* WRITE UNLOCK */
1066 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1067 return -1;
1068 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001069
Michal Vasko3031aae2016-01-27 16:07:18 +01001070 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &idx);
Michal Vaskob48aa812016-01-18 14:13:09 +01001071
Michal Vasko50456e82016-02-02 12:16:08 +01001072 if (ret < 1) {
Michal Vasko51e514d2016-02-02 15:51:52 +01001073 /* WRITE UNLOCK */
Michal Vasko3031aae2016-01-27 16:07:18 +01001074 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob737d752016-02-09 09:01:27 +01001075 free(host);
Michal Vaskob48aa812016-01-18 14:13:09 +01001076 return ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001077 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001078 sock = ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001079
Michal Vasko1a38c862016-01-15 15:50:07 +01001080 *session = calloc(1, sizeof **session);
Michal Vasko686aa312016-01-21 15:58:18 +01001081 if (!(*session)) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001082 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001083 close(sock);
Michal Vasko5c2f7952016-01-22 13:16:31 +01001084 free(host);
Michal Vasko3031aae2016-01-27 16:07:18 +01001085 ret = -1;
1086 goto fail;
Michal Vasko9e036d52016-01-08 10:49:26 +01001087 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001088 (*session)->status = NC_STATUS_STARTING;
1089 (*session)->side = NC_SERVER;
1090 (*session)->ctx = server_opts.ctx;
1091 (*session)->flags = NC_SESSION_SHAREDCTX;
1092 (*session)->host = lydict_insert_zc(server_opts.ctx, host);
1093 (*session)->port = port;
Michal Vasko9e036d52016-01-08 10:49:26 +01001094
1095 /* transport lock */
Michal Vasko1a38c862016-01-15 15:50:07 +01001096 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1097 if (!(*session)->ti_lock) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001098 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001099 close(sock);
Michal Vasko1a38c862016-01-15 15:50:07 +01001100 ret = -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001101 goto fail;
1102 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001103 pthread_mutex_init((*session)->ti_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001104
Michal Vasko3031aae2016-01-27 16:07:18 +01001105 (*session)->ti_opts = server_opts.endpts[idx].ti_opts;
1106
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001107 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001108#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001109 if (server_opts.binds[idx].ti == NC_TI_LIBSSH) {
1110 ret = nc_accept_ssh_session(*session, sock, timeout);
Michal Vasko1a38c862016-01-15 15:50:07 +01001111 if (ret < 1) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001112 goto fail;
1113 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001114 } else
1115#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001116#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001117 if (server_opts.binds[idx].ti == NC_TI_OPENSSL) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001118 ret = nc_accept_tls_session(*session, sock, timeout);
Michal Vasko1a38c862016-01-15 15:50:07 +01001119 if (ret < 1) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001120 goto fail;
1121 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001122 } else
1123#endif
1124 {
Michal Vasko9e036d52016-01-08 10:49:26 +01001125 ERRINT;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001126 close(sock);
Michal Vasko1a38c862016-01-15 15:50:07 +01001127 ret = -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001128 goto fail;
1129 }
1130
Michal Vasko51e514d2016-02-02 15:51:52 +01001131 /* WRITE UNLOCK */
Michal Vasko3031aae2016-01-27 16:07:18 +01001132 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1133
Michal Vaskob48aa812016-01-18 14:13:09 +01001134 /* assign new SID atomically */
1135 /* LOCK */
1136 pthread_spin_lock(&server_opts.sid_lock);
1137 (*session)->id = server_opts.new_session_id++;
1138 /* UNLOCK */
1139 pthread_spin_unlock(&server_opts.sid_lock);
1140
Michal Vasko9e036d52016-01-08 10:49:26 +01001141 /* NETCONF handshake */
Michal Vasko1a38c862016-01-15 15:50:07 +01001142 if (nc_handshake(*session)) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001143 nc_session_free(*session);
1144 *session = NULL;
1145 return -1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001146 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001147 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko9e036d52016-01-08 10:49:26 +01001148
Michal Vasko1a38c862016-01-15 15:50:07 +01001149 return 1;
Michal Vasko9e036d52016-01-08 10:49:26 +01001150
1151fail:
Michal Vasko3031aae2016-01-27 16:07:18 +01001152 /* WRITE UNLOCK */
1153 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1154
Michal Vasko1a38c862016-01-15 15:50:07 +01001155 nc_session_free(*session);
1156 *session = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001157 return ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001158}
1159
Michal Vasko3031aae2016-01-27 16:07:18 +01001160int
Michal Vaskob05053d2016-01-22 16:12:06 +01001161nc_connect_callhome(const char *host, uint16_t port, NC_TRANSPORT_IMPL ti, int timeout, struct nc_session **session)
1162{
1163 int sock, ret;
1164
Michal Vaskoc61c4492016-01-25 11:13:34 +01001165 if (!host || !port || !ti || !session) {
1166 ERRARG;
1167 return -1;
1168 }
1169
Michal Vaskob05053d2016-01-22 16:12:06 +01001170 sock = nc_sock_connect(host, port);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001171 if (sock < 0) {
1172 return -1;
Michal Vaskob05053d2016-01-22 16:12:06 +01001173 }
1174
1175 *session = calloc(1, sizeof **session);
1176 if (!(*session)) {
1177 ERRMEM;
1178 close(sock);
1179 return -1;
1180 }
1181 (*session)->status = NC_STATUS_STARTING;
1182 (*session)->side = NC_SERVER;
1183 (*session)->ctx = server_opts.ctx;
1184 (*session)->flags = NC_SESSION_SHAREDCTX | NC_SESSION_CALLHOME;
Michal Vaskob05053d2016-01-22 16:12:06 +01001185 (*session)->host = lydict_insert(server_opts.ctx, host, 0);
Michal Vaskob05053d2016-01-22 16:12:06 +01001186 (*session)->port = port;
1187
1188 /* transport lock */
1189 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1190 if (!(*session)->ti_lock) {
1191 ERRMEM;
1192 close(sock);
1193 ret = -1;
1194 goto fail;
1195 }
1196 pthread_mutex_init((*session)->ti_lock, NULL);
1197
1198 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001199#ifdef NC_ENABLED_SSH
Michal Vaskob05053d2016-01-22 16:12:06 +01001200 if (ti == NC_TI_LIBSSH) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001201 /* OPTS LOCK */
1202 pthread_mutex_lock(&ssh_ch_opts_lock);
1203
Michal Vasko3031aae2016-01-27 16:07:18 +01001204 (*session)->ti_opts = &ssh_ch_opts;
1205 ret = nc_accept_ssh_session(*session, sock, timeout);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001206 (*session)->ti_opts = NULL;
1207
1208 /* OPTS UNLOCK */
1209 pthread_mutex_unlock(&ssh_ch_opts_lock);
1210
Michal Vaskob05053d2016-01-22 16:12:06 +01001211 if (ret < 1) {
1212 goto fail;
1213 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001214 } else
1215#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001216#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001217 if (ti == NC_TI_OPENSSL) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001218 /* OPTS LOCK */
1219 pthread_mutex_lock(&tls_ch_opts_lock);
1220
Michal Vasko3031aae2016-01-27 16:07:18 +01001221 (*session)->ti_opts = &tls_ch_opts;
1222 ret = nc_accept_tls_session(*session, sock, timeout);
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001223 (*session)->ti_opts = NULL;
1224
1225 /* OPTS UNLOCK */
1226 pthread_mutex_unlock(&tls_ch_opts_lock);
1227
Michal Vaskob05053d2016-01-22 16:12:06 +01001228 if (ret < 1) {
1229 goto fail;
1230 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001231 } else
1232#endif
1233 {
Michal Vaskob05053d2016-01-22 16:12:06 +01001234 ERRINT;
1235 close(sock);
1236 ret = -1;
1237 goto fail;
1238 }
1239
1240 /* assign new SID atomically */
1241 /* LOCK */
1242 pthread_spin_lock(&server_opts.sid_lock);
1243 (*session)->id = server_opts.new_session_id++;
1244 /* UNLOCK */
1245 pthread_spin_unlock(&server_opts.sid_lock);
1246
1247 /* NETCONF handshake */
1248 if (nc_handshake(*session)) {
1249 ret = -1;
1250 goto fail;
1251 }
1252 (*session)->status = NC_STATUS_RUNNING;
1253
1254 return 1;
1255
1256fail:
1257 nc_session_free(*session);
1258 *session = NULL;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001259 return ret;
Michal Vaskob05053d2016-01-22 16:12:06 +01001260}
1261
Radek Krejci53691be2016-02-22 13:58:37 +01001262#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */