blob: 1a7f1e5e523e3cb94e4ef78d21e1f143ac38c2f5 [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
Michal Vaskoddce1212019-05-24 09:58:49 +0200110const char *
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200111nc_keytype2str(NC_SSH_KEY_TYPE type)
Michal Vaskoddce1212019-05-24 09:58:49 +0200112{
113 switch (type) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200114 case NC_SSH_KEY_DSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200115 return "DSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200116 case NC_SSH_KEY_RSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200117 return "RSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200118 case NC_SSH_KEY_ECDSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200119 return "EC";
120 default:
121 break;
122 }
123
124 return NULL;
125}
126
Michal Vaskobe52dc22018-10-17 09:28:17 +0200127int
romanc1d2b092023-02-02 08:58:27 +0100128nc_sock_configure_keepalive(int sock, struct nc_keepalives *ka)
Michal Vaskobe52dc22018-10-17 09:28:17 +0200129{
130 int opt;
131
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200132 opt = ka->enabled;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200133 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200134 ERR(NULL, "Could not set SO_KEEPALIVE option (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200135 return -1;
136 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200137 if (!ka->enabled) {
138 return 0;
139 }
Michal Vaskobe52dc22018-10-17 09:28:17 +0200140
141#ifdef TCP_KEEPIDLE
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200142 opt = ka->idle_time;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200143 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200144 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200145 return -1;
146 }
147#endif
148
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200149#ifdef TCP_KEEPCNT
150 opt = ka->max_probes;
151 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200152 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200153 return -1;
154 }
155#endif
156
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200157#ifdef TCP_KEEPINTVL
158 opt = ka->probe_interval;
159 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &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
165 return 0;
166}
167
Michal Vaskoade892d2017-02-22 13:40:35 +0100168struct nc_session *
Michal Vasko131120a2018-05-29 15:44:02 +0200169nc_new_session(NC_SIDE side, int shared_ti)
Michal Vaskoade892d2017-02-22 13:40:35 +0100170{
171 struct nc_session *sess;
172
173 sess = calloc(1, sizeof *sess);
174 if (!sess) {
175 return NULL;
176 }
177
Michal Vasko131120a2018-05-29 15:44:02 +0200178 sess->side = side;
179
180 if (side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200181 pthread_mutex_init(&sess->opts.server.ntf_status_lock, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100182 pthread_mutex_init(&sess->opts.server.rpc_lock, NULL);
183 pthread_cond_init(&sess->opts.server.rpc_cond, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100184
185 pthread_mutex_init(&sess->opts.server.ch_lock, NULL);
186 pthread_cond_init(&sess->opts.server.ch_cond, NULL);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200187 } else {
188 pthread_mutex_init(&sess->opts.client.msgs_lock, NULL);
Michal Vasko131120a2018-05-29 15:44:02 +0200189 }
190
191 if (!shared_ti) {
192 sess->io_lock = malloc(sizeof *sess->io_lock);
193 if (!sess->io_lock) {
194 goto error;
195 }
196 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100197 }
198
199 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200200
201error:
Michal Vasko131120a2018-05-29 15:44:02 +0200202 free(sess);
203 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100204}
205
Michal Vasko96164bf2016-01-21 15:41:58 +0100206/*
207 * @return 1 - success
208 * 0 - timeout
209 * -1 - error
210 */
211int
Michal Vasko131120a2018-05-29 15:44:02 +0200212nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100213{
214 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100215 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100216
Michal Vasko131120a2018-05-29 15:44:02 +0200217 if (session->side != NC_SERVER) {
218 ERRINT;
219 return -1;
220 }
221
Michal Vasko96164bf2016-01-21 15:41:58 +0100222 if (timeout > 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100223 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100224
Michal Vaskoade892d2017-02-22 13:40:35 +0100225 /* LOCK */
Michal Vaskod8a74192023-02-06 15:51:50 +0100226 ret = pthread_mutex_clocklock(&session->opts.server.rpc_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100227 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100228 while (session->opts.server.rpc_inuse) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100229 ret = pthread_cond_clockwait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock,
230 COMPAT_CLOCK_ID, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100231 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100232 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100233 break;
234 }
235 }
236 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100237 } else if (!timeout) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100238 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100239 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100240 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100241 if (session->opts.server.rpc_inuse) {
242 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100243 return 0;
244 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200245 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100246 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100247 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100248 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100249 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100250 while (session->opts.server.rpc_inuse) {
251 ret = pthread_cond_wait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100252 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100253 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100254 break;
255 }
256 }
257 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100258 }
259
Michal Vaskoade892d2017-02-22 13:40:35 +0100260 if (ret) {
261 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
262 /* timeout */
263 return 0;
264 }
265
Michal Vasko96164bf2016-01-21 15:41:58 +0100266 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200267 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100268 return -1;
269 }
270
271 /* ok */
Michal Vaskoacf98472021-02-04 15:33:57 +0100272 assert(session->opts.server.rpc_inuse == 0);
273 session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100274
275 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100276 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100277 if (ret) {
278 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200279 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100280 return -1;
281 }
282
283 return 1;
284}
285
286int
Michal Vasko131120a2018-05-29 15:44:02 +0200287nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100288{
289 int ret;
290 struct timespec ts_timeout;
291
Michal Vasko131120a2018-05-29 15:44:02 +0200292 if (session->side != NC_SERVER) {
293 ERRINT;
294 return -1;
295 }
296
Michal Vaskoacf98472021-02-04 15:33:57 +0100297 assert(session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100298
299 if (timeout > 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100300 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100301
302 /* LOCK */
Michal Vaskod8a74192023-02-06 15:51:50 +0100303 ret = pthread_mutex_clocklock(&session->opts.server.rpc_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100304 } else if (!timeout) {
305 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100306 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100307 } else { /* timeout == -1 */
308 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100309 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100310 }
311
312 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
313 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200314 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100315 return -1;
316 } else if (ret) {
Michal Vasko69e98752022-12-14 14:20:17 +0100317 WRN(session, "%s: session RPC lock timeout, should not happen.", func);
Michal Vaskoade892d2017-02-22 13:40:35 +0100318 }
319
Michal Vaskoacf98472021-02-04 15:33:57 +0100320 session->opts.server.rpc_inuse = 0;
321 pthread_cond_signal(&session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100322
323 if (!ret) {
324 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100325 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100326 if (ret) {
327 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200328 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100329 return -1;
330 }
331 }
332
Michal Vasko96164bf2016-01-21 15:41:58 +0100333 return 1;
334}
335
Michal Vasko131120a2018-05-29 15:44:02 +0200336int
337nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
338{
339 int ret;
340 struct timespec ts_timeout;
341
342 if (timeout > 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100343 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko131120a2018-05-29 15:44:02 +0200344
Michal Vaskod8a74192023-02-06 15:51:50 +0100345 ret = pthread_mutex_clocklock(session->io_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vasko131120a2018-05-29 15:44:02 +0200346 } else if (!timeout) {
347 ret = pthread_mutex_trylock(session->io_lock);
348 } else { /* timeout == -1 */
Robin Jarry54ea2962018-10-10 10:33:40 +0200349 ret = pthread_mutex_lock(session->io_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200350 }
351
352 if (ret) {
353 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
354 /* timeout */
355 return 0;
356 }
357
358 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200359 ERR(session, "%s: failed to IO lock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200360 return -1;
361 }
362
363 return 1;
364}
365
366int
367nc_session_io_unlock(struct nc_session *session, const char *func)
368{
369 int ret;
370
371 ret = pthread_mutex_unlock(session->io_lock);
372 if (ret) {
373 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200374 ERR(session, "%s: failed to IO unlock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200375 return -1;
376 }
377
378 return 1;
379}
380
Michal Vasko01130bd2021-08-26 11:47:38 +0200381int
382nc_session_client_msgs_lock(struct nc_session *session, int *timeout, const char *func)
383{
384 int ret;
385 int32_t diff_msec;
roman6ece9c52022-06-22 09:29:17 +0200386 struct timespec ts_timeout, ts_start;
Michal Vasko01130bd2021-08-26 11:47:38 +0200387
388 assert(session->side == NC_CLIENT);
389
390 if (*timeout > 0) {
391 /* get current time */
Michal Vaskod8a74192023-02-06 15:51:50 +0100392 nc_timeouttime_get(&ts_start, 0);
Michal Vasko01130bd2021-08-26 11:47:38 +0200393
Michal Vaskod8a74192023-02-06 15:51:50 +0100394 nc_timeouttime_get(&ts_timeout, *timeout);
Michal Vasko01130bd2021-08-26 11:47:38 +0200395
Michal Vaskod8a74192023-02-06 15:51:50 +0100396 ret = pthread_mutex_clocklock(&session->opts.client.msgs_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vasko01130bd2021-08-26 11:47:38 +0200397 if (!ret) {
398 /* update timeout based on what was elapsed */
Michal Vaskod8a74192023-02-06 15:51:50 +0100399 diff_msec = nc_timeouttime_cur_diff(&ts_start);
Michal Vasko01130bd2021-08-26 11:47:38 +0200400 *timeout -= diff_msec;
401 }
402 } else if (!*timeout) {
403 ret = pthread_mutex_trylock(&session->opts.client.msgs_lock);
404 } else { /* timeout == -1 */
405 ret = pthread_mutex_lock(&session->opts.client.msgs_lock);
406 }
407
408 if (ret) {
409 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
410 /* timeout */
411 return 0;
412 }
413
414 /* error */
415 ERR(session, "%s: failed to MSGS lock a session (%s).", func, strerror(ret));
416 return -1;
417 }
418
419 return 1;
420}
421
422int
423nc_session_client_msgs_unlock(struct nc_session *session, const char *func)
424{
425 int ret;
426
427 assert(session->side == NC_CLIENT);
428
429 ret = pthread_mutex_unlock(&session->opts.client.msgs_lock);
430 if (ret) {
431 /* error */
432 ERR(session, "%s: failed to MSGS unlock a session (%s).", func, strerror(ret));
433 return -1;
434 }
435
436 return 1;
437}
438
Michal Vasko8dadf782016-01-15 10:29:36 +0100439API NC_STATUS
440nc_session_get_status(const struct nc_session *session)
441{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100442 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200443 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200444 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100445 }
446
Michal Vasko8dadf782016-01-15 10:29:36 +0100447 return session->status;
448}
449
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100450API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200451nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100452{
453 if (!session) {
454 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200455 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100456 }
457
458 return session->term_reason;
459}
460
Michal Vasko8dadf782016-01-15 10:29:36 +0100461API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200462nc_session_get_killed_by(const struct nc_session *session)
463{
464 if (!session) {
465 ERRARG("session");
466 return 0;
467 }
468
469 return session->killed_by;
470}
471
472API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100473nc_session_get_id(const struct nc_session *session)
474{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100475 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200476 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100477 return 0;
478 }
479
Michal Vasko8dadf782016-01-15 10:29:36 +0100480 return session->id;
481}
482
Michal Vasko174fe8e2016-02-17 15:38:09 +0100483API int
484nc_session_get_version(const struct nc_session *session)
485{
486 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200487 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100488 return -1;
489 }
490
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200491 return session->version == NC_VERSION_10 ? 0 : 1;
Michal Vasko174fe8e2016-02-17 15:38:09 +0100492}
493
Michal Vasko8dadf782016-01-15 10:29:36 +0100494API NC_TRANSPORT_IMPL
495nc_session_get_ti(const struct nc_session *session)
496{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100497 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200498 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100499 return 0;
500 }
501
Michal Vasko8dadf782016-01-15 10:29:36 +0100502 return session->ti_type;
503}
504
505API const char *
506nc_session_get_username(const struct nc_session *session)
507{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100508 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200509 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100510 return NULL;
511 }
512
Michal Vasko8dadf782016-01-15 10:29:36 +0100513 return session->username;
514}
515
516API const char *
517nc_session_get_host(const struct nc_session *session)
518{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100519 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200520 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100521 return NULL;
522 }
523
Michal Vasko8dadf782016-01-15 10:29:36 +0100524 return session->host;
525}
526
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200527API const char *
528nc_session_get_path(const struct nc_session *session)
529{
530 if (!session) {
531 ERRARG("session");
532 return NULL;
533 }
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200534 if (session->ti_type != NC_TI_UNIX) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200535 return NULL;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200536 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200537
538 return session->path;
539}
540
Michal Vasko8dadf782016-01-15 10:29:36 +0100541API uint16_t
542nc_session_get_port(const struct nc_session *session)
543{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100544 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200545 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100546 return 0;
547 }
548
Michal Vasko8dadf782016-01-15 10:29:36 +0100549 return session->port;
550}
551
Michal Vasko93224072021-11-09 12:14:28 +0100552API const struct ly_ctx *
Michal Vasko9a25e932016-02-01 10:36:42 +0100553nc_session_get_ctx(const struct nc_session *session)
554{
555 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200556 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100557 return NULL;
558 }
559
560 return session->ctx;
561}
562
Michal Vasko2cc4c682016-03-01 09:16:48 +0100563API void
564nc_session_set_data(struct nc_session *session, void *data)
565{
566 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200567 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100568 return;
569 }
570
571 session->data = data;
572}
573
574API void *
575nc_session_get_data(const struct nc_session *session)
576{
577 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200578 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100579 return NULL;
580 }
581
582 return session->data;
583}
584
Michal Vaskodc96bb92023-03-28 08:52:48 +0200585API int
586nc_session_is_callhome(const struct nc_session *session)
587{
588 if (!session) {
589 ERRARG("session");
590 return 0;
591 }
592
593 if (session->flags & NC_SESSION_CALLHOME) {
594 return 1;
595 }
596
597 return 0;
598}
599
Michal Vasko086311b2016-01-08 09:53:11 +0100600NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200601nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200602{
Michal Vasko7e1f5fb2021-11-10 10:14:45 +0100603 if (session->ctx != LYD_CTX(op)) {
604 ERR(session, "RPC \"%s\" was created in different context than that of the session.", LYD_NAME(op));
Michal Vasko086311b2016-01-08 09:53:11 +0100605 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100606 }
607
Michal Vasko131120a2018-05-29 15:44:02 +0200608 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100609}
610
Michal Vaskod4da3632022-05-25 11:49:10 +0200611/**
612 * @brief Send \<close-session\> and read the reply on a session.
613 *
614 * @param[in] session Closing NETCONF session.
615 */
616static void
617nc_session_free_close_session(struct nc_session *session)
618{
619 struct ly_in *msg;
620 struct lyd_node *close_rpc, *envp;
621 const struct lys_module *ietfnc;
622
623 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
624 if (!ietfnc) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200625 WRN(session, "Missing ietf-netconf module in context, unable to send <close-session>.");
Michal Vaskod4da3632022-05-25 11:49:10 +0200626 return;
627 }
628 if (lyd_new_inner(NULL, ietfnc, "close-session", 0, &close_rpc)) {
629 WRN(session, "Failed to create <close-session> RPC.");
630 return;
631 }
632
633 /* send the RPC */
634 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
635
636read_msg:
637 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &msg)) {
638 case 1:
639 if (!strncmp(ly_in_memory(msg, NULL), "<notification", 13)) {
640 /* ignore */
641 ly_in_free(msg, 1);
642 goto read_msg;
643 }
644 if (lyd_parse_op(session->ctx, close_rpc, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, &envp, NULL)) {
645 WRN(session, "Failed to parse <close-session> reply.");
646 } else if (!lyd_child(envp) || strcmp(LYD_NAME(lyd_child(envp)), "ok")) {
647 WRN(session, "Reply to <close-session> was not <ok> as expected.");
648 }
649 lyd_free_tree(envp);
650 ly_in_free(msg, 1);
651 break;
652 case 0:
653 WRN(session, "Timeout for receiving a reply to <close-session> elapsed.");
654 break;
655 case -1:
656 ERR(session, "Failed to receive a reply to <close-session>.");
657 break;
658 default:
659 /* cannot happen */
660 break;
661 }
662 lyd_free_tree(close_rpc);
663}
664
Michal Vasko33476c32022-09-09 11:21:40 +0200665/**
666 * @brief Free transport implementation members of a session.
667 *
668 * @param[in] session Session to free.
669 * @param[out] multisession Whether there are other NC sessions on the same SSH sessions.
670 */
671static void
672nc_session_free_transport(struct nc_session *session, int *multisession)
673{
674 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskoe44f2702022-12-12 07:58:06 +0100675 int sock = -1;
Michal Vasko33476c32022-09-09 11:21:40 +0200676 struct nc_session *siter;
677
678 *multisession = 0;
679 connected = nc_session_is_connected(session);
680
681 /* transport implementation cleanup */
682 switch (session->ti_type) {
683 case NC_TI_FD:
684 /* nothing needed - file descriptors were provided by caller,
685 * so it is up to the caller to close them correctly
686 * TODO use callbacks
687 */
688 /* just to avoid compiler warning */
689 (void)connected;
690 (void)siter;
691 break;
692
693 case NC_TI_UNIX:
694 sock = session->ti.unixsock.sock;
695 (void)connected;
696 (void)siter;
697 break;
698
699#ifdef NC_ENABLED_SSH
Michal Vaskoe44f2702022-12-12 07:58:06 +0100700 case NC_TI_LIBSSH: {
701 int r;
702
Michal Vasko33476c32022-09-09 11:21:40 +0200703 if (connected) {
Michal Vasko64734402022-09-09 11:22:00 +0200704 ssh_channel_send_eof(session->ti.libssh.channel);
Michal Vasko33476c32022-09-09 11:21:40 +0200705 ssh_channel_free(session->ti.libssh.channel);
706 }
707 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
708 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
709 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
710 */
711 /* SESSION IO LOCK */
712 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
713
714 if (session->ti.libssh.next) {
715 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
716 if (siter->status != NC_STATUS_STARTING) {
717 *multisession = 1;
718 break;
719 }
720 }
721 }
722
723 if (!*multisession) {
724 /* it's not multisession yet, but we still need to free the starting sessions */
725 if (session->ti.libssh.next) {
726 do {
727 siter = session->ti.libssh.next;
728 session->ti.libssh.next = siter->ti.libssh.next;
729
730 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
731 free(siter->username);
732 free(siter->host);
733 if (!(siter->flags & NC_SESSION_SHAREDCTX)) {
734 ly_ctx_destroy((struct ly_ctx *)siter->ctx);
735 }
736
737 free(siter);
738 } while (session->ti.libssh.next != session);
739 }
740 /* remember sock so we can close it */
741 sock = ssh_get_fd(session->ti.libssh.session);
742 if (connected) {
743 ssh_disconnect(session->ti.libssh.session);
744 sock = -1;
745 }
746 ssh_free(session->ti.libssh.session);
747 } else {
748 /* remove the session from the list */
749 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {}
750 if (session->ti.libssh.next == siter) {
751 /* there will be only one session */
752 siter->ti.libssh.next = NULL;
753 } else {
754 /* there are still multiple sessions, keep the ring list */
755 siter->ti.libssh.next = session->ti.libssh.next;
756 }
Michal Vasko33476c32022-09-09 11:21:40 +0200757 }
758
759 /* SESSION IO UNLOCK */
760 if (r == 1) {
761 nc_session_io_unlock(session, __func__);
762 }
763 break;
Michal Vaskoe44f2702022-12-12 07:58:06 +0100764 }
Michal Vasko33476c32022-09-09 11:21:40 +0200765#endif
766
767#ifdef NC_ENABLED_TLS
768 case NC_TI_OPENSSL:
769 /* remember sock so we can close it */
770 sock = SSL_get_fd(session->ti.tls);
771
772 if (connected) {
773 SSL_shutdown(session->ti.tls);
774 }
775 SSL_free(session->ti.tls);
776
777 if (session->side == NC_SERVER) {
778 X509_free(session->opts.server.client_cert);
779 }
780 break;
781#endif
782 case NC_TI_NONE:
783 break;
784 }
785
786 /* close socket separately */
787 if (sock > -1) {
788 close(sock);
789 }
790}
791
Radek Krejci695d4fa2015-10-22 13:23:54 +0200792API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100793nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200794{
Michal Vasko33476c32022-09-09 11:21:40 +0200795 int r, i, rpc_locked = 0, msgs_locked = 0, timeout;
Michal Vaskob48aa812016-01-18 14:13:09 +0100796 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoad611702015-12-03 13:41:51 +0100797 struct nc_msg_cont *contiter;
Michal Vasko77367452021-02-16 16:32:18 +0100798 struct ly_in *msg;
roman6ece9c52022-06-22 09:29:17 +0200799 struct timespec ts;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200800 void *p;
801
Michal Vasko428087d2016-01-14 16:04:28 +0100802 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200803 return;
804 }
805
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200806 /* stop notification threads if any */
807 if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running)) {
808 /* let the threads know they should quit */
809 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 0);
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200810
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200811 /* wait for them */
Michal Vaskod8a74192023-02-06 15:51:50 +0100812 nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200813 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_count)) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200814 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +0100815 if (nc_timeouttime_cur_diff(&ts) < 1) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200816 ERR(session, "Waiting for notification thread exit failed (timed out).");
817 break;
818 }
819 }
Michal Vasko86d357c2016-03-11 13:46:38 +0100820 }
821
Michal Vaskoacf98472021-02-04 15:33:57 +0100822 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200823 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100824 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100825 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200826 } else if (r) {
827 rpc_locked = 1;
Michal Vasko96a28a32021-02-04 15:35:20 +0100828 } else {
829 /* else failed to lock it, too bad */
Michal Vasko05532772021-06-03 12:12:38 +0200830 ERR(session, "Freeing a session while an RPC is being processed.");
Michal Vasko96a28a32021-02-04 15:35:20 +0100831 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200832 }
833
Michal Vasko8c247822020-09-07 13:23:23 +0200834 if (session->side == NC_CLIENT) {
Michal Vasko01130bd2021-08-26 11:47:38 +0200835 timeout = NC_SESSION_FREE_LOCK_TIMEOUT;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200836
Michal Vasko01130bd2021-08-26 11:47:38 +0200837 /* MSGS LOCK */
838 r = nc_session_client_msgs_lock(session, &timeout, __func__);
839 if (r == -1) {
840 return;
841 } else if (r) {
842 msgs_locked = 1;
843 } else {
844 /* else failed to lock it, too bad */
845 ERR(session, "Freeing a session while messages are being received.");
846 }
847
848 /* cleanup message queue */
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200849 for (contiter = session->opts.client.msgs; contiter; ) {
Michal Vasko77367452021-02-16 16:32:18 +0100850 ly_in_free(contiter->msg, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200851
Michal Vaskoad611702015-12-03 13:41:51 +0100852 p = contiter;
853 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200854 free(p);
855 }
856
Michal Vasko01130bd2021-08-26 11:47:38 +0200857 if (msgs_locked) {
858 /* MSGS UNLOCK */
859 nc_session_client_msgs_unlock(session, __func__);
860 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200861
Michal Vasko8c247822020-09-07 13:23:23 +0200862 if (session->status == NC_STATUS_RUNNING) {
Michal Vasko9c6d38c2021-09-03 13:02:53 +0200863 /* receive any leftover messages */
864 while (nc_read_msg_poll_io(session, 0, &msg) == 1) {
865 ly_in_free(msg, 1);
866 }
867
Michal Vasko8c247822020-09-07 13:23:23 +0200868 /* send closing info to the other side */
Michal Vaskod4da3632022-05-25 11:49:10 +0200869 nc_session_free_close_session(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200870 }
871
872 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200873 if (session->opts.client.cpblts) {
874 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200875 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200876 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200877 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200878 }
Michal Vasko78939072022-12-12 07:43:18 +0100879
880 /* LY ext data */
Michal Vaskoe44f2702022-12-12 07:58:06 +0100881#ifdef NC_ENABLED_SSH
882 struct nc_session *siter;
883
Michal Vasko78939072022-12-12 07:43:18 +0100884 if ((session->flags & NC_SESSION_SHAREDCTX) && session->ti.libssh.next) {
885 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
886 if (siter->status != NC_STATUS_STARTING) {
887 /* move LY ext data to this session */
888 assert(!siter->opts.client.ext_data);
889 siter->opts.client.ext_data = session->opts.client.ext_data;
890 session->opts.client.ext_data = NULL;
891 break;
892 }
893 }
Michal Vaskoe44f2702022-12-12 07:58:06 +0100894 } else
895#endif
896 {
Michal Vasko78939072022-12-12 07:43:18 +0100897 lyd_free_siblings(session->opts.client.ext_data);
898 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200899 }
900
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100901 if (session->data && data_free) {
902 data_free(session->data);
903 }
904
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200905 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
906 /* CH LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100907 pthread_mutex_lock(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200908 }
909
Michal Vasko86d357c2016-03-11 13:46:38 +0100910 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200911 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200912
Michal Vaskofeccb312022-03-24 15:24:59 +0100913 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100914 pthread_cond_signal(&session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200915
Michal Vaskod8a74192023-02-06 15:51:50 +0100916 nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vasko0db3db52021-03-03 10:45:42 +0100917
918 /* wait for CH thread to actually wake up and terminate */
919 r = 0;
Michal Vaskofeccb312022-03-24 15:24:59 +0100920 while (!r && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100921 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 +0100922 }
Michal Vasko0db3db52021-03-03 10:45:42 +0100923 if (r) {
Michal Vasko05532772021-06-03 12:12:38 +0200924 ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r));
Michal Vasko3f05a092018-03-13 10:39:49 +0100925 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200926 }
927
Michal Vaskofeccb312022-03-24 15:24:59 +0100928 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
929 /* CH UNLOCK */
930 pthread_mutex_unlock(&session->opts.server.ch_lock);
931 }
932
Radek Krejci695d4fa2015-10-22 13:23:54 +0200933 /* transport implementation cleanup */
Michal Vasko33476c32022-09-09 11:21:40 +0200934 nc_session_free_transport(session, &multisession);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200935
Michal Vasko33476c32022-09-09 11:21:40 +0200936 /* final cleanup */
Michal Vasko93224072021-11-09 12:14:28 +0100937 free(session->username);
938 free(session->host);
939 free(session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200940
Michal Vaskoacf98472021-02-04 15:33:57 +0100941 if (session->side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200942 pthread_mutex_destroy(&session->opts.server.ntf_status_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200943 if (rpc_locked) {
944 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100945 }
Michal Vaskoacf98472021-02-04 15:33:57 +0100946 pthread_mutex_destroy(&session->opts.server.rpc_lock);
947 pthread_cond_destroy(&session->opts.server.rpc_cond);
Michal Vasko131120a2018-05-29 15:44:02 +0200948 }
949
950 if (session->io_lock && !multisession) {
951 pthread_mutex_destroy(session->io_lock);
952 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200953 }
954
955 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko93224072021-11-09 12:14:28 +0100956 ly_ctx_destroy((struct ly_ctx *)session->ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200957 }
958
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200959 if (session->side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100960 /* free CH synchronization structures */
961 pthread_cond_destroy(&session->opts.server.ch_cond);
962 pthread_mutex_destroy(&session->opts.server.ch_lock);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200963 } else {
964 pthread_mutex_destroy(&session->opts.client.msgs_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200965 }
966
Radek Krejci695d4fa2015-10-22 13:23:54 +0200967 free(session);
968}
969
Michal Vasko086311b2016-01-08 09:53:11 +0100970static void
Michal Vasko93224072021-11-09 12:14:28 +0100971add_cpblt(const char *capab, char ***cpblts, int *size, int *count)
Michal Vasko086311b2016-01-08 09:53:11 +0100972{
Radek Krejci658782b2016-12-04 22:04:55 +0100973 size_t len;
974 int i;
975 char *p;
976
977 if (capab) {
978 /* check if already present */
979 p = strchr(capab, '?');
980 if (p) {
981 len = p - capab;
982 } else {
983 len = strlen(capab);
984 }
985 for (i = 0; i < *count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200986 if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) {
Radek Krejci658782b2016-12-04 22:04:55 +0100987 /* already present, do not duplicate it */
988 return;
989 }
990 }
991 }
992
993 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100994 if (*count == *size) {
995 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100996 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
997 if (!(*cpblts)) {
998 ERRMEM;
999 return;
1000 }
Michal Vasko086311b2016-01-08 09:53:11 +01001001 }
1002
Michal Vasko93224072021-11-09 12:14:28 +01001003 (*cpblts)[*count] = capab ? strdup(capab) : NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001004 ++(*count);
1005}
1006
Michal Vasko93224072021-11-09 12:14:28 +01001007API char **
1008nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +01001009{
Michal Vasko93224072021-11-09 12:14:28 +01001010 char **cpblts;
Michal Vasko77367452021-02-16 16:32:18 +01001011 const struct lys_module *mod;
1012 struct lysp_feature *feat;
1013 int size = 10, count, features_count = 0, dev_count = 0, str_len, len;
Michal Vasko1440a742021-03-31 11:11:03 +02001014 uint32_t i, u;
Michal Vasko77367452021-02-16 16:32:18 +01001015 LY_ARRAY_COUNT_TYPE v;
Michal Vasko1440a742021-03-31 11:11:03 +02001016 char *yl_content_id;
romanc1d2b092023-02-02 08:58:27 +01001017 uint32_t wd_also_supported;
1018 uint32_t wd_basic_mode;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001019
Radek Krejci24a18412018-05-16 15:09:10 +02001020#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +02001021 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001022
Michal Vasko4ffa3b22016-05-24 16:36:25 +02001023 if (!ctx) {
1024 ERRARG("ctx");
1025 return NULL;
1026 }
1027
Michal Vasko086311b2016-01-08 09:53:11 +01001028 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001029 if (!cpblts) {
1030 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +02001031 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001032 }
Michal Vasko93224072021-11-09 12:14:28 +01001033 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1034 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko086311b2016-01-08 09:53:11 +01001035 count = 2;
1036
1037 /* capabilities */
1038
Michal Vasko77367452021-02-16 16:32:18 +01001039 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf");
Michal Vasko086311b2016-01-08 09:53:11 +01001040 if (mod) {
Michal Vasko77367452021-02-16 16:32:18 +01001041 if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001042 add_cpblt("urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001043 }
Michal Vasko77367452021-02-16 16:32:18 +01001044 if (lys_feature_value(mod, "candidate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001045 add_cpblt("urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko77367452021-02-16 16:32:18 +01001046 if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001047 add_cpblt("urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001048 }
1049 }
Michal Vasko77367452021-02-16 16:32:18 +01001050 if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001051 add_cpblt("urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001052 }
Michal Vasko77367452021-02-16 16:32:18 +01001053 if (lys_feature_value(mod, "validate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001054 add_cpblt("urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001055 }
Michal Vasko77367452021-02-16 16:32:18 +01001056 if (lys_feature_value(mod, "startup") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001057 add_cpblt("urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001058 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001059
1060 /* The URL capability must be set manually using nc_server_set_capability()
1061 * because of the need for supported protocols to be included.
1062 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1063 */
Michal Vasko77367452021-02-16 16:32:18 +01001064 // if (lys_feature_value(mod, "url") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001065 // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
mekleoa8de5e92020-02-13 09:05:56 +01001066 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001067
Michal Vasko77367452021-02-16 16:32:18 +01001068 if (lys_feature_value(mod, "xpath") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001069 add_cpblt("urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001070 }
1071 }
1072
Michal Vasko77367452021-02-16 16:32:18 +01001073 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01001074 if (mod) {
romanc1d2b092023-02-02 08:58:27 +01001075 wd_basic_mode = ATOMIC_LOAD_RELAXED(server_opts.wd_basic_mode);
1076 if (!wd_basic_mode) {
Michal Vasko05532772021-06-03 12:12:38 +02001077 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 +01001078 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001079 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
romanc1d2b092023-02-02 08:58:27 +01001080 switch (wd_basic_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01001081 case NC_WD_ALL:
1082 strcat(str, "?basic-mode=report-all");
1083 break;
1084 case NC_WD_TRIM:
1085 strcat(str, "?basic-mode=trim");
1086 break;
1087 case NC_WD_EXPLICIT:
1088 strcat(str, "?basic-mode=explicit");
1089 break;
1090 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001091 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001092 break;
1093 }
1094
romanc1d2b092023-02-02 08:58:27 +01001095 wd_also_supported = ATOMIC_LOAD_RELAXED(server_opts.wd_also_supported);
1096 if (wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001097 strcat(str, "&also-supported=");
romanc1d2b092023-02-02 08:58:27 +01001098 if (wd_also_supported & NC_WD_ALL) {
Michal Vasko086311b2016-01-08 09:53:11 +01001099 strcat(str, "report-all,");
1100 }
romanc1d2b092023-02-02 08:58:27 +01001101 if (wd_also_supported & NC_WD_ALL_TAG) {
Michal Vasko086311b2016-01-08 09:53:11 +01001102 strcat(str, "report-all-tagged,");
1103 }
romanc1d2b092023-02-02 08:58:27 +01001104 if (wd_also_supported & NC_WD_TRIM) {
Michal Vasko086311b2016-01-08 09:53:11 +01001105 strcat(str, "trim,");
1106 }
romanc1d2b092023-02-02 08:58:27 +01001107 if (wd_also_supported & NC_WD_EXPLICIT) {
Michal Vasko086311b2016-01-08 09:53:11 +01001108 strcat(str, "explicit,");
1109 }
1110 str[strlen(str) - 1] = '\0';
1111
Michal Vasko93224072021-11-09 12:14:28 +01001112 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001113 }
1114 }
1115 }
1116
Radek Krejci658782b2016-12-04 22:04:55 +01001117 /* other capabilities */
1118 for (u = 0; u < server_opts.capabilities_count; u++) {
Michal Vasko93224072021-11-09 12:14:28 +01001119 add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001120 }
1121
1122 /* models */
Michal Vasko1440a742021-03-31 11:11:03 +02001123 u = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001124 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001125 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vasko77367452021-02-16 16:32:18 +01001126 if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) {
Michal Vasko05532772021-06-03 12:12:38 +02001127 ERR(NULL, "Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
Michal Vaskod5ada122020-03-19 18:28:06 +01001128 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001129 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001130
Michal Vasko1440a742021-03-31 11:11:03 +02001131 /* get content-id */
1132 if (server_opts.content_id_clb) {
1133 yl_content_id = server_opts.content_id_clb(server_opts.content_id_data);
1134 if (!yl_content_id) {
1135 ERRMEM;
1136 goto error;
1137 }
1138 } else {
1139 yl_content_id = malloc(11);
1140 if (!yl_content_id) {
1141 ERRMEM;
1142 goto error;
1143 }
1144 sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx));
1145 }
1146
Michal Vasko77367452021-02-16 16:32:18 +01001147 if (!strcmp(mod->revision, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001148 /* new one (capab defined in RFC 8526 section 2) */
Michal Vasko1440a742021-03-31 11:11:03 +02001149 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s",
1150 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001151 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001152 } else {
1153 /* old one (capab defined in RFC 7950 section 5.6.4) */
Michal Vasko1440a742021-03-31 11:11:03 +02001154 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s",
1155 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001156 add_cpblt(str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001157 }
Michal Vasko1440a742021-03-31 11:11:03 +02001158 free(yl_content_id);
Radek Krejci24a18412018-05-16 15:09:10 +02001159 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001160 } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001161 /* skip YANG 1.1 modules */
Radek Krejci24a18412018-05-16 15:09:10 +02001162 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001163 } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001164 /* skip YANG 1.0 modules */
Radek Krejci24a18412018-05-16 15:09:10 +02001165 continue;
1166 }
Michal Vasko086311b2016-01-08 09:53:11 +01001167
Michal Vasko77367452021-02-16 16:32:18 +01001168 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "",
1169 mod->revision ? mod->revision : "");
Radek Krejci24a18412018-05-16 15:09:10 +02001170
Michal Vaskodafdc742020-03-11 16:15:59 +01001171 features_count = 0;
1172 i = 0;
1173 feat = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001174 while ((feat = lysp_feature_next(feat, mod->parsed, &i))) {
Michal Vaskodafdc742020-03-11 16:15:59 +01001175 if (!(feat->flags & LYS_FENABLED)) {
1176 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001177 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001178 if (!features_count) {
1179 strcat(str, "&features=");
1180 str_len += 10;
1181 }
1182 len = strlen(feat->name);
1183 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1184 ERRINT;
1185 break;
1186 }
1187 if (features_count) {
1188 strcat(str, ",");
1189 ++str_len;
1190 }
1191 strcat(str, feat->name);
1192 str_len += len;
1193 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001194 }
Michal Vasko086311b2016-01-08 09:53:11 +01001195
Michal Vasko77367452021-02-16 16:32:18 +01001196 if (mod->deviated_by) {
Radek Krejci24a18412018-05-16 15:09:10 +02001197 strcat(str, "&deviations=");
1198 str_len += 12;
1199 dev_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +01001200 LY_ARRAY_FOR(mod->deviated_by, v) {
1201 len = strlen(mod->deviated_by[v]->name);
1202 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1203 ERRINT;
1204 break;
Radek Krejci24a18412018-05-16 15:09:10 +02001205 }
Michal Vasko77367452021-02-16 16:32:18 +01001206 if (dev_count) {
1207 strcat(str, ",");
1208 ++str_len;
Radek Krejci24a18412018-05-16 15:09:10 +02001209 }
Michal Vasko77367452021-02-16 16:32:18 +01001210 strcat(str, mod->deviated_by[v]->name);
1211 str_len += len;
1212 dev_count++;
Radek Krejci24a18412018-05-16 15:09:10 +02001213 }
1214 }
1215
Michal Vasko93224072021-11-09 12:14:28 +01001216 add_cpblt(str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +02001217 }
Michal Vasko086311b2016-01-08 09:53:11 +01001218
1219 /* ending NULL capability */
Michal Vasko93224072021-11-09 12:14:28 +01001220 add_cpblt(NULL, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001221
1222 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001223
1224error:
Radek Krejcif906e412017-09-22 14:44:45 +02001225 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001226 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001227}
1228
Michal Vasko93224072021-11-09 12:14:28 +01001229API char **
1230nc_server_get_cpblts(const struct ly_ctx *ctx)
Radek Krejci24a18412018-05-16 15:09:10 +02001231{
1232 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1233}
1234
Radek Krejci695d4fa2015-10-22 13:23:54 +02001235static int
Michal Vasko77367452021-02-16 16:32:18 +01001236parse_cpblts(struct lyd_node *capabilities, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001237{
Michal Vasko77367452021-02-16 16:32:18 +01001238 struct lyd_node *iter;
1239 struct lyd_node_opaq *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001240 int ver = -1, i = 0;
1241 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001242
1243 if (list) {
1244 /* get the storage for server's capabilities */
Michal Vasko77367452021-02-16 16:32:18 +01001245 LY_LIST_FOR(lyd_child(capabilities), iter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001246 i++;
1247 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001248 /* last item remains NULL */
1249 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001250 if (!*list) {
1251 ERRMEM;
1252 return -1;
1253 }
1254 i = 0;
1255 }
1256
Michal Vasko77367452021-02-16 16:32:18 +01001257 LY_LIST_FOR(lyd_child(capabilities), iter) {
1258 cpblt = (struct lyd_node_opaq *)iter;
1259
1260 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 +02001261 ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001262 return -1;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001263 }
1264
Michal Vasko156d3272017-04-11 11:46:49 +02001265 /* skip leading/trailing whitespaces */
Michal Vasko77367452021-02-16 16:32:18 +01001266 for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {}
1267 for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {}
1268 if (!cpb_start[0] || (cpb_end == cpblt->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001269 ERR(NULL, "Empty capability \"%s\" received.", cpblt->value);
Michal Vasko156d3272017-04-11 11:46:49 +02001270 return -1;
1271 }
1272
Radek Krejci695d4fa2015-10-22 13:23:54 +02001273 /* detect NETCONF version */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001274 if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001275 ver = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001276 } 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 +02001277 ver = 1;
1278 }
1279
1280 /* store capabilities */
1281 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001282 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1283 if (!(*list)[i]) {
1284 ERRMEM;
1285 return -1;
1286 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001287 i++;
1288 }
1289 }
1290
1291 if (ver == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001292 ERR(NULL, "Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001293 }
1294
1295 return ver;
1296}
1297
1298static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001299nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001300{
Michal Vasko131120a2018-05-29 15:44:02 +02001301 NC_MSG_TYPE ret;
1302 int i, io_timeout;
Michal Vasko93224072021-11-09 12:14:28 +01001303 char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001304 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001305
Michal Vasko131120a2018-05-29 15:44:02 +02001306 if (session->side == NC_CLIENT) {
1307 /* client side hello - send only NETCONF base capabilities */
1308 cpblts = malloc(3 * sizeof *cpblts);
1309 if (!cpblts) {
1310 ERRMEM;
1311 return NC_MSG_ERROR;
1312 }
Michal Vasko93224072021-11-09 12:14:28 +01001313 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1314 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko131120a2018-05-29 15:44:02 +02001315 cpblts[2] = NULL;
1316
1317 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1318 sid = NULL;
1319 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001320 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0);
Michal Vasko5b24b6b2020-12-04 09:29:47 +01001321 if (!cpblts) {
1322 return NC_MSG_ERROR;
1323 }
Michal Vasko131120a2018-05-29 15:44:02 +02001324
1325 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1326 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001327 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001328
Michal Vasko131120a2018-05-29 15:44:02 +02001329 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001330
Michal Vasko086311b2016-01-08 09:53:11 +01001331 for (i = 0; cpblts[i]; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001332 free(cpblts[i]);
Michal Vasko086311b2016-01-08 09:53:11 +01001333 }
1334 free(cpblts);
1335
Michal Vasko131120a2018-05-29 15:44:02 +02001336 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001337}
1338
1339static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001340nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001341{
Michal Vasko77367452021-02-16 16:32:18 +01001342 struct ly_in *msg;
1343 struct lyd_node *hello = NULL, *iter;
1344 struct lyd_node_opaq *node;
1345 int r, ver = -1, flag = 0;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001346 char *str;
Michal Vasko292c5542023-02-01 14:33:17 +01001347 long long id;
Michal Vasko77367452021-02-16 16:32:18 +01001348 NC_MSG_TYPE rc = NC_MSG_HELLO;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001349
Michal Vasko77367452021-02-16 16:32:18 +01001350 r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg);
1351 switch (r) {
1352 case 1:
Radek Krejci695d4fa2015-10-22 13:23:54 +02001353 /* parse <hello> data */
Michal Vasko77367452021-02-16 16:32:18 +01001354 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 +02001355 ERR(session, "Failed to parse server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001356 rc = NC_MSG_ERROR;
1357 goto cleanup;
1358 }
1359
1360 LY_LIST_FOR(lyd_child(hello), iter) {
1361 node = (struct lyd_node_opaq *)iter;
1362
1363 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001364 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001365 } else if (!strcmp(node->name.name, "session-id")) {
1366 if (!node->value || !strlen(node->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001367 ERR(session, "No value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001368 rc = NC_MSG_ERROR;
1369 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001370 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001371 str = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001372 id = strtoll(node->value, &str, 10);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001373 if (*str || (id < 1) || (id > UINT32_MAX)) {
Michal Vasko05532772021-06-03 12:12:38 +02001374 ERR(session, "Invalid value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001375 rc = NC_MSG_ERROR;
1376 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001377 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001378 session->id = (uint32_t)id;
1379 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001380 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001381 ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001382 rc = NC_MSG_ERROR;
1383 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001384 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001385
1386 if (flag) {
1387 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001388 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001389 rc = NC_MSG_ERROR;
1390 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001391 }
1392 flag = 1;
1393
Michal Vasko77367452021-02-16 16:32:18 +01001394 if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) {
1395 rc = NC_MSG_ERROR;
1396 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001397 }
1398 session->version = ver;
1399 }
1400
1401 if (!session->id) {
Michal Vasko05532772021-06-03 12:12:38 +02001402 ERR(session, "Missing <session-id> in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001403 rc = NC_MSG_ERROR;
1404 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001405 }
1406 break;
Michal Vasko77367452021-02-16 16:32:18 +01001407 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001408 ERR(session, "Server <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001409 rc = NC_MSG_WOULDBLOCK;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001410 break;
1411 default:
Michal Vasko77367452021-02-16 16:32:18 +01001412 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001413 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001414 }
1415
Michal Vasko77367452021-02-16 16:32:18 +01001416cleanup:
1417 ly_in_free(msg, 1);
1418 lyd_free_tree(hello);
1419 return rc;
Radek Krejci5686ff72015-10-09 13:33:56 +02001420}
1421
Michal Vasko11d142a2016-01-19 15:58:24 +01001422static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001423nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001424{
Michal Vasko77367452021-02-16 16:32:18 +01001425 struct ly_in *msg;
1426 struct lyd_node *hello = NULL, *iter;
1427 struct lyd_node_opaq *node;
1428 NC_MSG_TYPE rc = NC_MSG_HELLO;
1429 int r, ver = -1, flag = 0, timeout_io;
Michal Vasko11d142a2016-01-19 15:58:24 +01001430
Michal Vasko77367452021-02-16 16:32:18 +01001431 timeout_io = server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000;
1432 r = nc_read_msg_poll_io(session, timeout_io, &msg);
1433 switch (r) {
1434 case 1:
1435 /* parse <hello> data */
1436 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 +02001437 ERR(session, "Failed to parse client <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001438 rc = NC_MSG_ERROR;
1439 goto cleanup;
1440 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001441
Michal Vasko77367452021-02-16 16:32:18 +01001442 /* learn NETCONF version */
1443 LY_LIST_FOR(lyd_child(hello), iter) {
1444 node = (struct lyd_node_opaq *)iter;
1445
1446 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001447 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001448 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001449 ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001450 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001451 goto cleanup;
1452 }
1453
1454 if (flag) {
1455 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001456 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001457 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001458 goto cleanup;
1459 }
1460 flag = 1;
1461
Michal Vasko77367452021-02-16 16:32:18 +01001462 if ((ver = parse_cpblts(&node->node, NULL)) < 0) {
1463 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001464 goto cleanup;
1465 }
1466 session->version = ver;
1467 }
1468 break;
Michal Vasko77367452021-02-16 16:32:18 +01001469 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001470 ERR(session, "Client <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001471 rc = NC_MSG_WOULDBLOCK;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001472 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001473 default:
Michal Vasko77367452021-02-16 16:32:18 +01001474 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001475 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001476 }
1477
1478cleanup:
Michal Vasko77367452021-02-16 16:32:18 +01001479 ly_in_free(msg, 1);
1480 lyd_free_tree(hello);
1481 return rc;
Michal Vasko11d142a2016-01-19 15:58:24 +01001482}
1483
Michal Vasko71090fc2016-05-24 16:37:28 +02001484NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001485nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001486{
Michal Vasko086311b2016-01-08 09:53:11 +01001487 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001488
Michal Vasko131120a2018-05-29 15:44:02 +02001489 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001490 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001491 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001492 }
1493
Michal Vasko11d142a2016-01-19 15:58:24 +01001494 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001495 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001496 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001497 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001498 }
1499
Michal Vasko71090fc2016-05-24 16:37:28 +02001500 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001501}
Michal Vasko086311b2016-01-08 09:53:11 +01001502
Michal Vasko889162e2019-09-06 14:43:37 +02001503#ifdef NC_ENABLED_SSH
1504
Michal Vasko999b64a2019-07-10 16:06:15 +02001505static void
1506nc_ssh_init(void)
1507{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001508#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001509 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1510 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001511#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001512}
1513
1514static void
1515nc_ssh_destroy(void)
1516{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001517#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001518 FIPS_mode_set(0);
1519 CONF_modules_unload(1);
1520 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001521#endif
1522
Michal Vaskoe1e82632019-09-09 09:18:03 +02001523#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1524 ssh_finalize();
1525#endif
1526}
1527
Michal Vasko889162e2019-09-06 14:43:37 +02001528#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001529
Radek Krejci53691be2016-02-22 13:58:37 +01001530#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001531
Michal Vasko770b4362017-02-17 14:44:18 +01001532#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1533
Michal Vaskof0c92c02016-01-29 09:41:45 +01001534struct CRYPTO_dynlock_value {
1535 pthread_mutex_t lock;
1536};
1537
Michal Vaskof0c92c02016-01-29 09:41:45 +01001538static struct CRYPTO_dynlock_value *
1539tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1540{
1541 struct CRYPTO_dynlock_value *value;
1542
1543 value = malloc(sizeof *value);
1544 if (!value) {
1545 ERRMEM;
1546 return NULL;
1547 }
1548 pthread_mutex_init(&value->lock, NULL);
1549
1550 return value;
1551}
1552
1553static void
1554tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1555{
1556 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1557 * I found ignored this fact, what do I know... */
1558 if (mode & CRYPTO_LOCK) {
1559 pthread_mutex_lock(&l->lock);
1560 } else {
1561 pthread_mutex_unlock(&l->lock);
1562 }
1563}
1564
1565static void
1566tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1567{
1568 pthread_mutex_destroy(&l->lock);
1569 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001570}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001571
Michal Vasko770b4362017-02-17 14:44:18 +01001572#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001573
Michal Vasko8f0c0282016-02-29 10:17:14 +01001574#endif /* NC_ENABLED_TLS */
1575
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001576#if defined (NC_ENABLED_TLS) && !defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001577
Michal Vasko770b4362017-02-17 14:44:18 +01001578#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001579static pthread_mutex_t *tls_locks;
1580
1581static void
1582tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1583{
1584 if (mode & CRYPTO_LOCK) {
1585 pthread_mutex_lock(tls_locks + n);
1586 } else {
1587 pthread_mutex_unlock(tls_locks + n);
1588 }
1589}
1590
1591static void
1592tls_thread_id_func(CRYPTO_THREADID *tid)
1593{
1594 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1595}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001596
Michal Vasko770b4362017-02-17 14:44:18 +01001597#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001598
1599static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001600nc_tls_init(void)
1601{
Rosen Penev4f552d62019-06-26 16:10:43 -07001602#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001603 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001604 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001605 SSL_library_init();
1606
Michal Vaskof89948c2018-01-04 09:19:46 +01001607 int i;
1608
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001609 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001610 if (!tls_locks) {
1611 ERRMEM;
1612 return;
1613 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001614 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1615 pthread_mutex_init(tls_locks + i, NULL);
1616 }
1617
Michal Vaskof0c92c02016-01-29 09:41:45 +01001618 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001619 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001620
1621 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1622 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1623 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001624#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001625}
1626
Michal Vasko8f0c0282016-02-29 10:17:14 +01001627static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001628nc_tls_destroy(void)
1629{
Rosen Penev4f552d62019-06-26 16:10:43 -07001630#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001631 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001632 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001633 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001634 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001635 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001636#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001637 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001638#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1639 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001640#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001641
Michal Vaskof89948c2018-01-04 09:19:46 +01001642 int i;
1643
Michal Vaskob6e37262016-02-25 14:49:00 +01001644 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001645 CRYPTO_set_locking_callback(NULL);
1646 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1647 pthread_mutex_destroy(tls_locks + i);
1648 }
1649 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001650
1651 CRYPTO_set_dynlock_create_callback(NULL);
1652 CRYPTO_set_dynlock_lock_callback(NULL);
1653 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001654#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001655}
1656
Michal Vasko8f0c0282016-02-29 10:17:14 +01001657#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001658
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001659#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001660
Michal Vasko8f0c0282016-02-29 10:17:14 +01001661static void
Michal Vasko5e228792016-02-03 15:30:24 +01001662nc_ssh_tls_init(void)
1663{
Rosen Penev4f552d62019-06-26 16:10:43 -07001664#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001665 SSL_load_error_strings();
1666 ERR_load_BIO_strings();
1667 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001668#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001669
1670 nc_ssh_init();
1671
Michal Vaskoe1e82632019-09-09 09:18:03 +02001672#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001673 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1674 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1675 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001676#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001677}
1678
Michal Vasko8f0c0282016-02-29 10:17:14 +01001679static void
Michal Vasko5e228792016-02-03 15:30:24 +01001680nc_ssh_tls_destroy(void)
1681{
Rosen Penev4f552d62019-06-26 16:10:43 -07001682#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001683 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001684# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001685 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001686# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001687 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001688# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001689#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001690
1691 nc_ssh_destroy();
1692
Michal Vaskoe1e82632019-09-09 09:18:03 +02001693#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001694 CRYPTO_set_dynlock_create_callback(NULL);
1695 CRYPTO_set_dynlock_lock_callback(NULL);
1696 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001697#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001698}
1699
Radek Krejci53691be2016-02-22 13:58:37 +01001700#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001701
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001702#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001703
1704API void
1705nc_thread_destroy(void)
1706{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001707 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001708 // CRYPTO_cleanup_all_ex_data();
Michal Vasko8f0c0282016-02-29 10:17:14 +01001709
Michal Vasko770b4362017-02-17 14:44:18 +01001710#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1711 CRYPTO_THREADID crypto_tid;
1712
Michal Vasko8f0c0282016-02-29 10:17:14 +01001713 CRYPTO_THREADID_current(&crypto_tid);
1714 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001715#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001716}
1717
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001718#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1719
1720void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001721nc_init(void)
1722{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001723#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001724 nc_ssh_tls_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001725#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001726 nc_ssh_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001727#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001728 nc_tls_init();
1729#endif
1730}
1731
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001732void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001733nc_destroy(void)
1734{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001735#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001736 nc_ssh_tls_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001737#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001738 nc_ssh_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001739#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001740 nc_tls_destroy();
1741#endif
1742}