blob: 91f996e733e9d84e00917e394db053a5bd601f31 [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 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
Michal Vaskod8a74192023-02-06 15:51:50 +010059void
60nc_timeouttime_get(struct timespec *ts, uint32_t add_ms)
Radek Krejci7ac16052016-07-15 11:48:18 +020061{
Michal Vaskod8a74192023-02-06 15:51:50 +010062 if (clock_gettime(COMPAT_CLOCK_ID, ts) == -1) {
63 ERR(NULL, "clock_gettime() failed (%s).", strerror(errno));
64 return;
65 }
66
67 if (!add_ms) {
68 return;
69 }
70
71 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
72
73 ts->tv_sec += add_ms / 1000;
74 ts->tv_nsec += (add_ms % 1000) * 1000000L;
75
76 if (ts->tv_nsec >= 1000000000L) {
77 ++ts->tv_sec;
78 ts->tv_nsec -= 1000000000L;
79 } else if (ts->tv_nsec < 0) {
80 --ts->tv_sec;
81 ts->tv_nsec += 1000000000L;
82 }
83
84 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
85}
86
87int32_t
88nc_timeouttime_cur_diff(const struct timespec *ts)
89{
90 struct timespec cur;
91 int64_t nsec_diff = 0;
92
93 nc_timeouttime_get(&cur, 0);
94
95 nsec_diff += (((int64_t)ts->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
96 nsec_diff += ((int64_t)ts->tv_nsec) - ((int64_t)cur.tv_nsec);
97
98 return nsec_diff / 1000000L;
99}
100
101void
102nc_realtime_get(struct timespec *ts)
103{
roman6ece9c52022-06-22 09:29:17 +0200104 if (clock_gettime(CLOCK_REALTIME, ts)) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100105 ERR(NULL, "clock_gettime() failed (%s).", strerror(errno));
106 return;
roman6ece9c52022-06-22 09:29:17 +0200107 }
Michal Vasko36c7be82017-02-22 13:37:59 +0100108}
109
roman6e5fd702023-04-27 14:30:27 +0200110/**
111 * @brief Convert key type to string.
112 *
113 * @param[in] type Type of the key.
114 * @return String literal representing the key type or NULL.
115 */
Michal Vaskoddce1212019-05-24 09:58:49 +0200116const char *
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200117nc_keytype2str(NC_SSH_KEY_TYPE type)
Michal Vaskoddce1212019-05-24 09:58:49 +0200118{
119 switch (type) {
roman6e5fd702023-04-27 14:30:27 +0200120 case NC_SSH_KEY_UNKNOWN:
121 return "unknown";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200122 case NC_SSH_KEY_DSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200123 return "DSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200124 case NC_SSH_KEY_RSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200125 return "RSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200126 case NC_SSH_KEY_ECDSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200127 return "EC";
roman6e5fd702023-04-27 14:30:27 +0200128 case NC_SSH_KEY_ECDSA_P256:
129 return "ECDSA_P256";
130 case NC_SSH_KEY_ECDSA_P384:
131 return "ECDSA_P384";
132 case NC_SSH_KEY_ECDSA_P521:
133 return "ECDSA_P521";
roman44600f42023-04-28 15:54:27 +0200134 case NC_SSH_KEY_ED25519:
135 return NULL;
Michal Vaskoddce1212019-05-24 09:58:49 +0200136 default:
137 break;
138 }
139
140 return NULL;
141}
142
Michal Vaskobe52dc22018-10-17 09:28:17 +0200143int
romanc1d2b092023-02-02 08:58:27 +0100144nc_sock_configure_keepalive(int sock, struct nc_keepalives *ka)
Michal Vaskobe52dc22018-10-17 09:28:17 +0200145{
146 int opt;
147
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200148 opt = ka->enabled;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200149 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200150 ERR(NULL, "Could not set SO_KEEPALIVE option (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200151 return -1;
152 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200153 if (!ka->enabled) {
154 return 0;
155 }
Michal Vaskobe52dc22018-10-17 09:28:17 +0200156
157#ifdef TCP_KEEPIDLE
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200158 opt = ka->idle_time;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200159 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200160 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200161 return -1;
162 }
163#endif
164
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200165#ifdef TCP_KEEPCNT
166 opt = ka->max_probes;
167 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200168 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200169 return -1;
170 }
171#endif
172
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200173#ifdef TCP_KEEPINTVL
174 opt = ka->probe_interval;
175 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200176 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200177 return -1;
178 }
179#endif
180
181 return 0;
182}
183
Michal Vaskoade892d2017-02-22 13:40:35 +0100184struct nc_session *
Michal Vasko131120a2018-05-29 15:44:02 +0200185nc_new_session(NC_SIDE side, int shared_ti)
Michal Vaskoade892d2017-02-22 13:40:35 +0100186{
187 struct nc_session *sess;
188
189 sess = calloc(1, sizeof *sess);
190 if (!sess) {
191 return NULL;
192 }
193
Michal Vasko131120a2018-05-29 15:44:02 +0200194 sess->side = side;
195
196 if (side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200197 pthread_mutex_init(&sess->opts.server.ntf_status_lock, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100198 pthread_mutex_init(&sess->opts.server.rpc_lock, NULL);
199 pthread_cond_init(&sess->opts.server.rpc_cond, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100200
201 pthread_mutex_init(&sess->opts.server.ch_lock, NULL);
202 pthread_cond_init(&sess->opts.server.ch_cond, NULL);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200203 } else {
204 pthread_mutex_init(&sess->opts.client.msgs_lock, NULL);
Michal Vasko131120a2018-05-29 15:44:02 +0200205 }
206
207 if (!shared_ti) {
208 sess->io_lock = malloc(sizeof *sess->io_lock);
209 if (!sess->io_lock) {
210 goto error;
211 }
212 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100213 }
214
215 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200216
217error:
Michal Vasko131120a2018-05-29 15:44:02 +0200218 free(sess);
219 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100220}
221
Michal Vasko96164bf2016-01-21 15:41:58 +0100222/*
223 * @return 1 - success
224 * 0 - timeout
225 * -1 - error
226 */
227int
Michal Vasko131120a2018-05-29 15:44:02 +0200228nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100229{
230 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100231 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100232
Michal Vasko131120a2018-05-29 15:44:02 +0200233 if (session->side != NC_SERVER) {
234 ERRINT;
235 return -1;
236 }
237
Michal Vasko96164bf2016-01-21 15:41:58 +0100238 if (timeout > 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100239 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100240
Michal Vaskoade892d2017-02-22 13:40:35 +0100241 /* LOCK */
Michal Vaskod8a74192023-02-06 15:51:50 +0100242 ret = pthread_mutex_clocklock(&session->opts.server.rpc_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100243 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100244 while (session->opts.server.rpc_inuse) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100245 ret = pthread_cond_clockwait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock,
246 COMPAT_CLOCK_ID, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100247 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100248 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100249 break;
250 }
251 }
252 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100253 } else if (!timeout) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100254 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100255 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100256 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100257 if (session->opts.server.rpc_inuse) {
258 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100259 return 0;
260 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200261 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100262 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100263 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100264 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100265 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100266 while (session->opts.server.rpc_inuse) {
267 ret = pthread_cond_wait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100268 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100269 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100270 break;
271 }
272 }
273 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100274 }
275
Michal Vaskoade892d2017-02-22 13:40:35 +0100276 if (ret) {
277 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
278 /* timeout */
279 return 0;
280 }
281
Michal Vasko96164bf2016-01-21 15:41:58 +0100282 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200283 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100284 return -1;
285 }
286
287 /* ok */
Michal Vaskoacf98472021-02-04 15:33:57 +0100288 assert(session->opts.server.rpc_inuse == 0);
289 session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100290
291 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100292 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100293 if (ret) {
294 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200295 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100296 return -1;
297 }
298
299 return 1;
300}
301
302int
Michal Vasko131120a2018-05-29 15:44:02 +0200303nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100304{
305 int ret;
306 struct timespec ts_timeout;
307
Michal Vasko131120a2018-05-29 15:44:02 +0200308 if (session->side != NC_SERVER) {
309 ERRINT;
310 return -1;
311 }
312
Michal Vaskoacf98472021-02-04 15:33:57 +0100313 assert(session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100314
315 if (timeout > 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100316 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100317
318 /* LOCK */
Michal Vaskod8a74192023-02-06 15:51:50 +0100319 ret = pthread_mutex_clocklock(&session->opts.server.rpc_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100320 } else if (!timeout) {
321 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100322 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100323 } else { /* timeout == -1 */
324 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100325 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100326 }
327
328 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
329 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200330 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100331 return -1;
332 } else if (ret) {
Michal Vasko69e98752022-12-14 14:20:17 +0100333 WRN(session, "%s: session RPC lock timeout, should not happen.", func);
Michal Vaskoade892d2017-02-22 13:40:35 +0100334 }
335
Michal Vaskoacf98472021-02-04 15:33:57 +0100336 session->opts.server.rpc_inuse = 0;
337 pthread_cond_signal(&session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100338
339 if (!ret) {
340 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100341 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100342 if (ret) {
343 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200344 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100345 return -1;
346 }
347 }
348
Michal Vasko96164bf2016-01-21 15:41:58 +0100349 return 1;
350}
351
Michal Vasko131120a2018-05-29 15:44:02 +0200352int
353nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
354{
355 int ret;
356 struct timespec ts_timeout;
357
358 if (timeout > 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100359 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko131120a2018-05-29 15:44:02 +0200360
Michal Vaskod8a74192023-02-06 15:51:50 +0100361 ret = pthread_mutex_clocklock(session->io_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vasko131120a2018-05-29 15:44:02 +0200362 } else if (!timeout) {
363 ret = pthread_mutex_trylock(session->io_lock);
364 } else { /* timeout == -1 */
Robin Jarry54ea2962018-10-10 10:33:40 +0200365 ret = pthread_mutex_lock(session->io_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200366 }
367
368 if (ret) {
369 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
370 /* timeout */
371 return 0;
372 }
373
374 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200375 ERR(session, "%s: failed to IO lock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200376 return -1;
377 }
378
379 return 1;
380}
381
382int
383nc_session_io_unlock(struct nc_session *session, const char *func)
384{
385 int ret;
386
387 ret = pthread_mutex_unlock(session->io_lock);
388 if (ret) {
389 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200390 ERR(session, "%s: failed to IO unlock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200391 return -1;
392 }
393
394 return 1;
395}
396
Michal Vasko01130bd2021-08-26 11:47:38 +0200397int
398nc_session_client_msgs_lock(struct nc_session *session, int *timeout, const char *func)
399{
400 int ret;
401 int32_t diff_msec;
roman6ece9c52022-06-22 09:29:17 +0200402 struct timespec ts_timeout, ts_start;
Michal Vasko01130bd2021-08-26 11:47:38 +0200403
404 assert(session->side == NC_CLIENT);
405
406 if (*timeout > 0) {
407 /* get current time */
Michal Vaskod8a74192023-02-06 15:51:50 +0100408 nc_timeouttime_get(&ts_start, 0);
Michal Vasko01130bd2021-08-26 11:47:38 +0200409
Michal Vaskod8a74192023-02-06 15:51:50 +0100410 nc_timeouttime_get(&ts_timeout, *timeout);
Michal Vasko01130bd2021-08-26 11:47:38 +0200411
Michal Vaskod8a74192023-02-06 15:51:50 +0100412 ret = pthread_mutex_clocklock(&session->opts.client.msgs_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vasko01130bd2021-08-26 11:47:38 +0200413 if (!ret) {
414 /* update timeout based on what was elapsed */
Michal Vaskod8a74192023-02-06 15:51:50 +0100415 diff_msec = nc_timeouttime_cur_diff(&ts_start);
Michal Vasko01130bd2021-08-26 11:47:38 +0200416 *timeout -= diff_msec;
417 }
418 } else if (!*timeout) {
419 ret = pthread_mutex_trylock(&session->opts.client.msgs_lock);
420 } else { /* timeout == -1 */
421 ret = pthread_mutex_lock(&session->opts.client.msgs_lock);
422 }
423
424 if (ret) {
425 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
426 /* timeout */
427 return 0;
428 }
429
430 /* error */
431 ERR(session, "%s: failed to MSGS lock a session (%s).", func, strerror(ret));
432 return -1;
433 }
434
435 return 1;
436}
437
438int
439nc_session_client_msgs_unlock(struct nc_session *session, const char *func)
440{
441 int ret;
442
443 assert(session->side == NC_CLIENT);
444
445 ret = pthread_mutex_unlock(&session->opts.client.msgs_lock);
446 if (ret) {
447 /* error */
448 ERR(session, "%s: failed to MSGS unlock a session (%s).", func, strerror(ret));
449 return -1;
450 }
451
452 return 1;
453}
454
Michal Vasko8dadf782016-01-15 10:29:36 +0100455API NC_STATUS
456nc_session_get_status(const struct nc_session *session)
457{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100458 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200459 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200460 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100461 }
462
Michal Vasko8dadf782016-01-15 10:29:36 +0100463 return session->status;
464}
465
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100466API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200467nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100468{
469 if (!session) {
470 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200471 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100472 }
473
474 return session->term_reason;
475}
476
Michal Vasko8dadf782016-01-15 10:29:36 +0100477API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200478nc_session_get_killed_by(const struct nc_session *session)
479{
480 if (!session) {
481 ERRARG("session");
482 return 0;
483 }
484
485 return session->killed_by;
486}
487
488API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100489nc_session_get_id(const struct nc_session *session)
490{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100491 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200492 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100493 return 0;
494 }
495
Michal Vasko8dadf782016-01-15 10:29:36 +0100496 return session->id;
497}
498
Michal Vasko174fe8e2016-02-17 15:38:09 +0100499API int
500nc_session_get_version(const struct nc_session *session)
501{
502 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200503 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100504 return -1;
505 }
506
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200507 return session->version == NC_VERSION_10 ? 0 : 1;
Michal Vasko174fe8e2016-02-17 15:38:09 +0100508}
509
Michal Vasko8dadf782016-01-15 10:29:36 +0100510API NC_TRANSPORT_IMPL
511nc_session_get_ti(const struct nc_session *session)
512{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100513 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200514 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100515 return 0;
516 }
517
Michal Vasko8dadf782016-01-15 10:29:36 +0100518 return session->ti_type;
519}
520
521API const char *
522nc_session_get_username(const struct nc_session *session)
523{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100524 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200525 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100526 return NULL;
527 }
528
Michal Vasko8dadf782016-01-15 10:29:36 +0100529 return session->username;
530}
531
532API const char *
533nc_session_get_host(const struct nc_session *session)
534{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100535 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200536 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100537 return NULL;
538 }
539
Michal Vasko8dadf782016-01-15 10:29:36 +0100540 return session->host;
541}
542
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200543API const char *
544nc_session_get_path(const struct nc_session *session)
545{
546 if (!session) {
547 ERRARG("session");
548 return NULL;
549 }
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200550 if (session->ti_type != NC_TI_UNIX) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200551 return NULL;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200552 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200553
554 return session->path;
555}
556
Michal Vasko8dadf782016-01-15 10:29:36 +0100557API uint16_t
558nc_session_get_port(const struct nc_session *session)
559{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100560 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200561 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100562 return 0;
563 }
564
Michal Vasko8dadf782016-01-15 10:29:36 +0100565 return session->port;
566}
567
Michal Vasko93224072021-11-09 12:14:28 +0100568API const struct ly_ctx *
Michal Vasko9a25e932016-02-01 10:36:42 +0100569nc_session_get_ctx(const struct nc_session *session)
570{
571 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200572 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100573 return NULL;
574 }
575
576 return session->ctx;
577}
578
Michal Vasko2cc4c682016-03-01 09:16:48 +0100579API void
580nc_session_set_data(struct nc_session *session, void *data)
581{
582 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200583 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100584 return;
585 }
586
587 session->data = data;
588}
589
590API void *
591nc_session_get_data(const struct nc_session *session)
592{
593 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200594 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100595 return NULL;
596 }
597
598 return session->data;
599}
600
Michal Vaskodc96bb92023-03-28 08:52:48 +0200601API int
602nc_session_is_callhome(const struct nc_session *session)
603{
604 if (!session) {
605 ERRARG("session");
606 return 0;
607 }
608
609 if (session->flags & NC_SESSION_CALLHOME) {
610 return 1;
611 }
612
613 return 0;
614}
615
Michal Vasko086311b2016-01-08 09:53:11 +0100616NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200617nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200618{
Michal Vasko7e1f5fb2021-11-10 10:14:45 +0100619 if (session->ctx != LYD_CTX(op)) {
620 ERR(session, "RPC \"%s\" was created in different context than that of the session.", LYD_NAME(op));
Michal Vasko086311b2016-01-08 09:53:11 +0100621 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100622 }
623
Michal Vasko131120a2018-05-29 15:44:02 +0200624 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100625}
626
Michal Vaskod4da3632022-05-25 11:49:10 +0200627/**
628 * @brief Send \<close-session\> and read the reply on a session.
629 *
630 * @param[in] session Closing NETCONF session.
631 */
632static void
633nc_session_free_close_session(struct nc_session *session)
634{
635 struct ly_in *msg;
636 struct lyd_node *close_rpc, *envp;
637 const struct lys_module *ietfnc;
638
639 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
640 if (!ietfnc) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200641 WRN(session, "Missing ietf-netconf module in context, unable to send <close-session>.");
Michal Vaskod4da3632022-05-25 11:49:10 +0200642 return;
643 }
644 if (lyd_new_inner(NULL, ietfnc, "close-session", 0, &close_rpc)) {
645 WRN(session, "Failed to create <close-session> RPC.");
646 return;
647 }
648
649 /* send the RPC */
650 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
651
652read_msg:
653 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &msg)) {
654 case 1:
655 if (!strncmp(ly_in_memory(msg, NULL), "<notification", 13)) {
656 /* ignore */
657 ly_in_free(msg, 1);
658 goto read_msg;
659 }
660 if (lyd_parse_op(session->ctx, close_rpc, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, &envp, NULL)) {
661 WRN(session, "Failed to parse <close-session> reply.");
662 } else if (!lyd_child(envp) || strcmp(LYD_NAME(lyd_child(envp)), "ok")) {
663 WRN(session, "Reply to <close-session> was not <ok> as expected.");
664 }
665 lyd_free_tree(envp);
666 ly_in_free(msg, 1);
667 break;
668 case 0:
669 WRN(session, "Timeout for receiving a reply to <close-session> elapsed.");
670 break;
671 case -1:
672 ERR(session, "Failed to receive a reply to <close-session>.");
673 break;
674 default:
675 /* cannot happen */
676 break;
677 }
678 lyd_free_tree(close_rpc);
679}
680
Michal Vasko33476c32022-09-09 11:21:40 +0200681/**
682 * @brief Free transport implementation members of a session.
683 *
684 * @param[in] session Session to free.
685 * @param[out] multisession Whether there are other NC sessions on the same SSH sessions.
686 */
687static void
688nc_session_free_transport(struct nc_session *session, int *multisession)
689{
690 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskoe44f2702022-12-12 07:58:06 +0100691 int sock = -1;
Michal Vasko33476c32022-09-09 11:21:40 +0200692 struct nc_session *siter;
693
694 *multisession = 0;
695 connected = nc_session_is_connected(session);
696
697 /* transport implementation cleanup */
698 switch (session->ti_type) {
699 case NC_TI_FD:
700 /* nothing needed - file descriptors were provided by caller,
701 * so it is up to the caller to close them correctly
702 * TODO use callbacks
703 */
704 /* just to avoid compiler warning */
705 (void)connected;
706 (void)siter;
707 break;
708
709 case NC_TI_UNIX:
710 sock = session->ti.unixsock.sock;
711 (void)connected;
712 (void)siter;
713 break;
714
715#ifdef NC_ENABLED_SSH
Michal Vaskoe44f2702022-12-12 07:58:06 +0100716 case NC_TI_LIBSSH: {
717 int r;
718
Michal Vasko33476c32022-09-09 11:21:40 +0200719 if (connected) {
Michal Vasko64734402022-09-09 11:22:00 +0200720 ssh_channel_send_eof(session->ti.libssh.channel);
Michal Vasko33476c32022-09-09 11:21:40 +0200721 ssh_channel_free(session->ti.libssh.channel);
722 }
723 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
724 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
725 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
726 */
727 /* SESSION IO LOCK */
728 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
729
730 if (session->ti.libssh.next) {
731 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
732 if (siter->status != NC_STATUS_STARTING) {
733 *multisession = 1;
734 break;
735 }
736 }
737 }
738
739 if (!*multisession) {
740 /* it's not multisession yet, but we still need to free the starting sessions */
741 if (session->ti.libssh.next) {
742 do {
743 siter = session->ti.libssh.next;
744 session->ti.libssh.next = siter->ti.libssh.next;
745
746 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
747 free(siter->username);
748 free(siter->host);
749 if (!(siter->flags & NC_SESSION_SHAREDCTX)) {
750 ly_ctx_destroy((struct ly_ctx *)siter->ctx);
751 }
752
753 free(siter);
754 } while (session->ti.libssh.next != session);
755 }
756 /* remember sock so we can close it */
757 sock = ssh_get_fd(session->ti.libssh.session);
758 if (connected) {
759 ssh_disconnect(session->ti.libssh.session);
760 sock = -1;
761 }
762 ssh_free(session->ti.libssh.session);
763 } else {
764 /* remove the session from the list */
765 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {}
766 if (session->ti.libssh.next == siter) {
767 /* there will be only one session */
768 siter->ti.libssh.next = NULL;
769 } else {
770 /* there are still multiple sessions, keep the ring list */
771 siter->ti.libssh.next = session->ti.libssh.next;
772 }
Michal Vasko33476c32022-09-09 11:21:40 +0200773 }
774
775 /* SESSION IO UNLOCK */
776 if (r == 1) {
777 nc_session_io_unlock(session, __func__);
778 }
779 break;
Michal Vaskoe44f2702022-12-12 07:58:06 +0100780 }
Michal Vasko33476c32022-09-09 11:21:40 +0200781#endif
782
783#ifdef NC_ENABLED_TLS
784 case NC_TI_OPENSSL:
785 /* remember sock so we can close it */
786 sock = SSL_get_fd(session->ti.tls);
787
788 if (connected) {
789 SSL_shutdown(session->ti.tls);
790 }
791 SSL_free(session->ti.tls);
792
793 if (session->side == NC_SERVER) {
794 X509_free(session->opts.server.client_cert);
795 }
796 break;
797#endif
798 case NC_TI_NONE:
799 break;
800 }
801
802 /* close socket separately */
803 if (sock > -1) {
804 close(sock);
805 }
806}
807
Radek Krejci695d4fa2015-10-22 13:23:54 +0200808API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100809nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200810{
Michal Vasko33476c32022-09-09 11:21:40 +0200811 int r, i, rpc_locked = 0, msgs_locked = 0, timeout;
Michal Vaskob48aa812016-01-18 14:13:09 +0100812 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoad611702015-12-03 13:41:51 +0100813 struct nc_msg_cont *contiter;
Michal Vasko77367452021-02-16 16:32:18 +0100814 struct ly_in *msg;
roman6ece9c52022-06-22 09:29:17 +0200815 struct timespec ts;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200816 void *p;
817
Michal Vasko428087d2016-01-14 16:04:28 +0100818 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200819 return;
820 }
821
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200822 /* stop notification threads if any */
823 if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running)) {
824 /* let the threads know they should quit */
825 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 0);
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200826
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200827 /* wait for them */
Michal Vaskod8a74192023-02-06 15:51:50 +0100828 nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200829 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_count)) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200830 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +0100831 if (nc_timeouttime_cur_diff(&ts) < 1) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200832 ERR(session, "Waiting for notification thread exit failed (timed out).");
833 break;
834 }
835 }
Michal Vasko86d357c2016-03-11 13:46:38 +0100836 }
837
Michal Vaskoacf98472021-02-04 15:33:57 +0100838 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200839 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100840 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100841 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200842 } else if (r) {
843 rpc_locked = 1;
Michal Vasko96a28a32021-02-04 15:35:20 +0100844 } else {
845 /* else failed to lock it, too bad */
Michal Vasko05532772021-06-03 12:12:38 +0200846 ERR(session, "Freeing a session while an RPC is being processed.");
Michal Vasko96a28a32021-02-04 15:35:20 +0100847 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200848 }
849
Michal Vasko8c247822020-09-07 13:23:23 +0200850 if (session->side == NC_CLIENT) {
Michal Vasko01130bd2021-08-26 11:47:38 +0200851 timeout = NC_SESSION_FREE_LOCK_TIMEOUT;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200852
Michal Vasko01130bd2021-08-26 11:47:38 +0200853 /* MSGS LOCK */
854 r = nc_session_client_msgs_lock(session, &timeout, __func__);
855 if (r == -1) {
856 return;
857 } else if (r) {
858 msgs_locked = 1;
859 } else {
860 /* else failed to lock it, too bad */
861 ERR(session, "Freeing a session while messages are being received.");
862 }
863
864 /* cleanup message queue */
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200865 for (contiter = session->opts.client.msgs; contiter; ) {
Michal Vasko77367452021-02-16 16:32:18 +0100866 ly_in_free(contiter->msg, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200867
Michal Vaskoad611702015-12-03 13:41:51 +0100868 p = contiter;
869 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200870 free(p);
871 }
872
Michal Vasko01130bd2021-08-26 11:47:38 +0200873 if (msgs_locked) {
874 /* MSGS UNLOCK */
875 nc_session_client_msgs_unlock(session, __func__);
876 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200877
Michal Vasko8c247822020-09-07 13:23:23 +0200878 if (session->status == NC_STATUS_RUNNING) {
Michal Vasko9c6d38c2021-09-03 13:02:53 +0200879 /* receive any leftover messages */
880 while (nc_read_msg_poll_io(session, 0, &msg) == 1) {
881 ly_in_free(msg, 1);
882 }
883
Michal Vasko8c247822020-09-07 13:23:23 +0200884 /* send closing info to the other side */
Michal Vaskod4da3632022-05-25 11:49:10 +0200885 nc_session_free_close_session(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200886 }
887
888 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200889 if (session->opts.client.cpblts) {
890 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200891 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200892 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200893 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200894 }
Michal Vasko78939072022-12-12 07:43:18 +0100895
896 /* LY ext data */
Michal Vaskoe44f2702022-12-12 07:58:06 +0100897#ifdef NC_ENABLED_SSH
898 struct nc_session *siter;
899
Michal Vasko78939072022-12-12 07:43:18 +0100900 if ((session->flags & NC_SESSION_SHAREDCTX) && session->ti.libssh.next) {
901 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
902 if (siter->status != NC_STATUS_STARTING) {
903 /* move LY ext data to this session */
904 assert(!siter->opts.client.ext_data);
905 siter->opts.client.ext_data = session->opts.client.ext_data;
906 session->opts.client.ext_data = NULL;
907 break;
908 }
909 }
Michal Vaskoe44f2702022-12-12 07:58:06 +0100910 } else
911#endif
912 {
Michal Vasko78939072022-12-12 07:43:18 +0100913 lyd_free_siblings(session->opts.client.ext_data);
914 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200915 }
916
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100917 if (session->data && data_free) {
918 data_free(session->data);
919 }
920
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200921 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
922 /* CH LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100923 pthread_mutex_lock(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200924 }
925
Michal Vasko86d357c2016-03-11 13:46:38 +0100926 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200927 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200928
Michal Vaskofeccb312022-03-24 15:24:59 +0100929 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100930 pthread_cond_signal(&session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200931
Michal Vaskod8a74192023-02-06 15:51:50 +0100932 nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vasko0db3db52021-03-03 10:45:42 +0100933
934 /* wait for CH thread to actually wake up and terminate */
935 r = 0;
Michal Vaskofeccb312022-03-24 15:24:59 +0100936 while (!r && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100937 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 +0100938 }
Michal Vasko0db3db52021-03-03 10:45:42 +0100939 if (r) {
Michal Vasko05532772021-06-03 12:12:38 +0200940 ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r));
Michal Vasko3f05a092018-03-13 10:39:49 +0100941 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200942 }
943
Michal Vaskofeccb312022-03-24 15:24:59 +0100944 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
945 /* CH UNLOCK */
946 pthread_mutex_unlock(&session->opts.server.ch_lock);
947 }
948
Radek Krejci695d4fa2015-10-22 13:23:54 +0200949 /* transport implementation cleanup */
Michal Vasko33476c32022-09-09 11:21:40 +0200950 nc_session_free_transport(session, &multisession);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200951
Michal Vasko33476c32022-09-09 11:21:40 +0200952 /* final cleanup */
Michal Vasko93224072021-11-09 12:14:28 +0100953 free(session->username);
954 free(session->host);
955 free(session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200956
Michal Vaskoacf98472021-02-04 15:33:57 +0100957 if (session->side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200958 pthread_mutex_destroy(&session->opts.server.ntf_status_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200959 if (rpc_locked) {
960 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100961 }
Michal Vaskoacf98472021-02-04 15:33:57 +0100962 pthread_mutex_destroy(&session->opts.server.rpc_lock);
963 pthread_cond_destroy(&session->opts.server.rpc_cond);
Michal Vasko131120a2018-05-29 15:44:02 +0200964 }
965
966 if (session->io_lock && !multisession) {
967 pthread_mutex_destroy(session->io_lock);
968 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200969 }
970
971 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko93224072021-11-09 12:14:28 +0100972 ly_ctx_destroy((struct ly_ctx *)session->ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200973 }
974
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200975 if (session->side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100976 /* free CH synchronization structures */
977 pthread_cond_destroy(&session->opts.server.ch_cond);
978 pthread_mutex_destroy(&session->opts.server.ch_lock);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200979 } else {
980 pthread_mutex_destroy(&session->opts.client.msgs_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200981 }
982
Radek Krejci695d4fa2015-10-22 13:23:54 +0200983 free(session);
984}
985
Michal Vasko086311b2016-01-08 09:53:11 +0100986static void
Michal Vasko93224072021-11-09 12:14:28 +0100987add_cpblt(const char *capab, char ***cpblts, int *size, int *count)
Michal Vasko086311b2016-01-08 09:53:11 +0100988{
Radek Krejci658782b2016-12-04 22:04:55 +0100989 size_t len;
990 int i;
991 char *p;
992
993 if (capab) {
994 /* check if already present */
995 p = strchr(capab, '?');
996 if (p) {
997 len = p - capab;
998 } else {
999 len = strlen(capab);
1000 }
1001 for (i = 0; i < *count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001002 if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) {
Radek Krejci658782b2016-12-04 22:04:55 +01001003 /* already present, do not duplicate it */
1004 return;
1005 }
1006 }
1007 }
1008
1009 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +01001010 if (*count == *size) {
1011 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001012 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
1013 if (!(*cpblts)) {
1014 ERRMEM;
1015 return;
1016 }
Michal Vasko086311b2016-01-08 09:53:11 +01001017 }
1018
Michal Vasko93224072021-11-09 12:14:28 +01001019 (*cpblts)[*count] = capab ? strdup(capab) : NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001020 ++(*count);
1021}
1022
Michal Vasko93224072021-11-09 12:14:28 +01001023API char **
1024nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +01001025{
Michal Vasko93224072021-11-09 12:14:28 +01001026 char **cpblts;
Michal Vasko77367452021-02-16 16:32:18 +01001027 const struct lys_module *mod;
1028 struct lysp_feature *feat;
1029 int size = 10, count, features_count = 0, dev_count = 0, str_len, len;
Michal Vasko1440a742021-03-31 11:11:03 +02001030 uint32_t i, u;
Michal Vasko77367452021-02-16 16:32:18 +01001031 LY_ARRAY_COUNT_TYPE v;
Michal Vasko1440a742021-03-31 11:11:03 +02001032 char *yl_content_id;
romanc1d2b092023-02-02 08:58:27 +01001033 uint32_t wd_also_supported;
1034 uint32_t wd_basic_mode;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001035
Radek Krejci24a18412018-05-16 15:09:10 +02001036#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +02001037 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001038
Michal Vasko4ffa3b22016-05-24 16:36:25 +02001039 if (!ctx) {
1040 ERRARG("ctx");
1041 return NULL;
1042 }
1043
Michal Vasko086311b2016-01-08 09:53:11 +01001044 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001045 if (!cpblts) {
1046 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +02001047 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001048 }
Michal Vasko93224072021-11-09 12:14:28 +01001049 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1050 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko086311b2016-01-08 09:53:11 +01001051 count = 2;
1052
1053 /* capabilities */
1054
Michal Vasko77367452021-02-16 16:32:18 +01001055 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf");
Michal Vasko086311b2016-01-08 09:53:11 +01001056 if (mod) {
Michal Vasko77367452021-02-16 16:32:18 +01001057 if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001058 add_cpblt("urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001059 }
Michal Vasko77367452021-02-16 16:32:18 +01001060 if (lys_feature_value(mod, "candidate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001061 add_cpblt("urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko77367452021-02-16 16:32:18 +01001062 if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001063 add_cpblt("urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001064 }
1065 }
Michal Vasko77367452021-02-16 16:32:18 +01001066 if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001067 add_cpblt("urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001068 }
Michal Vasko77367452021-02-16 16:32:18 +01001069 if (lys_feature_value(mod, "validate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001070 add_cpblt("urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001071 }
Michal Vasko77367452021-02-16 16:32:18 +01001072 if (lys_feature_value(mod, "startup") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001073 add_cpblt("urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001074 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001075
1076 /* The URL capability must be set manually using nc_server_set_capability()
1077 * because of the need for supported protocols to be included.
1078 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1079 */
Michal Vasko77367452021-02-16 16:32:18 +01001080 // if (lys_feature_value(mod, "url") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001081 // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
mekleoa8de5e92020-02-13 09:05:56 +01001082 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001083
Michal Vasko77367452021-02-16 16:32:18 +01001084 if (lys_feature_value(mod, "xpath") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001085 add_cpblt("urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001086 }
1087 }
1088
Michal Vasko77367452021-02-16 16:32:18 +01001089 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01001090 if (mod) {
romanc1d2b092023-02-02 08:58:27 +01001091 wd_basic_mode = ATOMIC_LOAD_RELAXED(server_opts.wd_basic_mode);
1092 if (!wd_basic_mode) {
Michal Vasko05532772021-06-03 12:12:38 +02001093 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 +01001094 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001095 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
romanc1d2b092023-02-02 08:58:27 +01001096 switch (wd_basic_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01001097 case NC_WD_ALL:
1098 strcat(str, "?basic-mode=report-all");
1099 break;
1100 case NC_WD_TRIM:
1101 strcat(str, "?basic-mode=trim");
1102 break;
1103 case NC_WD_EXPLICIT:
1104 strcat(str, "?basic-mode=explicit");
1105 break;
1106 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001107 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001108 break;
1109 }
1110
romanc1d2b092023-02-02 08:58:27 +01001111 wd_also_supported = ATOMIC_LOAD_RELAXED(server_opts.wd_also_supported);
1112 if (wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001113 strcat(str, "&also-supported=");
romanc1d2b092023-02-02 08:58:27 +01001114 if (wd_also_supported & NC_WD_ALL) {
Michal Vasko086311b2016-01-08 09:53:11 +01001115 strcat(str, "report-all,");
1116 }
romanc1d2b092023-02-02 08:58:27 +01001117 if (wd_also_supported & NC_WD_ALL_TAG) {
Michal Vasko086311b2016-01-08 09:53:11 +01001118 strcat(str, "report-all-tagged,");
1119 }
romanc1d2b092023-02-02 08:58:27 +01001120 if (wd_also_supported & NC_WD_TRIM) {
Michal Vasko086311b2016-01-08 09:53:11 +01001121 strcat(str, "trim,");
1122 }
romanc1d2b092023-02-02 08:58:27 +01001123 if (wd_also_supported & NC_WD_EXPLICIT) {
Michal Vasko086311b2016-01-08 09:53:11 +01001124 strcat(str, "explicit,");
1125 }
1126 str[strlen(str) - 1] = '\0';
1127
Michal Vasko93224072021-11-09 12:14:28 +01001128 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001129 }
1130 }
1131 }
1132
Radek Krejci658782b2016-12-04 22:04:55 +01001133 /* other capabilities */
1134 for (u = 0; u < server_opts.capabilities_count; u++) {
Michal Vasko93224072021-11-09 12:14:28 +01001135 add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001136 }
1137
1138 /* models */
Michal Vasko1440a742021-03-31 11:11:03 +02001139 u = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001140 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001141 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vasko77367452021-02-16 16:32:18 +01001142 if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) {
Michal Vasko05532772021-06-03 12:12:38 +02001143 ERR(NULL, "Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
Michal Vaskod5ada122020-03-19 18:28:06 +01001144 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001145 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001146
Michal Vasko1440a742021-03-31 11:11:03 +02001147 /* get content-id */
1148 if (server_opts.content_id_clb) {
1149 yl_content_id = server_opts.content_id_clb(server_opts.content_id_data);
1150 if (!yl_content_id) {
1151 ERRMEM;
1152 goto error;
1153 }
1154 } else {
1155 yl_content_id = malloc(11);
1156 if (!yl_content_id) {
1157 ERRMEM;
1158 goto error;
1159 }
1160 sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx));
1161 }
1162
Michal Vasko77367452021-02-16 16:32:18 +01001163 if (!strcmp(mod->revision, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001164 /* new one (capab defined in RFC 8526 section 2) */
Michal Vasko1440a742021-03-31 11:11:03 +02001165 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s",
1166 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001167 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001168 } else {
1169 /* old one (capab defined in RFC 7950 section 5.6.4) */
Michal Vasko1440a742021-03-31 11:11:03 +02001170 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s",
1171 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001172 add_cpblt(str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001173 }
Michal Vasko1440a742021-03-31 11:11:03 +02001174 free(yl_content_id);
Radek Krejci24a18412018-05-16 15:09:10 +02001175 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001176 } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001177 /* skip YANG 1.1 modules */
Radek Krejci24a18412018-05-16 15:09:10 +02001178 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001179 } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001180 /* skip YANG 1.0 modules */
Radek Krejci24a18412018-05-16 15:09:10 +02001181 continue;
1182 }
Michal Vasko086311b2016-01-08 09:53:11 +01001183
Michal Vasko77367452021-02-16 16:32:18 +01001184 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "",
1185 mod->revision ? mod->revision : "");
Radek Krejci24a18412018-05-16 15:09:10 +02001186
Michal Vaskodafdc742020-03-11 16:15:59 +01001187 features_count = 0;
1188 i = 0;
1189 feat = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001190 while ((feat = lysp_feature_next(feat, mod->parsed, &i))) {
Michal Vaskodafdc742020-03-11 16:15:59 +01001191 if (!(feat->flags & LYS_FENABLED)) {
1192 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001193 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001194 if (!features_count) {
1195 strcat(str, "&features=");
1196 str_len += 10;
1197 }
1198 len = strlen(feat->name);
1199 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1200 ERRINT;
1201 break;
1202 }
1203 if (features_count) {
1204 strcat(str, ",");
1205 ++str_len;
1206 }
1207 strcat(str, feat->name);
1208 str_len += len;
1209 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001210 }
Michal Vasko086311b2016-01-08 09:53:11 +01001211
Michal Vasko77367452021-02-16 16:32:18 +01001212 if (mod->deviated_by) {
Radek Krejci24a18412018-05-16 15:09:10 +02001213 strcat(str, "&deviations=");
1214 str_len += 12;
1215 dev_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +01001216 LY_ARRAY_FOR(mod->deviated_by, v) {
1217 len = strlen(mod->deviated_by[v]->name);
1218 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1219 ERRINT;
1220 break;
Radek Krejci24a18412018-05-16 15:09:10 +02001221 }
Michal Vasko77367452021-02-16 16:32:18 +01001222 if (dev_count) {
1223 strcat(str, ",");
1224 ++str_len;
Radek Krejci24a18412018-05-16 15:09:10 +02001225 }
Michal Vasko77367452021-02-16 16:32:18 +01001226 strcat(str, mod->deviated_by[v]->name);
1227 str_len += len;
1228 dev_count++;
Radek Krejci24a18412018-05-16 15:09:10 +02001229 }
1230 }
1231
Michal Vasko93224072021-11-09 12:14:28 +01001232 add_cpblt(str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +02001233 }
Michal Vasko086311b2016-01-08 09:53:11 +01001234
1235 /* ending NULL capability */
Michal Vasko93224072021-11-09 12:14:28 +01001236 add_cpblt(NULL, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001237
1238 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001239
1240error:
Radek Krejcif906e412017-09-22 14:44:45 +02001241 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001242 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001243}
1244
Michal Vasko93224072021-11-09 12:14:28 +01001245API char **
1246nc_server_get_cpblts(const struct ly_ctx *ctx)
Radek Krejci24a18412018-05-16 15:09:10 +02001247{
1248 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1249}
1250
Radek Krejci695d4fa2015-10-22 13:23:54 +02001251static int
Michal Vasko77367452021-02-16 16:32:18 +01001252parse_cpblts(struct lyd_node *capabilities, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001253{
Michal Vasko77367452021-02-16 16:32:18 +01001254 struct lyd_node *iter;
1255 struct lyd_node_opaq *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001256 int ver = -1, i = 0;
1257 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001258
1259 if (list) {
1260 /* get the storage for server's capabilities */
Michal Vasko77367452021-02-16 16:32:18 +01001261 LY_LIST_FOR(lyd_child(capabilities), iter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001262 i++;
1263 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001264 /* last item remains NULL */
1265 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001266 if (!*list) {
1267 ERRMEM;
1268 return -1;
1269 }
1270 i = 0;
1271 }
1272
Michal Vasko77367452021-02-16 16:32:18 +01001273 LY_LIST_FOR(lyd_child(capabilities), iter) {
1274 cpblt = (struct lyd_node_opaq *)iter;
1275
1276 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 +02001277 ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001278 return -1;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001279 }
1280
Michal Vasko156d3272017-04-11 11:46:49 +02001281 /* skip leading/trailing whitespaces */
Michal Vasko77367452021-02-16 16:32:18 +01001282 for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {}
1283 for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {}
1284 if (!cpb_start[0] || (cpb_end == cpblt->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001285 ERR(NULL, "Empty capability \"%s\" received.", cpblt->value);
Michal Vasko156d3272017-04-11 11:46:49 +02001286 return -1;
1287 }
1288
Radek Krejci695d4fa2015-10-22 13:23:54 +02001289 /* detect NETCONF version */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001290 if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001291 ver = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001292 } 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 +02001293 ver = 1;
1294 }
1295
1296 /* store capabilities */
1297 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001298 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1299 if (!(*list)[i]) {
1300 ERRMEM;
1301 return -1;
1302 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001303 i++;
1304 }
1305 }
1306
1307 if (ver == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001308 ERR(NULL, "Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001309 }
1310
1311 return ver;
1312}
1313
1314static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001315nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001316{
Michal Vasko131120a2018-05-29 15:44:02 +02001317 NC_MSG_TYPE ret;
1318 int i, io_timeout;
Michal Vasko93224072021-11-09 12:14:28 +01001319 char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001320 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001321
Michal Vasko131120a2018-05-29 15:44:02 +02001322 if (session->side == NC_CLIENT) {
1323 /* client side hello - send only NETCONF base capabilities */
1324 cpblts = malloc(3 * sizeof *cpblts);
1325 if (!cpblts) {
1326 ERRMEM;
1327 return NC_MSG_ERROR;
1328 }
Michal Vasko93224072021-11-09 12:14:28 +01001329 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1330 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko131120a2018-05-29 15:44:02 +02001331 cpblts[2] = NULL;
1332
1333 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1334 sid = NULL;
1335 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001336 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0);
Michal Vasko5b24b6b2020-12-04 09:29:47 +01001337 if (!cpblts) {
1338 return NC_MSG_ERROR;
1339 }
Michal Vasko131120a2018-05-29 15:44:02 +02001340
1341 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1342 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001343 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001344
Michal Vasko131120a2018-05-29 15:44:02 +02001345 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001346
Michal Vasko086311b2016-01-08 09:53:11 +01001347 for (i = 0; cpblts[i]; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001348 free(cpblts[i]);
Michal Vasko086311b2016-01-08 09:53:11 +01001349 }
1350 free(cpblts);
1351
Michal Vasko131120a2018-05-29 15:44:02 +02001352 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001353}
1354
1355static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001356nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001357{
Michal Vasko77367452021-02-16 16:32:18 +01001358 struct ly_in *msg;
1359 struct lyd_node *hello = NULL, *iter;
1360 struct lyd_node_opaq *node;
1361 int r, ver = -1, flag = 0;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001362 char *str;
Michal Vasko292c5542023-02-01 14:33:17 +01001363 long long id;
Michal Vasko77367452021-02-16 16:32:18 +01001364 NC_MSG_TYPE rc = NC_MSG_HELLO;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001365
Michal Vasko77367452021-02-16 16:32:18 +01001366 r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg);
1367 switch (r) {
1368 case 1:
Radek Krejci695d4fa2015-10-22 13:23:54 +02001369 /* parse <hello> data */
Michal Vasko77367452021-02-16 16:32:18 +01001370 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 +02001371 ERR(session, "Failed to parse server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001372 rc = NC_MSG_ERROR;
1373 goto cleanup;
1374 }
1375
1376 LY_LIST_FOR(lyd_child(hello), iter) {
1377 node = (struct lyd_node_opaq *)iter;
1378
1379 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001380 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001381 } else if (!strcmp(node->name.name, "session-id")) {
1382 if (!node->value || !strlen(node->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001383 ERR(session, "No value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001384 rc = NC_MSG_ERROR;
1385 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001386 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001387 str = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001388 id = strtoll(node->value, &str, 10);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001389 if (*str || (id < 1) || (id > UINT32_MAX)) {
Michal Vasko05532772021-06-03 12:12:38 +02001390 ERR(session, "Invalid value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001391 rc = NC_MSG_ERROR;
1392 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001393 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001394 session->id = (uint32_t)id;
1395 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001396 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001397 ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001398 rc = NC_MSG_ERROR;
1399 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001400 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001401
1402 if (flag) {
1403 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001404 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001405 rc = NC_MSG_ERROR;
1406 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001407 }
1408 flag = 1;
1409
Michal Vasko77367452021-02-16 16:32:18 +01001410 if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) {
1411 rc = NC_MSG_ERROR;
1412 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001413 }
1414 session->version = ver;
1415 }
1416
1417 if (!session->id) {
Michal Vasko05532772021-06-03 12:12:38 +02001418 ERR(session, "Missing <session-id> in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001419 rc = NC_MSG_ERROR;
1420 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001421 }
1422 break;
Michal Vasko77367452021-02-16 16:32:18 +01001423 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001424 ERR(session, "Server <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001425 rc = NC_MSG_WOULDBLOCK;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001426 break;
1427 default:
Michal Vasko77367452021-02-16 16:32:18 +01001428 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001429 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001430 }
1431
Michal Vasko77367452021-02-16 16:32:18 +01001432cleanup:
1433 ly_in_free(msg, 1);
1434 lyd_free_tree(hello);
1435 return rc;
Radek Krejci5686ff72015-10-09 13:33:56 +02001436}
1437
Michal Vasko11d142a2016-01-19 15:58:24 +01001438static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001439nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001440{
Michal Vasko77367452021-02-16 16:32:18 +01001441 struct ly_in *msg;
1442 struct lyd_node *hello = NULL, *iter;
1443 struct lyd_node_opaq *node;
1444 NC_MSG_TYPE rc = NC_MSG_HELLO;
1445 int r, ver = -1, flag = 0, timeout_io;
Michal Vasko11d142a2016-01-19 15:58:24 +01001446
Michal Vasko77367452021-02-16 16:32:18 +01001447 timeout_io = server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000;
1448 r = nc_read_msg_poll_io(session, timeout_io, &msg);
1449 switch (r) {
1450 case 1:
1451 /* parse <hello> data */
1452 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 +02001453 ERR(session, "Failed to parse client <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001454 rc = NC_MSG_ERROR;
1455 goto cleanup;
1456 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001457
Michal Vasko77367452021-02-16 16:32:18 +01001458 /* learn NETCONF version */
1459 LY_LIST_FOR(lyd_child(hello), iter) {
1460 node = (struct lyd_node_opaq *)iter;
1461
1462 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001463 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001464 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001465 ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001466 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001467 goto cleanup;
1468 }
1469
1470 if (flag) {
1471 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001472 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001473 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001474 goto cleanup;
1475 }
1476 flag = 1;
1477
Michal Vasko77367452021-02-16 16:32:18 +01001478 if ((ver = parse_cpblts(&node->node, NULL)) < 0) {
1479 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001480 goto cleanup;
1481 }
1482 session->version = ver;
1483 }
1484 break;
Michal Vasko77367452021-02-16 16:32:18 +01001485 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001486 ERR(session, "Client <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001487 rc = NC_MSG_WOULDBLOCK;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001488 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001489 default:
Michal Vasko77367452021-02-16 16:32:18 +01001490 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001491 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001492 }
1493
1494cleanup:
Michal Vasko77367452021-02-16 16:32:18 +01001495 ly_in_free(msg, 1);
1496 lyd_free_tree(hello);
1497 return rc;
Michal Vasko11d142a2016-01-19 15:58:24 +01001498}
1499
Michal Vasko71090fc2016-05-24 16:37:28 +02001500NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001501nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001502{
Michal Vasko086311b2016-01-08 09:53:11 +01001503 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001504
Michal Vasko131120a2018-05-29 15:44:02 +02001505 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001506 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001507 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001508 }
1509
Michal Vasko11d142a2016-01-19 15:58:24 +01001510 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001511 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001512 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001513 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001514 }
1515
Michal Vasko71090fc2016-05-24 16:37:28 +02001516 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001517}
Michal Vasko086311b2016-01-08 09:53:11 +01001518
Michal Vasko889162e2019-09-06 14:43:37 +02001519#ifdef NC_ENABLED_SSH
1520
Michal Vasko999b64a2019-07-10 16:06:15 +02001521static void
1522nc_ssh_init(void)
1523{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001524#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001525 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1526 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001527#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001528}
1529
1530static void
1531nc_ssh_destroy(void)
1532{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001533#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001534 FIPS_mode_set(0);
1535 CONF_modules_unload(1);
1536 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001537#endif
1538
Michal Vaskoe1e82632019-09-09 09:18:03 +02001539#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1540 ssh_finalize();
1541#endif
1542}
1543
Michal Vasko889162e2019-09-06 14:43:37 +02001544#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001545
Radek Krejci53691be2016-02-22 13:58:37 +01001546#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001547
Michal Vasko770b4362017-02-17 14:44:18 +01001548#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1549
Michal Vaskof0c92c02016-01-29 09:41:45 +01001550struct CRYPTO_dynlock_value {
1551 pthread_mutex_t lock;
1552};
1553
Michal Vaskof0c92c02016-01-29 09:41:45 +01001554static struct CRYPTO_dynlock_value *
1555tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1556{
1557 struct CRYPTO_dynlock_value *value;
1558
1559 value = malloc(sizeof *value);
1560 if (!value) {
1561 ERRMEM;
1562 return NULL;
1563 }
1564 pthread_mutex_init(&value->lock, NULL);
1565
1566 return value;
1567}
1568
1569static void
1570tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1571{
1572 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1573 * I found ignored this fact, what do I know... */
1574 if (mode & CRYPTO_LOCK) {
1575 pthread_mutex_lock(&l->lock);
1576 } else {
1577 pthread_mutex_unlock(&l->lock);
1578 }
1579}
1580
1581static void
1582tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1583{
1584 pthread_mutex_destroy(&l->lock);
1585 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001586}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001587
Michal Vasko770b4362017-02-17 14:44:18 +01001588#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001589
Michal Vasko8f0c0282016-02-29 10:17:14 +01001590#endif /* NC_ENABLED_TLS */
1591
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001592#if defined (NC_ENABLED_TLS) && !defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001593
Michal Vasko770b4362017-02-17 14:44:18 +01001594#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001595static pthread_mutex_t *tls_locks;
1596
1597static void
1598tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1599{
1600 if (mode & CRYPTO_LOCK) {
1601 pthread_mutex_lock(tls_locks + n);
1602 } else {
1603 pthread_mutex_unlock(tls_locks + n);
1604 }
1605}
1606
1607static void
1608tls_thread_id_func(CRYPTO_THREADID *tid)
1609{
1610 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1611}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001612
Michal Vasko770b4362017-02-17 14:44:18 +01001613#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001614
1615static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001616nc_tls_init(void)
1617{
Rosen Penev4f552d62019-06-26 16:10:43 -07001618#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001619 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001620 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001621 SSL_library_init();
1622
Michal Vaskof89948c2018-01-04 09:19:46 +01001623 int i;
1624
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001625 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001626 if (!tls_locks) {
1627 ERRMEM;
1628 return;
1629 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001630 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1631 pthread_mutex_init(tls_locks + i, NULL);
1632 }
1633
Michal Vaskof0c92c02016-01-29 09:41:45 +01001634 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001635 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001636
1637 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1638 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1639 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001640#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001641}
1642
Michal Vasko8f0c0282016-02-29 10:17:14 +01001643static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001644nc_tls_destroy(void)
1645{
Rosen Penev4f552d62019-06-26 16:10:43 -07001646#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001647 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001648 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001649 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001650 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001651 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001652#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001653 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001654#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1655 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001656#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001657
Michal Vaskof89948c2018-01-04 09:19:46 +01001658 int i;
1659
Michal Vaskob6e37262016-02-25 14:49:00 +01001660 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001661 CRYPTO_set_locking_callback(NULL);
1662 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1663 pthread_mutex_destroy(tls_locks + i);
1664 }
1665 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001666
1667 CRYPTO_set_dynlock_create_callback(NULL);
1668 CRYPTO_set_dynlock_lock_callback(NULL);
1669 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001670#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001671}
1672
Michal Vasko8f0c0282016-02-29 10:17:14 +01001673#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001674
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001675#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001676
Michal Vasko8f0c0282016-02-29 10:17:14 +01001677static void
Michal Vasko5e228792016-02-03 15:30:24 +01001678nc_ssh_tls_init(void)
1679{
Rosen Penev4f552d62019-06-26 16:10:43 -07001680#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001681 SSL_load_error_strings();
1682 ERR_load_BIO_strings();
1683 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001684#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001685
1686 nc_ssh_init();
1687
Michal Vaskoe1e82632019-09-09 09:18:03 +02001688#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001689 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1690 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1691 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001692#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001693}
1694
Michal Vasko8f0c0282016-02-29 10:17:14 +01001695static void
Michal Vasko5e228792016-02-03 15:30:24 +01001696nc_ssh_tls_destroy(void)
1697{
Rosen Penev4f552d62019-06-26 16:10:43 -07001698#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001699 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001700# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001701 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001702# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001703 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001704# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001705#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001706
1707 nc_ssh_destroy();
1708
Michal Vaskoe1e82632019-09-09 09:18:03 +02001709#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001710 CRYPTO_set_dynlock_create_callback(NULL);
1711 CRYPTO_set_dynlock_lock_callback(NULL);
1712 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001713#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001714}
1715
Radek Krejci53691be2016-02-22 13:58:37 +01001716#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001717
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001718#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001719
1720API void
1721nc_thread_destroy(void)
1722{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001723 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001724 // CRYPTO_cleanup_all_ex_data();
Michal Vasko8f0c0282016-02-29 10:17:14 +01001725
Michal Vasko770b4362017-02-17 14:44:18 +01001726#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1727 CRYPTO_THREADID crypto_tid;
1728
Michal Vasko8f0c0282016-02-29 10:17:14 +01001729 CRYPTO_THREADID_current(&crypto_tid);
1730 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001731#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001732}
1733
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001734#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1735
1736void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001737nc_init(void)
1738{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001739#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001740 nc_ssh_tls_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001741#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001742 nc_ssh_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001743#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001744 nc_tls_init();
1745#endif
1746}
1747
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001748void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001749nc_destroy(void)
1750{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001751#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001752 nc_ssh_tls_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001753#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001754 nc_ssh_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001755#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001756 nc_tls_destroy();
1757#endif
1758}