blob: 6473fe23b0286711247f6dbc903d0814b46f8c39 [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 Vasko0190bc32016-03-02 15:47:49 +010025#include <fcntl.h>
Michal Vaskob48aa812016-01-18 14:13:09 +010026#include <pthread.h>
Michal Vasko11d142a2016-01-19 15:58:24 +010027#include <time.h>
Michal Vasko086311b2016-01-08 09:53:11 +010028
Michal Vasko1a38c862016-01-15 15:50:07 +010029#include "libnetconf.h"
Michal Vasko086311b2016-01-08 09:53:11 +010030#include "session_server.h"
31
Michal Vaskob48aa812016-01-18 14:13:09 +010032struct nc_server_opts server_opts = {
Michal Vasko3031aae2016-01-27 16:07:18 +010033 .endpt_array_lock = PTHREAD_RWLOCK_INITIALIZER
Michal Vaskob48aa812016-01-18 14:13:09 +010034};
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010035
Michal Vasko3031aae2016-01-27 16:07:18 +010036extern struct nc_server_ssh_opts ssh_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010037extern pthread_mutex_t ssh_ch_opts_lock;
38
Michal Vasko3031aae2016-01-27 16:07:18 +010039extern struct nc_server_tls_opts tls_ch_opts;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010040extern pthread_mutex_t tls_ch_opts_lock;
Michal Vasko3031aae2016-01-27 16:07:18 +010041
42struct nc_endpt *
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010043nc_server_endpt_lock(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko3031aae2016-01-27 16:07:18 +010044{
45 uint16_t i;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010046 struct nc_endpt *endpt = NULL;
47
48 /* READ LOCK */
49 pthread_rwlock_rdlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010050
51 for (i = 0; i < server_opts.endpt_count; ++i) {
52 if ((server_opts.binds[i].ti == ti) && !strcmp(server_opts.endpts[i].name, name)) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010053 endpt = &server_opts.endpts[i];
54 break;
Michal Vasko3031aae2016-01-27 16:07:18 +010055 }
56 }
57
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +010058 if (!endpt) {
59 ERR("Endpoint \"%s\" was not found.", name);
60 /* READ UNLOCK */
61 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
62 return NULL;
63 }
64
65 /* ENDPT LOCK */
66 pthread_mutex_lock(&endpt->endpt_lock);
67
68 return endpt;
69}
70
71void
72nc_server_endpt_unlock(struct nc_endpt *endpt)
73{
74 /* ENDPT UNLOCK */
75 pthread_mutex_unlock(&endpt->endpt_lock);
76
77 /* READ UNLOCK */
Michal Vasko27562ad2016-02-02 15:50:39 +010078 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +010079}
Michal Vasko086311b2016-01-08 09:53:11 +010080
Michal Vasko1a38c862016-01-15 15:50:07 +010081API void
82nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason)
83{
Michal Vasko45e53ae2016-04-07 11:46:03 +020084 if (!session) {
85 ERRARG("session");
86 return;
87 } else if (!reason) {
88 ERRARG("reason");
Michal Vasko1a38c862016-01-15 15:50:07 +010089 return;
90 }
91
92 session->term_reason = reason;
93}
94
Michal Vasko086311b2016-01-08 09:53:11 +010095int
Michal Vaskof05562c2016-01-20 12:06:43 +010096nc_sock_listen(const char *address, uint16_t port)
Michal Vasko086311b2016-01-08 09:53:11 +010097{
98 const int optVal = 1;
99 const socklen_t optLen = sizeof(optVal);
100 int is_ipv4, sock;
101 struct sockaddr_storage saddr;
102
103 struct sockaddr_in *saddr4;
104 struct sockaddr_in6 *saddr6;
105
106
107 if (!strchr(address, ':')) {
108 is_ipv4 = 1;
109 } else {
110 is_ipv4 = 0;
111 }
112
113 sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0);
114 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100115 ERR("Failed to create socket (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100116 goto fail;
117 }
118
119 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, optLen)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100120 ERR("Could not set socket SO_REUSEADDR socket option (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100121 goto fail;
122 }
123
124 bzero(&saddr, sizeof(struct sockaddr_storage));
125 if (is_ipv4) {
126 saddr4 = (struct sockaddr_in *)&saddr;
127
128 saddr4->sin_family = AF_INET;
129 saddr4->sin_port = htons(port);
130
131 if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100132 ERR("Failed to convert IPv4 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100133 goto fail;
134 }
135
136 if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100137 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100138 goto fail;
139 }
140
141 } else {
142 saddr6 = (struct sockaddr_in6 *)&saddr;
143
144 saddr6->sin6_family = AF_INET6;
145 saddr6->sin6_port = htons(port);
146
147 if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100148 ERR("Failed to convert IPv6 address \"%s\".", address);
Michal Vasko086311b2016-01-08 09:53:11 +0100149 goto fail;
150 }
151
152 if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100153 ERR("Could not bind \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100154 goto fail;
155 }
156 }
157
Michal Vaskofb89d772016-01-08 12:25:35 +0100158 if (listen(sock, NC_REVERSE_QUEUE) == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100159 ERR("Unable to start listening on \"%s\" port %d (%s).", address, port, strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100160 goto fail;
161 }
162
163 return sock;
164
165fail:
166 if (sock > -1) {
167 close(sock);
168 }
169
170 return -1;
171}
172
173int
Michal Vasko3031aae2016-01-27 16:07:18 +0100174nc_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 +0100175{
176 uint16_t i;
177 struct pollfd *pfd;
178 struct sockaddr_storage saddr;
179 socklen_t saddr_len = sizeof(saddr);
Michal Vasko0190bc32016-03-02 15:47:49 +0100180 int ret, sock = -1, flags;
Michal Vasko086311b2016-01-08 09:53:11 +0100181
182 pfd = malloc(bind_count * sizeof *pfd);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100183 if (!pfd) {
184 ERRMEM;
185 return -1;
186 }
187
Michal Vasko086311b2016-01-08 09:53:11 +0100188 for (i = 0; i < bind_count; ++i) {
189 pfd[i].fd = binds[i].sock;
190 pfd[i].events = POLLIN;
191 pfd[i].revents = 0;
192 }
193
194 /* poll for a new connection */
195 errno = 0;
196 ret = poll(pfd, bind_count, timeout);
197 if (!ret) {
198 /* we timeouted */
199 free(pfd);
200 return 0;
201 } else if (ret == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100202 ERR("Poll failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100203 free(pfd);
204 return -1;
205 }
206
207 for (i = 0; i < bind_count; ++i) {
208 if (pfd[i].revents & POLLIN) {
209 sock = pfd[i].fd;
210 break;
211 }
212 }
213 free(pfd);
214
215 if (sock == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100216 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100217 return -1;
218 }
219
220 ret = accept(sock, (struct sockaddr *)&saddr, &saddr_len);
Michal Vasko3f6cc4a2016-01-21 15:58:53 +0100221 if (ret < 0) {
Michal Vaskod083db62016-01-19 10:31:29 +0100222 ERR("Accept failed (%s).", strerror(errno));
Michal Vasko086311b2016-01-08 09:53:11 +0100223 return -1;
224 }
225
Michal Vasko0190bc32016-03-02 15:47:49 +0100226 /* make the socket non-blocking */
227 if (((flags = fcntl(ret, F_GETFL)) == -1) || (fcntl(ret, F_SETFL, flags | O_NONBLOCK) == -1)) {
228 ERR("Fcntl failed (%s).", strerror(errno));
Michal Vasko0f74da52016-03-03 08:52:52 +0100229 close(ret);
Michal Vasko0190bc32016-03-02 15:47:49 +0100230 return -1;
231 }
232
Michal Vasko3031aae2016-01-27 16:07:18 +0100233 if (idx) {
234 *idx = i;
Michal Vasko9e036d52016-01-08 10:49:26 +0100235 }
236
Michal Vasko086311b2016-01-08 09:53:11 +0100237 /* host was requested */
238 if (host) {
239 if (saddr.ss_family == AF_INET) {
240 *host = malloc(15);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100241 if (*host) {
242 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)&saddr)->sin_addr.s_addr, *host, 15)) {
243 ERR("inet_ntop failed (%s).", strerror(errno));
244 free(*host);
245 *host = NULL;
246 }
Michal Vasko086311b2016-01-08 09:53:11 +0100247
Michal Vasko4eb3c312016-03-01 14:09:37 +0100248 if (port) {
249 *port = ntohs(((struct sockaddr_in *)&saddr)->sin_port);
250 }
251 } else {
252 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100253 }
254 } else if (saddr.ss_family == AF_INET6) {
255 *host = malloc(40);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100256 if (*host) {
257 if (!inet_ntop(AF_INET6, ((struct sockaddr_in6 *)&saddr)->sin6_addr.s6_addr, *host, 40)) {
258 ERR("inet_ntop failed (%s).", strerror(errno));
259 free(*host);
260 *host = NULL;
261 }
Michal Vasko086311b2016-01-08 09:53:11 +0100262
Michal Vasko4eb3c312016-03-01 14:09:37 +0100263 if (port) {
264 *port = ntohs(((struct sockaddr_in6 *)&saddr)->sin6_port);
265 }
266 } else {
267 ERRMEM;
Michal Vasko086311b2016-01-08 09:53:11 +0100268 }
269 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100270 ERR("Source host of an unknown protocol family.");
Michal Vasko086311b2016-01-08 09:53:11 +0100271 }
272 }
273
274 return ret;
275}
276
Michal Vasko05ba9df2016-01-13 14:40:27 +0100277static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100278nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *UNUSED(session))
Michal Vasko05ba9df2016-01-13 14:40:27 +0100279{
280 const char *identifier = NULL, *version = NULL, *format = NULL;
281 char *model_data = NULL;
282 const struct lys_module *module;
283 struct nc_server_error *err;
284 struct lyd_node *child, *data = NULL;
Michal Vasko11d142a2016-01-19 15:58:24 +0100285 const struct lys_node *sdata = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100286
287 LY_TREE_FOR(rpc->child, child) {
288 if (!strcmp(child->schema->name, "identifier")) {
289 identifier = ((struct lyd_node_leaf_list *)child)->value_str;
290 } else if (!strcmp(child->schema->name, "version")) {
291 version = ((struct lyd_node_leaf_list *)child)->value_str;
292 } else if (!strcmp(child->schema->name, "format")) {
293 format = ((struct lyd_node_leaf_list *)child)->value_str;
294 }
295 }
296
297 /* check version */
298 if (version && (strlen(version) != 10) && strcmp(version, "1.0")) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100299 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
300 nc_err_set_msg(err, "The requested version is not supported.", "en");
301 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100302 }
303
304 /* check and get module with the name identifier */
305 module = ly_ctx_get_module(server_opts.ctx, identifier, version);
306 if (!module) {
Michal Vaskod91f6e62016-04-05 11:34:22 +0200307 module = (const struct lys_module *)ly_ctx_get_submodule(server_opts.ctx, NULL, NULL, identifier, version);
308 }
309 if (!module) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100310 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
311 nc_err_set_msg(err, "The requested schema was not found.", "en");
312 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100313 }
314
315 /* check format */
316 if (!format || !strcmp(format, "yang")) {
317 lys_print_mem(&model_data, module, LYS_OUT_YANG, NULL);
318 } else if (!strcmp(format, "yin")) {
319 lys_print_mem(&model_data, module, LYS_OUT_YIN, NULL);
320 } else {
Michal Vasko1a38c862016-01-15 15:50:07 +0100321 err = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP);
322 nc_err_set_msg(err, "The requested format is not supported.", "en");
323 return nc_server_reply_err(err);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100324 }
Michal Vaskod91f6e62016-04-05 11:34:22 +0200325 if (!model_data) {
326 ERRINT;
327 return NULL;
328 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100329
Michal Vasko303245c2016-03-24 15:20:03 +0100330 sdata = ly_ctx_get_node(server_opts.ctx, NULL, "/ietf-netconf-monitoring:get-schema/output/data");
Michal Vaskod91f6e62016-04-05 11:34:22 +0200331 if (!sdata) {
332 ERRINT;
333 free(model_data);
334 return NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100335 }
Michal Vaskod91f6e62016-04-05 11:34:22 +0200336
Michal Vaskob2583f12016-05-12 11:40:23 +0200337 data = lyd_new_path(NULL, server_opts.ctx, "/ietf-netconf-monitoring:get-schema/data", model_data, LYD_PATH_OPT_OUTPUT);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100338 if (!data) {
339 ERRINT;
Michal Vaskod91f6e62016-04-05 11:34:22 +0200340 free(model_data);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100341 return NULL;
342 }
Michal Vaskob2583f12016-05-12 11:40:23 +0200343 free(model_data);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100344
345 return nc_server_reply_data(data, NC_PARAMTYPE_FREE);
346}
347
348static struct nc_server_reply *
Michal Vasko428087d2016-01-14 16:04:28 +0100349nc_clb_default_close_session(struct lyd_node *UNUSED(rpc), struct nc_session *session)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100350{
Michal Vasko428087d2016-01-14 16:04:28 +0100351 session->term_reason = NC_SESSION_TERM_CLOSED;
352 return nc_server_reply_ok();
Michal Vasko05ba9df2016-01-13 14:40:27 +0100353}
354
Michal Vasko086311b2016-01-08 09:53:11 +0100355API int
356nc_server_init(struct ly_ctx *ctx)
357{
Michal Vasko05ba9df2016-01-13 14:40:27 +0100358 const struct lys_node *rpc;
359
Michal Vasko086311b2016-01-08 09:53:11 +0100360 if (!ctx) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200361 ERRARG("ctx");
Michal Vasko086311b2016-01-08 09:53:11 +0100362 return -1;
363 }
364
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100365 nc_init();
366
Michal Vasko05ba9df2016-01-13 14:40:27 +0100367 /* set default <get-schema> callback if not specified */
Michal Vasko303245c2016-03-24 15:20:03 +0100368 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema");
Michal Vaskofd100c92016-03-01 15:23:46 +0100369 if (rpc && !rpc->priv) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100370 lys_set_private(rpc, nc_clb_default_get_schema);
371 }
372
373 /* set default <close-session> callback if not specififed */
Michal Vasko303245c2016-03-24 15:20:03 +0100374 rpc = ly_ctx_get_node(ctx, NULL, "/ietf-netconf:close-session");
Michal Vaskofd100c92016-03-01 15:23:46 +0100375 if (rpc && !rpc->priv) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100376 lys_set_private(rpc, nc_clb_default_close_session);
377 }
378
Michal Vasko086311b2016-01-08 09:53:11 +0100379 server_opts.ctx = ctx;
Michal Vaskob48aa812016-01-18 14:13:09 +0100380
381 server_opts.new_session_id = 1;
382 pthread_spin_init(&server_opts.sid_lock, PTHREAD_PROCESS_PRIVATE);
383
Michal Vasko086311b2016-01-08 09:53:11 +0100384 return 0;
385}
386
Michal Vaskob48aa812016-01-18 14:13:09 +0100387API void
388nc_server_destroy(void)
389{
390 pthread_spin_destroy(&server_opts.sid_lock);
391
Radek Krejci53691be2016-02-22 13:58:37 +0100392#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko3031aae2016-01-27 16:07:18 +0100393 nc_server_del_endpt(NULL, 0);
Michal Vaskob48aa812016-01-18 14:13:09 +0100394#endif
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100395 nc_destroy();
Michal Vaskob48aa812016-01-18 14:13:09 +0100396}
397
Michal Vasko086311b2016-01-08 09:53:11 +0100398API int
399nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported)
400{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200401 if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)) {
402 ERRARG("basic_mode");
403 return -1;
404 } else if (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT))) {
405 ERRARG("also_supported");
Michal Vasko086311b2016-01-08 09:53:11 +0100406 return -1;
407 }
408
409 server_opts.wd_basic_mode = basic_mode;
410 server_opts.wd_also_supported = also_supported;
411 return 0;
412}
413
Michal Vasko1a38c862016-01-15 15:50:07 +0100414API void
Michal Vasko55f03972016-04-13 08:56:01 +0200415nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported)
416{
417 if (!basic_mode && !also_supported) {
418 ERRARG("basic_mode and also_supported");
419 return;
420 }
421
422 if (basic_mode) {
423 *basic_mode = server_opts.wd_basic_mode;
424 }
425 if (also_supported) {
426 *also_supported = server_opts.wd_also_supported;
427 }
428}
429
430API void
Michal Vasko086311b2016-01-08 09:53:11 +0100431nc_server_set_capab_interleave(int interleave_support)
432{
433 if (interleave_support) {
434 server_opts.interleave_capab = 1;
435 } else {
436 server_opts.interleave_capab = 0;
437 }
Michal Vasko086311b2016-01-08 09:53:11 +0100438}
439
Michal Vasko55f03972016-04-13 08:56:01 +0200440API int
441nc_server_get_capab_interleave(void)
442{
443 return server_opts.interleave_capab;
444}
445
Michal Vasko1a38c862016-01-15 15:50:07 +0100446API void
Michal Vasko086311b2016-01-08 09:53:11 +0100447nc_server_set_hello_timeout(uint16_t hello_timeout)
448{
Michal Vasko086311b2016-01-08 09:53:11 +0100449 server_opts.hello_timeout = hello_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100450}
451
Michal Vasko55f03972016-04-13 08:56:01 +0200452API uint16_t
453nc_server_get_hello_timeout(void)
454{
455 return server_opts.hello_timeout;
456}
457
Michal Vasko1a38c862016-01-15 15:50:07 +0100458API void
Michal Vasko086311b2016-01-08 09:53:11 +0100459nc_server_set_idle_timeout(uint16_t idle_timeout)
460{
Michal Vasko086311b2016-01-08 09:53:11 +0100461 server_opts.idle_timeout = idle_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +0100462}
463
Michal Vasko55f03972016-04-13 08:56:01 +0200464API uint16_t
465nc_server_get_idle_timeout(void)
466{
467 return server_opts.idle_timeout;
468}
469
Michal Vasko71090fc2016-05-24 16:37:28 +0200470API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +0100471nc_accept_inout(int fdin, int fdout, const char *username, struct nc_session **session)
Michal Vasko086311b2016-01-08 09:53:11 +0100472{
Michal Vasko71090fc2016-05-24 16:37:28 +0200473 NC_MSG_TYPE msgtype;
474
Michal Vasko45e53ae2016-04-07 11:46:03 +0200475 if (!server_opts.ctx) {
476 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +0200477 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200478 } else if (fdin < 0) {
479 ERRARG("fdin");
Michal Vasko71090fc2016-05-24 16:37:28 +0200480 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200481 } else if (fdout < 0) {
482 ERRARG("fdout");
Michal Vasko71090fc2016-05-24 16:37:28 +0200483 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200484 } else if (!username) {
485 ERRARG("username");
Michal Vasko71090fc2016-05-24 16:37:28 +0200486 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200487 } else if (!session) {
488 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200489 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100490 }
491
492 /* prepare session structure */
Michal Vasko1a38c862016-01-15 15:50:07 +0100493 *session = calloc(1, sizeof **session);
494 if (!(*session)) {
Michal Vasko086311b2016-01-08 09:53:11 +0100495 ERRMEM;
Michal Vasko71090fc2016-05-24 16:37:28 +0200496 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100497 }
Michal Vasko1a38c862016-01-15 15:50:07 +0100498 (*session)->status = NC_STATUS_STARTING;
499 (*session)->side = NC_SERVER;
Michal Vasko086311b2016-01-08 09:53:11 +0100500
501 /* transport specific data */
Michal Vasko1a38c862016-01-15 15:50:07 +0100502 (*session)->ti_type = NC_TI_FD;
503 (*session)->ti.fd.in = fdin;
504 (*session)->ti.fd.out = fdout;
Michal Vasko086311b2016-01-08 09:53:11 +0100505
506 /* assign context (dicionary needed for handshake) */
Michal Vasko1a38c862016-01-15 15:50:07 +0100507 (*session)->flags = NC_SESSION_SHAREDCTX;
508 (*session)->ctx = server_opts.ctx;
Michal Vasko086311b2016-01-08 09:53:11 +0100509
Michal Vaskob48aa812016-01-18 14:13:09 +0100510 /* assign new SID atomically */
511 pthread_spin_lock(&server_opts.sid_lock);
512 (*session)->id = server_opts.new_session_id++;
513 pthread_spin_unlock(&server_opts.sid_lock);
514
Michal Vasko086311b2016-01-08 09:53:11 +0100515 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +0200516 msgtype = nc_handshake(*session);
517 if (msgtype != NC_MSG_HELLO) {
518 nc_session_free(*session, NULL);
519 *session = NULL;
520 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100521 }
Michal Vaskof8352352016-05-24 09:11:36 +0200522 (*session)->session_start = (*session)->last_rpc = time(NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +0100523 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko086311b2016-01-08 09:53:11 +0100524
Michal Vasko71090fc2016-05-24 16:37:28 +0200525 return msgtype;
Michal Vasko086311b2016-01-08 09:53:11 +0100526}
Michal Vasko9e036d52016-01-08 10:49:26 +0100527
Michal Vaskof04a52a2016-04-07 10:52:10 +0200528int
Michal Vaskobe86fe32016-04-07 10:43:03 +0200529nc_ps_lock(struct nc_pollsession *ps)
530{
531 int ret;
532 uint8_t our_id, queue_last;
533 struct timespec ts;
534
Radek Krejci7ac16052016-07-15 11:48:18 +0200535 nc_gettimespec(&ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200536 ts.tv_sec += NC_READ_TIMEOUT;
537
538 /* LOCK */
539 ret = pthread_mutex_timedlock(&ps->lock, &ts);
540 if (ret) {
541 ERR("Failed to lock a pollsession (%s).", strerror(ret));
542 return -1;
543 }
544
545 /* get a unique queue value (by adding 1 to the last added value, if any) */
546 if (ps->queue_len) {
547 queue_last = ps->queue_begin + ps->queue_len - 1;
548 if (queue_last > NC_PS_QUEUE_SIZE - 1) {
549 queue_last -= NC_PS_QUEUE_SIZE;
550 }
551 our_id = ps->queue[queue_last] + 1;
552 } else {
553 our_id = 0;
554 }
555
556 /* add ourselves into the queue */
557 if (ps->queue_len == NC_PS_QUEUE_SIZE) {
558 ERR("Pollsession queue too small.");
559 return -1;
560 }
561 ++ps->queue_len;
562 queue_last = ps->queue_begin + ps->queue_len - 1;
563 if (queue_last > NC_PS_QUEUE_SIZE - 1) {
564 queue_last -= NC_PS_QUEUE_SIZE;
565 }
566 ps->queue[queue_last] = our_id;
567
568 /* is it our turn? */
569 while (ps->queue[ps->queue_begin] != our_id) {
Radek Krejci7ac16052016-07-15 11:48:18 +0200570 nc_gettimespec(&ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200571 ts.tv_sec += NC_READ_TIMEOUT;
572
573 ret = pthread_cond_timedwait(&ps->cond, &ps->lock, &ts);
574 if (ret) {
575 ERR("Failed to wait for a pollsession condition (%s).", strerror(ret));
576 /* remove ourselves from the queue */
577 ps->queue_begin = (ps->queue_begin < NC_PS_QUEUE_SIZE - 1 ? ps->queue_begin + 1 : 0);
578 --ps->queue_len;
579 return -1;
580 }
581 }
582
Michal Vaskobe86fe32016-04-07 10:43:03 +0200583 /* UNLOCK */
584 pthread_mutex_unlock(&ps->lock);
585
586 return 0;
587}
588
Michal Vaskof04a52a2016-04-07 10:52:10 +0200589int
Michal Vaskobe86fe32016-04-07 10:43:03 +0200590nc_ps_unlock(struct nc_pollsession *ps)
591{
592 int ret;
593 struct timespec ts;
594
Radek Krejci7ac16052016-07-15 11:48:18 +0200595 nc_gettimespec(&ts);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200596 ts.tv_sec += NC_READ_TIMEOUT;
597
598 /* LOCK */
599 ret = pthread_mutex_timedlock(&ps->lock, &ts);
600 if (ret) {
601 ERR("Failed to lock a pollsession (%s).", strerror(ret));
602 ret = -1;
603 }
604
605 /* remove ourselves from the queue */
606 ps->queue_begin = (ps->queue_begin < NC_PS_QUEUE_SIZE - 1 ? ps->queue_begin + 1 : 0);
607 --ps->queue_len;
608
609 /* broadcast to all other threads that the queue moved */
610 pthread_cond_broadcast(&ps->cond);
611
Michal Vaskobe86fe32016-04-07 10:43:03 +0200612 /* UNLOCK */
613 if (!ret) {
614 pthread_mutex_unlock(&ps->lock);
615 }
616
617 return ret;
618}
619
Michal Vasko428087d2016-01-14 16:04:28 +0100620API struct nc_pollsession *
621nc_ps_new(void)
622{
Michal Vasko48a63ed2016-03-01 09:48:21 +0100623 struct nc_pollsession *ps;
624
625 ps = calloc(1, sizeof(struct nc_pollsession));
Michal Vasko4eb3c312016-03-01 14:09:37 +0100626 if (!ps) {
627 ERRMEM;
628 return NULL;
629 }
Michal Vaskobe86fe32016-04-07 10:43:03 +0200630 pthread_cond_init(&ps->cond, NULL);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100631 pthread_mutex_init(&ps->lock, NULL);
632
633 return ps;
Michal Vasko428087d2016-01-14 16:04:28 +0100634}
635
636API void
637nc_ps_free(struct nc_pollsession *ps)
638{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100639 if (!ps) {
640 return;
641 }
642
Michal Vaskobe86fe32016-04-07 10:43:03 +0200643 if (ps->queue_len) {
644 ERR("FATAL: Freeing a pollsession structure that is currently being worked with!");
645 }
646
Michal Vasko3a715132016-01-21 15:40:31 +0100647 free(ps->pfds);
Michal Vasko428087d2016-01-14 16:04:28 +0100648 free(ps->sessions);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100649 pthread_mutex_destroy(&ps->lock);
Michal Vaskobe86fe32016-04-07 10:43:03 +0200650 pthread_cond_destroy(&ps->cond);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100651
Michal Vasko428087d2016-01-14 16:04:28 +0100652 free(ps);
653}
654
655API int
656nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session)
657{
Michal Vasko45e53ae2016-04-07 11:46:03 +0200658 if (!ps) {
659 ERRARG("ps");
660 return -1;
661 } else if (!session) {
662 ERRARG("session");
Michal Vasko428087d2016-01-14 16:04:28 +0100663 return -1;
664 }
665
Michal Vasko48a63ed2016-03-01 09:48:21 +0100666 /* LOCK */
Michal Vaskobe86fe32016-04-07 10:43:03 +0200667 if (nc_ps_lock(ps)) {
668 return -1;
669 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100670
Michal Vasko428087d2016-01-14 16:04:28 +0100671 ++ps->session_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100672 ps->pfds = nc_realloc(ps->pfds, ps->session_count * sizeof *ps->pfds);
673 ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions);
674 if (!ps->pfds || !ps->sessions) {
675 ERRMEM;
676 /* UNLOCK */
Michal Vaskobe86fe32016-04-07 10:43:03 +0200677 nc_ps_unlock(ps);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100678 return -1;
679 }
Michal Vasko428087d2016-01-14 16:04:28 +0100680
681 switch (session->ti_type) {
682 case NC_TI_FD:
Michal Vasko3a715132016-01-21 15:40:31 +0100683 ps->pfds[ps->session_count - 1].fd = session->ti.fd.in;
Michal Vasko428087d2016-01-14 16:04:28 +0100684 break;
685
Radek Krejci53691be2016-02-22 13:58:37 +0100686#ifdef NC_ENABLED_SSH
Michal Vasko428087d2016-01-14 16:04:28 +0100687 case NC_TI_LIBSSH:
Michal Vasko3a715132016-01-21 15:40:31 +0100688 ps->pfds[ps->session_count - 1].fd = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100689 break;
690#endif
691
Radek Krejci53691be2016-02-22 13:58:37 +0100692#ifdef NC_ENABLED_TLS
Michal Vasko428087d2016-01-14 16:04:28 +0100693 case NC_TI_OPENSSL:
Michal Vasko3a715132016-01-21 15:40:31 +0100694 ps->pfds[ps->session_count - 1].fd = SSL_get_rfd(session->ti.tls);
Michal Vasko428087d2016-01-14 16:04:28 +0100695 break;
696#endif
697
698 default:
699 ERRINT;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100700 /* UNLOCK */
Michal Vaskobe86fe32016-04-07 10:43:03 +0200701 nc_ps_unlock(ps);
Michal Vasko428087d2016-01-14 16:04:28 +0100702 return -1;
703 }
Michal Vasko3a715132016-01-21 15:40:31 +0100704 ps->pfds[ps->session_count - 1].events = POLLIN;
705 ps->pfds[ps->session_count - 1].revents = 0;
706 ps->sessions[ps->session_count - 1] = session;
Michal Vasko428087d2016-01-14 16:04:28 +0100707
Michal Vasko48a63ed2016-03-01 09:48:21 +0100708 /* UNLOCK */
Michal Vaskobe86fe32016-04-07 10:43:03 +0200709 return nc_ps_unlock(ps);
Michal Vasko428087d2016-01-14 16:04:28 +0100710}
711
Michal Vasko48a63ed2016-03-01 09:48:21 +0100712static int
Radek Krejcid5f978f2016-03-03 13:14:45 +0100713_nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session, int index)
Michal Vasko428087d2016-01-14 16:04:28 +0100714{
715 uint16_t i;
716
Radek Krejcid5f978f2016-03-03 13:14:45 +0100717 if (index >= 0) {
718 i = (uint16_t)index;
719 goto remove;
720 }
Michal Vasko428087d2016-01-14 16:04:28 +0100721 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100722 if (ps->sessions[i] == session) {
Radek Krejcid5f978f2016-03-03 13:14:45 +0100723remove:
Michal Vasko428087d2016-01-14 16:04:28 +0100724 --ps->session_count;
Michal Vasko58005732016-02-02 15:50:52 +0100725 if (i < ps->session_count) {
726 ps->sessions[i] = ps->sessions[ps->session_count];
727 memcpy(&ps->pfds[i], &ps->pfds[ps->session_count], sizeof *ps->pfds);
728 } else if (!ps->session_count) {
729 free(ps->sessions);
730 ps->sessions = NULL;
731 free(ps->pfds);
732 ps->pfds = NULL;
733 }
Michal Vasko428087d2016-01-14 16:04:28 +0100734 return 0;
735 }
736 }
737
Michal Vaskof0537d82016-01-29 14:42:38 +0100738 return -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100739}
740
Michal Vasko48a63ed2016-03-01 09:48:21 +0100741API int
742nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session)
743{
Michal Vaskobe86fe32016-04-07 10:43:03 +0200744 int ret, ret2;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100745
Michal Vasko45e53ae2016-04-07 11:46:03 +0200746 if (!ps) {
747 ERRARG("ps");
748 return -1;
749 } else if (!session) {
750 ERRARG("session");
Michal Vasko48a63ed2016-03-01 09:48:21 +0100751 return -1;
752 }
753
754 /* LOCK */
Michal Vaskobe86fe32016-04-07 10:43:03 +0200755 if (nc_ps_lock(ps)) {
756 return -1;
757 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100758
Radek Krejcid5f978f2016-03-03 13:14:45 +0100759 ret = _nc_ps_del_session(ps, session, -1);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100760
761 /* UNLOCK */
Michal Vaskobe86fe32016-04-07 10:43:03 +0200762 ret2 = nc_ps_unlock(ps);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100763
Michal Vaskobe86fe32016-04-07 10:43:03 +0200764 return (ret || ret2 ? -1 : 0);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100765}
766
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100767API uint16_t
768nc_ps_session_count(struct nc_pollsession *ps)
769{
Michal Vasko48a63ed2016-03-01 09:48:21 +0100770 uint16_t count;
771
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100772 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200773 ERRARG("ps");
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100774 return 0;
775 }
776
Michal Vasko48a63ed2016-03-01 09:48:21 +0100777 /* LOCK */
Michal Vaskobe86fe32016-04-07 10:43:03 +0200778 if (nc_ps_lock(ps)) {
779 return -1;
780 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100781
782 count = ps->session_count;
783
784 /* UNLOCK */
Michal Vaskobe86fe32016-04-07 10:43:03 +0200785 nc_ps_unlock(ps);
Michal Vasko48a63ed2016-03-01 09:48:21 +0100786
787 return count;
Michal Vasko0fdb7ac2016-03-01 09:03:12 +0100788}
789
Michal Vasko71090fc2016-05-24 16:37:28 +0200790/* must be called holding the session lock!
791 * returns: NC_PSPOLL_ERROR,
792 * NC_PSPOLL_BAD_RPC,
793 * NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR,
794 * NC_PSPOLL_RPC
795 */
796static int
Michal Vasko428087d2016-01-14 16:04:28 +0100797nc_recv_rpc(struct nc_session *session, struct nc_server_rpc **rpc)
798{
799 struct lyxml_elem *xml = NULL;
800 NC_MSG_TYPE msgtype;
Radek Krejcif93c7d42016-04-06 13:41:15 +0200801 struct nc_server_reply *reply = NULL;
Radek Krejcif93c7d42016-04-06 13:41:15 +0200802 int ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100803
Michal Vasko45e53ae2016-04-07 11:46:03 +0200804 if (!session) {
805 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +0200806 return NC_PSPOLL_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +0200807 } else if (!rpc) {
808 ERRARG("rpc");
Michal Vasko71090fc2016-05-24 16:37:28 +0200809 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100810 } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100811 ERR("Session %u: invalid session to receive RPCs.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200812 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100813 }
814
815 msgtype = nc_read_msg(session, &xml);
816
817 switch (msgtype) {
818 case NC_MSG_RPC:
Radek Krejcif93c7d42016-04-06 13:41:15 +0200819 *rpc = calloc(1, sizeof **rpc);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100820 if (!*rpc) {
821 ERRMEM;
822 goto error;
823 }
Michal Vaskoca4a2422016-02-02 12:17:14 +0100824
Radek Krejcif93c7d42016-04-06 13:41:15 +0200825 ly_errno = LY_SUCCESS;
Michal Vasko428087d2016-01-14 16:04:28 +0100826 (*rpc)->tree = lyd_parse_xml(server_opts.ctx, &xml->child, LYD_OPT_DESTRUCT | LYD_OPT_RPC);
Michal Vaskoca4a2422016-02-02 12:17:14 +0100827 if (!(*rpc)->tree) {
Radek Krejcif93c7d42016-04-06 13:41:15 +0200828 /* parsing RPC failed */
Radek Krejci877e1822016-04-06 16:37:43 +0200829 reply = nc_server_reply_err(nc_err_libyang());
Radek Krejci844662e2016-04-13 16:54:43 +0200830 ret = nc_write_msg(session, NC_MSG_REPLY, xml, reply);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200831 nc_server_reply_free(reply);
832 if (ret == -1) {
833 ERR("Session %u: failed to write reply.", session->id);
Radek Krejcif93c7d42016-04-06 13:41:15 +0200834 }
Michal Vasko71090fc2016-05-24 16:37:28 +0200835 ret = NC_PSPOLL_REPLY_ERROR | NC_PSPOLL_BAD_RPC;
836 } else {
837 ret = NC_PSPOLL_RPC;
Michal Vaskoca4a2422016-02-02 12:17:14 +0100838 }
Michal Vasko428087d2016-01-14 16:04:28 +0100839 (*rpc)->root = xml;
840 break;
841 case NC_MSG_HELLO:
Michal Vaskod083db62016-01-19 10:31:29 +0100842 ERR("Session %u: received another <hello> message.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200843 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100844 goto error;
845 case NC_MSG_REPLY:
Michal Vasko81614ee2016-02-02 12:20:14 +0100846 ERR("Session %u: received <rpc-reply> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200847 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100848 goto error;
849 case NC_MSG_NOTIF:
Michal Vasko81614ee2016-02-02 12:20:14 +0100850 ERR("Session %u: received <notification> from a NETCONF client.", session->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200851 ret = NC_PSPOLL_BAD_RPC;
Michal Vasko428087d2016-01-14 16:04:28 +0100852 goto error;
853 default:
Michal Vasko71090fc2016-05-24 16:37:28 +0200854 /* NC_MSG_ERROR,
Michal Vasko428087d2016-01-14 16:04:28 +0100855 * NC_MSG_WOULDBLOCK and NC_MSG_NONE is not returned by nc_read_msg()
856 */
Michal Vasko71090fc2016-05-24 16:37:28 +0200857 ret = NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100858 break;
859 }
860
Michal Vasko71090fc2016-05-24 16:37:28 +0200861 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100862
863error:
864 /* cleanup */
865 lyxml_free(server_opts.ctx, xml);
866
Michal Vasko71090fc2016-05-24 16:37:28 +0200867 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100868}
869
Michal Vasko71090fc2016-05-24 16:37:28 +0200870/* must be called holding the session lock!
871 * returns: NC_PSPOLL_ERROR,
872 * NC_PSPOLL_ERROR | NC_PSPOLL_REPLY_ERROR,
873 * NC_PSPOLL_REPLY_ERROR,
874 * 0
875 */
876static int
Michal Vasko428087d2016-01-14 16:04:28 +0100877nc_send_reply(struct nc_session *session, struct nc_server_rpc *rpc)
878{
879 nc_rpc_clb clb;
880 struct nc_server_reply *reply;
Michal Vasko71090fc2016-05-24 16:37:28 +0200881 int ret = 0, r;
Michal Vasko428087d2016-01-14 16:04:28 +0100882
Michal Vasko4a827e52016-03-03 10:59:00 +0100883 if (!rpc) {
884 ERRINT;
Michal Vasko71090fc2016-05-24 16:37:28 +0200885 return NC_PSPOLL_ERROR;
Michal Vasko4a827e52016-03-03 10:59:00 +0100886 }
887
Michal Vasko428087d2016-01-14 16:04:28 +0100888 /* no callback, reply with a not-implemented error */
Radek Krejcif93c7d42016-04-06 13:41:15 +0200889 if (!rpc->tree->schema->priv) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100890 reply = nc_server_reply_err(nc_err(NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT));
Michal Vasko428087d2016-01-14 16:04:28 +0100891 } else {
Michal Vaskofd100c92016-03-01 15:23:46 +0100892 clb = (nc_rpc_clb)rpc->tree->schema->priv;
Michal Vasko428087d2016-01-14 16:04:28 +0100893 reply = clb(rpc->tree, session);
894 }
895
896 if (!reply) {
Michal Vasko1a38c862016-01-15 15:50:07 +0100897 reply = nc_server_reply_err(nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP));
Michal Vasko428087d2016-01-14 16:04:28 +0100898 }
Michal Vasko71090fc2016-05-24 16:37:28 +0200899 r = nc_write_msg(session, NC_MSG_REPLY, rpc->root, reply);
900 if (reply->type == NC_RPL_ERROR) {
901 ret |= NC_PSPOLL_REPLY_ERROR;
902 }
903 nc_server_reply_free(reply);
Michal Vasko428087d2016-01-14 16:04:28 +0100904
Michal Vasko71090fc2016-05-24 16:37:28 +0200905 if (r == -1) {
906 ERR("Session %u: failed to write reply.", session->id);
907 ret |= NC_PSPOLL_ERROR;
908 }
Michal Vasko428087d2016-01-14 16:04:28 +0100909
910 /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */
911 if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) {
912 session->status = NC_STATUS_INVALID;
913 }
914
Michal Vasko71090fc2016-05-24 16:37:28 +0200915 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +0100916}
917
918API int
Michal Vasko71090fc2016-05-24 16:37:28 +0200919nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session)
Michal Vasko428087d2016-01-14 16:04:28 +0100920{
921 int ret;
Michal Vasko3512e402016-01-28 16:22:34 +0100922 uint16_t i;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100923 time_t cur_time;
Michal Vasko71090fc2016-05-24 16:37:28 +0200924 struct nc_session *cur_session;
Michal Vasko4a827e52016-03-03 10:59:00 +0100925 struct nc_server_rpc *rpc = NULL;
Michal Vasko428087d2016-01-14 16:04:28 +0100926
927 if (!ps || !ps->session_count) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200928 ERRARG("ps");
Michal Vasko71090fc2016-05-24 16:37:28 +0200929 return NC_PSPOLL_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100930 }
931
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100932 cur_time = time(NULL);
933
Michal Vasko48a63ed2016-03-01 09:48:21 +0100934 /* LOCK */
Michal Vaskobe86fe32016-04-07 10:43:03 +0200935 if (nc_ps_lock(ps)) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200936 return NC_PSPOLL_ERROR;
Michal Vaskobe86fe32016-04-07 10:43:03 +0200937 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100938
Michal Vasko428087d2016-01-14 16:04:28 +0100939 for (i = 0; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +0100940 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
941 ERR("Session %u: session not running.", ps->sessions[i]->id);
Michal Vasko71090fc2016-05-24 16:37:28 +0200942 ret = NC_PSPOLL_ERROR;
943 if (session) {
944 *session = ps->sessions[i];
945 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100946 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +0100947 }
Michal Vaskobd8ef262016-01-20 11:09:27 +0100948
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100949 /* TODO invalidate only sessions without subscription */
Michal Vasko3a715132016-01-21 15:40:31 +0100950 if (server_opts.idle_timeout && (ps->sessions[i]->last_rpc + server_opts.idle_timeout >= cur_time)) {
951 ERR("Session %u: session idle timeout elapsed.", ps->sessions[i]->id);
952 ps->sessions[i]->status = NC_STATUS_INVALID;
953 ps->sessions[i]->term_reason = NC_SESSION_TERM_TIMEOUT;
Michal Vasko71090fc2016-05-24 16:37:28 +0200954 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
955 if (session) {
956 *session = ps->sessions[i];
957 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100958 goto finish;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100959 }
960
Michal Vasko3a715132016-01-21 15:40:31 +0100961 if (ps->pfds[i].revents) {
Michal Vaskobd8ef262016-01-20 11:09:27 +0100962 break;
963 }
Michal Vasko428087d2016-01-14 16:04:28 +0100964 }
965
Michal Vaskobd8ef262016-01-20 11:09:27 +0100966 if (i == ps->session_count) {
Radek Krejci53691be2016-02-22 13:58:37 +0100967#ifdef NC_ENABLED_SSH
Michal Vasko3a715132016-01-21 15:40:31 +0100968retry_poll:
Michal Vasko3512e402016-01-28 16:22:34 +0100969#endif
Michal Vaskobd8ef262016-01-20 11:09:27 +0100970 /* no leftover event */
971 i = 0;
Michal Vasko3a715132016-01-21 15:40:31 +0100972 ret = poll(ps->pfds, ps->session_count, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +0200973 if (ret < 0) {
974 ERR("Poll failed (%s).", strerror(errno));
975 ret = NC_PSPOLL_ERROR;
976 goto finish;
977 } else if (!ret) {
978 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko48a63ed2016-03-01 09:48:21 +0100979 goto finish;
Michal Vaskobd8ef262016-01-20 11:09:27 +0100980 }
Michal Vasko428087d2016-01-14 16:04:28 +0100981 }
982
Michal Vaskobd8ef262016-01-20 11:09:27 +0100983 /* find the first fd with POLLIN, we don't care if there are more now */
984 for (; i < ps->session_count; ++i) {
Michal Vasko46eac552016-05-30 15:27:25 +0200985 if (ps->pfds[i].revents & (POLLHUP | POLLNVAL)) {
Michal Vasko3a715132016-01-21 15:40:31 +0100986 ERR("Session %u: communication socket unexpectedly closed.", ps->sessions[i]->id);
987 ps->sessions[i]->status = NC_STATUS_INVALID;
988 ps->sessions[i]->term_reason = NC_SESSION_TERM_DROPPED;
Michal Vasko71090fc2016-05-24 16:37:28 +0200989 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
990 if (session) {
991 *session = ps->sessions[i];
992 }
Michal Vasko48a63ed2016-03-01 09:48:21 +0100993 goto finish;
Michal Vasko3a715132016-01-21 15:40:31 +0100994 } else if (ps->pfds[i].revents & POLLERR) {
995 ERR("Session %u: communication socket error.", ps->sessions[i]->id);
996 ps->sessions[i]->status = NC_STATUS_INVALID;
997 ps->sessions[i]->term_reason = NC_SESSION_TERM_OTHER;
Michal Vasko71090fc2016-05-24 16:37:28 +0200998 ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
999 if (session) {
1000 *session = ps->sessions[i];
1001 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001002 goto finish;
Michal Vasko3a715132016-01-21 15:40:31 +01001003 } else if (ps->pfds[i].revents & POLLIN) {
Radek Krejci53691be2016-02-22 13:58:37 +01001004#ifdef NC_ENABLED_SSH
Michal Vasko96164bf2016-01-21 15:41:58 +01001005 if (ps->sessions[i]->ti_type == NC_TI_LIBSSH) {
Michal Vasko3512e402016-01-28 16:22:34 +01001006 uint16_t j;
1007
Michal Vasko96164bf2016-01-21 15:41:58 +01001008 /* things are not that simple with SSH... */
Michal Vasko62be1ce2016-03-03 13:24:52 +01001009 ret = nc_ssh_pollin(ps->sessions[i], timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +01001010
1011 /* clear POLLIN on sessions sharing this session's SSH session */
Michal Vasko71090fc2016-05-24 16:37:28 +02001012 if (ret & (NC_PSPOLL_RPC | NC_PSPOLL_SSH_MSG | NC_PSPOLL_SSH_CHANNEL)) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001013 for (j = i + 1; j < ps->session_count; ++j) {
1014 if (ps->pfds[j].fd == ps->pfds[i].fd) {
1015 ps->pfds[j].revents = 0;
1016 }
1017 }
1018 }
1019
Michal Vasko71090fc2016-05-24 16:37:28 +02001020 /* SSH message only */
1021 if (!(ret & (NC_PSPOLL_RPC | NC_PSPOLL_PENDING))) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001022 ps->pfds[i].revents = 0;
Michal Vasko71090fc2016-05-24 16:37:28 +02001023 if (session) {
1024 *session = ps->sessions[i];
1025 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001026 goto finish;
Michal Vasko96164bf2016-01-21 15:41:58 +01001027
1028 /* event occurred on some other channel */
Michal Vasko71090fc2016-05-24 16:37:28 +02001029 } else if (ret & NC_PSPOLL_PENDING) {
Michal Vasko96164bf2016-01-21 15:41:58 +01001030 ps->pfds[i].revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +01001031 if (i == ps->session_count - 1) {
1032 /* last session and it is not the right channel, ... */
Michal Vasko8c748832016-02-03 15:32:16 +01001033 if (!timeout) {
Michal Vasko428087d2016-01-14 16:04:28 +01001034 /* ... timeout is 0, so that is it */
Michal Vasko71090fc2016-05-24 16:37:28 +02001035 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001036 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001037 }
Michal Vasko8c748832016-02-03 15:32:16 +01001038 /* ... retry polling reasonable time apart ... */
1039 usleep(NC_TIMEOUT_STEP);
1040 if (timeout > 0) {
1041 /* ... and decrease timeout, if not -1 */
Michal Vasko7b38e232016-02-26 15:01:07 +01001042 timeout -= NC_TIMEOUT_STEP * 1000;
Michal Vasko8c748832016-02-03 15:32:16 +01001043 }
1044 goto retry_poll;
Michal Vasko428087d2016-01-14 16:04:28 +01001045 }
1046 /* check other sessions */
1047 continue;
Michal Vasko428087d2016-01-14 16:04:28 +01001048 }
1049 }
Radek Krejci53691be2016-02-22 13:58:37 +01001050#endif /* NC_ENABLED_SSH */
Michal Vasko428087d2016-01-14 16:04:28 +01001051
Michal Vaskobd8ef262016-01-20 11:09:27 +01001052 /* we are going to process it now */
Michal Vasko3a715132016-01-21 15:40:31 +01001053 ps->pfds[i].revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +01001054 break;
1055 }
1056 }
1057
1058 if (i == ps->session_count) {
1059 ERRINT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001060 ret = NC_PSPOLL_ERROR;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001061 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001062 }
1063
1064 /* this is the session with some data available for reading */
Michal Vasko71090fc2016-05-24 16:37:28 +02001065 cur_session = ps->sessions[i];
1066 if (session) {
1067 *session = cur_session;
1068 }
Michal Vasko428087d2016-01-14 16:04:28 +01001069
Michal Vaskobd8ef262016-01-20 11:09:27 +01001070 /* reading an RPC and sending a reply must be atomic (no other RPC should be read) */
Michal Vasko71090fc2016-05-24 16:37:28 +02001071 ret = nc_timedlock(cur_session->ti_lock, timeout);
1072 if (ret < 0) {
1073 ret = NC_PSPOLL_ERROR;
1074 goto finish;
1075 } else if (!ret) {
1076 ret = NC_PSPOLL_TIMEOUT;
Michal Vasko48a63ed2016-03-01 09:48:21 +01001077 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001078 }
1079
Michal Vasko71090fc2016-05-24 16:37:28 +02001080 ret = nc_recv_rpc(cur_session, &rpc);
1081 if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) {
1082 pthread_mutex_unlock(cur_session->ti_lock);
1083 if (cur_session->status != NC_STATUS_RUNNING) {
1084 ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001085 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001086 goto finish;
Michal Vasko428087d2016-01-14 16:04:28 +01001087 }
1088
Michal Vasko71090fc2016-05-24 16:37:28 +02001089 cur_session->last_rpc = time(NULL);
Michal Vaskoca4a2422016-02-02 12:17:14 +01001090
Michal Vasko428087d2016-01-14 16:04:28 +01001091 /* process RPC */
Michal Vasko71090fc2016-05-24 16:37:28 +02001092 ret |= nc_send_reply(cur_session, rpc);
Michal Vasko428087d2016-01-14 16:04:28 +01001093
Michal Vasko71090fc2016-05-24 16:37:28 +02001094 pthread_mutex_unlock(cur_session->ti_lock);
1095 if (cur_session->status != NC_STATUS_RUNNING) {
1096 ret |= NC_PSPOLL_SESSION_TERM;
1097 if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) {
1098 ret |= NC_PSPOLL_SESSION_ERROR;
1099 }
Michal Vasko428087d2016-01-14 16:04:28 +01001100 }
Radek Krejcif93c7d42016-04-06 13:41:15 +02001101
Michal Vaskoca4a2422016-02-02 12:17:14 +01001102 nc_server_rpc_free(rpc, server_opts.ctx);
Michal Vaskobd8ef262016-01-20 11:09:27 +01001103
1104 /* is there some other socket waiting? */
1105 for (++i; i < ps->session_count; ++i) {
Michal Vasko3a715132016-01-21 15:40:31 +01001106 if (ps->pfds[i].revents) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001107 ret |= NC_PSPOLL_PENDING;
1108 break;
Michal Vaskobd8ef262016-01-20 11:09:27 +01001109 }
1110 }
1111
Michal Vasko48a63ed2016-03-01 09:48:21 +01001112finish:
1113 /* UNLOCK */
Michal Vaskobe86fe32016-04-07 10:43:03 +02001114 nc_ps_unlock(ps);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001115 return ret;
Michal Vasko428087d2016-01-14 16:04:28 +01001116}
1117
Michal Vaskod09eae62016-02-01 10:32:52 +01001118API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001119nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *))
Michal Vaskod09eae62016-02-01 10:32:52 +01001120{
1121 uint16_t i;
1122 struct nc_session *session;
1123
Michal Vasko9a25e932016-02-01 10:36:42 +01001124 if (!ps) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001125 ERRARG("ps");
Michal Vasko9a25e932016-02-01 10:36:42 +01001126 return;
1127 }
1128
Michal Vasko48a63ed2016-03-01 09:48:21 +01001129 /* LOCK */
Michal Vaskobe86fe32016-04-07 10:43:03 +02001130 if (nc_ps_lock(ps)) {
1131 return;
1132 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001133
Michal Vasko48a63ed2016-03-01 09:48:21 +01001134 if (all) {
Radek Krejci4f8042c2016-03-03 13:11:26 +01001135 for (i = 0; i < ps->session_count; i++) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001136 nc_session_free(ps->sessions[i], data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001137 }
1138 free(ps->sessions);
1139 ps->sessions = NULL;
1140 free(ps->pfds);
1141 ps->pfds = NULL;
1142 ps->session_count = 0;
1143 } else {
1144 for (i = 0; i < ps->session_count; ) {
1145 if (ps->sessions[i]->status != NC_STATUS_RUNNING) {
1146 session = ps->sessions[i];
Radek Krejcid5f978f2016-03-03 13:14:45 +01001147 _nc_ps_del_session(ps, NULL, i);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001148 nc_session_free(session, data_free);
Michal Vasko48a63ed2016-03-01 09:48:21 +01001149 continue;
1150 }
1151
1152 ++i;
1153 }
Michal Vaskod09eae62016-02-01 10:32:52 +01001154 }
Michal Vasko48a63ed2016-03-01 09:48:21 +01001155
1156 /* UNLOCK */
Michal Vaskobe86fe32016-04-07 10:43:03 +02001157 nc_ps_unlock(ps);
Michal Vaskod09eae62016-02-01 10:32:52 +01001158}
1159
Radek Krejci53691be2016-02-22 13:58:37 +01001160#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vasko9e036d52016-01-08 10:49:26 +01001161
Michal Vasko3031aae2016-01-27 16:07:18 +01001162int
1163nc_server_add_endpt_listen(const char *name, const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01001164{
1165 int sock;
Michal Vasko3031aae2016-01-27 16:07:18 +01001166 uint16_t i;
Radek Krejci53691be2016-02-22 13:58:37 +01001167#ifdef NC_ENABLED_SSH
Michal Vasko08a629a2016-02-02 12:20:47 +01001168 struct nc_server_ssh_opts *ssh_opts;
1169#endif
Michal Vasko9e036d52016-01-08 10:49:26 +01001170
Michal Vasko45e53ae2016-04-07 11:46:03 +02001171 if (!name) {
1172 ERRARG("name");
1173 return -1;
1174 } else if (!address) {
1175 ERRARG("address");
1176 return -1;
1177 } else if (!port) {
1178 ERRARG("port");
Michal Vasko9e036d52016-01-08 10:49:26 +01001179 return -1;
1180 }
1181
Michal Vasko51e514d2016-02-02 15:51:52 +01001182 /* WRITE LOCK */
1183 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001184
1185 /* check name uniqueness */
1186 for (i = 0; i < server_opts.endpt_count; ++i) {
Michal Vaskod4c03a82016-02-08 15:27:26 +01001187 if ((server_opts.binds[i].ti == ti) && !strcmp(server_opts.endpts[i].name, name)) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001188 ERR("Endpoint \"%s\" already exists.", name);
Michal Vasko51e514d2016-02-02 15:51:52 +01001189 /* WRITE UNLOCK */
Michal Vasko9faf1c82016-02-01 13:26:19 +01001190 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko3031aae2016-01-27 16:07:18 +01001191 return -1;
1192 }
1193 }
1194
Michal Vasko9e036d52016-01-08 10:49:26 +01001195 sock = nc_sock_listen(address, port);
1196 if (sock == -1) {
Michal Vasko51e514d2016-02-02 15:51:52 +01001197 /* WRITE UNLOCK */
1198 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko9e036d52016-01-08 10:49:26 +01001199 return -1;
1200 }
1201
Michal Vasko3031aae2016-01-27 16:07:18 +01001202 ++server_opts.endpt_count;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001203 server_opts.binds = nc_realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds);
1204 server_opts.endpts = nc_realloc(server_opts.endpts, server_opts.endpt_count * sizeof *server_opts.endpts);
1205 if (!server_opts.binds || !server_opts.endpts) {
1206 ERRMEM;
1207 /* WRITE UNLOCK */
1208 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko0f74da52016-03-03 08:52:52 +01001209 close(sock);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001210 return -1;
1211 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001212
Michal Vasko3031aae2016-01-27 16:07:18 +01001213 server_opts.endpts[server_opts.endpt_count - 1].name = lydict_insert(server_opts.ctx, name, 0);
1214 server_opts.binds[server_opts.endpt_count - 1].address = lydict_insert(server_opts.ctx, address, 0);
Michal Vasko3031aae2016-01-27 16:07:18 +01001215 server_opts.binds[server_opts.endpt_count - 1].port = port;
1216 server_opts.binds[server_opts.endpt_count - 1].sock = sock;
1217 server_opts.binds[server_opts.endpt_count - 1].ti = ti;
1218 switch (ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001219#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001220 case NC_TI_LIBSSH:
Michal Vasko08a629a2016-02-02 12:20:47 +01001221 ssh_opts = calloc(1, sizeof *ssh_opts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001222 if (!ssh_opts) {
1223 ERRMEM;
1224 /* WRITE UNLOCK */
1225 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1226 return -1;
1227 }
Michal Vasko08a629a2016-02-02 12:20:47 +01001228 /* set default values */
1229 ssh_opts->auth_methods = NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE;
1230 ssh_opts->auth_attempts = 3;
1231 ssh_opts->auth_timeout = 10;
1232
1233 server_opts.endpts[server_opts.endpt_count - 1].ti_opts = ssh_opts;
Michal Vasko3031aae2016-01-27 16:07:18 +01001234 break;
1235#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001236#ifdef NC_ENABLED_TLS
Michal Vasko3031aae2016-01-27 16:07:18 +01001237 case NC_TI_OPENSSL:
1238 server_opts.endpts[server_opts.endpt_count - 1].ti_opts = calloc(1, sizeof(struct nc_server_tls_opts));
Michal Vasko4eb3c312016-03-01 14:09:37 +01001239 if (!server_opts.endpts[server_opts.endpt_count - 1].ti_opts) {
1240 ERRMEM;
1241 /* WRITE UNLOCK */
1242 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1243 return -1;
1244 }
Michal Vasko3031aae2016-01-27 16:07:18 +01001245 break;
1246#endif
1247 default:
1248 ERRINT;
1249 server_opts.endpts[server_opts.endpt_count - 1].ti_opts = NULL;
1250 break;
1251 }
1252 pthread_mutex_init(&server_opts.endpts[server_opts.endpt_count - 1].endpt_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001253
Michal Vasko3031aae2016-01-27 16:07:18 +01001254 /* WRITE UNLOCK */
1255 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001256
Michal Vasko9e036d52016-01-08 10:49:26 +01001257 return 0;
1258}
1259
Michal Vasko3031aae2016-01-27 16:07:18 +01001260int
Michal Vaskoda514772016-02-01 11:32:01 +01001261nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port, NC_TRANSPORT_IMPL ti)
1262{
1263 struct nc_endpt *endpt;
1264 struct nc_bind *bind = NULL;
1265 uint16_t i;
1266 int sock;
1267
Michal Vasko45e53ae2016-04-07 11:46:03 +02001268 if (!endpt_name) {
1269 ERRARG("endpt_name");
1270 return -1;
1271 } else if ((!address && !port) || (address && port)) {
1272 ERRARG("address and port");
1273 return -1;
1274 } else if (!ti) {
1275 ERRARG("ti");
Michal Vaskoda514772016-02-01 11:32:01 +01001276 return -1;
1277 }
1278
Michal Vasko51e514d2016-02-02 15:51:52 +01001279 /* LOCK */
Michal Vaskoda514772016-02-01 11:32:01 +01001280 endpt = nc_server_endpt_lock(endpt_name, ti);
1281 if (!endpt) {
1282 return -1;
1283 }
1284
1285 /* we need to learn the index, to get the bind :-/ */
1286 for (i = 0; i < server_opts.endpt_count; ++i) {
1287 if (&server_opts.endpts[i] == endpt) {
1288 bind = &server_opts.binds[i];
1289 }
1290 }
1291 if (!bind) {
1292 ERRINT;
Michal Vasko51e514d2016-02-02 15:51:52 +01001293 goto fail;
Michal Vaskoda514772016-02-01 11:32:01 +01001294 }
1295
1296 if (address) {
1297 sock = nc_sock_listen(address, bind->port);
1298 } else {
1299 sock = nc_sock_listen(bind->address, port);
1300 }
1301 if (sock == -1) {
Michal Vasko51e514d2016-02-02 15:51:52 +01001302 goto fail;
Michal Vaskoda514772016-02-01 11:32:01 +01001303 }
1304
1305 /* close old socket, update parameters */
1306 close(bind->sock);
1307 bind->sock = sock;
1308 if (address) {
1309 lydict_remove(server_opts.ctx, bind->address);
1310 bind->address = lydict_insert(server_opts.ctx, address, 0);
1311 } else {
1312 bind->port = port;
1313 }
1314
Michal Vasko51e514d2016-02-02 15:51:52 +01001315 /* UNLOCK */
Michal Vasko7a93af72016-02-01 16:00:15 +01001316 nc_server_endpt_unlock(endpt);
Michal Vaskoda514772016-02-01 11:32:01 +01001317 return 0;
Michal Vasko51e514d2016-02-02 15:51:52 +01001318
1319fail:
1320 /* UNLOCK */
1321 nc_server_endpt_unlock(endpt);
1322 return -1;
Michal Vaskoda514772016-02-01 11:32:01 +01001323}
1324
1325int
Michal Vasko3031aae2016-01-27 16:07:18 +01001326nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti)
Michal Vasko9e036d52016-01-08 10:49:26 +01001327{
1328 uint32_t i;
1329 int ret = -1;
1330
Michal Vasko3031aae2016-01-27 16:07:18 +01001331 /* WRITE LOCK */
1332 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001333
Michal Vasko3031aae2016-01-27 16:07:18 +01001334 if (!name && !ti) {
1335 /* remove all */
Michal Vasko3031aae2016-01-27 16:07:18 +01001336 for (i = 0; i < server_opts.endpt_count; ++i) {
1337 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko11d142a2016-01-19 15:58:24 +01001338 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
Michal Vasko51e514d2016-02-02 15:51:52 +01001339
Michal Vasko3031aae2016-01-27 16:07:18 +01001340 close(server_opts.binds[i].sock);
1341 pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock);
1342 switch (server_opts.binds[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001343#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001344 case NC_TI_LIBSSH:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001345 nc_server_ssh_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001346 break;
1347#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001348#ifdef NC_ENABLED_TLS
Michal Vasko3031aae2016-01-27 16:07:18 +01001349 case NC_TI_OPENSSL:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001350 nc_server_tls_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001351 break;
1352#endif
1353 default:
1354 ERRINT;
1355 break;
1356 }
1357 free(server_opts.endpts[i].ti_opts);
Michal Vasko9e036d52016-01-08 10:49:26 +01001358
Michal Vasko9e036d52016-01-08 10:49:26 +01001359 ret = 0;
1360 }
Michal Vasko7ddc5702016-02-08 15:29:39 +01001361 free(server_opts.binds);
1362 server_opts.binds = NULL;
Michal Vasko3031aae2016-01-27 16:07:18 +01001363 free(server_opts.endpts);
1364 server_opts.endpts = NULL;
1365 server_opts.endpt_count = 0;
1366
Michal Vasko1a38c862016-01-15 15:50:07 +01001367 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001368 /* remove one name endpoint or all ti endpoints */
1369 for (i = 0; i < server_opts.endpt_count; ++i) {
1370 if ((server_opts.binds[i].ti == ti) &&
1371 (!name || !strcmp(server_opts.endpts[i].name, name))) {
1372
Michal Vasko3031aae2016-01-27 16:07:18 +01001373 lydict_remove(server_opts.ctx, server_opts.endpts[i].name);
Michal Vasko11d142a2016-01-19 15:58:24 +01001374 lydict_remove(server_opts.ctx, server_opts.binds[i].address);
Michal Vasko3031aae2016-01-27 16:07:18 +01001375 close(server_opts.binds[i].sock);
1376 pthread_mutex_destroy(&server_opts.endpts[i].endpt_lock);
1377 switch (server_opts.binds[i].ti) {
Radek Krejci53691be2016-02-22 13:58:37 +01001378#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001379 case NC_TI_LIBSSH:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001380 nc_server_ssh_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001381 break;
1382#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001383#ifdef NC_ENABLED_TLS
Michal Vasko3031aae2016-01-27 16:07:18 +01001384 case NC_TI_OPENSSL:
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001385 nc_server_tls_clear_opts(server_opts.endpts[i].ti_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +01001386 break;
1387#endif
1388 default:
1389 ERRINT;
1390 break;
1391 }
1392 free(server_opts.endpts[i].ti_opts);
Michal Vasko1a38c862016-01-15 15:50:07 +01001393
Michal Vasko3031aae2016-01-27 16:07:18 +01001394 --server_opts.endpt_count;
Michal Vaskoc0256492016-02-02 12:19:06 +01001395 if (i < server_opts.endpt_count) {
1396 memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds);
1397 memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts);
1398 } else if (!server_opts.endpt_count) {
1399 free(server_opts.binds);
1400 server_opts.binds = NULL;
1401 free(server_opts.endpts);
1402 server_opts.endpts = NULL;
1403 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001404
1405 ret = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +01001406
1407 if (name) {
1408 /* one name endpoint removed, they are unique, we're done */
1409 break;
1410 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001411 }
1412 }
Michal Vasko9e036d52016-01-08 10:49:26 +01001413 }
1414
Michal Vasko3031aae2016-01-27 16:07:18 +01001415 /* WRITE UNLOCK */
1416 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +01001417
Michal Vasko9e036d52016-01-08 10:49:26 +01001418 return ret;
1419}
1420
Michal Vasko71090fc2016-05-24 16:37:28 +02001421API NC_MSG_TYPE
Michal Vasko1a38c862016-01-15 15:50:07 +01001422nc_accept(int timeout, struct nc_session **session)
Michal Vasko9e036d52016-01-08 10:49:26 +01001423{
Michal Vasko71090fc2016-05-24 16:37:28 +02001424 NC_MSG_TYPE msgtype;
Michal Vasko1a38c862016-01-15 15:50:07 +01001425 int sock, ret;
Michal Vasko5c2f7952016-01-22 13:16:31 +01001426 char *host = NULL;
Michal Vasko3031aae2016-01-27 16:07:18 +01001427 uint16_t port, idx;
Michal Vasko9e036d52016-01-08 10:49:26 +01001428
Michal Vasko45e53ae2016-04-07 11:46:03 +02001429 if (!server_opts.ctx) {
1430 ERRINIT;
Michal Vasko71090fc2016-05-24 16:37:28 +02001431 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001432 } else if (!session) {
1433 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001434 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001435 }
1436
Michal Vasko51e514d2016-02-02 15:51:52 +01001437 /* we have to hold WRITE for the whole time, since there is not
1438 * a way of downgrading the lock to READ */
1439 /* WRITE LOCK */
1440 pthread_rwlock_wrlock(&server_opts.endpt_array_lock);
1441
1442 if (!server_opts.endpt_count) {
Michal Vasko45e53ae2016-04-07 11:46:03 +02001443 ERRINIT;
Michal Vasko51e514d2016-02-02 15:51:52 +01001444 /* WRITE UNLOCK */
1445 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001446 return NC_MSG_ERROR;
Michal Vasko51e514d2016-02-02 15:51:52 +01001447 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001448
Michal Vasko3031aae2016-01-27 16:07:18 +01001449 ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &idx);
Michal Vaskob48aa812016-01-18 14:13:09 +01001450
Michal Vasko50456e82016-02-02 12:16:08 +01001451 if (ret < 1) {
Michal Vasko51e514d2016-02-02 15:51:52 +01001452 /* WRITE UNLOCK */
Michal Vasko3031aae2016-01-27 16:07:18 +01001453 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
Michal Vaskob737d752016-02-09 09:01:27 +01001454 free(host);
Michal Vasko5e203472016-05-30 15:27:58 +02001455 if (!ret) {
1456 return NC_MSG_WOULDBLOCK;
1457 }
Michal Vasko71090fc2016-05-24 16:37:28 +02001458 return NC_MSG_ERROR;
Michal Vasko9e036d52016-01-08 10:49:26 +01001459 }
Michal Vaskob48aa812016-01-18 14:13:09 +01001460 sock = ret;
Michal Vasko9e036d52016-01-08 10:49:26 +01001461
Michal Vasko1a38c862016-01-15 15:50:07 +01001462 *session = calloc(1, sizeof **session);
Michal Vasko686aa312016-01-21 15:58:18 +01001463 if (!(*session)) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001464 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001465 close(sock);
Michal Vasko5c2f7952016-01-22 13:16:31 +01001466 free(host);
Michal Vasko71090fc2016-05-24 16:37:28 +02001467 msgtype = NC_MSG_ERROR;
1468 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001469 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001470 (*session)->status = NC_STATUS_STARTING;
1471 (*session)->side = NC_SERVER;
1472 (*session)->ctx = server_opts.ctx;
1473 (*session)->flags = NC_SESSION_SHAREDCTX;
1474 (*session)->host = lydict_insert_zc(server_opts.ctx, host);
1475 (*session)->port = port;
Michal Vasko9e036d52016-01-08 10:49:26 +01001476
1477 /* transport lock */
Michal Vasko1a38c862016-01-15 15:50:07 +01001478 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1479 if (!(*session)->ti_lock) {
Michal Vasko9e036d52016-01-08 10:49:26 +01001480 ERRMEM;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001481 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001482 msgtype = NC_MSG_ERROR;
1483 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001484 }
Michal Vasko1a38c862016-01-15 15:50:07 +01001485 pthread_mutex_init((*session)->ti_lock, NULL);
Michal Vasko9e036d52016-01-08 10:49:26 +01001486
Michal Vasko2cc4c682016-03-01 09:16:48 +01001487 (*session)->data = server_opts.endpts[idx].ti_opts;
Michal Vasko3031aae2016-01-27 16:07:18 +01001488
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001489 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001490#ifdef NC_ENABLED_SSH
Michal Vasko3031aae2016-01-27 16:07:18 +01001491 if (server_opts.binds[idx].ti == NC_TI_LIBSSH) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001492 ret = nc_accept_ssh_session(*session, sock, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001493 if (ret < 0) {
1494 msgtype = NC_MSG_ERROR;
1495 goto cleanup;
1496 } else if (!ret) {
1497 msgtype = NC_MSG_WOULDBLOCK;
1498 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001499 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001500 } else
1501#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001502#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001503 if (server_opts.binds[idx].ti == NC_TI_OPENSSL) {
Michal Vasko0190bc32016-03-02 15:47:49 +01001504 ret = nc_accept_tls_session(*session, sock, timeout);
Michal Vasko71090fc2016-05-24 16:37:28 +02001505 if (ret < 0) {
1506 msgtype = NC_MSG_ERROR;
1507 goto cleanup;
1508 } else if (!ret) {
1509 msgtype = NC_MSG_WOULDBLOCK;
1510 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001511 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001512 } else
1513#endif
1514 {
Michal Vasko9e036d52016-01-08 10:49:26 +01001515 ERRINT;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001516 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001517 msgtype = NC_MSG_ERROR;
1518 goto cleanup;
Michal Vasko9e036d52016-01-08 10:49:26 +01001519 }
1520
Michal Vasko2cc4c682016-03-01 09:16:48 +01001521 (*session)->data = NULL;
1522
Michal Vasko51e514d2016-02-02 15:51:52 +01001523 /* WRITE UNLOCK */
Michal Vasko3031aae2016-01-27 16:07:18 +01001524 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1525
Michal Vaskob48aa812016-01-18 14:13:09 +01001526 /* assign new SID atomically */
1527 /* LOCK */
1528 pthread_spin_lock(&server_opts.sid_lock);
1529 (*session)->id = server_opts.new_session_id++;
1530 /* UNLOCK */
1531 pthread_spin_unlock(&server_opts.sid_lock);
1532
Michal Vasko9e036d52016-01-08 10:49:26 +01001533 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001534 msgtype = nc_handshake(*session);
1535 if (msgtype != NC_MSG_HELLO) {
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001536 nc_session_free(*session, NULL);
Michal Vasko3031aae2016-01-27 16:07:18 +01001537 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001538 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001539 }
Michal Vaskof8352352016-05-24 09:11:36 +02001540 (*session)->session_start = time(NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001541 (*session)->status = NC_STATUS_RUNNING;
Michal Vasko9e036d52016-01-08 10:49:26 +01001542
Michal Vasko71090fc2016-05-24 16:37:28 +02001543 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001544
Michal Vasko71090fc2016-05-24 16:37:28 +02001545cleanup:
Michal Vasko3031aae2016-01-27 16:07:18 +01001546 /* WRITE UNLOCK */
1547 pthread_rwlock_unlock(&server_opts.endpt_array_lock);
1548
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001549 nc_session_free(*session, NULL);
Michal Vasko1a38c862016-01-15 15:50:07 +01001550 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001551 return msgtype;
Michal Vasko9e036d52016-01-08 10:49:26 +01001552}
1553
Michal Vasko71090fc2016-05-24 16:37:28 +02001554NC_MSG_TYPE
Michal Vasko8f5270d2016-02-29 16:22:25 +01001555nc_connect_callhome(const char *host, uint16_t port, NC_TRANSPORT_IMPL ti, struct nc_session **session)
Michal Vaskob05053d2016-01-22 16:12:06 +01001556{
Michal Vasko71090fc2016-05-24 16:37:28 +02001557 NC_MSG_TYPE msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01001558 int sock, ret;
1559
Michal Vasko45e53ae2016-04-07 11:46:03 +02001560 if (!host) {
1561 ERRARG("host");
Michal Vasko71090fc2016-05-24 16:37:28 +02001562 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001563 } else if (!port) {
1564 ERRARG("port");
Michal Vasko71090fc2016-05-24 16:37:28 +02001565 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001566 } else if (!ti) {
1567 ERRARG("ti");
Michal Vasko71090fc2016-05-24 16:37:28 +02001568 return NC_MSG_ERROR;
Michal Vasko45e53ae2016-04-07 11:46:03 +02001569 } else if (!session) {
1570 ERRARG("session");
Michal Vasko71090fc2016-05-24 16:37:28 +02001571 return NC_MSG_ERROR;
Michal Vaskoc61c4492016-01-25 11:13:34 +01001572 }
1573
Michal Vaskob05053d2016-01-22 16:12:06 +01001574 sock = nc_sock_connect(host, port);
Michal Vaskoc61c4492016-01-25 11:13:34 +01001575 if (sock < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001576 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001577 }
1578
1579 *session = calloc(1, sizeof **session);
1580 if (!(*session)) {
1581 ERRMEM;
1582 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001583 return NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001584 }
1585 (*session)->status = NC_STATUS_STARTING;
1586 (*session)->side = NC_SERVER;
1587 (*session)->ctx = server_opts.ctx;
1588 (*session)->flags = NC_SESSION_SHAREDCTX | NC_SESSION_CALLHOME;
Michal Vaskob05053d2016-01-22 16:12:06 +01001589 (*session)->host = lydict_insert(server_opts.ctx, host, 0);
Michal Vaskob05053d2016-01-22 16:12:06 +01001590 (*session)->port = port;
1591
1592 /* transport lock */
1593 (*session)->ti_lock = malloc(sizeof *(*session)->ti_lock);
1594 if (!(*session)->ti_lock) {
1595 ERRMEM;
1596 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001597 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001598 goto fail;
1599 }
1600 pthread_mutex_init((*session)->ti_lock, NULL);
1601
1602 /* sock gets assigned to session or closed */
Radek Krejci53691be2016-02-22 13:58:37 +01001603#ifdef NC_ENABLED_SSH
Michal Vaskob05053d2016-01-22 16:12:06 +01001604 if (ti == NC_TI_LIBSSH) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001605 /* OPTS LOCK */
1606 pthread_mutex_lock(&ssh_ch_opts_lock);
1607
Michal Vasko2cc4c682016-03-01 09:16:48 +01001608 (*session)->data = &ssh_ch_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001609 ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01001610 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001611
1612 /* OPTS UNLOCK */
1613 pthread_mutex_unlock(&ssh_ch_opts_lock);
1614
Michal Vasko71090fc2016-05-24 16:37:28 +02001615 if (ret < 0) {
1616 msgtype = NC_MSG_ERROR;
1617 goto fail;
1618 } else if (!ret) {
1619 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01001620 goto fail;
1621 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001622 } else
1623#endif
Radek Krejci53691be2016-02-22 13:58:37 +01001624#ifdef NC_ENABLED_TLS
Michal Vasko3d865d22016-01-28 16:00:53 +01001625 if (ti == NC_TI_OPENSSL) {
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001626 /* OPTS LOCK */
1627 pthread_mutex_lock(&tls_ch_opts_lock);
1628
Michal Vasko2cc4c682016-03-01 09:16:48 +01001629 (*session)->data = &tls_ch_opts;
Michal Vasko0190bc32016-03-02 15:47:49 +01001630 ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT);
Michal Vasko2cc4c682016-03-01 09:16:48 +01001631 (*session)->data = NULL;
Michal Vaskoc6b9c7b2016-01-28 11:10:08 +01001632
1633 /* OPTS UNLOCK */
1634 pthread_mutex_unlock(&tls_ch_opts_lock);
1635
Michal Vasko71090fc2016-05-24 16:37:28 +02001636 if (ret < 0) {
1637 msgtype = NC_MSG_ERROR;
1638 goto fail;
1639 } else if (!ret) {
1640 msgtype = NC_MSG_WOULDBLOCK;
Michal Vaskob05053d2016-01-22 16:12:06 +01001641 goto fail;
1642 }
Michal Vasko3d865d22016-01-28 16:00:53 +01001643 } else
1644#endif
1645 {
Michal Vaskob05053d2016-01-22 16:12:06 +01001646 ERRINT;
1647 close(sock);
Michal Vasko71090fc2016-05-24 16:37:28 +02001648 msgtype = NC_MSG_ERROR;
Michal Vaskob05053d2016-01-22 16:12:06 +01001649 goto fail;
1650 }
1651
1652 /* assign new SID atomically */
1653 /* LOCK */
1654 pthread_spin_lock(&server_opts.sid_lock);
1655 (*session)->id = server_opts.new_session_id++;
1656 /* UNLOCK */
1657 pthread_spin_unlock(&server_opts.sid_lock);
1658
1659 /* NETCONF handshake */
Michal Vasko71090fc2016-05-24 16:37:28 +02001660 msgtype = nc_handshake(*session);
1661 if (msgtype != NC_MSG_HELLO) {
Michal Vaskob05053d2016-01-22 16:12:06 +01001662 goto fail;
1663 }
Michal Vaskof8352352016-05-24 09:11:36 +02001664 (*session)->session_start = time(NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01001665 (*session)->status = NC_STATUS_RUNNING;
1666
Michal Vasko71090fc2016-05-24 16:37:28 +02001667 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01001668
1669fail:
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001670 nc_session_free(*session, NULL);
Michal Vaskob05053d2016-01-22 16:12:06 +01001671 *session = NULL;
Michal Vasko71090fc2016-05-24 16:37:28 +02001672 return msgtype;
Michal Vaskob05053d2016-01-22 16:12:06 +01001673}
1674
Radek Krejci53691be2016-02-22 13:58:37 +01001675#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskof8352352016-05-24 09:11:36 +02001676
Michal Vaskoc45ebd32016-05-25 11:17:36 +02001677API time_t
1678nc_session_get_start_time(const struct nc_session *session)
Michal Vaskof8352352016-05-24 09:11:36 +02001679{
1680 if (!session) {
1681 ERRARG("session");
Michal Vaskoc45ebd32016-05-25 11:17:36 +02001682 return 0;
Michal Vaskof8352352016-05-24 09:11:36 +02001683 }
1684
Michal Vaskoc45ebd32016-05-25 11:17:36 +02001685 return session->session_start;
Michal Vaskof8352352016-05-24 09:11:36 +02001686}