blob: 20c2625343db48ddf444c85eb69f9b6c16813145 [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()) */
384 if (session->side == NC_SERVER) {
385 nc_ctx_lock(-1, NULL);
386 }
387 lydict_remove(session->ctx, session->username);
388 lydict_remove(session->ctx, session->host);
389 if (session->side == NC_SERVER) {
390 nc_ctx_unlock();
391 }
392 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100393 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100394 }
395
396 free(siter);
397 } while (session->ti.libssh.next != session);
398 }
Michal Vasko428087d2016-01-14 16:04:28 +0100399 if (connected) {
400 ssh_disconnect(session->ti.libssh.session);
401 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200402 ssh_free(session->ti.libssh.session);
403 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200404 /* remove the session from the list */
405 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100406 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200407 /* there will be only one session */
408 siter->ti.libssh.next = NULL;
409 } else {
410 /* there are still multiple sessions, keep the ring list */
411 siter->ti.libssh.next = session->ti.libssh.next;
412 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100413 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
414 if (session->flags & NC_SESSION_SSH_MSG_CB) {
415 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
416 if (siter->ti.libssh.next == session) {
417 ERRINT;
418 break;
419 }
420 }
421 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
422 siter->flags |= NC_SESSION_SSH_MSG_CB;
423 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200424 }
425 break;
426#endif
427
428#ifdef ENABLE_TLS
429 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100430 if (connected) {
431 SSL_shutdown(session->ti.tls);
432 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200433 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100434
435 X509_free(session->tls_cert);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200436 break;
437#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100438 case NC_TI_NONE:
439 ERRINT;
440 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200441 }
Michal Vasko428087d2016-01-14 16:04:28 +0100442
Michal Vasko11d142a2016-01-19 15:58:24 +0100443 if (session->side == NC_SERVER) {
444 nc_ctx_lock(-1, NULL);
445 }
Radek Krejciac6d3472015-10-22 15:47:18 +0200446 lydict_remove(session->ctx, session->username);
447 lydict_remove(session->ctx, session->host);
Michal Vasko11d142a2016-01-19 15:58:24 +0100448 if (session->side == NC_SERVER) {
449 nc_ctx_unlock();
450 }
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
Michal Vasko11d142a2016-01-19 15:58:24 +0100494 nc_ctx_lock(-1, NULL);
495
Michal Vasko086311b2016-01-08 09:53:11 +0100496 yanglib = ly_ctx_info(ctx);
497 if (!yanglib) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100498 nc_ctx_unlock();
Michal Vasko086311b2016-01-08 09:53:11 +0100499 return NULL;
500 }
501
502 cpblts = malloc(size * sizeof *cpblts);
503 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
504 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
505 count = 2;
506
507 /* capabilities */
508
509 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
510 if (mod) {
511 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100512 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100513 }
514 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100515 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100516 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100517 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100518 }
519 }
520 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100521 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100522 }
523 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100524 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100525 }
526 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100527 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100528 }
529 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100530 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100531 }
532 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100533 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100534 }
535 }
536
537 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
538 if (mod) {
539 if (!server_opts.wd_basic_mode) {
540 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
541 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100542 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100543 switch (server_opts.wd_basic_mode) {
544 case NC_WD_ALL:
545 strcat(str, "?basic-mode=report-all");
546 break;
547 case NC_WD_TRIM:
548 strcat(str, "?basic-mode=trim");
549 break;
550 case NC_WD_EXPLICIT:
551 strcat(str, "?basic-mode=explicit");
552 break;
553 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100554 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100555 break;
556 }
557
558 if (server_opts.wd_also_supported) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100559 strcat(str, "&amp;also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100560 if (server_opts.wd_also_supported & NC_WD_ALL) {
561 strcat(str, "report-all,");
562 }
563 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
564 strcat(str, "report-all-tagged,");
565 }
566 if (server_opts.wd_also_supported & NC_WD_TRIM) {
567 strcat(str, "trim,");
568 }
569 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
570 strcat(str, "explicit,");
571 }
572 str[strlen(str) - 1] = '\0';
573
574 add_cpblt(ctx, str, &cpblts, &size, &count);
575 }
576 }
577 }
578
Michal Vasko1a38c862016-01-15 15:50:07 +0100579 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100580 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100581 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100582 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100583 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100584 }
585 }
586
587 /* models */
588
589 LY_TREE_FOR(yanglib->child, child) {
590 if (!strcmp(child->schema->name, "module")) {
591 LY_TREE_FOR(child->child, child2) {
592 if (!strcmp(child2->schema->name, "namespace")) {
593 ns = (struct lyd_node_leaf_list *)child2;
594 } else if (!strcmp(child2->schema->name, "name")) {
595 name = (struct lyd_node_leaf_list *)child2;
596 } else if (!strcmp(child2->schema->name, "revision")) {
597 rev = (struct lyd_node_leaf_list *)child2;
598 } else if (!strcmp(child2->schema->name, "feature")) {
599 features = realloc(features, feat_count++ * sizeof *features);
600 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
601 }
602 }
603
604 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100605 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100606 continue;
607 }
608
Michal Vasko11d142a2016-01-19 15:58:24 +0100609 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 +0100610 if (feat_count) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100611 strcat(str, "&amp;features=");
Michal Vasko11d142a2016-01-19 15:58:24 +0100612 str_len += 14;
Michal Vasko086311b2016-01-08 09:53:11 +0100613 for (i = 0; i < feat_count; ++i) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100614 if (str_len + 1 + strlen(features[i]->value_str) >= 512) {
615 ERRINT;
616 break;
617 }
Michal Vasko086311b2016-01-08 09:53:11 +0100618 if (i) {
619 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100620 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100621 }
622 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100623 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100624 }
625 }
626
627 add_cpblt(ctx, str, &cpblts, &size, &count);
628
629 ns = NULL;
630 name = NULL;
631 rev = NULL;
632 free(features);
633 features = NULL;
634 feat_count = 0;
635 }
636 }
637
638 lyd_free(yanglib);
639
Michal Vasko11d142a2016-01-19 15:58:24 +0100640 nc_ctx_unlock();
641
Michal Vasko086311b2016-01-08 09:53:11 +0100642 /* ending NULL capability */
643 add_cpblt(ctx, NULL, &cpblts, &size, &count);
644
645 return cpblts;
646}
647
Radek Krejci695d4fa2015-10-22 13:23:54 +0200648static int
649parse_cpblts(struct lyxml_elem *xml, const char ***list)
650{
651 struct lyxml_elem *cpblt;
652 int ver = -1;
653 int i = 0;
654
655 if (list) {
656 /* get the storage for server's capabilities */
657 LY_TREE_FOR(xml->child, cpblt) {
658 i++;
659 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100660 /* last item remains NULL */
661 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200662 if (!*list) {
663 ERRMEM;
664 return -1;
665 }
666 i = 0;
667 }
668
669 LY_TREE_FOR(xml->child, cpblt) {
670 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
671 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
672 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
673 return -1;
674 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
675 continue;
676 }
677
678 /* detect NETCONF version */
679 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
680 ver = 0;
681 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
682 ver = 1;
683 }
684
685 /* store capabilities */
686 if (list) {
687 (*list)[i] = cpblt->content;
688 cpblt->content = NULL;
689 i++;
690 }
691 }
692
693 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100694 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200695 }
696
697 return ver;
698}
699
700static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100701nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100702{
703 int r, i;
704 const char **cpblts;
705
Michal Vasko11d142a2016-01-19 15:58:24 +0100706 /* client side hello - send only NETCONF base capabilities */
707 cpblts = malloc(3 * sizeof *cpblts);
708 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
709 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
710 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100711
Michal Vasko11d142a2016-01-19 15:58:24 +0100712 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100713
Michal Vasko086311b2016-01-08 09:53:11 +0100714 for (i = 0; cpblts[i]; ++i) {
715 lydict_remove(session->ctx, cpblts[i]);
716 }
717 free(cpblts);
718
719 if (r) {
720 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100721 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100722
723 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100724}
725
726static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100727nc_send_server_hello(struct nc_session *session)
728{
729 int r, i;
730 const char **cpblts;
731
732 cpblts = create_cpblts(session->ctx);
733
734 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
735
736 nc_ctx_lock(-1, NULL);
737 for (i = 0; cpblts[i]; ++i) {
738 lydict_remove(session->ctx, cpblts[i]);
739 }
740 nc_ctx_unlock();
741 free(cpblts);
742
743 if (r) {
744 return NC_MSG_ERROR;
745 }
746
747 return NC_MSG_HELLO;
748}
749
750static NC_MSG_TYPE
751nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200752{
753 struct lyxml_elem *xml = NULL, *node;
754 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
755 int ver = -1;
756 char *str;
757 long long int id;
758 int flag = 0;
759
Michal Vasko05ba9df2016-01-13 14:40:27 +0100760 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200761
762 switch(msgtype) {
763 case NC_MSG_HELLO:
764 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100765 LY_TREE_FOR(xml->child, node) {
766 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
767 continue;
768 } else if (!strcmp(node->name, "session-id")) {
769 if (!node->content || !strlen(node->content)) {
770 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200771 goto error;
772 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100773 str = NULL;
774 id = strtoll(node->content, &str, 10);
775 if (*str || id < 1 || id > UINT32_MAX) {
776 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200777 goto error;
778 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100779 session->id = (uint32_t)id;
780 continue;
781 } else if (strcmp(node->name, "capabilities")) {
782 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200783 goto error;
784 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100785
786 if (flag) {
787 /* multiple capabilities elements */
788 ERR("Invalid <hello> message (multiple <capabilities> elements).");
789 goto error;
790 }
791 flag = 1;
792
793 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
794 goto error;
795 }
796 session->version = ver;
797 }
798
799 if (!session->id) {
800 ERR("Missing <session-id> in server's <hello>.");
801 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200802 }
803 break;
804 case NC_MSG_ERROR:
805 /* nothing special, just pass it out */
806 break;
807 default:
808 ERR("Unexpected message received instead of <hello>.");
809 msgtype = NC_MSG_ERROR;
810 }
811
812 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100813 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200814
815 return msgtype;
816
817error:
818 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100819 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200820
821 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200822}
823
Michal Vasko11d142a2016-01-19 15:58:24 +0100824static NC_MSG_TYPE
825nc_recv_server_hello(struct nc_session *session)
826{
827 struct lyxml_elem *xml = NULL, *node;
828 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
829 int ver = -1;
830 int flag = 0;
831
Michal Vaskoadb850e2016-01-20 14:06:32 +0100832 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100833
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100834 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100835 case NC_MSG_HELLO:
836 /* get know NETCONF version */
837 LY_TREE_FOR(xml->child, node) {
838 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
839 continue;
840 } else if (strcmp(node->name, "capabilities")) {
841 ERR("Unexpected <%s> element in client's <hello>.", node->name);
842 msgtype = NC_MSG_ERROR;
843 goto cleanup;
844 }
845
846 if (flag) {
847 /* multiple capabilities elements */
848 ERR("Invalid <hello> message (multiple <capabilities> elements).");
849 msgtype = NC_MSG_ERROR;
850 goto cleanup;
851 }
852 flag = 1;
853
854 if ((ver = parse_cpblts(node, NULL)) < 0) {
855 msgtype = NC_MSG_ERROR;
856 goto cleanup;
857 }
858 session->version = ver;
859 }
860 break;
861 case NC_MSG_ERROR:
862 /* nothing special, just pass it out */
863 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100864 case NC_MSG_WOULDBLOCK:
865 ERR("Client's <hello> timeout elapsed.");
866 msgtype = NC_MSG_ERROR;
867 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100868 default:
869 ERR("Unexpected message received instead of <hello>.");
870 msgtype = NC_MSG_ERROR;
871 }
872
873cleanup:
874 nc_ctx_lock(-1, NULL);
875 lyxml_free(session->ctx, xml);
876 nc_ctx_unlock();
877
878 return msgtype;
879}
880
Michal Vasko80cad7f2015-12-08 14:42:27 +0100881int
Michal Vasko086311b2016-01-08 09:53:11 +0100882nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100883{
Michal Vasko086311b2016-01-08 09:53:11 +0100884 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100885
Michal Vasko11d142a2016-01-19 15:58:24 +0100886 if (session->side == NC_CLIENT) {
887 type = nc_send_client_hello(session);
888 } else {
889 type = nc_send_server_hello(session);
890 }
891
Michal Vasko086311b2016-01-08 09:53:11 +0100892 if (type != NC_MSG_HELLO) {
893 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100894 }
895
Michal Vasko11d142a2016-01-19 15:58:24 +0100896 if (session->side == NC_CLIENT) {
897 type = nc_recv_client_hello(session);
898 } else {
899 type = nc_recv_server_hello(session);
900 }
901
Michal Vasko086311b2016-01-08 09:53:11 +0100902 if (type != NC_MSG_HELLO) {
903 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100904 }
905
Michal Vasko086311b2016-01-08 09:53:11 +0100906 return 0;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100907}
Michal Vasko086311b2016-01-08 09:53:11 +0100908
909#ifdef ENABLE_SSH
910
911API void
912nc_ssh_init(void)
913{
914 ssh_threads_set_callbacks(ssh_threads_get_pthread());
915 ssh_init();
916 ssh_set_log_level(verbose_level);
917}
918
919API void
920nc_ssh_destroy(void)
921{
Michal Vasko5e228792016-02-03 15:30:24 +0100922 ENGINE_cleanup();
923 CONF_modules_unload(1);
924 ERR_remove_state(0);
Michal Vasko086311b2016-01-08 09:53:11 +0100925 ssh_finalize();
926}
927
928#endif /* ENABLE_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100929
930#ifdef ENABLE_TLS
931
932static pthread_mutex_t *tls_locks;
933
Michal Vaskof0c92c02016-01-29 09:41:45 +0100934struct CRYPTO_dynlock_value {
935 pthread_mutex_t lock;
936};
937
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100938static void
Michal Vaskob48aa812016-01-18 14:13:09 +0100939tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100940{
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100941 if (mode & CRYPTO_LOCK) {
942 pthread_mutex_lock(tls_locks + n);
943 } else {
944 pthread_mutex_unlock(tls_locks + n);
945 }
946}
947
Michal Vaskof0c92c02016-01-29 09:41:45 +0100948static void
949tls_thread_id_func(CRYPTO_THREADID *tid)
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100950{
Michal Vaskof0c92c02016-01-29 09:41:45 +0100951 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
952}
953
954static struct CRYPTO_dynlock_value *
955tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
956{
957 struct CRYPTO_dynlock_value *value;
958
959 value = malloc(sizeof *value);
960 if (!value) {
961 ERRMEM;
962 return NULL;
963 }
964 pthread_mutex_init(&value->lock, NULL);
965
966 return value;
967}
968
969static void
970tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
971{
972 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
973 * I found ignored this fact, what do I know... */
974 if (mode & CRYPTO_LOCK) {
975 pthread_mutex_lock(&l->lock);
976 } else {
977 pthread_mutex_unlock(&l->lock);
978 }
979}
980
981static void
982tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
983{
984 pthread_mutex_destroy(&l->lock);
985 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100986}
987
988API void
989nc_tls_init(void)
990{
991 int i;
992
993 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100994 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100995 SSL_library_init();
996
997 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
998 for (i = 0; i < CRYPTO_num_locks(); ++i) {
999 pthread_mutex_init(tls_locks + i, NULL);
1000 }
1001
Michal Vaskof0c92c02016-01-29 09:41:45 +01001002 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001003 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001004
1005 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1006 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1007 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001008}
1009
1010API void
1011nc_tls_destroy(void)
1012{
1013 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001014
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001015 CRYPTO_cleanup_all_ex_data();
Michal Vasko5e228792016-02-03 15:30:24 +01001016 ERR_remove_state(0);
1017 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001018 ERR_free_strings();
1019 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001020
1021 CRYPTO_set_id_callback(NULL);
1022 CRYPTO_set_locking_callback(NULL);
1023 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1024 pthread_mutex_destroy(tls_locks + i);
1025 }
1026 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001027
1028 CRYPTO_set_dynlock_create_callback(NULL);
1029 CRYPTO_set_dynlock_lock_callback(NULL);
1030 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001031}
1032
1033#endif /* ENABLE_TLS */
Michal Vasko5e228792016-02-03 15:30:24 +01001034
1035#if defined(ENABLE_SSH) || defined(ENABLE_TLS)
1036
1037API void
1038nc_thread_destroy(void) {
1039 CRYPTO_THREADID crypto_tid;
1040
1041 CRYPTO_cleanup_all_ex_data();
1042
1043 CRYPTO_THREADID_current(&crypto_tid);
1044 ERR_remove_thread_state(&crypto_tid);
1045}
1046
1047#endif /* ENABLE_SSH || ENABLE_TLS */
1048
1049#if defined(ENABLE_SSH) && defined(ENABLE_TLS)
1050
1051API void
1052nc_ssh_tls_init(void)
1053{
1054 SSL_load_error_strings();
1055 ERR_load_BIO_strings();
1056 SSL_library_init();
1057
1058 nc_ssh_init();
1059
1060 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1061 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1062 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1063}
1064
1065API void
1066nc_ssh_tls_destroy(void)
1067{
1068 ERR_free_strings();
1069 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1070
1071 nc_ssh_destroy();
1072
1073 CRYPTO_set_dynlock_create_callback(NULL);
1074 CRYPTO_set_dynlock_lock_callback(NULL);
1075 CRYPTO_set_dynlock_destroy_callback(NULL);
1076}
1077
1078#endif /* ENABLE_SSH && ENABLE_TLS */