Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @file session.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libnetconf2 - general session functions |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 6 | * @copyright |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2023 CESNET, z.s.p.o. |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 8 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 9 | * 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 Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 12 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 13 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 14 | */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 16 | |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 17 | #include <assert.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 19 | #include <errno.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 20 | #include <libyang/libyang.h> |
roman | 3c5b75d | 2024-04-18 14:56:53 +0200 | [diff] [blame] | 21 | #include <netinet/in.h> |
| 22 | #include <netinet/tcp.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 23 | #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 Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 29 | #include <time.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 30 | #include <unistd.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 31 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 32 | #include "compat.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 33 | #include "config.h" |
| 34 | #include "log_p.h" |
| 35 | #include "netconf.h" |
| 36 | #include "session_p.h" |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 37 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 38 | #ifdef NC_ENABLED_SSH_TLS |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 39 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 40 | #include <libssh/libssh.h> |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 41 | #include <openssl/bio.h> |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 42 | #include <openssl/conf.h> |
| 43 | #include <openssl/err.h> |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 44 | #include <openssl/evp.h> |
| 45 | #include <openssl/x509.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 46 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 47 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 48 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 49 | /* in seconds */ |
| 50 | #define NC_CLIENT_HELLO_TIMEOUT 60 |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 51 | #define NC_SERVER_CH_HELLO_TIMEOUT 180 |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 52 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 53 | /* in milliseconds */ |
| 54 | #define NC_CLOSE_REPLY_TIMEOUT 200 |
| 55 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 56 | extern struct nc_server_opts server_opts; |
| 57 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 58 | void |
| 59 | nc_timeouttime_get(struct timespec *ts, uint32_t add_ms) |
Radek Krejci | 7ac1605 | 2016-07-15 11:48:18 +0200 | [diff] [blame] | 60 | { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 61 | 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 | |
| 86 | int32_t |
| 87 | nc_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 | |
| 100 | void |
| 101 | nc_realtime_get(struct timespec *ts) |
| 102 | { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 103 | if (clock_gettime(CLOCK_REALTIME, ts)) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 104 | ERR(NULL, "clock_gettime() failed (%s).", strerror(errno)); |
| 105 | return; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 106 | } |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Michal Vasko | 63b92d6 | 2024-04-29 10:04:56 +0200 | [diff] [blame] | 109 | int |
| 110 | nc_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 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 142 | #ifdef NC_ENABLED_SSH_TLS |
| 143 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 144 | const char * |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 145 | nc_privkey_format_to_str(NC_PRIVKEY_FORMAT format) |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 146 | { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 147 | switch (format) { |
| 148 | case NC_PRIVKEY_FORMAT_RSA: |
roman | d0fe595 | 2024-03-21 15:59:33 +0100 | [diff] [blame] | 149 | return " RSA "; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 150 | case NC_PRIVKEY_FORMAT_EC: |
roman | d0fe595 | 2024-03-21 15:59:33 +0100 | [diff] [blame] | 151 | return " EC "; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 152 | case NC_PRIVKEY_FORMAT_X509: |
roman | d0fe595 | 2024-03-21 15:59:33 +0100 | [diff] [blame] | 153 | return " "; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 154 | case NC_PRIVKEY_FORMAT_OPENSSH: |
roman | d0fe595 | 2024-03-21 15:59:33 +0100 | [diff] [blame] | 155 | return " OPENSSH "; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 156 | default: |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 157 | return NULL; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 158 | } |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 159 | } |
| 160 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 161 | int |
| 162 | nc_base64_to_bin(const char *base64, char **bin) |
| 163 | { |
roman | 46172ac | 2023-11-08 15:12:40 +0100 | [diff] [blame] | 164 | BIO *bio, *bio64 = NULL; |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 165 | size_t used = 0, size = 0, r = 0; |
| 166 | void *tmp = NULL; |
roman | 46172ac | 2023-11-08 15:12:40 +0100 | [diff] [blame] | 167 | int nl_count, i, remainder, ret = 0; |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 168 | 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); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 174 | NC_CHECK_ERRMEM_RET(!b64, -1); |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 175 | |
| 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())); |
roman | 46172ac | 2023-11-08 15:12:40 +0100 | [diff] [blame] | 188 | ret = -1; |
| 189 | goto cleanup; |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 190 | } |
| 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())); |
roman | 46172ac | 2023-11-08 15:12:40 +0100 | [diff] [blame] | 195 | ret = -1; |
| 196 | goto cleanup; |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 197 | } |
| 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) { |
roman | 46172ac | 2023-11-08 15:12:40 +0100 | [diff] [blame] | 208 | ERRMEM; |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 209 | free(*bin); |
roman | 46172ac | 2023-11-08 15:12:40 +0100 | [diff] [blame] | 210 | *bin = NULL; |
| 211 | ret = -1; |
| 212 | goto cleanup; |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 213 | } |
| 214 | *bin = tmp; |
| 215 | |
| 216 | r = BIO_read(bio64, *bin + used, 64); |
| 217 | used += r; |
| 218 | } while (r == 64); |
| 219 | |
roman | 46172ac | 2023-11-08 15:12:40 +0100 | [diff] [blame] | 220 | ret = size; |
| 221 | |
| 222 | cleanup: |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 223 | free(b64); |
| 224 | BIO_free_all(bio64); |
roman | 46172ac | 2023-11-08 15:12:40 +0100 | [diff] [blame] | 225 | return ret; |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 226 | } |
| 227 | |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 228 | int |
| 229 | nc_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 | |
| 257 | cleanup: |
| 258 | EVP_PKEY_free(pkey); |
| 259 | free(bin); |
| 260 | return ret; |
| 261 | } |
| 262 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 263 | #endif /* NC_ENABLED_SSH_TLS */ |
| 264 | |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 265 | int |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 266 | nc_sock_configure_ka(int sock, int enabled) |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 267 | { |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 268 | if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &enabled, sizeof enabled) == -1) { |
| 269 | ERR(NULL, "Failed to set SO_KEEPALIVE (%s).", strerror(errno)); |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 270 | return -1; |
| 271 | } |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | int |
| 277 | nc_sock_configure_ka_idle_time(int sock, int idle_time) |
| 278 | { |
| 279 | if (idle_time < 1) { |
| 280 | ERRARG(NULL, "idle_time"); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 281 | } |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 282 | |
| 283 | #ifdef TCP_KEEPIDLE |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 284 | 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 Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 286 | return -1; |
| 287 | } |
| 288 | #endif |
| 289 | |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | int |
| 294 | nc_sock_configure_ka_max_probes(int sock, int max_probes) |
| 295 | { |
| 296 | if (max_probes < 1) { |
| 297 | ERRARG(NULL, "max_probes"); |
| 298 | } |
| 299 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 300 | #ifdef TCP_KEEPCNT |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 301 | 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 Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 303 | return -1; |
| 304 | } |
| 305 | #endif |
| 306 | |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | int |
| 311 | nc_sock_configure_ka_probe_interval(int sock, int probe_interval) |
| 312 | { |
| 313 | if (probe_interval < 1) { |
| 314 | ERRARG(NULL, "probe_interval"); |
| 315 | } |
| 316 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 317 | #ifdef TCP_KEEPINTVL |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 318 | 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 Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 320 | return -1; |
| 321 | } |
| 322 | #endif |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 327 | struct nc_session * |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 328 | nc_new_session(NC_SIDE side, int shared_ti) |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 329 | { |
| 330 | struct nc_session *sess; |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 331 | struct timespec ts_cur; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 332 | |
| 333 | sess = calloc(1, sizeof *sess); |
| 334 | if (!sess) { |
| 335 | return NULL; |
| 336 | } |
| 337 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 338 | sess->side = side; |
| 339 | |
| 340 | if (side == NC_SERVER) { |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 341 | pthread_mutex_init(&sess->opts.server.ntf_status_lock, NULL); |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 342 | pthread_mutex_init(&sess->opts.server.rpc_lock, NULL); |
| 343 | pthread_cond_init(&sess->opts.server.rpc_cond, NULL); |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 344 | |
| 345 | pthread_mutex_init(&sess->opts.server.ch_lock, NULL); |
| 346 | pthread_cond_init(&sess->opts.server.ch_cond, NULL); |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 347 | |
| 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-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 351 | } else { |
| 352 | pthread_mutex_init(&sess->opts.client.msgs_lock, NULL); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 353 | } |
| 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 Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | return sess; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 364 | |
| 365 | error: |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 366 | free(sess); |
| 367 | return NULL; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 368 | } |
| 369 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 370 | /* |
| 371 | * @return 1 - success |
| 372 | * 0 - timeout |
| 373 | * -1 - error |
| 374 | */ |
| 375 | int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 376 | nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func) |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 377 | { |
| 378 | int ret; |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 379 | struct timespec ts_timeout; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 380 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 381 | if (session->side != NC_SERVER) { |
| 382 | ERRINT; |
| 383 | return -1; |
| 384 | } |
| 385 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 386 | if (timeout > 0) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 387 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 388 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 389 | /* LOCK */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 390 | ret = pthread_mutex_clocklock(&session->opts.server.rpc_lock, COMPAT_CLOCK_ID, &ts_timeout); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 391 | if (!ret) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 392 | while (session->opts.server.rpc_inuse) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 393 | ret = pthread_cond_clockwait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock, |
| 394 | COMPAT_CLOCK_ID, &ts_timeout); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 395 | if (ret) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 396 | pthread_mutex_unlock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 397 | break; |
| 398 | } |
| 399 | } |
| 400 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 401 | } else if (!timeout) { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 402 | /* LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 403 | ret = pthread_mutex_trylock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 404 | if (!ret) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 405 | if (session->opts.server.rpc_inuse) { |
| 406 | pthread_mutex_unlock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 407 | return 0; |
| 408 | } |
Michal vasko | 2f8e4b5 | 2016-10-05 13:04:11 +0200 | [diff] [blame] | 409 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 410 | } else { /* timeout == -1 */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 411 | /* LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 412 | ret = pthread_mutex_lock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 413 | if (!ret) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 414 | while (session->opts.server.rpc_inuse) { |
| 415 | ret = pthread_cond_wait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 416 | if (ret) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 417 | pthread_mutex_unlock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 418 | break; |
| 419 | } |
| 420 | } |
| 421 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 422 | } |
| 423 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 424 | if (ret) { |
| 425 | if ((ret == EBUSY) || (ret == ETIMEDOUT)) { |
| 426 | /* timeout */ |
| 427 | return 0; |
| 428 | } |
| 429 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 430 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 431 | ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret)); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 432 | return -1; |
| 433 | } |
| 434 | |
| 435 | /* ok */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 436 | assert(session->opts.server.rpc_inuse == 0); |
| 437 | session->opts.server.rpc_inuse = 1; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 438 | |
| 439 | /* UNLOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 440 | ret = pthread_mutex_unlock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 441 | if (ret) { |
| 442 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 443 | ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret)); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 444 | return -1; |
| 445 | } |
| 446 | |
| 447 | return 1; |
| 448 | } |
| 449 | |
| 450 | int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 451 | nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func) |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 452 | { |
| 453 | int ret; |
| 454 | struct timespec ts_timeout; |
| 455 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 456 | if (session->side != NC_SERVER) { |
| 457 | ERRINT; |
| 458 | return -1; |
| 459 | } |
| 460 | |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 461 | assert(session->opts.server.rpc_inuse); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 462 | |
| 463 | if (timeout > 0) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 464 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 465 | |
| 466 | /* LOCK */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 467 | ret = pthread_mutex_clocklock(&session->opts.server.rpc_lock, COMPAT_CLOCK_ID, &ts_timeout); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 468 | } else if (!timeout) { |
| 469 | /* LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 470 | ret = pthread_mutex_trylock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 471 | } else { /* timeout == -1 */ |
| 472 | /* LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 473 | ret = pthread_mutex_lock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) { |
| 477 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 478 | ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret)); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 479 | return -1; |
| 480 | } else if (ret) { |
Michal Vasko | 69e9875 | 2022-12-14 14:20:17 +0100 | [diff] [blame] | 481 | WRN(session, "%s: session RPC lock timeout, should not happen.", func); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 482 | } |
| 483 | |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 484 | session->opts.server.rpc_inuse = 0; |
| 485 | pthread_cond_signal(&session->opts.server.rpc_cond); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 486 | |
| 487 | if (!ret) { |
| 488 | /* UNLOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 489 | ret = pthread_mutex_unlock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 490 | if (ret) { |
| 491 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 492 | ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret)); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 493 | return -1; |
| 494 | } |
| 495 | } |
| 496 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 497 | return 1; |
| 498 | } |
| 499 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 500 | int |
| 501 | nc_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 Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 507 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 508 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 509 | ret = pthread_mutex_clocklock(session->io_lock, COMPAT_CLOCK_ID, &ts_timeout); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 510 | } else if (!timeout) { |
| 511 | ret = pthread_mutex_trylock(session->io_lock); |
| 512 | } else { /* timeout == -1 */ |
Robin Jarry | 54ea296 | 2018-10-10 10:33:40 +0200 | [diff] [blame] | 513 | ret = pthread_mutex_lock(session->io_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | if (ret) { |
| 517 | if ((ret == EBUSY) || (ret == ETIMEDOUT)) { |
| 518 | /* timeout */ |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 523 | ERR(session, "%s: failed to IO lock a session (%s).", func, strerror(ret)); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 524 | return -1; |
| 525 | } |
| 526 | |
| 527 | return 1; |
| 528 | } |
| 529 | |
| 530 | int |
| 531 | nc_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 Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 538 | ERR(session, "%s: failed to IO unlock a session (%s).", func, strerror(ret)); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 539 | return -1; |
| 540 | } |
| 541 | |
| 542 | return 1; |
| 543 | } |
| 544 | |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 545 | int |
| 546 | nc_session_client_msgs_lock(struct nc_session *session, int *timeout, const char *func) |
| 547 | { |
| 548 | int ret; |
| 549 | int32_t diff_msec; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 550 | struct timespec ts_timeout, ts_start; |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 551 | |
| 552 | assert(session->side == NC_CLIENT); |
| 553 | |
| 554 | if (*timeout > 0) { |
| 555 | /* get current time */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 556 | nc_timeouttime_get(&ts_start, 0); |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 557 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 558 | nc_timeouttime_get(&ts_timeout, *timeout); |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 559 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 560 | ret = pthread_mutex_clocklock(&session->opts.client.msgs_lock, COMPAT_CLOCK_ID, &ts_timeout); |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 561 | if (!ret) { |
| 562 | /* update timeout based on what was elapsed */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 563 | diff_msec = nc_timeouttime_cur_diff(&ts_start); |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 564 | *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 | |
| 586 | int |
| 587 | nc_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 Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 603 | API NC_STATUS |
| 604 | nc_session_get_status(const struct nc_session *session) |
| 605 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 606 | NC_CHECK_ARG_RET(session, session, NC_STATUS_ERR); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 607 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 608 | return session->status; |
| 609 | } |
| 610 | |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 611 | API NC_SESSION_TERM_REASON |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 612 | nc_session_get_term_reason(const struct nc_session *session) |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 613 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 614 | NC_CHECK_ARG_RET(session, session, NC_SESSION_TERM_ERR); |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 615 | |
| 616 | return session->term_reason; |
| 617 | } |
| 618 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 619 | API uint32_t |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 620 | nc_session_get_killed_by(const struct nc_session *session) |
| 621 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 622 | NC_CHECK_ARG_RET(session, session, 0); |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 623 | |
| 624 | return session->killed_by; |
| 625 | } |
| 626 | |
| 627 | API uint32_t |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 628 | nc_session_get_id(const struct nc_session *session) |
| 629 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 630 | NC_CHECK_ARG_RET(session, session, 0); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 631 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 632 | return session->id; |
| 633 | } |
| 634 | |
Michal Vasko | 174fe8e | 2016-02-17 15:38:09 +0100 | [diff] [blame] | 635 | API int |
| 636 | nc_session_get_version(const struct nc_session *session) |
| 637 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 638 | NC_CHECK_ARG_RET(session, session, -1); |
Michal Vasko | 174fe8e | 2016-02-17 15:38:09 +0100 | [diff] [blame] | 639 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 640 | return session->version == NC_VERSION_10 ? 0 : 1; |
Michal Vasko | 174fe8e | 2016-02-17 15:38:09 +0100 | [diff] [blame] | 641 | } |
| 642 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 643 | API NC_TRANSPORT_IMPL |
| 644 | nc_session_get_ti(const struct nc_session *session) |
| 645 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 646 | NC_CHECK_ARG_RET(session, session, 0); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 647 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 648 | return session->ti_type; |
| 649 | } |
| 650 | |
| 651 | API const char * |
| 652 | nc_session_get_username(const struct nc_session *session) |
| 653 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 654 | NC_CHECK_ARG_RET(session, session, NULL); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 655 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 656 | return session->username; |
| 657 | } |
| 658 | |
| 659 | API const char * |
| 660 | nc_session_get_host(const struct nc_session *session) |
| 661 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 662 | NC_CHECK_ARG_RET(session, session, NULL); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 663 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 664 | return session->host; |
| 665 | } |
| 666 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 667 | API const char * |
| 668 | nc_session_get_path(const struct nc_session *session) |
| 669 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 670 | NC_CHECK_ARG_RET(session, session, NULL); |
| 671 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 672 | if (session->ti_type != NC_TI_UNIX) { |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 673 | return NULL; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 674 | } |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 675 | |
| 676 | return session->path; |
| 677 | } |
| 678 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 679 | API uint16_t |
| 680 | nc_session_get_port(const struct nc_session *session) |
| 681 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 682 | NC_CHECK_ARG_RET(session, session, 0); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 683 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 684 | return session->port; |
| 685 | } |
| 686 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 687 | API const struct ly_ctx * |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 688 | nc_session_get_ctx(const struct nc_session *session) |
| 689 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 690 | NC_CHECK_ARG_RET(session, session, NULL); |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 691 | |
| 692 | return session->ctx; |
| 693 | } |
| 694 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 695 | API void |
| 696 | nc_session_set_data(struct nc_session *session, void *data) |
| 697 | { |
| 698 | if (!session) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 699 | ERRARG(NULL, "session"); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 700 | return; |
| 701 | } |
| 702 | |
| 703 | session->data = data; |
| 704 | } |
| 705 | |
| 706 | API void * |
| 707 | nc_session_get_data(const struct nc_session *session) |
| 708 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 709 | NC_CHECK_ARG_RET(session, session, NULL); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 710 | |
| 711 | return session->data; |
| 712 | } |
| 713 | |
Michal Vasko | dc96bb9 | 2023-03-28 08:52:48 +0200 | [diff] [blame] | 714 | API int |
| 715 | nc_session_is_callhome(const struct nc_session *session) |
| 716 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 717 | NC_CHECK_ARG_RET(session, session, 0); |
Michal Vasko | dc96bb9 | 2023-03-28 08:52:48 +0200 | [diff] [blame] | 718 | |
| 719 | if (session->flags & NC_SESSION_CALLHOME) { |
| 720 | return 1; |
| 721 | } |
| 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 726 | NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 727 | nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 728 | { |
Michal Vasko | 7e1f5fb | 2021-11-10 10:14:45 +0100 | [diff] [blame] | 729 | 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 Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 731 | return NC_MSG_ERROR; |
Michal Vasko | 7df39ec | 2015-12-09 15:26:24 +0100 | [diff] [blame] | 732 | } |
| 733 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 734 | return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL); |
Michal Vasko | 7df39ec | 2015-12-09 15:26:24 +0100 | [diff] [blame] | 735 | } |
| 736 | |
Michal Vasko | d4da363 | 2022-05-25 11:49:10 +0200 | [diff] [blame] | 737 | /** |
| 738 | * @brief Send \<close-session\> and read the reply on a session. |
| 739 | * |
| 740 | * @param[in] session Closing NETCONF session. |
| 741 | */ |
| 742 | static void |
| 743 | nc_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 Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 751 | WRN(session, "Missing ietf-netconf module in context, unable to send <close-session>."); |
Michal Vasko | d4da363 | 2022-05-25 11:49:10 +0200 | [diff] [blame] | 752 | 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 | |
| 762 | read_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 Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 791 | /** |
| 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 | */ |
| 797 | static void |
| 798 | nc_session_free_transport(struct nc_session *session, int *multisession) |
| 799 | { |
| 800 | int connected; /* flag to indicate whether the transport socket is still connected */ |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 801 | int sock = -1; |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 802 | 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 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 825 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 826 | case NC_TI_LIBSSH: { |
| 827 | int r; |
| 828 | |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 829 | if (connected) { |
Michal Vasko | 6473440 | 2022-09-09 11:22:00 +0200 | [diff] [blame] | 830 | ssh_channel_send_eof(session->ti.libssh.channel); |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 831 | 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 Vasko | 70e9062 | 2023-12-07 08:43:14 +0100 | [diff] [blame] | 869 | /* does not close sock */ |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 870 | ssh_disconnect(session->ti.libssh.session); |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 871 | } |
| 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 Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /* SESSION IO UNLOCK */ |
| 886 | if (r == 1) { |
| 887 | nc_session_io_unlock(session, __func__); |
| 888 | } |
| 889 | break; |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 890 | } |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 891 | 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; |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 904 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 905 | case NC_TI_NONE: |
| 906 | break; |
| 907 | } |
| 908 | |
| 909 | /* close socket separately */ |
| 910 | if (sock > -1) { |
| 911 | close(sock); |
| 912 | } |
| 913 | } |
| 914 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 915 | API void |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 916 | nc_session_free(struct nc_session *session, void (*data_free)(void *)) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 917 | { |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 918 | int r, i, rpc_locked = 0, msgs_locked = 0, timeout; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 919 | int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 920 | struct nc_msg_cont *contiter; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 921 | struct ly_in *msg; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 922 | struct timespec ts; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 923 | void *p; |
| 924 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 925 | if (!session || (session->status == NC_STATUS_CLOSING)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 926 | return; |
| 927 | } |
| 928 | |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 929 | /* 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 Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 933 | |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 934 | /* wait for them */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 935 | nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT); |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 936 | while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_count)) { |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 937 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 938 | if (nc_timeouttime_cur_diff(&ts) < 1) { |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 939 | ERR(session, "Waiting for notification thread exit failed (timed out)."); |
| 940 | break; |
| 941 | } |
| 942 | } |
Michal Vasko | 86d357c | 2016-03-11 13:46:38 +0100 | [diff] [blame] | 943 | } |
| 944 | |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 945 | if (session->side == NC_SERVER) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 946 | r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 947 | if (r == -1) { |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 948 | return; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 949 | } else if (r) { |
| 950 | rpc_locked = 1; |
Michal Vasko | 96a28a3 | 2021-02-04 15:35:20 +0100 | [diff] [blame] | 951 | } else { |
| 952 | /* else failed to lock it, too bad */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 953 | ERR(session, "Freeing a session while an RPC is being processed."); |
Michal Vasko | 96a28a3 | 2021-02-04 15:35:20 +0100 | [diff] [blame] | 954 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 955 | } |
| 956 | |
Michal Vasko | 8c24782 | 2020-09-07 13:23:23 +0200 | [diff] [blame] | 957 | if (session->side == NC_CLIENT) { |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 958 | timeout = NC_SESSION_FREE_LOCK_TIMEOUT; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 959 | |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 960 | /* 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-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 972 | for (contiter = session->opts.client.msgs; contiter; ) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 973 | ly_in_free(contiter->msg, 1); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 974 | |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 975 | p = contiter; |
| 976 | contiter = contiter->next; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 977 | free(p); |
| 978 | } |
| 979 | |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 980 | if (msgs_locked) { |
| 981 | /* MSGS UNLOCK */ |
| 982 | nc_session_client_msgs_unlock(session, __func__); |
| 983 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 984 | |
Michal Vasko | 8c24782 | 2020-09-07 13:23:23 +0200 | [diff] [blame] | 985 | if (session->status == NC_STATUS_RUNNING) { |
Michal Vasko | 9c6d38c | 2021-09-03 13:02:53 +0200 | [diff] [blame] | 986 | /* receive any leftover messages */ |
| 987 | while (nc_read_msg_poll_io(session, 0, &msg) == 1) { |
| 988 | ly_in_free(msg, 1); |
| 989 | } |
| 990 | |
Michal Vasko | 8c24782 | 2020-09-07 13:23:23 +0200 | [diff] [blame] | 991 | /* send closing info to the other side */ |
Michal Vasko | d4da363 | 2022-05-25 11:49:10 +0200 | [diff] [blame] | 992 | nc_session_free_close_session(session); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | /* list of server's capabilities */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 996 | if (session->opts.client.cpblts) { |
| 997 | for (i = 0; session->opts.client.cpblts[i]; i++) { |
Michal Vasko | 96fc4bb | 2017-05-23 14:58:34 +0200 | [diff] [blame] | 998 | free(session->opts.client.cpblts[i]); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 999 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1000 | free(session->opts.client.cpblts); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1001 | } |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1002 | |
| 1003 | /* LY ext data */ |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1004 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 1005 | struct nc_session *siter; |
| 1006 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1007 | 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 Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 1017 | } else |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 1018 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 1019 | { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 1020 | lyd_free_siblings(session->opts.client.ext_data); |
| 1021 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1022 | } |
| 1023 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1024 | if (session->data && data_free) { |
| 1025 | data_free(session->data); |
| 1026 | } |
| 1027 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 1028 | if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) { |
| 1029 | /* CH LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 1030 | pthread_mutex_lock(&session->opts.server.ch_lock); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 1031 | } |
| 1032 | |
Michal Vasko | 86d357c | 2016-03-11 13:46:38 +0100 | [diff] [blame] | 1033 | /* mark session for closing */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1034 | session->status = NC_STATUS_CLOSING; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 1035 | |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 1036 | if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CH_THREAD)) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 1037 | pthread_cond_signal(&session->opts.server.ch_cond); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 1038 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1039 | nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT); |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 1040 | |
| 1041 | /* wait for CH thread to actually wake up and terminate */ |
| 1042 | r = 0; |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 1043 | while (!r && (session->flags & NC_SESSION_CH_THREAD)) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1044 | r = pthread_cond_clockwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, COMPAT_CLOCK_ID, &ts); |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 1045 | } |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 1046 | if (r) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1047 | ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r)); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 1048 | } |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 1049 | } |
| 1050 | |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 1051 | 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 Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1056 | /* transport implementation cleanup */ |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 1057 | nc_session_free_transport(session, &multisession); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1058 | |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 1059 | /* final cleanup */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1060 | free(session->username); |
| 1061 | free(session->host); |
| 1062 | free(session->path); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1063 | |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 1064 | if (session->side == NC_SERVER) { |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 1065 | pthread_mutex_destroy(&session->opts.server.ntf_status_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1066 | if (rpc_locked) { |
| 1067 | nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | 9e99f01 | 2016-03-03 13:25:20 +0100 | [diff] [blame] | 1068 | } |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 1069 | pthread_mutex_destroy(&session->opts.server.rpc_lock); |
| 1070 | pthread_cond_destroy(&session->opts.server.rpc_cond); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | if (session->io_lock && !multisession) { |
| 1074 | pthread_mutex_destroy(session->io_lock); |
| 1075 | free(session->io_lock); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1079 | ly_ctx_destroy((struct ly_ctx *)session->ctx); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1080 | } |
| 1081 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 1082 | if (session->side == NC_SERVER) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 1083 | /* free CH synchronization structures */ |
| 1084 | pthread_cond_destroy(&session->opts.server.ch_cond); |
| 1085 | pthread_mutex_destroy(&session->opts.server.ch_lock); |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 1086 | } else { |
| 1087 | pthread_mutex_destroy(&session->opts.client.msgs_lock); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 1088 | } |
| 1089 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1090 | free(session); |
| 1091 | } |
| 1092 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1093 | static void |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1094 | add_cpblt(const char *capab, char ***cpblts, int *size, int *count) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1095 | { |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 1096 | 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 Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1109 | if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) { |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 1110 | /* already present, do not duplicate it */ |
| 1111 | return; |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | /* add another capability */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1117 | if (*count == *size) { |
| 1118 | *size += 5; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1119 | *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts); |
| 1120 | if (!(*cpblts)) { |
| 1121 | ERRMEM; |
| 1122 | return; |
| 1123 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1124 | } |
| 1125 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1126 | (*cpblts)[*count] = capab ? strdup(capab) : NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1127 | ++(*count); |
| 1128 | } |
| 1129 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1130 | API char ** |
| 1131 | nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1132 | { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1133 | char **cpblts; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1134 | 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 Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1137 | uint32_t i, u; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1138 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1139 | char *yl_content_id; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1140 | uint32_t wd_also_supported; |
| 1141 | uint32_t wd_basic_mode; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1142 | |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1143 | #define NC_CPBLT_BUF_LEN 4096 |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 1144 | char str[NC_CPBLT_BUF_LEN]; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1145 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1146 | NC_CHECK_ARG_RET(NULL, ctx, NULL); |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 1147 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1148 | cpblts = malloc(size * sizeof *cpblts); |
roman | 124a436 | 2023-10-26 15:36:22 +0200 | [diff] [blame] | 1149 | NC_CHECK_ERRMEM_GOTO(!cpblts, , error); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1150 | cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0"); |
| 1151 | cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1152 | count = 2; |
| 1153 | |
| 1154 | /* capabilities */ |
| 1155 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1156 | mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1157 | if (mod) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1158 | if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1159 | add_cpblt("urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1160 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1161 | if (lys_feature_value(mod, "candidate") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1162 | add_cpblt("urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1163 | if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1164 | add_cpblt("urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1165 | } |
| 1166 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1167 | if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1168 | add_cpblt("urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1169 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1170 | if (lys_feature_value(mod, "validate") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1171 | add_cpblt("urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1172 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1173 | if (lys_feature_value(mod, "startup") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1174 | add_cpblt("urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1175 | } |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1176 | |
| 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 Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1181 | // if (lys_feature_value(mod, "url") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1182 | // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count); |
mekleo | a8de5e9 | 2020-02-13 09:05:56 +0100 | [diff] [blame] | 1183 | // } |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1184 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1185 | if (lys_feature_value(mod, "xpath") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1186 | add_cpblt("urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1190 | mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1191 | if (mod) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1192 | wd_basic_mode = ATOMIC_LOAD_RELAXED(server_opts.wd_basic_mode); |
| 1193 | if (!wd_basic_mode) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1194 | VRB(NULL, "with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1195 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1196 | strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0"); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1197 | switch (wd_basic_mode) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1198 | 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 Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1208 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1209 | break; |
| 1210 | } |
| 1211 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1212 | wd_also_supported = ATOMIC_LOAD_RELAXED(server_opts.wd_also_supported); |
| 1213 | if (wd_also_supported) { |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 1214 | strcat(str, "&also-supported="); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1215 | if (wd_also_supported & NC_WD_ALL) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1216 | strcat(str, "report-all,"); |
| 1217 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1218 | if (wd_also_supported & NC_WD_ALL_TAG) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1219 | strcat(str, "report-all-tagged,"); |
| 1220 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1221 | if (wd_also_supported & NC_WD_TRIM) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1222 | strcat(str, "trim,"); |
| 1223 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1224 | if (wd_also_supported & NC_WD_EXPLICIT) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1225 | strcat(str, "explicit,"); |
| 1226 | } |
| 1227 | str[strlen(str) - 1] = '\0'; |
| 1228 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1229 | add_cpblt(str, &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 1234 | /* other capabilities */ |
| 1235 | for (u = 0; u < server_opts.capabilities_count; u++) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1236 | add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | /* models */ |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1240 | u = 0; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1241 | while ((mod = ly_ctx_get_module_iter(ctx, &u))) { |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1242 | if (!strcmp(mod->name, "ietf-yang-library")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1243 | if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1244 | ERR(NULL, "Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported."); |
Michal Vasko | d5ada12 | 2020-03-19 18:28:06 +0100 | [diff] [blame] | 1245 | goto error; |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1246 | } |
Michal Vasko | d5ada12 | 2020-03-19 18:28:06 +0100 | [diff] [blame] | 1247 | |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1248 | /* get content-id */ |
| 1249 | if (server_opts.content_id_clb) { |
| 1250 | yl_content_id = server_opts.content_id_clb(server_opts.content_id_data); |
roman | 124a436 | 2023-10-26 15:36:22 +0200 | [diff] [blame] | 1251 | NC_CHECK_ERRMEM_GOTO(!yl_content_id, , error); |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1252 | } else { |
| 1253 | yl_content_id = malloc(11); |
roman | 124a436 | 2023-10-26 15:36:22 +0200 | [diff] [blame] | 1254 | NC_CHECK_ERRMEM_GOTO(!yl_content_id, , error); |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1255 | sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx)); |
| 1256 | } |
| 1257 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1258 | if (!strcmp(mod->revision, "2019-01-04")) { |
Michal Vasko | 7b5e3d9 | 2020-04-08 14:40:31 +0200 | [diff] [blame] | 1259 | /* new one (capab defined in RFC 8526 section 2) */ |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1260 | sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s", |
| 1261 | mod->revision, yl_content_id); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1262 | add_cpblt(str, &cpblts, &size, &count); |
Michal Vasko | 7b5e3d9 | 2020-04-08 14:40:31 +0200 | [diff] [blame] | 1263 | } else { |
| 1264 | /* old one (capab defined in RFC 7950 section 5.6.4) */ |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1265 | sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s", |
| 1266 | mod->revision, yl_content_id); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1267 | add_cpblt(str, &cpblts, &size, &count); |
Michal Vasko | d5ada12 | 2020-03-19 18:28:06 +0100 | [diff] [blame] | 1268 | } |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1269 | free(yl_content_id); |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1270 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1271 | } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1272 | /* skip YANG 1.1 modules */ |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1273 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1274 | } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1275 | /* skip YANG 1.0 modules */ |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1276 | continue; |
| 1277 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1278 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1279 | str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "", |
| 1280 | mod->revision ? mod->revision : ""); |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1281 | |
Michal Vasko | dafdc74 | 2020-03-11 16:15:59 +0100 | [diff] [blame] | 1282 | features_count = 0; |
| 1283 | i = 0; |
| 1284 | feat = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1285 | while ((feat = lysp_feature_next(feat, mod->parsed, &i))) { |
Michal Vasko | dafdc74 | 2020-03-11 16:15:59 +0100 | [diff] [blame] | 1286 | if (!(feat->flags & LYS_FENABLED)) { |
| 1287 | continue; |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 1288 | } |
Michal Vasko | dafdc74 | 2020-03-11 16:15:59 +0100 | [diff] [blame] | 1289 | 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 Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1305 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1306 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1307 | if (mod->deviated_by) { |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1308 | strcat(str, "&deviations="); |
| 1309 | str_len += 12; |
| 1310 | dev_count = 0; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1311 | 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 Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1316 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1317 | if (dev_count) { |
| 1318 | strcat(str, ","); |
| 1319 | ++str_len; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1320 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1321 | strcat(str, mod->deviated_by[v]->name); |
| 1322 | str_len += len; |
| 1323 | dev_count++; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1327 | add_cpblt(str, &cpblts, &size, &count); |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1328 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1329 | |
| 1330 | /* ending NULL capability */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1331 | add_cpblt(NULL, &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1332 | |
| 1333 | return cpblts; |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1334 | |
| 1335 | error: |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1336 | free(cpblts); |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1337 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1338 | } |
| 1339 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1340 | API char ** |
| 1341 | nc_server_get_cpblts(const struct ly_ctx *ctx) |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1342 | { |
| 1343 | return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF); |
| 1344 | } |
| 1345 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1346 | static int |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1347 | parse_cpblts(struct lyd_node *capabilities, char ***list) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1348 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1349 | struct lyd_node *iter; |
| 1350 | struct lyd_node_opaq *cpblt; |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1351 | int ver = -1, i = 0; |
| 1352 | const char *cpb_start, *cpb_end; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1353 | |
| 1354 | if (list) { |
| 1355 | /* get the storage for server's capabilities */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1356 | LY_LIST_FOR(lyd_child(capabilities), iter) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1357 | i++; |
| 1358 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1359 | /* last item remains NULL */ |
| 1360 | *list = calloc(i + 1, sizeof **list); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1361 | NC_CHECK_ERRMEM_RET(!*list, -1); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1362 | i = 0; |
| 1363 | } |
| 1364 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1365 | 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 Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1369 | ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1370 | return -1; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1371 | } |
| 1372 | |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1373 | /* skip leading/trailing whitespaces */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1374 | 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 Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1377 | ERR(NULL, "Empty capability \"%s\" received.", cpblt->value); |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1378 | return -1; |
| 1379 | } |
| 1380 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1381 | /* detect NETCONF version */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1382 | if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1383 | ver = 0; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1384 | } else if ((ver < 1) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.1", cpb_end - cpb_start)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1385 | ver = 1; |
| 1386 | } |
| 1387 | |
| 1388 | /* store capabilities */ |
| 1389 | if (list) { |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1390 | (*list)[i] = strndup(cpb_start, cpb_end - cpb_start); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1391 | NC_CHECK_ERRMEM_RET(!(*list)[i], -1); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1392 | i++; |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | if (ver == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1397 | ERR(NULL, "Peer does not support a compatible NETCONF version."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | return ver; |
| 1401 | } |
| 1402 | |
| 1403 | static NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1404 | nc_send_hello_io(struct nc_session *session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1405 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1406 | NC_MSG_TYPE ret; |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1407 | int i, timeout_io; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1408 | char **cpblts; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1409 | uint32_t *sid; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1410 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1411 | if (session->side == NC_CLIENT) { |
| 1412 | /* client side hello - send only NETCONF base capabilities */ |
| 1413 | cpblts = malloc(3 * sizeof *cpblts); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1414 | NC_CHECK_ERRMEM_RET(!cpblts, NC_MSG_ERROR); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1415 | cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0"); |
| 1416 | cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1"); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1417 | cpblts[2] = NULL; |
| 1418 | |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1419 | timeout_io = NC_CLIENT_HELLO_TIMEOUT * 1000; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1420 | sid = NULL; |
| 1421 | } else { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1422 | cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0); |
Michal Vasko | 5b24b6b | 2020-12-04 09:29:47 +0100 | [diff] [blame] | 1423 | if (!cpblts) { |
| 1424 | return NC_MSG_ERROR; |
| 1425 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1426 | |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1427 | if (session->flags & NC_SESSION_CALLHOME) { |
| 1428 | timeout_io = NC_SERVER_CH_HELLO_TIMEOUT * 1000; |
| 1429 | } else { |
Michal Vasko | 5c1c9b3 | 2024-02-02 09:02:42 +0100 | [diff] [blame] | 1430 | timeout_io = server_opts.idle_timeout ? server_opts.idle_timeout * 1000 : -1; |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1431 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1432 | sid = &session->id; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1433 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1434 | |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1435 | ret = nc_write_msg_io(session, timeout_io, NC_MSG_HELLO, cpblts, sid); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1436 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1437 | for (i = 0; cpblts[i]; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1438 | free(cpblts[i]); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1439 | } |
| 1440 | free(cpblts); |
| 1441 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1442 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | static NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1446 | nc_recv_client_hello_io(struct nc_session *session) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1447 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1448 | 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 Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1452 | char *str; |
Michal Vasko | 292c554 | 2023-02-01 14:33:17 +0100 | [diff] [blame] | 1453 | long long id; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1454 | NC_MSG_TYPE rc = NC_MSG_HELLO; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1455 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1456 | r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg); |
| 1457 | switch (r) { |
| 1458 | case 1: |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1459 | /* parse <hello> data */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1460 | if (lyd_parse_data(session->ctx, NULL, msg, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_OPAQ, 0, &hello)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1461 | ERR(session, "Failed to parse server <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1462 | 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 Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1470 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1471 | } else if (!strcmp(node->name.name, "session-id")) { |
| 1472 | if (!node->value || !strlen(node->value)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1473 | ERR(session, "No value of <session-id> element in server <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1474 | rc = NC_MSG_ERROR; |
| 1475 | goto cleanup; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1476 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1477 | str = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1478 | id = strtoll(node->value, &str, 10); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1479 | if (*str || (id < 1) || (id > UINT32_MAX)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1480 | ERR(session, "Invalid value of <session-id> element in server <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1481 | rc = NC_MSG_ERROR; |
| 1482 | goto cleanup; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1483 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1484 | session->id = (uint32_t)id; |
| 1485 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1486 | } else if (strcmp(node->name.name, "capabilities")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1487 | ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1488 | rc = NC_MSG_ERROR; |
| 1489 | goto cleanup; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1490 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1491 | |
| 1492 | if (flag) { |
| 1493 | /* multiple capabilities elements */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1494 | ERR(session, "Invalid <hello> message (multiple <capabilities> elements)."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1495 | rc = NC_MSG_ERROR; |
| 1496 | goto cleanup; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1497 | } |
| 1498 | flag = 1; |
| 1499 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1500 | if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) { |
| 1501 | rc = NC_MSG_ERROR; |
| 1502 | goto cleanup; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1503 | } |
| 1504 | session->version = ver; |
| 1505 | } |
| 1506 | |
| 1507 | if (!session->id) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1508 | ERR(session, "Missing <session-id> in server <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1509 | rc = NC_MSG_ERROR; |
| 1510 | goto cleanup; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1511 | } |
| 1512 | break; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1513 | case 0: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1514 | ERR(session, "Server <hello> timeout elapsed."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1515 | rc = NC_MSG_WOULDBLOCK; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1516 | break; |
| 1517 | default: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1518 | rc = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1519 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1520 | } |
| 1521 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1522 | cleanup: |
| 1523 | ly_in_free(msg, 1); |
| 1524 | lyd_free_tree(hello); |
| 1525 | return rc; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 1526 | } |
| 1527 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1528 | static NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1529 | nc_recv_server_hello_io(struct nc_session *session) |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1530 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1531 | 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 Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1536 | |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1537 | if (session->flags & NC_SESSION_CALLHOME) { |
| 1538 | timeout_io = NC_SERVER_CH_HELLO_TIMEOUT * 1000; |
| 1539 | } else { |
Michal Vasko | 5c1c9b3 | 2024-02-02 09:02:42 +0100 | [diff] [blame] | 1540 | timeout_io = server_opts.idle_timeout ? server_opts.idle_timeout * 1000 : -1; |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1541 | } |
| 1542 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1543 | 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 Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1548 | ERR(session, "Failed to parse client <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1549 | rc = NC_MSG_ERROR; |
| 1550 | goto cleanup; |
| 1551 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1552 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1553 | /* 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 Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1558 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1559 | } else if (strcmp(node->name.name, "capabilities")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1560 | ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1561 | rc = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1562 | goto cleanup; |
| 1563 | } |
| 1564 | |
| 1565 | if (flag) { |
| 1566 | /* multiple capabilities elements */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1567 | ERR(session, "Invalid <hello> message (multiple <capabilities> elements)."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1568 | rc = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1569 | goto cleanup; |
| 1570 | } |
| 1571 | flag = 1; |
| 1572 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1573 | if ((ver = parse_cpblts(&node->node, NULL)) < 0) { |
| 1574 | rc = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1575 | goto cleanup; |
| 1576 | } |
| 1577 | session->version = ver; |
| 1578 | } |
| 1579 | break; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1580 | case 0: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1581 | ERR(session, "Client <hello> timeout elapsed."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1582 | rc = NC_MSG_WOULDBLOCK; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 1583 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1584 | default: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1585 | rc = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1586 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | cleanup: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1590 | ly_in_free(msg, 1); |
| 1591 | lyd_free_tree(hello); |
| 1592 | return rc; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1593 | } |
| 1594 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1595 | NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1596 | nc_handshake_io(struct nc_session *session) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1597 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1598 | NC_MSG_TYPE type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1599 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1600 | type = nc_send_hello_io(session); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1601 | if (type != NC_MSG_HELLO) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1602 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1603 | } |
| 1604 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1605 | if (session->side == NC_CLIENT) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1606 | type = nc_recv_client_hello_io(session); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1607 | } else { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1608 | type = nc_recv_server_hello_io(session); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1609 | } |
| 1610 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1611 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1612 | } |