blob: 17c6bea3b5c4cc77aef495e8f2d26342b67e5bbd [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{
roman40672412023-05-04 11:10:22 +0200458 NC_CHECK_ARG_RET(session, session, NC_STATUS_ERR);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100459
Michal Vasko8dadf782016-01-15 10:29:36 +0100460 return session->status;
461}
462
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100463API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200464nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100465{
roman40672412023-05-04 11:10:22 +0200466 NC_CHECK_ARG_RET(session, session, NC_SESSION_TERM_ERR);
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100467
468 return session->term_reason;
469}
470
Michal Vasko8dadf782016-01-15 10:29:36 +0100471API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200472nc_session_get_killed_by(const struct nc_session *session)
473{
roman40672412023-05-04 11:10:22 +0200474 NC_CHECK_ARG_RET(session, session, 0);
Michal Vasko142cfea2017-08-07 10:12:11 +0200475
476 return session->killed_by;
477}
478
479API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100480nc_session_get_id(const struct nc_session *session)
481{
roman40672412023-05-04 11:10:22 +0200482 NC_CHECK_ARG_RET(session, session, 0);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100483
Michal Vasko8dadf782016-01-15 10:29:36 +0100484 return session->id;
485}
486
Michal Vasko174fe8e2016-02-17 15:38:09 +0100487API int
488nc_session_get_version(const struct nc_session *session)
489{
roman40672412023-05-04 11:10:22 +0200490 NC_CHECK_ARG_RET(session, session, -1);
Michal Vasko174fe8e2016-02-17 15:38:09 +0100491
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200492 return session->version == NC_VERSION_10 ? 0 : 1;
Michal Vasko174fe8e2016-02-17 15:38:09 +0100493}
494
Michal Vasko8dadf782016-01-15 10:29:36 +0100495API NC_TRANSPORT_IMPL
496nc_session_get_ti(const struct nc_session *session)
497{
roman40672412023-05-04 11:10:22 +0200498 NC_CHECK_ARG_RET(session, session, 0);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100499
Michal Vasko8dadf782016-01-15 10:29:36 +0100500 return session->ti_type;
501}
502
503API const char *
504nc_session_get_username(const struct nc_session *session)
505{
roman40672412023-05-04 11:10:22 +0200506 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100507
Michal Vasko8dadf782016-01-15 10:29:36 +0100508 return session->username;
509}
510
511API const char *
512nc_session_get_host(const struct nc_session *session)
513{
roman40672412023-05-04 11:10:22 +0200514 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100515
Michal Vasko8dadf782016-01-15 10:29:36 +0100516 return session->host;
517}
518
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200519API const char *
520nc_session_get_path(const struct nc_session *session)
521{
roman40672412023-05-04 11:10:22 +0200522 NC_CHECK_ARG_RET(session, session, NULL);
523
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200524 if (session->ti_type != NC_TI_UNIX) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200525 return NULL;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200526 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200527
528 return session->path;
529}
530
Michal Vasko8dadf782016-01-15 10:29:36 +0100531API uint16_t
532nc_session_get_port(const struct nc_session *session)
533{
roman40672412023-05-04 11:10:22 +0200534 NC_CHECK_ARG_RET(session, session, 0);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100535
Michal Vasko8dadf782016-01-15 10:29:36 +0100536 return session->port;
537}
538
Michal Vasko93224072021-11-09 12:14:28 +0100539API const struct ly_ctx *
Michal Vasko9a25e932016-02-01 10:36:42 +0100540nc_session_get_ctx(const struct nc_session *session)
541{
roman40672412023-05-04 11:10:22 +0200542 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vasko9a25e932016-02-01 10:36:42 +0100543
544 return session->ctx;
545}
546
Michal Vasko2cc4c682016-03-01 09:16:48 +0100547API void
548nc_session_set_data(struct nc_session *session, void *data)
549{
550 if (!session) {
roman40672412023-05-04 11:10:22 +0200551 ERRARG(NULL, "session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100552 return;
553 }
554
555 session->data = data;
556}
557
558API void *
559nc_session_get_data(const struct nc_session *session)
560{
roman40672412023-05-04 11:10:22 +0200561 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vasko2cc4c682016-03-01 09:16:48 +0100562
563 return session->data;
564}
565
Michal Vaskodc96bb92023-03-28 08:52:48 +0200566API int
567nc_session_is_callhome(const struct nc_session *session)
568{
roman40672412023-05-04 11:10:22 +0200569 NC_CHECK_ARG_RET(session, session, 0);
Michal Vaskodc96bb92023-03-28 08:52:48 +0200570
571 if (session->flags & NC_SESSION_CALLHOME) {
572 return 1;
573 }
574
575 return 0;
576}
577
Michal Vasko086311b2016-01-08 09:53:11 +0100578NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200579nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200580{
Michal Vasko7e1f5fb2021-11-10 10:14:45 +0100581 if (session->ctx != LYD_CTX(op)) {
582 ERR(session, "RPC \"%s\" was created in different context than that of the session.", LYD_NAME(op));
Michal Vasko086311b2016-01-08 09:53:11 +0100583 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100584 }
585
Michal Vasko131120a2018-05-29 15:44:02 +0200586 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100587}
588
Michal Vaskod4da3632022-05-25 11:49:10 +0200589/**
590 * @brief Send \<close-session\> and read the reply on a session.
591 *
592 * @param[in] session Closing NETCONF session.
593 */
594static void
595nc_session_free_close_session(struct nc_session *session)
596{
597 struct ly_in *msg;
598 struct lyd_node *close_rpc, *envp;
599 const struct lys_module *ietfnc;
600
601 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
602 if (!ietfnc) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200603 WRN(session, "Missing ietf-netconf module in context, unable to send <close-session>.");
Michal Vaskod4da3632022-05-25 11:49:10 +0200604 return;
605 }
606 if (lyd_new_inner(NULL, ietfnc, "close-session", 0, &close_rpc)) {
607 WRN(session, "Failed to create <close-session> RPC.");
608 return;
609 }
610
611 /* send the RPC */
612 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
613
614read_msg:
615 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &msg)) {
616 case 1:
617 if (!strncmp(ly_in_memory(msg, NULL), "<notification", 13)) {
618 /* ignore */
619 ly_in_free(msg, 1);
620 goto read_msg;
621 }
622 if (lyd_parse_op(session->ctx, close_rpc, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, &envp, NULL)) {
623 WRN(session, "Failed to parse <close-session> reply.");
624 } else if (!lyd_child(envp) || strcmp(LYD_NAME(lyd_child(envp)), "ok")) {
625 WRN(session, "Reply to <close-session> was not <ok> as expected.");
626 }
627 lyd_free_tree(envp);
628 ly_in_free(msg, 1);
629 break;
630 case 0:
631 WRN(session, "Timeout for receiving a reply to <close-session> elapsed.");
632 break;
633 case -1:
634 ERR(session, "Failed to receive a reply to <close-session>.");
635 break;
636 default:
637 /* cannot happen */
638 break;
639 }
640 lyd_free_tree(close_rpc);
641}
642
Michal Vasko33476c32022-09-09 11:21:40 +0200643/**
644 * @brief Free transport implementation members of a session.
645 *
646 * @param[in] session Session to free.
647 * @param[out] multisession Whether there are other NC sessions on the same SSH sessions.
648 */
649static void
650nc_session_free_transport(struct nc_session *session, int *multisession)
651{
652 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskoe44f2702022-12-12 07:58:06 +0100653 int sock = -1;
Michal Vasko33476c32022-09-09 11:21:40 +0200654 struct nc_session *siter;
655
656 *multisession = 0;
657 connected = nc_session_is_connected(session);
658
659 /* transport implementation cleanup */
660 switch (session->ti_type) {
661 case NC_TI_FD:
662 /* nothing needed - file descriptors were provided by caller,
663 * so it is up to the caller to close them correctly
664 * TODO use callbacks
665 */
666 /* just to avoid compiler warning */
667 (void)connected;
668 (void)siter;
669 break;
670
671 case NC_TI_UNIX:
672 sock = session->ti.unixsock.sock;
673 (void)connected;
674 (void)siter;
675 break;
676
677#ifdef NC_ENABLED_SSH
Michal Vaskoe44f2702022-12-12 07:58:06 +0100678 case NC_TI_LIBSSH: {
679 int r;
680
Michal Vasko33476c32022-09-09 11:21:40 +0200681 if (connected) {
Michal Vasko64734402022-09-09 11:22:00 +0200682 ssh_channel_send_eof(session->ti.libssh.channel);
Michal Vasko33476c32022-09-09 11:21:40 +0200683 ssh_channel_free(session->ti.libssh.channel);
684 }
685 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
686 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
687 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
688 */
689 /* SESSION IO LOCK */
690 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
691
692 if (session->ti.libssh.next) {
693 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
694 if (siter->status != NC_STATUS_STARTING) {
695 *multisession = 1;
696 break;
697 }
698 }
699 }
700
701 if (!*multisession) {
702 /* it's not multisession yet, but we still need to free the starting sessions */
703 if (session->ti.libssh.next) {
704 do {
705 siter = session->ti.libssh.next;
706 session->ti.libssh.next = siter->ti.libssh.next;
707
708 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
709 free(siter->username);
710 free(siter->host);
711 if (!(siter->flags & NC_SESSION_SHAREDCTX)) {
712 ly_ctx_destroy((struct ly_ctx *)siter->ctx);
713 }
714
715 free(siter);
716 } while (session->ti.libssh.next != session);
717 }
718 /* remember sock so we can close it */
719 sock = ssh_get_fd(session->ti.libssh.session);
720 if (connected) {
721 ssh_disconnect(session->ti.libssh.session);
722 sock = -1;
723 }
724 ssh_free(session->ti.libssh.session);
725 } else {
726 /* remove the session from the list */
727 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {}
728 if (session->ti.libssh.next == siter) {
729 /* there will be only one session */
730 siter->ti.libssh.next = NULL;
731 } else {
732 /* there are still multiple sessions, keep the ring list */
733 siter->ti.libssh.next = session->ti.libssh.next;
734 }
Michal Vasko33476c32022-09-09 11:21:40 +0200735 }
736
737 /* SESSION IO UNLOCK */
738 if (r == 1) {
739 nc_session_io_unlock(session, __func__);
740 }
741 break;
Michal Vaskoe44f2702022-12-12 07:58:06 +0100742 }
Michal Vasko33476c32022-09-09 11:21:40 +0200743#endif
744
745#ifdef NC_ENABLED_TLS
746 case NC_TI_OPENSSL:
747 /* remember sock so we can close it */
748 sock = SSL_get_fd(session->ti.tls);
749
750 if (connected) {
751 SSL_shutdown(session->ti.tls);
752 }
753 SSL_free(session->ti.tls);
754
755 if (session->side == NC_SERVER) {
756 X509_free(session->opts.server.client_cert);
757 }
758 break;
759#endif
760 case NC_TI_NONE:
761 break;
762 }
763
764 /* close socket separately */
765 if (sock > -1) {
766 close(sock);
767 }
768}
769
Radek Krejci695d4fa2015-10-22 13:23:54 +0200770API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100771nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200772{
Michal Vasko33476c32022-09-09 11:21:40 +0200773 int r, i, rpc_locked = 0, msgs_locked = 0, timeout;
Michal Vaskob48aa812016-01-18 14:13:09 +0100774 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoad611702015-12-03 13:41:51 +0100775 struct nc_msg_cont *contiter;
Michal Vasko77367452021-02-16 16:32:18 +0100776 struct ly_in *msg;
roman6ece9c52022-06-22 09:29:17 +0200777 struct timespec ts;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200778 void *p;
779
Michal Vasko428087d2016-01-14 16:04:28 +0100780 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200781 return;
782 }
783
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200784 /* stop notification threads if any */
785 if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running)) {
786 /* let the threads know they should quit */
787 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 0);
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200788
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200789 /* wait for them */
Michal Vaskod8a74192023-02-06 15:51:50 +0100790 nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200791 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_count)) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200792 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +0100793 if (nc_timeouttime_cur_diff(&ts) < 1) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200794 ERR(session, "Waiting for notification thread exit failed (timed out).");
795 break;
796 }
797 }
Michal Vasko86d357c2016-03-11 13:46:38 +0100798 }
799
Michal Vaskoacf98472021-02-04 15:33:57 +0100800 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200801 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100802 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100803 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200804 } else if (r) {
805 rpc_locked = 1;
Michal Vasko96a28a32021-02-04 15:35:20 +0100806 } else {
807 /* else failed to lock it, too bad */
Michal Vasko05532772021-06-03 12:12:38 +0200808 ERR(session, "Freeing a session while an RPC is being processed.");
Michal Vasko96a28a32021-02-04 15:35:20 +0100809 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200810 }
811
Michal Vasko8c247822020-09-07 13:23:23 +0200812 if (session->side == NC_CLIENT) {
Michal Vasko01130bd2021-08-26 11:47:38 +0200813 timeout = NC_SESSION_FREE_LOCK_TIMEOUT;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200814
Michal Vasko01130bd2021-08-26 11:47:38 +0200815 /* MSGS LOCK */
816 r = nc_session_client_msgs_lock(session, &timeout, __func__);
817 if (r == -1) {
818 return;
819 } else if (r) {
820 msgs_locked = 1;
821 } else {
822 /* else failed to lock it, too bad */
823 ERR(session, "Freeing a session while messages are being received.");
824 }
825
826 /* cleanup message queue */
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200827 for (contiter = session->opts.client.msgs; contiter; ) {
Michal Vasko77367452021-02-16 16:32:18 +0100828 ly_in_free(contiter->msg, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200829
Michal Vaskoad611702015-12-03 13:41:51 +0100830 p = contiter;
831 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200832 free(p);
833 }
834
Michal Vasko01130bd2021-08-26 11:47:38 +0200835 if (msgs_locked) {
836 /* MSGS UNLOCK */
837 nc_session_client_msgs_unlock(session, __func__);
838 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200839
Michal Vasko8c247822020-09-07 13:23:23 +0200840 if (session->status == NC_STATUS_RUNNING) {
Michal Vasko9c6d38c2021-09-03 13:02:53 +0200841 /* receive any leftover messages */
842 while (nc_read_msg_poll_io(session, 0, &msg) == 1) {
843 ly_in_free(msg, 1);
844 }
845
Michal Vasko8c247822020-09-07 13:23:23 +0200846 /* send closing info to the other side */
Michal Vaskod4da3632022-05-25 11:49:10 +0200847 nc_session_free_close_session(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200848 }
849
850 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200851 if (session->opts.client.cpblts) {
852 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200853 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200854 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200855 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200856 }
Michal Vasko78939072022-12-12 07:43:18 +0100857
858 /* LY ext data */
Michal Vaskoe44f2702022-12-12 07:58:06 +0100859#ifdef NC_ENABLED_SSH
860 struct nc_session *siter;
861
Michal Vasko78939072022-12-12 07:43:18 +0100862 if ((session->flags & NC_SESSION_SHAREDCTX) && session->ti.libssh.next) {
863 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
864 if (siter->status != NC_STATUS_STARTING) {
865 /* move LY ext data to this session */
866 assert(!siter->opts.client.ext_data);
867 siter->opts.client.ext_data = session->opts.client.ext_data;
868 session->opts.client.ext_data = NULL;
869 break;
870 }
871 }
Michal Vaskoe44f2702022-12-12 07:58:06 +0100872 } else
873#endif
874 {
Michal Vasko78939072022-12-12 07:43:18 +0100875 lyd_free_siblings(session->opts.client.ext_data);
876 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200877 }
878
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100879 if (session->data && data_free) {
880 data_free(session->data);
881 }
882
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200883 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
884 /* CH LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100885 pthread_mutex_lock(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200886 }
887
Michal Vasko86d357c2016-03-11 13:46:38 +0100888 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200889 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200890
Michal Vaskofeccb312022-03-24 15:24:59 +0100891 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100892 pthread_cond_signal(&session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200893
Michal Vaskod8a74192023-02-06 15:51:50 +0100894 nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vasko0db3db52021-03-03 10:45:42 +0100895
896 /* wait for CH thread to actually wake up and terminate */
897 r = 0;
Michal Vaskofeccb312022-03-24 15:24:59 +0100898 while (!r && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100899 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 +0100900 }
Michal Vasko0db3db52021-03-03 10:45:42 +0100901 if (r) {
Michal Vasko05532772021-06-03 12:12:38 +0200902 ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r));
Michal Vasko3f05a092018-03-13 10:39:49 +0100903 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200904 }
905
Michal Vaskofeccb312022-03-24 15:24:59 +0100906 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
907 /* CH UNLOCK */
908 pthread_mutex_unlock(&session->opts.server.ch_lock);
909 }
910
Radek Krejci695d4fa2015-10-22 13:23:54 +0200911 /* transport implementation cleanup */
Michal Vasko33476c32022-09-09 11:21:40 +0200912 nc_session_free_transport(session, &multisession);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200913
Michal Vasko33476c32022-09-09 11:21:40 +0200914 /* final cleanup */
Michal Vasko93224072021-11-09 12:14:28 +0100915 free(session->username);
916 free(session->host);
917 free(session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200918
Michal Vaskoacf98472021-02-04 15:33:57 +0100919 if (session->side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200920 pthread_mutex_destroy(&session->opts.server.ntf_status_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200921 if (rpc_locked) {
922 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100923 }
Michal Vaskoacf98472021-02-04 15:33:57 +0100924 pthread_mutex_destroy(&session->opts.server.rpc_lock);
925 pthread_cond_destroy(&session->opts.server.rpc_cond);
Michal Vasko131120a2018-05-29 15:44:02 +0200926 }
927
928 if (session->io_lock && !multisession) {
929 pthread_mutex_destroy(session->io_lock);
930 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200931 }
932
933 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko93224072021-11-09 12:14:28 +0100934 ly_ctx_destroy((struct ly_ctx *)session->ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200935 }
936
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200937 if (session->side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100938 /* free CH synchronization structures */
939 pthread_cond_destroy(&session->opts.server.ch_cond);
940 pthread_mutex_destroy(&session->opts.server.ch_lock);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200941 } else {
942 pthread_mutex_destroy(&session->opts.client.msgs_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200943 }
944
Radek Krejci695d4fa2015-10-22 13:23:54 +0200945 free(session);
946}
947
Michal Vasko086311b2016-01-08 09:53:11 +0100948static void
Michal Vasko93224072021-11-09 12:14:28 +0100949add_cpblt(const char *capab, char ***cpblts, int *size, int *count)
Michal Vasko086311b2016-01-08 09:53:11 +0100950{
Radek Krejci658782b2016-12-04 22:04:55 +0100951 size_t len;
952 int i;
953 char *p;
954
955 if (capab) {
956 /* check if already present */
957 p = strchr(capab, '?');
958 if (p) {
959 len = p - capab;
960 } else {
961 len = strlen(capab);
962 }
963 for (i = 0; i < *count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200964 if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) {
Radek Krejci658782b2016-12-04 22:04:55 +0100965 /* already present, do not duplicate it */
966 return;
967 }
968 }
969 }
970
971 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100972 if (*count == *size) {
973 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100974 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
975 if (!(*cpblts)) {
976 ERRMEM;
977 return;
978 }
Michal Vasko086311b2016-01-08 09:53:11 +0100979 }
980
Michal Vasko93224072021-11-09 12:14:28 +0100981 (*cpblts)[*count] = capab ? strdup(capab) : NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100982 ++(*count);
983}
984
Michal Vasko93224072021-11-09 12:14:28 +0100985API char **
986nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +0100987{
Michal Vasko93224072021-11-09 12:14:28 +0100988 char **cpblts;
Michal Vasko77367452021-02-16 16:32:18 +0100989 const struct lys_module *mod;
990 struct lysp_feature *feat;
991 int size = 10, count, features_count = 0, dev_count = 0, str_len, len;
Michal Vasko1440a742021-03-31 11:11:03 +0200992 uint32_t i, u;
Michal Vasko77367452021-02-16 16:32:18 +0100993 LY_ARRAY_COUNT_TYPE v;
Michal Vasko1440a742021-03-31 11:11:03 +0200994 char *yl_content_id;
romanc1d2b092023-02-02 08:58:27 +0100995 uint32_t wd_also_supported;
996 uint32_t wd_basic_mode;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200997
Radek Krejci24a18412018-05-16 15:09:10 +0200998#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +0200999 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001000
roman40672412023-05-04 11:10:22 +02001001 NC_CHECK_ARG_RET(NULL, ctx, NULL);
Michal Vasko4ffa3b22016-05-24 16:36:25 +02001002
Michal Vasko086311b2016-01-08 09:53:11 +01001003 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001004 if (!cpblts) {
1005 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +02001006 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001007 }
Michal Vasko93224072021-11-09 12:14:28 +01001008 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1009 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko086311b2016-01-08 09:53:11 +01001010 count = 2;
1011
1012 /* capabilities */
1013
Michal Vasko77367452021-02-16 16:32:18 +01001014 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf");
Michal Vasko086311b2016-01-08 09:53:11 +01001015 if (mod) {
Michal Vasko77367452021-02-16 16:32:18 +01001016 if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001017 add_cpblt("urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001018 }
Michal Vasko77367452021-02-16 16:32:18 +01001019 if (lys_feature_value(mod, "candidate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001020 add_cpblt("urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko77367452021-02-16 16:32:18 +01001021 if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001022 add_cpblt("urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001023 }
1024 }
Michal Vasko77367452021-02-16 16:32:18 +01001025 if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001026 add_cpblt("urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001027 }
Michal Vasko77367452021-02-16 16:32:18 +01001028 if (lys_feature_value(mod, "validate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001029 add_cpblt("urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001030 }
Michal Vasko77367452021-02-16 16:32:18 +01001031 if (lys_feature_value(mod, "startup") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001032 add_cpblt("urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001033 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001034
1035 /* The URL capability must be set manually using nc_server_set_capability()
1036 * because of the need for supported protocols to be included.
1037 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1038 */
Michal Vasko77367452021-02-16 16:32:18 +01001039 // if (lys_feature_value(mod, "url") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001040 // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
mekleoa8de5e92020-02-13 09:05:56 +01001041 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001042
Michal Vasko77367452021-02-16 16:32:18 +01001043 if (lys_feature_value(mod, "xpath") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001044 add_cpblt("urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001045 }
1046 }
1047
Michal Vasko77367452021-02-16 16:32:18 +01001048 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01001049 if (mod) {
romanc1d2b092023-02-02 08:58:27 +01001050 wd_basic_mode = ATOMIC_LOAD_RELAXED(server_opts.wd_basic_mode);
1051 if (!wd_basic_mode) {
Michal Vasko05532772021-06-03 12:12:38 +02001052 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 +01001053 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001054 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
romanc1d2b092023-02-02 08:58:27 +01001055 switch (wd_basic_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01001056 case NC_WD_ALL:
1057 strcat(str, "?basic-mode=report-all");
1058 break;
1059 case NC_WD_TRIM:
1060 strcat(str, "?basic-mode=trim");
1061 break;
1062 case NC_WD_EXPLICIT:
1063 strcat(str, "?basic-mode=explicit");
1064 break;
1065 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001066 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001067 break;
1068 }
1069
romanc1d2b092023-02-02 08:58:27 +01001070 wd_also_supported = ATOMIC_LOAD_RELAXED(server_opts.wd_also_supported);
1071 if (wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001072 strcat(str, "&also-supported=");
romanc1d2b092023-02-02 08:58:27 +01001073 if (wd_also_supported & NC_WD_ALL) {
Michal Vasko086311b2016-01-08 09:53:11 +01001074 strcat(str, "report-all,");
1075 }
romanc1d2b092023-02-02 08:58:27 +01001076 if (wd_also_supported & NC_WD_ALL_TAG) {
Michal Vasko086311b2016-01-08 09:53:11 +01001077 strcat(str, "report-all-tagged,");
1078 }
romanc1d2b092023-02-02 08:58:27 +01001079 if (wd_also_supported & NC_WD_TRIM) {
Michal Vasko086311b2016-01-08 09:53:11 +01001080 strcat(str, "trim,");
1081 }
romanc1d2b092023-02-02 08:58:27 +01001082 if (wd_also_supported & NC_WD_EXPLICIT) {
Michal Vasko086311b2016-01-08 09:53:11 +01001083 strcat(str, "explicit,");
1084 }
1085 str[strlen(str) - 1] = '\0';
1086
Michal Vasko93224072021-11-09 12:14:28 +01001087 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001088 }
1089 }
1090 }
1091
Radek Krejci658782b2016-12-04 22:04:55 +01001092 /* other capabilities */
1093 for (u = 0; u < server_opts.capabilities_count; u++) {
Michal Vasko93224072021-11-09 12:14:28 +01001094 add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001095 }
1096
1097 /* models */
Michal Vasko1440a742021-03-31 11:11:03 +02001098 u = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001099 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001100 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vasko77367452021-02-16 16:32:18 +01001101 if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) {
Michal Vasko05532772021-06-03 12:12:38 +02001102 ERR(NULL, "Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
Michal Vaskod5ada122020-03-19 18:28:06 +01001103 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001104 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001105
Michal Vasko1440a742021-03-31 11:11:03 +02001106 /* get content-id */
1107 if (server_opts.content_id_clb) {
1108 yl_content_id = server_opts.content_id_clb(server_opts.content_id_data);
1109 if (!yl_content_id) {
1110 ERRMEM;
1111 goto error;
1112 }
1113 } else {
1114 yl_content_id = malloc(11);
1115 if (!yl_content_id) {
1116 ERRMEM;
1117 goto error;
1118 }
1119 sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx));
1120 }
1121
Michal Vasko77367452021-02-16 16:32:18 +01001122 if (!strcmp(mod->revision, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001123 /* new one (capab defined in RFC 8526 section 2) */
Michal Vasko1440a742021-03-31 11:11:03 +02001124 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s",
1125 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001126 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001127 } else {
1128 /* old one (capab defined in RFC 7950 section 5.6.4) */
Michal Vasko1440a742021-03-31 11:11:03 +02001129 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s",
1130 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001131 add_cpblt(str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001132 }
Michal Vasko1440a742021-03-31 11:11:03 +02001133 free(yl_content_id);
Radek Krejci24a18412018-05-16 15:09:10 +02001134 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001135 } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001136 /* skip YANG 1.1 modules */
Radek Krejci24a18412018-05-16 15:09:10 +02001137 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001138 } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001139 /* skip YANG 1.0 modules */
Radek Krejci24a18412018-05-16 15:09:10 +02001140 continue;
1141 }
Michal Vasko086311b2016-01-08 09:53:11 +01001142
Michal Vasko77367452021-02-16 16:32:18 +01001143 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "",
1144 mod->revision ? mod->revision : "");
Radek Krejci24a18412018-05-16 15:09:10 +02001145
Michal Vaskodafdc742020-03-11 16:15:59 +01001146 features_count = 0;
1147 i = 0;
1148 feat = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001149 while ((feat = lysp_feature_next(feat, mod->parsed, &i))) {
Michal Vaskodafdc742020-03-11 16:15:59 +01001150 if (!(feat->flags & LYS_FENABLED)) {
1151 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001152 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001153 if (!features_count) {
1154 strcat(str, "&features=");
1155 str_len += 10;
1156 }
1157 len = strlen(feat->name);
1158 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1159 ERRINT;
1160 break;
1161 }
1162 if (features_count) {
1163 strcat(str, ",");
1164 ++str_len;
1165 }
1166 strcat(str, feat->name);
1167 str_len += len;
1168 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001169 }
Michal Vasko086311b2016-01-08 09:53:11 +01001170
Michal Vasko77367452021-02-16 16:32:18 +01001171 if (mod->deviated_by) {
Radek Krejci24a18412018-05-16 15:09:10 +02001172 strcat(str, "&deviations=");
1173 str_len += 12;
1174 dev_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +01001175 LY_ARRAY_FOR(mod->deviated_by, v) {
1176 len = strlen(mod->deviated_by[v]->name);
1177 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1178 ERRINT;
1179 break;
Radek Krejci24a18412018-05-16 15:09:10 +02001180 }
Michal Vasko77367452021-02-16 16:32:18 +01001181 if (dev_count) {
1182 strcat(str, ",");
1183 ++str_len;
Radek Krejci24a18412018-05-16 15:09:10 +02001184 }
Michal Vasko77367452021-02-16 16:32:18 +01001185 strcat(str, mod->deviated_by[v]->name);
1186 str_len += len;
1187 dev_count++;
Radek Krejci24a18412018-05-16 15:09:10 +02001188 }
1189 }
1190
Michal Vasko93224072021-11-09 12:14:28 +01001191 add_cpblt(str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +02001192 }
Michal Vasko086311b2016-01-08 09:53:11 +01001193
1194 /* ending NULL capability */
Michal Vasko93224072021-11-09 12:14:28 +01001195 add_cpblt(NULL, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001196
1197 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001198
1199error:
Radek Krejcif906e412017-09-22 14:44:45 +02001200 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001201 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001202}
1203
Michal Vasko93224072021-11-09 12:14:28 +01001204API char **
1205nc_server_get_cpblts(const struct ly_ctx *ctx)
Radek Krejci24a18412018-05-16 15:09:10 +02001206{
1207 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1208}
1209
Radek Krejci695d4fa2015-10-22 13:23:54 +02001210static int
Michal Vasko77367452021-02-16 16:32:18 +01001211parse_cpblts(struct lyd_node *capabilities, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001212{
Michal Vasko77367452021-02-16 16:32:18 +01001213 struct lyd_node *iter;
1214 struct lyd_node_opaq *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001215 int ver = -1, i = 0;
1216 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001217
1218 if (list) {
1219 /* get the storage for server's capabilities */
Michal Vasko77367452021-02-16 16:32:18 +01001220 LY_LIST_FOR(lyd_child(capabilities), iter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001221 i++;
1222 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001223 /* last item remains NULL */
1224 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001225 if (!*list) {
1226 ERRMEM;
1227 return -1;
1228 }
1229 i = 0;
1230 }
1231
Michal Vasko77367452021-02-16 16:32:18 +01001232 LY_LIST_FOR(lyd_child(capabilities), iter) {
1233 cpblt = (struct lyd_node_opaq *)iter;
1234
1235 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 +02001236 ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001237 return -1;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001238 }
1239
Michal Vasko156d3272017-04-11 11:46:49 +02001240 /* skip leading/trailing whitespaces */
Michal Vasko77367452021-02-16 16:32:18 +01001241 for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {}
1242 for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {}
1243 if (!cpb_start[0] || (cpb_end == cpblt->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001244 ERR(NULL, "Empty capability \"%s\" received.", cpblt->value);
Michal Vasko156d3272017-04-11 11:46:49 +02001245 return -1;
1246 }
1247
Radek Krejci695d4fa2015-10-22 13:23:54 +02001248 /* detect NETCONF version */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001249 if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001250 ver = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001251 } 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 +02001252 ver = 1;
1253 }
1254
1255 /* store capabilities */
1256 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001257 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1258 if (!(*list)[i]) {
1259 ERRMEM;
1260 return -1;
1261 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001262 i++;
1263 }
1264 }
1265
1266 if (ver == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001267 ERR(NULL, "Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001268 }
1269
1270 return ver;
1271}
1272
1273static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001274nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001275{
Michal Vasko131120a2018-05-29 15:44:02 +02001276 NC_MSG_TYPE ret;
1277 int i, io_timeout;
Michal Vasko93224072021-11-09 12:14:28 +01001278 char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001279 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001280
Michal Vasko131120a2018-05-29 15:44:02 +02001281 if (session->side == NC_CLIENT) {
1282 /* client side hello - send only NETCONF base capabilities */
1283 cpblts = malloc(3 * sizeof *cpblts);
1284 if (!cpblts) {
1285 ERRMEM;
1286 return NC_MSG_ERROR;
1287 }
Michal Vasko93224072021-11-09 12:14:28 +01001288 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1289 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko131120a2018-05-29 15:44:02 +02001290 cpblts[2] = NULL;
1291
1292 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1293 sid = NULL;
1294 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001295 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0);
Michal Vasko5b24b6b2020-12-04 09:29:47 +01001296 if (!cpblts) {
1297 return NC_MSG_ERROR;
1298 }
Michal Vasko131120a2018-05-29 15:44:02 +02001299
1300 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1301 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001302 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001303
Michal Vasko131120a2018-05-29 15:44:02 +02001304 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001305
Michal Vasko086311b2016-01-08 09:53:11 +01001306 for (i = 0; cpblts[i]; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001307 free(cpblts[i]);
Michal Vasko086311b2016-01-08 09:53:11 +01001308 }
1309 free(cpblts);
1310
Michal Vasko131120a2018-05-29 15:44:02 +02001311 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001312}
1313
1314static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001315nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001316{
Michal Vasko77367452021-02-16 16:32:18 +01001317 struct ly_in *msg;
1318 struct lyd_node *hello = NULL, *iter;
1319 struct lyd_node_opaq *node;
1320 int r, ver = -1, flag = 0;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001321 char *str;
Michal Vasko292c5542023-02-01 14:33:17 +01001322 long long id;
Michal Vasko77367452021-02-16 16:32:18 +01001323 NC_MSG_TYPE rc = NC_MSG_HELLO;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001324
Michal Vasko77367452021-02-16 16:32:18 +01001325 r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg);
1326 switch (r) {
1327 case 1:
Radek Krejci695d4fa2015-10-22 13:23:54 +02001328 /* parse <hello> data */
Michal Vasko77367452021-02-16 16:32:18 +01001329 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 +02001330 ERR(session, "Failed to parse server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001331 rc = NC_MSG_ERROR;
1332 goto cleanup;
1333 }
1334
1335 LY_LIST_FOR(lyd_child(hello), iter) {
1336 node = (struct lyd_node_opaq *)iter;
1337
1338 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001339 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001340 } else if (!strcmp(node->name.name, "session-id")) {
1341 if (!node->value || !strlen(node->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001342 ERR(session, "No value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001343 rc = NC_MSG_ERROR;
1344 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001345 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001346 str = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001347 id = strtoll(node->value, &str, 10);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001348 if (*str || (id < 1) || (id > UINT32_MAX)) {
Michal Vasko05532772021-06-03 12:12:38 +02001349 ERR(session, "Invalid value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001350 rc = NC_MSG_ERROR;
1351 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001352 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001353 session->id = (uint32_t)id;
1354 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001355 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001356 ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001357 rc = NC_MSG_ERROR;
1358 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001359 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001360
1361 if (flag) {
1362 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001363 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001364 rc = NC_MSG_ERROR;
1365 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001366 }
1367 flag = 1;
1368
Michal Vasko77367452021-02-16 16:32:18 +01001369 if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) {
1370 rc = NC_MSG_ERROR;
1371 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001372 }
1373 session->version = ver;
1374 }
1375
1376 if (!session->id) {
Michal Vasko05532772021-06-03 12:12:38 +02001377 ERR(session, "Missing <session-id> in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001378 rc = NC_MSG_ERROR;
1379 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001380 }
1381 break;
Michal Vasko77367452021-02-16 16:32:18 +01001382 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001383 ERR(session, "Server <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001384 rc = NC_MSG_WOULDBLOCK;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001385 break;
1386 default:
Michal Vasko77367452021-02-16 16:32:18 +01001387 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001388 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001389 }
1390
Michal Vasko77367452021-02-16 16:32:18 +01001391cleanup:
1392 ly_in_free(msg, 1);
1393 lyd_free_tree(hello);
1394 return rc;
Radek Krejci5686ff72015-10-09 13:33:56 +02001395}
1396
Michal Vasko11d142a2016-01-19 15:58:24 +01001397static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001398nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001399{
Michal Vasko77367452021-02-16 16:32:18 +01001400 struct ly_in *msg;
1401 struct lyd_node *hello = NULL, *iter;
1402 struct lyd_node_opaq *node;
1403 NC_MSG_TYPE rc = NC_MSG_HELLO;
1404 int r, ver = -1, flag = 0, timeout_io;
Michal Vasko11d142a2016-01-19 15:58:24 +01001405
Michal Vasko77367452021-02-16 16:32:18 +01001406 timeout_io = server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000;
1407 r = nc_read_msg_poll_io(session, timeout_io, &msg);
1408 switch (r) {
1409 case 1:
1410 /* parse <hello> data */
1411 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 +02001412 ERR(session, "Failed to parse client <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001413 rc = NC_MSG_ERROR;
1414 goto cleanup;
1415 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001416
Michal Vasko77367452021-02-16 16:32:18 +01001417 /* learn NETCONF version */
1418 LY_LIST_FOR(lyd_child(hello), iter) {
1419 node = (struct lyd_node_opaq *)iter;
1420
1421 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001422 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001423 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001424 ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001425 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001426 goto cleanup;
1427 }
1428
1429 if (flag) {
1430 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001431 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001432 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001433 goto cleanup;
1434 }
1435 flag = 1;
1436
Michal Vasko77367452021-02-16 16:32:18 +01001437 if ((ver = parse_cpblts(&node->node, NULL)) < 0) {
1438 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001439 goto cleanup;
1440 }
1441 session->version = ver;
1442 }
1443 break;
Michal Vasko77367452021-02-16 16:32:18 +01001444 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001445 ERR(session, "Client <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001446 rc = NC_MSG_WOULDBLOCK;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001447 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001448 default:
Michal Vasko77367452021-02-16 16:32:18 +01001449 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001450 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001451 }
1452
1453cleanup:
Michal Vasko77367452021-02-16 16:32:18 +01001454 ly_in_free(msg, 1);
1455 lyd_free_tree(hello);
1456 return rc;
Michal Vasko11d142a2016-01-19 15:58:24 +01001457}
1458
Michal Vasko71090fc2016-05-24 16:37:28 +02001459NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001460nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001461{
Michal Vasko086311b2016-01-08 09:53:11 +01001462 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001463
Michal Vasko131120a2018-05-29 15:44:02 +02001464 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001465 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001466 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001467 }
1468
Michal Vasko11d142a2016-01-19 15:58:24 +01001469 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001470 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001471 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001472 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001473 }
1474
Michal Vasko71090fc2016-05-24 16:37:28 +02001475 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001476}
Michal Vasko086311b2016-01-08 09:53:11 +01001477
Michal Vasko889162e2019-09-06 14:43:37 +02001478#ifdef NC_ENABLED_SSH
1479
Michal Vasko999b64a2019-07-10 16:06:15 +02001480static void
1481nc_ssh_init(void)
1482{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001483#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001484 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1485 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001486#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001487}
1488
1489static void
1490nc_ssh_destroy(void)
1491{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001492#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001493 FIPS_mode_set(0);
1494 CONF_modules_unload(1);
1495 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001496#endif
1497
Michal Vaskoe1e82632019-09-09 09:18:03 +02001498#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1499 ssh_finalize();
1500#endif
1501}
1502
Michal Vasko889162e2019-09-06 14:43:37 +02001503#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001504
Radek Krejci53691be2016-02-22 13:58:37 +01001505#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001506
Michal Vasko770b4362017-02-17 14:44:18 +01001507#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1508
Michal Vaskof0c92c02016-01-29 09:41:45 +01001509struct CRYPTO_dynlock_value {
1510 pthread_mutex_t lock;
1511};
1512
Michal Vaskof0c92c02016-01-29 09:41:45 +01001513static struct CRYPTO_dynlock_value *
1514tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1515{
1516 struct CRYPTO_dynlock_value *value;
1517
1518 value = malloc(sizeof *value);
1519 if (!value) {
1520 ERRMEM;
1521 return NULL;
1522 }
1523 pthread_mutex_init(&value->lock, NULL);
1524
1525 return value;
1526}
1527
1528static void
1529tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1530{
1531 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1532 * I found ignored this fact, what do I know... */
1533 if (mode & CRYPTO_LOCK) {
1534 pthread_mutex_lock(&l->lock);
1535 } else {
1536 pthread_mutex_unlock(&l->lock);
1537 }
1538}
1539
1540static void
1541tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1542{
1543 pthread_mutex_destroy(&l->lock);
1544 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001545}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001546
Michal Vasko770b4362017-02-17 14:44:18 +01001547#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001548
Michal Vasko8f0c0282016-02-29 10:17:14 +01001549#endif /* NC_ENABLED_TLS */
1550
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001551#if defined (NC_ENABLED_TLS) && !defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001552
Michal Vasko770b4362017-02-17 14:44:18 +01001553#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001554static pthread_mutex_t *tls_locks;
1555
1556static void
1557tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1558{
1559 if (mode & CRYPTO_LOCK) {
1560 pthread_mutex_lock(tls_locks + n);
1561 } else {
1562 pthread_mutex_unlock(tls_locks + n);
1563 }
1564}
1565
1566static void
1567tls_thread_id_func(CRYPTO_THREADID *tid)
1568{
1569 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1570}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001571
Michal Vasko770b4362017-02-17 14:44:18 +01001572#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001573
1574static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001575nc_tls_init(void)
1576{
Rosen Penev4f552d62019-06-26 16:10:43 -07001577#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001578 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001579 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001580 SSL_library_init();
1581
Michal Vaskof89948c2018-01-04 09:19:46 +01001582 int i;
1583
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001584 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001585 if (!tls_locks) {
1586 ERRMEM;
1587 return;
1588 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001589 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1590 pthread_mutex_init(tls_locks + i, NULL);
1591 }
1592
Michal Vaskof0c92c02016-01-29 09:41:45 +01001593 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001594 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001595
1596 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1597 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1598 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001599#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001600}
1601
Michal Vasko8f0c0282016-02-29 10:17:14 +01001602static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001603nc_tls_destroy(void)
1604{
Rosen Penev4f552d62019-06-26 16:10:43 -07001605#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001606 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001607 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001608 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001609 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001610 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001611#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001612 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001613#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1614 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001615#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001616
Michal Vaskof89948c2018-01-04 09:19:46 +01001617 int i;
1618
Michal Vaskob6e37262016-02-25 14:49:00 +01001619 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001620 CRYPTO_set_locking_callback(NULL);
1621 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1622 pthread_mutex_destroy(tls_locks + i);
1623 }
1624 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001625
1626 CRYPTO_set_dynlock_create_callback(NULL);
1627 CRYPTO_set_dynlock_lock_callback(NULL);
1628 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001629#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001630}
1631
Michal Vasko8f0c0282016-02-29 10:17:14 +01001632#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001633
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001634#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001635
Michal Vasko8f0c0282016-02-29 10:17:14 +01001636static void
Michal Vasko5e228792016-02-03 15:30:24 +01001637nc_ssh_tls_init(void)
1638{
Rosen Penev4f552d62019-06-26 16:10:43 -07001639#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001640 SSL_load_error_strings();
1641 ERR_load_BIO_strings();
1642 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001643#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001644
1645 nc_ssh_init();
1646
Michal Vaskoe1e82632019-09-09 09:18:03 +02001647#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001648 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1649 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1650 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001651#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001652}
1653
Michal Vasko8f0c0282016-02-29 10:17:14 +01001654static void
Michal Vasko5e228792016-02-03 15:30:24 +01001655nc_ssh_tls_destroy(void)
1656{
Rosen Penev4f552d62019-06-26 16:10:43 -07001657#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001658 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001659# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001660 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001661# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001662 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001663# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001664#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001665
1666 nc_ssh_destroy();
1667
Michal Vaskoe1e82632019-09-09 09:18:03 +02001668#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001669 CRYPTO_set_dynlock_create_callback(NULL);
1670 CRYPTO_set_dynlock_lock_callback(NULL);
1671 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001672#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001673}
1674
Radek Krejci53691be2016-02-22 13:58:37 +01001675#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001676
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001677#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001678
1679API void
1680nc_thread_destroy(void)
1681{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001682 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001683 // CRYPTO_cleanup_all_ex_data();
Michal Vasko8f0c0282016-02-29 10:17:14 +01001684
Michal Vasko770b4362017-02-17 14:44:18 +01001685#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1686 CRYPTO_THREADID crypto_tid;
1687
Michal Vasko8f0c0282016-02-29 10:17:14 +01001688 CRYPTO_THREADID_current(&crypto_tid);
1689 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001690#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001691}
1692
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001693#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1694
1695void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001696nc_init(void)
1697{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001698#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001699 nc_ssh_tls_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001700#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001701 nc_ssh_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001702#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001703 nc_tls_init();
1704#endif
1705}
1706
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001707void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001708nc_destroy(void)
1709{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001710#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001711 nc_ssh_tls_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001712#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001713 nc_ssh_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001714#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001715 nc_tls_destroy();
1716#endif
1717}