blob: ab5351627f3fd39e4452b42aa0711f855f99421a [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 Vasko2e47ef92016-06-20 10:03:24 +0200468#define NC_CPBLT_BUF_LEN 512
469 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100470
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200471 if (!ctx) {
472 ERRARG("ctx");
473 return NULL;
474 }
475
Michal Vasko086311b2016-01-08 09:53:11 +0100476 yanglib = ly_ctx_info(ctx);
477 if (!yanglib) {
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200478 ERR("Failed to get ietf-yang-library data from the context.");
Michal Vasko086311b2016-01-08 09:53:11 +0100479 return NULL;
480 }
481
482 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100483 if (!cpblts) {
484 ERRMEM;
485 return NULL;
486 }
Michal Vasko086311b2016-01-08 09:53:11 +0100487 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
488 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
489 count = 2;
490
491 /* capabilities */
492
493 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
494 if (mod) {
495 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100496 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100497 }
498 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100499 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100500 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100501 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100502 }
503 }
504 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100505 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100506 }
507 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100508 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100509 }
510 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100511 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100512 }
513 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100514 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100515 }
516 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100517 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100518 }
519 }
520
521 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
522 if (mod) {
523 if (!server_opts.wd_basic_mode) {
524 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
525 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100526 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100527 switch (server_opts.wd_basic_mode) {
528 case NC_WD_ALL:
529 strcat(str, "?basic-mode=report-all");
530 break;
531 case NC_WD_TRIM:
532 strcat(str, "?basic-mode=trim");
533 break;
534 case NC_WD_EXPLICIT:
535 strcat(str, "?basic-mode=explicit");
536 break;
537 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100538 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100539 break;
540 }
541
542 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200543 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100544 if (server_opts.wd_also_supported & NC_WD_ALL) {
545 strcat(str, "report-all,");
546 }
547 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
548 strcat(str, "report-all-tagged,");
549 }
550 if (server_opts.wd_also_supported & NC_WD_TRIM) {
551 strcat(str, "trim,");
552 }
553 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
554 strcat(str, "explicit,");
555 }
556 str[strlen(str) - 1] = '\0';
557
558 add_cpblt(ctx, str, &cpblts, &size, &count);
559 }
560 }
561 }
562
Michal Vasko1a38c862016-01-15 15:50:07 +0100563 mod = ly_ctx_get_module(ctx, "nc-notifications", NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100564 if (mod) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100565 add_cpblt(ctx, "urn:ietf:params:netconf:capability:notification:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100566 if (server_opts.interleave_capab) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100567 add_cpblt(ctx, "urn:ietf:params:netconf:capability:interleave:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100568 }
569 }
570
571 /* models */
Michal Vasko086311b2016-01-08 09:53:11 +0100572 LY_TREE_FOR(yanglib->child, child) {
573 if (!strcmp(child->schema->name, "module")) {
574 LY_TREE_FOR(child->child, child2) {
575 if (!strcmp(child2->schema->name, "namespace")) {
576 ns = (struct lyd_node_leaf_list *)child2;
577 } else if (!strcmp(child2->schema->name, "name")) {
578 name = (struct lyd_node_leaf_list *)child2;
579 } else if (!strcmp(child2->schema->name, "revision")) {
580 rev = (struct lyd_node_leaf_list *)child2;
581 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100582 features = nc_realloc(features, ++feat_count * sizeof *features);
583 if (!features) {
584 ERRMEM;
585 free(cpblts);
586 return NULL;
587 }
Michal Vasko086311b2016-01-08 09:53:11 +0100588 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
589 }
590 }
591
592 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100593 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100594 continue;
595 }
596
Radek Krejcidf7ba522016-03-01 16:05:25 +0100597 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
Michal Vasko2e47ef92016-06-20 10:03:24 +0200598 rev->value_str[0] ? "&revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100599 if (feat_count) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200600 strcat(str, "&features=");
601 str_len += 10;
Michal Vasko086311b2016-01-08 09:53:11 +0100602 for (i = 0; i < feat_count; ++i) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200603 if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100604 ERRINT;
605 break;
606 }
Michal Vasko086311b2016-01-08 09:53:11 +0100607 if (i) {
608 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100609 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100610 }
611 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100612 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100613 }
614 }
615
616 add_cpblt(ctx, str, &cpblts, &size, &count);
617
618 ns = NULL;
619 name = NULL;
620 rev = NULL;
621 free(features);
622 features = NULL;
623 feat_count = 0;
624 }
625 }
626
627 lyd_free(yanglib);
628
629 /* ending NULL capability */
630 add_cpblt(ctx, NULL, &cpblts, &size, &count);
631
632 return cpblts;
633}
634
Radek Krejci695d4fa2015-10-22 13:23:54 +0200635static int
636parse_cpblts(struct lyxml_elem *xml, const char ***list)
637{
638 struct lyxml_elem *cpblt;
639 int ver = -1;
640 int i = 0;
641
642 if (list) {
643 /* get the storage for server's capabilities */
644 LY_TREE_FOR(xml->child, cpblt) {
645 i++;
646 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100647 /* last item remains NULL */
648 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200649 if (!*list) {
650 ERRMEM;
651 return -1;
652 }
653 i = 0;
654 }
655
656 LY_TREE_FOR(xml->child, cpblt) {
657 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
658 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
659 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
660 return -1;
661 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
662 continue;
663 }
664
665 /* detect NETCONF version */
666 if (ver < 0 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.0")) {
667 ver = 0;
668 } else if (ver < 1 && !strcmp(cpblt->content, "urn:ietf:params:netconf:base:1.1")) {
669 ver = 1;
670 }
671
672 /* store capabilities */
673 if (list) {
674 (*list)[i] = cpblt->content;
675 cpblt->content = NULL;
676 i++;
677 }
678 }
679
680 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100681 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200682 }
683
684 return ver;
685}
686
687static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100688nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +0100689{
690 int r, i;
691 const char **cpblts;
692
Michal Vasko11d142a2016-01-19 15:58:24 +0100693 /* client side hello - send only NETCONF base capabilities */
694 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100695 if (!cpblts) {
696 ERRMEM;
697 return NC_MSG_ERROR;
698 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100699 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
700 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
701 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100702
Michal Vasko11d142a2016-01-19 15:58:24 +0100703 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +0100704
Michal Vasko086311b2016-01-08 09:53:11 +0100705 for (i = 0; cpblts[i]; ++i) {
706 lydict_remove(session->ctx, cpblts[i]);
707 }
708 free(cpblts);
709
710 if (r) {
711 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +0100712 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100713
714 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +0100715}
716
717static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +0100718nc_send_server_hello(struct nc_session *session)
719{
720 int r, i;
721 const char **cpblts;
722
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200723 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +0100724
725 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
726
Michal Vasko11d142a2016-01-19 15:58:24 +0100727 for (i = 0; cpblts[i]; ++i) {
728 lydict_remove(session->ctx, cpblts[i]);
729 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100730 free(cpblts);
731
732 if (r) {
733 return NC_MSG_ERROR;
734 }
735
736 return NC_MSG_HELLO;
737}
738
739static NC_MSG_TYPE
740nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200741{
742 struct lyxml_elem *xml = NULL, *node;
743 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
744 int ver = -1;
745 char *str;
746 long long int id;
747 int flag = 0;
748
Michal Vasko05ba9df2016-01-13 14:40:27 +0100749 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200750
751 switch(msgtype) {
752 case NC_MSG_HELLO:
753 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +0100754 LY_TREE_FOR(xml->child, node) {
755 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
756 continue;
757 } else if (!strcmp(node->name, "session-id")) {
758 if (!node->content || !strlen(node->content)) {
759 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200760 goto error;
761 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100762 str = NULL;
763 id = strtoll(node->content, &str, 10);
764 if (*str || id < 1 || id > UINT32_MAX) {
765 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200766 goto error;
767 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100768 session->id = (uint32_t)id;
769 continue;
770 } else if (strcmp(node->name, "capabilities")) {
771 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200772 goto error;
773 }
Michal Vasko11d142a2016-01-19 15:58:24 +0100774
775 if (flag) {
776 /* multiple capabilities elements */
777 ERR("Invalid <hello> message (multiple <capabilities> elements).");
778 goto error;
779 }
780 flag = 1;
781
782 if ((ver = parse_cpblts(node, &session->cpblts)) < 0) {
783 goto error;
784 }
785 session->version = ver;
786 }
787
788 if (!session->id) {
789 ERR("Missing <session-id> in server's <hello>.");
790 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200791 }
792 break;
Michal Vasko71090fc2016-05-24 16:37:28 +0200793 case NC_MSG_WOULDBLOCK:
794 ERR("Server's <hello> timeout elapsed.");
795 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200796 case NC_MSG_ERROR:
797 /* nothing special, just pass it out */
798 break;
799 default:
800 ERR("Unexpected message received instead of <hello>.");
801 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200802 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200803 }
804
805 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100806 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200807
808 return msgtype;
809
810error:
811 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +0100812 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200813
814 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +0200815}
816
Michal Vasko11d142a2016-01-19 15:58:24 +0100817static NC_MSG_TYPE
818nc_recv_server_hello(struct nc_session *session)
819{
820 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +0200821 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +0100822 int ver = -1;
823 int flag = 0;
824
Michal Vaskoadb850e2016-01-20 14:06:32 +0100825 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : -1), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100826
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100827 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100828 case NC_MSG_HELLO:
829 /* get know NETCONF version */
830 LY_TREE_FOR(xml->child, node) {
831 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
832 continue;
833 } else if (strcmp(node->name, "capabilities")) {
834 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +0200835 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100836 goto cleanup;
837 }
838
839 if (flag) {
840 /* multiple capabilities elements */
841 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +0200842 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100843 goto cleanup;
844 }
845 flag = 1;
846
847 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200848 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +0100849 goto cleanup;
850 }
851 session->version = ver;
852 }
853 break;
854 case NC_MSG_ERROR:
855 /* nothing special, just pass it out */
856 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100857 case NC_MSG_WOULDBLOCK:
858 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +0100859 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100860 default:
861 ERR("Unexpected message received instead of <hello>.");
862 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +0200863 break;
Michal Vasko11d142a2016-01-19 15:58:24 +0100864 }
865
866cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +0100867 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +0100868
869 return msgtype;
870}
871
Michal Vasko71090fc2016-05-24 16:37:28 +0200872NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +0100873nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100874{
Michal Vasko086311b2016-01-08 09:53:11 +0100875 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100876
Michal Vasko11d142a2016-01-19 15:58:24 +0100877 if (session->side == NC_CLIENT) {
878 type = nc_send_client_hello(session);
879 } else {
880 type = nc_send_server_hello(session);
881 }
882
Michal Vasko086311b2016-01-08 09:53:11 +0100883 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +0200884 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100885 }
886
Michal Vasko11d142a2016-01-19 15:58:24 +0100887 if (session->side == NC_CLIENT) {
888 type = nc_recv_client_hello(session);
889 } else {
890 type = nc_recv_server_hello(session);
891 }
892
Michal Vasko71090fc2016-05-24 16:37:28 +0200893 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100894}
Michal Vasko086311b2016-01-08 09:53:11 +0100895
Radek Krejci53691be2016-02-22 13:58:37 +0100896#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +0100897
Michal Vasko8f0c0282016-02-29 10:17:14 +0100898static void
Michal Vasko086311b2016-01-08 09:53:11 +0100899nc_ssh_init(void)
900{
901 ssh_threads_set_callbacks(ssh_threads_get_pthread());
902 ssh_init();
903 ssh_set_log_level(verbose_level);
904}
905
Michal Vasko8f0c0282016-02-29 10:17:14 +0100906static void
Michal Vasko086311b2016-01-08 09:53:11 +0100907nc_ssh_destroy(void)
908{
Michal Vasko8f0c0282016-02-29 10:17:14 +0100909 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +0100910 ENGINE_cleanup();
911 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +0100912 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +0100913 ssh_finalize();
914}
915
Radek Krejci53691be2016-02-22 13:58:37 +0100916#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100917
Radek Krejci53691be2016-02-22 13:58:37 +0100918#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100919
Michal Vaskof0c92c02016-01-29 09:41:45 +0100920struct CRYPTO_dynlock_value {
921 pthread_mutex_t lock;
922};
923
Michal Vaskof0c92c02016-01-29 09:41:45 +0100924static struct CRYPTO_dynlock_value *
925tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
926{
927 struct CRYPTO_dynlock_value *value;
928
929 value = malloc(sizeof *value);
930 if (!value) {
931 ERRMEM;
932 return NULL;
933 }
934 pthread_mutex_init(&value->lock, NULL);
935
936 return value;
937}
938
939static void
940tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
941{
942 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
943 * I found ignored this fact, what do I know... */
944 if (mode & CRYPTO_LOCK) {
945 pthread_mutex_lock(&l->lock);
946 } else {
947 pthread_mutex_unlock(&l->lock);
948 }
949}
950
951static void
952tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
953{
954 pthread_mutex_destroy(&l->lock);
955 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100956}
957
Michal Vasko8f0c0282016-02-29 10:17:14 +0100958#endif /* NC_ENABLED_TLS */
959
960#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
961
962static pthread_mutex_t *tls_locks;
963
964static void
965tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
966{
967 if (mode & CRYPTO_LOCK) {
968 pthread_mutex_lock(tls_locks + n);
969 } else {
970 pthread_mutex_unlock(tls_locks + n);
971 }
972}
973
974static void
975tls_thread_id_func(CRYPTO_THREADID *tid)
976{
977 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
978}
979
980static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100981nc_tls_init(void)
982{
983 int i;
984
985 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100986 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100987 SSL_library_init();
988
989 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100990 if (!tls_locks) {
991 ERRMEM;
992 return;
993 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100994 for (i = 0; i < CRYPTO_num_locks(); ++i) {
995 pthread_mutex_init(tls_locks + i, NULL);
996 }
997
Michal Vaskof0c92c02016-01-29 09:41:45 +0100998 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +0100999 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001000
1001 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1002 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1003 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001004}
1005
Michal Vasko8f0c0282016-02-29 10:17:14 +01001006static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001007nc_tls_destroy(void)
1008{
1009 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001010
Michal Vasko8f0c0282016-02-29 10:17:14 +01001011 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001012 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001013 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001014 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001015 ERR_free_strings();
1016 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001017
Michal Vaskob6e37262016-02-25 14:49:00 +01001018 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001019 CRYPTO_set_locking_callback(NULL);
1020 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1021 pthread_mutex_destroy(tls_locks + i);
1022 }
1023 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001024
1025 CRYPTO_set_dynlock_create_callback(NULL);
1026 CRYPTO_set_dynlock_lock_callback(NULL);
1027 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001028}
1029
Michal Vasko8f0c0282016-02-29 10:17:14 +01001030#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001031
Radek Krejci53691be2016-02-22 13:58:37 +01001032#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001033
Michal Vasko8f0c0282016-02-29 10:17:14 +01001034static void
Michal Vasko5e228792016-02-03 15:30:24 +01001035nc_ssh_tls_init(void)
1036{
1037 SSL_load_error_strings();
1038 ERR_load_BIO_strings();
1039 SSL_library_init();
1040
1041 nc_ssh_init();
1042
1043 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1044 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1045 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
1046}
1047
Michal Vasko8f0c0282016-02-29 10:17:14 +01001048static void
Michal Vasko5e228792016-02-03 15:30:24 +01001049nc_ssh_tls_destroy(void)
1050{
1051 ERR_free_strings();
1052 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
1053
1054 nc_ssh_destroy();
1055
1056 CRYPTO_set_dynlock_create_callback(NULL);
1057 CRYPTO_set_dynlock_lock_callback(NULL);
1058 CRYPTO_set_dynlock_destroy_callback(NULL);
1059}
1060
Radek Krejci53691be2016-02-22 13:58:37 +01001061#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001062
1063#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1064
1065API void
1066nc_thread_destroy(void)
1067{
1068 CRYPTO_THREADID crypto_tid;
1069
1070 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1071 //CRYPTO_cleanup_all_ex_data();
1072
1073 CRYPTO_THREADID_current(&crypto_tid);
1074 ERR_remove_thread_state(&crypto_tid);
1075}
1076
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001077#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1078
1079void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001080nc_init(void)
1081{
1082#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1083 nc_ssh_tls_init();
1084#elif defined(NC_ENABLED_SSH)
1085 nc_ssh_init();
1086#elif defined(NC_ENABLED_TLS)
1087 nc_tls_init();
1088#endif
1089}
1090
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001091void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001092nc_destroy(void)
1093{
1094#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1095 nc_ssh_tls_destroy();
1096#elif defined(NC_ENABLED_SSH)
1097 nc_ssh_destroy();
1098#elif defined(NC_ENABLED_TLS)
1099 nc_tls_destroy();
1100#endif
1101}