blob: a3c7fb0238668136605637da3aaff6d7c4766eca [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
Michal Vaskod8a74192023-02-06 15:51:50 +01007 * Copyright (c) 2015 - 2023 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 Vaskob83a3fa2021-05-26 09:53:42 +020021#include <pthread.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/socket.h>
25#include <sys/time.h>
26#include <sys/types.h>
Michal Vasko58f31552016-01-19 12:39:15 +010027#include <time.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020028#include <unistd.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020029
Michal Vasko9e8ac262020-04-07 13:06:45 +020030#include "compat.h"
roman3f9b65c2023-06-05 14:26:58 +020031#include "config.h"
32#include "log_p.h"
33#include "netconf.h"
34#include "session_p.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010035
Radek Krejci53691be2016-02-22 13:58:37 +010036#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020037
Michal Vasko086311b2016-01-08 09:53:11 +010038# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020039
Radek Krejci53691be2016-02-22 13:58:37 +010040#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020041
Michal Vaskob83a3fa2021-05-26 09:53:42 +020042#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010043
Michal Vasko5e228792016-02-03 15:30:24 +010044# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010045# include <openssl/err.h>
46
Radek Krejci53691be2016-02-22 13:58:37 +010047#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010048
Michal Vasko086311b2016-01-08 09:53:11 +010049/* in seconds */
50#define NC_CLIENT_HELLO_TIMEOUT 60
fanchanghu75888b62017-08-01 19:51:20 +080051#define NC_SERVER_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020052
Michal Vasko05ba9df2016-01-13 14:40:27 +010053/* in milliseconds */
54#define NC_CLOSE_REPLY_TIMEOUT 200
55
Michal Vasko086311b2016-01-08 09:53:11 +010056extern struct nc_server_opts server_opts;
57
Michal Vaskod8a74192023-02-06 15:51:50 +010058void
59nc_timeouttime_get(struct timespec *ts, uint32_t add_ms)
Radek Krejci7ac16052016-07-15 11:48:18 +020060{
Michal Vaskod8a74192023-02-06 15:51:50 +010061 if (clock_gettime(COMPAT_CLOCK_ID, ts) == -1) {
62 ERR(NULL, "clock_gettime() failed (%s).", strerror(errno));
63 return;
64 }
65
66 if (!add_ms) {
67 return;
68 }
69
70 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
71
72 ts->tv_sec += add_ms / 1000;
73 ts->tv_nsec += (add_ms % 1000) * 1000000L;
74
75 if (ts->tv_nsec >= 1000000000L) {
76 ++ts->tv_sec;
77 ts->tv_nsec -= 1000000000L;
78 } else if (ts->tv_nsec < 0) {
79 --ts->tv_sec;
80 ts->tv_nsec += 1000000000L;
81 }
82
83 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
84}
85
86int32_t
87nc_timeouttime_cur_diff(const struct timespec *ts)
88{
89 struct timespec cur;
90 int64_t nsec_diff = 0;
91
92 nc_timeouttime_get(&cur, 0);
93
94 nsec_diff += (((int64_t)ts->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
95 nsec_diff += ((int64_t)ts->tv_nsec) - ((int64_t)cur.tv_nsec);
96
97 return nsec_diff / 1000000L;
98}
99
100void
101nc_realtime_get(struct timespec *ts)
102{
roman6ece9c52022-06-22 09:29:17 +0200103 if (clock_gettime(CLOCK_REALTIME, ts)) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100104 ERR(NULL, "clock_gettime() failed (%s).", strerror(errno));
105 return;
roman6ece9c52022-06-22 09:29:17 +0200106 }
Michal Vasko36c7be82017-02-22 13:37:59 +0100107}
108
Michal Vaskoddce1212019-05-24 09:58:49 +0200109const char *
roman3f9b65c2023-06-05 14:26:58 +0200110nc_privkey_format_to_str(NC_PRIVKEY_FORMAT format)
Michal Vaskoddce1212019-05-24 09:58:49 +0200111{
roman3f9b65c2023-06-05 14:26:58 +0200112 switch (format) {
113 case NC_PRIVKEY_FORMAT_RSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200114 return "RSA";
roman3f9b65c2023-06-05 14:26:58 +0200115 case NC_PRIVKEY_FORMAT_EC:
Michal Vaskoddce1212019-05-24 09:58:49 +0200116 return "EC";
roman3f9b65c2023-06-05 14:26:58 +0200117 case NC_PRIVKEY_FORMAT_X509:
roman44600f42023-04-28 15:54:27 +0200118 return NULL;
roman3f9b65c2023-06-05 14:26:58 +0200119 case NC_PRIVKEY_FORMAT_OPENSSH:
120 return "OPENSSH";
Michal Vaskoddce1212019-05-24 09:58:49 +0200121 default:
roman3f9b65c2023-06-05 14:26:58 +0200122 return NULL;
Michal Vaskoddce1212019-05-24 09:58:49 +0200123 }
Michal Vaskoddce1212019-05-24 09:58:49 +0200124}
125
Michal Vaskobe52dc22018-10-17 09:28:17 +0200126int
romanc1d2b092023-02-02 08:58:27 +0100127nc_sock_configure_keepalive(int sock, struct nc_keepalives *ka)
Michal Vaskobe52dc22018-10-17 09:28:17 +0200128{
129 int opt;
130
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200131 opt = ka->enabled;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200132 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200133 ERR(NULL, "Could not set SO_KEEPALIVE option (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200134 return -1;
135 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200136 if (!ka->enabled) {
137 return 0;
138 }
Michal Vaskobe52dc22018-10-17 09:28:17 +0200139
140#ifdef TCP_KEEPIDLE
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200141 opt = ka->idle_time;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200142 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200143 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200144 return -1;
145 }
146#endif
147
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200148#ifdef TCP_KEEPCNT
149 opt = ka->max_probes;
150 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200151 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200152 return -1;
153 }
154#endif
155
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200156#ifdef TCP_KEEPINTVL
157 opt = ka->probe_interval;
158 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200159 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200160 return -1;
161 }
162#endif
163
164 return 0;
165}
166
Michal Vaskoade892d2017-02-22 13:40:35 +0100167struct nc_session *
Michal Vasko131120a2018-05-29 15:44:02 +0200168nc_new_session(NC_SIDE side, int shared_ti)
Michal Vaskoade892d2017-02-22 13:40:35 +0100169{
170 struct nc_session *sess;
171
172 sess = calloc(1, sizeof *sess);
173 if (!sess) {
174 return NULL;
175 }
176
Michal Vasko131120a2018-05-29 15:44:02 +0200177 sess->side = side;
178
179 if (side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200180 pthread_mutex_init(&sess->opts.server.ntf_status_lock, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100181 pthread_mutex_init(&sess->opts.server.rpc_lock, NULL);
182 pthread_cond_init(&sess->opts.server.rpc_cond, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100183
184 pthread_mutex_init(&sess->opts.server.ch_lock, NULL);
185 pthread_cond_init(&sess->opts.server.ch_cond, NULL);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200186 } else {
187 pthread_mutex_init(&sess->opts.client.msgs_lock, NULL);
Michal Vasko131120a2018-05-29 15:44:02 +0200188 }
189
190 if (!shared_ti) {
191 sess->io_lock = malloc(sizeof *sess->io_lock);
192 if (!sess->io_lock) {
193 goto error;
194 }
195 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100196 }
197
198 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200199
200error:
Michal Vasko131120a2018-05-29 15:44:02 +0200201 free(sess);
202 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100203}
204
Michal Vasko96164bf2016-01-21 15:41:58 +0100205/*
206 * @return 1 - success
207 * 0 - timeout
208 * -1 - error
209 */
210int
Michal Vasko131120a2018-05-29 15:44:02 +0200211nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100212{
213 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100214 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100215
Michal Vasko131120a2018-05-29 15:44:02 +0200216 if (session->side != NC_SERVER) {
217 ERRINT;
218 return -1;
219 }
220
Michal Vasko96164bf2016-01-21 15:41:58 +0100221 if (timeout > 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100222 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100223
Michal Vaskoade892d2017-02-22 13:40:35 +0100224 /* LOCK */
Michal Vaskod8a74192023-02-06 15:51:50 +0100225 ret = pthread_mutex_clocklock(&session->opts.server.rpc_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100226 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100227 while (session->opts.server.rpc_inuse) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100228 ret = pthread_cond_clockwait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock,
229 COMPAT_CLOCK_ID, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100230 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100231 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100232 break;
233 }
234 }
235 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100236 } else if (!timeout) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100237 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100238 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100239 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100240 if (session->opts.server.rpc_inuse) {
241 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100242 return 0;
243 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200244 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100245 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100246 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100247 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100248 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100249 while (session->opts.server.rpc_inuse) {
250 ret = pthread_cond_wait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100251 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100252 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100253 break;
254 }
255 }
256 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100257 }
258
Michal Vaskoade892d2017-02-22 13:40:35 +0100259 if (ret) {
260 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
261 /* timeout */
262 return 0;
263 }
264
Michal Vasko96164bf2016-01-21 15:41:58 +0100265 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200266 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100267 return -1;
268 }
269
270 /* ok */
Michal Vaskoacf98472021-02-04 15:33:57 +0100271 assert(session->opts.server.rpc_inuse == 0);
272 session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100273
274 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100275 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100276 if (ret) {
277 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200278 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100279 return -1;
280 }
281
282 return 1;
283}
284
285int
Michal Vasko131120a2018-05-29 15:44:02 +0200286nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100287{
288 int ret;
289 struct timespec ts_timeout;
290
Michal Vasko131120a2018-05-29 15:44:02 +0200291 if (session->side != NC_SERVER) {
292 ERRINT;
293 return -1;
294 }
295
Michal Vaskoacf98472021-02-04 15:33:57 +0100296 assert(session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100297
298 if (timeout > 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100299 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100300
301 /* LOCK */
Michal Vaskod8a74192023-02-06 15:51:50 +0100302 ret = pthread_mutex_clocklock(&session->opts.server.rpc_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100303 } else if (!timeout) {
304 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100305 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100306 } else { /* timeout == -1 */
307 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100308 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100309 }
310
311 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
312 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200313 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100314 return -1;
315 } else if (ret) {
Michal Vasko69e98752022-12-14 14:20:17 +0100316 WRN(session, "%s: session RPC lock timeout, should not happen.", func);
Michal Vaskoade892d2017-02-22 13:40:35 +0100317 }
318
Michal Vaskoacf98472021-02-04 15:33:57 +0100319 session->opts.server.rpc_inuse = 0;
320 pthread_cond_signal(&session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100321
322 if (!ret) {
323 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100324 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100325 if (ret) {
326 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200327 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100328 return -1;
329 }
330 }
331
Michal Vasko96164bf2016-01-21 15:41:58 +0100332 return 1;
333}
334
Michal Vasko131120a2018-05-29 15:44:02 +0200335int
336nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
337{
338 int ret;
339 struct timespec ts_timeout;
340
341 if (timeout > 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100342 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko131120a2018-05-29 15:44:02 +0200343
Michal Vaskod8a74192023-02-06 15:51:50 +0100344 ret = pthread_mutex_clocklock(session->io_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vasko131120a2018-05-29 15:44:02 +0200345 } else if (!timeout) {
346 ret = pthread_mutex_trylock(session->io_lock);
347 } else { /* timeout == -1 */
Robin Jarry54ea2962018-10-10 10:33:40 +0200348 ret = pthread_mutex_lock(session->io_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200349 }
350
351 if (ret) {
352 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
353 /* timeout */
354 return 0;
355 }
356
357 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200358 ERR(session, "%s: failed to IO lock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200359 return -1;
360 }
361
362 return 1;
363}
364
365int
366nc_session_io_unlock(struct nc_session *session, const char *func)
367{
368 int ret;
369
370 ret = pthread_mutex_unlock(session->io_lock);
371 if (ret) {
372 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200373 ERR(session, "%s: failed to IO unlock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200374 return -1;
375 }
376
377 return 1;
378}
379
Michal Vasko01130bd2021-08-26 11:47:38 +0200380int
381nc_session_client_msgs_lock(struct nc_session *session, int *timeout, const char *func)
382{
383 int ret;
384 int32_t diff_msec;
roman6ece9c52022-06-22 09:29:17 +0200385 struct timespec ts_timeout, ts_start;
Michal Vasko01130bd2021-08-26 11:47:38 +0200386
387 assert(session->side == NC_CLIENT);
388
389 if (*timeout > 0) {
390 /* get current time */
Michal Vaskod8a74192023-02-06 15:51:50 +0100391 nc_timeouttime_get(&ts_start, 0);
Michal Vasko01130bd2021-08-26 11:47:38 +0200392
Michal Vaskod8a74192023-02-06 15:51:50 +0100393 nc_timeouttime_get(&ts_timeout, *timeout);
Michal Vasko01130bd2021-08-26 11:47:38 +0200394
Michal Vaskod8a74192023-02-06 15:51:50 +0100395 ret = pthread_mutex_clocklock(&session->opts.client.msgs_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vasko01130bd2021-08-26 11:47:38 +0200396 if (!ret) {
397 /* update timeout based on what was elapsed */
Michal Vaskod8a74192023-02-06 15:51:50 +0100398 diff_msec = nc_timeouttime_cur_diff(&ts_start);
Michal Vasko01130bd2021-08-26 11:47:38 +0200399 *timeout -= diff_msec;
400 }
401 } else if (!*timeout) {
402 ret = pthread_mutex_trylock(&session->opts.client.msgs_lock);
403 } else { /* timeout == -1 */
404 ret = pthread_mutex_lock(&session->opts.client.msgs_lock);
405 }
406
407 if (ret) {
408 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
409 /* timeout */
410 return 0;
411 }
412
413 /* error */
414 ERR(session, "%s: failed to MSGS lock a session (%s).", func, strerror(ret));
415 return -1;
416 }
417
418 return 1;
419}
420
421int
422nc_session_client_msgs_unlock(struct nc_session *session, const char *func)
423{
424 int ret;
425
426 assert(session->side == NC_CLIENT);
427
428 ret = pthread_mutex_unlock(&session->opts.client.msgs_lock);
429 if (ret) {
430 /* error */
431 ERR(session, "%s: failed to MSGS unlock a session (%s).", func, strerror(ret));
432 return -1;
433 }
434
435 return 1;
436}
437
Michal Vasko8dadf782016-01-15 10:29:36 +0100438API NC_STATUS
439nc_session_get_status(const struct nc_session *session)
440{
roman40672412023-05-04 11:10:22 +0200441 NC_CHECK_ARG_RET(session, session, NC_STATUS_ERR);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100442
Michal Vasko8dadf782016-01-15 10:29:36 +0100443 return session->status;
444}
445
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100446API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200447nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100448{
roman40672412023-05-04 11:10:22 +0200449 NC_CHECK_ARG_RET(session, session, NC_SESSION_TERM_ERR);
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100450
451 return session->term_reason;
452}
453
Michal Vasko8dadf782016-01-15 10:29:36 +0100454API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200455nc_session_get_killed_by(const struct nc_session *session)
456{
roman40672412023-05-04 11:10:22 +0200457 NC_CHECK_ARG_RET(session, session, 0);
Michal Vasko142cfea2017-08-07 10:12:11 +0200458
459 return session->killed_by;
460}
461
462API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100463nc_session_get_id(const struct nc_session *session)
464{
roman40672412023-05-04 11:10:22 +0200465 NC_CHECK_ARG_RET(session, session, 0);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100466
Michal Vasko8dadf782016-01-15 10:29:36 +0100467 return session->id;
468}
469
Michal Vasko174fe8e2016-02-17 15:38:09 +0100470API int
471nc_session_get_version(const struct nc_session *session)
472{
roman40672412023-05-04 11:10:22 +0200473 NC_CHECK_ARG_RET(session, session, -1);
Michal Vasko174fe8e2016-02-17 15:38:09 +0100474
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200475 return session->version == NC_VERSION_10 ? 0 : 1;
Michal Vasko174fe8e2016-02-17 15:38:09 +0100476}
477
Michal Vasko8dadf782016-01-15 10:29:36 +0100478API NC_TRANSPORT_IMPL
479nc_session_get_ti(const struct nc_session *session)
480{
roman40672412023-05-04 11:10:22 +0200481 NC_CHECK_ARG_RET(session, session, 0);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100482
Michal Vasko8dadf782016-01-15 10:29:36 +0100483 return session->ti_type;
484}
485
486API const char *
487nc_session_get_username(const struct nc_session *session)
488{
roman40672412023-05-04 11:10:22 +0200489 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100490
Michal Vasko8dadf782016-01-15 10:29:36 +0100491 return session->username;
492}
493
494API const char *
495nc_session_get_host(const struct nc_session *session)
496{
roman40672412023-05-04 11:10:22 +0200497 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100498
Michal Vasko8dadf782016-01-15 10:29:36 +0100499 return session->host;
500}
501
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200502API const char *
503nc_session_get_path(const struct nc_session *session)
504{
roman40672412023-05-04 11:10:22 +0200505 NC_CHECK_ARG_RET(session, session, NULL);
506
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200507 if (session->ti_type != NC_TI_UNIX) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200508 return NULL;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200509 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200510
511 return session->path;
512}
513
Michal Vasko8dadf782016-01-15 10:29:36 +0100514API uint16_t
515nc_session_get_port(const struct nc_session *session)
516{
roman40672412023-05-04 11:10:22 +0200517 NC_CHECK_ARG_RET(session, session, 0);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100518
Michal Vasko8dadf782016-01-15 10:29:36 +0100519 return session->port;
520}
521
Michal Vasko93224072021-11-09 12:14:28 +0100522API const struct ly_ctx *
Michal Vasko9a25e932016-02-01 10:36:42 +0100523nc_session_get_ctx(const struct nc_session *session)
524{
roman40672412023-05-04 11:10:22 +0200525 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vasko9a25e932016-02-01 10:36:42 +0100526
527 return session->ctx;
528}
529
Michal Vasko2cc4c682016-03-01 09:16:48 +0100530API void
531nc_session_set_data(struct nc_session *session, void *data)
532{
533 if (!session) {
roman40672412023-05-04 11:10:22 +0200534 ERRARG(NULL, "session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100535 return;
536 }
537
538 session->data = data;
539}
540
541API void *
542nc_session_get_data(const struct nc_session *session)
543{
roman40672412023-05-04 11:10:22 +0200544 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vasko2cc4c682016-03-01 09:16:48 +0100545
546 return session->data;
547}
548
Michal Vaskodc96bb92023-03-28 08:52:48 +0200549API int
550nc_session_is_callhome(const struct nc_session *session)
551{
roman40672412023-05-04 11:10:22 +0200552 NC_CHECK_ARG_RET(session, session, 0);
Michal Vaskodc96bb92023-03-28 08:52:48 +0200553
554 if (session->flags & NC_SESSION_CALLHOME) {
555 return 1;
556 }
557
558 return 0;
559}
560
Michal Vasko086311b2016-01-08 09:53:11 +0100561NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200562nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200563{
Michal Vasko7e1f5fb2021-11-10 10:14:45 +0100564 if (session->ctx != LYD_CTX(op)) {
565 ERR(session, "RPC \"%s\" was created in different context than that of the session.", LYD_NAME(op));
Michal Vasko086311b2016-01-08 09:53:11 +0100566 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100567 }
568
Michal Vasko131120a2018-05-29 15:44:02 +0200569 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100570}
571
Michal Vaskod4da3632022-05-25 11:49:10 +0200572/**
573 * @brief Send \<close-session\> and read the reply on a session.
574 *
575 * @param[in] session Closing NETCONF session.
576 */
577static void
578nc_session_free_close_session(struct nc_session *session)
579{
580 struct ly_in *msg;
581 struct lyd_node *close_rpc, *envp;
582 const struct lys_module *ietfnc;
583
584 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
585 if (!ietfnc) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200586 WRN(session, "Missing ietf-netconf module in context, unable to send <close-session>.");
Michal Vaskod4da3632022-05-25 11:49:10 +0200587 return;
588 }
589 if (lyd_new_inner(NULL, ietfnc, "close-session", 0, &close_rpc)) {
590 WRN(session, "Failed to create <close-session> RPC.");
591 return;
592 }
593
594 /* send the RPC */
595 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
596
597read_msg:
598 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &msg)) {
599 case 1:
600 if (!strncmp(ly_in_memory(msg, NULL), "<notification", 13)) {
601 /* ignore */
602 ly_in_free(msg, 1);
603 goto read_msg;
604 }
605 if (lyd_parse_op(session->ctx, close_rpc, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, &envp, NULL)) {
606 WRN(session, "Failed to parse <close-session> reply.");
607 } else if (!lyd_child(envp) || strcmp(LYD_NAME(lyd_child(envp)), "ok")) {
608 WRN(session, "Reply to <close-session> was not <ok> as expected.");
609 }
610 lyd_free_tree(envp);
611 ly_in_free(msg, 1);
612 break;
613 case 0:
614 WRN(session, "Timeout for receiving a reply to <close-session> elapsed.");
615 break;
616 case -1:
617 ERR(session, "Failed to receive a reply to <close-session>.");
618 break;
619 default:
620 /* cannot happen */
621 break;
622 }
623 lyd_free_tree(close_rpc);
624}
625
Michal Vasko33476c32022-09-09 11:21:40 +0200626/**
627 * @brief Free transport implementation members of a session.
628 *
629 * @param[in] session Session to free.
630 * @param[out] multisession Whether there are other NC sessions on the same SSH sessions.
631 */
632static void
633nc_session_free_transport(struct nc_session *session, int *multisession)
634{
635 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskoe44f2702022-12-12 07:58:06 +0100636 int sock = -1;
Michal Vasko33476c32022-09-09 11:21:40 +0200637 struct nc_session *siter;
638
639 *multisession = 0;
640 connected = nc_session_is_connected(session);
641
642 /* transport implementation cleanup */
643 switch (session->ti_type) {
644 case NC_TI_FD:
645 /* nothing needed - file descriptors were provided by caller,
646 * so it is up to the caller to close them correctly
647 * TODO use callbacks
648 */
649 /* just to avoid compiler warning */
650 (void)connected;
651 (void)siter;
652 break;
653
654 case NC_TI_UNIX:
655 sock = session->ti.unixsock.sock;
656 (void)connected;
657 (void)siter;
658 break;
659
660#ifdef NC_ENABLED_SSH
Michal Vaskoe44f2702022-12-12 07:58:06 +0100661 case NC_TI_LIBSSH: {
662 int r;
663
Michal Vasko33476c32022-09-09 11:21:40 +0200664 if (connected) {
Michal Vasko64734402022-09-09 11:22:00 +0200665 ssh_channel_send_eof(session->ti.libssh.channel);
Michal Vasko33476c32022-09-09 11:21:40 +0200666 ssh_channel_free(session->ti.libssh.channel);
667 }
668 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
669 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
670 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
671 */
672 /* SESSION IO LOCK */
673 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
674
675 if (session->ti.libssh.next) {
676 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
677 if (siter->status != NC_STATUS_STARTING) {
678 *multisession = 1;
679 break;
680 }
681 }
682 }
683
684 if (!*multisession) {
685 /* it's not multisession yet, but we still need to free the starting sessions */
686 if (session->ti.libssh.next) {
687 do {
688 siter = session->ti.libssh.next;
689 session->ti.libssh.next = siter->ti.libssh.next;
690
691 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
692 free(siter->username);
693 free(siter->host);
694 if (!(siter->flags & NC_SESSION_SHAREDCTX)) {
695 ly_ctx_destroy((struct ly_ctx *)siter->ctx);
696 }
697
698 free(siter);
699 } while (session->ti.libssh.next != session);
700 }
701 /* remember sock so we can close it */
702 sock = ssh_get_fd(session->ti.libssh.session);
703 if (connected) {
704 ssh_disconnect(session->ti.libssh.session);
705 sock = -1;
706 }
707 ssh_free(session->ti.libssh.session);
708 } else {
709 /* remove the session from the list */
710 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {}
711 if (session->ti.libssh.next == siter) {
712 /* there will be only one session */
713 siter->ti.libssh.next = NULL;
714 } else {
715 /* there are still multiple sessions, keep the ring list */
716 siter->ti.libssh.next = session->ti.libssh.next;
717 }
Michal Vasko33476c32022-09-09 11:21:40 +0200718 }
719
720 /* SESSION IO UNLOCK */
721 if (r == 1) {
722 nc_session_io_unlock(session, __func__);
723 }
724 break;
Michal Vaskoe44f2702022-12-12 07:58:06 +0100725 }
Michal Vasko33476c32022-09-09 11:21:40 +0200726#endif
727
728#ifdef NC_ENABLED_TLS
729 case NC_TI_OPENSSL:
730 /* remember sock so we can close it */
731 sock = SSL_get_fd(session->ti.tls);
732
733 if (connected) {
734 SSL_shutdown(session->ti.tls);
735 }
736 SSL_free(session->ti.tls);
737
738 if (session->side == NC_SERVER) {
739 X509_free(session->opts.server.client_cert);
740 }
741 break;
742#endif
743 case NC_TI_NONE:
744 break;
745 }
746
747 /* close socket separately */
748 if (sock > -1) {
749 close(sock);
750 }
751}
752
Radek Krejci695d4fa2015-10-22 13:23:54 +0200753API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100754nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200755{
Michal Vasko33476c32022-09-09 11:21:40 +0200756 int r, i, rpc_locked = 0, msgs_locked = 0, timeout;
Michal Vaskob48aa812016-01-18 14:13:09 +0100757 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoad611702015-12-03 13:41:51 +0100758 struct nc_msg_cont *contiter;
Michal Vasko77367452021-02-16 16:32:18 +0100759 struct ly_in *msg;
roman6ece9c52022-06-22 09:29:17 +0200760 struct timespec ts;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200761 void *p;
762
Michal Vasko428087d2016-01-14 16:04:28 +0100763 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200764 return;
765 }
766
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200767 /* stop notification threads if any */
768 if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running)) {
769 /* let the threads know they should quit */
770 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 0);
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200771
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200772 /* wait for them */
Michal Vaskod8a74192023-02-06 15:51:50 +0100773 nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200774 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_count)) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200775 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +0100776 if (nc_timeouttime_cur_diff(&ts) < 1) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200777 ERR(session, "Waiting for notification thread exit failed (timed out).");
778 break;
779 }
780 }
Michal Vasko86d357c2016-03-11 13:46:38 +0100781 }
782
Michal Vaskoacf98472021-02-04 15:33:57 +0100783 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200784 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100785 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100786 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200787 } else if (r) {
788 rpc_locked = 1;
Michal Vasko96a28a32021-02-04 15:35:20 +0100789 } else {
790 /* else failed to lock it, too bad */
Michal Vasko05532772021-06-03 12:12:38 +0200791 ERR(session, "Freeing a session while an RPC is being processed.");
Michal Vasko96a28a32021-02-04 15:35:20 +0100792 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200793 }
794
Michal Vasko8c247822020-09-07 13:23:23 +0200795 if (session->side == NC_CLIENT) {
Michal Vasko01130bd2021-08-26 11:47:38 +0200796 timeout = NC_SESSION_FREE_LOCK_TIMEOUT;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200797
Michal Vasko01130bd2021-08-26 11:47:38 +0200798 /* MSGS LOCK */
799 r = nc_session_client_msgs_lock(session, &timeout, __func__);
800 if (r == -1) {
801 return;
802 } else if (r) {
803 msgs_locked = 1;
804 } else {
805 /* else failed to lock it, too bad */
806 ERR(session, "Freeing a session while messages are being received.");
807 }
808
809 /* cleanup message queue */
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200810 for (contiter = session->opts.client.msgs; contiter; ) {
Michal Vasko77367452021-02-16 16:32:18 +0100811 ly_in_free(contiter->msg, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200812
Michal Vaskoad611702015-12-03 13:41:51 +0100813 p = contiter;
814 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200815 free(p);
816 }
817
Michal Vasko01130bd2021-08-26 11:47:38 +0200818 if (msgs_locked) {
819 /* MSGS UNLOCK */
820 nc_session_client_msgs_unlock(session, __func__);
821 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200822
Michal Vasko8c247822020-09-07 13:23:23 +0200823 if (session->status == NC_STATUS_RUNNING) {
Michal Vasko9c6d38c2021-09-03 13:02:53 +0200824 /* receive any leftover messages */
825 while (nc_read_msg_poll_io(session, 0, &msg) == 1) {
826 ly_in_free(msg, 1);
827 }
828
Michal Vasko8c247822020-09-07 13:23:23 +0200829 /* send closing info to the other side */
Michal Vaskod4da3632022-05-25 11:49:10 +0200830 nc_session_free_close_session(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200831 }
832
833 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200834 if (session->opts.client.cpblts) {
835 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200836 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200837 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200838 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200839 }
Michal Vasko78939072022-12-12 07:43:18 +0100840
841 /* LY ext data */
Michal Vaskoe44f2702022-12-12 07:58:06 +0100842#ifdef NC_ENABLED_SSH
843 struct nc_session *siter;
844
Michal Vasko78939072022-12-12 07:43:18 +0100845 if ((session->flags & NC_SESSION_SHAREDCTX) && session->ti.libssh.next) {
846 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
847 if (siter->status != NC_STATUS_STARTING) {
848 /* move LY ext data to this session */
849 assert(!siter->opts.client.ext_data);
850 siter->opts.client.ext_data = session->opts.client.ext_data;
851 session->opts.client.ext_data = NULL;
852 break;
853 }
854 }
Michal Vaskoe44f2702022-12-12 07:58:06 +0100855 } else
856#endif
857 {
Michal Vasko78939072022-12-12 07:43:18 +0100858 lyd_free_siblings(session->opts.client.ext_data);
859 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200860 }
861
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100862 if (session->data && data_free) {
863 data_free(session->data);
864 }
865
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200866 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
867 /* CH LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100868 pthread_mutex_lock(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200869 }
870
Michal Vasko86d357c2016-03-11 13:46:38 +0100871 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200872 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200873
Michal Vaskofeccb312022-03-24 15:24:59 +0100874 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100875 pthread_cond_signal(&session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200876
Michal Vaskod8a74192023-02-06 15:51:50 +0100877 nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vasko0db3db52021-03-03 10:45:42 +0100878
879 /* wait for CH thread to actually wake up and terminate */
880 r = 0;
Michal Vaskofeccb312022-03-24 15:24:59 +0100881 while (!r && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100882 r = pthread_cond_clockwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, COMPAT_CLOCK_ID, &ts);
Michal Vasko0db3db52021-03-03 10:45:42 +0100883 }
Michal Vasko0db3db52021-03-03 10:45:42 +0100884 if (r) {
Michal Vasko05532772021-06-03 12:12:38 +0200885 ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r));
Michal Vasko3f05a092018-03-13 10:39:49 +0100886 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200887 }
888
Michal Vaskofeccb312022-03-24 15:24:59 +0100889 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
890 /* CH UNLOCK */
891 pthread_mutex_unlock(&session->opts.server.ch_lock);
892 }
893
Radek Krejci695d4fa2015-10-22 13:23:54 +0200894 /* transport implementation cleanup */
Michal Vasko33476c32022-09-09 11:21:40 +0200895 nc_session_free_transport(session, &multisession);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200896
Michal Vasko33476c32022-09-09 11:21:40 +0200897 /* final cleanup */
Michal Vasko93224072021-11-09 12:14:28 +0100898 free(session->username);
899 free(session->host);
900 free(session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200901
Michal Vaskoacf98472021-02-04 15:33:57 +0100902 if (session->side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200903 pthread_mutex_destroy(&session->opts.server.ntf_status_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200904 if (rpc_locked) {
905 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100906 }
Michal Vaskoacf98472021-02-04 15:33:57 +0100907 pthread_mutex_destroy(&session->opts.server.rpc_lock);
908 pthread_cond_destroy(&session->opts.server.rpc_cond);
Michal Vasko131120a2018-05-29 15:44:02 +0200909 }
910
911 if (session->io_lock && !multisession) {
912 pthread_mutex_destroy(session->io_lock);
913 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200914 }
915
916 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko93224072021-11-09 12:14:28 +0100917 ly_ctx_destroy((struct ly_ctx *)session->ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200918 }
919
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200920 if (session->side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100921 /* free CH synchronization structures */
922 pthread_cond_destroy(&session->opts.server.ch_cond);
923 pthread_mutex_destroy(&session->opts.server.ch_lock);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200924 } else {
925 pthread_mutex_destroy(&session->opts.client.msgs_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200926 }
927
Radek Krejci695d4fa2015-10-22 13:23:54 +0200928 free(session);
929}
930
Michal Vasko086311b2016-01-08 09:53:11 +0100931static void
Michal Vasko93224072021-11-09 12:14:28 +0100932add_cpblt(const char *capab, char ***cpblts, int *size, int *count)
Michal Vasko086311b2016-01-08 09:53:11 +0100933{
Radek Krejci658782b2016-12-04 22:04:55 +0100934 size_t len;
935 int i;
936 char *p;
937
938 if (capab) {
939 /* check if already present */
940 p = strchr(capab, '?');
941 if (p) {
942 len = p - capab;
943 } else {
944 len = strlen(capab);
945 }
946 for (i = 0; i < *count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200947 if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) {
Radek Krejci658782b2016-12-04 22:04:55 +0100948 /* already present, do not duplicate it */
949 return;
950 }
951 }
952 }
953
954 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100955 if (*count == *size) {
956 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100957 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
958 if (!(*cpblts)) {
959 ERRMEM;
960 return;
961 }
Michal Vasko086311b2016-01-08 09:53:11 +0100962 }
963
Michal Vasko93224072021-11-09 12:14:28 +0100964 (*cpblts)[*count] = capab ? strdup(capab) : NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100965 ++(*count);
966}
967
Michal Vasko93224072021-11-09 12:14:28 +0100968API char **
969nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +0100970{
Michal Vasko93224072021-11-09 12:14:28 +0100971 char **cpblts;
Michal Vasko77367452021-02-16 16:32:18 +0100972 const struct lys_module *mod;
973 struct lysp_feature *feat;
974 int size = 10, count, features_count = 0, dev_count = 0, str_len, len;
Michal Vasko1440a742021-03-31 11:11:03 +0200975 uint32_t i, u;
Michal Vasko77367452021-02-16 16:32:18 +0100976 LY_ARRAY_COUNT_TYPE v;
Michal Vasko1440a742021-03-31 11:11:03 +0200977 char *yl_content_id;
romanc1d2b092023-02-02 08:58:27 +0100978 uint32_t wd_also_supported;
979 uint32_t wd_basic_mode;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200980
Radek Krejci24a18412018-05-16 15:09:10 +0200981#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +0200982 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100983
roman40672412023-05-04 11:10:22 +0200984 NC_CHECK_ARG_RET(NULL, ctx, NULL);
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200985
Michal Vasko086311b2016-01-08 09:53:11 +0100986 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100987 if (!cpblts) {
988 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200989 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100990 }
Michal Vasko93224072021-11-09 12:14:28 +0100991 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
992 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko086311b2016-01-08 09:53:11 +0100993 count = 2;
994
995 /* capabilities */
996
Michal Vasko77367452021-02-16 16:32:18 +0100997 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf");
Michal Vasko086311b2016-01-08 09:53:11 +0100998 if (mod) {
Michal Vasko77367452021-02-16 16:32:18 +0100999 if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001000 add_cpblt("urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001001 }
Michal Vasko77367452021-02-16 16:32:18 +01001002 if (lys_feature_value(mod, "candidate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001003 add_cpblt("urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko77367452021-02-16 16:32:18 +01001004 if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001005 add_cpblt("urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001006 }
1007 }
Michal Vasko77367452021-02-16 16:32:18 +01001008 if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001009 add_cpblt("urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001010 }
Michal Vasko77367452021-02-16 16:32:18 +01001011 if (lys_feature_value(mod, "validate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001012 add_cpblt("urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001013 }
Michal Vasko77367452021-02-16 16:32:18 +01001014 if (lys_feature_value(mod, "startup") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001015 add_cpblt("urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001016 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001017
1018 /* The URL capability must be set manually using nc_server_set_capability()
1019 * because of the need for supported protocols to be included.
1020 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1021 */
Michal Vasko77367452021-02-16 16:32:18 +01001022 // if (lys_feature_value(mod, "url") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001023 // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
mekleoa8de5e92020-02-13 09:05:56 +01001024 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001025
Michal Vasko77367452021-02-16 16:32:18 +01001026 if (lys_feature_value(mod, "xpath") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001027 add_cpblt("urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001028 }
1029 }
1030
Michal Vasko77367452021-02-16 16:32:18 +01001031 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01001032 if (mod) {
romanc1d2b092023-02-02 08:58:27 +01001033 wd_basic_mode = ATOMIC_LOAD_RELAXED(server_opts.wd_basic_mode);
1034 if (!wd_basic_mode) {
Michal Vasko05532772021-06-03 12:12:38 +02001035 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 +01001036 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001037 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
romanc1d2b092023-02-02 08:58:27 +01001038 switch (wd_basic_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01001039 case NC_WD_ALL:
1040 strcat(str, "?basic-mode=report-all");
1041 break;
1042 case NC_WD_TRIM:
1043 strcat(str, "?basic-mode=trim");
1044 break;
1045 case NC_WD_EXPLICIT:
1046 strcat(str, "?basic-mode=explicit");
1047 break;
1048 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001049 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001050 break;
1051 }
1052
romanc1d2b092023-02-02 08:58:27 +01001053 wd_also_supported = ATOMIC_LOAD_RELAXED(server_opts.wd_also_supported);
1054 if (wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001055 strcat(str, "&also-supported=");
romanc1d2b092023-02-02 08:58:27 +01001056 if (wd_also_supported & NC_WD_ALL) {
Michal Vasko086311b2016-01-08 09:53:11 +01001057 strcat(str, "report-all,");
1058 }
romanc1d2b092023-02-02 08:58:27 +01001059 if (wd_also_supported & NC_WD_ALL_TAG) {
Michal Vasko086311b2016-01-08 09:53:11 +01001060 strcat(str, "report-all-tagged,");
1061 }
romanc1d2b092023-02-02 08:58:27 +01001062 if (wd_also_supported & NC_WD_TRIM) {
Michal Vasko086311b2016-01-08 09:53:11 +01001063 strcat(str, "trim,");
1064 }
romanc1d2b092023-02-02 08:58:27 +01001065 if (wd_also_supported & NC_WD_EXPLICIT) {
Michal Vasko086311b2016-01-08 09:53:11 +01001066 strcat(str, "explicit,");
1067 }
1068 str[strlen(str) - 1] = '\0';
1069
Michal Vasko93224072021-11-09 12:14:28 +01001070 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001071 }
1072 }
1073 }
1074
Radek Krejci658782b2016-12-04 22:04:55 +01001075 /* other capabilities */
1076 for (u = 0; u < server_opts.capabilities_count; u++) {
Michal Vasko93224072021-11-09 12:14:28 +01001077 add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001078 }
1079
1080 /* models */
Michal Vasko1440a742021-03-31 11:11:03 +02001081 u = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001082 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001083 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vasko77367452021-02-16 16:32:18 +01001084 if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) {
Michal Vasko05532772021-06-03 12:12:38 +02001085 ERR(NULL, "Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
Michal Vaskod5ada122020-03-19 18:28:06 +01001086 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001087 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001088
Michal Vasko1440a742021-03-31 11:11:03 +02001089 /* get content-id */
1090 if (server_opts.content_id_clb) {
1091 yl_content_id = server_opts.content_id_clb(server_opts.content_id_data);
1092 if (!yl_content_id) {
1093 ERRMEM;
1094 goto error;
1095 }
1096 } else {
1097 yl_content_id = malloc(11);
1098 if (!yl_content_id) {
1099 ERRMEM;
1100 goto error;
1101 }
1102 sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx));
1103 }
1104
Michal Vasko77367452021-02-16 16:32:18 +01001105 if (!strcmp(mod->revision, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001106 /* new one (capab defined in RFC 8526 section 2) */
Michal Vasko1440a742021-03-31 11:11:03 +02001107 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s",
1108 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001109 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001110 } else {
1111 /* old one (capab defined in RFC 7950 section 5.6.4) */
Michal Vasko1440a742021-03-31 11:11:03 +02001112 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s",
1113 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001114 add_cpblt(str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001115 }
Michal Vasko1440a742021-03-31 11:11:03 +02001116 free(yl_content_id);
Radek Krejci24a18412018-05-16 15:09:10 +02001117 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001118 } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001119 /* skip YANG 1.1 modules */
Radek Krejci24a18412018-05-16 15:09:10 +02001120 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001121 } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001122 /* skip YANG 1.0 modules */
Radek Krejci24a18412018-05-16 15:09:10 +02001123 continue;
1124 }
Michal Vasko086311b2016-01-08 09:53:11 +01001125
Michal Vasko77367452021-02-16 16:32:18 +01001126 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "",
1127 mod->revision ? mod->revision : "");
Radek Krejci24a18412018-05-16 15:09:10 +02001128
Michal Vaskodafdc742020-03-11 16:15:59 +01001129 features_count = 0;
1130 i = 0;
1131 feat = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001132 while ((feat = lysp_feature_next(feat, mod->parsed, &i))) {
Michal Vaskodafdc742020-03-11 16:15:59 +01001133 if (!(feat->flags & LYS_FENABLED)) {
1134 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001135 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001136 if (!features_count) {
1137 strcat(str, "&features=");
1138 str_len += 10;
1139 }
1140 len = strlen(feat->name);
1141 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1142 ERRINT;
1143 break;
1144 }
1145 if (features_count) {
1146 strcat(str, ",");
1147 ++str_len;
1148 }
1149 strcat(str, feat->name);
1150 str_len += len;
1151 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001152 }
Michal Vasko086311b2016-01-08 09:53:11 +01001153
Michal Vasko77367452021-02-16 16:32:18 +01001154 if (mod->deviated_by) {
Radek Krejci24a18412018-05-16 15:09:10 +02001155 strcat(str, "&deviations=");
1156 str_len += 12;
1157 dev_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +01001158 LY_ARRAY_FOR(mod->deviated_by, v) {
1159 len = strlen(mod->deviated_by[v]->name);
1160 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1161 ERRINT;
1162 break;
Radek Krejci24a18412018-05-16 15:09:10 +02001163 }
Michal Vasko77367452021-02-16 16:32:18 +01001164 if (dev_count) {
1165 strcat(str, ",");
1166 ++str_len;
Radek Krejci24a18412018-05-16 15:09:10 +02001167 }
Michal Vasko77367452021-02-16 16:32:18 +01001168 strcat(str, mod->deviated_by[v]->name);
1169 str_len += len;
1170 dev_count++;
Radek Krejci24a18412018-05-16 15:09:10 +02001171 }
1172 }
1173
Michal Vasko93224072021-11-09 12:14:28 +01001174 add_cpblt(str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +02001175 }
Michal Vasko086311b2016-01-08 09:53:11 +01001176
1177 /* ending NULL capability */
Michal Vasko93224072021-11-09 12:14:28 +01001178 add_cpblt(NULL, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001179
1180 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001181
1182error:
Radek Krejcif906e412017-09-22 14:44:45 +02001183 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001184 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001185}
1186
Michal Vasko93224072021-11-09 12:14:28 +01001187API char **
1188nc_server_get_cpblts(const struct ly_ctx *ctx)
Radek Krejci24a18412018-05-16 15:09:10 +02001189{
1190 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1191}
1192
Radek Krejci695d4fa2015-10-22 13:23:54 +02001193static int
Michal Vasko77367452021-02-16 16:32:18 +01001194parse_cpblts(struct lyd_node *capabilities, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001195{
Michal Vasko77367452021-02-16 16:32:18 +01001196 struct lyd_node *iter;
1197 struct lyd_node_opaq *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001198 int ver = -1, i = 0;
1199 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001200
1201 if (list) {
1202 /* get the storage for server's capabilities */
Michal Vasko77367452021-02-16 16:32:18 +01001203 LY_LIST_FOR(lyd_child(capabilities), iter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001204 i++;
1205 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001206 /* last item remains NULL */
1207 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001208 if (!*list) {
1209 ERRMEM;
1210 return -1;
1211 }
1212 i = 0;
1213 }
1214
Michal Vasko77367452021-02-16 16:32:18 +01001215 LY_LIST_FOR(lyd_child(capabilities), iter) {
1216 cpblt = (struct lyd_node_opaq *)iter;
1217
1218 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 +02001219 ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001220 return -1;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001221 }
1222
Michal Vasko156d3272017-04-11 11:46:49 +02001223 /* skip leading/trailing whitespaces */
Michal Vasko77367452021-02-16 16:32:18 +01001224 for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {}
1225 for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {}
1226 if (!cpb_start[0] || (cpb_end == cpblt->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001227 ERR(NULL, "Empty capability \"%s\" received.", cpblt->value);
Michal Vasko156d3272017-04-11 11:46:49 +02001228 return -1;
1229 }
1230
Radek Krejci695d4fa2015-10-22 13:23:54 +02001231 /* detect NETCONF version */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001232 if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001233 ver = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001234 } 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 +02001235 ver = 1;
1236 }
1237
1238 /* store capabilities */
1239 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001240 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1241 if (!(*list)[i]) {
1242 ERRMEM;
1243 return -1;
1244 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001245 i++;
1246 }
1247 }
1248
1249 if (ver == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001250 ERR(NULL, "Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001251 }
1252
1253 return ver;
1254}
1255
1256static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001257nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001258{
Michal Vasko131120a2018-05-29 15:44:02 +02001259 NC_MSG_TYPE ret;
1260 int i, io_timeout;
Michal Vasko93224072021-11-09 12:14:28 +01001261 char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001262 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001263
Michal Vasko131120a2018-05-29 15:44:02 +02001264 if (session->side == NC_CLIENT) {
1265 /* client side hello - send only NETCONF base capabilities */
1266 cpblts = malloc(3 * sizeof *cpblts);
1267 if (!cpblts) {
1268 ERRMEM;
1269 return NC_MSG_ERROR;
1270 }
Michal Vasko93224072021-11-09 12:14:28 +01001271 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1272 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko131120a2018-05-29 15:44:02 +02001273 cpblts[2] = NULL;
1274
1275 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1276 sid = NULL;
1277 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001278 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0);
Michal Vasko5b24b6b2020-12-04 09:29:47 +01001279 if (!cpblts) {
1280 return NC_MSG_ERROR;
1281 }
Michal Vasko131120a2018-05-29 15:44:02 +02001282
1283 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1284 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001285 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001286
Michal Vasko131120a2018-05-29 15:44:02 +02001287 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001288
Michal Vasko086311b2016-01-08 09:53:11 +01001289 for (i = 0; cpblts[i]; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001290 free(cpblts[i]);
Michal Vasko086311b2016-01-08 09:53:11 +01001291 }
1292 free(cpblts);
1293
Michal Vasko131120a2018-05-29 15:44:02 +02001294 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001295}
1296
1297static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001298nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001299{
Michal Vasko77367452021-02-16 16:32:18 +01001300 struct ly_in *msg;
1301 struct lyd_node *hello = NULL, *iter;
1302 struct lyd_node_opaq *node;
1303 int r, ver = -1, flag = 0;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001304 char *str;
Michal Vasko292c5542023-02-01 14:33:17 +01001305 long long id;
Michal Vasko77367452021-02-16 16:32:18 +01001306 NC_MSG_TYPE rc = NC_MSG_HELLO;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001307
Michal Vasko77367452021-02-16 16:32:18 +01001308 r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg);
1309 switch (r) {
1310 case 1:
Radek Krejci695d4fa2015-10-22 13:23:54 +02001311 /* parse <hello> data */
Michal Vasko77367452021-02-16 16:32:18 +01001312 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 +02001313 ERR(session, "Failed to parse server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001314 rc = NC_MSG_ERROR;
1315 goto cleanup;
1316 }
1317
1318 LY_LIST_FOR(lyd_child(hello), iter) {
1319 node = (struct lyd_node_opaq *)iter;
1320
1321 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001322 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001323 } else if (!strcmp(node->name.name, "session-id")) {
1324 if (!node->value || !strlen(node->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001325 ERR(session, "No 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 str = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001330 id = strtoll(node->value, &str, 10);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001331 if (*str || (id < 1) || (id > UINT32_MAX)) {
Michal Vasko05532772021-06-03 12:12:38 +02001332 ERR(session, "Invalid value of <session-id> element in server <hello>.");
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 session->id = (uint32_t)id;
1337 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001338 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001339 ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001340 rc = NC_MSG_ERROR;
1341 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001342 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001343
1344 if (flag) {
1345 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001346 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001347 rc = NC_MSG_ERROR;
1348 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001349 }
1350 flag = 1;
1351
Michal Vasko77367452021-02-16 16:32:18 +01001352 if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) {
1353 rc = NC_MSG_ERROR;
1354 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001355 }
1356 session->version = ver;
1357 }
1358
1359 if (!session->id) {
Michal Vasko05532772021-06-03 12:12:38 +02001360 ERR(session, "Missing <session-id> in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001361 rc = NC_MSG_ERROR;
1362 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001363 }
1364 break;
Michal Vasko77367452021-02-16 16:32:18 +01001365 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001366 ERR(session, "Server <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001367 rc = NC_MSG_WOULDBLOCK;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001368 break;
1369 default:
Michal Vasko77367452021-02-16 16:32:18 +01001370 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001371 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001372 }
1373
Michal Vasko77367452021-02-16 16:32:18 +01001374cleanup:
1375 ly_in_free(msg, 1);
1376 lyd_free_tree(hello);
1377 return rc;
Radek Krejci5686ff72015-10-09 13:33:56 +02001378}
1379
Michal Vasko11d142a2016-01-19 15:58:24 +01001380static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001381nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001382{
Michal Vasko77367452021-02-16 16:32:18 +01001383 struct ly_in *msg;
1384 struct lyd_node *hello = NULL, *iter;
1385 struct lyd_node_opaq *node;
1386 NC_MSG_TYPE rc = NC_MSG_HELLO;
1387 int r, ver = -1, flag = 0, timeout_io;
Michal Vasko11d142a2016-01-19 15:58:24 +01001388
Michal Vasko77367452021-02-16 16:32:18 +01001389 timeout_io = server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000;
1390 r = nc_read_msg_poll_io(session, timeout_io, &msg);
1391 switch (r) {
1392 case 1:
1393 /* parse <hello> data */
1394 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 +02001395 ERR(session, "Failed to parse client <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001396 rc = NC_MSG_ERROR;
1397 goto cleanup;
1398 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001399
Michal Vasko77367452021-02-16 16:32:18 +01001400 /* learn NETCONF version */
1401 LY_LIST_FOR(lyd_child(hello), iter) {
1402 node = (struct lyd_node_opaq *)iter;
1403
1404 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001405 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001406 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001407 ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name);
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
1412 if (flag) {
1413 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001414 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001415 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001416 goto cleanup;
1417 }
1418 flag = 1;
1419
Michal Vasko77367452021-02-16 16:32:18 +01001420 if ((ver = parse_cpblts(&node->node, NULL)) < 0) {
1421 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001422 goto cleanup;
1423 }
1424 session->version = ver;
1425 }
1426 break;
Michal Vasko77367452021-02-16 16:32:18 +01001427 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001428 ERR(session, "Client <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001429 rc = NC_MSG_WOULDBLOCK;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001430 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001431 default:
Michal Vasko77367452021-02-16 16:32:18 +01001432 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001433 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001434 }
1435
1436cleanup:
Michal Vasko77367452021-02-16 16:32:18 +01001437 ly_in_free(msg, 1);
1438 lyd_free_tree(hello);
1439 return rc;
Michal Vasko11d142a2016-01-19 15:58:24 +01001440}
1441
Michal Vasko71090fc2016-05-24 16:37:28 +02001442NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001443nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001444{
Michal Vasko086311b2016-01-08 09:53:11 +01001445 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001446
Michal Vasko131120a2018-05-29 15:44:02 +02001447 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001448 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001449 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001450 }
1451
Michal Vasko11d142a2016-01-19 15:58:24 +01001452 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001453 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001454 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001455 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001456 }
1457
Michal Vasko71090fc2016-05-24 16:37:28 +02001458 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001459}
Michal Vasko086311b2016-01-08 09:53:11 +01001460
Michal Vasko889162e2019-09-06 14:43:37 +02001461#ifdef NC_ENABLED_SSH
1462
Michal Vasko999b64a2019-07-10 16:06:15 +02001463static void
1464nc_ssh_init(void)
1465{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001466#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001467 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1468 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001469#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001470}
1471
1472static void
1473nc_ssh_destroy(void)
1474{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001475#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001476 FIPS_mode_set(0);
1477 CONF_modules_unload(1);
1478 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001479#endif
1480
Michal Vaskoe1e82632019-09-09 09:18:03 +02001481#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1482 ssh_finalize();
1483#endif
1484}
1485
Michal Vasko889162e2019-09-06 14:43:37 +02001486#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001487
Radek Krejci53691be2016-02-22 13:58:37 +01001488#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001489
Michal Vasko770b4362017-02-17 14:44:18 +01001490#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1491
Michal Vaskof0c92c02016-01-29 09:41:45 +01001492struct CRYPTO_dynlock_value {
1493 pthread_mutex_t lock;
1494};
1495
Michal Vaskof0c92c02016-01-29 09:41:45 +01001496static struct CRYPTO_dynlock_value *
1497tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1498{
1499 struct CRYPTO_dynlock_value *value;
1500
1501 value = malloc(sizeof *value);
1502 if (!value) {
1503 ERRMEM;
1504 return NULL;
1505 }
1506 pthread_mutex_init(&value->lock, NULL);
1507
1508 return value;
1509}
1510
1511static void
1512tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1513{
1514 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1515 * I found ignored this fact, what do I know... */
1516 if (mode & CRYPTO_LOCK) {
1517 pthread_mutex_lock(&l->lock);
1518 } else {
1519 pthread_mutex_unlock(&l->lock);
1520 }
1521}
1522
1523static void
1524tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1525{
1526 pthread_mutex_destroy(&l->lock);
1527 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001528}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001529
Michal Vasko770b4362017-02-17 14:44:18 +01001530#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001531
Michal Vasko8f0c0282016-02-29 10:17:14 +01001532#endif /* NC_ENABLED_TLS */
1533
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001534#if defined (NC_ENABLED_TLS) && !defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001535
Michal Vasko770b4362017-02-17 14:44:18 +01001536#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001537static pthread_mutex_t *tls_locks;
1538
1539static void
1540tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1541{
1542 if (mode & CRYPTO_LOCK) {
1543 pthread_mutex_lock(tls_locks + n);
1544 } else {
1545 pthread_mutex_unlock(tls_locks + n);
1546 }
1547}
1548
1549static void
1550tls_thread_id_func(CRYPTO_THREADID *tid)
1551{
1552 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1553}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001554
Michal Vasko770b4362017-02-17 14:44:18 +01001555#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001556
1557static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001558nc_tls_init(void)
1559{
Rosen Penev4f552d62019-06-26 16:10:43 -07001560#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001561 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001562 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001563 SSL_library_init();
1564
Michal Vaskof89948c2018-01-04 09:19:46 +01001565 int i;
1566
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001567 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001568 if (!tls_locks) {
1569 ERRMEM;
1570 return;
1571 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001572 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1573 pthread_mutex_init(tls_locks + i, NULL);
1574 }
1575
Michal Vaskof0c92c02016-01-29 09:41:45 +01001576 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001577 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001578
1579 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1580 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1581 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001582#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001583}
1584
Michal Vasko8f0c0282016-02-29 10:17:14 +01001585static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001586nc_tls_destroy(void)
1587{
Rosen Penev4f552d62019-06-26 16:10:43 -07001588#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001589 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001590 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001591 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001592 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001593 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001594#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001595 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001596#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1597 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001598#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001599
Michal Vaskof89948c2018-01-04 09:19:46 +01001600 int i;
1601
Michal Vaskob6e37262016-02-25 14:49:00 +01001602 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001603 CRYPTO_set_locking_callback(NULL);
1604 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1605 pthread_mutex_destroy(tls_locks + i);
1606 }
1607 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001608
1609 CRYPTO_set_dynlock_create_callback(NULL);
1610 CRYPTO_set_dynlock_lock_callback(NULL);
1611 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001612#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001613}
1614
Michal Vasko8f0c0282016-02-29 10:17:14 +01001615#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001616
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001617#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001618
Michal Vasko8f0c0282016-02-29 10:17:14 +01001619static void
Michal Vasko5e228792016-02-03 15:30:24 +01001620nc_ssh_tls_init(void)
1621{
Rosen Penev4f552d62019-06-26 16:10:43 -07001622#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001623 SSL_load_error_strings();
1624 ERR_load_BIO_strings();
1625 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001626#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001627
1628 nc_ssh_init();
1629
Michal Vaskoe1e82632019-09-09 09:18:03 +02001630#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001631 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1632 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1633 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001634#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001635}
1636
Michal Vasko8f0c0282016-02-29 10:17:14 +01001637static void
Michal Vasko5e228792016-02-03 15:30:24 +01001638nc_ssh_tls_destroy(void)
1639{
Rosen Penev4f552d62019-06-26 16:10:43 -07001640#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001641 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001642# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001643 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001644# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001645 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001646# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001647#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001648
1649 nc_ssh_destroy();
1650
Michal Vaskoe1e82632019-09-09 09:18:03 +02001651#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001652 CRYPTO_set_dynlock_create_callback(NULL);
1653 CRYPTO_set_dynlock_lock_callback(NULL);
1654 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001655#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001656}
1657
Radek Krejci53691be2016-02-22 13:58:37 +01001658#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001659
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001660#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001661
1662API void
1663nc_thread_destroy(void)
1664{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001665 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001666 // CRYPTO_cleanup_all_ex_data();
Michal Vasko8f0c0282016-02-29 10:17:14 +01001667
Michal Vasko770b4362017-02-17 14:44:18 +01001668#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1669 CRYPTO_THREADID crypto_tid;
1670
Michal Vasko8f0c0282016-02-29 10:17:14 +01001671 CRYPTO_THREADID_current(&crypto_tid);
1672 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001673#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001674}
1675
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001676#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1677
1678void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001679nc_init(void)
1680{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001681#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001682 nc_ssh_tls_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001683#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001684 nc_ssh_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001685#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001686 nc_tls_init();
1687#endif
1688}
1689
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001690void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001691nc_destroy(void)
1692{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001693#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001694 nc_ssh_tls_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001695#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001696 nc_ssh_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001697#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001698 nc_tls_destroy();
1699#endif
1700}