blob: 724d98a0c61f6c717357554dcbe68335b79e01bf [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
Michal Vaskod8a74192023-02-06 15:51:50 +01007 * Copyright (c) 2015 - 2023 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>
roman3c5b75d2024-04-18 14:56:53 +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"
roman3f9b65c2023-06-05 14:26:58 +020033#include "config.h"
34#include "log_p.h"
35#include "netconf.h"
36#include "session_p.h"
Michal Vaskob48aa812016-01-18 14:13:09 +010037
roman2eab4742023-06-06 10:00:26 +020038#ifdef NC_ENABLED_SSH_TLS
Radek Krejci206fcd62015-10-07 15:42:48 +020039
roman2eab4742023-06-06 10:00:26 +020040#include <libssh/libssh.h>
roman008cfe72024-04-05 12:36:18 +020041#include "session_wrapper.h"
Radek Krejci206fcd62015-10-07 15:42:48 +020042
roman2eab4742023-06-06 10:00:26 +020043#endif /* NC_ENABLED_SSH_TLS */
Michal Vaskoc14e3c82016-01-11 16:14:30 +010044
Michal Vasko086311b2016-01-08 09:53:11 +010045/* in seconds */
46#define NC_CLIENT_HELLO_TIMEOUT 60
Michal Vaskocf898172024-01-15 15:04:28 +010047#define NC_SERVER_CH_HELLO_TIMEOUT 180
Radek Krejci695d4fa2015-10-22 13:23:54 +020048
Michal Vasko05ba9df2016-01-13 14:40:27 +010049/* in milliseconds */
50#define NC_CLOSE_REPLY_TIMEOUT 200
51
Michal Vasko086311b2016-01-08 09:53:11 +010052extern struct nc_server_opts server_opts;
53
Michal Vaskod8a74192023-02-06 15:51:50 +010054void
55nc_timeouttime_get(struct timespec *ts, uint32_t add_ms)
Radek Krejci7ac16052016-07-15 11:48:18 +020056{
Michal Vaskod8a74192023-02-06 15:51:50 +010057 if (clock_gettime(COMPAT_CLOCK_ID, ts) == -1) {
58 ERR(NULL, "clock_gettime() failed (%s).", strerror(errno));
59 return;
60 }
61
62 if (!add_ms) {
63 return;
64 }
65
66 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
67
68 ts->tv_sec += add_ms / 1000;
69 ts->tv_nsec += (add_ms % 1000) * 1000000L;
70
71 if (ts->tv_nsec >= 1000000000L) {
72 ++ts->tv_sec;
73 ts->tv_nsec -= 1000000000L;
74 } else if (ts->tv_nsec < 0) {
75 --ts->tv_sec;
76 ts->tv_nsec += 1000000000L;
77 }
78
79 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
80}
81
82int32_t
83nc_timeouttime_cur_diff(const struct timespec *ts)
84{
85 struct timespec cur;
86 int64_t nsec_diff = 0;
87
88 nc_timeouttime_get(&cur, 0);
89
90 nsec_diff += (((int64_t)ts->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
91 nsec_diff += ((int64_t)ts->tv_nsec) - ((int64_t)cur.tv_nsec);
92
93 return nsec_diff / 1000000L;
94}
95
96void
97nc_realtime_get(struct timespec *ts)
98{
roman6ece9c52022-06-22 09:29:17 +020099 if (clock_gettime(CLOCK_REALTIME, ts)) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100100 ERR(NULL, "clock_gettime() failed (%s).", strerror(errno));
101 return;
roman6ece9c52022-06-22 09:29:17 +0200102 }
Michal Vasko36c7be82017-02-22 13:37:59 +0100103}
104
Michal Vasko63b92d62024-04-29 10:04:56 +0200105int
106nc_poll(struct pollfd *pfd, uint16_t pfd_count, int timeout_ms)
107{
108 int rc;
109 struct timespec start_ts;
110
111 if (timeout_ms > 0) {
112 /* get current time */
113 nc_timeouttime_get(&start_ts, 0);
114 }
115
116 do {
117 /* poll */
118 rc = poll(pfd, pfd_count, timeout_ms);
119
120 if (timeout_ms > 0) {
121 /* adjust the timeout by subtracting the elapsed time (relevant in case of EINTR) */
122 timeout_ms += nc_timeouttime_cur_diff(&start_ts);
123 if (timeout_ms < 0) {
124 /* manual timeout */
125 rc = 0;
126 errno = 0;
127 break;
128 }
129 }
130 } while ((rc == -1) && (errno == EINTR));
131
132 if (rc == -1) {
133 ERR(NULL, "Poll failed (%s).", strerror(errno));
134 }
135 return rc;
136}
137
roman2eab4742023-06-06 10:00:26 +0200138#ifdef NC_ENABLED_SSH_TLS
139
Michal Vaskoddce1212019-05-24 09:58:49 +0200140const char *
roman3f9b65c2023-06-05 14:26:58 +0200141nc_privkey_format_to_str(NC_PRIVKEY_FORMAT format)
Michal Vaskoddce1212019-05-24 09:58:49 +0200142{
roman3f9b65c2023-06-05 14:26:58 +0200143 switch (format) {
144 case NC_PRIVKEY_FORMAT_RSA:
romand0fe5952024-03-21 15:59:33 +0100145 return " RSA ";
roman3f9b65c2023-06-05 14:26:58 +0200146 case NC_PRIVKEY_FORMAT_EC:
romand0fe5952024-03-21 15:59:33 +0100147 return " EC ";
roman3f9b65c2023-06-05 14:26:58 +0200148 case NC_PRIVKEY_FORMAT_X509:
romand0fe5952024-03-21 15:59:33 +0100149 return " ";
roman3f9b65c2023-06-05 14:26:58 +0200150 case NC_PRIVKEY_FORMAT_OPENSSH:
romand0fe5952024-03-21 15:59:33 +0100151 return " OPENSSH ";
Michal Vaskoddce1212019-05-24 09:58:49 +0200152 default:
roman3f9b65c2023-06-05 14:26:58 +0200153 return NULL;
Michal Vaskoddce1212019-05-24 09:58:49 +0200154 }
Michal Vaskoddce1212019-05-24 09:58:49 +0200155}
156
roman13145912023-08-17 15:36:54 +0200157int
roman51a1e192023-09-14 10:13:45 +0200158nc_is_pk_subject_public_key_info(const char *b64)
159{
160 int ret = 0;
161 long len;
162 char *bin = NULL, *tmp;
roman51a1e192023-09-14 10:13:45 +0200163
roman9ba26aa2024-04-05 12:31:54 +0200164 /* decode base64 */
165 len = nc_base64_decode_wrap(b64, &bin);
roman51a1e192023-09-14 10:13:45 +0200166 if (len == -1) {
167 ERR(NULL, "Decoding base64 public key to binary failed.");
168 ret = -1;
169 goto cleanup;
170 }
171
172 /* for deallocation later */
173 tmp = bin;
174
roman9ba26aa2024-04-05 12:31:54 +0200175 /* try to parse the supposed SubjectPublicKeyInfo binary data */
176 if (!nc_der_to_pubkey_wrap((const unsigned char *)tmp, len)) {
177 /* success, it's most likely SubjectPublicKeyInfo */
roman51a1e192023-09-14 10:13:45 +0200178 ret = 1;
179 } else {
roman9ba26aa2024-04-05 12:31:54 +0200180 /* it's most likely not SubjectPublicKeyInfo */
roman51a1e192023-09-14 10:13:45 +0200181 ret = 0;
182 }
183
184cleanup:
roman51a1e192023-09-14 10:13:45 +0200185 free(bin);
186 return ret;
187}
188
roman2eab4742023-06-06 10:00:26 +0200189#endif /* NC_ENABLED_SSH_TLS */
190
Michal Vaskobe52dc22018-10-17 09:28:17 +0200191int
Michal Vaskoc4cdc9e2024-05-03 12:03:07 +0200192nc_sock_configure_ka(int sock, const struct nc_keepalives *ka)
Michal Vaskobe52dc22018-10-17 09:28:17 +0200193{
Michal Vaskoc4cdc9e2024-05-03 12:03:07 +0200194 int opt;
195
196 opt = ka->enabled;
197 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) {
roman56acd552024-04-18 16:00:37 +0200198 ERR(NULL, "Failed to set SO_KEEPALIVE (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200199 return -1;
200 }
roman56acd552024-04-18 16:00:37 +0200201
Michal Vaskoc4cdc9e2024-05-03 12:03:07 +0200202 if (!ka->enabled) {
203 return 0;
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200204 }
Michal Vaskobe52dc22018-10-17 09:28:17 +0200205
206#ifdef TCP_KEEPIDLE
Michal Vaskoc4cdc9e2024-05-03 12:03:07 +0200207 opt = ka->idle_time;
208 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) {
roman56acd552024-04-18 16:00:37 +0200209 ERR(NULL, "Failed to set TCP_KEEPIDLE (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200210 return -1;
211 }
212#endif
213
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200214#ifdef TCP_KEEPCNT
Michal Vaskoc4cdc9e2024-05-03 12:03:07 +0200215 opt = ka->max_probes;
216 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) {
roman56acd552024-04-18 16:00:37 +0200217 ERR(NULL, "Failed to set TCP_KEEPCNT (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200218 return -1;
219 }
220#endif
221
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200222#ifdef TCP_KEEPINTVL
Michal Vaskoc4cdc9e2024-05-03 12:03:07 +0200223 opt = ka->probe_interval;
224 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof opt) == -1) {
roman56acd552024-04-18 16:00:37 +0200225 ERR(NULL, "Failed to set TCP_KEEPINTVL (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200226 return -1;
227 }
228#endif
229
230 return 0;
231}
232
Michal Vaskoade892d2017-02-22 13:40:35 +0100233struct nc_session *
Michal Vasko131120a2018-05-29 15:44:02 +0200234nc_new_session(NC_SIDE side, int shared_ti)
Michal Vaskoade892d2017-02-22 13:40:35 +0100235{
236 struct nc_session *sess;
Michal Vaskocf898172024-01-15 15:04:28 +0100237 struct timespec ts_cur;
Michal Vaskoade892d2017-02-22 13:40:35 +0100238
239 sess = calloc(1, sizeof *sess);
240 if (!sess) {
241 return NULL;
242 }
243
Michal Vasko131120a2018-05-29 15:44:02 +0200244 sess->side = side;
245
246 if (side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200247 pthread_mutex_init(&sess->opts.server.ntf_status_lock, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100248 pthread_mutex_init(&sess->opts.server.rpc_lock, NULL);
249 pthread_cond_init(&sess->opts.server.rpc_cond, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100250
251 pthread_mutex_init(&sess->opts.server.ch_lock, NULL);
252 pthread_cond_init(&sess->opts.server.ch_cond, NULL);
Michal Vaskocf898172024-01-15 15:04:28 +0100253
254 /* initialize last_rpc for idle_timeout */
255 nc_timeouttime_get(&ts_cur, 0);
256 sess->opts.server.last_rpc = ts_cur.tv_sec;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200257 } else {
258 pthread_mutex_init(&sess->opts.client.msgs_lock, NULL);
Michal Vasko131120a2018-05-29 15:44:02 +0200259 }
260
261 if (!shared_ti) {
262 sess->io_lock = malloc(sizeof *sess->io_lock);
263 if (!sess->io_lock) {
264 goto error;
265 }
266 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100267 }
268
269 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200270
271error:
Michal Vasko131120a2018-05-29 15:44:02 +0200272 free(sess);
273 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100274}
275
Michal Vasko96164bf2016-01-21 15:41:58 +0100276/*
277 * @return 1 - success
278 * 0 - timeout
279 * -1 - error
280 */
281int
Michal Vasko131120a2018-05-29 15:44:02 +0200282nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100283{
284 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100285 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100286
Michal Vasko131120a2018-05-29 15:44:02 +0200287 if (session->side != NC_SERVER) {
288 ERRINT;
289 return -1;
290 }
291
Michal Vasko96164bf2016-01-21 15:41:58 +0100292 if (timeout > 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100293 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100294
Michal Vaskoade892d2017-02-22 13:40:35 +0100295 /* LOCK */
Michal Vaskod8a74192023-02-06 15:51:50 +0100296 ret = pthread_mutex_clocklock(&session->opts.server.rpc_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100297 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100298 while (session->opts.server.rpc_inuse) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100299 ret = pthread_cond_clockwait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock,
300 COMPAT_CLOCK_ID, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100301 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100302 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100303 break;
304 }
305 }
306 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100307 } else if (!timeout) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100308 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100309 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100310 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100311 if (session->opts.server.rpc_inuse) {
312 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100313 return 0;
314 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200315 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100316 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100317 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100318 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100319 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100320 while (session->opts.server.rpc_inuse) {
321 ret = pthread_cond_wait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100322 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100323 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100324 break;
325 }
326 }
327 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100328 }
329
Michal Vaskoade892d2017-02-22 13:40:35 +0100330 if (ret) {
331 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
332 /* timeout */
333 return 0;
334 }
335
Michal Vasko96164bf2016-01-21 15:41:58 +0100336 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200337 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100338 return -1;
339 }
340
341 /* ok */
Michal Vaskoacf98472021-02-04 15:33:57 +0100342 assert(session->opts.server.rpc_inuse == 0);
343 session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100344
345 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100346 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100347 if (ret) {
348 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200349 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100350 return -1;
351 }
352
353 return 1;
354}
355
356int
Michal Vasko131120a2018-05-29 15:44:02 +0200357nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100358{
359 int ret;
360 struct timespec ts_timeout;
361
Michal Vasko131120a2018-05-29 15:44:02 +0200362 if (session->side != NC_SERVER) {
363 ERRINT;
364 return -1;
365 }
366
Michal Vaskoacf98472021-02-04 15:33:57 +0100367 assert(session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100368
369 if (timeout > 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100370 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100371
372 /* LOCK */
Michal Vaskod8a74192023-02-06 15:51:50 +0100373 ret = pthread_mutex_clocklock(&session->opts.server.rpc_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100374 } else if (!timeout) {
375 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100376 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100377 } else { /* timeout == -1 */
378 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100379 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100380 }
381
382 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
383 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200384 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100385 return -1;
386 } else if (ret) {
Michal Vasko69e98752022-12-14 14:20:17 +0100387 WRN(session, "%s: session RPC lock timeout, should not happen.", func);
Michal Vaskoade892d2017-02-22 13:40:35 +0100388 }
389
Michal Vaskoacf98472021-02-04 15:33:57 +0100390 session->opts.server.rpc_inuse = 0;
391 pthread_cond_signal(&session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100392
393 if (!ret) {
394 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100395 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100396 if (ret) {
397 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200398 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100399 return -1;
400 }
401 }
402
Michal Vasko96164bf2016-01-21 15:41:58 +0100403 return 1;
404}
405
Michal Vasko131120a2018-05-29 15:44:02 +0200406int
407nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
408{
409 int ret;
410 struct timespec ts_timeout;
411
412 if (timeout > 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100413 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko131120a2018-05-29 15:44:02 +0200414
Michal Vaskod8a74192023-02-06 15:51:50 +0100415 ret = pthread_mutex_clocklock(session->io_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vasko131120a2018-05-29 15:44:02 +0200416 } else if (!timeout) {
417 ret = pthread_mutex_trylock(session->io_lock);
418 } else { /* timeout == -1 */
Robin Jarry54ea2962018-10-10 10:33:40 +0200419 ret = pthread_mutex_lock(session->io_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200420 }
421
422 if (ret) {
423 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
424 /* timeout */
425 return 0;
426 }
427
428 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200429 ERR(session, "%s: failed to IO lock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200430 return -1;
431 }
432
433 return 1;
434}
435
436int
437nc_session_io_unlock(struct nc_session *session, const char *func)
438{
439 int ret;
440
441 ret = pthread_mutex_unlock(session->io_lock);
442 if (ret) {
443 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200444 ERR(session, "%s: failed to IO unlock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200445 return -1;
446 }
447
448 return 1;
449}
450
Michal Vasko01130bd2021-08-26 11:47:38 +0200451int
452nc_session_client_msgs_lock(struct nc_session *session, int *timeout, const char *func)
453{
454 int ret;
455 int32_t diff_msec;
roman6ece9c52022-06-22 09:29:17 +0200456 struct timespec ts_timeout, ts_start;
Michal Vasko01130bd2021-08-26 11:47:38 +0200457
458 assert(session->side == NC_CLIENT);
459
460 if (*timeout > 0) {
461 /* get current time */
Michal Vaskod8a74192023-02-06 15:51:50 +0100462 nc_timeouttime_get(&ts_start, 0);
Michal Vasko01130bd2021-08-26 11:47:38 +0200463
Michal Vaskod8a74192023-02-06 15:51:50 +0100464 nc_timeouttime_get(&ts_timeout, *timeout);
Michal Vasko01130bd2021-08-26 11:47:38 +0200465
Michal Vaskod8a74192023-02-06 15:51:50 +0100466 ret = pthread_mutex_clocklock(&session->opts.client.msgs_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vasko01130bd2021-08-26 11:47:38 +0200467 if (!ret) {
468 /* update timeout based on what was elapsed */
Michal Vaskod8a74192023-02-06 15:51:50 +0100469 diff_msec = nc_timeouttime_cur_diff(&ts_start);
Michal Vasko01130bd2021-08-26 11:47:38 +0200470 *timeout -= diff_msec;
471 }
472 } else if (!*timeout) {
473 ret = pthread_mutex_trylock(&session->opts.client.msgs_lock);
474 } else { /* timeout == -1 */
475 ret = pthread_mutex_lock(&session->opts.client.msgs_lock);
476 }
477
478 if (ret) {
479 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
480 /* timeout */
481 return 0;
482 }
483
484 /* error */
485 ERR(session, "%s: failed to MSGS lock a session (%s).", func, strerror(ret));
486 return -1;
487 }
488
489 return 1;
490}
491
492int
493nc_session_client_msgs_unlock(struct nc_session *session, const char *func)
494{
495 int ret;
496
497 assert(session->side == NC_CLIENT);
498
499 ret = pthread_mutex_unlock(&session->opts.client.msgs_lock);
500 if (ret) {
501 /* error */
502 ERR(session, "%s: failed to MSGS unlock a session (%s).", func, strerror(ret));
503 return -1;
504 }
505
506 return 1;
507}
508
Michal Vasko8dadf782016-01-15 10:29:36 +0100509API NC_STATUS
510nc_session_get_status(const struct nc_session *session)
511{
roman40672412023-05-04 11:10:22 +0200512 NC_CHECK_ARG_RET(session, session, NC_STATUS_ERR);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100513
Michal Vasko8dadf782016-01-15 10:29:36 +0100514 return session->status;
515}
516
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100517API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200518nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100519{
roman40672412023-05-04 11:10:22 +0200520 NC_CHECK_ARG_RET(session, session, NC_SESSION_TERM_ERR);
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100521
522 return session->term_reason;
523}
524
Michal Vasko8dadf782016-01-15 10:29:36 +0100525API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200526nc_session_get_killed_by(const struct nc_session *session)
527{
roman40672412023-05-04 11:10:22 +0200528 NC_CHECK_ARG_RET(session, session, 0);
Michal Vasko142cfea2017-08-07 10:12:11 +0200529
530 return session->killed_by;
531}
532
533API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100534nc_session_get_id(const struct nc_session *session)
535{
roman40672412023-05-04 11:10:22 +0200536 NC_CHECK_ARG_RET(session, session, 0);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100537
Michal Vasko8dadf782016-01-15 10:29:36 +0100538 return session->id;
539}
540
Michal Vasko174fe8e2016-02-17 15:38:09 +0100541API int
542nc_session_get_version(const struct nc_session *session)
543{
roman40672412023-05-04 11:10:22 +0200544 NC_CHECK_ARG_RET(session, session, -1);
Michal Vasko174fe8e2016-02-17 15:38:09 +0100545
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200546 return session->version == NC_VERSION_10 ? 0 : 1;
Michal Vasko174fe8e2016-02-17 15:38:09 +0100547}
548
Michal Vasko8dadf782016-01-15 10:29:36 +0100549API NC_TRANSPORT_IMPL
550nc_session_get_ti(const struct nc_session *session)
551{
roman40672412023-05-04 11:10:22 +0200552 NC_CHECK_ARG_RET(session, session, 0);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100553
Michal Vasko8dadf782016-01-15 10:29:36 +0100554 return session->ti_type;
555}
556
557API const char *
558nc_session_get_username(const struct nc_session *session)
559{
roman40672412023-05-04 11:10:22 +0200560 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100561
Michal Vasko8dadf782016-01-15 10:29:36 +0100562 return session->username;
563}
564
565API const char *
566nc_session_get_host(const struct nc_session *session)
567{
roman40672412023-05-04 11:10:22 +0200568 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100569
Michal Vasko8dadf782016-01-15 10:29:36 +0100570 return session->host;
571}
572
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200573API const char *
574nc_session_get_path(const struct nc_session *session)
575{
roman40672412023-05-04 11:10:22 +0200576 NC_CHECK_ARG_RET(session, session, NULL);
577
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200578 if (session->ti_type != NC_TI_UNIX) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200579 return NULL;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200580 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200581
582 return session->path;
583}
584
Michal Vasko8dadf782016-01-15 10:29:36 +0100585API uint16_t
586nc_session_get_port(const struct nc_session *session)
587{
roman40672412023-05-04 11:10:22 +0200588 NC_CHECK_ARG_RET(session, session, 0);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100589
Michal Vasko8dadf782016-01-15 10:29:36 +0100590 return session->port;
591}
592
Michal Vasko93224072021-11-09 12:14:28 +0100593API const struct ly_ctx *
Michal Vasko9a25e932016-02-01 10:36:42 +0100594nc_session_get_ctx(const struct nc_session *session)
595{
roman40672412023-05-04 11:10:22 +0200596 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vasko9a25e932016-02-01 10:36:42 +0100597
598 return session->ctx;
599}
600
Michal Vasko2cc4c682016-03-01 09:16:48 +0100601API void
602nc_session_set_data(struct nc_session *session, void *data)
603{
604 if (!session) {
roman40672412023-05-04 11:10:22 +0200605 ERRARG(NULL, "session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100606 return;
607 }
608
609 session->data = data;
610}
611
612API void *
613nc_session_get_data(const struct nc_session *session)
614{
roman40672412023-05-04 11:10:22 +0200615 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vasko2cc4c682016-03-01 09:16:48 +0100616
617 return session->data;
618}
619
Michal Vaskodc96bb92023-03-28 08:52:48 +0200620API int
621nc_session_is_callhome(const struct nc_session *session)
622{
roman40672412023-05-04 11:10:22 +0200623 NC_CHECK_ARG_RET(session, session, 0);
Michal Vaskodc96bb92023-03-28 08:52:48 +0200624
625 if (session->flags & NC_SESSION_CALLHOME) {
626 return 1;
627 }
628
629 return 0;
630}
631
Michal Vasko086311b2016-01-08 09:53:11 +0100632NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200633nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200634{
Michal Vasko7e1f5fb2021-11-10 10:14:45 +0100635 if (session->ctx != LYD_CTX(op)) {
636 ERR(session, "RPC \"%s\" was created in different context than that of the session.", LYD_NAME(op));
Michal Vasko086311b2016-01-08 09:53:11 +0100637 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100638 }
639
Michal Vasko131120a2018-05-29 15:44:02 +0200640 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100641}
642
Michal Vaskod4da3632022-05-25 11:49:10 +0200643/**
644 * @brief Send \<close-session\> and read the reply on a session.
645 *
646 * @param[in] session Closing NETCONF session.
647 */
648static void
649nc_session_free_close_session(struct nc_session *session)
650{
651 struct ly_in *msg;
652 struct lyd_node *close_rpc, *envp;
653 const struct lys_module *ietfnc;
654
655 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
656 if (!ietfnc) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200657 WRN(session, "Missing ietf-netconf module in context, unable to send <close-session>.");
Michal Vaskod4da3632022-05-25 11:49:10 +0200658 return;
659 }
660 if (lyd_new_inner(NULL, ietfnc, "close-session", 0, &close_rpc)) {
661 WRN(session, "Failed to create <close-session> RPC.");
662 return;
663 }
664
665 /* send the RPC */
666 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
667
668read_msg:
669 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &msg)) {
670 case 1:
671 if (!strncmp(ly_in_memory(msg, NULL), "<notification", 13)) {
672 /* ignore */
673 ly_in_free(msg, 1);
674 goto read_msg;
675 }
676 if (lyd_parse_op(session->ctx, close_rpc, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, &envp, NULL)) {
677 WRN(session, "Failed to parse <close-session> reply.");
678 } else if (!lyd_child(envp) || strcmp(LYD_NAME(lyd_child(envp)), "ok")) {
679 WRN(session, "Reply to <close-session> was not <ok> as expected.");
680 }
681 lyd_free_tree(envp);
682 ly_in_free(msg, 1);
683 break;
684 case 0:
685 WRN(session, "Timeout for receiving a reply to <close-session> elapsed.");
686 break;
687 case -1:
688 ERR(session, "Failed to receive a reply to <close-session>.");
689 break;
690 default:
691 /* cannot happen */
692 break;
693 }
694 lyd_free_tree(close_rpc);
695}
696
Michal Vasko33476c32022-09-09 11:21:40 +0200697/**
698 * @brief Free transport implementation members of a session.
699 *
700 * @param[in] session Session to free.
701 * @param[out] multisession Whether there are other NC sessions on the same SSH sessions.
702 */
703static void
704nc_session_free_transport(struct nc_session *session, int *multisession)
705{
706 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskoe44f2702022-12-12 07:58:06 +0100707 int sock = -1;
Michal Vasko33476c32022-09-09 11:21:40 +0200708 struct nc_session *siter;
709
710 *multisession = 0;
711 connected = nc_session_is_connected(session);
712
713 /* transport implementation cleanup */
714 switch (session->ti_type) {
715 case NC_TI_FD:
716 /* nothing needed - file descriptors were provided by caller,
717 * so it is up to the caller to close them correctly
718 * TODO use callbacks
719 */
720 /* just to avoid compiler warning */
721 (void)connected;
722 (void)siter;
723 break;
724
725 case NC_TI_UNIX:
726 sock = session->ti.unixsock.sock;
727 (void)connected;
728 (void)siter;
729 break;
730
roman2eab4742023-06-06 10:00:26 +0200731#ifdef NC_ENABLED_SSH_TLS
Michal Vaskoe44f2702022-12-12 07:58:06 +0100732 case NC_TI_LIBSSH: {
733 int r;
734
Michal Vasko33476c32022-09-09 11:21:40 +0200735 if (connected) {
Michal Vasko64734402022-09-09 11:22:00 +0200736 ssh_channel_send_eof(session->ti.libssh.channel);
Michal Vasko33476c32022-09-09 11:21:40 +0200737 ssh_channel_free(session->ti.libssh.channel);
738 }
739 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
740 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
741 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
742 */
743 /* SESSION IO LOCK */
744 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
745
746 if (session->ti.libssh.next) {
747 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
748 if (siter->status != NC_STATUS_STARTING) {
749 *multisession = 1;
750 break;
751 }
752 }
753 }
754
755 if (!*multisession) {
756 /* it's not multisession yet, but we still need to free the starting sessions */
757 if (session->ti.libssh.next) {
758 do {
759 siter = session->ti.libssh.next;
760 session->ti.libssh.next = siter->ti.libssh.next;
761
762 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
763 free(siter->username);
764 free(siter->host);
765 if (!(siter->flags & NC_SESSION_SHAREDCTX)) {
766 ly_ctx_destroy((struct ly_ctx *)siter->ctx);
767 }
768
769 free(siter);
770 } while (session->ti.libssh.next != session);
771 }
772 /* remember sock so we can close it */
773 sock = ssh_get_fd(session->ti.libssh.session);
774 if (connected) {
Michal Vasko70e90622023-12-07 08:43:14 +0100775 /* does not close sock */
Michal Vasko33476c32022-09-09 11:21:40 +0200776 ssh_disconnect(session->ti.libssh.session);
Michal Vasko33476c32022-09-09 11:21:40 +0200777 }
778 ssh_free(session->ti.libssh.session);
779 } else {
780 /* remove the session from the list */
781 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {}
782 if (session->ti.libssh.next == siter) {
783 /* there will be only one session */
784 siter->ti.libssh.next = NULL;
785 } else {
786 /* there are still multiple sessions, keep the ring list */
787 siter->ti.libssh.next = session->ti.libssh.next;
788 }
Michal Vasko33476c32022-09-09 11:21:40 +0200789 }
790
791 /* SESSION IO UNLOCK */
792 if (r == 1) {
793 nc_session_io_unlock(session, __func__);
794 }
795 break;
Michal Vaskoe44f2702022-12-12 07:58:06 +0100796 }
Michal Vasko33476c32022-09-09 11:21:40 +0200797 case NC_TI_OPENSSL:
roman9ba26aa2024-04-05 12:31:54 +0200798 sock = nc_tls_get_fd_wrap(session);
Michal Vasko33476c32022-09-09 11:21:40 +0200799
800 if (connected) {
roman9ba26aa2024-04-05 12:31:54 +0200801 /* notify the peer that we're shutting down */
802 nc_tls_close_notify_wrap(session->ti.tls.session);
Michal Vasko33476c32022-09-09 11:21:40 +0200803 }
roman9ba26aa2024-04-05 12:31:54 +0200804
805 nc_tls_ctx_destroy_wrap(&session->ti.tls.ctx);
806 nc_tls_session_destroy_wrap(session->ti.tls.session);
807 nc_tls_config_destroy_wrap(session->ti.tls.config);
Michal Vasko33476c32022-09-09 11:21:40 +0200808
809 if (session->side == NC_SERVER) {
roman9ba26aa2024-04-05 12:31:54 +0200810 // TODO
811 nc_tls_cert_destroy_wrap(session->opts.server.client_cert);
Michal Vasko33476c32022-09-09 11:21:40 +0200812 }
roman9ba26aa2024-04-05 12:31:54 +0200813
Michal Vasko33476c32022-09-09 11:21:40 +0200814 break;
roman2eab4742023-06-06 10:00:26 +0200815#endif /* NC_ENABLED_SSH_TLS */
Michal Vasko33476c32022-09-09 11:21:40 +0200816 case NC_TI_NONE:
817 break;
818 }
819
820 /* close socket separately */
821 if (sock > -1) {
822 close(sock);
823 }
824}
825
Radek Krejci695d4fa2015-10-22 13:23:54 +0200826API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100827nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200828{
Michal Vasko33476c32022-09-09 11:21:40 +0200829 int r, i, rpc_locked = 0, msgs_locked = 0, timeout;
Michal Vaskob48aa812016-01-18 14:13:09 +0100830 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoad611702015-12-03 13:41:51 +0100831 struct nc_msg_cont *contiter;
Michal Vasko77367452021-02-16 16:32:18 +0100832 struct ly_in *msg;
roman6ece9c52022-06-22 09:29:17 +0200833 struct timespec ts;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200834 void *p;
835
Michal Vasko428087d2016-01-14 16:04:28 +0100836 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200837 return;
838 }
839
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200840 /* stop notification threads if any */
841 if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running)) {
842 /* let the threads know they should quit */
843 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 0);
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200844
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200845 /* wait for them */
Michal Vaskod8a74192023-02-06 15:51:50 +0100846 nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200847 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_count)) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200848 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +0100849 if (nc_timeouttime_cur_diff(&ts) < 1) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200850 ERR(session, "Waiting for notification thread exit failed (timed out).");
851 break;
852 }
853 }
Michal Vasko86d357c2016-03-11 13:46:38 +0100854 }
855
Michal Vaskoacf98472021-02-04 15:33:57 +0100856 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200857 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100858 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100859 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200860 } else if (r) {
861 rpc_locked = 1;
Michal Vasko96a28a32021-02-04 15:35:20 +0100862 } else {
863 /* else failed to lock it, too bad */
Michal Vasko05532772021-06-03 12:12:38 +0200864 ERR(session, "Freeing a session while an RPC is being processed.");
Michal Vasko96a28a32021-02-04 15:35:20 +0100865 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200866 }
867
Michal Vasko8c247822020-09-07 13:23:23 +0200868 if (session->side == NC_CLIENT) {
Michal Vasko01130bd2021-08-26 11:47:38 +0200869 timeout = NC_SESSION_FREE_LOCK_TIMEOUT;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200870
Michal Vasko01130bd2021-08-26 11:47:38 +0200871 /* MSGS LOCK */
872 r = nc_session_client_msgs_lock(session, &timeout, __func__);
873 if (r == -1) {
874 return;
875 } else if (r) {
876 msgs_locked = 1;
877 } else {
878 /* else failed to lock it, too bad */
879 ERR(session, "Freeing a session while messages are being received.");
880 }
881
882 /* cleanup message queue */
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200883 for (contiter = session->opts.client.msgs; contiter; ) {
Michal Vasko77367452021-02-16 16:32:18 +0100884 ly_in_free(contiter->msg, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200885
Michal Vaskoad611702015-12-03 13:41:51 +0100886 p = contiter;
887 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200888 free(p);
889 }
890
Michal Vasko01130bd2021-08-26 11:47:38 +0200891 if (msgs_locked) {
892 /* MSGS UNLOCK */
893 nc_session_client_msgs_unlock(session, __func__);
894 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200895
Michal Vasko8c247822020-09-07 13:23:23 +0200896 if (session->status == NC_STATUS_RUNNING) {
Michal Vasko9c6d38c2021-09-03 13:02:53 +0200897 /* receive any leftover messages */
898 while (nc_read_msg_poll_io(session, 0, &msg) == 1) {
899 ly_in_free(msg, 1);
900 }
901
Michal Vasko8c247822020-09-07 13:23:23 +0200902 /* send closing info to the other side */
Michal Vaskod4da3632022-05-25 11:49:10 +0200903 nc_session_free_close_session(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200904 }
905
906 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200907 if (session->opts.client.cpblts) {
908 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200909 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200910 }
Michal Vasko2e6defd2016-10-07 15:48:15 +0200911 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200912 }
Michal Vasko78939072022-12-12 07:43:18 +0100913
914 /* LY ext data */
roman2eab4742023-06-06 10:00:26 +0200915#ifdef NC_ENABLED_SSH_TLS
Michal Vaskoe44f2702022-12-12 07:58:06 +0100916 struct nc_session *siter;
917
Michal Vasko78939072022-12-12 07:43:18 +0100918 if ((session->flags & NC_SESSION_SHAREDCTX) && session->ti.libssh.next) {
919 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
920 if (siter->status != NC_STATUS_STARTING) {
921 /* move LY ext data to this session */
922 assert(!siter->opts.client.ext_data);
923 siter->opts.client.ext_data = session->opts.client.ext_data;
924 session->opts.client.ext_data = NULL;
925 break;
926 }
927 }
Michal Vaskoe44f2702022-12-12 07:58:06 +0100928 } else
roman2eab4742023-06-06 10:00:26 +0200929#endif /* NC_ENABLED_SSH_TLS */
Michal Vaskoe44f2702022-12-12 07:58:06 +0100930 {
Michal Vasko78939072022-12-12 07:43:18 +0100931 lyd_free_siblings(session->opts.client.ext_data);
932 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200933 }
934
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100935 if (session->data && data_free) {
936 data_free(session->data);
937 }
938
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200939 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
940 /* CH LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100941 pthread_mutex_lock(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200942 }
943
Michal Vasko86d357c2016-03-11 13:46:38 +0100944 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +0200945 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200946
Michal Vaskofeccb312022-03-24 15:24:59 +0100947 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100948 pthread_cond_signal(&session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200949
Michal Vaskod8a74192023-02-06 15:51:50 +0100950 nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vasko0db3db52021-03-03 10:45:42 +0100951
952 /* wait for CH thread to actually wake up and terminate */
953 r = 0;
Michal Vaskofeccb312022-03-24 15:24:59 +0100954 while (!r && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100955 r = pthread_cond_clockwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, COMPAT_CLOCK_ID, &ts);
Michal Vasko0db3db52021-03-03 10:45:42 +0100956 }
Michal Vasko0db3db52021-03-03 10:45:42 +0100957 if (r) {
Michal Vasko05532772021-06-03 12:12:38 +0200958 ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r));
Michal Vasko3f05a092018-03-13 10:39:49 +0100959 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200960 }
961
Michal Vaskofeccb312022-03-24 15:24:59 +0100962 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
963 /* CH UNLOCK */
964 pthread_mutex_unlock(&session->opts.server.ch_lock);
965 }
966
Radek Krejci695d4fa2015-10-22 13:23:54 +0200967 /* transport implementation cleanup */
Michal Vasko33476c32022-09-09 11:21:40 +0200968 nc_session_free_transport(session, &multisession);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200969
Michal Vasko33476c32022-09-09 11:21:40 +0200970 /* final cleanup */
Michal Vasko93224072021-11-09 12:14:28 +0100971 free(session->username);
972 free(session->host);
973 free(session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200974
Michal Vaskoacf98472021-02-04 15:33:57 +0100975 if (session->side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200976 pthread_mutex_destroy(&session->opts.server.ntf_status_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200977 if (rpc_locked) {
978 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +0100979 }
Michal Vaskoacf98472021-02-04 15:33:57 +0100980 pthread_mutex_destroy(&session->opts.server.rpc_lock);
981 pthread_cond_destroy(&session->opts.server.rpc_cond);
Michal Vasko131120a2018-05-29 15:44:02 +0200982 }
983
984 if (session->io_lock && !multisession) {
985 pthread_mutex_destroy(session->io_lock);
986 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200987 }
988
989 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko93224072021-11-09 12:14:28 +0100990 ly_ctx_destroy((struct ly_ctx *)session->ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200991 }
992
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200993 if (session->side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100994 /* free CH synchronization structures */
995 pthread_cond_destroy(&session->opts.server.ch_cond);
996 pthread_mutex_destroy(&session->opts.server.ch_lock);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200997 } else {
998 pthread_mutex_destroy(&session->opts.client.msgs_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +0200999 }
1000
Radek Krejci695d4fa2015-10-22 13:23:54 +02001001 free(session);
1002}
1003
Michal Vasko086311b2016-01-08 09:53:11 +01001004static void
Michal Vasko93224072021-11-09 12:14:28 +01001005add_cpblt(const char *capab, char ***cpblts, int *size, int *count)
Michal Vasko086311b2016-01-08 09:53:11 +01001006{
Radek Krejci658782b2016-12-04 22:04:55 +01001007 size_t len;
1008 int i;
1009 char *p;
1010
1011 if (capab) {
1012 /* check if already present */
1013 p = strchr(capab, '?');
1014 if (p) {
1015 len = p - capab;
1016 } else {
1017 len = strlen(capab);
1018 }
1019 for (i = 0; i < *count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001020 if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) {
Radek Krejci658782b2016-12-04 22:04:55 +01001021 /* already present, do not duplicate it */
1022 return;
1023 }
1024 }
1025 }
1026
1027 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +01001028 if (*count == *size) {
1029 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001030 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
1031 if (!(*cpblts)) {
1032 ERRMEM;
1033 return;
1034 }
Michal Vasko086311b2016-01-08 09:53:11 +01001035 }
1036
Michal Vasko93224072021-11-09 12:14:28 +01001037 (*cpblts)[*count] = capab ? strdup(capab) : NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001038 ++(*count);
1039}
1040
Michal Vasko93224072021-11-09 12:14:28 +01001041API char **
1042nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +01001043{
Michal Vasko93224072021-11-09 12:14:28 +01001044 char **cpblts;
Michal Vasko77367452021-02-16 16:32:18 +01001045 const struct lys_module *mod;
1046 struct lysp_feature *feat;
1047 int size = 10, count, features_count = 0, dev_count = 0, str_len, len;
Michal Vasko1440a742021-03-31 11:11:03 +02001048 uint32_t i, u;
Michal Vasko77367452021-02-16 16:32:18 +01001049 LY_ARRAY_COUNT_TYPE v;
Michal Vasko1440a742021-03-31 11:11:03 +02001050 char *yl_content_id;
romanc1d2b092023-02-02 08:58:27 +01001051 uint32_t wd_also_supported;
1052 uint32_t wd_basic_mode;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001053
Radek Krejci24a18412018-05-16 15:09:10 +02001054#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +02001055 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001056
roman40672412023-05-04 11:10:22 +02001057 NC_CHECK_ARG_RET(NULL, ctx, NULL);
Michal Vasko4ffa3b22016-05-24 16:36:25 +02001058
Michal Vasko086311b2016-01-08 09:53:11 +01001059 cpblts = malloc(size * sizeof *cpblts);
roman124a4362023-10-26 15:36:22 +02001060 NC_CHECK_ERRMEM_GOTO(!cpblts, , error);
Michal Vasko93224072021-11-09 12:14:28 +01001061 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1062 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko086311b2016-01-08 09:53:11 +01001063 count = 2;
1064
1065 /* capabilities */
1066
Michal Vasko77367452021-02-16 16:32:18 +01001067 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf");
Michal Vasko086311b2016-01-08 09:53:11 +01001068 if (mod) {
Michal Vasko77367452021-02-16 16:32:18 +01001069 if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001070 add_cpblt("urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001071 }
Michal Vasko77367452021-02-16 16:32:18 +01001072 if (lys_feature_value(mod, "candidate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001073 add_cpblt("urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko77367452021-02-16 16:32:18 +01001074 if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001075 add_cpblt("urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001076 }
1077 }
Michal Vasko77367452021-02-16 16:32:18 +01001078 if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001079 add_cpblt("urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001080 }
Michal Vasko77367452021-02-16 16:32:18 +01001081 if (lys_feature_value(mod, "validate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001082 add_cpblt("urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001083 }
Michal Vasko77367452021-02-16 16:32:18 +01001084 if (lys_feature_value(mod, "startup") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001085 add_cpblt("urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001086 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001087
1088 /* The URL capability must be set manually using nc_server_set_capability()
1089 * because of the need for supported protocols to be included.
1090 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1091 */
Michal Vasko77367452021-02-16 16:32:18 +01001092 // if (lys_feature_value(mod, "url") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001093 // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
mekleoa8de5e92020-02-13 09:05:56 +01001094 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001095
Michal Vasko77367452021-02-16 16:32:18 +01001096 if (lys_feature_value(mod, "xpath") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001097 add_cpblt("urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001098 }
1099 }
1100
Michal Vasko77367452021-02-16 16:32:18 +01001101 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01001102 if (mod) {
romanc1d2b092023-02-02 08:58:27 +01001103 wd_basic_mode = ATOMIC_LOAD_RELAXED(server_opts.wd_basic_mode);
1104 if (!wd_basic_mode) {
Michal Vasko05532772021-06-03 12:12:38 +02001105 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 +01001106 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001107 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
romanc1d2b092023-02-02 08:58:27 +01001108 switch (wd_basic_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01001109 case NC_WD_ALL:
1110 strcat(str, "?basic-mode=report-all");
1111 break;
1112 case NC_WD_TRIM:
1113 strcat(str, "?basic-mode=trim");
1114 break;
1115 case NC_WD_EXPLICIT:
1116 strcat(str, "?basic-mode=explicit");
1117 break;
1118 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001119 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001120 break;
1121 }
1122
romanc1d2b092023-02-02 08:58:27 +01001123 wd_also_supported = ATOMIC_LOAD_RELAXED(server_opts.wd_also_supported);
1124 if (wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001125 strcat(str, "&also-supported=");
romanc1d2b092023-02-02 08:58:27 +01001126 if (wd_also_supported & NC_WD_ALL) {
Michal Vasko086311b2016-01-08 09:53:11 +01001127 strcat(str, "report-all,");
1128 }
romanc1d2b092023-02-02 08:58:27 +01001129 if (wd_also_supported & NC_WD_ALL_TAG) {
Michal Vasko086311b2016-01-08 09:53:11 +01001130 strcat(str, "report-all-tagged,");
1131 }
romanc1d2b092023-02-02 08:58:27 +01001132 if (wd_also_supported & NC_WD_TRIM) {
Michal Vasko086311b2016-01-08 09:53:11 +01001133 strcat(str, "trim,");
1134 }
romanc1d2b092023-02-02 08:58:27 +01001135 if (wd_also_supported & NC_WD_EXPLICIT) {
Michal Vasko086311b2016-01-08 09:53:11 +01001136 strcat(str, "explicit,");
1137 }
1138 str[strlen(str) - 1] = '\0';
1139
Michal Vasko93224072021-11-09 12:14:28 +01001140 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001141 }
1142 }
1143 }
1144
Radek Krejci658782b2016-12-04 22:04:55 +01001145 /* other capabilities */
1146 for (u = 0; u < server_opts.capabilities_count; u++) {
Michal Vasko93224072021-11-09 12:14:28 +01001147 add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001148 }
1149
1150 /* models */
Michal Vasko1440a742021-03-31 11:11:03 +02001151 u = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001152 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001153 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vasko77367452021-02-16 16:32:18 +01001154 if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) {
Michal Vasko05532772021-06-03 12:12:38 +02001155 ERR(NULL, "Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
Michal Vaskod5ada122020-03-19 18:28:06 +01001156 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001157 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001158
Michal Vasko1440a742021-03-31 11:11:03 +02001159 /* get content-id */
1160 if (server_opts.content_id_clb) {
1161 yl_content_id = server_opts.content_id_clb(server_opts.content_id_data);
roman124a4362023-10-26 15:36:22 +02001162 NC_CHECK_ERRMEM_GOTO(!yl_content_id, , error);
Michal Vasko1440a742021-03-31 11:11:03 +02001163 } else {
1164 yl_content_id = malloc(11);
roman124a4362023-10-26 15:36:22 +02001165 NC_CHECK_ERRMEM_GOTO(!yl_content_id, , error);
Michal Vasko1440a742021-03-31 11:11:03 +02001166 sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx));
1167 }
1168
Michal Vasko77367452021-02-16 16:32:18 +01001169 if (!strcmp(mod->revision, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001170 /* new one (capab defined in RFC 8526 section 2) */
Michal Vasko1440a742021-03-31 11:11:03 +02001171 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s",
1172 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001173 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001174 } else {
1175 /* old one (capab defined in RFC 7950 section 5.6.4) */
Michal Vasko1440a742021-03-31 11:11:03 +02001176 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s",
1177 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001178 add_cpblt(str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001179 }
Michal Vasko1440a742021-03-31 11:11:03 +02001180 free(yl_content_id);
Radek Krejci24a18412018-05-16 15:09:10 +02001181 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001182 } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001183 /* skip YANG 1.1 modules */
Radek Krejci24a18412018-05-16 15:09:10 +02001184 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001185 } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001186 /* skip YANG 1.0 modules */
Radek Krejci24a18412018-05-16 15:09:10 +02001187 continue;
1188 }
Michal Vasko086311b2016-01-08 09:53:11 +01001189
Michal Vasko77367452021-02-16 16:32:18 +01001190 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "",
1191 mod->revision ? mod->revision : "");
Radek Krejci24a18412018-05-16 15:09:10 +02001192
Michal Vaskodafdc742020-03-11 16:15:59 +01001193 features_count = 0;
1194 i = 0;
1195 feat = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001196 while ((feat = lysp_feature_next(feat, mod->parsed, &i))) {
Michal Vaskodafdc742020-03-11 16:15:59 +01001197 if (!(feat->flags & LYS_FENABLED)) {
1198 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001199 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001200 if (!features_count) {
1201 strcat(str, "&features=");
1202 str_len += 10;
1203 }
1204 len = strlen(feat->name);
1205 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1206 ERRINT;
1207 break;
1208 }
1209 if (features_count) {
1210 strcat(str, ",");
1211 ++str_len;
1212 }
1213 strcat(str, feat->name);
1214 str_len += len;
1215 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001216 }
Michal Vasko086311b2016-01-08 09:53:11 +01001217
Michal Vasko77367452021-02-16 16:32:18 +01001218 if (mod->deviated_by) {
Radek Krejci24a18412018-05-16 15:09:10 +02001219 strcat(str, "&deviations=");
1220 str_len += 12;
1221 dev_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +01001222 LY_ARRAY_FOR(mod->deviated_by, v) {
1223 len = strlen(mod->deviated_by[v]->name);
1224 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1225 ERRINT;
1226 break;
Radek Krejci24a18412018-05-16 15:09:10 +02001227 }
Michal Vasko77367452021-02-16 16:32:18 +01001228 if (dev_count) {
1229 strcat(str, ",");
1230 ++str_len;
Radek Krejci24a18412018-05-16 15:09:10 +02001231 }
Michal Vasko77367452021-02-16 16:32:18 +01001232 strcat(str, mod->deviated_by[v]->name);
1233 str_len += len;
1234 dev_count++;
Radek Krejci24a18412018-05-16 15:09:10 +02001235 }
1236 }
1237
Michal Vasko93224072021-11-09 12:14:28 +01001238 add_cpblt(str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +02001239 }
Michal Vasko086311b2016-01-08 09:53:11 +01001240
1241 /* ending NULL capability */
Michal Vasko93224072021-11-09 12:14:28 +01001242 add_cpblt(NULL, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001243
1244 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001245
1246error:
Radek Krejcif906e412017-09-22 14:44:45 +02001247 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001248 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001249}
1250
Michal Vasko93224072021-11-09 12:14:28 +01001251API char **
1252nc_server_get_cpblts(const struct ly_ctx *ctx)
Radek Krejci24a18412018-05-16 15:09:10 +02001253{
1254 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1255}
1256
Radek Krejci695d4fa2015-10-22 13:23:54 +02001257static int
Michal Vasko77367452021-02-16 16:32:18 +01001258parse_cpblts(struct lyd_node *capabilities, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001259{
Michal Vasko77367452021-02-16 16:32:18 +01001260 struct lyd_node *iter;
1261 struct lyd_node_opaq *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001262 int ver = -1, i = 0;
1263 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001264
1265 if (list) {
1266 /* get the storage for server's capabilities */
Michal Vasko77367452021-02-16 16:32:18 +01001267 LY_LIST_FOR(lyd_child(capabilities), iter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001268 i++;
1269 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001270 /* last item remains NULL */
1271 *list = calloc(i + 1, sizeof **list);
roman3a95bb22023-10-26 11:07:17 +02001272 NC_CHECK_ERRMEM_RET(!*list, -1);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001273 i = 0;
1274 }
1275
Michal Vasko77367452021-02-16 16:32:18 +01001276 LY_LIST_FOR(lyd_child(capabilities), iter) {
1277 cpblt = (struct lyd_node_opaq *)iter;
1278
1279 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 +02001280 ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001281 return -1;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001282 }
1283
Michal Vasko156d3272017-04-11 11:46:49 +02001284 /* skip leading/trailing whitespaces */
Michal Vasko77367452021-02-16 16:32:18 +01001285 for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {}
1286 for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {}
1287 if (!cpb_start[0] || (cpb_end == cpblt->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001288 ERR(NULL, "Empty capability \"%s\" received.", cpblt->value);
Michal Vasko156d3272017-04-11 11:46:49 +02001289 return -1;
1290 }
1291
Radek Krejci695d4fa2015-10-22 13:23:54 +02001292 /* detect NETCONF version */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001293 if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001294 ver = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001295 } 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 +02001296 ver = 1;
1297 }
1298
1299 /* store capabilities */
1300 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001301 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
roman3a95bb22023-10-26 11:07:17 +02001302 NC_CHECK_ERRMEM_RET(!(*list)[i], -1);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001303 i++;
1304 }
1305 }
1306
1307 if (ver == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001308 ERR(NULL, "Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001309 }
1310
1311 return ver;
1312}
1313
1314static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001315nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001316{
Michal Vasko131120a2018-05-29 15:44:02 +02001317 NC_MSG_TYPE ret;
Michal Vaskocf898172024-01-15 15:04:28 +01001318 int i, timeout_io;
Michal Vasko93224072021-11-09 12:14:28 +01001319 char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001320 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001321
Michal Vasko131120a2018-05-29 15:44:02 +02001322 if (session->side == NC_CLIENT) {
1323 /* client side hello - send only NETCONF base capabilities */
1324 cpblts = malloc(3 * sizeof *cpblts);
roman3a95bb22023-10-26 11:07:17 +02001325 NC_CHECK_ERRMEM_RET(!cpblts, NC_MSG_ERROR);
Michal Vasko93224072021-11-09 12:14:28 +01001326 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1327 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko131120a2018-05-29 15:44:02 +02001328 cpblts[2] = NULL;
1329
Michal Vaskocf898172024-01-15 15:04:28 +01001330 timeout_io = NC_CLIENT_HELLO_TIMEOUT * 1000;
Michal Vasko131120a2018-05-29 15:44:02 +02001331 sid = NULL;
1332 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001333 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0);
Michal Vasko5b24b6b2020-12-04 09:29:47 +01001334 if (!cpblts) {
1335 return NC_MSG_ERROR;
1336 }
Michal Vasko131120a2018-05-29 15:44:02 +02001337
Michal Vaskocf898172024-01-15 15:04:28 +01001338 if (session->flags & NC_SESSION_CALLHOME) {
1339 timeout_io = NC_SERVER_CH_HELLO_TIMEOUT * 1000;
1340 } else {
Michal Vasko5c1c9b32024-02-02 09:02:42 +01001341 timeout_io = server_opts.idle_timeout ? server_opts.idle_timeout * 1000 : -1;
Michal Vaskocf898172024-01-15 15:04:28 +01001342 }
Michal Vasko131120a2018-05-29 15:44:02 +02001343 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001344 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001345
Michal Vaskocf898172024-01-15 15:04:28 +01001346 ret = nc_write_msg_io(session, timeout_io, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001347
Michal Vasko086311b2016-01-08 09:53:11 +01001348 for (i = 0; cpblts[i]; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001349 free(cpblts[i]);
Michal Vasko086311b2016-01-08 09:53:11 +01001350 }
1351 free(cpblts);
1352
Michal Vasko131120a2018-05-29 15:44:02 +02001353 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001354}
1355
1356static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001357nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001358{
Michal Vasko77367452021-02-16 16:32:18 +01001359 struct ly_in *msg;
1360 struct lyd_node *hello = NULL, *iter;
1361 struct lyd_node_opaq *node;
1362 int r, ver = -1, flag = 0;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001363 char *str;
Michal Vasko292c5542023-02-01 14:33:17 +01001364 long long id;
Michal Vasko77367452021-02-16 16:32:18 +01001365 NC_MSG_TYPE rc = NC_MSG_HELLO;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001366
Michal Vasko77367452021-02-16 16:32:18 +01001367 r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg);
1368 switch (r) {
1369 case 1:
Radek Krejci695d4fa2015-10-22 13:23:54 +02001370 /* parse <hello> data */
Michal Vasko77367452021-02-16 16:32:18 +01001371 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 +02001372 ERR(session, "Failed to parse server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001373 rc = NC_MSG_ERROR;
1374 goto cleanup;
1375 }
1376
1377 LY_LIST_FOR(lyd_child(hello), iter) {
1378 node = (struct lyd_node_opaq *)iter;
1379
1380 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001381 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001382 } else if (!strcmp(node->name.name, "session-id")) {
1383 if (!node->value || !strlen(node->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001384 ERR(session, "No value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001385 rc = NC_MSG_ERROR;
1386 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001387 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001388 str = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001389 id = strtoll(node->value, &str, 10);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001390 if (*str || (id < 1) || (id > UINT32_MAX)) {
Michal Vasko05532772021-06-03 12:12:38 +02001391 ERR(session, "Invalid value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001392 rc = NC_MSG_ERROR;
1393 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001394 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001395 session->id = (uint32_t)id;
1396 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001397 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001398 ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001399 rc = NC_MSG_ERROR;
1400 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001401 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001402
1403 if (flag) {
1404 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001405 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001406 rc = NC_MSG_ERROR;
1407 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001408 }
1409 flag = 1;
1410
Michal Vasko77367452021-02-16 16:32:18 +01001411 if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) {
1412 rc = NC_MSG_ERROR;
1413 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001414 }
1415 session->version = ver;
1416 }
1417
1418 if (!session->id) {
Michal Vasko05532772021-06-03 12:12:38 +02001419 ERR(session, "Missing <session-id> in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001420 rc = NC_MSG_ERROR;
1421 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001422 }
1423 break;
Michal Vasko77367452021-02-16 16:32:18 +01001424 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001425 ERR(session, "Server <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001426 rc = NC_MSG_WOULDBLOCK;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001427 break;
1428 default:
Michal Vasko77367452021-02-16 16:32:18 +01001429 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001430 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001431 }
1432
Michal Vasko77367452021-02-16 16:32:18 +01001433cleanup:
1434 ly_in_free(msg, 1);
1435 lyd_free_tree(hello);
1436 return rc;
Radek Krejci5686ff72015-10-09 13:33:56 +02001437}
1438
Michal Vasko11d142a2016-01-19 15:58:24 +01001439static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001440nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001441{
Michal Vasko77367452021-02-16 16:32:18 +01001442 struct ly_in *msg;
1443 struct lyd_node *hello = NULL, *iter;
1444 struct lyd_node_opaq *node;
1445 NC_MSG_TYPE rc = NC_MSG_HELLO;
1446 int r, ver = -1, flag = 0, timeout_io;
Michal Vasko11d142a2016-01-19 15:58:24 +01001447
Michal Vaskocf898172024-01-15 15:04:28 +01001448 if (session->flags & NC_SESSION_CALLHOME) {
1449 timeout_io = NC_SERVER_CH_HELLO_TIMEOUT * 1000;
1450 } else {
Michal Vasko5c1c9b32024-02-02 09:02:42 +01001451 timeout_io = server_opts.idle_timeout ? server_opts.idle_timeout * 1000 : -1;
Michal Vaskocf898172024-01-15 15:04:28 +01001452 }
1453
Michal Vasko77367452021-02-16 16:32:18 +01001454 r = nc_read_msg_poll_io(session, timeout_io, &msg);
1455 switch (r) {
1456 case 1:
1457 /* parse <hello> data */
1458 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 +02001459 ERR(session, "Failed to parse client <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001460 rc = NC_MSG_ERROR;
1461 goto cleanup;
1462 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001463
Michal Vasko77367452021-02-16 16:32:18 +01001464 /* learn NETCONF version */
1465 LY_LIST_FOR(lyd_child(hello), iter) {
1466 node = (struct lyd_node_opaq *)iter;
1467
1468 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001469 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001470 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001471 ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001472 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001473 goto cleanup;
1474 }
1475
1476 if (flag) {
1477 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001478 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001479 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001480 goto cleanup;
1481 }
1482 flag = 1;
1483
Michal Vasko77367452021-02-16 16:32:18 +01001484 if ((ver = parse_cpblts(&node->node, NULL)) < 0) {
1485 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001486 goto cleanup;
1487 }
1488 session->version = ver;
1489 }
1490 break;
Michal Vasko77367452021-02-16 16:32:18 +01001491 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001492 ERR(session, "Client <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001493 rc = NC_MSG_WOULDBLOCK;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001494 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001495 default:
Michal Vasko77367452021-02-16 16:32:18 +01001496 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001497 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001498 }
1499
1500cleanup:
Michal Vasko77367452021-02-16 16:32:18 +01001501 ly_in_free(msg, 1);
1502 lyd_free_tree(hello);
1503 return rc;
Michal Vasko11d142a2016-01-19 15:58:24 +01001504}
1505
Michal Vasko71090fc2016-05-24 16:37:28 +02001506NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001507nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001508{
Michal Vasko086311b2016-01-08 09:53:11 +01001509 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001510
Michal Vasko131120a2018-05-29 15:44:02 +02001511 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001512 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001513 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001514 }
1515
Michal Vasko11d142a2016-01-19 15:58:24 +01001516 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001517 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001518 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001519 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001520 }
1521
Michal Vasko71090fc2016-05-24 16:37:28 +02001522 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001523}