blob: 90751dc5c320a5be477e070b96c31bc5908f07d6 [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>
roman13145912023-08-17 15:36:54 +020041#include <openssl/bio.h>
roman2eab4742023-06-06 10:00:26 +020042#include <openssl/conf.h>
43#include <openssl/err.h>
roman51a1e192023-09-14 10:13:45 +020044#include <openssl/evp.h>
45#include <openssl/x509.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020046
roman2eab4742023-06-06 10:00:26 +020047#endif /* NC_ENABLED_SSH_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
Michal Vaskocf898172024-01-15 15:04:28 +010051#define NC_SERVER_CH_HELLO_TIMEOUT 180
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
Michal Vaskod8a74192023-02-06 15:51:50 +010058void
59nc_timeouttime_get(struct timespec *ts, uint32_t add_ms)
Radek Krejci7ac16052016-07-15 11:48:18 +020060{
Michal Vaskod8a74192023-02-06 15:51:50 +010061 if (clock_gettime(COMPAT_CLOCK_ID, ts) == -1) {
62 ERR(NULL, "clock_gettime() failed (%s).", strerror(errno));
63 return;
64 }
65
66 if (!add_ms) {
67 return;
68 }
69
70 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
71
72 ts->tv_sec += add_ms / 1000;
73 ts->tv_nsec += (add_ms % 1000) * 1000000L;
74
75 if (ts->tv_nsec >= 1000000000L) {
76 ++ts->tv_sec;
77 ts->tv_nsec -= 1000000000L;
78 } else if (ts->tv_nsec < 0) {
79 --ts->tv_sec;
80 ts->tv_nsec += 1000000000L;
81 }
82
83 assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
84}
85
86int32_t
87nc_timeouttime_cur_diff(const struct timespec *ts)
88{
89 struct timespec cur;
90 int64_t nsec_diff = 0;
91
92 nc_timeouttime_get(&cur, 0);
93
94 nsec_diff += (((int64_t)ts->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
95 nsec_diff += ((int64_t)ts->tv_nsec) - ((int64_t)cur.tv_nsec);
96
97 return nsec_diff / 1000000L;
98}
99
100void
101nc_realtime_get(struct timespec *ts)
102{
roman6ece9c52022-06-22 09:29:17 +0200103 if (clock_gettime(CLOCK_REALTIME, ts)) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100104 ERR(NULL, "clock_gettime() failed (%s).", strerror(errno));
105 return;
roman6ece9c52022-06-22 09:29:17 +0200106 }
Michal Vasko36c7be82017-02-22 13:37:59 +0100107}
108
Michal Vasko63b92d62024-04-29 10:04:56 +0200109int
110nc_poll(struct pollfd *pfd, uint16_t pfd_count, int timeout_ms)
111{
112 int rc;
113 struct timespec start_ts;
114
115 if (timeout_ms > 0) {
116 /* get current time */
117 nc_timeouttime_get(&start_ts, 0);
118 }
119
120 do {
121 /* poll */
122 rc = poll(pfd, pfd_count, timeout_ms);
123
124 if (timeout_ms > 0) {
125 /* adjust the timeout by subtracting the elapsed time (relevant in case of EINTR) */
126 timeout_ms += nc_timeouttime_cur_diff(&start_ts);
127 if (timeout_ms < 0) {
128 /* manual timeout */
129 rc = 0;
130 errno = 0;
131 break;
132 }
133 }
134 } while ((rc == -1) && (errno == EINTR));
135
136 if (rc == -1) {
137 ERR(NULL, "Poll failed (%s).", strerror(errno));
138 }
139 return rc;
140}
141
roman2eab4742023-06-06 10:00:26 +0200142#ifdef NC_ENABLED_SSH_TLS
143
Michal Vaskoddce1212019-05-24 09:58:49 +0200144const char *
roman3f9b65c2023-06-05 14:26:58 +0200145nc_privkey_format_to_str(NC_PRIVKEY_FORMAT format)
Michal Vaskoddce1212019-05-24 09:58:49 +0200146{
roman3f9b65c2023-06-05 14:26:58 +0200147 switch (format) {
148 case NC_PRIVKEY_FORMAT_RSA:
romand0fe5952024-03-21 15:59:33 +0100149 return " RSA ";
roman3f9b65c2023-06-05 14:26:58 +0200150 case NC_PRIVKEY_FORMAT_EC:
romand0fe5952024-03-21 15:59:33 +0100151 return " EC ";
roman3f9b65c2023-06-05 14:26:58 +0200152 case NC_PRIVKEY_FORMAT_X509:
romand0fe5952024-03-21 15:59:33 +0100153 return " ";
roman3f9b65c2023-06-05 14:26:58 +0200154 case NC_PRIVKEY_FORMAT_OPENSSH:
romand0fe5952024-03-21 15:59:33 +0100155 return " OPENSSH ";
Michal Vaskoddce1212019-05-24 09:58:49 +0200156 default:
roman3f9b65c2023-06-05 14:26:58 +0200157 return NULL;
Michal Vaskoddce1212019-05-24 09:58:49 +0200158 }
Michal Vaskoddce1212019-05-24 09:58:49 +0200159}
160
roman13145912023-08-17 15:36:54 +0200161int
162nc_base64_to_bin(const char *base64, char **bin)
163{
roman46172ac2023-11-08 15:12:40 +0100164 BIO *bio, *bio64 = NULL;
roman13145912023-08-17 15:36:54 +0200165 size_t used = 0, size = 0, r = 0;
166 void *tmp = NULL;
roman46172ac2023-11-08 15:12:40 +0100167 int nl_count, i, remainder, ret = 0;
roman13145912023-08-17 15:36:54 +0200168 char *b64;
169
170 /* insert new lines into the base64 string, so BIO_read works correctly */
171 nl_count = strlen(base64) / 64;
172 remainder = strlen(base64) - 64 * nl_count;
173 b64 = calloc(strlen(base64) + nl_count + 1, 1);
roman3a95bb22023-10-26 11:07:17 +0200174 NC_CHECK_ERRMEM_RET(!b64, -1);
roman13145912023-08-17 15:36:54 +0200175
176 for (i = 0; i < nl_count; i++) {
177 /* copy 64 bytes and add a NL */
178 strncpy(b64 + i * 65, base64 + i * 64, 64);
179 b64[i * 65 + 64] = '\n';
180 }
181
182 /* copy the rest */
183 strncpy(b64 + i * 65, base64 + i * 64, remainder);
184
185 bio64 = BIO_new(BIO_f_base64());
186 if (!bio64) {
187 ERR(NULL, "Error creating a bio (%s).", ERR_reason_error_string(ERR_get_error()));
roman46172ac2023-11-08 15:12:40 +0100188 ret = -1;
189 goto cleanup;
roman13145912023-08-17 15:36:54 +0200190 }
191
192 bio = BIO_new_mem_buf(b64, strlen(b64));
193 if (!bio) {
194 ERR(NULL, "Error creating a bio (%s).", ERR_reason_error_string(ERR_get_error()));
roman46172ac2023-11-08 15:12:40 +0100195 ret = -1;
196 goto cleanup;
roman13145912023-08-17 15:36:54 +0200197 }
198
199 BIO_push(bio64, bio);
200
201 /* store the decoded base64 in bin */
202 *bin = NULL;
203 do {
204 size += 64;
205
206 tmp = realloc(*bin, size);
207 if (!tmp) {
roman46172ac2023-11-08 15:12:40 +0100208 ERRMEM;
roman13145912023-08-17 15:36:54 +0200209 free(*bin);
roman46172ac2023-11-08 15:12:40 +0100210 *bin = NULL;
211 ret = -1;
212 goto cleanup;
roman13145912023-08-17 15:36:54 +0200213 }
214 *bin = tmp;
215
216 r = BIO_read(bio64, *bin + used, 64);
217 used += r;
218 } while (r == 64);
219
roman46172ac2023-11-08 15:12:40 +0100220 ret = size;
221
222cleanup:
roman13145912023-08-17 15:36:54 +0200223 free(b64);
224 BIO_free_all(bio64);
roman46172ac2023-11-08 15:12:40 +0100225 return ret;
roman13145912023-08-17 15:36:54 +0200226}
227
roman51a1e192023-09-14 10:13:45 +0200228int
229nc_is_pk_subject_public_key_info(const char *b64)
230{
231 int ret = 0;
232 long len;
233 char *bin = NULL, *tmp;
234 EVP_PKEY *pkey = NULL;
235
236 /* base64 2 binary */
237 len = nc_base64_to_bin(b64, &bin);
238 if (len == -1) {
239 ERR(NULL, "Decoding base64 public key to binary failed.");
240 ret = -1;
241 goto cleanup;
242 }
243
244 /* for deallocation later */
245 tmp = bin;
246
247 /* try to create EVP_PKEY from the supposed SubjectPublicKeyInfo binary data */
248 pkey = d2i_PUBKEY(NULL, (const unsigned char **)&tmp, len);
249 if (pkey) {
250 /* success, it's most likely SubjectPublicKeyInfo pubkey */
251 ret = 1;
252 } else {
253 /* fail, it's most likely not SubjectPublicKeyInfo pubkey */
254 ret = 0;
255 }
256
257cleanup:
258 EVP_PKEY_free(pkey);
259 free(bin);
260 return ret;
261}
262
roman2eab4742023-06-06 10:00:26 +0200263#endif /* NC_ENABLED_SSH_TLS */
264
Michal Vaskobe52dc22018-10-17 09:28:17 +0200265int
roman56acd552024-04-18 16:00:37 +0200266nc_sock_configure_ka(int sock, int enabled)
Michal Vaskobe52dc22018-10-17 09:28:17 +0200267{
roman56acd552024-04-18 16:00:37 +0200268 if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &enabled, sizeof enabled) == -1) {
269 ERR(NULL, "Failed to set SO_KEEPALIVE (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200270 return -1;
271 }
roman56acd552024-04-18 16:00:37 +0200272
273 return 0;
274}
275
276int
277nc_sock_configure_ka_idle_time(int sock, int idle_time)
278{
279 if (idle_time < 1) {
280 ERRARG(NULL, "idle_time");
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200281 }
Michal Vaskobe52dc22018-10-17 09:28:17 +0200282
283#ifdef TCP_KEEPIDLE
roman56acd552024-04-18 16:00:37 +0200284 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &idle_time, sizeof idle_time) == -1) {
285 ERR(NULL, "Failed to set TCP_KEEPIDLE (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200286 return -1;
287 }
288#endif
289
roman56acd552024-04-18 16:00:37 +0200290 return 0;
291}
292
293int
294nc_sock_configure_ka_max_probes(int sock, int max_probes)
295{
296 if (max_probes < 1) {
297 ERRARG(NULL, "max_probes");
298 }
299
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200300#ifdef TCP_KEEPCNT
roman56acd552024-04-18 16:00:37 +0200301 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &max_probes, sizeof max_probes) == -1) {
302 ERR(NULL, "Failed to set TCP_KEEPCNT (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200303 return -1;
304 }
305#endif
306
roman56acd552024-04-18 16:00:37 +0200307 return 0;
308}
309
310int
311nc_sock_configure_ka_probe_interval(int sock, int probe_interval)
312{
313 if (probe_interval < 1) {
314 ERRARG(NULL, "probe_interval");
315 }
316
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200317#ifdef TCP_KEEPINTVL
roman56acd552024-04-18 16:00:37 +0200318 if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &probe_interval, sizeof probe_interval) == -1) {
319 ERR(NULL, "Failed to set TCP_KEEPINTVL (%s).", strerror(errno));
Michal Vaskobe52dc22018-10-17 09:28:17 +0200320 return -1;
321 }
322#endif
323
324 return 0;
325}
326
Michal Vaskoade892d2017-02-22 13:40:35 +0100327struct nc_session *
Michal Vasko131120a2018-05-29 15:44:02 +0200328nc_new_session(NC_SIDE side, int shared_ti)
Michal Vaskoade892d2017-02-22 13:40:35 +0100329{
330 struct nc_session *sess;
Michal Vaskocf898172024-01-15 15:04:28 +0100331 struct timespec ts_cur;
Michal Vaskoade892d2017-02-22 13:40:35 +0100332
333 sess = calloc(1, sizeof *sess);
334 if (!sess) {
335 return NULL;
336 }
337
Michal Vasko131120a2018-05-29 15:44:02 +0200338 sess->side = side;
339
340 if (side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +0200341 pthread_mutex_init(&sess->opts.server.ntf_status_lock, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100342 pthread_mutex_init(&sess->opts.server.rpc_lock, NULL);
343 pthread_cond_init(&sess->opts.server.rpc_cond, NULL);
Michal Vaskoacf98472021-02-04 15:33:57 +0100344
345 pthread_mutex_init(&sess->opts.server.ch_lock, NULL);
346 pthread_cond_init(&sess->opts.server.ch_cond, NULL);
Michal Vaskocf898172024-01-15 15:04:28 +0100347
348 /* initialize last_rpc for idle_timeout */
349 nc_timeouttime_get(&ts_cur, 0);
350 sess->opts.server.last_rpc = ts_cur.tv_sec;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200351 } else {
352 pthread_mutex_init(&sess->opts.client.msgs_lock, NULL);
Michal Vasko131120a2018-05-29 15:44:02 +0200353 }
354
355 if (!shared_ti) {
356 sess->io_lock = malloc(sizeof *sess->io_lock);
357 if (!sess->io_lock) {
358 goto error;
359 }
360 pthread_mutex_init(sess->io_lock, NULL);
Michal Vaskoade892d2017-02-22 13:40:35 +0100361 }
362
363 return sess;
Michal Vasko131120a2018-05-29 15:44:02 +0200364
365error:
Michal Vasko131120a2018-05-29 15:44:02 +0200366 free(sess);
367 return NULL;
Michal Vaskoade892d2017-02-22 13:40:35 +0100368}
369
Michal Vasko96164bf2016-01-21 15:41:58 +0100370/*
371 * @return 1 - success
372 * 0 - timeout
373 * -1 - error
374 */
375int
Michal Vasko131120a2018-05-29 15:44:02 +0200376nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func)
Michal Vasko96164bf2016-01-21 15:41:58 +0100377{
378 int ret;
Michal Vasko62be1ce2016-03-03 13:24:52 +0100379 struct timespec ts_timeout;
Michal Vasko96164bf2016-01-21 15:41:58 +0100380
Michal Vasko131120a2018-05-29 15:44:02 +0200381 if (session->side != NC_SERVER) {
382 ERRINT;
383 return -1;
384 }
385
Michal Vasko96164bf2016-01-21 15:41:58 +0100386 if (timeout > 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100387 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko96164bf2016-01-21 15:41:58 +0100388
Michal Vaskoade892d2017-02-22 13:40:35 +0100389 /* LOCK */
Michal Vaskod8a74192023-02-06 15:51:50 +0100390 ret = pthread_mutex_clocklock(&session->opts.server.rpc_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100391 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100392 while (session->opts.server.rpc_inuse) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100393 ret = pthread_cond_clockwait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock,
394 COMPAT_CLOCK_ID, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100395 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100396 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100397 break;
398 }
399 }
400 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100401 } else if (!timeout) {
Michal Vaskoade892d2017-02-22 13:40:35 +0100402 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100403 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100404 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100405 if (session->opts.server.rpc_inuse) {
406 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100407 return 0;
408 }
Michal vasko2f8e4b52016-10-05 13:04:11 +0200409 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100410 } else { /* timeout == -1 */
Michal Vaskoade892d2017-02-22 13:40:35 +0100411 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100412 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100413 if (!ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100414 while (session->opts.server.rpc_inuse) {
415 ret = pthread_cond_wait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100416 if (ret) {
Michal Vaskoacf98472021-02-04 15:33:57 +0100417 pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100418 break;
419 }
420 }
421 }
Michal Vasko96164bf2016-01-21 15:41:58 +0100422 }
423
Michal Vaskoade892d2017-02-22 13:40:35 +0100424 if (ret) {
425 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
426 /* timeout */
427 return 0;
428 }
429
Michal Vasko96164bf2016-01-21 15:41:58 +0100430 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200431 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vasko96164bf2016-01-21 15:41:58 +0100432 return -1;
433 }
434
435 /* ok */
Michal Vaskoacf98472021-02-04 15:33:57 +0100436 assert(session->opts.server.rpc_inuse == 0);
437 session->opts.server.rpc_inuse = 1;
Michal Vaskoade892d2017-02-22 13:40:35 +0100438
439 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100440 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100441 if (ret) {
442 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200443 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100444 return -1;
445 }
446
447 return 1;
448}
449
450int
Michal Vasko131120a2018-05-29 15:44:02 +0200451nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func)
Michal Vaskoade892d2017-02-22 13:40:35 +0100452{
453 int ret;
454 struct timespec ts_timeout;
455
Michal Vasko131120a2018-05-29 15:44:02 +0200456 if (session->side != NC_SERVER) {
457 ERRINT;
458 return -1;
459 }
460
Michal Vaskoacf98472021-02-04 15:33:57 +0100461 assert(session->opts.server.rpc_inuse);
Michal Vaskoade892d2017-02-22 13:40:35 +0100462
463 if (timeout > 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100464 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100465
466 /* LOCK */
Michal Vaskod8a74192023-02-06 15:51:50 +0100467 ret = pthread_mutex_clocklock(&session->opts.server.rpc_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vaskoade892d2017-02-22 13:40:35 +0100468 } else if (!timeout) {
469 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100470 ret = pthread_mutex_trylock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100471 } else { /* timeout == -1 */
472 /* LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100473 ret = pthread_mutex_lock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100474 }
475
476 if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) {
477 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200478 ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100479 return -1;
480 } else if (ret) {
Michal Vasko69e98752022-12-14 14:20:17 +0100481 WRN(session, "%s: session RPC lock timeout, should not happen.", func);
Michal Vaskoade892d2017-02-22 13:40:35 +0100482 }
483
Michal Vaskoacf98472021-02-04 15:33:57 +0100484 session->opts.server.rpc_inuse = 0;
485 pthread_cond_signal(&session->opts.server.rpc_cond);
Michal Vaskoade892d2017-02-22 13:40:35 +0100486
487 if (!ret) {
488 /* UNLOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +0100489 ret = pthread_mutex_unlock(&session->opts.server.rpc_lock);
Michal Vaskoade892d2017-02-22 13:40:35 +0100490 if (ret) {
491 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200492 ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret));
Michal Vaskoade892d2017-02-22 13:40:35 +0100493 return -1;
494 }
495 }
496
Michal Vasko96164bf2016-01-21 15:41:58 +0100497 return 1;
498}
499
Michal Vasko131120a2018-05-29 15:44:02 +0200500int
501nc_session_io_lock(struct nc_session *session, int timeout, const char *func)
502{
503 int ret;
504 struct timespec ts_timeout;
505
506 if (timeout > 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100507 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko131120a2018-05-29 15:44:02 +0200508
Michal Vaskod8a74192023-02-06 15:51:50 +0100509 ret = pthread_mutex_clocklock(session->io_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vasko131120a2018-05-29 15:44:02 +0200510 } else if (!timeout) {
511 ret = pthread_mutex_trylock(session->io_lock);
512 } else { /* timeout == -1 */
Robin Jarry54ea2962018-10-10 10:33:40 +0200513 ret = pthread_mutex_lock(session->io_lock);
Michal Vasko131120a2018-05-29 15:44:02 +0200514 }
515
516 if (ret) {
517 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
518 /* timeout */
519 return 0;
520 }
521
522 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200523 ERR(session, "%s: failed to IO lock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200524 return -1;
525 }
526
527 return 1;
528}
529
530int
531nc_session_io_unlock(struct nc_session *session, const char *func)
532{
533 int ret;
534
535 ret = pthread_mutex_unlock(session->io_lock);
536 if (ret) {
537 /* error */
Michal Vasko05532772021-06-03 12:12:38 +0200538 ERR(session, "%s: failed to IO unlock a session (%s).", func, strerror(ret));
Michal Vasko131120a2018-05-29 15:44:02 +0200539 return -1;
540 }
541
542 return 1;
543}
544
Michal Vasko01130bd2021-08-26 11:47:38 +0200545int
546nc_session_client_msgs_lock(struct nc_session *session, int *timeout, const char *func)
547{
548 int ret;
549 int32_t diff_msec;
roman6ece9c52022-06-22 09:29:17 +0200550 struct timespec ts_timeout, ts_start;
Michal Vasko01130bd2021-08-26 11:47:38 +0200551
552 assert(session->side == NC_CLIENT);
553
554 if (*timeout > 0) {
555 /* get current time */
Michal Vaskod8a74192023-02-06 15:51:50 +0100556 nc_timeouttime_get(&ts_start, 0);
Michal Vasko01130bd2021-08-26 11:47:38 +0200557
Michal Vaskod8a74192023-02-06 15:51:50 +0100558 nc_timeouttime_get(&ts_timeout, *timeout);
Michal Vasko01130bd2021-08-26 11:47:38 +0200559
Michal Vaskod8a74192023-02-06 15:51:50 +0100560 ret = pthread_mutex_clocklock(&session->opts.client.msgs_lock, COMPAT_CLOCK_ID, &ts_timeout);
Michal Vasko01130bd2021-08-26 11:47:38 +0200561 if (!ret) {
562 /* update timeout based on what was elapsed */
Michal Vaskod8a74192023-02-06 15:51:50 +0100563 diff_msec = nc_timeouttime_cur_diff(&ts_start);
Michal Vasko01130bd2021-08-26 11:47:38 +0200564 *timeout -= diff_msec;
565 }
566 } else if (!*timeout) {
567 ret = pthread_mutex_trylock(&session->opts.client.msgs_lock);
568 } else { /* timeout == -1 */
569 ret = pthread_mutex_lock(&session->opts.client.msgs_lock);
570 }
571
572 if (ret) {
573 if ((ret == EBUSY) || (ret == ETIMEDOUT)) {
574 /* timeout */
575 return 0;
576 }
577
578 /* error */
579 ERR(session, "%s: failed to MSGS lock a session (%s).", func, strerror(ret));
580 return -1;
581 }
582
583 return 1;
584}
585
586int
587nc_session_client_msgs_unlock(struct nc_session *session, const char *func)
588{
589 int ret;
590
591 assert(session->side == NC_CLIENT);
592
593 ret = pthread_mutex_unlock(&session->opts.client.msgs_lock);
594 if (ret) {
595 /* error */
596 ERR(session, "%s: failed to MSGS unlock a session (%s).", func, strerror(ret));
597 return -1;
598 }
599
600 return 1;
601}
602
Michal Vasko8dadf782016-01-15 10:29:36 +0100603API NC_STATUS
604nc_session_get_status(const struct nc_session *session)
605{
roman40672412023-05-04 11:10:22 +0200606 NC_CHECK_ARG_RET(session, session, NC_STATUS_ERR);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100607
Michal Vasko8dadf782016-01-15 10:29:36 +0100608 return session->status;
609}
610
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100611API NC_SESSION_TERM_REASON
Michal Vasko142cfea2017-08-07 10:12:11 +0200612nc_session_get_term_reason(const struct nc_session *session)
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100613{
roman40672412023-05-04 11:10:22 +0200614 NC_CHECK_ARG_RET(session, session, NC_SESSION_TERM_ERR);
Radek Krejci6e36bfb2016-12-01 21:40:16 +0100615
616 return session->term_reason;
617}
618
Michal Vasko8dadf782016-01-15 10:29:36 +0100619API uint32_t
Michal Vasko142cfea2017-08-07 10:12:11 +0200620nc_session_get_killed_by(const struct nc_session *session)
621{
roman40672412023-05-04 11:10:22 +0200622 NC_CHECK_ARG_RET(session, session, 0);
Michal Vasko142cfea2017-08-07 10:12:11 +0200623
624 return session->killed_by;
625}
626
627API uint32_t
Michal Vasko8dadf782016-01-15 10:29:36 +0100628nc_session_get_id(const struct nc_session *session)
629{
roman40672412023-05-04 11:10:22 +0200630 NC_CHECK_ARG_RET(session, session, 0);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100631
Michal Vasko8dadf782016-01-15 10:29:36 +0100632 return session->id;
633}
634
Michal Vasko174fe8e2016-02-17 15:38:09 +0100635API int
636nc_session_get_version(const struct nc_session *session)
637{
roman40672412023-05-04 11:10:22 +0200638 NC_CHECK_ARG_RET(session, session, -1);
Michal Vasko174fe8e2016-02-17 15:38:09 +0100639
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200640 return session->version == NC_VERSION_10 ? 0 : 1;
Michal Vasko174fe8e2016-02-17 15:38:09 +0100641}
642
Michal Vasko8dadf782016-01-15 10:29:36 +0100643API NC_TRANSPORT_IMPL
644nc_session_get_ti(const struct nc_session *session)
645{
roman40672412023-05-04 11:10:22 +0200646 NC_CHECK_ARG_RET(session, session, 0);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100647
Michal Vasko8dadf782016-01-15 10:29:36 +0100648 return session->ti_type;
649}
650
651API const char *
652nc_session_get_username(const struct nc_session *session)
653{
roman40672412023-05-04 11:10:22 +0200654 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100655
Michal Vasko8dadf782016-01-15 10:29:36 +0100656 return session->username;
657}
658
659API const char *
660nc_session_get_host(const struct nc_session *session)
661{
roman40672412023-05-04 11:10:22 +0200662 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100663
Michal Vasko8dadf782016-01-15 10:29:36 +0100664 return session->host;
665}
666
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200667API const char *
668nc_session_get_path(const struct nc_session *session)
669{
roman40672412023-05-04 11:10:22 +0200670 NC_CHECK_ARG_RET(session, session, NULL);
671
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200672 if (session->ti_type != NC_TI_UNIX) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200673 return NULL;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200674 }
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200675
676 return session->path;
677}
678
Michal Vasko8dadf782016-01-15 10:29:36 +0100679API uint16_t
680nc_session_get_port(const struct nc_session *session)
681{
roman40672412023-05-04 11:10:22 +0200682 NC_CHECK_ARG_RET(session, session, 0);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100683
Michal Vasko8dadf782016-01-15 10:29:36 +0100684 return session->port;
685}
686
Michal Vasko93224072021-11-09 12:14:28 +0100687API const struct ly_ctx *
Michal Vasko9a25e932016-02-01 10:36:42 +0100688nc_session_get_ctx(const struct nc_session *session)
689{
roman40672412023-05-04 11:10:22 +0200690 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vasko9a25e932016-02-01 10:36:42 +0100691
692 return session->ctx;
693}
694
Michal Vasko2cc4c682016-03-01 09:16:48 +0100695API void
696nc_session_set_data(struct nc_session *session, void *data)
697{
698 if (!session) {
roman40672412023-05-04 11:10:22 +0200699 ERRARG(NULL, "session");
Michal Vasko2cc4c682016-03-01 09:16:48 +0100700 return;
701 }
702
703 session->data = data;
704}
705
706API void *
707nc_session_get_data(const struct nc_session *session)
708{
roman40672412023-05-04 11:10:22 +0200709 NC_CHECK_ARG_RET(session, session, NULL);
Michal Vasko2cc4c682016-03-01 09:16:48 +0100710
711 return session->data;
712}
713
Michal Vaskodc96bb92023-03-28 08:52:48 +0200714API int
715nc_session_is_callhome(const struct nc_session *session)
716{
roman40672412023-05-04 11:10:22 +0200717 NC_CHECK_ARG_RET(session, session, 0);
Michal Vaskodc96bb92023-03-28 08:52:48 +0200718
719 if (session->flags & NC_SESSION_CALLHOME) {
720 return 1;
721 }
722
723 return 0;
724}
725
Michal Vasko086311b2016-01-08 09:53:11 +0100726NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200727nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op)
Radek Krejci695d4fa2015-10-22 13:23:54 +0200728{
Michal Vasko7e1f5fb2021-11-10 10:14:45 +0100729 if (session->ctx != LYD_CTX(op)) {
730 ERR(session, "RPC \"%s\" was created in different context than that of the session.", LYD_NAME(op));
Michal Vasko086311b2016-01-08 09:53:11 +0100731 return NC_MSG_ERROR;
Michal Vasko7df39ec2015-12-09 15:26:24 +0100732 }
733
Michal Vasko131120a2018-05-29 15:44:02 +0200734 return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL);
Michal Vasko7df39ec2015-12-09 15:26:24 +0100735}
736
Michal Vaskod4da3632022-05-25 11:49:10 +0200737/**
738 * @brief Send \<close-session\> and read the reply on a session.
739 *
740 * @param[in] session Closing NETCONF session.
741 */
742static void
743nc_session_free_close_session(struct nc_session *session)
744{
745 struct ly_in *msg;
746 struct lyd_node *close_rpc, *envp;
747 const struct lys_module *ietfnc;
748
749 ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf");
750 if (!ietfnc) {
Michal Vasko5ca5d972022-09-14 13:51:31 +0200751 WRN(session, "Missing ietf-netconf module in context, unable to send <close-session>.");
Michal Vaskod4da3632022-05-25 11:49:10 +0200752 return;
753 }
754 if (lyd_new_inner(NULL, ietfnc, "close-session", 0, &close_rpc)) {
755 WRN(session, "Failed to create <close-session> RPC.");
756 return;
757 }
758
759 /* send the RPC */
760 nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc);
761
762read_msg:
763 switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &msg)) {
764 case 1:
765 if (!strncmp(ly_in_memory(msg, NULL), "<notification", 13)) {
766 /* ignore */
767 ly_in_free(msg, 1);
768 goto read_msg;
769 }
770 if (lyd_parse_op(session->ctx, close_rpc, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, &envp, NULL)) {
771 WRN(session, "Failed to parse <close-session> reply.");
772 } else if (!lyd_child(envp) || strcmp(LYD_NAME(lyd_child(envp)), "ok")) {
773 WRN(session, "Reply to <close-session> was not <ok> as expected.");
774 }
775 lyd_free_tree(envp);
776 ly_in_free(msg, 1);
777 break;
778 case 0:
779 WRN(session, "Timeout for receiving a reply to <close-session> elapsed.");
780 break;
781 case -1:
782 ERR(session, "Failed to receive a reply to <close-session>.");
783 break;
784 default:
785 /* cannot happen */
786 break;
787 }
788 lyd_free_tree(close_rpc);
789}
790
Michal Vasko33476c32022-09-09 11:21:40 +0200791/**
792 * @brief Free transport implementation members of a session.
793 *
794 * @param[in] session Session to free.
795 * @param[out] multisession Whether there are other NC sessions on the same SSH sessions.
796 */
797static void
798nc_session_free_transport(struct nc_session *session, int *multisession)
799{
800 int connected; /* flag to indicate whether the transport socket is still connected */
Michal Vaskoe44f2702022-12-12 07:58:06 +0100801 int sock = -1;
Michal Vasko33476c32022-09-09 11:21:40 +0200802 struct nc_session *siter;
803
804 *multisession = 0;
805 connected = nc_session_is_connected(session);
806
807 /* transport implementation cleanup */
808 switch (session->ti_type) {
809 case NC_TI_FD:
810 /* nothing needed - file descriptors were provided by caller,
811 * so it is up to the caller to close them correctly
812 * TODO use callbacks
813 */
814 /* just to avoid compiler warning */
815 (void)connected;
816 (void)siter;
817 break;
818
819 case NC_TI_UNIX:
820 sock = session->ti.unixsock.sock;
821 (void)connected;
822 (void)siter;
823 break;
824
roman2eab4742023-06-06 10:00:26 +0200825#ifdef NC_ENABLED_SSH_TLS
Michal Vaskoe44f2702022-12-12 07:58:06 +0100826 case NC_TI_LIBSSH: {
827 int r;
828
Michal Vasko33476c32022-09-09 11:21:40 +0200829 if (connected) {
Michal Vasko64734402022-09-09 11:22:00 +0200830 ssh_channel_send_eof(session->ti.libssh.channel);
Michal Vasko33476c32022-09-09 11:21:40 +0200831 ssh_channel_free(session->ti.libssh.channel);
832 }
833 /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to
834 * SSH channel). So destroy the SSH session only if there is no other NETCONF session using
835 * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session.
836 */
837 /* SESSION IO LOCK */
838 r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
839
840 if (session->ti.libssh.next) {
841 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
842 if (siter->status != NC_STATUS_STARTING) {
843 *multisession = 1;
844 break;
845 }
846 }
847 }
848
849 if (!*multisession) {
850 /* it's not multisession yet, but we still need to free the starting sessions */
851 if (session->ti.libssh.next) {
852 do {
853 siter = session->ti.libssh.next;
854 session->ti.libssh.next = siter->ti.libssh.next;
855
856 /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */
857 free(siter->username);
858 free(siter->host);
859 if (!(siter->flags & NC_SESSION_SHAREDCTX)) {
860 ly_ctx_destroy((struct ly_ctx *)siter->ctx);
861 }
862
863 free(siter);
864 } while (session->ti.libssh.next != session);
865 }
866 /* remember sock so we can close it */
867 sock = ssh_get_fd(session->ti.libssh.session);
868 if (connected) {
Michal Vasko70e90622023-12-07 08:43:14 +0100869 /* does not close sock */
Michal Vasko33476c32022-09-09 11:21:40 +0200870 ssh_disconnect(session->ti.libssh.session);
Michal Vasko33476c32022-09-09 11:21:40 +0200871 }
872 ssh_free(session->ti.libssh.session);
873 } else {
874 /* remove the session from the list */
875 for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {}
876 if (session->ti.libssh.next == siter) {
877 /* there will be only one session */
878 siter->ti.libssh.next = NULL;
879 } else {
880 /* there are still multiple sessions, keep the ring list */
881 siter->ti.libssh.next = session->ti.libssh.next;
882 }
Michal Vasko33476c32022-09-09 11:21:40 +0200883 }
884
885 /* SESSION IO UNLOCK */
886 if (r == 1) {
887 nc_session_io_unlock(session, __func__);
888 }
889 break;
Michal Vaskoe44f2702022-12-12 07:58:06 +0100890 }
Michal Vasko33476c32022-09-09 11:21:40 +0200891 case NC_TI_OPENSSL:
892 /* remember sock so we can close it */
893 sock = SSL_get_fd(session->ti.tls);
894
895 if (connected) {
896 SSL_shutdown(session->ti.tls);
897 }
898 SSL_free(session->ti.tls);
899
900 if (session->side == NC_SERVER) {
901 X509_free(session->opts.server.client_cert);
902 }
903 break;
roman2eab4742023-06-06 10:00:26 +0200904#endif /* NC_ENABLED_SSH_TLS */
Michal Vasko33476c32022-09-09 11:21:40 +0200905 case NC_TI_NONE:
906 break;
907 }
908
909 /* close socket separately */
910 if (sock > -1) {
911 close(sock);
912 }
913}
914
Radek Krejci695d4fa2015-10-22 13:23:54 +0200915API void
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100916nc_session_free(struct nc_session *session, void (*data_free)(void *))
Radek Krejci695d4fa2015-10-22 13:23:54 +0200917{
Michal Vasko33476c32022-09-09 11:21:40 +0200918 int r, i, rpc_locked = 0, msgs_locked = 0, timeout;
Michal Vaskob48aa812016-01-18 14:13:09 +0100919 int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */
Michal Vaskoad611702015-12-03 13:41:51 +0100920 struct nc_msg_cont *contiter;
Michal Vasko77367452021-02-16 16:32:18 +0100921 struct ly_in *msg;
roman6ece9c52022-06-22 09:29:17 +0200922 struct timespec ts;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200923 void *p;
924
Michal Vasko428087d2016-01-14 16:04:28 +0100925 if (!session || (session->status == NC_STATUS_CLOSING)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +0200926 return;
927 }
928
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200929 /* stop notification threads if any */
930 if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running)) {
931 /* let the threads know they should quit */
932 ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 0);
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200933
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200934 /* wait for them */
Michal Vaskod8a74192023-02-06 15:51:50 +0100935 nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vaskoa8ec54b2022-10-20 09:59:07 +0200936 while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_count)) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200937 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +0100938 if (nc_timeouttime_cur_diff(&ts) < 1) {
Michal Vasko5bd4a3f2021-06-17 16:40:10 +0200939 ERR(session, "Waiting for notification thread exit failed (timed out).");
940 break;
941 }
942 }
Michal Vasko86d357c2016-03-11 13:46:38 +0100943 }
944
Michal Vaskoacf98472021-02-04 15:33:57 +0100945 if (session->side == NC_SERVER) {
Michal Vasko131120a2018-05-29 15:44:02 +0200946 r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__);
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100947 if (r == -1) {
Michal Vaskoadd4c792015-10-26 15:36:58 +0100948 return;
Michal Vasko131120a2018-05-29 15:44:02 +0200949 } else if (r) {
950 rpc_locked = 1;
Michal Vasko96a28a32021-02-04 15:35:20 +0100951 } else {
952 /* else failed to lock it, too bad */
Michal Vasko05532772021-06-03 12:12:38 +0200953 ERR(session, "Freeing a session while an RPC is being processed.");
Michal Vasko96a28a32021-02-04 15:35:20 +0100954 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200955 }
956
Michal Vasko8c247822020-09-07 13:23:23 +0200957 if (session->side == NC_CLIENT) {
Michal Vasko01130bd2021-08-26 11:47:38 +0200958 timeout = NC_SESSION_FREE_LOCK_TIMEOUT;
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200959
Michal Vasko01130bd2021-08-26 11:47:38 +0200960 /* MSGS LOCK */
961 r = nc_session_client_msgs_lock(session, &timeout, __func__);
962 if (r == -1) {
963 return;
964 } else if (r) {
965 msgs_locked = 1;
966 } else {
967 /* else failed to lock it, too bad */
968 ERR(session, "Freeing a session while messages are being received.");
969 }
970
971 /* cleanup message queue */
tadeas-vintrlik54f142a2021-08-23 10:36:18 +0200972 for (contiter = session->opts.client.msgs; contiter; ) {
Michal Vasko77367452021-02-16 16:32:18 +0100973 ly_in_free(contiter->msg, 1);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200974
Michal Vaskoad611702015-12-03 13:41:51 +0100975 p = contiter;
976 contiter = contiter->next;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200977 free(p);
978 }
979
Michal Vasko01130bd2021-08-26 11:47:38 +0200980 if (msgs_locked) {
981 /* MSGS UNLOCK */
982 nc_session_client_msgs_unlock(session, __func__);
983 }
Radek Krejci695d4fa2015-10-22 13:23:54 +0200984
Michal Vasko8c247822020-09-07 13:23:23 +0200985 if (session->status == NC_STATUS_RUNNING) {
Michal Vasko9c6d38c2021-09-03 13:02:53 +0200986 /* receive any leftover messages */
987 while (nc_read_msg_poll_io(session, 0, &msg) == 1) {
988 ly_in_free(msg, 1);
989 }
990
Michal Vasko8c247822020-09-07 13:23:23 +0200991 /* send closing info to the other side */
Michal Vaskod4da3632022-05-25 11:49:10 +0200992 nc_session_free_close_session(session);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200993 }
994
995 /* list of server's capabilities */
Michal Vasko2e6defd2016-10-07 15:48:15 +0200996 if (session->opts.client.cpblts) {
997 for (i = 0; session->opts.client.cpblts[i]; i++) {
Michal Vasko96fc4bb2017-05-23 14:58:34 +0200998 free(session->opts.client.cpblts[i]);
Radek Krejci695d4fa2015-10-22 13:23:54 +0200999 }
Michal Vasko2e6defd2016-10-07 15:48:15 +02001000 free(session->opts.client.cpblts);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001001 }
Michal Vasko78939072022-12-12 07:43:18 +01001002
1003 /* LY ext data */
roman2eab4742023-06-06 10:00:26 +02001004#ifdef NC_ENABLED_SSH_TLS
Michal Vaskoe44f2702022-12-12 07:58:06 +01001005 struct nc_session *siter;
1006
Michal Vasko78939072022-12-12 07:43:18 +01001007 if ((session->flags & NC_SESSION_SHAREDCTX) && session->ti.libssh.next) {
1008 for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) {
1009 if (siter->status != NC_STATUS_STARTING) {
1010 /* move LY ext data to this session */
1011 assert(!siter->opts.client.ext_data);
1012 siter->opts.client.ext_data = session->opts.client.ext_data;
1013 session->opts.client.ext_data = NULL;
1014 break;
1015 }
1016 }
Michal Vaskoe44f2702022-12-12 07:58:06 +01001017 } else
roman2eab4742023-06-06 10:00:26 +02001018#endif /* NC_ENABLED_SSH_TLS */
Michal Vaskoe44f2702022-12-12 07:58:06 +01001019 {
Michal Vasko78939072022-12-12 07:43:18 +01001020 lyd_free_siblings(session->opts.client.ext_data);
1021 }
Radek Krejci695d4fa2015-10-22 13:23:54 +02001022 }
1023
Michal Vaskoe1a64ec2016-03-01 12:21:58 +01001024 if (session->data && data_free) {
1025 data_free(session->data);
1026 }
1027
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001028 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
1029 /* CH LOCK */
Michal Vaskoacf98472021-02-04 15:33:57 +01001030 pthread_mutex_lock(&session->opts.server.ch_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001031 }
1032
Michal Vasko86d357c2016-03-11 13:46:38 +01001033 /* mark session for closing */
Radek Krejci695d4fa2015-10-22 13:23:54 +02001034 session->status = NC_STATUS_CLOSING;
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001035
Michal Vaskofeccb312022-03-24 15:24:59 +01001036 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskoacf98472021-02-04 15:33:57 +01001037 pthread_cond_signal(&session->opts.server.ch_cond);
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001038
Michal Vaskod8a74192023-02-06 15:51:50 +01001039 nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT);
Michal Vasko0db3db52021-03-03 10:45:42 +01001040
1041 /* wait for CH thread to actually wake up and terminate */
1042 r = 0;
Michal Vaskofeccb312022-03-24 15:24:59 +01001043 while (!r && (session->flags & NC_SESSION_CH_THREAD)) {
Michal Vaskod8a74192023-02-06 15:51:50 +01001044 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 +01001045 }
Michal Vasko0db3db52021-03-03 10:45:42 +01001046 if (r) {
Michal Vasko05532772021-06-03 12:12:38 +02001047 ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r));
Michal Vasko3f05a092018-03-13 10:39:49 +01001048 }
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001049 }
1050
Michal Vaskofeccb312022-03-24 15:24:59 +01001051 if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) {
1052 /* CH UNLOCK */
1053 pthread_mutex_unlock(&session->opts.server.ch_lock);
1054 }
1055
Radek Krejci695d4fa2015-10-22 13:23:54 +02001056 /* transport implementation cleanup */
Michal Vasko33476c32022-09-09 11:21:40 +02001057 nc_session_free_transport(session, &multisession);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001058
Michal Vasko33476c32022-09-09 11:21:40 +02001059 /* final cleanup */
Michal Vasko93224072021-11-09 12:14:28 +01001060 free(session->username);
1061 free(session->host);
1062 free(session->path);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001063
Michal Vaskoacf98472021-02-04 15:33:57 +01001064 if (session->side == NC_SERVER) {
Michal Vaskodf68e7e2022-04-21 11:04:00 +02001065 pthread_mutex_destroy(&session->opts.server.ntf_status_lock);
Michal Vasko131120a2018-05-29 15:44:02 +02001066 if (rpc_locked) {
1067 nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__);
Michal Vasko9e99f012016-03-03 13:25:20 +01001068 }
Michal Vaskoacf98472021-02-04 15:33:57 +01001069 pthread_mutex_destroy(&session->opts.server.rpc_lock);
1070 pthread_cond_destroy(&session->opts.server.rpc_cond);
Michal Vasko131120a2018-05-29 15:44:02 +02001071 }
1072
1073 if (session->io_lock && !multisession) {
1074 pthread_mutex_destroy(session->io_lock);
1075 free(session->io_lock);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001076 }
1077
1078 if (!(session->flags & NC_SESSION_SHAREDCTX)) {
Michal Vasko93224072021-11-09 12:14:28 +01001079 ly_ctx_destroy((struct ly_ctx *)session->ctx);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001080 }
1081
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001082 if (session->side == NC_SERVER) {
Michal Vaskoacf98472021-02-04 15:33:57 +01001083 /* free CH synchronization structures */
1084 pthread_cond_destroy(&session->opts.server.ch_cond);
1085 pthread_mutex_destroy(&session->opts.server.ch_lock);
tadeas-vintrlik54f142a2021-08-23 10:36:18 +02001086 } else {
1087 pthread_mutex_destroy(&session->opts.client.msgs_lock);
Michal Vaskoc4bc5812016-10-13 10:59:36 +02001088 }
1089
Radek Krejci695d4fa2015-10-22 13:23:54 +02001090 free(session);
1091}
1092
Michal Vasko086311b2016-01-08 09:53:11 +01001093static void
Michal Vasko93224072021-11-09 12:14:28 +01001094add_cpblt(const char *capab, char ***cpblts, int *size, int *count)
Michal Vasko086311b2016-01-08 09:53:11 +01001095{
Radek Krejci658782b2016-12-04 22:04:55 +01001096 size_t len;
1097 int i;
1098 char *p;
1099
1100 if (capab) {
1101 /* check if already present */
1102 p = strchr(capab, '?');
1103 if (p) {
1104 len = p - capab;
1105 } else {
1106 len = strlen(capab);
1107 }
1108 for (i = 0; i < *count; i++) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001109 if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) {
Radek Krejci658782b2016-12-04 22:04:55 +01001110 /* already present, do not duplicate it */
1111 return;
1112 }
1113 }
1114 }
1115
1116 /* add another capability */
Michal Vasko086311b2016-01-08 09:53:11 +01001117 if (*count == *size) {
1118 *size += 5;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001119 *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts);
1120 if (!(*cpblts)) {
1121 ERRMEM;
1122 return;
1123 }
Michal Vasko086311b2016-01-08 09:53:11 +01001124 }
1125
Michal Vasko93224072021-11-09 12:14:28 +01001126 (*cpblts)[*count] = capab ? strdup(capab) : NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001127 ++(*count);
1128}
1129
Michal Vasko93224072021-11-09 12:14:28 +01001130API char **
1131nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version)
Michal Vasko086311b2016-01-08 09:53:11 +01001132{
Michal Vasko93224072021-11-09 12:14:28 +01001133 char **cpblts;
Michal Vasko77367452021-02-16 16:32:18 +01001134 const struct lys_module *mod;
1135 struct lysp_feature *feat;
1136 int size = 10, count, features_count = 0, dev_count = 0, str_len, len;
Michal Vasko1440a742021-03-31 11:11:03 +02001137 uint32_t i, u;
Michal Vasko77367452021-02-16 16:32:18 +01001138 LY_ARRAY_COUNT_TYPE v;
Michal Vasko1440a742021-03-31 11:11:03 +02001139 char *yl_content_id;
romanc1d2b092023-02-02 08:58:27 +01001140 uint32_t wd_also_supported;
1141 uint32_t wd_basic_mode;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001142
Radek Krejci24a18412018-05-16 15:09:10 +02001143#define NC_CPBLT_BUF_LEN 4096
Michal Vasko2e47ef92016-06-20 10:03:24 +02001144 char str[NC_CPBLT_BUF_LEN];
Michal Vasko086311b2016-01-08 09:53:11 +01001145
roman40672412023-05-04 11:10:22 +02001146 NC_CHECK_ARG_RET(NULL, ctx, NULL);
Michal Vasko4ffa3b22016-05-24 16:36:25 +02001147
Michal Vasko086311b2016-01-08 09:53:11 +01001148 cpblts = malloc(size * sizeof *cpblts);
roman124a4362023-10-26 15:36:22 +02001149 NC_CHECK_ERRMEM_GOTO(!cpblts, , error);
Michal Vasko93224072021-11-09 12:14:28 +01001150 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1151 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko086311b2016-01-08 09:53:11 +01001152 count = 2;
1153
1154 /* capabilities */
1155
Michal Vasko77367452021-02-16 16:32:18 +01001156 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf");
Michal Vasko086311b2016-01-08 09:53:11 +01001157 if (mod) {
Michal Vasko77367452021-02-16 16:32:18 +01001158 if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001159 add_cpblt("urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001160 }
Michal Vasko77367452021-02-16 16:32:18 +01001161 if (lys_feature_value(mod, "candidate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001162 add_cpblt("urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count);
Michal Vasko77367452021-02-16 16:32:18 +01001163 if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001164 add_cpblt("urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001165 }
1166 }
Michal Vasko77367452021-02-16 16:32:18 +01001167 if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001168 add_cpblt("urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001169 }
Michal Vasko77367452021-02-16 16:32:18 +01001170 if (lys_feature_value(mod, "validate") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001171 add_cpblt("urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001172 }
Michal Vasko77367452021-02-16 16:32:18 +01001173 if (lys_feature_value(mod, "startup") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001174 add_cpblt("urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001175 }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001176
1177 /* The URL capability must be set manually using nc_server_set_capability()
1178 * because of the need for supported protocols to be included.
1179 * https://tools.ietf.org/html/rfc6241#section-8.8.3
1180 */
Michal Vasko77367452021-02-16 16:32:18 +01001181 // if (lys_feature_value(mod, "url") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001182 // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count);
mekleoa8de5e92020-02-13 09:05:56 +01001183 // }
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001184
Michal Vasko77367452021-02-16 16:32:18 +01001185 if (lys_feature_value(mod, "xpath") == LY_SUCCESS) {
Michal Vasko93224072021-11-09 12:14:28 +01001186 add_cpblt("urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001187 }
1188 }
1189
Michal Vasko77367452021-02-16 16:32:18 +01001190 mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults");
Michal Vasko086311b2016-01-08 09:53:11 +01001191 if (mod) {
romanc1d2b092023-02-02 08:58:27 +01001192 wd_basic_mode = ATOMIC_LOAD_RELAXED(server_opts.wd_basic_mode);
1193 if (!wd_basic_mode) {
Michal Vasko05532772021-06-03 12:12:38 +02001194 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 +01001195 } else {
Michal Vasko3031aae2016-01-27 16:07:18 +01001196 strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0");
romanc1d2b092023-02-02 08:58:27 +01001197 switch (wd_basic_mode) {
Michal Vasko086311b2016-01-08 09:53:11 +01001198 case NC_WD_ALL:
1199 strcat(str, "?basic-mode=report-all");
1200 break;
1201 case NC_WD_TRIM:
1202 strcat(str, "?basic-mode=trim");
1203 break;
1204 case NC_WD_EXPLICIT:
1205 strcat(str, "?basic-mode=explicit");
1206 break;
1207 default:
Michal Vasko9e036d52016-01-08 10:49:26 +01001208 ERRINT;
Michal Vasko086311b2016-01-08 09:53:11 +01001209 break;
1210 }
1211
romanc1d2b092023-02-02 08:58:27 +01001212 wd_also_supported = ATOMIC_LOAD_RELAXED(server_opts.wd_also_supported);
1213 if (wd_also_supported) {
Michal Vasko2e47ef92016-06-20 10:03:24 +02001214 strcat(str, "&also-supported=");
romanc1d2b092023-02-02 08:58:27 +01001215 if (wd_also_supported & NC_WD_ALL) {
Michal Vasko086311b2016-01-08 09:53:11 +01001216 strcat(str, "report-all,");
1217 }
romanc1d2b092023-02-02 08:58:27 +01001218 if (wd_also_supported & NC_WD_ALL_TAG) {
Michal Vasko086311b2016-01-08 09:53:11 +01001219 strcat(str, "report-all-tagged,");
1220 }
romanc1d2b092023-02-02 08:58:27 +01001221 if (wd_also_supported & NC_WD_TRIM) {
Michal Vasko086311b2016-01-08 09:53:11 +01001222 strcat(str, "trim,");
1223 }
romanc1d2b092023-02-02 08:58:27 +01001224 if (wd_also_supported & NC_WD_EXPLICIT) {
Michal Vasko086311b2016-01-08 09:53:11 +01001225 strcat(str, "explicit,");
1226 }
1227 str[strlen(str) - 1] = '\0';
1228
Michal Vasko93224072021-11-09 12:14:28 +01001229 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001230 }
1231 }
1232 }
1233
Radek Krejci658782b2016-12-04 22:04:55 +01001234 /* other capabilities */
1235 for (u = 0; u < server_opts.capabilities_count; u++) {
Michal Vasko93224072021-11-09 12:14:28 +01001236 add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001237 }
1238
1239 /* models */
Michal Vasko1440a742021-03-31 11:11:03 +02001240 u = 0;
Radek Krejci24a18412018-05-16 15:09:10 +02001241 while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
Radek Krejci24a18412018-05-16 15:09:10 +02001242 if (!strcmp(mod->name, "ietf-yang-library")) {
Michal Vasko77367452021-02-16 16:32:18 +01001243 if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) {
Michal Vasko05532772021-06-03 12:12:38 +02001244 ERR(NULL, "Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported.");
Michal Vaskod5ada122020-03-19 18:28:06 +01001245 goto error;
Michal Vaskof0fba4e2020-02-14 17:15:31 +01001246 }
Michal Vaskod5ada122020-03-19 18:28:06 +01001247
Michal Vasko1440a742021-03-31 11:11:03 +02001248 /* get content-id */
1249 if (server_opts.content_id_clb) {
1250 yl_content_id = server_opts.content_id_clb(server_opts.content_id_data);
roman124a4362023-10-26 15:36:22 +02001251 NC_CHECK_ERRMEM_GOTO(!yl_content_id, , error);
Michal Vasko1440a742021-03-31 11:11:03 +02001252 } else {
1253 yl_content_id = malloc(11);
roman124a4362023-10-26 15:36:22 +02001254 NC_CHECK_ERRMEM_GOTO(!yl_content_id, , error);
Michal Vasko1440a742021-03-31 11:11:03 +02001255 sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx));
1256 }
1257
Michal Vasko77367452021-02-16 16:32:18 +01001258 if (!strcmp(mod->revision, "2019-01-04")) {
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001259 /* new one (capab defined in RFC 8526 section 2) */
Michal Vasko1440a742021-03-31 11:11:03 +02001260 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s",
1261 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001262 add_cpblt(str, &cpblts, &size, &count);
Michal Vasko7b5e3d92020-04-08 14:40:31 +02001263 } else {
1264 /* old one (capab defined in RFC 7950 section 5.6.4) */
Michal Vasko1440a742021-03-31 11:11:03 +02001265 sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s",
1266 mod->revision, yl_content_id);
Michal Vasko93224072021-11-09 12:14:28 +01001267 add_cpblt(str, &cpblts, &size, &count);
Michal Vaskod5ada122020-03-19 18:28:06 +01001268 }
Michal Vasko1440a742021-03-31 11:11:03 +02001269 free(yl_content_id);
Radek Krejci24a18412018-05-16 15:09:10 +02001270 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001271 } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001272 /* skip YANG 1.1 modules */
Radek Krejci24a18412018-05-16 15:09:10 +02001273 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001274 } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) {
Michal Vasko5ca5d972022-09-14 13:51:31 +02001275 /* skip YANG 1.0 modules */
Radek Krejci24a18412018-05-16 15:09:10 +02001276 continue;
1277 }
Michal Vasko086311b2016-01-08 09:53:11 +01001278
Michal Vasko77367452021-02-16 16:32:18 +01001279 str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "",
1280 mod->revision ? mod->revision : "");
Radek Krejci24a18412018-05-16 15:09:10 +02001281
Michal Vaskodafdc742020-03-11 16:15:59 +01001282 features_count = 0;
1283 i = 0;
1284 feat = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001285 while ((feat = lysp_feature_next(feat, mod->parsed, &i))) {
Michal Vaskodafdc742020-03-11 16:15:59 +01001286 if (!(feat->flags & LYS_FENABLED)) {
1287 continue;
Michal Vaskoe90e4d12016-06-20 10:05:01 +02001288 }
Michal Vaskodafdc742020-03-11 16:15:59 +01001289 if (!features_count) {
1290 strcat(str, "&features=");
1291 str_len += 10;
1292 }
1293 len = strlen(feat->name);
1294 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1295 ERRINT;
1296 break;
1297 }
1298 if (features_count) {
1299 strcat(str, ",");
1300 ++str_len;
1301 }
1302 strcat(str, feat->name);
1303 str_len += len;
1304 features_count++;
Michal Vasko086311b2016-01-08 09:53:11 +01001305 }
Michal Vasko086311b2016-01-08 09:53:11 +01001306
Michal Vasko77367452021-02-16 16:32:18 +01001307 if (mod->deviated_by) {
Radek Krejci24a18412018-05-16 15:09:10 +02001308 strcat(str, "&deviations=");
1309 str_len += 12;
1310 dev_count = 0;
Michal Vasko77367452021-02-16 16:32:18 +01001311 LY_ARRAY_FOR(mod->deviated_by, v) {
1312 len = strlen(mod->deviated_by[v]->name);
1313 if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) {
1314 ERRINT;
1315 break;
Radek Krejci24a18412018-05-16 15:09:10 +02001316 }
Michal Vasko77367452021-02-16 16:32:18 +01001317 if (dev_count) {
1318 strcat(str, ",");
1319 ++str_len;
Radek Krejci24a18412018-05-16 15:09:10 +02001320 }
Michal Vasko77367452021-02-16 16:32:18 +01001321 strcat(str, mod->deviated_by[v]->name);
1322 str_len += len;
1323 dev_count++;
Radek Krejci24a18412018-05-16 15:09:10 +02001324 }
1325 }
1326
Michal Vasko93224072021-11-09 12:14:28 +01001327 add_cpblt(str, &cpblts, &size, &count);
Radek Krejci24a18412018-05-16 15:09:10 +02001328 }
Michal Vasko086311b2016-01-08 09:53:11 +01001329
1330 /* ending NULL capability */
Michal Vasko93224072021-11-09 12:14:28 +01001331 add_cpblt(NULL, &cpblts, &size, &count);
Michal Vasko086311b2016-01-08 09:53:11 +01001332
1333 return cpblts;
Radek Krejcif906e412017-09-22 14:44:45 +02001334
1335error:
Radek Krejcif906e412017-09-22 14:44:45 +02001336 free(cpblts);
Radek Krejcif906e412017-09-22 14:44:45 +02001337 return NULL;
Michal Vasko086311b2016-01-08 09:53:11 +01001338}
1339
Michal Vasko93224072021-11-09 12:14:28 +01001340API char **
1341nc_server_get_cpblts(const struct ly_ctx *ctx)
Radek Krejci24a18412018-05-16 15:09:10 +02001342{
1343 return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF);
1344}
1345
Radek Krejci695d4fa2015-10-22 13:23:54 +02001346static int
Michal Vasko77367452021-02-16 16:32:18 +01001347parse_cpblts(struct lyd_node *capabilities, char ***list)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001348{
Michal Vasko77367452021-02-16 16:32:18 +01001349 struct lyd_node *iter;
1350 struct lyd_node_opaq *cpblt;
Michal Vasko156d3272017-04-11 11:46:49 +02001351 int ver = -1, i = 0;
1352 const char *cpb_start, *cpb_end;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001353
1354 if (list) {
1355 /* get the storage for server's capabilities */
Michal Vasko77367452021-02-16 16:32:18 +01001356 LY_LIST_FOR(lyd_child(capabilities), iter) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001357 i++;
1358 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +01001359 /* last item remains NULL */
1360 *list = calloc(i + 1, sizeof **list);
roman3a95bb22023-10-26 11:07:17 +02001361 NC_CHECK_ERRMEM_RET(!*list, -1);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001362 i = 0;
1363 }
1364
Michal Vasko77367452021-02-16 16:32:18 +01001365 LY_LIST_FOR(lyd_child(capabilities), iter) {
1366 cpblt = (struct lyd_node_opaq *)iter;
1367
1368 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 +02001369 ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001370 return -1;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001371 }
1372
Michal Vasko156d3272017-04-11 11:46:49 +02001373 /* skip leading/trailing whitespaces */
Michal Vasko77367452021-02-16 16:32:18 +01001374 for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {}
1375 for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {}
1376 if (!cpb_start[0] || (cpb_end == cpblt->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001377 ERR(NULL, "Empty capability \"%s\" received.", cpblt->value);
Michal Vasko156d3272017-04-11 11:46:49 +02001378 return -1;
1379 }
1380
Radek Krejci695d4fa2015-10-22 13:23:54 +02001381 /* detect NETCONF version */
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001382 if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) {
Radek Krejci695d4fa2015-10-22 13:23:54 +02001383 ver = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001384 } 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 +02001385 ver = 1;
1386 }
1387
1388 /* store capabilities */
1389 if (list) {
Michal Vasko156d3272017-04-11 11:46:49 +02001390 (*list)[i] = strndup(cpb_start, cpb_end - cpb_start);
roman3a95bb22023-10-26 11:07:17 +02001391 NC_CHECK_ERRMEM_RET(!(*list)[i], -1);
Radek Krejci695d4fa2015-10-22 13:23:54 +02001392 i++;
1393 }
1394 }
1395
1396 if (ver == -1) {
Michal Vasko05532772021-06-03 12:12:38 +02001397 ERR(NULL, "Peer does not support a compatible NETCONF version.");
Radek Krejci695d4fa2015-10-22 13:23:54 +02001398 }
1399
1400 return ver;
1401}
1402
1403static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001404nc_send_hello_io(struct nc_session *session)
Michal Vasko086311b2016-01-08 09:53:11 +01001405{
Michal Vasko131120a2018-05-29 15:44:02 +02001406 NC_MSG_TYPE ret;
Michal Vaskocf898172024-01-15 15:04:28 +01001407 int i, timeout_io;
Michal Vasko93224072021-11-09 12:14:28 +01001408 char **cpblts;
Michal Vasko131120a2018-05-29 15:44:02 +02001409 uint32_t *sid;
Michal Vasko086311b2016-01-08 09:53:11 +01001410
Michal Vasko131120a2018-05-29 15:44:02 +02001411 if (session->side == NC_CLIENT) {
1412 /* client side hello - send only NETCONF base capabilities */
1413 cpblts = malloc(3 * sizeof *cpblts);
roman3a95bb22023-10-26 11:07:17 +02001414 NC_CHECK_ERRMEM_RET(!cpblts, NC_MSG_ERROR);
Michal Vasko93224072021-11-09 12:14:28 +01001415 cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0");
1416 cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1");
Michal Vasko131120a2018-05-29 15:44:02 +02001417 cpblts[2] = NULL;
1418
Michal Vaskocf898172024-01-15 15:04:28 +01001419 timeout_io = NC_CLIENT_HELLO_TIMEOUT * 1000;
Michal Vasko131120a2018-05-29 15:44:02 +02001420 sid = NULL;
1421 } else {
Michal Vasko77367452021-02-16 16:32:18 +01001422 cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0);
Michal Vasko5b24b6b2020-12-04 09:29:47 +01001423 if (!cpblts) {
1424 return NC_MSG_ERROR;
1425 }
Michal Vasko131120a2018-05-29 15:44:02 +02001426
Michal Vaskocf898172024-01-15 15:04:28 +01001427 if (session->flags & NC_SESSION_CALLHOME) {
1428 timeout_io = NC_SERVER_CH_HELLO_TIMEOUT * 1000;
1429 } else {
Michal Vasko5c1c9b32024-02-02 09:02:42 +01001430 timeout_io = server_opts.idle_timeout ? server_opts.idle_timeout * 1000 : -1;
Michal Vaskocf898172024-01-15 15:04:28 +01001431 }
Michal Vasko131120a2018-05-29 15:44:02 +02001432 sid = &session->id;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001433 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001434
Michal Vaskocf898172024-01-15 15:04:28 +01001435 ret = nc_write_msg_io(session, timeout_io, NC_MSG_HELLO, cpblts, sid);
Michal Vasko086311b2016-01-08 09:53:11 +01001436
Michal Vasko086311b2016-01-08 09:53:11 +01001437 for (i = 0; cpblts[i]; ++i) {
Michal Vasko93224072021-11-09 12:14:28 +01001438 free(cpblts[i]);
Michal Vasko086311b2016-01-08 09:53:11 +01001439 }
1440 free(cpblts);
1441
Michal Vasko131120a2018-05-29 15:44:02 +02001442 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +01001443}
1444
1445static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001446nc_recv_client_hello_io(struct nc_session *session)
Radek Krejci695d4fa2015-10-22 13:23:54 +02001447{
Michal Vasko77367452021-02-16 16:32:18 +01001448 struct ly_in *msg;
1449 struct lyd_node *hello = NULL, *iter;
1450 struct lyd_node_opaq *node;
1451 int r, ver = -1, flag = 0;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001452 char *str;
Michal Vasko292c5542023-02-01 14:33:17 +01001453 long long id;
Michal Vasko77367452021-02-16 16:32:18 +01001454 NC_MSG_TYPE rc = NC_MSG_HELLO;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001455
Michal Vasko77367452021-02-16 16:32:18 +01001456 r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg);
1457 switch (r) {
1458 case 1:
Radek Krejci695d4fa2015-10-22 13:23:54 +02001459 /* parse <hello> data */
Michal Vasko77367452021-02-16 16:32:18 +01001460 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 +02001461 ERR(session, "Failed to parse server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001462 rc = NC_MSG_ERROR;
1463 goto cleanup;
1464 }
1465
1466 LY_LIST_FOR(lyd_child(hello), iter) {
1467 node = (struct lyd_node_opaq *)iter;
1468
1469 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001470 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001471 } else if (!strcmp(node->name.name, "session-id")) {
1472 if (!node->value || !strlen(node->value)) {
Michal Vasko05532772021-06-03 12:12:38 +02001473 ERR(session, "No value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001474 rc = NC_MSG_ERROR;
1475 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001476 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001477 str = NULL;
Michal Vasko77367452021-02-16 16:32:18 +01001478 id = strtoll(node->value, &str, 10);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001479 if (*str || (id < 1) || (id > UINT32_MAX)) {
Michal Vasko05532772021-06-03 12:12:38 +02001480 ERR(session, "Invalid value of <session-id> element in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001481 rc = NC_MSG_ERROR;
1482 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001483 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001484 session->id = (uint32_t)id;
1485 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001486 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001487 ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001488 rc = NC_MSG_ERROR;
1489 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001490 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001491
1492 if (flag) {
1493 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001494 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001495 rc = NC_MSG_ERROR;
1496 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001497 }
1498 flag = 1;
1499
Michal Vasko77367452021-02-16 16:32:18 +01001500 if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) {
1501 rc = NC_MSG_ERROR;
1502 goto cleanup;
Michal Vasko11d142a2016-01-19 15:58:24 +01001503 }
1504 session->version = ver;
1505 }
1506
1507 if (!session->id) {
Michal Vasko05532772021-06-03 12:12:38 +02001508 ERR(session, "Missing <session-id> in server <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001509 rc = NC_MSG_ERROR;
1510 goto cleanup;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001511 }
1512 break;
Michal Vasko77367452021-02-16 16:32:18 +01001513 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001514 ERR(session, "Server <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001515 rc = NC_MSG_WOULDBLOCK;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001516 break;
1517 default:
Michal Vasko77367452021-02-16 16:32:18 +01001518 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001519 break;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001520 }
1521
Michal Vasko77367452021-02-16 16:32:18 +01001522cleanup:
1523 ly_in_free(msg, 1);
1524 lyd_free_tree(hello);
1525 return rc;
Radek Krejci5686ff72015-10-09 13:33:56 +02001526}
1527
Michal Vasko11d142a2016-01-19 15:58:24 +01001528static NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001529nc_recv_server_hello_io(struct nc_session *session)
Michal Vasko11d142a2016-01-19 15:58:24 +01001530{
Michal Vasko77367452021-02-16 16:32:18 +01001531 struct ly_in *msg;
1532 struct lyd_node *hello = NULL, *iter;
1533 struct lyd_node_opaq *node;
1534 NC_MSG_TYPE rc = NC_MSG_HELLO;
1535 int r, ver = -1, flag = 0, timeout_io;
Michal Vasko11d142a2016-01-19 15:58:24 +01001536
Michal Vaskocf898172024-01-15 15:04:28 +01001537 if (session->flags & NC_SESSION_CALLHOME) {
1538 timeout_io = NC_SERVER_CH_HELLO_TIMEOUT * 1000;
1539 } else {
Michal Vasko5c1c9b32024-02-02 09:02:42 +01001540 timeout_io = server_opts.idle_timeout ? server_opts.idle_timeout * 1000 : -1;
Michal Vaskocf898172024-01-15 15:04:28 +01001541 }
1542
Michal Vasko77367452021-02-16 16:32:18 +01001543 r = nc_read_msg_poll_io(session, timeout_io, &msg);
1544 switch (r) {
1545 case 1:
1546 /* parse <hello> data */
1547 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 +02001548 ERR(session, "Failed to parse client <hello>.");
Michal Vasko77367452021-02-16 16:32:18 +01001549 rc = NC_MSG_ERROR;
1550 goto cleanup;
1551 }
Michal Vasko11d142a2016-01-19 15:58:24 +01001552
Michal Vasko77367452021-02-16 16:32:18 +01001553 /* learn NETCONF version */
1554 LY_LIST_FOR(lyd_child(hello), iter) {
1555 node = (struct lyd_node_opaq *)iter;
1556
1557 if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) {
Michal Vasko11d142a2016-01-19 15:58:24 +01001558 continue;
Michal Vasko77367452021-02-16 16:32:18 +01001559 } else if (strcmp(node->name.name, "capabilities")) {
Michal Vasko05532772021-06-03 12:12:38 +02001560 ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name);
Michal Vasko77367452021-02-16 16:32:18 +01001561 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001562 goto cleanup;
1563 }
1564
1565 if (flag) {
1566 /* multiple capabilities elements */
Michal Vasko05532772021-06-03 12:12:38 +02001567 ERR(session, "Invalid <hello> message (multiple <capabilities> elements).");
Michal Vasko77367452021-02-16 16:32:18 +01001568 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001569 goto cleanup;
1570 }
1571 flag = 1;
1572
Michal Vasko77367452021-02-16 16:32:18 +01001573 if ((ver = parse_cpblts(&node->node, NULL)) < 0) {
1574 rc = NC_MSG_BAD_HELLO;
Michal Vasko11d142a2016-01-19 15:58:24 +01001575 goto cleanup;
1576 }
1577 session->version = ver;
1578 }
1579 break;
Michal Vasko77367452021-02-16 16:32:18 +01001580 case 0:
Michal Vasko05532772021-06-03 12:12:38 +02001581 ERR(session, "Client <hello> timeout elapsed.");
Michal Vasko77367452021-02-16 16:32:18 +01001582 rc = NC_MSG_WOULDBLOCK;
Michal Vasko5e6f4cc2016-01-20 13:27:44 +01001583 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001584 default:
Michal Vasko77367452021-02-16 16:32:18 +01001585 rc = NC_MSG_ERROR;
Michal Vasko71090fc2016-05-24 16:37:28 +02001586 break;
Michal Vasko11d142a2016-01-19 15:58:24 +01001587 }
1588
1589cleanup:
Michal Vasko77367452021-02-16 16:32:18 +01001590 ly_in_free(msg, 1);
1591 lyd_free_tree(hello);
1592 return rc;
Michal Vasko11d142a2016-01-19 15:58:24 +01001593}
1594
Michal Vasko71090fc2016-05-24 16:37:28 +02001595NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +02001596nc_handshake_io(struct nc_session *session)
Michal Vasko80cad7f2015-12-08 14:42:27 +01001597{
Michal Vasko086311b2016-01-08 09:53:11 +01001598 NC_MSG_TYPE type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001599
Michal Vasko131120a2018-05-29 15:44:02 +02001600 type = nc_send_hello_io(session);
Michal Vasko086311b2016-01-08 09:53:11 +01001601 if (type != NC_MSG_HELLO) {
Michal Vasko71090fc2016-05-24 16:37:28 +02001602 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001603 }
1604
Michal Vasko11d142a2016-01-19 15:58:24 +01001605 if (session->side == NC_CLIENT) {
Michal Vasko131120a2018-05-29 15:44:02 +02001606 type = nc_recv_client_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001607 } else {
Michal Vasko131120a2018-05-29 15:44:02 +02001608 type = nc_recv_server_hello_io(session);
Michal Vasko11d142a2016-01-19 15:58:24 +01001609 }
1610
Michal Vasko71090fc2016-05-24 16:37:28 +02001611 return type;
Michal Vasko80cad7f2015-12-08 14:42:27 +01001612}