blob: 72d39b088334677a8c912340b78994e5ebb7807b [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) {
666 WRN(session, "Missing ietf-netconf schema in context, unable to send <close-session>.");
667 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
Radek Krejci695d4fa2015-10-22 13:23:54 +0200706API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100707nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200708{
Michal Vasko01130bd2021-08-26 11:47:38 +0200709 int r, i, rpc_locked = 0, msgs_locked = 0, sock = -1, timeout;
Michal Vasko428087d2016-01-14 16:04:28 +0100710 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100711 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vasko4589bbe2016-01-29 09:41:30 +0100712 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100713 struct nc_msg_cont *contiter;
Michal Vasko77367452021-02-16 16:32:18 +0100714 struct ly_in *msg;
roman6ece9c52022-06-22 09:29:17 +0200715 struct timespec ts;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200716 void *p;
717
Michal Vasko428087d2016-01-14 16:04:28 +0100718 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200719 return;
720 }
721
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200722 /* stop notifications thread if any */
723 if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
724 /* let the thread know it should quit */
725 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 2);
726
727 /* wait for it */
roman6ece9c52022-06-22 09:29:17 +0200728 nc_gettimespec_mono_add(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200729 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
730 usleep(NC_TIMEOUT_STEP);
Michal Vasko60d8ffb2022-07-21 11:08:34 +0200731 if (nc_difftimespec_mono_cur(&ts) < 1) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200732 ERR(session, "Waiting for notification thread exit failed (timed out).");
733 break;
734 }
735 }
Michal Vasko86d357c2016-03-11 13:46:38 +0100736 }
737
Michal Vaskoacf98472021-02-04 15:33:57 +0100738 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200739 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100740 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100741 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200742 } else if (r) {
743 rpc_locked = 1;
Michal Vasko96a28a32021-02-04 15:35:20 +0100744 } else {
745 /* else failed to lock it, too bad */
Michal Vasko05532772021-06-03 12:12:38 +0200746 ERR(session, "Freeing a session while an RPC is being processed.");
Michal Vasko96a28a32021-02-04 15:35:20 +0100747 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200748 }
749
Michal Vasko8c247822020-09-07 13:23:23 +0200750 if (session->side == NC_CLIENT) {
Michal Vasko01130bd2021-08-26 11:47:38 +0200751 timeout = NC_SESSION_FREE_LOCK_TIMEOUT;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200752
Michal Vasko01130bd2021-08-26 11:47:38 +0200753 /* MSGS LOCK */
754 r = nc_session_client_msgs_lock(session, &timeout, __func__);
755 if (r == -1) {
756 return;
757 } else if (r) {
758 msgs_locked = 1;
759 } else {
760 /* else failed to lock it, too bad */
761 ERR(session, "Freeing a session while messages are being received.");
762 }
763
764 /* cleanup message queue */
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200765 for (contiter = session->opts.client.msgs; contiter; ) {
Michal Vasko77367452021-02-16 16:32:18 +0100766 ly_in_free(contiter->msg, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200767
Michal Vaskoad611702015-12-03 13:41:51 +0100768 p = contiter;
769 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200770 free(p);
771 }
772
Michal Vasko01130bd2021-08-26 11:47:38 +0200773 if (msgs_locked) {
774 /* MSGS UNLOCK */
775 nc_session_client_msgs_unlock(session, __func__);
776 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200777
Michal Vasko8c247822020-09-07 13:23:23 +0200778 if (session->status == NC_STATUS_RUNNING) {
Michal Vasko9c6d38c2021-09-03 13:02:53 +0200779 /* receive any leftover messages */
780 while (nc_read_msg_poll_io(session, 0, &msg) == 1) {
781 ly_in_free(msg, 1);
782 }
783
Michal Vasko8c247822020-09-07 13:23:23 +0200784 /* send closing info to the other side */
Michal Vaskod4da3632022-05-25 11:49:10 +0200785 nc_session_free_close_session(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200786 }
787
788 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200789 if (session->opts.client.cpblts) {
790 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200791 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200792 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200793 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200794 }
795 }
796
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100797 if (session->data && data_free) {
798 data_free(session->data);
799 }
800
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200801 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
802 /* CH LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100803 pthread_mutex_lock(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200804 }
805
Michal Vasko86d357c2016-03-11 13:46:38 +0100806 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200807 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200808
Michal Vaskofeccb312022-03-24 15:24:59 +0100809 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100810 pthread_cond_signal(&session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200811
roman6ece9c52022-06-22 09:29:17 +0200812 nc_gettimespec_real_add(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vasko0db3db52021-03-03 10:45:42 +0100813
814 /* wait for CH thread to actually wake up and terminate */
815 r = 0;
Michal Vaskofeccb312022-03-24 15:24:59 +0100816 while (!r && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vasko0db3db52021-03-03 10:45:42 +0100817 r = pthread_cond_timedwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, &ts);
818 }
Michal Vasko0db3db52021-03-03 10:45:42 +0100819 if (r) {
Michal Vasko05532772021-06-03 12:12:38 +0200820 ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r));
Michal Vasko3f05a092018-03-13 10:39:49 +0100821 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200822 }
823
Michal Vaskofeccb312022-03-24 15:24:59 +0100824 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
825 /* CH UNLOCK */
826 pthread_mutex_unlock(&session->opts.server.ch_lock);
827 }
828
Michal Vasko428087d2016-01-14 16:04:28 +0100829 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200830
831 /* transport implementation cleanup */
832 switch (session->ti_type) {
833 case NC_TI_FD:
834 /* nothing needed - file descriptors were provided by caller,
835 * so it is up to the caller to close them correctly
836 * TODO use callbacks
837 */
Michal Vasko3512e402016-01-28 16:22:34 +0100838 /* just to avoid compiler warning */
839 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100840 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200841 break;
842
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200843 case NC_TI_UNIX:
844 sock = session->ti.unixsock.sock;
845 (void)connected;
846 (void)siter;
847 break;
848
Radek Krejci53691be2016-02-22 13:58:37 +0100849#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200850 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100851 if (connected) {
852 ssh_channel_free(session->ti.libssh.channel);
853 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200854 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
855 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200856 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
Radek Krejci695d4fa2015-10-22 13:23:54 +0200857 */
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200858 /* SESSION IO LOCK */
859 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
860
Michal Vasko96164bf2016-01-21 15:41:58 +0100861 multisession = 0;
862 if (session->ti.libssh.next) {
863 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
864 if (siter->status != NC_STATUS_STARTING) {
865 multisession = 1;
866 break;
867 }
868 }
869 }
870
871 if (!multisession) {
872 /* it's not multisession yet, but we still need to free the starting sessions */
873 if (session->ti.libssh.next) {
874 do {
875 siter = session->ti.libssh.next;
876 session->ti.libssh.next = siter->ti.libssh.next;
877
878 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vaskoe38655d2021-11-12 15:41:16 +0100879 free(siter->username);
880 free(siter->host);
881 if (!(siter->flags & NC_SESSION_SHAREDCTX)) {
882 ly_ctx_destroy((struct ly_ctx *)siter->ctx);
Michal Vasko96164bf2016-01-21 15:41:58 +0100883 }
884
885 free(siter);
886 } while (session->ti.libssh.next != session);
887 }
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100888 /* remember sock so we can close it */
889 sock = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100890 if (connected) {
891 ssh_disconnect(session->ti.libssh.session);
Michal Vasko06e0fc22019-05-09 09:32:27 +0200892 sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100893 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200894 ssh_free(session->ti.libssh.session);
895 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200896 /* remove the session from the list */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200897 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {}
Michal Vaskoaec4f212015-10-26 15:37:45 +0100898 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200899 /* there will be only one session */
900 siter->ti.libssh.next = NULL;
901 } else {
902 /* there are still multiple sessions, keep the ring list */
903 siter->ti.libssh.next = session->ti.libssh.next;
904 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100905 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
906 if (session->flags & NC_SESSION_SSH_MSG_CB) {
Michal Vasko5e0edd82020-07-29 15:26:13 +0200907 siter = session->ti.libssh.next;
908 while (siter && siter->status != NC_STATUS_RUNNING) {
Michal Vasko96164bf2016-01-21 15:41:58 +0100909 if (siter->ti.libssh.next == session) {
910 ERRINT;
911 break;
912 }
Michal Vasko5e0edd82020-07-29 15:26:13 +0200913 siter = siter->ti.libssh.next;
Michal Vasko96164bf2016-01-21 15:41:58 +0100914 }
Michal Vasko5e0edd82020-07-29 15:26:13 +0200915 /* siter may be NULL in case all the sessions terminated at the same time (socket was disconnected),
916 * we set session to NULL because we do not expect any new message to arrive */
Michal Vasko96164bf2016-01-21 15:41:58 +0100917 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
Michal Vasko5e0edd82020-07-29 15:26:13 +0200918 if (siter) {
919 siter->flags |= NC_SESSION_SSH_MSG_CB;
920 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100921 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200922 }
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200923
924 /* SESSION IO UNLOCK */
925 if (r == 1) {
926 nc_session_io_unlock(session, __func__);
927 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200928 break;
929#endif
930
Radek Krejci53691be2016-02-22 13:58:37 +0100931#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200932 case NC_TI_OPENSSL:
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100933 /* remember sock so we can close it */
934 sock = SSL_get_fd(session->ti.tls);
935
Michal Vasko428087d2016-01-14 16:04:28 +0100936 if (connected) {
937 SSL_shutdown(session->ti.tls);
938 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200939 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100940
Michal Vasko2e6defd2016-10-07 15:48:15 +0200941 if (session->side == NC_SERVER) {
942 X509_free(session->opts.server.client_cert);
943 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200944 break;
945#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100946 case NC_TI_NONE:
Michal Vasko428087d2016-01-14 16:04:28 +0100947 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200948 }
Michal Vasko428087d2016-01-14 16:04:28 +0100949
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100950 /* close socket separately */
951 if (sock > -1) {
952 close(sock);
953 }
954
Michal Vasko93224072021-11-09 12:14:28 +0100955 free(session->username);
956 free(session->host);
957 free(session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200958
959 /* final cleanup */
Michal Vaskoacf98472021-02-04 15:33:57 +0100960 if (session->side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200961 pthread_mutex_destroy(&session->opts.server.ntf_status_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200962 if (rpc_locked) {
963 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100964 }
Michal Vaskoacf98472021-02-04 15:33:57 +0100965 pthread_mutex_destroy(&session->opts.server.rpc_lock);
966 pthread_cond_destroy(&session->opts.server.rpc_cond);
Michal Vasko131120a2018-05-29 15:44:02 +0200967 }
968
969 if (session->io_lock && !multisession) {
970 pthread_mutex_destroy(session->io_lock);
971 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200972 }
973
974 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko93224072021-11-09 12:14:28 +0100975 ly_ctx_destroy((struct ly_ctx *)session->ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200976 }
977
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200978 if (session->side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100979 /* free CH synchronization structures */
980 pthread_cond_destroy(&session->opts.server.ch_cond);
981 pthread_mutex_destroy(&session->opts.server.ch_lock);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200982 } else {
983 pthread_mutex_destroy(&session->opts.client.msgs_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200984 }
985
Radek Krejci695d4fa2015-10-22 13:23:54 +0200986 free(session);
987}
988
Michal Vasko086311b2016-01-08 09:53:11 +0100989static void
Michal Vasko93224072021-11-09 12:14:28 +0100990add_cpblt(const char *capab, char ***cpblts, int *size, int *count)
Michal Vasko086311b2016-01-08 09:53:11 +0100991{
Radek Krejci658782b2016-12-04 22:04:55 +0100992 size_t len;
993 int i;
994 char *p;
995
996 if (capab) {
997 /* check if already present */
998 p = strchr(capab, '?');
999 if (p) {
1000 len = p - capab;
1001 } else {
1002 len = strlen(capab);
1003 }
1004 for (i = 0; i < *count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001005 if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) {
Radek Krejci658782b2016-12-04 22:04:55 +01001006 /* already present, do not duplicate it */
1007 return;
1008 }
1009 }
1010 }
1011
1012 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +01001013 if (*count == *size) {
1014 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001015 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
1016 if (!(*cpblts)) {
1017 ERRMEM;
1018 return;
1019 }
Michal Vasko086311b2016-01-08 09:53:11 +01001020 }
1021
Michal Vasko93224072021-11-09 12:14:28 +01001022 (*cpblts)[*count] = capab ? strdup(capab) : NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001023 ++(*count);
1024}
1025
Michal Vasko93224072021-11-09 12:14:28 +01001026API char **
1027nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +01001028{
Michal Vasko93224072021-11-09 12:14:28 +01001029 char **cpblts;
Michal Vasko77367452021-02-16 16:32:18 +01001030 const struct lys_module *mod;
1031 struct lysp_feature *feat;
1032 int size = 10, count, features_count = 0, dev_count = 0, str_len, len;
Michal Vasko1440a742021-03-31 11:11:03 +02001033 uint32_t i, u;
Michal Vasko77367452021-02-16 16:32:18 +01001034 LY_ARRAY_COUNT_TYPE v;
Michal Vasko1440a742021-03-31 11:11:03 +02001035 char *yl_content_id;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001036
Radek Krejci24a18412018-05-16 15:09:10 +02001037#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +02001038 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001039
Michal Vasko4ffa3b22016-05-24 16:36:25 +02001040 if (!ctx) {
1041 ERRARG("ctx");
1042 return NULL;
1043 }
1044
Michal Vasko086311b2016-01-08 09:53:11 +01001045 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001046 if (!cpblts) {
1047 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +02001048 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001049 }
Michal Vasko93224072021-11-09 12:14:28 +01001050 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1051 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko086311b2016-01-08 09:53:11 +01001052 count = 2;
1053
1054 /* capabilities */
1055
Michal Vasko77367452021-02-16 16:32:18 +01001056 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf");
Michal Vasko086311b2016-01-08 09:53:11 +01001057 if (mod) {
Michal Vasko77367452021-02-16 16:32:18 +01001058 if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001059 add_cpblt("urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001060 }
Michal Vasko77367452021-02-16 16:32:18 +01001061 if (lys_feature_value(mod, "candidate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001062 add_cpblt("urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko77367452021-02-16 16:32:18 +01001063 if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001064 add_cpblt("urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001065 }
1066 }
Michal Vasko77367452021-02-16 16:32:18 +01001067 if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001068 add_cpblt("urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001069 }
Michal Vasko77367452021-02-16 16:32:18 +01001070 if (lys_feature_value(mod, "validate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001071 add_cpblt("urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001072 }
Michal Vasko77367452021-02-16 16:32:18 +01001073 if (lys_feature_value(mod, "startup") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001074 add_cpblt("urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001075 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001076
1077 /* The URL capability must be set manually using nc_server_set_capability()
1078 * because of the need for supported protocols to be included.
1079 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1080 */
Michal Vasko77367452021-02-16 16:32:18 +01001081 // if (lys_feature_value(mod, "url") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001082 // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
mekleoa8de5e92020-02-13 09:05:56 +01001083 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001084
Michal Vasko77367452021-02-16 16:32:18 +01001085 if (lys_feature_value(mod, "xpath") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001086 add_cpblt("urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001087 }
1088 }
1089
Michal Vasko77367452021-02-16 16:32:18 +01001090 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01001091 if (mod) {
1092 if (!server_opts.wd_basic_mode) {
Michal Vasko05532772021-06-03 12:12:38 +02001093 VRB(NULL, "with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode.");
Michal Vasko086311b2016-01-08 09:53:11 +01001094 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001095 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +01001096 switch (server_opts.wd_basic_mode) {
1097 case NC_WD_ALL:
1098 strcat(str, "?basic-mode=report-all");
1099 break;
1100 case NC_WD_TRIM:
1101 strcat(str, "?basic-mode=trim");
1102 break;
1103 case NC_WD_EXPLICIT:
1104 strcat(str, "?basic-mode=explicit");
1105 break;
1106 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001107 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001108 break;
1109 }
1110
1111 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001112 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +01001113 if (server_opts.wd_also_supported & NC_WD_ALL) {
1114 strcat(str, "report-all,");
1115 }
1116 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
1117 strcat(str, "report-all-tagged,");
1118 }
1119 if (server_opts.wd_also_supported & NC_WD_TRIM) {
1120 strcat(str, "trim,");
1121 }
1122 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
1123 strcat(str, "explicit,");
1124 }
1125 str[strlen(str) - 1] = '\0';
1126
Michal Vasko93224072021-11-09 12:14:28 +01001127 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001128 }
1129 }
1130 }
1131
Radek Krejci658782b2016-12-04 22:04:55 +01001132 /* other capabilities */
1133 for (u = 0; u < server_opts.capabilities_count; u++) {
Michal Vasko93224072021-11-09 12:14:28 +01001134 add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001135 }
1136
1137 /* models */
Michal Vasko1440a742021-03-31 11:11:03 +02001138 u = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001139 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001140 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vasko77367452021-02-16 16:32:18 +01001141 if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) {
Michal Vasko05532772021-06-03 12:12:38 +02001142 ERR(NULL, "Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
Michal Vaskod5ada122020-03-19 18:28:06 +01001143 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001144 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001145
Michal Vasko1440a742021-03-31 11:11:03 +02001146 /* get content-id */
1147 if (server_opts.content_id_clb) {
1148 yl_content_id = server_opts.content_id_clb(server_opts.content_id_data);
1149 if (!yl_content_id) {
1150 ERRMEM;
1151 goto error;
1152 }
1153 } else {
1154 yl_content_id = malloc(11);
1155 if (!yl_content_id) {
1156 ERRMEM;
1157 goto error;
1158 }
1159 sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx));
1160 }
1161
Michal Vasko77367452021-02-16 16:32:18 +01001162 if (!strcmp(mod->revision, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001163 /* new one (capab defined in RFC 8526 section 2) */
Michal Vasko1440a742021-03-31 11:11:03 +02001164 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s",
1165 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001166 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001167 } else {
1168 /* old one (capab defined in RFC 7950 section 5.6.4) */
Michal Vasko1440a742021-03-31 11:11:03 +02001169 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s",
1170 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001171 add_cpblt(str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001172 }
Michal Vasko1440a742021-03-31 11:11:03 +02001173 free(yl_content_id);
Radek Krejci24a18412018-05-16 15:09:10 +02001174 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001175 } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) {
Radek Krejci24a18412018-05-16 15:09:10 +02001176 /* skip YANG 1.1 schemas */
1177 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001178 } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) {
Radek Krejci24a18412018-05-16 15:09:10 +02001179 /* skip YANG 1.0 schemas */
1180 continue;
1181 }
Michal Vasko086311b2016-01-08 09:53:11 +01001182
Michal Vasko77367452021-02-16 16:32:18 +01001183 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "",
1184 mod->revision ? mod->revision : "");
Radek Krejci24a18412018-05-16 15:09:10 +02001185
Michal Vaskodafdc742020-03-11 16:15:59 +01001186 features_count = 0;
1187 i = 0;
1188 feat = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001189 while ((feat = lysp_feature_next(feat, mod->parsed, &i))) {
Michal Vaskodafdc742020-03-11 16:15:59 +01001190 if (!(feat->flags & LYS_FENABLED)) {
1191 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001192 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001193 if (!features_count) {
1194 strcat(str, "&features=");
1195 str_len += 10;
1196 }
1197 len = strlen(feat->name);
1198 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1199 ERRINT;
1200 break;
1201 }
1202 if (features_count) {
1203 strcat(str, ",");
1204 ++str_len;
1205 }
1206 strcat(str, feat->name);
1207 str_len += len;
1208 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001209 }
Michal Vasko086311b2016-01-08 09:53:11 +01001210
Michal Vasko77367452021-02-16 16:32:18 +01001211 if (mod->deviated_by) {
Radek Krejci24a18412018-05-16 15:09:10 +02001212 strcat(str, "&deviations=");
1213 str_len += 12;
1214 dev_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +01001215 LY_ARRAY_FOR(mod->deviated_by, v) {
1216 len = strlen(mod->deviated_by[v]->name);
1217 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1218 ERRINT;
1219 break;
Radek Krejci24a18412018-05-16 15:09:10 +02001220 }
Michal Vasko77367452021-02-16 16:32:18 +01001221 if (dev_count) {
1222 strcat(str, ",");
1223 ++str_len;
Radek Krejci24a18412018-05-16 15:09:10 +02001224 }
Michal Vasko77367452021-02-16 16:32:18 +01001225 strcat(str, mod->deviated_by[v]->name);
1226 str_len += len;
1227 dev_count++;
Radek Krejci24a18412018-05-16 15:09:10 +02001228 }
1229 }
1230
Michal Vasko93224072021-11-09 12:14:28 +01001231 add_cpblt(str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +02001232 }
Michal Vasko086311b2016-01-08 09:53:11 +01001233
1234 /* ending NULL capability */
Michal Vasko93224072021-11-09 12:14:28 +01001235 add_cpblt(NULL, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001236
1237 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001238
1239error:
Radek Krejcif906e412017-09-22 14:44:45 +02001240 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001241 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001242}
1243
Michal Vasko93224072021-11-09 12:14:28 +01001244API char **
1245nc_server_get_cpblts(const struct ly_ctx *ctx)
Radek Krejci24a18412018-05-16 15:09:10 +02001246{
1247 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1248}
1249
Radek Krejci695d4fa2015-10-22 13:23:54 +02001250static int
Michal Vasko77367452021-02-16 16:32:18 +01001251parse_cpblts(struct lyd_node *capabilities, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001252{
Michal Vasko77367452021-02-16 16:32:18 +01001253 struct lyd_node *iter;
1254 struct lyd_node_opaq *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001255 int ver = -1, i = 0;
1256 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001257
1258 if (list) {
1259 /* get the storage for server's capabilities */
Michal Vasko77367452021-02-16 16:32:18 +01001260 LY_LIST_FOR(lyd_child(capabilities), iter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001261 i++;
1262 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001263 /* last item remains NULL */
1264 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001265 if (!*list) {
1266 ERRMEM;
1267 return -1;
1268 }
1269 i = 0;
1270 }
1271
Michal Vasko77367452021-02-16 16:32:18 +01001272 LY_LIST_FOR(lyd_child(capabilities), iter) {
1273 cpblt = (struct lyd_node_opaq *)iter;
1274
1275 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 +02001276 ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001277 return -1;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001278 }
1279
Michal Vasko156d3272017-04-11 11:46:49 +02001280 /* skip leading/trailing whitespaces */
Michal Vasko77367452021-02-16 16:32:18 +01001281 for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {}
1282 for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {}
1283 if (!cpb_start[0] || (cpb_end == cpblt->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001284 ERR(NULL, "Empty capability \"%s\" received.", cpblt->value);
Michal Vasko156d3272017-04-11 11:46:49 +02001285 return -1;
1286 }
1287
Radek Krejci695d4fa2015-10-22 13:23:54 +02001288 /* detect NETCONF version */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001289 if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001290 ver = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001291 } 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 +02001292 ver = 1;
1293 }
1294
1295 /* store capabilities */
1296 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001297 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1298 if (!(*list)[i]) {
1299 ERRMEM;
1300 return -1;
1301 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001302 i++;
1303 }
1304 }
1305
1306 if (ver == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001307 ERR(NULL, "Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001308 }
1309
1310 return ver;
1311}
1312
1313static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001314nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001315{
Michal Vasko131120a2018-05-29 15:44:02 +02001316 NC_MSG_TYPE ret;
1317 int i, io_timeout;
Michal Vasko93224072021-11-09 12:14:28 +01001318 char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001319 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001320
Michal Vasko131120a2018-05-29 15:44:02 +02001321 if (session->side == NC_CLIENT) {
1322 /* client side hello - send only NETCONF base capabilities */
1323 cpblts = malloc(3 * sizeof *cpblts);
1324 if (!cpblts) {
1325 ERRMEM;
1326 return NC_MSG_ERROR;
1327 }
Michal Vasko93224072021-11-09 12:14:28 +01001328 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1329 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko131120a2018-05-29 15:44:02 +02001330 cpblts[2] = NULL;
1331
1332 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1333 sid = NULL;
1334 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001335 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0);
Michal Vasko5b24b6b2020-12-04 09:29:47 +01001336 if (!cpblts) {
1337 return NC_MSG_ERROR;
1338 }
Michal Vasko131120a2018-05-29 15:44:02 +02001339
1340 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1341 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001342 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001343
Michal Vasko131120a2018-05-29 15:44:02 +02001344 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001345
Michal Vasko086311b2016-01-08 09:53:11 +01001346 for (i = 0; cpblts[i]; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001347 free(cpblts[i]);
Michal Vasko086311b2016-01-08 09:53:11 +01001348 }
1349 free(cpblts);
1350
Michal Vasko131120a2018-05-29 15:44:02 +02001351 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001352}
1353
1354static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001355nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001356{
Michal Vasko77367452021-02-16 16:32:18 +01001357 struct ly_in *msg;
1358 struct lyd_node *hello = NULL, *iter;
1359 struct lyd_node_opaq *node;
1360 int r, ver = -1, flag = 0;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001361 char *str;
1362 long long int id;
Michal Vasko77367452021-02-16 16:32:18 +01001363 NC_MSG_TYPE rc = NC_MSG_HELLO;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001364
Michal Vasko77367452021-02-16 16:32:18 +01001365 r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg);
1366 switch (r) {
1367 case 1:
Radek Krejci695d4fa2015-10-22 13:23:54 +02001368 /* parse <hello> data */
Michal Vasko77367452021-02-16 16:32:18 +01001369 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 +02001370 ERR(session, "Failed to parse server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001371 rc = NC_MSG_ERROR;
1372 goto cleanup;
1373 }
1374
1375 LY_LIST_FOR(lyd_child(hello), iter) {
1376 node = (struct lyd_node_opaq *)iter;
1377
1378 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001379 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001380 } else if (!strcmp(node->name.name, "session-id")) {
1381 if (!node->value || !strlen(node->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001382 ERR(session, "No value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001383 rc = NC_MSG_ERROR;
1384 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001385 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001386 str = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001387 id = strtoll(node->value, &str, 10);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001388 if (*str || (id < 1) || (id > UINT32_MAX)) {
Michal Vasko05532772021-06-03 12:12:38 +02001389 ERR(session, "Invalid value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001390 rc = NC_MSG_ERROR;
1391 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001392 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001393 session->id = (uint32_t)id;
1394 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001395 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001396 ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001397 rc = NC_MSG_ERROR;
1398 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001399 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001400
1401 if (flag) {
1402 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001403 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001404 rc = NC_MSG_ERROR;
1405 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001406 }
1407 flag = 1;
1408
Michal Vasko77367452021-02-16 16:32:18 +01001409 if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) {
1410 rc = NC_MSG_ERROR;
1411 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001412 }
1413 session->version = ver;
1414 }
1415
1416 if (!session->id) {
Michal Vasko05532772021-06-03 12:12:38 +02001417 ERR(session, "Missing <session-id> in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001418 rc = NC_MSG_ERROR;
1419 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001420 }
1421 break;
Michal Vasko77367452021-02-16 16:32:18 +01001422 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001423 ERR(session, "Server <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001424 rc = NC_MSG_WOULDBLOCK;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001425 break;
1426 default:
Michal Vasko77367452021-02-16 16:32:18 +01001427 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001428 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001429 }
1430
Michal Vasko77367452021-02-16 16:32:18 +01001431cleanup:
1432 ly_in_free(msg, 1);
1433 lyd_free_tree(hello);
1434 return rc;
Radek Krejci5686ff72015-10-09 13:33:56 +02001435}
1436
Michal Vasko11d142a2016-01-19 15:58:24 +01001437static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001438nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001439{
Michal Vasko77367452021-02-16 16:32:18 +01001440 struct ly_in *msg;
1441 struct lyd_node *hello = NULL, *iter;
1442 struct lyd_node_opaq *node;
1443 NC_MSG_TYPE rc = NC_MSG_HELLO;
1444 int r, ver = -1, flag = 0, timeout_io;
Michal Vasko11d142a2016-01-19 15:58:24 +01001445
Michal Vasko77367452021-02-16 16:32:18 +01001446 timeout_io = server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000;
1447 r = nc_read_msg_poll_io(session, timeout_io, &msg);
1448 switch (r) {
1449 case 1:
1450 /* parse <hello> data */
1451 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 +02001452 ERR(session, "Failed to parse client <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001453 rc = NC_MSG_ERROR;
1454 goto cleanup;
1455 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001456
Michal Vasko77367452021-02-16 16:32:18 +01001457 /* learn NETCONF version */
1458 LY_LIST_FOR(lyd_child(hello), iter) {
1459 node = (struct lyd_node_opaq *)iter;
1460
1461 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001462 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001463 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001464 ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001465 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001466 goto cleanup;
1467 }
1468
1469 if (flag) {
1470 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001471 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001472 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001473 goto cleanup;
1474 }
1475 flag = 1;
1476
Michal Vasko77367452021-02-16 16:32:18 +01001477 if ((ver = parse_cpblts(&node->node, NULL)) < 0) {
1478 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001479 goto cleanup;
1480 }
1481 session->version = ver;
1482 }
1483 break;
Michal Vasko77367452021-02-16 16:32:18 +01001484 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001485 ERR(session, "Client <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001486 rc = NC_MSG_WOULDBLOCK;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001487 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001488 default:
Michal Vasko77367452021-02-16 16:32:18 +01001489 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001490 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001491 }
1492
1493cleanup:
Michal Vasko77367452021-02-16 16:32:18 +01001494 ly_in_free(msg, 1);
1495 lyd_free_tree(hello);
1496 return rc;
Michal Vasko11d142a2016-01-19 15:58:24 +01001497}
1498
Michal Vasko71090fc2016-05-24 16:37:28 +02001499NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001500nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001501{
Michal Vasko086311b2016-01-08 09:53:11 +01001502 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001503
Michal Vasko131120a2018-05-29 15:44:02 +02001504 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001505 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001506 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001507 }
1508
Michal Vasko11d142a2016-01-19 15:58:24 +01001509 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001510 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001511 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001512 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001513 }
1514
Michal Vasko71090fc2016-05-24 16:37:28 +02001515 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001516}
Michal Vasko086311b2016-01-08 09:53:11 +01001517
Michal Vasko889162e2019-09-06 14:43:37 +02001518#ifdef NC_ENABLED_SSH
1519
Michal Vasko999b64a2019-07-10 16:06:15 +02001520static void
1521nc_ssh_init(void)
1522{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001523#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001524 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1525 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001526#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001527}
1528
1529static void
1530nc_ssh_destroy(void)
1531{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001532#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001533 FIPS_mode_set(0);
1534 CONF_modules_unload(1);
1535 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001536#endif
1537
Michal Vaskoe1e82632019-09-09 09:18:03 +02001538#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1539 ssh_finalize();
1540#endif
1541}
1542
Michal Vasko889162e2019-09-06 14:43:37 +02001543#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001544
Radek Krejci53691be2016-02-22 13:58:37 +01001545#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001546
Michal Vasko770b4362017-02-17 14:44:18 +01001547#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1548
Michal Vaskof0c92c02016-01-29 09:41:45 +01001549struct CRYPTO_dynlock_value {
1550 pthread_mutex_t lock;
1551};
1552
Michal Vaskof0c92c02016-01-29 09:41:45 +01001553static struct CRYPTO_dynlock_value *
1554tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1555{
1556 struct CRYPTO_dynlock_value *value;
1557
1558 value = malloc(sizeof *value);
1559 if (!value) {
1560 ERRMEM;
1561 return NULL;
1562 }
1563 pthread_mutex_init(&value->lock, NULL);
1564
1565 return value;
1566}
1567
1568static void
1569tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1570{
1571 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1572 * I found ignored this fact, what do I know... */
1573 if (mode & CRYPTO_LOCK) {
1574 pthread_mutex_lock(&l->lock);
1575 } else {
1576 pthread_mutex_unlock(&l->lock);
1577 }
1578}
1579
1580static void
1581tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1582{
1583 pthread_mutex_destroy(&l->lock);
1584 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001585}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001586
Michal Vasko770b4362017-02-17 14:44:18 +01001587#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001588
Michal Vasko8f0c0282016-02-29 10:17:14 +01001589#endif /* NC_ENABLED_TLS */
1590
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001591#if defined (NC_ENABLED_TLS) && !defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001592
Michal Vasko770b4362017-02-17 14:44:18 +01001593#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001594static pthread_mutex_t *tls_locks;
1595
1596static void
1597tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1598{
1599 if (mode & CRYPTO_LOCK) {
1600 pthread_mutex_lock(tls_locks + n);
1601 } else {
1602 pthread_mutex_unlock(tls_locks + n);
1603 }
1604}
1605
1606static void
1607tls_thread_id_func(CRYPTO_THREADID *tid)
1608{
1609 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1610}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001611
Michal Vasko770b4362017-02-17 14:44:18 +01001612#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001613
1614static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001615nc_tls_init(void)
1616{
Rosen Penev4f552d62019-06-26 16:10:43 -07001617#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001618 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001619 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001620 SSL_library_init();
1621
Michal Vaskof89948c2018-01-04 09:19:46 +01001622 int i;
1623
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001624 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001625 if (!tls_locks) {
1626 ERRMEM;
1627 return;
1628 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001629 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1630 pthread_mutex_init(tls_locks + i, NULL);
1631 }
1632
Michal Vaskof0c92c02016-01-29 09:41:45 +01001633 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001634 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001635
1636 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1637 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1638 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001639#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001640}
1641
Michal Vasko8f0c0282016-02-29 10:17:14 +01001642static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001643nc_tls_destroy(void)
1644{
Rosen Penev4f552d62019-06-26 16:10:43 -07001645#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001646 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001647 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001648 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001649 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001650 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001651#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001652 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001653#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1654 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001655#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001656
Michal Vaskof89948c2018-01-04 09:19:46 +01001657 int i;
1658
Michal Vaskob6e37262016-02-25 14:49:00 +01001659 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001660 CRYPTO_set_locking_callback(NULL);
1661 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1662 pthread_mutex_destroy(tls_locks + i);
1663 }
1664 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001665
1666 CRYPTO_set_dynlock_create_callback(NULL);
1667 CRYPTO_set_dynlock_lock_callback(NULL);
1668 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001669#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001670}
1671
Michal Vasko8f0c0282016-02-29 10:17:14 +01001672#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001673
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001674#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001675
Michal Vasko8f0c0282016-02-29 10:17:14 +01001676static void
Michal Vasko5e228792016-02-03 15:30:24 +01001677nc_ssh_tls_init(void)
1678{
Rosen Penev4f552d62019-06-26 16:10:43 -07001679#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001680 SSL_load_error_strings();
1681 ERR_load_BIO_strings();
1682 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001683#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001684
1685 nc_ssh_init();
1686
Michal Vaskoe1e82632019-09-09 09:18:03 +02001687#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001688 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1689 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1690 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001691#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001692}
1693
Michal Vasko8f0c0282016-02-29 10:17:14 +01001694static void
Michal Vasko5e228792016-02-03 15:30:24 +01001695nc_ssh_tls_destroy(void)
1696{
Rosen Penev4f552d62019-06-26 16:10:43 -07001697#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001698 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001699# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001700 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001701# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001702 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001703# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001704#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001705
1706 nc_ssh_destroy();
1707
Michal Vaskoe1e82632019-09-09 09:18:03 +02001708#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001709 CRYPTO_set_dynlock_create_callback(NULL);
1710 CRYPTO_set_dynlock_lock_callback(NULL);
1711 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001712#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001713}
1714
Radek Krejci53691be2016-02-22 13:58:37 +01001715#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001716
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001717#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001718
1719API void
1720nc_thread_destroy(void)
1721{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001722 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001723 // CRYPTO_cleanup_all_ex_data();
Michal Vasko8f0c0282016-02-29 10:17:14 +01001724
Michal Vasko770b4362017-02-17 14:44:18 +01001725#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1726 CRYPTO_THREADID crypto_tid;
1727
Michal Vasko8f0c0282016-02-29 10:17:14 +01001728 CRYPTO_THREADID_current(&crypto_tid);
1729 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001730#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001731}
1732
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001733#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1734
1735void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001736nc_init(void)
1737{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001738#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001739 nc_ssh_tls_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001740#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001741 nc_ssh_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001742#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001743 nc_tls_init();
1744#endif
1745}
1746
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001747void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001748nc_destroy(void)
1749{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001750#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001751 nc_ssh_tls_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001752#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001753 nc_ssh_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001754#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001755 nc_tls_destroy();
1756#endif
1757}