blob: 88eb17d96a9093928e0bd313279aa9c5789cce9e [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 Vasko18aeb5d2017-02-17 09:23:56 +01006 * Copyright (c) 2015 - 2017 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 */
14
Michal Vasko18aeb5d2017-02-17 09:23:56 +010015#include <assert.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020016#include <errno.h>
Radek Krejci952eb862016-01-08 14:22:55 +010017#include <stdlib.h>
Michal Vasko3512e402016-01-28 16:22:34 +010018#include <string.h>
Radek Krejciac6d3472015-10-22 15:47:18 +020019#include <pthread.h>
Radek Krejcid6b73e12016-07-15 12:00:23 +020020#include <sys/time.h>
Michal Vasko58f31552016-01-19 12:39:15 +010021#include <time.h>
Michal Vasko156d3272017-04-11 11:46:49 +020022#include <ctype.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020023#include <libyang/libyang.h>
24
Michal Vasko5e228792016-02-03 15:30:24 +010025#include "session.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010026#include "libnetconf.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010027#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010028
Radek Krejci53691be2016-02-22 13:58:37 +010029#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020030
Michal Vasko086311b2016-01-08 09:53:11 +010031# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020032
Radek Krejci53691be2016-02-22 13:58:37 +010033#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020034
Radek Krejci53691be2016-02-22 13:58:37 +010035#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010036
Michal Vasko5e228792016-02-03 15:30:24 +010037# include <openssl/engine.h>
38# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010039# include <openssl/err.h>
40
Radek Krejci53691be2016-02-22 13:58:37 +010041#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010042
Michal Vasko086311b2016-01-08 09:53:11 +010043/* in seconds */
44#define NC_CLIENT_HELLO_TIMEOUT 60
fanchanghu75888b62017-08-01 19:51:20 +080045#define NC_SERVER_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020046
Michal Vasko05ba9df2016-01-13 14:40:27 +010047/* in milliseconds */
48#define NC_CLOSE_REPLY_TIMEOUT 200
49
Michal Vasko086311b2016-01-08 09:53:11 +010050extern struct nc_server_opts server_opts;
51
Radek Krejci7ac16052016-07-15 11:48:18 +020052int
53nc_gettimespec(struct timespec *ts)
54{
Michal Vasko0ef46df2016-09-21 14:03:18 +020055#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020056 return clock_gettime(CLOCK_REALTIME, ts);
57#else
58 int rc;
59 struct timeval tv;
60
61 rc = gettimeofday(&tv, NULL);
62 if (!rc) {
63 ts->tv_sec = (time_t)tv.tv_sec;
64 ts->tv_nsec = 1000L * (long)tv.tv_usec;
65 }
66 return rc;
67#endif
68}
69
Michal Vasko36c7be82017-02-22 13:37:59 +010070/* ts1 < ts2 -> +, ts1 > ts2 -> -, returns milliseconds */
71int32_t
Radek Krejcida0afb22017-05-26 16:25:59 +020072nc_difftimespec(const struct timespec *ts1, const struct timespec *ts2)
Michal Vasko99f251b2017-01-11 11:31:46 +010073{
Michal Vasko36c7be82017-02-22 13:37:59 +010074 int64_t nsec_diff = 0;
Michal Vasko99f251b2017-01-11 11:31:46 +010075
Michal Vaskoa82bd202017-03-03 14:07:29 +010076 nsec_diff += (((int64_t)ts2->tv_sec) - ((int64_t)ts1->tv_sec)) * 1000000000L;
77 nsec_diff += ((int64_t)ts2->tv_nsec) - ((int64_t)ts1->tv_nsec);
Michal Vasko99f251b2017-01-11 11:31:46 +010078
79 return (nsec_diff ? nsec_diff / 1000000L : 0);
80}
81
Michal Vasko36c7be82017-02-22 13:37:59 +010082void
83nc_addtimespec(struct timespec *ts, uint32_t msec)
84{
Michal Vasko81c5b302017-03-15 12:10:40 +010085 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
86
Michal Vasko0dfcd332017-03-03 13:14:39 +010087 ts->tv_sec += msec / 1000;
88 ts->tv_nsec += (msec % 1000) * 1000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +010089
Michal Vasko81c5b302017-03-15 12:10:40 +010090 if (ts->tv_nsec >= 1000000000L) {
91 ++ts->tv_sec;
92 ts->tv_nsec -= 1000000000L;
93 } else if (ts->tv_nsec < 0) {
94 --ts->tv_sec;
95 ts->tv_nsec += 1000000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +010096 }
Michal Vasko81c5b302017-03-15 12:10:40 +010097
98 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
Michal Vasko36c7be82017-02-22 13:37:59 +010099}
100
Radek Krejci28472922016-07-15 11:51:16 +0200101#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
102int
103pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
104{
Michal Vasko81c5b302017-03-15 12:10:40 +0100105 int32_t diff;
Radek Krejci28472922016-07-15 11:51:16 +0200106 int rc;
107 struct timespec cur, dur;
108
109 /* Try to acquire the lock and, if we fail, sleep for 5ms. */
110 while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
111 nc_gettimespec(&cur);
112
Michal Vasko81c5b302017-03-15 12:10:40 +0100113 if ((diff = nc_difftimespec(&cur, abstime)) < 1) {
114 /* timeout */
Radek Krejci28472922016-07-15 11:51:16 +0200115 break;
Michal Vasko81c5b302017-03-15 12:10:40 +0100116 } else if (diff < 5) {
117 /* sleep until timeout */
118 dur = *abstime;
119 } else {
120 /* sleep 5 ms */
Radek Krejci28472922016-07-15 11:51:16 +0200121 dur.tv_sec = 0;
122 dur.tv_nsec = 5000000;
123 }
124
125 nanosleep(&dur, NULL);
126 }
127
128 return rc;
129}
130#endif
131
Michal Vaskoade892d2017-02-22 13:40:35 +0100132struct nc_session *
133nc_new_session(int not_allocate_ti)
134{
135 struct nc_session *sess;
136
137 sess = calloc(1, sizeof *sess);
138 if (!sess) {
139 return NULL;
140 }
141
142 if (!not_allocate_ti) {
143 sess->ti_lock = malloc(sizeof *sess->ti_lock);
144 sess->ti_cond = malloc(sizeof *sess->ti_cond);
145 sess->ti_inuse = malloc(sizeof *sess->ti_inuse);
146 if (!sess->ti_lock || !sess->ti_cond || !sess->ti_inuse) {
147 free(sess->ti_lock);
148 free(sess->ti_cond);
149 free((int *)sess->ti_inuse);
150 free(sess);
151 return NULL;
152 }
153 }
154
155 return sess;
156}
157
Michal Vasko96164bf2016-01-21 15:41:58 +0100158/*
159 * @return 1 - success
160 * 0 - timeout
161 * -1 - error
162 */
163int
Michal Vaskoade892d2017-02-22 13:40:35 +0100164nc_session_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100165{
166 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100167 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100168
169 if (timeout > 0) {
Radek Krejci7ac16052016-07-15 11:48:18 +0200170 nc_gettimespec(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100171 nc_addtimespec(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100172
Michal Vaskoade892d2017-02-22 13:40:35 +0100173 /* LOCK */
174 ret = pthread_mutex_timedlock(session->ti_lock, &ts_timeout);
175 if (!ret) {
176 while (*session->ti_inuse) {
177 ret = pthread_cond_timedwait(session->ti_cond, session->ti_lock, &ts_timeout);
178 if (ret) {
179 pthread_mutex_unlock(session->ti_lock);
180 break;
181 }
182 }
183 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100184 } else if (!timeout) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100185 if (*session->ti_inuse) {
186 /* immediate timeout */
187 return 0;
188 }
189
190 /* LOCK */
191 ret = pthread_mutex_trylock(session->ti_lock);
192 if (!ret) {
193 /* be extra careful, someone could have been faster */
194 if (*session->ti_inuse) {
195 pthread_mutex_unlock(session->ti_lock);
196 return 0;
197 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200198 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100199 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100200 /* LOCK */
201 ret = pthread_mutex_lock(session->ti_lock);
202 if (!ret) {
203 while (*session->ti_inuse) {
204 ret = pthread_cond_wait(session->ti_cond, session->ti_lock);
205 if (ret) {
206 pthread_mutex_unlock(session->ti_lock);
207 break;
208 }
209 }
210 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100211 }
212
Michal Vaskoade892d2017-02-22 13:40:35 +0100213 if (ret) {
214 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
215 /* timeout */
216 return 0;
217 }
218
Michal Vasko96164bf2016-01-21 15:41:58 +0100219 /* error */
Michal Vaskoade892d2017-02-22 13:40:35 +0100220 ERR("%s: failed to lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100221 return -1;
222 }
223
224 /* ok */
Michal Vaskoade892d2017-02-22 13:40:35 +0100225 assert(*session->ti_inuse == 0);
226 *session->ti_inuse = 1;
227
228 /* UNLOCK */
229 ret = pthread_mutex_unlock(session->ti_lock);
230 if (ret) {
231 /* error */
232 ERR("%s: faile to unlock a session (%s).", func, strerror(ret));
233 return -1;
234 }
235
236 return 1;
237}
238
239int
240nc_session_unlock(struct nc_session *session, int timeout, const char *func)
241{
242 int ret;
243 struct timespec ts_timeout;
244
245 assert(*session->ti_inuse);
246
247 if (timeout > 0) {
248 nc_gettimespec(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100249 nc_addtimespec(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100250
251 /* LOCK */
252 ret = pthread_mutex_timedlock(session->ti_lock, &ts_timeout);
253 } else if (!timeout) {
254 /* LOCK */
255 ret = pthread_mutex_trylock(session->ti_lock);
256 } else { /* timeout == -1 */
257 /* LOCK */
258 ret = pthread_mutex_lock(session->ti_lock);
259 }
260
261 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
262 /* error */
263 ERR("%s: failed to lock a session (%s).", func, strerror(ret));
264 return -1;
265 } else if (ret) {
266 WRN("%s: session lock timeout, should not happen.");
267 }
268
269 *session->ti_inuse = 0;
270 pthread_cond_signal(session->ti_cond);
271
272 if (!ret) {
273 /* UNLOCK */
274 ret = pthread_mutex_unlock(session->ti_lock);
275 if (ret) {
276 /* error */
277 ERR("%s: failed to unlock a session (%s).", func, strerror(ret));
278 return -1;
279 }
280 }
281
Michal Vasko96164bf2016-01-21 15:41:58 +0100282 return 1;
283}
284
Michal Vasko8dadf782016-01-15 10:29:36 +0100285API NC_STATUS
286nc_session_get_status(const struct nc_session *session)
287{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100288 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200289 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200290 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100291 }
292
Michal Vasko8dadf782016-01-15 10:29:36 +0100293 return session->status;
294}
295
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100296API NC_SESSION_TERM_REASON
297nc_session_get_termreason(const struct nc_session *session)
298{
299 if (!session) {
300 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200301 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100302 }
303
304 return session->term_reason;
305}
306
Michal Vasko8dadf782016-01-15 10:29:36 +0100307API uint32_t
308nc_session_get_id(const struct nc_session *session)
309{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100310 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200311 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100312 return 0;
313 }
314
Michal Vasko8dadf782016-01-15 10:29:36 +0100315 return session->id;
316}
317
Michal Vasko174fe8e2016-02-17 15:38:09 +0100318API int
319nc_session_get_version(const struct nc_session *session)
320{
321 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200322 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100323 return -1;
324 }
325
326 return (session->version == NC_VERSION_10 ? 0 : 1);
327}
328
Michal Vasko8dadf782016-01-15 10:29:36 +0100329API NC_TRANSPORT_IMPL
330nc_session_get_ti(const struct nc_session *session)
331{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100332 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200333 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100334 return 0;
335 }
336
Michal Vasko8dadf782016-01-15 10:29:36 +0100337 return session->ti_type;
338}
339
340API const char *
341nc_session_get_username(const struct nc_session *session)
342{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100343 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200344 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100345 return NULL;
346 }
347
Michal Vasko8dadf782016-01-15 10:29:36 +0100348 return session->username;
349}
350
351API const char *
352nc_session_get_host(const struct nc_session *session)
353{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100354 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200355 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100356 return NULL;
357 }
358
Michal Vasko8dadf782016-01-15 10:29:36 +0100359 return session->host;
360}
361
362API uint16_t
363nc_session_get_port(const struct nc_session *session)
364{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100365 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200366 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100367 return 0;
368 }
369
Michal Vasko8dadf782016-01-15 10:29:36 +0100370 return session->port;
371}
372
Michal Vasko9a25e932016-02-01 10:36:42 +0100373API struct ly_ctx *
374nc_session_get_ctx(const struct nc_session *session)
375{
376 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200377 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100378 return NULL;
379 }
380
381 return session->ctx;
382}
383
Michal Vasko2cc4c682016-03-01 09:16:48 +0100384API void
385nc_session_set_data(struct nc_session *session, void *data)
386{
387 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200388 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100389 return;
390 }
391
392 session->data = data;
393}
394
395API void *
396nc_session_get_data(const struct nc_session *session)
397{
398 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200399 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100400 return NULL;
401 }
402
403 return session->data;
404}
405
Michal Vasko086311b2016-01-08 09:53:11 +0100406NC_MSG_TYPE
407nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200408{
Michal Vasko086311b2016-01-08 09:53:11 +0100409 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200410
Michal Vasko086311b2016-01-08 09:53:11 +0100411 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100412 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
413 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100414 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100415 }
416
Michal Vasko086311b2016-01-08 09:53:11 +0100417 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
418
419 if (r) {
420 return NC_MSG_ERROR;
421 }
422
423 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100424}
425
Radek Krejci695d4fa2015-10-22 13:23:54 +0200426API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100427nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200428{
Michal Vasko9e99f012016-03-03 13:25:20 +0100429 int r, i, locked;
Michal Vasko428087d2016-01-14 16:04:28 +0100430 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100431 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100432 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100433 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100434 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100435 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200436 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100437 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200438 void *p;
439
Michal Vasko428087d2016-01-14 16:04:28 +0100440 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200441 return;
442 }
443
Michal Vasko86d357c2016-03-11 13:46:38 +0100444 /* stop notifications loop if any */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200445 if ((session->side == NC_CLIENT) && session->opts.client.ntf_tid) {
446 tid = *session->opts.client.ntf_tid;
447 free((pthread_t *)session->opts.client.ntf_tid);
448 session->opts.client.ntf_tid = NULL;
Michal Vasko86d357c2016-03-11 13:46:38 +0100449 /* the thread now knows it should quit */
450
451 pthread_join(tid, NULL);
452 }
453
Michal Vaskoadd4c792015-10-26 15:36:58 +0100454 if (session->ti_lock) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100455 r = nc_session_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100456 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100457 return;
Michal Vasko9e99f012016-03-03 13:25:20 +0100458 } else if (!r) {
459 /* we failed to lock it, too bad */
460 locked = 0;
461 } else {
462 locked = 1;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100463 }
Michal Vasko5ecbf8c2016-03-29 16:05:22 +0200464 } else {
465 ERRINT;
466 return;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200467 }
468
Michal Vasko9e99f012016-03-03 13:25:20 +0100469 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200470 /* cleanup message queues */
471 /* notifications */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200472 for (contiter = session->opts.client.notifs; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100473 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200474
Michal Vaskoad611702015-12-03 13:41:51 +0100475 p = contiter;
476 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200477 free(p);
478 }
479
480 /* rpc replies */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200481 for (contiter = session->opts.client.replies; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100482 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200483
Michal Vaskoad611702015-12-03 13:41:51 +0100484 p = contiter;
485 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200486 free(p);
487 }
488
489 /* send closing info to the other side */
490 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
491 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100492 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200493 } else {
494 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100495 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200496 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100497 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100498 case NC_MSG_REPLY:
499 LY_TREE_FOR(rpl->child, child) {
500 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
501 break;
502 }
503 }
504 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100505 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100506 }
Michal Vaskoad611702015-12-03 13:41:51 +0100507 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100508 break;
509 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100510 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100511 break;
512 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100513 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100514 break;
515 default:
516 /* cannot happen */
517 break;
518 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200519 }
520
521 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200522 if (session->opts.client.cpblts) {
523 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200524 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200525 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200526 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200527 }
528 }
529
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100530 if (session->data && data_free) {
531 data_free(session->data);
532 }
533
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200534 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
535 /* CH LOCK */
536 pthread_mutex_lock(session->opts.server.ch_lock);
537 }
538
Michal Vasko86d357c2016-03-11 13:46:38 +0100539 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200540 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200541
542 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
543 pthread_cond_signal(session->opts.server.ch_cond);
544
545 /* CH UNLOCK */
546 pthread_mutex_unlock(session->opts.server.ch_lock);
547 }
548
Michal Vasko428087d2016-01-14 16:04:28 +0100549 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200550
551 /* transport implementation cleanup */
552 switch (session->ti_type) {
553 case NC_TI_FD:
554 /* nothing needed - file descriptors were provided by caller,
555 * so it is up to the caller to close them correctly
556 * TODO use callbacks
557 */
Michal Vasko3512e402016-01-28 16:22:34 +0100558 /* just to avoid compiler warning */
559 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100560 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200561 break;
562
Radek Krejci53691be2016-02-22 13:58:37 +0100563#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200564 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100565 if (connected) {
566 ssh_channel_free(session->ti.libssh.channel);
567 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200568 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
569 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
570 * it.
571 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100572 multisession = 0;
573 if (session->ti.libssh.next) {
574 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
575 if (siter->status != NC_STATUS_STARTING) {
576 multisession = 1;
577 break;
578 }
579 }
580 }
581
582 if (!multisession) {
583 /* it's not multisession yet, but we still need to free the starting sessions */
584 if (session->ti.libssh.next) {
585 do {
586 siter = session->ti.libssh.next;
587 session->ti.libssh.next = siter->ti.libssh.next;
588
589 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100590 lydict_remove(session->ctx, session->username);
591 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100592 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100593 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100594 }
595
596 free(siter);
597 } while (session->ti.libssh.next != session);
598 }
Michal Vasko428087d2016-01-14 16:04:28 +0100599 if (connected) {
600 ssh_disconnect(session->ti.libssh.session);
601 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200602 ssh_free(session->ti.libssh.session);
603 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200604 /* remove the session from the list */
605 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100606 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200607 /* there will be only one session */
608 siter->ti.libssh.next = NULL;
609 } else {
610 /* there are still multiple sessions, keep the ring list */
611 siter->ti.libssh.next = session->ti.libssh.next;
612 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100613 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
614 if (session->flags & NC_SESSION_SSH_MSG_CB) {
615 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
616 if (siter->ti.libssh.next == session) {
617 ERRINT;
618 break;
619 }
620 }
621 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
622 siter->flags |= NC_SESSION_SSH_MSG_CB;
623 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200624 }
625 break;
626#endif
627
Radek Krejci53691be2016-02-22 13:58:37 +0100628#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200629 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100630 if (connected) {
631 SSL_shutdown(session->ti.tls);
632 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200633 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100634
Michal Vasko2e6defd2016-10-07 15:48:15 +0200635 if (session->side == NC_SERVER) {
636 X509_free(session->opts.server.client_cert);
637 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200638 break;
639#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100640 case NC_TI_NONE:
641 ERRINT;
642 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200643 }
Michal Vasko428087d2016-01-14 16:04:28 +0100644
Radek Krejciac6d3472015-10-22 15:47:18 +0200645 lydict_remove(session->ctx, session->username);
646 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200647
648 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100649 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100650 if (locked) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100651 nc_session_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100652 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100653 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100654 pthread_mutex_destroy(session->ti_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100655 pthread_cond_destroy(session->ti_cond);
Michal Vaskoadd4c792015-10-26 15:36:58 +0100656 free(session->ti_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100657 free(session->ti_cond);
658 free((int *)session->ti_inuse);
Michal Vaskoadd4c792015-10-26 15:36:58 +0100659 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200660 }
661
662 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100663 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200664 }
665
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200666 if (session->side == NC_SERVER) {
667 if (session->opts.server.ch_cond) {
668 pthread_cond_destroy(session->opts.server.ch_cond);
669 free(session->opts.server.ch_cond);
670 }
671 if (session->opts.server.ch_lock) {
672 pthread_mutex_destroy(session->opts.server.ch_lock);
673 free(session->opts.server.ch_lock);
674 }
675 }
676
Radek Krejci695d4fa2015-10-22 13:23:54 +0200677 free(session);
678}
679
Michal Vasko086311b2016-01-08 09:53:11 +0100680static void
681add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
682{
Radek Krejci658782b2016-12-04 22:04:55 +0100683 size_t len;
684 int i;
685 char *p;
686
687 if (capab) {
688 /* check if already present */
689 p = strchr(capab, '?');
690 if (p) {
691 len = p - capab;
692 } else {
693 len = strlen(capab);
694 }
695 for (i = 0; i < *count; i++) {
fanchanghu5244bf42017-03-13 16:27:48 +0800696 if (!strncmp((*cpblts)[i], capab, len) && ((*cpblts)[i][len] == '\0' || (*cpblts)[i][len] == '?')) {
Radek Krejci658782b2016-12-04 22:04:55 +0100697 /* already present, do not duplicate it */
698 return;
699 }
700 }
701 }
702
703 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100704 if (*count == *size) {
705 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100706 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
707 if (!(*cpblts)) {
708 ERRMEM;
709 return;
710 }
Michal Vasko086311b2016-01-08 09:53:11 +0100711 }
712
713 if (capab) {
714 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
715 } else {
716 (*cpblts)[*count] = NULL;
717 }
718 ++(*count);
719}
720
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200721API const char **
722nc_server_get_cpblts(struct ly_ctx *ctx)
Michal Vasko086311b2016-01-08 09:53:11 +0100723{
724 struct lyd_node *child, *child2, *yanglib;
Michal Vaskodd9fe652016-09-14 09:24:32 +0200725 struct lyd_node_leaf_list **features = NULL, **deviations = NULL, *ns = NULL, *rev = NULL, *name = NULL, *module_set_id = NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100726 const char **cpblts;
727 const struct lys_module *mod;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200728 int size = 10, count, feat_count = 0, dev_count = 0, i, str_len;
Radek Krejci658782b2016-12-04 22:04:55 +0100729 unsigned int u;
Michal Vasko2e47ef92016-06-20 10:03:24 +0200730#define NC_CPBLT_BUF_LEN 512
731 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100732
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200733 if (!ctx) {
734 ERRARG("ctx");
735 return NULL;
736 }
737
Michal Vasko086311b2016-01-08 09:53:11 +0100738 yanglib = ly_ctx_info(ctx);
739 if (!yanglib) {
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200740 ERR("Failed to get ietf-yang-library data from the context.");
Michal Vasko086311b2016-01-08 09:53:11 +0100741 return NULL;
742 }
743
744 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100745 if (!cpblts) {
746 ERRMEM;
747 return NULL;
748 }
Michal Vasko086311b2016-01-08 09:53:11 +0100749 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
750 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
751 count = 2;
752
753 /* capabilities */
754
755 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
756 if (mod) {
757 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100758 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100759 }
760 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100761 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100762 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100763 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100764 }
765 }
766 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100767 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100768 }
769 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100770 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100771 }
772 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100773 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100774 }
775 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100776 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100777 }
778 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100779 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100780 }
781 }
782
783 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
784 if (mod) {
785 if (!server_opts.wd_basic_mode) {
786 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
787 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100788 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100789 switch (server_opts.wd_basic_mode) {
790 case NC_WD_ALL:
791 strcat(str, "?basic-mode=report-all");
792 break;
793 case NC_WD_TRIM:
794 strcat(str, "?basic-mode=trim");
795 break;
796 case NC_WD_EXPLICIT:
797 strcat(str, "?basic-mode=explicit");
798 break;
799 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100800 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100801 break;
802 }
803
804 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200805 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100806 if (server_opts.wd_also_supported & NC_WD_ALL) {
807 strcat(str, "report-all,");
808 }
809 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
810 strcat(str, "report-all-tagged,");
811 }
812 if (server_opts.wd_also_supported & NC_WD_TRIM) {
813 strcat(str, "trim,");
814 }
815 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
816 strcat(str, "explicit,");
817 }
818 str[strlen(str) - 1] = '\0';
819
820 add_cpblt(ctx, str, &cpblts, &size, &count);
821 }
822 }
823 }
824
Radek Krejci658782b2016-12-04 22:04:55 +0100825 /* other capabilities */
826 for (u = 0; u < server_opts.capabilities_count; u++) {
827 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100828 }
829
830 /* models */
Michal Vasko086311b2016-01-08 09:53:11 +0100831 LY_TREE_FOR(yanglib->child, child) {
Michal Vaskodd9fe652016-09-14 09:24:32 +0200832 if (!module_set_id) {
833 if (strcmp(child->prev->schema->name, "module-set-id")) {
834 ERRINT;
Michal Vaskoa8a66b62016-10-05 14:14:19 +0200835 free(cpblts);
836 free(deviations);
Michal Vaskodd9fe652016-09-14 09:24:32 +0200837 return NULL;
838 }
839 module_set_id = (struct lyd_node_leaf_list *)child->prev;
840 }
Michal Vasko086311b2016-01-08 09:53:11 +0100841 if (!strcmp(child->schema->name, "module")) {
842 LY_TREE_FOR(child->child, child2) {
843 if (!strcmp(child2->schema->name, "namespace")) {
844 ns = (struct lyd_node_leaf_list *)child2;
845 } else if (!strcmp(child2->schema->name, "name")) {
846 name = (struct lyd_node_leaf_list *)child2;
847 } else if (!strcmp(child2->schema->name, "revision")) {
848 rev = (struct lyd_node_leaf_list *)child2;
849 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100850 features = nc_realloc(features, ++feat_count * sizeof *features);
851 if (!features) {
852 ERRMEM;
853 free(cpblts);
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200854 free(deviations);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100855 return NULL;
856 }
Michal Vasko086311b2016-01-08 09:53:11 +0100857 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200858 } else if (!strcmp(child2->schema->name, "deviation")) {
859 deviations = nc_realloc(deviations, ++dev_count * sizeof *deviations);
860 if (!deviations) {
861 ERRMEM;
862 free(cpblts);
863 free(features);
864 return NULL;
865 }
Michal Vasko086311b2016-01-08 09:53:11 +0100866 }
867 }
868
869 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100870 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100871 continue;
872 }
873
Radek Krejcidf7ba522016-03-01 16:05:25 +0100874 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
Michal Vasko2e47ef92016-06-20 10:03:24 +0200875 rev->value_str[0] ? "&revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100876 if (feat_count) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200877 strcat(str, "&features=");
878 str_len += 10;
Michal Vasko086311b2016-01-08 09:53:11 +0100879 for (i = 0; i < feat_count; ++i) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200880 if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100881 ERRINT;
882 break;
883 }
Michal Vasko086311b2016-01-08 09:53:11 +0100884 if (i) {
885 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100886 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100887 }
888 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100889 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100890 }
891 }
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200892 if (dev_count) {
893 strcat(str, "&deviations=");
894 str_len += 12;
895 for (i = 0; i < dev_count; ++i) {
896 if (str_len + 1 + strlen(deviations[i]->value_str) >= NC_CPBLT_BUF_LEN) {
897 ERRINT;
898 break;
899 }
900 if (i) {
901 strcat(str, ",");
902 ++str_len;
903 }
904 strcat(str, deviations[i]->value_str);
905 str_len += strlen(deviations[i]->value_str);
906 }
907 }
Michal Vaskodd9fe652016-09-14 09:24:32 +0200908 if (!strcmp(name->value_str, "ietf-yang-library")) {
Michal Vasko7a7d5742017-03-21 15:33:23 +0100909 sprintf(str + str_len, "&module-set-id=%s", module_set_id->value_str);
Michal Vaskodd9fe652016-09-14 09:24:32 +0200910 }
Michal Vasko086311b2016-01-08 09:53:11 +0100911
912 add_cpblt(ctx, str, &cpblts, &size, &count);
913
914 ns = NULL;
915 name = NULL;
916 rev = NULL;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200917 if (features || feat_count) {
918 free(features);
919 features = NULL;
920 feat_count = 0;
921 }
922 if (deviations || dev_count) {
923 free(deviations);
924 deviations = NULL;
925 dev_count = 0;
926 }
Michal Vasko086311b2016-01-08 09:53:11 +0100927 }
928 }
929
930 lyd_free(yanglib);
931
932 /* ending NULL capability */
933 add_cpblt(ctx, NULL, &cpblts, &size, &count);
934
935 return cpblts;
936}
937
Radek Krejci695d4fa2015-10-22 13:23:54 +0200938static int
Michal Vasko2cb24b42017-05-23 14:59:11 +0200939parse_cpblts(struct lyxml_elem *xml, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200940{
941 struct lyxml_elem *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +0200942 int ver = -1, i = 0;
943 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200944
945 if (list) {
946 /* get the storage for server's capabilities */
947 LY_TREE_FOR(xml->child, cpblt) {
948 i++;
949 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100950 /* last item remains NULL */
951 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200952 if (!*list) {
953 ERRMEM;
954 return -1;
955 }
956 i = 0;
957 }
958
959 LY_TREE_FOR(xml->child, cpblt) {
960 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
961 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
962 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
963 return -1;
964 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
965 continue;
966 }
967
Michal Vasko156d3272017-04-11 11:46:49 +0200968 /* skip leading/trailing whitespaces */
969 for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start);
970 for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end);
971 if (!cpb_start[0] || (cpb_end == cpblt->content)) {
972 ERR("Empty capability \"%s\" received.", cpblt->content);
973 return -1;
974 }
975
Radek Krejci695d4fa2015-10-22 13:23:54 +0200976 /* detect NETCONF version */
Michal Vasko156d3272017-04-11 11:46:49 +0200977 if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200978 ver = 0;
Michal Vasko156d3272017-04-11 11:46:49 +0200979 } 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 +0200980 ver = 1;
981 }
982
983 /* store capabilities */
984 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +0200985 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
986 if (!(*list)[i]) {
987 ERRMEM;
988 return -1;
989 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200990 i++;
991 }
992 }
993
994 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100995 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +0200996 }
997
998 return ver;
999}
1000
1001static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +01001002nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001003{
1004 int r, i;
1005 const char **cpblts;
1006
Michal Vasko11d142a2016-01-19 15:58:24 +01001007 /* client side hello - send only NETCONF base capabilities */
1008 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001009 if (!cpblts) {
1010 ERRMEM;
1011 return NC_MSG_ERROR;
1012 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001013 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
1014 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
1015 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001016
Michal Vasko11d142a2016-01-19 15:58:24 +01001017 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001018
Michal Vasko086311b2016-01-08 09:53:11 +01001019 for (i = 0; cpblts[i]; ++i) {
1020 lydict_remove(session->ctx, cpblts[i]);
1021 }
1022 free(cpblts);
1023
1024 if (r) {
1025 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001026 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001027
1028 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +01001029}
1030
1031static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +01001032nc_send_server_hello(struct nc_session *session)
1033{
1034 int r, i;
1035 const char **cpblts;
1036
Michal Vasko4ffa3b22016-05-24 16:36:25 +02001037 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +01001038
1039 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
1040
Michal Vasko11d142a2016-01-19 15:58:24 +01001041 for (i = 0; cpblts[i]; ++i) {
1042 lydict_remove(session->ctx, cpblts[i]);
1043 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001044 free(cpblts);
1045
1046 if (r) {
1047 return NC_MSG_ERROR;
1048 }
1049
1050 return NC_MSG_HELLO;
1051}
1052
1053static NC_MSG_TYPE
1054nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001055{
1056 struct lyxml_elem *xml = NULL, *node;
1057 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1058 int ver = -1;
1059 char *str;
1060 long long int id;
1061 int flag = 0;
1062
Michal Vasko05ba9df2016-01-13 14:40:27 +01001063 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001064
1065 switch(msgtype) {
1066 case NC_MSG_HELLO:
1067 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +01001068 LY_TREE_FOR(xml->child, node) {
1069 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1070 continue;
1071 } else if (!strcmp(node->name, "session-id")) {
1072 if (!node->content || !strlen(node->content)) {
1073 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001074 goto error;
1075 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001076 str = NULL;
1077 id = strtoll(node->content, &str, 10);
1078 if (*str || id < 1 || id > UINT32_MAX) {
1079 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001080 goto error;
1081 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001082 session->id = (uint32_t)id;
1083 continue;
1084 } else if (strcmp(node->name, "capabilities")) {
1085 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001086 goto error;
1087 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001088
1089 if (flag) {
1090 /* multiple capabilities elements */
1091 ERR("Invalid <hello> message (multiple <capabilities> elements).");
1092 goto error;
1093 }
1094 flag = 1;
1095
Michal Vasko2e6defd2016-10-07 15:48:15 +02001096 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001097 goto error;
1098 }
1099 session->version = ver;
1100 }
1101
1102 if (!session->id) {
1103 ERR("Missing <session-id> in server's <hello>.");
1104 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001105 }
1106 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001107 case NC_MSG_WOULDBLOCK:
1108 ERR("Server's <hello> timeout elapsed.");
1109 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001110 case NC_MSG_ERROR:
1111 /* nothing special, just pass it out */
1112 break;
1113 default:
1114 ERR("Unexpected message received instead of <hello>.");
1115 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001116 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001117 }
1118
1119 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001120 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001121
1122 return msgtype;
1123
1124error:
1125 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001126 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001127
1128 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +02001129}
1130
Michal Vasko11d142a2016-01-19 15:58:24 +01001131static NC_MSG_TYPE
1132nc_recv_server_hello(struct nc_session *session)
1133{
1134 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +02001135 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +01001136 int ver = -1;
1137 int flag = 0;
1138
fanchanghu75888b62017-08-01 19:51:20 +08001139 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 +01001140
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001141 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001142 case NC_MSG_HELLO:
1143 /* get know NETCONF version */
1144 LY_TREE_FOR(xml->child, node) {
1145 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1146 continue;
1147 } else if (strcmp(node->name, "capabilities")) {
1148 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +02001149 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001150 goto cleanup;
1151 }
1152
1153 if (flag) {
1154 /* multiple capabilities elements */
1155 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +02001156 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001157 goto cleanup;
1158 }
1159 flag = 1;
1160
1161 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001162 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001163 goto cleanup;
1164 }
1165 session->version = ver;
1166 }
1167 break;
1168 case NC_MSG_ERROR:
1169 /* nothing special, just pass it out */
1170 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001171 case NC_MSG_WOULDBLOCK:
1172 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001173 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001174 default:
1175 ERR("Unexpected message received instead of <hello>.");
1176 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001177 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001178 }
1179
1180cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +01001181 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001182
1183 return msgtype;
1184}
1185
Michal Vasko71090fc2016-05-24 16:37:28 +02001186NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +01001187nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001188{
Michal Vasko086311b2016-01-08 09:53:11 +01001189 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001190
Michal Vasko11d142a2016-01-19 15:58:24 +01001191 if (session->side == NC_CLIENT) {
1192 type = nc_send_client_hello(session);
1193 } else {
1194 type = nc_send_server_hello(session);
1195 }
1196
Michal Vasko086311b2016-01-08 09:53:11 +01001197 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001198 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001199 }
1200
Michal Vasko11d142a2016-01-19 15:58:24 +01001201 if (session->side == NC_CLIENT) {
1202 type = nc_recv_client_hello(session);
1203 } else {
1204 type = nc_recv_server_hello(session);
1205 }
1206
Michal Vasko71090fc2016-05-24 16:37:28 +02001207 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001208}
Michal Vasko086311b2016-01-08 09:53:11 +01001209
Radek Krejci53691be2016-02-22 13:58:37 +01001210#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +01001211
Michal Vasko8f0c0282016-02-29 10:17:14 +01001212static void
Michal Vasko086311b2016-01-08 09:53:11 +01001213nc_ssh_init(void)
1214{
1215 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1216 ssh_init();
Michal Vasko086311b2016-01-08 09:53:11 +01001217}
1218
Michal Vasko8f0c0282016-02-29 10:17:14 +01001219static void
Michal Vasko086311b2016-01-08 09:53:11 +01001220nc_ssh_destroy(void)
1221{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001222 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001223 ENGINE_cleanup();
1224 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001225 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001226 ssh_finalize();
1227}
1228
Radek Krejci53691be2016-02-22 13:58:37 +01001229#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001230
Radek Krejci53691be2016-02-22 13:58:37 +01001231#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001232
Michal Vasko770b4362017-02-17 14:44:18 +01001233#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1234
Michal Vaskof0c92c02016-01-29 09:41:45 +01001235struct CRYPTO_dynlock_value {
1236 pthread_mutex_t lock;
1237};
1238
Michal Vaskof0c92c02016-01-29 09:41:45 +01001239static struct CRYPTO_dynlock_value *
1240tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1241{
1242 struct CRYPTO_dynlock_value *value;
1243
1244 value = malloc(sizeof *value);
1245 if (!value) {
1246 ERRMEM;
1247 return NULL;
1248 }
1249 pthread_mutex_init(&value->lock, NULL);
1250
1251 return value;
1252}
1253
1254static void
1255tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1256{
1257 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1258 * I found ignored this fact, what do I know... */
1259 if (mode & CRYPTO_LOCK) {
1260 pthread_mutex_lock(&l->lock);
1261 } else {
1262 pthread_mutex_unlock(&l->lock);
1263 }
1264}
1265
1266static void
1267tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1268{
1269 pthread_mutex_destroy(&l->lock);
1270 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001271}
Michal Vasko770b4362017-02-17 14:44:18 +01001272#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001273
Michal Vasko8f0c0282016-02-29 10:17:14 +01001274#endif /* NC_ENABLED_TLS */
1275
1276#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1277
Michal Vasko770b4362017-02-17 14:44:18 +01001278#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001279static pthread_mutex_t *tls_locks;
1280
1281static void
1282tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1283{
1284 if (mode & CRYPTO_LOCK) {
1285 pthread_mutex_lock(tls_locks + n);
1286 } else {
1287 pthread_mutex_unlock(tls_locks + n);
1288 }
1289}
1290
1291static void
1292tls_thread_id_func(CRYPTO_THREADID *tid)
1293{
1294 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1295}
Michal Vasko770b4362017-02-17 14:44:18 +01001296#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001297
1298static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001299nc_tls_init(void)
1300{
1301 int i;
1302
1303 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001304 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001305 SSL_library_init();
1306
Michal Vasko770b4362017-02-17 14:44:18 +01001307#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001308 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001309 if (!tls_locks) {
1310 ERRMEM;
1311 return;
1312 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001313 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1314 pthread_mutex_init(tls_locks + i, NULL);
1315 }
1316
Michal Vaskof0c92c02016-01-29 09:41:45 +01001317 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001318 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001319
1320 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1321 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1322 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001323#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001324}
1325
Michal Vasko8f0c0282016-02-29 10:17:14 +01001326static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001327nc_tls_destroy(void)
1328{
1329 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001330
Michal Vasko8f0c0282016-02-29 10:17:14 +01001331 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001332 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001333 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001334 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001335 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001336#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001337 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001338#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1339 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001340#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001341
Michal Vasko770b4362017-02-17 14:44:18 +01001342#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskob6e37262016-02-25 14:49:00 +01001343 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001344 CRYPTO_set_locking_callback(NULL);
1345 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1346 pthread_mutex_destroy(tls_locks + i);
1347 }
1348 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001349
1350 CRYPTO_set_dynlock_create_callback(NULL);
1351 CRYPTO_set_dynlock_lock_callback(NULL);
1352 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001353#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001354}
1355
Michal Vasko8f0c0282016-02-29 10:17:14 +01001356#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001357
Radek Krejci53691be2016-02-22 13:58:37 +01001358#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001359
Michal Vasko8f0c0282016-02-29 10:17:14 +01001360static void
Michal Vasko5e228792016-02-03 15:30:24 +01001361nc_ssh_tls_init(void)
1362{
1363 SSL_load_error_strings();
1364 ERR_load_BIO_strings();
1365 SSL_library_init();
1366
1367 nc_ssh_init();
1368
Michal Vasko770b4362017-02-17 14:44:18 +01001369#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001370 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1371 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1372 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001373#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001374}
1375
Michal Vasko8f0c0282016-02-29 10:17:14 +01001376static void
Michal Vasko5e228792016-02-03 15:30:24 +01001377nc_ssh_tls_destroy(void)
1378{
1379 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001380#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001381 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001382#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1383 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001384#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001385
1386 nc_ssh_destroy();
1387
Michal Vasko770b4362017-02-17 14:44:18 +01001388#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001389 CRYPTO_set_dynlock_create_callback(NULL);
1390 CRYPTO_set_dynlock_lock_callback(NULL);
1391 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001392#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001393}
1394
Radek Krejci53691be2016-02-22 13:58:37 +01001395#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001396
1397#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1398
1399API void
1400nc_thread_destroy(void)
1401{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001402 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1403 //CRYPTO_cleanup_all_ex_data();
1404
Michal Vasko770b4362017-02-17 14:44:18 +01001405#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1406 CRYPTO_THREADID crypto_tid;
1407
Michal Vasko8f0c0282016-02-29 10:17:14 +01001408 CRYPTO_THREADID_current(&crypto_tid);
1409 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001410#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001411}
1412
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001413#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1414
1415void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001416nc_init(void)
1417{
1418#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1419 nc_ssh_tls_init();
1420#elif defined(NC_ENABLED_SSH)
1421 nc_ssh_init();
1422#elif defined(NC_ENABLED_TLS)
1423 nc_tls_init();
1424#endif
1425}
1426
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001427void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001428nc_destroy(void)
1429{
1430#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1431 nc_ssh_tls_destroy();
1432#elif defined(NC_ENABLED_SSH)
1433 nc_ssh_destroy();
1434#elif defined(NC_ENABLED_TLS)
1435 nc_tls_destroy();
1436#endif
1437}