blob: 5ddc607c64ada033e98cf0ede33e417caa0ad2a6 [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
Michal Vasko95ea9ff2021-11-09 12:29:14 +01002 * @file session.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libnetconf2 - general session functions
Radek Krejci206fcd62015-10-07 15:42:48 +02005 *
Michal Vasko95ea9ff2021-11-09 12:29:14 +01006 * @copyright
7 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
Radek Krejci206fcd62015-10-07 15:42:48 +02008 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01009 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010012 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010013 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci206fcd62015-10-07 15:42:48 +020014 */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +020015#define _GNU_SOURCE
Radek Krejci206fcd62015-10-07 15:42:48 +020016
Michal Vasko18aeb5d2017-02-17 09:23:56 +010017#include <assert.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020018#include <ctype.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020019#include <errno.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020020#include <libyang/libyang.h>
Michal Vaskobe52dc22018-10-17 09:28:17 +020021#include <netinet/in.h>
22#include <netinet/tcp.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020023#include <pthread.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/socket.h>
27#include <sys/time.h>
28#include <sys/types.h>
Michal Vasko58f31552016-01-19 12:39:15 +010029#include <time.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020030#include <unistd.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020031
Michal Vasko9e8ac262020-04-07 13:06:45 +020032#include "compat.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010033#include "libnetconf.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020034#include "session.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010035#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010036
Radek Krejci53691be2016-02-22 13:58:37 +010037#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020038
Michal Vasko086311b2016-01-08 09:53:11 +010039# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020040
Radek Krejci53691be2016-02-22 13:58:37 +010041#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020042
Michal Vaskob83a3fa2021-05-26 09:53:42 +020043#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010044
Michal Vasko5e228792016-02-03 15:30:24 +010045# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010046# include <openssl/err.h>
47
Radek Krejci53691be2016-02-22 13:58:37 +010048#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010049
Michal Vasko086311b2016-01-08 09:53:11 +010050/* in seconds */
51#define NC_CLIENT_HELLO_TIMEOUT 60
fanchanghu75888b62017-08-01 19:51:20 +080052#define NC_SERVER_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020053
Michal Vasko05ba9df2016-01-13 14:40:27 +010054/* in milliseconds */
55#define NC_CLOSE_REPLY_TIMEOUT 200
56
Michal Vasko086311b2016-01-08 09:53:11 +010057extern struct nc_server_opts server_opts;
58
Radek Krejci7ac16052016-07-15 11:48:18 +020059int
Michal Vasko77a6abe2017-10-05 10:02:20 +020060nc_gettimespec_mono(struct timespec *ts)
61{
62#ifdef CLOCK_MONOTONIC_RAW
63 return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
Michal Vaskob83a3fa2021-05-26 09:53:42 +020064#elif defined (CLOCK_MONOTONIC)
Michal Vasko77a6abe2017-10-05 10:02:20 +020065 return clock_gettime(CLOCK_MONOTONIC, ts);
66#else
67 /* no monotonic clock available, return realtime */
68 return nc_gettimespec_real(ts);
69#endif
70}
71
72int
73nc_gettimespec_real(struct timespec *ts)
Radek Krejci7ac16052016-07-15 11:48:18 +020074{
Michal Vasko0ef46df2016-09-21 14:03:18 +020075#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020076 return clock_gettime(CLOCK_REALTIME, ts);
77#else
78 int rc;
79 struct timeval tv;
80
81 rc = gettimeofday(&tv, NULL);
82 if (!rc) {
83 ts->tv_sec = (time_t)tv.tv_sec;
84 ts->tv_nsec = 1000L * (long)tv.tv_usec;
85 }
86 return rc;
87#endif
88}
89
Michal Vasko36c7be82017-02-22 13:37:59 +010090/* ts1 < ts2 -> +, ts1 > ts2 -> -, returns milliseconds */
91int32_t
Radek Krejcida0afb22017-05-26 16:25:59 +020092nc_difftimespec(const struct timespec *ts1, const struct timespec *ts2)
Michal Vasko99f251b2017-01-11 11:31:46 +010093{
Michal Vasko36c7be82017-02-22 13:37:59 +010094 int64_t nsec_diff = 0;
Michal Vasko99f251b2017-01-11 11:31:46 +010095
Michal Vaskoa82bd202017-03-03 14:07:29 +010096 nsec_diff += (((int64_t)ts2->tv_sec) - ((int64_t)ts1->tv_sec)) * 1000000000L;
97 nsec_diff += ((int64_t)ts2->tv_nsec) - ((int64_t)ts1->tv_nsec);
Michal Vasko99f251b2017-01-11 11:31:46 +010098
Michal Vaskob83a3fa2021-05-26 09:53:42 +020099 return nsec_diff ? nsec_diff / 1000000L : 0;
Michal Vasko99f251b2017-01-11 11:31:46 +0100100}
101
Michal Vasko36c7be82017-02-22 13:37:59 +0100102void
103nc_addtimespec(struct timespec *ts, uint32_t msec)
104{
Michal Vasko81c5b302017-03-15 12:10:40 +0100105 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
106
Michal Vasko0dfcd332017-03-03 13:14:39 +0100107 ts->tv_sec += msec / 1000;
108 ts->tv_nsec += (msec % 1000) * 1000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100109
Michal Vasko81c5b302017-03-15 12:10:40 +0100110 if (ts->tv_nsec >= 1000000000L) {
111 ++ts->tv_sec;
112 ts->tv_nsec -= 1000000000L;
113 } else if (ts->tv_nsec < 0) {
114 --ts->tv_sec;
115 ts->tv_nsec += 1000000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100116 }
Michal Vasko81c5b302017-03-15 12:10:40 +0100117
118 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
Michal Vasko36c7be82017-02-22 13:37:59 +0100119}
120
Michal Vaskoddce1212019-05-24 09:58:49 +0200121const char *
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200122nc_keytype2str(NC_SSH_KEY_TYPE type)
Michal Vaskoddce1212019-05-24 09:58:49 +0200123{
124 switch (type) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200125 case NC_SSH_KEY_DSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200126 return "DSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200127 case NC_SSH_KEY_RSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200128 return "RSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200129 case NC_SSH_KEY_ECDSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200130 return "EC";
131 default:
132 break;
133 }
134
135 return NULL;
136}
137
Michal Vaskobe52dc22018-10-17 09:28:17 +0200138int
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200139nc_sock_enable_keepalive(int sock, struct nc_keepalives *ka)
Michal Vaskobe52dc22018-10-17 09:28:17 +0200140{
141 int opt;
142
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200143 opt = ka->enabled;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200144 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200145 ERR(NULL, "Could not set SO_KEEPALIVE option (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200146 return -1;
147 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200148 if (!ka->enabled) {
149 return 0;
150 }
Michal Vaskobe52dc22018-10-17 09:28:17 +0200151
152#ifdef TCP_KEEPIDLE
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200153 opt = ka->idle_time;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200154 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200155 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200156 return -1;
157 }
158#endif
159
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200160#ifdef TCP_KEEPCNT
161 opt = ka->max_probes;
162 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200163 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200164 return -1;
165 }
166#endif
167
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200168#ifdef TCP_KEEPINTVL
169 opt = ka->probe_interval;
170 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200171 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200172 return -1;
173 }
174#endif
175
176 return 0;
177}
178
Michal Vaskoade892d2017-02-22 13:40:35 +0100179struct nc_session *
Michal Vasko131120a2018-05-29 15:44:02 +0200180nc_new_session(NC_SIDE side, int shared_ti)
Michal Vaskoade892d2017-02-22 13:40:35 +0100181{
182 struct nc_session *sess;
183
184 sess = calloc(1, sizeof *sess);
185 if (!sess) {
186 return NULL;
187 }
188
Michal Vasko131120a2018-05-29 15:44:02 +0200189 sess->side = side;
190
191 if (side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100192 pthread_mutex_init(&sess->opts.server.rpc_lock, NULL);
193 pthread_cond_init(&sess->opts.server.rpc_cond, NULL);
194 sess->opts.server.rpc_inuse = 0;
195
196 pthread_mutex_init(&sess->opts.server.ch_lock, NULL);
197 pthread_cond_init(&sess->opts.server.ch_cond, NULL);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200198 } else {
199 pthread_mutex_init(&sess->opts.client.msgs_lock, NULL);
Michal Vasko131120a2018-05-29 15:44:02 +0200200 }
201
202 if (!shared_ti) {
203 sess->io_lock = malloc(sizeof *sess->io_lock);
204 if (!sess->io_lock) {
205 goto error;
206 }
207 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100208 }
209
210 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200211
212error:
Michal Vasko131120a2018-05-29 15:44:02 +0200213 free(sess);
214 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100215}
216
Michal Vasko96164bf2016-01-21 15:41:58 +0100217/*
218 * @return 1 - success
219 * 0 - timeout
220 * -1 - error
221 */
222int
Michal Vasko131120a2018-05-29 15:44:02 +0200223nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100224{
225 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100226 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100227
Michal Vasko131120a2018-05-29 15:44:02 +0200228 if (session->side != NC_SERVER) {
229 ERRINT;
230 return -1;
231 }
232
Michal Vasko96164bf2016-01-21 15:41:58 +0100233 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200234 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100235 nc_addtimespec(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100236
Michal Vaskoade892d2017-02-22 13:40:35 +0100237 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100238 ret = pthread_mutex_timedlock(&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 while (session->opts.server.rpc_inuse) {
241 ret = pthread_cond_timedwait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100242 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100243 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100244 break;
245 }
246 }
247 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100248 } else if (!timeout) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100249 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100250 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100251 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100252 if (session->opts.server.rpc_inuse) {
253 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100254 return 0;
255 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200256 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100257 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100258 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100259 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100260 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100261 while (session->opts.server.rpc_inuse) {
262 ret = pthread_cond_wait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100263 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100264 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100265 break;
266 }
267 }
268 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100269 }
270
Michal Vaskoade892d2017-02-22 13:40:35 +0100271 if (ret) {
272 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
273 /* timeout */
274 return 0;
275 }
276
Michal Vasko96164bf2016-01-21 15:41:58 +0100277 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200278 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100279 return -1;
280 }
281
282 /* ok */
Michal Vaskoacf98472021-02-04 15:33:57 +0100283 assert(session->opts.server.rpc_inuse == 0);
284 session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100285
286 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100287 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100288 if (ret) {
289 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200290 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100291 return -1;
292 }
293
294 return 1;
295}
296
297int
Michal Vasko131120a2018-05-29 15:44:02 +0200298nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100299{
300 int ret;
301 struct timespec ts_timeout;
302
Michal Vasko131120a2018-05-29 15:44:02 +0200303 if (session->side != NC_SERVER) {
304 ERRINT;
305 return -1;
306 }
307
Michal Vaskoacf98472021-02-04 15:33:57 +0100308 assert(session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100309
310 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200311 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100312 nc_addtimespec(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100313
314 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100315 ret = pthread_mutex_timedlock(&session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100316 } else if (!timeout) {
317 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100318 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100319 } else { /* timeout == -1 */
320 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100321 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100322 }
323
324 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
325 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200326 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100327 return -1;
328 } else if (ret) {
Michal Vasko05532772021-06-03 12:12:38 +0200329 WRN(session, "%s: session RPC lock timeout, should not happen.");
Michal Vaskoade892d2017-02-22 13:40:35 +0100330 }
331
Michal Vaskoacf98472021-02-04 15:33:57 +0100332 session->opts.server.rpc_inuse = 0;
333 pthread_cond_signal(&session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100334
335 if (!ret) {
336 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100337 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100338 if (ret) {
339 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200340 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100341 return -1;
342 }
343 }
344
Michal Vasko96164bf2016-01-21 15:41:58 +0100345 return 1;
346}
347
Michal Vasko131120a2018-05-29 15:44:02 +0200348int
349nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
350{
351 int ret;
352 struct timespec ts_timeout;
353
354 if (timeout > 0) {
355 nc_gettimespec_real(&ts_timeout);
356 nc_addtimespec(&ts_timeout, timeout);
357
358 ret = pthread_mutex_timedlock(session->io_lock, &ts_timeout);
359 } else if (!timeout) {
360 ret = pthread_mutex_trylock(session->io_lock);
361 } else { /* timeout == -1 */
Robin Jarry54ea2962018-10-10 10:33:40 +0200362 ret = pthread_mutex_lock(session->io_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200363 }
364
365 if (ret) {
366 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
367 /* timeout */
368 return 0;
369 }
370
371 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200372 ERR(session, "%s: failed to IO lock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200373 return -1;
374 }
375
376 return 1;
377}
378
379int
380nc_session_io_unlock(struct nc_session *session, const char *func)
381{
382 int ret;
383
384 ret = pthread_mutex_unlock(session->io_lock);
385 if (ret) {
386 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200387 ERR(session, "%s: failed to IO unlock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200388 return -1;
389 }
390
391 return 1;
392}
393
Michal Vasko01130bd2021-08-26 11:47:38 +0200394int
395nc_session_client_msgs_lock(struct nc_session *session, int *timeout, const char *func)
396{
397 int ret;
398 int32_t diff_msec;
399 struct timespec ts_timeout, ts_start, ts_end;
400
401 assert(session->side == NC_CLIENT);
402
403 if (*timeout > 0) {
404 /* get current time */
405 nc_gettimespec_real(&ts_start);
406
407 nc_gettimespec_real(&ts_timeout);
408 nc_addtimespec(&ts_timeout, *timeout);
409
410 ret = pthread_mutex_timedlock(&session->opts.client.msgs_lock, &ts_timeout);
411 if (!ret) {
412 /* update timeout based on what was elapsed */
413 nc_gettimespec_real(&ts_end);
414 diff_msec = nc_difftimespec(&ts_start, &ts_end);
415 *timeout -= diff_msec;
416 }
417 } else if (!*timeout) {
418 ret = pthread_mutex_trylock(&session->opts.client.msgs_lock);
419 } else { /* timeout == -1 */
420 ret = pthread_mutex_lock(&session->opts.client.msgs_lock);
421 }
422
423 if (ret) {
424 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
425 /* timeout */
426 return 0;
427 }
428
429 /* error */
430 ERR(session, "%s: failed to MSGS lock a session (%s).", func, strerror(ret));
431 return -1;
432 }
433
434 return 1;
435}
436
437int
438nc_session_client_msgs_unlock(struct nc_session *session, const char *func)
439{
440 int ret;
441
442 assert(session->side == NC_CLIENT);
443
444 ret = pthread_mutex_unlock(&session->opts.client.msgs_lock);
445 if (ret) {
446 /* error */
447 ERR(session, "%s: failed to MSGS unlock a session (%s).", func, strerror(ret));
448 return -1;
449 }
450
451 return 1;
452}
453
Michal Vasko8dadf782016-01-15 10:29:36 +0100454API NC_STATUS
455nc_session_get_status(const struct nc_session *session)
456{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100457 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200458 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200459 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100460 }
461
Michal Vasko8dadf782016-01-15 10:29:36 +0100462 return session->status;
463}
464
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100465API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200466nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100467{
468 if (!session) {
469 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200470 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100471 }
472
473 return session->term_reason;
474}
475
Michal Vasko8dadf782016-01-15 10:29:36 +0100476API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200477nc_session_get_killed_by(const struct nc_session *session)
478{
479 if (!session) {
480 ERRARG("session");
481 return 0;
482 }
483
484 return session->killed_by;
485}
486
487API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100488nc_session_get_id(const struct nc_session *session)
489{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100490 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200491 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100492 return 0;
493 }
494
Michal Vasko8dadf782016-01-15 10:29:36 +0100495 return session->id;
496}
497
Michal Vasko174fe8e2016-02-17 15:38:09 +0100498API int
499nc_session_get_version(const struct nc_session *session)
500{
501 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200502 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100503 return -1;
504 }
505
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200506 return session->version == NC_VERSION_10 ? 0 : 1;
Michal Vasko174fe8e2016-02-17 15:38:09 +0100507}
508
Michal Vasko8dadf782016-01-15 10:29:36 +0100509API NC_TRANSPORT_IMPL
510nc_session_get_ti(const struct nc_session *session)
511{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100512 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200513 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100514 return 0;
515 }
516
Michal Vasko8dadf782016-01-15 10:29:36 +0100517 return session->ti_type;
518}
519
520API const char *
521nc_session_get_username(const struct nc_session *session)
522{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100523 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200524 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100525 return NULL;
526 }
527
Michal Vasko8dadf782016-01-15 10:29:36 +0100528 return session->username;
529}
530
531API const char *
532nc_session_get_host(const struct nc_session *session)
533{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100534 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200535 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100536 return NULL;
537 }
538
Michal Vasko8dadf782016-01-15 10:29:36 +0100539 return session->host;
540}
541
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200542API const char *
543nc_session_get_path(const struct nc_session *session)
544{
545 if (!session) {
546 ERRARG("session");
547 return NULL;
548 }
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200549 if (session->ti_type != NC_TI_UNIX) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200550 return NULL;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200551 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200552
553 return session->path;
554}
555
Michal Vasko8dadf782016-01-15 10:29:36 +0100556API uint16_t
557nc_session_get_port(const struct nc_session *session)
558{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100559 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200560 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100561 return 0;
562 }
563
Michal Vasko8dadf782016-01-15 10:29:36 +0100564 return session->port;
565}
566
Michal Vasko93224072021-11-09 12:14:28 +0100567API const struct ly_ctx *
Michal Vasko9a25e932016-02-01 10:36:42 +0100568nc_session_get_ctx(const struct nc_session *session)
569{
570 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200571 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100572 return NULL;
573 }
574
575 return session->ctx;
576}
577
Michal Vasko2cc4c682016-03-01 09:16:48 +0100578API void
579nc_session_set_data(struct nc_session *session, void *data)
580{
581 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200582 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100583 return;
584 }
585
586 session->data = data;
587}
588
589API void *
590nc_session_get_data(const struct nc_session *session)
591{
592 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200593 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100594 return NULL;
595 }
596
597 return session->data;
598}
599
Michal Vasko086311b2016-01-08 09:53:11 +0100600NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200601nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200602{
Michal Vasko7e1f5fb2021-11-10 10:14:45 +0100603 if (session->ctx != LYD_CTX(op)) {
604 ERR(session, "RPC \"%s\" was created in different context than that of the session.", LYD_NAME(op));
Michal Vasko086311b2016-01-08 09:53:11 +0100605 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100606 }
607
Michal Vasko131120a2018-05-29 15:44:02 +0200608 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100609}
610
Radek Krejci695d4fa2015-10-22 13:23:54 +0200611API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100612nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200613{
Michal Vasko01130bd2021-08-26 11:47:38 +0200614 int r, i, rpc_locked = 0, msgs_locked = 0, sock = -1, timeout;
Michal Vasko428087d2016-01-14 16:04:28 +0100615 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100616 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vasko4589bbe2016-01-29 09:41:30 +0100617 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100618 struct nc_msg_cont *contiter;
Michal Vasko77367452021-02-16 16:32:18 +0100619 struct ly_in *msg;
620 struct lyd_node *close_rpc, *envp;
Michal Vaskoad611702015-12-03 13:41:51 +0100621 const struct lys_module *ietfnc;
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200622 struct timespec ts, ts_cur;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200623 void *p;
624
Michal Vasko428087d2016-01-14 16:04:28 +0100625 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200626 return;
627 }
628
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200629 /* stop notifications thread if any */
630 if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
631 /* let the thread know it should quit */
632 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 2);
633
634 /* wait for it */
635 nc_gettimespec_mono(&ts);
636 nc_addtimespec(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
637 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
638 usleep(NC_TIMEOUT_STEP);
639 nc_gettimespec_mono(&ts_cur);
640 if (nc_difftimespec(&ts_cur, &ts) < 1) {
641 ERR(session, "Waiting for notification thread exit failed (timed out).");
642 break;
643 }
644 }
Michal Vasko86d357c2016-03-11 13:46:38 +0100645 }
646
Michal Vaskoacf98472021-02-04 15:33:57 +0100647 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200648 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100649 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100650 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200651 } else if (r) {
652 rpc_locked = 1;
Michal Vasko96a28a32021-02-04 15:35:20 +0100653 } else {
654 /* else failed to lock it, too bad */
Michal Vasko05532772021-06-03 12:12:38 +0200655 ERR(session, "Freeing a session while an RPC is being processed.");
Michal Vasko96a28a32021-02-04 15:35:20 +0100656 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200657 }
658
Michal Vasko8c247822020-09-07 13:23:23 +0200659 if (session->side == NC_CLIENT) {
Michal Vasko01130bd2021-08-26 11:47:38 +0200660 timeout = NC_SESSION_FREE_LOCK_TIMEOUT;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200661
Michal Vasko01130bd2021-08-26 11:47:38 +0200662 /* MSGS LOCK */
663 r = nc_session_client_msgs_lock(session, &timeout, __func__);
664 if (r == -1) {
665 return;
666 } else if (r) {
667 msgs_locked = 1;
668 } else {
669 /* else failed to lock it, too bad */
670 ERR(session, "Freeing a session while messages are being received.");
671 }
672
673 /* cleanup message queue */
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200674 for (contiter = session->opts.client.msgs; contiter; ) {
Michal Vasko77367452021-02-16 16:32:18 +0100675 ly_in_free(contiter->msg, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200676
Michal Vaskoad611702015-12-03 13:41:51 +0100677 p = contiter;
678 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200679 free(p);
680 }
681
Michal Vasko01130bd2021-08-26 11:47:38 +0200682 if (msgs_locked) {
683 /* MSGS UNLOCK */
684 nc_session_client_msgs_unlock(session, __func__);
685 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200686
Michal Vasko8c247822020-09-07 13:23:23 +0200687 if (session->status == NC_STATUS_RUNNING) {
Michal Vasko9c6d38c2021-09-03 13:02:53 +0200688 /* receive any leftover messages */
689 while (nc_read_msg_poll_io(session, 0, &msg) == 1) {
690 ly_in_free(msg, 1);
691 }
692
Michal Vasko8c247822020-09-07 13:23:23 +0200693 /* send closing info to the other side */
Michal Vasko77367452021-02-16 16:32:18 +0100694 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
Michal Vasko8c247822020-09-07 13:23:23 +0200695 if (!ietfnc) {
Michal Vasko05532772021-06-03 12:12:38 +0200696 WRN(session, "Missing ietf-netconf schema in context, unable to send <close-session>.");
Michal Vaskod9c38992021-05-28 13:32:15 +0200697 } else if (!lyd_new_inner(NULL, ietfnc, "close-session", 0, &close_rpc)) {
Michal Vasko8c247822020-09-07 13:23:23 +0200698 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
Michal Vasko77367452021-02-16 16:32:18 +0100699 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &msg)) {
700 case 1:
701 if (lyd_parse_op(session->ctx, close_rpc, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, &envp, NULL)) {
Michal Vasko05532772021-06-03 12:12:38 +0200702 WRN(session, "Failed to parse <close-session> reply.");
Michal Vasko77367452021-02-16 16:32:18 +0100703 } else if (!lyd_child(envp) || strcmp(LYD_NAME(lyd_child(envp)), "ok")) {
Michal Vasko05532772021-06-03 12:12:38 +0200704 WRN(session, "Reply to <close-session> was not <ok> as expected.");
Michal Vasko8c247822020-09-07 13:23:23 +0200705 }
Michal Vasko77367452021-02-16 16:32:18 +0100706 lyd_free_tree(envp);
707 ly_in_free(msg, 1);
Michal Vasko8c247822020-09-07 13:23:23 +0200708 break;
Michal Vasko77367452021-02-16 16:32:18 +0100709 case 0:
Michal Vasko05532772021-06-03 12:12:38 +0200710 WRN(session, "Timeout for receiving a reply to <close-session> elapsed.");
Michal Vasko8c247822020-09-07 13:23:23 +0200711 break;
Michal Vasko77367452021-02-16 16:32:18 +0100712 case -1:
Michal Vasko05532772021-06-03 12:12:38 +0200713 ERR(session, "Failed to receive a reply to <close-session>.");
Michal Vasko8c247822020-09-07 13:23:23 +0200714 break;
715 default:
716 /* cannot happen */
717 break;
Michal Vaskofad6e912015-10-26 15:37:22 +0100718 }
Michal Vasko77367452021-02-16 16:32:18 +0100719 lyd_free_tree(close_rpc);
Michal Vaskofad6e912015-10-26 15:37:22 +0100720 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200721 }
722
723 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200724 if (session->opts.client.cpblts) {
725 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200726 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200727 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200728 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200729 }
730 }
731
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100732 if (session->data && data_free) {
733 data_free(session->data);
734 }
735
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200736 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
737 /* CH LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100738 pthread_mutex_lock(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200739 }
740
Michal Vasko86d357c2016-03-11 13:46:38 +0100741 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200742 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200743
744 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100745 pthread_cond_signal(&session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200746
Michal Vasko0db3db52021-03-03 10:45:42 +0100747 nc_gettimespec_real(&ts);
748 nc_addtimespec(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
749
750 /* wait for CH thread to actually wake up and terminate */
751 r = 0;
752 while (!r && (session->flags & NC_SESSION_CALLHOME)) {
753 r = pthread_cond_timedwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, &ts);
754 }
755
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200756 /* CH UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100757 pthread_mutex_unlock(&session->opts.server.ch_lock);
Michal Vasko3f05a092018-03-13 10:39:49 +0100758
Michal Vasko0db3db52021-03-03 10:45:42 +0100759 if (r) {
Michal Vasko05532772021-06-03 12:12:38 +0200760 ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r));
Michal Vasko3f05a092018-03-13 10:39:49 +0100761 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200762 }
763
Michal Vasko428087d2016-01-14 16:04:28 +0100764 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200765
766 /* transport implementation cleanup */
767 switch (session->ti_type) {
768 case NC_TI_FD:
769 /* nothing needed - file descriptors were provided by caller,
770 * so it is up to the caller to close them correctly
771 * TODO use callbacks
772 */
Michal Vasko3512e402016-01-28 16:22:34 +0100773 /* just to avoid compiler warning */
774 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100775 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200776 break;
777
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200778 case NC_TI_UNIX:
779 sock = session->ti.unixsock.sock;
780 (void)connected;
781 (void)siter;
782 break;
783
Radek Krejci53691be2016-02-22 13:58:37 +0100784#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200785 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100786 if (connected) {
787 ssh_channel_free(session->ti.libssh.channel);
788 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200789 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
790 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200791 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
Radek Krejci695d4fa2015-10-22 13:23:54 +0200792 */
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200793 /* SESSION IO LOCK */
794 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
795
Michal Vasko96164bf2016-01-21 15:41:58 +0100796 multisession = 0;
797 if (session->ti.libssh.next) {
798 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
799 if (siter->status != NC_STATUS_STARTING) {
800 multisession = 1;
801 break;
802 }
803 }
804 }
805
806 if (!multisession) {
807 /* it's not multisession yet, but we still need to free the starting sessions */
808 if (session->ti.libssh.next) {
809 do {
810 siter = session->ti.libssh.next;
811 session->ti.libssh.next = siter->ti.libssh.next;
812
813 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vaskoe38655d2021-11-12 15:41:16 +0100814 free(siter->username);
815 free(siter->host);
816 if (!(siter->flags & NC_SESSION_SHAREDCTX)) {
817 ly_ctx_destroy((struct ly_ctx *)siter->ctx);
Michal Vasko96164bf2016-01-21 15:41:58 +0100818 }
819
820 free(siter);
821 } while (session->ti.libssh.next != session);
822 }
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100823 /* remember sock so we can close it */
824 sock = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100825 if (connected) {
826 ssh_disconnect(session->ti.libssh.session);
Michal Vasko06e0fc22019-05-09 09:32:27 +0200827 sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100828 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200829 ssh_free(session->ti.libssh.session);
830 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200831 /* remove the session from the list */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200832 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {}
Michal Vaskoaec4f212015-10-26 15:37:45 +0100833 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200834 /* there will be only one session */
835 siter->ti.libssh.next = NULL;
836 } else {
837 /* there are still multiple sessions, keep the ring list */
838 siter->ti.libssh.next = session->ti.libssh.next;
839 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100840 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
841 if (session->flags & NC_SESSION_SSH_MSG_CB) {
Michal Vasko5e0edd82020-07-29 15:26:13 +0200842 siter = session->ti.libssh.next;
843 while (siter && siter->status != NC_STATUS_RUNNING) {
Michal Vasko96164bf2016-01-21 15:41:58 +0100844 if (siter->ti.libssh.next == session) {
845 ERRINT;
846 break;
847 }
Michal Vasko5e0edd82020-07-29 15:26:13 +0200848 siter = siter->ti.libssh.next;
Michal Vasko96164bf2016-01-21 15:41:58 +0100849 }
Michal Vasko5e0edd82020-07-29 15:26:13 +0200850 /* siter may be NULL in case all the sessions terminated at the same time (socket was disconnected),
851 * we set session to NULL because we do not expect any new message to arrive */
Michal Vasko96164bf2016-01-21 15:41:58 +0100852 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
Michal Vasko5e0edd82020-07-29 15:26:13 +0200853 if (siter) {
854 siter->flags |= NC_SESSION_SSH_MSG_CB;
855 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100856 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200857 }
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200858
859 /* SESSION IO UNLOCK */
860 if (r == 1) {
861 nc_session_io_unlock(session, __func__);
862 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200863 break;
864#endif
865
Radek Krejci53691be2016-02-22 13:58:37 +0100866#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200867 case NC_TI_OPENSSL:
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100868 /* remember sock so we can close it */
869 sock = SSL_get_fd(session->ti.tls);
870
Michal Vasko428087d2016-01-14 16:04:28 +0100871 if (connected) {
872 SSL_shutdown(session->ti.tls);
873 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200874 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100875
Michal Vasko2e6defd2016-10-07 15:48:15 +0200876 if (session->side == NC_SERVER) {
877 X509_free(session->opts.server.client_cert);
878 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200879 break;
880#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100881 case NC_TI_NONE:
Michal Vasko428087d2016-01-14 16:04:28 +0100882 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200883 }
Michal Vasko428087d2016-01-14 16:04:28 +0100884
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100885 /* close socket separately */
886 if (sock > -1) {
887 close(sock);
888 }
889
Michal Vasko93224072021-11-09 12:14:28 +0100890 free(session->username);
891 free(session->host);
892 free(session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200893
894 /* final cleanup */
Michal Vaskoacf98472021-02-04 15:33:57 +0100895 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200896 if (rpc_locked) {
897 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100898 }
Michal Vaskoacf98472021-02-04 15:33:57 +0100899 pthread_mutex_destroy(&session->opts.server.rpc_lock);
900 pthread_cond_destroy(&session->opts.server.rpc_cond);
Michal Vasko131120a2018-05-29 15:44:02 +0200901 }
902
903 if (session->io_lock && !multisession) {
904 pthread_mutex_destroy(session->io_lock);
905 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200906 }
907
908 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko93224072021-11-09 12:14:28 +0100909 ly_ctx_destroy((struct ly_ctx *)session->ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200910 }
911
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200912 if (session->side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100913 /* free CH synchronization structures */
914 pthread_cond_destroy(&session->opts.server.ch_cond);
915 pthread_mutex_destroy(&session->opts.server.ch_lock);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200916 } else {
917 pthread_mutex_destroy(&session->opts.client.msgs_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200918 }
919
Radek Krejci695d4fa2015-10-22 13:23:54 +0200920 free(session);
921}
922
Michal Vasko086311b2016-01-08 09:53:11 +0100923static void
Michal Vasko93224072021-11-09 12:14:28 +0100924add_cpblt(const char *capab, char ***cpblts, int *size, int *count)
Michal Vasko086311b2016-01-08 09:53:11 +0100925{
Radek Krejci658782b2016-12-04 22:04:55 +0100926 size_t len;
927 int i;
928 char *p;
929
930 if (capab) {
931 /* check if already present */
932 p = strchr(capab, '?');
933 if (p) {
934 len = p - capab;
935 } else {
936 len = strlen(capab);
937 }
938 for (i = 0; i < *count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200939 if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) {
Radek Krejci658782b2016-12-04 22:04:55 +0100940 /* already present, do not duplicate it */
941 return;
942 }
943 }
944 }
945
946 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100947 if (*count == *size) {
948 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100949 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
950 if (!(*cpblts)) {
951 ERRMEM;
952 return;
953 }
Michal Vasko086311b2016-01-08 09:53:11 +0100954 }
955
Michal Vasko93224072021-11-09 12:14:28 +0100956 (*cpblts)[*count] = capab ? strdup(capab) : NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100957 ++(*count);
958}
959
Michal Vasko93224072021-11-09 12:14:28 +0100960API char **
961nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +0100962{
Michal Vasko93224072021-11-09 12:14:28 +0100963 char **cpblts;
Michal Vasko77367452021-02-16 16:32:18 +0100964 const struct lys_module *mod;
965 struct lysp_feature *feat;
966 int size = 10, count, features_count = 0, dev_count = 0, str_len, len;
Michal Vasko1440a742021-03-31 11:11:03 +0200967 uint32_t i, u;
Michal Vasko77367452021-02-16 16:32:18 +0100968 LY_ARRAY_COUNT_TYPE v;
Michal Vasko1440a742021-03-31 11:11:03 +0200969 char *yl_content_id;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200970
Radek Krejci24a18412018-05-16 15:09:10 +0200971#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +0200972 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100973
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200974 if (!ctx) {
975 ERRARG("ctx");
976 return NULL;
977 }
978
Michal Vasko086311b2016-01-08 09:53:11 +0100979 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100980 if (!cpblts) {
981 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200982 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100983 }
Michal Vasko93224072021-11-09 12:14:28 +0100984 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
985 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko086311b2016-01-08 09:53:11 +0100986 count = 2;
987
988 /* capabilities */
989
Michal Vasko77367452021-02-16 16:32:18 +0100990 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf");
Michal Vasko086311b2016-01-08 09:53:11 +0100991 if (mod) {
Michal Vasko77367452021-02-16 16:32:18 +0100992 if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +0100993 add_cpblt("urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100994 }
Michal Vasko77367452021-02-16 16:32:18 +0100995 if (lys_feature_value(mod, "candidate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +0100996 add_cpblt("urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko77367452021-02-16 16:32:18 +0100997 if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +0100998 add_cpblt("urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100999 }
1000 }
Michal Vasko77367452021-02-16 16:32:18 +01001001 if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001002 add_cpblt("urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001003 }
Michal Vasko77367452021-02-16 16:32:18 +01001004 if (lys_feature_value(mod, "validate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001005 add_cpblt("urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001006 }
Michal Vasko77367452021-02-16 16:32:18 +01001007 if (lys_feature_value(mod, "startup") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001008 add_cpblt("urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001009 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001010
1011 /* The URL capability must be set manually using nc_server_set_capability()
1012 * because of the need for supported protocols to be included.
1013 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1014 */
Michal Vasko77367452021-02-16 16:32:18 +01001015 // if (lys_feature_value(mod, "url") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001016 // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
mekleoa8de5e92020-02-13 09:05:56 +01001017 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001018
Michal Vasko77367452021-02-16 16:32:18 +01001019 if (lys_feature_value(mod, "xpath") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001020 add_cpblt("urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001021 }
1022 }
1023
Michal Vasko77367452021-02-16 16:32:18 +01001024 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01001025 if (mod) {
1026 if (!server_opts.wd_basic_mode) {
Michal Vasko05532772021-06-03 12:12:38 +02001027 VRB(NULL, "with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
Michal Vasko086311b2016-01-08 09:53:11 +01001028 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001029 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +01001030 switch (server_opts.wd_basic_mode) {
1031 case NC_WD_ALL:
1032 strcat(str, "?basic-mode=report-all");
1033 break;
1034 case NC_WD_TRIM:
1035 strcat(str, "?basic-mode=trim");
1036 break;
1037 case NC_WD_EXPLICIT:
1038 strcat(str, "?basic-mode=explicit");
1039 break;
1040 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001041 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001042 break;
1043 }
1044
1045 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001046 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +01001047 if (server_opts.wd_also_supported & NC_WD_ALL) {
1048 strcat(str, "report-all,");
1049 }
1050 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
1051 strcat(str, "report-all-tagged,");
1052 }
1053 if (server_opts.wd_also_supported & NC_WD_TRIM) {
1054 strcat(str, "trim,");
1055 }
1056 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
1057 strcat(str, "explicit,");
1058 }
1059 str[strlen(str) - 1] = '\0';
1060
Michal Vasko93224072021-11-09 12:14:28 +01001061 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001062 }
1063 }
1064 }
1065
Radek Krejci658782b2016-12-04 22:04:55 +01001066 /* other capabilities */
1067 for (u = 0; u < server_opts.capabilities_count; u++) {
Michal Vasko93224072021-11-09 12:14:28 +01001068 add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001069 }
1070
1071 /* models */
Michal Vasko1440a742021-03-31 11:11:03 +02001072 u = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001073 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001074 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vasko77367452021-02-16 16:32:18 +01001075 if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) {
Michal Vasko05532772021-06-03 12:12:38 +02001076 ERR(NULL, "Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
Michal Vaskod5ada122020-03-19 18:28:06 +01001077 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001078 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001079
Michal Vasko1440a742021-03-31 11:11:03 +02001080 /* get content-id */
1081 if (server_opts.content_id_clb) {
1082 yl_content_id = server_opts.content_id_clb(server_opts.content_id_data);
1083 if (!yl_content_id) {
1084 ERRMEM;
1085 goto error;
1086 }
1087 } else {
1088 yl_content_id = malloc(11);
1089 if (!yl_content_id) {
1090 ERRMEM;
1091 goto error;
1092 }
1093 sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx));
1094 }
1095
Michal Vasko77367452021-02-16 16:32:18 +01001096 if (!strcmp(mod->revision, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001097 /* new one (capab defined in RFC 8526 section 2) */
Michal Vasko1440a742021-03-31 11:11:03 +02001098 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s",
1099 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001100 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001101 } else {
1102 /* old one (capab defined in RFC 7950 section 5.6.4) */
Michal Vasko1440a742021-03-31 11:11:03 +02001103 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s",
1104 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001105 add_cpblt(str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001106 }
Michal Vasko1440a742021-03-31 11:11:03 +02001107 free(yl_content_id);
Radek Krejci24a18412018-05-16 15:09:10 +02001108 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001109 } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) {
Radek Krejci24a18412018-05-16 15:09:10 +02001110 /* skip YANG 1.1 schemas */
1111 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001112 } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) {
Radek Krejci24a18412018-05-16 15:09:10 +02001113 /* skip YANG 1.0 schemas */
1114 continue;
1115 }
Michal Vasko086311b2016-01-08 09:53:11 +01001116
Michal Vasko77367452021-02-16 16:32:18 +01001117 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "",
1118 mod->revision ? mod->revision : "");
Radek Krejci24a18412018-05-16 15:09:10 +02001119
Michal Vaskodafdc742020-03-11 16:15:59 +01001120 features_count = 0;
1121 i = 0;
1122 feat = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001123 while ((feat = lysp_feature_next(feat, mod->parsed, &i))) {
Michal Vaskodafdc742020-03-11 16:15:59 +01001124 if (!(feat->flags & LYS_FENABLED)) {
1125 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001126 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001127 if (!features_count) {
1128 strcat(str, "&features=");
1129 str_len += 10;
1130 }
1131 len = strlen(feat->name);
1132 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1133 ERRINT;
1134 break;
1135 }
1136 if (features_count) {
1137 strcat(str, ",");
1138 ++str_len;
1139 }
1140 strcat(str, feat->name);
1141 str_len += len;
1142 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001143 }
Michal Vasko086311b2016-01-08 09:53:11 +01001144
Michal Vasko77367452021-02-16 16:32:18 +01001145 if (mod->deviated_by) {
Radek Krejci24a18412018-05-16 15:09:10 +02001146 strcat(str, "&deviations=");
1147 str_len += 12;
1148 dev_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +01001149 LY_ARRAY_FOR(mod->deviated_by, v) {
1150 len = strlen(mod->deviated_by[v]->name);
1151 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1152 ERRINT;
1153 break;
Radek Krejci24a18412018-05-16 15:09:10 +02001154 }
Michal Vasko77367452021-02-16 16:32:18 +01001155 if (dev_count) {
1156 strcat(str, ",");
1157 ++str_len;
Radek Krejci24a18412018-05-16 15:09:10 +02001158 }
Michal Vasko77367452021-02-16 16:32:18 +01001159 strcat(str, mod->deviated_by[v]->name);
1160 str_len += len;
1161 dev_count++;
Radek Krejci24a18412018-05-16 15:09:10 +02001162 }
1163 }
1164
Michal Vasko93224072021-11-09 12:14:28 +01001165 add_cpblt(str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +02001166 }
Michal Vasko086311b2016-01-08 09:53:11 +01001167
1168 /* ending NULL capability */
Michal Vasko93224072021-11-09 12:14:28 +01001169 add_cpblt(NULL, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001170
1171 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001172
1173error:
Radek Krejcif906e412017-09-22 14:44:45 +02001174 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001175 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001176}
1177
Michal Vasko93224072021-11-09 12:14:28 +01001178API char **
1179nc_server_get_cpblts(const struct ly_ctx *ctx)
Radek Krejci24a18412018-05-16 15:09:10 +02001180{
1181 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1182}
1183
Radek Krejci695d4fa2015-10-22 13:23:54 +02001184static int
Michal Vasko77367452021-02-16 16:32:18 +01001185parse_cpblts(struct lyd_node *capabilities, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001186{
Michal Vasko77367452021-02-16 16:32:18 +01001187 struct lyd_node *iter;
1188 struct lyd_node_opaq *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001189 int ver = -1, i = 0;
1190 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001191
1192 if (list) {
1193 /* get the storage for server's capabilities */
Michal Vasko77367452021-02-16 16:32:18 +01001194 LY_LIST_FOR(lyd_child(capabilities), iter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001195 i++;
1196 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001197 /* last item remains NULL */
1198 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001199 if (!*list) {
1200 ERRMEM;
1201 return -1;
1202 }
1203 i = 0;
1204 }
1205
Michal Vasko77367452021-02-16 16:32:18 +01001206 LY_LIST_FOR(lyd_child(capabilities), iter) {
1207 cpblt = (struct lyd_node_opaq *)iter;
1208
1209 if (strcmp(cpblt->name.name, "capability") || !cpblt->name.module_ns || strcmp(cpblt->name.module_ns, NC_NS_BASE)) {
Michal Vasko05532772021-06-03 12:12:38 +02001210 ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001211 return -1;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001212 }
1213
Michal Vasko156d3272017-04-11 11:46:49 +02001214 /* skip leading/trailing whitespaces */
Michal Vasko77367452021-02-16 16:32:18 +01001215 for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {}
1216 for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {}
1217 if (!cpb_start[0] || (cpb_end == cpblt->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001218 ERR(NULL, "Empty capability \"%s\" received.", cpblt->value);
Michal Vasko156d3272017-04-11 11:46:49 +02001219 return -1;
1220 }
1221
Radek Krejci695d4fa2015-10-22 13:23:54 +02001222 /* detect NETCONF version */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001223 if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001224 ver = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001225 } 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 +02001226 ver = 1;
1227 }
1228
1229 /* store capabilities */
1230 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001231 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1232 if (!(*list)[i]) {
1233 ERRMEM;
1234 return -1;
1235 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001236 i++;
1237 }
1238 }
1239
1240 if (ver == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001241 ERR(NULL, "Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001242 }
1243
1244 return ver;
1245}
1246
1247static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001248nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001249{
Michal Vasko131120a2018-05-29 15:44:02 +02001250 NC_MSG_TYPE ret;
1251 int i, io_timeout;
Michal Vasko93224072021-11-09 12:14:28 +01001252 char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001253 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001254
Michal Vasko131120a2018-05-29 15:44:02 +02001255 if (session->side == NC_CLIENT) {
1256 /* client side hello - send only NETCONF base capabilities */
1257 cpblts = malloc(3 * sizeof *cpblts);
1258 if (!cpblts) {
1259 ERRMEM;
1260 return NC_MSG_ERROR;
1261 }
Michal Vasko93224072021-11-09 12:14:28 +01001262 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1263 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko131120a2018-05-29 15:44:02 +02001264 cpblts[2] = NULL;
1265
1266 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1267 sid = NULL;
1268 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001269 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0);
Michal Vasko5b24b6b2020-12-04 09:29:47 +01001270 if (!cpblts) {
1271 return NC_MSG_ERROR;
1272 }
Michal Vasko131120a2018-05-29 15:44:02 +02001273
1274 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1275 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001276 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001277
Michal Vasko131120a2018-05-29 15:44:02 +02001278 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001279
Michal Vasko086311b2016-01-08 09:53:11 +01001280 for (i = 0; cpblts[i]; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001281 free(cpblts[i]);
Michal Vasko086311b2016-01-08 09:53:11 +01001282 }
1283 free(cpblts);
1284
Michal Vasko131120a2018-05-29 15:44:02 +02001285 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001286}
1287
1288static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001289nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001290{
Michal Vasko77367452021-02-16 16:32:18 +01001291 struct ly_in *msg;
1292 struct lyd_node *hello = NULL, *iter;
1293 struct lyd_node_opaq *node;
1294 int r, ver = -1, flag = 0;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001295 char *str;
1296 long long int id;
Michal Vasko77367452021-02-16 16:32:18 +01001297 NC_MSG_TYPE rc = NC_MSG_HELLO;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001298
Michal Vasko77367452021-02-16 16:32:18 +01001299 r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg);
1300 switch (r) {
1301 case 1:
Radek Krejci695d4fa2015-10-22 13:23:54 +02001302 /* parse <hello> data */
Michal Vasko77367452021-02-16 16:32:18 +01001303 if (lyd_parse_data(session->ctx, NULL, msg, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_OPAQ, 0, &hello)) {
Michal Vasko05532772021-06-03 12:12:38 +02001304 ERR(session, "Failed to parse server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001305 rc = NC_MSG_ERROR;
1306 goto cleanup;
1307 }
1308
1309 LY_LIST_FOR(lyd_child(hello), iter) {
1310 node = (struct lyd_node_opaq *)iter;
1311
1312 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001313 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001314 } else if (!strcmp(node->name.name, "session-id")) {
1315 if (!node->value || !strlen(node->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001316 ERR(session, "No value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001317 rc = NC_MSG_ERROR;
1318 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001319 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001320 str = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001321 id = strtoll(node->value, &str, 10);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001322 if (*str || (id < 1) || (id > UINT32_MAX)) {
Michal Vasko05532772021-06-03 12:12:38 +02001323 ERR(session, "Invalid value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001324 rc = NC_MSG_ERROR;
1325 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001326 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001327 session->id = (uint32_t)id;
1328 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001329 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001330 ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001331 rc = NC_MSG_ERROR;
1332 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001333 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001334
1335 if (flag) {
1336 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001337 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001338 rc = NC_MSG_ERROR;
1339 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001340 }
1341 flag = 1;
1342
Michal Vasko77367452021-02-16 16:32:18 +01001343 if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) {
1344 rc = NC_MSG_ERROR;
1345 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001346 }
1347 session->version = ver;
1348 }
1349
1350 if (!session->id) {
Michal Vasko05532772021-06-03 12:12:38 +02001351 ERR(session, "Missing <session-id> in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001352 rc = NC_MSG_ERROR;
1353 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001354 }
1355 break;
Michal Vasko77367452021-02-16 16:32:18 +01001356 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001357 ERR(session, "Server <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001358 rc = NC_MSG_WOULDBLOCK;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001359 break;
1360 default:
Michal Vasko77367452021-02-16 16:32:18 +01001361 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001362 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001363 }
1364
Michal Vasko77367452021-02-16 16:32:18 +01001365cleanup:
1366 ly_in_free(msg, 1);
1367 lyd_free_tree(hello);
1368 return rc;
Radek Krejci5686ff72015-10-09 13:33:56 +02001369}
1370
Michal Vasko11d142a2016-01-19 15:58:24 +01001371static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001372nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001373{
Michal Vasko77367452021-02-16 16:32:18 +01001374 struct ly_in *msg;
1375 struct lyd_node *hello = NULL, *iter;
1376 struct lyd_node_opaq *node;
1377 NC_MSG_TYPE rc = NC_MSG_HELLO;
1378 int r, ver = -1, flag = 0, timeout_io;
Michal Vasko11d142a2016-01-19 15:58:24 +01001379
Michal Vasko77367452021-02-16 16:32:18 +01001380 timeout_io = server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000;
1381 r = nc_read_msg_poll_io(session, timeout_io, &msg);
1382 switch (r) {
1383 case 1:
1384 /* parse <hello> data */
1385 if (lyd_parse_data(session->ctx, NULL, msg, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_OPAQ, 0, &hello)) {
Michal Vasko05532772021-06-03 12:12:38 +02001386 ERR(session, "Failed to parse client <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001387 rc = NC_MSG_ERROR;
1388 goto cleanup;
1389 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001390
Michal Vasko77367452021-02-16 16:32:18 +01001391 /* learn NETCONF version */
1392 LY_LIST_FOR(lyd_child(hello), iter) {
1393 node = (struct lyd_node_opaq *)iter;
1394
1395 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001396 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001397 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001398 ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001399 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001400 goto cleanup;
1401 }
1402
1403 if (flag) {
1404 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001405 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001406 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001407 goto cleanup;
1408 }
1409 flag = 1;
1410
Michal Vasko77367452021-02-16 16:32:18 +01001411 if ((ver = parse_cpblts(&node->node, NULL)) < 0) {
1412 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001413 goto cleanup;
1414 }
1415 session->version = ver;
1416 }
1417 break;
Michal Vasko77367452021-02-16 16:32:18 +01001418 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001419 ERR(session, "Client <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001420 rc = NC_MSG_WOULDBLOCK;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001421 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001422 default:
Michal Vasko77367452021-02-16 16:32:18 +01001423 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001424 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001425 }
1426
1427cleanup:
Michal Vasko77367452021-02-16 16:32:18 +01001428 ly_in_free(msg, 1);
1429 lyd_free_tree(hello);
1430 return rc;
Michal Vasko11d142a2016-01-19 15:58:24 +01001431}
1432
Michal Vasko71090fc2016-05-24 16:37:28 +02001433NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001434nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001435{
Michal Vasko086311b2016-01-08 09:53:11 +01001436 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001437
Michal Vasko131120a2018-05-29 15:44:02 +02001438 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001439 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001440 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001441 }
1442
Michal Vasko11d142a2016-01-19 15:58:24 +01001443 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001444 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001445 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001446 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001447 }
1448
Michal Vasko71090fc2016-05-24 16:37:28 +02001449 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001450}
Michal Vasko086311b2016-01-08 09:53:11 +01001451
Michal Vasko889162e2019-09-06 14:43:37 +02001452#ifdef NC_ENABLED_SSH
1453
Michal Vasko999b64a2019-07-10 16:06:15 +02001454static void
1455nc_ssh_init(void)
1456{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001457#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001458 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1459 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001460#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001461}
1462
1463static void
1464nc_ssh_destroy(void)
1465{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001466#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001467 FIPS_mode_set(0);
1468 CONF_modules_unload(1);
1469 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001470#endif
1471
Michal Vaskoe1e82632019-09-09 09:18:03 +02001472#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1473 ssh_finalize();
1474#endif
1475}
1476
Michal Vasko889162e2019-09-06 14:43:37 +02001477#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001478
Radek Krejci53691be2016-02-22 13:58:37 +01001479#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001480
Michal Vasko770b4362017-02-17 14:44:18 +01001481#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1482
Michal Vaskof0c92c02016-01-29 09:41:45 +01001483struct CRYPTO_dynlock_value {
1484 pthread_mutex_t lock;
1485};
1486
Michal Vaskof0c92c02016-01-29 09:41:45 +01001487static struct CRYPTO_dynlock_value *
1488tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1489{
1490 struct CRYPTO_dynlock_value *value;
1491
1492 value = malloc(sizeof *value);
1493 if (!value) {
1494 ERRMEM;
1495 return NULL;
1496 }
1497 pthread_mutex_init(&value->lock, NULL);
1498
1499 return value;
1500}
1501
1502static void
1503tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1504{
1505 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1506 * I found ignored this fact, what do I know... */
1507 if (mode & CRYPTO_LOCK) {
1508 pthread_mutex_lock(&l->lock);
1509 } else {
1510 pthread_mutex_unlock(&l->lock);
1511 }
1512}
1513
1514static void
1515tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1516{
1517 pthread_mutex_destroy(&l->lock);
1518 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001519}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001520
Michal Vasko770b4362017-02-17 14:44:18 +01001521#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001522
Michal Vasko8f0c0282016-02-29 10:17:14 +01001523#endif /* NC_ENABLED_TLS */
1524
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001525#if defined (NC_ENABLED_TLS) && !defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001526
Michal Vasko770b4362017-02-17 14:44:18 +01001527#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001528static pthread_mutex_t *tls_locks;
1529
1530static void
1531tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1532{
1533 if (mode & CRYPTO_LOCK) {
1534 pthread_mutex_lock(tls_locks + n);
1535 } else {
1536 pthread_mutex_unlock(tls_locks + n);
1537 }
1538}
1539
1540static void
1541tls_thread_id_func(CRYPTO_THREADID *tid)
1542{
1543 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1544}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001545
Michal Vasko770b4362017-02-17 14:44:18 +01001546#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001547
1548static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001549nc_tls_init(void)
1550{
Rosen Penev4f552d62019-06-26 16:10:43 -07001551#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001552 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001553 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001554 SSL_library_init();
1555
Michal Vaskof89948c2018-01-04 09:19:46 +01001556 int i;
1557
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001558 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001559 if (!tls_locks) {
1560 ERRMEM;
1561 return;
1562 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001563 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1564 pthread_mutex_init(tls_locks + i, NULL);
1565 }
1566
Michal Vaskof0c92c02016-01-29 09:41:45 +01001567 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001568 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001569
1570 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1571 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1572 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001573#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001574}
1575
Michal Vasko8f0c0282016-02-29 10:17:14 +01001576static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001577nc_tls_destroy(void)
1578{
Rosen Penev4f552d62019-06-26 16:10:43 -07001579#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001580 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001581 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001582 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001583 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001584 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001585#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001586 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001587#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1588 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001589#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001590
Michal Vaskof89948c2018-01-04 09:19:46 +01001591 int i;
1592
Michal Vaskob6e37262016-02-25 14:49:00 +01001593 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001594 CRYPTO_set_locking_callback(NULL);
1595 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1596 pthread_mutex_destroy(tls_locks + i);
1597 }
1598 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001599
1600 CRYPTO_set_dynlock_create_callback(NULL);
1601 CRYPTO_set_dynlock_lock_callback(NULL);
1602 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001603#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001604}
1605
Michal Vasko8f0c0282016-02-29 10:17:14 +01001606#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001607
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001608#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001609
Michal Vasko8f0c0282016-02-29 10:17:14 +01001610static void
Michal Vasko5e228792016-02-03 15:30:24 +01001611nc_ssh_tls_init(void)
1612{
Rosen Penev4f552d62019-06-26 16:10:43 -07001613#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001614 SSL_load_error_strings();
1615 ERR_load_BIO_strings();
1616 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001617#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001618
1619 nc_ssh_init();
1620
Michal Vaskoe1e82632019-09-09 09:18:03 +02001621#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001622 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1623 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1624 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001625#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001626}
1627
Michal Vasko8f0c0282016-02-29 10:17:14 +01001628static void
Michal Vasko5e228792016-02-03 15:30:24 +01001629nc_ssh_tls_destroy(void)
1630{
Rosen Penev4f552d62019-06-26 16:10:43 -07001631#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001632 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001633# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001634 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001635# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001636 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001637# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001638#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001639
1640 nc_ssh_destroy();
1641
Michal Vaskoe1e82632019-09-09 09:18:03 +02001642#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001643 CRYPTO_set_dynlock_create_callback(NULL);
1644 CRYPTO_set_dynlock_lock_callback(NULL);
1645 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001646#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001647}
1648
Radek Krejci53691be2016-02-22 13:58:37 +01001649#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001650
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001651#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001652
1653API void
1654nc_thread_destroy(void)
1655{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001656 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001657 // CRYPTO_cleanup_all_ex_data();
Michal Vasko8f0c0282016-02-29 10:17:14 +01001658
Michal Vasko770b4362017-02-17 14:44:18 +01001659#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1660 CRYPTO_THREADID crypto_tid;
1661
Michal Vasko8f0c0282016-02-29 10:17:14 +01001662 CRYPTO_THREADID_current(&crypto_tid);
1663 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001664#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001665}
1666
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001667#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1668
1669void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001670nc_init(void)
1671{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001672#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001673 nc_ssh_tls_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001674#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001675 nc_ssh_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001676#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001677 nc_tls_init();
1678#endif
1679}
1680
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001681void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001682nc_destroy(void)
1683{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001684#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001685 nc_ssh_tls_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001686#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001687 nc_ssh_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001688#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001689 nc_tls_destroy();
1690#endif
1691}