blob: 80ec526be6abb237d2fae5bf14ada42b31a58e7f [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 Vasko2cc4c682016-03-01 09:16:48 +0100173API void
174nc_session_set_data(struct nc_session *session, void *data)
175{
176 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200177 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100178 return;
179 }
180
181 session->data = data;
182}
183
184API void *
185nc_session_get_data(const struct nc_session *session)
186{
187 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200188 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100189 return NULL;
190 }
191
192 return session->data;
193}
194
Michal Vasko086311b2016-01-08 09:53:11 +0100195NC_MSG_TYPE
196nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200197{
Michal Vasko086311b2016-01-08 09:53:11 +0100198 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200199
Michal Vasko086311b2016-01-08 09:53:11 +0100200 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100201 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
202 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100203 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100204 }
205
Michal Vasko086311b2016-01-08 09:53:11 +0100206 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
207
208 if (r) {
209 return NC_MSG_ERROR;
210 }
211
212 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100213}
214
Radek Krejci695d4fa2015-10-22 13:23:54 +0200215API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100216nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200217{
Michal Vasko9e99f012016-03-03 13:25:20 +0100218 int r, i, locked;
Michal Vasko428087d2016-01-14 16:04:28 +0100219 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100220 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100221 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100222 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100223 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100224 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200225 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100226 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200227 void *p;
228
Michal Vasko428087d2016-01-14 16:04:28 +0100229 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200230 return;
231 }
232
Michal Vasko86d357c2016-03-11 13:46:38 +0100233 /* stop notifications loop if any */
234 if (session->ntf_tid) {
235 tid = *session->ntf_tid;
236 free((pthread_t *)session->ntf_tid);
237 session->ntf_tid = NULL;
238 /* the thread now knows it should quit */
239
240 pthread_join(tid, NULL);
241 }
242
Michal Vaskoadd4c792015-10-26 15:36:58 +0100243 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100244 r = nc_timedlock(session->ti_lock, NC_READ_TIMEOUT * 1000);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100245 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100246 return;
Michal Vasko9e99f012016-03-03 13:25:20 +0100247 } else if (!r) {
248 /* we failed to lock it, too bad */
249 locked = 0;
250 } else {
251 locked = 1;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100252 }
Michal Vasko5ecbf8c2016-03-29 16:05:22 +0200253 } else {
254 ERRINT;
255 return;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200256 }
257
Michal Vasko9e99f012016-03-03 13:25:20 +0100258 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200259 /* cleanup message queues */
260 /* notifications */
Michal Vaskoad611702015-12-03 13:41:51 +0100261 for (contiter = session->notifs; contiter; ) {
262 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200263
Michal Vaskoad611702015-12-03 13:41:51 +0100264 p = contiter;
265 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200266 free(p);
267 }
268
269 /* rpc replies */
Michal Vaskoad611702015-12-03 13:41:51 +0100270 for (contiter = session->replies; contiter; ) {
271 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200272
Michal Vaskoad611702015-12-03 13:41:51 +0100273 p = contiter;
274 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200275 free(p);
276 }
277
278 /* send closing info to the other side */
279 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
280 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100281 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200282 } else {
283 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100284 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200285 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100286 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100287 case NC_MSG_REPLY:
288 LY_TREE_FOR(rpl->child, child) {
289 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
290 break;
291 }
292 }
293 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100294 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100295 }
Michal Vaskoad611702015-12-03 13:41:51 +0100296 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100297 break;
298 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100299 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100300 break;
301 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100302 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100303 break;
304 default:
305 /* cannot happen */
306 break;
307 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200308 }
309
310 /* list of server's capabilities */
311 if (session->cpblts) {
312 for (i = 0; session->cpblts[i]; i++) {
313 lydict_remove(session->ctx, session->cpblts[i]);
314 }
315 free(session->cpblts);
316 }
317 }
318
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100319 if (session->data && data_free) {
320 data_free(session->data);
321 }
322
Michal Vasko86d357c2016-03-11 13:46:38 +0100323 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200324 session->status = NC_STATUS_CLOSING;
Michal Vasko428087d2016-01-14 16:04:28 +0100325 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200326
327 /* transport implementation cleanup */
328 switch (session->ti_type) {
329 case NC_TI_FD:
330 /* nothing needed - file descriptors were provided by caller,
331 * so it is up to the caller to close them correctly
332 * TODO use callbacks
333 */
Michal Vasko3512e402016-01-28 16:22:34 +0100334 /* just to avoid compiler warning */
335 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100336 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200337 break;
338
Radek Krejci53691be2016-02-22 13:58:37 +0100339#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200340 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100341 if (connected) {
342 ssh_channel_free(session->ti.libssh.channel);
343 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200344 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
345 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
346 * it.
347 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100348 multisession = 0;
349 if (session->ti.libssh.next) {
350 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
351 if (siter->status != NC_STATUS_STARTING) {
352 multisession = 1;
353 break;
354 }
355 }
356 }
357
358 if (!multisession) {
359 /* it's not multisession yet, but we still need to free the starting sessions */
360 if (session->ti.libssh.next) {
361 do {
362 siter = session->ti.libssh.next;
363 session->ti.libssh.next = siter->ti.libssh.next;
364
365 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100366 lydict_remove(session->ctx, session->username);
367 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100368 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100369 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100370 }
371
372 free(siter);
373 } while (session->ti.libssh.next != session);
374 }
Michal Vasko428087d2016-01-14 16:04:28 +0100375 if (connected) {
376 ssh_disconnect(session->ti.libssh.session);
377 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200378 ssh_free(session->ti.libssh.session);
379 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200380 /* remove the session from the list */
381 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100382 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200383 /* there will be only one session */
384 siter->ti.libssh.next = NULL;
385 } else {
386 /* there are still multiple sessions, keep the ring list */
387 siter->ti.libssh.next = session->ti.libssh.next;
388 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100389 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
390 if (session->flags & NC_SESSION_SSH_MSG_CB) {
391 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
392 if (siter->ti.libssh.next == session) {
393 ERRINT;
394 break;
395 }
396 }
397 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
398 siter->flags |= NC_SESSION_SSH_MSG_CB;
399 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200400 }
401 break;
402#endif
403
Radek Krejci53691be2016-02-22 13:58:37 +0100404#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200405 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100406 if (connected) {
407 SSL_shutdown(session->ti.tls);
408 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200409 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100410
411 X509_free(session->tls_cert);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200412 break;
413#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100414 case NC_TI_NONE:
415 ERRINT;
416 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200417 }
Michal Vasko428087d2016-01-14 16:04:28 +0100418
Radek Krejciac6d3472015-10-22 15:47:18 +0200419 lydict_remove(session->ctx, session->username);
420 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200421
422 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100423 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100424 if (locked) {
425 pthread_mutex_unlock(session->ti_lock);
426 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100427 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100428 pthread_mutex_destroy(session->ti_lock);
429 free(session->ti_lock);
430 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200431 }
432
433 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100434 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200435 }
436
437 free(session);
438}
439
Michal Vasko086311b2016-01-08 09:53:11 +0100440static void
441add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
442{
443 if (*count == *size) {
444 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100445 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
446 if (!(*cpblts)) {
447 ERRMEM;
448 return;
449 }
Michal Vasko086311b2016-01-08 09:53:11 +0100450 }
451
452 if (capab) {
453 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
454 } else {
455 (*cpblts)[*count] = NULL;
456 }
457 ++(*count);
458}
459
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200460API const char **
461nc_server_get_cpblts(struct ly_ctx *ctx)
Michal Vasko086311b2016-01-08 09:53:11 +0100462{
463 struct lyd_node *child, *child2, *yanglib;
464 struct lyd_node_leaf_list **features = NULL, *ns = NULL, *rev = NULL, *name = NULL;
465 const char **cpblts;
466 const struct lys_module *mod;
Michal Vasko11d142a2016-01-19 15:58:24 +0100467 int size = 10, count, feat_count = 0, i, str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100468 char str[512];
469
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200470 if (!ctx) {
471 ERRARG("ctx");
472 return NULL;
473 }
474
Michal Vasko086311b2016-01-08 09:53:11 +0100475 yanglib = ly_ctx_info(ctx);
476 if (!yanglib) {
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200477 ERR("Failed to get ietf-yang-library data from the context.");
Michal Vasko086311b2016-01-08 09:53:11 +0100478 return NULL;
479 }
480
481 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100482 if (!cpblts) {
483 ERRMEM;
484 return NULL;
485 }
Michal Vasko086311b2016-01-08 09:53:11 +0100486 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
487 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
488 count = 2;
489
490 /* capabilities */
491
492 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
493 if (mod) {
494 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100495 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100496 }
497 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100498 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100499 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100500 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100501 }
502 }
503 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100504 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100505 }
506 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100507 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100508 }
509 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100510 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100511 }
512 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100513 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100514 }
515 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100516 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100517 }
518 }
519
520 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
521 if (mod) {
522 if (!server_opts.wd_basic_mode) {
523 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
524 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100525 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100526 switch (server_opts.wd_basic_mode) {
527 case NC_WD_ALL:
528 strcat(str, "?basic-mode=report-all");
529 break;
530 case NC_WD_TRIM:
531 strcat(str, "?basic-mode=trim");
532 break;
533 case NC_WD_EXPLICIT:
534 strcat(str, "?basic-mode=explicit");
535 break;
536 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100537 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100538 break;
539 }
540
541 if (server_opts.wd_also_supported) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100542 strcat(str, "&amp;also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100543 if (server_opts.wd_also_supported & NC_WD_ALL) {
544 strcat(str, "report-all,");
545 }
546 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
547 strcat(str, "report-all-tagged,");
548 }
549 if (server_opts.wd_also_supported & NC_WD_TRIM) {
550 strcat(str, "trim,");
551 }
552 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
553 strcat(str, "explicit,");
554 }
555 str[strlen(str) - 1] = '\0';
556
557 add_cpblt(ctx, str, &cpblts, &size, &count);
558 }
559 }
560 }
561
Michal Vasko1a38c862016-01-15 15:50:07 +0100562 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100563 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100564 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100565 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100566 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100567 }
568 }
569
570 /* models */
Michal Vasko086311b2016-01-08 09:53:11 +0100571 LY_TREE_FOR(yanglib->child, child) {
572 if (!strcmp(child->schema->name, "module")) {
573 LY_TREE_FOR(child->child, child2) {
574 if (!strcmp(child2->schema->name, "namespace")) {
575 ns = (struct lyd_node_leaf_list *)child2;
576 } else if (!strcmp(child2->schema->name, "name")) {
577 name = (struct lyd_node_leaf_list *)child2;
578 } else if (!strcmp(child2->schema->name, "revision")) {
579 rev = (struct lyd_node_leaf_list *)child2;
580 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100581 features = nc_realloc(features, ++feat_count * sizeof *features);
582 if (!features) {
583 ERRMEM;
584 free(cpblts);
585 return NULL;
586 }
Michal Vasko086311b2016-01-08 09:53:11 +0100587 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
588 }
589 }
590
591 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100592 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100593 continue;
594 }
595
Radek Krejcidf7ba522016-03-01 16:05:25 +0100596 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
597 rev->value_str[0] ? "&amp;revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100598 if (feat_count) {
Radek Krejcif9b28322016-01-08 14:56:43 +0100599 strcat(str, "&amp;features=");
Michal Vasko11d142a2016-01-19 15:58:24 +0100600 str_len += 14;
Michal Vasko086311b2016-01-08 09:53:11 +0100601 for (i = 0; i < feat_count; ++i) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100602 if (str_len + 1 + strlen(features[i]->value_str) >= 512) {
603 ERRINT;
604 break;
605 }
Michal Vasko086311b2016-01-08 09:53:11 +0100606 if (i) {
607 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100608 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100609 }
610 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100611 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100612 }
613 }
614
615 add_cpblt(ctx, str, &cpblts, &size, &count);
616
617 ns = NULL;
618 name = NULL;
619 rev = NULL;
620 free(features);
621 features = NULL;
622 feat_count = 0;
623 }
624 }
625
626 lyd_free(yanglib);
627
628 /* ending NULL capability */
629 add_cpblt(ctx, NULL, &cpblts, &size, &count);
630
631 return cpblts;
632}
633
Radek Krejci695d4fa2015-10-22 13:23:54 +0200634static int
635parse_cpblts(struct lyxml_elem *xml, const char ***list)
636{
637 struct lyxml_elem *cpblt;
638 int ver = -1;
639 int i = 0;
640
641 if (list) {
642 /* get the storage for server's capabilities */
643 LY_TREE_FOR(xml->child, cpblt) {
644 i++;
645 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100646 /* last item remains NULL */
647 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200648 if (!*list) {
649 ERRMEM;
650 return -1;
651 }
652 i = 0;
653 }
654
655 LY_TREE_FOR(xml->child, cpblt) {
656 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
657 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
658 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
659 return -1;
660 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
661 continue;
662 }
663
664 /* detect NETCONF version */
665 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
666 ver = 0;
667 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
668 ver = 1;
669 }
670
671 /* store capabilities */
672 if (list) {
673 (*list)[i] = cpblt->content;
674 cpblt->content = NULL;
675 i++;
676 }
677 }
678
679 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100680 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200681 }
682
683 return ver;
684}
685
686static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100687nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100688{
689 int r, i;
690 const char **cpblts;
691
Michal Vasko11d142a2016-01-19 15:58:24 +0100692 /* client side hello - send only NETCONF base capabilities */
693 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100694 if (!cpblts) {
695 ERRMEM;
696 return NC_MSG_ERROR;
697 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100698 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
699 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
700 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100701
Michal Vasko11d142a2016-01-19 15:58:24 +0100702 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100703
Michal Vasko086311b2016-01-08 09:53:11 +0100704 for (i = 0; cpblts[i]; ++i) {
705 lydict_remove(session->ctx, cpblts[i]);
706 }
707 free(cpblts);
708
709 if (r) {
710 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100711 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100712
713 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100714}
715
716static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100717nc_send_server_hello(struct nc_session *session)
718{
719 int r, i;
720 const char **cpblts;
721
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200722 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +0100723
724 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
725
Michal Vasko11d142a2016-01-19 15:58:24 +0100726 for (i = 0; cpblts[i]; ++i) {
727 lydict_remove(session->ctx, cpblts[i]);
728 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100729 free(cpblts);
730
731 if (r) {
732 return NC_MSG_ERROR;
733 }
734
735 return NC_MSG_HELLO;
736}
737
738static NC_MSG_TYPE
739nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200740{
741 struct lyxml_elem *xml = NULL, *node;
742 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
743 int ver = -1;
744 char *str;
745 long long int id;
746 int flag = 0;
747
Michal Vasko05ba9df2016-01-13 14:40:27 +0100748 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200749
750 switch(msgtype) {
751 case NC_MSG_HELLO:
752 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100753 LY_TREE_FOR(xml->child, node) {
754 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
755 continue;
756 } else if (!strcmp(node->name, "session-id")) {
757 if (!node->content || !strlen(node->content)) {
758 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200759 goto error;
760 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100761 str = NULL;
762 id = strtoll(node->content, &str, 10);
763 if (*str || id < 1 || id > UINT32_MAX) {
764 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200765 goto error;
766 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100767 session->id = (uint32_t)id;
768 continue;
769 } else if (strcmp(node->name, "capabilities")) {
770 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200771 goto error;
772 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100773
774 if (flag) {
775 /* multiple capabilities elements */
776 ERR("Invalid <hello> message (multiple <capabilities> elements).");
777 goto error;
778 }
779 flag = 1;
780
781 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
782 goto error;
783 }
784 session->version = ver;
785 }
786
787 if (!session->id) {
788 ERR("Missing <session-id> in server's <hello>.");
789 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200790 }
791 break;
Michal Vasko71090fc2016-05-24 16:37:28 +0200792 case NC_MSG_WOULDBLOCK:
793 ERR("Server's <hello> timeout elapsed.");
794 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200795 case NC_MSG_ERROR:
796 /* nothing special, just pass it out */
797 break;
798 default:
799 ERR("Unexpected message received instead of <hello>.");
800 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200801 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200802 }
803
804 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100805 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200806
807 return msgtype;
808
809error:
810 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100811 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200812
813 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200814}
815
Michal Vasko11d142a2016-01-19 15:58:24 +0100816static NC_MSG_TYPE
817nc_recv_server_hello(struct nc_session *session)
818{
819 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +0200820 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +0100821 int ver = -1;
822 int flag = 0;
823
Michal Vaskoadb850e2016-01-20 14:06:32 +0100824 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100825
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100826 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100827 case NC_MSG_HELLO:
828 /* get know NETCONF version */
829 LY_TREE_FOR(xml->child, node) {
830 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
831 continue;
832 } else if (strcmp(node->name, "capabilities")) {
833 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +0200834 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100835 goto cleanup;
836 }
837
838 if (flag) {
839 /* multiple capabilities elements */
840 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +0200841 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100842 goto cleanup;
843 }
844 flag = 1;
845
846 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200847 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100848 goto cleanup;
849 }
850 session->version = ver;
851 }
852 break;
853 case NC_MSG_ERROR:
854 /* nothing special, just pass it out */
855 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100856 case NC_MSG_WOULDBLOCK:
857 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100858 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100859 default:
860 ERR("Unexpected message received instead of <hello>.");
861 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200862 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100863 }
864
865cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +0100866 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100867
868 return msgtype;
869}
870
Michal Vasko71090fc2016-05-24 16:37:28 +0200871NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +0100872nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100873{
Michal Vasko086311b2016-01-08 09:53:11 +0100874 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100875
Michal Vasko11d142a2016-01-19 15:58:24 +0100876 if (session->side == NC_CLIENT) {
877 type = nc_send_client_hello(session);
878 } else {
879 type = nc_send_server_hello(session);
880 }
881
Michal Vasko086311b2016-01-08 09:53:11 +0100882 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200883 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100884 }
885
Michal Vasko11d142a2016-01-19 15:58:24 +0100886 if (session->side == NC_CLIENT) {
887 type = nc_recv_client_hello(session);
888 } else {
889 type = nc_recv_server_hello(session);
890 }
891
Michal Vasko71090fc2016-05-24 16:37:28 +0200892 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100893}
Michal Vasko086311b2016-01-08 09:53:11 +0100894
Radek Krejci53691be2016-02-22 13:58:37 +0100895#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100896
Michal Vasko8f0c0282016-02-29 10:17:14 +0100897static void
Michal Vasko086311b2016-01-08 09:53:11 +0100898nc_ssh_init(void)
899{
900 ssh_threads_set_callbacks(ssh_threads_get_pthread());
901 ssh_init();
902 ssh_set_log_level(verbose_level);
903}
904
Michal Vasko8f0c0282016-02-29 10:17:14 +0100905static void
Michal Vasko086311b2016-01-08 09:53:11 +0100906nc_ssh_destroy(void)
907{
Michal Vasko8f0c0282016-02-29 10:17:14 +0100908 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +0100909 ENGINE_cleanup();
910 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +0100911 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +0100912 ssh_finalize();
913}
914
Radek Krejci53691be2016-02-22 13:58:37 +0100915#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100916
Radek Krejci53691be2016-02-22 13:58:37 +0100917#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100918
Michal Vaskof0c92c02016-01-29 09:41:45 +0100919struct CRYPTO_dynlock_value {
920 pthread_mutex_t lock;
921};
922
Michal Vaskof0c92c02016-01-29 09:41:45 +0100923static struct CRYPTO_dynlock_value *
924tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
925{
926 struct CRYPTO_dynlock_value *value;
927
928 value = malloc(sizeof *value);
929 if (!value) {
930 ERRMEM;
931 return NULL;
932 }
933 pthread_mutex_init(&value->lock, NULL);
934
935 return value;
936}
937
938static void
939tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
940{
941 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
942 * I found ignored this fact, what do I know... */
943 if (mode & CRYPTO_LOCK) {
944 pthread_mutex_lock(&l->lock);
945 } else {
946 pthread_mutex_unlock(&l->lock);
947 }
948}
949
950static void
951tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
952{
953 pthread_mutex_destroy(&l->lock);
954 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100955}
956
Michal Vasko8f0c0282016-02-29 10:17:14 +0100957#endif /* NC_ENABLED_TLS */
958
959#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
960
961static pthread_mutex_t *tls_locks;
962
963static void
964tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
965{
966 if (mode & CRYPTO_LOCK) {
967 pthread_mutex_lock(tls_locks + n);
968 } else {
969 pthread_mutex_unlock(tls_locks + n);
970 }
971}
972
973static void
974tls_thread_id_func(CRYPTO_THREADID *tid)
975{
976 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
977}
978
979static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100980nc_tls_init(void)
981{
982 int i;
983
984 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100985 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100986 SSL_library_init();
987
988 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100989 if (!tls_locks) {
990 ERRMEM;
991 return;
992 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100993 for (i = 0; i < CRYPTO_num_locks(); ++i) {
994 pthread_mutex_init(tls_locks + i, NULL);
995 }
996
Michal Vaskof0c92c02016-01-29 09:41:45 +0100997 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100998 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +0100999
1000 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1001 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1002 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001003}
1004
Michal Vasko8f0c0282016-02-29 10:17:14 +01001005static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001006nc_tls_destroy(void)
1007{
1008 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001009
Michal Vasko8f0c0282016-02-29 10:17:14 +01001010 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001011 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001012 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001013 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001014 ERR_free_strings();
1015 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001016
Michal Vaskob6e37262016-02-25 14:49:00 +01001017 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001018 CRYPTO_set_locking_callback(NULL);
1019 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1020 pthread_mutex_destroy(tls_locks + i);
1021 }
1022 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001023
1024 CRYPTO_set_dynlock_create_callback(NULL);
1025 CRYPTO_set_dynlock_lock_callback(NULL);
1026 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001027}
1028
Michal Vasko8f0c0282016-02-29 10:17:14 +01001029#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001030
Radek Krejci53691be2016-02-22 13:58:37 +01001031#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001032
Michal Vasko8f0c0282016-02-29 10:17:14 +01001033static void
Michal Vasko5e228792016-02-03 15:30:24 +01001034nc_ssh_tls_init(void)
1035{
1036 SSL_load_error_strings();
1037 ERR_load_BIO_strings();
1038 SSL_library_init();
1039
1040 nc_ssh_init();
1041
1042 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1043 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1044 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1045}
1046
Michal Vasko8f0c0282016-02-29 10:17:14 +01001047static void
Michal Vasko5e228792016-02-03 15:30:24 +01001048nc_ssh_tls_destroy(void)
1049{
1050 ERR_free_strings();
1051 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1052
1053 nc_ssh_destroy();
1054
1055 CRYPTO_set_dynlock_create_callback(NULL);
1056 CRYPTO_set_dynlock_lock_callback(NULL);
1057 CRYPTO_set_dynlock_destroy_callback(NULL);
1058}
1059
Radek Krejci53691be2016-02-22 13:58:37 +01001060#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001061
1062#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1063
1064API void
1065nc_thread_destroy(void)
1066{
1067 CRYPTO_THREADID crypto_tid;
1068
1069 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1070 //CRYPTO_cleanup_all_ex_data();
1071
1072 CRYPTO_THREADID_current(&crypto_tid);
1073 ERR_remove_thread_state(&crypto_tid);
1074}
1075
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001076#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1077
1078void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001079nc_init(void)
1080{
1081#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1082 nc_ssh_tls_init();
1083#elif defined(NC_ENABLED_SSH)
1084 nc_ssh_init();
1085#elif defined(NC_ENABLED_TLS)
1086 nc_tls_init();
1087#endif
1088}
1089
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001090void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001091nc_destroy(void)
1092{
1093#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1094 nc_ssh_tls_destroy();
1095#elif defined(NC_ENABLED_SSH)
1096 nc_ssh_destroy();
1097#elif defined(NC_ENABLED_TLS)
1098 nc_tls_destroy();
1099#endif
1100}