blob: 813169fd1ab8321aa8276ba959e72db7ae0256f3 [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) {
Michal Vasko45e53ae2016-04-07 11:46:03 +020089 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +010090 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) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200100 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100101 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) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200111 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100112 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) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200122 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100123 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) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200133 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100134 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) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200144 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100145 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) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200155 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100156 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) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200166 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100167 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) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200177 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100178 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 Vasko45e53ae2016-04-07 11:46:03 +0200189 if (!session) {
190 ERRARG("session");
191 return NULL;
192 } else if (!capab) {
193 ERRARG("capab");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100194 return NULL;
195 }
196
Michal Vasko8dadf782016-01-15 10:29:36 +0100197 len = strlen(capab);
198 for (i = 0; session->cpblts[i]; ++i) {
199 if (!strncmp(session->cpblts[i], capab, len)) {
200 return session->cpblts[i];
201 }
202 }
203
204 return NULL;
205}
206
Michal Vasko2cc4c682016-03-01 09:16:48 +0100207API void
208nc_session_set_data(struct nc_session *session, void *data)
209{
210 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200211 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100212 return;
213 }
214
215 session->data = data;
216}
217
218API void *
219nc_session_get_data(const struct nc_session *session)
220{
221 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200222 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100223 return NULL;
224 }
225
226 return session->data;
227}
228
Michal Vasko086311b2016-01-08 09:53:11 +0100229NC_MSG_TYPE
230nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200231{
Michal Vasko086311b2016-01-08 09:53:11 +0100232 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200233
Michal Vasko086311b2016-01-08 09:53:11 +0100234 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100235 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
236 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100237 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100238 }
239
Michal Vasko086311b2016-01-08 09:53:11 +0100240 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
241
242 if (r) {
243 return NC_MSG_ERROR;
244 }
245
246 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100247}
248
Radek Krejci695d4fa2015-10-22 13:23:54 +0200249API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100250nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200251{
Michal Vasko9e99f012016-03-03 13:25:20 +0100252 int r, i, locked;
Michal Vasko428087d2016-01-14 16:04:28 +0100253 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100254 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100255 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100256 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100257 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100258 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200259 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100260 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200261 void *p;
262
Michal Vasko428087d2016-01-14 16:04:28 +0100263 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200264 return;
265 }
266
Michal Vasko86d357c2016-03-11 13:46:38 +0100267 /* stop notifications loop if any */
268 if (session->ntf_tid) {
269 tid = *session->ntf_tid;
270 free((pthread_t *)session->ntf_tid);
271 session->ntf_tid = NULL;
272 /* the thread now knows it should quit */
273
274 pthread_join(tid, NULL);
275 }
276
Michal Vaskoadd4c792015-10-26 15:36:58 +0100277 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100278 r = nc_timedlock(session->ti_lock, NC_READ_TIMEOUT * 1000);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100279 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100280 return;
Michal Vasko9e99f012016-03-03 13:25:20 +0100281 } else if (!r) {
282 /* we failed to lock it, too bad */
283 locked = 0;
284 } else {
285 locked = 1;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100286 }
Michal Vasko5ecbf8c2016-03-29 16:05:22 +0200287 } else {
288 ERRINT;
289 return;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200290 }
291
Michal Vasko9e99f012016-03-03 13:25:20 +0100292 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200293 /* cleanup message queues */
294 /* notifications */
Michal Vaskoad611702015-12-03 13:41:51 +0100295 for (contiter = session->notifs; contiter; ) {
296 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200297
Michal Vaskoad611702015-12-03 13:41:51 +0100298 p = contiter;
299 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200300 free(p);
301 }
302
303 /* rpc replies */
Michal Vaskoad611702015-12-03 13:41:51 +0100304 for (contiter = session->replies; contiter; ) {
305 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200306
Michal Vaskoad611702015-12-03 13:41:51 +0100307 p = contiter;
308 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200309 free(p);
310 }
311
312 /* send closing info to the other side */
313 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
314 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100315 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200316 } else {
317 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100318 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200319 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100320 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100321 case NC_MSG_REPLY:
322 LY_TREE_FOR(rpl->child, child) {
323 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
324 break;
325 }
326 }
327 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100328 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100329 }
Michal Vaskoad611702015-12-03 13:41:51 +0100330 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100331 break;
332 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100333 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100334 break;
335 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100336 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100337 break;
338 default:
339 /* cannot happen */
340 break;
341 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200342 }
343
344 /* list of server's capabilities */
345 if (session->cpblts) {
346 for (i = 0; session->cpblts[i]; i++) {
347 lydict_remove(session->ctx, session->cpblts[i]);
348 }
349 free(session->cpblts);
350 }
351 }
352
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100353 if (session->data && data_free) {
354 data_free(session->data);
355 }
356
Michal Vasko86d357c2016-03-11 13:46:38 +0100357 /* mark session for closing */
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 Vasko9e99f012016-03-03 13:25:20 +0100458 if (locked) {
459 pthread_mutex_unlock(session->ti_lock);
460 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100461 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100462 pthread_mutex_destroy(session->ti_lock);
463 free(session->ti_lock);
464 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200465 }
466
467 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100468 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200469 }
470
471 free(session);
472}
473
Michal Vasko086311b2016-01-08 09:53:11 +0100474static void
475add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
476{
477 if (*count == *size) {
478 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100479 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
480 if (!(*cpblts)) {
481 ERRMEM;
482 return;
483 }
Michal Vasko086311b2016-01-08 09:53:11 +0100484 }
485
486 if (capab) {
487 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
488 } else {
489 (*cpblts)[*count] = NULL;
490 }
491 ++(*count);
492}
493
494static const char **
495create_cpblts(struct ly_ctx *ctx)
496{
497 struct lyd_node *child, *child2, *yanglib;
498 struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL;
499 const char **cpblts;
500 const struct lys_module *mod;
Michal Vasko11d142a2016-01-19 15:58:24 +0100501 int size = 10, count, feat_count = 0, i, str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100502 char str[512];
503
504 yanglib = ly_ctx_info(ctx);
505 if (!yanglib) {
506 return NULL;
507 }
508
509 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100510 if (!cpblts) {
511 ERRMEM;
512 return NULL;
513 }
Michal Vasko086311b2016-01-08 09:53:11 +0100514 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
515 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
516 count = 2;
517
518 /* capabilities */
519
520 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
521 if (mod) {
522 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100523 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100524 }
525 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100526 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100527 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100528 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100529 }
530 }
531 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100532 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100533 }
534 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100535 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100536 }
537 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100538 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100539 }
540 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100541 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100542 }
543 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100544 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100545 }
546 }
547
548 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
549 if (mod) {
550 if (!server_opts.wd_basic_mode) {
551 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
552 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100553 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100554 switch (server_opts.wd_basic_mode) {
555 case NC_WD_ALL:
556 strcat(str, "?basic-mode=report-all");
557 break;
558 case NC_WD_TRIM:
559 strcat(str, "?basic-mode=trim");
560 break;
561 case NC_WD_EXPLICIT:
562 strcat(str, "?basic-mode=explicit");
563 break;
564 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100565 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100566 break;
567 }
568
569 if (server_opts.wd_also_supported) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100570 strcat(str, "&amp;also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100571 if (server_opts.wd_also_supported & NC_WD_ALL) {
572 strcat(str, "report-all,");
573 }
574 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
575 strcat(str, "report-all-tagged,");
576 }
577 if (server_opts.wd_also_supported & NC_WD_TRIM) {
578 strcat(str, "trim,");
579 }
580 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
581 strcat(str, "explicit,");
582 }
583 str[strlen(str) - 1] = '\0';
584
585 add_cpblt(ctx, str, &cpblts, &size, &count);
586 }
587 }
588 }
589
Michal Vasko1a38c862016-01-15 15:50:07 +0100590 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100591 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100592 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100593 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100594 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100595 }
596 }
597
598 /* models */
Michal Vasko086311b2016-01-08 09:53:11 +0100599 LY_TREE_FOR(yanglib->child, child) {
600 if (!strcmp(child->schema->name, "module")) {
601 LY_TREE_FOR(child->child, child2) {
602 if (!strcmp(child2->schema->name, "namespace")) {
603 ns = (struct lyd_node_leaf_list *)child2;
604 } else if (!strcmp(child2->schema->name, "name")) {
605 name = (struct lyd_node_leaf_list *)child2;
606 } else if (!strcmp(child2->schema->name, "revision")) {
607 rev = (struct lyd_node_leaf_list *)child2;
608 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100609 features = nc_realloc(features, ++feat_count * sizeof *features);
610 if (!features) {
611 ERRMEM;
612 free(cpblts);
613 return NULL;
614 }
Michal Vasko086311b2016-01-08 09:53:11 +0100615 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
616 }
617 }
618
619 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100620 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100621 continue;
622 }
623
Radek Krejcidf7ba522016-03-01 16:05:25 +0100624 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
625 rev->value_str[0] ? "&amp;revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100626 if (feat_count) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100627 strcat(str, "&amp;features=");
Michal Vasko11d142a2016-01-19 15:58:24 +0100628 str_len += 14;
Michal Vasko086311b2016-01-08 09:53:11 +0100629 for (i = 0; i < feat_count; ++i) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100630 if (str_len + 1 + strlen(features[i]->value_str) >= 512) {
631 ERRINT;
632 break;
633 }
Michal Vasko086311b2016-01-08 09:53:11 +0100634 if (i) {
635 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100636 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100637 }
638 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100639 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100640 }
641 }
642
643 add_cpblt(ctx, str, &cpblts, &size, &count);
644
645 ns = NULL;
646 name = NULL;
647 rev = NULL;
648 free(features);
649 features = NULL;
650 feat_count = 0;
651 }
652 }
653
654 lyd_free(yanglib);
655
656 /* ending NULL capability */
657 add_cpblt(ctx, NULL, &cpblts, &size, &count);
658
659 return cpblts;
660}
661
Radek Krejci695d4fa2015-10-22 13:23:54 +0200662static int
663parse_cpblts(struct lyxml_elem *xml, const char ***list)
664{
665 struct lyxml_elem *cpblt;
666 int ver = -1;
667 int i = 0;
668
669 if (list) {
670 /* get the storage for server's capabilities */
671 LY_TREE_FOR(xml->child, cpblt) {
672 i++;
673 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100674 /* last item remains NULL */
675 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200676 if (!*list) {
677 ERRMEM;
678 return -1;
679 }
680 i = 0;
681 }
682
683 LY_TREE_FOR(xml->child, cpblt) {
684 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
685 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
686 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
687 return -1;
688 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
689 continue;
690 }
691
692 /* detect NETCONF version */
693 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
694 ver = 0;
695 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
696 ver = 1;
697 }
698
699 /* store capabilities */
700 if (list) {
701 (*list)[i] = cpblt->content;
702 cpblt->content = NULL;
703 i++;
704 }
705 }
706
707 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100708 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200709 }
710
711 return ver;
712}
713
714static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100715nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100716{
717 int r, i;
718 const char **cpblts;
719
Michal Vasko11d142a2016-01-19 15:58:24 +0100720 /* client side hello - send only NETCONF base capabilities */
721 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100722 if (!cpblts) {
723 ERRMEM;
724 return NC_MSG_ERROR;
725 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100726 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
727 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
728 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100729
Michal Vasko11d142a2016-01-19 15:58:24 +0100730 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100731
Michal Vasko086311b2016-01-08 09:53:11 +0100732 for (i = 0; cpblts[i]; ++i) {
733 lydict_remove(session->ctx, cpblts[i]);
734 }
735 free(cpblts);
736
737 if (r) {
738 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100739 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100740
741 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100742}
743
744static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100745nc_send_server_hello(struct nc_session *session)
746{
747 int r, i;
748 const char **cpblts;
749
750 cpblts = create_cpblts(session->ctx);
751
752 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
753
Michal Vasko11d142a2016-01-19 15:58:24 +0100754 for (i = 0; cpblts[i]; ++i) {
755 lydict_remove(session->ctx, cpblts[i]);
756 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100757 free(cpblts);
758
759 if (r) {
760 return NC_MSG_ERROR;
761 }
762
763 return NC_MSG_HELLO;
764}
765
766static NC_MSG_TYPE
767nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200768{
769 struct lyxml_elem *xml = NULL, *node;
770 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
771 int ver = -1;
772 char *str;
773 long long int id;
774 int flag = 0;
775
Michal Vasko05ba9df2016-01-13 14:40:27 +0100776 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200777
778 switch(msgtype) {
779 case NC_MSG_HELLO:
780 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100781 LY_TREE_FOR(xml->child, node) {
782 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
783 continue;
784 } else if (!strcmp(node->name, "session-id")) {
785 if (!node->content || !strlen(node->content)) {
786 ERR("No 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 str = NULL;
790 id = strtoll(node->content, &str, 10);
791 if (*str || id < 1 || id > UINT32_MAX) {
792 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200793 goto error;
794 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100795 session->id = (uint32_t)id;
796 continue;
797 } else if (strcmp(node->name, "capabilities")) {
798 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200799 goto error;
800 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100801
802 if (flag) {
803 /* multiple capabilities elements */
804 ERR("Invalid <hello> message (multiple <capabilities> elements).");
805 goto error;
806 }
807 flag = 1;
808
809 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
810 goto error;
811 }
812 session->version = ver;
813 }
814
815 if (!session->id) {
816 ERR("Missing <session-id> in server's <hello>.");
817 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200818 }
819 break;
820 case NC_MSG_ERROR:
821 /* nothing special, just pass it out */
822 break;
823 default:
824 ERR("Unexpected message received instead of <hello>.");
825 msgtype = NC_MSG_ERROR;
826 }
827
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 msgtype;
832
833error:
834 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100835 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200836
837 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200838}
839
Michal Vasko11d142a2016-01-19 15:58:24 +0100840static NC_MSG_TYPE
841nc_recv_server_hello(struct nc_session *session)
842{
843 struct lyxml_elem *xml = NULL, *node;
844 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
845 int ver = -1;
846 int flag = 0;
847
Michal Vaskoadb850e2016-01-20 14:06:32 +0100848 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100849
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100850 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100851 case NC_MSG_HELLO:
852 /* get know NETCONF version */
853 LY_TREE_FOR(xml->child, node) {
854 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
855 continue;
856 } else if (strcmp(node->name, "capabilities")) {
857 ERR("Unexpected <%s> element in client's <hello>.", node->name);
858 msgtype = NC_MSG_ERROR;
859 goto cleanup;
860 }
861
862 if (flag) {
863 /* multiple capabilities elements */
864 ERR("Invalid <hello> message (multiple <capabilities> elements).");
865 msgtype = NC_MSG_ERROR;
866 goto cleanup;
867 }
868 flag = 1;
869
870 if ((ver = parse_cpblts(node, NULL)) < 0) {
871 msgtype = NC_MSG_ERROR;
872 goto cleanup;
873 }
874 session->version = ver;
875 }
876 break;
877 case NC_MSG_ERROR:
878 /* nothing special, just pass it out */
879 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100880 case NC_MSG_WOULDBLOCK:
881 ERR("Client's <hello> timeout elapsed.");
882 msgtype = NC_MSG_ERROR;
883 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100884 default:
885 ERR("Unexpected message received instead of <hello>.");
886 msgtype = NC_MSG_ERROR;
887 }
888
889cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +0100890 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100891
892 return msgtype;
893}
894
Michal Vasko80cad7f2015-12-08 14:42:27 +0100895int
Michal Vasko086311b2016-01-08 09:53:11 +0100896nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100897{
Michal Vasko086311b2016-01-08 09:53:11 +0100898 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100899
Michal Vasko11d142a2016-01-19 15:58:24 +0100900 if (session->side == NC_CLIENT) {
901 type = nc_send_client_hello(session);
902 } else {
903 type = nc_send_server_hello(session);
904 }
905
Michal Vasko086311b2016-01-08 09:53:11 +0100906 if (type != NC_MSG_HELLO) {
907 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100908 }
909
Michal Vasko11d142a2016-01-19 15:58:24 +0100910 if (session->side == NC_CLIENT) {
911 type = nc_recv_client_hello(session);
912 } else {
913 type = nc_recv_server_hello(session);
914 }
915
Michal Vasko086311b2016-01-08 09:53:11 +0100916 if (type != NC_MSG_HELLO) {
917 return 1;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100918 }
919
Michal Vasko086311b2016-01-08 09:53:11 +0100920 return 0;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100921}
Michal Vasko086311b2016-01-08 09:53:11 +0100922
Radek Krejci53691be2016-02-22 13:58:37 +0100923#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100924
Michal Vasko8f0c0282016-02-29 10:17:14 +0100925static void
Michal Vasko086311b2016-01-08 09:53:11 +0100926nc_ssh_init(void)
927{
928 ssh_threads_set_callbacks(ssh_threads_get_pthread());
929 ssh_init();
930 ssh_set_log_level(verbose_level);
931}
932
Michal Vasko8f0c0282016-02-29 10:17:14 +0100933static void
Michal Vasko086311b2016-01-08 09:53:11 +0100934nc_ssh_destroy(void)
935{
Michal Vasko8f0c0282016-02-29 10:17:14 +0100936 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +0100937 ENGINE_cleanup();
938 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +0100939 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +0100940 ssh_finalize();
941}
942
Radek Krejci53691be2016-02-22 13:58:37 +0100943#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100944
Radek Krejci53691be2016-02-22 13:58:37 +0100945#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100946
Michal Vaskof0c92c02016-01-29 09:41:45 +0100947struct CRYPTO_dynlock_value {
948 pthread_mutex_t lock;
949};
950
Michal Vaskof0c92c02016-01-29 09:41:45 +0100951static struct CRYPTO_dynlock_value *
952tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
953{
954 struct CRYPTO_dynlock_value *value;
955
956 value = malloc(sizeof *value);
957 if (!value) {
958 ERRMEM;
959 return NULL;
960 }
961 pthread_mutex_init(&value->lock, NULL);
962
963 return value;
964}
965
966static void
967tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
968{
969 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
970 * I found ignored this fact, what do I know... */
971 if (mode & CRYPTO_LOCK) {
972 pthread_mutex_lock(&l->lock);
973 } else {
974 pthread_mutex_unlock(&l->lock);
975 }
976}
977
978static void
979tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
980{
981 pthread_mutex_destroy(&l->lock);
982 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100983}
984
Michal Vasko8f0c0282016-02-29 10:17:14 +0100985#endif /* NC_ENABLED_TLS */
986
987#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
988
989static pthread_mutex_t *tls_locks;
990
991static void
992tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
993{
994 if (mode & CRYPTO_LOCK) {
995 pthread_mutex_lock(tls_locks + n);
996 } else {
997 pthread_mutex_unlock(tls_locks + n);
998 }
999}
1000
1001static void
1002tls_thread_id_func(CRYPTO_THREADID *tid)
1003{
1004 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1005}
1006
1007static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001008nc_tls_init(void)
1009{
1010 int i;
1011
1012 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001013 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001014 SSL_library_init();
1015
1016 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001017 if (!tls_locks) {
1018 ERRMEM;
1019 return;
1020 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001021 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1022 pthread_mutex_init(tls_locks + i, NULL);
1023 }
1024
Michal Vaskof0c92c02016-01-29 09:41:45 +01001025 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001026 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001027
1028 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1029 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1030 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001031}
1032
Michal Vasko8f0c0282016-02-29 10:17:14 +01001033static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001034nc_tls_destroy(void)
1035{
1036 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001037
Michal Vasko8f0c0282016-02-29 10:17:14 +01001038 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001039 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001040 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001041 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001042 ERR_free_strings();
1043 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001044
Michal Vaskob6e37262016-02-25 14:49:00 +01001045 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001046 CRYPTO_set_locking_callback(NULL);
1047 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1048 pthread_mutex_destroy(tls_locks + i);
1049 }
1050 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001051
1052 CRYPTO_set_dynlock_create_callback(NULL);
1053 CRYPTO_set_dynlock_lock_callback(NULL);
1054 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001055}
1056
Michal Vasko8f0c0282016-02-29 10:17:14 +01001057#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001058
Radek Krejci53691be2016-02-22 13:58:37 +01001059#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001060
Michal Vasko8f0c0282016-02-29 10:17:14 +01001061static void
Michal Vasko5e228792016-02-03 15:30:24 +01001062nc_ssh_tls_init(void)
1063{
1064 SSL_load_error_strings();
1065 ERR_load_BIO_strings();
1066 SSL_library_init();
1067
1068 nc_ssh_init();
1069
1070 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1071 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1072 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1073}
1074
Michal Vasko8f0c0282016-02-29 10:17:14 +01001075static void
Michal Vasko5e228792016-02-03 15:30:24 +01001076nc_ssh_tls_destroy(void)
1077{
1078 ERR_free_strings();
1079 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1080
1081 nc_ssh_destroy();
1082
1083 CRYPTO_set_dynlock_create_callback(NULL);
1084 CRYPTO_set_dynlock_lock_callback(NULL);
1085 CRYPTO_set_dynlock_destroy_callback(NULL);
1086}
1087
Radek Krejci53691be2016-02-22 13:58:37 +01001088#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001089
1090#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1091
1092API void
1093nc_thread_destroy(void)
1094{
1095 CRYPTO_THREADID crypto_tid;
1096
1097 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1098 //CRYPTO_cleanup_all_ex_data();
1099
1100 CRYPTO_THREADID_current(&crypto_tid);
1101 ERR_remove_thread_state(&crypto_tid);
1102}
1103
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001104#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1105
1106void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001107nc_init(void)
1108{
1109#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1110 nc_ssh_tls_init();
1111#elif defined(NC_ENABLED_SSH)
1112 nc_ssh_init();
1113#elif defined(NC_ENABLED_TLS)
1114 nc_tls_init();
1115#endif
1116}
1117
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001118void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001119nc_destroy(void)
1120{
1121#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1122 nc_ssh_tls_destroy();
1123#elif defined(NC_ENABLED_SSH)
1124 nc_ssh_destroy();
1125#elif defined(NC_ENABLED_TLS)
1126 nc_tls_destroy();
1127#endif
1128}