blob: e132f2beedc36bbb46f976e32d50a2e29ee1b344 [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
2 * \file session.c
Michal Vasko086311b2016-01-08 09:53:11 +01003 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 - general session functions
Radek Krejci206fcd62015-10-07 15:42:48 +02005 *
Michal Vaskob85767d2018-03-21 12:21:10 +01006 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
Radek Krejci206fcd62015-10-07 15:42:48 +02007 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci206fcd62015-10-07 15:42:48 +020013 */
Michal Vaskob85767d2018-03-21 12:21:10 +010014#define _DEFAULT_SOURCE
Radek Krejci206fcd62015-10-07 15:42:48 +020015
Michal Vasko18aeb5d2017-02-17 09:23:56 +010016#include <assert.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020017#include <ctype.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020018#include <errno.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020019#include <libyang/libyang.h>
Michal Vaskobe52dc22018-10-17 09:28:17 +020020#include <netinet/in.h>
21#include <netinet/tcp.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020022#include <pthread.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/socket.h>
26#include <sys/time.h>
27#include <sys/types.h>
Michal Vasko58f31552016-01-19 12:39:15 +010028#include <time.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020029#include <unistd.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020030
Michal Vasko9e8ac262020-04-07 13:06:45 +020031#include "compat.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010032#include "libnetconf.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020033#include "session.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010034#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010035
Radek Krejci53691be2016-02-22 13:58:37 +010036#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020037
Michal Vasko086311b2016-01-08 09:53:11 +010038# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020039
Radek Krejci53691be2016-02-22 13:58:37 +010040#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020041
Michal Vaskob83a3fa2021-05-26 09:53:42 +020042#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010043
Michal Vasko5e228792016-02-03 15:30:24 +010044# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010045# include <openssl/err.h>
46
Radek Krejci53691be2016-02-22 13:58:37 +010047#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010048
Michal Vasko086311b2016-01-08 09:53:11 +010049/* in seconds */
50#define NC_CLIENT_HELLO_TIMEOUT 60
fanchanghu75888b62017-08-01 19:51:20 +080051#define NC_SERVER_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020052
Michal Vasko05ba9df2016-01-13 14:40:27 +010053/* in milliseconds */
54#define NC_CLOSE_REPLY_TIMEOUT 200
55
Michal Vasko086311b2016-01-08 09:53:11 +010056extern struct nc_server_opts server_opts;
57
Radek Krejci7ac16052016-07-15 11:48:18 +020058int
Michal Vasko77a6abe2017-10-05 10:02:20 +020059nc_gettimespec_mono(struct timespec *ts)
60{
61#ifdef CLOCK_MONOTONIC_RAW
62 return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
Michal Vaskob83a3fa2021-05-26 09:53:42 +020063#elif defined (CLOCK_MONOTONIC)
Michal Vasko77a6abe2017-10-05 10:02:20 +020064 return clock_gettime(CLOCK_MONOTONIC, ts);
65#else
66 /* no monotonic clock available, return realtime */
67 return nc_gettimespec_real(ts);
68#endif
69}
70
71int
72nc_gettimespec_real(struct timespec *ts)
Radek Krejci7ac16052016-07-15 11:48:18 +020073{
Michal Vasko0ef46df2016-09-21 14:03:18 +020074#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020075 return clock_gettime(CLOCK_REALTIME, ts);
76#else
77 int rc;
78 struct timeval tv;
79
80 rc = gettimeofday(&tv, NULL);
81 if (!rc) {
82 ts->tv_sec = (time_t)tv.tv_sec;
83 ts->tv_nsec = 1000L * (long)tv.tv_usec;
84 }
85 return rc;
86#endif
87}
88
Michal Vasko36c7be82017-02-22 13:37:59 +010089/* ts1 < ts2 -> +, ts1 > ts2 -> -, returns milliseconds */
90int32_t
Radek Krejcida0afb22017-05-26 16:25:59 +020091nc_difftimespec(const struct timespec *ts1, const struct timespec *ts2)
Michal Vasko99f251b2017-01-11 11:31:46 +010092{
Michal Vasko36c7be82017-02-22 13:37:59 +010093 int64_t nsec_diff = 0;
Michal Vasko99f251b2017-01-11 11:31:46 +010094
Michal Vaskoa82bd202017-03-03 14:07:29 +010095 nsec_diff += (((int64_t)ts2->tv_sec) - ((int64_t)ts1->tv_sec)) * 1000000000L;
96 nsec_diff += ((int64_t)ts2->tv_nsec) - ((int64_t)ts1->tv_nsec);
Michal Vasko99f251b2017-01-11 11:31:46 +010097
Michal Vaskob83a3fa2021-05-26 09:53:42 +020098 return nsec_diff ? nsec_diff / 1000000L : 0;
Michal Vasko99f251b2017-01-11 11:31:46 +010099}
100
Michal Vasko36c7be82017-02-22 13:37:59 +0100101void
102nc_addtimespec(struct timespec *ts, uint32_t msec)
103{
Michal Vasko81c5b302017-03-15 12:10:40 +0100104 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
105
Michal Vasko0dfcd332017-03-03 13:14:39 +0100106 ts->tv_sec += msec / 1000;
107 ts->tv_nsec += (msec % 1000) * 1000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100108
Michal Vasko81c5b302017-03-15 12:10:40 +0100109 if (ts->tv_nsec >= 1000000000L) {
110 ++ts->tv_sec;
111 ts->tv_nsec -= 1000000000L;
112 } else if (ts->tv_nsec < 0) {
113 --ts->tv_sec;
114 ts->tv_nsec += 1000000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100115 }
Michal Vasko81c5b302017-03-15 12:10:40 +0100116
117 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
Michal Vasko36c7be82017-02-22 13:37:59 +0100118}
119
Michal Vaskoddce1212019-05-24 09:58:49 +0200120const char *
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200121nc_keytype2str(NC_SSH_KEY_TYPE type)
Michal Vaskoddce1212019-05-24 09:58:49 +0200122{
123 switch (type) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200124 case NC_SSH_KEY_DSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200125 return "DSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200126 case NC_SSH_KEY_RSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200127 return "RSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200128 case NC_SSH_KEY_ECDSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200129 return "EC";
130 default:
131 break;
132 }
133
134 return NULL;
135}
136
Michal Vaskobe52dc22018-10-17 09:28:17 +0200137int
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200138nc_sock_enable_keepalive(int sock, struct nc_keepalives *ka)
Michal Vaskobe52dc22018-10-17 09:28:17 +0200139{
140 int opt;
141
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200142 opt = ka->enabled;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200143 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) {
144 ERR("Could not set SO_KEEPALIVE option (%s).", strerror(errno));
145 return -1;
146 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200147 if (!ka->enabled) {
148 return 0;
149 }
Michal Vaskobe52dc22018-10-17 09:28:17 +0200150
151#ifdef TCP_KEEPIDLE
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200152 opt = ka->idle_time;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200153 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) {
154 ERR("Setsockopt failed (%s).", strerror(errno));
155 return -1;
156 }
157#endif
158
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200159#ifdef TCP_KEEPCNT
160 opt = ka->max_probes;
161 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) {
Michal Vaskobe52dc22018-10-17 09:28:17 +0200162 ERR("Setsockopt failed (%s).", strerror(errno));
163 return -1;
164 }
165#endif
166
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200167#ifdef TCP_KEEPINTVL
168 opt = ka->probe_interval;
169 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof opt) == -1) {
Michal Vaskobe52dc22018-10-17 09:28:17 +0200170 ERR("Setsockopt failed (%s).", strerror(errno));
171 return -1;
172 }
173#endif
174
175 return 0;
176}
177
Michal Vaskoade892d2017-02-22 13:40:35 +0100178struct nc_session *
Michal Vasko131120a2018-05-29 15:44:02 +0200179nc_new_session(NC_SIDE side, int shared_ti)
Michal Vaskoade892d2017-02-22 13:40:35 +0100180{
181 struct nc_session *sess;
182
183 sess = calloc(1, sizeof *sess);
184 if (!sess) {
185 return NULL;
186 }
187
Michal Vasko131120a2018-05-29 15:44:02 +0200188 sess->side = side;
189
190 if (side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100191 pthread_mutex_init(&sess->opts.server.rpc_lock, NULL);
192 pthread_cond_init(&sess->opts.server.rpc_cond, NULL);
193 sess->opts.server.rpc_inuse = 0;
194
195 pthread_mutex_init(&sess->opts.server.ch_lock, NULL);
196 pthread_cond_init(&sess->opts.server.ch_cond, NULL);
Michal Vasko131120a2018-05-29 15:44:02 +0200197 }
198
199 if (!shared_ti) {
200 sess->io_lock = malloc(sizeof *sess->io_lock);
201 if (!sess->io_lock) {
202 goto error;
203 }
204 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100205 }
206
207 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200208
209error:
Michal Vasko131120a2018-05-29 15:44:02 +0200210 free(sess);
211 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100212}
213
Michal Vasko96164bf2016-01-21 15:41:58 +0100214/*
215 * @return 1 - success
216 * 0 - timeout
217 * -1 - error
218 */
219int
Michal Vasko131120a2018-05-29 15:44:02 +0200220nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100221{
222 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100223 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100224
Michal Vasko131120a2018-05-29 15:44:02 +0200225 if (session->side != NC_SERVER) {
226 ERRINT;
227 return -1;
228 }
229
Michal Vasko96164bf2016-01-21 15:41:58 +0100230 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200231 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100232 nc_addtimespec(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100233
Michal Vaskoade892d2017-02-22 13:40:35 +0100234 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100235 ret = pthread_mutex_timedlock(&session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100236 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100237 while (session->opts.server.rpc_inuse) {
238 ret = pthread_cond_timedwait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100239 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100240 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100241 break;
242 }
243 }
244 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100245 } else if (!timeout) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100246 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100247 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100248 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100249 if (session->opts.server.rpc_inuse) {
250 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100251 return 0;
252 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200253 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100254 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100255 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100256 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100257 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100258 while (session->opts.server.rpc_inuse) {
259 ret = pthread_cond_wait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100260 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100261 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100262 break;
263 }
264 }
265 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100266 }
267
Michal Vaskoade892d2017-02-22 13:40:35 +0100268 if (ret) {
269 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
270 /* timeout */
271 return 0;
272 }
273
Michal Vasko96164bf2016-01-21 15:41:58 +0100274 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200275 ERR("%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100276 return -1;
277 }
278
279 /* ok */
Michal Vaskoacf98472021-02-04 15:33:57 +0100280 assert(session->opts.server.rpc_inuse == 0);
281 session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100282
283 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100284 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100285 if (ret) {
286 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200287 ERR("%s: faile to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100288 return -1;
289 }
290
291 return 1;
292}
293
294int
Michal Vasko131120a2018-05-29 15:44:02 +0200295nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100296{
297 int ret;
298 struct timespec ts_timeout;
299
Michal Vasko131120a2018-05-29 15:44:02 +0200300 if (session->side != NC_SERVER) {
301 ERRINT;
302 return -1;
303 }
304
Michal Vaskoacf98472021-02-04 15:33:57 +0100305 assert(session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100306
307 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200308 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100309 nc_addtimespec(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100310
311 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100312 ret = pthread_mutex_timedlock(&session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100313 } else if (!timeout) {
314 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100315 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100316 } else { /* timeout == -1 */
317 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100318 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100319 }
320
321 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
322 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200323 ERR("%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100324 return -1;
325 } else if (ret) {
Michal Vasko131120a2018-05-29 15:44:02 +0200326 WRN("%s: session RPC lock timeout, should not happen.");
Michal Vaskoade892d2017-02-22 13:40:35 +0100327 }
328
Michal Vaskoacf98472021-02-04 15:33:57 +0100329 session->opts.server.rpc_inuse = 0;
330 pthread_cond_signal(&session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100331
332 if (!ret) {
333 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100334 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100335 if (ret) {
336 /* error */
Michal Vasko131120a2018-05-29 15:44:02 +0200337 ERR("%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100338 return -1;
339 }
340 }
341
Michal Vasko96164bf2016-01-21 15:41:58 +0100342 return 1;
343}
344
Michal Vasko131120a2018-05-29 15:44:02 +0200345/*
346 * @return 1 - success
347 * 0 - timeout
348 * -1 - error
349 */
350int
351nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
352{
353 int ret;
354 struct timespec ts_timeout;
355
356 if (timeout > 0) {
357 nc_gettimespec_real(&ts_timeout);
358 nc_addtimespec(&ts_timeout, timeout);
359
360 ret = pthread_mutex_timedlock(session->io_lock, &ts_timeout);
361 } else if (!timeout) {
362 ret = pthread_mutex_trylock(session->io_lock);
363 } else { /* timeout == -1 */
Robin Jarry54ea2962018-10-10 10:33:40 +0200364 ret = pthread_mutex_lock(session->io_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200365 }
366
367 if (ret) {
368 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
369 /* timeout */
370 return 0;
371 }
372
373 /* error */
374 ERR("%s: failed to IO lock a session (%s).", func, strerror(ret));
375 return -1;
376 }
377
378 return 1;
379}
380
381int
382nc_session_io_unlock(struct nc_session *session, const char *func)
383{
384 int ret;
385
386 ret = pthread_mutex_unlock(session->io_lock);
387 if (ret) {
388 /* error */
389 ERR("%s: failed to IO unlock a session (%s).", func, strerror(ret));
390 return -1;
391 }
392
393 return 1;
394}
395
Michal Vasko8dadf782016-01-15 10:29:36 +0100396API NC_STATUS
397nc_session_get_status(const struct nc_session *session)
398{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100399 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200400 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200401 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100402 }
403
Michal Vasko8dadf782016-01-15 10:29:36 +0100404 return session->status;
405}
406
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100407API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200408nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100409{
410 if (!session) {
411 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200412 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100413 }
414
415 return session->term_reason;
416}
417
Michal Vasko8dadf782016-01-15 10:29:36 +0100418API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200419nc_session_get_killed_by(const struct nc_session *session)
420{
421 if (!session) {
422 ERRARG("session");
423 return 0;
424 }
425
426 return session->killed_by;
427}
428
429API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100430nc_session_get_id(const struct nc_session *session)
431{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100432 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200433 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100434 return 0;
435 }
436
Michal Vasko8dadf782016-01-15 10:29:36 +0100437 return session->id;
438}
439
Michal Vasko174fe8e2016-02-17 15:38:09 +0100440API int
441nc_session_get_version(const struct nc_session *session)
442{
443 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200444 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100445 return -1;
446 }
447
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200448 return session->version == NC_VERSION_10 ? 0 : 1;
Michal Vasko174fe8e2016-02-17 15:38:09 +0100449}
450
Michal Vasko8dadf782016-01-15 10:29:36 +0100451API NC_TRANSPORT_IMPL
452nc_session_get_ti(const struct nc_session *session)
453{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100454 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200455 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100456 return 0;
457 }
458
Michal Vasko8dadf782016-01-15 10:29:36 +0100459 return session->ti_type;
460}
461
462API const char *
463nc_session_get_username(const struct nc_session *session)
464{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100465 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200466 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100467 return NULL;
468 }
469
Michal Vasko8dadf782016-01-15 10:29:36 +0100470 return session->username;
471}
472
473API const char *
474nc_session_get_host(const struct nc_session *session)
475{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100476 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200477 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100478 return NULL;
479 }
480
Michal Vasko8dadf782016-01-15 10:29:36 +0100481 return session->host;
482}
483
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200484API const char *
485nc_session_get_path(const struct nc_session *session)
486{
487 if (!session) {
488 ERRARG("session");
489 return NULL;
490 }
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200491 if (session->ti_type != NC_TI_UNIX) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200492 return NULL;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200493 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200494
495 return session->path;
496}
497
Michal Vasko8dadf782016-01-15 10:29:36 +0100498API uint16_t
499nc_session_get_port(const struct nc_session *session)
500{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100501 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200502 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100503 return 0;
504 }
505
Michal Vasko8dadf782016-01-15 10:29:36 +0100506 return session->port;
507}
508
Michal Vasko9a25e932016-02-01 10:36:42 +0100509API struct ly_ctx *
510nc_session_get_ctx(const struct nc_session *session)
511{
512 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200513 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100514 return NULL;
515 }
516
517 return session->ctx;
518}
519
Michal Vasko2cc4c682016-03-01 09:16:48 +0100520API void
521nc_session_set_data(struct nc_session *session, void *data)
522{
523 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200524 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100525 return;
526 }
527
528 session->data = data;
529}
530
531API void *
532nc_session_get_data(const struct nc_session *session)
533{
534 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200535 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100536 return NULL;
537 }
538
539 return session->data;
540}
541
Michal Vasko086311b2016-01-08 09:53:11 +0100542NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200543nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200544{
Michal Vasko086311b2016-01-08 09:53:11 +0100545 if (session->ctx != op->schema->module->ctx) {
Michal Vaskod083db62016-01-19 10:31:29 +0100546 ERR("Session %u: RPC \"%s\" was created in different context than that of the session.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200547 session->id, op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100548 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100549 }
550
Michal Vasko131120a2018-05-29 15:44:02 +0200551 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100552}
553
Radek Krejci695d4fa2015-10-22 13:23:54 +0200554API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100555nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200556{
Michal Vasko131120a2018-05-29 15:44:02 +0200557 int r, i, rpc_locked = 0, sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100558 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100559 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vasko4589bbe2016-01-29 09:41:30 +0100560 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100561 struct nc_msg_cont *contiter;
Michal Vasko77367452021-02-16 16:32:18 +0100562 struct ly_in *msg;
563 struct lyd_node *close_rpc, *envp;
Michal Vaskoad611702015-12-03 13:41:51 +0100564 const struct lys_module *ietfnc;
Michal Vasko0db3db52021-03-03 10:45:42 +0100565 struct timespec ts;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200566 void *p;
567
Michal Vasko428087d2016-01-14 16:04:28 +0100568 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200569 return;
570 }
571
Michal Vasko86d357c2016-03-11 13:46:38 +0100572 /* stop notifications loop if any */
Michal Vasko7f1fa3c2020-09-08 16:30:41 +0200573 if ((session->side == NC_CLIENT) && ATOMIC_LOAD(session->opts.client.ntf_tid)) {
Michal Vaskob639e9c2020-09-09 09:51:48 +0200574 ATOMIC_STORE(session->opts.client.ntf_tid, (uintptr_t)NULL);
Michal Vasko86d357c2016-03-11 13:46:38 +0100575 /* the thread now knows it should quit */
Michal Vasko86d357c2016-03-11 13:46:38 +0100576 }
577
Michal Vaskoacf98472021-02-04 15:33:57 +0100578 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200579 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100580 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100581 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200582 } else if (r) {
583 rpc_locked = 1;
Michal Vasko96a28a32021-02-04 15:35:20 +0100584 } else {
585 /* else failed to lock it, too bad */
586 ERR("Session %u: freeing a session while an RPC is being processed.", session->id);
587 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200588 }
589
Michal Vasko8c247822020-09-07 13:23:23 +0200590 if (session->side == NC_CLIENT) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200591 /* cleanup message queues */
592 /* notifications */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200593 for (contiter = session->opts.client.notifs; contiter; ) {
Michal Vasko77367452021-02-16 16:32:18 +0100594 ly_in_free(contiter->msg, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200595
Michal Vaskoad611702015-12-03 13:41:51 +0100596 p = contiter;
597 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200598 free(p);
599 }
600
601 /* rpc replies */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200602 for (contiter = session->opts.client.replies; contiter; ) {
Michal Vasko77367452021-02-16 16:32:18 +0100603 ly_in_free(contiter->msg, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200604
Michal Vaskoad611702015-12-03 13:41:51 +0100605 p = contiter;
606 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200607 free(p);
608 }
609
Michal Vasko8c247822020-09-07 13:23:23 +0200610 if (session->status == NC_STATUS_RUNNING) {
611 /* send closing info to the other side */
Michal Vasko77367452021-02-16 16:32:18 +0100612 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
Michal Vasko8c247822020-09-07 13:23:23 +0200613 if (!ietfnc) {
614 WRN("Session %u: missing ietf-netconf schema in context, unable to send <close-session>.", session->id);
Michal Vaskod9c38992021-05-28 13:32:15 +0200615 } else if (!lyd_new_inner(NULL, ietfnc, "close-session", 0, &close_rpc)) {
Michal Vasko8c247822020-09-07 13:23:23 +0200616 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
Michal Vasko77367452021-02-16 16:32:18 +0100617 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &msg)) {
618 case 1:
619 if (lyd_parse_op(session->ctx, close_rpc, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, &envp, NULL)) {
620 WRN("Session %u: failed to parse <close-session> reply.", session->id);
621 } else if (!lyd_child(envp) || strcmp(LYD_NAME(lyd_child(envp)), "ok")) {
Michal Vasko8c247822020-09-07 13:23:23 +0200622 WRN("Session %u: the reply to <close-session> was not <ok> as expected.", session->id);
623 }
Michal Vasko77367452021-02-16 16:32:18 +0100624 lyd_free_tree(envp);
625 ly_in_free(msg, 1);
Michal Vasko8c247822020-09-07 13:23:23 +0200626 break;
Michal Vasko77367452021-02-16 16:32:18 +0100627 case 0:
Michal Vasko8c247822020-09-07 13:23:23 +0200628 WRN("Session %u: timeout for receiving a reply to <close-session> elapsed.", session->id);
629 break;
Michal Vasko77367452021-02-16 16:32:18 +0100630 case -1:
Michal Vasko8c247822020-09-07 13:23:23 +0200631 ERR("Session %u: failed to receive a reply to <close-session>.", session->id);
632 break;
633 default:
634 /* cannot happen */
635 break;
Michal Vaskofad6e912015-10-26 15:37:22 +0100636 }
Michal Vasko77367452021-02-16 16:32:18 +0100637 lyd_free_tree(close_rpc);
Michal Vaskofad6e912015-10-26 15:37:22 +0100638 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200639 }
640
641 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200642 if (session->opts.client.cpblts) {
643 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200644 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200645 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200646 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200647 }
648 }
649
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100650 if (session->data && data_free) {
651 data_free(session->data);
652 }
653
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200654 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
655 /* CH LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100656 pthread_mutex_lock(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200657 }
658
Michal Vasko86d357c2016-03-11 13:46:38 +0100659 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200660 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200661
662 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100663 pthread_cond_signal(&session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200664
Michal Vasko0db3db52021-03-03 10:45:42 +0100665 nc_gettimespec_real(&ts);
666 nc_addtimespec(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
667
668 /* wait for CH thread to actually wake up and terminate */
669 r = 0;
670 while (!r && (session->flags & NC_SESSION_CALLHOME)) {
671 r = pthread_cond_timedwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, &ts);
672 }
673
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200674 /* CH UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100675 pthread_mutex_unlock(&session->opts.server.ch_lock);
Michal Vasko3f05a092018-03-13 10:39:49 +0100676
Michal Vasko0db3db52021-03-03 10:45:42 +0100677 if (r) {
678 ERR("Session %u: waiting for Call Home thread failed (%s).", session->id, strerror(r));
Michal Vasko3f05a092018-03-13 10:39:49 +0100679 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200680 }
681
Michal Vasko428087d2016-01-14 16:04:28 +0100682 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200683
684 /* transport implementation cleanup */
685 switch (session->ti_type) {
686 case NC_TI_FD:
687 /* nothing needed - file descriptors were provided by caller,
688 * so it is up to the caller to close them correctly
689 * TODO use callbacks
690 */
Michal Vasko3512e402016-01-28 16:22:34 +0100691 /* just to avoid compiler warning */
692 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100693 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200694 break;
695
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200696 case NC_TI_UNIX:
697 sock = session->ti.unixsock.sock;
698 (void)connected;
699 (void)siter;
700 break;
701
Radek Krejci53691be2016-02-22 13:58:37 +0100702#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200703 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100704 if (connected) {
705 ssh_channel_free(session->ti.libssh.channel);
706 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200707 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
708 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200709 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
Radek Krejci695d4fa2015-10-22 13:23:54 +0200710 */
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200711 /* SESSION IO LOCK */
712 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
713
Michal Vasko96164bf2016-01-21 15:41:58 +0100714 multisession = 0;
715 if (session->ti.libssh.next) {
716 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
717 if (siter->status != NC_STATUS_STARTING) {
718 multisession = 1;
719 break;
720 }
721 }
722 }
723
724 if (!multisession) {
725 /* it's not multisession yet, but we still need to free the starting sessions */
726 if (session->ti.libssh.next) {
727 do {
728 siter = session->ti.libssh.next;
729 session->ti.libssh.next = siter->ti.libssh.next;
730
731 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100732 lydict_remove(session->ctx, session->username);
733 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100734 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko1dcb7642021-04-14 15:28:23 +0200735 ly_ctx_destroy(session->ctx);
Michal Vasko96164bf2016-01-21 15:41:58 +0100736 }
737
738 free(siter);
739 } while (session->ti.libssh.next != session);
740 }
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100741 /* remember sock so we can close it */
742 sock = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100743 if (connected) {
744 ssh_disconnect(session->ti.libssh.session);
Michal Vasko06e0fc22019-05-09 09:32:27 +0200745 sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100746 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200747 ssh_free(session->ti.libssh.session);
748 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200749 /* remove the session from the list */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200750 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {}
Michal Vaskoaec4f212015-10-26 15:37:45 +0100751 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200752 /* there will be only one session */
753 siter->ti.libssh.next = NULL;
754 } else {
755 /* there are still multiple sessions, keep the ring list */
756 siter->ti.libssh.next = session->ti.libssh.next;
757 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100758 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
759 if (session->flags & NC_SESSION_SSH_MSG_CB) {
Michal Vasko5e0edd82020-07-29 15:26:13 +0200760 siter = session->ti.libssh.next;
761 while (siter && siter->status != NC_STATUS_RUNNING) {
Michal Vasko96164bf2016-01-21 15:41:58 +0100762 if (siter->ti.libssh.next == session) {
763 ERRINT;
764 break;
765 }
Michal Vasko5e0edd82020-07-29 15:26:13 +0200766 siter = siter->ti.libssh.next;
Michal Vasko96164bf2016-01-21 15:41:58 +0100767 }
Michal Vasko5e0edd82020-07-29 15:26:13 +0200768 /* siter may be NULL in case all the sessions terminated at the same time (socket was disconnected),
769 * we set session to NULL because we do not expect any new message to arrive */
Michal Vasko96164bf2016-01-21 15:41:58 +0100770 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
Michal Vasko5e0edd82020-07-29 15:26:13 +0200771 if (siter) {
772 siter->flags |= NC_SESSION_SSH_MSG_CB;
773 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100774 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200775 }
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200776
777 /* SESSION IO UNLOCK */
778 if (r == 1) {
779 nc_session_io_unlock(session, __func__);
780 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200781 break;
782#endif
783
Radek Krejci53691be2016-02-22 13:58:37 +0100784#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200785 case NC_TI_OPENSSL:
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100786 /* remember sock so we can close it */
787 sock = SSL_get_fd(session->ti.tls);
788
Michal Vasko428087d2016-01-14 16:04:28 +0100789 if (connected) {
790 SSL_shutdown(session->ti.tls);
791 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200792 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100793
Michal Vasko2e6defd2016-10-07 15:48:15 +0200794 if (session->side == NC_SERVER) {
795 X509_free(session->opts.server.client_cert);
796 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200797 break;
798#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100799 case NC_TI_NONE:
Michal Vasko428087d2016-01-14 16:04:28 +0100800 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200801 }
Michal Vasko428087d2016-01-14 16:04:28 +0100802
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100803 /* close socket separately */
804 if (sock > -1) {
805 close(sock);
806 }
807
Radek Krejciac6d3472015-10-22 15:47:18 +0200808 lydict_remove(session->ctx, session->username);
809 lydict_remove(session->ctx, session->host);
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200810 lydict_remove(session->ctx, session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200811
812 /* final cleanup */
Michal Vaskoacf98472021-02-04 15:33:57 +0100813 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200814 if (rpc_locked) {
815 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100816 }
Michal Vaskoacf98472021-02-04 15:33:57 +0100817 pthread_mutex_destroy(&session->opts.server.rpc_lock);
818 pthread_cond_destroy(&session->opts.server.rpc_cond);
Michal Vasko131120a2018-05-29 15:44:02 +0200819 }
820
821 if (session->io_lock && !multisession) {
822 pthread_mutex_destroy(session->io_lock);
823 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200824 }
825
826 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko1dcb7642021-04-14 15:28:23 +0200827 ly_ctx_destroy(session->ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200828 }
829
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200830 if (session->side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100831 /* free CH synchronization structures */
832 pthread_cond_destroy(&session->opts.server.ch_cond);
833 pthread_mutex_destroy(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200834 }
835
Radek Krejci695d4fa2015-10-22 13:23:54 +0200836 free(session);
837}
838
Michal Vasko086311b2016-01-08 09:53:11 +0100839static void
840add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
841{
Radek Krejci658782b2016-12-04 22:04:55 +0100842 size_t len;
843 int i;
844 char *p;
845
846 if (capab) {
847 /* check if already present */
848 p = strchr(capab, '?');
849 if (p) {
850 len = p - capab;
851 } else {
852 len = strlen(capab);
853 }
854 for (i = 0; i < *count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200855 if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) {
Radek Krejci658782b2016-12-04 22:04:55 +0100856 /* already present, do not duplicate it */
857 return;
858 }
859 }
860 }
861
862 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100863 if (*count == *size) {
864 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100865 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
866 if (!(*cpblts)) {
867 ERRMEM;
868 return;
869 }
Michal Vasko086311b2016-01-08 09:53:11 +0100870 }
871
872 if (capab) {
Michal Vasko77367452021-02-16 16:32:18 +0100873 lydict_insert(ctx, capab, 0, &(*cpblts)[*count]);
Michal Vasko086311b2016-01-08 09:53:11 +0100874 } else {
875 (*cpblts)[*count] = NULL;
876 }
877 ++(*count);
878}
879
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200880API const char **
Radek Krejci24a18412018-05-16 15:09:10 +0200881nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +0100882{
Michal Vaskod5ada122020-03-19 18:28:06 +0100883 const char **cpblts;
Michal Vasko77367452021-02-16 16:32:18 +0100884 const struct lys_module *mod;
885 struct lysp_feature *feat;
886 int size = 10, count, features_count = 0, dev_count = 0, str_len, len;
Michal Vasko1440a742021-03-31 11:11:03 +0200887 uint32_t i, u;
Michal Vasko77367452021-02-16 16:32:18 +0100888 LY_ARRAY_COUNT_TYPE v;
Michal Vasko1440a742021-03-31 11:11:03 +0200889 char *yl_content_id;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200890
Radek Krejci24a18412018-05-16 15:09:10 +0200891#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +0200892 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100893
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200894 if (!ctx) {
895 ERRARG("ctx");
896 return NULL;
897 }
898
Michal Vasko086311b2016-01-08 09:53:11 +0100899 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100900 if (!cpblts) {
901 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200902 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100903 }
Michal Vasko77367452021-02-16 16:32:18 +0100904 lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0, &cpblts[0]);
905 lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0, &cpblts[1]);
Michal Vasko086311b2016-01-08 09:53:11 +0100906 count = 2;
907
908 /* capabilities */
909
Michal Vasko77367452021-02-16 16:32:18 +0100910 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf");
Michal Vasko086311b2016-01-08 09:53:11 +0100911 if (mod) {
Michal Vasko77367452021-02-16 16:32:18 +0100912 if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100913 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100914 }
Michal Vasko77367452021-02-16 16:32:18 +0100915 if (lys_feature_value(mod, "candidate") == LY_SUCCESS) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100916 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko77367452021-02-16 16:32:18 +0100917 if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100918 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100919 }
920 }
Michal Vasko77367452021-02-16 16:32:18 +0100921 if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100922 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100923 }
Michal Vasko77367452021-02-16 16:32:18 +0100924 if (lys_feature_value(mod, "validate") == LY_SUCCESS) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100925 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100926 }
Michal Vasko77367452021-02-16 16:32:18 +0100927 if (lys_feature_value(mod, "startup") == LY_SUCCESS) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100928 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100929 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +0100930
931 /* The URL capability must be set manually using nc_server_set_capability()
932 * because of the need for supported protocols to be included.
933 * https://tools.ietf.org/html/rfc6241#section-8.8.3
934 */
Michal Vasko77367452021-02-16 16:32:18 +0100935 // if (lys_feature_value(mod, "url") == LY_SUCCESS) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200936 // add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
mekleoa8de5e92020-02-13 09:05:56 +0100937 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +0100938
Michal Vasko77367452021-02-16 16:32:18 +0100939 if (lys_feature_value(mod, "xpath") == LY_SUCCESS) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100940 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100941 }
942 }
943
Michal Vasko77367452021-02-16 16:32:18 +0100944 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +0100945 if (mod) {
946 if (!server_opts.wd_basic_mode) {
947 VRB("with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
948 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +0100949 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +0100950 switch (server_opts.wd_basic_mode) {
951 case NC_WD_ALL:
952 strcat(str, "?basic-mode=report-all");
953 break;
954 case NC_WD_TRIM:
955 strcat(str, "?basic-mode=trim");
956 break;
957 case NC_WD_EXPLICIT:
958 strcat(str, "?basic-mode=explicit");
959 break;
960 default:
Michal Vasko9e036d52016-01-08 10:49:26 +0100961 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +0100962 break;
963 }
964
965 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +0200966 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +0100967 if (server_opts.wd_also_supported & NC_WD_ALL) {
968 strcat(str, "report-all,");
969 }
970 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
971 strcat(str, "report-all-tagged,");
972 }
973 if (server_opts.wd_also_supported & NC_WD_TRIM) {
974 strcat(str, "trim,");
975 }
976 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
977 strcat(str, "explicit,");
978 }
979 str[strlen(str) - 1] = '\0';
980
981 add_cpblt(ctx, str, &cpblts, &size, &count);
982 }
983 }
984 }
985
Radek Krejci658782b2016-12-04 22:04:55 +0100986 /* other capabilities */
987 for (u = 0; u < server_opts.capabilities_count; u++) {
988 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100989 }
990
991 /* models */
Michal Vasko1440a742021-03-31 11:11:03 +0200992 u = 0;
Radek Krejci24a18412018-05-16 15:09:10 +0200993 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +0200994 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vasko77367452021-02-16 16:32:18 +0100995 if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) {
Michal Vaskod5ada122020-03-19 18:28:06 +0100996 ERR("Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
997 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +0100998 }
Michal Vaskod5ada122020-03-19 18:28:06 +0100999
Michal Vasko1440a742021-03-31 11:11:03 +02001000 /* get content-id */
1001 if (server_opts.content_id_clb) {
1002 yl_content_id = server_opts.content_id_clb(server_opts.content_id_data);
1003 if (!yl_content_id) {
1004 ERRMEM;
1005 goto error;
1006 }
1007 } else {
1008 yl_content_id = malloc(11);
1009 if (!yl_content_id) {
1010 ERRMEM;
1011 goto error;
1012 }
1013 sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx));
1014 }
1015
Michal Vasko77367452021-02-16 16:32:18 +01001016 if (!strcmp(mod->revision, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001017 /* new one (capab defined in RFC 8526 section 2) */
Michal Vasko1440a742021-03-31 11:11:03 +02001018 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s",
1019 mod->revision, yl_content_id);
Michal Vaskod5ada122020-03-19 18:28:06 +01001020 add_cpblt(ctx, str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001021 } else {
1022 /* old one (capab defined in RFC 7950 section 5.6.4) */
Michal Vasko1440a742021-03-31 11:11:03 +02001023 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s",
1024 mod->revision, yl_content_id);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001025 add_cpblt(ctx, str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001026 }
Michal Vasko1440a742021-03-31 11:11:03 +02001027 free(yl_content_id);
Radek Krejci24a18412018-05-16 15:09:10 +02001028 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001029 } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) {
Radek Krejci24a18412018-05-16 15:09:10 +02001030 /* skip YANG 1.1 schemas */
1031 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001032 } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) {
Radek Krejci24a18412018-05-16 15:09:10 +02001033 /* skip YANG 1.0 schemas */
1034 continue;
1035 }
Michal Vasko086311b2016-01-08 09:53:11 +01001036
Michal Vasko77367452021-02-16 16:32:18 +01001037 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "",
1038 mod->revision ? mod->revision : "");
Radek Krejci24a18412018-05-16 15:09:10 +02001039
Michal Vaskodafdc742020-03-11 16:15:59 +01001040 features_count = 0;
1041 i = 0;
1042 feat = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001043 while ((feat = lysp_feature_next(feat, mod->parsed, &i))) {
Michal Vaskodafdc742020-03-11 16:15:59 +01001044 if (!(feat->flags & LYS_FENABLED)) {
1045 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001046 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001047 if (!features_count) {
1048 strcat(str, "&features=");
1049 str_len += 10;
1050 }
1051 len = strlen(feat->name);
1052 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1053 ERRINT;
1054 break;
1055 }
1056 if (features_count) {
1057 strcat(str, ",");
1058 ++str_len;
1059 }
1060 strcat(str, feat->name);
1061 str_len += len;
1062 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001063 }
Michal Vasko086311b2016-01-08 09:53:11 +01001064
Michal Vasko77367452021-02-16 16:32:18 +01001065 if (mod->deviated_by) {
Radek Krejci24a18412018-05-16 15:09:10 +02001066 strcat(str, "&deviations=");
1067 str_len += 12;
1068 dev_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +01001069 LY_ARRAY_FOR(mod->deviated_by, v) {
1070 len = strlen(mod->deviated_by[v]->name);
1071 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1072 ERRINT;
1073 break;
Radek Krejci24a18412018-05-16 15:09:10 +02001074 }
Michal Vasko77367452021-02-16 16:32:18 +01001075 if (dev_count) {
1076 strcat(str, ",");
1077 ++str_len;
Radek Krejci24a18412018-05-16 15:09:10 +02001078 }
Michal Vasko77367452021-02-16 16:32:18 +01001079 strcat(str, mod->deviated_by[v]->name);
1080 str_len += len;
1081 dev_count++;
Radek Krejci24a18412018-05-16 15:09:10 +02001082 }
1083 }
1084
1085 add_cpblt(ctx, str, &cpblts, &size, &count);
1086 }
Michal Vasko086311b2016-01-08 09:53:11 +01001087
1088 /* ending NULL capability */
1089 add_cpblt(ctx, NULL, &cpblts, &size, &count);
1090
1091 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001092
1093error:
Radek Krejcif906e412017-09-22 14:44:45 +02001094 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001095 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001096}
1097
Radek Krejci24a18412018-05-16 15:09:10 +02001098API const char **
1099nc_server_get_cpblts(struct ly_ctx *ctx)
1100{
1101 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1102}
1103
Radek Krejci695d4fa2015-10-22 13:23:54 +02001104static int
Michal Vasko77367452021-02-16 16:32:18 +01001105parse_cpblts(struct lyd_node *capabilities, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001106{
Michal Vasko77367452021-02-16 16:32:18 +01001107 struct lyd_node *iter;
1108 struct lyd_node_opaq *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001109 int ver = -1, i = 0;
1110 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001111
1112 if (list) {
1113 /* get the storage for server's capabilities */
Michal Vasko77367452021-02-16 16:32:18 +01001114 LY_LIST_FOR(lyd_child(capabilities), iter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001115 i++;
1116 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001117 /* last item remains NULL */
1118 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001119 if (!*list) {
1120 ERRMEM;
1121 return -1;
1122 }
1123 i = 0;
1124 }
1125
Michal Vasko77367452021-02-16 16:32:18 +01001126 LY_LIST_FOR(lyd_child(capabilities), iter) {
1127 cpblt = (struct lyd_node_opaq *)iter;
1128
1129 if (strcmp(cpblt->name.name, "capability") || !cpblt->name.module_ns || strcmp(cpblt->name.module_ns, NC_NS_BASE)) {
1130 ERR("Unexpected <%s> element in client's <hello>.", cpblt->name.name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001131 return -1;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001132 }
1133
Michal Vasko156d3272017-04-11 11:46:49 +02001134 /* skip leading/trailing whitespaces */
Michal Vasko77367452021-02-16 16:32:18 +01001135 for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {}
1136 for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {}
1137 if (!cpb_start[0] || (cpb_end == cpblt->value)) {
1138 ERR("Empty capability \"%s\" received.", cpblt->value);
Michal Vasko156d3272017-04-11 11:46:49 +02001139 return -1;
1140 }
1141
Radek Krejci695d4fa2015-10-22 13:23:54 +02001142 /* detect NETCONF version */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001143 if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001144 ver = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001145 } 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 +02001146 ver = 1;
1147 }
1148
1149 /* store capabilities */
1150 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001151 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1152 if (!(*list)[i]) {
1153 ERRMEM;
1154 return -1;
1155 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001156 i++;
1157 }
1158 }
1159
1160 if (ver == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +01001161 ERR("Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001162 }
1163
1164 return ver;
1165}
1166
1167static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001168nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001169{
Michal Vasko131120a2018-05-29 15:44:02 +02001170 NC_MSG_TYPE ret;
1171 int i, io_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +01001172 const char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001173 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001174
Michal Vasko131120a2018-05-29 15:44:02 +02001175 if (session->side == NC_CLIENT) {
1176 /* client side hello - send only NETCONF base capabilities */
1177 cpblts = malloc(3 * sizeof *cpblts);
1178 if (!cpblts) {
1179 ERRMEM;
1180 return NC_MSG_ERROR;
1181 }
Michal Vasko77367452021-02-16 16:32:18 +01001182 lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0, &cpblts[0]);
1183 lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0, &cpblts[1]);
Michal Vasko131120a2018-05-29 15:44:02 +02001184 cpblts[2] = NULL;
1185
1186 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1187 sid = NULL;
1188 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001189 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0);
Michal Vasko5b24b6b2020-12-04 09:29:47 +01001190 if (!cpblts) {
1191 return NC_MSG_ERROR;
1192 }
Michal Vasko131120a2018-05-29 15:44:02 +02001193
1194 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1195 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001196 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001197
Michal Vasko131120a2018-05-29 15:44:02 +02001198 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001199
Michal Vasko086311b2016-01-08 09:53:11 +01001200 for (i = 0; cpblts[i]; ++i) {
1201 lydict_remove(session->ctx, cpblts[i]);
1202 }
1203 free(cpblts);
1204
Michal Vasko131120a2018-05-29 15:44:02 +02001205 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001206}
1207
1208static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001209nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001210{
Michal Vasko77367452021-02-16 16:32:18 +01001211 struct ly_in *msg;
1212 struct lyd_node *hello = NULL, *iter;
1213 struct lyd_node_opaq *node;
1214 int r, ver = -1, flag = 0;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001215 char *str;
1216 long long int id;
Michal Vasko77367452021-02-16 16:32:18 +01001217 NC_MSG_TYPE rc = NC_MSG_HELLO;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001218
Michal Vasko77367452021-02-16 16:32:18 +01001219 r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg);
1220 switch (r) {
1221 case 1:
Radek Krejci695d4fa2015-10-22 13:23:54 +02001222 /* parse <hello> data */
Michal Vasko77367452021-02-16 16:32:18 +01001223 if (lyd_parse_data(session->ctx, NULL, msg, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_OPAQ, 0, &hello)) {
1224 ERR("Failed to parse server <hello>.");
1225 rc = NC_MSG_ERROR;
1226 goto cleanup;
1227 }
1228
1229 LY_LIST_FOR(lyd_child(hello), iter) {
1230 node = (struct lyd_node_opaq *)iter;
1231
1232 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001233 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001234 } else if (!strcmp(node->name.name, "session-id")) {
1235 if (!node->value || !strlen(node->value)) {
1236 ERR("No value of <session-id> element in server <hello>.");
1237 rc = NC_MSG_ERROR;
1238 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001239 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001240 str = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001241 id = strtoll(node->value, &str, 10);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001242 if (*str || (id < 1) || (id > UINT32_MAX)) {
Michal Vasko77367452021-02-16 16:32:18 +01001243 ERR("Invalid value of <session-id> element in server <hello>.");
1244 rc = NC_MSG_ERROR;
1245 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001246 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001247 session->id = (uint32_t)id;
1248 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001249 } else if (strcmp(node->name.name, "capabilities")) {
1250 ERR("Unexpected <%s> element in server <hello>.", node->name.name);
1251 rc = NC_MSG_ERROR;
1252 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001253 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001254
1255 if (flag) {
1256 /* multiple capabilities elements */
1257 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001258 rc = NC_MSG_ERROR;
1259 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001260 }
1261 flag = 1;
1262
Michal Vasko77367452021-02-16 16:32:18 +01001263 if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) {
1264 rc = NC_MSG_ERROR;
1265 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001266 }
1267 session->version = ver;
1268 }
1269
1270 if (!session->id) {
Michal Vasko77367452021-02-16 16:32:18 +01001271 ERR("Missing <session-id> in server <hello>.");
1272 rc = NC_MSG_ERROR;
1273 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001274 }
1275 break;
Michal Vasko77367452021-02-16 16:32:18 +01001276 case 0:
1277 ERR("Server <hello> timeout elapsed.");
1278 rc = NC_MSG_WOULDBLOCK;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001279 break;
1280 default:
Michal Vasko77367452021-02-16 16:32:18 +01001281 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001282 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001283 }
1284
Michal Vasko77367452021-02-16 16:32:18 +01001285cleanup:
1286 ly_in_free(msg, 1);
1287 lyd_free_tree(hello);
1288 return rc;
Radek Krejci5686ff72015-10-09 13:33:56 +02001289}
1290
Michal Vasko11d142a2016-01-19 15:58:24 +01001291static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001292nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001293{
Michal Vasko77367452021-02-16 16:32:18 +01001294 struct ly_in *msg;
1295 struct lyd_node *hello = NULL, *iter;
1296 struct lyd_node_opaq *node;
1297 NC_MSG_TYPE rc = NC_MSG_HELLO;
1298 int r, ver = -1, flag = 0, timeout_io;
Michal Vasko11d142a2016-01-19 15:58:24 +01001299
Michal Vasko77367452021-02-16 16:32:18 +01001300 timeout_io = server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000;
1301 r = nc_read_msg_poll_io(session, timeout_io, &msg);
1302 switch (r) {
1303 case 1:
1304 /* parse <hello> data */
1305 if (lyd_parse_data(session->ctx, NULL, msg, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_OPAQ, 0, &hello)) {
1306 ERR("Failed to parse client <hello>.");
1307 rc = NC_MSG_ERROR;
1308 goto cleanup;
1309 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001310
Michal Vasko77367452021-02-16 16:32:18 +01001311 /* learn NETCONF version */
1312 LY_LIST_FOR(lyd_child(hello), iter) {
1313 node = (struct lyd_node_opaq *)iter;
1314
1315 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001316 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001317 } else if (strcmp(node->name.name, "capabilities")) {
1318 ERR("Unexpected <%s> element in client <hello>.", node->name.name);
1319 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001320 goto cleanup;
1321 }
1322
1323 if (flag) {
1324 /* multiple capabilities elements */
1325 ERR("Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001326 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001327 goto cleanup;
1328 }
1329 flag = 1;
1330
Michal Vasko77367452021-02-16 16:32:18 +01001331 if ((ver = parse_cpblts(&node->node, NULL)) < 0) {
1332 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001333 goto cleanup;
1334 }
1335 session->version = ver;
1336 }
1337 break;
Michal Vasko77367452021-02-16 16:32:18 +01001338 case 0:
1339 ERR("Client <hello> timeout elapsed.");
1340 rc = NC_MSG_WOULDBLOCK;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001341 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001342 default:
Michal Vasko77367452021-02-16 16:32:18 +01001343 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001344 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001345 }
1346
1347cleanup:
Michal Vasko77367452021-02-16 16:32:18 +01001348 ly_in_free(msg, 1);
1349 lyd_free_tree(hello);
1350 return rc;
Michal Vasko11d142a2016-01-19 15:58:24 +01001351}
1352
Michal Vasko71090fc2016-05-24 16:37:28 +02001353NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001354nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001355{
Michal Vasko086311b2016-01-08 09:53:11 +01001356 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001357
Michal Vasko131120a2018-05-29 15:44:02 +02001358 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001359 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001360 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001361 }
1362
Michal Vasko11d142a2016-01-19 15:58:24 +01001363 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001364 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001365 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001366 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001367 }
1368
Michal Vasko71090fc2016-05-24 16:37:28 +02001369 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001370}
Michal Vasko086311b2016-01-08 09:53:11 +01001371
Michal Vasko889162e2019-09-06 14:43:37 +02001372#ifdef NC_ENABLED_SSH
1373
Michal Vasko999b64a2019-07-10 16:06:15 +02001374static void
1375nc_ssh_init(void)
1376{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001377#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001378 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1379 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001380#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001381}
1382
1383static void
1384nc_ssh_destroy(void)
1385{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001386#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001387 FIPS_mode_set(0);
1388 CONF_modules_unload(1);
1389 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001390#endif
1391
Michal Vaskoe1e82632019-09-09 09:18:03 +02001392#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1393 ssh_finalize();
1394#endif
1395}
1396
Michal Vasko889162e2019-09-06 14:43:37 +02001397#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001398
Radek Krejci53691be2016-02-22 13:58:37 +01001399#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001400
Michal Vasko770b4362017-02-17 14:44:18 +01001401#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1402
Michal Vaskof0c92c02016-01-29 09:41:45 +01001403struct CRYPTO_dynlock_value {
1404 pthread_mutex_t lock;
1405};
1406
Michal Vaskof0c92c02016-01-29 09:41:45 +01001407static struct CRYPTO_dynlock_value *
1408tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1409{
1410 struct CRYPTO_dynlock_value *value;
1411
1412 value = malloc(sizeof *value);
1413 if (!value) {
1414 ERRMEM;
1415 return NULL;
1416 }
1417 pthread_mutex_init(&value->lock, NULL);
1418
1419 return value;
1420}
1421
1422static void
1423tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1424{
1425 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1426 * I found ignored this fact, what do I know... */
1427 if (mode & CRYPTO_LOCK) {
1428 pthread_mutex_lock(&l->lock);
1429 } else {
1430 pthread_mutex_unlock(&l->lock);
1431 }
1432}
1433
1434static void
1435tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1436{
1437 pthread_mutex_destroy(&l->lock);
1438 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001439}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001440
Michal Vasko770b4362017-02-17 14:44:18 +01001441#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001442
Michal Vasko8f0c0282016-02-29 10:17:14 +01001443#endif /* NC_ENABLED_TLS */
1444
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001445#if defined (NC_ENABLED_TLS) && !defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001446
Michal Vasko770b4362017-02-17 14:44:18 +01001447#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001448static pthread_mutex_t *tls_locks;
1449
1450static void
1451tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1452{
1453 if (mode & CRYPTO_LOCK) {
1454 pthread_mutex_lock(tls_locks + n);
1455 } else {
1456 pthread_mutex_unlock(tls_locks + n);
1457 }
1458}
1459
1460static void
1461tls_thread_id_func(CRYPTO_THREADID *tid)
1462{
1463 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1464}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001465
Michal Vasko770b4362017-02-17 14:44:18 +01001466#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001467
1468static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001469nc_tls_init(void)
1470{
Rosen Penev4f552d62019-06-26 16:10:43 -07001471#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001472 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001473 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001474 SSL_library_init();
1475
Michal Vaskof89948c2018-01-04 09:19:46 +01001476 int i;
1477
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001478 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001479 if (!tls_locks) {
1480 ERRMEM;
1481 return;
1482 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001483 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1484 pthread_mutex_init(tls_locks + i, NULL);
1485 }
1486
Michal Vaskof0c92c02016-01-29 09:41:45 +01001487 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001488 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001489
1490 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1491 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1492 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001493#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001494}
1495
Michal Vasko8f0c0282016-02-29 10:17:14 +01001496static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001497nc_tls_destroy(void)
1498{
Rosen Penev4f552d62019-06-26 16:10:43 -07001499#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001500 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001501 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001502 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001503 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001504 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001505#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001506 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001507#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1508 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001509#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001510
Michal Vaskof89948c2018-01-04 09:19:46 +01001511 int i;
1512
Michal Vaskob6e37262016-02-25 14:49:00 +01001513 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001514 CRYPTO_set_locking_callback(NULL);
1515 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1516 pthread_mutex_destroy(tls_locks + i);
1517 }
1518 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001519
1520 CRYPTO_set_dynlock_create_callback(NULL);
1521 CRYPTO_set_dynlock_lock_callback(NULL);
1522 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001523#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001524}
1525
Michal Vasko8f0c0282016-02-29 10:17:14 +01001526#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001527
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001528#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001529
Michal Vasko8f0c0282016-02-29 10:17:14 +01001530static void
Michal Vasko5e228792016-02-03 15:30:24 +01001531nc_ssh_tls_init(void)
1532{
Rosen Penev4f552d62019-06-26 16:10:43 -07001533#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001534 SSL_load_error_strings();
1535 ERR_load_BIO_strings();
1536 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001537#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001538
1539 nc_ssh_init();
1540
Michal Vaskoe1e82632019-09-09 09:18:03 +02001541#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001542 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1543 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1544 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001545#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001546}
1547
Michal Vasko8f0c0282016-02-29 10:17:14 +01001548static void
Michal Vasko5e228792016-02-03 15:30:24 +01001549nc_ssh_tls_destroy(void)
1550{
Rosen Penev4f552d62019-06-26 16:10:43 -07001551#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001552 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001553# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001554 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001555# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001556 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001557# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001558#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001559
1560 nc_ssh_destroy();
1561
Michal Vaskoe1e82632019-09-09 09:18:03 +02001562#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001563 CRYPTO_set_dynlock_create_callback(NULL);
1564 CRYPTO_set_dynlock_lock_callback(NULL);
1565 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001566#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001567}
1568
Radek Krejci53691be2016-02-22 13:58:37 +01001569#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001570
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001571#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001572
1573API void
1574nc_thread_destroy(void)
1575{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001576 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001577 // CRYPTO_cleanup_all_ex_data();
Michal Vasko8f0c0282016-02-29 10:17:14 +01001578
Michal Vasko770b4362017-02-17 14:44:18 +01001579#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1580 CRYPTO_THREADID crypto_tid;
1581
Michal Vasko8f0c0282016-02-29 10:17:14 +01001582 CRYPTO_THREADID_current(&crypto_tid);
1583 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001584#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001585}
1586
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001587#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1588
1589void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001590nc_init(void)
1591{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001592#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001593 nc_ssh_tls_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001594#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001595 nc_ssh_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001596#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001597 nc_tls_init();
1598#endif
1599}
1600
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001601void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001602nc_destroy(void)
1603{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001604#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001605 nc_ssh_tls_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001606#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001607 nc_ssh_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001608#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001609 nc_tls_destroy();
1610#endif
1611}