blob: 443e0f19f8b25472f428ca380f6d750a87aa5f96 [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
140nc_difftimespec_cur(const struct timespec *ts)
141{
142 struct timespec cur;
143 int64_t nsec_diff = 0;
144
145 nc_gettimespec_mono_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;
Michal Vasko36c7be82017-02-22 13:37:59 +0100151}
152
Michal Vaskoddce1212019-05-24 09:58:49 +0200153const char *
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200154nc_keytype2str(NC_SSH_KEY_TYPE type)
Michal Vaskoddce1212019-05-24 09:58:49 +0200155{
156 switch (type) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200157 case NC_SSH_KEY_DSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200158 return "DSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200159 case NC_SSH_KEY_RSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200160 return "RSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200161 case NC_SSH_KEY_ECDSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200162 return "EC";
163 default:
164 break;
165 }
166
167 return NULL;
168}
169
Michal Vaskobe52dc22018-10-17 09:28:17 +0200170int
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200171nc_sock_enable_keepalive(int sock, struct nc_keepalives *ka)
Michal Vaskobe52dc22018-10-17 09:28:17 +0200172{
173 int opt;
174
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200175 opt = ka->enabled;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200176 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200177 ERR(NULL, "Could not set SO_KEEPALIVE option (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200178 return -1;
179 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200180 if (!ka->enabled) {
181 return 0;
182 }
Michal Vaskobe52dc22018-10-17 09:28:17 +0200183
184#ifdef TCP_KEEPIDLE
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200185 opt = ka->idle_time;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200186 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200187 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200188 return -1;
189 }
190#endif
191
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200192#ifdef TCP_KEEPCNT
193 opt = ka->max_probes;
194 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200195 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200196 return -1;
197 }
198#endif
199
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200200#ifdef TCP_KEEPINTVL
201 opt = ka->probe_interval;
202 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200203 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200204 return -1;
205 }
206#endif
207
208 return 0;
209}
210
Michal Vaskoade892d2017-02-22 13:40:35 +0100211struct nc_session *
Michal Vasko131120a2018-05-29 15:44:02 +0200212nc_new_session(NC_SIDE side, int shared_ti)
Michal Vaskoade892d2017-02-22 13:40:35 +0100213{
214 struct nc_session *sess;
215
216 sess = calloc(1, sizeof *sess);
217 if (!sess) {
218 return NULL;
219 }
220
Michal Vasko131120a2018-05-29 15:44:02 +0200221 sess->side = side;
222
223 if (side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200224 pthread_mutex_init(&sess->opts.server.ntf_status_lock, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100225 pthread_mutex_init(&sess->opts.server.rpc_lock, NULL);
226 pthread_cond_init(&sess->opts.server.rpc_cond, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100227
228 pthread_mutex_init(&sess->opts.server.ch_lock, NULL);
229 pthread_cond_init(&sess->opts.server.ch_cond, NULL);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200230 } else {
231 pthread_mutex_init(&sess->opts.client.msgs_lock, NULL);
Michal Vasko131120a2018-05-29 15:44:02 +0200232 }
233
234 if (!shared_ti) {
235 sess->io_lock = malloc(sizeof *sess->io_lock);
236 if (!sess->io_lock) {
237 goto error;
238 }
239 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100240 }
241
242 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200243
244error:
Michal Vasko131120a2018-05-29 15:44:02 +0200245 free(sess);
246 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100247}
248
Michal Vasko96164bf2016-01-21 15:41:58 +0100249/*
250 * @return 1 - success
251 * 0 - timeout
252 * -1 - error
253 */
254int
Michal Vasko131120a2018-05-29 15:44:02 +0200255nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100256{
257 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100258 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100259
Michal Vasko131120a2018-05-29 15:44:02 +0200260 if (session->side != NC_SERVER) {
261 ERRINT;
262 return -1;
263 }
264
Michal Vasko96164bf2016-01-21 15:41:58 +0100265 if (timeout > 0) {
roman6ece9c52022-06-22 09:29:17 +0200266 nc_gettimespec_real_add(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100267
Michal Vaskoade892d2017-02-22 13:40:35 +0100268 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100269 ret = pthread_mutex_timedlock(&session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100270 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100271 while (session->opts.server.rpc_inuse) {
272 ret = pthread_cond_timedwait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100273 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100274 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100275 break;
276 }
277 }
278 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100279 } else if (!timeout) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100280 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100281 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100282 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100283 if (session->opts.server.rpc_inuse) {
284 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100285 return 0;
286 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200287 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100288 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100289 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100290 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100291 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100292 while (session->opts.server.rpc_inuse) {
293 ret = pthread_cond_wait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100294 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100295 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100296 break;
297 }
298 }
299 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100300 }
301
Michal Vaskoade892d2017-02-22 13:40:35 +0100302 if (ret) {
303 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
304 /* timeout */
305 return 0;
306 }
307
Michal Vasko96164bf2016-01-21 15:41:58 +0100308 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200309 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100310 return -1;
311 }
312
313 /* ok */
Michal Vaskoacf98472021-02-04 15:33:57 +0100314 assert(session->opts.server.rpc_inuse == 0);
315 session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100316
317 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100318 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100319 if (ret) {
320 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200321 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100322 return -1;
323 }
324
325 return 1;
326}
327
328int
Michal Vasko131120a2018-05-29 15:44:02 +0200329nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100330{
331 int ret;
332 struct timespec ts_timeout;
333
Michal Vasko131120a2018-05-29 15:44:02 +0200334 if (session->side != NC_SERVER) {
335 ERRINT;
336 return -1;
337 }
338
Michal Vaskoacf98472021-02-04 15:33:57 +0100339 assert(session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100340
341 if (timeout > 0) {
roman6ece9c52022-06-22 09:29:17 +0200342 nc_gettimespec_real_add(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100343
344 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100345 ret = pthread_mutex_timedlock(&session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100346 } else if (!timeout) {
347 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100348 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100349 } else { /* timeout == -1 */
350 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100351 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100352 }
353
354 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
355 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200356 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100357 return -1;
358 } else if (ret) {
Michal Vasko05532772021-06-03 12:12:38 +0200359 WRN(session, "%s: session RPC lock timeout, should not happen.");
Michal Vaskoade892d2017-02-22 13:40:35 +0100360 }
361
Michal Vaskoacf98472021-02-04 15:33:57 +0100362 session->opts.server.rpc_inuse = 0;
363 pthread_cond_signal(&session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100364
365 if (!ret) {
366 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100367 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100368 if (ret) {
369 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200370 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100371 return -1;
372 }
373 }
374
Michal Vasko96164bf2016-01-21 15:41:58 +0100375 return 1;
376}
377
Michal Vasko131120a2018-05-29 15:44:02 +0200378int
379nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
380{
381 int ret;
382 struct timespec ts_timeout;
383
384 if (timeout > 0) {
roman6ece9c52022-06-22 09:29:17 +0200385 nc_gettimespec_real_add(&ts_timeout, timeout);
Michal Vasko131120a2018-05-29 15:44:02 +0200386
387 ret = pthread_mutex_timedlock(session->io_lock, &ts_timeout);
388 } else if (!timeout) {
389 ret = pthread_mutex_trylock(session->io_lock);
390 } else { /* timeout == -1 */
Robin Jarry54ea2962018-10-10 10:33:40 +0200391 ret = pthread_mutex_lock(session->io_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200392 }
393
394 if (ret) {
395 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
396 /* timeout */
397 return 0;
398 }
399
400 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200401 ERR(session, "%s: failed to IO lock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200402 return -1;
403 }
404
405 return 1;
406}
407
408int
409nc_session_io_unlock(struct nc_session *session, const char *func)
410{
411 int ret;
412
413 ret = pthread_mutex_unlock(session->io_lock);
414 if (ret) {
415 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200416 ERR(session, "%s: failed to IO unlock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200417 return -1;
418 }
419
420 return 1;
421}
422
Michal Vasko01130bd2021-08-26 11:47:38 +0200423int
424nc_session_client_msgs_lock(struct nc_session *session, int *timeout, const char *func)
425{
426 int ret;
427 int32_t diff_msec;
roman6ece9c52022-06-22 09:29:17 +0200428 struct timespec ts_timeout, ts_start;
Michal Vasko01130bd2021-08-26 11:47:38 +0200429
430 assert(session->side == NC_CLIENT);
431
432 if (*timeout > 0) {
433 /* get current time */
roman6ece9c52022-06-22 09:29:17 +0200434 nc_gettimespec_real_add(&ts_start, 0);
Michal Vasko01130bd2021-08-26 11:47:38 +0200435
roman6ece9c52022-06-22 09:29:17 +0200436 nc_gettimespec_real_add(&ts_timeout, *timeout);
Michal Vasko01130bd2021-08-26 11:47:38 +0200437
438 ret = pthread_mutex_timedlock(&session->opts.client.msgs_lock, &ts_timeout);
439 if (!ret) {
440 /* update timeout based on what was elapsed */
roman6ece9c52022-06-22 09:29:17 +0200441 diff_msec = nc_difftimespec_cur(&ts_start);
Michal Vasko01130bd2021-08-26 11:47:38 +0200442 *timeout -= diff_msec;
443 }
444 } else if (!*timeout) {
445 ret = pthread_mutex_trylock(&session->opts.client.msgs_lock);
446 } else { /* timeout == -1 */
447 ret = pthread_mutex_lock(&session->opts.client.msgs_lock);
448 }
449
450 if (ret) {
451 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
452 /* timeout */
453 return 0;
454 }
455
456 /* error */
457 ERR(session, "%s: failed to MSGS lock a session (%s).", func, strerror(ret));
458 return -1;
459 }
460
461 return 1;
462}
463
464int
465nc_session_client_msgs_unlock(struct nc_session *session, const char *func)
466{
467 int ret;
468
469 assert(session->side == NC_CLIENT);
470
471 ret = pthread_mutex_unlock(&session->opts.client.msgs_lock);
472 if (ret) {
473 /* error */
474 ERR(session, "%s: failed to MSGS unlock a session (%s).", func, strerror(ret));
475 return -1;
476 }
477
478 return 1;
479}
480
Michal Vasko8dadf782016-01-15 10:29:36 +0100481API NC_STATUS
482nc_session_get_status(const struct nc_session *session)
483{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100484 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200485 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200486 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100487 }
488
Michal Vasko8dadf782016-01-15 10:29:36 +0100489 return session->status;
490}
491
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100492API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200493nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100494{
495 if (!session) {
496 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200497 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100498 }
499
500 return session->term_reason;
501}
502
Michal Vasko8dadf782016-01-15 10:29:36 +0100503API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200504nc_session_get_killed_by(const struct nc_session *session)
505{
506 if (!session) {
507 ERRARG("session");
508 return 0;
509 }
510
511 return session->killed_by;
512}
513
514API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100515nc_session_get_id(const struct nc_session *session)
516{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100517 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200518 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100519 return 0;
520 }
521
Michal Vasko8dadf782016-01-15 10:29:36 +0100522 return session->id;
523}
524
Michal Vasko174fe8e2016-02-17 15:38:09 +0100525API int
526nc_session_get_version(const struct nc_session *session)
527{
528 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200529 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100530 return -1;
531 }
532
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200533 return session->version == NC_VERSION_10 ? 0 : 1;
Michal Vasko174fe8e2016-02-17 15:38:09 +0100534}
535
Michal Vasko8dadf782016-01-15 10:29:36 +0100536API NC_TRANSPORT_IMPL
537nc_session_get_ti(const struct nc_session *session)
538{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100539 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200540 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100541 return 0;
542 }
543
Michal Vasko8dadf782016-01-15 10:29:36 +0100544 return session->ti_type;
545}
546
547API const char *
548nc_session_get_username(const struct nc_session *session)
549{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100550 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200551 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100552 return NULL;
553 }
554
Michal Vasko8dadf782016-01-15 10:29:36 +0100555 return session->username;
556}
557
558API const char *
559nc_session_get_host(const struct nc_session *session)
560{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100561 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200562 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100563 return NULL;
564 }
565
Michal Vasko8dadf782016-01-15 10:29:36 +0100566 return session->host;
567}
568
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200569API const char *
570nc_session_get_path(const struct nc_session *session)
571{
572 if (!session) {
573 ERRARG("session");
574 return NULL;
575 }
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200576 if (session->ti_type != NC_TI_UNIX) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200577 return NULL;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200578 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200579
580 return session->path;
581}
582
Michal Vasko8dadf782016-01-15 10:29:36 +0100583API uint16_t
584nc_session_get_port(const struct nc_session *session)
585{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100586 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200587 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100588 return 0;
589 }
590
Michal Vasko8dadf782016-01-15 10:29:36 +0100591 return session->port;
592}
593
Michal Vasko93224072021-11-09 12:14:28 +0100594API const struct ly_ctx *
Michal Vasko9a25e932016-02-01 10:36:42 +0100595nc_session_get_ctx(const struct nc_session *session)
596{
597 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200598 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100599 return NULL;
600 }
601
602 return session->ctx;
603}
604
Michal Vasko2cc4c682016-03-01 09:16:48 +0100605API void
606nc_session_set_data(struct nc_session *session, void *data)
607{
608 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200609 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100610 return;
611 }
612
613 session->data = data;
614}
615
616API void *
617nc_session_get_data(const struct nc_session *session)
618{
619 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200620 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100621 return NULL;
622 }
623
624 return session->data;
625}
626
Michal Vasko086311b2016-01-08 09:53:11 +0100627NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200628nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200629{
Michal Vasko7e1f5fb2021-11-10 10:14:45 +0100630 if (session->ctx != LYD_CTX(op)) {
631 ERR(session, "RPC \"%s\" was created in different context than that of the session.", LYD_NAME(op));
Michal Vasko086311b2016-01-08 09:53:11 +0100632 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100633 }
634
Michal Vasko131120a2018-05-29 15:44:02 +0200635 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100636}
637
Michal Vaskod4da3632022-05-25 11:49:10 +0200638/**
639 * @brief Send \<close-session\> and read the reply on a session.
640 *
641 * @param[in] session Closing NETCONF session.
642 */
643static void
644nc_session_free_close_session(struct nc_session *session)
645{
646 struct ly_in *msg;
647 struct lyd_node *close_rpc, *envp;
648 const struct lys_module *ietfnc;
649
650 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
651 if (!ietfnc) {
652 WRN(session, "Missing ietf-netconf schema in context, unable to send <close-session>.");
653 return;
654 }
655 if (lyd_new_inner(NULL, ietfnc, "close-session", 0, &close_rpc)) {
656 WRN(session, "Failed to create <close-session> RPC.");
657 return;
658 }
659
660 /* send the RPC */
661 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
662
663read_msg:
664 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &msg)) {
665 case 1:
666 if (!strncmp(ly_in_memory(msg, NULL), "<notification", 13)) {
667 /* ignore */
668 ly_in_free(msg, 1);
669 goto read_msg;
670 }
671 if (lyd_parse_op(session->ctx, close_rpc, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, &envp, NULL)) {
672 WRN(session, "Failed to parse <close-session> reply.");
673 } else if (!lyd_child(envp) || strcmp(LYD_NAME(lyd_child(envp)), "ok")) {
674 WRN(session, "Reply to <close-session> was not <ok> as expected.");
675 }
676 lyd_free_tree(envp);
677 ly_in_free(msg, 1);
678 break;
679 case 0:
680 WRN(session, "Timeout for receiving a reply to <close-session> elapsed.");
681 break;
682 case -1:
683 ERR(session, "Failed to receive a reply to <close-session>.");
684 break;
685 default:
686 /* cannot happen */
687 break;
688 }
689 lyd_free_tree(close_rpc);
690}
691
Radek Krejci695d4fa2015-10-22 13:23:54 +0200692API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100693nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200694{
Michal Vasko01130bd2021-08-26 11:47:38 +0200695 int r, i, rpc_locked = 0, msgs_locked = 0, sock = -1, timeout;
Michal Vasko428087d2016-01-14 16:04:28 +0100696 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100697 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vasko4589bbe2016-01-29 09:41:30 +0100698 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100699 struct nc_msg_cont *contiter;
Michal Vasko77367452021-02-16 16:32:18 +0100700 struct ly_in *msg;
roman6ece9c52022-06-22 09:29:17 +0200701 struct timespec ts;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200702 void *p;
703
Michal Vasko428087d2016-01-14 16:04:28 +0100704 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200705 return;
706 }
707
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200708 /* stop notifications thread if any */
709 if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
710 /* let the thread know it should quit */
711 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 2);
712
713 /* wait for it */
roman6ece9c52022-06-22 09:29:17 +0200714 nc_gettimespec_mono_add(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200715 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
716 usleep(NC_TIMEOUT_STEP);
roman6ece9c52022-06-22 09:29:17 +0200717 if (nc_difftimespec_cur(&ts) < 1) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200718 ERR(session, "Waiting for notification thread exit failed (timed out).");
719 break;
720 }
721 }
Michal Vasko86d357c2016-03-11 13:46:38 +0100722 }
723
Michal Vaskoacf98472021-02-04 15:33:57 +0100724 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200725 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100726 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100727 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200728 } else if (r) {
729 rpc_locked = 1;
Michal Vasko96a28a32021-02-04 15:35:20 +0100730 } else {
731 /* else failed to lock it, too bad */
Michal Vasko05532772021-06-03 12:12:38 +0200732 ERR(session, "Freeing a session while an RPC is being processed.");
Michal Vasko96a28a32021-02-04 15:35:20 +0100733 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200734 }
735
Michal Vasko8c247822020-09-07 13:23:23 +0200736 if (session->side == NC_CLIENT) {
Michal Vasko01130bd2021-08-26 11:47:38 +0200737 timeout = NC_SESSION_FREE_LOCK_TIMEOUT;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200738
Michal Vasko01130bd2021-08-26 11:47:38 +0200739 /* MSGS LOCK */
740 r = nc_session_client_msgs_lock(session, &timeout, __func__);
741 if (r == -1) {
742 return;
743 } else if (r) {
744 msgs_locked = 1;
745 } else {
746 /* else failed to lock it, too bad */
747 ERR(session, "Freeing a session while messages are being received.");
748 }
749
750 /* cleanup message queue */
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200751 for (contiter = session->opts.client.msgs; contiter; ) {
Michal Vasko77367452021-02-16 16:32:18 +0100752 ly_in_free(contiter->msg, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200753
Michal Vaskoad611702015-12-03 13:41:51 +0100754 p = contiter;
755 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200756 free(p);
757 }
758
Michal Vasko01130bd2021-08-26 11:47:38 +0200759 if (msgs_locked) {
760 /* MSGS UNLOCK */
761 nc_session_client_msgs_unlock(session, __func__);
762 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200763
Michal Vasko8c247822020-09-07 13:23:23 +0200764 if (session->status == NC_STATUS_RUNNING) {
Michal Vasko9c6d38c2021-09-03 13:02:53 +0200765 /* receive any leftover messages */
766 while (nc_read_msg_poll_io(session, 0, &msg) == 1) {
767 ly_in_free(msg, 1);
768 }
769
Michal Vasko8c247822020-09-07 13:23:23 +0200770 /* send closing info to the other side */
Michal Vaskod4da3632022-05-25 11:49:10 +0200771 nc_session_free_close_session(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200772 }
773
774 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200775 if (session->opts.client.cpblts) {
776 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200777 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200778 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200779 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200780 }
781 }
782
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100783 if (session->data && data_free) {
784 data_free(session->data);
785 }
786
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200787 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
788 /* CH LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100789 pthread_mutex_lock(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200790 }
791
Michal Vasko86d357c2016-03-11 13:46:38 +0100792 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200793 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200794
Michal Vaskofeccb312022-03-24 15:24:59 +0100795 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100796 pthread_cond_signal(&session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200797
roman6ece9c52022-06-22 09:29:17 +0200798 nc_gettimespec_real_add(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vasko0db3db52021-03-03 10:45:42 +0100799
800 /* wait for CH thread to actually wake up and terminate */
801 r = 0;
Michal Vaskofeccb312022-03-24 15:24:59 +0100802 while (!r && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vasko0db3db52021-03-03 10:45:42 +0100803 r = pthread_cond_timedwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, &ts);
804 }
Michal Vasko0db3db52021-03-03 10:45:42 +0100805 if (r) {
Michal Vasko05532772021-06-03 12:12:38 +0200806 ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r));
Michal Vasko3f05a092018-03-13 10:39:49 +0100807 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200808 }
809
Michal Vaskofeccb312022-03-24 15:24:59 +0100810 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
811 /* CH UNLOCK */
812 pthread_mutex_unlock(&session->opts.server.ch_lock);
813 }
814
Michal Vasko428087d2016-01-14 16:04:28 +0100815 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200816
817 /* transport implementation cleanup */
818 switch (session->ti_type) {
819 case NC_TI_FD:
820 /* nothing needed - file descriptors were provided by caller,
821 * so it is up to the caller to close them correctly
822 * TODO use callbacks
823 */
Michal Vasko3512e402016-01-28 16:22:34 +0100824 /* just to avoid compiler warning */
825 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100826 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200827 break;
828
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200829 case NC_TI_UNIX:
830 sock = session->ti.unixsock.sock;
831 (void)connected;
832 (void)siter;
833 break;
834
Radek Krejci53691be2016-02-22 13:58:37 +0100835#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200836 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100837 if (connected) {
838 ssh_channel_free(session->ti.libssh.channel);
839 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200840 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
841 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200842 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
Radek Krejci695d4fa2015-10-22 13:23:54 +0200843 */
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200844 /* SESSION IO LOCK */
845 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
846
Michal Vasko96164bf2016-01-21 15:41:58 +0100847 multisession = 0;
848 if (session->ti.libssh.next) {
849 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
850 if (siter->status != NC_STATUS_STARTING) {
851 multisession = 1;
852 break;
853 }
854 }
855 }
856
857 if (!multisession) {
858 /* it's not multisession yet, but we still need to free the starting sessions */
859 if (session->ti.libssh.next) {
860 do {
861 siter = session->ti.libssh.next;
862 session->ti.libssh.next = siter->ti.libssh.next;
863
864 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vaskoe38655d2021-11-12 15:41:16 +0100865 free(siter->username);
866 free(siter->host);
867 if (!(siter->flags & NC_SESSION_SHAREDCTX)) {
868 ly_ctx_destroy((struct ly_ctx *)siter->ctx);
Michal Vasko96164bf2016-01-21 15:41:58 +0100869 }
870
871 free(siter);
872 } while (session->ti.libssh.next != session);
873 }
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100874 /* remember sock so we can close it */
875 sock = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100876 if (connected) {
877 ssh_disconnect(session->ti.libssh.session);
Michal Vasko06e0fc22019-05-09 09:32:27 +0200878 sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100879 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200880 ssh_free(session->ti.libssh.session);
881 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200882 /* remove the session from the list */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200883 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {}
Michal Vaskoaec4f212015-10-26 15:37:45 +0100884 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200885 /* there will be only one session */
886 siter->ti.libssh.next = NULL;
887 } else {
888 /* there are still multiple sessions, keep the ring list */
889 siter->ti.libssh.next = session->ti.libssh.next;
890 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100891 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
892 if (session->flags & NC_SESSION_SSH_MSG_CB) {
Michal Vasko5e0edd82020-07-29 15:26:13 +0200893 siter = session->ti.libssh.next;
894 while (siter && siter->status != NC_STATUS_RUNNING) {
Michal Vasko96164bf2016-01-21 15:41:58 +0100895 if (siter->ti.libssh.next == session) {
896 ERRINT;
897 break;
898 }
Michal Vasko5e0edd82020-07-29 15:26:13 +0200899 siter = siter->ti.libssh.next;
Michal Vasko96164bf2016-01-21 15:41:58 +0100900 }
Michal Vasko5e0edd82020-07-29 15:26:13 +0200901 /* siter may be NULL in case all the sessions terminated at the same time (socket was disconnected),
902 * we set session to NULL because we do not expect any new message to arrive */
Michal Vasko96164bf2016-01-21 15:41:58 +0100903 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
Michal Vasko5e0edd82020-07-29 15:26:13 +0200904 if (siter) {
905 siter->flags |= NC_SESSION_SSH_MSG_CB;
906 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100907 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200908 }
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200909
910 /* SESSION IO UNLOCK */
911 if (r == 1) {
912 nc_session_io_unlock(session, __func__);
913 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200914 break;
915#endif
916
Radek Krejci53691be2016-02-22 13:58:37 +0100917#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200918 case NC_TI_OPENSSL:
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100919 /* remember sock so we can close it */
920 sock = SSL_get_fd(session->ti.tls);
921
Michal Vasko428087d2016-01-14 16:04:28 +0100922 if (connected) {
923 SSL_shutdown(session->ti.tls);
924 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200925 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100926
Michal Vasko2e6defd2016-10-07 15:48:15 +0200927 if (session->side == NC_SERVER) {
928 X509_free(session->opts.server.client_cert);
929 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200930 break;
931#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100932 case NC_TI_NONE:
Michal Vasko428087d2016-01-14 16:04:28 +0100933 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200934 }
Michal Vasko428087d2016-01-14 16:04:28 +0100935
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100936 /* close socket separately */
937 if (sock > -1) {
938 close(sock);
939 }
940
Michal Vasko93224072021-11-09 12:14:28 +0100941 free(session->username);
942 free(session->host);
943 free(session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200944
945 /* final cleanup */
Michal Vaskoacf98472021-02-04 15:33:57 +0100946 if (session->side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200947 pthread_mutex_destroy(&session->opts.server.ntf_status_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200948 if (rpc_locked) {
949 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100950 }
Michal Vaskoacf98472021-02-04 15:33:57 +0100951 pthread_mutex_destroy(&session->opts.server.rpc_lock);
952 pthread_cond_destroy(&session->opts.server.rpc_cond);
Michal Vasko131120a2018-05-29 15:44:02 +0200953 }
954
955 if (session->io_lock && !multisession) {
956 pthread_mutex_destroy(session->io_lock);
957 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200958 }
959
960 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko93224072021-11-09 12:14:28 +0100961 ly_ctx_destroy((struct ly_ctx *)session->ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200962 }
963
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200964 if (session->side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100965 /* free CH synchronization structures */
966 pthread_cond_destroy(&session->opts.server.ch_cond);
967 pthread_mutex_destroy(&session->opts.server.ch_lock);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200968 } else {
969 pthread_mutex_destroy(&session->opts.client.msgs_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200970 }
971
Radek Krejci695d4fa2015-10-22 13:23:54 +0200972 free(session);
973}
974
Michal Vasko086311b2016-01-08 09:53:11 +0100975static void
Michal Vasko93224072021-11-09 12:14:28 +0100976add_cpblt(const char *capab, char ***cpblts, int *size, int *count)
Michal Vasko086311b2016-01-08 09:53:11 +0100977{
Radek Krejci658782b2016-12-04 22:04:55 +0100978 size_t len;
979 int i;
980 char *p;
981
982 if (capab) {
983 /* check if already present */
984 p = strchr(capab, '?');
985 if (p) {
986 len = p - capab;
987 } else {
988 len = strlen(capab);
989 }
990 for (i = 0; i < *count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200991 if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) {
Radek Krejci658782b2016-12-04 22:04:55 +0100992 /* already present, do not duplicate it */
993 return;
994 }
995 }
996 }
997
998 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100999 if (*count == *size) {
1000 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001001 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
1002 if (!(*cpblts)) {
1003 ERRMEM;
1004 return;
1005 }
Michal Vasko086311b2016-01-08 09:53:11 +01001006 }
1007
Michal Vasko93224072021-11-09 12:14:28 +01001008 (*cpblts)[*count] = capab ? strdup(capab) : NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001009 ++(*count);
1010}
1011
Michal Vasko93224072021-11-09 12:14:28 +01001012API char **
1013nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +01001014{
Michal Vasko93224072021-11-09 12:14:28 +01001015 char **cpblts;
Michal Vasko77367452021-02-16 16:32:18 +01001016 const struct lys_module *mod;
1017 struct lysp_feature *feat;
1018 int size = 10, count, features_count = 0, dev_count = 0, str_len, len;
Michal Vasko1440a742021-03-31 11:11:03 +02001019 uint32_t i, u;
Michal Vasko77367452021-02-16 16:32:18 +01001020 LY_ARRAY_COUNT_TYPE v;
Michal Vasko1440a742021-03-31 11:11:03 +02001021 char *yl_content_id;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001022
Radek Krejci24a18412018-05-16 15:09:10 +02001023#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +02001024 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001025
Michal Vasko4ffa3b22016-05-24 16:36:25 +02001026 if (!ctx) {
1027 ERRARG("ctx");
1028 return NULL;
1029 }
1030
Michal Vasko086311b2016-01-08 09:53:11 +01001031 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001032 if (!cpblts) {
1033 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +02001034 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001035 }
Michal Vasko93224072021-11-09 12:14:28 +01001036 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1037 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko086311b2016-01-08 09:53:11 +01001038 count = 2;
1039
1040 /* capabilities */
1041
Michal Vasko77367452021-02-16 16:32:18 +01001042 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf");
Michal Vasko086311b2016-01-08 09:53:11 +01001043 if (mod) {
Michal Vasko77367452021-02-16 16:32:18 +01001044 if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001045 add_cpblt("urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001046 }
Michal Vasko77367452021-02-16 16:32:18 +01001047 if (lys_feature_value(mod, "candidate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001048 add_cpblt("urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko77367452021-02-16 16:32:18 +01001049 if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001050 add_cpblt("urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001051 }
1052 }
Michal Vasko77367452021-02-16 16:32:18 +01001053 if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001054 add_cpblt("urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001055 }
Michal Vasko77367452021-02-16 16:32:18 +01001056 if (lys_feature_value(mod, "validate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001057 add_cpblt("urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001058 }
Michal Vasko77367452021-02-16 16:32:18 +01001059 if (lys_feature_value(mod, "startup") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001060 add_cpblt("urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001061 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001062
1063 /* The URL capability must be set manually using nc_server_set_capability()
1064 * because of the need for supported protocols to be included.
1065 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1066 */
Michal Vasko77367452021-02-16 16:32:18 +01001067 // if (lys_feature_value(mod, "url") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001068 // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
mekleoa8de5e92020-02-13 09:05:56 +01001069 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001070
Michal Vasko77367452021-02-16 16:32:18 +01001071 if (lys_feature_value(mod, "xpath") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001072 add_cpblt("urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001073 }
1074 }
1075
Michal Vasko77367452021-02-16 16:32:18 +01001076 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01001077 if (mod) {
1078 if (!server_opts.wd_basic_mode) {
Michal Vasko05532772021-06-03 12:12:38 +02001079 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 +01001080 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001081 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +01001082 switch (server_opts.wd_basic_mode) {
1083 case NC_WD_ALL:
1084 strcat(str, "?basic-mode=report-all");
1085 break;
1086 case NC_WD_TRIM:
1087 strcat(str, "?basic-mode=trim");
1088 break;
1089 case NC_WD_EXPLICIT:
1090 strcat(str, "?basic-mode=explicit");
1091 break;
1092 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001093 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001094 break;
1095 }
1096
1097 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001098 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +01001099 if (server_opts.wd_also_supported & NC_WD_ALL) {
1100 strcat(str, "report-all,");
1101 }
1102 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
1103 strcat(str, "report-all-tagged,");
1104 }
1105 if (server_opts.wd_also_supported & NC_WD_TRIM) {
1106 strcat(str, "trim,");
1107 }
1108 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
1109 strcat(str, "explicit,");
1110 }
1111 str[strlen(str) - 1] = '\0';
1112
Michal Vasko93224072021-11-09 12:14:28 +01001113 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001114 }
1115 }
1116 }
1117
Radek Krejci658782b2016-12-04 22:04:55 +01001118 /* other capabilities */
1119 for (u = 0; u < server_opts.capabilities_count; u++) {
Michal Vasko93224072021-11-09 12:14:28 +01001120 add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001121 }
1122
1123 /* models */
Michal Vasko1440a742021-03-31 11:11:03 +02001124 u = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001125 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001126 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vasko77367452021-02-16 16:32:18 +01001127 if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) {
Michal Vasko05532772021-06-03 12:12:38 +02001128 ERR(NULL, "Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
Michal Vaskod5ada122020-03-19 18:28:06 +01001129 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001130 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001131
Michal Vasko1440a742021-03-31 11:11:03 +02001132 /* get content-id */
1133 if (server_opts.content_id_clb) {
1134 yl_content_id = server_opts.content_id_clb(server_opts.content_id_data);
1135 if (!yl_content_id) {
1136 ERRMEM;
1137 goto error;
1138 }
1139 } else {
1140 yl_content_id = malloc(11);
1141 if (!yl_content_id) {
1142 ERRMEM;
1143 goto error;
1144 }
1145 sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx));
1146 }
1147
Michal Vasko77367452021-02-16 16:32:18 +01001148 if (!strcmp(mod->revision, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001149 /* new one (capab defined in RFC 8526 section 2) */
Michal Vasko1440a742021-03-31 11:11:03 +02001150 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s",
1151 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001152 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001153 } else {
1154 /* old one (capab defined in RFC 7950 section 5.6.4) */
Michal Vasko1440a742021-03-31 11:11:03 +02001155 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s",
1156 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001157 add_cpblt(str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001158 }
Michal Vasko1440a742021-03-31 11:11:03 +02001159 free(yl_content_id);
Radek Krejci24a18412018-05-16 15:09:10 +02001160 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001161 } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) {
Radek Krejci24a18412018-05-16 15:09:10 +02001162 /* skip YANG 1.1 schemas */
1163 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001164 } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) {
Radek Krejci24a18412018-05-16 15:09:10 +02001165 /* skip YANG 1.0 schemas */
1166 continue;
1167 }
Michal Vasko086311b2016-01-08 09:53:11 +01001168
Michal Vasko77367452021-02-16 16:32:18 +01001169 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "",
1170 mod->revision ? mod->revision : "");
Radek Krejci24a18412018-05-16 15:09:10 +02001171
Michal Vaskodafdc742020-03-11 16:15:59 +01001172 features_count = 0;
1173 i = 0;
1174 feat = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001175 while ((feat = lysp_feature_next(feat, mod->parsed, &i))) {
Michal Vaskodafdc742020-03-11 16:15:59 +01001176 if (!(feat->flags & LYS_FENABLED)) {
1177 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001178 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001179 if (!features_count) {
1180 strcat(str, "&features=");
1181 str_len += 10;
1182 }
1183 len = strlen(feat->name);
1184 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1185 ERRINT;
1186 break;
1187 }
1188 if (features_count) {
1189 strcat(str, ",");
1190 ++str_len;
1191 }
1192 strcat(str, feat->name);
1193 str_len += len;
1194 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001195 }
Michal Vasko086311b2016-01-08 09:53:11 +01001196
Michal Vasko77367452021-02-16 16:32:18 +01001197 if (mod->deviated_by) {
Radek Krejci24a18412018-05-16 15:09:10 +02001198 strcat(str, "&deviations=");
1199 str_len += 12;
1200 dev_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +01001201 LY_ARRAY_FOR(mod->deviated_by, v) {
1202 len = strlen(mod->deviated_by[v]->name);
1203 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1204 ERRINT;
1205 break;
Radek Krejci24a18412018-05-16 15:09:10 +02001206 }
Michal Vasko77367452021-02-16 16:32:18 +01001207 if (dev_count) {
1208 strcat(str, ",");
1209 ++str_len;
Radek Krejci24a18412018-05-16 15:09:10 +02001210 }
Michal Vasko77367452021-02-16 16:32:18 +01001211 strcat(str, mod->deviated_by[v]->name);
1212 str_len += len;
1213 dev_count++;
Radek Krejci24a18412018-05-16 15:09:10 +02001214 }
1215 }
1216
Michal Vasko93224072021-11-09 12:14:28 +01001217 add_cpblt(str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +02001218 }
Michal Vasko086311b2016-01-08 09:53:11 +01001219
1220 /* ending NULL capability */
Michal Vasko93224072021-11-09 12:14:28 +01001221 add_cpblt(NULL, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001222
1223 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001224
1225error:
Radek Krejcif906e412017-09-22 14:44:45 +02001226 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001227 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001228}
1229
Michal Vasko93224072021-11-09 12:14:28 +01001230API char **
1231nc_server_get_cpblts(const struct ly_ctx *ctx)
Radek Krejci24a18412018-05-16 15:09:10 +02001232{
1233 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1234}
1235
Radek Krejci695d4fa2015-10-22 13:23:54 +02001236static int
Michal Vasko77367452021-02-16 16:32:18 +01001237parse_cpblts(struct lyd_node *capabilities, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001238{
Michal Vasko77367452021-02-16 16:32:18 +01001239 struct lyd_node *iter;
1240 struct lyd_node_opaq *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001241 int ver = -1, i = 0;
1242 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001243
1244 if (list) {
1245 /* get the storage for server's capabilities */
Michal Vasko77367452021-02-16 16:32:18 +01001246 LY_LIST_FOR(lyd_child(capabilities), iter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001247 i++;
1248 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001249 /* last item remains NULL */
1250 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001251 if (!*list) {
1252 ERRMEM;
1253 return -1;
1254 }
1255 i = 0;
1256 }
1257
Michal Vasko77367452021-02-16 16:32:18 +01001258 LY_LIST_FOR(lyd_child(capabilities), iter) {
1259 cpblt = (struct lyd_node_opaq *)iter;
1260
1261 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 +02001262 ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001263 return -1;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001264 }
1265
Michal Vasko156d3272017-04-11 11:46:49 +02001266 /* skip leading/trailing whitespaces */
Michal Vasko77367452021-02-16 16:32:18 +01001267 for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {}
1268 for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {}
1269 if (!cpb_start[0] || (cpb_end == cpblt->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001270 ERR(NULL, "Empty capability \"%s\" received.", cpblt->value);
Michal Vasko156d3272017-04-11 11:46:49 +02001271 return -1;
1272 }
1273
Radek Krejci695d4fa2015-10-22 13:23:54 +02001274 /* detect NETCONF version */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001275 if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001276 ver = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001277 } 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 +02001278 ver = 1;
1279 }
1280
1281 /* store capabilities */
1282 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001283 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1284 if (!(*list)[i]) {
1285 ERRMEM;
1286 return -1;
1287 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001288 i++;
1289 }
1290 }
1291
1292 if (ver == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001293 ERR(NULL, "Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001294 }
1295
1296 return ver;
1297}
1298
1299static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001300nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001301{
Michal Vasko131120a2018-05-29 15:44:02 +02001302 NC_MSG_TYPE ret;
1303 int i, io_timeout;
Michal Vasko93224072021-11-09 12:14:28 +01001304 char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001305 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001306
Michal Vasko131120a2018-05-29 15:44:02 +02001307 if (session->side == NC_CLIENT) {
1308 /* client side hello - send only NETCONF base capabilities */
1309 cpblts = malloc(3 * sizeof *cpblts);
1310 if (!cpblts) {
1311 ERRMEM;
1312 return NC_MSG_ERROR;
1313 }
Michal Vasko93224072021-11-09 12:14:28 +01001314 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1315 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko131120a2018-05-29 15:44:02 +02001316 cpblts[2] = NULL;
1317
1318 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1319 sid = NULL;
1320 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001321 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0);
Michal Vasko5b24b6b2020-12-04 09:29:47 +01001322 if (!cpblts) {
1323 return NC_MSG_ERROR;
1324 }
Michal Vasko131120a2018-05-29 15:44:02 +02001325
1326 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1327 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001328 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001329
Michal Vasko131120a2018-05-29 15:44:02 +02001330 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001331
Michal Vasko086311b2016-01-08 09:53:11 +01001332 for (i = 0; cpblts[i]; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001333 free(cpblts[i]);
Michal Vasko086311b2016-01-08 09:53:11 +01001334 }
1335 free(cpblts);
1336
Michal Vasko131120a2018-05-29 15:44:02 +02001337 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001338}
1339
1340static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001341nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001342{
Michal Vasko77367452021-02-16 16:32:18 +01001343 struct ly_in *msg;
1344 struct lyd_node *hello = NULL, *iter;
1345 struct lyd_node_opaq *node;
1346 int r, ver = -1, flag = 0;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001347 char *str;
1348 long long int id;
Michal Vasko77367452021-02-16 16:32:18 +01001349 NC_MSG_TYPE rc = NC_MSG_HELLO;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001350
Michal Vasko77367452021-02-16 16:32:18 +01001351 r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg);
1352 switch (r) {
1353 case 1:
Radek Krejci695d4fa2015-10-22 13:23:54 +02001354 /* parse <hello> data */
Michal Vasko77367452021-02-16 16:32:18 +01001355 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 +02001356 ERR(session, "Failed to parse server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001357 rc = NC_MSG_ERROR;
1358 goto cleanup;
1359 }
1360
1361 LY_LIST_FOR(lyd_child(hello), iter) {
1362 node = (struct lyd_node_opaq *)iter;
1363
1364 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001365 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001366 } else if (!strcmp(node->name.name, "session-id")) {
1367 if (!node->value || !strlen(node->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001368 ERR(session, "No value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001369 rc = NC_MSG_ERROR;
1370 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001371 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001372 str = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001373 id = strtoll(node->value, &str, 10);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001374 if (*str || (id < 1) || (id > UINT32_MAX)) {
Michal Vasko05532772021-06-03 12:12:38 +02001375 ERR(session, "Invalid value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001376 rc = NC_MSG_ERROR;
1377 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001378 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001379 session->id = (uint32_t)id;
1380 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001381 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001382 ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name);
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
1387 if (flag) {
1388 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001389 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001390 rc = NC_MSG_ERROR;
1391 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001392 }
1393 flag = 1;
1394
Michal Vasko77367452021-02-16 16:32:18 +01001395 if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) {
1396 rc = NC_MSG_ERROR;
1397 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001398 }
1399 session->version = ver;
1400 }
1401
1402 if (!session->id) {
Michal Vasko05532772021-06-03 12:12:38 +02001403 ERR(session, "Missing <session-id> in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001404 rc = NC_MSG_ERROR;
1405 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001406 }
1407 break;
Michal Vasko77367452021-02-16 16:32:18 +01001408 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001409 ERR(session, "Server <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001410 rc = NC_MSG_WOULDBLOCK;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001411 break;
1412 default:
Michal Vasko77367452021-02-16 16:32:18 +01001413 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001414 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001415 }
1416
Michal Vasko77367452021-02-16 16:32:18 +01001417cleanup:
1418 ly_in_free(msg, 1);
1419 lyd_free_tree(hello);
1420 return rc;
Radek Krejci5686ff72015-10-09 13:33:56 +02001421}
1422
Michal Vasko11d142a2016-01-19 15:58:24 +01001423static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001424nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001425{
Michal Vasko77367452021-02-16 16:32:18 +01001426 struct ly_in *msg;
1427 struct lyd_node *hello = NULL, *iter;
1428 struct lyd_node_opaq *node;
1429 NC_MSG_TYPE rc = NC_MSG_HELLO;
1430 int r, ver = -1, flag = 0, timeout_io;
Michal Vasko11d142a2016-01-19 15:58:24 +01001431
Michal Vasko77367452021-02-16 16:32:18 +01001432 timeout_io = server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000;
1433 r = nc_read_msg_poll_io(session, timeout_io, &msg);
1434 switch (r) {
1435 case 1:
1436 /* parse <hello> data */
1437 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 +02001438 ERR(session, "Failed to parse client <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001439 rc = NC_MSG_ERROR;
1440 goto cleanup;
1441 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001442
Michal Vasko77367452021-02-16 16:32:18 +01001443 /* learn NETCONF version */
1444 LY_LIST_FOR(lyd_child(hello), iter) {
1445 node = (struct lyd_node_opaq *)iter;
1446
1447 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001448 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001449 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001450 ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001451 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001452 goto cleanup;
1453 }
1454
1455 if (flag) {
1456 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001457 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001458 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001459 goto cleanup;
1460 }
1461 flag = 1;
1462
Michal Vasko77367452021-02-16 16:32:18 +01001463 if ((ver = parse_cpblts(&node->node, NULL)) < 0) {
1464 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001465 goto cleanup;
1466 }
1467 session->version = ver;
1468 }
1469 break;
Michal Vasko77367452021-02-16 16:32:18 +01001470 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001471 ERR(session, "Client <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001472 rc = NC_MSG_WOULDBLOCK;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001473 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001474 default:
Michal Vasko77367452021-02-16 16:32:18 +01001475 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001476 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001477 }
1478
1479cleanup:
Michal Vasko77367452021-02-16 16:32:18 +01001480 ly_in_free(msg, 1);
1481 lyd_free_tree(hello);
1482 return rc;
Michal Vasko11d142a2016-01-19 15:58:24 +01001483}
1484
Michal Vasko71090fc2016-05-24 16:37:28 +02001485NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001486nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001487{
Michal Vasko086311b2016-01-08 09:53:11 +01001488 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001489
Michal Vasko131120a2018-05-29 15:44:02 +02001490 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001491 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001492 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001493 }
1494
Michal Vasko11d142a2016-01-19 15:58:24 +01001495 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001496 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001497 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001498 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001499 }
1500
Michal Vasko71090fc2016-05-24 16:37:28 +02001501 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001502}
Michal Vasko086311b2016-01-08 09:53:11 +01001503
Michal Vasko889162e2019-09-06 14:43:37 +02001504#ifdef NC_ENABLED_SSH
1505
Michal Vasko999b64a2019-07-10 16:06:15 +02001506static void
1507nc_ssh_init(void)
1508{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001509#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001510 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1511 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001512#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001513}
1514
1515static void
1516nc_ssh_destroy(void)
1517{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001518#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001519 FIPS_mode_set(0);
1520 CONF_modules_unload(1);
1521 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001522#endif
1523
Michal Vaskoe1e82632019-09-09 09:18:03 +02001524#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1525 ssh_finalize();
1526#endif
1527}
1528
Michal Vasko889162e2019-09-06 14:43:37 +02001529#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001530
Radek Krejci53691be2016-02-22 13:58:37 +01001531#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001532
Michal Vasko770b4362017-02-17 14:44:18 +01001533#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1534
Michal Vaskof0c92c02016-01-29 09:41:45 +01001535struct CRYPTO_dynlock_value {
1536 pthread_mutex_t lock;
1537};
1538
Michal Vaskof0c92c02016-01-29 09:41:45 +01001539static struct CRYPTO_dynlock_value *
1540tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1541{
1542 struct CRYPTO_dynlock_value *value;
1543
1544 value = malloc(sizeof *value);
1545 if (!value) {
1546 ERRMEM;
1547 return NULL;
1548 }
1549 pthread_mutex_init(&value->lock, NULL);
1550
1551 return value;
1552}
1553
1554static void
1555tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1556{
1557 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1558 * I found ignored this fact, what do I know... */
1559 if (mode & CRYPTO_LOCK) {
1560 pthread_mutex_lock(&l->lock);
1561 } else {
1562 pthread_mutex_unlock(&l->lock);
1563 }
1564}
1565
1566static void
1567tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1568{
1569 pthread_mutex_destroy(&l->lock);
1570 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001571}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001572
Michal Vasko770b4362017-02-17 14:44:18 +01001573#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001574
Michal Vasko8f0c0282016-02-29 10:17:14 +01001575#endif /* NC_ENABLED_TLS */
1576
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001577#if defined (NC_ENABLED_TLS) && !defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001578
Michal Vasko770b4362017-02-17 14:44:18 +01001579#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001580static pthread_mutex_t *tls_locks;
1581
1582static void
1583tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1584{
1585 if (mode & CRYPTO_LOCK) {
1586 pthread_mutex_lock(tls_locks + n);
1587 } else {
1588 pthread_mutex_unlock(tls_locks + n);
1589 }
1590}
1591
1592static void
1593tls_thread_id_func(CRYPTO_THREADID *tid)
1594{
1595 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1596}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001597
Michal Vasko770b4362017-02-17 14:44:18 +01001598#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001599
1600static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001601nc_tls_init(void)
1602{
Rosen Penev4f552d62019-06-26 16:10:43 -07001603#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001604 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001605 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001606 SSL_library_init();
1607
Michal Vaskof89948c2018-01-04 09:19:46 +01001608 int i;
1609
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001610 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001611 if (!tls_locks) {
1612 ERRMEM;
1613 return;
1614 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001615 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1616 pthread_mutex_init(tls_locks + i, NULL);
1617 }
1618
Michal Vaskof0c92c02016-01-29 09:41:45 +01001619 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001620 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001621
1622 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1623 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1624 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001625#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001626}
1627
Michal Vasko8f0c0282016-02-29 10:17:14 +01001628static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001629nc_tls_destroy(void)
1630{
Rosen Penev4f552d62019-06-26 16:10:43 -07001631#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001632 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001633 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001634 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001635 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001636 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001637#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001638 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001639#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1640 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001641#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001642
Michal Vaskof89948c2018-01-04 09:19:46 +01001643 int i;
1644
Michal Vaskob6e37262016-02-25 14:49:00 +01001645 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001646 CRYPTO_set_locking_callback(NULL);
1647 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1648 pthread_mutex_destroy(tls_locks + i);
1649 }
1650 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001651
1652 CRYPTO_set_dynlock_create_callback(NULL);
1653 CRYPTO_set_dynlock_lock_callback(NULL);
1654 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001655#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001656}
1657
Michal Vasko8f0c0282016-02-29 10:17:14 +01001658#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001659
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001660#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001661
Michal Vasko8f0c0282016-02-29 10:17:14 +01001662static void
Michal Vasko5e228792016-02-03 15:30:24 +01001663nc_ssh_tls_init(void)
1664{
Rosen Penev4f552d62019-06-26 16:10:43 -07001665#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001666 SSL_load_error_strings();
1667 ERR_load_BIO_strings();
1668 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001669#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001670
1671 nc_ssh_init();
1672
Michal Vaskoe1e82632019-09-09 09:18:03 +02001673#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001674 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1675 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1676 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001677#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001678}
1679
Michal Vasko8f0c0282016-02-29 10:17:14 +01001680static void
Michal Vasko5e228792016-02-03 15:30:24 +01001681nc_ssh_tls_destroy(void)
1682{
Rosen Penev4f552d62019-06-26 16:10:43 -07001683#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001684 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001685# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001686 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001687# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001688 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001689# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001690#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001691
1692 nc_ssh_destroy();
1693
Michal Vaskoe1e82632019-09-09 09:18:03 +02001694#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001695 CRYPTO_set_dynlock_create_callback(NULL);
1696 CRYPTO_set_dynlock_lock_callback(NULL);
1697 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001698#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001699}
1700
Radek Krejci53691be2016-02-22 13:58:37 +01001701#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001702
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001703#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001704
1705API void
1706nc_thread_destroy(void)
1707{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001708 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001709 // CRYPTO_cleanup_all_ex_data();
Michal Vasko8f0c0282016-02-29 10:17:14 +01001710
Michal Vasko770b4362017-02-17 14:44:18 +01001711#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1712 CRYPTO_THREADID crypto_tid;
1713
Michal Vasko8f0c0282016-02-29 10:17:14 +01001714 CRYPTO_THREADID_current(&crypto_tid);
1715 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001716#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001717}
1718
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001719#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1720
1721void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001722nc_init(void)
1723{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001724#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001725 nc_ssh_tls_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001726#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001727 nc_ssh_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001728#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001729 nc_tls_init();
1730#endif
1731}
1732
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001733void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001734nc_destroy(void)
1735{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001736#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001737 nc_ssh_tls_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001738#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001739 nc_ssh_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001740#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001741 nc_tls_destroy();
1742#endif
1743}