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