blob: 08315ec0db3b9ed85be9bfc661b4b663ddc32032 [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
2 * \file session.c
Michal Vasko086311b2016-01-08 09:53:11 +01003 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 - general session functions
Radek Krejci206fcd62015-10-07 15:42:48 +02005 *
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
Radek Krejci206fcd62015-10-07 15:42:48 +020013 */
14
Radek Krejci206fcd62015-10-07 15:42:48 +020015#include <errno.h>
Radek Krejci952eb862016-01-08 14:22:55 +010016#include <stdlib.h>
Michal Vasko3512e402016-01-28 16:22:34 +010017#include <string.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020018#include <pthread.h>
Michal Vasko58f31552016-01-19 12:39:15 +010019#include <time.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020020#include <libyang/libyang.h>
21
Michal Vasko5e228792016-02-03 15:30:24 +010022#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010023#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010024#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010025
Radek Krejci53691be2016-02-22 13:58:37 +010026#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020027
Michal Vasko086311b2016-01-08 09:53:11 +010028# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020029
Radek Krejci53691be2016-02-22 13:58:37 +010030#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020031
Radek Krejci53691be2016-02-22 13:58:37 +010032#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010033
Michal Vasko5e228792016-02-03 15:30:24 +010034# include <openssl/engine.h>
35# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010036# include <openssl/err.h>
37
Radek Krejci53691be2016-02-22 13:58:37 +010038#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010039
Michal Vasko086311b2016-01-08 09:53:11 +010040/* in seconds */
41#define NC_CLIENT_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020042
Michal Vasko05ba9df2016-01-13 14:40:27 +010043/* in milliseconds */
44#define NC_CLOSE_REPLY_TIMEOUT 200
45
Michal Vasko086311b2016-01-08 09:53:11 +010046extern struct nc_server_opts server_opts;
47
Michal Vasko96164bf2016-01-21 15:41:58 +010048/*
49 * @return 1 - success
50 * 0 - timeout
51 * -1 - error
52 */
53int
54nc_timedlock(pthread_mutex_t *lock, int timeout, int *elapsed)
55{
56 int ret;
57 struct timespec ts_timeout, ts_old, ts_new;
58
59 if (timeout > 0) {
60 clock_gettime(CLOCK_REALTIME, &ts_timeout);
61
62 if (elapsed) {
63 ts_old = ts_timeout;
64 }
65
66 ts_timeout.tv_sec += timeout / 1000;
67 ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
68
69 ret = pthread_mutex_timedlock(lock, &ts_timeout);
70
71 if (elapsed) {
72 clock_gettime(CLOCK_REALTIME, &ts_new);
73
74 *elapsed += (ts_new.tv_sec - ts_old.tv_sec) * 1000;
75 *elapsed += (ts_new.tv_nsec - ts_old.tv_nsec) / 1000000;
76 }
77 } else if (!timeout) {
78 ret = pthread_mutex_trylock(lock);
79 } else { /* timeout == -1 */
80 ret = pthread_mutex_lock(lock);
81 }
82
83 if (ret == ETIMEDOUT) {
84 /* timeout */
85 return 0;
86 } else if (ret) {
87 /* error */
88 ERR("Mutex lock failed (%s).", strerror(errno));
89 return -1;
90 }
91
92 /* ok */
93 return 1;
94}
95
Michal Vasko8dadf782016-01-15 10:29:36 +010096API NC_STATUS
97nc_session_get_status(const struct nc_session *session)
98{
Michal Vasko7f1c78b2016-01-19 09:52:14 +010099 if (!session) {
100 ERRARG;
101 return 0;
102 }
103
Michal Vasko8dadf782016-01-15 10:29:36 +0100104 return session->status;
105}
106
107API uint32_t
108nc_session_get_id(const struct nc_session *session)
109{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100110 if (!session) {
111 ERRARG;
112 return 0;
113 }
114
Michal Vasko8dadf782016-01-15 10:29:36 +0100115 return session->id;
116}
117
Michal Vasko174fe8e2016-02-17 15:38:09 +0100118API int
119nc_session_get_version(const struct nc_session *session)
120{
121 if (!session) {
122 ERRARG;
123 return -1;
124 }
125
126 return (session->version == NC_VERSION_10 ? 0 : 1);
127}
128
Michal Vasko8dadf782016-01-15 10:29:36 +0100129API NC_TRANSPORT_IMPL
130nc_session_get_ti(const struct nc_session *session)
131{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100132 if (!session) {
133 ERRARG;
134 return 0;
135 }
136
Michal Vasko8dadf782016-01-15 10:29:36 +0100137 return session->ti_type;
138}
139
140API const char *
141nc_session_get_username(const struct nc_session *session)
142{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100143 if (!session) {
144 ERRARG;
145 return NULL;
146 }
147
Michal Vasko8dadf782016-01-15 10:29:36 +0100148 return session->username;
149}
150
151API const char *
152nc_session_get_host(const struct nc_session *session)
153{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100154 if (!session) {
155 ERRARG;
156 return NULL;
157 }
158
Michal Vasko8dadf782016-01-15 10:29:36 +0100159 return session->host;
160}
161
162API uint16_t
163nc_session_get_port(const struct nc_session *session)
164{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100165 if (!session) {
166 ERRARG;
167 return 0;
168 }
169
Michal Vasko8dadf782016-01-15 10:29:36 +0100170 return session->port;
171}
172
Michal Vasko9a25e932016-02-01 10:36:42 +0100173API struct ly_ctx *
174nc_session_get_ctx(const struct nc_session *session)
175{
176 if (!session) {
177 ERRARG;
178 return NULL;
179 }
180
181 return session->ctx;
182}
183
Michal Vasko8dadf782016-01-15 10:29:36 +0100184API const char **
185nc_session_get_cpblts(const struct nc_session *session)
186{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100187 if (!session) {
188 ERRARG;
189 return NULL;
190 }
191
Michal Vasko8dadf782016-01-15 10:29:36 +0100192 return session->cpblts;
193}
194
195API const char *
196nc_session_cpblt(const struct nc_session *session, const char *capab)
197{
198 int i, len;
199
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100200 if (!session || !capab) {
201 ERRARG;
202 return NULL;
203 }
204
Michal Vasko8dadf782016-01-15 10:29:36 +0100205 len = strlen(capab);
206 for (i = 0; session->cpblts[i]; ++i) {
207 if (!strncmp(session->cpblts[i], capab, len)) {
208 return session->cpblts[i];
209 }
210 }
211
212 return NULL;
213}
214
Michal Vasko2cc4c682016-03-01 09:16:48 +0100215API void
216nc_session_set_data(struct nc_session *session, void *data)
217{
218 if (!session) {
219 ERRARG;
220 return;
221 }
222
223 session->data = data;
224}
225
226API void *
227nc_session_get_data(const struct nc_session *session)
228{
229 if (!session) {
230 ERRARG;
231 return NULL;
232 }
233
234 return session->data;
235}
236
Michal Vasko086311b2016-01-08 09:53:11 +0100237NC_MSG_TYPE
238nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200239{
Michal Vasko086311b2016-01-08 09:53:11 +0100240 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200241
Michal Vasko086311b2016-01-08 09:53:11 +0100242 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100243 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
244 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100245 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100246 }
247
Michal Vasko086311b2016-01-08 09:53:11 +0100248 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
249
250 if (r) {
251 return NC_MSG_ERROR;
252 }
253
254 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100255}
256
Radek Krejci695d4fa2015-10-22 13:23:54 +0200257API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100258nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200259{
260 int r, i;
Michal Vasko428087d2016-01-14 16:04:28 +0100261 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100262 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100263 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100264 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100265 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100266 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200267 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100268 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200269 void *p;
270
Michal Vasko428087d2016-01-14 16:04:28 +0100271 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200272 return;
273 }
274
275 /* mark session for closing */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100276 if (session->ti_lock) {
Michal Vasko4589bbe2016-01-29 09:41:30 +0100277 r = nc_timedlock(session->ti_lock, -1, NULL);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100278 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100279 return;
280 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200281 }
282
283 /* stop notifications loop if any */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100284 if (session->ntf_tid) {
285 tid = *session->ntf_tid;
286 free((pthread_t *)session->ntf_tid);
287 session->ntf_tid = NULL;
288 /* the thread now knows it should quit */
289
290 pthread_join(tid, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200291 }
292
Michal Vasko428087d2016-01-14 16:04:28 +0100293 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200294 /* cleanup message queues */
295 /* notifications */
Michal Vaskoad611702015-12-03 13:41:51 +0100296 for (contiter = session->notifs; contiter; ) {
297 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200298
Michal Vaskoad611702015-12-03 13:41:51 +0100299 p = contiter;
300 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200301 free(p);
302 }
303
304 /* rpc replies */
Michal Vaskoad611702015-12-03 13:41:51 +0100305 for (contiter = session->replies; contiter; ) {
306 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200307
Michal Vaskoad611702015-12-03 13:41:51 +0100308 p = contiter;
309 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200310 free(p);
311 }
312
313 /* send closing info to the other side */
314 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
315 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100316 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200317 } else {
318 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100319 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200320 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100321 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100322 case NC_MSG_REPLY:
323 LY_TREE_FOR(rpl->child, child) {
324 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
325 break;
326 }
327 }
328 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100329 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100330 }
Michal Vaskoad611702015-12-03 13:41:51 +0100331 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100332 break;
333 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100334 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100335 break;
336 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100337 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100338 break;
339 default:
340 /* cannot happen */
341 break;
342 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200343 }
344
345 /* list of server's capabilities */
346 if (session->cpblts) {
347 for (i = 0; session->cpblts[i]; i++) {
348 lydict_remove(session->ctx, session->cpblts[i]);
349 }
350 free(session->cpblts);
351 }
352 }
353
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100354 if (session->data && data_free) {
355 data_free(session->data);
356 }
357
Radek Krejci695d4fa2015-10-22 13:23:54 +0200358 session->status = NC_STATUS_CLOSING;
Michal Vasko428087d2016-01-14 16:04:28 +0100359 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200360
361 /* transport implementation cleanup */
362 switch (session->ti_type) {
363 case NC_TI_FD:
364 /* nothing needed - file descriptors were provided by caller,
365 * so it is up to the caller to close them correctly
366 * TODO use callbacks
367 */
Michal Vasko3512e402016-01-28 16:22:34 +0100368 /* just to avoid compiler warning */
369 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100370 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200371 break;
372
Radek Krejci53691be2016-02-22 13:58:37 +0100373#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200374 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100375 if (connected) {
376 ssh_channel_free(session->ti.libssh.channel);
377 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200378 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
379 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
380 * it.
381 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100382 multisession = 0;
383 if (session->ti.libssh.next) {
384 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
385 if (siter->status != NC_STATUS_STARTING) {
386 multisession = 1;
387 break;
388 }
389 }
390 }
391
392 if (!multisession) {
393 /* it's not multisession yet, but we still need to free the starting sessions */
394 if (session->ti.libssh.next) {
395 do {
396 siter = session->ti.libssh.next;
397 session->ti.libssh.next = siter->ti.libssh.next;
398
399 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100400 lydict_remove(session->ctx, session->username);
401 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100402 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100403 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100404 }
405
406 free(siter);
407 } while (session->ti.libssh.next != session);
408 }
Michal Vasko428087d2016-01-14 16:04:28 +0100409 if (connected) {
410 ssh_disconnect(session->ti.libssh.session);
411 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200412 ssh_free(session->ti.libssh.session);
413 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200414 /* remove the session from the list */
415 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100416 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200417 /* there will be only one session */
418 siter->ti.libssh.next = NULL;
419 } else {
420 /* there are still multiple sessions, keep the ring list */
421 siter->ti.libssh.next = session->ti.libssh.next;
422 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100423 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
424 if (session->flags & NC_SESSION_SSH_MSG_CB) {
425 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
426 if (siter->ti.libssh.next == session) {
427 ERRINT;
428 break;
429 }
430 }
431 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
432 siter->flags |= NC_SESSION_SSH_MSG_CB;
433 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200434 }
435 break;
436#endif
437
Radek Krejci53691be2016-02-22 13:58:37 +0100438#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200439 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100440 if (connected) {
441 SSL_shutdown(session->ti.tls);
442 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200443 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100444
445 X509_free(session->tls_cert);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200446 break;
447#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100448 case NC_TI_NONE:
449 ERRINT;
450 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200451 }
Michal Vasko428087d2016-01-14 16:04:28 +0100452
Radek Krejciac6d3472015-10-22 15:47:18 +0200453 lydict_remove(session->ctx, session->username);
454 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200455
456 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100457 if (session->ti_lock) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100458 pthread_mutex_unlock(session->ti_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +0100459 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100460 pthread_mutex_destroy(session->ti_lock);
461 free(session->ti_lock);
462 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200463 }
464
465 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100466 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200467 }
468
469 free(session);
470}
471
Michal Vasko086311b2016-01-08 09:53:11 +0100472static void
473add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
474{
475 if (*count == *size) {
476 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100477 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
478 if (!(*cpblts)) {
479 ERRMEM;
480 return;
481 }
Michal Vasko086311b2016-01-08 09:53:11 +0100482 }
483
484 if (capab) {
485 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
486 } else {
487 (*cpblts)[*count] = NULL;
488 }
489 ++(*count);
490}
491
492static const char **
493create_cpblts(struct ly_ctx *ctx)
494{
495 struct lyd_node *child, *child2, *yanglib;
496 struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL;
497 const char **cpblts;
498 const struct lys_module *mod;
Michal Vasko11d142a2016-01-19 15:58:24 +0100499 int size = 10, count, feat_count = 0, i, str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100500 char str[512];
501
502 yanglib = ly_ctx_info(ctx);
503 if (!yanglib) {
504 return NULL;
505 }
506
507 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100508 if (!cpblts) {
509 ERRMEM;
510 return NULL;
511 }
Michal Vasko086311b2016-01-08 09:53:11 +0100512 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
513 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
514 count = 2;
515
516 /* capabilities */
517
518 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
519 if (mod) {
520 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100521 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100522 }
523 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100524 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100525 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100526 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100527 }
528 }
529 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100530 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100531 }
532 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100533 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100534 }
535 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100536 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100537 }
538 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100539 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100540 }
541 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100542 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100543 }
544 }
545
546 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
547 if (mod) {
548 if (!server_opts.wd_basic_mode) {
549 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
550 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100551 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100552 switch (server_opts.wd_basic_mode) {
553 case NC_WD_ALL:
554 strcat(str, "?basic-mode=report-all");
555 break;
556 case NC_WD_TRIM:
557 strcat(str, "?basic-mode=trim");
558 break;
559 case NC_WD_EXPLICIT:
560 strcat(str, "?basic-mode=explicit");
561 break;
562 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100563 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100564 break;
565 }
566
567 if (server_opts.wd_also_supported) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100568 strcat(str, "&amp;also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100569 if (server_opts.wd_also_supported & NC_WD_ALL) {
570 strcat(str, "report-all,");
571 }
572 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
573 strcat(str, "report-all-tagged,");
574 }
575 if (server_opts.wd_also_supported & NC_WD_TRIM) {
576 strcat(str, "trim,");
577 }
578 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
579 strcat(str, "explicit,");
580 }
581 str[strlen(str) - 1] = '\0';
582
583 add_cpblt(ctx, str, &cpblts, &size, &count);
584 }
585 }
586 }
587
Michal Vasko1a38c862016-01-15 15:50:07 +0100588 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100589 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100590 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100591 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100592 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100593 }
594 }
595
596 /* models */
Michal Vasko086311b2016-01-08 09:53:11 +0100597 LY_TREE_FOR(yanglib->child, child) {
598 if (!strcmp(child->schema->name, "module")) {
599 LY_TREE_FOR(child->child, child2) {
600 if (!strcmp(child2->schema->name, "namespace")) {
601 ns = (struct lyd_node_leaf_list *)child2;
602 } else if (!strcmp(child2->schema->name, "name")) {
603 name = (struct lyd_node_leaf_list *)child2;
604 } else if (!strcmp(child2->schema->name, "revision")) {
605 rev = (struct lyd_node_leaf_list *)child2;
606 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100607 features = nc_realloc(features, ++feat_count * sizeof *features);
608 if (!features) {
609 ERRMEM;
610 free(cpblts);
611 return NULL;
612 }
Michal Vasko086311b2016-01-08 09:53:11 +0100613 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
614 }
615 }
616
617 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100618 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100619 continue;
620 }
621
Michal Vasko11d142a2016-01-19 15:58:24 +0100622 str_len = sprintf(str, "%s?module=%s&amp;revision=%s", ns->value_str, name->value_str, rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100623 if (feat_count) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100624 strcat(str, "&amp;features=");
Michal Vasko11d142a2016-01-19 15:58:24 +0100625 str_len += 14;
Michal Vasko086311b2016-01-08 09:53:11 +0100626 for (i = 0; i < feat_count; ++i) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100627 if (str_len + 1 + strlen(features[i]->value_str) >= 512) {
628 ERRINT;
629 break;
630 }
Michal Vasko086311b2016-01-08 09:53:11 +0100631 if (i) {
632 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100633 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100634 }
635 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100636 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100637 }
638 }
639
640 add_cpblt(ctx, str, &cpblts, &size, &count);
641
642 ns = NULL;
643 name = NULL;
644 rev = NULL;
645 free(features);
646 features = NULL;
647 feat_count = 0;
648 }
649 }
650
651 lyd_free(yanglib);
652
653 /* ending NULL capability */
654 add_cpblt(ctx, NULL, &cpblts, &size, &count);
655
656 return cpblts;
657}
658
Radek Krejci695d4fa2015-10-22 13:23:54 +0200659static int
660parse_cpblts(struct lyxml_elem *xml, const char ***list)
661{
662 struct lyxml_elem *cpblt;
663 int ver = -1;
664 int i = 0;
665
666 if (list) {
667 /* get the storage for server's capabilities */
668 LY_TREE_FOR(xml->child, cpblt) {
669 i++;
670 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100671 /* last item remains NULL */
672 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200673 if (!*list) {
674 ERRMEM;
675 return -1;
676 }
677 i = 0;
678 }
679
680 LY_TREE_FOR(xml->child, cpblt) {
681 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
682 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
683 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
684 return -1;
685 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
686 continue;
687 }
688
689 /* detect NETCONF version */
690 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
691 ver = 0;
692 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
693 ver = 1;
694 }
695
696 /* store capabilities */
697 if (list) {
698 (*list)[i] = cpblt->content;
699 cpblt->content = NULL;
700 i++;
701 }
702 }
703
704 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100705 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200706 }
707
708 return ver;
709}
710
711static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100712nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100713{
714 int r, i;
715 const char **cpblts;
716
Michal Vasko11d142a2016-01-19 15:58:24 +0100717 /* client side hello - send only NETCONF base capabilities */
718 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100719 if (!cpblts) {
720 ERRMEM;
721 return NC_MSG_ERROR;
722 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100723 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
724 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
725 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100726
Michal Vasko11d142a2016-01-19 15:58:24 +0100727 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100728
Michal Vasko086311b2016-01-08 09:53:11 +0100729 for (i = 0; cpblts[i]; ++i) {
730 lydict_remove(session->ctx, cpblts[i]);
731 }
732 free(cpblts);
733
734 if (r) {
735 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100736 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100737
738 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100739}
740
741static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100742nc_send_server_hello(struct nc_session *session)
743{
744 int r, i;
745 const char **cpblts;
746
747 cpblts = create_cpblts(session->ctx);
748
749 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
750
Michal Vasko11d142a2016-01-19 15:58:24 +0100751 for (i = 0; cpblts[i]; ++i) {
752 lydict_remove(session->ctx, cpblts[i]);
753 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100754 free(cpblts);
755
756 if (r) {
757 return NC_MSG_ERROR;
758 }
759
760 return NC_MSG_HELLO;
761}
762
763static NC_MSG_TYPE
764nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200765{
766 struct lyxml_elem *xml = NULL, *node;
767 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
768 int ver = -1;
769 char *str;
770 long long int id;
771 int flag = 0;
772
Michal Vasko05ba9df2016-01-13 14:40:27 +0100773 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200774
775 switch(msgtype) {
776 case NC_MSG_HELLO:
777 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100778 LY_TREE_FOR(xml->child, node) {
779 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
780 continue;
781 } else if (!strcmp(node->name, "session-id")) {
782 if (!node->content || !strlen(node->content)) {
783 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200784 goto error;
785 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100786 str = NULL;
787 id = strtoll(node->content, &str, 10);
788 if (*str || id < 1 || id > UINT32_MAX) {
789 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200790 goto error;
791 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100792 session->id = (uint32_t)id;
793 continue;
794 } else if (strcmp(node->name, "capabilities")) {
795 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200796 goto error;
797 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100798
799 if (flag) {
800 /* multiple capabilities elements */
801 ERR("Invalid <hello> message (multiple <capabilities> elements).");
802 goto error;
803 }
804 flag = 1;
805
806 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
807 goto error;
808 }
809 session->version = ver;
810 }
811
812 if (!session->id) {
813 ERR("Missing <session-id> in server's <hello>.");
814 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200815 }
816 break;
817 case NC_MSG_ERROR:
818 /* nothing special, just pass it out */
819 break;
820 default:
821 ERR("Unexpected message received instead of <hello>.");
822 msgtype = NC_MSG_ERROR;
823 }
824
825 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100826 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200827
828 return msgtype;
829
830error:
831 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100832 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200833
834 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200835}
836
Michal Vasko11d142a2016-01-19 15:58:24 +0100837static NC_MSG_TYPE
838nc_recv_server_hello(struct nc_session *session)
839{
840 struct lyxml_elem *xml = NULL, *node;
841 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
842 int ver = -1;
843 int flag = 0;
844
Michal Vaskoadb850e2016-01-20 14:06:32 +0100845 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100846
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100847 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100848 case NC_MSG_HELLO:
849 /* get know NETCONF version */
850 LY_TREE_FOR(xml->child, node) {
851 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
852 continue;
853 } else if (strcmp(node->name, "capabilities")) {
854 ERR("Unexpected <%s> element in client's <hello>.", node->name);
855 msgtype = NC_MSG_ERROR;
856 goto cleanup;
857 }
858
859 if (flag) {
860 /* multiple capabilities elements */
861 ERR("Invalid <hello> message (multiple <capabilities> elements).");
862 msgtype = NC_MSG_ERROR;
863 goto cleanup;
864 }
865 flag = 1;
866
867 if ((ver = parse_cpblts(node, NULL)) < 0) {
868 msgtype = NC_MSG_ERROR;
869 goto cleanup;
870 }
871 session->version = ver;
872 }
873 break;
874 case NC_MSG_ERROR:
875 /* nothing special, just pass it out */
876 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100877 case NC_MSG_WOULDBLOCK:
878 ERR("Client's <hello> timeout elapsed.");
879 msgtype = NC_MSG_ERROR;
880 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100881 default:
882 ERR("Unexpected message received instead of <hello>.");
883 msgtype = NC_MSG_ERROR;
884 }
885
886cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +0100887 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100888
889 return msgtype;
890}
891
Michal Vasko80cad7f2015-12-08 14:42:27 +0100892int
Michal Vasko086311b2016-01-08 09:53:11 +0100893nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100894{
Michal Vasko086311b2016-01-08 09:53:11 +0100895 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100896
Michal Vasko11d142a2016-01-19 15:58:24 +0100897 if (session->side == NC_CLIENT) {
898 type = nc_send_client_hello(session);
899 } else {
900 type = nc_send_server_hello(session);
901 }
902
Michal Vasko086311b2016-01-08 09:53:11 +0100903 if (type != NC_MSG_HELLO) {
904 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100905 }
906
Michal Vasko11d142a2016-01-19 15:58:24 +0100907 if (session->side == NC_CLIENT) {
908 type = nc_recv_client_hello(session);
909 } else {
910 type = nc_recv_server_hello(session);
911 }
912
Michal Vasko086311b2016-01-08 09:53:11 +0100913 if (type != NC_MSG_HELLO) {
914 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100915 }
916
Michal Vasko086311b2016-01-08 09:53:11 +0100917 return 0;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100918}
Michal Vasko086311b2016-01-08 09:53:11 +0100919
Radek Krejci53691be2016-02-22 13:58:37 +0100920#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100921
Michal Vasko8f0c0282016-02-29 10:17:14 +0100922static void
Michal Vasko086311b2016-01-08 09:53:11 +0100923nc_ssh_init(void)
924{
925 ssh_threads_set_callbacks(ssh_threads_get_pthread());
926 ssh_init();
927 ssh_set_log_level(verbose_level);
928}
929
Michal Vasko8f0c0282016-02-29 10:17:14 +0100930static void
Michal Vasko086311b2016-01-08 09:53:11 +0100931nc_ssh_destroy(void)
932{
Michal Vasko8f0c0282016-02-29 10:17:14 +0100933 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +0100934 ENGINE_cleanup();
935 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +0100936 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +0100937 ssh_finalize();
938}
939
Radek Krejci53691be2016-02-22 13:58:37 +0100940#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100941
Radek Krejci53691be2016-02-22 13:58:37 +0100942#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100943
Michal Vaskof0c92c02016-01-29 09:41:45 +0100944struct CRYPTO_dynlock_value {
945 pthread_mutex_t lock;
946};
947
Michal Vaskof0c92c02016-01-29 09:41:45 +0100948static struct CRYPTO_dynlock_value *
949tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
950{
951 struct CRYPTO_dynlock_value *value;
952
953 value = malloc(sizeof *value);
954 if (!value) {
955 ERRMEM;
956 return NULL;
957 }
958 pthread_mutex_init(&value->lock, NULL);
959
960 return value;
961}
962
963static void
964tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
965{
966 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
967 * I found ignored this fact, what do I know... */
968 if (mode & CRYPTO_LOCK) {
969 pthread_mutex_lock(&l->lock);
970 } else {
971 pthread_mutex_unlock(&l->lock);
972 }
973}
974
975static void
976tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
977{
978 pthread_mutex_destroy(&l->lock);
979 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100980}
981
Michal Vasko8f0c0282016-02-29 10:17:14 +0100982#endif /* NC_ENABLED_TLS */
983
984#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
985
986static pthread_mutex_t *tls_locks;
987
988static void
989tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
990{
991 if (mode & CRYPTO_LOCK) {
992 pthread_mutex_lock(tls_locks + n);
993 } else {
994 pthread_mutex_unlock(tls_locks + n);
995 }
996}
997
998static void
999tls_thread_id_func(CRYPTO_THREADID *tid)
1000{
1001 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1002}
1003
1004static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001005nc_tls_init(void)
1006{
1007 int i;
1008
1009 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001010 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001011 SSL_library_init();
1012
1013 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001014 if (!tls_locks) {
1015 ERRMEM;
1016 return;
1017 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001018 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1019 pthread_mutex_init(tls_locks + i, NULL);
1020 }
1021
Michal Vaskof0c92c02016-01-29 09:41:45 +01001022 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001023 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001024
1025 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1026 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1027 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001028}
1029
Michal Vasko8f0c0282016-02-29 10:17:14 +01001030static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001031nc_tls_destroy(void)
1032{
1033 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001034
Michal Vasko8f0c0282016-02-29 10:17:14 +01001035 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001036 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001037 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001038 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001039 ERR_free_strings();
1040 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001041
Michal Vaskob6e37262016-02-25 14:49:00 +01001042 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001043 CRYPTO_set_locking_callback(NULL);
1044 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1045 pthread_mutex_destroy(tls_locks + i);
1046 }
1047 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001048
1049 CRYPTO_set_dynlock_create_callback(NULL);
1050 CRYPTO_set_dynlock_lock_callback(NULL);
1051 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001052}
1053
Michal Vasko8f0c0282016-02-29 10:17:14 +01001054#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001055
Radek Krejci53691be2016-02-22 13:58:37 +01001056#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001057
Michal Vasko8f0c0282016-02-29 10:17:14 +01001058static void
Michal Vasko5e228792016-02-03 15:30:24 +01001059nc_ssh_tls_init(void)
1060{
1061 SSL_load_error_strings();
1062 ERR_load_BIO_strings();
1063 SSL_library_init();
1064
1065 nc_ssh_init();
1066
1067 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1068 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1069 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1070}
1071
Michal Vasko8f0c0282016-02-29 10:17:14 +01001072static void
Michal Vasko5e228792016-02-03 15:30:24 +01001073nc_ssh_tls_destroy(void)
1074{
1075 ERR_free_strings();
1076 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1077
1078 nc_ssh_destroy();
1079
1080 CRYPTO_set_dynlock_create_callback(NULL);
1081 CRYPTO_set_dynlock_lock_callback(NULL);
1082 CRYPTO_set_dynlock_destroy_callback(NULL);
1083}
1084
Radek Krejci53691be2016-02-22 13:58:37 +01001085#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001086
1087#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1088
1089API void
1090nc_thread_destroy(void)
1091{
1092 CRYPTO_THREADID crypto_tid;
1093
1094 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1095 //CRYPTO_cleanup_all_ex_data();
1096
1097 CRYPTO_THREADID_current(&crypto_tid);
1098 ERR_remove_thread_state(&crypto_tid);
1099}
1100
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001101#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1102
1103void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001104nc_init(void)
1105{
1106#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1107 nc_ssh_tls_init();
1108#elif defined(NC_ENABLED_SSH)
1109 nc_ssh_init();
1110#elif defined(NC_ENABLED_TLS)
1111 nc_tls_init();
1112#endif
1113}
1114
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001115void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001116nc_destroy(void)
1117{
1118#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1119 nc_ssh_tls_destroy();
1120#elif defined(NC_ENABLED_SSH)
1121 nc_ssh_destroy();
1122#elif defined(NC_ENABLED_TLS)
1123 nc_tls_destroy();
1124#endif
1125}