blob: 6ee961ec391e012fbedf879ab06dc5ae509c5093 [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
Michal Vasko142cfea2017-08-07 10:12:11 +0200297nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100298{
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
Michal Vasko142cfea2017-08-07 10:12:11 +0200308nc_session_get_killed_by(const struct nc_session *session)
309{
310 if (!session) {
311 ERRARG("session");
312 return 0;
313 }
314
315 return session->killed_by;
316}
317
318API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100319nc_session_get_id(const struct nc_session *session)
320{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100321 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200322 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100323 return 0;
324 }
325
Michal Vasko8dadf782016-01-15 10:29:36 +0100326 return session->id;
327}
328
Michal Vasko174fe8e2016-02-17 15:38:09 +0100329API int
330nc_session_get_version(const struct nc_session *session)
331{
332 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200333 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100334 return -1;
335 }
336
337 return (session->version == NC_VERSION_10 ? 0 : 1);
338}
339
Michal Vasko8dadf782016-01-15 10:29:36 +0100340API NC_TRANSPORT_IMPL
341nc_session_get_ti(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 0;
346 }
347
Michal Vasko8dadf782016-01-15 10:29:36 +0100348 return session->ti_type;
349}
350
351API const char *
352nc_session_get_username(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->username;
360}
361
362API const char *
363nc_session_get_host(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 NULL;
368 }
369
Michal Vasko8dadf782016-01-15 10:29:36 +0100370 return session->host;
371}
372
373API uint16_t
374nc_session_get_port(const struct nc_session *session)
375{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100376 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200377 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100378 return 0;
379 }
380
Michal Vasko8dadf782016-01-15 10:29:36 +0100381 return session->port;
382}
383
Michal Vasko9a25e932016-02-01 10:36:42 +0100384API struct ly_ctx *
385nc_session_get_ctx(const struct nc_session *session)
386{
387 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200388 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100389 return NULL;
390 }
391
392 return session->ctx;
393}
394
Michal Vasko2cc4c682016-03-01 09:16:48 +0100395API void
396nc_session_set_data(struct nc_session *session, void *data)
397{
398 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200399 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100400 return;
401 }
402
403 session->data = data;
404}
405
406API void *
407nc_session_get_data(const struct nc_session *session)
408{
409 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200410 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100411 return NULL;
412 }
413
414 return session->data;
415}
416
Michal Vasko086311b2016-01-08 09:53:11 +0100417NC_MSG_TYPE
418nc_send_msg(struct nc_session *session, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200419{
Michal Vasko086311b2016-01-08 09:53:11 +0100420 int r;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200421
Michal Vasko086311b2016-01-08 09:53:11 +0100422 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100423 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
424 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100425 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100426 }
427
Michal Vasko086311b2016-01-08 09:53:11 +0100428 r = nc_write_msg(session, NC_MSG_RPC, op, NULL);
429
430 if (r) {
431 return NC_MSG_ERROR;
432 }
433
434 return NC_MSG_RPC;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100435}
436
Radek Krejci695d4fa2015-10-22 13:23:54 +0200437API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100438nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200439{
Michal Vasko9e99f012016-03-03 13:25:20 +0100440 int r, i, locked;
Michal Vasko428087d2016-01-14 16:04:28 +0100441 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100442 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoa8ad4482016-01-28 14:25:54 +0100443 pthread_t tid;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100444 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100445 struct nc_msg_cont *contiter;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100446 struct lyxml_elem *rpl, *child;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200447 struct lyd_node *close_rpc;
Michal Vaskoad611702015-12-03 13:41:51 +0100448 const struct lys_module *ietfnc;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200449 void *p;
450
Michal Vasko428087d2016-01-14 16:04:28 +0100451 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200452 return;
453 }
454
Michal Vasko86d357c2016-03-11 13:46:38 +0100455 /* stop notifications loop if any */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200456 if ((session->side == NC_CLIENT) && session->opts.client.ntf_tid) {
457 tid = *session->opts.client.ntf_tid;
458 free((pthread_t *)session->opts.client.ntf_tid);
459 session->opts.client.ntf_tid = NULL;
Michal Vasko86d357c2016-03-11 13:46:38 +0100460 /* the thread now knows it should quit */
461
462 pthread_join(tid, NULL);
463 }
464
Michal Vaskoadd4c792015-10-26 15:36:58 +0100465 if (session->ti_lock) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100466 r = nc_session_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100467 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100468 return;
Michal Vasko9e99f012016-03-03 13:25:20 +0100469 } else if (!r) {
470 /* we failed to lock it, too bad */
471 locked = 0;
472 } else {
473 locked = 1;
Michal Vaskoadd4c792015-10-26 15:36:58 +0100474 }
Michal Vasko5ecbf8c2016-03-29 16:05:22 +0200475 } else {
476 ERRINT;
477 return;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200478 }
479
Michal Vasko9e99f012016-03-03 13:25:20 +0100480 if ((session->side == NC_CLIENT) && (session->status == NC_STATUS_RUNNING) && locked) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200481 /* cleanup message queues */
482 /* notifications */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200483 for (contiter = session->opts.client.notifs; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100484 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200485
Michal Vaskoad611702015-12-03 13:41:51 +0100486 p = contiter;
487 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200488 free(p);
489 }
490
491 /* rpc replies */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200492 for (contiter = session->opts.client.replies; contiter; ) {
Michal Vaskoad611702015-12-03 13:41:51 +0100493 lyxml_free(session->ctx, contiter->msg);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200494
Michal Vaskoad611702015-12-03 13:41:51 +0100495 p = contiter;
496 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200497 free(p);
498 }
499
500 /* send closing info to the other side */
501 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL);
502 if (!ietfnc) {
Michal Vasko428087d2016-01-14 16:04:28 +0100503 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200504 } else {
505 close_rpc = lyd_new(NULL, ietfnc, "close-session");
Michal Vaskoad611702015-12-03 13:41:51 +0100506 nc_send_msg(session, close_rpc);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200507 lyd_free(close_rpc);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100508 switch (nc_read_msg_poll(session, NC_CLOSE_REPLY_TIMEOUT, &rpl)) {
Michal Vaskofad6e912015-10-26 15:37:22 +0100509 case NC_MSG_REPLY:
510 LY_TREE_FOR(rpl->child, child) {
511 if (!strcmp(child->name, "ok") && child->ns && !strcmp(child->ns->value, NC_NS_BASE)) {
512 break;
513 }
514 }
515 if (!child) {
Michal Vasko428087d2016-01-14 16:04:28 +0100516 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100517 }
Michal Vaskoad611702015-12-03 13:41:51 +0100518 lyxml_free(session->ctx, rpl);
Michal Vaskofad6e912015-10-26 15:37:22 +0100519 break;
520 case NC_MSG_WOULDBLOCK:
Michal Vasko428087d2016-01-14 16:04:28 +0100521 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100522 break;
523 case NC_MSG_ERROR:
Michal Vaskod083db62016-01-19 10:31:29 +0100524 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
Michal Vaskofad6e912015-10-26 15:37:22 +0100525 break;
526 default:
527 /* cannot happen */
528 break;
529 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200530 }
531
532 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200533 if (session->opts.client.cpblts) {
534 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200535 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200536 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200537 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200538 }
539 }
540
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100541 if (session->data && data_free) {
542 data_free(session->data);
543 }
544
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200545 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
546 /* CH LOCK */
547 pthread_mutex_lock(session->opts.server.ch_lock);
548 }
549
Michal Vasko86d357c2016-03-11 13:46:38 +0100550 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200551 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200552
553 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
554 pthread_cond_signal(session->opts.server.ch_cond);
555
556 /* CH UNLOCK */
557 pthread_mutex_unlock(session->opts.server.ch_lock);
558 }
559
Michal Vasko428087d2016-01-14 16:04:28 +0100560 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200561
562 /* transport implementation cleanup */
563 switch (session->ti_type) {
564 case NC_TI_FD:
565 /* nothing needed - file descriptors were provided by caller,
566 * so it is up to the caller to close them correctly
567 * TODO use callbacks
568 */
Michal Vasko3512e402016-01-28 16:22:34 +0100569 /* just to avoid compiler warning */
570 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100571 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200572 break;
573
Radek Krejci53691be2016-02-22 13:58:37 +0100574#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200575 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100576 if (connected) {
577 ssh_channel_free(session->ti.libssh.channel);
578 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200579 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
580 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
581 * it.
582 */
Michal Vasko96164bf2016-01-21 15:41:58 +0100583 multisession = 0;
584 if (session->ti.libssh.next) {
585 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
586 if (siter->status != NC_STATUS_STARTING) {
587 multisession = 1;
588 break;
589 }
590 }
591 }
592
593 if (!multisession) {
594 /* it's not multisession yet, but we still need to free the starting sessions */
595 if (session->ti.libssh.next) {
596 do {
597 siter = session->ti.libssh.next;
598 session->ti.libssh.next = siter->ti.libssh.next;
599
600 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100601 lydict_remove(session->ctx, session->username);
602 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100603 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100604 ly_ctx_destroy(session->ctx, NULL);
Michal Vasko96164bf2016-01-21 15:41:58 +0100605 }
606
607 free(siter);
608 } while (session->ti.libssh.next != session);
609 }
Michal Vasko428087d2016-01-14 16:04:28 +0100610 if (connected) {
611 ssh_disconnect(session->ti.libssh.session);
612 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200613 ssh_free(session->ti.libssh.session);
614 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200615 /* remove the session from the list */
616 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next);
Michal Vaskoaec4f212015-10-26 15:37:45 +0100617 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200618 /* there will be only one session */
619 siter->ti.libssh.next = NULL;
620 } else {
621 /* there are still multiple sessions, keep the ring list */
622 siter->ti.libssh.next = session->ti.libssh.next;
623 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100624 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
625 if (session->flags & NC_SESSION_SSH_MSG_CB) {
626 for (siter = session->ti.libssh.next; siter->status != NC_STATUS_RUNNING; siter = siter->ti.libssh.next) {
627 if (siter->ti.libssh.next == session) {
628 ERRINT;
629 break;
630 }
631 }
632 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
633 siter->flags |= NC_SESSION_SSH_MSG_CB;
634 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200635 }
636 break;
637#endif
638
Radek Krejci53691be2016-02-22 13:58:37 +0100639#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200640 case NC_TI_OPENSSL:
Michal Vasko428087d2016-01-14 16:04:28 +0100641 if (connected) {
642 SSL_shutdown(session->ti.tls);
643 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200644 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100645
Michal Vasko2e6defd2016-10-07 15:48:15 +0200646 if (session->side == NC_SERVER) {
647 X509_free(session->opts.server.client_cert);
648 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200649 break;
650#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100651 case NC_TI_NONE:
652 ERRINT;
653 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200654 }
Michal Vasko428087d2016-01-14 16:04:28 +0100655
Radek Krejciac6d3472015-10-22 15:47:18 +0200656 lydict_remove(session->ctx, session->username);
657 lydict_remove(session->ctx, session->host);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200658
659 /* final cleanup */
Michal Vaskoadd4c792015-10-26 15:36:58 +0100660 if (session->ti_lock) {
Michal Vasko9e99f012016-03-03 13:25:20 +0100661 if (locked) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100662 nc_session_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100663 }
Michal Vaskob48aa812016-01-18 14:13:09 +0100664 if (!multisession) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100665 pthread_mutex_destroy(session->ti_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100666 pthread_cond_destroy(session->ti_cond);
Michal Vaskoadd4c792015-10-26 15:36:58 +0100667 free(session->ti_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100668 free(session->ti_cond);
669 free((int *)session->ti_inuse);
Michal Vaskoadd4c792015-10-26 15:36:58 +0100670 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200671 }
672
673 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Radek Krejci4ba285b2016-02-04 17:34:06 +0100674 ly_ctx_destroy(session->ctx, NULL);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200675 }
676
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200677 if (session->side == NC_SERVER) {
678 if (session->opts.server.ch_cond) {
679 pthread_cond_destroy(session->opts.server.ch_cond);
680 free(session->opts.server.ch_cond);
681 }
682 if (session->opts.server.ch_lock) {
683 pthread_mutex_destroy(session->opts.server.ch_lock);
684 free(session->opts.server.ch_lock);
685 }
686 }
687
Radek Krejci695d4fa2015-10-22 13:23:54 +0200688 free(session);
689}
690
Michal Vasko086311b2016-01-08 09:53:11 +0100691static void
692add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
693{
Radek Krejci658782b2016-12-04 22:04:55 +0100694 size_t len;
695 int i;
696 char *p;
697
698 if (capab) {
699 /* check if already present */
700 p = strchr(capab, '?');
701 if (p) {
702 len = p - capab;
703 } else {
704 len = strlen(capab);
705 }
706 for (i = 0; i < *count; i++) {
fanchanghu5244bf42017-03-13 16:27:48 +0800707 if (!strncmp((*cpblts)[i], capab, len) && ((*cpblts)[i][len] == '\0' || (*cpblts)[i][len] == '?')) {
Radek Krejci658782b2016-12-04 22:04:55 +0100708 /* already present, do not duplicate it */
709 return;
710 }
711 }
712 }
713
714 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100715 if (*count == *size) {
716 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100717 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
718 if (!(*cpblts)) {
719 ERRMEM;
720 return;
721 }
Michal Vasko086311b2016-01-08 09:53:11 +0100722 }
723
724 if (capab) {
725 (*cpblts)[*count] = lydict_insert(ctx, capab, 0);
726 } else {
727 (*cpblts)[*count] = NULL;
728 }
729 ++(*count);
730}
731
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200732API const char **
733nc_server_get_cpblts(struct ly_ctx *ctx)
Michal Vasko086311b2016-01-08 09:53:11 +0100734{
735 struct lyd_node *child, *child2, *yanglib;
Michal Vaskodd9fe652016-09-14 09:24:32 +0200736 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 +0100737 const char **cpblts;
738 const struct lys_module *mod;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200739 int size = 10, count, feat_count = 0, dev_count = 0, i, str_len;
Radek Krejci658782b2016-12-04 22:04:55 +0100740 unsigned int u;
Michal Vasko2e47ef92016-06-20 10:03:24 +0200741#define NC_CPBLT_BUF_LEN 512
742 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100743
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200744 if (!ctx) {
745 ERRARG("ctx");
746 return NULL;
747 }
748
Michal Vasko086311b2016-01-08 09:53:11 +0100749 yanglib = ly_ctx_info(ctx);
750 if (!yanglib) {
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200751 ERR("Failed to get ietf-yang-library data from the context.");
Michal Vasko086311b2016-01-08 09:53:11 +0100752 return NULL;
753 }
754
755 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100756 if (!cpblts) {
757 ERRMEM;
758 return NULL;
759 }
Michal Vasko086311b2016-01-08 09:53:11 +0100760 cpblts[0] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0);
761 cpblts[1] = lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0);
762 count = 2;
763
764 /* capabilities */
765
766 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL);
767 if (mod) {
768 if (lys_features_state(mod, "writable-running") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100769 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100770 }
771 if (lys_features_state(mod, "candidate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100772 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100773 if (lys_features_state(mod, "confirmed-commit") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100774 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100775 }
776 }
777 if (lys_features_state(mod, "rollback-on-error") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100778 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100779 }
780 if (lys_features_state(mod, "validate") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100781 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100782 }
783 if (lys_features_state(mod, "startup") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100784 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100785 }
786 if (lys_features_state(mod, "url") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100787 add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100788 }
789 if (lys_features_state(mod, "xpath") == 1) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100790 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100791 }
792 }
793
794 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL);
795 if (mod) {
796 if (!server_opts.wd_basic_mode) {
797 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
798 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100799 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100800 switch (server_opts.wd_basic_mode) {
801 case NC_WD_ALL:
802 strcat(str, "?basic-mode=report-all");
803 break;
804 case NC_WD_TRIM:
805 strcat(str, "?basic-mode=trim");
806 break;
807 case NC_WD_EXPLICIT:
808 strcat(str, "?basic-mode=explicit");
809 break;
810 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100811 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100812 break;
813 }
814
815 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200816 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100817 if (server_opts.wd_also_supported & NC_WD_ALL) {
818 strcat(str, "report-all,");
819 }
820 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
821 strcat(str, "report-all-tagged,");
822 }
823 if (server_opts.wd_also_supported & NC_WD_TRIM) {
824 strcat(str, "trim,");
825 }
826 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
827 strcat(str, "explicit,");
828 }
829 str[strlen(str) - 1] = '\0';
830
831 add_cpblt(ctx, str, &cpblts, &size, &count);
832 }
833 }
834 }
835
Radek Krejci658782b2016-12-04 22:04:55 +0100836 /* other capabilities */
837 for (u = 0; u < server_opts.capabilities_count; u++) {
838 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100839 }
840
841 /* models */
Michal Vasko086311b2016-01-08 09:53:11 +0100842 LY_TREE_FOR(yanglib->child, child) {
Michal Vaskodd9fe652016-09-14 09:24:32 +0200843 if (!module_set_id) {
844 if (strcmp(child->prev->schema->name, "module-set-id")) {
845 ERRINT;
Michal Vaskoa8a66b62016-10-05 14:14:19 +0200846 free(cpblts);
847 free(deviations);
Michal Vaskodd9fe652016-09-14 09:24:32 +0200848 return NULL;
849 }
850 module_set_id = (struct lyd_node_leaf_list *)child->prev;
851 }
Michal Vasko086311b2016-01-08 09:53:11 +0100852 if (!strcmp(child->schema->name, "module")) {
853 LY_TREE_FOR(child->child, child2) {
854 if (!strcmp(child2->schema->name, "namespace")) {
855 ns = (struct lyd_node_leaf_list *)child2;
856 } else if (!strcmp(child2->schema->name, "name")) {
857 name = (struct lyd_node_leaf_list *)child2;
858 } else if (!strcmp(child2->schema->name, "revision")) {
859 rev = (struct lyd_node_leaf_list *)child2;
860 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100861 features = nc_realloc(features, ++feat_count * sizeof *features);
862 if (!features) {
863 ERRMEM;
864 free(cpblts);
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200865 free(deviations);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100866 return NULL;
867 }
Michal Vasko086311b2016-01-08 09:53:11 +0100868 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200869 } else if (!strcmp(child2->schema->name, "deviation")) {
870 deviations = nc_realloc(deviations, ++dev_count * sizeof *deviations);
871 if (!deviations) {
872 ERRMEM;
873 free(cpblts);
874 free(features);
875 return NULL;
876 }
Michal Vasko086311b2016-01-08 09:53:11 +0100877 }
878 }
879
880 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100881 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100882 continue;
883 }
884
Radek Krejcidf7ba522016-03-01 16:05:25 +0100885 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
Michal Vasko2e47ef92016-06-20 10:03:24 +0200886 rev->value_str[0] ? "&revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100887 if (feat_count) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200888 strcat(str, "&features=");
889 str_len += 10;
Michal Vasko086311b2016-01-08 09:53:11 +0100890 for (i = 0; i < feat_count; ++i) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200891 if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100892 ERRINT;
893 break;
894 }
Michal Vasko086311b2016-01-08 09:53:11 +0100895 if (i) {
896 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100897 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100898 }
899 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100900 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100901 }
902 }
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200903 if (dev_count) {
904 strcat(str, "&deviations=");
905 str_len += 12;
906 for (i = 0; i < dev_count; ++i) {
907 if (str_len + 1 + strlen(deviations[i]->value_str) >= NC_CPBLT_BUF_LEN) {
908 ERRINT;
909 break;
910 }
911 if (i) {
912 strcat(str, ",");
913 ++str_len;
914 }
915 strcat(str, deviations[i]->value_str);
916 str_len += strlen(deviations[i]->value_str);
917 }
918 }
Michal Vaskodd9fe652016-09-14 09:24:32 +0200919 if (!strcmp(name->value_str, "ietf-yang-library")) {
Michal Vasko7a7d5742017-03-21 15:33:23 +0100920 sprintf(str + str_len, "&module-set-id=%s", module_set_id->value_str);
Michal Vaskodd9fe652016-09-14 09:24:32 +0200921 }
Michal Vasko086311b2016-01-08 09:53:11 +0100922
923 add_cpblt(ctx, str, &cpblts, &size, &count);
924
925 ns = NULL;
926 name = NULL;
927 rev = NULL;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200928 if (features || feat_count) {
929 free(features);
930 features = NULL;
931 feat_count = 0;
932 }
933 if (deviations || dev_count) {
934 free(deviations);
935 deviations = NULL;
936 dev_count = 0;
937 }
Michal Vasko086311b2016-01-08 09:53:11 +0100938 }
939 }
940
941 lyd_free(yanglib);
942
943 /* ending NULL capability */
944 add_cpblt(ctx, NULL, &cpblts, &size, &count);
945
946 return cpblts;
947}
948
Radek Krejci695d4fa2015-10-22 13:23:54 +0200949static int
Michal Vasko2cb24b42017-05-23 14:59:11 +0200950parse_cpblts(struct lyxml_elem *xml, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200951{
952 struct lyxml_elem *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +0200953 int ver = -1, i = 0;
954 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200955
956 if (list) {
957 /* get the storage for server's capabilities */
958 LY_TREE_FOR(xml->child, cpblt) {
959 i++;
960 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100961 /* last item remains NULL */
962 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200963 if (!*list) {
964 ERRMEM;
965 return -1;
966 }
967 i = 0;
968 }
969
970 LY_TREE_FOR(xml->child, cpblt) {
971 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
972 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
973 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
974 return -1;
975 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
976 continue;
977 }
978
Michal Vasko156d3272017-04-11 11:46:49 +0200979 /* skip leading/trailing whitespaces */
980 for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start);
981 for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end);
982 if (!cpb_start[0] || (cpb_end == cpblt->content)) {
983 ERR("Empty capability \"%s\" received.", cpblt->content);
984 return -1;
985 }
986
Radek Krejci695d4fa2015-10-22 13:23:54 +0200987 /* detect NETCONF version */
Michal Vasko156d3272017-04-11 11:46:49 +0200988 if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200989 ver = 0;
Michal Vasko156d3272017-04-11 11:46:49 +0200990 } 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 +0200991 ver = 1;
992 }
993
994 /* store capabilities */
995 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +0200996 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
997 if (!(*list)[i]) {
998 ERRMEM;
999 return -1;
1000 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001001 i++;
1002 }
1003 }
1004
1005 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001006 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001007 }
1008
1009 return ver;
1010}
1011
1012static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +01001013nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001014{
1015 int r, i;
1016 const char **cpblts;
1017
Michal Vasko11d142a2016-01-19 15:58:24 +01001018 /* client side hello - send only NETCONF base capabilities */
1019 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001020 if (!cpblts) {
1021 ERRMEM;
1022 return NC_MSG_ERROR;
1023 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001024 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
1025 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
1026 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001027
Michal Vasko11d142a2016-01-19 15:58:24 +01001028 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001029
Michal Vasko086311b2016-01-08 09:53:11 +01001030 for (i = 0; cpblts[i]; ++i) {
1031 lydict_remove(session->ctx, cpblts[i]);
1032 }
1033 free(cpblts);
1034
1035 if (r) {
1036 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001037 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001038
1039 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +01001040}
1041
1042static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +01001043nc_send_server_hello(struct nc_session *session)
1044{
1045 int r, i;
1046 const char **cpblts;
1047
Michal Vasko4ffa3b22016-05-24 16:36:25 +02001048 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +01001049
1050 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
1051
Michal Vasko11d142a2016-01-19 15:58:24 +01001052 for (i = 0; cpblts[i]; ++i) {
1053 lydict_remove(session->ctx, cpblts[i]);
1054 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001055 free(cpblts);
1056
1057 if (r) {
1058 return NC_MSG_ERROR;
1059 }
1060
1061 return NC_MSG_HELLO;
1062}
1063
1064static NC_MSG_TYPE
1065nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001066{
1067 struct lyxml_elem *xml = NULL, *node;
1068 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1069 int ver = -1;
1070 char *str;
1071 long long int id;
1072 int flag = 0;
1073
Michal Vasko05ba9df2016-01-13 14:40:27 +01001074 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001075
1076 switch(msgtype) {
1077 case NC_MSG_HELLO:
1078 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +01001079 LY_TREE_FOR(xml->child, node) {
1080 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1081 continue;
1082 } else if (!strcmp(node->name, "session-id")) {
1083 if (!node->content || !strlen(node->content)) {
1084 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001085 goto error;
1086 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001087 str = NULL;
1088 id = strtoll(node->content, &str, 10);
1089 if (*str || id < 1 || id > UINT32_MAX) {
1090 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001091 goto error;
1092 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001093 session->id = (uint32_t)id;
1094 continue;
1095 } else if (strcmp(node->name, "capabilities")) {
1096 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001097 goto error;
1098 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001099
1100 if (flag) {
1101 /* multiple capabilities elements */
1102 ERR("Invalid <hello> message (multiple <capabilities> elements).");
1103 goto error;
1104 }
1105 flag = 1;
1106
Michal Vasko2e6defd2016-10-07 15:48:15 +02001107 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001108 goto error;
1109 }
1110 session->version = ver;
1111 }
1112
1113 if (!session->id) {
1114 ERR("Missing <session-id> in server's <hello>.");
1115 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001116 }
1117 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001118 case NC_MSG_WOULDBLOCK:
1119 ERR("Server's <hello> timeout elapsed.");
1120 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001121 case NC_MSG_ERROR:
1122 /* nothing special, just pass it out */
1123 break;
1124 default:
1125 ERR("Unexpected message received instead of <hello>.");
1126 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001127 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001128 }
1129
1130 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001131 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001132
1133 return msgtype;
1134
1135error:
1136 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001137 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001138
1139 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +02001140}
1141
Michal Vasko11d142a2016-01-19 15:58:24 +01001142static NC_MSG_TYPE
1143nc_recv_server_hello(struct nc_session *session)
1144{
1145 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +02001146 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +01001147 int ver = -1;
1148 int flag = 0;
1149
fanchanghu75888b62017-08-01 19:51:20 +08001150 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 +01001151
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001152 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001153 case NC_MSG_HELLO:
1154 /* get know NETCONF version */
1155 LY_TREE_FOR(xml->child, node) {
1156 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1157 continue;
1158 } else if (strcmp(node->name, "capabilities")) {
1159 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Michal Vasko71090fc2016-05-24 16:37:28 +02001160 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001161 goto cleanup;
1162 }
1163
1164 if (flag) {
1165 /* multiple capabilities elements */
1166 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +02001167 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001168 goto cleanup;
1169 }
1170 flag = 1;
1171
1172 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001173 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001174 goto cleanup;
1175 }
1176 session->version = ver;
1177 }
1178 break;
1179 case NC_MSG_ERROR:
1180 /* nothing special, just pass it out */
1181 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001182 case NC_MSG_WOULDBLOCK:
1183 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001184 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001185 default:
1186 ERR("Unexpected message received instead of <hello>.");
1187 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001188 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001189 }
1190
1191cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +01001192 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001193
1194 return msgtype;
1195}
1196
Michal Vasko71090fc2016-05-24 16:37:28 +02001197NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +01001198nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001199{
Michal Vasko086311b2016-01-08 09:53:11 +01001200 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001201
Michal Vasko11d142a2016-01-19 15:58:24 +01001202 if (session->side == NC_CLIENT) {
1203 type = nc_send_client_hello(session);
1204 } else {
1205 type = nc_send_server_hello(session);
1206 }
1207
Michal Vasko086311b2016-01-08 09:53:11 +01001208 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001209 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001210 }
1211
Michal Vasko11d142a2016-01-19 15:58:24 +01001212 if (session->side == NC_CLIENT) {
1213 type = nc_recv_client_hello(session);
1214 } else {
1215 type = nc_recv_server_hello(session);
1216 }
1217
Michal Vasko71090fc2016-05-24 16:37:28 +02001218 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001219}
Michal Vasko086311b2016-01-08 09:53:11 +01001220
Radek Krejci53691be2016-02-22 13:58:37 +01001221#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +01001222
Michal Vasko8f0c0282016-02-29 10:17:14 +01001223static void
Michal Vasko086311b2016-01-08 09:53:11 +01001224nc_ssh_init(void)
1225{
1226 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1227 ssh_init();
Michal Vasko086311b2016-01-08 09:53:11 +01001228}
1229
Michal Vasko8f0c0282016-02-29 10:17:14 +01001230static void
Michal Vasko086311b2016-01-08 09:53:11 +01001231nc_ssh_destroy(void)
1232{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001233 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001234 ENGINE_cleanup();
1235 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001236 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001237 ssh_finalize();
1238}
1239
Radek Krejci53691be2016-02-22 13:58:37 +01001240#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001241
Radek Krejci53691be2016-02-22 13:58:37 +01001242#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001243
Michal Vasko770b4362017-02-17 14:44:18 +01001244#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1245
Michal Vaskof0c92c02016-01-29 09:41:45 +01001246struct CRYPTO_dynlock_value {
1247 pthread_mutex_t lock;
1248};
1249
Michal Vaskof0c92c02016-01-29 09:41:45 +01001250static struct CRYPTO_dynlock_value *
1251tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1252{
1253 struct CRYPTO_dynlock_value *value;
1254
1255 value = malloc(sizeof *value);
1256 if (!value) {
1257 ERRMEM;
1258 return NULL;
1259 }
1260 pthread_mutex_init(&value->lock, NULL);
1261
1262 return value;
1263}
1264
1265static void
1266tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1267{
1268 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1269 * I found ignored this fact, what do I know... */
1270 if (mode & CRYPTO_LOCK) {
1271 pthread_mutex_lock(&l->lock);
1272 } else {
1273 pthread_mutex_unlock(&l->lock);
1274 }
1275}
1276
1277static void
1278tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1279{
1280 pthread_mutex_destroy(&l->lock);
1281 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001282}
Michal Vasko770b4362017-02-17 14:44:18 +01001283#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001284
Michal Vasko8f0c0282016-02-29 10:17:14 +01001285#endif /* NC_ENABLED_TLS */
1286
1287#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1288
Michal Vasko770b4362017-02-17 14:44:18 +01001289#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001290static pthread_mutex_t *tls_locks;
1291
1292static void
1293tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1294{
1295 if (mode & CRYPTO_LOCK) {
1296 pthread_mutex_lock(tls_locks + n);
1297 } else {
1298 pthread_mutex_unlock(tls_locks + n);
1299 }
1300}
1301
1302static void
1303tls_thread_id_func(CRYPTO_THREADID *tid)
1304{
1305 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1306}
Michal Vasko770b4362017-02-17 14:44:18 +01001307#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001308
1309static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001310nc_tls_init(void)
1311{
1312 int i;
1313
1314 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001315 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001316 SSL_library_init();
1317
Michal Vasko770b4362017-02-17 14:44:18 +01001318#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001319 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001320 if (!tls_locks) {
1321 ERRMEM;
1322 return;
1323 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001324 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1325 pthread_mutex_init(tls_locks + i, NULL);
1326 }
1327
Michal Vaskof0c92c02016-01-29 09:41:45 +01001328 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001329 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001330
1331 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1332 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1333 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001334#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001335}
1336
Michal Vasko8f0c0282016-02-29 10:17:14 +01001337static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001338nc_tls_destroy(void)
1339{
1340 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001341
Michal Vasko8f0c0282016-02-29 10:17:14 +01001342 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001343 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001344 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001345 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001346 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001347#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001348 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001349#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1350 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001351#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001352
Michal Vasko770b4362017-02-17 14:44:18 +01001353#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskob6e37262016-02-25 14:49:00 +01001354 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001355 CRYPTO_set_locking_callback(NULL);
1356 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1357 pthread_mutex_destroy(tls_locks + i);
1358 }
1359 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001360
1361 CRYPTO_set_dynlock_create_callback(NULL);
1362 CRYPTO_set_dynlock_lock_callback(NULL);
1363 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001364#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001365}
1366
Michal Vasko8f0c0282016-02-29 10:17:14 +01001367#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001368
Radek Krejci53691be2016-02-22 13:58:37 +01001369#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001370
Michal Vasko8f0c0282016-02-29 10:17:14 +01001371static void
Michal Vasko5e228792016-02-03 15:30:24 +01001372nc_ssh_tls_init(void)
1373{
1374 SSL_load_error_strings();
1375 ERR_load_BIO_strings();
1376 SSL_library_init();
1377
1378 nc_ssh_init();
1379
Michal Vasko770b4362017-02-17 14:44:18 +01001380#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001381 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1382 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1383 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001384#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001385}
1386
Michal Vasko8f0c0282016-02-29 10:17:14 +01001387static void
Michal Vasko5e228792016-02-03 15:30:24 +01001388nc_ssh_tls_destroy(void)
1389{
1390 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001391#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001392 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001393#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1394 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001395#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001396
1397 nc_ssh_destroy();
1398
Michal Vasko770b4362017-02-17 14:44:18 +01001399#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001400 CRYPTO_set_dynlock_create_callback(NULL);
1401 CRYPTO_set_dynlock_lock_callback(NULL);
1402 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001403#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001404}
1405
Radek Krejci53691be2016-02-22 13:58:37 +01001406#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001407
1408#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1409
1410API void
1411nc_thread_destroy(void)
1412{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001413 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1414 //CRYPTO_cleanup_all_ex_data();
1415
Michal Vasko770b4362017-02-17 14:44:18 +01001416#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1417 CRYPTO_THREADID crypto_tid;
1418
Michal Vasko8f0c0282016-02-29 10:17:14 +01001419 CRYPTO_THREADID_current(&crypto_tid);
1420 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001421#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001422}
1423
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001424#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1425
1426void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001427nc_init(void)
1428{
1429#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1430 nc_ssh_tls_init();
1431#elif defined(NC_ENABLED_SSH)
1432 nc_ssh_init();
1433#elif defined(NC_ENABLED_TLS)
1434 nc_tls_init();
1435#endif
1436}
1437
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001438void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001439nc_destroy(void)
1440{
1441#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1442 nc_ssh_tls_destroy();
1443#elif defined(NC_ENABLED_SSH)
1444 nc_ssh_destroy();
1445#elif defined(NC_ENABLED_TLS)
1446 nc_tls_destroy();
1447#endif
1448}