blob: 3870235dc5ce5cf66164e9d1f19437ef1f22d46a [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
2 * \file session.c
Michal Vasko086311b2016-01-08 09:53:11 +01003 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 - general session functions
Radek Krejci206fcd62015-10-07 15:42:48 +02005 *
Michal Vaskob85767d2018-03-21 12:21:10 +01006 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
Radek Krejci206fcd62015-10-07 15:42:48 +02007 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci206fcd62015-10-07 15:42:48 +020013 */
Michal Vasko5bd4a3f2021-06-17 16:40:10 +020014#define _GNU_SOURCE
Radek Krejci206fcd62015-10-07 15:42:48 +020015
Michal Vasko18aeb5d2017-02-17 09:23:56 +010016#include <assert.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020017#include <ctype.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020018#include <errno.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020019#include <libyang/libyang.h>
Michal Vaskobe52dc22018-10-17 09:28:17 +020020#include <netinet/in.h>
21#include <netinet/tcp.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020022#include <pthread.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/socket.h>
26#include <sys/time.h>
27#include <sys/types.h>
Michal Vasko58f31552016-01-19 12:39:15 +010028#include <time.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020029#include <unistd.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020030
Michal Vasko9e8ac262020-04-07 13:06:45 +020031#include "compat.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010032#include "libnetconf.h"
Michal Vaskob83a3fa2021-05-26 09:53:42 +020033#include "session.h"
Michal Vasko11d142a2016-01-19 15:58:24 +010034#include "session_server.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010035
Radek Krejci53691be2016-02-22 13:58:37 +010036#ifdef NC_ENABLED_SSH
Radek Krejci206fcd62015-10-07 15:42:48 +020037
Michal Vasko086311b2016-01-08 09:53:11 +010038# include <libssh/libssh.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020039
Radek Krejci53691be2016-02-22 13:58:37 +010040#endif /* NC_ENABLED_SSH */
Radek Krejci695d4fa2015-10-22 13:23:54 +020041
Michal Vaskob83a3fa2021-05-26 09:53:42 +020042#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vaskoc14e3c82016-01-11 16:14:30 +010043
Michal Vasko5e228792016-02-03 15:30:24 +010044# include <openssl/conf.h>
Michal Vaskoc14e3c82016-01-11 16:14:30 +010045# include <openssl/err.h>
46
Radek Krejci53691be2016-02-22 13:58:37 +010047#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010048
Michal Vasko086311b2016-01-08 09:53:11 +010049/* in seconds */
50#define NC_CLIENT_HELLO_TIMEOUT 60
fanchanghu75888b62017-08-01 19:51:20 +080051#define NC_SERVER_HELLO_TIMEOUT 60
Radek Krejci695d4fa2015-10-22 13:23:54 +020052
Michal Vasko05ba9df2016-01-13 14:40:27 +010053/* in milliseconds */
54#define NC_CLOSE_REPLY_TIMEOUT 200
55
Michal Vasko086311b2016-01-08 09:53:11 +010056extern struct nc_server_opts server_opts;
57
Radek Krejci7ac16052016-07-15 11:48:18 +020058int
Michal Vasko77a6abe2017-10-05 10:02:20 +020059nc_gettimespec_mono(struct timespec *ts)
60{
61#ifdef CLOCK_MONOTONIC_RAW
62 return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
Michal Vaskob83a3fa2021-05-26 09:53:42 +020063#elif defined (CLOCK_MONOTONIC)
Michal Vasko77a6abe2017-10-05 10:02:20 +020064 return clock_gettime(CLOCK_MONOTONIC, ts);
65#else
66 /* no monotonic clock available, return realtime */
67 return nc_gettimespec_real(ts);
68#endif
69}
70
71int
72nc_gettimespec_real(struct timespec *ts)
Radek Krejci7ac16052016-07-15 11:48:18 +020073{
Michal Vasko0ef46df2016-09-21 14:03:18 +020074#ifdef CLOCK_REALTIME
Radek Krejci7ac16052016-07-15 11:48:18 +020075 return clock_gettime(CLOCK_REALTIME, ts);
76#else
77 int rc;
78 struct timeval tv;
79
80 rc = gettimeofday(&tv, NULL);
81 if (!rc) {
82 ts->tv_sec = (time_t)tv.tv_sec;
83 ts->tv_nsec = 1000L * (long)tv.tv_usec;
84 }
85 return rc;
86#endif
87}
88
Michal Vasko36c7be82017-02-22 13:37:59 +010089/* ts1 < ts2 -> +, ts1 > ts2 -> -, returns milliseconds */
90int32_t
Radek Krejcida0afb22017-05-26 16:25:59 +020091nc_difftimespec(const struct timespec *ts1, const struct timespec *ts2)
Michal Vasko99f251b2017-01-11 11:31:46 +010092{
Michal Vasko36c7be82017-02-22 13:37:59 +010093 int64_t nsec_diff = 0;
Michal Vasko99f251b2017-01-11 11:31:46 +010094
Michal Vaskoa82bd202017-03-03 14:07:29 +010095 nsec_diff += (((int64_t)ts2->tv_sec) - ((int64_t)ts1->tv_sec)) * 1000000000L;
96 nsec_diff += ((int64_t)ts2->tv_nsec) - ((int64_t)ts1->tv_nsec);
Michal Vasko99f251b2017-01-11 11:31:46 +010097
Michal Vaskob83a3fa2021-05-26 09:53:42 +020098 return nsec_diff ? nsec_diff / 1000000L : 0;
Michal Vasko99f251b2017-01-11 11:31:46 +010099}
100
Michal Vasko36c7be82017-02-22 13:37:59 +0100101void
102nc_addtimespec(struct timespec *ts, uint32_t msec)
103{
Michal Vasko81c5b302017-03-15 12:10:40 +0100104 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
105
Michal Vasko0dfcd332017-03-03 13:14:39 +0100106 ts->tv_sec += msec / 1000;
107 ts->tv_nsec += (msec % 1000) * 1000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100108
Michal Vasko81c5b302017-03-15 12:10:40 +0100109 if (ts->tv_nsec >= 1000000000L) {
110 ++ts->tv_sec;
111 ts->tv_nsec -= 1000000000L;
112 } else if (ts->tv_nsec < 0) {
113 --ts->tv_sec;
114 ts->tv_nsec += 1000000000L;
Michal Vasko36c7be82017-02-22 13:37:59 +0100115 }
Michal Vasko81c5b302017-03-15 12:10:40 +0100116
117 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
Michal Vasko36c7be82017-02-22 13:37:59 +0100118}
119
Michal Vaskoddce1212019-05-24 09:58:49 +0200120const char *
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200121nc_keytype2str(NC_SSH_KEY_TYPE type)
Michal Vaskoddce1212019-05-24 09:58:49 +0200122{
123 switch (type) {
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200124 case NC_SSH_KEY_DSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200125 return "DSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200126 case NC_SSH_KEY_RSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200127 return "RSA";
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200128 case NC_SSH_KEY_ECDSA:
Michal Vaskoddce1212019-05-24 09:58:49 +0200129 return "EC";
130 default:
131 break;
132 }
133
134 return NULL;
135}
136
Michal Vaskobe52dc22018-10-17 09:28:17 +0200137int
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200138nc_sock_enable_keepalive(int sock, struct nc_keepalives *ka)
Michal Vaskobe52dc22018-10-17 09:28:17 +0200139{
140 int opt;
141
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200142 opt = ka->enabled;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200143 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200144 ERR(NULL, "Could not set SO_KEEPALIVE option (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200145 return -1;
146 }
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200147 if (!ka->enabled) {
148 return 0;
149 }
Michal Vaskobe52dc22018-10-17 09:28:17 +0200150
151#ifdef TCP_KEEPIDLE
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200152 opt = ka->idle_time;
Michal Vaskobe52dc22018-10-17 09:28:17 +0200153 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200154 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200155 return -1;
156 }
157#endif
158
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200159#ifdef TCP_KEEPCNT
160 opt = ka->max_probes;
161 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200162 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200163 return -1;
164 }
165#endif
166
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200167#ifdef TCP_KEEPINTVL
168 opt = ka->probe_interval;
169 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof opt) == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200170 ERR(NULL, "Setsockopt failed (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200171 return -1;
172 }
173#endif
174
175 return 0;
176}
177
Michal Vaskoade892d2017-02-22 13:40:35 +0100178struct nc_session *
Michal Vasko131120a2018-05-29 15:44:02 +0200179nc_new_session(NC_SIDE side, int shared_ti)
Michal Vaskoade892d2017-02-22 13:40:35 +0100180{
181 struct nc_session *sess;
182
183 sess = calloc(1, sizeof *sess);
184 if (!sess) {
185 return NULL;
186 }
187
Michal Vasko131120a2018-05-29 15:44:02 +0200188 sess->side = side;
189
190 if (side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100191 pthread_mutex_init(&sess->opts.server.rpc_lock, NULL);
192 pthread_cond_init(&sess->opts.server.rpc_cond, NULL);
193 sess->opts.server.rpc_inuse = 0;
194
195 pthread_mutex_init(&sess->opts.server.ch_lock, NULL);
196 pthread_cond_init(&sess->opts.server.ch_cond, NULL);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200197 } else {
198 pthread_mutex_init(&sess->opts.client.msgs_lock, NULL);
Michal Vasko131120a2018-05-29 15:44:02 +0200199 }
200
201 if (!shared_ti) {
202 sess->io_lock = malloc(sizeof *sess->io_lock);
203 if (!sess->io_lock) {
204 goto error;
205 }
206 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100207 }
208
209 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200210
211error:
Michal Vasko131120a2018-05-29 15:44:02 +0200212 free(sess);
213 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100214}
215
Michal Vasko96164bf2016-01-21 15:41:58 +0100216/*
217 * @return 1 - success
218 * 0 - timeout
219 * -1 - error
220 */
221int
Michal Vasko131120a2018-05-29 15:44:02 +0200222nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100223{
224 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100225 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100226
Michal Vasko131120a2018-05-29 15:44:02 +0200227 if (session->side != NC_SERVER) {
228 ERRINT;
229 return -1;
230 }
231
Michal Vasko96164bf2016-01-21 15:41:58 +0100232 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200233 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100234 nc_addtimespec(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100235
Michal Vaskoade892d2017-02-22 13:40:35 +0100236 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100237 ret = pthread_mutex_timedlock(&session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100238 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100239 while (session->opts.server.rpc_inuse) {
240 ret = pthread_cond_timedwait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100241 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100242 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100243 break;
244 }
245 }
246 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100247 } else if (!timeout) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100248 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100249 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100250 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100251 if (session->opts.server.rpc_inuse) {
252 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100253 return 0;
254 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200255 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100256 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100257 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100258 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100259 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100260 while (session->opts.server.rpc_inuse) {
261 ret = pthread_cond_wait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100262 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100263 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100264 break;
265 }
266 }
267 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100268 }
269
Michal Vaskoade892d2017-02-22 13:40:35 +0100270 if (ret) {
271 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
272 /* timeout */
273 return 0;
274 }
275
Michal Vasko96164bf2016-01-21 15:41:58 +0100276 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200277 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100278 return -1;
279 }
280
281 /* ok */
Michal Vaskoacf98472021-02-04 15:33:57 +0100282 assert(session->opts.server.rpc_inuse == 0);
283 session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100284
285 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100286 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100287 if (ret) {
288 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200289 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100290 return -1;
291 }
292
293 return 1;
294}
295
296int
Michal Vasko131120a2018-05-29 15:44:02 +0200297nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100298{
299 int ret;
300 struct timespec ts_timeout;
301
Michal Vasko131120a2018-05-29 15:44:02 +0200302 if (session->side != NC_SERVER) {
303 ERRINT;
304 return -1;
305 }
306
Michal Vaskoacf98472021-02-04 15:33:57 +0100307 assert(session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100308
309 if (timeout > 0) {
Michal Vasko77a6abe2017-10-05 10:02:20 +0200310 nc_gettimespec_real(&ts_timeout);
Michal Vasko81c5b302017-03-15 12:10:40 +0100311 nc_addtimespec(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100312
313 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100314 ret = pthread_mutex_timedlock(&session->opts.server.rpc_lock, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100315 } else if (!timeout) {
316 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100317 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100318 } else { /* timeout == -1 */
319 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100320 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100321 }
322
323 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
324 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200325 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100326 return -1;
327 } else if (ret) {
Michal Vasko05532772021-06-03 12:12:38 +0200328 WRN(session, "%s: session RPC lock timeout, should not happen.");
Michal Vaskoade892d2017-02-22 13:40:35 +0100329 }
330
Michal Vaskoacf98472021-02-04 15:33:57 +0100331 session->opts.server.rpc_inuse = 0;
332 pthread_cond_signal(&session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100333
334 if (!ret) {
335 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100336 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100337 if (ret) {
338 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200339 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100340 return -1;
341 }
342 }
343
Michal Vasko96164bf2016-01-21 15:41:58 +0100344 return 1;
345}
346
Michal Vasko131120a2018-05-29 15:44:02 +0200347int
348nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
349{
350 int ret;
351 struct timespec ts_timeout;
352
353 if (timeout > 0) {
354 nc_gettimespec_real(&ts_timeout);
355 nc_addtimespec(&ts_timeout, timeout);
356
357 ret = pthread_mutex_timedlock(session->io_lock, &ts_timeout);
358 } else if (!timeout) {
359 ret = pthread_mutex_trylock(session->io_lock);
360 } else { /* timeout == -1 */
Robin Jarry54ea2962018-10-10 10:33:40 +0200361 ret = pthread_mutex_lock(session->io_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200362 }
363
364 if (ret) {
365 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
366 /* timeout */
367 return 0;
368 }
369
370 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200371 ERR(session, "%s: failed to IO lock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200372 return -1;
373 }
374
375 return 1;
376}
377
378int
379nc_session_io_unlock(struct nc_session *session, const char *func)
380{
381 int ret;
382
383 ret = pthread_mutex_unlock(session->io_lock);
384 if (ret) {
385 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200386 ERR(session, "%s: failed to IO unlock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200387 return -1;
388 }
389
390 return 1;
391}
392
Michal Vasko01130bd2021-08-26 11:47:38 +0200393int
394nc_session_client_msgs_lock(struct nc_session *session, int *timeout, const char *func)
395{
396 int ret;
397 int32_t diff_msec;
398 struct timespec ts_timeout, ts_start, ts_end;
399
400 assert(session->side == NC_CLIENT);
401
402 if (*timeout > 0) {
403 /* get current time */
404 nc_gettimespec_real(&ts_start);
405
406 nc_gettimespec_real(&ts_timeout);
407 nc_addtimespec(&ts_timeout, *timeout);
408
409 ret = pthread_mutex_timedlock(&session->opts.client.msgs_lock, &ts_timeout);
410 if (!ret) {
411 /* update timeout based on what was elapsed */
412 nc_gettimespec_real(&ts_end);
413 diff_msec = nc_difftimespec(&ts_start, &ts_end);
414 *timeout -= diff_msec;
415 }
416 } else if (!*timeout) {
417 ret = pthread_mutex_trylock(&session->opts.client.msgs_lock);
418 } else { /* timeout == -1 */
419 ret = pthread_mutex_lock(&session->opts.client.msgs_lock);
420 }
421
422 if (ret) {
423 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
424 /* timeout */
425 return 0;
426 }
427
428 /* error */
429 ERR(session, "%s: failed to MSGS lock a session (%s).", func, strerror(ret));
430 return -1;
431 }
432
433 return 1;
434}
435
436int
437nc_session_client_msgs_unlock(struct nc_session *session, const char *func)
438{
439 int ret;
440
441 assert(session->side == NC_CLIENT);
442
443 ret = pthread_mutex_unlock(&session->opts.client.msgs_lock);
444 if (ret) {
445 /* error */
446 ERR(session, "%s: failed to MSGS unlock a session (%s).", func, strerror(ret));
447 return -1;
448 }
449
450 return 1;
451}
452
Michal Vasko8dadf782016-01-15 10:29:36 +0100453API NC_STATUS
454nc_session_get_status(const struct nc_session *session)
455{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100456 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200457 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200458 return NC_STATUS_ERR;
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100459 }
460
Michal Vasko8dadf782016-01-15 10:29:36 +0100461 return session->status;
462}
463
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100464API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200465nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100466{
467 if (!session) {
468 ERRARG("session");
Radek Krejci465308c2017-05-22 14:49:10 +0200469 return NC_SESSION_TERM_ERR;
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100470 }
471
472 return session->term_reason;
473}
474
Michal Vasko8dadf782016-01-15 10:29:36 +0100475API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200476nc_session_get_killed_by(const struct nc_session *session)
477{
478 if (!session) {
479 ERRARG("session");
480 return 0;
481 }
482
483 return session->killed_by;
484}
485
486API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100487nc_session_get_id(const struct nc_session *session)
488{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100489 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200490 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100491 return 0;
492 }
493
Michal Vasko8dadf782016-01-15 10:29:36 +0100494 return session->id;
495}
496
Michal Vasko174fe8e2016-02-17 15:38:09 +0100497API int
498nc_session_get_version(const struct nc_session *session)
499{
500 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200501 ERRARG("session");
Michal Vasko174fe8e2016-02-17 15:38:09 +0100502 return -1;
503 }
504
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200505 return session->version == NC_VERSION_10 ? 0 : 1;
Michal Vasko174fe8e2016-02-17 15:38:09 +0100506}
507
Michal Vasko8dadf782016-01-15 10:29:36 +0100508API NC_TRANSPORT_IMPL
509nc_session_get_ti(const struct nc_session *session)
510{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100511 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200512 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100513 return 0;
514 }
515
Michal Vasko8dadf782016-01-15 10:29:36 +0100516 return session->ti_type;
517}
518
519API const char *
520nc_session_get_username(const struct nc_session *session)
521{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100522 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200523 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100524 return NULL;
525 }
526
Michal Vasko8dadf782016-01-15 10:29:36 +0100527 return session->username;
528}
529
530API const char *
531nc_session_get_host(const struct nc_session *session)
532{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100533 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200534 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100535 return NULL;
536 }
537
Michal Vasko8dadf782016-01-15 10:29:36 +0100538 return session->host;
539}
540
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200541API const char *
542nc_session_get_path(const struct nc_session *session)
543{
544 if (!session) {
545 ERRARG("session");
546 return NULL;
547 }
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200548 if (session->ti_type != NC_TI_UNIX) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200549 return NULL;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200550 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200551
552 return session->path;
553}
554
Michal Vasko8dadf782016-01-15 10:29:36 +0100555API uint16_t
556nc_session_get_port(const struct nc_session *session)
557{
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100558 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200559 ERRARG("session");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100560 return 0;
561 }
562
Michal Vasko8dadf782016-01-15 10:29:36 +0100563 return session->port;
564}
565
Michal Vasko9a25e932016-02-01 10:36:42 +0100566API struct ly_ctx *
567nc_session_get_ctx(const struct nc_session *session)
568{
569 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200570 ERRARG("session");
Michal Vasko9a25e932016-02-01 10:36:42 +0100571 return NULL;
572 }
573
574 return session->ctx;
575}
576
Michal Vasko2cc4c682016-03-01 09:16:48 +0100577API void
578nc_session_set_data(struct nc_session *session, void *data)
579{
580 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200581 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100582 return;
583 }
584
585 session->data = data;
586}
587
588API void *
589nc_session_get_data(const struct nc_session *session)
590{
591 if (!session) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200592 ERRARG("session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100593 return NULL;
594 }
595
596 return session->data;
597}
598
Michal Vasko086311b2016-01-08 09:53:11 +0100599NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200600nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200601{
Michal Vasko086311b2016-01-08 09:53:11 +0100602 if (session->ctx != op->schema->module->ctx) {
Michal Vasko05532772021-06-03 12:12:38 +0200603 ERR(session, "RPC \"%s\" was created in different context than that of the session.", op->schema->name);
Michal Vasko086311b2016-01-08 09:53:11 +0100604 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100605 }
606
Michal Vasko131120a2018-05-29 15:44:02 +0200607 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100608}
609
Radek Krejci695d4fa2015-10-22 13:23:54 +0200610API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100611nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200612{
Michal Vasko01130bd2021-08-26 11:47:38 +0200613 int r, i, rpc_locked = 0, msgs_locked = 0, sock = -1, timeout;
Michal Vasko428087d2016-01-14 16:04:28 +0100614 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskob48aa812016-01-18 14:13:09 +0100615 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vasko4589bbe2016-01-29 09:41:30 +0100616 struct nc_session *siter;
Michal Vaskoad611702015-12-03 13:41:51 +0100617 struct nc_msg_cont *contiter;
Michal Vasko77367452021-02-16 16:32:18 +0100618 struct ly_in *msg;
619 struct lyd_node *close_rpc, *envp;
Michal Vaskoad611702015-12-03 13:41:51 +0100620 const struct lys_module *ietfnc;
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200621 struct timespec ts, ts_cur;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200622 void *p;
623
Michal Vasko428087d2016-01-14 16:04:28 +0100624 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200625 return;
626 }
627
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200628 /* stop notifications thread if any */
629 if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
630 /* let the thread know it should quit */
631 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 2);
632
633 /* wait for it */
634 nc_gettimespec_mono(&ts);
635 nc_addtimespec(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
636 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) {
637 usleep(NC_TIMEOUT_STEP);
638 nc_gettimespec_mono(&ts_cur);
639 if (nc_difftimespec(&ts_cur, &ts) < 1) {
640 ERR(session, "Waiting for notification thread exit failed (timed out).");
641 break;
642 }
643 }
Michal Vasko86d357c2016-03-11 13:46:38 +0100644 }
645
Michal Vaskoacf98472021-02-04 15:33:57 +0100646 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200647 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100648 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100649 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200650 } else if (r) {
651 rpc_locked = 1;
Michal Vasko96a28a32021-02-04 15:35:20 +0100652 } else {
653 /* else failed to lock it, too bad */
Michal Vasko05532772021-06-03 12:12:38 +0200654 ERR(session, "Freeing a session while an RPC is being processed.");
Michal Vasko96a28a32021-02-04 15:35:20 +0100655 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200656 }
657
Michal Vasko8c247822020-09-07 13:23:23 +0200658 if (session->side == NC_CLIENT) {
Michal Vasko01130bd2021-08-26 11:47:38 +0200659 timeout = NC_SESSION_FREE_LOCK_TIMEOUT;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200660
Michal Vasko01130bd2021-08-26 11:47:38 +0200661 /* MSGS LOCK */
662 r = nc_session_client_msgs_lock(session, &timeout, __func__);
663 if (r == -1) {
664 return;
665 } else if (r) {
666 msgs_locked = 1;
667 } else {
668 /* else failed to lock it, too bad */
669 ERR(session, "Freeing a session while messages are being received.");
670 }
671
672 /* cleanup message queue */
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200673 for (contiter = session->opts.client.msgs; contiter; ) {
Michal Vasko77367452021-02-16 16:32:18 +0100674 ly_in_free(contiter->msg, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200675
Michal Vaskoad611702015-12-03 13:41:51 +0100676 p = contiter;
677 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200678 free(p);
679 }
680
Michal Vasko01130bd2021-08-26 11:47:38 +0200681 if (msgs_locked) {
682 /* MSGS UNLOCK */
683 nc_session_client_msgs_unlock(session, __func__);
684 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200685
Michal Vasko8c247822020-09-07 13:23:23 +0200686 if (session->status == NC_STATUS_RUNNING) {
Michal Vasko9c6d38c2021-09-03 13:02:53 +0200687 /* receive any leftover messages */
688 while (nc_read_msg_poll_io(session, 0, &msg) == 1) {
689 ly_in_free(msg, 1);
690 }
691
Michal Vasko8c247822020-09-07 13:23:23 +0200692 /* send closing info to the other side */
Michal Vasko77367452021-02-16 16:32:18 +0100693 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
Michal Vasko8c247822020-09-07 13:23:23 +0200694 if (!ietfnc) {
Michal Vasko05532772021-06-03 12:12:38 +0200695 WRN(session, "Missing ietf-netconf schema in context, unable to send <close-session>.");
Michal Vaskod9c38992021-05-28 13:32:15 +0200696 } else if (!lyd_new_inner(NULL, ietfnc, "close-session", 0, &close_rpc)) {
Michal Vasko8c247822020-09-07 13:23:23 +0200697 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
Michal Vasko77367452021-02-16 16:32:18 +0100698 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &msg)) {
699 case 1:
700 if (lyd_parse_op(session->ctx, close_rpc, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, &envp, NULL)) {
Michal Vasko05532772021-06-03 12:12:38 +0200701 WRN(session, "Failed to parse <close-session> reply.");
Michal Vasko77367452021-02-16 16:32:18 +0100702 } else if (!lyd_child(envp) || strcmp(LYD_NAME(lyd_child(envp)), "ok")) {
Michal Vasko05532772021-06-03 12:12:38 +0200703 WRN(session, "Reply to <close-session> was not <ok> as expected.");
Michal Vasko8c247822020-09-07 13:23:23 +0200704 }
Michal Vasko77367452021-02-16 16:32:18 +0100705 lyd_free_tree(envp);
706 ly_in_free(msg, 1);
Michal Vasko8c247822020-09-07 13:23:23 +0200707 break;
Michal Vasko77367452021-02-16 16:32:18 +0100708 case 0:
Michal Vasko05532772021-06-03 12:12:38 +0200709 WRN(session, "Timeout for receiving a reply to <close-session> elapsed.");
Michal Vasko8c247822020-09-07 13:23:23 +0200710 break;
Michal Vasko77367452021-02-16 16:32:18 +0100711 case -1:
Michal Vasko05532772021-06-03 12:12:38 +0200712 ERR(session, "Failed to receive a reply to <close-session>.");
Michal Vasko8c247822020-09-07 13:23:23 +0200713 break;
714 default:
715 /* cannot happen */
716 break;
Michal Vaskofad6e912015-10-26 15:37:22 +0100717 }
Michal Vasko77367452021-02-16 16:32:18 +0100718 lyd_free_tree(close_rpc);
Michal Vaskofad6e912015-10-26 15:37:22 +0100719 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200720 }
721
722 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200723 if (session->opts.client.cpblts) {
724 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200725 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200726 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200727 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200728 }
729 }
730
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100731 if (session->data && data_free) {
732 data_free(session->data);
733 }
734
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200735 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
736 /* CH LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100737 pthread_mutex_lock(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200738 }
739
Michal Vasko86d357c2016-03-11 13:46:38 +0100740 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200741 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200742
743 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100744 pthread_cond_signal(&session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200745
Michal Vasko0db3db52021-03-03 10:45:42 +0100746 nc_gettimespec_real(&ts);
747 nc_addtimespec(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
748
749 /* wait for CH thread to actually wake up and terminate */
750 r = 0;
751 while (!r && (session->flags & NC_SESSION_CALLHOME)) {
752 r = pthread_cond_timedwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, &ts);
753 }
754
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200755 /* CH UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100756 pthread_mutex_unlock(&session->opts.server.ch_lock);
Michal Vasko3f05a092018-03-13 10:39:49 +0100757
Michal Vasko0db3db52021-03-03 10:45:42 +0100758 if (r) {
Michal Vasko05532772021-06-03 12:12:38 +0200759 ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r));
Michal Vasko3f05a092018-03-13 10:39:49 +0100760 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200761 }
762
Michal Vasko428087d2016-01-14 16:04:28 +0100763 connected = nc_session_is_connected(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200764
765 /* transport implementation cleanup */
766 switch (session->ti_type) {
767 case NC_TI_FD:
768 /* nothing needed - file descriptors were provided by caller,
769 * so it is up to the caller to close them correctly
770 * TODO use callbacks
771 */
Michal Vasko3512e402016-01-28 16:22:34 +0100772 /* just to avoid compiler warning */
773 (void)connected;
Michal Vasko4589bbe2016-01-29 09:41:30 +0100774 (void)siter;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200775 break;
776
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200777 case NC_TI_UNIX:
778 sock = session->ti.unixsock.sock;
779 (void)connected;
780 (void)siter;
781 break;
782
Radek Krejci53691be2016-02-22 13:58:37 +0100783#ifdef NC_ENABLED_SSH
Radek Krejci695d4fa2015-10-22 13:23:54 +0200784 case NC_TI_LIBSSH:
Michal Vasko428087d2016-01-14 16:04:28 +0100785 if (connected) {
786 ssh_channel_free(session->ti.libssh.channel);
787 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200788 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
789 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200790 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
Radek Krejci695d4fa2015-10-22 13:23:54 +0200791 */
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200792 /* SESSION IO LOCK */
793 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
794
Michal Vasko96164bf2016-01-21 15:41:58 +0100795 multisession = 0;
796 if (session->ti.libssh.next) {
797 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
798 if (siter->status != NC_STATUS_STARTING) {
799 multisession = 1;
800 break;
801 }
802 }
803 }
804
805 if (!multisession) {
806 /* it's not multisession yet, but we still need to free the starting sessions */
807 if (session->ti.libssh.next) {
808 do {
809 siter = session->ti.libssh.next;
810 session->ti.libssh.next = siter->ti.libssh.next;
811
812 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
Michal Vasko96164bf2016-01-21 15:41:58 +0100813 lydict_remove(session->ctx, session->username);
814 lydict_remove(session->ctx, session->host);
Michal Vasko96164bf2016-01-21 15:41:58 +0100815 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko1dcb7642021-04-14 15:28:23 +0200816 ly_ctx_destroy(session->ctx);
Michal Vasko96164bf2016-01-21 15:41:58 +0100817 }
818
819 free(siter);
820 } while (session->ti.libssh.next != session);
821 }
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100822 /* remember sock so we can close it */
823 sock = ssh_get_fd(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100824 if (connected) {
825 ssh_disconnect(session->ti.libssh.session);
Michal Vasko06e0fc22019-05-09 09:32:27 +0200826 sock = -1;
Michal Vasko428087d2016-01-14 16:04:28 +0100827 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200828 ssh_free(session->ti.libssh.session);
829 } else {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200830 /* remove the session from the list */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200831 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {}
Michal Vaskoaec4f212015-10-26 15:37:45 +0100832 if (session->ti.libssh.next == siter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200833 /* there will be only one session */
834 siter->ti.libssh.next = NULL;
835 } else {
836 /* there are still multiple sessions, keep the ring list */
837 siter->ti.libssh.next = session->ti.libssh.next;
838 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100839 /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */
840 if (session->flags & NC_SESSION_SSH_MSG_CB) {
Michal Vasko5e0edd82020-07-29 15:26:13 +0200841 siter = session->ti.libssh.next;
842 while (siter && siter->status != NC_STATUS_RUNNING) {
Michal Vasko96164bf2016-01-21 15:41:58 +0100843 if (siter->ti.libssh.next == session) {
844 ERRINT;
845 break;
846 }
Michal Vasko5e0edd82020-07-29 15:26:13 +0200847 siter = siter->ti.libssh.next;
Michal Vasko96164bf2016-01-21 15:41:58 +0100848 }
Michal Vasko5e0edd82020-07-29 15:26:13 +0200849 /* siter may be NULL in case all the sessions terminated at the same time (socket was disconnected),
850 * we set session to NULL because we do not expect any new message to arrive */
Michal Vasko96164bf2016-01-21 15:41:58 +0100851 ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter);
Michal Vasko5e0edd82020-07-29 15:26:13 +0200852 if (siter) {
853 siter->flags |= NC_SESSION_SSH_MSG_CB;
854 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100855 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200856 }
Michal Vaskoe50b6b12018-10-11 10:27:50 +0200857
858 /* SESSION IO UNLOCK */
859 if (r == 1) {
860 nc_session_io_unlock(session, __func__);
861 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200862 break;
863#endif
864
Radek Krejci53691be2016-02-22 13:58:37 +0100865#ifdef NC_ENABLED_TLS
Radek Krejci695d4fa2015-10-22 13:23:54 +0200866 case NC_TI_OPENSSL:
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100867 /* remember sock so we can close it */
868 sock = SSL_get_fd(session->ti.tls);
869
Michal Vasko428087d2016-01-14 16:04:28 +0100870 if (connected) {
871 SSL_shutdown(session->ti.tls);
872 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200873 SSL_free(session->ti.tls);
Michal Vasko06e22432016-01-15 10:30:06 +0100874
Michal Vasko2e6defd2016-10-07 15:48:15 +0200875 if (session->side == NC_SERVER) {
876 X509_free(session->opts.server.client_cert);
877 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200878 break;
879#endif
Michal Vasko428087d2016-01-14 16:04:28 +0100880 case NC_TI_NONE:
Michal Vasko428087d2016-01-14 16:04:28 +0100881 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200882 }
Michal Vasko428087d2016-01-14 16:04:28 +0100883
Michal Vasko4d57e1b2018-03-21 12:21:25 +0100884 /* close socket separately */
885 if (sock > -1) {
886 close(sock);
887 }
888
Radek Krejciac6d3472015-10-22 15:47:18 +0200889 lydict_remove(session->ctx, session->username);
890 lydict_remove(session->ctx, session->host);
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200891 lydict_remove(session->ctx, session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200892
893 /* final cleanup */
Michal Vaskoacf98472021-02-04 15:33:57 +0100894 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200895 if (rpc_locked) {
896 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100897 }
Michal Vaskoacf98472021-02-04 15:33:57 +0100898 pthread_mutex_destroy(&session->opts.server.rpc_lock);
899 pthread_cond_destroy(&session->opts.server.rpc_cond);
Michal Vasko131120a2018-05-29 15:44:02 +0200900 }
901
902 if (session->io_lock && !multisession) {
903 pthread_mutex_destroy(session->io_lock);
904 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200905 }
906
907 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko1dcb7642021-04-14 15:28:23 +0200908 ly_ctx_destroy(session->ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200909 }
910
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200911 if (session->side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100912 /* free CH synchronization structures */
913 pthread_cond_destroy(&session->opts.server.ch_cond);
914 pthread_mutex_destroy(&session->opts.server.ch_lock);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200915 } else {
916 pthread_mutex_destroy(&session->opts.client.msgs_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200917 }
918
Radek Krejci695d4fa2015-10-22 13:23:54 +0200919 free(session);
920}
921
Michal Vasko086311b2016-01-08 09:53:11 +0100922static void
923add_cpblt(struct ly_ctx *ctx, const char *capab, const char ***cpblts, int *size, int *count)
924{
Radek Krejci658782b2016-12-04 22:04:55 +0100925 size_t len;
926 int i;
927 char *p;
928
929 if (capab) {
930 /* check if already present */
931 p = strchr(capab, '?');
932 if (p) {
933 len = p - capab;
934 } else {
935 len = strlen(capab);
936 }
937 for (i = 0; i < *count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200938 if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) {
Radek Krejci658782b2016-12-04 22:04:55 +0100939 /* already present, do not duplicate it */
940 return;
941 }
942 }
943 }
944
945 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +0100946 if (*count == *size) {
947 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100948 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
949 if (!(*cpblts)) {
950 ERRMEM;
951 return;
952 }
Michal Vasko086311b2016-01-08 09:53:11 +0100953 }
954
955 if (capab) {
Michal Vasko77367452021-02-16 16:32:18 +0100956 lydict_insert(ctx, capab, 0, &(*cpblts)[*count]);
Michal Vasko086311b2016-01-08 09:53:11 +0100957 } else {
958 (*cpblts)[*count] = NULL;
959 }
960 ++(*count);
961}
962
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200963API const char **
Radek Krejci24a18412018-05-16 15:09:10 +0200964nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +0100965{
Michal Vaskod5ada122020-03-19 18:28:06 +0100966 const char **cpblts;
Michal Vasko77367452021-02-16 16:32:18 +0100967 const struct lys_module *mod;
968 struct lysp_feature *feat;
969 int size = 10, count, features_count = 0, dev_count = 0, str_len, len;
Michal Vasko1440a742021-03-31 11:11:03 +0200970 uint32_t i, u;
Michal Vasko77367452021-02-16 16:32:18 +0100971 LY_ARRAY_COUNT_TYPE v;
Michal Vasko1440a742021-03-31 11:11:03 +0200972 char *yl_content_id;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200973
Radek Krejci24a18412018-05-16 15:09:10 +0200974#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +0200975 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +0100976
Michal Vasko4ffa3b22016-05-24 16:36:25 +0200977 if (!ctx) {
978 ERRARG("ctx");
979 return NULL;
980 }
981
Michal Vasko086311b2016-01-08 09:53:11 +0100982 cpblts = malloc(size * sizeof *cpblts);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100983 if (!cpblts) {
984 ERRMEM;
Radek Krejcif906e412017-09-22 14:44:45 +0200985 goto error;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100986 }
Michal Vasko77367452021-02-16 16:32:18 +0100987 lydict_insert(ctx, "urn:ietf:params:netconf:base:1.0", 0, &cpblts[0]);
988 lydict_insert(ctx, "urn:ietf:params:netconf:base:1.1", 0, &cpblts[1]);
Michal Vasko086311b2016-01-08 09:53:11 +0100989 count = 2;
990
991 /* capabilities */
992
Michal Vasko77367452021-02-16 16:32:18 +0100993 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf");
Michal Vasko086311b2016-01-08 09:53:11 +0100994 if (mod) {
Michal Vasko77367452021-02-16 16:32:18 +0100995 if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100996 add_cpblt(ctx, "urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +0100997 }
Michal Vasko77367452021-02-16 16:32:18 +0100998 if (lys_feature_value(mod, "candidate") == LY_SUCCESS) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100999 add_cpblt(ctx, "urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko77367452021-02-16 16:32:18 +01001000 if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001001 add_cpblt(ctx, "urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001002 }
1003 }
Michal Vasko77367452021-02-16 16:32:18 +01001004 if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001005 add_cpblt(ctx, "urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001006 }
Michal Vasko77367452021-02-16 16:32:18 +01001007 if (lys_feature_value(mod, "validate") == LY_SUCCESS) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001008 add_cpblt(ctx, "urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001009 }
Michal Vasko77367452021-02-16 16:32:18 +01001010 if (lys_feature_value(mod, "startup") == LY_SUCCESS) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001011 add_cpblt(ctx, "urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001012 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001013
1014 /* The URL capability must be set manually using nc_server_set_capability()
1015 * because of the need for supported protocols to be included.
1016 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1017 */
Michal Vasko77367452021-02-16 16:32:18 +01001018 // if (lys_feature_value(mod, "url") == LY_SUCCESS) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001019 // add_cpblt(ctx, "urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
mekleoa8de5e92020-02-13 09:05:56 +01001020 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001021
Michal Vasko77367452021-02-16 16:32:18 +01001022 if (lys_feature_value(mod, "xpath") == LY_SUCCESS) {
Michal Vasko3031aae2016-01-27 16:07:18 +01001023 add_cpblt(ctx, "urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001024 }
1025 }
1026
Michal Vasko77367452021-02-16 16:32:18 +01001027 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01001028 if (mod) {
1029 if (!server_opts.wd_basic_mode) {
Michal Vasko05532772021-06-03 12:12:38 +02001030 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 +01001031 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001032 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
Michal Vasko086311b2016-01-08 09:53:11 +01001033 switch (server_opts.wd_basic_mode) {
1034 case NC_WD_ALL:
1035 strcat(str, "?basic-mode=report-all");
1036 break;
1037 case NC_WD_TRIM:
1038 strcat(str, "?basic-mode=trim");
1039 break;
1040 case NC_WD_EXPLICIT:
1041 strcat(str, "?basic-mode=explicit");
1042 break;
1043 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001044 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001045 break;
1046 }
1047
1048 if (server_opts.wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001049 strcat(str, "&also-supported=");
Michal Vasko086311b2016-01-08 09:53:11 +01001050 if (server_opts.wd_also_supported & NC_WD_ALL) {
1051 strcat(str, "report-all,");
1052 }
1053 if (server_opts.wd_also_supported & NC_WD_ALL_TAG) {
1054 strcat(str, "report-all-tagged,");
1055 }
1056 if (server_opts.wd_also_supported & NC_WD_TRIM) {
1057 strcat(str, "trim,");
1058 }
1059 if (server_opts.wd_also_supported & NC_WD_EXPLICIT) {
1060 strcat(str, "explicit,");
1061 }
1062 str[strlen(str) - 1] = '\0';
1063
1064 add_cpblt(ctx, str, &cpblts, &size, &count);
1065 }
1066 }
1067 }
1068
Radek Krejci658782b2016-12-04 22:04:55 +01001069 /* other capabilities */
1070 for (u = 0; u < server_opts.capabilities_count; u++) {
1071 add_cpblt(ctx, server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001072 }
1073
1074 /* models */
Michal Vasko1440a742021-03-31 11:11:03 +02001075 u = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001076 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001077 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vasko77367452021-02-16 16:32:18 +01001078 if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) {
Michal Vasko05532772021-06-03 12:12:38 +02001079 ERR(NULL, "Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
Michal Vaskod5ada122020-03-19 18:28:06 +01001080 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001081 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001082
Michal Vasko1440a742021-03-31 11:11:03 +02001083 /* get content-id */
1084 if (server_opts.content_id_clb) {
1085 yl_content_id = server_opts.content_id_clb(server_opts.content_id_data);
1086 if (!yl_content_id) {
1087 ERRMEM;
1088 goto error;
1089 }
1090 } else {
1091 yl_content_id = malloc(11);
1092 if (!yl_content_id) {
1093 ERRMEM;
1094 goto error;
1095 }
1096 sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx));
1097 }
1098
Michal Vasko77367452021-02-16 16:32:18 +01001099 if (!strcmp(mod->revision, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001100 /* new one (capab defined in RFC 8526 section 2) */
Michal Vasko1440a742021-03-31 11:11:03 +02001101 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s",
1102 mod->revision, yl_content_id);
Michal Vaskod5ada122020-03-19 18:28:06 +01001103 add_cpblt(ctx, str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001104 } else {
1105 /* old one (capab defined in RFC 7950 section 5.6.4) */
Michal Vasko1440a742021-03-31 11:11:03 +02001106 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s",
1107 mod->revision, yl_content_id);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001108 add_cpblt(ctx, str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001109 }
Michal Vasko1440a742021-03-31 11:11:03 +02001110 free(yl_content_id);
Radek Krejci24a18412018-05-16 15:09:10 +02001111 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001112 } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) {
Radek Krejci24a18412018-05-16 15:09:10 +02001113 /* skip YANG 1.1 schemas */
1114 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001115 } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) {
Radek Krejci24a18412018-05-16 15:09:10 +02001116 /* skip YANG 1.0 schemas */
1117 continue;
1118 }
Michal Vasko086311b2016-01-08 09:53:11 +01001119
Michal Vasko77367452021-02-16 16:32:18 +01001120 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "",
1121 mod->revision ? mod->revision : "");
Radek Krejci24a18412018-05-16 15:09:10 +02001122
Michal Vaskodafdc742020-03-11 16:15:59 +01001123 features_count = 0;
1124 i = 0;
1125 feat = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001126 while ((feat = lysp_feature_next(feat, mod->parsed, &i))) {
Michal Vaskodafdc742020-03-11 16:15:59 +01001127 if (!(feat->flags & LYS_FENABLED)) {
1128 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001129 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001130 if (!features_count) {
1131 strcat(str, "&features=");
1132 str_len += 10;
1133 }
1134 len = strlen(feat->name);
1135 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1136 ERRINT;
1137 break;
1138 }
1139 if (features_count) {
1140 strcat(str, ",");
1141 ++str_len;
1142 }
1143 strcat(str, feat->name);
1144 str_len += len;
1145 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001146 }
Michal Vasko086311b2016-01-08 09:53:11 +01001147
Michal Vasko77367452021-02-16 16:32:18 +01001148 if (mod->deviated_by) {
Radek Krejci24a18412018-05-16 15:09:10 +02001149 strcat(str, "&deviations=");
1150 str_len += 12;
1151 dev_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +01001152 LY_ARRAY_FOR(mod->deviated_by, v) {
1153 len = strlen(mod->deviated_by[v]->name);
1154 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1155 ERRINT;
1156 break;
Radek Krejci24a18412018-05-16 15:09:10 +02001157 }
Michal Vasko77367452021-02-16 16:32:18 +01001158 if (dev_count) {
1159 strcat(str, ",");
1160 ++str_len;
Radek Krejci24a18412018-05-16 15:09:10 +02001161 }
Michal Vasko77367452021-02-16 16:32:18 +01001162 strcat(str, mod->deviated_by[v]->name);
1163 str_len += len;
1164 dev_count++;
Radek Krejci24a18412018-05-16 15:09:10 +02001165 }
1166 }
1167
1168 add_cpblt(ctx, str, &cpblts, &size, &count);
1169 }
Michal Vasko086311b2016-01-08 09:53:11 +01001170
1171 /* ending NULL capability */
1172 add_cpblt(ctx, NULL, &cpblts, &size, &count);
1173
1174 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001175
1176error:
Radek Krejcif906e412017-09-22 14:44:45 +02001177 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001178 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001179}
1180
Radek Krejci24a18412018-05-16 15:09:10 +02001181API const char **
1182nc_server_get_cpblts(struct ly_ctx *ctx)
1183{
1184 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1185}
1186
Radek Krejci695d4fa2015-10-22 13:23:54 +02001187static int
Michal Vasko77367452021-02-16 16:32:18 +01001188parse_cpblts(struct lyd_node *capabilities, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001189{
Michal Vasko77367452021-02-16 16:32:18 +01001190 struct lyd_node *iter;
1191 struct lyd_node_opaq *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001192 int ver = -1, i = 0;
1193 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001194
1195 if (list) {
1196 /* get the storage for server's capabilities */
Michal Vasko77367452021-02-16 16:32:18 +01001197 LY_LIST_FOR(lyd_child(capabilities), iter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001198 i++;
1199 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001200 /* last item remains NULL */
1201 *list = calloc(i + 1, sizeof **list);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001202 if (!*list) {
1203 ERRMEM;
1204 return -1;
1205 }
1206 i = 0;
1207 }
1208
Michal Vasko77367452021-02-16 16:32:18 +01001209 LY_LIST_FOR(lyd_child(capabilities), iter) {
1210 cpblt = (struct lyd_node_opaq *)iter;
1211
1212 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 +02001213 ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001214 return -1;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001215 }
1216
Michal Vasko156d3272017-04-11 11:46:49 +02001217 /* skip leading/trailing whitespaces */
Michal Vasko77367452021-02-16 16:32:18 +01001218 for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {}
1219 for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {}
1220 if (!cpb_start[0] || (cpb_end == cpblt->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001221 ERR(NULL, "Empty capability \"%s\" received.", cpblt->value);
Michal Vasko156d3272017-04-11 11:46:49 +02001222 return -1;
1223 }
1224
Radek Krejci695d4fa2015-10-22 13:23:54 +02001225 /* detect NETCONF version */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001226 if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001227 ver = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001228 } 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 +02001229 ver = 1;
1230 }
1231
1232 /* store capabilities */
1233 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001234 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
1235 if (!(*list)[i]) {
1236 ERRMEM;
1237 return -1;
1238 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001239 i++;
1240 }
1241 }
1242
1243 if (ver == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001244 ERR(NULL, "Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001245 }
1246
1247 return ver;
1248}
1249
1250static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001251nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001252{
Michal Vasko131120a2018-05-29 15:44:02 +02001253 NC_MSG_TYPE ret;
1254 int i, io_timeout;
Michal Vasko086311b2016-01-08 09:53:11 +01001255 const char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001256 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001257
Michal Vasko131120a2018-05-29 15:44:02 +02001258 if (session->side == NC_CLIENT) {
1259 /* client side hello - send only NETCONF base capabilities */
1260 cpblts = malloc(3 * sizeof *cpblts);
1261 if (!cpblts) {
1262 ERRMEM;
1263 return NC_MSG_ERROR;
1264 }
Michal Vasko77367452021-02-16 16:32:18 +01001265 lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.0", 0, &cpblts[0]);
1266 lydict_insert(session->ctx, "urn:ietf:params:netconf:base:1.1", 0, &cpblts[1]);
Michal Vasko131120a2018-05-29 15:44:02 +02001267 cpblts[2] = NULL;
1268
1269 io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000;
1270 sid = NULL;
1271 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001272 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0);
Michal Vasko5b24b6b2020-12-04 09:29:47 +01001273 if (!cpblts) {
1274 return NC_MSG_ERROR;
1275 }
Michal Vasko131120a2018-05-29 15:44:02 +02001276
1277 io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000;
1278 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001279 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001280
Michal Vasko131120a2018-05-29 15:44:02 +02001281 ret = nc_write_msg_io(session, io_timeout, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001282
Michal Vasko086311b2016-01-08 09:53:11 +01001283 for (i = 0; cpblts[i]; ++i) {
1284 lydict_remove(session->ctx, cpblts[i]);
1285 }
1286 free(cpblts);
1287
Michal Vasko131120a2018-05-29 15:44:02 +02001288 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001289}
1290
1291static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001292nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001293{
Michal Vasko77367452021-02-16 16:32:18 +01001294 struct ly_in *msg;
1295 struct lyd_node *hello = NULL, *iter;
1296 struct lyd_node_opaq *node;
1297 int r, ver = -1, flag = 0;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001298 char *str;
1299 long long int id;
Michal Vasko77367452021-02-16 16:32:18 +01001300 NC_MSG_TYPE rc = NC_MSG_HELLO;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001301
Michal Vasko77367452021-02-16 16:32:18 +01001302 r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg);
1303 switch (r) {
1304 case 1:
Radek Krejci695d4fa2015-10-22 13:23:54 +02001305 /* parse <hello> data */
Michal Vasko77367452021-02-16 16:32:18 +01001306 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 +02001307 ERR(session, "Failed to parse server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001308 rc = NC_MSG_ERROR;
1309 goto cleanup;
1310 }
1311
1312 LY_LIST_FOR(lyd_child(hello), iter) {
1313 node = (struct lyd_node_opaq *)iter;
1314
1315 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001316 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001317 } else if (!strcmp(node->name.name, "session-id")) {
1318 if (!node->value || !strlen(node->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001319 ERR(session, "No value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001320 rc = NC_MSG_ERROR;
1321 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001322 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001323 str = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001324 id = strtoll(node->value, &str, 10);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001325 if (*str || (id < 1) || (id > UINT32_MAX)) {
Michal Vasko05532772021-06-03 12:12:38 +02001326 ERR(session, "Invalid value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001327 rc = NC_MSG_ERROR;
1328 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001329 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001330 session->id = (uint32_t)id;
1331 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001332 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001333 ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001334 rc = NC_MSG_ERROR;
1335 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001336 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001337
1338 if (flag) {
1339 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001340 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001341 rc = NC_MSG_ERROR;
1342 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001343 }
1344 flag = 1;
1345
Michal Vasko77367452021-02-16 16:32:18 +01001346 if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) {
1347 rc = NC_MSG_ERROR;
1348 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001349 }
1350 session->version = ver;
1351 }
1352
1353 if (!session->id) {
Michal Vasko05532772021-06-03 12:12:38 +02001354 ERR(session, "Missing <session-id> in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001355 rc = NC_MSG_ERROR;
1356 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001357 }
1358 break;
Michal Vasko77367452021-02-16 16:32:18 +01001359 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001360 ERR(session, "Server <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001361 rc = NC_MSG_WOULDBLOCK;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001362 break;
1363 default:
Michal Vasko77367452021-02-16 16:32:18 +01001364 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001365 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001366 }
1367
Michal Vasko77367452021-02-16 16:32:18 +01001368cleanup:
1369 ly_in_free(msg, 1);
1370 lyd_free_tree(hello);
1371 return rc;
Radek Krejci5686ff72015-10-09 13:33:56 +02001372}
1373
Michal Vasko11d142a2016-01-19 15:58:24 +01001374static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001375nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001376{
Michal Vasko77367452021-02-16 16:32:18 +01001377 struct ly_in *msg;
1378 struct lyd_node *hello = NULL, *iter;
1379 struct lyd_node_opaq *node;
1380 NC_MSG_TYPE rc = NC_MSG_HELLO;
1381 int r, ver = -1, flag = 0, timeout_io;
Michal Vasko11d142a2016-01-19 15:58:24 +01001382
Michal Vasko77367452021-02-16 16:32:18 +01001383 timeout_io = server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000;
1384 r = nc_read_msg_poll_io(session, timeout_io, &msg);
1385 switch (r) {
1386 case 1:
1387 /* parse <hello> data */
1388 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 +02001389 ERR(session, "Failed to parse client <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001390 rc = NC_MSG_ERROR;
1391 goto cleanup;
1392 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001393
Michal Vasko77367452021-02-16 16:32:18 +01001394 /* learn NETCONF version */
1395 LY_LIST_FOR(lyd_child(hello), iter) {
1396 node = (struct lyd_node_opaq *)iter;
1397
1398 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001399 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001400 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001401 ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001402 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001403 goto cleanup;
1404 }
1405
1406 if (flag) {
1407 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001408 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001409 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001410 goto cleanup;
1411 }
1412 flag = 1;
1413
Michal Vasko77367452021-02-16 16:32:18 +01001414 if ((ver = parse_cpblts(&node->node, NULL)) < 0) {
1415 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001416 goto cleanup;
1417 }
1418 session->version = ver;
1419 }
1420 break;
Michal Vasko77367452021-02-16 16:32:18 +01001421 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001422 ERR(session, "Client <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001423 rc = NC_MSG_WOULDBLOCK;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001424 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001425 default:
Michal Vasko77367452021-02-16 16:32:18 +01001426 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001427 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001428 }
1429
1430cleanup:
Michal Vasko77367452021-02-16 16:32:18 +01001431 ly_in_free(msg, 1);
1432 lyd_free_tree(hello);
1433 return rc;
Michal Vasko11d142a2016-01-19 15:58:24 +01001434}
1435
Michal Vasko71090fc2016-05-24 16:37:28 +02001436NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001437nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001438{
Michal Vasko086311b2016-01-08 09:53:11 +01001439 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001440
Michal Vasko131120a2018-05-29 15:44:02 +02001441 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001442 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001443 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001444 }
1445
Michal Vasko11d142a2016-01-19 15:58:24 +01001446 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001447 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001448 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001449 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001450 }
1451
Michal Vasko71090fc2016-05-24 16:37:28 +02001452 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001453}
Michal Vasko086311b2016-01-08 09:53:11 +01001454
Michal Vasko889162e2019-09-06 14:43:37 +02001455#ifdef NC_ENABLED_SSH
1456
Michal Vasko999b64a2019-07-10 16:06:15 +02001457static void
1458nc_ssh_init(void)
1459{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001460#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
Michal Vasko999b64a2019-07-10 16:06:15 +02001461 ssh_threads_set_callbacks(ssh_threads_get_pthread());
1462 ssh_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001463#endif
Michal Vasko999b64a2019-07-10 16:06:15 +02001464}
1465
1466static void
1467nc_ssh_destroy(void)
1468{
Michal Vaskoe1e82632019-09-09 09:18:03 +02001469#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko999b64a2019-07-10 16:06:15 +02001470 FIPS_mode_set(0);
1471 CONF_modules_unload(1);
1472 nc_thread_destroy();
Michal Vasko889162e2019-09-06 14:43:37 +02001473#endif
1474
Michal Vaskoe1e82632019-09-09 09:18:03 +02001475#if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0))
1476 ssh_finalize();
1477#endif
1478}
1479
Michal Vasko889162e2019-09-06 14:43:37 +02001480#endif /* NC_ENABLED_SSH */
Michal Vasko999b64a2019-07-10 16:06:15 +02001481
Radek Krejci53691be2016-02-22 13:58:37 +01001482#ifdef NC_ENABLED_TLS
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001483
Michal Vasko770b4362017-02-17 14:44:18 +01001484#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1485
Michal Vaskof0c92c02016-01-29 09:41:45 +01001486struct CRYPTO_dynlock_value {
1487 pthread_mutex_t lock;
1488};
1489
Michal Vaskof0c92c02016-01-29 09:41:45 +01001490static struct CRYPTO_dynlock_value *
1491tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line))
1492{
1493 struct CRYPTO_dynlock_value *value;
1494
1495 value = malloc(sizeof *value);
1496 if (!value) {
1497 ERRMEM;
1498 return NULL;
1499 }
1500 pthread_mutex_init(&value->lock, NULL);
1501
1502 return value;
1503}
1504
1505static void
1506tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1507{
1508 /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples
1509 * I found ignored this fact, what do I know... */
1510 if (mode & CRYPTO_LOCK) {
1511 pthread_mutex_lock(&l->lock);
1512 } else {
1513 pthread_mutex_unlock(&l->lock);
1514 }
1515}
1516
1517static void
1518tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line))
1519{
1520 pthread_mutex_destroy(&l->lock);
1521 free(l);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001522}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001523
Michal Vasko770b4362017-02-17 14:44:18 +01001524#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001525
Michal Vasko8f0c0282016-02-29 10:17:14 +01001526#endif /* NC_ENABLED_TLS */
1527
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001528#if defined (NC_ENABLED_TLS) && !defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001529
Michal Vasko770b4362017-02-17 14:44:18 +01001530#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001531static pthread_mutex_t *tls_locks;
1532
1533static void
1534tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line))
1535{
1536 if (mode & CRYPTO_LOCK) {
1537 pthread_mutex_lock(tls_locks + n);
1538 } else {
1539 pthread_mutex_unlock(tls_locks + n);
1540 }
1541}
1542
1543static void
1544tls_thread_id_func(CRYPTO_THREADID *tid)
1545{
1546 CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self());
1547}
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001548
Michal Vasko770b4362017-02-17 14:44:18 +01001549#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001550
1551static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001552nc_tls_init(void)
1553{
Rosen Penev4f552d62019-06-26 16:10:43 -07001554#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001555 SSL_load_error_strings();
Michal Vasko7f1c78b2016-01-19 09:52:14 +01001556 ERR_load_BIO_strings();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001557 SSL_library_init();
1558
Michal Vaskof89948c2018-01-04 09:19:46 +01001559 int i;
1560
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001561 tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001562 if (!tls_locks) {
1563 ERRMEM;
1564 return;
1565 }
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001566 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1567 pthread_mutex_init(tls_locks + i, NULL);
1568 }
1569
Michal Vaskof0c92c02016-01-29 09:41:45 +01001570 CRYPTO_THREADID_set_callback(tls_thread_id_func);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001571 CRYPTO_set_locking_callback(tls_thread_locking_func);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001572
1573 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1574 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1575 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001576#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001577}
1578
Michal Vasko8f0c0282016-02-29 10:17:14 +01001579static void
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001580nc_tls_destroy(void)
1581{
Rosen Penev4f552d62019-06-26 16:10:43 -07001582#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko8f0c0282016-02-29 10:17:14 +01001583 FIPS_mode_set(0);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001584 CRYPTO_cleanup_all_ex_data();
Michal Vaskob6e37262016-02-25 14:49:00 +01001585 nc_thread_destroy();
Michal Vasko5e228792016-02-03 15:30:24 +01001586 EVP_cleanup();
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001587 ERR_free_strings();
Michal Vasko770b4362017-02-17 14:44:18 +01001588#if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001589 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vasko770b4362017-02-17 14:44:18 +01001590#elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1591 SSL_COMP_free_compression_methods();
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001592#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001593
Michal Vaskof89948c2018-01-04 09:19:46 +01001594 int i;
1595
Michal Vaskob6e37262016-02-25 14:49:00 +01001596 CRYPTO_THREADID_set_callback(NULL);
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001597 CRYPTO_set_locking_callback(NULL);
1598 for (i = 0; i < CRYPTO_num_locks(); ++i) {
1599 pthread_mutex_destroy(tls_locks + i);
1600 }
1601 free(tls_locks);
Michal Vaskof0c92c02016-01-29 09:41:45 +01001602
1603 CRYPTO_set_dynlock_create_callback(NULL);
1604 CRYPTO_set_dynlock_lock_callback(NULL);
1605 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001606#endif
Michal Vaskoc14e3c82016-01-11 16:14:30 +01001607}
1608
Michal Vasko8f0c0282016-02-29 10:17:14 +01001609#endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */
Michal Vasko5e228792016-02-03 15:30:24 +01001610
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001611#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko5e228792016-02-03 15:30:24 +01001612
Michal Vasko8f0c0282016-02-29 10:17:14 +01001613static void
Michal Vasko5e228792016-02-03 15:30:24 +01001614nc_ssh_tls_init(void)
1615{
Rosen Penev4f552d62019-06-26 16:10:43 -07001616#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001617 SSL_load_error_strings();
1618 ERR_load_BIO_strings();
1619 SSL_library_init();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001620#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001621
1622 nc_ssh_init();
1623
Michal Vaskoe1e82632019-09-09 09:18:03 +02001624#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001625 CRYPTO_set_dynlock_create_callback(tls_dyn_create_func);
1626 CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func);
1627 CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func);
Michal Vasko770b4362017-02-17 14:44:18 +01001628#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001629}
1630
Michal Vasko8f0c0282016-02-29 10:17:14 +01001631static void
Michal Vasko5e228792016-02-03 15:30:24 +01001632nc_ssh_tls_destroy(void)
1633{
Rosen Penev4f552d62019-06-26 16:10:43 -07001634#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001635 ERR_free_strings();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001636# if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2
Michal Vasko5e228792016-02-03 15:30:24 +01001637 sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
Michal Vaskoe1e82632019-09-09 09:18:03 +02001638# elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko770b4362017-02-17 14:44:18 +01001639 SSL_COMP_free_compression_methods();
Michal Vaskoe1e82632019-09-09 09:18:03 +02001640# endif
Jan Kundrát47e9e1a2016-11-21 09:41:59 +01001641#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001642
1643 nc_ssh_destroy();
1644
Michal Vaskoe1e82632019-09-09 09:18:03 +02001645#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
Michal Vasko5e228792016-02-03 15:30:24 +01001646 CRYPTO_set_dynlock_create_callback(NULL);
1647 CRYPTO_set_dynlock_lock_callback(NULL);
1648 CRYPTO_set_dynlock_destroy_callback(NULL);
Michal Vasko770b4362017-02-17 14:44:18 +01001649#endif
Michal Vasko5e228792016-02-03 15:30:24 +01001650}
1651
Radek Krejci53691be2016-02-22 13:58:37 +01001652#endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */
Michal Vasko8f0c0282016-02-29 10:17:14 +01001653
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001654#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001655
1656API void
1657nc_thread_destroy(void)
1658{
Michal Vasko8f0c0282016-02-29 10:17:14 +01001659 /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001660 // CRYPTO_cleanup_all_ex_data();
Michal Vasko8f0c0282016-02-29 10:17:14 +01001661
Michal Vasko770b4362017-02-17 14:44:18 +01001662#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
1663 CRYPTO_THREADID crypto_tid;
1664
Michal Vasko8f0c0282016-02-29 10:17:14 +01001665 CRYPTO_THREADID_current(&crypto_tid);
1666 ERR_remove_thread_state(&crypto_tid);
Michal Vasko770b4362017-02-17 14:44:18 +01001667#endif
Michal Vasko8f0c0282016-02-29 10:17:14 +01001668}
1669
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001670#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
1671
1672void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001673nc_init(void)
1674{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001675#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001676 nc_ssh_tls_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001677#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001678 nc_ssh_init();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001679#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001680 nc_tls_init();
1681#endif
1682}
1683
Michal Vaskoa7b8ca52016-03-01 12:09:29 +01001684void
Michal Vasko8f0c0282016-02-29 10:17:14 +01001685nc_destroy(void)
1686{
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001687#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001688 nc_ssh_tls_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001689#elif defined (NC_ENABLED_SSH)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001690 nc_ssh_destroy();
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001691#elif defined (NC_ENABLED_TLS)
Michal Vasko8f0c0282016-02-29 10:17:14 +01001692 nc_tls_destroy();
1693#endif
1694}