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 | 15fbc59 | 2024-07-02 16:01:23 +0200 | [diff] [blame^] | 40 | #include <curl/curl.h> |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 41 | #include <libssh/libssh.h> |
roman | 008cfe7 | 2024-04-05 12:36:18 +0200 | [diff] [blame] | 42 | #include "session_wrapper.h" |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 43 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 44 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 45 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 46 | /* in seconds */ |
| 47 | #define NC_CLIENT_HELLO_TIMEOUT 60 |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 48 | #define NC_SERVER_CH_HELLO_TIMEOUT 180 |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 49 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 50 | /* in milliseconds */ |
| 51 | #define NC_CLOSE_REPLY_TIMEOUT 200 |
| 52 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 53 | extern struct nc_server_opts server_opts; |
| 54 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 55 | void |
| 56 | nc_timeouttime_get(struct timespec *ts, uint32_t add_ms) |
Radek Krejci | 7ac1605 | 2016-07-15 11:48:18 +0200 | [diff] [blame] | 57 | { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 58 | if (clock_gettime(COMPAT_CLOCK_ID, ts) == -1) { |
| 59 | ERR(NULL, "clock_gettime() failed (%s).", strerror(errno)); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | if (!add_ms) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L)); |
| 68 | |
| 69 | ts->tv_sec += add_ms / 1000; |
| 70 | ts->tv_nsec += (add_ms % 1000) * 1000000L; |
| 71 | |
| 72 | if (ts->tv_nsec >= 1000000000L) { |
| 73 | ++ts->tv_sec; |
| 74 | ts->tv_nsec -= 1000000000L; |
| 75 | } else if (ts->tv_nsec < 0) { |
| 76 | --ts->tv_sec; |
| 77 | ts->tv_nsec += 1000000000L; |
| 78 | } |
| 79 | |
| 80 | assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L)); |
| 81 | } |
| 82 | |
| 83 | int32_t |
| 84 | nc_timeouttime_cur_diff(const struct timespec *ts) |
| 85 | { |
| 86 | struct timespec cur; |
| 87 | int64_t nsec_diff = 0; |
| 88 | |
| 89 | nc_timeouttime_get(&cur, 0); |
| 90 | |
| 91 | nsec_diff += (((int64_t)ts->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L; |
| 92 | nsec_diff += ((int64_t)ts->tv_nsec) - ((int64_t)cur.tv_nsec); |
| 93 | |
| 94 | return nsec_diff / 1000000L; |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | nc_realtime_get(struct timespec *ts) |
| 99 | { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 100 | if (clock_gettime(CLOCK_REALTIME, ts)) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 101 | ERR(NULL, "clock_gettime() failed (%s).", strerror(errno)); |
| 102 | return; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 103 | } |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Michal Vasko | 63b92d6 | 2024-04-29 10:04:56 +0200 | [diff] [blame] | 106 | int |
| 107 | nc_poll(struct pollfd *pfd, uint16_t pfd_count, int timeout_ms) |
| 108 | { |
| 109 | int rc; |
| 110 | struct timespec start_ts; |
| 111 | |
| 112 | if (timeout_ms > 0) { |
| 113 | /* get current time */ |
| 114 | nc_timeouttime_get(&start_ts, 0); |
| 115 | } |
| 116 | |
| 117 | do { |
| 118 | /* poll */ |
| 119 | rc = poll(pfd, pfd_count, timeout_ms); |
| 120 | |
| 121 | if (timeout_ms > 0) { |
| 122 | /* adjust the timeout by subtracting the elapsed time (relevant in case of EINTR) */ |
| 123 | timeout_ms += nc_timeouttime_cur_diff(&start_ts); |
| 124 | if (timeout_ms < 0) { |
| 125 | /* manual timeout */ |
| 126 | rc = 0; |
| 127 | errno = 0; |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | } while ((rc == -1) && (errno == EINTR)); |
| 132 | |
| 133 | if (rc == -1) { |
| 134 | ERR(NULL, "Poll failed (%s).", strerror(errno)); |
| 135 | } |
| 136 | return rc; |
| 137 | } |
| 138 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 139 | #ifdef NC_ENABLED_SSH_TLS |
| 140 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 141 | const char * |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 142 | nc_privkey_format_to_str(NC_PRIVKEY_FORMAT format) |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 143 | { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 144 | switch (format) { |
| 145 | case NC_PRIVKEY_FORMAT_RSA: |
roman | d0fe595 | 2024-03-21 15:59:33 +0100 | [diff] [blame] | 146 | return " RSA "; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 147 | case NC_PRIVKEY_FORMAT_EC: |
roman | d0fe595 | 2024-03-21 15:59:33 +0100 | [diff] [blame] | 148 | return " EC "; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 149 | case NC_PRIVKEY_FORMAT_X509: |
roman | d0fe595 | 2024-03-21 15:59:33 +0100 | [diff] [blame] | 150 | return " "; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 151 | case NC_PRIVKEY_FORMAT_OPENSSH: |
roman | d0fe595 | 2024-03-21 15:59:33 +0100 | [diff] [blame] | 152 | return " OPENSSH "; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 153 | default: |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 154 | return NULL; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 155 | } |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 156 | } |
| 157 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 158 | int |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 159 | nc_is_pk_subject_public_key_info(const char *b64) |
| 160 | { |
| 161 | int ret = 0; |
| 162 | long len; |
roman | c95a14f | 2024-04-23 15:13:00 +0200 | [diff] [blame] | 163 | unsigned char *bin = NULL, *tmp; |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 164 | |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 165 | /* decode base64 */ |
| 166 | len = nc_base64_decode_wrap(b64, &bin); |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 167 | if (len == -1) { |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 168 | ret = -1; |
| 169 | goto cleanup; |
| 170 | } |
| 171 | |
| 172 | /* for deallocation later */ |
| 173 | tmp = bin; |
| 174 | |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 175 | /* try to parse the supposed SubjectPublicKeyInfo binary data */ |
roman | c95a14f | 2024-04-23 15:13:00 +0200 | [diff] [blame] | 176 | if (nc_tls_is_der_subpubkey_wrap(tmp, len)) { |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 177 | /* success, it's most likely SubjectPublicKeyInfo */ |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 178 | ret = 1; |
| 179 | } else { |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 180 | /* it's most likely not SubjectPublicKeyInfo */ |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 181 | ret = 0; |
| 182 | } |
| 183 | |
| 184 | cleanup: |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 185 | free(bin); |
| 186 | return ret; |
| 187 | } |
| 188 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 189 | #endif /* NC_ENABLED_SSH_TLS */ |
| 190 | |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 191 | int |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame] | 192 | nc_sock_configure_ka(int sock, const struct nc_keepalives *ka) |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 193 | { |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame] | 194 | int opt; |
| 195 | |
| 196 | opt = ka->enabled; |
| 197 | if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) { |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 198 | ERR(NULL, "Failed to set SO_KEEPALIVE (%s).", strerror(errno)); |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 199 | return -1; |
| 200 | } |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 201 | |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame] | 202 | if (!ka->enabled) { |
| 203 | return 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 204 | } |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 205 | |
| 206 | #ifdef TCP_KEEPIDLE |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame] | 207 | opt = ka->idle_time; |
| 208 | if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) { |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 209 | ERR(NULL, "Failed to set TCP_KEEPIDLE (%s).", strerror(errno)); |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 210 | return -1; |
| 211 | } |
| 212 | #endif |
| 213 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 214 | #ifdef TCP_KEEPCNT |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame] | 215 | opt = ka->max_probes; |
| 216 | if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) { |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 217 | ERR(NULL, "Failed to set TCP_KEEPCNT (%s).", strerror(errno)); |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 218 | return -1; |
| 219 | } |
| 220 | #endif |
| 221 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 222 | #ifdef TCP_KEEPINTVL |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame] | 223 | opt = ka->probe_interval; |
| 224 | if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof opt) == -1) { |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 225 | ERR(NULL, "Failed to set TCP_KEEPINTVL (%s).", strerror(errno)); |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 226 | return -1; |
| 227 | } |
| 228 | #endif |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 233 | struct nc_session * |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 234 | nc_new_session(NC_SIDE side, int shared_ti) |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 235 | { |
| 236 | struct nc_session *sess; |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 237 | struct timespec ts_cur; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 238 | |
| 239 | sess = calloc(1, sizeof *sess); |
| 240 | if (!sess) { |
| 241 | return NULL; |
| 242 | } |
| 243 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 244 | sess->side = side; |
| 245 | |
| 246 | if (side == NC_SERVER) { |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 247 | pthread_mutex_init(&sess->opts.server.ntf_status_lock, NULL); |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 248 | pthread_mutex_init(&sess->opts.server.rpc_lock, NULL); |
| 249 | pthread_cond_init(&sess->opts.server.rpc_cond, NULL); |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 250 | |
| 251 | pthread_mutex_init(&sess->opts.server.ch_lock, NULL); |
| 252 | pthread_cond_init(&sess->opts.server.ch_cond, NULL); |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 253 | |
| 254 | /* initialize last_rpc for idle_timeout */ |
| 255 | nc_timeouttime_get(&ts_cur, 0); |
| 256 | sess->opts.server.last_rpc = ts_cur.tv_sec; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 257 | } else { |
| 258 | pthread_mutex_init(&sess->opts.client.msgs_lock, NULL); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | if (!shared_ti) { |
| 262 | sess->io_lock = malloc(sizeof *sess->io_lock); |
| 263 | if (!sess->io_lock) { |
| 264 | goto error; |
| 265 | } |
| 266 | pthread_mutex_init(sess->io_lock, NULL); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | return sess; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 270 | |
| 271 | error: |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 272 | free(sess); |
| 273 | return NULL; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 276 | /* |
| 277 | * @return 1 - success |
| 278 | * 0 - timeout |
| 279 | * -1 - error |
| 280 | */ |
| 281 | int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 282 | 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] | 283 | { |
| 284 | int ret; |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 285 | struct timespec ts_timeout; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 286 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 287 | if (session->side != NC_SERVER) { |
| 288 | ERRINT; |
| 289 | return -1; |
| 290 | } |
| 291 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 292 | if (timeout > 0) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 293 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 294 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 295 | /* LOCK */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 296 | 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] | 297 | if (!ret) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 298 | while (session->opts.server.rpc_inuse) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 299 | ret = pthread_cond_clockwait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock, |
| 300 | COMPAT_CLOCK_ID, &ts_timeout); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 301 | if (ret) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 302 | pthread_mutex_unlock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 303 | break; |
| 304 | } |
| 305 | } |
| 306 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 307 | } else if (!timeout) { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 308 | /* LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 309 | ret = pthread_mutex_trylock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 310 | if (!ret) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 311 | if (session->opts.server.rpc_inuse) { |
| 312 | pthread_mutex_unlock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 313 | return 0; |
| 314 | } |
Michal vasko | 2f8e4b5 | 2016-10-05 13:04:11 +0200 | [diff] [blame] | 315 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 316 | } else { /* timeout == -1 */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 317 | /* LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 318 | ret = pthread_mutex_lock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 319 | if (!ret) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 320 | while (session->opts.server.rpc_inuse) { |
| 321 | 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] | 322 | if (ret) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 323 | pthread_mutex_unlock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 324 | break; |
| 325 | } |
| 326 | } |
| 327 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 328 | } |
| 329 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 330 | if (ret) { |
| 331 | if ((ret == EBUSY) || (ret == ETIMEDOUT)) { |
| 332 | /* timeout */ |
| 333 | return 0; |
| 334 | } |
| 335 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 336 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 337 | 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] | 338 | return -1; |
| 339 | } |
| 340 | |
| 341 | /* ok */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 342 | assert(session->opts.server.rpc_inuse == 0); |
| 343 | session->opts.server.rpc_inuse = 1; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 344 | |
| 345 | /* UNLOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 346 | ret = pthread_mutex_unlock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 347 | if (ret) { |
| 348 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 349 | 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] | 350 | return -1; |
| 351 | } |
| 352 | |
| 353 | return 1; |
| 354 | } |
| 355 | |
| 356 | int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 357 | 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] | 358 | { |
| 359 | int ret; |
| 360 | struct timespec ts_timeout; |
| 361 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 362 | if (session->side != NC_SERVER) { |
| 363 | ERRINT; |
| 364 | return -1; |
| 365 | } |
| 366 | |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 367 | assert(session->opts.server.rpc_inuse); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 368 | |
| 369 | if (timeout > 0) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 370 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 371 | |
| 372 | /* LOCK */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 373 | 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] | 374 | } else if (!timeout) { |
| 375 | /* LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 376 | ret = pthread_mutex_trylock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 377 | } else { /* timeout == -1 */ |
| 378 | /* LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 379 | ret = pthread_mutex_lock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) { |
| 383 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 384 | 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] | 385 | return -1; |
| 386 | } else if (ret) { |
Michal Vasko | 69e9875 | 2022-12-14 14:20:17 +0100 | [diff] [blame] | 387 | WRN(session, "%s: session RPC lock timeout, should not happen.", func); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 388 | } |
| 389 | |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 390 | session->opts.server.rpc_inuse = 0; |
| 391 | pthread_cond_signal(&session->opts.server.rpc_cond); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 392 | |
| 393 | if (!ret) { |
| 394 | /* UNLOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 395 | ret = pthread_mutex_unlock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 396 | if (ret) { |
| 397 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 398 | 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] | 399 | return -1; |
| 400 | } |
| 401 | } |
| 402 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 403 | return 1; |
| 404 | } |
| 405 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 406 | int |
| 407 | nc_session_io_lock(struct nc_session *session, int timeout, const char *func) |
| 408 | { |
| 409 | int ret; |
| 410 | struct timespec ts_timeout; |
| 411 | |
| 412 | if (timeout > 0) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 413 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 414 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 415 | ret = pthread_mutex_clocklock(session->io_lock, COMPAT_CLOCK_ID, &ts_timeout); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 416 | } else if (!timeout) { |
| 417 | ret = pthread_mutex_trylock(session->io_lock); |
| 418 | } else { /* timeout == -1 */ |
Robin Jarry | 54ea296 | 2018-10-10 10:33:40 +0200 | [diff] [blame] | 419 | ret = pthread_mutex_lock(session->io_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | if (ret) { |
| 423 | if ((ret == EBUSY) || (ret == ETIMEDOUT)) { |
| 424 | /* timeout */ |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 429 | 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] | 430 | return -1; |
| 431 | } |
| 432 | |
| 433 | return 1; |
| 434 | } |
| 435 | |
| 436 | int |
| 437 | nc_session_io_unlock(struct nc_session *session, const char *func) |
| 438 | { |
| 439 | int ret; |
| 440 | |
| 441 | ret = pthread_mutex_unlock(session->io_lock); |
| 442 | if (ret) { |
| 443 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 444 | 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] | 445 | return -1; |
| 446 | } |
| 447 | |
| 448 | return 1; |
| 449 | } |
| 450 | |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 451 | int |
| 452 | nc_session_client_msgs_lock(struct nc_session *session, int *timeout, const char *func) |
| 453 | { |
| 454 | int ret; |
| 455 | int32_t diff_msec; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 456 | struct timespec ts_timeout, ts_start; |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 457 | |
| 458 | assert(session->side == NC_CLIENT); |
| 459 | |
| 460 | if (*timeout > 0) { |
| 461 | /* get current time */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 462 | nc_timeouttime_get(&ts_start, 0); |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 463 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 464 | nc_timeouttime_get(&ts_timeout, *timeout); |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 465 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 466 | 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] | 467 | if (!ret) { |
| 468 | /* update timeout based on what was elapsed */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 469 | diff_msec = nc_timeouttime_cur_diff(&ts_start); |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 470 | *timeout -= diff_msec; |
| 471 | } |
| 472 | } else if (!*timeout) { |
| 473 | ret = pthread_mutex_trylock(&session->opts.client.msgs_lock); |
| 474 | } else { /* timeout == -1 */ |
| 475 | ret = pthread_mutex_lock(&session->opts.client.msgs_lock); |
| 476 | } |
| 477 | |
| 478 | if (ret) { |
| 479 | if ((ret == EBUSY) || (ret == ETIMEDOUT)) { |
| 480 | /* timeout */ |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | /* error */ |
| 485 | ERR(session, "%s: failed to MSGS lock a session (%s).", func, strerror(ret)); |
| 486 | return -1; |
| 487 | } |
| 488 | |
| 489 | return 1; |
| 490 | } |
| 491 | |
| 492 | int |
| 493 | nc_session_client_msgs_unlock(struct nc_session *session, const char *func) |
| 494 | { |
| 495 | int ret; |
| 496 | |
| 497 | assert(session->side == NC_CLIENT); |
| 498 | |
| 499 | ret = pthread_mutex_unlock(&session->opts.client.msgs_lock); |
| 500 | if (ret) { |
| 501 | /* error */ |
| 502 | ERR(session, "%s: failed to MSGS unlock a session (%s).", func, strerror(ret)); |
| 503 | return -1; |
| 504 | } |
| 505 | |
| 506 | return 1; |
| 507 | } |
| 508 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 509 | API NC_STATUS |
| 510 | nc_session_get_status(const struct nc_session *session) |
| 511 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 512 | NC_CHECK_ARG_RET(session, session, NC_STATUS_ERR); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 513 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 514 | return session->status; |
| 515 | } |
| 516 | |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 517 | API NC_SESSION_TERM_REASON |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 518 | nc_session_get_term_reason(const struct nc_session *session) |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 519 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 520 | NC_CHECK_ARG_RET(session, session, NC_SESSION_TERM_ERR); |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 521 | |
| 522 | return session->term_reason; |
| 523 | } |
| 524 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 525 | API uint32_t |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 526 | nc_session_get_killed_by(const struct nc_session *session) |
| 527 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 528 | NC_CHECK_ARG_RET(session, session, 0); |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 529 | |
| 530 | return session->killed_by; |
| 531 | } |
| 532 | |
| 533 | API uint32_t |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 534 | nc_session_get_id(const struct nc_session *session) |
| 535 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 536 | NC_CHECK_ARG_RET(session, session, 0); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 537 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 538 | return session->id; |
| 539 | } |
| 540 | |
Michal Vasko | 174fe8e | 2016-02-17 15:38:09 +0100 | [diff] [blame] | 541 | API int |
| 542 | nc_session_get_version(const struct nc_session *session) |
| 543 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 544 | NC_CHECK_ARG_RET(session, session, -1); |
Michal Vasko | 174fe8e | 2016-02-17 15:38:09 +0100 | [diff] [blame] | 545 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 546 | return session->version == NC_VERSION_10 ? 0 : 1; |
Michal Vasko | 174fe8e | 2016-02-17 15:38:09 +0100 | [diff] [blame] | 547 | } |
| 548 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 549 | API NC_TRANSPORT_IMPL |
| 550 | nc_session_get_ti(const struct nc_session *session) |
| 551 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 552 | NC_CHECK_ARG_RET(session, session, 0); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 553 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 554 | return session->ti_type; |
| 555 | } |
| 556 | |
| 557 | API const char * |
| 558 | nc_session_get_username(const struct nc_session *session) |
| 559 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 560 | NC_CHECK_ARG_RET(session, session, NULL); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 561 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 562 | return session->username; |
| 563 | } |
| 564 | |
| 565 | API const char * |
| 566 | nc_session_get_host(const struct nc_session *session) |
| 567 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 568 | NC_CHECK_ARG_RET(session, session, NULL); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 569 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 570 | return session->host; |
| 571 | } |
| 572 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 573 | API const char * |
| 574 | nc_session_get_path(const struct nc_session *session) |
| 575 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 576 | NC_CHECK_ARG_RET(session, session, NULL); |
| 577 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 578 | if (session->ti_type != NC_TI_UNIX) { |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 579 | return NULL; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 580 | } |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 581 | |
| 582 | return session->path; |
| 583 | } |
| 584 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 585 | API uint16_t |
| 586 | nc_session_get_port(const struct nc_session *session) |
| 587 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 588 | NC_CHECK_ARG_RET(session, session, 0); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 589 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 590 | return session->port; |
| 591 | } |
| 592 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 593 | API const struct ly_ctx * |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 594 | nc_session_get_ctx(const struct nc_session *session) |
| 595 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 596 | NC_CHECK_ARG_RET(session, session, NULL); |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 597 | |
| 598 | return session->ctx; |
| 599 | } |
| 600 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 601 | API void |
| 602 | nc_session_set_data(struct nc_session *session, void *data) |
| 603 | { |
| 604 | if (!session) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 605 | ERRARG(NULL, "session"); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 606 | return; |
| 607 | } |
| 608 | |
| 609 | session->data = data; |
| 610 | } |
| 611 | |
| 612 | API void * |
| 613 | nc_session_get_data(const struct nc_session *session) |
| 614 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 615 | NC_CHECK_ARG_RET(session, session, NULL); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 616 | |
| 617 | return session->data; |
| 618 | } |
| 619 | |
Michal Vasko | dc96bb9 | 2023-03-28 08:52:48 +0200 | [diff] [blame] | 620 | API int |
| 621 | nc_session_is_callhome(const struct nc_session *session) |
| 622 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 623 | NC_CHECK_ARG_RET(session, session, 0); |
Michal Vasko | dc96bb9 | 2023-03-28 08:52:48 +0200 | [diff] [blame] | 624 | |
| 625 | if (session->flags & NC_SESSION_CALLHOME) { |
| 626 | return 1; |
| 627 | } |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 632 | NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 633 | 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] | 634 | { |
Michal Vasko | 7e1f5fb | 2021-11-10 10:14:45 +0100 | [diff] [blame] | 635 | if (session->ctx != LYD_CTX(op)) { |
| 636 | ERR(session, "RPC \"%s\" was created in different context than that of the session.", LYD_NAME(op)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 637 | return NC_MSG_ERROR; |
Michal Vasko | 7df39ec | 2015-12-09 15:26:24 +0100 | [diff] [blame] | 638 | } |
| 639 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 640 | 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] | 641 | } |
| 642 | |
Michal Vasko | d4da363 | 2022-05-25 11:49:10 +0200 | [diff] [blame] | 643 | /** |
| 644 | * @brief Send \<close-session\> and read the reply on a session. |
| 645 | * |
| 646 | * @param[in] session Closing NETCONF session. |
| 647 | */ |
| 648 | static void |
| 649 | nc_session_free_close_session(struct nc_session *session) |
| 650 | { |
| 651 | struct ly_in *msg; |
| 652 | struct lyd_node *close_rpc, *envp; |
| 653 | const struct lys_module *ietfnc; |
| 654 | |
| 655 | ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf"); |
| 656 | if (!ietfnc) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 657 | 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] | 658 | return; |
| 659 | } |
| 660 | if (lyd_new_inner(NULL, ietfnc, "close-session", 0, &close_rpc)) { |
| 661 | WRN(session, "Failed to create <close-session> RPC."); |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | /* send the RPC */ |
| 666 | nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc); |
| 667 | |
| 668 | read_msg: |
| 669 | switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &msg)) { |
| 670 | case 1: |
| 671 | if (!strncmp(ly_in_memory(msg, NULL), "<notification", 13)) { |
| 672 | /* ignore */ |
| 673 | ly_in_free(msg, 1); |
| 674 | goto read_msg; |
| 675 | } |
| 676 | if (lyd_parse_op(session->ctx, close_rpc, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, &envp, NULL)) { |
| 677 | WRN(session, "Failed to parse <close-session> reply."); |
| 678 | } else if (!lyd_child(envp) || strcmp(LYD_NAME(lyd_child(envp)), "ok")) { |
| 679 | WRN(session, "Reply to <close-session> was not <ok> as expected."); |
| 680 | } |
| 681 | lyd_free_tree(envp); |
| 682 | ly_in_free(msg, 1); |
| 683 | break; |
| 684 | case 0: |
| 685 | WRN(session, "Timeout for receiving a reply to <close-session> elapsed."); |
| 686 | break; |
| 687 | case -1: |
| 688 | ERR(session, "Failed to receive a reply to <close-session>."); |
| 689 | break; |
| 690 | default: |
| 691 | /* cannot happen */ |
| 692 | break; |
| 693 | } |
| 694 | lyd_free_tree(close_rpc); |
| 695 | } |
| 696 | |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 697 | /** |
| 698 | * @brief Free transport implementation members of a session. |
| 699 | * |
| 700 | * @param[in] session Session to free. |
| 701 | * @param[out] multisession Whether there are other NC sessions on the same SSH sessions. |
| 702 | */ |
| 703 | static void |
| 704 | nc_session_free_transport(struct nc_session *session, int *multisession) |
| 705 | { |
| 706 | int connected; /* flag to indicate whether the transport socket is still connected */ |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 707 | int sock = -1; |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 708 | struct nc_session *siter; |
| 709 | |
| 710 | *multisession = 0; |
| 711 | connected = nc_session_is_connected(session); |
| 712 | |
| 713 | /* transport implementation cleanup */ |
| 714 | switch (session->ti_type) { |
| 715 | case NC_TI_FD: |
| 716 | /* nothing needed - file descriptors were provided by caller, |
| 717 | * so it is up to the caller to close them correctly |
| 718 | * TODO use callbacks |
| 719 | */ |
| 720 | /* just to avoid compiler warning */ |
| 721 | (void)connected; |
| 722 | (void)siter; |
| 723 | break; |
| 724 | |
| 725 | case NC_TI_UNIX: |
| 726 | sock = session->ti.unixsock.sock; |
| 727 | (void)connected; |
| 728 | (void)siter; |
| 729 | break; |
| 730 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 731 | #ifdef NC_ENABLED_SSH_TLS |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 732 | case NC_TI_SSH: { |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 733 | int r; |
| 734 | |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 735 | if (connected) { |
Michal Vasko | 6473440 | 2022-09-09 11:22:00 +0200 | [diff] [blame] | 736 | ssh_channel_send_eof(session->ti.libssh.channel); |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 737 | ssh_channel_free(session->ti.libssh.channel); |
| 738 | } |
| 739 | /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to |
| 740 | * SSH channel). So destroy the SSH session only if there is no other NETCONF session using |
| 741 | * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session. |
| 742 | */ |
| 743 | /* SESSION IO LOCK */ |
| 744 | r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__); |
| 745 | |
| 746 | if (session->ti.libssh.next) { |
| 747 | for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) { |
| 748 | if (siter->status != NC_STATUS_STARTING) { |
| 749 | *multisession = 1; |
| 750 | break; |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | if (!*multisession) { |
| 756 | /* it's not multisession yet, but we still need to free the starting sessions */ |
| 757 | if (session->ti.libssh.next) { |
| 758 | do { |
| 759 | siter = session->ti.libssh.next; |
| 760 | session->ti.libssh.next = siter->ti.libssh.next; |
| 761 | |
| 762 | /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */ |
| 763 | free(siter->username); |
| 764 | free(siter->host); |
| 765 | if (!(siter->flags & NC_SESSION_SHAREDCTX)) { |
| 766 | ly_ctx_destroy((struct ly_ctx *)siter->ctx); |
| 767 | } |
| 768 | |
| 769 | free(siter); |
| 770 | } while (session->ti.libssh.next != session); |
| 771 | } |
| 772 | /* remember sock so we can close it */ |
| 773 | sock = ssh_get_fd(session->ti.libssh.session); |
| 774 | if (connected) { |
Michal Vasko | 70e9062 | 2023-12-07 08:43:14 +0100 | [diff] [blame] | 775 | /* does not close sock */ |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 776 | ssh_disconnect(session->ti.libssh.session); |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 777 | } |
| 778 | ssh_free(session->ti.libssh.session); |
| 779 | } else { |
| 780 | /* remove the session from the list */ |
| 781 | for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {} |
| 782 | if (session->ti.libssh.next == siter) { |
| 783 | /* there will be only one session */ |
| 784 | siter->ti.libssh.next = NULL; |
| 785 | } else { |
| 786 | /* there are still multiple sessions, keep the ring list */ |
| 787 | siter->ti.libssh.next = session->ti.libssh.next; |
| 788 | } |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | /* SESSION IO UNLOCK */ |
| 792 | if (r == 1) { |
| 793 | nc_session_io_unlock(session, __func__); |
| 794 | } |
| 795 | break; |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 796 | } |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 797 | case NC_TI_TLS: |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 798 | sock = nc_tls_get_fd_wrap(session); |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 799 | |
| 800 | if (connected) { |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 801 | /* notify the peer that we're shutting down */ |
| 802 | nc_tls_close_notify_wrap(session->ti.tls.session); |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 803 | } |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 804 | |
| 805 | nc_tls_ctx_destroy_wrap(&session->ti.tls.ctx); |
roman | c95a14f | 2024-04-23 15:13:00 +0200 | [diff] [blame] | 806 | memset(&session->ti.tls.ctx, 0, sizeof session->ti.tls.ctx); |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 807 | nc_tls_session_destroy_wrap(session->ti.tls.session); |
roman | c95a14f | 2024-04-23 15:13:00 +0200 | [diff] [blame] | 808 | session->ti.tls.session = NULL; |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 809 | nc_tls_config_destroy_wrap(session->ti.tls.config); |
roman | c95a14f | 2024-04-23 15:13:00 +0200 | [diff] [blame] | 810 | session->ti.tls.config = NULL; |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 811 | |
| 812 | if (session->side == NC_SERVER) { |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 813 | nc_tls_cert_destroy_wrap(session->opts.server.client_cert); |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 814 | } |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 815 | |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 816 | break; |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 817 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 818 | case NC_TI_NONE: |
| 819 | break; |
| 820 | } |
| 821 | |
| 822 | /* close socket separately */ |
| 823 | if (sock > -1) { |
| 824 | close(sock); |
| 825 | } |
| 826 | } |
| 827 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 828 | API void |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 829 | nc_session_free(struct nc_session *session, void (*data_free)(void *)) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 830 | { |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 831 | int r, i, rpc_locked = 0, msgs_locked = 0, timeout; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 832 | 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] | 833 | struct nc_msg_cont *contiter; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 834 | struct ly_in *msg; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 835 | struct timespec ts; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 836 | void *p; |
| 837 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 838 | if (!session || (session->status == NC_STATUS_CLOSING)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 839 | return; |
| 840 | } |
| 841 | |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 842 | /* stop notification threads if any */ |
| 843 | if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running)) { |
| 844 | /* let the threads know they should quit */ |
| 845 | ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 0); |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 846 | |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 847 | /* wait for them */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 848 | nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT); |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 849 | while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_count)) { |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 850 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 851 | if (nc_timeouttime_cur_diff(&ts) < 1) { |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 852 | ERR(session, "Waiting for notification thread exit failed (timed out)."); |
| 853 | break; |
| 854 | } |
| 855 | } |
Michal Vasko | 86d357c | 2016-03-11 13:46:38 +0100 | [diff] [blame] | 856 | } |
| 857 | |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 858 | if (session->side == NC_SERVER) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 859 | r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 860 | if (r == -1) { |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 861 | return; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 862 | } else if (r) { |
| 863 | rpc_locked = 1; |
Michal Vasko | 96a28a3 | 2021-02-04 15:35:20 +0100 | [diff] [blame] | 864 | } else { |
| 865 | /* else failed to lock it, too bad */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 866 | ERR(session, "Freeing a session while an RPC is being processed."); |
Michal Vasko | 96a28a3 | 2021-02-04 15:35:20 +0100 | [diff] [blame] | 867 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 868 | } |
| 869 | |
Michal Vasko | 8c24782 | 2020-09-07 13:23:23 +0200 | [diff] [blame] | 870 | if (session->side == NC_CLIENT) { |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 871 | timeout = NC_SESSION_FREE_LOCK_TIMEOUT; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 872 | |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 873 | /* MSGS LOCK */ |
| 874 | r = nc_session_client_msgs_lock(session, &timeout, __func__); |
| 875 | if (r == -1) { |
| 876 | return; |
| 877 | } else if (r) { |
| 878 | msgs_locked = 1; |
| 879 | } else { |
| 880 | /* else failed to lock it, too bad */ |
| 881 | ERR(session, "Freeing a session while messages are being received."); |
| 882 | } |
| 883 | |
| 884 | /* cleanup message queue */ |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 885 | for (contiter = session->opts.client.msgs; contiter; ) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 886 | ly_in_free(contiter->msg, 1); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 887 | |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 888 | p = contiter; |
| 889 | contiter = contiter->next; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 890 | free(p); |
| 891 | } |
| 892 | |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 893 | if (msgs_locked) { |
| 894 | /* MSGS UNLOCK */ |
| 895 | nc_session_client_msgs_unlock(session, __func__); |
| 896 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 897 | |
Michal Vasko | 8c24782 | 2020-09-07 13:23:23 +0200 | [diff] [blame] | 898 | if (session->status == NC_STATUS_RUNNING) { |
Michal Vasko | 9c6d38c | 2021-09-03 13:02:53 +0200 | [diff] [blame] | 899 | /* receive any leftover messages */ |
| 900 | while (nc_read_msg_poll_io(session, 0, &msg) == 1) { |
| 901 | ly_in_free(msg, 1); |
| 902 | } |
| 903 | |
Michal Vasko | 8c24782 | 2020-09-07 13:23:23 +0200 | [diff] [blame] | 904 | /* send closing info to the other side */ |
Michal Vasko | d4da363 | 2022-05-25 11:49:10 +0200 | [diff] [blame] | 905 | nc_session_free_close_session(session); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /* list of server's capabilities */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 909 | if (session->opts.client.cpblts) { |
| 910 | for (i = 0; session->opts.client.cpblts[i]; i++) { |
Michal Vasko | 96fc4bb | 2017-05-23 14:58:34 +0200 | [diff] [blame] | 911 | free(session->opts.client.cpblts[i]); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 912 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 913 | free(session->opts.client.cpblts); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 914 | } |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 915 | |
| 916 | /* LY ext data */ |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 917 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 918 | struct nc_session *siter; |
| 919 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 920 | if ((session->flags & NC_SESSION_SHAREDCTX) && session->ti.libssh.next) { |
| 921 | for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) { |
| 922 | if (siter->status != NC_STATUS_STARTING) { |
| 923 | /* move LY ext data to this session */ |
| 924 | assert(!siter->opts.client.ext_data); |
| 925 | siter->opts.client.ext_data = session->opts.client.ext_data; |
| 926 | session->opts.client.ext_data = NULL; |
| 927 | break; |
| 928 | } |
| 929 | } |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 930 | } else |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 931 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 932 | { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 933 | lyd_free_siblings(session->opts.client.ext_data); |
| 934 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 935 | } |
| 936 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 937 | if (session->data && data_free) { |
| 938 | data_free(session->data); |
| 939 | } |
| 940 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 941 | if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) { |
| 942 | /* CH LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 943 | pthread_mutex_lock(&session->opts.server.ch_lock); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 944 | } |
| 945 | |
Michal Vasko | 86d357c | 2016-03-11 13:46:38 +0100 | [diff] [blame] | 946 | /* mark session for closing */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 947 | session->status = NC_STATUS_CLOSING; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 948 | |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 949 | if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CH_THREAD)) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 950 | pthread_cond_signal(&session->opts.server.ch_cond); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 951 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 952 | nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT); |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 953 | |
| 954 | /* wait for CH thread to actually wake up and terminate */ |
| 955 | r = 0; |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 956 | while (!r && (session->flags & NC_SESSION_CH_THREAD)) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 957 | 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] | 958 | } |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 959 | if (r) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 960 | ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r)); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 961 | } |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 962 | } |
| 963 | |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 964 | if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) { |
| 965 | /* CH UNLOCK */ |
| 966 | pthread_mutex_unlock(&session->opts.server.ch_lock); |
| 967 | } |
| 968 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 969 | /* transport implementation cleanup */ |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 970 | nc_session_free_transport(session, &multisession); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 971 | |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 972 | /* final cleanup */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 973 | free(session->username); |
| 974 | free(session->host); |
| 975 | free(session->path); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 976 | |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 977 | if (session->side == NC_SERVER) { |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 978 | pthread_mutex_destroy(&session->opts.server.ntf_status_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 979 | if (rpc_locked) { |
| 980 | nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | 9e99f01 | 2016-03-03 13:25:20 +0100 | [diff] [blame] | 981 | } |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 982 | pthread_mutex_destroy(&session->opts.server.rpc_lock); |
| 983 | pthread_cond_destroy(&session->opts.server.rpc_cond); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | if (session->io_lock && !multisession) { |
| 987 | pthread_mutex_destroy(session->io_lock); |
| 988 | free(session->io_lock); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 992 | ly_ctx_destroy((struct ly_ctx *)session->ctx); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 993 | } |
| 994 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 995 | if (session->side == NC_SERVER) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 996 | /* free CH synchronization structures */ |
| 997 | pthread_cond_destroy(&session->opts.server.ch_cond); |
| 998 | pthread_mutex_destroy(&session->opts.server.ch_lock); |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 999 | } else { |
| 1000 | pthread_mutex_destroy(&session->opts.client.msgs_lock); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 1001 | } |
| 1002 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1003 | free(session); |
| 1004 | } |
| 1005 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1006 | static void |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1007 | add_cpblt(const char *capab, char ***cpblts, int *size, int *count) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1008 | { |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 1009 | size_t len; |
| 1010 | int i; |
| 1011 | char *p; |
| 1012 | |
| 1013 | if (capab) { |
| 1014 | /* check if already present */ |
| 1015 | p = strchr(capab, '?'); |
| 1016 | if (p) { |
| 1017 | len = p - capab; |
| 1018 | } else { |
| 1019 | len = strlen(capab); |
| 1020 | } |
| 1021 | for (i = 0; i < *count; i++) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1022 | 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] | 1023 | /* already present, do not duplicate it */ |
| 1024 | return; |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | /* add another capability */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1030 | if (*count == *size) { |
| 1031 | *size += 5; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1032 | *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts); |
| 1033 | if (!(*cpblts)) { |
| 1034 | ERRMEM; |
| 1035 | return; |
| 1036 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1037 | } |
| 1038 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1039 | (*cpblts)[*count] = capab ? strdup(capab) : NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1040 | ++(*count); |
| 1041 | } |
| 1042 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1043 | API char ** |
| 1044 | 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] | 1045 | { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1046 | char **cpblts; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1047 | const struct lys_module *mod; |
| 1048 | struct lysp_feature *feat; |
| 1049 | 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] | 1050 | uint32_t i, u; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1051 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1052 | char *yl_content_id; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1053 | uint32_t wd_also_supported; |
| 1054 | uint32_t wd_basic_mode; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1055 | |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1056 | #define NC_CPBLT_BUF_LEN 4096 |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 1057 | char str[NC_CPBLT_BUF_LEN]; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1058 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1059 | NC_CHECK_ARG_RET(NULL, ctx, NULL); |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 1060 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1061 | cpblts = malloc(size * sizeof *cpblts); |
roman | 124a436 | 2023-10-26 15:36:22 +0200 | [diff] [blame] | 1062 | NC_CHECK_ERRMEM_GOTO(!cpblts, , error); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1063 | cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0"); |
| 1064 | cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1065 | count = 2; |
| 1066 | |
| 1067 | /* capabilities */ |
| 1068 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1069 | mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1070 | if (mod) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1071 | if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1072 | 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] | 1073 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1074 | if (lys_feature_value(mod, "candidate") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1075 | 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] | 1076 | if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1077 | 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] | 1078 | } |
| 1079 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1080 | if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1081 | 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] | 1082 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1083 | if (lys_feature_value(mod, "validate") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1084 | 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] | 1085 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1086 | if (lys_feature_value(mod, "startup") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1087 | 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] | 1088 | } |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1089 | |
| 1090 | /* The URL capability must be set manually using nc_server_set_capability() |
| 1091 | * because of the need for supported protocols to be included. |
| 1092 | * https://tools.ietf.org/html/rfc6241#section-8.8.3 |
| 1093 | */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1094 | // if (lys_feature_value(mod, "url") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1095 | // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count); |
mekleo | a8de5e9 | 2020-02-13 09:05:56 +0100 | [diff] [blame] | 1096 | // } |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1097 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1098 | if (lys_feature_value(mod, "xpath") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1099 | 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] | 1100 | } |
| 1101 | } |
| 1102 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1103 | mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1104 | if (mod) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1105 | wd_basic_mode = ATOMIC_LOAD_RELAXED(server_opts.wd_basic_mode); |
| 1106 | if (!wd_basic_mode) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1107 | 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] | 1108 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1109 | strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0"); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1110 | switch (wd_basic_mode) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1111 | case NC_WD_ALL: |
| 1112 | strcat(str, "?basic-mode=report-all"); |
| 1113 | break; |
| 1114 | case NC_WD_TRIM: |
| 1115 | strcat(str, "?basic-mode=trim"); |
| 1116 | break; |
| 1117 | case NC_WD_EXPLICIT: |
| 1118 | strcat(str, "?basic-mode=explicit"); |
| 1119 | break; |
| 1120 | default: |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1121 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1122 | break; |
| 1123 | } |
| 1124 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1125 | wd_also_supported = ATOMIC_LOAD_RELAXED(server_opts.wd_also_supported); |
| 1126 | if (wd_also_supported) { |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 1127 | strcat(str, "&also-supported="); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1128 | if (wd_also_supported & NC_WD_ALL) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1129 | strcat(str, "report-all,"); |
| 1130 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1131 | if (wd_also_supported & NC_WD_ALL_TAG) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1132 | strcat(str, "report-all-tagged,"); |
| 1133 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1134 | if (wd_also_supported & NC_WD_TRIM) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1135 | strcat(str, "trim,"); |
| 1136 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1137 | if (wd_also_supported & NC_WD_EXPLICIT) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1138 | strcat(str, "explicit,"); |
| 1139 | } |
| 1140 | str[strlen(str) - 1] = '\0'; |
| 1141 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1142 | add_cpblt(str, &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 1147 | /* other capabilities */ |
| 1148 | for (u = 0; u < server_opts.capabilities_count; u++) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1149 | add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | /* models */ |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1153 | u = 0; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1154 | while ((mod = ly_ctx_get_module_iter(ctx, &u))) { |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1155 | if (!strcmp(mod->name, "ietf-yang-library")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1156 | 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] | 1157 | 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] | 1158 | goto error; |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1159 | } |
Michal Vasko | d5ada12 | 2020-03-19 18:28:06 +0100 | [diff] [blame] | 1160 | |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1161 | /* get content-id */ |
| 1162 | if (server_opts.content_id_clb) { |
| 1163 | yl_content_id = server_opts.content_id_clb(server_opts.content_id_data); |
roman | 124a436 | 2023-10-26 15:36:22 +0200 | [diff] [blame] | 1164 | NC_CHECK_ERRMEM_GOTO(!yl_content_id, , error); |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1165 | } else { |
| 1166 | yl_content_id = malloc(11); |
roman | 124a436 | 2023-10-26 15:36:22 +0200 | [diff] [blame] | 1167 | NC_CHECK_ERRMEM_GOTO(!yl_content_id, , error); |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1168 | sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx)); |
| 1169 | } |
| 1170 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1171 | if (!strcmp(mod->revision, "2019-01-04")) { |
Michal Vasko | 7b5e3d9 | 2020-04-08 14:40:31 +0200 | [diff] [blame] | 1172 | /* new one (capab defined in RFC 8526 section 2) */ |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1173 | sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s", |
| 1174 | mod->revision, yl_content_id); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1175 | add_cpblt(str, &cpblts, &size, &count); |
Michal Vasko | 7b5e3d9 | 2020-04-08 14:40:31 +0200 | [diff] [blame] | 1176 | } else { |
| 1177 | /* old one (capab defined in RFC 7950 section 5.6.4) */ |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1178 | sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s", |
| 1179 | mod->revision, yl_content_id); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1180 | add_cpblt(str, &cpblts, &size, &count); |
Michal Vasko | d5ada12 | 2020-03-19 18:28:06 +0100 | [diff] [blame] | 1181 | } |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1182 | free(yl_content_id); |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1183 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1184 | } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1185 | /* skip YANG 1.1 modules */ |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1186 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1187 | } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1188 | /* skip YANG 1.0 modules */ |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1189 | continue; |
| 1190 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1191 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1192 | str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "", |
| 1193 | mod->revision ? mod->revision : ""); |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1194 | |
Michal Vasko | dafdc74 | 2020-03-11 16:15:59 +0100 | [diff] [blame] | 1195 | features_count = 0; |
| 1196 | i = 0; |
| 1197 | feat = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1198 | while ((feat = lysp_feature_next(feat, mod->parsed, &i))) { |
Michal Vasko | dafdc74 | 2020-03-11 16:15:59 +0100 | [diff] [blame] | 1199 | if (!(feat->flags & LYS_FENABLED)) { |
| 1200 | continue; |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 1201 | } |
Michal Vasko | dafdc74 | 2020-03-11 16:15:59 +0100 | [diff] [blame] | 1202 | if (!features_count) { |
| 1203 | strcat(str, "&features="); |
| 1204 | str_len += 10; |
| 1205 | } |
| 1206 | len = strlen(feat->name); |
| 1207 | if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) { |
| 1208 | ERRINT; |
| 1209 | break; |
| 1210 | } |
| 1211 | if (features_count) { |
| 1212 | strcat(str, ","); |
| 1213 | ++str_len; |
| 1214 | } |
| 1215 | strcat(str, feat->name); |
| 1216 | str_len += len; |
| 1217 | features_count++; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1218 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1219 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1220 | if (mod->deviated_by) { |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1221 | strcat(str, "&deviations="); |
| 1222 | str_len += 12; |
| 1223 | dev_count = 0; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1224 | LY_ARRAY_FOR(mod->deviated_by, v) { |
| 1225 | len = strlen(mod->deviated_by[v]->name); |
| 1226 | if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) { |
| 1227 | ERRINT; |
| 1228 | break; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1229 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1230 | if (dev_count) { |
| 1231 | strcat(str, ","); |
| 1232 | ++str_len; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1233 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1234 | strcat(str, mod->deviated_by[v]->name); |
| 1235 | str_len += len; |
| 1236 | dev_count++; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1237 | } |
| 1238 | } |
| 1239 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1240 | add_cpblt(str, &cpblts, &size, &count); |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1241 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1242 | |
| 1243 | /* ending NULL capability */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1244 | add_cpblt(NULL, &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1245 | |
| 1246 | return cpblts; |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1247 | |
| 1248 | error: |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1249 | free(cpblts); |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1250 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1251 | } |
| 1252 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1253 | API char ** |
| 1254 | nc_server_get_cpblts(const struct ly_ctx *ctx) |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1255 | { |
| 1256 | return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF); |
| 1257 | } |
| 1258 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1259 | static int |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1260 | parse_cpblts(struct lyd_node *capabilities, char ***list) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1261 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1262 | struct lyd_node *iter; |
| 1263 | struct lyd_node_opaq *cpblt; |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1264 | int ver = -1, i = 0; |
| 1265 | const char *cpb_start, *cpb_end; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1266 | |
| 1267 | if (list) { |
| 1268 | /* get the storage for server's capabilities */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1269 | LY_LIST_FOR(lyd_child(capabilities), iter) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1270 | i++; |
| 1271 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1272 | /* last item remains NULL */ |
| 1273 | *list = calloc(i + 1, sizeof **list); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1274 | NC_CHECK_ERRMEM_RET(!*list, -1); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1275 | i = 0; |
| 1276 | } |
| 1277 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1278 | LY_LIST_FOR(lyd_child(capabilities), iter) { |
| 1279 | cpblt = (struct lyd_node_opaq *)iter; |
| 1280 | |
| 1281 | 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] | 1282 | ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1283 | return -1; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1284 | } |
| 1285 | |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1286 | /* skip leading/trailing whitespaces */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1287 | for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {} |
| 1288 | for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {} |
| 1289 | if (!cpb_start[0] || (cpb_end == cpblt->value)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1290 | ERR(NULL, "Empty capability \"%s\" received.", cpblt->value); |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1291 | return -1; |
| 1292 | } |
| 1293 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1294 | /* detect NETCONF version */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1295 | 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] | 1296 | ver = 0; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1297 | } 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] | 1298 | ver = 1; |
| 1299 | } |
| 1300 | |
| 1301 | /* store capabilities */ |
| 1302 | if (list) { |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1303 | (*list)[i] = strndup(cpb_start, cpb_end - cpb_start); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1304 | NC_CHECK_ERRMEM_RET(!(*list)[i], -1); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1305 | i++; |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | if (ver == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1310 | ERR(NULL, "Peer does not support a compatible NETCONF version."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | return ver; |
| 1314 | } |
| 1315 | |
| 1316 | static NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1317 | nc_send_hello_io(struct nc_session *session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1318 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1319 | NC_MSG_TYPE ret; |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1320 | int i, timeout_io; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1321 | char **cpblts; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1322 | uint32_t *sid; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1323 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1324 | if (session->side == NC_CLIENT) { |
| 1325 | /* client side hello - send only NETCONF base capabilities */ |
| 1326 | cpblts = malloc(3 * sizeof *cpblts); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1327 | NC_CHECK_ERRMEM_RET(!cpblts, NC_MSG_ERROR); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1328 | cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0"); |
| 1329 | cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1"); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1330 | cpblts[2] = NULL; |
| 1331 | |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1332 | timeout_io = NC_CLIENT_HELLO_TIMEOUT * 1000; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1333 | sid = NULL; |
| 1334 | } else { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1335 | cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0); |
Michal Vasko | 5b24b6b | 2020-12-04 09:29:47 +0100 | [diff] [blame] | 1336 | if (!cpblts) { |
| 1337 | return NC_MSG_ERROR; |
| 1338 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1339 | |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1340 | if (session->flags & NC_SESSION_CALLHOME) { |
| 1341 | timeout_io = NC_SERVER_CH_HELLO_TIMEOUT * 1000; |
| 1342 | } else { |
Michal Vasko | 5c1c9b3 | 2024-02-02 09:02:42 +0100 | [diff] [blame] | 1343 | timeout_io = server_opts.idle_timeout ? server_opts.idle_timeout * 1000 : -1; |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1344 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1345 | sid = &session->id; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1346 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1347 | |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1348 | 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] | 1349 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1350 | for (i = 0; cpblts[i]; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1351 | free(cpblts[i]); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1352 | } |
| 1353 | free(cpblts); |
| 1354 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1355 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1356 | } |
| 1357 | |
Michal Vasko | 1022f92 | 2024-05-27 11:12:02 +0200 | [diff] [blame] | 1358 | /** |
| 1359 | * @brief Receive server hello message on the client. |
| 1360 | * |
| 1361 | * @param[in] session Client session to use. |
| 1362 | * @return Received message type. |
| 1363 | */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1364 | static NC_MSG_TYPE |
Michal Vasko | 1022f92 | 2024-05-27 11:12:02 +0200 | [diff] [blame] | 1365 | nc_client_recv_hello_io(struct nc_session *session) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1366 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1367 | struct ly_in *msg; |
| 1368 | struct lyd_node *hello = NULL, *iter; |
| 1369 | struct lyd_node_opaq *node; |
| 1370 | int r, ver = -1, flag = 0; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1371 | char *str; |
Michal Vasko | 292c554 | 2023-02-01 14:33:17 +0100 | [diff] [blame] | 1372 | long long id; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1373 | NC_MSG_TYPE rc = NC_MSG_HELLO; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1374 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1375 | r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg); |
| 1376 | switch (r) { |
| 1377 | case 1: |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1378 | /* parse <hello> data */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1379 | 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] | 1380 | ERR(session, "Failed to parse server <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1381 | rc = NC_MSG_ERROR; |
| 1382 | goto cleanup; |
| 1383 | } |
| 1384 | |
| 1385 | LY_LIST_FOR(lyd_child(hello), iter) { |
| 1386 | node = (struct lyd_node_opaq *)iter; |
| 1387 | |
| 1388 | 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] | 1389 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1390 | } else if (!strcmp(node->name.name, "session-id")) { |
| 1391 | if (!node->value || !strlen(node->value)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1392 | ERR(session, "No value of <session-id> element in server <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1393 | rc = NC_MSG_ERROR; |
| 1394 | goto cleanup; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1395 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1396 | str = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1397 | id = strtoll(node->value, &str, 10); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1398 | if (*str || (id < 1) || (id > UINT32_MAX)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1399 | ERR(session, "Invalid value of <session-id> element in server <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1400 | rc = NC_MSG_ERROR; |
| 1401 | goto cleanup; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1402 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1403 | session->id = (uint32_t)id; |
| 1404 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1405 | } else if (strcmp(node->name.name, "capabilities")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1406 | ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1407 | rc = NC_MSG_ERROR; |
| 1408 | goto cleanup; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1409 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1410 | |
| 1411 | if (flag) { |
| 1412 | /* multiple capabilities elements */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1413 | ERR(session, "Invalid <hello> message (multiple <capabilities> elements)."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1414 | rc = NC_MSG_ERROR; |
| 1415 | goto cleanup; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1416 | } |
| 1417 | flag = 1; |
| 1418 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1419 | if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) { |
| 1420 | rc = NC_MSG_ERROR; |
| 1421 | goto cleanup; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1422 | } |
| 1423 | session->version = ver; |
| 1424 | } |
| 1425 | |
| 1426 | if (!session->id) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1427 | ERR(session, "Missing <session-id> in server <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1428 | rc = NC_MSG_ERROR; |
| 1429 | goto cleanup; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1430 | } |
| 1431 | break; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1432 | case 0: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1433 | ERR(session, "Server <hello> timeout elapsed."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1434 | rc = NC_MSG_WOULDBLOCK; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1435 | break; |
| 1436 | default: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1437 | rc = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1438 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1439 | } |
| 1440 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1441 | cleanup: |
| 1442 | ly_in_free(msg, 1); |
| 1443 | lyd_free_tree(hello); |
| 1444 | return rc; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 1445 | } |
| 1446 | |
Michal Vasko | 1022f92 | 2024-05-27 11:12:02 +0200 | [diff] [blame] | 1447 | /** |
| 1448 | * @brief Receive client hello message on the server. |
| 1449 | * |
| 1450 | * @param[in] session Server session to use. |
| 1451 | * @return Received message type. |
| 1452 | */ |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1453 | static NC_MSG_TYPE |
Michal Vasko | 1022f92 | 2024-05-27 11:12:02 +0200 | [diff] [blame] | 1454 | nc_server_recv_hello_io(struct nc_session *session) |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1455 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1456 | struct ly_in *msg; |
| 1457 | struct lyd_node *hello = NULL, *iter; |
| 1458 | struct lyd_node_opaq *node; |
| 1459 | NC_MSG_TYPE rc = NC_MSG_HELLO; |
| 1460 | int r, ver = -1, flag = 0, timeout_io; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1461 | |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1462 | if (session->flags & NC_SESSION_CALLHOME) { |
| 1463 | timeout_io = NC_SERVER_CH_HELLO_TIMEOUT * 1000; |
| 1464 | } else { |
Michal Vasko | 5c1c9b3 | 2024-02-02 09:02:42 +0100 | [diff] [blame] | 1465 | timeout_io = server_opts.idle_timeout ? server_opts.idle_timeout * 1000 : -1; |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1466 | } |
| 1467 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1468 | r = nc_read_msg_poll_io(session, timeout_io, &msg); |
| 1469 | switch (r) { |
| 1470 | case 1: |
| 1471 | /* parse <hello> data */ |
| 1472 | 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] | 1473 | ERR(session, "Failed to parse client <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1474 | rc = NC_MSG_ERROR; |
| 1475 | goto cleanup; |
| 1476 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1477 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1478 | /* learn NETCONF version */ |
| 1479 | LY_LIST_FOR(lyd_child(hello), iter) { |
| 1480 | node = (struct lyd_node_opaq *)iter; |
| 1481 | |
| 1482 | 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] | 1483 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1484 | } else if (strcmp(node->name.name, "capabilities")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1485 | ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1486 | rc = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1487 | goto cleanup; |
| 1488 | } |
| 1489 | |
| 1490 | if (flag) { |
| 1491 | /* multiple capabilities elements */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1492 | ERR(session, "Invalid <hello> message (multiple <capabilities> elements)."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1493 | rc = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1494 | goto cleanup; |
| 1495 | } |
| 1496 | flag = 1; |
| 1497 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1498 | if ((ver = parse_cpblts(&node->node, NULL)) < 0) { |
| 1499 | rc = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1500 | goto cleanup; |
| 1501 | } |
| 1502 | session->version = ver; |
| 1503 | } |
| 1504 | break; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1505 | case 0: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1506 | ERR(session, "Client <hello> timeout elapsed."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1507 | rc = NC_MSG_WOULDBLOCK; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 1508 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1509 | default: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1510 | rc = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1511 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | cleanup: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1515 | ly_in_free(msg, 1); |
| 1516 | lyd_free_tree(hello); |
| 1517 | return rc; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1518 | } |
| 1519 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1520 | NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1521 | nc_handshake_io(struct nc_session *session) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1522 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1523 | NC_MSG_TYPE type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1524 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1525 | type = nc_send_hello_io(session); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1526 | if (type != NC_MSG_HELLO) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1527 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1528 | } |
| 1529 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1530 | if (session->side == NC_CLIENT) { |
Michal Vasko | 1022f92 | 2024-05-27 11:12:02 +0200 | [diff] [blame] | 1531 | type = nc_client_recv_hello_io(session); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1532 | } else { |
Michal Vasko | 1022f92 | 2024-05-27 11:12:02 +0200 | [diff] [blame] | 1533 | type = nc_server_recv_hello_io(session); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1534 | } |
| 1535 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1536 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1537 | } |
roman | 15fbc59 | 2024-07-02 16:01:23 +0200 | [diff] [blame^] | 1538 | |
| 1539 | #ifdef NC_ENABLED_SSH_TLS |
| 1540 | |
| 1541 | /** |
| 1542 | * @brief CURL callback for downloading data. |
| 1543 | * |
| 1544 | * @param[in] ptr Downloaded data. |
| 1545 | * @param[in] size Size of one element. |
| 1546 | * @param[in] nmemb Number of elements. |
| 1547 | * @param[in,out] userdata Storage the downloaded data. |
| 1548 | * @return Number of bytes processed. |
| 1549 | */ |
| 1550 | static size_t |
| 1551 | nc_session_curl_cb(char *ptr, size_t size, size_t nmemb, void *userdata) |
| 1552 | { |
| 1553 | struct nc_curl_data *data; |
| 1554 | |
| 1555 | size = nmemb; |
| 1556 | |
| 1557 | data = (struct nc_curl_data *)userdata; |
| 1558 | |
| 1559 | data->data = nc_realloc(data->data, data->size + size); |
| 1560 | NC_CHECK_ERRMEM_RET(!data->data, 0); |
| 1561 | |
| 1562 | memcpy(&data->data[data->size], ptr, size); |
| 1563 | data->size += size; |
| 1564 | |
| 1565 | return size; |
| 1566 | } |
| 1567 | |
| 1568 | /** |
| 1569 | * @brief Download data using CURL. |
| 1570 | * |
| 1571 | * @param[in] handle CURL handle. |
| 1572 | * @param[in] url URL to download the data from. |
| 1573 | * @return 0 on success, 1 on failure. |
| 1574 | */ |
| 1575 | static int |
| 1576 | nc_session_curl_fetch(CURL *handle, const char *url) |
| 1577 | { |
| 1578 | char err_buf[CURL_ERROR_SIZE]; |
| 1579 | |
| 1580 | /* set uri */ |
| 1581 | if (curl_easy_setopt(handle, CURLOPT_URL, url)) { |
| 1582 | ERR(NULL, "Setting URI \"%s\" to download CRL from failed.", url); |
| 1583 | return 1; |
| 1584 | } |
| 1585 | |
| 1586 | /* set err buf */ |
| 1587 | if (curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, err_buf)) { |
| 1588 | ERR(NULL, "Setting CURL error buffer option failed."); |
| 1589 | return 1; |
| 1590 | } |
| 1591 | |
| 1592 | /* download */ |
| 1593 | if (curl_easy_perform(handle)) { |
| 1594 | ERR(NULL, "Downloading CRL from \"%s\" failed (%s).", url, err_buf); |
| 1595 | return 1; |
| 1596 | } |
| 1597 | |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
| 1601 | /** |
| 1602 | * @brief Initialize CURL handle for downloading CRL. |
| 1603 | * |
| 1604 | * @param[out] handle CURL handle. |
| 1605 | * @param[out] data Stores the downloaded data. |
| 1606 | * @return 0 on success, 1 on failure. |
| 1607 | */ |
| 1608 | static int |
| 1609 | nc_session_curl_init(CURL **handle, struct nc_curl_data *data) |
| 1610 | { |
| 1611 | NC_CHECK_ARG_RET(NULL, handle, data, -1); |
| 1612 | |
| 1613 | *handle = NULL; |
| 1614 | |
| 1615 | *handle = curl_easy_init(); |
| 1616 | if (!*handle) { |
| 1617 | ERR(NULL, "Initializing CURL failed."); |
| 1618 | return 1; |
| 1619 | } |
| 1620 | |
| 1621 | if (curl_easy_setopt(*handle, CURLOPT_WRITEFUNCTION, nc_session_curl_cb)) { |
| 1622 | ERR(NULL, "Setting curl callback failed."); |
| 1623 | return 1; |
| 1624 | } |
| 1625 | |
| 1626 | if (curl_easy_setopt(*handle, CURLOPT_WRITEDATA, data)) { |
| 1627 | ERR(NULL, "Setting curl callback data failed."); |
| 1628 | return 1; |
| 1629 | } |
| 1630 | |
| 1631 | return 0; |
| 1632 | } |
| 1633 | |
| 1634 | int |
| 1635 | nc_session_tls_crl_from_cert_ext_fetch(void *leaf_cert, void *cert_store, void **crl_store) |
| 1636 | { |
| 1637 | int ret = 0, uri_count = 0, i; |
| 1638 | CURL *handle = NULL; |
| 1639 | struct nc_curl_data downloaded = {0}; |
| 1640 | char **uris = NULL; |
| 1641 | void *crl_store_aux = NULL; |
| 1642 | |
| 1643 | *crl_store = NULL; |
| 1644 | |
| 1645 | crl_store_aux = nc_tls_crl_store_new_wrap(); |
| 1646 | if (!crl_store_aux) { |
| 1647 | goto cleanup; |
| 1648 | } |
| 1649 | |
| 1650 | /* init curl */ |
| 1651 | ret = nc_session_curl_init(&handle, &downloaded); |
| 1652 | if (ret) { |
| 1653 | goto cleanup; |
| 1654 | } |
| 1655 | |
| 1656 | /* get all the uris we can, even though some may point to the same CRL */ |
| 1657 | ret = nc_server_tls_get_crl_distpoint_uris_wrap(leaf_cert, cert_store, &uris, &uri_count); |
| 1658 | if (ret) { |
| 1659 | goto cleanup; |
| 1660 | } |
| 1661 | |
| 1662 | if (!uri_count) { |
| 1663 | /* no CRL distribution points, nothing to download */ |
| 1664 | goto cleanup; |
| 1665 | } |
| 1666 | |
| 1667 | for (i = 0; i < uri_count; i++) { |
| 1668 | VRB(NULL, "Downloading CRL from \"%s\".", uris[i]); |
| 1669 | ret = nc_session_curl_fetch(handle, uris[i]); |
| 1670 | if (ret) { |
| 1671 | /* failed to download the CRL from this entry, try the next entry */ |
| 1672 | WRN(NULL, "Failed to fetch CRL from \"%s\".", uris[i]); |
| 1673 | continue; |
| 1674 | } |
| 1675 | |
| 1676 | /* convert the downloaded data to CRL and add it to the store */ |
| 1677 | ret = nc_server_tls_add_crl_to_store_wrap(downloaded.data, downloaded.size, crl_store_aux); |
| 1678 | |
| 1679 | /* free the downloaded data */ |
| 1680 | free(downloaded.data); |
| 1681 | downloaded.data = NULL; |
| 1682 | downloaded.size = 0; |
| 1683 | |
| 1684 | if (ret) { |
| 1685 | goto cleanup; |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | *crl_store = crl_store_aux; |
| 1690 | crl_store_aux = NULL; |
| 1691 | |
| 1692 | cleanup: |
| 1693 | for (i = 0; i < uri_count; i++) { |
| 1694 | free(uris[i]); |
| 1695 | } |
| 1696 | free(uris); |
| 1697 | curl_easy_cleanup(handle); |
| 1698 | nc_tls_crl_store_destroy_wrap(crl_store_aux); |
| 1699 | return ret; |
| 1700 | } |
| 1701 | |
| 1702 | #endif |