blob: 3e47a47fc0bb8c71ffaebaad6be35ee42d81702f [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 Vasko69e98752022-12-14 14:20:17 +0100373 WRN(session, "%s: session RPC lock timeout, should not happen.", func);
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 */
Michal Vaskoe44f2702022-12-12 07:58:06 +0100716 int sock = -1;
Michal Vasko33476c32022-09-09 11:21:40 +0200717 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
Michal Vaskoe44f2702022-12-12 07:58:06 +0100741 case NC_TI_LIBSSH: {
742 int r;
743
Michal Vasko33476c32022-09-09 11:21:40 +0200744 if (connected) {
Michal Vasko64734402022-09-09 11:22:00 +0200745 ssh_channel_send_eof(session->ti.libssh.channel);
Michal Vasko33476c32022-09-09 11:21:40 +0200746 ssh_channel_free(session->ti.libssh.channel);
747 }
748 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
749 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
750 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
751 */
752 /* SESSION IO LOCK */
753 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
754
755 if (session->ti.libssh.next) {
756 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
757 if (siter->status != NC_STATUS_STARTING) {
758 *multisession = 1;
759 break;
760 }
761 }
762 }
763
764 if (!*multisession) {
765 /* it's not multisession yet, but we still need to free the starting sessions */
766 if (session->ti.libssh.next) {
767 do {
768 siter = session->ti.libssh.next;
769 session->ti.libssh.next = siter->ti.libssh.next;
770
771 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
772 free(siter->username);
773 free(siter->host);
774 if (!(siter->flags & NC_SESSION_SHAREDCTX)) {
775 ly_ctx_destroy((struct ly_ctx *)siter->ctx);
776 }
777
778 free(siter);
779 } while (session->ti.libssh.next != session);
780 }
781 /* remember sock so we can close it */
782 sock = ssh_get_fd(session->ti.libssh.session);
783 if (connected) {
784 ssh_disconnect(session->ti.libssh.session);
785 sock = -1;
786 }
787 ssh_free(session->ti.libssh.session);
788 } else {
789 /* remove the session from the list */
790 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {}
791 if (session->ti.libssh.next == siter) {
792 /* there will be only one session */
793 siter->ti.libssh.next = NULL;
794 } else {
795 /* there are still multiple sessions, keep the ring list */
796 siter->ti.libssh.next = session->ti.libssh.next;
797 }
Michal Vasko78939072022-12-12 07:43:18 +0100798
Michal Vasko33476c32022-09-09 11:21:40 +0200799 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
800 if (session->flags & NC_SESSION_SSH_MSG_CB) {
801 siter = session->ti.libssh.next;
802 while (siter && (siter->status != NC_STATUS_RUNNING)) {
803 if (siter->ti.libssh.next == session) {
804 ERRINT;
805 break;
806 }
807 siter = siter->ti.libssh.next;
808 }
809 /* siter may be NULL in case all the sessions terminated at the same time (socket was disconnected),
810 * we set session to NULL because we do not expect any new message to arrive */
811 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
812 if (siter) {
813 siter->flags |= NC_SESSION_SSH_MSG_CB;
814 }
815 }
816 }
817
818 /* SESSION IO UNLOCK */
819 if (r == 1) {
820 nc_session_io_unlock(session, __func__);
821 }
822 break;
Michal Vaskoe44f2702022-12-12 07:58:06 +0100823 }
Michal Vasko33476c32022-09-09 11:21:40 +0200824#endif
825
826#ifdef NC_ENABLED_TLS
827 case NC_TI_OPENSSL:
828 /* remember sock so we can close it */
829 sock = SSL_get_fd(session->ti.tls);
830
831 if (connected) {
832 SSL_shutdown(session->ti.tls);
833 }
834 SSL_free(session->ti.tls);
835
836 if (session->side == NC_SERVER) {
837 X509_free(session->opts.server.client_cert);
838 }
839 break;
840#endif
841 case NC_TI_NONE:
842 break;
843 }
844
845 /* close socket separately */
846 if (sock > -1) {
847 close(sock);
848 }
849}
850
Radek Krejci695d4fa2015-10-22 13:23:54 +0200851API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100852nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200853{
Michal Vasko33476c32022-09-09 11:21:40 +0200854 int r, i, rpc_locked = 0, msgs_locked = 0, timeout;
Michal Vaskob48aa812016-01-18 14:13:09 +0100855 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoad611702015-12-03 13:41:51 +0100856 struct nc_msg_cont *contiter;
Michal Vasko77367452021-02-16 16:32:18 +0100857 struct ly_in *msg;
roman6ece9c52022-06-22 09:29:17 +0200858 struct timespec ts;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200859 void *p;
860
Michal Vasko428087d2016-01-14 16:04:28 +0100861 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200862 return;
863 }
864
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200865 /* stop notification threads if any */
866 if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running)) {
867 /* let the threads know they should quit */
868 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 0);
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200869
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200870 /* wait for them */
roman6ece9c52022-06-22 09:29:17 +0200871 nc_gettimespec_mono_add(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200872 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_count)) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200873 usleep(NC_TIMEOUT_STEP);
Michal Vasko60d8ffb2022-07-21 11:08:34 +0200874 if (nc_difftimespec_mono_cur(&ts) < 1) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200875 ERR(session, "Waiting for notification thread exit failed (timed out).");
876 break;
877 }
878 }
Michal Vasko86d357c2016-03-11 13:46:38 +0100879 }
880
Michal Vaskoacf98472021-02-04 15:33:57 +0100881 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200882 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100883 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100884 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200885 } else if (r) {
886 rpc_locked = 1;
Michal Vasko96a28a32021-02-04 15:35:20 +0100887 } else {
888 /* else failed to lock it, too bad */
Michal Vasko05532772021-06-03 12:12:38 +0200889 ERR(session, "Freeing a session while an RPC is being processed.");
Michal Vasko96a28a32021-02-04 15:35:20 +0100890 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200891 }
892
Michal Vasko8c247822020-09-07 13:23:23 +0200893 if (session->side == NC_CLIENT) {
Michal Vasko01130bd2021-08-26 11:47:38 +0200894 timeout = NC_SESSION_FREE_LOCK_TIMEOUT;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200895
Michal Vasko01130bd2021-08-26 11:47:38 +0200896 /* MSGS LOCK */
897 r = nc_session_client_msgs_lock(session, &timeout, __func__);
898 if (r == -1) {
899 return;
900 } else if (r) {
901 msgs_locked = 1;
902 } else {
903 /* else failed to lock it, too bad */
904 ERR(session, "Freeing a session while messages are being received.");
905 }
906
907 /* cleanup message queue */
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200908 for (contiter = session->opts.client.msgs; contiter; ) {
Michal Vasko77367452021-02-16 16:32:18 +0100909 ly_in_free(contiter->msg, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200910
Michal Vaskoad611702015-12-03 13:41:51 +0100911 p = contiter;
912 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200913 free(p);
914 }
915
Michal Vasko01130bd2021-08-26 11:47:38 +0200916 if (msgs_locked) {
917 /* MSGS UNLOCK */
918 nc_session_client_msgs_unlock(session, __func__);
919 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200920
Michal Vasko8c247822020-09-07 13:23:23 +0200921 if (session->status == NC_STATUS_RUNNING) {
Michal Vasko9c6d38c2021-09-03 13:02:53 +0200922 /* receive any leftover messages */
923 while (nc_read_msg_poll_io(session, 0, &msg) == 1) {
924 ly_in_free(msg, 1);
925 }
926
Michal Vasko8c247822020-09-07 13:23:23 +0200927 /* send closing info to the other side */
Michal Vaskod4da3632022-05-25 11:49:10 +0200928 nc_session_free_close_session(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200929 }
930
931 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200932 if (session->opts.client.cpblts) {
933 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200934 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200935 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200936 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200937 }
Michal Vasko78939072022-12-12 07:43:18 +0100938
939 /* LY ext data */
Michal Vaskoe44f2702022-12-12 07:58:06 +0100940#ifdef NC_ENABLED_SSH
941 struct nc_session *siter;
942
Michal Vasko78939072022-12-12 07:43:18 +0100943 if ((session->flags & NC_SESSION_SHAREDCTX) && session->ti.libssh.next) {
944 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
945 if (siter->status != NC_STATUS_STARTING) {
946 /* move LY ext data to this session */
947 assert(!siter->opts.client.ext_data);
948 siter->opts.client.ext_data = session->opts.client.ext_data;
949 session->opts.client.ext_data = NULL;
950 break;
951 }
952 }
Michal Vaskoe44f2702022-12-12 07:58:06 +0100953 } else
954#endif
955 {
Michal Vasko78939072022-12-12 07:43:18 +0100956 lyd_free_siblings(session->opts.client.ext_data);
957 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200958 }
959
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100960 if (session->data && data_free) {
961 data_free(session->data);
962 }
963
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200964 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
965 /* CH LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100966 pthread_mutex_lock(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200967 }
968
Michal Vasko86d357c2016-03-11 13:46:38 +0100969 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200970 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200971
Michal Vaskofeccb312022-03-24 15:24:59 +0100972 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100973 pthread_cond_signal(&session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200974
roman6ece9c52022-06-22 09:29:17 +0200975 nc_gettimespec_real_add(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vasko0db3db52021-03-03 10:45:42 +0100976
977 /* wait for CH thread to actually wake up and terminate */
978 r = 0;
Michal Vaskofeccb312022-03-24 15:24:59 +0100979 while (!r && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vasko0db3db52021-03-03 10:45:42 +0100980 r = pthread_cond_timedwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, &ts);
981 }
Michal Vasko0db3db52021-03-03 10:45:42 +0100982 if (r) {
Michal Vasko05532772021-06-03 12:12:38 +0200983 ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r));
Michal Vasko3f05a092018-03-13 10:39:49 +0100984 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200985 }
986
Michal Vaskofeccb312022-03-24 15:24:59 +0100987 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
988 /* CH UNLOCK */
989 pthread_mutex_unlock(&session->opts.server.ch_lock);
990 }
991
Radek Krejci695d4fa2015-10-22 13:23:54 +0200992 /* transport implementation cleanup */
Michal Vasko33476c32022-09-09 11:21:40 +0200993 nc_session_free_transport(session, &multisession);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200994
Michal Vasko33476c32022-09-09 11:21:40 +0200995 /* final cleanup */
Michal Vasko93224072021-11-09 12:14:28 +0100996 free(session->username);
997 free(session->host);
998 free(session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200999
Michal Vaskoacf98472021-02-04 15:33:57 +01001000 if (session->side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +02001001 pthread_mutex_destroy(&session->opts.server.ntf_status_lock);
Michal Vasko131120a2018-05-29 15:44:02 +02001002 if (rpc_locked) {
1003 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +01001004 }
Michal Vaskoacf98472021-02-04 15:33:57 +01001005 pthread_mutex_destroy(&session->opts.server.rpc_lock);
1006 pthread_cond_destroy(&session->opts.server.rpc_cond);
Michal Vasko131120a2018-05-29 15:44:02 +02001007 }
1008
1009 if (session->io_lock && !multisession) {
1010 pthread_mutex_destroy(session->io_lock);
1011 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001012 }
1013
1014 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko93224072021-11-09 12:14:28 +01001015 ly_ctx_destroy((struct ly_ctx *)session->ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001016 }
1017
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001018 if (session->side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +01001019 /* free CH synchronization structures */
1020 pthread_cond_destroy(&session->opts.server.ch_cond);
1021 pthread_mutex_destroy(&session->opts.server.ch_lock);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001022 } else {
1023 pthread_mutex_destroy(&session->opts.client.msgs_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001024 }
1025
Radek Krejci695d4fa2015-10-22 13:23:54 +02001026 free(session);
1027}
1028
Michal Vasko086311b2016-01-08 09:53:11 +01001029static void
Michal Vasko93224072021-11-09 12:14:28 +01001030add_cpblt(const char *capab, char ***cpblts, int *size, int *count)
Michal Vasko086311b2016-01-08 09:53:11 +01001031{
Radek Krejci658782b2016-12-04 22:04:55 +01001032 size_t len;
1033 int i;
1034 char *p;
1035
1036 if (capab) {
1037 /* check if already present */
1038 p = strchr(capab, '?');
1039 if (p) {
1040 len = p - capab;
1041 } else {
1042 len = strlen(capab);
1043 }
1044 for (i = 0; i < *count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001045 if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) {
Radek Krejci658782b2016-12-04 22:04:55 +01001046 /* already present, do not duplicate it */
1047 return;
1048 }
1049 }
1050 }
1051
1052 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +01001053 if (*count == *size) {
1054 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001055 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
1056 if (!(*cpblts)) {
1057 ERRMEM;
1058 return;
1059 }
Michal Vasko086311b2016-01-08 09:53:11 +01001060 }
1061
Michal Vasko93224072021-11-09 12:14:28 +01001062 (*cpblts)[*count] = capab ? strdup(capab) : NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001063 ++(*count);
1064}
1065
Michal Vasko93224072021-11-09 12:14:28 +01001066API char **
1067nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +01001068{
Michal Vasko93224072021-11-09 12:14:28 +01001069 char **cpblts;
Michal Vasko77367452021-02-16 16:32:18 +01001070 const struct lys_module *mod;
1071 struct lysp_feature *feat;
1072 int size = 10, count, features_count = 0, dev_count = 0, str_len, len;
Michal Vasko1440a742021-03-31 11:11:03 +02001073 uint32_t i, u;
Michal Vasko77367452021-02-16 16:32:18 +01001074 LY_ARRAY_COUNT_TYPE v;
Michal Vasko1440a742021-03-31 11:11:03 +02001075 char *yl_content_id;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001076
Radek Krejci24a18412018-05-16 15:09:10 +02001077#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +02001078 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001079
Michal Vasko4ffa3b22016-05-24 16:36:25 +02001080 if (!ctx) {
1081 ERRARG("ctx");
1082 return NULL;
1083 }
1084
Michal Vasko086311b2016-01-08 09:53:11 +01001085 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001086 if (!cpblts) {
1087 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +02001088 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001089 }
Michal Vasko93224072021-11-09 12:14:28 +01001090 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1091 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko086311b2016-01-08 09:53:11 +01001092 count = 2;
1093
1094 /* capabilities */
1095
Michal Vasko77367452021-02-16 16:32:18 +01001096 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf");
Michal Vasko086311b2016-01-08 09:53:11 +01001097 if (mod) {
Michal Vasko77367452021-02-16 16:32:18 +01001098 if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001099 add_cpblt("urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001100 }
Michal Vasko77367452021-02-16 16:32:18 +01001101 if (lys_feature_value(mod, "candidate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001102 add_cpblt("urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko77367452021-02-16 16:32:18 +01001103 if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001104 add_cpblt("urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001105 }
1106 }
Michal Vasko77367452021-02-16 16:32:18 +01001107 if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001108 add_cpblt("urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001109 }
Michal Vasko77367452021-02-16 16:32:18 +01001110 if (lys_feature_value(mod, "validate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001111 add_cpblt("urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001112 }
Michal Vasko77367452021-02-16 16:32:18 +01001113 if (lys_feature_value(mod, "startup") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001114 add_cpblt("urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001115 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001116
1117 /* The URL capability must be set manually using nc_server_set_capability()
1118 * because of the need for supported protocols to be included.
1119 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1120 */
Michal Vasko77367452021-02-16 16:32:18 +01001121 // if (lys_feature_value(mod, "url") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001122 // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
mekleoa8de5e92020-02-13 09:05:56 +01001123 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001124
Michal Vasko77367452021-02-16 16:32:18 +01001125 if (lys_feature_value(mod, "xpath") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001126 add_cpblt("urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001127 }
1128 }
1129
Michal Vasko77367452021-02-16 16:32:18 +01001130 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01001131 if (mod) {
1132 if (!server_opts.wd_basic_mode) {
Michal Vasko05532772021-06-03 12:12:38 +02001133 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 +01001134 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001135 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +01001136 switch (server_opts.wd_basic_mode) {
1137 case NC_WD_ALL:
1138 strcat(str, "?basic-mode=report-all");
1139 break;
1140 case NC_WD_TRIM:
1141 strcat(str, "?basic-mode=trim");
1142 break;
1143 case NC_WD_EXPLICIT:
1144 strcat(str, "?basic-mode=explicit");
1145 break;
1146 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001147 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001148 break;
1149 }
1150
1151 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001152 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +01001153 if (server_opts.wd_also_supported & NC_WD_ALL) {
1154 strcat(str, "report-all,");
1155 }
1156 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
1157 strcat(str, "report-all-tagged,");
1158 }
1159 if (server_opts.wd_also_supported & NC_WD_TRIM) {
1160 strcat(str, "trim,");
1161 }
1162 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
1163 strcat(str, "explicit,");
1164 }
1165 str[strlen(str) - 1] = '\0';
1166
Michal Vasko93224072021-11-09 12:14:28 +01001167 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001168 }
1169 }
1170 }
1171
Radek Krejci658782b2016-12-04 22:04:55 +01001172 /* other capabilities */
1173 for (u = 0; u < server_opts.capabilities_count; u++) {
Michal Vasko93224072021-11-09 12:14:28 +01001174 add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001175 }
1176
1177 /* models */
Michal Vasko1440a742021-03-31 11:11:03 +02001178 u = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001179 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001180 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vasko77367452021-02-16 16:32:18 +01001181 if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) {
Michal Vasko05532772021-06-03 12:12:38 +02001182 ERR(NULL, "Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
Michal Vaskod5ada122020-03-19 18:28:06 +01001183 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001184 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001185
Michal Vasko1440a742021-03-31 11:11:03 +02001186 /* get content-id */
1187 if (server_opts.content_id_clb) {
1188 yl_content_id = server_opts.content_id_clb(server_opts.content_id_data);
1189 if (!yl_content_id) {
1190 ERRMEM;
1191 goto error;
1192 }
1193 } else {
1194 yl_content_id = malloc(11);
1195 if (!yl_content_id) {
1196 ERRMEM;
1197 goto error;
1198 }
1199 sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx));
1200 }
1201
Michal Vasko77367452021-02-16 16:32:18 +01001202 if (!strcmp(mod->revision, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001203 /* new one (capab defined in RFC 8526 section 2) */
Michal Vasko1440a742021-03-31 11:11:03 +02001204 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s",
1205 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001206 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001207 } else {
1208 /* old one (capab defined in RFC 7950 section 5.6.4) */
Michal Vasko1440a742021-03-31 11:11:03 +02001209 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s",
1210 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001211 add_cpblt(str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001212 }
Michal Vasko1440a742021-03-31 11:11:03 +02001213 free(yl_content_id);
Radek Krejci24a18412018-05-16 15:09:10 +02001214 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001215 } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001216 /* skip YANG 1.1 modules */
Radek Krejci24a18412018-05-16 15:09:10 +02001217 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001218 } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001219 /* skip YANG 1.0 modules */
Radek Krejci24a18412018-05-16 15:09:10 +02001220 continue;
1221 }
Michal Vasko086311b2016-01-08 09:53:11 +01001222
Michal Vasko77367452021-02-16 16:32:18 +01001223 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "",
1224 mod->revision ? mod->revision : "");
Radek Krejci24a18412018-05-16 15:09:10 +02001225
Michal Vaskodafdc742020-03-11 16:15:59 +01001226 features_count = 0;
1227 i = 0;
1228 feat = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001229 while ((feat = lysp_feature_next(feat, mod->parsed, &i))) {
Michal Vaskodafdc742020-03-11 16:15:59 +01001230 if (!(feat->flags & LYS_FENABLED)) {
1231 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001232 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001233 if (!features_count) {
1234 strcat(str, "&features=");
1235 str_len += 10;
1236 }
1237 len = strlen(feat->name);
1238 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1239 ERRINT;
1240 break;
1241 }
1242 if (features_count) {
1243 strcat(str, ",");
1244 ++str_len;
1245 }
1246 strcat(str, feat->name);
1247 str_len += len;
1248 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001249 }
Michal Vasko086311b2016-01-08 09:53:11 +01001250
Michal Vasko77367452021-02-16 16:32:18 +01001251 if (mod->deviated_by) {
Radek Krejci24a18412018-05-16 15:09:10 +02001252 strcat(str, "&deviations=");
1253 str_len += 12;
1254 dev_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +01001255 LY_ARRAY_FOR(mod->deviated_by, v) {
1256 len = strlen(mod->deviated_by[v]->name);
1257 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1258 ERRINT;
1259 break;
Radek Krejci24a18412018-05-16 15:09:10 +02001260 }
Michal Vasko77367452021-02-16 16:32:18 +01001261 if (dev_count) {
1262 strcat(str, ",");
1263 ++str_len;
Radek Krejci24a18412018-05-16 15:09:10 +02001264 }
Michal Vasko77367452021-02-16 16:32:18 +01001265 strcat(str, mod->deviated_by[v]->name);
1266 str_len += len;
1267 dev_count++;
Radek Krejci24a18412018-05-16 15:09:10 +02001268 }
1269 }
1270
Michal Vasko93224072021-11-09 12:14:28 +01001271 add_cpblt(str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +02001272 }
Michal Vasko086311b2016-01-08 09:53:11 +01001273
1274 /* ending NULL capability */
Michal Vasko93224072021-11-09 12:14:28 +01001275 add_cpblt(NULL, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001276
1277 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001278
1279error:
Radek Krejcif906e412017-09-22 14:44:45 +02001280 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001281 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001282}
1283
Michal Vasko93224072021-11-09 12:14:28 +01001284API char **
1285nc_server_get_cpblts(const struct ly_ctx *ctx)
Radek Krejci24a18412018-05-16 15:09:10 +02001286{
1287 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1288}
1289
Radek Krejci695d4fa2015-10-22 13:23:54 +02001290static int
Michal Vasko77367452021-02-16 16:32:18 +01001291parse_cpblts(struct lyd_node *capabilities, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001292{
Michal Vasko77367452021-02-16 16:32:18 +01001293 struct lyd_node *iter;
1294 struct lyd_node_opaq *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001295 int ver = -1, i = 0;
1296 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001297
1298 if (list) {
1299 /* get the storage for server's capabilities */
Michal Vasko77367452021-02-16 16:32:18 +01001300 LY_LIST_FOR(lyd_child(capabilities), iter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001301 i++;
1302 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001303 /* last item remains NULL */
1304 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001305 if (!*list) {
1306 ERRMEM;
1307 return -1;
1308 }
1309 i = 0;
1310 }
1311
Michal Vasko77367452021-02-16 16:32:18 +01001312 LY_LIST_FOR(lyd_child(capabilities), iter) {
1313 cpblt = (struct lyd_node_opaq *)iter;
1314
1315 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 +02001316 ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001317 return -1;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001318 }
1319
Michal Vasko156d3272017-04-11 11:46:49 +02001320 /* skip leading/trailing whitespaces */
Michal Vasko77367452021-02-16 16:32:18 +01001321 for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {}
1322 for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {}
1323 if (!cpb_start[0] || (cpb_end == cpblt->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001324 ERR(NULL, "Empty capability \"%s\" received.", cpblt->value);
Michal Vasko156d3272017-04-11 11:46:49 +02001325 return -1;
1326 }
1327
Radek Krejci695d4fa2015-10-22 13:23:54 +02001328 /* detect NETCONF version */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001329 if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001330 ver = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001331 } 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 +02001332 ver = 1;
1333 }
1334
1335 /* store capabilities */
1336 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001337 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1338 if (!(*list)[i]) {
1339 ERRMEM;
1340 return -1;
1341 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001342 i++;
1343 }
1344 }
1345
1346 if (ver == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001347 ERR(NULL, "Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001348 }
1349
1350 return ver;
1351}
1352
1353static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001354nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001355{
Michal Vasko131120a2018-05-29 15:44:02 +02001356 NC_MSG_TYPE ret;
1357 int i, io_timeout;
Michal Vasko93224072021-11-09 12:14:28 +01001358 char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001359 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001360
Michal Vasko131120a2018-05-29 15:44:02 +02001361 if (session->side == NC_CLIENT) {
1362 /* client side hello - send only NETCONF base capabilities */
1363 cpblts = malloc(3 * sizeof *cpblts);
1364 if (!cpblts) {
1365 ERRMEM;
1366 return NC_MSG_ERROR;
1367 }
Michal Vasko93224072021-11-09 12:14:28 +01001368 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1369 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko131120a2018-05-29 15:44:02 +02001370 cpblts[2] = NULL;
1371
1372 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1373 sid = NULL;
1374 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001375 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0);
Michal Vasko5b24b6b2020-12-04 09:29:47 +01001376 if (!cpblts) {
1377 return NC_MSG_ERROR;
1378 }
Michal Vasko131120a2018-05-29 15:44:02 +02001379
1380 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1381 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001382 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001383
Michal Vasko131120a2018-05-29 15:44:02 +02001384 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001385
Michal Vasko086311b2016-01-08 09:53:11 +01001386 for (i = 0; cpblts[i]; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001387 free(cpblts[i]);
Michal Vasko086311b2016-01-08 09:53:11 +01001388 }
1389 free(cpblts);
1390
Michal Vasko131120a2018-05-29 15:44:02 +02001391 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001392}
1393
1394static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001395nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001396{
Michal Vasko77367452021-02-16 16:32:18 +01001397 struct ly_in *msg;
1398 struct lyd_node *hello = NULL, *iter;
1399 struct lyd_node_opaq *node;
1400 int r, ver = -1, flag = 0;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001401 char *str;
1402 long long int id;
Michal Vasko77367452021-02-16 16:32:18 +01001403 NC_MSG_TYPE rc = NC_MSG_HELLO;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001404
Michal Vasko77367452021-02-16 16:32:18 +01001405 r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg);
1406 switch (r) {
1407 case 1:
Radek Krejci695d4fa2015-10-22 13:23:54 +02001408 /* parse <hello> data */
Michal Vasko77367452021-02-16 16:32:18 +01001409 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 +02001410 ERR(session, "Failed to parse server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001411 rc = NC_MSG_ERROR;
1412 goto cleanup;
1413 }
1414
1415 LY_LIST_FOR(lyd_child(hello), iter) {
1416 node = (struct lyd_node_opaq *)iter;
1417
1418 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001419 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001420 } else if (!strcmp(node->name.name, "session-id")) {
1421 if (!node->value || !strlen(node->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001422 ERR(session, "No value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001423 rc = NC_MSG_ERROR;
1424 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001425 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001426 str = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001427 id = strtoll(node->value, &str, 10);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001428 if (*str || (id < 1) || (id > UINT32_MAX)) {
Michal Vasko05532772021-06-03 12:12:38 +02001429 ERR(session, "Invalid value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001430 rc = NC_MSG_ERROR;
1431 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001432 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001433 session->id = (uint32_t)id;
1434 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001435 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001436 ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001437 rc = NC_MSG_ERROR;
1438 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001439 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001440
1441 if (flag) {
1442 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001443 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001444 rc = NC_MSG_ERROR;
1445 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001446 }
1447 flag = 1;
1448
Michal Vasko77367452021-02-16 16:32:18 +01001449 if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) {
1450 rc = NC_MSG_ERROR;
1451 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001452 }
1453 session->version = ver;
1454 }
1455
1456 if (!session->id) {
Michal Vasko05532772021-06-03 12:12:38 +02001457 ERR(session, "Missing <session-id> in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001458 rc = NC_MSG_ERROR;
1459 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001460 }
1461 break;
Michal Vasko77367452021-02-16 16:32:18 +01001462 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001463 ERR(session, "Server <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001464 rc = NC_MSG_WOULDBLOCK;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001465 break;
1466 default:
Michal Vasko77367452021-02-16 16:32:18 +01001467 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001468 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001469 }
1470
Michal Vasko77367452021-02-16 16:32:18 +01001471cleanup:
1472 ly_in_free(msg, 1);
1473 lyd_free_tree(hello);
1474 return rc;
Radek Krejci5686ff72015-10-09 13:33:56 +02001475}
1476
Michal Vasko11d142a2016-01-19 15:58:24 +01001477static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001478nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001479{
Michal Vasko77367452021-02-16 16:32:18 +01001480 struct ly_in *msg;
1481 struct lyd_node *hello = NULL, *iter;
1482 struct lyd_node_opaq *node;
1483 NC_MSG_TYPE rc = NC_MSG_HELLO;
1484 int r, ver = -1, flag = 0, timeout_io;
Michal Vasko11d142a2016-01-19 15:58:24 +01001485
Michal Vasko77367452021-02-16 16:32:18 +01001486 timeout_io = server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000;
1487 r = nc_read_msg_poll_io(session, timeout_io, &msg);
1488 switch (r) {
1489 case 1:
1490 /* parse <hello> data */
1491 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 +02001492 ERR(session, "Failed to parse client <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001493 rc = NC_MSG_ERROR;
1494 goto cleanup;
1495 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001496
Michal Vasko77367452021-02-16 16:32:18 +01001497 /* learn NETCONF version */
1498 LY_LIST_FOR(lyd_child(hello), iter) {
1499 node = (struct lyd_node_opaq *)iter;
1500
1501 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001502 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001503 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001504 ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001505 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001506 goto cleanup;
1507 }
1508
1509 if (flag) {
1510 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001511 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001512 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001513 goto cleanup;
1514 }
1515 flag = 1;
1516
Michal Vasko77367452021-02-16 16:32:18 +01001517 if ((ver = parse_cpblts(&node->node, NULL)) < 0) {
1518 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001519 goto cleanup;
1520 }
1521 session->version = ver;
1522 }
1523 break;
Michal Vasko77367452021-02-16 16:32:18 +01001524 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001525 ERR(session, "Client <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001526 rc = NC_MSG_WOULDBLOCK;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001527 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001528 default:
Michal Vasko77367452021-02-16 16:32:18 +01001529 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001530 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001531 }
1532
1533cleanup:
Michal Vasko77367452021-02-16 16:32:18 +01001534 ly_in_free(msg, 1);
1535 lyd_free_tree(hello);
1536 return rc;
Michal Vasko11d142a2016-01-19 15:58:24 +01001537}
1538
Michal Vasko71090fc2016-05-24 16:37:28 +02001539NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001540nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001541{
Michal Vasko086311b2016-01-08 09:53:11 +01001542 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001543
Michal Vasko131120a2018-05-29 15:44:02 +02001544 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001545 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001546 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001547 }
1548
Michal Vasko11d142a2016-01-19 15:58:24 +01001549 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001550 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001551 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001552 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001553 }
1554
Michal Vasko71090fc2016-05-24 16:37:28 +02001555 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001556}
Michal Vasko086311b2016-01-08 09:53:11 +01001557
Michal Vasko889162e2019-09-06 14:43:37 +02001558#ifdef NC_ENABLED_SSH
1559
Michal Vasko999b64a2019-07-10 16:06:15 +02001560static void
1561nc_ssh_init(void)
1562{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001563#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001564 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1565 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001566#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001567}
1568
1569static void
1570nc_ssh_destroy(void)
1571{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001572#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001573 FIPS_mode_set(0);
1574 CONF_modules_unload(1);
1575 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001576#endif
1577
Michal Vaskoe1e82632019-09-09 09:18:03 +02001578#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1579 ssh_finalize();
1580#endif
1581}
1582
Michal Vasko889162e2019-09-06 14:43:37 +02001583#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001584
Radek Krejci53691be2016-02-22 13:58:37 +01001585#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001586
Michal Vasko770b4362017-02-17 14:44:18 +01001587#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1588
Michal Vaskof0c92c02016-01-29 09:41:45 +01001589struct CRYPTO_dynlock_value {
1590 pthread_mutex_t lock;
1591};
1592
Michal Vaskof0c92c02016-01-29 09:41:45 +01001593static struct CRYPTO_dynlock_value *
1594tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1595{
1596 struct CRYPTO_dynlock_value *value;
1597
1598 value = malloc(sizeof *value);
1599 if (!value) {
1600 ERRMEM;
1601 return NULL;
1602 }
1603 pthread_mutex_init(&value->lock, NULL);
1604
1605 return value;
1606}
1607
1608static void
1609tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1610{
1611 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1612 * I found ignored this fact, what do I know... */
1613 if (mode & CRYPTO_LOCK) {
1614 pthread_mutex_lock(&l->lock);
1615 } else {
1616 pthread_mutex_unlock(&l->lock);
1617 }
1618}
1619
1620static void
1621tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1622{
1623 pthread_mutex_destroy(&l->lock);
1624 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001625}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001626
Michal Vasko770b4362017-02-17 14:44:18 +01001627#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001628
Michal Vasko8f0c0282016-02-29 10:17:14 +01001629#endif /* NC_ENABLED_TLS */
1630
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001631#if defined (NC_ENABLED_TLS) && !defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001632
Michal Vasko770b4362017-02-17 14:44:18 +01001633#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001634static pthread_mutex_t *tls_locks;
1635
1636static void
1637tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1638{
1639 if (mode & CRYPTO_LOCK) {
1640 pthread_mutex_lock(tls_locks + n);
1641 } else {
1642 pthread_mutex_unlock(tls_locks + n);
1643 }
1644}
1645
1646static void
1647tls_thread_id_func(CRYPTO_THREADID *tid)
1648{
1649 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1650}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001651
Michal Vasko770b4362017-02-17 14:44:18 +01001652#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001653
1654static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001655nc_tls_init(void)
1656{
Rosen Penev4f552d62019-06-26 16:10:43 -07001657#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001658 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001659 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001660 SSL_library_init();
1661
Michal Vaskof89948c2018-01-04 09:19:46 +01001662 int i;
1663
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001664 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001665 if (!tls_locks) {
1666 ERRMEM;
1667 return;
1668 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001669 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1670 pthread_mutex_init(tls_locks + i, NULL);
1671 }
1672
Michal Vaskof0c92c02016-01-29 09:41:45 +01001673 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001674 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001675
1676 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1677 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1678 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001679#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001680}
1681
Michal Vasko8f0c0282016-02-29 10:17:14 +01001682static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001683nc_tls_destroy(void)
1684{
Rosen Penev4f552d62019-06-26 16:10:43 -07001685#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001686 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001687 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001688 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001689 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001690 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001691#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001692 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001693#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1694 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001695#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001696
Michal Vaskof89948c2018-01-04 09:19:46 +01001697 int i;
1698
Michal Vaskob6e37262016-02-25 14:49:00 +01001699 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001700 CRYPTO_set_locking_callback(NULL);
1701 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1702 pthread_mutex_destroy(tls_locks + i);
1703 }
1704 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001705
1706 CRYPTO_set_dynlock_create_callback(NULL);
1707 CRYPTO_set_dynlock_lock_callback(NULL);
1708 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001709#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001710}
1711
Michal Vasko8f0c0282016-02-29 10:17:14 +01001712#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001713
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001714#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001715
Michal Vasko8f0c0282016-02-29 10:17:14 +01001716static void
Michal Vasko5e228792016-02-03 15:30:24 +01001717nc_ssh_tls_init(void)
1718{
Rosen Penev4f552d62019-06-26 16:10:43 -07001719#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001720 SSL_load_error_strings();
1721 ERR_load_BIO_strings();
1722 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001723#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001724
1725 nc_ssh_init();
1726
Michal Vaskoe1e82632019-09-09 09:18:03 +02001727#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001728 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1729 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1730 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001731#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001732}
1733
Michal Vasko8f0c0282016-02-29 10:17:14 +01001734static void
Michal Vasko5e228792016-02-03 15:30:24 +01001735nc_ssh_tls_destroy(void)
1736{
Rosen Penev4f552d62019-06-26 16:10:43 -07001737#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001738 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001739# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001740 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001741# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001742 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001743# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001744#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001745
1746 nc_ssh_destroy();
1747
Michal Vaskoe1e82632019-09-09 09:18:03 +02001748#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001749 CRYPTO_set_dynlock_create_callback(NULL);
1750 CRYPTO_set_dynlock_lock_callback(NULL);
1751 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001752#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001753}
1754
Radek Krejci53691be2016-02-22 13:58:37 +01001755#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001756
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001757#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001758
1759API void
1760nc_thread_destroy(void)
1761{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001762 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001763 // CRYPTO_cleanup_all_ex_data();
Michal Vasko8f0c0282016-02-29 10:17:14 +01001764
Michal Vasko770b4362017-02-17 14:44:18 +01001765#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1766 CRYPTO_THREADID crypto_tid;
1767
Michal Vasko8f0c0282016-02-29 10:17:14 +01001768 CRYPTO_THREADID_current(&crypto_tid);
1769 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001770#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001771}
1772
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001773#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1774
1775void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001776nc_init(void)
1777{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001778#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001779 nc_ssh_tls_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001780#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001781 nc_ssh_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001782#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001783 nc_tls_init();
1784#endif
1785}
1786
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001787void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001788nc_destroy(void)
1789{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001790#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001791 nc_ssh_tls_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001792#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001793 nc_ssh_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001794#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001795 nc_tls_destroy();
1796#endif
1797}