blob: 0b2db507913ba762490ef58a1e897ad499651739 [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 *
Michal Vaskob85767d2018-03-21 12:21:10 +01006 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
Radek Krejci206fcd62015-10-07 15:42:48 +02007 *
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 */
Michal Vaskob85767d2018-03-21 12:21:10 +010014#define _DEFAULT_SOURCE
Radek Krejci206fcd62015-10-07 15:42:48 +020015
Michal Vasko18aeb5d2017-02-17 09:23:56 +010016#include <assert.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020017#include <errno.h>
Radek Krejci952eb862016-01-08 14:22:55 +010018#include <stdlib.h>
Michal Vasko3512e402016-01-28 16:22:34 +010019#include <string.h>
Michal Vaskob85767d2018-03-21 12:21:10 +010020#include <unistd.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020021#include <pthread.h>
Radek Krejcid6b73e12016-07-15 12:00:23 +020022#include <sys/time.h>
Michal Vasko58f31552016-01-19 12:39:15 +010023#include <time.h>
Michal Vasko156d3272017-04-11 11:46:49 +020024#include <ctype.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020025#include <libyang/libyang.h>
26
Michal Vasko5e228792016-02-03 15:30:24 +010027#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010028#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010029#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010030
Radek Krejci53691be2016-02-22 13:58:37 +010031#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020032
Michal Vasko086311b2016-01-08 09:53:11 +010033# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020034
Radek Krejci53691be2016-02-22 13:58:37 +010035#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020036
Radek Krejci53691be2016-02-22 13:58:37 +010037#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010038
Michal Vasko5e228792016-02-03 15:30:24 +010039# include <openssl/engine.h>
40# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010041# include <openssl/err.h>
42
Radek Krejci53691be2016-02-22 13:58:37 +010043#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010044
Michal Vasko086311b2016-01-08 09:53:11 +010045/* in seconds */
46#define NC_CLIENT_HELLO_TIMEOUT 60
fanchanghu75888b62017-08-01 19:51:20 +080047#define NC_SERVER_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020048
Michal Vasko05ba9df2016-01-13 14:40:27 +010049/* in milliseconds */
50#define NC_CLOSE_REPLY_TIMEOUT 200
51
Michal Vasko086311b2016-01-08 09:53:11 +010052extern struct nc_server_opts server_opts;
53
Radek Krejci7ac16052016-07-15 11:48:18 +020054int
Michal Vasko77a6abe2017-10-05 10:02:20 +020055nc_gettimespec_mono(struct timespec *ts)
56{
57#ifdef CLOCK_MONOTONIC_RAW
58 return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
59#elif defined(CLOCK_MONOTONIC)
60 return clock_gettime(CLOCK_MONOTONIC, ts);
61#else
62 /* no monotonic clock available, return realtime */
63 return nc_gettimespec_real(ts);
64#endif
65}
66
67int
68nc_gettimespec_real(struct timespec *ts)
Radek Krejci7ac16052016-07-15 11:48:18 +020069{
Michal Vasko0ef46df2016-09-21 14:03:18 +020070#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020071 return clock_gettime(CLOCK_REALTIME, ts);
72#else
73 int rc;
74 struct timeval tv;
75
76 rc = gettimeofday(&tv, NULL);
77 if (!rc) {
78 ts->tv_sec = (time_t)tv.tv_sec;
79 ts->tv_nsec = 1000L * (long)tv.tv_usec;
80 }
81 return rc;
82#endif
83}
84
Michal Vasko36c7be82017-02-22 13:37:59 +010085/* ts1 < ts2 -> +, ts1 > ts2 -> -, returns milliseconds */
86int32_t
Radek Krejcida0afb22017-05-26 16:25:59 +020087nc_difftimespec(const struct timespec *ts1, const struct timespec *ts2)
Michal Vasko99f251b2017-01-11 11:31:46 +010088{
Michal Vasko36c7be82017-02-22 13:37:59 +010089 int64_t nsec_diff = 0;
Michal Vasko99f251b2017-01-11 11:31:46 +010090
Michal Vaskoa82bd202017-03-03 14:07:29 +010091 nsec_diff += (((int64_t)ts2->tv_sec) - ((int64_t)ts1->tv_sec)) * 1000000000L;
92 nsec_diff += ((int64_t)ts2->tv_nsec) - ((int64_t)ts1->tv_nsec);
Michal Vasko99f251b2017-01-11 11:31:46 +010093
94 return (nsec_diff ? nsec_diff / 1000000L : 0);
95}
96
Michal Vasko36c7be82017-02-22 13:37:59 +010097void
98nc_addtimespec(struct timespec *ts, uint32_t msec)
99{
Michal Vasko81c5b302017-03-15 12:10:40 +0100100 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
101
Michal Vasko0dfcd332017-03-03 13:14:39 +0100102 ts->tv_sec += msec / 1000;
103 ts->tv_nsec += (msec % 1000) * 1000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100104
Michal Vasko81c5b302017-03-15 12:10:40 +0100105 if (ts->tv_nsec >= 1000000000L) {
106 ++ts->tv_sec;
107 ts->tv_nsec -= 1000000000L;
108 } else if (ts->tv_nsec < 0) {
109 --ts->tv_sec;
110 ts->tv_nsec += 1000000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100111 }
Michal Vasko81c5b302017-03-15 12:10:40 +0100112
113 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
Michal Vasko36c7be82017-02-22 13:37:59 +0100114}
115
Radek Krejci28472922016-07-15 11:51:16 +0200116#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
117int
118pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
119{
Michal Vasko81c5b302017-03-15 12:10:40 +0100120 int32_t diff;
Radek Krejci28472922016-07-15 11:51:16 +0200121 int rc;
122 struct timespec cur, dur;
123
124 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
125 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
Michal Vasko145619f2018-02-16 10:08:45 +0100126 nc_gettimespec_real(&cur);
Radek Krejci28472922016-07-15 11:51:16 +0200127
Michal Vasko81c5b302017-03-15 12:10:40 +0100128 if ((diff = nc_difftimespec(&cur, abstime)) < 1) {
129 /* timeout */
Radek Krejci28472922016-07-15 11:51:16 +0200130 break;
Michal Vasko81c5b302017-03-15 12:10:40 +0100131 } else if (diff < 5) {
132 /* sleep until timeout */
133 dur = *abstime;
134 } else {
135 /* sleep 5 ms */
Radek Krejci28472922016-07-15 11:51:16 +0200136 dur.tv_sec = 0;
137 dur.tv_nsec = 5000000;
138 }
139
140 nanosleep(&dur, NULL);
141 }
142
143 return rc;
144}
145#endif
146
Michal Vaskoade892d2017-02-22 13:40:35 +0100147struct nc_session *
148nc_new_session(int not_allocate_ti)
149{
150 struct nc_session *sess;
151
152 sess = calloc(1, sizeof *sess);
153 if (!sess) {
154 return NULL;
155 }
156
157 if (!not_allocate_ti) {
158 sess->ti_lock = malloc(sizeof *sess->ti_lock);
159 sess->ti_cond = malloc(sizeof *sess->ti_cond);
160 sess->ti_inuse = malloc(sizeof *sess->ti_inuse);
161 if (!sess->ti_lock || !sess->ti_cond || !sess->ti_inuse) {
162 free(sess->ti_lock);
163 free(sess->ti_cond);
164 free((int *)sess->ti_inuse);
165 free(sess);
166 return NULL;
167 }
168 }
169
170 return sess;
171}
172
Michal Vasko96164bf2016-01-21 15:41:58 +0100173/*
174 * @return 1 - success
175 * 0 - timeout
176 * -1 - error
177 */
178int
Michal Vaskoade892d2017-02-22 13:40:35 +0100179nc_session_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100180{
181 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100182 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100183
184 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200185 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100186 nc_addtimespec(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100187
Michal Vaskoade892d2017-02-22 13:40:35 +0100188 /* LOCK */
189 ret = pthread_mutex_timedlock(session->ti_lock, &ts_timeout);
190 if (!ret) {
191 while (*session->ti_inuse) {
192 ret = pthread_cond_timedwait(session->ti_cond, session->ti_lock, &ts_timeout);
193 if (ret) {
194 pthread_mutex_unlock(session->ti_lock);
195 break;
196 }
197 }
198 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100199 } else if (!timeout) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100200 if (*session->ti_inuse) {
201 /* immediate timeout */
202 return 0;
203 }
204
205 /* LOCK */
206 ret = pthread_mutex_trylock(session->ti_lock);
207 if (!ret) {
208 /* be extra careful, someone could have been faster */
209 if (*session->ti_inuse) {
210 pthread_mutex_unlock(session->ti_lock);
211 return 0;
212 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200213 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100214 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100215 /* LOCK */
216 ret = pthread_mutex_lock(session->ti_lock);
217 if (!ret) {
218 while (*session->ti_inuse) {
219 ret = pthread_cond_wait(session->ti_cond, session->ti_lock);
220 if (ret) {
221 pthread_mutex_unlock(session->ti_lock);
222 break;
223 }
224 }
225 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100226 }
227
Michal Vaskoade892d2017-02-22 13:40:35 +0100228 if (ret) {
229 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
230 /* timeout */
231 return 0;
232 }
233
Michal Vasko96164bf2016-01-21 15:41:58 +0100234 /* error */
Michal Vaskoade892d2017-02-22 13:40:35 +0100235 ERR("%s: failed to lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100236 return -1;
237 }
238
239 /* ok */
Michal Vaskoade892d2017-02-22 13:40:35 +0100240 assert(*session->ti_inuse == 0);
241 *session->ti_inuse = 1;
242
243 /* UNLOCK */
244 ret = pthread_mutex_unlock(session->ti_lock);
245 if (ret) {
246 /* error */
247 ERR("%s: faile to unlock a session (%s).", func, strerror(ret));
248 return -1;
249 }
250
251 return 1;
252}
253
254int
255nc_session_unlock(struct nc_session *session, int timeout, const char *func)
256{
257 int ret;
258 struct timespec ts_timeout;
259
260 assert(*session->ti_inuse);
261
262 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200263 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100264 nc_addtimespec(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100265
266 /* LOCK */
267 ret = pthread_mutex_timedlock(session->ti_lock, &ts_timeout);
268 } else if (!timeout) {
269 /* LOCK */
270 ret = pthread_mutex_trylock(session->ti_lock);
271 } else { /* timeout == -1 */
272 /* LOCK */
273 ret = pthread_mutex_lock(session->ti_lock);
274 }
275
276 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
277 /* error */
278 ERR("%s: failed to lock a session (%s).", func, strerror(ret));
279 return -1;
280 } else if (ret) {
281 WRN("%s: session lock timeout, should not happen.");
282 }
283
284 *session->ti_inuse = 0;
285 pthread_cond_signal(session->ti_cond);
286
287 if (!ret) {
288 /* UNLOCK */
289 ret = pthread_mutex_unlock(session->ti_lock);
290 if (ret) {
291 /* error */
292 ERR("%s: failed to unlock a session (%s).", func, strerror(ret));
293 return -1;
294 }
295 }
296
Michal Vasko96164bf2016-01-21 15:41:58 +0100297 return 1;
298}
299
Michal Vasko8dadf782016-01-15 10:29:36 +0100300API NC_STATUS
301nc_session_get_status(const struct nc_session *session)
302{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100303 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200304 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200305 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100306 }
307
Michal Vasko8dadf782016-01-15 10:29:36 +0100308 return session->status;
309}
310
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100311API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200312nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100313{
314 if (!session) {
315 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200316 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100317 }
318
319 return session->term_reason;
320}
321
Michal Vasko8dadf782016-01-15 10:29:36 +0100322API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200323nc_session_get_killed_by(const struct nc_session *session)
324{
325 if (!session) {
326 ERRARG("session");
327 return 0;
328 }
329
330 return session->killed_by;
331}
332
333API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100334nc_session_get_id(const struct nc_session *session)
335{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100336 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200337 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100338 return 0;
339 }
340
Michal Vasko8dadf782016-01-15 10:29:36 +0100341 return session->id;
342}
343
Michal Vasko174fe8e2016-02-17 15:38:09 +0100344API int
345nc_session_get_version(const struct nc_session *session)
346{
347 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200348 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100349 return -1;
350 }
351
352 return (session->version == NC_VERSION_10 ? 0 : 1);
353}
354
Michal Vasko8dadf782016-01-15 10:29:36 +0100355API NC_TRANSPORT_IMPL
356nc_session_get_ti(const struct nc_session *session)
357{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100358 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200359 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100360 return 0;
361 }
362
Michal Vasko8dadf782016-01-15 10:29:36 +0100363 return session->ti_type;
364}
365
366API const char *
367nc_session_get_username(const struct nc_session *session)
368{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100369 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200370 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100371 return NULL;
372 }
373
Michal Vasko8dadf782016-01-15 10:29:36 +0100374 return session->username;
375}
376
377API const char *
378nc_session_get_host(const struct nc_session *session)
379{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100380 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200381 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100382 return NULL;
383 }
384
Michal Vasko8dadf782016-01-15 10:29:36 +0100385 return session->host;
386}
387
388API uint16_t
389nc_session_get_port(const struct nc_session *session)
390{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100391 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200392 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100393 return 0;
394 }
395
Michal Vasko8dadf782016-01-15 10:29:36 +0100396 return session->port;
397}
398
Michal Vasko9a25e932016-02-01 10:36:42 +0100399API struct ly_ctx *
400nc_session_get_ctx(const struct nc_session *session)
401{
402 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200403 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100404 return NULL;
405 }
406
407 return session->ctx;
408}
409
Michal Vasko2cc4c682016-03-01 09:16:48 +0100410API void
411nc_session_set_data(struct nc_session *session, void *data)
412{
413 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200414 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100415 return;
416 }
417
418 session->data = data;
419}
420
421API void *
422nc_session_get_data(const struct nc_session *session)
423{
424 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200425 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100426 return NULL;
427 }
428
429 return session->data;
430}
431
Michal Vasko086311b2016-01-08 09:53:11 +0100432NC_MSG_TYPE
433nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200434{
Michal Vasko086311b2016-01-08 09:53:11 +0100435 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200436
Michal Vasko086311b2016-01-08 09:53:11 +0100437 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100438 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
439 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100440 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100441 }
442
Michal Vasko086311b2016-01-08 09:53:11 +0100443 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
444
445 if (r) {
446 return NC_MSG_ERROR;
447 }
448
449 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100450}
451
Radek Krejci695d4fa2015-10-22 13:23:54 +0200452API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100453nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200454{
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100455 int r, i, locked, sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100456 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100457 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100458 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100459 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100460 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100461 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200462 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100463 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200464 void *p;
465
Michal Vasko428087d2016-01-14 16:04:28 +0100466 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200467 return;
468 }
469
Michal Vasko86d357c2016-03-11 13:46:38 +0100470 /* stop notifications loop if any */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200471 if ((session->side == NC_CLIENT) && session->opts.client.ntf_tid) {
472 tid = *session->opts.client.ntf_tid;
473 free((pthread_t *)session->opts.client.ntf_tid);
474 session->opts.client.ntf_tid = NULL;
Michal Vasko86d357c2016-03-11 13:46:38 +0100475 /* the thread now knows it should quit */
476
477 pthread_join(tid, NULL);
478 }
479
Michal Vaskoadd4c792015-10-26 15:36:58 +0100480 if (session->ti_lock) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100481 r = nc_session_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100482 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100483 return;
Michal Vasko9e99f012016-03-03 13:25:20 +0100484 } else if (!r) {
485 /* we failed to lock it, too bad */
486 locked = 0;
487 } else {
488 locked = 1;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100489 }
Michal Vasko5ecbf8c2016-03-29 16:05:22 +0200490 } else {
491 ERRINT;
492 return;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200493 }
494
Michal Vasko9e99f012016-03-03 13:25:20 +0100495 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200496 /* cleanup message queues */
497 /* notifications */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200498 for (contiter = session->opts.client.notifs; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100499 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200500
Michal Vaskoad611702015-12-03 13:41:51 +0100501 p = contiter;
502 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200503 free(p);
504 }
505
506 /* rpc replies */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200507 for (contiter = session->opts.client.replies; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100508 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200509
Michal Vaskoad611702015-12-03 13:41:51 +0100510 p = contiter;
511 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200512 free(p);
513 }
514
515 /* send closing info to the other side */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200516 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200517 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100518 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200519 } else {
520 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100521 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200522 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100523 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100524 case NC_MSG_REPLY:
525 LY_TREE_FOR(rpl->child, child) {
526 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
527 break;
528 }
529 }
530 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100531 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100532 }
Michal Vaskoad611702015-12-03 13:41:51 +0100533 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100534 break;
535 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100536 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100537 break;
538 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100539 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100540 break;
541 default:
542 /* cannot happen */
543 break;
544 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200545 }
546
547 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200548 if (session->opts.client.cpblts) {
549 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200550 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200551 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200552 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200553 }
554 }
555
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100556 if (session->data && data_free) {
557 data_free(session->data);
558 }
559
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200560 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
561 /* CH LOCK */
562 pthread_mutex_lock(session->opts.server.ch_lock);
563 }
564
Michal Vasko86d357c2016-03-11 13:46:38 +0100565 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200566 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200567
568 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
569 pthread_cond_signal(session->opts.server.ch_cond);
570
571 /* CH UNLOCK */
572 pthread_mutex_unlock(session->opts.server.ch_lock);
Michal Vasko3f05a092018-03-13 10:39:49 +0100573
574 /* wait for CH thread to actually wake up */
575 i = (NC_SESSION_FREE_LOCK_TIMEOUT * 1000) / NC_TIMEOUT_STEP;
576 while (i && (session->flags & NC_SESSION_CALLHOME)) {
577 usleep(NC_TIMEOUT_STEP);
578 --i;
579 }
580 if (session->flags & NC_SESSION_CALLHOME) {
581 ERR("Session %u: Call Home thread failed to wake up in a timely manner, fatal synchronization problem.", session->id);
582 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200583 }
584
Michal Vasko428087d2016-01-14 16:04:28 +0100585 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200586
587 /* transport implementation cleanup */
588 switch (session->ti_type) {
589 case NC_TI_FD:
590 /* nothing needed - file descriptors were provided by caller,
591 * so it is up to the caller to close them correctly
592 * TODO use callbacks
593 */
Michal Vasko3512e402016-01-28 16:22:34 +0100594 /* just to avoid compiler warning */
595 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100596 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200597 break;
598
Radek Krejci53691be2016-02-22 13:58:37 +0100599#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200600 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100601 if (connected) {
602 ssh_channel_free(session->ti.libssh.channel);
603 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200604 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
605 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
606 * it.
607 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100608 multisession = 0;
609 if (session->ti.libssh.next) {
610 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
611 if (siter->status != NC_STATUS_STARTING) {
612 multisession = 1;
613 break;
614 }
615 }
616 }
617
618 if (!multisession) {
619 /* it's not multisession yet, but we still need to free the starting sessions */
620 if (session->ti.libssh.next) {
621 do {
622 siter = session->ti.libssh.next;
623 session->ti.libssh.next = siter->ti.libssh.next;
624
625 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100626 lydict_remove(session->ctx, session->username);
627 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100628 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100629 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100630 }
631
632 free(siter);
633 } while (session->ti.libssh.next != session);
634 }
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100635 /* remember sock so we can close it */
636 sock = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100637 if (connected) {
638 ssh_disconnect(session->ti.libssh.session);
639 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200640 ssh_free(session->ti.libssh.session);
641 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200642 /* remove the session from the list */
643 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100644 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200645 /* there will be only one session */
646 siter->ti.libssh.next = NULL;
647 } else {
648 /* there are still multiple sessions, keep the ring list */
649 siter->ti.libssh.next = session->ti.libssh.next;
650 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100651 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
652 if (session->flags & NC_SESSION_SSH_MSG_CB) {
653 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
654 if (siter->ti.libssh.next == session) {
655 ERRINT;
656 break;
657 }
658 }
659 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
660 siter->flags |= NC_SESSION_SSH_MSG_CB;
661 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200662 }
663 break;
664#endif
665
Radek Krejci53691be2016-02-22 13:58:37 +0100666#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200667 case NC_TI_OPENSSL:
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100668 /* remember sock so we can close it */
669 sock = SSL_get_fd(session->ti.tls);
670
Michal Vasko428087d2016-01-14 16:04:28 +0100671 if (connected) {
672 SSL_shutdown(session->ti.tls);
673 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200674 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100675
Michal Vasko2e6defd2016-10-07 15:48:15 +0200676 if (session->side == NC_SERVER) {
677 X509_free(session->opts.server.client_cert);
678 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200679 break;
680#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100681 case NC_TI_NONE:
682 ERRINT;
683 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200684 }
Michal Vasko428087d2016-01-14 16:04:28 +0100685
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100686 /* close socket separately */
687 if (sock > -1) {
688 close(sock);
689 }
690
Radek Krejciac6d3472015-10-22 15:47:18 +0200691 lydict_remove(session->ctx, session->username);
692 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200693
694 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100695 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100696 if (locked) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100697 nc_session_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100698 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100699 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100700 pthread_mutex_destroy(session->ti_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100701 pthread_cond_destroy(session->ti_cond);
Michal Vaskoadd4c792015-10-26 15:36:58 +0100702 free(session->ti_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100703 free(session->ti_cond);
704 free((int *)session->ti_inuse);
Michal Vaskoadd4c792015-10-26 15:36:58 +0100705 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200706 }
707
708 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100709 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200710 }
711
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200712 if (session->side == NC_SERVER) {
Michal Vasko3f05a092018-03-13 10:39:49 +0100713 /* free CH synchronization structures if used */
714 if (session->opts.server.ch_cond) {
715 pthread_cond_destroy(session->opts.server.ch_cond);
716 free(session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200717 }
Michal Vasko3f05a092018-03-13 10:39:49 +0100718 if (session->opts.server.ch_lock) {
719 pthread_mutex_destroy(session->opts.server.ch_lock);
720 free(session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200721 }
722 }
723
Radek Krejci695d4fa2015-10-22 13:23:54 +0200724 free(session);
725}
726
Michal Vasko086311b2016-01-08 09:53:11 +0100727static void
728add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
729{
Radek Krejci658782b2016-12-04 22:04:55 +0100730 size_t len;
731 int i;
732 char *p;
733
734 if (capab) {
735 /* check if already present */
736 p = strchr(capab, '?');
737 if (p) {
738 len = p - capab;
739 } else {
740 len = strlen(capab);
741 }
742 for (i = 0; i < *count; i++) {
fanchanghu5244bf42017-03-13 16:27:48 +0800743 if (!strncmp((*cpblts)[i], capab, len) && ((*cpblts)[i][len] == '\0' || (*cpblts)[i][len] == '?')) {
Radek Krejci658782b2016-12-04 22:04:55 +0100744 /* already present, do not duplicate it */
745 return;
746 }
747 }
748 }
749
750 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100751 if (*count == *size) {
752 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100753 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
754 if (!(*cpblts)) {
755 ERRMEM;
756 return;
757 }
Michal Vasko086311b2016-01-08 09:53:11 +0100758 }
759
760 if (capab) {
761 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
762 } else {
763 (*cpblts)[*count] = NULL;
764 }
765 ++(*count);
766}
767
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200768API const char **
Radek Krejci24a18412018-05-16 15:09:10 +0200769nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +0100770{
Michal Vasko086311b2016-01-08 09:53:11 +0100771 const char **cpblts;
Radek Krejci24a18412018-05-16 15:09:10 +0200772 const struct lys_module *mod, *devmod;
773 int size = 10, count, features_count = 0, dev_count = 0, i, str_len, len;
774 unsigned int u, v, module_set_id;
775 char *s;
776#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +0200777 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100778
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200779 if (!ctx) {
780 ERRARG("ctx");
781 return NULL;
782 }
783
Michal Vasko086311b2016-01-08 09:53:11 +0100784 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100785 if (!cpblts) {
786 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200787 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100788 }
Michal Vasko086311b2016-01-08 09:53:11 +0100789 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
790 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
791 count = 2;
792
793 /* capabilities */
794
Radek Krejci3222b7d2017-09-21 16:04:30 +0200795 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100796 if (mod) {
797 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100798 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100799 }
800 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100801 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100802 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100803 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100804 }
805 }
806 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100807 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100808 }
809 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100810 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100811 }
812 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100813 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100814 }
815 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100816 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100817 }
818 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100819 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100820 }
821 }
822
Radek Krejci3222b7d2017-09-21 16:04:30 +0200823 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100824 if (mod) {
825 if (!server_opts.wd_basic_mode) {
826 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
827 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100828 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100829 switch (server_opts.wd_basic_mode) {
830 case NC_WD_ALL:
831 strcat(str, "?basic-mode=report-all");
832 break;
833 case NC_WD_TRIM:
834 strcat(str, "?basic-mode=trim");
835 break;
836 case NC_WD_EXPLICIT:
837 strcat(str, "?basic-mode=explicit");
838 break;
839 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100840 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100841 break;
842 }
843
844 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200845 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100846 if (server_opts.wd_also_supported & NC_WD_ALL) {
847 strcat(str, "report-all,");
848 }
849 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
850 strcat(str, "report-all-tagged,");
851 }
852 if (server_opts.wd_also_supported & NC_WD_TRIM) {
853 strcat(str, "trim,");
854 }
855 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
856 strcat(str, "explicit,");
857 }
858 str[strlen(str) - 1] = '\0';
859
860 add_cpblt(ctx, str, &cpblts, &size, &count);
861 }
862 }
863 }
864
Radek Krejci658782b2016-12-04 22:04:55 +0100865 /* other capabilities */
866 for (u = 0; u < server_opts.capabilities_count; u++) {
867 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100868 }
869
870 /* models */
Radek Krejci24a18412018-05-16 15:09:10 +0200871 u = module_set_id = 0;
872 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
873 VRB("HELLO module %s", mod->name);
874 if (!strcmp(mod->name, "ietf-yang-library")) {
875 /* ietf-yang-library is always part of the list, but it is specific since it is 1.1 schema */
876 str_len = sprintf(str, "%s?%s%s&module-set-id=%u", mod->ns, mod->rev_size ? "revision=" : "",
877 mod->rev_size ? mod->rev[0].date : "", ly_ctx_get_module_set_id(ctx));
Michal Vasko086311b2016-01-08 09:53:11 +0100878 add_cpblt(ctx, str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +0200879 continue;
880 } else if (mod->type) {
881 /* skip submodules */
882 continue;
883 } else if (version == LYS_VERSION_1 && mod->version > version) {
884 /* skip YANG 1.1 schemas */
885 continue;
886 } else if (version == LYS_VERSION_1_1 && mod->version != version) {
887 /* skip YANG 1.0 schemas */
888 continue;
889 }
Michal Vasko086311b2016-01-08 09:53:11 +0100890
Radek Krejci24a18412018-05-16 15:09:10 +0200891 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name,
892 mod->rev_size ? "&revision=" : "", mod->rev_size ? mod->rev[0].date : "");
893
894 if (mod->features_size) {
895 features_count = 0;
896 for (i = 0; i < mod->features_size; ++i) {
897 if (!(mod->features[i].flags & LYS_FENABLED)) {
898 continue;
899 }
900 if (!features_count) {
901 strcat(str, "&features=");
902 str_len += 10;
903 }
904 len = strlen(mod->features[i].name);
905 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
906 ERRINT;
907 break;
908 }
909 if (i) {
910 strcat(str, ",");
911 ++str_len;
912 }
913 strcat(str, mod->features[i].name);
914 str_len += len;
915 features_count++;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200916 }
Michal Vasko086311b2016-01-08 09:53:11 +0100917 }
Michal Vasko086311b2016-01-08 09:53:11 +0100918
Radek Krejci24a18412018-05-16 15:09:10 +0200919 if (mod->deviated) {
920 strcat(str, "&deviations=");
921 str_len += 12;
922 dev_count = 0;
923 while ((devmod = ly_ctx_get_module_iter(ctx, &v))) {
924 if (devmod == mod) {
925 continue;
926 }
927
928 for (i = 0; i < devmod->deviation_size; ++i) {
929 s = strstr(devmod->deviation[i].target_name, mod->name);
930 if (s && s[strlen(mod->name)] == ':') {
931 /* we have the module deviating the module being processed */
932 len = strlen(devmod->name);
933 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
934 ERRINT;
935 break;
936 }
937 if (dev_count) {
938 strcat(str, ",");
939 ++str_len;
940 }
941 strcat(str, devmod->name);
942 str_len += len;
943 dev_count++;
944
945 break;
946 }
947 }
948 }
949 }
950
951 add_cpblt(ctx, str, &cpblts, &size, &count);
952 }
Michal Vasko086311b2016-01-08 09:53:11 +0100953
954 /* ending NULL capability */
955 add_cpblt(ctx, NULL, &cpblts, &size, &count);
956
957 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +0200958
959error:
960
961 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +0200962 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100963}
964
Radek Krejci24a18412018-05-16 15:09:10 +0200965API const char **
966nc_server_get_cpblts(struct ly_ctx *ctx)
967{
968 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
969}
970
Radek Krejci695d4fa2015-10-22 13:23:54 +0200971static int
Michal Vasko2cb24b42017-05-23 14:59:11 +0200972parse_cpblts(struct lyxml_elem *xml, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200973{
974 struct lyxml_elem *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +0200975 int ver = -1, i = 0;
976 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200977
978 if (list) {
979 /* get the storage for server's capabilities */
980 LY_TREE_FOR(xml->child, cpblt) {
981 i++;
982 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100983 /* last item remains NULL */
984 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200985 if (!*list) {
986 ERRMEM;
987 return -1;
988 }
989 i = 0;
990 }
991
992 LY_TREE_FOR(xml->child, cpblt) {
993 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
994 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
995 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
996 return -1;
997 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
998 continue;
999 }
1000
Michal Vasko156d3272017-04-11 11:46:49 +02001001 /* skip leading/trailing whitespaces */
1002 for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start);
1003 for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end);
1004 if (!cpb_start[0] || (cpb_end == cpblt->content)) {
1005 ERR("Empty capability \"%s\" received.", cpblt->content);
1006 return -1;
1007 }
1008
Radek Krejci695d4fa2015-10-22 13:23:54 +02001009 /* detect NETCONF version */
Michal Vasko156d3272017-04-11 11:46:49 +02001010 if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001011 ver = 0;
Michal Vasko156d3272017-04-11 11:46:49 +02001012 } else if (ver < 1 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.1", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001013 ver = 1;
1014 }
1015
1016 /* store capabilities */
1017 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001018 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1019 if (!(*list)[i]) {
1020 ERRMEM;
1021 return -1;
1022 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001023 i++;
1024 }
1025 }
1026
1027 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001028 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001029 }
1030
1031 return ver;
1032}
1033
1034static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +01001035nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001036{
1037 int r, i;
1038 const char **cpblts;
1039
Michal Vasko11d142a2016-01-19 15:58:24 +01001040 /* client side hello - send only NETCONF base capabilities */
1041 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001042 if (!cpblts) {
1043 ERRMEM;
1044 return NC_MSG_ERROR;
1045 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001046 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
1047 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
1048 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001049
Michal Vasko11d142a2016-01-19 15:58:24 +01001050 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001051
Michal Vasko086311b2016-01-08 09:53:11 +01001052 for (i = 0; cpblts[i]; ++i) {
1053 lydict_remove(session->ctx, cpblts[i]);
1054 }
1055 free(cpblts);
1056
1057 if (r) {
1058 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001059 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001060
1061 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +01001062}
1063
1064static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +01001065nc_send_server_hello(struct nc_session *session)
1066{
1067 int r, i;
1068 const char **cpblts;
1069
Radek Krejci24a18412018-05-16 15:09:10 +02001070 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1);
Michal Vasko11d142a2016-01-19 15:58:24 +01001071
1072 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
1073
Michal Vasko11d142a2016-01-19 15:58:24 +01001074 for (i = 0; cpblts[i]; ++i) {
1075 lydict_remove(session->ctx, cpblts[i]);
1076 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001077 free(cpblts);
1078
1079 if (r) {
1080 return NC_MSG_ERROR;
1081 }
1082
1083 return NC_MSG_HELLO;
1084}
1085
1086static NC_MSG_TYPE
1087nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001088{
1089 struct lyxml_elem *xml = NULL, *node;
1090 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1091 int ver = -1;
1092 char *str;
1093 long long int id;
1094 int flag = 0;
1095
Michal Vasko05ba9df2016-01-13 14:40:27 +01001096 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001097
1098 switch(msgtype) {
1099 case NC_MSG_HELLO:
1100 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +01001101 LY_TREE_FOR(xml->child, node) {
1102 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1103 continue;
1104 } else if (!strcmp(node->name, "session-id")) {
1105 if (!node->content || !strlen(node->content)) {
1106 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001107 goto error;
1108 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001109 str = NULL;
1110 id = strtoll(node->content, &str, 10);
1111 if (*str || id < 1 || id > UINT32_MAX) {
1112 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001113 goto error;
1114 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001115 session->id = (uint32_t)id;
1116 continue;
1117 } else if (strcmp(node->name, "capabilities")) {
1118 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001119 goto error;
1120 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001121
1122 if (flag) {
1123 /* multiple capabilities elements */
1124 ERR("Invalid <hello> message (multiple <capabilities> elements).");
1125 goto error;
1126 }
1127 flag = 1;
1128
Michal Vasko2e6defd2016-10-07 15:48:15 +02001129 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001130 goto error;
1131 }
1132 session->version = ver;
1133 }
1134
1135 if (!session->id) {
1136 ERR("Missing <session-id> in server's <hello>.");
1137 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001138 }
1139 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001140 case NC_MSG_WOULDBLOCK:
1141 ERR("Server's <hello> timeout elapsed.");
1142 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001143 case NC_MSG_ERROR:
1144 /* nothing special, just pass it out */
1145 break;
1146 default:
1147 ERR("Unexpected message received instead of <hello>.");
1148 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001149 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001150 }
1151
1152 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001153 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001154
1155 return msgtype;
1156
1157error:
1158 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001159 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001160
1161 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +02001162}
1163
Michal Vasko11d142a2016-01-19 15:58:24 +01001164static NC_MSG_TYPE
1165nc_recv_server_hello(struct nc_session *session)
1166{
1167 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +02001168 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +01001169 int ver = -1;
1170 int flag = 0;
1171
fanchanghu75888b62017-08-01 19:51:20 +08001172 msgtype = nc_read_msg_poll(session, (server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000), &xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001173
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001174 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001175 case NC_MSG_HELLO:
1176 /* get know NETCONF version */
1177 LY_TREE_FOR(xml->child, node) {
1178 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1179 continue;
1180 } else if (strcmp(node->name, "capabilities")) {
1181 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +02001182 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001183 goto cleanup;
1184 }
1185
1186 if (flag) {
1187 /* multiple capabilities elements */
1188 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +02001189 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001190 goto cleanup;
1191 }
1192 flag = 1;
1193
1194 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001195 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001196 goto cleanup;
1197 }
1198 session->version = ver;
1199 }
1200 break;
1201 case NC_MSG_ERROR:
1202 /* nothing special, just pass it out */
1203 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001204 case NC_MSG_WOULDBLOCK:
1205 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001206 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001207 default:
1208 ERR("Unexpected message received instead of <hello>.");
1209 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001210 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001211 }
1212
1213cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +01001214 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001215
1216 return msgtype;
1217}
1218
Michal Vasko71090fc2016-05-24 16:37:28 +02001219NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +01001220nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001221{
Michal Vasko086311b2016-01-08 09:53:11 +01001222 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001223
Michal Vasko11d142a2016-01-19 15:58:24 +01001224 if (session->side == NC_CLIENT) {
1225 type = nc_send_client_hello(session);
1226 } else {
1227 type = nc_send_server_hello(session);
1228 }
1229
Michal Vasko086311b2016-01-08 09:53:11 +01001230 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001231 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001232 }
1233
Michal Vasko11d142a2016-01-19 15:58:24 +01001234 if (session->side == NC_CLIENT) {
1235 type = nc_recv_client_hello(session);
1236 } else {
1237 type = nc_recv_server_hello(session);
1238 }
1239
Michal Vasko71090fc2016-05-24 16:37:28 +02001240 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001241}
Michal Vasko086311b2016-01-08 09:53:11 +01001242
Radek Krejci53691be2016-02-22 13:58:37 +01001243#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +01001244
Michal Vasko8f0c0282016-02-29 10:17:14 +01001245static void
Michal Vasko086311b2016-01-08 09:53:11 +01001246nc_ssh_init(void)
1247{
1248 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1249 ssh_init();
Michal Vasko086311b2016-01-08 09:53:11 +01001250}
1251
Michal Vasko8f0c0282016-02-29 10:17:14 +01001252static void
Michal Vasko086311b2016-01-08 09:53:11 +01001253nc_ssh_destroy(void)
1254{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001255 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001256 ENGINE_cleanup();
1257 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001258 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001259 ssh_finalize();
1260}
1261
Radek Krejci53691be2016-02-22 13:58:37 +01001262#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001263
Radek Krejci53691be2016-02-22 13:58:37 +01001264#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001265
Michal Vasko770b4362017-02-17 14:44:18 +01001266#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1267
Michal Vaskof0c92c02016-01-29 09:41:45 +01001268struct CRYPTO_dynlock_value {
1269 pthread_mutex_t lock;
1270};
1271
Michal Vaskof0c92c02016-01-29 09:41:45 +01001272static struct CRYPTO_dynlock_value *
1273tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1274{
1275 struct CRYPTO_dynlock_value *value;
1276
1277 value = malloc(sizeof *value);
1278 if (!value) {
1279 ERRMEM;
1280 return NULL;
1281 }
1282 pthread_mutex_init(&value->lock, NULL);
1283
1284 return value;
1285}
1286
1287static void
1288tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1289{
1290 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1291 * I found ignored this fact, what do I know... */
1292 if (mode & CRYPTO_LOCK) {
1293 pthread_mutex_lock(&l->lock);
1294 } else {
1295 pthread_mutex_unlock(&l->lock);
1296 }
1297}
1298
1299static void
1300tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1301{
1302 pthread_mutex_destroy(&l->lock);
1303 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001304}
Michal Vasko770b4362017-02-17 14:44:18 +01001305#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001306
Michal Vasko8f0c0282016-02-29 10:17:14 +01001307#endif /* NC_ENABLED_TLS */
1308
1309#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1310
Michal Vasko770b4362017-02-17 14:44:18 +01001311#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001312static pthread_mutex_t *tls_locks;
1313
1314static void
1315tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1316{
1317 if (mode & CRYPTO_LOCK) {
1318 pthread_mutex_lock(tls_locks + n);
1319 } else {
1320 pthread_mutex_unlock(tls_locks + n);
1321 }
1322}
1323
1324static void
1325tls_thread_id_func(CRYPTO_THREADID *tid)
1326{
1327 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1328}
Michal Vasko770b4362017-02-17 14:44:18 +01001329#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001330
1331static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001332nc_tls_init(void)
1333{
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001334 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001335 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001336 SSL_library_init();
1337
Michal Vasko770b4362017-02-17 14:44:18 +01001338#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskof89948c2018-01-04 09:19:46 +01001339 int i;
1340
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001341 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001342 if (!tls_locks) {
1343 ERRMEM;
1344 return;
1345 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001346 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1347 pthread_mutex_init(tls_locks + i, NULL);
1348 }
1349
Michal Vaskof0c92c02016-01-29 09:41:45 +01001350 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001351 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001352
1353 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1354 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1355 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001356#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001357}
1358
Michal Vasko8f0c0282016-02-29 10:17:14 +01001359static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001360nc_tls_destroy(void)
1361{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001362 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001363 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001364 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001365 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001366 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001367#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001368 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001369#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1370 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001371#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001372
Michal Vasko770b4362017-02-17 14:44:18 +01001373#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskof89948c2018-01-04 09:19:46 +01001374 int i;
1375
Michal Vaskob6e37262016-02-25 14:49:00 +01001376 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001377 CRYPTO_set_locking_callback(NULL);
1378 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1379 pthread_mutex_destroy(tls_locks + i);
1380 }
1381 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001382
1383 CRYPTO_set_dynlock_create_callback(NULL);
1384 CRYPTO_set_dynlock_lock_callback(NULL);
1385 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001386#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001387}
1388
Michal Vasko8f0c0282016-02-29 10:17:14 +01001389#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001390
Radek Krejci53691be2016-02-22 13:58:37 +01001391#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001392
Michal Vasko8f0c0282016-02-29 10:17:14 +01001393static void
Michal Vasko5e228792016-02-03 15:30:24 +01001394nc_ssh_tls_init(void)
1395{
1396 SSL_load_error_strings();
1397 ERR_load_BIO_strings();
1398 SSL_library_init();
1399
1400 nc_ssh_init();
1401
Michal Vasko770b4362017-02-17 14:44:18 +01001402#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001403 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1404 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1405 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001406#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001407}
1408
Michal Vasko8f0c0282016-02-29 10:17:14 +01001409static void
Michal Vasko5e228792016-02-03 15:30:24 +01001410nc_ssh_tls_destroy(void)
1411{
1412 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001413#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001414 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001415#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1416 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001417#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001418
1419 nc_ssh_destroy();
1420
Michal Vasko770b4362017-02-17 14:44:18 +01001421#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001422 CRYPTO_set_dynlock_create_callback(NULL);
1423 CRYPTO_set_dynlock_lock_callback(NULL);
1424 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001425#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001426}
1427
Radek Krejci53691be2016-02-22 13:58:37 +01001428#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001429
1430#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1431
1432API void
1433nc_thread_destroy(void)
1434{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001435 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1436 //CRYPTO_cleanup_all_ex_data();
1437
Michal Vasko770b4362017-02-17 14:44:18 +01001438#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1439 CRYPTO_THREADID crypto_tid;
1440
Michal Vasko8f0c0282016-02-29 10:17:14 +01001441 CRYPTO_THREADID_current(&crypto_tid);
1442 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001443#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001444}
1445
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001446#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1447
1448void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001449nc_init(void)
1450{
1451#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1452 nc_ssh_tls_init();
1453#elif defined(NC_ENABLED_SSH)
1454 nc_ssh_init();
1455#elif defined(NC_ENABLED_TLS)
1456 nc_tls_init();
1457#endif
1458}
1459
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001460void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001461nc_destroy(void)
1462{
1463#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1464 nc_ssh_tls_destroy();
1465#elif defined(NC_ENABLED_SSH)
1466 nc_ssh_destroy();
1467#elif defined(NC_ENABLED_TLS)
1468 nc_tls_destroy();
1469#endif
1470}