blob: 788e28f5311d5315943e2e9af1723e4c982401b7 [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 */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200501 ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200502 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;
Radek Krejcif906e412017-09-22 14:44:45 +0200758 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100759 }
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
Radek Krejci3222b7d2017-09-21 16:04:30 +0200766 mod = ly_ctx_get_module(ctx, "ietf-netconf", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100767 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
Radek Krejci3222b7d2017-09-21 16:04:30 +0200794 mod = ly_ctx_get_module(ctx, "ietf-netconf-with-defaults", NULL, 1);
Michal Vasko086311b2016-01-08 09:53:11 +0100795 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 */
Radek Krejci3222b7d2017-09-21 16:04:30 +0200842 LY_TREE_FOR(yanglib->prev->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;
Radek Krejcif906e412017-09-22 14:44:45 +0200846 goto error;
Michal Vaskodd9fe652016-09-14 09:24:32 +0200847 }
848 module_set_id = (struct lyd_node_leaf_list *)child->prev;
849 }
Michal Vasko086311b2016-01-08 09:53:11 +0100850 if (!strcmp(child->schema->name, "module")) {
851 LY_TREE_FOR(child->child, child2) {
852 if (!strcmp(child2->schema->name, "namespace")) {
853 ns = (struct lyd_node_leaf_list *)child2;
854 } else if (!strcmp(child2->schema->name, "name")) {
855 name = (struct lyd_node_leaf_list *)child2;
856 } else if (!strcmp(child2->schema->name, "revision")) {
857 rev = (struct lyd_node_leaf_list *)child2;
858 } else if (!strcmp(child2->schema->name, "feature")) {
Michal Vasko4eb3c312016-03-01 14:09:37 +0100859 features = nc_realloc(features, ++feat_count * sizeof *features);
860 if (!features) {
861 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200862 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100863 }
Michal Vasko086311b2016-01-08 09:53:11 +0100864 features[feat_count - 1] = (struct lyd_node_leaf_list *)child2;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200865 } else if (!strcmp(child2->schema->name, "deviation")) {
866 deviations = nc_realloc(deviations, ++dev_count * sizeof *deviations);
867 if (!deviations) {
868 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200869 goto error;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200870 }
Michal Vasko086311b2016-01-08 09:53:11 +0100871 }
872 }
873
874 if (!ns || !name || !rev) {
Michal Vasko9e036d52016-01-08 10:49:26 +0100875 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100876 continue;
877 }
878
Radek Krejcidf7ba522016-03-01 16:05:25 +0100879 str_len = sprintf(str, "%s?module=%s%s%s", ns->value_str, name->value_str,
Michal Vasko2e47ef92016-06-20 10:03:24 +0200880 rev->value_str[0] ? "&revision=" : "", rev->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100881 if (feat_count) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200882 strcat(str, "&features=");
883 str_len += 10;
Michal Vasko086311b2016-01-08 09:53:11 +0100884 for (i = 0; i < feat_count; ++i) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200885 if (str_len + 1 + strlen(features[i]->value_str) >= NC_CPBLT_BUF_LEN) {
Michal Vasko11d142a2016-01-19 15:58:24 +0100886 ERRINT;
887 break;
888 }
Michal Vasko086311b2016-01-08 09:53:11 +0100889 if (i) {
890 strcat(str, ",");
Michal Vasko11d142a2016-01-19 15:58:24 +0100891 ++str_len;
Michal Vasko086311b2016-01-08 09:53:11 +0100892 }
893 strcat(str, features[i]->value_str);
Michal Vasko11d142a2016-01-19 15:58:24 +0100894 str_len += strlen(features[i]->value_str);
Michal Vasko086311b2016-01-08 09:53:11 +0100895 }
896 }
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200897 if (dev_count) {
898 strcat(str, "&deviations=");
899 str_len += 12;
900 for (i = 0; i < dev_count; ++i) {
901 if (str_len + 1 + strlen(deviations[i]->value_str) >= NC_CPBLT_BUF_LEN) {
902 ERRINT;
903 break;
904 }
905 if (i) {
906 strcat(str, ",");
907 ++str_len;
908 }
909 strcat(str, deviations[i]->value_str);
910 str_len += strlen(deviations[i]->value_str);
911 }
912 }
Michal Vaskodd9fe652016-09-14 09:24:32 +0200913 if (!strcmp(name->value_str, "ietf-yang-library")) {
Michal Vasko7a7d5742017-03-21 15:33:23 +0100914 sprintf(str + str_len, "&module-set-id=%s", module_set_id->value_str);
Michal Vaskodd9fe652016-09-14 09:24:32 +0200915 }
Michal Vasko086311b2016-01-08 09:53:11 +0100916
917 add_cpblt(ctx, str, &cpblts, &size, &count);
918
919 ns = NULL;
920 name = NULL;
921 rev = NULL;
Michal Vaskoe90e4d12016-06-20 10:05:01 +0200922 if (features || feat_count) {
923 free(features);
924 features = NULL;
925 feat_count = 0;
926 }
927 if (deviations || dev_count) {
928 free(deviations);
929 deviations = NULL;
930 dev_count = 0;
931 }
Michal Vasko086311b2016-01-08 09:53:11 +0100932 }
933 }
934
Radek Krejcif906e412017-09-22 14:44:45 +0200935 lyd_free_withsiblings(yanglib);
Michal Vasko086311b2016-01-08 09:53:11 +0100936
937 /* ending NULL capability */
938 add_cpblt(ctx, NULL, &cpblts, &size, &count);
939
940 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +0200941
942error:
943
944 free(cpblts);
945 free(features);
946 free(deviations);
947 lyd_free_withsiblings(yanglib);
948 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100949}
950
Radek Krejci695d4fa2015-10-22 13:23:54 +0200951static int
Michal Vasko2cb24b42017-05-23 14:59:11 +0200952parse_cpblts(struct lyxml_elem *xml, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200953{
954 struct lyxml_elem *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +0200955 int ver = -1, i = 0;
956 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200957
958 if (list) {
959 /* get the storage for server's capabilities */
960 LY_TREE_FOR(xml->child, cpblt) {
961 i++;
962 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100963 /* last item remains NULL */
964 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200965 if (!*list) {
966 ERRMEM;
967 return -1;
968 }
969 i = 0;
970 }
971
972 LY_TREE_FOR(xml->child, cpblt) {
973 if (strcmp(cpblt->name, "capability") && cpblt->ns && cpblt->ns->value &&
974 !strcmp(cpblt->ns->value, NC_NS_BASE)) {
975 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name);
976 return -1;
977 } else if (!cpblt->ns || !cpblt->ns->value || strcmp(cpblt->ns->value, NC_NS_BASE)) {
978 continue;
979 }
980
Michal Vasko156d3272017-04-11 11:46:49 +0200981 /* skip leading/trailing whitespaces */
982 for (cpb_start = cpblt->content; isspace(cpb_start[0]); ++cpb_start);
983 for (cpb_end = cpblt->content + strlen(cpblt->content); (cpb_end > cpblt->content) && isspace(cpb_end[-1]); --cpb_end);
984 if (!cpb_start[0] || (cpb_end == cpblt->content)) {
985 ERR("Empty capability \"%s\" received.", cpblt->content);
986 return -1;
987 }
988
Radek Krejci695d4fa2015-10-22 13:23:54 +0200989 /* detect NETCONF version */
Michal Vasko156d3272017-04-11 11:46:49 +0200990 if (ver < 0 && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200991 ver = 0;
Michal Vasko156d3272017-04-11 11:46:49 +0200992 } 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 +0200993 ver = 1;
994 }
995
996 /* store capabilities */
997 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +0200998 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
999 if (!(*list)[i]) {
1000 ERRMEM;
1001 return -1;
1002 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001003 i++;
1004 }
1005 }
1006
1007 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001008 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001009 }
1010
1011 return ver;
1012}
1013
1014static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +01001015nc_send_client_hello(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001016{
1017 int r, i;
1018 const char **cpblts;
1019
Michal Vasko11d142a2016-01-19 15:58:24 +01001020 /* client side hello - send only NETCONF base capabilities */
1021 cpblts = malloc(3 * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001022 if (!cpblts) {
1023 ERRMEM;
1024 return NC_MSG_ERROR;
1025 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001026 cpblts[0] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0);
1027 cpblts[1] = lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0);
1028 cpblts[2] = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001029
Michal Vasko11d142a2016-01-19 15:58:24 +01001030 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, NULL);
Michal Vasko086311b2016-01-08 09:53:11 +01001031
Michal Vasko086311b2016-01-08 09:53:11 +01001032 for (i = 0; cpblts[i]; ++i) {
1033 lydict_remove(session->ctx, cpblts[i]);
1034 }
1035 free(cpblts);
1036
1037 if (r) {
1038 return NC_MSG_ERROR;
Michal Vasko086311b2016-01-08 09:53:11 +01001039 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001040
1041 return NC_MSG_HELLO;
Michal Vasko086311b2016-01-08 09:53:11 +01001042}
1043
1044static NC_MSG_TYPE
Michal Vasko11d142a2016-01-19 15:58:24 +01001045nc_send_server_hello(struct nc_session *session)
1046{
1047 int r, i;
1048 const char **cpblts;
1049
Michal Vasko4ffa3b22016-05-24 16:36:25 +02001050 cpblts = nc_server_get_cpblts(session->ctx);
Michal Vasko11d142a2016-01-19 15:58:24 +01001051
1052 r = nc_write_msg(session, NC_MSG_HELLO, cpblts, &session->id);
1053
Michal Vasko11d142a2016-01-19 15:58:24 +01001054 for (i = 0; cpblts[i]; ++i) {
1055 lydict_remove(session->ctx, cpblts[i]);
1056 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001057 free(cpblts);
1058
1059 if (r) {
1060 return NC_MSG_ERROR;
1061 }
1062
1063 return NC_MSG_HELLO;
1064}
1065
1066static NC_MSG_TYPE
1067nc_recv_client_hello(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001068{
1069 struct lyxml_elem *xml = NULL, *node;
1070 NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */
1071 int ver = -1;
1072 char *str;
1073 long long int id;
1074 int flag = 0;
1075
Michal Vasko05ba9df2016-01-13 14:40:27 +01001076 msgtype = nc_read_msg_poll(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001077
1078 switch(msgtype) {
1079 case NC_MSG_HELLO:
1080 /* parse <hello> data */
Michal Vasko11d142a2016-01-19 15:58:24 +01001081 LY_TREE_FOR(xml->child, node) {
1082 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1083 continue;
1084 } else if (!strcmp(node->name, "session-id")) {
1085 if (!node->content || !strlen(node->content)) {
1086 ERR("No value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001087 goto error;
1088 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001089 str = NULL;
1090 id = strtoll(node->content, &str, 10);
1091 if (*str || id < 1 || id > UINT32_MAX) {
1092 ERR("Invalid value of <session-id> element in server's <hello>.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001093 goto error;
1094 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001095 session->id = (uint32_t)id;
1096 continue;
1097 } else if (strcmp(node->name, "capabilities")) {
1098 ERR("Unexpected <%s> element in client's <hello>.", node->name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001099 goto error;
1100 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001101
1102 if (flag) {
1103 /* multiple capabilities elements */
1104 ERR("Invalid <hello> message (multiple <capabilities> elements).");
1105 goto error;
1106 }
1107 flag = 1;
1108
Michal Vasko2e6defd2016-10-07 15:48:15 +02001109 if ((ver = parse_cpblts(node, &session->opts.client.cpblts)) < 0) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001110 goto error;
1111 }
1112 session->version = ver;
1113 }
1114
1115 if (!session->id) {
1116 ERR("Missing <session-id> in server's <hello>.");
1117 goto error;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001118 }
1119 break;
Michal Vasko71090fc2016-05-24 16:37:28 +02001120 case NC_MSG_WOULDBLOCK:
1121 ERR("Server's <hello> timeout elapsed.");
1122 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001123 case NC_MSG_ERROR:
1124 /* nothing special, just pass it out */
1125 break;
1126 default:
1127 ERR("Unexpected message received instead of <hello>.");
1128 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001129 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001130 }
1131
1132 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001133 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001134
1135 return msgtype;
1136
1137error:
1138 /* cleanup */
Michal Vaskoad611702015-12-03 13:41:51 +01001139 lyxml_free(session->ctx, xml);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001140
1141 return NC_MSG_ERROR;
Radek Krejci5686ff72015-10-09 13:33:56 +02001142}
1143
Michal Vasko11d142a2016-01-19 15:58:24 +01001144static NC_MSG_TYPE
1145nc_recv_server_hello(struct nc_session *session)
1146{
1147 struct lyxml_elem *xml = NULL, *node;
Michal Vasko71090fc2016-05-24 16:37:28 +02001148 NC_MSG_TYPE msgtype;
Michal Vasko11d142a2016-01-19 15:58:24 +01001149 int ver = -1;
1150 int flag = 0;
1151
fanchanghu75888b62017-08-01 19:51:20 +08001152 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 +01001153
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001154 switch (msgtype) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001155 case NC_MSG_HELLO:
1156 /* get know NETCONF version */
1157 LY_TREE_FOR(xml->child, node) {
1158 if (!node->ns || !node->ns->value || strcmp(node->ns->value, NC_NS_BASE)) {
1159 continue;
1160 } else if (strcmp(node->name, "capabilities")) {
1161 ERR("Unexpected <%s> element in client's <hello>.", node->name);
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
1166 if (flag) {
1167 /* multiple capabilities elements */
1168 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko71090fc2016-05-24 16:37:28 +02001169 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001170 goto cleanup;
1171 }
1172 flag = 1;
1173
1174 if ((ver = parse_cpblts(node, NULL)) < 0) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001175 msgtype = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001176 goto cleanup;
1177 }
1178 session->version = ver;
1179 }
1180 break;
1181 case NC_MSG_ERROR:
1182 /* nothing special, just pass it out */
1183 break;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001184 case NC_MSG_WOULDBLOCK:
1185 ERR("Client's <hello> timeout elapsed.");
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001186 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001187 default:
1188 ERR("Unexpected message received instead of <hello>.");
1189 msgtype = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001190 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001191 }
1192
1193cleanup:
Michal Vasko11d142a2016-01-19 15:58:24 +01001194 lyxml_free(session->ctx, xml);
Michal Vasko11d142a2016-01-19 15:58:24 +01001195
1196 return msgtype;
1197}
1198
Michal Vasko71090fc2016-05-24 16:37:28 +02001199NC_MSG_TYPE
Michal Vasko086311b2016-01-08 09:53:11 +01001200nc_handshake(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001201{
Michal Vasko086311b2016-01-08 09:53:11 +01001202 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001203
Michal Vasko11d142a2016-01-19 15:58:24 +01001204 if (session->side == NC_CLIENT) {
1205 type = nc_send_client_hello(session);
1206 } else {
1207 type = nc_send_server_hello(session);
1208 }
1209
Michal Vasko086311b2016-01-08 09:53:11 +01001210 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001211 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001212 }
1213
Michal Vasko11d142a2016-01-19 15:58:24 +01001214 if (session->side == NC_CLIENT) {
1215 type = nc_recv_client_hello(session);
1216 } else {
1217 type = nc_recv_server_hello(session);
1218 }
1219
Michal Vasko71090fc2016-05-24 16:37:28 +02001220 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001221}
Michal Vasko086311b2016-01-08 09:53:11 +01001222
Radek Krejci53691be2016-02-22 13:58:37 +01001223#ifdef NC_ENABLED_SSH
Michal Vasko086311b2016-01-08 09:53:11 +01001224
Michal Vasko8f0c0282016-02-29 10:17:14 +01001225static void
Michal Vasko086311b2016-01-08 09:53:11 +01001226nc_ssh_init(void)
1227{
1228 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1229 ssh_init();
Michal Vasko086311b2016-01-08 09:53:11 +01001230}
1231
Michal Vasko8f0c0282016-02-29 10:17:14 +01001232static void
Michal Vasko086311b2016-01-08 09:53:11 +01001233nc_ssh_destroy(void)
1234{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001235 FIPS_mode_set(0);
Michal Vasko5e228792016-02-03 15:30:24 +01001236 ENGINE_cleanup();
1237 CONF_modules_unload(1);
Michal Vaskob6e37262016-02-25 14:49:00 +01001238 nc_thread_destroy();
Michal Vasko086311b2016-01-08 09:53:11 +01001239 ssh_finalize();
1240}
1241
Radek Krejci53691be2016-02-22 13:58:37 +01001242#endif /* NC_ENABLED_SSH */
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001243
Radek Krejci53691be2016-02-22 13:58:37 +01001244#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001245
Michal Vasko770b4362017-02-17 14:44:18 +01001246#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1247
Michal Vaskof0c92c02016-01-29 09:41:45 +01001248struct CRYPTO_dynlock_value {
1249 pthread_mutex_t lock;
1250};
1251
Michal Vaskof0c92c02016-01-29 09:41:45 +01001252static struct CRYPTO_dynlock_value *
1253tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1254{
1255 struct CRYPTO_dynlock_value *value;
1256
1257 value = malloc(sizeof *value);
1258 if (!value) {
1259 ERRMEM;
1260 return NULL;
1261 }
1262 pthread_mutex_init(&value->lock, NULL);
1263
1264 return value;
1265}
1266
1267static void
1268tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1269{
1270 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1271 * I found ignored this fact, what do I know... */
1272 if (mode & CRYPTO_LOCK) {
1273 pthread_mutex_lock(&l->lock);
1274 } else {
1275 pthread_mutex_unlock(&l->lock);
1276 }
1277}
1278
1279static void
1280tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1281{
1282 pthread_mutex_destroy(&l->lock);
1283 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001284}
Michal Vasko770b4362017-02-17 14:44:18 +01001285#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001286
Michal Vasko8f0c0282016-02-29 10:17:14 +01001287#endif /* NC_ENABLED_TLS */
1288
1289#if defined(NC_ENABLED_TLS) && !defined(NC_ENABLED_SSH)
1290
Michal Vasko770b4362017-02-17 14:44:18 +01001291#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001292static pthread_mutex_t *tls_locks;
1293
1294static void
1295tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1296{
1297 if (mode & CRYPTO_LOCK) {
1298 pthread_mutex_lock(tls_locks + n);
1299 } else {
1300 pthread_mutex_unlock(tls_locks + n);
1301 }
1302}
1303
1304static void
1305tls_thread_id_func(CRYPTO_THREADID *tid)
1306{
1307 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1308}
Michal Vasko770b4362017-02-17 14:44:18 +01001309#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001310
1311static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001312nc_tls_init(void)
1313{
1314 int i;
1315
1316 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001317 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001318 SSL_library_init();
1319
Michal Vasko770b4362017-02-17 14:44:18 +01001320#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001321 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001322 if (!tls_locks) {
1323 ERRMEM;
1324 return;
1325 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001326 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1327 pthread_mutex_init(tls_locks + i, NULL);
1328 }
1329
Michal Vaskof0c92c02016-01-29 09:41:45 +01001330 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001331 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001332
1333 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1334 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1335 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001336#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001337}
1338
Michal Vasko8f0c0282016-02-29 10:17:14 +01001339static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001340nc_tls_destroy(void)
1341{
1342 int i;
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001343
Michal Vasko8f0c0282016-02-29 10:17:14 +01001344 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001345 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001346 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001347 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001348 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001349#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001350 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001351#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1352 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001353#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001354
Michal Vasko770b4362017-02-17 14:44:18 +01001355#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskob6e37262016-02-25 14:49:00 +01001356 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001357 CRYPTO_set_locking_callback(NULL);
1358 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1359 pthread_mutex_destroy(tls_locks + i);
1360 }
1361 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001362
1363 CRYPTO_set_dynlock_create_callback(NULL);
1364 CRYPTO_set_dynlock_lock_callback(NULL);
1365 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001366#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001367}
1368
Michal Vasko8f0c0282016-02-29 10:17:14 +01001369#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001370
Radek Krejci53691be2016-02-22 13:58:37 +01001371#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001372
Michal Vasko8f0c0282016-02-29 10:17:14 +01001373static void
Michal Vasko5e228792016-02-03 15:30:24 +01001374nc_ssh_tls_init(void)
1375{
1376 SSL_load_error_strings();
1377 ERR_load_BIO_strings();
1378 SSL_library_init();
1379
1380 nc_ssh_init();
1381
Michal Vasko770b4362017-02-17 14:44:18 +01001382#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001383 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1384 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1385 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001386#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001387}
1388
Michal Vasko8f0c0282016-02-29 10:17:14 +01001389static void
Michal Vasko5e228792016-02-03 15:30:24 +01001390nc_ssh_tls_destroy(void)
1391{
1392 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001393#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001394 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001395#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1396 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001397#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001398
1399 nc_ssh_destroy();
1400
Michal Vasko770b4362017-02-17 14:44:18 +01001401#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001402 CRYPTO_set_dynlock_create_callback(NULL);
1403 CRYPTO_set_dynlock_lock_callback(NULL);
1404 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001405#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001406}
1407
Radek Krejci53691be2016-02-22 13:58:37 +01001408#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001409
1410#if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS)
1411
1412API void
1413nc_thread_destroy(void)
1414{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001415 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
1416 //CRYPTO_cleanup_all_ex_data();
1417
Michal Vasko770b4362017-02-17 14:44:18 +01001418#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1419 CRYPTO_THREADID crypto_tid;
1420
Michal Vasko8f0c0282016-02-29 10:17:14 +01001421 CRYPTO_THREADID_current(&crypto_tid);
1422 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001423#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001424}
1425
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001426#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1427
1428void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001429nc_init(void)
1430{
1431#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1432 nc_ssh_tls_init();
1433#elif defined(NC_ENABLED_SSH)
1434 nc_ssh_init();
1435#elif defined(NC_ENABLED_TLS)
1436 nc_tls_init();
1437#endif
1438}
1439
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001440void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001441nc_destroy(void)
1442{
1443#if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS)
1444 nc_ssh_tls_destroy();
1445#elif defined(NC_ENABLED_SSH)
1446 nc_ssh_destroy();
1447#elif defined(NC_ENABLED_TLS)
1448 nc_tls_destroy();
1449#endif
1450}