blob: e2194d9fcf79b0893362a004104f4fe8fe4e449c [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
258nc_session_free(struct nc_session *session)
259{
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
354 session->status = NC_STATUS_CLOSING;
Michal Vasko428087d2016-01-14 16:04:28 +0100355 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200356
357 /* transport implementation cleanup */
358 switch (session->ti_type) {
359 case NC_TI_FD:
360 /* nothing needed - file descriptors were provided by caller,
361 * so it is up to the caller to close them correctly
362 * TODO use callbacks
363 */
Michal Vasko3512e402016-01-28 16:22:34 +0100364 /* just to avoid compiler warning */
365 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100366 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200367 break;
368
Radek Krejci53691be2016-02-22 13:58:37 +0100369#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200370 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100371 if (connected) {
372 ssh_channel_free(session->ti.libssh.channel);
373 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200374 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
375 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
376 * it.
377 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100378 multisession = 0;
379 if (session->ti.libssh.next) {
380 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
381 if (siter->status != NC_STATUS_STARTING) {
382 multisession = 1;
383 break;
384 }
385 }
386 }
387
388 if (!multisession) {
389 /* it's not multisession yet, but we still need to free the starting sessions */
390 if (session->ti.libssh.next) {
391 do {
392 siter = session->ti.libssh.next;
393 session->ti.libssh.next = siter->ti.libssh.next;
394
395 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100396 lydict_remove(session->ctx, session->username);
397 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100398 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100399 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100400 }
401
402 free(siter);
403 } while (session->ti.libssh.next != session);
404 }
Michal Vasko428087d2016-01-14 16:04:28 +0100405 if (connected) {
406 ssh_disconnect(session->ti.libssh.session);
407 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200408 ssh_free(session->ti.libssh.session);
409 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200410 /* remove the session from the list */
411 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100412 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200413 /* there will be only one session */
414 siter->ti.libssh.next = NULL;
415 } else {
416 /* there are still multiple sessions, keep the ring list */
417 siter->ti.libssh.next = session->ti.libssh.next;
418 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100419 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
420 if (session->flags & NC_SESSION_SSH_MSG_CB) {
421 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
422 if (siter->ti.libssh.next == session) {
423 ERRINT;
424 break;
425 }
426 }
427 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
428 siter->flags |= NC_SESSION_SSH_MSG_CB;
429 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200430 }
431 break;
432#endif
433
Radek Krejci53691be2016-02-22 13:58:37 +0100434#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200435 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100436 if (connected) {
437 SSL_shutdown(session->ti.tls);
438 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200439 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100440
441 X509_free(session->tls_cert);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200442 break;
443#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100444 case NC_TI_NONE:
445 ERRINT;
446 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200447 }
Michal Vasko428087d2016-01-14 16:04:28 +0100448
Radek Krejciac6d3472015-10-22 15:47:18 +0200449 lydict_remove(session->ctx, session->username);
450 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200451
452 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100453 if (session->ti_lock) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100454 pthread_mutex_unlock(session->ti_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +0100455 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100456 pthread_mutex_destroy(session->ti_lock);
457 free(session->ti_lock);
458 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200459 }
460
461 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100462 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200463 }
464
465 free(session);
466}
467
Michal Vasko086311b2016-01-08 09:53:11 +0100468static void
469add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
470{
471 if (*count == *size) {
472 *size += 5;
473 *cpblts = realloc(*cpblts, *size * sizeof **cpblts);
474 }
475
476 if (capab) {
477 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
478 } else {
479 (*cpblts)[*count] = NULL;
480 }
481 ++(*count);
482}
483
484static const char **
485create_cpblts(struct ly_ctx *ctx)
486{
487 struct lyd_node *child, *child2, *yanglib;
488 struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL;
489 const char **cpblts;
490 const struct lys_module *mod;
Michal Vasko11d142a2016-01-19 15:58:24 +0100491 int size = 10, count, feat_count = 0, i, str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100492 char str[512];
493
494 yanglib = ly_ctx_info(ctx);
495 if (!yanglib) {
496 return NULL;
497 }
498
499 cpblts = malloc(size * sizeof *cpblts);
500 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
501 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
502 count = 2;
503
504 /* capabilities */
505
506 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
507 if (mod) {
508 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100509 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100510 }
511 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100512 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100513 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100514 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100515 }
516 }
517 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100518 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100519 }
520 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100521 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100522 }
523 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100524 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100525 }
526 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100527 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100528 }
529 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100530 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100531 }
532 }
533
534 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
535 if (mod) {
536 if (!server_opts.wd_basic_mode) {
537 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
538 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100539 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100540 switch (server_opts.wd_basic_mode) {
541 case NC_WD_ALL:
542 strcat(str, "?basic-mode=report-all");
543 break;
544 case NC_WD_TRIM:
545 strcat(str, "?basic-mode=trim");
546 break;
547 case NC_WD_EXPLICIT:
548 strcat(str, "?basic-mode=explicit");
549 break;
550 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100551 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100552 break;
553 }
554
555 if (server_opts.wd_also_supported) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100556 strcat(str, "&amp;also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100557 if (server_opts.wd_also_supported & NC_WD_ALL) {
558 strcat(str, "report-all,");
559 }
560 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
561 strcat(str, "report-all-tagged,");
562 }
563 if (server_opts.wd_also_supported & NC_WD_TRIM) {
564 strcat(str, "trim,");
565 }
566 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
567 strcat(str, "explicit,");
568 }
569 str[strlen(str) - 1] = '\0';
570
571 add_cpblt(ctx, str, &cpblts, &size, &count);
572 }
573 }
574 }
575
Michal Vasko1a38c862016-01-15 15:50:07 +0100576 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100577 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100578 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100579 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100580 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100581 }
582 }
583
584 /* models */
585
586 LY_TREE_FOR(yanglib->child, child) {
587 if (!strcmp(child->schema->name, "module")) {
588 LY_TREE_FOR(child->child, child2) {
589 if (!strcmp(child2->schema->name, "namespace")) {
590 ns = (struct lyd_node_leaf_list *)child2;
591 } else if (!strcmp(child2->schema->name, "name")) {
592 name = (struct lyd_node_leaf_list *)child2;
593 } else if (!strcmp(child2->schema->name, "revision")) {
594 rev = (struct lyd_node_leaf_list *)child2;
595 } else if (!strcmp(child2->schema->name, "feature")) {
596 features = realloc(features, feat_count++ * sizeof *features);
597 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
598 }
599 }
600
601 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100602 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100603 continue;
604 }
605
Michal Vasko11d142a2016-01-19 15:58:24 +0100606 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 +0100607 if (feat_count) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100608 strcat(str, "&amp;features=");
Michal Vasko11d142a2016-01-19 15:58:24 +0100609 str_len += 14;
Michal Vasko086311b2016-01-08 09:53:11 +0100610 for (i = 0; i < feat_count; ++i) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100611 if (str_len + 1 + strlen(features[i]->value_str) >= 512) {
612 ERRINT;
613 break;
614 }
Michal Vasko086311b2016-01-08 09:53:11 +0100615 if (i) {
616 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100617 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100618 }
619 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100620 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100621 }
622 }
623
624 add_cpblt(ctx, str, &cpblts, &size, &count);
625
626 ns = NULL;
627 name = NULL;
628 rev = NULL;
629 free(features);
630 features = NULL;
631 feat_count = 0;
632 }
633 }
634
635 lyd_free(yanglib);
636
637 /* ending NULL capability */
638 add_cpblt(ctx, NULL, &cpblts, &size, &count);
639
640 return cpblts;
641}
642
Radek Krejci695d4fa2015-10-22 13:23:54 +0200643static int
644parse_cpblts(struct lyxml_elem *xml, const char ***list)
645{
646 struct lyxml_elem *cpblt;
647 int ver = -1;
648 int i = 0;
649
650 if (list) {
651 /* get the storage for server's capabilities */
652 LY_TREE_FOR(xml->child, cpblt) {
653 i++;
654 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100655 /* last item remains NULL */
656 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200657 if (!*list) {
658 ERRMEM;
659 return -1;
660 }
661 i = 0;
662 }
663
664 LY_TREE_FOR(xml->child, cpblt) {
665 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
666 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
667 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
668 return -1;
669 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
670 continue;
671 }
672
673 /* detect NETCONF version */
674 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
675 ver = 0;
676 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
677 ver = 1;
678 }
679
680 /* store capabilities */
681 if (list) {
682 (*list)[i] = cpblt->content;
683 cpblt->content = NULL;
684 i++;
685 }
686 }
687
688 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100689 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200690 }
691
692 return ver;
693}
694
695static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100696nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100697{
698 int r, i;
699 const char **cpblts;
700
Michal Vasko11d142a2016-01-19 15:58:24 +0100701 /* client side hello - send only NETCONF base capabilities */
702 cpblts = malloc(3 * sizeof *cpblts);
703 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
704 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
705 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100706
Michal Vasko11d142a2016-01-19 15:58:24 +0100707 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100708
Michal Vasko086311b2016-01-08 09:53:11 +0100709 for (i = 0; cpblts[i]; ++i) {
710 lydict_remove(session->ctx, cpblts[i]);
711 }
712 free(cpblts);
713
714 if (r) {
715 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100716 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100717
718 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100719}
720
721static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100722nc_send_server_hello(struct nc_session *session)
723{
724 int r, i;
725 const char **cpblts;
726
727 cpblts = create_cpblts(session->ctx);
728
729 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
730
Michal Vasko11d142a2016-01-19 15:58:24 +0100731 for (i = 0; cpblts[i]; ++i) {
732 lydict_remove(session->ctx, cpblts[i]);
733 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100734 free(cpblts);
735
736 if (r) {
737 return NC_MSG_ERROR;
738 }
739
740 return NC_MSG_HELLO;
741}
742
743static NC_MSG_TYPE
744nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200745{
746 struct lyxml_elem *xml = NULL, *node;
747 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
748 int ver = -1;
749 char *str;
750 long long int id;
751 int flag = 0;
752
Michal Vasko05ba9df2016-01-13 14:40:27 +0100753 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200754
755 switch(msgtype) {
756 case NC_MSG_HELLO:
757 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100758 LY_TREE_FOR(xml->child, node) {
759 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
760 continue;
761 } else if (!strcmp(node->name, "session-id")) {
762 if (!node->content || !strlen(node->content)) {
763 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200764 goto error;
765 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100766 str = NULL;
767 id = strtoll(node->content, &str, 10);
768 if (*str || id < 1 || id > UINT32_MAX) {
769 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200770 goto error;
771 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100772 session->id = (uint32_t)id;
773 continue;
774 } else if (strcmp(node->name, "capabilities")) {
775 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200776 goto error;
777 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100778
779 if (flag) {
780 /* multiple capabilities elements */
781 ERR("Invalid <hello> message (multiple <capabilities> elements).");
782 goto error;
783 }
784 flag = 1;
785
786 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
787 goto error;
788 }
789 session->version = ver;
790 }
791
792 if (!session->id) {
793 ERR("Missing <session-id> in server's <hello>.");
794 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200795 }
796 break;
797 case NC_MSG_ERROR:
798 /* nothing special, just pass it out */
799 break;
800 default:
801 ERR("Unexpected message received instead of <hello>.");
802 msgtype = NC_MSG_ERROR;
803 }
804
805 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100806 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200807
808 return msgtype;
809
810error:
811 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100812 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200813
814 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200815}
816
Michal Vasko11d142a2016-01-19 15:58:24 +0100817static NC_MSG_TYPE
818nc_recv_server_hello(struct nc_session *session)
819{
820 struct lyxml_elem *xml = NULL, *node;
821 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
822 int ver = -1;
823 int flag = 0;
824
Michal Vaskoadb850e2016-01-20 14:06:32 +0100825 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100826
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100827 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100828 case NC_MSG_HELLO:
829 /* get know NETCONF version */
830 LY_TREE_FOR(xml->child, node) {
831 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
832 continue;
833 } else if (strcmp(node->name, "capabilities")) {
834 ERR("Unexpected <%s> element in client's <hello>.", node->name);
835 msgtype = NC_MSG_ERROR;
836 goto cleanup;
837 }
838
839 if (flag) {
840 /* multiple capabilities elements */
841 ERR("Invalid <hello> message (multiple <capabilities> elements).");
842 msgtype = NC_MSG_ERROR;
843 goto cleanup;
844 }
845 flag = 1;
846
847 if ((ver = parse_cpblts(node, NULL)) < 0) {
848 msgtype = NC_MSG_ERROR;
849 goto cleanup;
850 }
851 session->version = ver;
852 }
853 break;
854 case NC_MSG_ERROR:
855 /* nothing special, just pass it out */
856 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100857 case NC_MSG_WOULDBLOCK:
858 ERR("Client's <hello> timeout elapsed.");
859 msgtype = NC_MSG_ERROR;
860 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100861 default:
862 ERR("Unexpected message received instead of <hello>.");
863 msgtype = NC_MSG_ERROR;
864 }
865
866cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +0100867 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100868
869 return msgtype;
870}
871
Michal Vasko80cad7f2015-12-08 14:42:27 +0100872int
Michal Vasko086311b2016-01-08 09:53:11 +0100873nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100874{
Michal Vasko086311b2016-01-08 09:53:11 +0100875 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100876
Michal Vasko11d142a2016-01-19 15:58:24 +0100877 if (session->side == NC_CLIENT) {
878 type = nc_send_client_hello(session);
879 } else {
880 type = nc_send_server_hello(session);
881 }
882
Michal Vasko086311b2016-01-08 09:53:11 +0100883 if (type != NC_MSG_HELLO) {
884 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100885 }
886
Michal Vasko11d142a2016-01-19 15:58:24 +0100887 if (session->side == NC_CLIENT) {
888 type = nc_recv_client_hello(session);
889 } else {
890 type = nc_recv_server_hello(session);
891 }
892
Michal Vasko086311b2016-01-08 09:53:11 +0100893 if (type != NC_MSG_HELLO) {
894 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100895 }
896
Michal Vasko086311b2016-01-08 09:53:11 +0100897 return 0;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100898}
Michal Vasko086311b2016-01-08 09:53:11 +0100899
Radek Krejci53691be2016-02-22 13:58:37 +0100900#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100901
Michal Vasko8f0c0282016-02-29 10:17:14 +0100902static void
Michal Vasko086311b2016-01-08 09:53:11 +0100903nc_ssh_init(void)
904{
905 ssh_threads_set_callbacks(ssh_threads_get_pthread());
906 ssh_init();
907 ssh_set_log_level(verbose_level);
908}
909
Michal Vasko8f0c0282016-02-29 10:17:14 +0100910static void
Michal Vasko086311b2016-01-08 09:53:11 +0100911nc_ssh_destroy(void)
912{
Michal Vasko8f0c0282016-02-29 10:17:14 +0100913 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +0100914 ENGINE_cleanup();
915 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +0100916 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +0100917 ssh_finalize();
918}
919
Radek Krejci53691be2016-02-22 13:58:37 +0100920#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100921
Radek Krejci53691be2016-02-22 13:58:37 +0100922#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100923
Michal Vaskof0c92c02016-01-29 09:41:45 +0100924struct CRYPTO_dynlock_value {
925 pthread_mutex_t lock;
926};
927
Michal Vaskof0c92c02016-01-29 09:41:45 +0100928static struct CRYPTO_dynlock_value *
929tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
930{
931 struct CRYPTO_dynlock_value *value;
932
933 value = malloc(sizeof *value);
934 if (!value) {
935 ERRMEM;
936 return NULL;
937 }
938 pthread_mutex_init(&value->lock, NULL);
939
940 return value;
941}
942
943static void
944tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
945{
946 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
947 * I found ignored this fact, what do I know... */
948 if (mode & CRYPTO_LOCK) {
949 pthread_mutex_lock(&l->lock);
950 } else {
951 pthread_mutex_unlock(&l->lock);
952 }
953}
954
955static void
956tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
957{
958 pthread_mutex_destroy(&l->lock);
959 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100960}
961
Michal Vasko8f0c0282016-02-29 10:17:14 +0100962#endif /* NC_ENABLED_TLS */
963
964#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
965
966static pthread_mutex_t *tls_locks;
967
968static void
969tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
970{
971 if (mode & CRYPTO_LOCK) {
972 pthread_mutex_lock(tls_locks + n);
973 } else {
974 pthread_mutex_unlock(tls_locks + n);
975 }
976}
977
978static void
979tls_thread_id_func(CRYPTO_THREADID *tid)
980{
981 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
982}
983
984static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100985nc_tls_init(void)
986{
987 int i;
988
989 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100990 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100991 SSL_library_init();
992
993 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
994 for (i = 0; i < CRYPTO_num_locks(); ++i) {
995 pthread_mutex_init(tls_locks + i, NULL);
996 }
997
Michal Vaskof0c92c02016-01-29 09:41:45 +0100998 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100999 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001000
1001 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1002 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1003 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001004}
1005
Michal Vasko8f0c0282016-02-29 10:17:14 +01001006static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001007nc_tls_destroy(void)
1008{
1009 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001010
Michal Vasko8f0c0282016-02-29 10:17:14 +01001011 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001012 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001013 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001014 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001015 ERR_free_strings();
1016 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001017
Michal Vaskob6e37262016-02-25 14:49:00 +01001018 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001019 CRYPTO_set_locking_callback(NULL);
1020 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1021 pthread_mutex_destroy(tls_locks + i);
1022 }
1023 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001024
1025 CRYPTO_set_dynlock_create_callback(NULL);
1026 CRYPTO_set_dynlock_lock_callback(NULL);
1027 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001028}
1029
Michal Vasko8f0c0282016-02-29 10:17:14 +01001030#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001031
Radek Krejci53691be2016-02-22 13:58:37 +01001032#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001033
Michal Vasko8f0c0282016-02-29 10:17:14 +01001034static void
Michal Vasko5e228792016-02-03 15:30:24 +01001035nc_ssh_tls_init(void)
1036{
1037 SSL_load_error_strings();
1038 ERR_load_BIO_strings();
1039 SSL_library_init();
1040
1041 nc_ssh_init();
1042
1043 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1044 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1045 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1046}
1047
Michal Vasko8f0c0282016-02-29 10:17:14 +01001048static void
Michal Vasko5e228792016-02-03 15:30:24 +01001049nc_ssh_tls_destroy(void)
1050{
1051 ERR_free_strings();
1052 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1053
1054 nc_ssh_destroy();
1055
1056 CRYPTO_set_dynlock_create_callback(NULL);
1057 CRYPTO_set_dynlock_lock_callback(NULL);
1058 CRYPTO_set_dynlock_destroy_callback(NULL);
1059}
1060
Radek Krejci53691be2016-02-22 13:58:37 +01001061#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001062
1063#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1064
1065API void
1066nc_thread_destroy(void)
1067{
1068 CRYPTO_THREADID crypto_tid;
1069
1070 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1071 //CRYPTO_cleanup_all_ex_data();
1072
1073 CRYPTO_THREADID_current(&crypto_tid);
1074 ERR_remove_thread_state(&crypto_tid);
1075}
1076
1077API void
1078nc_init(void)
1079{
1080#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1081 nc_ssh_tls_init();
1082#elif defined(NC_ENABLED_SSH)
1083 nc_ssh_init();
1084#elif defined(NC_ENABLED_TLS)
1085 nc_tls_init();
1086#endif
1087}
1088
1089API void
1090nc_destroy(void)
1091{
1092#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1093 nc_ssh_tls_destroy();
1094#elif defined(NC_ENABLED_SSH)
1095 nc_ssh_destroy();
1096#elif defined(NC_ENABLED_TLS)
1097 nc_tls_destroy();
1098#endif
1099}
1100
1101#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */