blob: 4e6d2693baa7c86853e585b1e9a03e8fc0acdfa7 [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 Vaskodf68e7e2022-04-21 11:04:00 +0200192 pthread_mutex_init(&sess->opts.server.ntf_status_lock, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100193 pthread_mutex_init(&sess->opts.server.rpc_lock, NULL);
194 pthread_cond_init(&sess->opts.server.rpc_cond, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100195
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
Michal Vaskofeccb312022-03-24 15:24:59 +0100744 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CH_THREAD)) {
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;
Michal Vaskofeccb312022-03-24 15:24:59 +0100752 while (!r && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vasko0db3db52021-03-03 10:45:42 +0100753 r = pthread_cond_timedwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, &ts);
754 }
Michal Vasko0db3db52021-03-03 10:45:42 +0100755 if (r) {
Michal Vasko05532772021-06-03 12:12:38 +0200756 ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r));
Michal Vasko3f05a092018-03-13 10:39:49 +0100757 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200758 }
759
Michal Vaskofeccb312022-03-24 15:24:59 +0100760 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
761 /* CH UNLOCK */
762 pthread_mutex_unlock(&session->opts.server.ch_lock);
763 }
764
Michal Vasko428087d2016-01-14 16:04:28 +0100765 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200766
767 /* transport implementation cleanup */
768 switch (session->ti_type) {
769 case NC_TI_FD:
770 /* nothing needed - file descriptors were provided by caller,
771 * so it is up to the caller to close them correctly
772 * TODO use callbacks
773 */
Michal Vasko3512e402016-01-28 16:22:34 +0100774 /* just to avoid compiler warning */
775 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100776 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200777 break;
778
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200779 case NC_TI_UNIX:
780 sock = session->ti.unixsock.sock;
781 (void)connected;
782 (void)siter;
783 break;
784
Radek Krejci53691be2016-02-22 13:58:37 +0100785#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200786 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100787 if (connected) {
788 ssh_channel_free(session->ti.libssh.channel);
789 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200790 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
791 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200792 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
Radek Krejci695d4fa2015-10-22 13:23:54 +0200793 */
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200794 /* SESSION IO LOCK */
795 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
796
Michal Vasko96164bf2016-01-21 15:41:58 +0100797 multisession = 0;
798 if (session->ti.libssh.next) {
799 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
800 if (siter->status != NC_STATUS_STARTING) {
801 multisession = 1;
802 break;
803 }
804 }
805 }
806
807 if (!multisession) {
808 /* it's not multisession yet, but we still need to free the starting sessions */
809 if (session->ti.libssh.next) {
810 do {
811 siter = session->ti.libssh.next;
812 session->ti.libssh.next = siter->ti.libssh.next;
813
814 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vaskoe38655d2021-11-12 15:41:16 +0100815 free(siter->username);
816 free(siter->host);
817 if (!(siter->flags & NC_SESSION_SHAREDCTX)) {
818 ly_ctx_destroy((struct ly_ctx *)siter->ctx);
Michal Vasko96164bf2016-01-21 15:41:58 +0100819 }
820
821 free(siter);
822 } while (session->ti.libssh.next != session);
823 }
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100824 /* remember sock so we can close it */
825 sock = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100826 if (connected) {
827 ssh_disconnect(session->ti.libssh.session);
Michal Vasko06e0fc22019-05-09 09:32:27 +0200828 sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100829 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200830 ssh_free(session->ti.libssh.session);
831 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200832 /* remove the session from the list */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200833 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {}
Michal Vaskoaec4f212015-10-26 15:37:45 +0100834 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200835 /* there will be only one session */
836 siter->ti.libssh.next = NULL;
837 } else {
838 /* there are still multiple sessions, keep the ring list */
839 siter->ti.libssh.next = session->ti.libssh.next;
840 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100841 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
842 if (session->flags & NC_SESSION_SSH_MSG_CB) {
Michal Vasko5e0edd82020-07-29 15:26:13 +0200843 siter = session->ti.libssh.next;
844 while (siter && siter->status != NC_STATUS_RUNNING) {
Michal Vasko96164bf2016-01-21 15:41:58 +0100845 if (siter->ti.libssh.next == session) {
846 ERRINT;
847 break;
848 }
Michal Vasko5e0edd82020-07-29 15:26:13 +0200849 siter = siter->ti.libssh.next;
Michal Vasko96164bf2016-01-21 15:41:58 +0100850 }
Michal Vasko5e0edd82020-07-29 15:26:13 +0200851 /* siter may be NULL in case all the sessions terminated at the same time (socket was disconnected),
852 * we set session to NULL because we do not expect any new message to arrive */
Michal Vasko96164bf2016-01-21 15:41:58 +0100853 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
Michal Vasko5e0edd82020-07-29 15:26:13 +0200854 if (siter) {
855 siter->flags |= NC_SESSION_SSH_MSG_CB;
856 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100857 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200858 }
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200859
860 /* SESSION IO UNLOCK */
861 if (r == 1) {
862 nc_session_io_unlock(session, __func__);
863 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200864 break;
865#endif
866
Radek Krejci53691be2016-02-22 13:58:37 +0100867#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200868 case NC_TI_OPENSSL:
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100869 /* remember sock so we can close it */
870 sock = SSL_get_fd(session->ti.tls);
871
Michal Vasko428087d2016-01-14 16:04:28 +0100872 if (connected) {
873 SSL_shutdown(session->ti.tls);
874 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200875 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100876
Michal Vasko2e6defd2016-10-07 15:48:15 +0200877 if (session->side == NC_SERVER) {
878 X509_free(session->opts.server.client_cert);
879 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200880 break;
881#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100882 case NC_TI_NONE:
Michal Vasko428087d2016-01-14 16:04:28 +0100883 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200884 }
Michal Vasko428087d2016-01-14 16:04:28 +0100885
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100886 /* close socket separately */
887 if (sock > -1) {
888 close(sock);
889 }
890
Michal Vasko93224072021-11-09 12:14:28 +0100891 free(session->username);
892 free(session->host);
893 free(session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200894
895 /* final cleanup */
Michal Vaskoacf98472021-02-04 15:33:57 +0100896 if (session->side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200897 pthread_mutex_destroy(&session->opts.server.ntf_status_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200898 if (rpc_locked) {
899 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100900 }
Michal Vaskoacf98472021-02-04 15:33:57 +0100901 pthread_mutex_destroy(&session->opts.server.rpc_lock);
902 pthread_cond_destroy(&session->opts.server.rpc_cond);
Michal Vasko131120a2018-05-29 15:44:02 +0200903 }
904
905 if (session->io_lock && !multisession) {
906 pthread_mutex_destroy(session->io_lock);
907 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200908 }
909
910 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko93224072021-11-09 12:14:28 +0100911 ly_ctx_destroy((struct ly_ctx *)session->ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200912 }
913
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200914 if (session->side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100915 /* free CH synchronization structures */
916 pthread_cond_destroy(&session->opts.server.ch_cond);
917 pthread_mutex_destroy(&session->opts.server.ch_lock);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200918 } else {
919 pthread_mutex_destroy(&session->opts.client.msgs_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200920 }
921
Radek Krejci695d4fa2015-10-22 13:23:54 +0200922 free(session);
923}
924
Michal Vasko086311b2016-01-08 09:53:11 +0100925static void
Michal Vasko93224072021-11-09 12:14:28 +0100926add_cpblt(const char *capab, char ***cpblts, int *size, int *count)
Michal Vasko086311b2016-01-08 09:53:11 +0100927{
Radek Krejci658782b2016-12-04 22:04:55 +0100928 size_t len;
929 int i;
930 char *p;
931
932 if (capab) {
933 /* check if already present */
934 p = strchr(capab, '?');
935 if (p) {
936 len = p - capab;
937 } else {
938 len = strlen(capab);
939 }
940 for (i = 0; i < *count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200941 if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) {
Radek Krejci658782b2016-12-04 22:04:55 +0100942 /* already present, do not duplicate it */
943 return;
944 }
945 }
946 }
947
948 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100949 if (*count == *size) {
950 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100951 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
952 if (!(*cpblts)) {
953 ERRMEM;
954 return;
955 }
Michal Vasko086311b2016-01-08 09:53:11 +0100956 }
957
Michal Vasko93224072021-11-09 12:14:28 +0100958 (*cpblts)[*count] = capab ? strdup(capab) : NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100959 ++(*count);
960}
961
Michal Vasko93224072021-11-09 12:14:28 +0100962API char **
963nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +0100964{
Michal Vasko93224072021-11-09 12:14:28 +0100965 char **cpblts;
Michal Vasko77367452021-02-16 16:32:18 +0100966 const struct lys_module *mod;
967 struct lysp_feature *feat;
968 int size = 10, count, features_count = 0, dev_count = 0, str_len, len;
Michal Vasko1440a742021-03-31 11:11:03 +0200969 uint32_t i, u;
Michal Vasko77367452021-02-16 16:32:18 +0100970 LY_ARRAY_COUNT_TYPE v;
Michal Vasko1440a742021-03-31 11:11:03 +0200971 char *yl_content_id;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200972
Radek Krejci24a18412018-05-16 15:09:10 +0200973#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +0200974 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100975
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200976 if (!ctx) {
977 ERRARG("ctx");
978 return NULL;
979 }
980
Michal Vasko086311b2016-01-08 09:53:11 +0100981 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100982 if (!cpblts) {
983 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200984 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100985 }
Michal Vasko93224072021-11-09 12:14:28 +0100986 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
987 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko086311b2016-01-08 09:53:11 +0100988 count = 2;
989
990 /* capabilities */
991
Michal Vasko77367452021-02-16 16:32:18 +0100992 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf");
Michal Vasko086311b2016-01-08 09:53:11 +0100993 if (mod) {
Michal Vasko77367452021-02-16 16:32:18 +0100994 if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +0100995 add_cpblt("urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100996 }
Michal Vasko77367452021-02-16 16:32:18 +0100997 if (lys_feature_value(mod, "candidate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +0100998 add_cpblt("urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko77367452021-02-16 16:32:18 +0100999 if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001000 add_cpblt("urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001001 }
1002 }
Michal Vasko77367452021-02-16 16:32:18 +01001003 if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001004 add_cpblt("urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001005 }
Michal Vasko77367452021-02-16 16:32:18 +01001006 if (lys_feature_value(mod, "validate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001007 add_cpblt("urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001008 }
Michal Vasko77367452021-02-16 16:32:18 +01001009 if (lys_feature_value(mod, "startup") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001010 add_cpblt("urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001011 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001012
1013 /* The URL capability must be set manually using nc_server_set_capability()
1014 * because of the need for supported protocols to be included.
1015 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1016 */
Michal Vasko77367452021-02-16 16:32:18 +01001017 // if (lys_feature_value(mod, "url") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001018 // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
mekleoa8de5e92020-02-13 09:05:56 +01001019 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001020
Michal Vasko77367452021-02-16 16:32:18 +01001021 if (lys_feature_value(mod, "xpath") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001022 add_cpblt("urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001023 }
1024 }
1025
Michal Vasko77367452021-02-16 16:32:18 +01001026 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01001027 if (mod) {
1028 if (!server_opts.wd_basic_mode) {
Michal Vasko05532772021-06-03 12:12:38 +02001029 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 +01001030 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001031 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +01001032 switch (server_opts.wd_basic_mode) {
1033 case NC_WD_ALL:
1034 strcat(str, "?basic-mode=report-all");
1035 break;
1036 case NC_WD_TRIM:
1037 strcat(str, "?basic-mode=trim");
1038 break;
1039 case NC_WD_EXPLICIT:
1040 strcat(str, "?basic-mode=explicit");
1041 break;
1042 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001043 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001044 break;
1045 }
1046
1047 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001048 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +01001049 if (server_opts.wd_also_supported & NC_WD_ALL) {
1050 strcat(str, "report-all,");
1051 }
1052 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
1053 strcat(str, "report-all-tagged,");
1054 }
1055 if (server_opts.wd_also_supported & NC_WD_TRIM) {
1056 strcat(str, "trim,");
1057 }
1058 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
1059 strcat(str, "explicit,");
1060 }
1061 str[strlen(str) - 1] = '\0';
1062
Michal Vasko93224072021-11-09 12:14:28 +01001063 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001064 }
1065 }
1066 }
1067
Radek Krejci658782b2016-12-04 22:04:55 +01001068 /* other capabilities */
1069 for (u = 0; u < server_opts.capabilities_count; u++) {
Michal Vasko93224072021-11-09 12:14:28 +01001070 add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001071 }
1072
1073 /* models */
Michal Vasko1440a742021-03-31 11:11:03 +02001074 u = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001075 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001076 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vasko77367452021-02-16 16:32:18 +01001077 if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) {
Michal Vasko05532772021-06-03 12:12:38 +02001078 ERR(NULL, "Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
Michal Vaskod5ada122020-03-19 18:28:06 +01001079 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001080 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001081
Michal Vasko1440a742021-03-31 11:11:03 +02001082 /* get content-id */
1083 if (server_opts.content_id_clb) {
1084 yl_content_id = server_opts.content_id_clb(server_opts.content_id_data);
1085 if (!yl_content_id) {
1086 ERRMEM;
1087 goto error;
1088 }
1089 } else {
1090 yl_content_id = malloc(11);
1091 if (!yl_content_id) {
1092 ERRMEM;
1093 goto error;
1094 }
1095 sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx));
1096 }
1097
Michal Vasko77367452021-02-16 16:32:18 +01001098 if (!strcmp(mod->revision, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001099 /* new one (capab defined in RFC 8526 section 2) */
Michal Vasko1440a742021-03-31 11:11:03 +02001100 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s",
1101 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001102 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001103 } else {
1104 /* old one (capab defined in RFC 7950 section 5.6.4) */
Michal Vasko1440a742021-03-31 11:11:03 +02001105 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s",
1106 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001107 add_cpblt(str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001108 }
Michal Vasko1440a742021-03-31 11:11:03 +02001109 free(yl_content_id);
Radek Krejci24a18412018-05-16 15:09:10 +02001110 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001111 } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) {
Radek Krejci24a18412018-05-16 15:09:10 +02001112 /* skip YANG 1.1 schemas */
1113 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001114 } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) {
Radek Krejci24a18412018-05-16 15:09:10 +02001115 /* skip YANG 1.0 schemas */
1116 continue;
1117 }
Michal Vasko086311b2016-01-08 09:53:11 +01001118
Michal Vasko77367452021-02-16 16:32:18 +01001119 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "",
1120 mod->revision ? mod->revision : "");
Radek Krejci24a18412018-05-16 15:09:10 +02001121
Michal Vaskodafdc742020-03-11 16:15:59 +01001122 features_count = 0;
1123 i = 0;
1124 feat = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001125 while ((feat = lysp_feature_next(feat, mod->parsed, &i))) {
Michal Vaskodafdc742020-03-11 16:15:59 +01001126 if (!(feat->flags & LYS_FENABLED)) {
1127 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001128 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001129 if (!features_count) {
1130 strcat(str, "&features=");
1131 str_len += 10;
1132 }
1133 len = strlen(feat->name);
1134 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1135 ERRINT;
1136 break;
1137 }
1138 if (features_count) {
1139 strcat(str, ",");
1140 ++str_len;
1141 }
1142 strcat(str, feat->name);
1143 str_len += len;
1144 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001145 }
Michal Vasko086311b2016-01-08 09:53:11 +01001146
Michal Vasko77367452021-02-16 16:32:18 +01001147 if (mod->deviated_by) {
Radek Krejci24a18412018-05-16 15:09:10 +02001148 strcat(str, "&deviations=");
1149 str_len += 12;
1150 dev_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +01001151 LY_ARRAY_FOR(mod->deviated_by, v) {
1152 len = strlen(mod->deviated_by[v]->name);
1153 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1154 ERRINT;
1155 break;
Radek Krejci24a18412018-05-16 15:09:10 +02001156 }
Michal Vasko77367452021-02-16 16:32:18 +01001157 if (dev_count) {
1158 strcat(str, ",");
1159 ++str_len;
Radek Krejci24a18412018-05-16 15:09:10 +02001160 }
Michal Vasko77367452021-02-16 16:32:18 +01001161 strcat(str, mod->deviated_by[v]->name);
1162 str_len += len;
1163 dev_count++;
Radek Krejci24a18412018-05-16 15:09:10 +02001164 }
1165 }
1166
Michal Vasko93224072021-11-09 12:14:28 +01001167 add_cpblt(str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +02001168 }
Michal Vasko086311b2016-01-08 09:53:11 +01001169
1170 /* ending NULL capability */
Michal Vasko93224072021-11-09 12:14:28 +01001171 add_cpblt(NULL, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001172
1173 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001174
1175error:
Radek Krejcif906e412017-09-22 14:44:45 +02001176 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001177 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001178}
1179
Michal Vasko93224072021-11-09 12:14:28 +01001180API char **
1181nc_server_get_cpblts(const struct ly_ctx *ctx)
Radek Krejci24a18412018-05-16 15:09:10 +02001182{
1183 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1184}
1185
Radek Krejci695d4fa2015-10-22 13:23:54 +02001186static int
Michal Vasko77367452021-02-16 16:32:18 +01001187parse_cpblts(struct lyd_node *capabilities, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001188{
Michal Vasko77367452021-02-16 16:32:18 +01001189 struct lyd_node *iter;
1190 struct lyd_node_opaq *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001191 int ver = -1, i = 0;
1192 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001193
1194 if (list) {
1195 /* get the storage for server's capabilities */
Michal Vasko77367452021-02-16 16:32:18 +01001196 LY_LIST_FOR(lyd_child(capabilities), iter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001197 i++;
1198 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001199 /* last item remains NULL */
1200 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001201 if (!*list) {
1202 ERRMEM;
1203 return -1;
1204 }
1205 i = 0;
1206 }
1207
Michal Vasko77367452021-02-16 16:32:18 +01001208 LY_LIST_FOR(lyd_child(capabilities), iter) {
1209 cpblt = (struct lyd_node_opaq *)iter;
1210
1211 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 +02001212 ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001213 return -1;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001214 }
1215
Michal Vasko156d3272017-04-11 11:46:49 +02001216 /* skip leading/trailing whitespaces */
Michal Vasko77367452021-02-16 16:32:18 +01001217 for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {}
1218 for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {}
1219 if (!cpb_start[0] || (cpb_end == cpblt->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001220 ERR(NULL, "Empty capability \"%s\" received.", cpblt->value);
Michal Vasko156d3272017-04-11 11:46:49 +02001221 return -1;
1222 }
1223
Radek Krejci695d4fa2015-10-22 13:23:54 +02001224 /* detect NETCONF version */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001225 if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001226 ver = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001227 } 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 +02001228 ver = 1;
1229 }
1230
1231 /* store capabilities */
1232 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001233 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1234 if (!(*list)[i]) {
1235 ERRMEM;
1236 return -1;
1237 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001238 i++;
1239 }
1240 }
1241
1242 if (ver == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001243 ERR(NULL, "Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001244 }
1245
1246 return ver;
1247}
1248
1249static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001250nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001251{
Michal Vasko131120a2018-05-29 15:44:02 +02001252 NC_MSG_TYPE ret;
1253 int i, io_timeout;
Michal Vasko93224072021-11-09 12:14:28 +01001254 char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001255 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001256
Michal Vasko131120a2018-05-29 15:44:02 +02001257 if (session->side == NC_CLIENT) {
1258 /* client side hello - send only NETCONF base capabilities */
1259 cpblts = malloc(3 * sizeof *cpblts);
1260 if (!cpblts) {
1261 ERRMEM;
1262 return NC_MSG_ERROR;
1263 }
Michal Vasko93224072021-11-09 12:14:28 +01001264 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1265 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko131120a2018-05-29 15:44:02 +02001266 cpblts[2] = NULL;
1267
1268 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1269 sid = NULL;
1270 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001271 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0);
Michal Vasko5b24b6b2020-12-04 09:29:47 +01001272 if (!cpblts) {
1273 return NC_MSG_ERROR;
1274 }
Michal Vasko131120a2018-05-29 15:44:02 +02001275
1276 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1277 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001278 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001279
Michal Vasko131120a2018-05-29 15:44:02 +02001280 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001281
Michal Vasko086311b2016-01-08 09:53:11 +01001282 for (i = 0; cpblts[i]; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001283 free(cpblts[i]);
Michal Vasko086311b2016-01-08 09:53:11 +01001284 }
1285 free(cpblts);
1286
Michal Vasko131120a2018-05-29 15:44:02 +02001287 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001288}
1289
1290static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001291nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001292{
Michal Vasko77367452021-02-16 16:32:18 +01001293 struct ly_in *msg;
1294 struct lyd_node *hello = NULL, *iter;
1295 struct lyd_node_opaq *node;
1296 int r, ver = -1, flag = 0;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001297 char *str;
1298 long long int id;
Michal Vasko77367452021-02-16 16:32:18 +01001299 NC_MSG_TYPE rc = NC_MSG_HELLO;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001300
Michal Vasko77367452021-02-16 16:32:18 +01001301 r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg);
1302 switch (r) {
1303 case 1:
Radek Krejci695d4fa2015-10-22 13:23:54 +02001304 /* parse <hello> data */
Michal Vasko77367452021-02-16 16:32:18 +01001305 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 +02001306 ERR(session, "Failed to parse server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001307 rc = NC_MSG_ERROR;
1308 goto cleanup;
1309 }
1310
1311 LY_LIST_FOR(lyd_child(hello), iter) {
1312 node = (struct lyd_node_opaq *)iter;
1313
1314 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001315 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001316 } else if (!strcmp(node->name.name, "session-id")) {
1317 if (!node->value || !strlen(node->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001318 ERR(session, "No value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001319 rc = NC_MSG_ERROR;
1320 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001321 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001322 str = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001323 id = strtoll(node->value, &str, 10);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001324 if (*str || (id < 1) || (id > UINT32_MAX)) {
Michal Vasko05532772021-06-03 12:12:38 +02001325 ERR(session, "Invalid value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001326 rc = NC_MSG_ERROR;
1327 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001328 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001329 session->id = (uint32_t)id;
1330 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001331 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001332 ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001333 rc = NC_MSG_ERROR;
1334 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001335 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001336
1337 if (flag) {
1338 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001339 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001340 rc = NC_MSG_ERROR;
1341 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001342 }
1343 flag = 1;
1344
Michal Vasko77367452021-02-16 16:32:18 +01001345 if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) {
1346 rc = NC_MSG_ERROR;
1347 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001348 }
1349 session->version = ver;
1350 }
1351
1352 if (!session->id) {
Michal Vasko05532772021-06-03 12:12:38 +02001353 ERR(session, "Missing <session-id> in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001354 rc = NC_MSG_ERROR;
1355 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001356 }
1357 break;
Michal Vasko77367452021-02-16 16:32:18 +01001358 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001359 ERR(session, "Server <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001360 rc = NC_MSG_WOULDBLOCK;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001361 break;
1362 default:
Michal Vasko77367452021-02-16 16:32:18 +01001363 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001364 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001365 }
1366
Michal Vasko77367452021-02-16 16:32:18 +01001367cleanup:
1368 ly_in_free(msg, 1);
1369 lyd_free_tree(hello);
1370 return rc;
Radek Krejci5686ff72015-10-09 13:33:56 +02001371}
1372
Michal Vasko11d142a2016-01-19 15:58:24 +01001373static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001374nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001375{
Michal Vasko77367452021-02-16 16:32:18 +01001376 struct ly_in *msg;
1377 struct lyd_node *hello = NULL, *iter;
1378 struct lyd_node_opaq *node;
1379 NC_MSG_TYPE rc = NC_MSG_HELLO;
1380 int r, ver = -1, flag = 0, timeout_io;
Michal Vasko11d142a2016-01-19 15:58:24 +01001381
Michal Vasko77367452021-02-16 16:32:18 +01001382 timeout_io = server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000;
1383 r = nc_read_msg_poll_io(session, timeout_io, &msg);
1384 switch (r) {
1385 case 1:
1386 /* parse <hello> data */
1387 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 +02001388 ERR(session, "Failed to parse client <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001389 rc = NC_MSG_ERROR;
1390 goto cleanup;
1391 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001392
Michal Vasko77367452021-02-16 16:32:18 +01001393 /* learn NETCONF version */
1394 LY_LIST_FOR(lyd_child(hello), iter) {
1395 node = (struct lyd_node_opaq *)iter;
1396
1397 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001398 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001399 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001400 ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001401 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001402 goto cleanup;
1403 }
1404
1405 if (flag) {
1406 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001407 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001408 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001409 goto cleanup;
1410 }
1411 flag = 1;
1412
Michal Vasko77367452021-02-16 16:32:18 +01001413 if ((ver = parse_cpblts(&node->node, NULL)) < 0) {
1414 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001415 goto cleanup;
1416 }
1417 session->version = ver;
1418 }
1419 break;
Michal Vasko77367452021-02-16 16:32:18 +01001420 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001421 ERR(session, "Client <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001422 rc = NC_MSG_WOULDBLOCK;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001423 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001424 default:
Michal Vasko77367452021-02-16 16:32:18 +01001425 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001426 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001427 }
1428
1429cleanup:
Michal Vasko77367452021-02-16 16:32:18 +01001430 ly_in_free(msg, 1);
1431 lyd_free_tree(hello);
1432 return rc;
Michal Vasko11d142a2016-01-19 15:58:24 +01001433}
1434
Michal Vasko71090fc2016-05-24 16:37:28 +02001435NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001436nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001437{
Michal Vasko086311b2016-01-08 09:53:11 +01001438 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001439
Michal Vasko131120a2018-05-29 15:44:02 +02001440 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001441 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001442 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001443 }
1444
Michal Vasko11d142a2016-01-19 15:58:24 +01001445 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001446 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001447 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001448 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001449 }
1450
Michal Vasko71090fc2016-05-24 16:37:28 +02001451 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001452}
Michal Vasko086311b2016-01-08 09:53:11 +01001453
Michal Vasko889162e2019-09-06 14:43:37 +02001454#ifdef NC_ENABLED_SSH
1455
Michal Vasko999b64a2019-07-10 16:06:15 +02001456static void
1457nc_ssh_init(void)
1458{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001459#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001460 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1461 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001462#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001463}
1464
1465static void
1466nc_ssh_destroy(void)
1467{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001468#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001469 FIPS_mode_set(0);
1470 CONF_modules_unload(1);
1471 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001472#endif
1473
Michal Vaskoe1e82632019-09-09 09:18:03 +02001474#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1475 ssh_finalize();
1476#endif
1477}
1478
Michal Vasko889162e2019-09-06 14:43:37 +02001479#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001480
Radek Krejci53691be2016-02-22 13:58:37 +01001481#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001482
Michal Vasko770b4362017-02-17 14:44:18 +01001483#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1484
Michal Vaskof0c92c02016-01-29 09:41:45 +01001485struct CRYPTO_dynlock_value {
1486 pthread_mutex_t lock;
1487};
1488
Michal Vaskof0c92c02016-01-29 09:41:45 +01001489static struct CRYPTO_dynlock_value *
1490tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1491{
1492 struct CRYPTO_dynlock_value *value;
1493
1494 value = malloc(sizeof *value);
1495 if (!value) {
1496 ERRMEM;
1497 return NULL;
1498 }
1499 pthread_mutex_init(&value->lock, NULL);
1500
1501 return value;
1502}
1503
1504static void
1505tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1506{
1507 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1508 * I found ignored this fact, what do I know... */
1509 if (mode & CRYPTO_LOCK) {
1510 pthread_mutex_lock(&l->lock);
1511 } else {
1512 pthread_mutex_unlock(&l->lock);
1513 }
1514}
1515
1516static void
1517tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1518{
1519 pthread_mutex_destroy(&l->lock);
1520 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001521}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001522
Michal Vasko770b4362017-02-17 14:44:18 +01001523#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001524
Michal Vasko8f0c0282016-02-29 10:17:14 +01001525#endif /* NC_ENABLED_TLS */
1526
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001527#if defined (NC_ENABLED_TLS) && !defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001528
Michal Vasko770b4362017-02-17 14:44:18 +01001529#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001530static pthread_mutex_t *tls_locks;
1531
1532static void
1533tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1534{
1535 if (mode & CRYPTO_LOCK) {
1536 pthread_mutex_lock(tls_locks + n);
1537 } else {
1538 pthread_mutex_unlock(tls_locks + n);
1539 }
1540}
1541
1542static void
1543tls_thread_id_func(CRYPTO_THREADID *tid)
1544{
1545 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1546}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001547
Michal Vasko770b4362017-02-17 14:44:18 +01001548#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001549
1550static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001551nc_tls_init(void)
1552{
Rosen Penev4f552d62019-06-26 16:10:43 -07001553#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001554 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001555 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001556 SSL_library_init();
1557
Michal Vaskof89948c2018-01-04 09:19:46 +01001558 int i;
1559
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001560 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001561 if (!tls_locks) {
1562 ERRMEM;
1563 return;
1564 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001565 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1566 pthread_mutex_init(tls_locks + i, NULL);
1567 }
1568
Michal Vaskof0c92c02016-01-29 09:41:45 +01001569 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001570 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001571
1572 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1573 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1574 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001575#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001576}
1577
Michal Vasko8f0c0282016-02-29 10:17:14 +01001578static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001579nc_tls_destroy(void)
1580{
Rosen Penev4f552d62019-06-26 16:10:43 -07001581#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001582 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001583 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001584 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001585 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001586 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001587#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001588 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001589#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1590 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001591#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001592
Michal Vaskof89948c2018-01-04 09:19:46 +01001593 int i;
1594
Michal Vaskob6e37262016-02-25 14:49:00 +01001595 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001596 CRYPTO_set_locking_callback(NULL);
1597 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1598 pthread_mutex_destroy(tls_locks + i);
1599 }
1600 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001601
1602 CRYPTO_set_dynlock_create_callback(NULL);
1603 CRYPTO_set_dynlock_lock_callback(NULL);
1604 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001605#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001606}
1607
Michal Vasko8f0c0282016-02-29 10:17:14 +01001608#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001609
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001610#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001611
Michal Vasko8f0c0282016-02-29 10:17:14 +01001612static void
Michal Vasko5e228792016-02-03 15:30:24 +01001613nc_ssh_tls_init(void)
1614{
Rosen Penev4f552d62019-06-26 16:10:43 -07001615#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001616 SSL_load_error_strings();
1617 ERR_load_BIO_strings();
1618 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001619#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001620
1621 nc_ssh_init();
1622
Michal Vaskoe1e82632019-09-09 09:18:03 +02001623#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001624 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1625 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1626 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001627#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001628}
1629
Michal Vasko8f0c0282016-02-29 10:17:14 +01001630static void
Michal Vasko5e228792016-02-03 15:30:24 +01001631nc_ssh_tls_destroy(void)
1632{
Rosen Penev4f552d62019-06-26 16:10:43 -07001633#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001634 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001635# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001636 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001637# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001638 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001639# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001640#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001641
1642 nc_ssh_destroy();
1643
Michal Vaskoe1e82632019-09-09 09:18:03 +02001644#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001645 CRYPTO_set_dynlock_create_callback(NULL);
1646 CRYPTO_set_dynlock_lock_callback(NULL);
1647 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001648#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001649}
1650
Radek Krejci53691be2016-02-22 13:58:37 +01001651#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001652
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001653#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001654
1655API void
1656nc_thread_destroy(void)
1657{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001658 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001659 // CRYPTO_cleanup_all_ex_data();
Michal Vasko8f0c0282016-02-29 10:17:14 +01001660
Michal Vasko770b4362017-02-17 14:44:18 +01001661#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1662 CRYPTO_THREADID crypto_tid;
1663
Michal Vasko8f0c0282016-02-29 10:17:14 +01001664 CRYPTO_THREADID_current(&crypto_tid);
1665 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001666#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001667}
1668
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001669#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1670
1671void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001672nc_init(void)
1673{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001674#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001675 nc_ssh_tls_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001676#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001677 nc_ssh_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001678#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001679 nc_tls_init();
1680#endif
1681}
1682
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001683void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001684nc_destroy(void)
1685{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001686#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001687 nc_ssh_tls_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001688#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001689 nc_ssh_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001690#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001691 nc_tls_destroy();
1692#endif
1693}