blob: a703a83da1d4abed56b1893ab3254fab2375fdd0 [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 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 */
22
Radek Krejci206fcd62015-10-07 15:42:48 +020023#include <errno.h>
Radek Krejci952eb862016-01-08 14:22:55 +010024#include <stdlib.h>
Michal Vasko3512e402016-01-28 16:22:34 +010025#include <string.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020026#include <pthread.h>
Michal Vasko58f31552016-01-19 12:39:15 +010027#include <time.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020028#include <libyang/libyang.h>
29
Michal Vasko5e228792016-02-03 15:30:24 +010030#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010031#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010032#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010033
Michal Vasko086311b2016-01-08 09:53:11 +010034#ifdef ENABLE_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020035
Michal Vasko086311b2016-01-08 09:53:11 +010036# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020037
Michal Vasko086311b2016-01-08 09:53:11 +010038#endif /* ENABLE_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020039
Michal Vasko5e228792016-02-03 15:30:24 +010040#if defined(ENABLE_SSH) || defined(ENABLE_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010041
Michal Vasko5e228792016-02-03 15:30:24 +010042# include <openssl/engine.h>
43# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010044# include <openssl/err.h>
45
Michal Vasko5e228792016-02-03 15:30:24 +010046#endif /* ENABLE_SSH || ENABLE_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010047
Michal Vasko086311b2016-01-08 09:53:11 +010048/* in seconds */
49#define NC_CLIENT_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020050
Michal Vasko05ba9df2016-01-13 14:40:27 +010051/* in milliseconds */
52#define NC_CLOSE_REPLY_TIMEOUT 200
53
Michal Vasko086311b2016-01-08 09:53:11 +010054extern struct nc_server_opts server_opts;
55
Michal Vasko96164bf2016-01-21 15:41:58 +010056/*
57 * @return 1 - success
58 * 0 - timeout
59 * -1 - error
60 */
61int
62nc_timedlock(pthread_mutex_t *lock, int timeout, int *elapsed)
63{
64 int ret;
65 struct timespec ts_timeout, ts_old, ts_new;
66
67 if (timeout > 0) {
68 clock_gettime(CLOCK_REALTIME, &ts_timeout);
69
70 if (elapsed) {
71 ts_old = ts_timeout;
72 }
73
74 ts_timeout.tv_sec += timeout / 1000;
75 ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
76
77 ret = pthread_mutex_timedlock(lock, &ts_timeout);
78
79 if (elapsed) {
80 clock_gettime(CLOCK_REALTIME, &ts_new);
81
82 *elapsed += (ts_new.tv_sec - ts_old.tv_sec) * 1000;
83 *elapsed += (ts_new.tv_nsec - ts_old.tv_nsec) / 1000000;
84 }
85 } else if (!timeout) {
86 ret = pthread_mutex_trylock(lock);
87 } else { /* timeout == -1 */
88 ret = pthread_mutex_lock(lock);
89 }
90
91 if (ret == ETIMEDOUT) {
92 /* timeout */
93 return 0;
94 } else if (ret) {
95 /* error */
96 ERR("Mutex lock failed (%s).", strerror(errno));
97 return -1;
98 }
99
100 /* ok */
101 return 1;
102}
103
104void
105nc_subtract_elapsed(int *timeout, struct timespec *old_ts)
106{
107 struct timespec new_ts;
108
109 clock_gettime(CLOCK_MONOTONIC_RAW, &new_ts);
110
111 *timeout -= (new_ts.tv_sec - old_ts->tv_sec) * 1000;
112 *timeout -= (new_ts.tv_nsec - old_ts->tv_nsec) / 1000000;
113
114 *old_ts = new_ts;
115}
116
Michal Vasko8dadf782016-01-15 10:29:36 +0100117API NC_STATUS
118nc_session_get_status(const struct nc_session *session)
119{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100120 if (!session) {
121 ERRARG;
122 return 0;
123 }
124
Michal Vasko8dadf782016-01-15 10:29:36 +0100125 return session->status;
126}
127
128API uint32_t
129nc_session_get_id(const struct nc_session *session)
130{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100131 if (!session) {
132 ERRARG;
133 return 0;
134 }
135
Michal Vasko8dadf782016-01-15 10:29:36 +0100136 return session->id;
137}
138
139API NC_TRANSPORT_IMPL
140nc_session_get_ti(const struct nc_session *session)
141{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100142 if (!session) {
143 ERRARG;
144 return 0;
145 }
146
Michal Vasko8dadf782016-01-15 10:29:36 +0100147 return session->ti_type;
148}
149
150API const char *
151nc_session_get_username(const struct nc_session *session)
152{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100153 if (!session) {
154 ERRARG;
155 return NULL;
156 }
157
Michal Vasko8dadf782016-01-15 10:29:36 +0100158 return session->username;
159}
160
161API const char *
162nc_session_get_host(const struct nc_session *session)
163{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100164 if (!session) {
165 ERRARG;
166 return NULL;
167 }
168
Michal Vasko8dadf782016-01-15 10:29:36 +0100169 return session->host;
170}
171
172API uint16_t
173nc_session_get_port(const struct nc_session *session)
174{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100175 if (!session) {
176 ERRARG;
177 return 0;
178 }
179
Michal Vasko8dadf782016-01-15 10:29:36 +0100180 return session->port;
181}
182
Michal Vasko9a25e932016-02-01 10:36:42 +0100183API struct ly_ctx *
184nc_session_get_ctx(const struct nc_session *session)
185{
186 if (!session) {
187 ERRARG;
188 return NULL;
189 }
190
191 return session->ctx;
192}
193
Michal Vasko8dadf782016-01-15 10:29:36 +0100194API const char **
195nc_session_get_cpblts(const struct nc_session *session)
196{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100197 if (!session) {
198 ERRARG;
199 return NULL;
200 }
201
Michal Vasko8dadf782016-01-15 10:29:36 +0100202 return session->cpblts;
203}
204
205API const char *
206nc_session_cpblt(const struct nc_session *session, const char *capab)
207{
208 int i, len;
209
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100210 if (!session || !capab) {
211 ERRARG;
212 return NULL;
213 }
214
Michal Vasko8dadf782016-01-15 10:29:36 +0100215 len = strlen(capab);
216 for (i = 0; session->cpblts[i]; ++i) {
217 if (!strncmp(session->cpblts[i], capab, len)) {
218 return session->cpblts[i];
219 }
220 }
221
222 return NULL;
223}
224
Michal Vasko086311b2016-01-08 09:53:11 +0100225NC_MSG_TYPE
226nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200227{
Michal Vasko086311b2016-01-08 09:53:11 +0100228 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200229
Michal Vasko086311b2016-01-08 09:53:11 +0100230 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100231 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
232 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100233 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100234 }
235
Michal Vasko086311b2016-01-08 09:53:11 +0100236 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
237
238 if (r) {
239 return NC_MSG_ERROR;
240 }
241
242 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100243}
244
Radek Krejci695d4fa2015-10-22 13:23:54 +0200245API void
246nc_session_free(struct nc_session *session)
247{
248 int r, i;
Michal Vasko428087d2016-01-14 16:04:28 +0100249 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100250 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100251 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100252 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100253 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100254 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200255 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100256 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200257 void *p;
258
Michal Vasko428087d2016-01-14 16:04:28 +0100259 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200260 return;
261 }
262
263 /* mark session for closing */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100264 if (session->ti_lock) {
Michal Vasko4589bbe2016-01-29 09:41:30 +0100265 r = nc_timedlock(session->ti_lock, -1, NULL);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100266 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100267 return;
268 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200269 }
270
271 /* stop notifications loop if any */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100272 if (session->ntf_tid) {
273 tid = *session->ntf_tid;
274 free((pthread_t *)session->ntf_tid);
275 session->ntf_tid = NULL;
276 /* the thread now knows it should quit */
277
278 pthread_join(tid, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200279 }
280
Michal Vasko428087d2016-01-14 16:04:28 +0100281 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200282 /* cleanup message queues */
283 /* notifications */
Michal Vaskoad611702015-12-03 13:41:51 +0100284 for (contiter = session->notifs; contiter; ) {
285 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200286
Michal Vaskoad611702015-12-03 13:41:51 +0100287 p = contiter;
288 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200289 free(p);
290 }
291
292 /* rpc replies */
Michal Vaskoad611702015-12-03 13:41:51 +0100293 for (contiter = session->replies; contiter; ) {
294 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200295
Michal Vaskoad611702015-12-03 13:41:51 +0100296 p = contiter;
297 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200298 free(p);
299 }
300
301 /* send closing info to the other side */
302 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
303 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100304 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200305 } else {
306 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100307 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200308 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100309 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100310 case NC_MSG_REPLY:
311 LY_TREE_FOR(rpl->child, child) {
312 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
313 break;
314 }
315 }
316 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100317 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100318 }
Michal Vaskoad611702015-12-03 13:41:51 +0100319 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100320 break;
321 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100322 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100323 break;
324 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100325 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100326 break;
327 default:
328 /* cannot happen */
329 break;
330 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200331 }
332
333 /* list of server's capabilities */
334 if (session->cpblts) {
335 for (i = 0; session->cpblts[i]; i++) {
336 lydict_remove(session->ctx, session->cpblts[i]);
337 }
338 free(session->cpblts);
339 }
340 }
341
342 session->status = NC_STATUS_CLOSING;
Michal Vasko428087d2016-01-14 16:04:28 +0100343 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200344
345 /* transport implementation cleanup */
346 switch (session->ti_type) {
347 case NC_TI_FD:
348 /* nothing needed - file descriptors were provided by caller,
349 * so it is up to the caller to close them correctly
350 * TODO use callbacks
351 */
Michal Vasko3512e402016-01-28 16:22:34 +0100352 /* just to avoid compiler warning */
353 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100354 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200355 break;
356
Michal Vaskofb2fb762015-10-27 11:44:32 +0100357#ifdef ENABLE_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200358 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100359 if (connected) {
360 ssh_channel_free(session->ti.libssh.channel);
361 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200362 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
363 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
364 * it.
365 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100366 multisession = 0;
367 if (session->ti.libssh.next) {
368 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
369 if (siter->status != NC_STATUS_STARTING) {
370 multisession = 1;
371 break;
372 }
373 }
374 }
375
376 if (!multisession) {
377 /* it's not multisession yet, but we still need to free the starting sessions */
378 if (session->ti.libssh.next) {
379 do {
380 siter = session->ti.libssh.next;
381 session->ti.libssh.next = siter->ti.libssh.next;
382
383 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100384 lydict_remove(session->ctx, session->username);
385 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100386 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100387 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100388 }
389
390 free(siter);
391 } while (session->ti.libssh.next != session);
392 }
Michal Vasko428087d2016-01-14 16:04:28 +0100393 if (connected) {
394 ssh_disconnect(session->ti.libssh.session);
395 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200396 ssh_free(session->ti.libssh.session);
397 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200398 /* remove the session from the list */
399 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100400 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200401 /* there will be only one session */
402 siter->ti.libssh.next = NULL;
403 } else {
404 /* there are still multiple sessions, keep the ring list */
405 siter->ti.libssh.next = session->ti.libssh.next;
406 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100407 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
408 if (session->flags & NC_SESSION_SSH_MSG_CB) {
409 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
410 if (siter->ti.libssh.next == session) {
411 ERRINT;
412 break;
413 }
414 }
415 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
416 siter->flags |= NC_SESSION_SSH_MSG_CB;
417 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200418 }
419 break;
420#endif
421
422#ifdef ENABLE_TLS
423 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100424 if (connected) {
425 SSL_shutdown(session->ti.tls);
426 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200427 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100428
429 X509_free(session->tls_cert);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200430 break;
431#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100432 case NC_TI_NONE:
433 ERRINT;
434 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200435 }
Michal Vasko428087d2016-01-14 16:04:28 +0100436
Radek Krejciac6d3472015-10-22 15:47:18 +0200437 lydict_remove(session->ctx, session->username);
438 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200439
440 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100441 if (session->ti_lock) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100442 pthread_mutex_unlock(session->ti_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +0100443 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100444 pthread_mutex_destroy(session->ti_lock);
445 free(session->ti_lock);
446 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200447 }
448
449 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100450 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200451 }
452
453 free(session);
454}
455
Michal Vasko086311b2016-01-08 09:53:11 +0100456static void
457add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
458{
459 if (*count == *size) {
460 *size += 5;
461 *cpblts = realloc(*cpblts, *size * sizeof **cpblts);
462 }
463
464 if (capab) {
465 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
466 } else {
467 (*cpblts)[*count] = NULL;
468 }
469 ++(*count);
470}
471
472static const char **
473create_cpblts(struct ly_ctx *ctx)
474{
475 struct lyd_node *child, *child2, *yanglib;
476 struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL;
477 const char **cpblts;
478 const struct lys_module *mod;
Michal Vasko11d142a2016-01-19 15:58:24 +0100479 int size = 10, count, feat_count = 0, i, str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100480 char str[512];
481
482 yanglib = ly_ctx_info(ctx);
483 if (!yanglib) {
484 return NULL;
485 }
486
487 cpblts = malloc(size * sizeof *cpblts);
488 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
489 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
490 count = 2;
491
492 /* capabilities */
493
494 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
495 if (mod) {
496 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100497 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100498 }
499 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100500 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100501 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100502 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100503 }
504 }
505 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100506 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100507 }
508 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100509 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100510 }
511 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100512 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100513 }
514 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100515 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100516 }
517 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100518 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100519 }
520 }
521
522 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
523 if (mod) {
524 if (!server_opts.wd_basic_mode) {
525 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
526 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100527 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100528 switch (server_opts.wd_basic_mode) {
529 case NC_WD_ALL:
530 strcat(str, "?basic-mode=report-all");
531 break;
532 case NC_WD_TRIM:
533 strcat(str, "?basic-mode=trim");
534 break;
535 case NC_WD_EXPLICIT:
536 strcat(str, "?basic-mode=explicit");
537 break;
538 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100539 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100540 break;
541 }
542
543 if (server_opts.wd_also_supported) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100544 strcat(str, "&amp;also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100545 if (server_opts.wd_also_supported & NC_WD_ALL) {
546 strcat(str, "report-all,");
547 }
548 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
549 strcat(str, "report-all-tagged,");
550 }
551 if (server_opts.wd_also_supported & NC_WD_TRIM) {
552 strcat(str, "trim,");
553 }
554 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
555 strcat(str, "explicit,");
556 }
557 str[strlen(str) - 1] = '\0';
558
559 add_cpblt(ctx, str, &cpblts, &size, &count);
560 }
561 }
562 }
563
Michal Vasko1a38c862016-01-15 15:50:07 +0100564 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100565 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100566 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100567 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100568 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100569 }
570 }
571
572 /* models */
573
574 LY_TREE_FOR(yanglib->child, child) {
575 if (!strcmp(child->schema->name, "module")) {
576 LY_TREE_FOR(child->child, child2) {
577 if (!strcmp(child2->schema->name, "namespace")) {
578 ns = (struct lyd_node_leaf_list *)child2;
579 } else if (!strcmp(child2->schema->name, "name")) {
580 name = (struct lyd_node_leaf_list *)child2;
581 } else if (!strcmp(child2->schema->name, "revision")) {
582 rev = (struct lyd_node_leaf_list *)child2;
583 } else if (!strcmp(child2->schema->name, "feature")) {
584 features = realloc(features, feat_count++ * sizeof *features);
585 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
586 }
587 }
588
589 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100590 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100591 continue;
592 }
593
Michal Vasko11d142a2016-01-19 15:58:24 +0100594 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 +0100595 if (feat_count) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100596 strcat(str, "&amp;features=");
Michal Vasko11d142a2016-01-19 15:58:24 +0100597 str_len += 14;
Michal Vasko086311b2016-01-08 09:53:11 +0100598 for (i = 0; i < feat_count; ++i) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100599 if (str_len + 1 + strlen(features[i]->value_str) >= 512) {
600 ERRINT;
601 break;
602 }
Michal Vasko086311b2016-01-08 09:53:11 +0100603 if (i) {
604 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100605 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100606 }
607 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100608 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100609 }
610 }
611
612 add_cpblt(ctx, str, &cpblts, &size, &count);
613
614 ns = NULL;
615 name = NULL;
616 rev = NULL;
617 free(features);
618 features = NULL;
619 feat_count = 0;
620 }
621 }
622
623 lyd_free(yanglib);
624
625 /* ending NULL capability */
626 add_cpblt(ctx, NULL, &cpblts, &size, &count);
627
628 return cpblts;
629}
630
Radek Krejci695d4fa2015-10-22 13:23:54 +0200631static int
632parse_cpblts(struct lyxml_elem *xml, const char ***list)
633{
634 struct lyxml_elem *cpblt;
635 int ver = -1;
636 int i = 0;
637
638 if (list) {
639 /* get the storage for server's capabilities */
640 LY_TREE_FOR(xml->child, cpblt) {
641 i++;
642 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100643 /* last item remains NULL */
644 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200645 if (!*list) {
646 ERRMEM;
647 return -1;
648 }
649 i = 0;
650 }
651
652 LY_TREE_FOR(xml->child, cpblt) {
653 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
654 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
655 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
656 return -1;
657 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
658 continue;
659 }
660
661 /* detect NETCONF version */
662 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
663 ver = 0;
664 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
665 ver = 1;
666 }
667
668 /* store capabilities */
669 if (list) {
670 (*list)[i] = cpblt->content;
671 cpblt->content = NULL;
672 i++;
673 }
674 }
675
676 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100677 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200678 }
679
680 return ver;
681}
682
683static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100684nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100685{
686 int r, i;
687 const char **cpblts;
688
Michal Vasko11d142a2016-01-19 15:58:24 +0100689 /* client side hello - send only NETCONF base capabilities */
690 cpblts = malloc(3 * sizeof *cpblts);
691 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
692 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
693 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100694
Michal Vasko11d142a2016-01-19 15:58:24 +0100695 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100696
Michal Vasko086311b2016-01-08 09:53:11 +0100697 for (i = 0; cpblts[i]; ++i) {
698 lydict_remove(session->ctx, cpblts[i]);
699 }
700 free(cpblts);
701
702 if (r) {
703 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100704 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100705
706 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100707}
708
709static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100710nc_send_server_hello(struct nc_session *session)
711{
712 int r, i;
713 const char **cpblts;
714
715 cpblts = create_cpblts(session->ctx);
716
717 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
718
Michal Vasko11d142a2016-01-19 15:58:24 +0100719 for (i = 0; cpblts[i]; ++i) {
720 lydict_remove(session->ctx, cpblts[i]);
721 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100722 free(cpblts);
723
724 if (r) {
725 return NC_MSG_ERROR;
726 }
727
728 return NC_MSG_HELLO;
729}
730
731static NC_MSG_TYPE
732nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200733{
734 struct lyxml_elem *xml = NULL, *node;
735 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
736 int ver = -1;
737 char *str;
738 long long int id;
739 int flag = 0;
740
Michal Vasko05ba9df2016-01-13 14:40:27 +0100741 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200742
743 switch(msgtype) {
744 case NC_MSG_HELLO:
745 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100746 LY_TREE_FOR(xml->child, node) {
747 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
748 continue;
749 } else if (!strcmp(node->name, "session-id")) {
750 if (!node->content || !strlen(node->content)) {
751 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200752 goto error;
753 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100754 str = NULL;
755 id = strtoll(node->content, &str, 10);
756 if (*str || id < 1 || id > UINT32_MAX) {
757 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200758 goto error;
759 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100760 session->id = (uint32_t)id;
761 continue;
762 } else if (strcmp(node->name, "capabilities")) {
763 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200764 goto error;
765 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100766
767 if (flag) {
768 /* multiple capabilities elements */
769 ERR("Invalid <hello> message (multiple <capabilities> elements).");
770 goto error;
771 }
772 flag = 1;
773
774 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
775 goto error;
776 }
777 session->version = ver;
778 }
779
780 if (!session->id) {
781 ERR("Missing <session-id> in server's <hello>.");
782 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200783 }
784 break;
785 case NC_MSG_ERROR:
786 /* nothing special, just pass it out */
787 break;
788 default:
789 ERR("Unexpected message received instead of <hello>.");
790 msgtype = NC_MSG_ERROR;
791 }
792
793 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100794 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200795
796 return msgtype;
797
798error:
799 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100800 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200801
802 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200803}
804
Michal Vasko11d142a2016-01-19 15:58:24 +0100805static NC_MSG_TYPE
806nc_recv_server_hello(struct nc_session *session)
807{
808 struct lyxml_elem *xml = NULL, *node;
809 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
810 int ver = -1;
811 int flag = 0;
812
Michal Vaskoadb850e2016-01-20 14:06:32 +0100813 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100814
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100815 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100816 case NC_MSG_HELLO:
817 /* get know NETCONF version */
818 LY_TREE_FOR(xml->child, node) {
819 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
820 continue;
821 } else if (strcmp(node->name, "capabilities")) {
822 ERR("Unexpected <%s> element in client's <hello>.", node->name);
823 msgtype = NC_MSG_ERROR;
824 goto cleanup;
825 }
826
827 if (flag) {
828 /* multiple capabilities elements */
829 ERR("Invalid <hello> message (multiple <capabilities> elements).");
830 msgtype = NC_MSG_ERROR;
831 goto cleanup;
832 }
833 flag = 1;
834
835 if ((ver = parse_cpblts(node, NULL)) < 0) {
836 msgtype = NC_MSG_ERROR;
837 goto cleanup;
838 }
839 session->version = ver;
840 }
841 break;
842 case NC_MSG_ERROR:
843 /* nothing special, just pass it out */
844 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100845 case NC_MSG_WOULDBLOCK:
846 ERR("Client's <hello> timeout elapsed.");
847 msgtype = NC_MSG_ERROR;
848 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100849 default:
850 ERR("Unexpected message received instead of <hello>.");
851 msgtype = NC_MSG_ERROR;
852 }
853
854cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +0100855 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100856
857 return msgtype;
858}
859
Michal Vasko80cad7f2015-12-08 14:42:27 +0100860int
Michal Vasko086311b2016-01-08 09:53:11 +0100861nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100862{
Michal Vasko086311b2016-01-08 09:53:11 +0100863 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100864
Michal Vasko11d142a2016-01-19 15:58:24 +0100865 if (session->side == NC_CLIENT) {
866 type = nc_send_client_hello(session);
867 } else {
868 type = nc_send_server_hello(session);
869 }
870
Michal Vasko086311b2016-01-08 09:53:11 +0100871 if (type != NC_MSG_HELLO) {
872 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100873 }
874
Michal Vasko11d142a2016-01-19 15:58:24 +0100875 if (session->side == NC_CLIENT) {
876 type = nc_recv_client_hello(session);
877 } else {
878 type = nc_recv_server_hello(session);
879 }
880
Michal Vasko086311b2016-01-08 09:53:11 +0100881 if (type != NC_MSG_HELLO) {
882 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100883 }
884
Michal Vasko086311b2016-01-08 09:53:11 +0100885 return 0;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100886}
Michal Vasko086311b2016-01-08 09:53:11 +0100887
888#ifdef ENABLE_SSH
889
890API void
891nc_ssh_init(void)
892{
893 ssh_threads_set_callbacks(ssh_threads_get_pthread());
894 ssh_init();
895 ssh_set_log_level(verbose_level);
896}
897
898API void
899nc_ssh_destroy(void)
900{
Michal Vasko5e228792016-02-03 15:30:24 +0100901 ENGINE_cleanup();
902 CONF_modules_unload(1);
903 ERR_remove_state(0);
Michal Vasko086311b2016-01-08 09:53:11 +0100904 ssh_finalize();
905}
906
907#endif /* ENABLE_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100908
909#ifdef ENABLE_TLS
910
911static pthread_mutex_t *tls_locks;
912
Michal Vaskof0c92c02016-01-29 09:41:45 +0100913struct CRYPTO_dynlock_value {
914 pthread_mutex_t lock;
915};
916
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100917static void
Michal Vaskob48aa812016-01-18 14:13:09 +0100918tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100919{
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100920 if (mode & CRYPTO_LOCK) {
921 pthread_mutex_lock(tls_locks + n);
922 } else {
923 pthread_mutex_unlock(tls_locks + n);
924 }
925}
926
Michal Vaskof0c92c02016-01-29 09:41:45 +0100927static void
928tls_thread_id_func(CRYPTO_THREADID *tid)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100929{
Michal Vaskof0c92c02016-01-29 09:41:45 +0100930 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
931}
932
933static struct CRYPTO_dynlock_value *
934tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
935{
936 struct CRYPTO_dynlock_value *value;
937
938 value = malloc(sizeof *value);
939 if (!value) {
940 ERRMEM;
941 return NULL;
942 }
943 pthread_mutex_init(&value->lock, NULL);
944
945 return value;
946}
947
948static void
949tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
950{
951 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
952 * I found ignored this fact, what do I know... */
953 if (mode & CRYPTO_LOCK) {
954 pthread_mutex_lock(&l->lock);
955 } else {
956 pthread_mutex_unlock(&l->lock);
957 }
958}
959
960static void
961tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
962{
963 pthread_mutex_destroy(&l->lock);
964 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100965}
966
967API void
968nc_tls_init(void)
969{
970 int i;
971
972 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100973 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100974 SSL_library_init();
975
976 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
977 for (i = 0; i < CRYPTO_num_locks(); ++i) {
978 pthread_mutex_init(tls_locks + i, NULL);
979 }
980
Michal Vaskof0c92c02016-01-29 09:41:45 +0100981 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100982 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +0100983
984 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
985 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
986 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100987}
988
989API void
990nc_tls_destroy(void)
991{
992 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100993
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100994 CRYPTO_cleanup_all_ex_data();
Michal Vasko5e228792016-02-03 15:30:24 +0100995 ERR_remove_state(0);
996 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100997 ERR_free_strings();
998 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100999
1000 CRYPTO_set_id_callback(NULL);
1001 CRYPTO_set_locking_callback(NULL);
1002 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1003 pthread_mutex_destroy(tls_locks + i);
1004 }
1005 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001006
1007 CRYPTO_set_dynlock_create_callback(NULL);
1008 CRYPTO_set_dynlock_lock_callback(NULL);
1009 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001010}
1011
1012#endif /* ENABLE_TLS */
Michal Vasko5e228792016-02-03 15:30:24 +01001013
1014#if defined(ENABLE_SSH) || defined(ENABLE_TLS)
1015
1016API void
1017nc_thread_destroy(void) {
1018 CRYPTO_THREADID crypto_tid;
1019
Michal Vasko0be5dff2016-02-08 15:26:37 +01001020 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1021 //CRYPTO_cleanup_all_ex_data();
Michal Vasko5e228792016-02-03 15:30:24 +01001022
1023 CRYPTO_THREADID_current(&crypto_tid);
1024 ERR_remove_thread_state(&crypto_tid);
1025}
1026
1027#endif /* ENABLE_SSH || ENABLE_TLS */
1028
1029#if defined(ENABLE_SSH) && defined(ENABLE_TLS)
1030
1031API void
1032nc_ssh_tls_init(void)
1033{
1034 SSL_load_error_strings();
1035 ERR_load_BIO_strings();
1036 SSL_library_init();
1037
1038 nc_ssh_init();
1039
1040 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1041 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1042 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1043}
1044
1045API void
1046nc_ssh_tls_destroy(void)
1047{
1048 ERR_free_strings();
1049 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1050
1051 nc_ssh_destroy();
1052
1053 CRYPTO_set_dynlock_create_callback(NULL);
1054 CRYPTO_set_dynlock_lock_callback(NULL);
1055 CRYPTO_set_dynlock_destroy_callback(NULL);
1056}
1057
1058#endif /* ENABLE_SSH && ENABLE_TLS */