blob: f9a58b934291ad78a62bebc539c0d26939c8d4f1 [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
Michal Vasko95ea9ff2021-11-09 12:29:14 +01002 * @file session.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libnetconf2 - general session functions
Radek Krejci206fcd62015-10-07 15:42:48 +02005 *
Michal Vasko95ea9ff2021-11-09 12:29:14 +01006 * @copyright
7 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
Radek Krejci206fcd62015-10-07 15:42:48 +02008 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01009 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010012 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010013 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci206fcd62015-10-07 15:42:48 +020014 */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +020015#define _GNU_SOURCE
Radek Krejci206fcd62015-10-07 15:42:48 +020016
Michal Vasko18aeb5d2017-02-17 09:23:56 +010017#include <assert.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020018#include <ctype.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020019#include <errno.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020020#include <libyang/libyang.h>
Michal Vaskobe52dc22018-10-17 09:28:17 +020021#include <netinet/in.h>
22#include <netinet/tcp.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020023#include <pthread.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/socket.h>
27#include <sys/time.h>
28#include <sys/types.h>
Michal Vasko58f31552016-01-19 12:39:15 +010029#include <time.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020030#include <unistd.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020031
Michal Vasko9e8ac262020-04-07 13:06:45 +020032#include "compat.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010033#include "libnetconf.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020034#include "session.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010035#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010036
Radek Krejci53691be2016-02-22 13:58:37 +010037#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020038
Michal Vasko086311b2016-01-08 09:53:11 +010039# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020040
Radek Krejci53691be2016-02-22 13:58:37 +010041#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020042
Michal Vaskob83a3fa2021-05-26 09:53:42 +020043#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010044
Michal Vasko5e228792016-02-03 15:30:24 +010045# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010046# include <openssl/err.h>
47
Radek Krejci53691be2016-02-22 13:58:37 +010048#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010049
Michal Vasko086311b2016-01-08 09:53:11 +010050/* in seconds */
51#define NC_CLIENT_HELLO_TIMEOUT 60
fanchanghu75888b62017-08-01 19:51:20 +080052#define NC_SERVER_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020053
Michal Vasko05ba9df2016-01-13 14:40:27 +010054/* in milliseconds */
55#define NC_CLOSE_REPLY_TIMEOUT 200
56
Michal Vasko086311b2016-01-08 09:53:11 +010057extern struct nc_server_opts server_opts;
58
Radek Krejci7ac16052016-07-15 11:48:18 +020059int
roman6ece9c52022-06-22 09:29:17 +020060nc_gettimespec_real_add(struct timespec *ts, uint32_t msec)
Radek Krejci7ac16052016-07-15 11:48:18 +020061{
Michal Vasko0ef46df2016-09-21 14:03:18 +020062#ifdef CLOCK_REALTIME
roman6ece9c52022-06-22 09:29:17 +020063 if (clock_gettime(CLOCK_REALTIME, ts)) {
64 return -1;
65 }
Radek Krejci7ac16052016-07-15 11:48:18 +020066#else
67 int rc;
68 struct timeval tv;
69
70 rc = gettimeofday(&tv, NULL);
roman6ece9c52022-06-22 09:29:17 +020071 if (rc) {
72 return -1;
73 } else {
Radek Krejci7ac16052016-07-15 11:48:18 +020074 ts->tv_sec = (time_t)tv.tv_sec;
75 ts->tv_nsec = 1000L * (long)tv.tv_usec;
76 }
Radek Krejci7ac16052016-07-15 11:48:18 +020077#endif
Radek Krejci7ac16052016-07-15 11:48:18 +020078
roman6ece9c52022-06-22 09:29:17 +020079 if (!msec) {
80 return 0;
81 }
Michal Vasko99f251b2017-01-11 11:31:46 +010082
Michal Vasko81c5b302017-03-15 12:10:40 +010083 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
84
Michal Vasko0dfcd332017-03-03 13:14:39 +010085 ts->tv_sec += msec / 1000;
86 ts->tv_nsec += (msec % 1000) * 1000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +010087
Michal Vasko81c5b302017-03-15 12:10:40 +010088 if (ts->tv_nsec >= 1000000000L) {
89 ++ts->tv_sec;
90 ts->tv_nsec -= 1000000000L;
91 } else if (ts->tv_nsec < 0) {
92 --ts->tv_sec;
93 ts->tv_nsec += 1000000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +010094 }
Michal Vasko81c5b302017-03-15 12:10:40 +010095
96 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
roman6ece9c52022-06-22 09:29:17 +020097 return 0;
98}
99
100int
101nc_gettimespec_mono_add(struct timespec *ts, uint32_t msec)
102{
roman41a11e42022-06-22 09:27:08 +0200103#ifdef CLOCK_MONOTONIC_RAW
roman6ece9c52022-06-22 09:29:17 +0200104 if (clock_gettime(CLOCK_MONOTONIC_RAW, ts)) {
105 return -1;
106 }
roman41a11e42022-06-22 09:27:08 +0200107#elif defined (CLOCK_MONOTONIC)
roman6ece9c52022-06-22 09:29:17 +0200108 if (clock_gettime(CLOCK_MONOTONIC, ts)) {
109 return -1;
110 }
roman41a11e42022-06-22 09:27:08 +0200111#else
112 /* no monotonic clock available, return real time */
roman6ece9c52022-06-22 09:29:17 +0200113 if (nc_gettimespec_real_add(ts, 0)) {
114 return -1;
115 }
116#endif
117
118 if (!msec) {
119 return 0;
120 }
121
122 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
123
124 ts->tv_sec += msec / 1000;
125 ts->tv_nsec += (msec % 1000) * 1000000L;
126
127 if (ts->tv_nsec >= 1000000000L) {
128 ++ts->tv_sec;
129 ts->tv_nsec -= 1000000000L;
130 } else if (ts->tv_nsec < 0) {
131 --ts->tv_sec;
132 ts->tv_nsec += 1000000000L;
133 }
134
135 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
136 return 0;
137}
138
139int32_t
Michal Vasko60d8ffb2022-07-21 11:08:34 +0200140nc_difftimespec_real_cur(const struct timespec *ts)
141{
142 struct timespec cur;
143 int64_t nsec_diff = 0;
144
145 nc_gettimespec_real_add(&cur, 0);
146
147 nsec_diff += (((int64_t)ts->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
148 nsec_diff += ((int64_t)ts->tv_nsec) - ((int64_t)cur.tv_nsec);
149
150 return nsec_diff / 1000000L;
151}
152
153int32_t
154nc_difftimespec_mono_cur(const struct timespec *ts)
roman6ece9c52022-06-22 09:29:17 +0200155{
156 struct timespec cur;
157 int64_t nsec_diff = 0;
158
159 nc_gettimespec_mono_add(&cur, 0);
160
161 nsec_diff += (((int64_t)ts->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
162 nsec_diff += ((int64_t)ts->tv_nsec) - ((int64_t)cur.tv_nsec);
163
164 return nsec_diff / 1000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100165}
166
Michal Vaskoddce1212019-05-24 09:58:49 +0200167const char *
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200168nc_keytype2str(NC_SSH_KEY_TYPE type)
Michal Vaskoddce1212019-05-24 09:58:49 +0200169{
170 switch (type) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200171 case NC_SSH_KEY_DSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200172 return "DSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200173 case NC_SSH_KEY_RSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200174 return "RSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200175 case NC_SSH_KEY_ECDSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200176 return "EC";
177 default:
178 break;
179 }
180
181 return NULL;
182}
183
Michal Vaskobe52dc22018-10-17 09:28:17 +0200184int
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200185nc_sock_enable_keepalive(int sock, struct nc_keepalives *ka)
Michal Vaskobe52dc22018-10-17 09:28:17 +0200186{
187 int opt;
188
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200189 opt = ka->enabled;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200190 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200191 ERR(NULL, "Could not set SO_KEEPALIVE option (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200192 return -1;
193 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200194 if (!ka->enabled) {
195 return 0;
196 }
Michal Vaskobe52dc22018-10-17 09:28:17 +0200197
198#ifdef TCP_KEEPIDLE
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200199 opt = ka->idle_time;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200200 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200201 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200202 return -1;
203 }
204#endif
205
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200206#ifdef TCP_KEEPCNT
207 opt = ka->max_probes;
208 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200209 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200210 return -1;
211 }
212#endif
213
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200214#ifdef TCP_KEEPINTVL
215 opt = ka->probe_interval;
216 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200217 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200218 return -1;
219 }
220#endif
221
222 return 0;
223}
224
Michal Vaskoade892d2017-02-22 13:40:35 +0100225struct nc_session *
Michal Vasko131120a2018-05-29 15:44:02 +0200226nc_new_session(NC_SIDE side, int shared_ti)
Michal Vaskoade892d2017-02-22 13:40:35 +0100227{
228 struct nc_session *sess;
229
230 sess = calloc(1, sizeof *sess);
231 if (!sess) {
232 return NULL;
233 }
234
Michal Vasko131120a2018-05-29 15:44:02 +0200235 sess->side = side;
236
237 if (side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200238 pthread_mutex_init(&sess->opts.server.ntf_status_lock, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100239 pthread_mutex_init(&sess->opts.server.rpc_lock, NULL);
240 pthread_cond_init(&sess->opts.server.rpc_cond, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100241
242 pthread_mutex_init(&sess->opts.server.ch_lock, NULL);
243 pthread_cond_init(&sess->opts.server.ch_cond, NULL);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200244 } else {
245 pthread_mutex_init(&sess->opts.client.msgs_lock, NULL);
Michal Vasko131120a2018-05-29 15:44:02 +0200246 }
247
248 if (!shared_ti) {
249 sess->io_lock = malloc(sizeof *sess->io_lock);
250 if (!sess->io_lock) {
251 goto error;
252 }
253 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100254 }
255
256 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200257
258error:
Michal Vasko131120a2018-05-29 15:44:02 +0200259 free(sess);
260 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100261}
262
Michal Vasko96164bf2016-01-21 15:41:58 +0100263/*
264 * @return 1 - success
265 * 0 - timeout
266 * -1 - error
267 */
268int
Michal Vasko131120a2018-05-29 15:44:02 +0200269nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100270{
271 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100272 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100273
Michal Vasko131120a2018-05-29 15:44:02 +0200274 if (session->side != NC_SERVER) {
275 ERRINT;
276 return -1;
277 }
278
Michal Vasko96164bf2016-01-21 15:41:58 +0100279 if (timeout > 0) {
roman6ece9c52022-06-22 09:29:17 +0200280 nc_gettimespec_real_add(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100281
Michal Vaskoade892d2017-02-22 13:40:35 +0100282 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100283 ret = pthread_mutex_timedlock(&session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100284 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100285 while (session->opts.server.rpc_inuse) {
286 ret = pthread_cond_timedwait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100287 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100288 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100289 break;
290 }
291 }
292 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100293 } else if (!timeout) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100294 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100295 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100296 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100297 if (session->opts.server.rpc_inuse) {
298 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100299 return 0;
300 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200301 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100302 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100303 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100304 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100305 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100306 while (session->opts.server.rpc_inuse) {
307 ret = pthread_cond_wait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100308 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100309 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100310 break;
311 }
312 }
313 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100314 }
315
Michal Vaskoade892d2017-02-22 13:40:35 +0100316 if (ret) {
317 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
318 /* timeout */
319 return 0;
320 }
321
Michal Vasko96164bf2016-01-21 15:41:58 +0100322 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200323 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100324 return -1;
325 }
326
327 /* ok */
Michal Vaskoacf98472021-02-04 15:33:57 +0100328 assert(session->opts.server.rpc_inuse == 0);
329 session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100330
331 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100332 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100333 if (ret) {
334 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200335 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100336 return -1;
337 }
338
339 return 1;
340}
341
342int
Michal Vasko131120a2018-05-29 15:44:02 +0200343nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100344{
345 int ret;
346 struct timespec ts_timeout;
347
Michal Vasko131120a2018-05-29 15:44:02 +0200348 if (session->side != NC_SERVER) {
349 ERRINT;
350 return -1;
351 }
352
Michal Vaskoacf98472021-02-04 15:33:57 +0100353 assert(session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100354
355 if (timeout > 0) {
roman6ece9c52022-06-22 09:29:17 +0200356 nc_gettimespec_real_add(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100357
358 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100359 ret = pthread_mutex_timedlock(&session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100360 } else if (!timeout) {
361 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100362 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100363 } else { /* timeout == -1 */
364 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100365 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100366 }
367
368 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
369 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200370 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100371 return -1;
372 } else if (ret) {
Michal Vasko05532772021-06-03 12:12:38 +0200373 WRN(session, "%s: session RPC lock timeout, should not happen.");
Michal Vaskoade892d2017-02-22 13:40:35 +0100374 }
375
Michal Vaskoacf98472021-02-04 15:33:57 +0100376 session->opts.server.rpc_inuse = 0;
377 pthread_cond_signal(&session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100378
379 if (!ret) {
380 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100381 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100382 if (ret) {
383 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200384 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100385 return -1;
386 }
387 }
388
Michal Vasko96164bf2016-01-21 15:41:58 +0100389 return 1;
390}
391
Michal Vasko131120a2018-05-29 15:44:02 +0200392int
393nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
394{
395 int ret;
396 struct timespec ts_timeout;
397
398 if (timeout > 0) {
roman6ece9c52022-06-22 09:29:17 +0200399 nc_gettimespec_real_add(&ts_timeout, timeout);
Michal Vasko131120a2018-05-29 15:44:02 +0200400
401 ret = pthread_mutex_timedlock(session->io_lock, &ts_timeout);
402 } else if (!timeout) {
403 ret = pthread_mutex_trylock(session->io_lock);
404 } else { /* timeout == -1 */
Robin Jarry54ea2962018-10-10 10:33:40 +0200405 ret = pthread_mutex_lock(session->io_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200406 }
407
408 if (ret) {
409 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
410 /* timeout */
411 return 0;
412 }
413
414 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200415 ERR(session, "%s: failed to IO lock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200416 return -1;
417 }
418
419 return 1;
420}
421
422int
423nc_session_io_unlock(struct nc_session *session, const char *func)
424{
425 int ret;
426
427 ret = pthread_mutex_unlock(session->io_lock);
428 if (ret) {
429 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200430 ERR(session, "%s: failed to IO unlock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200431 return -1;
432 }
433
434 return 1;
435}
436
Michal Vasko01130bd2021-08-26 11:47:38 +0200437int
438nc_session_client_msgs_lock(struct nc_session *session, int *timeout, const char *func)
439{
440 int ret;
441 int32_t diff_msec;
roman6ece9c52022-06-22 09:29:17 +0200442 struct timespec ts_timeout, ts_start;
Michal Vasko01130bd2021-08-26 11:47:38 +0200443
444 assert(session->side == NC_CLIENT);
445
446 if (*timeout > 0) {
447 /* get current time */
roman6ece9c52022-06-22 09:29:17 +0200448 nc_gettimespec_real_add(&ts_start, 0);
Michal Vasko01130bd2021-08-26 11:47:38 +0200449
roman6ece9c52022-06-22 09:29:17 +0200450 nc_gettimespec_real_add(&ts_timeout, *timeout);
Michal Vasko01130bd2021-08-26 11:47:38 +0200451
452 ret = pthread_mutex_timedlock(&session->opts.client.msgs_lock, &ts_timeout);
453 if (!ret) {
454 /* update timeout based on what was elapsed */
Michal Vasko60d8ffb2022-07-21 11:08:34 +0200455 diff_msec = nc_difftimespec_real_cur(&ts_start);
Michal Vasko01130bd2021-08-26 11:47:38 +0200456 *timeout -= diff_msec;
457 }
458 } else if (!*timeout) {
459 ret = pthread_mutex_trylock(&session->opts.client.msgs_lock);
460 } else { /* timeout == -1 */
461 ret = pthread_mutex_lock(&session->opts.client.msgs_lock);
462 }
463
464 if (ret) {
465 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
466 /* timeout */
467 return 0;
468 }
469
470 /* error */
471 ERR(session, "%s: failed to MSGS lock a session (%s).", func, strerror(ret));
472 return -1;
473 }
474
475 return 1;
476}
477
478int
479nc_session_client_msgs_unlock(struct nc_session *session, const char *func)
480{
481 int ret;
482
483 assert(session->side == NC_CLIENT);
484
485 ret = pthread_mutex_unlock(&session->opts.client.msgs_lock);
486 if (ret) {
487 /* error */
488 ERR(session, "%s: failed to MSGS unlock a session (%s).", func, strerror(ret));
489 return -1;
490 }
491
492 return 1;
493}
494
Michal Vasko8dadf782016-01-15 10:29:36 +0100495API NC_STATUS
496nc_session_get_status(const struct nc_session *session)
497{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100498 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200499 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200500 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100501 }
502
Michal Vasko8dadf782016-01-15 10:29:36 +0100503 return session->status;
504}
505
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100506API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200507nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100508{
509 if (!session) {
510 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200511 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100512 }
513
514 return session->term_reason;
515}
516
Michal Vasko8dadf782016-01-15 10:29:36 +0100517API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200518nc_session_get_killed_by(const struct nc_session *session)
519{
520 if (!session) {
521 ERRARG("session");
522 return 0;
523 }
524
525 return session->killed_by;
526}
527
528API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100529nc_session_get_id(const struct nc_session *session)
530{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100531 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200532 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100533 return 0;
534 }
535
Michal Vasko8dadf782016-01-15 10:29:36 +0100536 return session->id;
537}
538
Michal Vasko174fe8e2016-02-17 15:38:09 +0100539API int
540nc_session_get_version(const struct nc_session *session)
541{
542 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200543 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100544 return -1;
545 }
546
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200547 return session->version == NC_VERSION_10 ? 0 : 1;
Michal Vasko174fe8e2016-02-17 15:38:09 +0100548}
549
Michal Vasko8dadf782016-01-15 10:29:36 +0100550API NC_TRANSPORT_IMPL
551nc_session_get_ti(const struct nc_session *session)
552{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100553 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200554 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100555 return 0;
556 }
557
Michal Vasko8dadf782016-01-15 10:29:36 +0100558 return session->ti_type;
559}
560
561API const char *
562nc_session_get_username(const struct nc_session *session)
563{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100564 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200565 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100566 return NULL;
567 }
568
Michal Vasko8dadf782016-01-15 10:29:36 +0100569 return session->username;
570}
571
572API const char *
573nc_session_get_host(const struct nc_session *session)
574{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100575 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200576 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100577 return NULL;
578 }
579
Michal Vasko8dadf782016-01-15 10:29:36 +0100580 return session->host;
581}
582
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200583API const char *
584nc_session_get_path(const struct nc_session *session)
585{
586 if (!session) {
587 ERRARG("session");
588 return NULL;
589 }
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200590 if (session->ti_type != NC_TI_UNIX) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200591 return NULL;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200592 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200593
594 return session->path;
595}
596
Michal Vasko8dadf782016-01-15 10:29:36 +0100597API uint16_t
598nc_session_get_port(const struct nc_session *session)
599{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100600 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200601 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100602 return 0;
603 }
604
Michal Vasko8dadf782016-01-15 10:29:36 +0100605 return session->port;
606}
607
Michal Vasko93224072021-11-09 12:14:28 +0100608API const struct ly_ctx *
Michal Vasko9a25e932016-02-01 10:36:42 +0100609nc_session_get_ctx(const struct nc_session *session)
610{
611 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200612 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100613 return NULL;
614 }
615
616 return session->ctx;
617}
618
Michal Vasko2cc4c682016-03-01 09:16:48 +0100619API void
620nc_session_set_data(struct nc_session *session, void *data)
621{
622 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200623 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100624 return;
625 }
626
627 session->data = data;
628}
629
630API void *
631nc_session_get_data(const struct nc_session *session)
632{
633 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200634 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100635 return NULL;
636 }
637
638 return session->data;
639}
640
Michal Vasko086311b2016-01-08 09:53:11 +0100641NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200642nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200643{
Michal Vasko7e1f5fb2021-11-10 10:14:45 +0100644 if (session->ctx != LYD_CTX(op)) {
645 ERR(session, "RPC \"%s\" was created in different context than that of the session.", LYD_NAME(op));
Michal Vasko086311b2016-01-08 09:53:11 +0100646 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100647 }
648
Michal Vasko131120a2018-05-29 15:44:02 +0200649 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100650}
651
Michal Vaskod4da3632022-05-25 11:49:10 +0200652/**
653 * @brief Send \<close-session\> and read the reply on a session.
654 *
655 * @param[in] session Closing NETCONF session.
656 */
657static void
658nc_session_free_close_session(struct nc_session *session)
659{
660 struct ly_in *msg;
661 struct lyd_node *close_rpc, *envp;
662 const struct lys_module *ietfnc;
663
664 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
665 if (!ietfnc) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200666 WRN(session, "Missing ietf-netconf module in context, unable to send <close-session>.");
Michal Vaskod4da3632022-05-25 11:49:10 +0200667 return;
668 }
669 if (lyd_new_inner(NULL, ietfnc, "close-session", 0, &close_rpc)) {
670 WRN(session, "Failed to create <close-session> RPC.");
671 return;
672 }
673
674 /* send the RPC */
675 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
676
677read_msg:
678 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &msg)) {
679 case 1:
680 if (!strncmp(ly_in_memory(msg, NULL), "<notification", 13)) {
681 /* ignore */
682 ly_in_free(msg, 1);
683 goto read_msg;
684 }
685 if (lyd_parse_op(session->ctx, close_rpc, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, &envp, NULL)) {
686 WRN(session, "Failed to parse <close-session> reply.");
687 } else if (!lyd_child(envp) || strcmp(LYD_NAME(lyd_child(envp)), "ok")) {
688 WRN(session, "Reply to <close-session> was not <ok> as expected.");
689 }
690 lyd_free_tree(envp);
691 ly_in_free(msg, 1);
692 break;
693 case 0:
694 WRN(session, "Timeout for receiving a reply to <close-session> elapsed.");
695 break;
696 case -1:
697 ERR(session, "Failed to receive a reply to <close-session>.");
698 break;
699 default:
700 /* cannot happen */
701 break;
702 }
703 lyd_free_tree(close_rpc);
704}
705
Michal Vasko33476c32022-09-09 11:21:40 +0200706/**
707 * @brief Free transport implementation members of a session.
708 *
709 * @param[in] session Session to free.
710 * @param[out] multisession Whether there are other NC sessions on the same SSH sessions.
711 */
712static void
713nc_session_free_transport(struct nc_session *session, int *multisession)
714{
715 int connected; /* flag to indicate whether the transport socket is still connected */
716 int sock = -1, r;
717 struct nc_session *siter;
718
719 *multisession = 0;
720 connected = nc_session_is_connected(session);
721
722 /* transport implementation cleanup */
723 switch (session->ti_type) {
724 case NC_TI_FD:
725 /* nothing needed - file descriptors were provided by caller,
726 * so it is up to the caller to close them correctly
727 * TODO use callbacks
728 */
729 /* just to avoid compiler warning */
730 (void)connected;
731 (void)siter;
732 break;
733
734 case NC_TI_UNIX:
735 sock = session->ti.unixsock.sock;
736 (void)connected;
737 (void)siter;
738 break;
739
740#ifdef NC_ENABLED_SSH
741 case NC_TI_LIBSSH:
742 if (connected) {
Michal Vasko64734402022-09-09 11:22:00 +0200743 ssh_channel_send_eof(session->ti.libssh.channel);
Michal Vasko33476c32022-09-09 11:21:40 +0200744 ssh_channel_free(session->ti.libssh.channel);
745 }
746 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
747 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
748 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
749 */
750 /* SESSION IO LOCK */
751 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
752
753 if (session->ti.libssh.next) {
754 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
755 if (siter->status != NC_STATUS_STARTING) {
756 *multisession = 1;
757 break;
758 }
759 }
760 }
761
762 if (!*multisession) {
763 /* it's not multisession yet, but we still need to free the starting sessions */
764 if (session->ti.libssh.next) {
765 do {
766 siter = session->ti.libssh.next;
767 session->ti.libssh.next = siter->ti.libssh.next;
768
769 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
770 free(siter->username);
771 free(siter->host);
772 if (!(siter->flags & NC_SESSION_SHAREDCTX)) {
773 ly_ctx_destroy((struct ly_ctx *)siter->ctx);
774 }
775
776 free(siter);
777 } while (session->ti.libssh.next != session);
778 }
779 /* remember sock so we can close it */
780 sock = ssh_get_fd(session->ti.libssh.session);
781 if (connected) {
782 ssh_disconnect(session->ti.libssh.session);
783 sock = -1;
784 }
785 ssh_free(session->ti.libssh.session);
786 } else {
787 /* remove the session from the list */
788 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {}
789 if (session->ti.libssh.next == siter) {
790 /* there will be only one session */
791 siter->ti.libssh.next = NULL;
792 } else {
793 /* there are still multiple sessions, keep the ring list */
794 siter->ti.libssh.next = session->ti.libssh.next;
795 }
796 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
797 if (session->flags & NC_SESSION_SSH_MSG_CB) {
798 siter = session->ti.libssh.next;
799 while (siter && (siter->status != NC_STATUS_RUNNING)) {
800 if (siter->ti.libssh.next == session) {
801 ERRINT;
802 break;
803 }
804 siter = siter->ti.libssh.next;
805 }
806 /* siter may be NULL in case all the sessions terminated at the same time (socket was disconnected),
807 * we set session to NULL because we do not expect any new message to arrive */
808 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
809 if (siter) {
810 siter->flags |= NC_SESSION_SSH_MSG_CB;
811 }
812 }
813 }
814
815 /* SESSION IO UNLOCK */
816 if (r == 1) {
817 nc_session_io_unlock(session, __func__);
818 }
819 break;
820#endif
821
822#ifdef NC_ENABLED_TLS
823 case NC_TI_OPENSSL:
824 /* remember sock so we can close it */
825 sock = SSL_get_fd(session->ti.tls);
826
827 if (connected) {
828 SSL_shutdown(session->ti.tls);
829 }
830 SSL_free(session->ti.tls);
831
832 if (session->side == NC_SERVER) {
833 X509_free(session->opts.server.client_cert);
834 }
835 break;
836#endif
837 case NC_TI_NONE:
838 break;
839 }
840
841 /* close socket separately */
842 if (sock > -1) {
843 close(sock);
844 }
845}
846
Radek Krejci695d4fa2015-10-22 13:23:54 +0200847API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100848nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200849{
Michal Vasko33476c32022-09-09 11:21:40 +0200850 int r, i, rpc_locked = 0, msgs_locked = 0, timeout;
Michal Vaskob48aa812016-01-18 14:13:09 +0100851 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoad611702015-12-03 13:41:51 +0100852 struct nc_msg_cont *contiter;
Michal Vasko77367452021-02-16 16:32:18 +0100853 struct ly_in *msg;
roman6ece9c52022-06-22 09:29:17 +0200854 struct timespec ts;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200855 void *p;
856
Michal Vasko428087d2016-01-14 16:04:28 +0100857 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200858 return;
859 }
860
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200861 /* stop notifications thread if any */
862 if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
863 /* let the thread know it should quit */
864 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 2);
865
866 /* wait for it */
roman6ece9c52022-06-22 09:29:17 +0200867 nc_gettimespec_mono_add(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200868 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
869 usleep(NC_TIMEOUT_STEP);
Michal Vasko60d8ffb2022-07-21 11:08:34 +0200870 if (nc_difftimespec_mono_cur(&ts) < 1) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200871 ERR(session, "Waiting for notification thread exit failed (timed out).");
872 break;
873 }
874 }
Michal Vasko86d357c2016-03-11 13:46:38 +0100875 }
876
Michal Vaskoacf98472021-02-04 15:33:57 +0100877 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200878 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100879 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100880 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200881 } else if (r) {
882 rpc_locked = 1;
Michal Vasko96a28a32021-02-04 15:35:20 +0100883 } else {
884 /* else failed to lock it, too bad */
Michal Vasko05532772021-06-03 12:12:38 +0200885 ERR(session, "Freeing a session while an RPC is being processed.");
Michal Vasko96a28a32021-02-04 15:35:20 +0100886 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200887 }
888
Michal Vasko8c247822020-09-07 13:23:23 +0200889 if (session->side == NC_CLIENT) {
Michal Vasko01130bd2021-08-26 11:47:38 +0200890 timeout = NC_SESSION_FREE_LOCK_TIMEOUT;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200891
Michal Vasko01130bd2021-08-26 11:47:38 +0200892 /* MSGS LOCK */
893 r = nc_session_client_msgs_lock(session, &timeout, __func__);
894 if (r == -1) {
895 return;
896 } else if (r) {
897 msgs_locked = 1;
898 } else {
899 /* else failed to lock it, too bad */
900 ERR(session, "Freeing a session while messages are being received.");
901 }
902
903 /* cleanup message queue */
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200904 for (contiter = session->opts.client.msgs; contiter; ) {
Michal Vasko77367452021-02-16 16:32:18 +0100905 ly_in_free(contiter->msg, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200906
Michal Vaskoad611702015-12-03 13:41:51 +0100907 p = contiter;
908 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200909 free(p);
910 }
911
Michal Vasko01130bd2021-08-26 11:47:38 +0200912 if (msgs_locked) {
913 /* MSGS UNLOCK */
914 nc_session_client_msgs_unlock(session, __func__);
915 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200916
Michal Vasko8c247822020-09-07 13:23:23 +0200917 if (session->status == NC_STATUS_RUNNING) {
Michal Vasko9c6d38c2021-09-03 13:02:53 +0200918 /* receive any leftover messages */
919 while (nc_read_msg_poll_io(session, 0, &msg) == 1) {
920 ly_in_free(msg, 1);
921 }
922
Michal Vasko8c247822020-09-07 13:23:23 +0200923 /* send closing info to the other side */
Michal Vaskod4da3632022-05-25 11:49:10 +0200924 nc_session_free_close_session(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200925 }
926
927 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200928 if (session->opts.client.cpblts) {
929 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200930 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200931 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200932 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200933 }
934 }
935
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100936 if (session->data && data_free) {
937 data_free(session->data);
938 }
939
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200940 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
941 /* CH LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100942 pthread_mutex_lock(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200943 }
944
Michal Vasko86d357c2016-03-11 13:46:38 +0100945 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200946 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200947
Michal Vaskofeccb312022-03-24 15:24:59 +0100948 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100949 pthread_cond_signal(&session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200950
roman6ece9c52022-06-22 09:29:17 +0200951 nc_gettimespec_real_add(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vasko0db3db52021-03-03 10:45:42 +0100952
953 /* wait for CH thread to actually wake up and terminate */
954 r = 0;
Michal Vaskofeccb312022-03-24 15:24:59 +0100955 while (!r && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vasko0db3db52021-03-03 10:45:42 +0100956 r = pthread_cond_timedwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, &ts);
957 }
Michal Vasko0db3db52021-03-03 10:45:42 +0100958 if (r) {
Michal Vasko05532772021-06-03 12:12:38 +0200959 ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r));
Michal Vasko3f05a092018-03-13 10:39:49 +0100960 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200961 }
962
Michal Vaskofeccb312022-03-24 15:24:59 +0100963 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
964 /* CH UNLOCK */
965 pthread_mutex_unlock(&session->opts.server.ch_lock);
966 }
967
Radek Krejci695d4fa2015-10-22 13:23:54 +0200968 /* transport implementation cleanup */
Michal Vasko33476c32022-09-09 11:21:40 +0200969 nc_session_free_transport(session, &multisession);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200970
Michal Vasko33476c32022-09-09 11:21:40 +0200971 /* final cleanup */
Michal Vasko93224072021-11-09 12:14:28 +0100972 free(session->username);
973 free(session->host);
974 free(session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200975
Michal Vaskoacf98472021-02-04 15:33:57 +0100976 if (session->side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200977 pthread_mutex_destroy(&session->opts.server.ntf_status_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200978 if (rpc_locked) {
979 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100980 }
Michal Vaskoacf98472021-02-04 15:33:57 +0100981 pthread_mutex_destroy(&session->opts.server.rpc_lock);
982 pthread_cond_destroy(&session->opts.server.rpc_cond);
Michal Vasko131120a2018-05-29 15:44:02 +0200983 }
984
985 if (session->io_lock && !multisession) {
986 pthread_mutex_destroy(session->io_lock);
987 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200988 }
989
990 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko93224072021-11-09 12:14:28 +0100991 ly_ctx_destroy((struct ly_ctx *)session->ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200992 }
993
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200994 if (session->side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100995 /* free CH synchronization structures */
996 pthread_cond_destroy(&session->opts.server.ch_cond);
997 pthread_mutex_destroy(&session->opts.server.ch_lock);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200998 } else {
999 pthread_mutex_destroy(&session->opts.client.msgs_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001000 }
1001
Radek Krejci695d4fa2015-10-22 13:23:54 +02001002 free(session);
1003}
1004
Michal Vasko086311b2016-01-08 09:53:11 +01001005static void
Michal Vasko93224072021-11-09 12:14:28 +01001006add_cpblt(const char *capab, char ***cpblts, int *size, int *count)
Michal Vasko086311b2016-01-08 09:53:11 +01001007{
Radek Krejci658782b2016-12-04 22:04:55 +01001008 size_t len;
1009 int i;
1010 char *p;
1011
1012 if (capab) {
1013 /* check if already present */
1014 p = strchr(capab, '?');
1015 if (p) {
1016 len = p - capab;
1017 } else {
1018 len = strlen(capab);
1019 }
1020 for (i = 0; i < *count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001021 if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) {
Radek Krejci658782b2016-12-04 22:04:55 +01001022 /* already present, do not duplicate it */
1023 return;
1024 }
1025 }
1026 }
1027
1028 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +01001029 if (*count == *size) {
1030 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001031 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
1032 if (!(*cpblts)) {
1033 ERRMEM;
1034 return;
1035 }
Michal Vasko086311b2016-01-08 09:53:11 +01001036 }
1037
Michal Vasko93224072021-11-09 12:14:28 +01001038 (*cpblts)[*count] = capab ? strdup(capab) : NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001039 ++(*count);
1040}
1041
Michal Vasko93224072021-11-09 12:14:28 +01001042API char **
1043nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +01001044{
Michal Vasko93224072021-11-09 12:14:28 +01001045 char **cpblts;
Michal Vasko77367452021-02-16 16:32:18 +01001046 const struct lys_module *mod;
1047 struct lysp_feature *feat;
1048 int size = 10, count, features_count = 0, dev_count = 0, str_len, len;
Michal Vasko1440a742021-03-31 11:11:03 +02001049 uint32_t i, u;
Michal Vasko77367452021-02-16 16:32:18 +01001050 LY_ARRAY_COUNT_TYPE v;
Michal Vasko1440a742021-03-31 11:11:03 +02001051 char *yl_content_id;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001052
Radek Krejci24a18412018-05-16 15:09:10 +02001053#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +02001054 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001055
Michal Vasko4ffa3b22016-05-24 16:36:25 +02001056 if (!ctx) {
1057 ERRARG("ctx");
1058 return NULL;
1059 }
1060
Michal Vasko086311b2016-01-08 09:53:11 +01001061 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001062 if (!cpblts) {
1063 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +02001064 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001065 }
Michal Vasko93224072021-11-09 12:14:28 +01001066 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1067 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko086311b2016-01-08 09:53:11 +01001068 count = 2;
1069
1070 /* capabilities */
1071
Michal Vasko77367452021-02-16 16:32:18 +01001072 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf");
Michal Vasko086311b2016-01-08 09:53:11 +01001073 if (mod) {
Michal Vasko77367452021-02-16 16:32:18 +01001074 if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001075 add_cpblt("urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001076 }
Michal Vasko77367452021-02-16 16:32:18 +01001077 if (lys_feature_value(mod, "candidate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001078 add_cpblt("urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko77367452021-02-16 16:32:18 +01001079 if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001080 add_cpblt("urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001081 }
1082 }
Michal Vasko77367452021-02-16 16:32:18 +01001083 if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001084 add_cpblt("urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001085 }
Michal Vasko77367452021-02-16 16:32:18 +01001086 if (lys_feature_value(mod, "validate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001087 add_cpblt("urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001088 }
Michal Vasko77367452021-02-16 16:32:18 +01001089 if (lys_feature_value(mod, "startup") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001090 add_cpblt("urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001091 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001092
1093 /* The URL capability must be set manually using nc_server_set_capability()
1094 * because of the need for supported protocols to be included.
1095 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1096 */
Michal Vasko77367452021-02-16 16:32:18 +01001097 // if (lys_feature_value(mod, "url") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001098 // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
mekleoa8de5e92020-02-13 09:05:56 +01001099 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001100
Michal Vasko77367452021-02-16 16:32:18 +01001101 if (lys_feature_value(mod, "xpath") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001102 add_cpblt("urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001103 }
1104 }
1105
Michal Vasko77367452021-02-16 16:32:18 +01001106 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01001107 if (mod) {
1108 if (!server_opts.wd_basic_mode) {
Michal Vasko05532772021-06-03 12:12:38 +02001109 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 +01001110 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001111 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +01001112 switch (server_opts.wd_basic_mode) {
1113 case NC_WD_ALL:
1114 strcat(str, "?basic-mode=report-all");
1115 break;
1116 case NC_WD_TRIM:
1117 strcat(str, "?basic-mode=trim");
1118 break;
1119 case NC_WD_EXPLICIT:
1120 strcat(str, "?basic-mode=explicit");
1121 break;
1122 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001123 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001124 break;
1125 }
1126
1127 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001128 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +01001129 if (server_opts.wd_also_supported & NC_WD_ALL) {
1130 strcat(str, "report-all,");
1131 }
1132 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
1133 strcat(str, "report-all-tagged,");
1134 }
1135 if (server_opts.wd_also_supported & NC_WD_TRIM) {
1136 strcat(str, "trim,");
1137 }
1138 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
1139 strcat(str, "explicit,");
1140 }
1141 str[strlen(str) - 1] = '\0';
1142
Michal Vasko93224072021-11-09 12:14:28 +01001143 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001144 }
1145 }
1146 }
1147
Radek Krejci658782b2016-12-04 22:04:55 +01001148 /* other capabilities */
1149 for (u = 0; u < server_opts.capabilities_count; u++) {
Michal Vasko93224072021-11-09 12:14:28 +01001150 add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001151 }
1152
1153 /* models */
Michal Vasko1440a742021-03-31 11:11:03 +02001154 u = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001155 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001156 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vasko77367452021-02-16 16:32:18 +01001157 if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) {
Michal Vasko05532772021-06-03 12:12:38 +02001158 ERR(NULL, "Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
Michal Vaskod5ada122020-03-19 18:28:06 +01001159 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001160 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001161
Michal Vasko1440a742021-03-31 11:11:03 +02001162 /* get content-id */
1163 if (server_opts.content_id_clb) {
1164 yl_content_id = server_opts.content_id_clb(server_opts.content_id_data);
1165 if (!yl_content_id) {
1166 ERRMEM;
1167 goto error;
1168 }
1169 } else {
1170 yl_content_id = malloc(11);
1171 if (!yl_content_id) {
1172 ERRMEM;
1173 goto error;
1174 }
1175 sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx));
1176 }
1177
Michal Vasko77367452021-02-16 16:32:18 +01001178 if (!strcmp(mod->revision, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001179 /* new one (capab defined in RFC 8526 section 2) */
Michal Vasko1440a742021-03-31 11:11:03 +02001180 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s",
1181 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001182 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001183 } else {
1184 /* old one (capab defined in RFC 7950 section 5.6.4) */
Michal Vasko1440a742021-03-31 11:11:03 +02001185 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s",
1186 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001187 add_cpblt(str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001188 }
Michal Vasko1440a742021-03-31 11:11:03 +02001189 free(yl_content_id);
Radek Krejci24a18412018-05-16 15:09:10 +02001190 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001191 } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001192 /* skip YANG 1.1 modules */
Radek Krejci24a18412018-05-16 15:09:10 +02001193 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001194 } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001195 /* skip YANG 1.0 modules */
Radek Krejci24a18412018-05-16 15:09:10 +02001196 continue;
1197 }
Michal Vasko086311b2016-01-08 09:53:11 +01001198
Michal Vasko77367452021-02-16 16:32:18 +01001199 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "",
1200 mod->revision ? mod->revision : "");
Radek Krejci24a18412018-05-16 15:09:10 +02001201
Michal Vaskodafdc742020-03-11 16:15:59 +01001202 features_count = 0;
1203 i = 0;
1204 feat = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001205 while ((feat = lysp_feature_next(feat, mod->parsed, &i))) {
Michal Vaskodafdc742020-03-11 16:15:59 +01001206 if (!(feat->flags & LYS_FENABLED)) {
1207 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001208 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001209 if (!features_count) {
1210 strcat(str, "&features=");
1211 str_len += 10;
1212 }
1213 len = strlen(feat->name);
1214 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1215 ERRINT;
1216 break;
1217 }
1218 if (features_count) {
1219 strcat(str, ",");
1220 ++str_len;
1221 }
1222 strcat(str, feat->name);
1223 str_len += len;
1224 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001225 }
Michal Vasko086311b2016-01-08 09:53:11 +01001226
Michal Vasko77367452021-02-16 16:32:18 +01001227 if (mod->deviated_by) {
Radek Krejci24a18412018-05-16 15:09:10 +02001228 strcat(str, "&deviations=");
1229 str_len += 12;
1230 dev_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +01001231 LY_ARRAY_FOR(mod->deviated_by, v) {
1232 len = strlen(mod->deviated_by[v]->name);
1233 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1234 ERRINT;
1235 break;
Radek Krejci24a18412018-05-16 15:09:10 +02001236 }
Michal Vasko77367452021-02-16 16:32:18 +01001237 if (dev_count) {
1238 strcat(str, ",");
1239 ++str_len;
Radek Krejci24a18412018-05-16 15:09:10 +02001240 }
Michal Vasko77367452021-02-16 16:32:18 +01001241 strcat(str, mod->deviated_by[v]->name);
1242 str_len += len;
1243 dev_count++;
Radek Krejci24a18412018-05-16 15:09:10 +02001244 }
1245 }
1246
Michal Vasko93224072021-11-09 12:14:28 +01001247 add_cpblt(str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +02001248 }
Michal Vasko086311b2016-01-08 09:53:11 +01001249
1250 /* ending NULL capability */
Michal Vasko93224072021-11-09 12:14:28 +01001251 add_cpblt(NULL, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001252
1253 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001254
1255error:
Radek Krejcif906e412017-09-22 14:44:45 +02001256 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001257 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001258}
1259
Michal Vasko93224072021-11-09 12:14:28 +01001260API char **
1261nc_server_get_cpblts(const struct ly_ctx *ctx)
Radek Krejci24a18412018-05-16 15:09:10 +02001262{
1263 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1264}
1265
Radek Krejci695d4fa2015-10-22 13:23:54 +02001266static int
Michal Vasko77367452021-02-16 16:32:18 +01001267parse_cpblts(struct lyd_node *capabilities, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001268{
Michal Vasko77367452021-02-16 16:32:18 +01001269 struct lyd_node *iter;
1270 struct lyd_node_opaq *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001271 int ver = -1, i = 0;
1272 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001273
1274 if (list) {
1275 /* get the storage for server's capabilities */
Michal Vasko77367452021-02-16 16:32:18 +01001276 LY_LIST_FOR(lyd_child(capabilities), iter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001277 i++;
1278 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001279 /* last item remains NULL */
1280 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001281 if (!*list) {
1282 ERRMEM;
1283 return -1;
1284 }
1285 i = 0;
1286 }
1287
Michal Vasko77367452021-02-16 16:32:18 +01001288 LY_LIST_FOR(lyd_child(capabilities), iter) {
1289 cpblt = (struct lyd_node_opaq *)iter;
1290
1291 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 +02001292 ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001293 return -1;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001294 }
1295
Michal Vasko156d3272017-04-11 11:46:49 +02001296 /* skip leading/trailing whitespaces */
Michal Vasko77367452021-02-16 16:32:18 +01001297 for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {}
1298 for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {}
1299 if (!cpb_start[0] || (cpb_end == cpblt->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001300 ERR(NULL, "Empty capability \"%s\" received.", cpblt->value);
Michal Vasko156d3272017-04-11 11:46:49 +02001301 return -1;
1302 }
1303
Radek Krejci695d4fa2015-10-22 13:23:54 +02001304 /* detect NETCONF version */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001305 if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001306 ver = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001307 } 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 +02001308 ver = 1;
1309 }
1310
1311 /* store capabilities */
1312 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001313 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1314 if (!(*list)[i]) {
1315 ERRMEM;
1316 return -1;
1317 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001318 i++;
1319 }
1320 }
1321
1322 if (ver == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001323 ERR(NULL, "Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001324 }
1325
1326 return ver;
1327}
1328
1329static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001330nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001331{
Michal Vasko131120a2018-05-29 15:44:02 +02001332 NC_MSG_TYPE ret;
1333 int i, io_timeout;
Michal Vasko93224072021-11-09 12:14:28 +01001334 char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001335 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001336
Michal Vasko131120a2018-05-29 15:44:02 +02001337 if (session->side == NC_CLIENT) {
1338 /* client side hello - send only NETCONF base capabilities */
1339 cpblts = malloc(3 * sizeof *cpblts);
1340 if (!cpblts) {
1341 ERRMEM;
1342 return NC_MSG_ERROR;
1343 }
Michal Vasko93224072021-11-09 12:14:28 +01001344 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1345 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko131120a2018-05-29 15:44:02 +02001346 cpblts[2] = NULL;
1347
1348 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1349 sid = NULL;
1350 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001351 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0);
Michal Vasko5b24b6b2020-12-04 09:29:47 +01001352 if (!cpblts) {
1353 return NC_MSG_ERROR;
1354 }
Michal Vasko131120a2018-05-29 15:44:02 +02001355
1356 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1357 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001358 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001359
Michal Vasko131120a2018-05-29 15:44:02 +02001360 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001361
Michal Vasko086311b2016-01-08 09:53:11 +01001362 for (i = 0; cpblts[i]; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001363 free(cpblts[i]);
Michal Vasko086311b2016-01-08 09:53:11 +01001364 }
1365 free(cpblts);
1366
Michal Vasko131120a2018-05-29 15:44:02 +02001367 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001368}
1369
1370static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001371nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001372{
Michal Vasko77367452021-02-16 16:32:18 +01001373 struct ly_in *msg;
1374 struct lyd_node *hello = NULL, *iter;
1375 struct lyd_node_opaq *node;
1376 int r, ver = -1, flag = 0;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001377 char *str;
1378 long long int id;
Michal Vasko77367452021-02-16 16:32:18 +01001379 NC_MSG_TYPE rc = NC_MSG_HELLO;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001380
Michal Vasko77367452021-02-16 16:32:18 +01001381 r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg);
1382 switch (r) {
1383 case 1:
Radek Krejci695d4fa2015-10-22 13:23:54 +02001384 /* parse <hello> data */
Michal Vasko77367452021-02-16 16:32:18 +01001385 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 +02001386 ERR(session, "Failed to parse server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001387 rc = NC_MSG_ERROR;
1388 goto cleanup;
1389 }
1390
1391 LY_LIST_FOR(lyd_child(hello), iter) {
1392 node = (struct lyd_node_opaq *)iter;
1393
1394 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001395 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001396 } else if (!strcmp(node->name.name, "session-id")) {
1397 if (!node->value || !strlen(node->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001398 ERR(session, "No value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001399 rc = NC_MSG_ERROR;
1400 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001401 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001402 str = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001403 id = strtoll(node->value, &str, 10);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001404 if (*str || (id < 1) || (id > UINT32_MAX)) {
Michal Vasko05532772021-06-03 12:12:38 +02001405 ERR(session, "Invalid value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001406 rc = NC_MSG_ERROR;
1407 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001408 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001409 session->id = (uint32_t)id;
1410 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001411 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001412 ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001413 rc = NC_MSG_ERROR;
1414 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001415 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001416
1417 if (flag) {
1418 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001419 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001420 rc = NC_MSG_ERROR;
1421 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001422 }
1423 flag = 1;
1424
Michal Vasko77367452021-02-16 16:32:18 +01001425 if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) {
1426 rc = NC_MSG_ERROR;
1427 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001428 }
1429 session->version = ver;
1430 }
1431
1432 if (!session->id) {
Michal Vasko05532772021-06-03 12:12:38 +02001433 ERR(session, "Missing <session-id> in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001434 rc = NC_MSG_ERROR;
1435 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001436 }
1437 break;
Michal Vasko77367452021-02-16 16:32:18 +01001438 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001439 ERR(session, "Server <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001440 rc = NC_MSG_WOULDBLOCK;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001441 break;
1442 default:
Michal Vasko77367452021-02-16 16:32:18 +01001443 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001444 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001445 }
1446
Michal Vasko77367452021-02-16 16:32:18 +01001447cleanup:
1448 ly_in_free(msg, 1);
1449 lyd_free_tree(hello);
1450 return rc;
Radek Krejci5686ff72015-10-09 13:33:56 +02001451}
1452
Michal Vasko11d142a2016-01-19 15:58:24 +01001453static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001454nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001455{
Michal Vasko77367452021-02-16 16:32:18 +01001456 struct ly_in *msg;
1457 struct lyd_node *hello = NULL, *iter;
1458 struct lyd_node_opaq *node;
1459 NC_MSG_TYPE rc = NC_MSG_HELLO;
1460 int r, ver = -1, flag = 0, timeout_io;
Michal Vasko11d142a2016-01-19 15:58:24 +01001461
Michal Vasko77367452021-02-16 16:32:18 +01001462 timeout_io = server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000;
1463 r = nc_read_msg_poll_io(session, timeout_io, &msg);
1464 switch (r) {
1465 case 1:
1466 /* parse <hello> data */
1467 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 +02001468 ERR(session, "Failed to parse client <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001469 rc = NC_MSG_ERROR;
1470 goto cleanup;
1471 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001472
Michal Vasko77367452021-02-16 16:32:18 +01001473 /* learn NETCONF version */
1474 LY_LIST_FOR(lyd_child(hello), iter) {
1475 node = (struct lyd_node_opaq *)iter;
1476
1477 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001478 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001479 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001480 ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001481 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001482 goto cleanup;
1483 }
1484
1485 if (flag) {
1486 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001487 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001488 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001489 goto cleanup;
1490 }
1491 flag = 1;
1492
Michal Vasko77367452021-02-16 16:32:18 +01001493 if ((ver = parse_cpblts(&node->node, NULL)) < 0) {
1494 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001495 goto cleanup;
1496 }
1497 session->version = ver;
1498 }
1499 break;
Michal Vasko77367452021-02-16 16:32:18 +01001500 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001501 ERR(session, "Client <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001502 rc = NC_MSG_WOULDBLOCK;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001503 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001504 default:
Michal Vasko77367452021-02-16 16:32:18 +01001505 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001506 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001507 }
1508
1509cleanup:
Michal Vasko77367452021-02-16 16:32:18 +01001510 ly_in_free(msg, 1);
1511 lyd_free_tree(hello);
1512 return rc;
Michal Vasko11d142a2016-01-19 15:58:24 +01001513}
1514
Michal Vasko71090fc2016-05-24 16:37:28 +02001515NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001516nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001517{
Michal Vasko086311b2016-01-08 09:53:11 +01001518 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001519
Michal Vasko131120a2018-05-29 15:44:02 +02001520 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001521 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001522 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001523 }
1524
Michal Vasko11d142a2016-01-19 15:58:24 +01001525 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001526 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001527 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001528 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001529 }
1530
Michal Vasko71090fc2016-05-24 16:37:28 +02001531 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001532}
Michal Vasko086311b2016-01-08 09:53:11 +01001533
Michal Vasko889162e2019-09-06 14:43:37 +02001534#ifdef NC_ENABLED_SSH
1535
Michal Vasko999b64a2019-07-10 16:06:15 +02001536static void
1537nc_ssh_init(void)
1538{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001539#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001540 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1541 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001542#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001543}
1544
1545static void
1546nc_ssh_destroy(void)
1547{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001548#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001549 FIPS_mode_set(0);
1550 CONF_modules_unload(1);
1551 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001552#endif
1553
Michal Vaskoe1e82632019-09-09 09:18:03 +02001554#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1555 ssh_finalize();
1556#endif
1557}
1558
Michal Vasko889162e2019-09-06 14:43:37 +02001559#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001560
Radek Krejci53691be2016-02-22 13:58:37 +01001561#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001562
Michal Vasko770b4362017-02-17 14:44:18 +01001563#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1564
Michal Vaskof0c92c02016-01-29 09:41:45 +01001565struct CRYPTO_dynlock_value {
1566 pthread_mutex_t lock;
1567};
1568
Michal Vaskof0c92c02016-01-29 09:41:45 +01001569static struct CRYPTO_dynlock_value *
1570tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1571{
1572 struct CRYPTO_dynlock_value *value;
1573
1574 value = malloc(sizeof *value);
1575 if (!value) {
1576 ERRMEM;
1577 return NULL;
1578 }
1579 pthread_mutex_init(&value->lock, NULL);
1580
1581 return value;
1582}
1583
1584static void
1585tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1586{
1587 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1588 * I found ignored this fact, what do I know... */
1589 if (mode & CRYPTO_LOCK) {
1590 pthread_mutex_lock(&l->lock);
1591 } else {
1592 pthread_mutex_unlock(&l->lock);
1593 }
1594}
1595
1596static void
1597tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1598{
1599 pthread_mutex_destroy(&l->lock);
1600 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001601}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001602
Michal Vasko770b4362017-02-17 14:44:18 +01001603#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001604
Michal Vasko8f0c0282016-02-29 10:17:14 +01001605#endif /* NC_ENABLED_TLS */
1606
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001607#if defined (NC_ENABLED_TLS) && !defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001608
Michal Vasko770b4362017-02-17 14:44:18 +01001609#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001610static pthread_mutex_t *tls_locks;
1611
1612static void
1613tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1614{
1615 if (mode & CRYPTO_LOCK) {
1616 pthread_mutex_lock(tls_locks + n);
1617 } else {
1618 pthread_mutex_unlock(tls_locks + n);
1619 }
1620}
1621
1622static void
1623tls_thread_id_func(CRYPTO_THREADID *tid)
1624{
1625 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1626}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001627
Michal Vasko770b4362017-02-17 14:44:18 +01001628#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001629
1630static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001631nc_tls_init(void)
1632{
Rosen Penev4f552d62019-06-26 16:10:43 -07001633#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001634 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001635 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001636 SSL_library_init();
1637
Michal Vaskof89948c2018-01-04 09:19:46 +01001638 int i;
1639
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001640 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001641 if (!tls_locks) {
1642 ERRMEM;
1643 return;
1644 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001645 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1646 pthread_mutex_init(tls_locks + i, NULL);
1647 }
1648
Michal Vaskof0c92c02016-01-29 09:41:45 +01001649 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001650 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001651
1652 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1653 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1654 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001655#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001656}
1657
Michal Vasko8f0c0282016-02-29 10:17:14 +01001658static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001659nc_tls_destroy(void)
1660{
Rosen Penev4f552d62019-06-26 16:10:43 -07001661#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001662 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001663 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001664 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001665 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001666 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001667#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001668 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001669#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1670 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001671#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001672
Michal Vaskof89948c2018-01-04 09:19:46 +01001673 int i;
1674
Michal Vaskob6e37262016-02-25 14:49:00 +01001675 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001676 CRYPTO_set_locking_callback(NULL);
1677 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1678 pthread_mutex_destroy(tls_locks + i);
1679 }
1680 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001681
1682 CRYPTO_set_dynlock_create_callback(NULL);
1683 CRYPTO_set_dynlock_lock_callback(NULL);
1684 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001685#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001686}
1687
Michal Vasko8f0c0282016-02-29 10:17:14 +01001688#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001689
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001690#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001691
Michal Vasko8f0c0282016-02-29 10:17:14 +01001692static void
Michal Vasko5e228792016-02-03 15:30:24 +01001693nc_ssh_tls_init(void)
1694{
Rosen Penev4f552d62019-06-26 16:10:43 -07001695#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001696 SSL_load_error_strings();
1697 ERR_load_BIO_strings();
1698 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001699#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001700
1701 nc_ssh_init();
1702
Michal Vaskoe1e82632019-09-09 09:18:03 +02001703#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001704 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1705 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1706 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001707#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001708}
1709
Michal Vasko8f0c0282016-02-29 10:17:14 +01001710static void
Michal Vasko5e228792016-02-03 15:30:24 +01001711nc_ssh_tls_destroy(void)
1712{
Rosen Penev4f552d62019-06-26 16:10:43 -07001713#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001714 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001715# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001716 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001717# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001718 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001719# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001720#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001721
1722 nc_ssh_destroy();
1723
Michal Vaskoe1e82632019-09-09 09:18:03 +02001724#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001725 CRYPTO_set_dynlock_create_callback(NULL);
1726 CRYPTO_set_dynlock_lock_callback(NULL);
1727 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001728#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001729}
1730
Radek Krejci53691be2016-02-22 13:58:37 +01001731#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001732
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001733#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001734
1735API void
1736nc_thread_destroy(void)
1737{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001738 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001739 // CRYPTO_cleanup_all_ex_data();
Michal Vasko8f0c0282016-02-29 10:17:14 +01001740
Michal Vasko770b4362017-02-17 14:44:18 +01001741#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1742 CRYPTO_THREADID crypto_tid;
1743
Michal Vasko8f0c0282016-02-29 10:17:14 +01001744 CRYPTO_THREADID_current(&crypto_tid);
1745 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001746#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001747}
1748
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001749#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1750
1751void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001752nc_init(void)
1753{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001754#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001755 nc_ssh_tls_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001756#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001757 nc_ssh_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001758#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001759 nc_tls_init();
1760#endif
1761}
1762
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001763void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001764nc_destroy(void)
1765{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001766#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001767 nc_ssh_tls_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001768#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001769 nc_ssh_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001770#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001771 nc_tls_destroy();
1772#endif
1773}