blob: e97ea7baa52b07d8246b78bcc4beb46b5fe7f623 [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
Michal Vasko77a6abe2017-10-05 10:02:20 +020060nc_gettimespec_mono(struct timespec *ts)
61{
62#ifdef CLOCK_MONOTONIC_RAW
63 return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
Michal Vaskob83a3fa2021-05-26 09:53:42 +020064#elif defined (CLOCK_MONOTONIC)
Michal Vasko77a6abe2017-10-05 10:02:20 +020065 return clock_gettime(CLOCK_MONOTONIC, ts);
66#else
67 /* no monotonic clock available, return realtime */
68 return nc_gettimespec_real(ts);
69#endif
70}
71
72int
73nc_gettimespec_real(struct timespec *ts)
Radek Krejci7ac16052016-07-15 11:48:18 +020074{
Michal Vasko0ef46df2016-09-21 14:03:18 +020075#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020076 return clock_gettime(CLOCK_REALTIME, ts);
77#else
78 int rc;
79 struct timeval tv;
80
81 rc = gettimeofday(&tv, NULL);
82 if (!rc) {
83 ts->tv_sec = (time_t)tv.tv_sec;
84 ts->tv_nsec = 1000L * (long)tv.tv_usec;
85 }
86 return rc;
87#endif
88}
89
Michal Vasko36c7be82017-02-22 13:37:59 +010090/* ts1 < ts2 -> +, ts1 > ts2 -> -, returns milliseconds */
91int32_t
Radek Krejcida0afb22017-05-26 16:25:59 +020092nc_difftimespec(const struct timespec *ts1, const struct timespec *ts2)
Michal Vasko99f251b2017-01-11 11:31:46 +010093{
Michal Vasko36c7be82017-02-22 13:37:59 +010094 int64_t nsec_diff = 0;
Michal Vasko99f251b2017-01-11 11:31:46 +010095
Michal Vaskoa82bd202017-03-03 14:07:29 +010096 nsec_diff += (((int64_t)ts2->tv_sec) - ((int64_t)ts1->tv_sec)) * 1000000000L;
97 nsec_diff += ((int64_t)ts2->tv_nsec) - ((int64_t)ts1->tv_nsec);
Michal Vasko99f251b2017-01-11 11:31:46 +010098
Michal Vaskob83a3fa2021-05-26 09:53:42 +020099 return nsec_diff ? nsec_diff / 1000000L : 0;
Michal Vasko99f251b2017-01-11 11:31:46 +0100100}
101
Michal Vasko36c7be82017-02-22 13:37:59 +0100102void
103nc_addtimespec(struct timespec *ts, uint32_t msec)
104{
Michal Vasko81c5b302017-03-15 12:10:40 +0100105 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
106
Michal Vasko0dfcd332017-03-03 13:14:39 +0100107 ts->tv_sec += msec / 1000;
108 ts->tv_nsec += (msec % 1000) * 1000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100109
Michal Vasko81c5b302017-03-15 12:10:40 +0100110 if (ts->tv_nsec >= 1000000000L) {
111 ++ts->tv_sec;
112 ts->tv_nsec -= 1000000000L;
113 } else if (ts->tv_nsec < 0) {
114 --ts->tv_sec;
115 ts->tv_nsec += 1000000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100116 }
Michal Vasko81c5b302017-03-15 12:10:40 +0100117
118 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
Michal Vasko36c7be82017-02-22 13:37:59 +0100119}
120
Michal Vaskoddce1212019-05-24 09:58:49 +0200121const char *
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200122nc_keytype2str(NC_SSH_KEY_TYPE type)
Michal Vaskoddce1212019-05-24 09:58:49 +0200123{
124 switch (type) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200125 case NC_SSH_KEY_DSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200126 return "DSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200127 case NC_SSH_KEY_RSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200128 return "RSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200129 case NC_SSH_KEY_ECDSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200130 return "EC";
131 default:
132 break;
133 }
134
135 return NULL;
136}
137
Michal Vaskobe52dc22018-10-17 09:28:17 +0200138int
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200139nc_sock_enable_keepalive(int sock, struct nc_keepalives *ka)
Michal Vaskobe52dc22018-10-17 09:28:17 +0200140{
141 int opt;
142
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200143 opt = ka->enabled;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200144 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200145 ERR(NULL, "Could not set SO_KEEPALIVE option (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200146 return -1;
147 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200148 if (!ka->enabled) {
149 return 0;
150 }
Michal Vaskobe52dc22018-10-17 09:28:17 +0200151
152#ifdef TCP_KEEPIDLE
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200153 opt = ka->idle_time;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200154 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200155 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200156 return -1;
157 }
158#endif
159
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200160#ifdef TCP_KEEPCNT
161 opt = ka->max_probes;
162 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200163 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200164 return -1;
165 }
166#endif
167
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200168#ifdef TCP_KEEPINTVL
169 opt = ka->probe_interval;
170 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200171 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200172 return -1;
173 }
174#endif
175
176 return 0;
177}
178
Michal Vaskoade892d2017-02-22 13:40:35 +0100179struct nc_session *
Michal Vasko131120a2018-05-29 15:44:02 +0200180nc_new_session(NC_SIDE side, int shared_ti)
Michal Vaskoade892d2017-02-22 13:40:35 +0100181{
182 struct nc_session *sess;
183
184 sess = calloc(1, sizeof *sess);
185 if (!sess) {
186 return NULL;
187 }
188
Michal Vasko131120a2018-05-29 15:44:02 +0200189 sess->side = side;
190
191 if (side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200192 pthread_mutex_init(&sess->opts.server.ntf_status_lock, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100193 pthread_mutex_init(&sess->opts.server.rpc_lock, NULL);
194 pthread_cond_init(&sess->opts.server.rpc_cond, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100195
196 pthread_mutex_init(&sess->opts.server.ch_lock, NULL);
197 pthread_cond_init(&sess->opts.server.ch_cond, NULL);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200198 } else {
199 pthread_mutex_init(&sess->opts.client.msgs_lock, NULL);
Michal Vasko131120a2018-05-29 15:44:02 +0200200 }
201
202 if (!shared_ti) {
203 sess->io_lock = malloc(sizeof *sess->io_lock);
204 if (!sess->io_lock) {
205 goto error;
206 }
207 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100208 }
209
210 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200211
212error:
Michal Vasko131120a2018-05-29 15:44:02 +0200213 free(sess);
214 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100215}
216
Michal Vasko96164bf2016-01-21 15:41:58 +0100217/*
218 * @return 1 - success
219 * 0 - timeout
220 * -1 - error
221 */
222int
Michal Vasko131120a2018-05-29 15:44:02 +0200223nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100224{
225 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100226 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100227
Michal Vasko131120a2018-05-29 15:44:02 +0200228 if (session->side != NC_SERVER) {
229 ERRINT;
230 return -1;
231 }
232
Michal Vasko96164bf2016-01-21 15:41:58 +0100233 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200234 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100235 nc_addtimespec(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100236
Michal Vaskoade892d2017-02-22 13:40:35 +0100237 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100238 ret = pthread_mutex_timedlock(&session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100239 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100240 while (session->opts.server.rpc_inuse) {
241 ret = pthread_cond_timedwait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100242 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100243 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100244 break;
245 }
246 }
247 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100248 } else if (!timeout) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100249 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100250 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100251 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100252 if (session->opts.server.rpc_inuse) {
253 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100254 return 0;
255 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200256 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100257 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100258 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100259 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100260 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100261 while (session->opts.server.rpc_inuse) {
262 ret = pthread_cond_wait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100263 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100264 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100265 break;
266 }
267 }
268 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100269 }
270
Michal Vaskoade892d2017-02-22 13:40:35 +0100271 if (ret) {
272 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
273 /* timeout */
274 return 0;
275 }
276
Michal Vasko96164bf2016-01-21 15:41:58 +0100277 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200278 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100279 return -1;
280 }
281
282 /* ok */
Michal Vaskoacf98472021-02-04 15:33:57 +0100283 assert(session->opts.server.rpc_inuse == 0);
284 session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100285
286 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100287 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100288 if (ret) {
289 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200290 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100291 return -1;
292 }
293
294 return 1;
295}
296
297int
Michal Vasko131120a2018-05-29 15:44:02 +0200298nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100299{
300 int ret;
301 struct timespec ts_timeout;
302
Michal Vasko131120a2018-05-29 15:44:02 +0200303 if (session->side != NC_SERVER) {
304 ERRINT;
305 return -1;
306 }
307
Michal Vaskoacf98472021-02-04 15:33:57 +0100308 assert(session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100309
310 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200311 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100312 nc_addtimespec(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100313
314 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100315 ret = pthread_mutex_timedlock(&session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100316 } else if (!timeout) {
317 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100318 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100319 } else { /* timeout == -1 */
320 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100321 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100322 }
323
324 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
325 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200326 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100327 return -1;
328 } else if (ret) {
Michal Vasko05532772021-06-03 12:12:38 +0200329 WRN(session, "%s: session RPC lock timeout, should not happen.");
Michal Vaskoade892d2017-02-22 13:40:35 +0100330 }
331
Michal Vaskoacf98472021-02-04 15:33:57 +0100332 session->opts.server.rpc_inuse = 0;
333 pthread_cond_signal(&session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100334
335 if (!ret) {
336 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100337 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100338 if (ret) {
339 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200340 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100341 return -1;
342 }
343 }
344
Michal Vasko96164bf2016-01-21 15:41:58 +0100345 return 1;
346}
347
Michal Vasko131120a2018-05-29 15:44:02 +0200348int
349nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
350{
351 int ret;
352 struct timespec ts_timeout;
353
354 if (timeout > 0) {
355 nc_gettimespec_real(&ts_timeout);
356 nc_addtimespec(&ts_timeout, timeout);
357
358 ret = pthread_mutex_timedlock(session->io_lock, &ts_timeout);
359 } else if (!timeout) {
360 ret = pthread_mutex_trylock(session->io_lock);
361 } else { /* timeout == -1 */
Robin Jarry54ea2962018-10-10 10:33:40 +0200362 ret = pthread_mutex_lock(session->io_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200363 }
364
365 if (ret) {
366 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
367 /* timeout */
368 return 0;
369 }
370
371 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200372 ERR(session, "%s: failed to IO lock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200373 return -1;
374 }
375
376 return 1;
377}
378
379int
380nc_session_io_unlock(struct nc_session *session, const char *func)
381{
382 int ret;
383
384 ret = pthread_mutex_unlock(session->io_lock);
385 if (ret) {
386 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200387 ERR(session, "%s: failed to IO unlock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200388 return -1;
389 }
390
391 return 1;
392}
393
Michal Vasko01130bd2021-08-26 11:47:38 +0200394int
395nc_session_client_msgs_lock(struct nc_session *session, int *timeout, const char *func)
396{
397 int ret;
398 int32_t diff_msec;
399 struct timespec ts_timeout, ts_start, ts_end;
400
401 assert(session->side == NC_CLIENT);
402
403 if (*timeout > 0) {
404 /* get current time */
405 nc_gettimespec_real(&ts_start);
406
407 nc_gettimespec_real(&ts_timeout);
408 nc_addtimespec(&ts_timeout, *timeout);
409
410 ret = pthread_mutex_timedlock(&session->opts.client.msgs_lock, &ts_timeout);
411 if (!ret) {
412 /* update timeout based on what was elapsed */
413 nc_gettimespec_real(&ts_end);
414 diff_msec = nc_difftimespec(&ts_start, &ts_end);
415 *timeout -= diff_msec;
416 }
417 } else if (!*timeout) {
418 ret = pthread_mutex_trylock(&session->opts.client.msgs_lock);
419 } else { /* timeout == -1 */
420 ret = pthread_mutex_lock(&session->opts.client.msgs_lock);
421 }
422
423 if (ret) {
424 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
425 /* timeout */
426 return 0;
427 }
428
429 /* error */
430 ERR(session, "%s: failed to MSGS lock a session (%s).", func, strerror(ret));
431 return -1;
432 }
433
434 return 1;
435}
436
437int
438nc_session_client_msgs_unlock(struct nc_session *session, const char *func)
439{
440 int ret;
441
442 assert(session->side == NC_CLIENT);
443
444 ret = pthread_mutex_unlock(&session->opts.client.msgs_lock);
445 if (ret) {
446 /* error */
447 ERR(session, "%s: failed to MSGS unlock a session (%s).", func, strerror(ret));
448 return -1;
449 }
450
451 return 1;
452}
453
Michal Vasko8dadf782016-01-15 10:29:36 +0100454API NC_STATUS
455nc_session_get_status(const struct nc_session *session)
456{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100457 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200458 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200459 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100460 }
461
Michal Vasko8dadf782016-01-15 10:29:36 +0100462 return session->status;
463}
464
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100465API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200466nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100467{
468 if (!session) {
469 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200470 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100471 }
472
473 return session->term_reason;
474}
475
Michal Vasko8dadf782016-01-15 10:29:36 +0100476API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200477nc_session_get_killed_by(const struct nc_session *session)
478{
479 if (!session) {
480 ERRARG("session");
481 return 0;
482 }
483
484 return session->killed_by;
485}
486
487API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100488nc_session_get_id(const struct nc_session *session)
489{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100490 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200491 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100492 return 0;
493 }
494
Michal Vasko8dadf782016-01-15 10:29:36 +0100495 return session->id;
496}
497
Michal Vasko174fe8e2016-02-17 15:38:09 +0100498API int
499nc_session_get_version(const struct nc_session *session)
500{
501 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200502 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100503 return -1;
504 }
505
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200506 return session->version == NC_VERSION_10 ? 0 : 1;
Michal Vasko174fe8e2016-02-17 15:38:09 +0100507}
508
Michal Vasko8dadf782016-01-15 10:29:36 +0100509API NC_TRANSPORT_IMPL
510nc_session_get_ti(const struct nc_session *session)
511{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100512 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200513 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100514 return 0;
515 }
516
Michal Vasko8dadf782016-01-15 10:29:36 +0100517 return session->ti_type;
518}
519
520API const char *
521nc_session_get_username(const struct nc_session *session)
522{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100523 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200524 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100525 return NULL;
526 }
527
Michal Vasko8dadf782016-01-15 10:29:36 +0100528 return session->username;
529}
530
531API const char *
532nc_session_get_host(const struct nc_session *session)
533{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100534 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200535 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100536 return NULL;
537 }
538
Michal Vasko8dadf782016-01-15 10:29:36 +0100539 return session->host;
540}
541
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200542API const char *
543nc_session_get_path(const struct nc_session *session)
544{
545 if (!session) {
546 ERRARG("session");
547 return NULL;
548 }
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200549 if (session->ti_type != NC_TI_UNIX) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200550 return NULL;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200551 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200552
553 return session->path;
554}
555
Michal Vasko8dadf782016-01-15 10:29:36 +0100556API uint16_t
557nc_session_get_port(const struct nc_session *session)
558{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100559 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200560 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100561 return 0;
562 }
563
Michal Vasko8dadf782016-01-15 10:29:36 +0100564 return session->port;
565}
566
Michal Vasko93224072021-11-09 12:14:28 +0100567API const struct ly_ctx *
Michal Vasko9a25e932016-02-01 10:36:42 +0100568nc_session_get_ctx(const struct nc_session *session)
569{
570 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200571 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100572 return NULL;
573 }
574
575 return session->ctx;
576}
577
Michal Vasko2cc4c682016-03-01 09:16:48 +0100578API void
579nc_session_set_data(struct nc_session *session, void *data)
580{
581 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200582 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100583 return;
584 }
585
586 session->data = data;
587}
588
589API void *
590nc_session_get_data(const struct nc_session *session)
591{
592 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200593 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100594 return NULL;
595 }
596
597 return session->data;
598}
599
Michal Vasko086311b2016-01-08 09:53:11 +0100600NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200601nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200602{
Michal Vasko7e1f5fb2021-11-10 10:14:45 +0100603 if (session->ctx != LYD_CTX(op)) {
604 ERR(session, "RPC \"%s\" was created in different context than that of the session.", LYD_NAME(op));
Michal Vasko086311b2016-01-08 09:53:11 +0100605 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100606 }
607
Michal Vasko131120a2018-05-29 15:44:02 +0200608 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100609}
610
Michal Vaskod4da3632022-05-25 11:49:10 +0200611/**
612 * @brief Send \<close-session\> and read the reply on a session.
613 *
614 * @param[in] session Closing NETCONF session.
615 */
616static void
617nc_session_free_close_session(struct nc_session *session)
618{
619 struct ly_in *msg;
620 struct lyd_node *close_rpc, *envp;
621 const struct lys_module *ietfnc;
622
623 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
624 if (!ietfnc) {
625 WRN(session, "Missing ietf-netconf schema in context, unable to send <close-session>.");
626 return;
627 }
628 if (lyd_new_inner(NULL, ietfnc, "close-session", 0, &close_rpc)) {
629 WRN(session, "Failed to create <close-session> RPC.");
630 return;
631 }
632
633 /* send the RPC */
634 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
635
636read_msg:
637 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &msg)) {
638 case 1:
639 if (!strncmp(ly_in_memory(msg, NULL), "<notification", 13)) {
640 /* ignore */
641 ly_in_free(msg, 1);
642 goto read_msg;
643 }
644 if (lyd_parse_op(session->ctx, close_rpc, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, &envp, NULL)) {
645 WRN(session, "Failed to parse <close-session> reply.");
646 } else if (!lyd_child(envp) || strcmp(LYD_NAME(lyd_child(envp)), "ok")) {
647 WRN(session, "Reply to <close-session> was not <ok> as expected.");
648 }
649 lyd_free_tree(envp);
650 ly_in_free(msg, 1);
651 break;
652 case 0:
653 WRN(session, "Timeout for receiving a reply to <close-session> elapsed.");
654 break;
655 case -1:
656 ERR(session, "Failed to receive a reply to <close-session>.");
657 break;
658 default:
659 /* cannot happen */
660 break;
661 }
662 lyd_free_tree(close_rpc);
663}
664
Radek Krejci695d4fa2015-10-22 13:23:54 +0200665API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100666nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200667{
Michal Vasko01130bd2021-08-26 11:47:38 +0200668 int r, i, rpc_locked = 0, msgs_locked = 0, sock = -1, timeout;
Michal Vasko428087d2016-01-14 16:04:28 +0100669 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100670 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vasko4589bbe2016-01-29 09:41:30 +0100671 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100672 struct nc_msg_cont *contiter;
Michal Vasko77367452021-02-16 16:32:18 +0100673 struct ly_in *msg;
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200674 struct timespec ts, ts_cur;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200675 void *p;
676
Michal Vasko428087d2016-01-14 16:04:28 +0100677 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200678 return;
679 }
680
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200681 /* stop notifications thread if any */
682 if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
683 /* let the thread know it should quit */
684 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 2);
685
686 /* wait for it */
687 nc_gettimespec_mono(&ts);
688 nc_addtimespec(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
689 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
690 usleep(NC_TIMEOUT_STEP);
691 nc_gettimespec_mono(&ts_cur);
692 if (nc_difftimespec(&ts_cur, &ts) < 1) {
693 ERR(session, "Waiting for notification thread exit failed (timed out).");
694 break;
695 }
696 }
Michal Vasko86d357c2016-03-11 13:46:38 +0100697 }
698
Michal Vaskoacf98472021-02-04 15:33:57 +0100699 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200700 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100701 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100702 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200703 } else if (r) {
704 rpc_locked = 1;
Michal Vasko96a28a32021-02-04 15:35:20 +0100705 } else {
706 /* else failed to lock it, too bad */
Michal Vasko05532772021-06-03 12:12:38 +0200707 ERR(session, "Freeing a session while an RPC is being processed.");
Michal Vasko96a28a32021-02-04 15:35:20 +0100708 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200709 }
710
Michal Vasko8c247822020-09-07 13:23:23 +0200711 if (session->side == NC_CLIENT) {
Michal Vasko01130bd2021-08-26 11:47:38 +0200712 timeout = NC_SESSION_FREE_LOCK_TIMEOUT;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200713
Michal Vasko01130bd2021-08-26 11:47:38 +0200714 /* MSGS LOCK */
715 r = nc_session_client_msgs_lock(session, &timeout, __func__);
716 if (r == -1) {
717 return;
718 } else if (r) {
719 msgs_locked = 1;
720 } else {
721 /* else failed to lock it, too bad */
722 ERR(session, "Freeing a session while messages are being received.");
723 }
724
725 /* cleanup message queue */
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200726 for (contiter = session->opts.client.msgs; contiter; ) {
Michal Vasko77367452021-02-16 16:32:18 +0100727 ly_in_free(contiter->msg, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200728
Michal Vaskoad611702015-12-03 13:41:51 +0100729 p = contiter;
730 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200731 free(p);
732 }
733
Michal Vasko01130bd2021-08-26 11:47:38 +0200734 if (msgs_locked) {
735 /* MSGS UNLOCK */
736 nc_session_client_msgs_unlock(session, __func__);
737 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200738
Michal Vasko8c247822020-09-07 13:23:23 +0200739 if (session->status == NC_STATUS_RUNNING) {
Michal Vasko9c6d38c2021-09-03 13:02:53 +0200740 /* receive any leftover messages */
741 while (nc_read_msg_poll_io(session, 0, &msg) == 1) {
742 ly_in_free(msg, 1);
743 }
744
Michal Vasko8c247822020-09-07 13:23:23 +0200745 /* send closing info to the other side */
Michal Vaskod4da3632022-05-25 11:49:10 +0200746 nc_session_free_close_session(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200747 }
748
749 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200750 if (session->opts.client.cpblts) {
751 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200752 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200753 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200754 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200755 }
756 }
757
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100758 if (session->data && data_free) {
759 data_free(session->data);
760 }
761
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200762 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
763 /* CH LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100764 pthread_mutex_lock(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200765 }
766
Michal Vasko86d357c2016-03-11 13:46:38 +0100767 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200768 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200769
Michal Vaskofeccb312022-03-24 15:24:59 +0100770 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100771 pthread_cond_signal(&session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200772
Michal Vasko0db3db52021-03-03 10:45:42 +0100773 nc_gettimespec_real(&ts);
774 nc_addtimespec(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
775
776 /* wait for CH thread to actually wake up and terminate */
777 r = 0;
Michal Vaskofeccb312022-03-24 15:24:59 +0100778 while (!r && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vasko0db3db52021-03-03 10:45:42 +0100779 r = pthread_cond_timedwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, &ts);
780 }
Michal Vasko0db3db52021-03-03 10:45:42 +0100781 if (r) {
Michal Vasko05532772021-06-03 12:12:38 +0200782 ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r));
Michal Vasko3f05a092018-03-13 10:39:49 +0100783 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200784 }
785
Michal Vaskofeccb312022-03-24 15:24:59 +0100786 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
787 /* CH UNLOCK */
788 pthread_mutex_unlock(&session->opts.server.ch_lock);
789 }
790
Michal Vasko428087d2016-01-14 16:04:28 +0100791 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200792
793 /* transport implementation cleanup */
794 switch (session->ti_type) {
795 case NC_TI_FD:
796 /* nothing needed - file descriptors were provided by caller,
797 * so it is up to the caller to close them correctly
798 * TODO use callbacks
799 */
Michal Vasko3512e402016-01-28 16:22:34 +0100800 /* just to avoid compiler warning */
801 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100802 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200803 break;
804
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200805 case NC_TI_UNIX:
806 sock = session->ti.unixsock.sock;
807 (void)connected;
808 (void)siter;
809 break;
810
Radek Krejci53691be2016-02-22 13:58:37 +0100811#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200812 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100813 if (connected) {
814 ssh_channel_free(session->ti.libssh.channel);
815 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200816 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
817 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200818 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
Radek Krejci695d4fa2015-10-22 13:23:54 +0200819 */
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200820 /* SESSION IO LOCK */
821 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
822
Michal Vasko96164bf2016-01-21 15:41:58 +0100823 multisession = 0;
824 if (session->ti.libssh.next) {
825 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
826 if (siter->status != NC_STATUS_STARTING) {
827 multisession = 1;
828 break;
829 }
830 }
831 }
832
833 if (!multisession) {
834 /* it's not multisession yet, but we still need to free the starting sessions */
835 if (session->ti.libssh.next) {
836 do {
837 siter = session->ti.libssh.next;
838 session->ti.libssh.next = siter->ti.libssh.next;
839
840 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vaskoe38655d2021-11-12 15:41:16 +0100841 free(siter->username);
842 free(siter->host);
843 if (!(siter->flags & NC_SESSION_SHAREDCTX)) {
844 ly_ctx_destroy((struct ly_ctx *)siter->ctx);
Michal Vasko96164bf2016-01-21 15:41:58 +0100845 }
846
847 free(siter);
848 } while (session->ti.libssh.next != session);
849 }
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100850 /* remember sock so we can close it */
851 sock = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100852 if (connected) {
853 ssh_disconnect(session->ti.libssh.session);
Michal Vasko06e0fc22019-05-09 09:32:27 +0200854 sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100855 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200856 ssh_free(session->ti.libssh.session);
857 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200858 /* remove the session from the list */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200859 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {}
Michal Vaskoaec4f212015-10-26 15:37:45 +0100860 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200861 /* there will be only one session */
862 siter->ti.libssh.next = NULL;
863 } else {
864 /* there are still multiple sessions, keep the ring list */
865 siter->ti.libssh.next = session->ti.libssh.next;
866 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100867 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
868 if (session->flags & NC_SESSION_SSH_MSG_CB) {
Michal Vasko5e0edd82020-07-29 15:26:13 +0200869 siter = session->ti.libssh.next;
870 while (siter && siter->status != NC_STATUS_RUNNING) {
Michal Vasko96164bf2016-01-21 15:41:58 +0100871 if (siter->ti.libssh.next == session) {
872 ERRINT;
873 break;
874 }
Michal Vasko5e0edd82020-07-29 15:26:13 +0200875 siter = siter->ti.libssh.next;
Michal Vasko96164bf2016-01-21 15:41:58 +0100876 }
Michal Vasko5e0edd82020-07-29 15:26:13 +0200877 /* siter may be NULL in case all the sessions terminated at the same time (socket was disconnected),
878 * we set session to NULL because we do not expect any new message to arrive */
Michal Vasko96164bf2016-01-21 15:41:58 +0100879 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
Michal Vasko5e0edd82020-07-29 15:26:13 +0200880 if (siter) {
881 siter->flags |= NC_SESSION_SSH_MSG_CB;
882 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100883 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200884 }
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200885
886 /* SESSION IO UNLOCK */
887 if (r == 1) {
888 nc_session_io_unlock(session, __func__);
889 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200890 break;
891#endif
892
Radek Krejci53691be2016-02-22 13:58:37 +0100893#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200894 case NC_TI_OPENSSL:
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100895 /* remember sock so we can close it */
896 sock = SSL_get_fd(session->ti.tls);
897
Michal Vasko428087d2016-01-14 16:04:28 +0100898 if (connected) {
899 SSL_shutdown(session->ti.tls);
900 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200901 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100902
Michal Vasko2e6defd2016-10-07 15:48:15 +0200903 if (session->side == NC_SERVER) {
904 X509_free(session->opts.server.client_cert);
905 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200906 break;
907#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100908 case NC_TI_NONE:
Michal Vasko428087d2016-01-14 16:04:28 +0100909 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200910 }
Michal Vasko428087d2016-01-14 16:04:28 +0100911
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100912 /* close socket separately */
913 if (sock > -1) {
914 close(sock);
915 }
916
Michal Vasko93224072021-11-09 12:14:28 +0100917 free(session->username);
918 free(session->host);
919 free(session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200920
921 /* final cleanup */
Michal Vaskoacf98472021-02-04 15:33:57 +0100922 if (session->side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200923 pthread_mutex_destroy(&session->opts.server.ntf_status_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200924 if (rpc_locked) {
925 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100926 }
Michal Vaskoacf98472021-02-04 15:33:57 +0100927 pthread_mutex_destroy(&session->opts.server.rpc_lock);
928 pthread_cond_destroy(&session->opts.server.rpc_cond);
Michal Vasko131120a2018-05-29 15:44:02 +0200929 }
930
931 if (session->io_lock && !multisession) {
932 pthread_mutex_destroy(session->io_lock);
933 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200934 }
935
936 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko93224072021-11-09 12:14:28 +0100937 ly_ctx_destroy((struct ly_ctx *)session->ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200938 }
939
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200940 if (session->side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100941 /* free CH synchronization structures */
942 pthread_cond_destroy(&session->opts.server.ch_cond);
943 pthread_mutex_destroy(&session->opts.server.ch_lock);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200944 } else {
945 pthread_mutex_destroy(&session->opts.client.msgs_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200946 }
947
Radek Krejci695d4fa2015-10-22 13:23:54 +0200948 free(session);
949}
950
Michal Vasko086311b2016-01-08 09:53:11 +0100951static void
Michal Vasko93224072021-11-09 12:14:28 +0100952add_cpblt(const char *capab, char ***cpblts, int *size, int *count)
Michal Vasko086311b2016-01-08 09:53:11 +0100953{
Radek Krejci658782b2016-12-04 22:04:55 +0100954 size_t len;
955 int i;
956 char *p;
957
958 if (capab) {
959 /* check if already present */
960 p = strchr(capab, '?');
961 if (p) {
962 len = p - capab;
963 } else {
964 len = strlen(capab);
965 }
966 for (i = 0; i < *count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200967 if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) {
Radek Krejci658782b2016-12-04 22:04:55 +0100968 /* already present, do not duplicate it */
969 return;
970 }
971 }
972 }
973
974 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100975 if (*count == *size) {
976 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100977 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
978 if (!(*cpblts)) {
979 ERRMEM;
980 return;
981 }
Michal Vasko086311b2016-01-08 09:53:11 +0100982 }
983
Michal Vasko93224072021-11-09 12:14:28 +0100984 (*cpblts)[*count] = capab ? strdup(capab) : NULL;
Michal Vasko086311b2016-01-08 09:53:11 +0100985 ++(*count);
986}
987
Michal Vasko93224072021-11-09 12:14:28 +0100988API char **
989nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +0100990{
Michal Vasko93224072021-11-09 12:14:28 +0100991 char **cpblts;
Michal Vasko77367452021-02-16 16:32:18 +0100992 const struct lys_module *mod;
993 struct lysp_feature *feat;
994 int size = 10, count, features_count = 0, dev_count = 0, str_len, len;
Michal Vasko1440a742021-03-31 11:11:03 +0200995 uint32_t i, u;
Michal Vasko77367452021-02-16 16:32:18 +0100996 LY_ARRAY_COUNT_TYPE v;
Michal Vasko1440a742021-03-31 11:11:03 +0200997 char *yl_content_id;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200998
Radek Krejci24a18412018-05-16 15:09:10 +0200999#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +02001000 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001001
Michal Vasko4ffa3b22016-05-24 16:36:25 +02001002 if (!ctx) {
1003 ERRARG("ctx");
1004 return NULL;
1005 }
1006
Michal Vasko086311b2016-01-08 09:53:11 +01001007 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001008 if (!cpblts) {
1009 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +02001010 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001011 }
Michal Vasko93224072021-11-09 12:14:28 +01001012 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1013 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko086311b2016-01-08 09:53:11 +01001014 count = 2;
1015
1016 /* capabilities */
1017
Michal Vasko77367452021-02-16 16:32:18 +01001018 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf");
Michal Vasko086311b2016-01-08 09:53:11 +01001019 if (mod) {
Michal Vasko77367452021-02-16 16:32:18 +01001020 if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001021 add_cpblt("urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001022 }
Michal Vasko77367452021-02-16 16:32:18 +01001023 if (lys_feature_value(mod, "candidate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001024 add_cpblt("urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko77367452021-02-16 16:32:18 +01001025 if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001026 add_cpblt("urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001027 }
1028 }
Michal Vasko77367452021-02-16 16:32:18 +01001029 if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001030 add_cpblt("urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001031 }
Michal Vasko77367452021-02-16 16:32:18 +01001032 if (lys_feature_value(mod, "validate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001033 add_cpblt("urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001034 }
Michal Vasko77367452021-02-16 16:32:18 +01001035 if (lys_feature_value(mod, "startup") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001036 add_cpblt("urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001037 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001038
1039 /* The URL capability must be set manually using nc_server_set_capability()
1040 * because of the need for supported protocols to be included.
1041 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1042 */
Michal Vasko77367452021-02-16 16:32:18 +01001043 // if (lys_feature_value(mod, "url") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001044 // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
mekleoa8de5e92020-02-13 09:05:56 +01001045 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001046
Michal Vasko77367452021-02-16 16:32:18 +01001047 if (lys_feature_value(mod, "xpath") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001048 add_cpblt("urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001049 }
1050 }
1051
Michal Vasko77367452021-02-16 16:32:18 +01001052 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01001053 if (mod) {
1054 if (!server_opts.wd_basic_mode) {
Michal Vasko05532772021-06-03 12:12:38 +02001055 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 +01001056 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001057 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +01001058 switch (server_opts.wd_basic_mode) {
1059 case NC_WD_ALL:
1060 strcat(str, "?basic-mode=report-all");
1061 break;
1062 case NC_WD_TRIM:
1063 strcat(str, "?basic-mode=trim");
1064 break;
1065 case NC_WD_EXPLICIT:
1066 strcat(str, "?basic-mode=explicit");
1067 break;
1068 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001069 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001070 break;
1071 }
1072
1073 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001074 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +01001075 if (server_opts.wd_also_supported & NC_WD_ALL) {
1076 strcat(str, "report-all,");
1077 }
1078 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
1079 strcat(str, "report-all-tagged,");
1080 }
1081 if (server_opts.wd_also_supported & NC_WD_TRIM) {
1082 strcat(str, "trim,");
1083 }
1084 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
1085 strcat(str, "explicit,");
1086 }
1087 str[strlen(str) - 1] = '\0';
1088
Michal Vasko93224072021-11-09 12:14:28 +01001089 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001090 }
1091 }
1092 }
1093
Radek Krejci658782b2016-12-04 22:04:55 +01001094 /* other capabilities */
1095 for (u = 0; u < server_opts.capabilities_count; u++) {
Michal Vasko93224072021-11-09 12:14:28 +01001096 add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001097 }
1098
1099 /* models */
Michal Vasko1440a742021-03-31 11:11:03 +02001100 u = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001101 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001102 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vasko77367452021-02-16 16:32:18 +01001103 if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) {
Michal Vasko05532772021-06-03 12:12:38 +02001104 ERR(NULL, "Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
Michal Vaskod5ada122020-03-19 18:28:06 +01001105 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001106 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001107
Michal Vasko1440a742021-03-31 11:11:03 +02001108 /* get content-id */
1109 if (server_opts.content_id_clb) {
1110 yl_content_id = server_opts.content_id_clb(server_opts.content_id_data);
1111 if (!yl_content_id) {
1112 ERRMEM;
1113 goto error;
1114 }
1115 } else {
1116 yl_content_id = malloc(11);
1117 if (!yl_content_id) {
1118 ERRMEM;
1119 goto error;
1120 }
1121 sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx));
1122 }
1123
Michal Vasko77367452021-02-16 16:32:18 +01001124 if (!strcmp(mod->revision, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001125 /* new one (capab defined in RFC 8526 section 2) */
Michal Vasko1440a742021-03-31 11:11:03 +02001126 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s",
1127 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001128 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001129 } else {
1130 /* old one (capab defined in RFC 7950 section 5.6.4) */
Michal Vasko1440a742021-03-31 11:11:03 +02001131 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s",
1132 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001133 add_cpblt(str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001134 }
Michal Vasko1440a742021-03-31 11:11:03 +02001135 free(yl_content_id);
Radek Krejci24a18412018-05-16 15:09:10 +02001136 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001137 } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) {
Radek Krejci24a18412018-05-16 15:09:10 +02001138 /* skip YANG 1.1 schemas */
1139 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001140 } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) {
Radek Krejci24a18412018-05-16 15:09:10 +02001141 /* skip YANG 1.0 schemas */
1142 continue;
1143 }
Michal Vasko086311b2016-01-08 09:53:11 +01001144
Michal Vasko77367452021-02-16 16:32:18 +01001145 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "",
1146 mod->revision ? mod->revision : "");
Radek Krejci24a18412018-05-16 15:09:10 +02001147
Michal Vaskodafdc742020-03-11 16:15:59 +01001148 features_count = 0;
1149 i = 0;
1150 feat = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001151 while ((feat = lysp_feature_next(feat, mod->parsed, &i))) {
Michal Vaskodafdc742020-03-11 16:15:59 +01001152 if (!(feat->flags & LYS_FENABLED)) {
1153 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001154 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001155 if (!features_count) {
1156 strcat(str, "&features=");
1157 str_len += 10;
1158 }
1159 len = strlen(feat->name);
1160 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1161 ERRINT;
1162 break;
1163 }
1164 if (features_count) {
1165 strcat(str, ",");
1166 ++str_len;
1167 }
1168 strcat(str, feat->name);
1169 str_len += len;
1170 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001171 }
Michal Vasko086311b2016-01-08 09:53:11 +01001172
Michal Vasko77367452021-02-16 16:32:18 +01001173 if (mod->deviated_by) {
Radek Krejci24a18412018-05-16 15:09:10 +02001174 strcat(str, "&deviations=");
1175 str_len += 12;
1176 dev_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +01001177 LY_ARRAY_FOR(mod->deviated_by, v) {
1178 len = strlen(mod->deviated_by[v]->name);
1179 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1180 ERRINT;
1181 break;
Radek Krejci24a18412018-05-16 15:09:10 +02001182 }
Michal Vasko77367452021-02-16 16:32:18 +01001183 if (dev_count) {
1184 strcat(str, ",");
1185 ++str_len;
Radek Krejci24a18412018-05-16 15:09:10 +02001186 }
Michal Vasko77367452021-02-16 16:32:18 +01001187 strcat(str, mod->deviated_by[v]->name);
1188 str_len += len;
1189 dev_count++;
Radek Krejci24a18412018-05-16 15:09:10 +02001190 }
1191 }
1192
Michal Vasko93224072021-11-09 12:14:28 +01001193 add_cpblt(str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +02001194 }
Michal Vasko086311b2016-01-08 09:53:11 +01001195
1196 /* ending NULL capability */
Michal Vasko93224072021-11-09 12:14:28 +01001197 add_cpblt(NULL, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001198
1199 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001200
1201error:
Radek Krejcif906e412017-09-22 14:44:45 +02001202 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001203 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001204}
1205
Michal Vasko93224072021-11-09 12:14:28 +01001206API char **
1207nc_server_get_cpblts(const struct ly_ctx *ctx)
Radek Krejci24a18412018-05-16 15:09:10 +02001208{
1209 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1210}
1211
Radek Krejci695d4fa2015-10-22 13:23:54 +02001212static int
Michal Vasko77367452021-02-16 16:32:18 +01001213parse_cpblts(struct lyd_node *capabilities, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001214{
Michal Vasko77367452021-02-16 16:32:18 +01001215 struct lyd_node *iter;
1216 struct lyd_node_opaq *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001217 int ver = -1, i = 0;
1218 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001219
1220 if (list) {
1221 /* get the storage for server's capabilities */
Michal Vasko77367452021-02-16 16:32:18 +01001222 LY_LIST_FOR(lyd_child(capabilities), iter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001223 i++;
1224 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001225 /* last item remains NULL */
1226 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001227 if (!*list) {
1228 ERRMEM;
1229 return -1;
1230 }
1231 i = 0;
1232 }
1233
Michal Vasko77367452021-02-16 16:32:18 +01001234 LY_LIST_FOR(lyd_child(capabilities), iter) {
1235 cpblt = (struct lyd_node_opaq *)iter;
1236
1237 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 +02001238 ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001239 return -1;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001240 }
1241
Michal Vasko156d3272017-04-11 11:46:49 +02001242 /* skip leading/trailing whitespaces */
Michal Vasko77367452021-02-16 16:32:18 +01001243 for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {}
1244 for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {}
1245 if (!cpb_start[0] || (cpb_end == cpblt->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001246 ERR(NULL, "Empty capability \"%s\" received.", cpblt->value);
Michal Vasko156d3272017-04-11 11:46:49 +02001247 return -1;
1248 }
1249
Radek Krejci695d4fa2015-10-22 13:23:54 +02001250 /* detect NETCONF version */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001251 if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001252 ver = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001253 } 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 +02001254 ver = 1;
1255 }
1256
1257 /* store capabilities */
1258 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001259 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1260 if (!(*list)[i]) {
1261 ERRMEM;
1262 return -1;
1263 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001264 i++;
1265 }
1266 }
1267
1268 if (ver == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001269 ERR(NULL, "Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001270 }
1271
1272 return ver;
1273}
1274
1275static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001276nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001277{
Michal Vasko131120a2018-05-29 15:44:02 +02001278 NC_MSG_TYPE ret;
1279 int i, io_timeout;
Michal Vasko93224072021-11-09 12:14:28 +01001280 char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001281 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001282
Michal Vasko131120a2018-05-29 15:44:02 +02001283 if (session->side == NC_CLIENT) {
1284 /* client side hello - send only NETCONF base capabilities */
1285 cpblts = malloc(3 * sizeof *cpblts);
1286 if (!cpblts) {
1287 ERRMEM;
1288 return NC_MSG_ERROR;
1289 }
Michal Vasko93224072021-11-09 12:14:28 +01001290 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1291 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko131120a2018-05-29 15:44:02 +02001292 cpblts[2] = NULL;
1293
1294 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1295 sid = NULL;
1296 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001297 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0);
Michal Vasko5b24b6b2020-12-04 09:29:47 +01001298 if (!cpblts) {
1299 return NC_MSG_ERROR;
1300 }
Michal Vasko131120a2018-05-29 15:44:02 +02001301
1302 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1303 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001304 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001305
Michal Vasko131120a2018-05-29 15:44:02 +02001306 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001307
Michal Vasko086311b2016-01-08 09:53:11 +01001308 for (i = 0; cpblts[i]; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001309 free(cpblts[i]);
Michal Vasko086311b2016-01-08 09:53:11 +01001310 }
1311 free(cpblts);
1312
Michal Vasko131120a2018-05-29 15:44:02 +02001313 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001314}
1315
1316static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001317nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001318{
Michal Vasko77367452021-02-16 16:32:18 +01001319 struct ly_in *msg;
1320 struct lyd_node *hello = NULL, *iter;
1321 struct lyd_node_opaq *node;
1322 int r, ver = -1, flag = 0;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001323 char *str;
1324 long long int id;
Michal Vasko77367452021-02-16 16:32:18 +01001325 NC_MSG_TYPE rc = NC_MSG_HELLO;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001326
Michal Vasko77367452021-02-16 16:32:18 +01001327 r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg);
1328 switch (r) {
1329 case 1:
Radek Krejci695d4fa2015-10-22 13:23:54 +02001330 /* parse <hello> data */
Michal Vasko77367452021-02-16 16:32:18 +01001331 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 +02001332 ERR(session, "Failed to parse server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001333 rc = NC_MSG_ERROR;
1334 goto cleanup;
1335 }
1336
1337 LY_LIST_FOR(lyd_child(hello), iter) {
1338 node = (struct lyd_node_opaq *)iter;
1339
1340 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001341 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001342 } else if (!strcmp(node->name.name, "session-id")) {
1343 if (!node->value || !strlen(node->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001344 ERR(session, "No value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001345 rc = NC_MSG_ERROR;
1346 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001347 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001348 str = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001349 id = strtoll(node->value, &str, 10);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001350 if (*str || (id < 1) || (id > UINT32_MAX)) {
Michal Vasko05532772021-06-03 12:12:38 +02001351 ERR(session, "Invalid value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001352 rc = NC_MSG_ERROR;
1353 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001354 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001355 session->id = (uint32_t)id;
1356 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001357 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001358 ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001359 rc = NC_MSG_ERROR;
1360 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001361 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001362
1363 if (flag) {
1364 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001365 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001366 rc = NC_MSG_ERROR;
1367 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001368 }
1369 flag = 1;
1370
Michal Vasko77367452021-02-16 16:32:18 +01001371 if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) {
1372 rc = NC_MSG_ERROR;
1373 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001374 }
1375 session->version = ver;
1376 }
1377
1378 if (!session->id) {
Michal Vasko05532772021-06-03 12:12:38 +02001379 ERR(session, "Missing <session-id> in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001380 rc = NC_MSG_ERROR;
1381 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001382 }
1383 break;
Michal Vasko77367452021-02-16 16:32:18 +01001384 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001385 ERR(session, "Server <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001386 rc = NC_MSG_WOULDBLOCK;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001387 break;
1388 default:
Michal Vasko77367452021-02-16 16:32:18 +01001389 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001390 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001391 }
1392
Michal Vasko77367452021-02-16 16:32:18 +01001393cleanup:
1394 ly_in_free(msg, 1);
1395 lyd_free_tree(hello);
1396 return rc;
Radek Krejci5686ff72015-10-09 13:33:56 +02001397}
1398
Michal Vasko11d142a2016-01-19 15:58:24 +01001399static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001400nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001401{
Michal Vasko77367452021-02-16 16:32:18 +01001402 struct ly_in *msg;
1403 struct lyd_node *hello = NULL, *iter;
1404 struct lyd_node_opaq *node;
1405 NC_MSG_TYPE rc = NC_MSG_HELLO;
1406 int r, ver = -1, flag = 0, timeout_io;
Michal Vasko11d142a2016-01-19 15:58:24 +01001407
Michal Vasko77367452021-02-16 16:32:18 +01001408 timeout_io = server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000;
1409 r = nc_read_msg_poll_io(session, timeout_io, &msg);
1410 switch (r) {
1411 case 1:
1412 /* parse <hello> data */
1413 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 +02001414 ERR(session, "Failed to parse client <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001415 rc = NC_MSG_ERROR;
1416 goto cleanup;
1417 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001418
Michal Vasko77367452021-02-16 16:32:18 +01001419 /* learn NETCONF version */
1420 LY_LIST_FOR(lyd_child(hello), iter) {
1421 node = (struct lyd_node_opaq *)iter;
1422
1423 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001424 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001425 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001426 ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001427 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001428 goto cleanup;
1429 }
1430
1431 if (flag) {
1432 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001433 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001434 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001435 goto cleanup;
1436 }
1437 flag = 1;
1438
Michal Vasko77367452021-02-16 16:32:18 +01001439 if ((ver = parse_cpblts(&node->node, NULL)) < 0) {
1440 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001441 goto cleanup;
1442 }
1443 session->version = ver;
1444 }
1445 break;
Michal Vasko77367452021-02-16 16:32:18 +01001446 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001447 ERR(session, "Client <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001448 rc = NC_MSG_WOULDBLOCK;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001449 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001450 default:
Michal Vasko77367452021-02-16 16:32:18 +01001451 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001452 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001453 }
1454
1455cleanup:
Michal Vasko77367452021-02-16 16:32:18 +01001456 ly_in_free(msg, 1);
1457 lyd_free_tree(hello);
1458 return rc;
Michal Vasko11d142a2016-01-19 15:58:24 +01001459}
1460
Michal Vasko71090fc2016-05-24 16:37:28 +02001461NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001462nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001463{
Michal Vasko086311b2016-01-08 09:53:11 +01001464 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001465
Michal Vasko131120a2018-05-29 15:44:02 +02001466 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001467 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001468 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001469 }
1470
Michal Vasko11d142a2016-01-19 15:58:24 +01001471 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001472 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001473 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001474 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001475 }
1476
Michal Vasko71090fc2016-05-24 16:37:28 +02001477 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001478}
Michal Vasko086311b2016-01-08 09:53:11 +01001479
Michal Vasko889162e2019-09-06 14:43:37 +02001480#ifdef NC_ENABLED_SSH
1481
Michal Vasko999b64a2019-07-10 16:06:15 +02001482static void
1483nc_ssh_init(void)
1484{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001485#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001486 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1487 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001488#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001489}
1490
1491static void
1492nc_ssh_destroy(void)
1493{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001494#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001495 FIPS_mode_set(0);
1496 CONF_modules_unload(1);
1497 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001498#endif
1499
Michal Vaskoe1e82632019-09-09 09:18:03 +02001500#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1501 ssh_finalize();
1502#endif
1503}
1504
Michal Vasko889162e2019-09-06 14:43:37 +02001505#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001506
Radek Krejci53691be2016-02-22 13:58:37 +01001507#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001508
Michal Vasko770b4362017-02-17 14:44:18 +01001509#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1510
Michal Vaskof0c92c02016-01-29 09:41:45 +01001511struct CRYPTO_dynlock_value {
1512 pthread_mutex_t lock;
1513};
1514
Michal Vaskof0c92c02016-01-29 09:41:45 +01001515static struct CRYPTO_dynlock_value *
1516tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1517{
1518 struct CRYPTO_dynlock_value *value;
1519
1520 value = malloc(sizeof *value);
1521 if (!value) {
1522 ERRMEM;
1523 return NULL;
1524 }
1525 pthread_mutex_init(&value->lock, NULL);
1526
1527 return value;
1528}
1529
1530static void
1531tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1532{
1533 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1534 * I found ignored this fact, what do I know... */
1535 if (mode & CRYPTO_LOCK) {
1536 pthread_mutex_lock(&l->lock);
1537 } else {
1538 pthread_mutex_unlock(&l->lock);
1539 }
1540}
1541
1542static void
1543tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1544{
1545 pthread_mutex_destroy(&l->lock);
1546 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001547}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001548
Michal Vasko770b4362017-02-17 14:44:18 +01001549#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001550
Michal Vasko8f0c0282016-02-29 10:17:14 +01001551#endif /* NC_ENABLED_TLS */
1552
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001553#if defined (NC_ENABLED_TLS) && !defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001554
Michal Vasko770b4362017-02-17 14:44:18 +01001555#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001556static pthread_mutex_t *tls_locks;
1557
1558static void
1559tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1560{
1561 if (mode & CRYPTO_LOCK) {
1562 pthread_mutex_lock(tls_locks + n);
1563 } else {
1564 pthread_mutex_unlock(tls_locks + n);
1565 }
1566}
1567
1568static void
1569tls_thread_id_func(CRYPTO_THREADID *tid)
1570{
1571 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1572}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001573
Michal Vasko770b4362017-02-17 14:44:18 +01001574#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001575
1576static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001577nc_tls_init(void)
1578{
Rosen Penev4f552d62019-06-26 16:10:43 -07001579#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001580 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001581 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001582 SSL_library_init();
1583
Michal Vaskof89948c2018-01-04 09:19:46 +01001584 int i;
1585
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001586 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001587 if (!tls_locks) {
1588 ERRMEM;
1589 return;
1590 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001591 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1592 pthread_mutex_init(tls_locks + i, NULL);
1593 }
1594
Michal Vaskof0c92c02016-01-29 09:41:45 +01001595 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001596 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001597
1598 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1599 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1600 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001601#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001602}
1603
Michal Vasko8f0c0282016-02-29 10:17:14 +01001604static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001605nc_tls_destroy(void)
1606{
Rosen Penev4f552d62019-06-26 16:10:43 -07001607#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001608 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001609 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001610 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001611 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001612 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001613#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001614 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001615#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1616 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001617#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001618
Michal Vaskof89948c2018-01-04 09:19:46 +01001619 int i;
1620
Michal Vaskob6e37262016-02-25 14:49:00 +01001621 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001622 CRYPTO_set_locking_callback(NULL);
1623 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1624 pthread_mutex_destroy(tls_locks + i);
1625 }
1626 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001627
1628 CRYPTO_set_dynlock_create_callback(NULL);
1629 CRYPTO_set_dynlock_lock_callback(NULL);
1630 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001631#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001632}
1633
Michal Vasko8f0c0282016-02-29 10:17:14 +01001634#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001635
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001636#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001637
Michal Vasko8f0c0282016-02-29 10:17:14 +01001638static void
Michal Vasko5e228792016-02-03 15:30:24 +01001639nc_ssh_tls_init(void)
1640{
Rosen Penev4f552d62019-06-26 16:10:43 -07001641#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001642 SSL_load_error_strings();
1643 ERR_load_BIO_strings();
1644 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001645#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001646
1647 nc_ssh_init();
1648
Michal Vaskoe1e82632019-09-09 09:18:03 +02001649#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001650 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1651 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1652 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001653#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001654}
1655
Michal Vasko8f0c0282016-02-29 10:17:14 +01001656static void
Michal Vasko5e228792016-02-03 15:30:24 +01001657nc_ssh_tls_destroy(void)
1658{
Rosen Penev4f552d62019-06-26 16:10:43 -07001659#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001660 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001661# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001662 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001663# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001664 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001665# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001666#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001667
1668 nc_ssh_destroy();
1669
Michal Vaskoe1e82632019-09-09 09:18:03 +02001670#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001671 CRYPTO_set_dynlock_create_callback(NULL);
1672 CRYPTO_set_dynlock_lock_callback(NULL);
1673 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001674#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001675}
1676
Radek Krejci53691be2016-02-22 13:58:37 +01001677#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001678
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001679#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001680
1681API void
1682nc_thread_destroy(void)
1683{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001684 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001685 // CRYPTO_cleanup_all_ex_data();
Michal Vasko8f0c0282016-02-29 10:17:14 +01001686
Michal Vasko770b4362017-02-17 14:44:18 +01001687#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1688 CRYPTO_THREADID crypto_tid;
1689
Michal Vasko8f0c0282016-02-29 10:17:14 +01001690 CRYPTO_THREADID_current(&crypto_tid);
1691 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001692#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001693}
1694
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001695#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1696
1697void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001698nc_init(void)
1699{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001700#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001701 nc_ssh_tls_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001702#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001703 nc_ssh_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001704#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001705 nc_tls_init();
1706#endif
1707}
1708
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001709void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001710nc_destroy(void)
1711{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001712#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001713 nc_ssh_tls_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001714#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001715 nc_ssh_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001716#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001717 nc_tls_destroy();
1718#endif
1719}