blob: 9a8153e5ffcfbafd118560dea8540be3c791bb9a [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
Michal Vasko62be1ce2016-03-03 13:24:52 +010054nc_timedlock(pthread_mutex_t *lock, int timeout)
Michal Vasko96164bf2016-01-21 15:41:58 +010055{
56 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +010057 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +010058
59 if (timeout > 0) {
60 clock_gettime(CLOCK_REALTIME, &ts_timeout);
61
Michal Vasko96164bf2016-01-21 15:41:58 +010062 ts_timeout.tv_sec += timeout / 1000;
63 ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
64
65 ret = pthread_mutex_timedlock(lock, &ts_timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +010066 } else if (!timeout) {
67 ret = pthread_mutex_trylock(lock);
68 } else { /* timeout == -1 */
69 ret = pthread_mutex_lock(lock);
70 }
71
72 if (ret == ETIMEDOUT) {
73 /* timeout */
74 return 0;
75 } else if (ret) {
76 /* error */
Radek Krejcida8f58d2016-03-03 13:10:21 +010077 ERR("Mutex lock failed (%s).", strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +010078 return -1;
79 }
80
81 /* ok */
82 return 1;
83}
84
Michal Vasko8dadf782016-01-15 10:29:36 +010085API NC_STATUS
86nc_session_get_status(const struct nc_session *session)
87{
Michal Vasko7f1c78b2016-01-19 09:52:14 +010088 if (!session) {
89 ERRARG;
90 return 0;
91 }
92
Michal Vasko8dadf782016-01-15 10:29:36 +010093 return session->status;
94}
95
96API uint32_t
97nc_session_get_id(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->id;
105}
106
Michal Vasko174fe8e2016-02-17 15:38:09 +0100107API int
108nc_session_get_version(const struct nc_session *session)
109{
110 if (!session) {
111 ERRARG;
112 return -1;
113 }
114
115 return (session->version == NC_VERSION_10 ? 0 : 1);
116}
117
Michal Vasko8dadf782016-01-15 10:29:36 +0100118API NC_TRANSPORT_IMPL
119nc_session_get_ti(const struct nc_session *session)
120{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100121 if (!session) {
122 ERRARG;
123 return 0;
124 }
125
Michal Vasko8dadf782016-01-15 10:29:36 +0100126 return session->ti_type;
127}
128
129API const char *
130nc_session_get_username(const struct nc_session *session)
131{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100132 if (!session) {
133 ERRARG;
134 return NULL;
135 }
136
Michal Vasko8dadf782016-01-15 10:29:36 +0100137 return session->username;
138}
139
140API const char *
141nc_session_get_host(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->host;
149}
150
151API uint16_t
152nc_session_get_port(const struct nc_session *session)
153{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100154 if (!session) {
155 ERRARG;
156 return 0;
157 }
158
Michal Vasko8dadf782016-01-15 10:29:36 +0100159 return session->port;
160}
161
Michal Vasko9a25e932016-02-01 10:36:42 +0100162API struct ly_ctx *
163nc_session_get_ctx(const struct nc_session *session)
164{
165 if (!session) {
166 ERRARG;
167 return NULL;
168 }
169
170 return session->ctx;
171}
172
Michal Vasko8dadf782016-01-15 10:29:36 +0100173API const char **
174nc_session_get_cpblts(const struct nc_session *session)
175{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100176 if (!session) {
177 ERRARG;
178 return NULL;
179 }
180
Michal Vasko8dadf782016-01-15 10:29:36 +0100181 return session->cpblts;
182}
183
184API const char *
185nc_session_cpblt(const struct nc_session *session, const char *capab)
186{
187 int i, len;
188
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100189 if (!session || !capab) {
190 ERRARG;
191 return NULL;
192 }
193
Michal Vasko8dadf782016-01-15 10:29:36 +0100194 len = strlen(capab);
195 for (i = 0; session->cpblts[i]; ++i) {
196 if (!strncmp(session->cpblts[i], capab, len)) {
197 return session->cpblts[i];
198 }
199 }
200
201 return NULL;
202}
203
Michal Vasko2cc4c682016-03-01 09:16:48 +0100204API void
205nc_session_set_data(struct nc_session *session, void *data)
206{
207 if (!session) {
208 ERRARG;
209 return;
210 }
211
212 session->data = data;
213}
214
215API void *
216nc_session_get_data(const struct nc_session *session)
217{
218 if (!session) {
219 ERRARG;
220 return NULL;
221 }
222
223 return session->data;
224}
225
Michal Vasko086311b2016-01-08 09:53:11 +0100226NC_MSG_TYPE
227nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200228{
Michal Vasko086311b2016-01-08 09:53:11 +0100229 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200230
Michal Vasko086311b2016-01-08 09:53:11 +0100231 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100232 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
233 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100234 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100235 }
236
Michal Vasko086311b2016-01-08 09:53:11 +0100237 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
238
239 if (r) {
240 return NC_MSG_ERROR;
241 }
242
243 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100244}
245
Radek Krejci695d4fa2015-10-22 13:23:54 +0200246API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100247nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200248{
Michal Vasko9e99f012016-03-03 13:25:20 +0100249 int r, i, locked;
Michal Vasko428087d2016-01-14 16:04:28 +0100250 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100251 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100252 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100253 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100254 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100255 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200256 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100257 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200258 void *p;
259
Michal Vasko428087d2016-01-14 16:04:28 +0100260 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200261 return;
262 }
263
Michal Vasko86d357c2016-03-11 13:46:38 +0100264 /* stop notifications loop if any */
265 if (session->ntf_tid) {
266 tid = *session->ntf_tid;
267 free((pthread_t *)session->ntf_tid);
268 session->ntf_tid = NULL;
269 /* the thread now knows it should quit */
270
271 pthread_join(tid, NULL);
272 }
273
Michal Vaskoadd4c792015-10-26 15:36:58 +0100274 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100275 r = nc_timedlock(session->ti_lock, NC_READ_TIMEOUT * 1000);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100276 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100277 return;
Michal Vasko9e99f012016-03-03 13:25:20 +0100278 } else if (!r) {
279 /* we failed to lock it, too bad */
280 locked = 0;
281 } else {
282 locked = 1;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100283 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200284 }
285
Michal Vasko9e99f012016-03-03 13:25:20 +0100286 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200287 /* cleanup message queues */
288 /* notifications */
Michal Vaskoad611702015-12-03 13:41:51 +0100289 for (contiter = session->notifs; contiter; ) {
290 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200291
Michal Vaskoad611702015-12-03 13:41:51 +0100292 p = contiter;
293 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200294 free(p);
295 }
296
297 /* rpc replies */
Michal Vaskoad611702015-12-03 13:41:51 +0100298 for (contiter = session->replies; contiter; ) {
299 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200300
Michal Vaskoad611702015-12-03 13:41:51 +0100301 p = contiter;
302 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200303 free(p);
304 }
305
306 /* send closing info to the other side */
307 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
308 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100309 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200310 } else {
311 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100312 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200313 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100314 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100315 case NC_MSG_REPLY:
316 LY_TREE_FOR(rpl->child, child) {
317 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
318 break;
319 }
320 }
321 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100322 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100323 }
Michal Vaskoad611702015-12-03 13:41:51 +0100324 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100325 break;
326 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100327 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100328 break;
329 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100330 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100331 break;
332 default:
333 /* cannot happen */
334 break;
335 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200336 }
337
338 /* list of server's capabilities */
339 if (session->cpblts) {
340 for (i = 0; session->cpblts[i]; i++) {
341 lydict_remove(session->ctx, session->cpblts[i]);
342 }
343 free(session->cpblts);
344 }
345 }
346
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100347 if (session->data && data_free) {
348 data_free(session->data);
349 }
350
Michal Vasko86d357c2016-03-11 13:46:38 +0100351 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200352 session->status = NC_STATUS_CLOSING;
Michal Vasko428087d2016-01-14 16:04:28 +0100353 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200354
355 /* transport implementation cleanup */
356 switch (session->ti_type) {
357 case NC_TI_FD:
358 /* nothing needed - file descriptors were provided by caller,
359 * so it is up to the caller to close them correctly
360 * TODO use callbacks
361 */
Michal Vasko3512e402016-01-28 16:22:34 +0100362 /* just to avoid compiler warning */
363 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100364 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200365 break;
366
Radek Krejci53691be2016-02-22 13:58:37 +0100367#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200368 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100369 if (connected) {
370 ssh_channel_free(session->ti.libssh.channel);
371 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200372 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
373 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
374 * it.
375 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100376 multisession = 0;
377 if (session->ti.libssh.next) {
378 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
379 if (siter->status != NC_STATUS_STARTING) {
380 multisession = 1;
381 break;
382 }
383 }
384 }
385
386 if (!multisession) {
387 /* it's not multisession yet, but we still need to free the starting sessions */
388 if (session->ti.libssh.next) {
389 do {
390 siter = session->ti.libssh.next;
391 session->ti.libssh.next = siter->ti.libssh.next;
392
393 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100394 lydict_remove(session->ctx, session->username);
395 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100396 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100397 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100398 }
399
400 free(siter);
401 } while (session->ti.libssh.next != session);
402 }
Michal Vasko428087d2016-01-14 16:04:28 +0100403 if (connected) {
404 ssh_disconnect(session->ti.libssh.session);
405 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200406 ssh_free(session->ti.libssh.session);
407 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200408 /* remove the session from the list */
409 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100410 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200411 /* there will be only one session */
412 siter->ti.libssh.next = NULL;
413 } else {
414 /* there are still multiple sessions, keep the ring list */
415 siter->ti.libssh.next = session->ti.libssh.next;
416 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100417 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
418 if (session->flags & NC_SESSION_SSH_MSG_CB) {
419 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
420 if (siter->ti.libssh.next == session) {
421 ERRINT;
422 break;
423 }
424 }
425 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
426 siter->flags |= NC_SESSION_SSH_MSG_CB;
427 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200428 }
429 break;
430#endif
431
Radek Krejci53691be2016-02-22 13:58:37 +0100432#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200433 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100434 if (connected) {
435 SSL_shutdown(session->ti.tls);
436 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200437 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100438
439 X509_free(session->tls_cert);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200440 break;
441#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100442 case NC_TI_NONE:
443 ERRINT;
444 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200445 }
Michal Vasko428087d2016-01-14 16:04:28 +0100446
Radek Krejciac6d3472015-10-22 15:47:18 +0200447 lydict_remove(session->ctx, session->username);
448 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200449
450 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100451 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100452 if (locked) {
453 pthread_mutex_unlock(session->ti_lock);
454 }
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;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100473 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
474 if (!(*cpblts)) {
475 ERRMEM;
476 return;
477 }
Michal Vasko086311b2016-01-08 09:53:11 +0100478 }
479
480 if (capab) {
481 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
482 } else {
483 (*cpblts)[*count] = NULL;
484 }
485 ++(*count);
486}
487
488static const char **
489create_cpblts(struct ly_ctx *ctx)
490{
491 struct lyd_node *child, *child2, *yanglib;
492 struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL;
493 const char **cpblts;
494 const struct lys_module *mod;
Michal Vasko11d142a2016-01-19 15:58:24 +0100495 int size = 10, count, feat_count = 0, i, str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100496 char str[512];
497
498 yanglib = ly_ctx_info(ctx);
499 if (!yanglib) {
500 return NULL;
501 }
502
503 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100504 if (!cpblts) {
505 ERRMEM;
506 return NULL;
507 }
Michal Vasko086311b2016-01-08 09:53:11 +0100508 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
509 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
510 count = 2;
511
512 /* capabilities */
513
514 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
515 if (mod) {
516 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100517 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100518 }
519 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100520 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100521 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100522 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100523 }
524 }
525 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100526 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100527 }
528 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100529 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100530 }
531 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100532 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100533 }
534 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100535 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100536 }
537 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100538 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100539 }
540 }
541
542 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
543 if (mod) {
544 if (!server_opts.wd_basic_mode) {
545 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
546 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100547 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100548 switch (server_opts.wd_basic_mode) {
549 case NC_WD_ALL:
550 strcat(str, "?basic-mode=report-all");
551 break;
552 case NC_WD_TRIM:
553 strcat(str, "?basic-mode=trim");
554 break;
555 case NC_WD_EXPLICIT:
556 strcat(str, "?basic-mode=explicit");
557 break;
558 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100559 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100560 break;
561 }
562
563 if (server_opts.wd_also_supported) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100564 strcat(str, "&amp;also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100565 if (server_opts.wd_also_supported & NC_WD_ALL) {
566 strcat(str, "report-all,");
567 }
568 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
569 strcat(str, "report-all-tagged,");
570 }
571 if (server_opts.wd_also_supported & NC_WD_TRIM) {
572 strcat(str, "trim,");
573 }
574 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
575 strcat(str, "explicit,");
576 }
577 str[strlen(str) - 1] = '\0';
578
579 add_cpblt(ctx, str, &cpblts, &size, &count);
580 }
581 }
582 }
583
Michal Vasko1a38c862016-01-15 15:50:07 +0100584 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100585 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100586 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100587 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100588 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100589 }
590 }
591
592 /* models */
Michal Vasko086311b2016-01-08 09:53:11 +0100593 LY_TREE_FOR(yanglib->child, child) {
594 if (!strcmp(child->schema->name, "module")) {
595 LY_TREE_FOR(child->child, child2) {
596 if (!strcmp(child2->schema->name, "namespace")) {
597 ns = (struct lyd_node_leaf_list *)child2;
598 } else if (!strcmp(child2->schema->name, "name")) {
599 name = (struct lyd_node_leaf_list *)child2;
600 } else if (!strcmp(child2->schema->name, "revision")) {
601 rev = (struct lyd_node_leaf_list *)child2;
602 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100603 features = nc_realloc(features, ++feat_count * sizeof *features);
604 if (!features) {
605 ERRMEM;
606 free(cpblts);
607 return NULL;
608 }
Michal Vasko086311b2016-01-08 09:53:11 +0100609 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
610 }
611 }
612
613 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100614 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100615 continue;
616 }
617
Radek Krejcidf7ba522016-03-01 16:05:25 +0100618 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
619 rev->value_str[0] ? "&amp;revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100620 if (feat_count) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100621 strcat(str, "&amp;features=");
Michal Vasko11d142a2016-01-19 15:58:24 +0100622 str_len += 14;
Michal Vasko086311b2016-01-08 09:53:11 +0100623 for (i = 0; i < feat_count; ++i) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100624 if (str_len + 1 + strlen(features[i]->value_str) >= 512) {
625 ERRINT;
626 break;
627 }
Michal Vasko086311b2016-01-08 09:53:11 +0100628 if (i) {
629 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100630 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100631 }
632 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100633 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100634 }
635 }
636
637 add_cpblt(ctx, str, &cpblts, &size, &count);
638
639 ns = NULL;
640 name = NULL;
641 rev = NULL;
642 free(features);
643 features = NULL;
644 feat_count = 0;
645 }
646 }
647
648 lyd_free(yanglib);
649
650 /* ending NULL capability */
651 add_cpblt(ctx, NULL, &cpblts, &size, &count);
652
653 return cpblts;
654}
655
Radek Krejci695d4fa2015-10-22 13:23:54 +0200656static int
657parse_cpblts(struct lyxml_elem *xml, const char ***list)
658{
659 struct lyxml_elem *cpblt;
660 int ver = -1;
661 int i = 0;
662
663 if (list) {
664 /* get the storage for server's capabilities */
665 LY_TREE_FOR(xml->child, cpblt) {
666 i++;
667 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100668 /* last item remains NULL */
669 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200670 if (!*list) {
671 ERRMEM;
672 return -1;
673 }
674 i = 0;
675 }
676
677 LY_TREE_FOR(xml->child, cpblt) {
678 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
679 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
680 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
681 return -1;
682 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
683 continue;
684 }
685
686 /* detect NETCONF version */
687 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
688 ver = 0;
689 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
690 ver = 1;
691 }
692
693 /* store capabilities */
694 if (list) {
695 (*list)[i] = cpblt->content;
696 cpblt->content = NULL;
697 i++;
698 }
699 }
700
701 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100702 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200703 }
704
705 return ver;
706}
707
708static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100709nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100710{
711 int r, i;
712 const char **cpblts;
713
Michal Vasko11d142a2016-01-19 15:58:24 +0100714 /* client side hello - send only NETCONF base capabilities */
715 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100716 if (!cpblts) {
717 ERRMEM;
718 return NC_MSG_ERROR;
719 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100720 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
721 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
722 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100723
Michal Vasko11d142a2016-01-19 15:58:24 +0100724 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100725
Michal Vasko086311b2016-01-08 09:53:11 +0100726 for (i = 0; cpblts[i]; ++i) {
727 lydict_remove(session->ctx, cpblts[i]);
728 }
729 free(cpblts);
730
731 if (r) {
732 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100733 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100734
735 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100736}
737
738static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100739nc_send_server_hello(struct nc_session *session)
740{
741 int r, i;
742 const char **cpblts;
743
744 cpblts = create_cpblts(session->ctx);
745
746 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
747
Michal Vasko11d142a2016-01-19 15:58:24 +0100748 for (i = 0; cpblts[i]; ++i) {
749 lydict_remove(session->ctx, cpblts[i]);
750 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100751 free(cpblts);
752
753 if (r) {
754 return NC_MSG_ERROR;
755 }
756
757 return NC_MSG_HELLO;
758}
759
760static NC_MSG_TYPE
761nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200762{
763 struct lyxml_elem *xml = NULL, *node;
764 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
765 int ver = -1;
766 char *str;
767 long long int id;
768 int flag = 0;
769
Michal Vasko05ba9df2016-01-13 14:40:27 +0100770 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200771
772 switch(msgtype) {
773 case NC_MSG_HELLO:
774 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100775 LY_TREE_FOR(xml->child, node) {
776 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
777 continue;
778 } else if (!strcmp(node->name, "session-id")) {
779 if (!node->content || !strlen(node->content)) {
780 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200781 goto error;
782 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100783 str = NULL;
784 id = strtoll(node->content, &str, 10);
785 if (*str || id < 1 || id > UINT32_MAX) {
786 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200787 goto error;
788 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100789 session->id = (uint32_t)id;
790 continue;
791 } else if (strcmp(node->name, "capabilities")) {
792 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200793 goto error;
794 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100795
796 if (flag) {
797 /* multiple capabilities elements */
798 ERR("Invalid <hello> message (multiple <capabilities> elements).");
799 goto error;
800 }
801 flag = 1;
802
803 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
804 goto error;
805 }
806 session->version = ver;
807 }
808
809 if (!session->id) {
810 ERR("Missing <session-id> in server's <hello>.");
811 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200812 }
813 break;
814 case NC_MSG_ERROR:
815 /* nothing special, just pass it out */
816 break;
817 default:
818 ERR("Unexpected message received instead of <hello>.");
819 msgtype = NC_MSG_ERROR;
820 }
821
822 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100823 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200824
825 return msgtype;
826
827error:
828 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100829 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200830
831 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200832}
833
Michal Vasko11d142a2016-01-19 15:58:24 +0100834static NC_MSG_TYPE
835nc_recv_server_hello(struct nc_session *session)
836{
837 struct lyxml_elem *xml = NULL, *node;
838 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
839 int ver = -1;
840 int flag = 0;
841
Michal Vaskoadb850e2016-01-20 14:06:32 +0100842 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100843
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100844 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100845 case NC_MSG_HELLO:
846 /* get know NETCONF version */
847 LY_TREE_FOR(xml->child, node) {
848 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
849 continue;
850 } else if (strcmp(node->name, "capabilities")) {
851 ERR("Unexpected <%s> element in client's <hello>.", node->name);
852 msgtype = NC_MSG_ERROR;
853 goto cleanup;
854 }
855
856 if (flag) {
857 /* multiple capabilities elements */
858 ERR("Invalid <hello> message (multiple <capabilities> elements).");
859 msgtype = NC_MSG_ERROR;
860 goto cleanup;
861 }
862 flag = 1;
863
864 if ((ver = parse_cpblts(node, NULL)) < 0) {
865 msgtype = NC_MSG_ERROR;
866 goto cleanup;
867 }
868 session->version = ver;
869 }
870 break;
871 case NC_MSG_ERROR:
872 /* nothing special, just pass it out */
873 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100874 case NC_MSG_WOULDBLOCK:
875 ERR("Client's <hello> timeout elapsed.");
876 msgtype = NC_MSG_ERROR;
877 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100878 default:
879 ERR("Unexpected message received instead of <hello>.");
880 msgtype = NC_MSG_ERROR;
881 }
882
883cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +0100884 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100885
886 return msgtype;
887}
888
Michal Vasko80cad7f2015-12-08 14:42:27 +0100889int
Michal Vasko086311b2016-01-08 09:53:11 +0100890nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100891{
Michal Vasko086311b2016-01-08 09:53:11 +0100892 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100893
Michal Vasko11d142a2016-01-19 15:58:24 +0100894 if (session->side == NC_CLIENT) {
895 type = nc_send_client_hello(session);
896 } else {
897 type = nc_send_server_hello(session);
898 }
899
Michal Vasko086311b2016-01-08 09:53:11 +0100900 if (type != NC_MSG_HELLO) {
901 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100902 }
903
Michal Vasko11d142a2016-01-19 15:58:24 +0100904 if (session->side == NC_CLIENT) {
905 type = nc_recv_client_hello(session);
906 } else {
907 type = nc_recv_server_hello(session);
908 }
909
Michal Vasko086311b2016-01-08 09:53:11 +0100910 if (type != NC_MSG_HELLO) {
911 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100912 }
913
Michal Vasko086311b2016-01-08 09:53:11 +0100914 return 0;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100915}
Michal Vasko086311b2016-01-08 09:53:11 +0100916
Radek Krejci53691be2016-02-22 13:58:37 +0100917#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100918
Michal Vasko8f0c0282016-02-29 10:17:14 +0100919static void
Michal Vasko086311b2016-01-08 09:53:11 +0100920nc_ssh_init(void)
921{
922 ssh_threads_set_callbacks(ssh_threads_get_pthread());
923 ssh_init();
924 ssh_set_log_level(verbose_level);
925}
926
Michal Vasko8f0c0282016-02-29 10:17:14 +0100927static void
Michal Vasko086311b2016-01-08 09:53:11 +0100928nc_ssh_destroy(void)
929{
Michal Vasko8f0c0282016-02-29 10:17:14 +0100930 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +0100931 ENGINE_cleanup();
932 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +0100933 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +0100934 ssh_finalize();
935}
936
Radek Krejci53691be2016-02-22 13:58:37 +0100937#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100938
Radek Krejci53691be2016-02-22 13:58:37 +0100939#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100940
Michal Vaskof0c92c02016-01-29 09:41:45 +0100941struct CRYPTO_dynlock_value {
942 pthread_mutex_t lock;
943};
944
Michal Vaskof0c92c02016-01-29 09:41:45 +0100945static struct CRYPTO_dynlock_value *
946tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
947{
948 struct CRYPTO_dynlock_value *value;
949
950 value = malloc(sizeof *value);
951 if (!value) {
952 ERRMEM;
953 return NULL;
954 }
955 pthread_mutex_init(&value->lock, NULL);
956
957 return value;
958}
959
960static void
961tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
962{
963 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
964 * I found ignored this fact, what do I know... */
965 if (mode & CRYPTO_LOCK) {
966 pthread_mutex_lock(&l->lock);
967 } else {
968 pthread_mutex_unlock(&l->lock);
969 }
970}
971
972static void
973tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
974{
975 pthread_mutex_destroy(&l->lock);
976 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100977}
978
Michal Vasko8f0c0282016-02-29 10:17:14 +0100979#endif /* NC_ENABLED_TLS */
980
981#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
982
983static pthread_mutex_t *tls_locks;
984
985static void
986tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
987{
988 if (mode & CRYPTO_LOCK) {
989 pthread_mutex_lock(tls_locks + n);
990 } else {
991 pthread_mutex_unlock(tls_locks + n);
992 }
993}
994
995static void
996tls_thread_id_func(CRYPTO_THREADID *tid)
997{
998 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
999}
1000
1001static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001002nc_tls_init(void)
1003{
1004 int i;
1005
1006 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001007 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001008 SSL_library_init();
1009
1010 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001011 if (!tls_locks) {
1012 ERRMEM;
1013 return;
1014 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001015 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1016 pthread_mutex_init(tls_locks + i, NULL);
1017 }
1018
Michal Vaskof0c92c02016-01-29 09:41:45 +01001019 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001020 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001021
1022 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1023 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1024 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001025}
1026
Michal Vasko8f0c0282016-02-29 10:17:14 +01001027static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001028nc_tls_destroy(void)
1029{
1030 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001031
Michal Vasko8f0c0282016-02-29 10:17:14 +01001032 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001033 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001034 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001035 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001036 ERR_free_strings();
1037 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001038
Michal Vaskob6e37262016-02-25 14:49:00 +01001039 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001040 CRYPTO_set_locking_callback(NULL);
1041 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1042 pthread_mutex_destroy(tls_locks + i);
1043 }
1044 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001045
1046 CRYPTO_set_dynlock_create_callback(NULL);
1047 CRYPTO_set_dynlock_lock_callback(NULL);
1048 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001049}
1050
Michal Vasko8f0c0282016-02-29 10:17:14 +01001051#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001052
Radek Krejci53691be2016-02-22 13:58:37 +01001053#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001054
Michal Vasko8f0c0282016-02-29 10:17:14 +01001055static void
Michal Vasko5e228792016-02-03 15:30:24 +01001056nc_ssh_tls_init(void)
1057{
1058 SSL_load_error_strings();
1059 ERR_load_BIO_strings();
1060 SSL_library_init();
1061
1062 nc_ssh_init();
1063
1064 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1065 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1066 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1067}
1068
Michal Vasko8f0c0282016-02-29 10:17:14 +01001069static void
Michal Vasko5e228792016-02-03 15:30:24 +01001070nc_ssh_tls_destroy(void)
1071{
1072 ERR_free_strings();
1073 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1074
1075 nc_ssh_destroy();
1076
1077 CRYPTO_set_dynlock_create_callback(NULL);
1078 CRYPTO_set_dynlock_lock_callback(NULL);
1079 CRYPTO_set_dynlock_destroy_callback(NULL);
1080}
1081
Radek Krejci53691be2016-02-22 13:58:37 +01001082#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001083
1084#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1085
1086API void
1087nc_thread_destroy(void)
1088{
1089 CRYPTO_THREADID crypto_tid;
1090
1091 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1092 //CRYPTO_cleanup_all_ex_data();
1093
1094 CRYPTO_THREADID_current(&crypto_tid);
1095 ERR_remove_thread_state(&crypto_tid);
1096}
1097
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001098#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1099
1100void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001101nc_init(void)
1102{
1103#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1104 nc_ssh_tls_init();
1105#elif defined(NC_ENABLED_SSH)
1106 nc_ssh_init();
1107#elif defined(NC_ENABLED_TLS)
1108 nc_tls_init();
1109#endif
1110}
1111
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001112void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001113nc_destroy(void)
1114{
1115#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1116 nc_ssh_tls_destroy();
1117#elif defined(NC_ENABLED_SSH)
1118 nc_ssh_destroy();
1119#elif defined(NC_ENABLED_TLS)
1120 nc_tls_destroy();
1121#endif
1122}