blob: 49f059184334357f9cb69250aa461aabd32059b2 [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 Vasko086311b2016-01-08 09:53:11 +0100215NC_MSG_TYPE
216nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200217{
Michal Vasko086311b2016-01-08 09:53:11 +0100218 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200219
Michal Vasko086311b2016-01-08 09:53:11 +0100220 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100221 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
222 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100223 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100224 }
225
Michal Vasko086311b2016-01-08 09:53:11 +0100226 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
227
228 if (r) {
229 return NC_MSG_ERROR;
230 }
231
232 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100233}
234
Radek Krejci695d4fa2015-10-22 13:23:54 +0200235API void
236nc_session_free(struct nc_session *session)
237{
238 int r, i;
Michal Vasko428087d2016-01-14 16:04:28 +0100239 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100240 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100241 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100242 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100243 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100244 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200245 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100246 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200247 void *p;
248
Michal Vasko428087d2016-01-14 16:04:28 +0100249 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200250 return;
251 }
252
253 /* mark session for closing */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100254 if (session->ti_lock) {
Michal Vasko4589bbe2016-01-29 09:41:30 +0100255 r = nc_timedlock(session->ti_lock, -1, NULL);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100256 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100257 return;
258 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200259 }
260
261 /* stop notifications loop if any */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100262 if (session->ntf_tid) {
263 tid = *session->ntf_tid;
264 free((pthread_t *)session->ntf_tid);
265 session->ntf_tid = NULL;
266 /* the thread now knows it should quit */
267
268 pthread_join(tid, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200269 }
270
Michal Vasko428087d2016-01-14 16:04:28 +0100271 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200272 /* cleanup message queues */
273 /* notifications */
Michal Vaskoad611702015-12-03 13:41:51 +0100274 for (contiter = session->notifs; contiter; ) {
275 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200276
Michal Vaskoad611702015-12-03 13:41:51 +0100277 p = contiter;
278 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200279 free(p);
280 }
281
282 /* rpc replies */
Michal Vaskoad611702015-12-03 13:41:51 +0100283 for (contiter = session->replies; contiter; ) {
284 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200285
Michal Vaskoad611702015-12-03 13:41:51 +0100286 p = contiter;
287 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200288 free(p);
289 }
290
291 /* send closing info to the other side */
292 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
293 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100294 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200295 } else {
296 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100297 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200298 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100299 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100300 case NC_MSG_REPLY:
301 LY_TREE_FOR(rpl->child, child) {
302 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
303 break;
304 }
305 }
306 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100307 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100308 }
Michal Vaskoad611702015-12-03 13:41:51 +0100309 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100310 break;
311 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100312 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100313 break;
314 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100315 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100316 break;
317 default:
318 /* cannot happen */
319 break;
320 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200321 }
322
323 /* list of server's capabilities */
324 if (session->cpblts) {
325 for (i = 0; session->cpblts[i]; i++) {
326 lydict_remove(session->ctx, session->cpblts[i]);
327 }
328 free(session->cpblts);
329 }
330 }
331
332 session->status = NC_STATUS_CLOSING;
Michal Vasko428087d2016-01-14 16:04:28 +0100333 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200334
335 /* transport implementation cleanup */
336 switch (session->ti_type) {
337 case NC_TI_FD:
338 /* nothing needed - file descriptors were provided by caller,
339 * so it is up to the caller to close them correctly
340 * TODO use callbacks
341 */
Michal Vasko3512e402016-01-28 16:22:34 +0100342 /* just to avoid compiler warning */
343 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100344 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200345 break;
346
Radek Krejci53691be2016-02-22 13:58:37 +0100347#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200348 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100349 if (connected) {
350 ssh_channel_free(session->ti.libssh.channel);
351 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200352 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
353 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
354 * it.
355 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100356 multisession = 0;
357 if (session->ti.libssh.next) {
358 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
359 if (siter->status != NC_STATUS_STARTING) {
360 multisession = 1;
361 break;
362 }
363 }
364 }
365
366 if (!multisession) {
367 /* it's not multisession yet, but we still need to free the starting sessions */
368 if (session->ti.libssh.next) {
369 do {
370 siter = session->ti.libssh.next;
371 session->ti.libssh.next = siter->ti.libssh.next;
372
373 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100374 lydict_remove(session->ctx, session->username);
375 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100376 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100377 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100378 }
379
380 free(siter);
381 } while (session->ti.libssh.next != session);
382 }
Michal Vasko428087d2016-01-14 16:04:28 +0100383 if (connected) {
384 ssh_disconnect(session->ti.libssh.session);
385 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200386 ssh_free(session->ti.libssh.session);
387 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200388 /* remove the session from the list */
389 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100390 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200391 /* there will be only one session */
392 siter->ti.libssh.next = NULL;
393 } else {
394 /* there are still multiple sessions, keep the ring list */
395 siter->ti.libssh.next = session->ti.libssh.next;
396 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100397 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
398 if (session->flags & NC_SESSION_SSH_MSG_CB) {
399 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
400 if (siter->ti.libssh.next == session) {
401 ERRINT;
402 break;
403 }
404 }
405 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
406 siter->flags |= NC_SESSION_SSH_MSG_CB;
407 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200408 }
409 break;
410#endif
411
Radek Krejci53691be2016-02-22 13:58:37 +0100412#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200413 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100414 if (connected) {
415 SSL_shutdown(session->ti.tls);
416 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200417 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100418
419 X509_free(session->tls_cert);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200420 break;
421#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100422 case NC_TI_NONE:
423 ERRINT;
424 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200425 }
Michal Vasko428087d2016-01-14 16:04:28 +0100426
Radek Krejciac6d3472015-10-22 15:47:18 +0200427 lydict_remove(session->ctx, session->username);
428 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200429
430 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100431 if (session->ti_lock) {
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100432 pthread_mutex_unlock(session->ti_lock);
Michal Vaskob48aa812016-01-18 14:13:09 +0100433 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100434 pthread_mutex_destroy(session->ti_lock);
435 free(session->ti_lock);
436 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200437 }
438
439 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100440 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200441 }
442
443 free(session);
444}
445
Michal Vasko086311b2016-01-08 09:53:11 +0100446static void
447add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
448{
449 if (*count == *size) {
450 *size += 5;
451 *cpblts = realloc(*cpblts, *size * sizeof **cpblts);
452 }
453
454 if (capab) {
455 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
456 } else {
457 (*cpblts)[*count] = NULL;
458 }
459 ++(*count);
460}
461
462static const char **
463create_cpblts(struct ly_ctx *ctx)
464{
465 struct lyd_node *child, *child2, *yanglib;
466 struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL;
467 const char **cpblts;
468 const struct lys_module *mod;
Michal Vasko11d142a2016-01-19 15:58:24 +0100469 int size = 10, count, feat_count = 0, i, str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100470 char str[512];
471
472 yanglib = ly_ctx_info(ctx);
473 if (!yanglib) {
474 return NULL;
475 }
476
477 cpblts = malloc(size * sizeof *cpblts);
478 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
479 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
480 count = 2;
481
482 /* capabilities */
483
484 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
485 if (mod) {
486 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100487 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100488 }
489 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100490 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100491 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100492 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100493 }
494 }
495 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100496 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100497 }
498 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100499 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100500 }
501 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100502 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100503 }
504 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100505 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100506 }
507 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100508 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100509 }
510 }
511
512 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
513 if (mod) {
514 if (!server_opts.wd_basic_mode) {
515 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
516 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100517 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100518 switch (server_opts.wd_basic_mode) {
519 case NC_WD_ALL:
520 strcat(str, "?basic-mode=report-all");
521 break;
522 case NC_WD_TRIM:
523 strcat(str, "?basic-mode=trim");
524 break;
525 case NC_WD_EXPLICIT:
526 strcat(str, "?basic-mode=explicit");
527 break;
528 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100529 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100530 break;
531 }
532
533 if (server_opts.wd_also_supported) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100534 strcat(str, "&amp;also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100535 if (server_opts.wd_also_supported & NC_WD_ALL) {
536 strcat(str, "report-all,");
537 }
538 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
539 strcat(str, "report-all-tagged,");
540 }
541 if (server_opts.wd_also_supported & NC_WD_TRIM) {
542 strcat(str, "trim,");
543 }
544 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
545 strcat(str, "explicit,");
546 }
547 str[strlen(str) - 1] = '\0';
548
549 add_cpblt(ctx, str, &cpblts, &size, &count);
550 }
551 }
552 }
553
Michal Vasko1a38c862016-01-15 15:50:07 +0100554 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100555 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100556 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100557 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100558 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100559 }
560 }
561
562 /* models */
563
564 LY_TREE_FOR(yanglib->child, child) {
565 if (!strcmp(child->schema->name, "module")) {
566 LY_TREE_FOR(child->child, child2) {
567 if (!strcmp(child2->schema->name, "namespace")) {
568 ns = (struct lyd_node_leaf_list *)child2;
569 } else if (!strcmp(child2->schema->name, "name")) {
570 name = (struct lyd_node_leaf_list *)child2;
571 } else if (!strcmp(child2->schema->name, "revision")) {
572 rev = (struct lyd_node_leaf_list *)child2;
573 } else if (!strcmp(child2->schema->name, "feature")) {
574 features = realloc(features, feat_count++ * sizeof *features);
575 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
576 }
577 }
578
579 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100580 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100581 continue;
582 }
583
Michal Vasko11d142a2016-01-19 15:58:24 +0100584 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 +0100585 if (feat_count) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100586 strcat(str, "&amp;features=");
Michal Vasko11d142a2016-01-19 15:58:24 +0100587 str_len += 14;
Michal Vasko086311b2016-01-08 09:53:11 +0100588 for (i = 0; i < feat_count; ++i) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100589 if (str_len + 1 + strlen(features[i]->value_str) >= 512) {
590 ERRINT;
591 break;
592 }
Michal Vasko086311b2016-01-08 09:53:11 +0100593 if (i) {
594 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100595 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100596 }
597 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100598 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100599 }
600 }
601
602 add_cpblt(ctx, str, &cpblts, &size, &count);
603
604 ns = NULL;
605 name = NULL;
606 rev = NULL;
607 free(features);
608 features = NULL;
609 feat_count = 0;
610 }
611 }
612
613 lyd_free(yanglib);
614
615 /* ending NULL capability */
616 add_cpblt(ctx, NULL, &cpblts, &size, &count);
617
618 return cpblts;
619}
620
Radek Krejci695d4fa2015-10-22 13:23:54 +0200621static int
622parse_cpblts(struct lyxml_elem *xml, const char ***list)
623{
624 struct lyxml_elem *cpblt;
625 int ver = -1;
626 int i = 0;
627
628 if (list) {
629 /* get the storage for server's capabilities */
630 LY_TREE_FOR(xml->child, cpblt) {
631 i++;
632 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100633 /* last item remains NULL */
634 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200635 if (!*list) {
636 ERRMEM;
637 return -1;
638 }
639 i = 0;
640 }
641
642 LY_TREE_FOR(xml->child, cpblt) {
643 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
644 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
645 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
646 return -1;
647 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
648 continue;
649 }
650
651 /* detect NETCONF version */
652 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
653 ver = 0;
654 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
655 ver = 1;
656 }
657
658 /* store capabilities */
659 if (list) {
660 (*list)[i] = cpblt->content;
661 cpblt->content = NULL;
662 i++;
663 }
664 }
665
666 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100667 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200668 }
669
670 return ver;
671}
672
673static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100674nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100675{
676 int r, i;
677 const char **cpblts;
678
Michal Vasko11d142a2016-01-19 15:58:24 +0100679 /* client side hello - send only NETCONF base capabilities */
680 cpblts = malloc(3 * sizeof *cpblts);
681 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
682 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
683 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100684
Michal Vasko11d142a2016-01-19 15:58:24 +0100685 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100686
Michal Vasko086311b2016-01-08 09:53:11 +0100687 for (i = 0; cpblts[i]; ++i) {
688 lydict_remove(session->ctx, cpblts[i]);
689 }
690 free(cpblts);
691
692 if (r) {
693 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100694 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100695
696 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100697}
698
699static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100700nc_send_server_hello(struct nc_session *session)
701{
702 int r, i;
703 const char **cpblts;
704
705 cpblts = create_cpblts(session->ctx);
706
707 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
708
Michal Vasko11d142a2016-01-19 15:58:24 +0100709 for (i = 0; cpblts[i]; ++i) {
710 lydict_remove(session->ctx, cpblts[i]);
711 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100712 free(cpblts);
713
714 if (r) {
715 return NC_MSG_ERROR;
716 }
717
718 return NC_MSG_HELLO;
719}
720
721static NC_MSG_TYPE
722nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200723{
724 struct lyxml_elem *xml = NULL, *node;
725 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
726 int ver = -1;
727 char *str;
728 long long int id;
729 int flag = 0;
730
Michal Vasko05ba9df2016-01-13 14:40:27 +0100731 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200732
733 switch(msgtype) {
734 case NC_MSG_HELLO:
735 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100736 LY_TREE_FOR(xml->child, node) {
737 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
738 continue;
739 } else if (!strcmp(node->name, "session-id")) {
740 if (!node->content || !strlen(node->content)) {
741 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200742 goto error;
743 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100744 str = NULL;
745 id = strtoll(node->content, &str, 10);
746 if (*str || id < 1 || id > UINT32_MAX) {
747 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200748 goto error;
749 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100750 session->id = (uint32_t)id;
751 continue;
752 } else if (strcmp(node->name, "capabilities")) {
753 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200754 goto error;
755 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100756
757 if (flag) {
758 /* multiple capabilities elements */
759 ERR("Invalid <hello> message (multiple <capabilities> elements).");
760 goto error;
761 }
762 flag = 1;
763
764 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
765 goto error;
766 }
767 session->version = ver;
768 }
769
770 if (!session->id) {
771 ERR("Missing <session-id> in server's <hello>.");
772 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200773 }
774 break;
775 case NC_MSG_ERROR:
776 /* nothing special, just pass it out */
777 break;
778 default:
779 ERR("Unexpected message received instead of <hello>.");
780 msgtype = NC_MSG_ERROR;
781 }
782
783 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100784 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200785
786 return msgtype;
787
788error:
789 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100790 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200791
792 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200793}
794
Michal Vasko11d142a2016-01-19 15:58:24 +0100795static NC_MSG_TYPE
796nc_recv_server_hello(struct nc_session *session)
797{
798 struct lyxml_elem *xml = NULL, *node;
799 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
800 int ver = -1;
801 int flag = 0;
802
Michal Vaskoadb850e2016-01-20 14:06:32 +0100803 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100804
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100805 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100806 case NC_MSG_HELLO:
807 /* get know NETCONF version */
808 LY_TREE_FOR(xml->child, node) {
809 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
810 continue;
811 } else if (strcmp(node->name, "capabilities")) {
812 ERR("Unexpected <%s> element in client's <hello>.", node->name);
813 msgtype = NC_MSG_ERROR;
814 goto cleanup;
815 }
816
817 if (flag) {
818 /* multiple capabilities elements */
819 ERR("Invalid <hello> message (multiple <capabilities> elements).");
820 msgtype = NC_MSG_ERROR;
821 goto cleanup;
822 }
823 flag = 1;
824
825 if ((ver = parse_cpblts(node, NULL)) < 0) {
826 msgtype = NC_MSG_ERROR;
827 goto cleanup;
828 }
829 session->version = ver;
830 }
831 break;
832 case NC_MSG_ERROR:
833 /* nothing special, just pass it out */
834 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100835 case NC_MSG_WOULDBLOCK:
836 ERR("Client's <hello> timeout elapsed.");
837 msgtype = NC_MSG_ERROR;
838 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100839 default:
840 ERR("Unexpected message received instead of <hello>.");
841 msgtype = NC_MSG_ERROR;
842 }
843
844cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +0100845 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100846
847 return msgtype;
848}
849
Michal Vasko80cad7f2015-12-08 14:42:27 +0100850int
Michal Vasko086311b2016-01-08 09:53:11 +0100851nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100852{
Michal Vasko086311b2016-01-08 09:53:11 +0100853 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100854
Michal Vasko11d142a2016-01-19 15:58:24 +0100855 if (session->side == NC_CLIENT) {
856 type = nc_send_client_hello(session);
857 } else {
858 type = nc_send_server_hello(session);
859 }
860
Michal Vasko086311b2016-01-08 09:53:11 +0100861 if (type != NC_MSG_HELLO) {
862 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100863 }
864
Michal Vasko11d142a2016-01-19 15:58:24 +0100865 if (session->side == NC_CLIENT) {
866 type = nc_recv_client_hello(session);
867 } else {
868 type = nc_recv_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 Vasko086311b2016-01-08 09:53:11 +0100875 return 0;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100876}
Michal Vasko086311b2016-01-08 09:53:11 +0100877
Radek Krejci53691be2016-02-22 13:58:37 +0100878#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100879
Michal Vasko8f0c0282016-02-29 10:17:14 +0100880static void
Michal Vasko086311b2016-01-08 09:53:11 +0100881nc_ssh_init(void)
882{
883 ssh_threads_set_callbacks(ssh_threads_get_pthread());
884 ssh_init();
885 ssh_set_log_level(verbose_level);
886}
887
Michal Vasko8f0c0282016-02-29 10:17:14 +0100888static void
Michal Vasko086311b2016-01-08 09:53:11 +0100889nc_ssh_destroy(void)
890{
Michal Vasko8f0c0282016-02-29 10:17:14 +0100891 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +0100892 ENGINE_cleanup();
893 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +0100894 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +0100895 ssh_finalize();
896}
897
Radek Krejci53691be2016-02-22 13:58:37 +0100898#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100899
Radek Krejci53691be2016-02-22 13:58:37 +0100900#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100901
Michal Vaskof0c92c02016-01-29 09:41:45 +0100902struct CRYPTO_dynlock_value {
903 pthread_mutex_t lock;
904};
905
Michal Vaskof0c92c02016-01-29 09:41:45 +0100906static struct CRYPTO_dynlock_value *
907tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
908{
909 struct CRYPTO_dynlock_value *value;
910
911 value = malloc(sizeof *value);
912 if (!value) {
913 ERRMEM;
914 return NULL;
915 }
916 pthread_mutex_init(&value->lock, NULL);
917
918 return value;
919}
920
921static void
922tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
923{
924 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
925 * I found ignored this fact, what do I know... */
926 if (mode & CRYPTO_LOCK) {
927 pthread_mutex_lock(&l->lock);
928 } else {
929 pthread_mutex_unlock(&l->lock);
930 }
931}
932
933static void
934tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
935{
936 pthread_mutex_destroy(&l->lock);
937 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100938}
939
Michal Vasko8f0c0282016-02-29 10:17:14 +0100940#endif /* NC_ENABLED_TLS */
941
942#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
943
944static pthread_mutex_t *tls_locks;
945
946static void
947tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
948{
949 if (mode & CRYPTO_LOCK) {
950 pthread_mutex_lock(tls_locks + n);
951 } else {
952 pthread_mutex_unlock(tls_locks + n);
953 }
954}
955
956static void
957tls_thread_id_func(CRYPTO_THREADID *tid)
958{
959 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
960}
961
962static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100963nc_tls_init(void)
964{
965 int i;
966
967 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100968 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100969 SSL_library_init();
970
971 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
972 for (i = 0; i < CRYPTO_num_locks(); ++i) {
973 pthread_mutex_init(tls_locks + i, NULL);
974 }
975
Michal Vaskof0c92c02016-01-29 09:41:45 +0100976 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100977 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +0100978
979 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
980 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
981 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100982}
983
Michal Vasko8f0c0282016-02-29 10:17:14 +0100984static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100985nc_tls_destroy(void)
986{
987 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100988
Michal Vasko8f0c0282016-02-29 10:17:14 +0100989 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100990 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +0100991 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +0100992 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100993 ERR_free_strings();
994 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100995
Michal Vaskob6e37262016-02-25 14:49:00 +0100996 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100997 CRYPTO_set_locking_callback(NULL);
998 for (i = 0; i < CRYPTO_num_locks(); ++i) {
999 pthread_mutex_destroy(tls_locks + i);
1000 }
1001 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001002
1003 CRYPTO_set_dynlock_create_callback(NULL);
1004 CRYPTO_set_dynlock_lock_callback(NULL);
1005 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001006}
1007
Michal Vasko8f0c0282016-02-29 10:17:14 +01001008#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001009
Radek Krejci53691be2016-02-22 13:58:37 +01001010#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001011
Michal Vasko8f0c0282016-02-29 10:17:14 +01001012static void
Michal Vasko5e228792016-02-03 15:30:24 +01001013nc_ssh_tls_init(void)
1014{
1015 SSL_load_error_strings();
1016 ERR_load_BIO_strings();
1017 SSL_library_init();
1018
1019 nc_ssh_init();
1020
1021 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1022 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1023 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1024}
1025
Michal Vasko8f0c0282016-02-29 10:17:14 +01001026static void
Michal Vasko5e228792016-02-03 15:30:24 +01001027nc_ssh_tls_destroy(void)
1028{
1029 ERR_free_strings();
1030 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1031
1032 nc_ssh_destroy();
1033
1034 CRYPTO_set_dynlock_create_callback(NULL);
1035 CRYPTO_set_dynlock_lock_callback(NULL);
1036 CRYPTO_set_dynlock_destroy_callback(NULL);
1037}
1038
Radek Krejci53691be2016-02-22 13:58:37 +01001039#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001040
1041#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1042
1043API void
1044nc_thread_destroy(void)
1045{
1046 CRYPTO_THREADID crypto_tid;
1047
1048 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1049 //CRYPTO_cleanup_all_ex_data();
1050
1051 CRYPTO_THREADID_current(&crypto_tid);
1052 ERR_remove_thread_state(&crypto_tid);
1053}
1054
1055API void
1056nc_init(void)
1057{
1058#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1059 nc_ssh_tls_init();
1060#elif defined(NC_ENABLED_SSH)
1061 nc_ssh_init();
1062#elif defined(NC_ENABLED_TLS)
1063 nc_tls_init();
1064#endif
1065}
1066
1067API void
1068nc_destroy(void)
1069{
1070#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1071 nc_ssh_tls_destroy();
1072#elif defined(NC_ENABLED_SSH)
1073 nc_ssh_destroy();
1074#elif defined(NC_ENABLED_TLS)
1075 nc_tls_destroy();
1076#endif
1077}
1078
1079#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */