Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @file session_server.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libnetconf2 server session manipulation functions |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 5 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 6 | * @copyright |
| 7 | * Copyright (c) 2015 - 2021 CESNET, z.s.p.o. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [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 |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 14 | */ |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 15 | #define _QNX_SOURCE /* getpeereid */ |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 16 | #define _GNU_SOURCE /* signals, threads, SO_PEERCRED */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 17 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 18 | #include <arpa/inet.h> |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 19 | #include <assert.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 20 | #include <errno.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 21 | #include <fcntl.h> |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 22 | #include <netinet/in.h> |
| 23 | #include <netinet/tcp.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 24 | #include <poll.h> |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 25 | #include <pthread.h> |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 26 | #include <pwd.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 27 | #include <signal.h> |
| 28 | #include <stdint.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <sys/socket.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/un.h> |
| 34 | #include <time.h> |
| 35 | #include <unistd.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 36 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 37 | #include "compat.h" |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 38 | #include "libnetconf.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 39 | #include "session_server.h" |
Michal Vasko | 0bdf70b | 2019-06-24 19:20:20 +0200 | [diff] [blame] | 40 | #include "session_server_ch.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 41 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 42 | struct nc_server_opts server_opts = { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 43 | #ifdef NC_ENABLED_SSH |
| 44 | .authkey_lock = PTHREAD_MUTEX_INITIALIZER, |
| 45 | #endif |
| 46 | .bind_lock = PTHREAD_MUTEX_INITIALIZER, |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 47 | .endpt_lock = PTHREAD_RWLOCK_INITIALIZER, |
| 48 | .ch_client_lock = PTHREAD_RWLOCK_INITIALIZER |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 49 | }; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 50 | |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 51 | static nc_rpc_clb global_rpc_clb = NULL; |
| 52 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 53 | struct nc_endpt * |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 54 | nc_server_endpt_lock_get(const char *name, NC_TRANSPORT_IMPL ti, uint16_t *idx) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 55 | { |
| 56 | uint16_t i; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 57 | struct nc_endpt *endpt = NULL; |
| 58 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 59 | if (!name) { |
| 60 | ERRARG("endpt_name"); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 64 | /* WRITE LOCK */ |
| 65 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 66 | |
| 67 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 68 | if (!strcmp(server_opts.endpts[i].name, name) && (!ti || (server_opts.endpts[i].ti == ti))) { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 69 | endpt = &server_opts.endpts[i]; |
| 70 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 74 | if (!endpt) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 75 | ERR(NULL, "Endpoint \"%s\" was not found.", name); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 76 | /* UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 77 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 78 | return NULL; |
| 79 | } |
| 80 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 81 | if (idx) { |
| 82 | *idx = i; |
| 83 | } |
| 84 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 85 | return endpt; |
| 86 | } |
| 87 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 88 | struct nc_ch_endpt * |
| 89 | nc_server_ch_client_lock(const char *name, const char *endpt_name, NC_TRANSPORT_IMPL ti, struct nc_ch_client **client_p) |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 90 | { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 91 | uint16_t i, j; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 92 | struct nc_ch_client *client = NULL; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 93 | struct nc_ch_endpt *endpt = NULL; |
| 94 | |
| 95 | *client_p = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 96 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 97 | if (!name) { |
| 98 | ERRARG("client_name"); |
| 99 | return NULL; |
| 100 | } |
| 101 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 102 | /* READ LOCK */ |
| 103 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 104 | |
| 105 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 106 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 107 | client = &server_opts.ch_clients[i]; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 108 | if (!endpt_name && !ti) { |
| 109 | /* return only client */ |
| 110 | break; |
| 111 | } |
| 112 | for (j = 0; j < client->ch_endpt_count; ++j) { |
Michal Vasko | 530d95c | 2021-05-28 13:32:02 +0200 | [diff] [blame] | 113 | if ((!endpt_name || !strcmp(client->ch_endpts[j].name, endpt_name)) && |
| 114 | (!ti || (ti == client->ch_endpts[j].ti))) { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 115 | endpt = &client->ch_endpts[j]; |
| 116 | break; |
| 117 | } |
| 118 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if (!client) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 124 | ERR(NULL, "Call Home client \"%s\" was not found.", name); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 125 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 126 | /* READ UNLOCK */ |
| 127 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 128 | } else if (endpt_name && ti && !endpt) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 129 | ERR(NULL, "Call Home client \"%s\" endpoint \"%s\" was not found.", name, endpt_name); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 130 | |
| 131 | /* READ UNLOCK */ |
| 132 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 133 | } else { |
| 134 | /* CH CLIENT LOCK */ |
| 135 | pthread_mutex_lock(&client->lock); |
| 136 | |
| 137 | *client_p = client; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 140 | return endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void |
| 144 | nc_server_ch_client_unlock(struct nc_ch_client *client) |
| 145 | { |
| 146 | /* CH CLIENT UNLOCK */ |
| 147 | pthread_mutex_unlock(&client->lock); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 148 | |
| 149 | /* READ UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 150 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 151 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 152 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 153 | API void |
| 154 | nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason) |
| 155 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 156 | if (!session) { |
| 157 | ERRARG("session"); |
| 158 | return; |
| 159 | } else if (!reason) { |
| 160 | ERRARG("reason"); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 164 | if ((reason != NC_SESSION_TERM_KILLED) && (session->term_reason == NC_SESSION_TERM_KILLED)) { |
| 165 | session->killed_by = 0; |
| 166 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 167 | session->term_reason = reason; |
| 168 | } |
| 169 | |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 170 | API void |
| 171 | nc_session_set_killed_by(struct nc_session *session, uint32_t sid) |
| 172 | { |
| 173 | if (!session || (session->term_reason != NC_SESSION_TERM_KILLED)) { |
| 174 | ERRARG("session"); |
| 175 | return; |
| 176 | } else if (!sid) { |
| 177 | ERRARG("sid"); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | session->killed_by = sid; |
| 182 | } |
| 183 | |
| 184 | API void |
| 185 | nc_session_set_status(struct nc_session *session, NC_STATUS status) |
| 186 | { |
| 187 | if (!session) { |
| 188 | ERRARG("session"); |
| 189 | return; |
| 190 | } else if (!status) { |
| 191 | ERRARG("status"); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | session->status = status; |
| 196 | } |
| 197 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 198 | int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 199 | nc_sock_listen_inet(const char *address, uint16_t port, struct nc_keepalives *ka) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 200 | { |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 201 | int opt; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 202 | int is_ipv4, sock; |
| 203 | struct sockaddr_storage saddr; |
| 204 | |
| 205 | struct sockaddr_in *saddr4; |
| 206 | struct sockaddr_in6 *saddr6; |
| 207 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 208 | if (!strchr(address, ':')) { |
| 209 | is_ipv4 = 1; |
| 210 | } else { |
| 211 | is_ipv4 = 0; |
| 212 | } |
| 213 | |
| 214 | sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0); |
| 215 | if (sock == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 216 | ERR(NULL, "Failed to create socket (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 217 | goto fail; |
| 218 | } |
| 219 | |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 220 | /* these options will be inherited by accepted sockets */ |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 221 | opt = 1; |
| 222 | if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 223 | ERR(NULL, "Could not set SO_REUSEADDR socket option (%s).", strerror(errno)); |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 224 | goto fail; |
| 225 | } |
Michal Vasko | 83ad17e | 2019-01-30 10:11:37 +0100 | [diff] [blame] | 226 | if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 227 | ERR(NULL, "Could not set TCP_NODELAY socket option (%s).", strerror(errno)); |
Michal Vasko | 83ad17e | 2019-01-30 10:11:37 +0100 | [diff] [blame] | 228 | goto fail; |
| 229 | } |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 230 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 231 | if (nc_sock_enable_keepalive(sock, ka)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 232 | goto fail; |
| 233 | } |
| 234 | |
Michal Vasko | f22d5ff | 2020-04-15 11:10:27 +0200 | [diff] [blame] | 235 | memset(&saddr, 0, sizeof(struct sockaddr_storage)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 236 | if (is_ipv4) { |
| 237 | saddr4 = (struct sockaddr_in *)&saddr; |
| 238 | |
| 239 | saddr4->sin_family = AF_INET; |
| 240 | saddr4->sin_port = htons(port); |
| 241 | |
| 242 | if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 243 | ERR(NULL, "Failed to convert IPv4 address \"%s\".", address); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 244 | goto fail; |
| 245 | } |
| 246 | |
| 247 | if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 248 | ERR(NULL, "Could not bind \"%s\" port %d (%s).", address, port, strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 249 | goto fail; |
| 250 | } |
| 251 | |
| 252 | } else { |
| 253 | saddr6 = (struct sockaddr_in6 *)&saddr; |
| 254 | |
| 255 | saddr6->sin6_family = AF_INET6; |
| 256 | saddr6->sin6_port = htons(port); |
| 257 | |
| 258 | if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 259 | ERR(NULL, "Failed to convert IPv6 address \"%s\".", address); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 260 | goto fail; |
| 261 | } |
| 262 | |
| 263 | if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 264 | ERR(NULL, "Could not bind \"%s\" port %d (%s).", address, port, strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 265 | goto fail; |
| 266 | } |
| 267 | } |
| 268 | |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 269 | if (listen(sock, NC_REVERSE_QUEUE) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 270 | ERR(NULL, "Unable to start listening on \"%s\" port %d (%s).", address, port, strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 271 | goto fail; |
| 272 | } |
| 273 | |
| 274 | return sock; |
| 275 | |
| 276 | fail: |
| 277 | if (sock > -1) { |
| 278 | close(sock); |
| 279 | } |
| 280 | |
| 281 | return -1; |
| 282 | } |
| 283 | |
| 284 | int |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 285 | nc_sock_listen_unix(const char *address, const struct nc_server_unix_opts *opts) |
| 286 | { |
| 287 | struct sockaddr_un sun; |
| 288 | int sock = -1; |
| 289 | |
Michal Vasko | 93e96f1 | 2021-09-30 10:02:09 +0200 | [diff] [blame] | 290 | if (strlen(address) > sizeof(sun.sun_path) - 1) { |
| 291 | ERR(NULL, "Socket path \"%s\" is longer than maximum length %d.", address, (int)(sizeof(sun.sun_path) - 1)); |
| 292 | goto fail; |
| 293 | } |
| 294 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 295 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 296 | if (sock == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 297 | ERR(NULL, "Failed to create socket (%s).", strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 298 | goto fail; |
| 299 | } |
| 300 | |
| 301 | memset(&sun, 0, sizeof(sun)); |
| 302 | sun.sun_family = AF_UNIX; |
Michal Vasko | 93e96f1 | 2021-09-30 10:02:09 +0200 | [diff] [blame] | 303 | snprintf(sun.sun_path, sizeof(sun.sun_path) - 1, "%s", address); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 304 | |
| 305 | unlink(sun.sun_path); |
| 306 | if (bind(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 307 | ERR(NULL, "Could not bind \"%s\" (%s).", address, strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 308 | goto fail; |
| 309 | } |
| 310 | |
| 311 | if (opts->mode != (mode_t)-1) { |
| 312 | if (chmod(sun.sun_path, opts->mode) < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 313 | ERR(NULL, "Failed to set unix socket permissions (%s).", strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 314 | goto fail; |
| 315 | } |
| 316 | } |
| 317 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 318 | if ((opts->uid != (uid_t)-1) || (opts->gid != (gid_t)-1)) { |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 319 | if (chown(sun.sun_path, opts->uid, opts->gid) < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 320 | ERR(NULL, "Failed to set unix socket uid/gid (%s).", strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 321 | goto fail; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | if (listen(sock, NC_REVERSE_QUEUE) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 326 | ERR(NULL, "Unable to start listening on \"%s\" (%s).", address, strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 327 | goto fail; |
| 328 | } |
| 329 | |
| 330 | return sock; |
| 331 | |
| 332 | fail: |
| 333 | if (sock > -1) { |
| 334 | close(sock); |
| 335 | } |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 336 | return -1; |
| 337 | } |
| 338 | |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 339 | /** |
| 340 | * @brief Evaluate socket name for AF_UNIX socket. |
| 341 | * @param[in] acc_sock_fd is file descriptor for the accepted socket (a nonnegative). |
| 342 | * @param[out] host is pointer to char* to which the socket name will be set. It must not be NULL. |
| 343 | * @return 0 in case of success. Call free function for parameter host to avoid a memory leak. |
| 344 | * @return 0 if the stream socket is unnamed. Parameter host is set to NULL. |
| 345 | * @return -1 in case of error. Parameter host is set to NULL. |
| 346 | */ |
| 347 | static int |
| 348 | sock_host_unix(int acc_sock_fd, char **host) |
| 349 | { |
| 350 | char *sun_path; |
| 351 | struct sockaddr_storage saddr; |
| 352 | socklen_t addr_len; |
| 353 | |
| 354 | *host = NULL; |
| 355 | saddr.ss_family = AF_UNIX; |
| 356 | addr_len = sizeof(saddr); |
| 357 | |
| 358 | if (getsockname(acc_sock_fd, (struct sockaddr *)&saddr, &addr_len)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 359 | ERR(NULL, "getsockname failed (%s).", strerror(errno)); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 360 | return -1; |
| 361 | } |
| 362 | |
| 363 | sun_path = ((struct sockaddr_un *)&saddr)->sun_path; |
| 364 | if (!sun_path) { |
| 365 | /* stream socket is unnamed */ |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | if (!(*host = strdup(sun_path))) { |
| 370 | ERRMEM; |
| 371 | return -1; |
| 372 | } |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * @brief Evaluate socket name and port number for AF_INET socket. |
| 379 | * @param[in] addr is pointing to structure filled by accept function which was successful. |
| 380 | * @param[out] host is pointer to char* to which the socket name will be set. It must not be NULL. |
| 381 | * @param[out] port is pointer to uint16_t to which the port number will be set. It must not be NULL. |
| 382 | * @return 0 in case of success. Call free function for parameter host to avoid a memory leak. |
| 383 | * @return -1 in case of error. Parameter host is set to NULL and port is unchanged. |
| 384 | */ |
| 385 | static int |
| 386 | sock_host_inet(const struct sockaddr_in *addr, char **host, uint16_t *port) |
| 387 | { |
| 388 | *host = malloc(INET_ADDRSTRLEN); |
| 389 | if (!(*host)) { |
| 390 | ERRMEM; |
| 391 | return -1; |
| 392 | } |
| 393 | |
aPiecek | 3da9b34 | 2021-02-18 15:00:03 +0100 | [diff] [blame] | 394 | if (!inet_ntop(AF_INET, &addr->sin_addr, *host, INET_ADDRSTRLEN)) { |
Michal Vasko | 69e9875 | 2022-12-14 14:20:17 +0100 | [diff] [blame] | 395 | ERR(NULL, "inet_ntop failed (%s).", strerror(errno)); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 396 | free(*host); |
| 397 | *host = NULL; |
| 398 | return -1; |
| 399 | } |
| 400 | |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 401 | *port = ntohs(addr->sin_port); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * @brief Evaluate socket name and port number for AF_INET6 socket. |
| 408 | * @param[in] addr is pointing to structure filled by accept function which was successful. |
| 409 | * @param[out] host is pointer to char* to which the socket name will be set. It must not be NULL. |
| 410 | * @param[out] port is pointer to uint16_t to which the port number will be set. It must not be NULL. |
| 411 | * @return 0 in case of success. Call free function for parameter host to avoid a memory leak. |
| 412 | * @return -1 in case of error. Parameter host is set to the NULL and port is unchanged. |
| 413 | */ |
| 414 | static int |
| 415 | sock_host_inet6(const struct sockaddr_in6 *addr, char **host, uint16_t *port) |
| 416 | { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 417 | *host = malloc(INET6_ADDRSTRLEN); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 418 | if (!(*host)) { |
| 419 | ERRMEM; |
| 420 | return -1; |
| 421 | } |
| 422 | |
aPiecek | 3da9b34 | 2021-02-18 15:00:03 +0100 | [diff] [blame] | 423 | if (!inet_ntop(AF_INET6, &addr->sin6_addr, *host, INET6_ADDRSTRLEN)) { |
Michal Vasko | 69e9875 | 2022-12-14 14:20:17 +0100 | [diff] [blame] | 424 | ERR(NULL, "inet_ntop failed (%s).", strerror(errno)); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 425 | free(*host); |
| 426 | *host = NULL; |
| 427 | return -1; |
| 428 | } |
| 429 | |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 430 | *port = ntohs(addr->sin6_port); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 435 | int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 436 | nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, int timeout, char **host, uint16_t *port, uint16_t *idx) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 437 | { |
Michal Vasko | f54cd35 | 2017-02-22 13:42:02 +0100 | [diff] [blame] | 438 | sigset_t sigmask, origmask; |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 439 | uint16_t i, j, pfd_count, client_port; |
| 440 | char *client_address; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 441 | struct pollfd *pfd; |
| 442 | struct sockaddr_storage saddr; |
| 443 | socklen_t saddr_len = sizeof(saddr); |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 444 | int ret, client_sock, sock = -1, flags; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 445 | |
| 446 | pfd = malloc(bind_count * sizeof *pfd); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 447 | if (!pfd) { |
| 448 | ERRMEM; |
| 449 | return -1; |
| 450 | } |
| 451 | |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 452 | for (i = 0, pfd_count = 0; i < bind_count; ++i) { |
Michal Vasko | 94acafc | 2016-09-23 13:40:10 +0200 | [diff] [blame] | 453 | if (binds[i].sock < 0) { |
| 454 | /* invalid socket */ |
Michal Vasko | 94acafc | 2016-09-23 13:40:10 +0200 | [diff] [blame] | 455 | continue; |
| 456 | } |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 457 | if (binds[i].pollin) { |
| 458 | binds[i].pollin = 0; |
| 459 | /* leftover pollin */ |
| 460 | sock = binds[i].sock; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 461 | break; |
| 462 | } |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 463 | pfd[pfd_count].fd = binds[i].sock; |
| 464 | pfd[pfd_count].events = POLLIN; |
| 465 | pfd[pfd_count].revents = 0; |
| 466 | |
| 467 | ++pfd_count; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 468 | } |
| 469 | |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 470 | if (sock == -1) { |
| 471 | /* poll for a new connection */ |
Michal Vasko | f54cd35 | 2017-02-22 13:42:02 +0100 | [diff] [blame] | 472 | sigfillset(&sigmask); |
| 473 | pthread_sigmask(SIG_SETMASK, &sigmask, &origmask); |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 474 | ret = poll(pfd, pfd_count, timeout); |
Michal Vasko | f54cd35 | 2017-02-22 13:42:02 +0100 | [diff] [blame] | 475 | pthread_sigmask(SIG_SETMASK, &origmask, NULL); |
| 476 | |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 477 | if (!ret) { |
| 478 | /* we timeouted */ |
| 479 | free(pfd); |
| 480 | return 0; |
| 481 | } else if (ret == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 482 | ERR(NULL, "Poll failed (%s).", strerror(errno)); |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 483 | free(pfd); |
| 484 | return -1; |
| 485 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 486 | |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 487 | for (i = 0, j = 0; j < pfd_count; ++i, ++j) { |
| 488 | /* adjust i so that indices in binds and pfd always match */ |
| 489 | while (binds[i].sock != pfd[j].fd) { |
| 490 | ++i; |
| 491 | } |
| 492 | |
| 493 | if (pfd[j].revents & POLLIN) { |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 494 | --ret; |
| 495 | |
| 496 | if (!ret) { |
| 497 | /* the last socket with an event, use it */ |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 498 | sock = pfd[j].fd; |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 499 | break; |
| 500 | } else { |
| 501 | /* just remember the event for next time */ |
| 502 | binds[i].pollin = 1; |
| 503 | } |
| 504 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | free(pfd); |
| 508 | |
| 509 | if (sock == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 510 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 511 | return -1; |
| 512 | } |
| 513 | |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 514 | /* accept connection */ |
| 515 | client_sock = accept(sock, (struct sockaddr *)&saddr, &saddr_len); |
| 516 | if (client_sock < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 517 | ERR(NULL, "Accept failed (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 518 | return -1; |
| 519 | } |
| 520 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 521 | /* make the socket non-blocking */ |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 522 | if (((flags = fcntl(client_sock, F_GETFL)) == -1) || (fcntl(client_sock, F_SETFL, flags | O_NONBLOCK) == -1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 523 | ERR(NULL, "Fcntl failed (%s).", strerror(errno)); |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 524 | goto fail; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 527 | /* learn information about the client end */ |
| 528 | if (saddr.ss_family == AF_UNIX) { |
| 529 | if (sock_host_unix(client_sock, &client_address)) { |
| 530 | goto fail; |
| 531 | } |
| 532 | client_port = 0; |
| 533 | } else if (saddr.ss_family == AF_INET) { |
| 534 | if (sock_host_inet((struct sockaddr_in *)&saddr, &client_address, &client_port)) { |
| 535 | goto fail; |
| 536 | } |
| 537 | } else if (saddr.ss_family == AF_INET6) { |
| 538 | if (sock_host_inet6((struct sockaddr_in6 *)&saddr, &client_address, &client_port)) { |
| 539 | goto fail; |
| 540 | } |
| 541 | } else { |
| 542 | ERR(NULL, "Source host of an unknown protocol family."); |
| 543 | goto fail; |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 544 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 545 | |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 546 | if (saddr.ss_family == AF_UNIX) { |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 547 | VRB(NULL, "Accepted a connection on %s.", binds[i].address); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 548 | } else { |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 549 | VRB(NULL, "Accepted a connection on %s:%u from %s:%u.", binds[i].address, binds[i].port, client_address, client_port); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 550 | } |
| 551 | |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 552 | if (host) { |
| 553 | *host = client_address; |
| 554 | } else { |
| 555 | free(client_address); |
| 556 | } |
| 557 | if (port) { |
| 558 | *port = client_port; |
| 559 | } |
| 560 | if (idx) { |
| 561 | *idx = i; |
| 562 | } |
| 563 | return client_sock; |
| 564 | |
| 565 | fail: |
| 566 | close(client_sock); |
| 567 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 568 | } |
| 569 | |
Michal Vasko | 238b6c1 | 2021-12-14 15:14:09 +0100 | [diff] [blame] | 570 | API struct nc_server_reply * |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 571 | nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *session) |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 572 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 573 | const char *identifier = NULL, *revision = NULL, *format = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 574 | char *model_data = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 575 | struct ly_out *out; |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 576 | const struct lys_module *module = NULL, *mod; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 577 | const struct lysp_submodule *submodule = NULL; |
| 578 | struct lyd_node *child, *err, *data = NULL; |
| 579 | LYS_OUTFORMAT outformat = 0; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 580 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 581 | LY_LIST_FOR(lyd_child(rpc), child) { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 582 | if (!strcmp(child->schema->name, "identifier")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 583 | identifier = lyd_get_value(child); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 584 | } else if (!strcmp(child->schema->name, "version")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 585 | revision = lyd_get_value(child); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 586 | if (revision && (revision[0] == '\0')) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 587 | revision = NULL; |
Radek Krejci | 1afa779 | 2017-03-26 11:24:16 -0500 | [diff] [blame] | 588 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 589 | } else if (!strcmp(child->schema->name, "format")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 590 | format = lyd_get_value(child); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 591 | } |
| 592 | } |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 593 | VRB(session, "Module \"%s@%s\" was requested.", identifier, revision ? revision : "<any>"); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 594 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 595 | /* check revision */ |
| 596 | if (revision && (strlen(revision) != 10) && strcmp(revision, "1.0")) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 597 | err = nc_err(session->ctx, NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 598 | nc_err_set_msg(err, "The requested version is not supported.", "en"); |
| 599 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 600 | } |
| 601 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 602 | if (revision) { |
| 603 | /* get specific module */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 604 | module = ly_ctx_get_module(session->ctx, identifier, revision); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 605 | if (!module) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 606 | submodule = ly_ctx_get_submodule(session->ctx, identifier, revision); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 607 | } |
| 608 | } else { |
| 609 | /* try to get implemented, then latest module */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 610 | module = ly_ctx_get_module_implemented(session->ctx, identifier); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 611 | if (!module) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 612 | module = ly_ctx_get_module_latest(session->ctx, identifier); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 613 | } |
| 614 | if (!module) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 615 | submodule = ly_ctx_get_submodule_latest(session->ctx, identifier); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 616 | } |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 617 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 618 | if (!module && !submodule) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 619 | err = nc_err(session->ctx, NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 620 | nc_err_set_msg(err, "The requested module was not found.", "en"); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 621 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /* check format */ |
Radek Krejci | 90fba64 | 2016-12-07 15:59:45 +0100 | [diff] [blame] | 625 | if (!format || !strcmp(format, "ietf-netconf-monitoring:yang")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 626 | outformat = LYS_OUT_YANG; |
Radek Krejci | 90fba64 | 2016-12-07 15:59:45 +0100 | [diff] [blame] | 627 | } else if (!strcmp(format, "ietf-netconf-monitoring:yin")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 628 | outformat = LYS_OUT_YIN; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 629 | } else { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 630 | err = nc_err(session->ctx, NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 631 | nc_err_set_msg(err, "The requested format is not supported.", "en"); |
| 632 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 633 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 634 | |
| 635 | /* print */ |
| 636 | ly_out_new_memory(&model_data, 0, &out); |
| 637 | if (module) { |
| 638 | lys_print_module(out, module, outformat, 0, 0); |
| 639 | } else { |
| 640 | lys_print_submodule(out, submodule, outformat, 0, 0); |
| 641 | } |
| 642 | ly_out_free(out, NULL, 0); |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 643 | if (!model_data) { |
| 644 | ERRINT; |
| 645 | return NULL; |
| 646 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 647 | |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 648 | /* create reply */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 649 | mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-monitoring"); |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 650 | if (!mod || lyd_new_inner(NULL, mod, "get-schema", 0, &data)) { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 651 | ERRINT; |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 652 | free(model_data); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 653 | return NULL; |
| 654 | } |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 655 | if (lyd_new_any(data, NULL, "data", model_data, 1, LYD_ANYDATA_STRING, 1, NULL)) { |
| 656 | ERRINT; |
Michal Vasko | a50f68e | 2022-02-24 16:10:54 +0100 | [diff] [blame] | 657 | free(model_data); |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 658 | lyd_free_tree(data); |
| 659 | return NULL; |
| 660 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 661 | |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 662 | return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 663 | } |
| 664 | |
Michal Vasko | 238b6c1 | 2021-12-14 15:14:09 +0100 | [diff] [blame] | 665 | API struct nc_server_reply * |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 666 | nc_clb_default_close_session(struct lyd_node *UNUSED(rpc), struct nc_session *session) |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 667 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 668 | session->term_reason = NC_SESSION_TERM_CLOSED; |
| 669 | return nc_server_reply_ok(); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 670 | } |
| 671 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 672 | /** |
| 673 | * @brief Initialize a context with default RPC callbacks if none are set. |
| 674 | * |
| 675 | * @param[in] ctx Context to initialize. |
| 676 | */ |
| 677 | static void |
| 678 | nc_server_init_ctx(const struct ly_ctx *ctx) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 679 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 680 | struct lysc_node *rpc; |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 681 | |
Michal Vasko | 238b6c1 | 2021-12-14 15:14:09 +0100 | [diff] [blame] | 682 | if (global_rpc_clb) { |
| 683 | /* expect it to handle these RPCs as well */ |
| 684 | return; |
| 685 | } |
| 686 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 687 | /* set default <get-schema> callback if not specified */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 688 | rpc = NULL; |
| 689 | if (ly_ctx_get_module_implemented(ctx, "ietf-netconf-monitoring")) { |
| 690 | rpc = (struct lysc_node *)lys_find_path(ctx, NULL, "/ietf-netconf-monitoring:get-schema", 0); |
| 691 | } |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 692 | if (rpc && !rpc->priv) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 693 | rpc->priv = nc_clb_default_get_schema; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 694 | } |
| 695 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 696 | /* set default <close-session> callback if not specified */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 697 | rpc = (struct lysc_node *)lys_find_path(ctx, NULL, "/ietf-netconf:close-session", 0); |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 698 | if (rpc && !rpc->priv) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 699 | rpc->priv = nc_clb_default_close_session; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 700 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 701 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 702 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 703 | API int |
| 704 | nc_server_init(void) |
| 705 | { |
| 706 | pthread_rwlockattr_t attr, *attr_p = NULL; |
| 707 | int r; |
| 708 | |
| 709 | nc_init(); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 710 | |
| 711 | server_opts.new_session_id = 1; |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 712 | server_opts.new_client_id = 1; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 713 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 714 | #ifdef HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP |
| 715 | if ((r = pthread_rwlockattr_init(&attr))) { |
| 716 | ERR(NULL, "%s: failed init attribute (%s).", __func__, strerror(r)); |
| 717 | goto error; |
| 718 | } |
| 719 | attr_p = &attr; |
| 720 | if ((r = pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP))) { |
| 721 | ERR(NULL, "%s: failed set attribute (%s).", __func__, strerror(r)); |
| 722 | goto error; |
| 723 | } |
Rosen Penev | ef2f3ac | 2019-07-15 18:15:28 -0700 | [diff] [blame] | 724 | #endif |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 725 | |
| 726 | if ((r = pthread_rwlock_init(&server_opts.endpt_lock, attr_p))) { |
| 727 | ERR(NULL, "%s: failed to init rwlock(%s).", __func__, strerror(r)); |
| 728 | goto error; |
| 729 | } |
| 730 | if ((r = pthread_rwlock_init(&server_opts.ch_client_lock, attr_p))) { |
| 731 | ERR(NULL, "%s: failed to init rwlock(%s).", __func__, strerror(r)); |
| 732 | goto error; |
| 733 | } |
| 734 | |
| 735 | if (attr_p) { |
| 736 | pthread_rwlockattr_destroy(attr_p); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 737 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 738 | return 0; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 739 | |
| 740 | error: |
| 741 | if (attr_p) { |
| 742 | pthread_rwlockattr_destroy(attr_p); |
| 743 | } |
| 744 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 745 | } |
| 746 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 747 | API void |
| 748 | nc_server_destroy(void) |
| 749 | { |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 750 | uint32_t i; |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 751 | |
| 752 | for (i = 0; i < server_opts.capabilities_count; i++) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 753 | free(server_opts.capabilities[i]); |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 754 | } |
| 755 | free(server_opts.capabilities); |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 756 | server_opts.capabilities = NULL; |
| 757 | server_opts.capabilities_count = 0; |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 758 | if (server_opts.content_id_data && server_opts.content_id_data_free) { |
| 759 | server_opts.content_id_data_free(server_opts.content_id_data); |
| 760 | } |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 761 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 762 | #if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS) |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 763 | nc_server_del_endpt(NULL, 0); |
Michal Vasko | 0bdf70b | 2019-06-24 19:20:20 +0200 | [diff] [blame] | 764 | nc_server_ch_del_client(NULL); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 765 | #endif |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 766 | #ifdef NC_ENABLED_SSH |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 767 | if (server_opts.passwd_auth_data && server_opts.passwd_auth_data_free) { |
| 768 | server_opts.passwd_auth_data_free(server_opts.passwd_auth_data); |
| 769 | } |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 770 | server_opts.passwd_auth_data = NULL; |
| 771 | server_opts.passwd_auth_data_free = NULL; |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 772 | |
Michal Vasko | 1c2d265 | 2023-10-17 08:53:36 +0200 | [diff] [blame] | 773 | if (server_opts.pubkey_auth_data && server_opts.pubkey_auth_data_free) { |
| 774 | server_opts.pubkey_auth_data_free(server_opts.pubkey_auth_data); |
| 775 | } |
| 776 | server_opts.pubkey_auth_data = NULL; |
| 777 | server_opts.pubkey_auth_data_free = NULL; |
| 778 | |
| 779 | if (server_opts.interactive_auth_sess_data && server_opts.interactive_auth_sess_data_free) { |
| 780 | server_opts.interactive_auth_sess_data_free(server_opts.interactive_auth_sess_data); |
| 781 | } |
| 782 | server_opts.interactive_auth_sess_data = NULL; |
| 783 | server_opts.interactive_auth_sess_data_free = NULL; |
| 784 | |
| 785 | if (server_opts.interactive_auth_data && server_opts.interactive_auth_data_free) { |
| 786 | server_opts.interactive_auth_data_free(server_opts.interactive_auth_data); |
| 787 | } |
| 788 | server_opts.interactive_auth_data = NULL; |
| 789 | server_opts.interactive_auth_data_free = NULL; |
| 790 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 791 | nc_server_ssh_del_authkey(NULL, NULL, 0, NULL); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 792 | |
| 793 | if (server_opts.hostkey_data && server_opts.hostkey_data_free) { |
| 794 | server_opts.hostkey_data_free(server_opts.hostkey_data); |
| 795 | } |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 796 | server_opts.hostkey_data = NULL; |
| 797 | server_opts.hostkey_data_free = NULL; |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 798 | |
| 799 | /* PAM */ |
| 800 | free(server_opts.conf_name); |
| 801 | free(server_opts.conf_dir); |
| 802 | server_opts.conf_name = NULL; |
| 803 | server_opts.conf_dir = NULL; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 804 | #endif |
| 805 | #ifdef NC_ENABLED_TLS |
| 806 | if (server_opts.server_cert_data && server_opts.server_cert_data_free) { |
| 807 | server_opts.server_cert_data_free(server_opts.server_cert_data); |
| 808 | } |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 809 | server_opts.server_cert_data = NULL; |
| 810 | server_opts.server_cert_data_free = NULL; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 811 | if (server_opts.trusted_cert_list_data && server_opts.trusted_cert_list_data_free) { |
| 812 | server_opts.trusted_cert_list_data_free(server_opts.trusted_cert_list_data); |
| 813 | } |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 814 | server_opts.trusted_cert_list_data = NULL; |
| 815 | server_opts.trusted_cert_list_data_free = NULL; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 816 | #endif |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 817 | nc_destroy(); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 818 | } |
| 819 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 820 | API int |
| 821 | nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported) |
| 822 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 823 | if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)) { |
| 824 | ERRARG("basic_mode"); |
| 825 | return -1; |
| 826 | } else if (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT))) { |
| 827 | ERRARG("also_supported"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 828 | return -1; |
| 829 | } |
| 830 | |
| 831 | server_opts.wd_basic_mode = basic_mode; |
| 832 | server_opts.wd_also_supported = also_supported; |
| 833 | return 0; |
| 834 | } |
| 835 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 836 | API void |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 837 | nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported) |
| 838 | { |
| 839 | if (!basic_mode && !also_supported) { |
| 840 | ERRARG("basic_mode and also_supported"); |
| 841 | return; |
| 842 | } |
| 843 | |
| 844 | if (basic_mode) { |
| 845 | *basic_mode = server_opts.wd_basic_mode; |
| 846 | } |
| 847 | if (also_supported) { |
| 848 | *also_supported = server_opts.wd_also_supported; |
| 849 | } |
| 850 | } |
| 851 | |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 852 | API int |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 853 | nc_server_set_capability(const char *value) |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 854 | { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 855 | void *mem; |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 856 | |
| 857 | if (!value || !value[0]) { |
| 858 | ERRARG("value must not be empty"); |
| 859 | return EXIT_FAILURE; |
| 860 | } |
| 861 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 862 | mem = realloc(server_opts.capabilities, (server_opts.capabilities_count + 1) * sizeof *server_opts.capabilities); |
| 863 | if (!mem) { |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 864 | ERRMEM; |
| 865 | return EXIT_FAILURE; |
| 866 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 867 | server_opts.capabilities = mem; |
| 868 | |
| 869 | server_opts.capabilities[server_opts.capabilities_count] = strdup(value); |
| 870 | server_opts.capabilities_count++; |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 871 | |
| 872 | return EXIT_SUCCESS; |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 873 | } |
| 874 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 875 | API void |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 876 | nc_server_set_content_id_clb(char *(*content_id_clb)(void *user_data), void *user_data, |
| 877 | void (*free_user_data)(void *user_data)) |
| 878 | { |
| 879 | server_opts.content_id_clb = content_id_clb; |
| 880 | server_opts.content_id_data = user_data; |
| 881 | server_opts.content_id_data_free = free_user_data; |
| 882 | } |
| 883 | |
| 884 | API void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 885 | nc_server_set_hello_timeout(uint16_t hello_timeout) |
| 886 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 887 | server_opts.hello_timeout = hello_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 888 | } |
| 889 | |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 890 | API uint16_t |
| 891 | nc_server_get_hello_timeout(void) |
| 892 | { |
| 893 | return server_opts.hello_timeout; |
| 894 | } |
| 895 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 896 | API void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 897 | nc_server_set_idle_timeout(uint16_t idle_timeout) |
| 898 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 899 | server_opts.idle_timeout = idle_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 900 | } |
| 901 | |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 902 | API uint16_t |
| 903 | nc_server_get_idle_timeout(void) |
| 904 | { |
| 905 | return server_opts.idle_timeout; |
| 906 | } |
| 907 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 908 | API NC_MSG_TYPE |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 909 | nc_accept_inout(int fdin, int fdout, const char *username, const struct ly_ctx *ctx, struct nc_session **session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 910 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 911 | NC_MSG_TYPE msgtype; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 912 | struct timespec ts_cur; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 913 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 914 | if (!ctx) { |
| 915 | ERRARG("ctx"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 916 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 917 | } else if (fdin < 0) { |
| 918 | ERRARG("fdin"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 919 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 920 | } else if (fdout < 0) { |
| 921 | ERRARG("fdout"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 922 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 923 | } else if (!username) { |
| 924 | ERRARG("username"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 925 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 926 | } else if (!session) { |
| 927 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 928 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 929 | } |
| 930 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 931 | /* init ctx as needed */ |
| 932 | nc_server_init_ctx(ctx); |
| 933 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 934 | /* prepare session structure */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 935 | *session = nc_new_session(NC_SERVER, 0); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 936 | if (!(*session)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 937 | ERRMEM; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 938 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 939 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 940 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 941 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 942 | /* transport specific data */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 943 | (*session)->ti_type = NC_TI_FD; |
| 944 | (*session)->ti.fd.in = fdin; |
| 945 | (*session)->ti.fd.out = fdout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 946 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 947 | /* assign context */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 948 | (*session)->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 949 | (*session)->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 950 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 951 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 952 | (*session)->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 953 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 954 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 955 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 956 | if (msgtype != NC_MSG_HELLO) { |
| 957 | nc_session_free(*session, NULL); |
| 958 | *session = NULL; |
| 959 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 960 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 961 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 962 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 963 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 964 | nc_realtime_get(&ts_cur); |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 965 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
| 966 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 967 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 968 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 969 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 970 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 971 | |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 972 | static void |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 973 | nc_ps_queue_add_id(struct nc_pollsession *ps, uint8_t *id) |
| 974 | { |
| 975 | uint8_t q_last; |
| 976 | |
| 977 | if (ps->queue_len == NC_PS_QUEUE_SIZE) { |
| 978 | ERRINT; |
| 979 | return; |
| 980 | } |
| 981 | |
| 982 | /* get a unique queue value (by adding 1 to the last added value, if any) */ |
| 983 | if (ps->queue_len) { |
| 984 | q_last = (ps->queue_begin + ps->queue_len - 1) % NC_PS_QUEUE_SIZE; |
| 985 | *id = ps->queue[q_last] + 1; |
| 986 | } else { |
| 987 | *id = 0; |
| 988 | } |
| 989 | |
| 990 | /* add the id into the queue */ |
| 991 | ++ps->queue_len; |
| 992 | q_last = (ps->queue_begin + ps->queue_len - 1) % NC_PS_QUEUE_SIZE; |
| 993 | ps->queue[q_last] = *id; |
| 994 | } |
| 995 | |
| 996 | static void |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 997 | nc_ps_queue_remove_id(struct nc_pollsession *ps, uint8_t id) |
| 998 | { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 999 | uint8_t i, q_idx, found = 0; |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1000 | |
| 1001 | for (i = 0; i < ps->queue_len; ++i) { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1002 | /* get the actual queue idx */ |
| 1003 | q_idx = (ps->queue_begin + i) % NC_PS_QUEUE_SIZE; |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1004 | |
| 1005 | if (found) { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1006 | if (ps->queue[q_idx] == id) { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1007 | /* another equal value, simply cannot be */ |
| 1008 | ERRINT; |
| 1009 | } |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 1010 | if (found == 2) { |
| 1011 | /* move the following values */ |
| 1012 | ps->queue[q_idx ? q_idx - 1 : NC_PS_QUEUE_SIZE - 1] = ps->queue[q_idx]; |
| 1013 | } |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1014 | } else if (ps->queue[q_idx] == id) { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1015 | /* found our id, there can be no more equal valid values */ |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 1016 | if (i == 0) { |
| 1017 | found = 1; |
| 1018 | } else { |
| 1019 | /* this is not okay, our id is in the middle of the queue */ |
| 1020 | found = 2; |
| 1021 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1022 | } |
| 1023 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1024 | if (!found) { |
| 1025 | ERRINT; |
Michal Vasko | 103fe63 | 2018-02-12 16:37:45 +0100 | [diff] [blame] | 1026 | return; |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1027 | } |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1028 | |
Michal Vasko | 103fe63 | 2018-02-12 16:37:45 +0100 | [diff] [blame] | 1029 | --ps->queue_len; |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 1030 | if (found == 1) { |
Michal Vasko | 103fe63 | 2018-02-12 16:37:45 +0100 | [diff] [blame] | 1031 | /* remove the id by moving the queue, otherwise all the values in the queue were moved */ |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 1032 | ps->queue_begin = (ps->queue_begin + 1) % NC_PS_QUEUE_SIZE; |
| 1033 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1036 | int |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1037 | nc_ps_lock(struct nc_pollsession *ps, uint8_t *id, const char *func) |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1038 | { |
| 1039 | int ret; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1040 | struct timespec ts; |
| 1041 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1042 | /* LOCK */ |
Michal Vasko | 8c7def5 | 2023-06-06 14:48:56 +0200 | [diff] [blame] | 1043 | ret = pthread_mutex_lock(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1044 | if (ret) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1045 | ERR(NULL, "%s: failed to lock a pollsession (%s).", func, strerror(ret)); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1046 | return -1; |
| 1047 | } |
| 1048 | |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1049 | /* check that the queue is long enough */ |
Michal Vasko | 8bc747c | 2018-02-09 16:37:04 +0100 | [diff] [blame] | 1050 | if (ps->queue_len == NC_PS_QUEUE_SIZE) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1051 | ERR(NULL, "%s: pollsession queue size (%d) too small.", func, NC_PS_QUEUE_SIZE); |
Michal Vasko | 8bc747c | 2018-02-09 16:37:04 +0100 | [diff] [blame] | 1052 | pthread_mutex_unlock(&ps->lock); |
| 1053 | return -1; |
| 1054 | } |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1055 | |
| 1056 | /* add ourselves into the queue */ |
| 1057 | nc_ps_queue_add_id(ps, id); |
Michal Vasko | fdba4a3 | 2022-01-05 12:13:53 +0100 | [diff] [blame] | 1058 | DBL(NULL, "PS 0x%p TID %lu queue: added %u, head %u, length %u", ps, (long unsigned int)pthread_self(), *id, |
Michal Vasko | 9129095 | 2019-09-27 11:30:55 +0200 | [diff] [blame] | 1059 | ps->queue[ps->queue_begin], ps->queue_len); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1060 | |
| 1061 | /* is it our turn? */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1062 | while (ps->queue[ps->queue_begin] != *id) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1063 | nc_timeouttime_get(&ts, NC_PS_QUEUE_TIMEOUT); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1064 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1065 | ret = pthread_cond_clockwait(&ps->cond, &ps->lock, COMPAT_CLOCK_ID, &ts); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1066 | if (ret) { |
preetbhansali | f3edf49 | 2018-12-14 17:53:32 +0530 | [diff] [blame] | 1067 | /** |
| 1068 | * This may happen when another thread releases the lock and broadcasts the condition |
| 1069 | * and this thread had already timed out. When this thread is scheduled, it returns timed out error |
| 1070 | * but when actually this thread was ready for condition. |
| 1071 | */ |
preetbhansali | 629dfc4 | 2018-12-17 16:04:40 +0530 | [diff] [blame] | 1072 | if ((ETIMEDOUT == ret) && (ps->queue[ps->queue_begin] == *id)) { |
preetbhansali | f3edf49 | 2018-12-14 17:53:32 +0530 | [diff] [blame] | 1073 | break; |
| 1074 | } |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1075 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1076 | ERR(NULL, "%s: failed to wait for a pollsession condition (%s).", func, strerror(ret)); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1077 | /* remove ourselves from the queue */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1078 | nc_ps_queue_remove_id(ps, *id); |
| 1079 | pthread_mutex_unlock(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1080 | return -1; |
| 1081 | } |
| 1082 | } |
| 1083 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1084 | /* UNLOCK */ |
| 1085 | pthread_mutex_unlock(&ps->lock); |
| 1086 | |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1090 | int |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1091 | nc_ps_unlock(struct nc_pollsession *ps, uint8_t id, const char *func) |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1092 | { |
| 1093 | int ret; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1094 | |
| 1095 | /* LOCK */ |
Michal Vasko | 8c7def5 | 2023-06-06 14:48:56 +0200 | [diff] [blame] | 1096 | ret = pthread_mutex_lock(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1097 | if (ret) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1098 | ERR(NULL, "%s: failed to lock a pollsession (%s).", func, strerror(ret)); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1099 | ret = -1; |
| 1100 | } |
| 1101 | |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1102 | /* we must be the first, it was our turn after all, right? */ |
| 1103 | if (ps->queue[ps->queue_begin] != id) { |
| 1104 | ERRINT; |
Michal Vasko | b1a094b | 2016-10-05 14:04:52 +0200 | [diff] [blame] | 1105 | /* UNLOCK */ |
| 1106 | if (!ret) { |
| 1107 | pthread_mutex_unlock(&ps->lock); |
| 1108 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1109 | return -1; |
| 1110 | } |
| 1111 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1112 | /* remove ourselves from the queue */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1113 | nc_ps_queue_remove_id(ps, id); |
Michal Vasko | fdba4a3 | 2022-01-05 12:13:53 +0100 | [diff] [blame] | 1114 | DBL(NULL, "PS 0x%p TID %lu queue: removed %u, head %u, length %u", ps, (long unsigned int)pthread_self(), id, |
Michal Vasko | 9129095 | 2019-09-27 11:30:55 +0200 | [diff] [blame] | 1115 | ps->queue[ps->queue_begin], ps->queue_len); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1116 | |
| 1117 | /* broadcast to all other threads that the queue moved */ |
| 1118 | pthread_cond_broadcast(&ps->cond); |
| 1119 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1120 | /* UNLOCK */ |
| 1121 | if (!ret) { |
| 1122 | pthread_mutex_unlock(&ps->lock); |
| 1123 | } |
| 1124 | |
| 1125 | return ret; |
| 1126 | } |
| 1127 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1128 | API struct nc_pollsession * |
| 1129 | nc_ps_new(void) |
| 1130 | { |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1131 | struct nc_pollsession *ps; |
| 1132 | |
| 1133 | ps = calloc(1, sizeof(struct nc_pollsession)); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1134 | if (!ps) { |
| 1135 | ERRMEM; |
| 1136 | return NULL; |
| 1137 | } |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1138 | pthread_cond_init(&ps->cond, NULL); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1139 | pthread_mutex_init(&ps->lock, NULL); |
| 1140 | |
| 1141 | return ps; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | API void |
| 1145 | nc_ps_free(struct nc_pollsession *ps) |
| 1146 | { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1147 | uint16_t i; |
| 1148 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1149 | if (!ps) { |
| 1150 | return; |
| 1151 | } |
| 1152 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1153 | if (ps->queue_len) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1154 | ERR(NULL, "FATAL: Freeing a pollsession structure that is currently being worked with!"); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1155 | } |
| 1156 | |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1157 | for (i = 0; i < ps->session_count; i++) { |
| 1158 | free(ps->sessions[i]); |
| 1159 | } |
| 1160 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1161 | free(ps->sessions); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1162 | pthread_mutex_destroy(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1163 | pthread_cond_destroy(&ps->cond); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1164 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1165 | free(ps); |
| 1166 | } |
| 1167 | |
| 1168 | API int |
| 1169 | nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session) |
| 1170 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1171 | uint8_t q_id; |
| 1172 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1173 | if (!ps) { |
| 1174 | ERRARG("ps"); |
| 1175 | return -1; |
| 1176 | } else if (!session) { |
| 1177 | ERRARG("session"); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1178 | return -1; |
| 1179 | } |
| 1180 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1181 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1182 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1183 | return -1; |
| 1184 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1185 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1186 | ++ps->session_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1187 | ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions); |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1188 | if (!ps->sessions) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1189 | ERRMEM; |
| 1190 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1191 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1192 | return -1; |
| 1193 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1194 | ps->sessions[ps->session_count - 1] = calloc(1, sizeof **ps->sessions); |
| 1195 | if (!ps->sessions[ps->session_count - 1]) { |
| 1196 | ERRMEM; |
| 1197 | --ps->session_count; |
| 1198 | /* UNLOCK */ |
| 1199 | nc_ps_unlock(ps, q_id, __func__); |
| 1200 | return -1; |
| 1201 | } |
| 1202 | ps->sessions[ps->session_count - 1]->session = session; |
| 1203 | ps->sessions[ps->session_count - 1]->state = NC_PS_STATE_NONE; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1204 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1205 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1206 | return nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1207 | } |
| 1208 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1209 | static int |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1210 | _nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session, int index) |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1211 | { |
| 1212 | uint16_t i; |
| 1213 | |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1214 | if (index >= 0) { |
| 1215 | i = (uint16_t)index; |
| 1216 | goto remove; |
| 1217 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1218 | for (i = 0; i < ps->session_count; ++i) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1219 | if (ps->sessions[i]->session == session) { |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1220 | remove: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1221 | --ps->session_count; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1222 | if (i <= ps->session_count) { |
| 1223 | free(ps->sessions[i]); |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 1224 | ps->sessions[i] = ps->sessions[ps->session_count]; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1225 | } |
| 1226 | if (!ps->session_count) { |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 1227 | free(ps->sessions); |
| 1228 | ps->sessions = NULL; |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 1229 | } |
Michal Vasko | 11d2f6a | 2017-02-02 11:15:06 +0100 | [diff] [blame] | 1230 | ps->last_event_session = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1231 | return 0; |
| 1232 | } |
| 1233 | } |
| 1234 | |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 1235 | return -1; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1236 | } |
| 1237 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1238 | API int |
| 1239 | nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session) |
| 1240 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1241 | uint8_t q_id; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1242 | int ret, ret2; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1243 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1244 | if (!ps) { |
| 1245 | ERRARG("ps"); |
| 1246 | return -1; |
| 1247 | } else if (!session) { |
| 1248 | ERRARG("session"); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1249 | return -1; |
| 1250 | } |
| 1251 | |
| 1252 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1253 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1254 | return -1; |
| 1255 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1256 | |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1257 | ret = _nc_ps_del_session(ps, session, -1); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1258 | |
| 1259 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1260 | ret2 = nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1261 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1262 | return ret || ret2 ? -1 : 0; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1263 | } |
| 1264 | |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1265 | API struct nc_session * |
Michal Vasko | 4871c9d | 2017-10-09 14:48:39 +0200 | [diff] [blame] | 1266 | nc_ps_get_session(const struct nc_pollsession *ps, uint16_t idx) |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1267 | { |
| 1268 | uint8_t q_id; |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1269 | struct nc_session *ret = NULL; |
| 1270 | |
| 1271 | if (!ps) { |
| 1272 | ERRARG("ps"); |
| 1273 | return NULL; |
| 1274 | } |
| 1275 | |
| 1276 | /* LOCK */ |
| 1277 | if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) { |
| 1278 | return NULL; |
| 1279 | } |
| 1280 | |
Michal Vasko | 4871c9d | 2017-10-09 14:48:39 +0200 | [diff] [blame] | 1281 | if (idx < ps->session_count) { |
| 1282 | ret = ps->sessions[idx]->session; |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | /* UNLOCK */ |
| 1286 | nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__); |
| 1287 | |
| 1288 | return ret; |
| 1289 | } |
| 1290 | |
Michal Vasko | 3ec3b11 | 2022-07-21 12:32:33 +0200 | [diff] [blame] | 1291 | API struct nc_session * |
| 1292 | nc_ps_find_session(const struct nc_pollsession *ps, nc_ps_session_match_cb match_cb, void *cb_data) |
| 1293 | { |
| 1294 | uint8_t q_id; |
| 1295 | uint16_t i; |
| 1296 | struct nc_session *ret = NULL; |
| 1297 | |
| 1298 | if (!ps) { |
| 1299 | ERRARG("ps"); |
| 1300 | return NULL; |
| 1301 | } |
| 1302 | |
| 1303 | /* LOCK */ |
| 1304 | if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) { |
| 1305 | return NULL; |
| 1306 | } |
| 1307 | |
| 1308 | for (i = 0; i < ps->session_count; ++i) { |
| 1309 | if (match_cb(ps->sessions[i]->session, cb_data)) { |
| 1310 | ret = ps->sessions[i]->session; |
| 1311 | break; |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | /* UNLOCK */ |
| 1316 | nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__); |
| 1317 | |
| 1318 | return ret; |
| 1319 | } |
| 1320 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1321 | API uint16_t |
| 1322 | nc_ps_session_count(struct nc_pollsession *ps) |
| 1323 | { |
Michal Vasko | 4700394 | 2019-03-14 12:25:23 +0100 | [diff] [blame] | 1324 | uint8_t q_id; |
| 1325 | uint16_t session_count; |
| 1326 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1327 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1328 | ERRARG("ps"); |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1329 | return 0; |
| 1330 | } |
| 1331 | |
Michal Vasko | 4700394 | 2019-03-14 12:25:23 +0100 | [diff] [blame] | 1332 | /* LOCK (just for memory barrier so that we read the current value) */ |
| 1333 | if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) { |
| 1334 | return 0; |
| 1335 | } |
| 1336 | |
| 1337 | session_count = ps->session_count; |
| 1338 | |
| 1339 | /* UNLOCK */ |
| 1340 | nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__); |
| 1341 | |
| 1342 | return session_count; |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1343 | } |
| 1344 | |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1345 | static NC_MSG_TYPE |
| 1346 | recv_rpc_check_msgid(struct nc_session *session, const struct lyd_node *envp) |
| 1347 | { |
| 1348 | struct lyd_attr *attr; |
| 1349 | |
| 1350 | assert(envp && !envp->schema); |
| 1351 | |
| 1352 | /* find the message-id attribute */ |
| 1353 | LY_LIST_FOR(((struct lyd_node_opaq *)envp)->attr, attr) { |
| 1354 | if (!strcmp(attr->name.name, "message-id")) { |
| 1355 | break; |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | if (!attr) { |
| 1360 | ERR(session, "Received an <rpc> without a message-id."); |
| 1361 | return NC_MSG_REPLY_ERR_MSGID; |
| 1362 | } |
| 1363 | |
| 1364 | return NC_MSG_RPC; |
| 1365 | } |
| 1366 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1367 | /* should be called holding the session RPC lock! IO lock will be acquired as needed |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1368 | * returns: NC_PSPOLL_ERROR, |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1369 | * NC_PSPOLL_TIMEOUT, |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1370 | * NC_PSPOLL_BAD_RPC, |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1371 | * NC_PSPOLL_RPC |
| 1372 | */ |
| 1373 | static int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1374 | nc_server_recv_rpc_io(struct nc_session *session, int io_timeout, struct nc_server_rpc **rpc) |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1375 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1376 | struct ly_in *msg; |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1377 | struct nc_server_reply *reply = NULL; |
Michal Vasko | 939ffce | 2021-04-12 13:02:01 +0200 | [diff] [blame] | 1378 | struct lyd_node *e; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1379 | int r, ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1380 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1381 | if (!session) { |
| 1382 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1383 | return NC_PSPOLL_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1384 | } else if (!rpc) { |
| 1385 | ERRARG("rpc"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1386 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1387 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1388 | ERR(session, "Invalid session to receive RPCs."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1389 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1390 | } |
| 1391 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1392 | *rpc = NULL; |
| 1393 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1394 | /* get a message */ |
| 1395 | r = nc_read_msg_io(session, io_timeout, &msg, 0); |
| 1396 | if (r == -2) { |
| 1397 | /* malformed message */ |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1398 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1399 | reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_MALFORMED_MSG)); |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1400 | goto cleanup; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1401 | } |
| 1402 | if (r == -1) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1403 | return NC_PSPOLL_ERROR; |
| 1404 | } else if (!r) { |
| 1405 | return NC_PSPOLL_TIMEOUT; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1406 | } |
| 1407 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1408 | *rpc = calloc(1, sizeof **rpc); |
| 1409 | if (!*rpc) { |
| 1410 | ERRMEM; |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1411 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1412 | goto cleanup; |
| 1413 | } |
| 1414 | |
| 1415 | /* parse the RPC */ |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1416 | if (!lyd_parse_op(session->ctx, NULL, msg, LYD_XML, LYD_TYPE_RPC_NETCONF, &(*rpc)->envp, &(*rpc)->rpc)) { |
| 1417 | /* check message-id */ |
| 1418 | if (recv_rpc_check_msgid(session, (*rpc)->envp) == NC_MSG_RPC) { |
| 1419 | /* valid RPC */ |
| 1420 | ret = NC_PSPOLL_RPC; |
| 1421 | } else { |
| 1422 | /* no message-id */ |
| 1423 | ret = NC_PSPOLL_BAD_RPC; |
| 1424 | reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_MISSING_ATTR, NC_ERR_TYPE_RPC, "message-id", "rpc")); |
| 1425 | } |
| 1426 | } else { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1427 | /* bad RPC received */ |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1428 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1429 | |
| 1430 | if ((*rpc)->envp) { |
| 1431 | /* at least the envelopes were parsed */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1432 | e = nc_err(session->ctx, NC_ERR_OP_FAILED, NC_ERR_TYPE_APP); |
| 1433 | nc_err_set_msg(e, ly_errmsg(session->ctx), "en"); |
Michal Vasko | 939ffce | 2021-04-12 13:02:01 +0200 | [diff] [blame] | 1434 | reply = nc_server_reply_err(e); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1435 | } else if (session->version == NC_VERSION_11) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1436 | /* completely malformed message, NETCONF version 1.1 defines sending error reply from |
| 1437 | * the server (RFC 6241 sec. 3) */ |
| 1438 | reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_MALFORMED_MSG)); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1439 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | cleanup: |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1443 | if (reply) { |
| 1444 | /* send error reply */ |
| 1445 | r = nc_write_msg_io(session, io_timeout, NC_MSG_REPLY, *rpc ? (*rpc)->envp : NULL, reply); |
| 1446 | nc_server_reply_free(reply); |
| 1447 | if (r != NC_MSG_REPLY) { |
| 1448 | ERR(session, "Failed to write reply (%s), terminating session.", nc_msgtype2str[r]); |
| 1449 | if (session->status != NC_STATUS_INVALID) { |
| 1450 | session->status = NC_STATUS_INVALID; |
| 1451 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1456 | ly_in_free(msg, 1); |
| 1457 | if (ret != NC_PSPOLL_RPC) { |
| 1458 | nc_server_rpc_free(*rpc); |
| 1459 | *rpc = NULL; |
| 1460 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1461 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1462 | } |
| 1463 | |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1464 | API void |
| 1465 | nc_set_global_rpc_clb(nc_rpc_clb clb) |
| 1466 | { |
| 1467 | global_rpc_clb = clb; |
| 1468 | } |
| 1469 | |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1470 | API NC_MSG_TYPE |
| 1471 | nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout) |
| 1472 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1473 | NC_MSG_TYPE ret; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1474 | |
| 1475 | /* check parameters */ |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 1476 | if (!session || (session->side != NC_SERVER) || !nc_session_get_notif_status(session)) { |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1477 | ERRARG("session"); |
| 1478 | return NC_MSG_ERROR; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1479 | } else if (!notif || !notif->ntf || !notif->eventtime) { |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1480 | ERRARG("notif"); |
| 1481 | return NC_MSG_ERROR; |
| 1482 | } |
| 1483 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1484 | /* we do not need RPC lock for this, IO lock will be acquired properly */ |
| 1485 | ret = nc_write_msg_io(session, timeout, NC_MSG_NOTIF, notif); |
Michal Vasko | 8fe604c | 2020-02-10 15:25:04 +0100 | [diff] [blame] | 1486 | if (ret != NC_MSG_NOTIF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1487 | ERR(session, "Failed to write notification (%s).", nc_msgtype2str[ret]); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1488 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1489 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1490 | return ret; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1491 | } |
| 1492 | |
Michal Vasko | f946776 | 2023-03-28 09:02:08 +0200 | [diff] [blame] | 1493 | /** |
| 1494 | * @brief Send a reply acquiring IO lock as needed. |
| 1495 | * Session RPC lock must be held! |
| 1496 | * |
| 1497 | * @param[in] session Session to use. |
| 1498 | * @param[in] io_timeout Timeout to use for acquiring IO lock. |
| 1499 | * @param[in] rpc RPC to sent. |
| 1500 | * @return 0 on success. |
| 1501 | * @return Bitmask of NC_PSPOLL_ERROR (any fatal error) and NC_PSPOLL_REPLY_ERROR (reply failed to be sent). |
| 1502 | * @return NC_PSPOLL_ERROR on other errors. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1503 | */ |
| 1504 | static int |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1505 | nc_server_send_reply_io(struct nc_session *session, int io_timeout, const struct nc_server_rpc *rpc) |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1506 | { |
| 1507 | nc_rpc_clb clb; |
| 1508 | struct nc_server_reply *reply; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1509 | const struct lysc_node *rpc_act = NULL; |
| 1510 | struct lyd_node *elem; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1511 | int ret = 0; |
| 1512 | NC_MSG_TYPE r; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1513 | |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1514 | if (!rpc) { |
| 1515 | ERRINT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1516 | return NC_PSPOLL_ERROR; |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1517 | } |
| 1518 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1519 | if (rpc->rpc->schema->nodetype == LYS_RPC) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1520 | /* RPC */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1521 | rpc_act = rpc->rpc->schema; |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1522 | } else { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1523 | /* action */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1524 | LYD_TREE_DFS_BEGIN(rpc->rpc, elem) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1525 | if (elem->schema->nodetype == LYS_ACTION) { |
| 1526 | rpc_act = elem->schema; |
| 1527 | break; |
| 1528 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1529 | LYD_TREE_DFS_END(rpc->rpc, elem); |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1530 | } |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1531 | if (!rpc_act) { |
| 1532 | ERRINT; |
| 1533 | return NC_PSPOLL_ERROR; |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | if (!rpc_act->priv) { |
Petr A. Sokolov | 16284ca | 2019-02-04 09:02:40 +0300 | [diff] [blame] | 1538 | if (!global_rpc_clb) { |
| 1539 | /* no callback, reply with a not-implemented error */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1540 | reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT)); |
Petr A. Sokolov | 16284ca | 2019-02-04 09:02:40 +0300 | [diff] [blame] | 1541 | } else { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1542 | reply = global_rpc_clb(rpc->rpc, session); |
Petr A. Sokolov | 16284ca | 2019-02-04 09:02:40 +0300 | [diff] [blame] | 1543 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1544 | } else { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1545 | clb = (nc_rpc_clb)rpc_act->priv; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1546 | reply = clb(rpc->rpc, session); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | if (!reply) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1550 | reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_OP_FAILED, NC_ERR_TYPE_APP)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1551 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1552 | r = nc_write_msg_io(session, io_timeout, NC_MSG_REPLY, rpc->envp, reply); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1553 | if (reply->type == NC_RPL_ERROR) { |
| 1554 | ret |= NC_PSPOLL_REPLY_ERROR; |
| 1555 | } |
| 1556 | nc_server_reply_free(reply); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1557 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1558 | if (r != NC_MSG_REPLY) { |
Michal Vasko | 1546949 | 2021-06-09 08:40:48 +0200 | [diff] [blame] | 1559 | ERR(session, "Failed to write reply (%s).", nc_msgtype2str[r]); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1560 | ret |= NC_PSPOLL_ERROR; |
| 1561 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1562 | |
| 1563 | /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */ |
| 1564 | if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) { |
| 1565 | session->status = NC_STATUS_INVALID; |
| 1566 | } |
| 1567 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1568 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1569 | } |
| 1570 | |
Michal Vasko | f946776 | 2023-03-28 09:02:08 +0200 | [diff] [blame] | 1571 | /** |
| 1572 | * @brief Poll a session from pspoll acquiring IO lock as needed. |
| 1573 | * Session must be running and session RPC lock held! |
| 1574 | * |
| 1575 | * @param[in] session Session to use. |
| 1576 | * @param[in] io_timeout Timeout to use for acquiring IO lock. |
| 1577 | * @param[in] now_mono Current monotonic timestamp. |
| 1578 | * @param[in,out] msg Message to fill in case of an error. |
| 1579 | * @return NC_PSPOLL_RPC if some application data are available. |
| 1580 | * @return NC_PSPOLL_TIMEOUT if a timeout elapsed. |
| 1581 | * @return NC_PSPOLL_SSH_CHANNEL if a new SSH channel has been created. |
| 1582 | * @return NC_PSPOLL_SSH_MSG if just an SSH message has been processed. |
| 1583 | * @return NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR if session has been terminated (@p msg filled). |
| 1584 | * @return NC_PSPOLL_ERROR on other fatal errors (@p msg filled). |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1585 | */ |
| 1586 | static int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1587 | nc_ps_poll_session_io(struct nc_session *session, int io_timeout, time_t now_mono, char *msg) |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1588 | { |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1589 | struct pollfd pfd; |
Michal Vasko | 2260f55 | 2018-06-06 10:06:12 +0200 | [diff] [blame] | 1590 | int r, ret = 0; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1591 | |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1592 | #ifdef NC_ENABLED_SSH |
| 1593 | struct nc_session *new; |
| 1594 | #endif |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1595 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1596 | /* check timeout first */ |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 1597 | if (!(session->flags & NC_SESSION_CALLHOME) && !nc_session_get_notif_status(session) && server_opts.idle_timeout && |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1598 | (now_mono >= session->opts.server.last_rpc + server_opts.idle_timeout)) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1599 | sprintf(msg, "session idle timeout elapsed"); |
| 1600 | session->status = NC_STATUS_INVALID; |
| 1601 | session->term_reason = NC_SESSION_TERM_TIMEOUT; |
| 1602 | return NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1603 | } |
| 1604 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1605 | r = nc_session_io_lock(session, io_timeout, __func__); |
| 1606 | if (r < 0) { |
| 1607 | sprintf(msg, "session IO lock failed to be acquired"); |
| 1608 | return NC_PSPOLL_ERROR; |
| 1609 | } else if (!r) { |
| 1610 | return NC_PSPOLL_TIMEOUT; |
| 1611 | } |
| 1612 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1613 | switch (session->ti_type) { |
| 1614 | #ifdef NC_ENABLED_SSH |
| 1615 | case NC_TI_LIBSSH: |
| 1616 | r = ssh_channel_poll_timeout(session->ti.libssh.channel, 0, 0); |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1617 | if (r == SSH_EOF) { |
| 1618 | sprintf(msg, "SSH channel unexpected EOF"); |
| 1619 | session->status = NC_STATUS_INVALID; |
| 1620 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1621 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1622 | } else if (r == SSH_ERROR) { |
| 1623 | sprintf(msg, "SSH channel poll error (%s)", ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1624 | session->status = NC_STATUS_INVALID; |
| 1625 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1626 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1627 | } else if (!r) { |
| 1628 | if (session->flags & NC_SESSION_SSH_NEW_MSG) { |
| 1629 | /* new SSH message */ |
| 1630 | session->flags &= ~NC_SESSION_SSH_NEW_MSG; |
| 1631 | if (session->ti.libssh.next) { |
| 1632 | for (new = session->ti.libssh.next; new != session; new = new->ti.libssh.next) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1633 | if ((new->status == NC_STATUS_STARTING) && new->ti.libssh.channel && |
| 1634 | (new->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1635 | /* new NETCONF SSH channel */ |
| 1636 | ret = NC_PSPOLL_SSH_CHANNEL; |
| 1637 | break; |
| 1638 | } |
| 1639 | } |
| 1640 | if (new != session) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1641 | break; |
| 1642 | } |
| 1643 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1644 | |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1645 | /* just some SSH message */ |
| 1646 | ret = NC_PSPOLL_SSH_MSG; |
| 1647 | } else { |
| 1648 | ret = NC_PSPOLL_TIMEOUT; |
| 1649 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1650 | } else { |
| 1651 | /* we have some application data */ |
| 1652 | ret = NC_PSPOLL_RPC; |
| 1653 | } |
| 1654 | break; |
| 1655 | #endif |
| 1656 | #ifdef NC_ENABLED_TLS |
| 1657 | case NC_TI_OPENSSL: |
| 1658 | r = SSL_pending(session->ti.tls); |
| 1659 | if (!r) { |
| 1660 | /* no data pending in the SSL buffer, poll fd */ |
| 1661 | pfd.fd = SSL_get_rfd(session->ti.tls); |
| 1662 | if (pfd.fd < 0) { |
| 1663 | sprintf(msg, "internal error (%s:%d)", __FILE__, __LINE__); |
| 1664 | ret = NC_PSPOLL_ERROR; |
| 1665 | break; |
| 1666 | } |
| 1667 | pfd.events = POLLIN; |
| 1668 | pfd.revents = 0; |
| 1669 | r = poll(&pfd, 1, 0); |
| 1670 | |
| 1671 | if ((r < 0) && (errno != EINTR)) { |
| 1672 | sprintf(msg, "poll failed (%s)", strerror(errno)); |
| 1673 | session->status = NC_STATUS_INVALID; |
| 1674 | ret = NC_PSPOLL_ERROR; |
| 1675 | } else if (r > 0) { |
| 1676 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
| 1677 | sprintf(msg, "communication socket unexpectedly closed"); |
| 1678 | session->status = NC_STATUS_INVALID; |
| 1679 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1680 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1681 | } else if (pfd.revents & POLLERR) { |
| 1682 | sprintf(msg, "communication socket error"); |
| 1683 | session->status = NC_STATUS_INVALID; |
| 1684 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1685 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1686 | } else { |
| 1687 | ret = NC_PSPOLL_RPC; |
| 1688 | } |
| 1689 | } else { |
| 1690 | ret = NC_PSPOLL_TIMEOUT; |
| 1691 | } |
| 1692 | } else { |
| 1693 | ret = NC_PSPOLL_RPC; |
| 1694 | } |
| 1695 | break; |
| 1696 | #endif |
| 1697 | case NC_TI_FD: |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1698 | case NC_TI_UNIX: |
| 1699 | pfd.fd = (session->ti_type == NC_TI_FD) ? session->ti.fd.in : session->ti.unixsock.sock; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1700 | pfd.events = POLLIN; |
| 1701 | pfd.revents = 0; |
| 1702 | r = poll(&pfd, 1, 0); |
| 1703 | |
| 1704 | if ((r < 0) && (errno != EINTR)) { |
| 1705 | sprintf(msg, "poll failed (%s)", strerror(errno)); |
| 1706 | session->status = NC_STATUS_INVALID; |
| 1707 | ret = NC_PSPOLL_ERROR; |
| 1708 | } else if (r > 0) { |
| 1709 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
| 1710 | sprintf(msg, "communication socket unexpectedly closed"); |
| 1711 | session->status = NC_STATUS_INVALID; |
| 1712 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1713 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1714 | } else if (pfd.revents & POLLERR) { |
| 1715 | sprintf(msg, "communication socket error"); |
| 1716 | session->status = NC_STATUS_INVALID; |
| 1717 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1718 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1719 | } else { |
| 1720 | ret = NC_PSPOLL_RPC; |
| 1721 | } |
| 1722 | } else { |
| 1723 | ret = NC_PSPOLL_TIMEOUT; |
| 1724 | } |
| 1725 | break; |
| 1726 | case NC_TI_NONE: |
| 1727 | sprintf(msg, "internal error (%s:%d)", __FILE__, __LINE__); |
| 1728 | ret = NC_PSPOLL_ERROR; |
| 1729 | break; |
| 1730 | } |
| 1731 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1732 | nc_session_io_unlock(session, __func__); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1733 | return ret; |
| 1734 | } |
| 1735 | |
| 1736 | API int |
| 1737 | nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session) |
| 1738 | { |
Michal Vasko | 443faa0 | 2022-10-20 09:09:03 +0200 | [diff] [blame] | 1739 | int ret = NC_PSPOLL_ERROR, r; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1740 | uint8_t q_id; |
| 1741 | uint16_t i, j; |
| 1742 | char msg[256]; |
| 1743 | struct timespec ts_timeout, ts_cur; |
| 1744 | struct nc_session *cur_session; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1745 | struct nc_ps_session *cur_ps_session; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1746 | struct nc_server_rpc *rpc = NULL; |
| 1747 | |
Michal Vasko | 30a5d6b | 2017-02-15 14:29:39 +0100 | [diff] [blame] | 1748 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1749 | ERRARG("ps"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1750 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1751 | } |
| 1752 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1753 | /* PS LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1754 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1755 | return NC_PSPOLL_ERROR; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1756 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1757 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1758 | if (!ps->session_count) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1759 | nc_ps_unlock(ps, q_id, __func__); |
| 1760 | return NC_PSPOLL_NOSESSIONS; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1761 | } |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1762 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1763 | /* fill timespecs */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1764 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1765 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1766 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | /* poll all the sessions one-by-one */ |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1770 | do { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1771 | /* loop from i to j once (all sessions) */ |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1772 | if (ps->last_event_session == ps->session_count - 1) { |
| 1773 | i = j = 0; |
| 1774 | } else { |
| 1775 | i = j = ps->last_event_session + 1; |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1776 | } |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1777 | do { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1778 | cur_ps_session = ps->sessions[i]; |
| 1779 | cur_session = cur_ps_session->session; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1780 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1781 | /* SESSION RPC LOCK */ |
| 1782 | r = nc_session_rpc_lock(cur_session, 0, __func__); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1783 | if (r == -1) { |
| 1784 | ret = NC_PSPOLL_ERROR; |
| 1785 | } else if (r == 1) { |
| 1786 | /* no one else is currently working with the session, so we can, otherwise skip it */ |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1787 | switch (cur_ps_session->state) { |
| 1788 | case NC_PS_STATE_NONE: |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1789 | if (cur_session->status == NC_STATUS_RUNNING) { |
| 1790 | /* session is fine, work with it */ |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1791 | cur_ps_session->state = NC_PS_STATE_BUSY; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1792 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1793 | ret = nc_ps_poll_session_io(cur_session, NC_SESSION_LOCK_TIMEOUT, ts_cur.tv_sec, msg); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1794 | switch (ret) { |
| 1795 | case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1796 | ERR(cur_session, "%s.", msg); |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1797 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1798 | break; |
| 1799 | case NC_PSPOLL_ERROR: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1800 | ERR(cur_session, "%s.", msg); |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1801 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1802 | break; |
| 1803 | case NC_PSPOLL_TIMEOUT: |
| 1804 | #ifdef NC_ENABLED_SSH |
| 1805 | case NC_PSPOLL_SSH_CHANNEL: |
| 1806 | case NC_PSPOLL_SSH_MSG: |
| 1807 | #endif |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1808 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1809 | break; |
| 1810 | case NC_PSPOLL_RPC: |
| 1811 | /* let's keep the state busy, we are not done with this session */ |
| 1812 | break; |
| 1813 | } |
| 1814 | } else { |
| 1815 | /* session is not fine, let the caller know */ |
| 1816 | ret = NC_PSPOLL_SESSION_TERM; |
| 1817 | if (cur_session->term_reason != NC_SESSION_TERM_CLOSED) { |
| 1818 | ret |= NC_PSPOLL_SESSION_ERROR; |
| 1819 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1820 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1821 | } |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1822 | break; |
| 1823 | case NC_PS_STATE_BUSY: |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1824 | /* it definitely should not be busy because we have the lock */ |
| 1825 | ERRINT; |
Michal Vasko | 4992d80 | 2017-08-09 12:20:01 +0200 | [diff] [blame] | 1826 | ret = NC_PSPOLL_ERROR; |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1827 | break; |
| 1828 | case NC_PS_STATE_INVALID: |
| 1829 | /* we got it locked, but it will be freed, let it be */ |
| 1830 | ret = NC_PSPOLL_TIMEOUT; |
| 1831 | break; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1832 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1833 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1834 | /* keep RPC lock in this one case */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1835 | if (ret != NC_PSPOLL_RPC) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1836 | /* SESSION RPC UNLOCK */ |
| 1837 | nc_session_rpc_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1838 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1839 | } else { |
| 1840 | /* timeout */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1841 | ret = NC_PSPOLL_TIMEOUT; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1842 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1843 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1844 | /* something happened */ |
| 1845 | if (ret != NC_PSPOLL_TIMEOUT) { |
| 1846 | break; |
| 1847 | } |
| 1848 | |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1849 | if (i == ps->session_count - 1) { |
| 1850 | i = 0; |
| 1851 | } else { |
| 1852 | ++i; |
| 1853 | } |
| 1854 | } while (i != j); |
| 1855 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1856 | /* no event, no session remains locked */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1857 | if (ret == NC_PSPOLL_TIMEOUT) { |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1858 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1859 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1860 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1861 | /* final timeout */ |
| 1862 | break; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1863 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1864 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1865 | } while (ret == NC_PSPOLL_TIMEOUT); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1866 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1867 | /* do we want to return the session? */ |
| 1868 | switch (ret) { |
| 1869 | case NC_PSPOLL_RPC: |
| 1870 | case NC_PSPOLL_SESSION_TERM: |
| 1871 | case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR: |
| 1872 | #ifdef NC_ENABLED_SSH |
| 1873 | case NC_PSPOLL_SSH_CHANNEL: |
| 1874 | case NC_PSPOLL_SSH_MSG: |
| 1875 | #endif |
| 1876 | if (session) { |
| 1877 | *session = cur_session; |
| 1878 | } |
| 1879 | ps->last_event_session = i; |
| 1880 | break; |
| 1881 | default: |
| 1882 | break; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1883 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1884 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1885 | /* PS UNLOCK */ |
| 1886 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1887 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1888 | /* we have some data available and the session is RPC locked (but not IO locked) */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1889 | if (ret == NC_PSPOLL_RPC) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1890 | ret = nc_server_recv_rpc_io(cur_session, timeout, &rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1891 | if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) { |
| 1892 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1893 | ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1894 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1895 | } else { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1896 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1897 | } |
| 1898 | } else { |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1899 | cur_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1900 | |
Michal Vasko | 7f1ee93 | 2018-10-11 09:41:42 +0200 | [diff] [blame] | 1901 | /* process RPC */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1902 | ret |= nc_server_send_reply_io(cur_session, timeout, rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1903 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1904 | ret |= NC_PSPOLL_SESSION_TERM; |
| 1905 | if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) { |
| 1906 | ret |= NC_PSPOLL_SESSION_ERROR; |
| 1907 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1908 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1909 | } else { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1910 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1911 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1912 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1913 | nc_server_rpc_free(rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1914 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1915 | /* SESSION RPC UNLOCK */ |
| 1916 | nc_session_rpc_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1917 | } |
| 1918 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1919 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1920 | } |
| 1921 | |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1922 | API void |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1923 | nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *)) |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1924 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1925 | uint8_t q_id; |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1926 | uint16_t i; |
| 1927 | struct nc_session *session; |
| 1928 | |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1929 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1930 | ERRARG("ps"); |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1931 | return; |
| 1932 | } |
| 1933 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1934 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1935 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1936 | return; |
| 1937 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1938 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1939 | if (all) { |
Radek Krejci | 4f8042c | 2016-03-03 13:11:26 +0100 | [diff] [blame] | 1940 | for (i = 0; i < ps->session_count; i++) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1941 | nc_session_free(ps->sessions[i]->session, data_free); |
| 1942 | free(ps->sessions[i]); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1943 | } |
| 1944 | free(ps->sessions); |
| 1945 | ps->sessions = NULL; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1946 | ps->session_count = 0; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1947 | ps->last_event_session = 0; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1948 | } else { |
| 1949 | for (i = 0; i < ps->session_count; ) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1950 | if (ps->sessions[i]->session->status != NC_STATUS_RUNNING) { |
| 1951 | session = ps->sessions[i]->session; |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1952 | _nc_ps_del_session(ps, NULL, i); |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1953 | nc_session_free(session, data_free); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1954 | continue; |
| 1955 | } |
| 1956 | |
| 1957 | ++i; |
| 1958 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1959 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1960 | |
| 1961 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1962 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1963 | } |
| 1964 | |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1965 | static int |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1966 | nc_get_uid(int sock, uid_t *uid) |
| 1967 | { |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1968 | int ret; |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1969 | |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1970 | #ifdef SO_PEERCRED |
| 1971 | struct ucred ucred; |
| 1972 | socklen_t len; |
Michal Vasko | 292c554 | 2023-02-01 14:33:17 +0100 | [diff] [blame] | 1973 | |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1974 | len = sizeof(ucred); |
| 1975 | ret = getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len); |
| 1976 | if (!ret) { |
| 1977 | *uid = ucred.uid; |
| 1978 | } |
| 1979 | #else |
| 1980 | ret = getpeereid(sock, uid, NULL); |
| 1981 | #endif |
| 1982 | |
| 1983 | if (ret < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1984 | ERR(NULL, "Failed to get credentials from unix socket (%s).", strerror(errno)); |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1985 | return -1; |
| 1986 | } |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1987 | return 0; |
| 1988 | } |
| 1989 | |
| 1990 | static int |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1991 | nc_accept_unix(struct nc_session *session, int sock) |
| 1992 | { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1993 | #if defined (SO_PEERCRED) || defined (HAVE_GETPEEREID) |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1994 | struct passwd *pw, pw_buf; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1995 | char *username; |
Michal Vasko | 292c554 | 2023-02-01 14:33:17 +0100 | [diff] [blame] | 1996 | |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1997 | session->ti_type = NC_TI_UNIX; |
Michal Vasko | 143aa14 | 2021-10-01 15:31:48 +0200 | [diff] [blame] | 1998 | uid_t uid = 0; |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1999 | char *buf = NULL; |
| 2000 | size_t buf_len = 0; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2001 | |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 2002 | if (nc_get_uid(sock, &uid)) { |
| 2003 | close(sock); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2004 | return -1; |
| 2005 | } |
| 2006 | |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 2007 | pw = nc_getpwuid(uid, &pw_buf, &buf, &buf_len); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2008 | if (pw == NULL) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2009 | ERR(NULL, "Failed to find username for uid=%u (%s).\n", uid, strerror(errno)); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2010 | close(sock); |
| 2011 | return -1; |
| 2012 | } |
| 2013 | |
| 2014 | username = strdup(pw->pw_name); |
Michal Vasko | ccd2dd0 | 2021-10-11 09:13:01 +0200 | [diff] [blame] | 2015 | free(buf); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2016 | if (username == NULL) { |
| 2017 | ERRMEM; |
| 2018 | close(sock); |
| 2019 | return -1; |
| 2020 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2021 | session->username = username; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2022 | |
| 2023 | session->ti.unixsock.sock = sock; |
| 2024 | |
| 2025 | return 1; |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 2026 | #else |
| 2027 | return -1; |
| 2028 | #endif |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2029 | } |
| 2030 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2031 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2032 | nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2033 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2034 | uint16_t i; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2035 | int ret = 0; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2036 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2037 | if (!name) { |
| 2038 | ERRARG("name"); |
| 2039 | return -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2040 | } |
| 2041 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2042 | /* BIND LOCK */ |
| 2043 | pthread_mutex_lock(&server_opts.bind_lock); |
| 2044 | |
| 2045 | /* ENDPT WRITE LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2046 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2047 | |
| 2048 | /* check name uniqueness */ |
| 2049 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2050 | if (!strcmp(server_opts.endpts[i].name, name)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2051 | ERR(NULL, "Endpoint \"%s\" already exists.", name); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2052 | ret = -1; |
| 2053 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2054 | } |
| 2055 | } |
| 2056 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2057 | server_opts.endpts = nc_realloc(server_opts.endpts, (server_opts.endpt_count + 1) * sizeof *server_opts.endpts); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2058 | if (!server_opts.endpts) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 2059 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2060 | ret = -1; |
| 2061 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2062 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2063 | memset(&server_opts.endpts[server_opts.endpt_count], 0, sizeof *server_opts.endpts); |
| 2064 | ++server_opts.endpt_count; |
| 2065 | |
| 2066 | server_opts.endpts[server_opts.endpt_count - 1].name = strdup(name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2067 | server_opts.endpts[server_opts.endpt_count - 1].ti = ti; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2068 | server_opts.endpts[server_opts.endpt_count - 1].ka.idle_time = 1; |
| 2069 | server_opts.endpts[server_opts.endpt_count - 1].ka.max_probes = 10; |
| 2070 | server_opts.endpts[server_opts.endpt_count - 1].ka.probe_interval = 5; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2071 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2072 | server_opts.binds = nc_realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2073 | if (!server_opts.binds) { |
| 2074 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2075 | ret = -1; |
| 2076 | goto cleanup; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 2077 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2078 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2079 | memset(&server_opts.binds[server_opts.endpt_count - 1], 0, sizeof *server_opts.binds); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2080 | server_opts.binds[server_opts.endpt_count - 1].sock = -1; |
| 2081 | |
| 2082 | switch (ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2083 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2084 | case NC_TI_LIBSSH: |
| 2085 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 2086 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.ssh) { |
| 2087 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2088 | ret = -1; |
| 2089 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2090 | } |
| 2091 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_methods = |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 2092 | NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2093 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_attempts = 3; |
Michal Vasko | cbad4c5 | 2019-06-27 16:30:35 +0200 | [diff] [blame] | 2094 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_timeout = 30; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2095 | break; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2096 | #endif |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2097 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2098 | case NC_TI_OPENSSL: |
| 2099 | server_opts.endpts[server_opts.endpt_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 2100 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.tls) { |
| 2101 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2102 | ret = -1; |
| 2103 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2104 | } |
| 2105 | break; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2106 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2107 | case NC_TI_UNIX: |
| 2108 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock = calloc(1, sizeof(struct nc_server_unix_opts)); |
| 2109 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock) { |
| 2110 | ERRMEM; |
| 2111 | ret = -1; |
| 2112 | goto cleanup; |
| 2113 | } |
| 2114 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->mode = (mode_t)-1; |
| 2115 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->uid = (uid_t)-1; |
| 2116 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->gid = (gid_t)-1; |
| 2117 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2118 | default: |
| 2119 | ERRINT; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2120 | ret = -1; |
| 2121 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2122 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2123 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2124 | cleanup: |
| 2125 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2126 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2127 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2128 | /* BIND UNLOCK */ |
| 2129 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2130 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2131 | return ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2132 | } |
| 2133 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2134 | API int |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2135 | nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2136 | { |
| 2137 | uint32_t i; |
| 2138 | int ret = -1; |
| 2139 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2140 | /* BIND LOCK */ |
| 2141 | pthread_mutex_lock(&server_opts.bind_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2142 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2143 | /* ENDPT WRITE LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2144 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2145 | |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2146 | if (!name && !ti) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2147 | /* remove all endpoints */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2148 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2149 | free(server_opts.endpts[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2150 | switch (server_opts.endpts[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2151 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2152 | case NC_TI_LIBSSH: |
| 2153 | nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh); |
| 2154 | free(server_opts.endpts[i].opts.ssh); |
| 2155 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2156 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2157 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2158 | case NC_TI_OPENSSL: |
| 2159 | nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls); |
| 2160 | free(server_opts.endpts[i].opts.tls); |
| 2161 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2162 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2163 | case NC_TI_UNIX: |
| 2164 | free(server_opts.endpts[i].opts.unixsock); |
| 2165 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2166 | default: |
| 2167 | ERRINT; |
| 2168 | /* won't get here ...*/ |
| 2169 | break; |
| 2170 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2171 | ret = 0; |
| 2172 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2173 | free(server_opts.endpts); |
| 2174 | server_opts.endpts = NULL; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2175 | |
| 2176 | /* remove all binds */ |
| 2177 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2178 | free(server_opts.binds[i].address); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2179 | if (server_opts.binds[i].sock > -1) { |
| 2180 | close(server_opts.binds[i].sock); |
| 2181 | } |
| 2182 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2183 | free(server_opts.binds); |
| 2184 | server_opts.binds = NULL; |
| 2185 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2186 | server_opts.endpt_count = 0; |
| 2187 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2188 | } else { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2189 | /* remove one endpoint with bind(s) or all endpoints using one transport protocol */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2190 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2191 | if ((name && !strcmp(server_opts.endpts[i].name, name)) || (!name && (server_opts.endpts[i].ti == ti))) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2192 | /* remove endpt */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2193 | free(server_opts.endpts[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2194 | switch (server_opts.endpts[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2195 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2196 | case NC_TI_LIBSSH: |
| 2197 | nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh); |
| 2198 | free(server_opts.endpts[i].opts.ssh); |
| 2199 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2200 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2201 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2202 | case NC_TI_OPENSSL: |
| 2203 | nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls); |
| 2204 | free(server_opts.endpts[i].opts.tls); |
| 2205 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2206 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2207 | case NC_TI_UNIX: |
| 2208 | free(server_opts.endpts[i].opts.unixsock); |
| 2209 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2210 | default: |
| 2211 | ERRINT; |
| 2212 | break; |
| 2213 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2214 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2215 | /* remove bind(s) */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2216 | free(server_opts.binds[i].address); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2217 | if (server_opts.binds[i].sock > -1) { |
| 2218 | close(server_opts.binds[i].sock); |
| 2219 | } |
| 2220 | |
| 2221 | /* move last endpt and bind(s) to the empty space */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2222 | --server_opts.endpt_count; |
Michal Vasko | 9ed67a7 | 2016-10-13 15:00:51 +0200 | [diff] [blame] | 2223 | if (!server_opts.endpt_count) { |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 2224 | free(server_opts.binds); |
| 2225 | server_opts.binds = NULL; |
| 2226 | free(server_opts.endpts); |
| 2227 | server_opts.endpts = NULL; |
Michal Vasko | 9ed67a7 | 2016-10-13 15:00:51 +0200 | [diff] [blame] | 2228 | } else if (i < server_opts.endpt_count) { |
| 2229 | memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds); |
| 2230 | memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts); |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 2231 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2232 | |
| 2233 | ret = 0; |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2234 | if (name) { |
| 2235 | break; |
| 2236 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2237 | } |
| 2238 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2239 | } |
| 2240 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2241 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2242 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2243 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2244 | /* BIND UNLOCK */ |
| 2245 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2246 | |
| 2247 | return ret; |
| 2248 | } |
| 2249 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2250 | API int |
| 2251 | nc_server_endpt_count(void) |
| 2252 | { |
| 2253 | return server_opts.endpt_count; |
| 2254 | } |
| 2255 | |
Michal Vasko | 1b5973e | 2020-01-30 16:05:46 +0100 | [diff] [blame] | 2256 | API int |
| 2257 | nc_server_is_endpt(const char *name) |
| 2258 | { |
| 2259 | uint16_t i; |
| 2260 | int found = 0; |
| 2261 | |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2262 | if (!name) { |
| 2263 | return found; |
| 2264 | } |
| 2265 | |
Michal Vasko | 1b5973e | 2020-01-30 16:05:46 +0100 | [diff] [blame] | 2266 | /* ENDPT READ LOCK */ |
| 2267 | pthread_rwlock_rdlock(&server_opts.endpt_lock); |
| 2268 | |
| 2269 | /* check name uniqueness */ |
| 2270 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 2271 | if (!strcmp(server_opts.endpts[i].name, name)) { |
| 2272 | found = 1; |
| 2273 | break; |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | /* ENDPT UNLOCK */ |
| 2278 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2279 | |
| 2280 | return found; |
| 2281 | } |
| 2282 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2283 | int |
| 2284 | nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port) |
| 2285 | { |
| 2286 | struct nc_endpt *endpt; |
| 2287 | struct nc_bind *bind = NULL; |
| 2288 | uint16_t i; |
| 2289 | int sock = -1, set_addr, ret = 0; |
| 2290 | |
| 2291 | if (!endpt_name) { |
| 2292 | ERRARG("endpt_name"); |
| 2293 | return -1; |
| 2294 | } else if ((!address && !port) || (address && port)) { |
| 2295 | ERRARG("address and port"); |
| 2296 | return -1; |
| 2297 | } |
| 2298 | |
| 2299 | if (address) { |
| 2300 | set_addr = 1; |
| 2301 | } else { |
| 2302 | set_addr = 0; |
| 2303 | } |
| 2304 | |
| 2305 | /* BIND LOCK */ |
| 2306 | pthread_mutex_lock(&server_opts.bind_lock); |
| 2307 | |
| 2308 | /* ENDPT LOCK */ |
| 2309 | endpt = nc_server_endpt_lock_get(endpt_name, 0, &i); |
| 2310 | if (!endpt) { |
| 2311 | /* BIND UNLOCK */ |
| 2312 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 2313 | return -1; |
| 2314 | } |
| 2315 | |
| 2316 | bind = &server_opts.binds[i]; |
| 2317 | |
| 2318 | if (set_addr) { |
| 2319 | port = bind->port; |
| 2320 | } else { |
| 2321 | address = bind->address; |
| 2322 | } |
| 2323 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2324 | if (!set_addr && (endpt->ti == NC_TI_UNIX)) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2325 | ret = -1; |
| 2326 | goto cleanup; |
| 2327 | } |
| 2328 | |
| 2329 | /* we have all the information we need to create a listening socket */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2330 | if (address && (port || (endpt->ti == NC_TI_UNIX))) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2331 | /* create new socket, close the old one */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2332 | if (endpt->ti == NC_TI_UNIX) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2333 | sock = nc_sock_listen_unix(address, endpt->opts.unixsock); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2334 | } else { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2335 | sock = nc_sock_listen_inet(address, port, &endpt->ka); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2336 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2337 | if (sock == -1) { |
| 2338 | ret = -1; |
| 2339 | goto cleanup; |
| 2340 | } |
| 2341 | |
| 2342 | if (bind->sock > -1) { |
| 2343 | close(bind->sock); |
| 2344 | } |
| 2345 | bind->sock = sock; |
| 2346 | } /* else we are just setting address or port */ |
| 2347 | |
| 2348 | if (set_addr) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2349 | free(bind->address); |
| 2350 | bind->address = strdup(address); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2351 | } else { |
| 2352 | bind->port = port; |
| 2353 | } |
| 2354 | |
| 2355 | if (sock > -1) { |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2356 | switch (endpt->ti) { |
| 2357 | case NC_TI_UNIX: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2358 | VRB(NULL, "Listening on %s for UNIX connections.", address); |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2359 | break; |
| 2360 | #ifdef NC_ENABLED_SSH |
| 2361 | case NC_TI_LIBSSH: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2362 | VRB(NULL, "Listening on %s:%u for SSH connections.", address, port); |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2363 | break; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2364 | #endif |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2365 | #ifdef NC_ENABLED_TLS |
| 2366 | case NC_TI_OPENSSL: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2367 | VRB(NULL, "Listening on %s:%u for TLS connections.", address, port); |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2368 | break; |
| 2369 | #endif |
| 2370 | default: |
| 2371 | ERRINT; |
| 2372 | break; |
| 2373 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | cleanup: |
| 2377 | /* ENDPT UNLOCK */ |
| 2378 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2379 | |
| 2380 | /* BIND UNLOCK */ |
| 2381 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 2382 | |
| 2383 | return ret; |
| 2384 | } |
| 2385 | |
| 2386 | API int |
| 2387 | nc_server_endpt_set_address(const char *endpt_name, const char *address) |
| 2388 | { |
| 2389 | return nc_server_endpt_set_address_port(endpt_name, address, 0); |
| 2390 | } |
| 2391 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2392 | #if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS) |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2393 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2394 | API int |
| 2395 | nc_server_endpt_set_port(const char *endpt_name, uint16_t port) |
| 2396 | { |
| 2397 | return nc_server_endpt_set_address_port(endpt_name, NULL, port); |
| 2398 | } |
| 2399 | |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2400 | #endif |
| 2401 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2402 | API int |
| 2403 | nc_server_endpt_set_perms(const char *endpt_name, mode_t mode, uid_t uid, gid_t gid) |
| 2404 | { |
| 2405 | struct nc_endpt *endpt; |
| 2406 | uint16_t i; |
| 2407 | int ret = 0; |
| 2408 | |
| 2409 | if (!endpt_name) { |
| 2410 | ERRARG("endpt_name"); |
| 2411 | return -1; |
| 2412 | } else if (mode == 0) { |
| 2413 | ERRARG("mode"); |
| 2414 | return -1; |
| 2415 | } |
| 2416 | |
| 2417 | /* ENDPT LOCK */ |
| 2418 | endpt = nc_server_endpt_lock_get(endpt_name, 0, &i); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2419 | if (!endpt) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2420 | return -1; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2421 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2422 | |
| 2423 | if (endpt->ti != NC_TI_UNIX) { |
| 2424 | ret = -1; |
| 2425 | goto cleanup; |
| 2426 | } |
| 2427 | |
| 2428 | endpt->opts.unixsock->mode = mode; |
| 2429 | endpt->opts.unixsock->uid = uid; |
| 2430 | endpt->opts.unixsock->gid = gid; |
| 2431 | |
| 2432 | cleanup: |
| 2433 | /* ENDPT UNLOCK */ |
| 2434 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2435 | |
| 2436 | return ret; |
| 2437 | } |
| 2438 | |
| 2439 | API int |
| 2440 | nc_server_endpt_enable_keepalives(const char *endpt_name, int enable) |
| 2441 | { |
| 2442 | struct nc_endpt *endpt; |
| 2443 | int ret = 0; |
| 2444 | |
| 2445 | if (!endpt_name) { |
| 2446 | ERRARG("endpt_name"); |
| 2447 | return -1; |
| 2448 | } |
| 2449 | |
| 2450 | /* ENDPT LOCK */ |
| 2451 | endpt = nc_server_endpt_lock_get(endpt_name, 0, NULL); |
| 2452 | if (!endpt) { |
| 2453 | return -1; |
| 2454 | } |
| 2455 | |
| 2456 | endpt->ka.enabled = (enable ? 1 : 0); |
| 2457 | |
| 2458 | /* ENDPT UNLOCK */ |
| 2459 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2460 | |
| 2461 | return ret; |
| 2462 | } |
| 2463 | |
| 2464 | API int |
| 2465 | nc_server_endpt_set_keepalives(const char *endpt_name, int idle_time, int max_probes, int probe_interval) |
| 2466 | { |
| 2467 | struct nc_endpt *endpt; |
| 2468 | int ret = 0; |
| 2469 | |
| 2470 | if (!endpt_name) { |
| 2471 | ERRARG("endpt_name"); |
| 2472 | return -1; |
| 2473 | } |
| 2474 | |
| 2475 | /* ENDPT LOCK */ |
| 2476 | endpt = nc_server_endpt_lock_get(endpt_name, 0, NULL); |
| 2477 | if (!endpt) { |
| 2478 | return -1; |
| 2479 | } |
| 2480 | |
| 2481 | if (idle_time > -1) { |
| 2482 | endpt->ka.idle_time = idle_time; |
| 2483 | } |
| 2484 | if (max_probes > -1) { |
| 2485 | endpt->ka.max_probes = max_probes; |
| 2486 | } |
| 2487 | if (probe_interval > -1) { |
| 2488 | endpt->ka.probe_interval = probe_interval; |
| 2489 | } |
| 2490 | |
| 2491 | /* ENDPT UNLOCK */ |
| 2492 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2493 | |
| 2494 | return ret; |
| 2495 | } |
| 2496 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2497 | API NC_MSG_TYPE |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2498 | nc_accept(int timeout, const struct ly_ctx *ctx, struct nc_session **session) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2499 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2500 | NC_MSG_TYPE msgtype; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2501 | int sock, ret; |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 2502 | char *host = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2503 | uint16_t port, bind_idx; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2504 | struct timespec ts_cur; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2505 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2506 | if (!ctx) { |
| 2507 | ERRARG("ctx"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2508 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2509 | } else if (!session) { |
| 2510 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2511 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2512 | } |
| 2513 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2514 | /* init ctx as needed */ |
| 2515 | nc_server_init_ctx(ctx); |
| 2516 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2517 | /* BIND LOCK */ |
| 2518 | pthread_mutex_lock(&server_opts.bind_lock); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 2519 | |
| 2520 | if (!server_opts.endpt_count) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2521 | ERR(NULL, "No endpoints to accept sessions on."); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2522 | /* BIND UNLOCK */ |
| 2523 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2524 | return NC_MSG_ERROR; |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 2525 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2526 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2527 | ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &bind_idx); |
Michal Vasko | 50456e8 | 2016-02-02 12:16:08 +0100 | [diff] [blame] | 2528 | if (ret < 1) { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2529 | /* BIND UNLOCK */ |
| 2530 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | b737d75 | 2016-02-09 09:01:27 +0100 | [diff] [blame] | 2531 | free(host); |
Michal Vasko | 5e20347 | 2016-05-30 15:27:58 +0200 | [diff] [blame] | 2532 | if (!ret) { |
| 2533 | return NC_MSG_WOULDBLOCK; |
| 2534 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2535 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2536 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2537 | |
| 2538 | /* switch bind_lock for endpt_lock, so that another thread can accept another session */ |
| 2539 | /* ENDPT READ LOCK */ |
| 2540 | pthread_rwlock_rdlock(&server_opts.endpt_lock); |
| 2541 | |
| 2542 | /* BIND UNLOCK */ |
| 2543 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 2544 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2545 | sock = ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2546 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2547 | *session = nc_new_session(NC_SERVER, 0); |
Michal Vasko | 686aa31 | 2016-01-21 15:58:18 +0100 | [diff] [blame] | 2548 | if (!(*session)) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2549 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2550 | close(sock); |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 2551 | free(host); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2552 | msgtype = NC_MSG_ERROR; |
| 2553 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2554 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2555 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2556 | (*session)->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2557 | (*session)->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2558 | (*session)->host = host; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2559 | (*session)->port = port; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2560 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2561 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2562 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2563 | if (server_opts.endpts[bind_idx].ti == NC_TI_LIBSSH) { |
| 2564 | (*session)->data = server_opts.endpts[bind_idx].opts.ssh; |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 2565 | ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2566 | if (ret < 0) { |
| 2567 | msgtype = NC_MSG_ERROR; |
| 2568 | goto cleanup; |
| 2569 | } else if (!ret) { |
| 2570 | msgtype = NC_MSG_WOULDBLOCK; |
| 2571 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2572 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2573 | } else |
| 2574 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2575 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2576 | if (server_opts.endpts[bind_idx].ti == NC_TI_OPENSSL) { |
| 2577 | (*session)->data = server_opts.endpts[bind_idx].opts.tls; |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 2578 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2579 | if (ret < 0) { |
| 2580 | msgtype = NC_MSG_ERROR; |
| 2581 | goto cleanup; |
| 2582 | } else if (!ret) { |
| 2583 | msgtype = NC_MSG_WOULDBLOCK; |
| 2584 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2585 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2586 | } else |
| 2587 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2588 | if (server_opts.endpts[bind_idx].ti == NC_TI_UNIX) { |
| 2589 | (*session)->data = server_opts.endpts[bind_idx].opts.unixsock; |
| 2590 | ret = nc_accept_unix(*session, sock); |
| 2591 | if (ret < 0) { |
| 2592 | msgtype = NC_MSG_ERROR; |
| 2593 | goto cleanup; |
| 2594 | } |
| 2595 | } else { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2596 | ERRINT; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2597 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2598 | msgtype = NC_MSG_ERROR; |
| 2599 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2600 | } |
| 2601 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 2602 | (*session)->data = NULL; |
| 2603 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2604 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2605 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2606 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2607 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2608 | (*session)->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2609 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2610 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2611 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2612 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2613 | nc_session_free(*session, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2614 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2615 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2616 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2617 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 2618 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2619 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 2620 | nc_realtime_get(&ts_cur); |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2621 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2622 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2623 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2624 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2625 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2626 | cleanup: |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2627 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2628 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2629 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2630 | nc_session_free(*session, NULL); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2631 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2632 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2633 | } |
| 2634 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2635 | #if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS) |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2636 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2637 | /* client is expected to be locked */ |
| 2638 | static int |
| 2639 | _nc_server_ch_client_del_endpt(struct nc_ch_client *client, const char *endpt_name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2640 | { |
| 2641 | uint16_t i; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2642 | int ret = -1; |
| 2643 | |
| 2644 | if (!endpt_name) { |
| 2645 | /* remove all endpoints */ |
| 2646 | for (i = 0; i < client->ch_endpt_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2647 | free(client->ch_endpts[i].name); |
| 2648 | free(client->ch_endpts[i].address); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2649 | if (client->ch_endpts[i].sock_pending != -1) { |
| 2650 | close(client->ch_endpts[i].sock_pending); |
| 2651 | } |
| 2652 | switch (client->ch_endpts[i].ti) { |
| 2653 | #ifdef NC_ENABLED_SSH |
| 2654 | case NC_TI_LIBSSH: |
| 2655 | nc_server_ssh_clear_opts(client->ch_endpts[i].opts.ssh); |
| 2656 | free(client->ch_endpts[i].opts.ssh); |
| 2657 | break; |
| 2658 | #endif |
| 2659 | #ifdef NC_ENABLED_TLS |
| 2660 | case NC_TI_OPENSSL: |
| 2661 | nc_server_tls_clear_opts(client->ch_endpts[i].opts.tls); |
| 2662 | free(client->ch_endpts[i].opts.tls); |
| 2663 | break; |
| 2664 | #endif |
| 2665 | default: |
| 2666 | ERRINT; |
| 2667 | /* won't get here ...*/ |
| 2668 | break; |
| 2669 | } |
| 2670 | } |
| 2671 | free(client->ch_endpts); |
| 2672 | client->ch_endpts = NULL; |
| 2673 | client->ch_endpt_count = 0; |
| 2674 | |
| 2675 | ret = 0; |
| 2676 | } else { |
| 2677 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2678 | if (!strcmp(client->ch_endpts[i].name, endpt_name) && (!ti || (ti == client->ch_endpts[i].ti))) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2679 | free(client->ch_endpts[i].name); |
| 2680 | free(client->ch_endpts[i].address); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2681 | if (client->ch_endpts[i].sock_pending != -1) { |
| 2682 | close(client->ch_endpts[i].sock_pending); |
| 2683 | } |
| 2684 | switch (client->ch_endpts[i].ti) { |
| 2685 | #ifdef NC_ENABLED_SSH |
| 2686 | case NC_TI_LIBSSH: |
| 2687 | nc_server_ssh_clear_opts(client->ch_endpts[i].opts.ssh); |
| 2688 | free(client->ch_endpts[i].opts.ssh); |
| 2689 | break; |
| 2690 | #endif |
| 2691 | #ifdef NC_ENABLED_TLS |
| 2692 | case NC_TI_OPENSSL: |
| 2693 | nc_server_tls_clear_opts(client->ch_endpts[i].opts.tls); |
| 2694 | free(client->ch_endpts[i].opts.tls); |
| 2695 | break; |
| 2696 | #endif |
| 2697 | default: |
| 2698 | ERRINT; |
| 2699 | /* won't get here ...*/ |
| 2700 | break; |
| 2701 | } |
| 2702 | |
| 2703 | /* move last endpoint to the empty space */ |
| 2704 | --client->ch_endpt_count; |
| 2705 | if (i < client->ch_endpt_count) { |
| 2706 | memcpy(&client->ch_endpts[i], &client->ch_endpts[client->ch_endpt_count], sizeof *client->ch_endpts); |
| 2707 | } else if (!server_opts.ch_client_count) { |
| 2708 | free(server_opts.ch_clients); |
| 2709 | server_opts.ch_clients = NULL; |
| 2710 | } |
| 2711 | |
| 2712 | ret = 0; |
| 2713 | break; |
| 2714 | } |
| 2715 | } |
| 2716 | } |
| 2717 | |
| 2718 | return ret; |
| 2719 | } |
| 2720 | |
| 2721 | API int |
| 2722 | nc_server_ch_add_client(const char *name) |
| 2723 | { |
| 2724 | uint16_t i; |
| 2725 | struct nc_ch_client *client; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2726 | |
| 2727 | if (!name) { |
| 2728 | ERRARG("name"); |
| 2729 | return -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | /* WRITE LOCK */ |
| 2733 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 2734 | |
| 2735 | /* check name uniqueness */ |
| 2736 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2737 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2738 | ERR(NULL, "Call Home client \"%s\" already exists.", name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2739 | /* WRITE UNLOCK */ |
| 2740 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2741 | return -1; |
| 2742 | } |
| 2743 | } |
| 2744 | |
| 2745 | ++server_opts.ch_client_count; |
| 2746 | server_opts.ch_clients = nc_realloc(server_opts.ch_clients, server_opts.ch_client_count * sizeof *server_opts.ch_clients); |
| 2747 | if (!server_opts.ch_clients) { |
| 2748 | ERRMEM; |
| 2749 | /* WRITE UNLOCK */ |
| 2750 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2751 | return -1; |
| 2752 | } |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2753 | client = &server_opts.ch_clients[server_opts.ch_client_count - 1]; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2754 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2755 | client->name = strdup(name); |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2756 | client->id = ATOMIC_INC_RELAXED(server_opts.new_client_id); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2757 | client->ch_endpts = NULL; |
| 2758 | client->ch_endpt_count = 0; |
| 2759 | client->conn_type = 0; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2760 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2761 | /* set CH default options */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2762 | client->start_with = NC_CH_FIRST_LISTED; |
| 2763 | client->max_attempts = 3; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2764 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2765 | pthread_mutex_init(&client->lock, NULL); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2766 | |
| 2767 | /* WRITE UNLOCK */ |
| 2768 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2769 | |
| 2770 | return 0; |
| 2771 | } |
| 2772 | |
| 2773 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2774 | nc_server_ch_del_client(const char *name) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2775 | { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2776 | uint16_t i; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2777 | int ret = -1; |
| 2778 | |
| 2779 | /* WRITE LOCK */ |
| 2780 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 2781 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2782 | if (!name) { |
| 2783 | /* remove all CH clients with endpoints */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2784 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2785 | free(server_opts.ch_clients[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2786 | |
| 2787 | /* remove all endpoints */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2788 | _nc_server_ch_client_del_endpt(&server_opts.ch_clients[i], NULL, 0); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2789 | |
| 2790 | pthread_mutex_destroy(&server_opts.ch_clients[i].lock); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2791 | ret = 0; |
| 2792 | } |
| 2793 | free(server_opts.ch_clients); |
| 2794 | server_opts.ch_clients = NULL; |
| 2795 | |
| 2796 | server_opts.ch_client_count = 0; |
| 2797 | |
| 2798 | } else { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2799 | /* remove one client with endpoints */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2800 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2801 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2802 | free(server_opts.ch_clients[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2803 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2804 | /* remove all endpoints */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2805 | _nc_server_ch_client_del_endpt(&server_opts.ch_clients[i], NULL, 0); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2806 | |
| 2807 | pthread_mutex_destroy(&server_opts.ch_clients[i].lock); |
| 2808 | |
| 2809 | /* move last client and endpoint(s) to the empty space */ |
| 2810 | --server_opts.ch_client_count; |
| 2811 | if (i < server_opts.ch_client_count) { |
| 2812 | memcpy(&server_opts.ch_clients[i], &server_opts.ch_clients[server_opts.ch_client_count], |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2813 | sizeof *server_opts.ch_clients); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2814 | } else if (!server_opts.ch_client_count) { |
| 2815 | free(server_opts.ch_clients); |
| 2816 | server_opts.ch_clients = NULL; |
| 2817 | } |
| 2818 | |
| 2819 | ret = 0; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2820 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2821 | } |
| 2822 | } |
| 2823 | } |
| 2824 | |
| 2825 | /* WRITE UNLOCK */ |
| 2826 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2827 | |
| 2828 | return ret; |
| 2829 | } |
| 2830 | |
| 2831 | API int |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2832 | nc_server_ch_is_client(const char *name) |
| 2833 | { |
| 2834 | uint16_t i; |
| 2835 | int found = 0; |
| 2836 | |
| 2837 | if (!name) { |
| 2838 | return found; |
| 2839 | } |
| 2840 | |
| 2841 | /* READ LOCK */ |
| 2842 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 2843 | |
| 2844 | /* check name uniqueness */ |
| 2845 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2846 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
| 2847 | found = 1; |
| 2848 | break; |
| 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | /* UNLOCK */ |
| 2853 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2854 | |
| 2855 | return found; |
| 2856 | } |
| 2857 | |
| 2858 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2859 | nc_server_ch_client_add_endpt(const char *client_name, const char *endpt_name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2860 | { |
| 2861 | uint16_t i; |
| 2862 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2863 | struct nc_ch_endpt *endpt; |
| 2864 | int ret = -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2865 | |
| 2866 | if (!client_name) { |
| 2867 | ERRARG("client_name"); |
| 2868 | return -1; |
| 2869 | } else if (!endpt_name) { |
| 2870 | ERRARG("endpt_name"); |
| 2871 | return -1; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2872 | } else if (!ti) { |
| 2873 | ERRARG("ti"); |
| 2874 | return -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2878 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2879 | if (!client) { |
| 2880 | return -1; |
| 2881 | } |
| 2882 | |
| 2883 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2884 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2885 | ERR(NULL, "Call Home client \"%s\" endpoint \"%s\" already exists.", client_name, endpt_name); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2886 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | |
| 2890 | ++client->ch_endpt_count; |
| 2891 | client->ch_endpts = realloc(client->ch_endpts, client->ch_endpt_count * sizeof *client->ch_endpts); |
| 2892 | if (!client->ch_endpts) { |
| 2893 | ERRMEM; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2894 | goto cleanup; |
| 2895 | } |
| 2896 | endpt = &client->ch_endpts[client->ch_endpt_count - 1]; |
| 2897 | |
| 2898 | memset(endpt, 0, sizeof *client->ch_endpts); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2899 | endpt->name = strdup(endpt_name); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2900 | endpt->ti = ti; |
| 2901 | endpt->sock_pending = -1; |
| 2902 | endpt->ka.idle_time = 1; |
| 2903 | endpt->ka.max_probes = 10; |
| 2904 | endpt->ka.probe_interval = 5; |
| 2905 | |
| 2906 | switch (ti) { |
| 2907 | #ifdef NC_ENABLED_SSH |
| 2908 | case NC_TI_LIBSSH: |
| 2909 | endpt->opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 2910 | if (!endpt->opts.ssh) { |
| 2911 | ERRMEM; |
| 2912 | goto cleanup; |
| 2913 | } |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 2914 | endpt->opts.ssh->auth_methods = NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2915 | endpt->opts.ssh->auth_attempts = 3; |
Michal Vasko | cbad4c5 | 2019-06-27 16:30:35 +0200 | [diff] [blame] | 2916 | endpt->opts.ssh->auth_timeout = 30; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2917 | break; |
| 2918 | #endif |
| 2919 | #ifdef NC_ENABLED_TLS |
| 2920 | case NC_TI_OPENSSL: |
| 2921 | endpt->opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 2922 | if (!endpt->opts.tls) { |
| 2923 | ERRMEM; |
| 2924 | goto cleanup; |
| 2925 | } |
| 2926 | break; |
| 2927 | #endif |
| 2928 | default: |
| 2929 | ERRINT; |
| 2930 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2931 | } |
| 2932 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2933 | /* success */ |
| 2934 | ret = 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2935 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2936 | cleanup: |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2937 | /* UNLOCK */ |
| 2938 | nc_server_ch_client_unlock(client); |
| 2939 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2940 | return ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2944 | nc_server_ch_client_del_endpt(const char *client_name, const char *endpt_name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2945 | { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2946 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2947 | struct nc_ch_client *client; |
| 2948 | |
| 2949 | if (!client_name) { |
| 2950 | ERRARG("client_name"); |
| 2951 | return -1; |
| 2952 | } |
| 2953 | |
| 2954 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2955 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2956 | if (!client) { |
| 2957 | return -1; |
| 2958 | } |
| 2959 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2960 | ret = _nc_server_ch_client_del_endpt(client, endpt_name, ti); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2961 | |
| 2962 | /* UNLOCK */ |
| 2963 | nc_server_ch_client_unlock(client); |
| 2964 | |
| 2965 | return ret; |
| 2966 | } |
| 2967 | |
| 2968 | API int |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2969 | nc_server_ch_client_is_endpt(const char *client_name, const char *endpt_name) |
| 2970 | { |
| 2971 | uint16_t i; |
| 2972 | struct nc_ch_client *client = NULL; |
| 2973 | int found = 0; |
| 2974 | |
| 2975 | if (!client_name || !endpt_name) { |
| 2976 | return found; |
| 2977 | } |
| 2978 | |
| 2979 | /* READ LOCK */ |
| 2980 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 2981 | |
| 2982 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2983 | if (!strcmp(server_opts.ch_clients[i].name, client_name)) { |
| 2984 | client = &server_opts.ch_clients[i]; |
| 2985 | break; |
| 2986 | } |
| 2987 | } |
| 2988 | |
| 2989 | if (!client) { |
| 2990 | goto cleanup; |
| 2991 | } |
| 2992 | |
| 2993 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2994 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2995 | found = 1; |
| 2996 | break; |
| 2997 | } |
| 2998 | } |
| 2999 | |
| 3000 | cleanup: |
| 3001 | /* UNLOCK */ |
| 3002 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 3003 | return found; |
| 3004 | } |
| 3005 | |
| 3006 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3007 | nc_server_ch_client_endpt_set_address(const char *client_name, const char *endpt_name, const char *address) |
| 3008 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3009 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3010 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3011 | |
| 3012 | if (!client_name) { |
| 3013 | ERRARG("client_name"); |
| 3014 | return -1; |
| 3015 | } else if (!endpt_name) { |
| 3016 | ERRARG("endpt_name"); |
| 3017 | return -1; |
| 3018 | } else if (!address) { |
| 3019 | ERRARG("address"); |
| 3020 | return -1; |
| 3021 | } |
| 3022 | |
| 3023 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3024 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 3025 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3026 | return -1; |
| 3027 | } |
| 3028 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3029 | free(endpt->address); |
| 3030 | endpt->address = strdup(address); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3031 | |
| 3032 | /* UNLOCK */ |
| 3033 | nc_server_ch_client_unlock(client); |
| 3034 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3035 | return 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | API int |
| 3039 | nc_server_ch_client_endpt_set_port(const char *client_name, const char *endpt_name, uint16_t port) |
| 3040 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3041 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3042 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3043 | |
| 3044 | if (!client_name) { |
| 3045 | ERRARG("client_name"); |
| 3046 | return -1; |
| 3047 | } else if (!endpt_name) { |
| 3048 | ERRARG("endpt_name"); |
| 3049 | return -1; |
| 3050 | } else if (!port) { |
| 3051 | ERRARG("port"); |
| 3052 | return -1; |
| 3053 | } |
| 3054 | |
| 3055 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3056 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 3057 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3058 | return -1; |
| 3059 | } |
| 3060 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3061 | endpt->port = port; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3062 | |
| 3063 | /* UNLOCK */ |
| 3064 | nc_server_ch_client_unlock(client); |
| 3065 | |
ravsz | 5c5a442 | 2020-03-31 15:53:21 +0200 | [diff] [blame] | 3066 | return 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3067 | } |
| 3068 | |
| 3069 | API int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3070 | nc_server_ch_client_endpt_enable_keepalives(const char *client_name, const char *endpt_name, int enable) |
| 3071 | { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3072 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3073 | struct nc_ch_endpt *endpt; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3074 | |
| 3075 | if (!client_name) { |
| 3076 | ERRARG("client_name"); |
| 3077 | return -1; |
| 3078 | } else if (!endpt_name) { |
| 3079 | ERRARG("endpt_name"); |
| 3080 | return -1; |
| 3081 | } |
| 3082 | |
| 3083 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3084 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 3085 | if (!endpt) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3086 | return -1; |
| 3087 | } |
| 3088 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3089 | endpt->ka.enabled = (enable ? 1 : 0); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3090 | |
| 3091 | /* UNLOCK */ |
| 3092 | nc_server_ch_client_unlock(client); |
| 3093 | |
Michal Vasko | 9af829a | 2019-09-12 13:50:00 +0200 | [diff] [blame] | 3094 | return 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3095 | } |
| 3096 | |
| 3097 | API int |
| 3098 | nc_server_ch_client_endpt_set_keepalives(const char *client_name, const char *endpt_name, int idle_time, int max_probes, |
| 3099 | int probe_interval) |
| 3100 | { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3101 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3102 | struct nc_ch_endpt *endpt; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3103 | |
| 3104 | if (!client_name) { |
| 3105 | ERRARG("client_name"); |
| 3106 | return -1; |
| 3107 | } else if (!endpt_name) { |
| 3108 | ERRARG("endpt_name"); |
| 3109 | return -1; |
| 3110 | } |
| 3111 | |
| 3112 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3113 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 3114 | if (!endpt) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3115 | return -1; |
| 3116 | } |
| 3117 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3118 | if (idle_time > -1) { |
| 3119 | endpt->ka.idle_time = idle_time; |
| 3120 | } |
| 3121 | if (max_probes > -1) { |
| 3122 | endpt->ka.max_probes = max_probes; |
| 3123 | } |
| 3124 | if (probe_interval > -1) { |
| 3125 | endpt->ka.probe_interval = probe_interval; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | /* UNLOCK */ |
| 3129 | nc_server_ch_client_unlock(client); |
| 3130 | |
Michal Vasko | 9af829a | 2019-09-12 13:50:00 +0200 | [diff] [blame] | 3131 | return 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3132 | } |
| 3133 | |
| 3134 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3135 | nc_server_ch_client_set_conn_type(const char *client_name, NC_CH_CONN_TYPE conn_type) |
| 3136 | { |
| 3137 | struct nc_ch_client *client; |
| 3138 | |
| 3139 | if (!client_name) { |
| 3140 | ERRARG("client_name"); |
| 3141 | return -1; |
| 3142 | } else if (!conn_type) { |
| 3143 | ERRARG("conn_type"); |
| 3144 | return -1; |
| 3145 | } |
| 3146 | |
| 3147 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3148 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3149 | if (!client) { |
| 3150 | return -1; |
| 3151 | } |
| 3152 | |
| 3153 | if (client->conn_type != conn_type) { |
| 3154 | client->conn_type = conn_type; |
| 3155 | |
| 3156 | /* set default options */ |
| 3157 | switch (conn_type) { |
| 3158 | case NC_CH_PERSIST: |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3159 | /* no options */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3160 | break; |
| 3161 | case NC_CH_PERIOD: |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3162 | client->conn.period.period = 60; |
| 3163 | client->conn.period.anchor_time = 0; |
| 3164 | client->conn.period.idle_timeout = 120; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3165 | break; |
| 3166 | default: |
| 3167 | ERRINT; |
| 3168 | break; |
| 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | /* UNLOCK */ |
| 3173 | nc_server_ch_client_unlock(client); |
| 3174 | |
| 3175 | return 0; |
| 3176 | } |
| 3177 | |
| 3178 | API int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3179 | nc_server_ch_client_periodic_set_period(const char *client_name, uint16_t period) |
| 3180 | { |
| 3181 | struct nc_ch_client *client; |
| 3182 | |
| 3183 | if (!client_name) { |
| 3184 | ERRARG("client_name"); |
| 3185 | return -1; |
| 3186 | } else if (!period) { |
| 3187 | ERRARG("period"); |
| 3188 | return -1; |
| 3189 | } |
| 3190 | |
| 3191 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3192 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3193 | if (!client) { |
| 3194 | return -1; |
| 3195 | } |
| 3196 | |
| 3197 | if (client->conn_type != NC_CH_PERIOD) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3198 | ERR(NULL, "Call Home client \"%s\" is not of periodic connection type.", client_name); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3199 | /* UNLOCK */ |
| 3200 | nc_server_ch_client_unlock(client); |
| 3201 | return -1; |
| 3202 | } |
| 3203 | |
| 3204 | client->conn.period.period = period; |
| 3205 | |
| 3206 | /* UNLOCK */ |
| 3207 | nc_server_ch_client_unlock(client); |
| 3208 | |
| 3209 | return 0; |
| 3210 | } |
| 3211 | |
| 3212 | API int |
| 3213 | nc_server_ch_client_periodic_set_anchor_time(const char *client_name, time_t anchor_time) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3214 | { |
| 3215 | struct nc_ch_client *client; |
| 3216 | |
| 3217 | if (!client_name) { |
| 3218 | ERRARG("client_name"); |
| 3219 | return -1; |
| 3220 | } |
| 3221 | |
| 3222 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3223 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3224 | if (!client) { |
| 3225 | return -1; |
| 3226 | } |
| 3227 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3228 | if (client->conn_type != NC_CH_PERIOD) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3229 | ERR(NULL, "Call Home client \"%s\" is not of periodic connection type.", client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3230 | /* UNLOCK */ |
| 3231 | nc_server_ch_client_unlock(client); |
| 3232 | return -1; |
| 3233 | } |
| 3234 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3235 | client->conn.period.anchor_time = anchor_time; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3236 | |
| 3237 | /* UNLOCK */ |
| 3238 | nc_server_ch_client_unlock(client); |
| 3239 | |
| 3240 | return 0; |
| 3241 | } |
| 3242 | |
| 3243 | API int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3244 | nc_server_ch_client_periodic_set_idle_timeout(const char *client_name, uint16_t idle_timeout) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3245 | { |
| 3246 | struct nc_ch_client *client; |
| 3247 | |
| 3248 | if (!client_name) { |
| 3249 | ERRARG("client_name"); |
| 3250 | return -1; |
| 3251 | } |
| 3252 | |
| 3253 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3254 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3255 | if (!client) { |
| 3256 | return -1; |
| 3257 | } |
| 3258 | |
| 3259 | if (client->conn_type != NC_CH_PERIOD) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3260 | ERR(NULL, "Call Home client \"%s\" is not of periodic connection type.", client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3261 | /* UNLOCK */ |
| 3262 | nc_server_ch_client_unlock(client); |
| 3263 | return -1; |
| 3264 | } |
| 3265 | |
| 3266 | client->conn.period.idle_timeout = idle_timeout; |
| 3267 | |
| 3268 | /* UNLOCK */ |
| 3269 | nc_server_ch_client_unlock(client); |
| 3270 | |
| 3271 | return 0; |
| 3272 | } |
| 3273 | |
| 3274 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3275 | nc_server_ch_client_set_start_with(const char *client_name, NC_CH_START_WITH start_with) |
| 3276 | { |
| 3277 | struct nc_ch_client *client; |
| 3278 | |
| 3279 | if (!client_name) { |
| 3280 | ERRARG("client_name"); |
| 3281 | return -1; |
| 3282 | } |
| 3283 | |
| 3284 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3285 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3286 | if (!client) { |
| 3287 | return -1; |
| 3288 | } |
| 3289 | |
| 3290 | client->start_with = start_with; |
| 3291 | |
| 3292 | /* UNLOCK */ |
| 3293 | nc_server_ch_client_unlock(client); |
| 3294 | |
| 3295 | return 0; |
| 3296 | } |
| 3297 | |
| 3298 | API int |
| 3299 | nc_server_ch_client_set_max_attempts(const char *client_name, uint8_t max_attempts) |
| 3300 | { |
| 3301 | struct nc_ch_client *client; |
| 3302 | |
| 3303 | if (!client_name) { |
| 3304 | ERRARG("client_name"); |
| 3305 | return -1; |
| 3306 | } else if (!max_attempts) { |
| 3307 | ERRARG("max_attempts"); |
| 3308 | return -1; |
| 3309 | } |
| 3310 | |
| 3311 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3312 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3313 | if (!client) { |
| 3314 | return -1; |
| 3315 | } |
| 3316 | |
| 3317 | client->max_attempts = max_attempts; |
| 3318 | |
| 3319 | /* UNLOCK */ |
| 3320 | nc_server_ch_client_unlock(client); |
| 3321 | |
| 3322 | return 0; |
| 3323 | } |
| 3324 | |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 3325 | /** |
| 3326 | * @brief Create a connection for an endpoint. |
| 3327 | * |
| 3328 | * Client lock is expected to be held. |
| 3329 | * |
| 3330 | * @param[in] endpt Endpoint to use. |
| 3331 | * @param[in] acquire_ctx_cb Callback for acquiring the libyang context. |
| 3332 | * @param[in] release_ctx_cb Callback for releasing the libyang context. |
| 3333 | * @param[in] ctx_cb_data Context callbacks data. |
| 3334 | * @param[out] session Created NC session. |
| 3335 | * @return NC_MSG values. |
| 3336 | */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3337 | static NC_MSG_TYPE |
Michal Vasko | 58bac1c | 2022-03-24 15:25:26 +0100 | [diff] [blame] | 3338 | nc_connect_ch_endpt(struct nc_ch_endpt *endpt, nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb, |
| 3339 | nc_server_ch_session_release_ctx_cb release_ctx_cb, void *ctx_cb_data, struct nc_session **session) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3340 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3341 | NC_MSG_TYPE msgtype; |
Michal Vasko | 58bac1c | 2022-03-24 15:25:26 +0100 | [diff] [blame] | 3342 | const struct ly_ctx *ctx = NULL; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3343 | int sock, ret; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3344 | struct timespec ts_cur; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 3345 | char *ip_host; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3346 | |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 3347 | sock = nc_sock_connect(endpt->address, endpt->port, NC_CH_CONNECT_TIMEOUT, &endpt->ka, &endpt->sock_pending, &ip_host); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 3348 | if (sock < 0) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3349 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3350 | } |
| 3351 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3352 | /* acquire context */ |
| 3353 | ctx = acquire_ctx_cb(ctx_cb_data); |
| 3354 | if (!ctx) { |
| 3355 | ERR(NULL, "Failed to acquire context for a new Call Home session."); |
| 3356 | close(sock); |
| 3357 | free(ip_host); |
| 3358 | return NC_MSG_ERROR; |
| 3359 | } |
| 3360 | |
| 3361 | /* create session */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3362 | *session = nc_new_session(NC_SERVER, 0); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3363 | if (!(*session)) { |
| 3364 | ERRMEM; |
| 3365 | close(sock); |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 3366 | free(ip_host); |
Michal Vasko | d530d36 | 2022-09-07 10:07:57 +0200 | [diff] [blame] | 3367 | msgtype = NC_MSG_ERROR; |
| 3368 | goto fail; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3369 | } |
| 3370 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3371 | (*session)->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | dc96bb9 | 2023-03-28 08:52:48 +0200 | [diff] [blame] | 3372 | (*session)->flags = NC_SESSION_SHAREDCTX | NC_SESSION_CALLHOME; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3373 | (*session)->host = ip_host; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3374 | (*session)->port = endpt->port; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3375 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3376 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3377 | #ifdef NC_ENABLED_SSH |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3378 | if (endpt->ti == NC_TI_LIBSSH) { |
| 3379 | (*session)->data = endpt->opts.ssh; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 3380 | ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 3381 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 3382 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3383 | if (ret < 0) { |
| 3384 | msgtype = NC_MSG_ERROR; |
| 3385 | goto fail; |
| 3386 | } else if (!ret) { |
| 3387 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3388 | goto fail; |
| 3389 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 3390 | } else |
| 3391 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3392 | #ifdef NC_ENABLED_TLS |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3393 | if (endpt->ti == NC_TI_OPENSSL) { |
| 3394 | (*session)->data = endpt->opts.tls; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 3395 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 3396 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 3397 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3398 | if (ret < 0) { |
| 3399 | msgtype = NC_MSG_ERROR; |
| 3400 | goto fail; |
| 3401 | } else if (!ret) { |
| 3402 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3403 | goto fail; |
| 3404 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 3405 | } else |
| 3406 | #endif |
| 3407 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3408 | ERRINT; |
| 3409 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3410 | msgtype = NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3411 | goto fail; |
| 3412 | } |
| 3413 | |
| 3414 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 3415 | (*session)->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3416 | |
| 3417 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3418 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3419 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3420 | goto fail; |
| 3421 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3422 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 3423 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3424 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 3425 | nc_realtime_get(&ts_cur); |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3426 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3427 | (*session)->status = NC_STATUS_RUNNING; |
| 3428 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3429 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3430 | |
| 3431 | fail: |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 3432 | nc_session_free(*session, NULL); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3433 | *session = NULL; |
Michal Vasko | 58bac1c | 2022-03-24 15:25:26 +0100 | [diff] [blame] | 3434 | if (ctx) { |
| 3435 | release_ctx_cb(ctx_cb_data); |
| 3436 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3437 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3438 | } |
| 3439 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3440 | struct nc_ch_client_thread_arg { |
| 3441 | char *client_name; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3442 | nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb; |
| 3443 | nc_server_ch_session_release_ctx_cb release_ctx_cb; |
| 3444 | void *ctx_cb_data; |
| 3445 | nc_server_ch_new_session_cb new_session_cb; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3446 | }; |
| 3447 | |
| 3448 | static struct nc_ch_client * |
| 3449 | nc_server_ch_client_with_endpt_lock(const char *name) |
| 3450 | { |
| 3451 | struct nc_ch_client *client; |
| 3452 | |
| 3453 | while (1) { |
| 3454 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3455 | nc_server_ch_client_lock(name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3456 | if (!client) { |
| 3457 | return NULL; |
| 3458 | } |
| 3459 | if (client->ch_endpt_count) { |
| 3460 | return client; |
| 3461 | } |
| 3462 | /* no endpoints defined yet */ |
| 3463 | |
| 3464 | /* UNLOCK */ |
| 3465 | nc_server_ch_client_unlock(client); |
| 3466 | |
| 3467 | usleep(NC_CH_NO_ENDPT_WAIT * 1000); |
| 3468 | } |
| 3469 | |
| 3470 | return NULL; |
| 3471 | } |
| 3472 | |
| 3473 | static int |
| 3474 | nc_server_ch_client_thread_session_cond_wait(struct nc_session *session, struct nc_ch_client_thread_arg *data) |
| 3475 | { |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3476 | int ret = 0, r; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3477 | uint32_t idle_timeout; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3478 | struct timespec ts; |
| 3479 | struct nc_ch_client *client; |
| 3480 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3481 | /* CH LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 3482 | pthread_mutex_lock(&session->opts.server.ch_lock); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3483 | |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 3484 | session->flags |= NC_SESSION_CH_THREAD; |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 3485 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3486 | /* give the session to the user */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3487 | if (data->new_session_cb(data->client_name, session)) { |
Michal Vasko | f1c26c2 | 2021-04-12 16:34:33 +0200 | [diff] [blame] | 3488 | /* something is wrong, free the session */ |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 3489 | session->flags &= ~NC_SESSION_CH_THREAD; |
Michal Vasko | f1c26c2 | 2021-04-12 16:34:33 +0200 | [diff] [blame] | 3490 | |
| 3491 | /* CH UNLOCK */ |
| 3492 | pthread_mutex_unlock(&session->opts.server.ch_lock); |
| 3493 | |
Michal Vasko | 77d56d7 | 2022-09-07 10:30:48 +0200 | [diff] [blame] | 3494 | /* session terminated, free it and release its context */ |
Michal Vasko | f1c26c2 | 2021-04-12 16:34:33 +0200 | [diff] [blame] | 3495 | nc_session_free(session, NULL); |
Michal Vasko | 58bac1c | 2022-03-24 15:25:26 +0100 | [diff] [blame] | 3496 | data->release_ctx_cb(data->ctx_cb_data); |
| 3497 | return ret; |
Michal Vasko | f1c26c2 | 2021-04-12 16:34:33 +0200 | [diff] [blame] | 3498 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3499 | |
| 3500 | do { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 3501 | nc_timeouttime_get(&ts, NC_CH_NO_ENDPT_WAIT); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3502 | |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 3503 | /* CH COND WAIT */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 3504 | r = pthread_cond_clockwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, COMPAT_CLOCK_ID, &ts); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3505 | if (!r) { |
| 3506 | /* we were woken up, something probably happened */ |
| 3507 | if (session->status != NC_STATUS_RUNNING) { |
| 3508 | break; |
| 3509 | } |
| 3510 | } else if (r != ETIMEDOUT) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3511 | ERR(session, "Pthread condition timedwait failed (%s).", strerror(r)); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3512 | ret = -1; |
| 3513 | break; |
Michal Vasko | 2e39ed9 | 2018-03-12 13:51:44 +0100 | [diff] [blame] | 3514 | } |
| 3515 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3516 | /* check whether the client was not removed */ |
| 3517 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3518 | nc_server_ch_client_lock(data->client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3519 | if (!client) { |
| 3520 | /* client was removed, finish thread */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3521 | VRB(session, "Call Home client \"%s\" removed, but an established session will not be terminated.", |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3522 | data->client_name); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3523 | ret = 1; |
| 3524 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3525 | } |
| 3526 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3527 | if (client->conn_type == NC_CH_PERIOD) { |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3528 | idle_timeout = client->conn.period.idle_timeout; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3529 | } else { |
| 3530 | idle_timeout = 0; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3531 | } |
| 3532 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 3533 | nc_timeouttime_get(&ts, 0); |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3534 | if (!nc_session_get_notif_status(session) && idle_timeout && (ts.tv_sec >= session->opts.server.last_rpc + idle_timeout)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3535 | VRB(session, "Call Home client \"%s\": session idle timeout elapsed.", client->name); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3536 | session->status = NC_STATUS_INVALID; |
| 3537 | session->term_reason = NC_SESSION_TERM_TIMEOUT; |
| 3538 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3539 | |
| 3540 | /* UNLOCK */ |
| 3541 | nc_server_ch_client_unlock(client); |
| 3542 | |
| 3543 | } while (session->status == NC_STATUS_RUNNING); |
| 3544 | |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 3545 | /* signal to nc_session_free() that CH thread is terminating */ |
| 3546 | session->flags &= ~NC_SESSION_CH_THREAD; |
| 3547 | pthread_cond_signal(&session->opts.server.ch_cond); |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 3548 | |
Michal Vasko | 2737742 | 2018-03-15 08:59:35 +0100 | [diff] [blame] | 3549 | /* CH UNLOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 3550 | pthread_mutex_unlock(&session->opts.server.ch_lock); |
Michal Vasko | 2737742 | 2018-03-15 08:59:35 +0100 | [diff] [blame] | 3551 | |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3552 | return ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | static void * |
| 3556 | nc_ch_client_thread(void *arg) |
| 3557 | { |
| 3558 | struct nc_ch_client_thread_arg *data = (struct nc_ch_client_thread_arg *)arg; |
| 3559 | NC_MSG_TYPE msgtype; |
| 3560 | uint8_t cur_attempts = 0; |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3561 | uint16_t next_endpt_index; |
Michal Vasko | 9550cf1 | 2017-03-21 15:33:58 +0100 | [diff] [blame] | 3562 | char *cur_endpt_name = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3563 | struct nc_ch_endpt *cur_endpt; |
| 3564 | struct nc_session *session; |
| 3565 | struct nc_ch_client *client; |
Michal Vasko | 5d0a1e3 | 2022-09-07 07:46:36 +0200 | [diff] [blame] | 3566 | uint32_t client_id, reconnect_in; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3567 | |
| 3568 | /* LOCK */ |
| 3569 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3570 | if (!client) { |
| 3571 | goto cleanup; |
| 3572 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3573 | client_id = client->id; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3574 | |
| 3575 | cur_endpt = &client->ch_endpts[0]; |
| 3576 | cur_endpt_name = strdup(cur_endpt->name); |
| 3577 | |
| 3578 | while (1) { |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 3579 | if (!cur_attempts) { |
| 3580 | VRB(NULL, "Call Home client \"%s\" endpoint \"%s\" connecting...", data->client_name, cur_endpt_name); |
| 3581 | } |
Michal Vasko | 58bac1c | 2022-03-24 15:25:26 +0100 | [diff] [blame] | 3582 | msgtype = nc_connect_ch_endpt(cur_endpt, data->acquire_ctx_cb, data->release_ctx_cb, data->ctx_cb_data, &session); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3583 | |
| 3584 | if (msgtype == NC_MSG_HELLO) { |
| 3585 | /* UNLOCK */ |
| 3586 | nc_server_ch_client_unlock(client); |
| 3587 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3588 | VRB(NULL, "Call Home client \"%s\" session %u established.", data->client_name, session->id); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3589 | if (nc_server_ch_client_thread_session_cond_wait(session, data)) { |
| 3590 | goto cleanup; |
| 3591 | } |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3592 | VRB(NULL, "Call Home client \"%s\" session terminated.", data->client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3593 | |
| 3594 | /* LOCK */ |
| 3595 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3596 | if (!client) { |
| 3597 | goto cleanup; |
| 3598 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3599 | if (client->id != client_id) { |
| 3600 | nc_server_ch_client_unlock(client); |
| 3601 | goto cleanup; |
| 3602 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3603 | |
| 3604 | /* session changed status -> it was disconnected for whatever reason, |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3605 | * persistent connection immediately tries to reconnect, periodic connects at specific times */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3606 | if (client->conn_type == NC_CH_PERIOD) { |
Michal Vasko | 18e1fa0 | 2021-11-29 09:02:05 +0100 | [diff] [blame] | 3607 | if (client->conn.period.anchor_time) { |
| 3608 | /* anchored */ |
| 3609 | reconnect_in = (time(NULL) - client->conn.period.anchor_time) % (client->conn.period.period * 60); |
| 3610 | } else { |
| 3611 | /* fixed timeout */ |
| 3612 | reconnect_in = client->conn.period.period * 60; |
| 3613 | } |
| 3614 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3615 | /* UNLOCK */ |
| 3616 | nc_server_ch_client_unlock(client); |
| 3617 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3618 | /* sleep until we should reconnect TODO wake up sometimes to check for new notifications */ |
Michal Vasko | 75b836e | 2022-09-07 07:51:45 +0200 | [diff] [blame] | 3619 | VRB(NULL, "Call Home client \"%s\" reconnecting in %" PRIu32 " seconds.", data->client_name, reconnect_in); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3620 | sleep(reconnect_in); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3621 | |
| 3622 | /* LOCK */ |
| 3623 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3624 | if (!client) { |
| 3625 | goto cleanup; |
| 3626 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3627 | if (client->id != client_id) { |
| 3628 | nc_server_ch_client_unlock(client); |
| 3629 | goto cleanup; |
| 3630 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3631 | } |
| 3632 | |
| 3633 | /* set next endpoint to try */ |
| 3634 | if (client->start_with == NC_CH_FIRST_LISTED) { |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3635 | next_endpt_index = 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3636 | } else if (client->start_with == NC_CH_LAST_CONNECTED) { |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3637 | /* we keep the current one but due to unlock/lock we have to find it again */ |
| 3638 | for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) { |
| 3639 | if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) { |
| 3640 | break; |
| 3641 | } |
| 3642 | } |
| 3643 | if (next_endpt_index >= client->ch_endpt_count) { |
| 3644 | /* endpoint was removed, start with the first one */ |
| 3645 | next_endpt_index = 0; |
| 3646 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3647 | } else { |
| 3648 | /* just get a random index */ |
| 3649 | next_endpt_index = rand() % client->ch_endpt_count; |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3652 | } else { |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3653 | /* UNLOCK */ |
| 3654 | nc_server_ch_client_unlock(client); |
| 3655 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3656 | /* session was not created */ |
Michal Vasko | d8951c7 | 2022-09-26 09:39:29 +0200 | [diff] [blame] | 3657 | sleep(NC_CH_ENDPT_BACKOFF_WAIT); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3658 | |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3659 | /* LOCK */ |
| 3660 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3661 | if (!client) { |
| 3662 | goto cleanup; |
| 3663 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3664 | if (client->id != client_id) { |
| 3665 | nc_server_ch_client_unlock(client); |
| 3666 | goto cleanup; |
| 3667 | } |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3668 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3669 | ++cur_attempts; |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3670 | |
| 3671 | /* try to find our endpoint again */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3672 | for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) { |
| 3673 | if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3674 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3675 | } |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3676 | } |
| 3677 | |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3678 | if (next_endpt_index >= client->ch_endpt_count) { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3679 | /* endpoint was removed, start with the first one */ |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 3680 | VRB(NULL, "Call Home client \"%s\" endpoint \"%s\" removed.", data->client_name, cur_endpt_name); |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3681 | next_endpt_index = 0; |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3682 | cur_attempts = 0; |
| 3683 | } else if (cur_attempts == client->max_attempts) { |
| 3684 | /* we have tried to connect to this endpoint enough times */ |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 3685 | VRB(NULL, "Call Home client \"%s\" endpoint \"%s\" failed connection attempt limit %" PRIu8 " reached.", |
| 3686 | data->client_name, cur_endpt_name, client->max_attempts); |
| 3687 | |
| 3688 | /* clear a pending socket, if any */ |
| 3689 | cur_endpt = &client->ch_endpts[next_endpt_index]; |
| 3690 | if (cur_endpt->sock_pending > -1) { |
| 3691 | close(cur_endpt->sock_pending); |
| 3692 | cur_endpt->sock_pending = -1; |
| 3693 | } |
| 3694 | |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3695 | if (next_endpt_index < client->ch_endpt_count - 1) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3696 | /* just go to the next endpoint */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3697 | ++next_endpt_index; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3698 | } else { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3699 | /* cur_endpoint is the last, start with the first one */ |
Michal Vasko | 2a22534 | 2018-09-05 08:38:34 +0200 | [diff] [blame] | 3700 | next_endpt_index = 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3701 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3702 | cur_attempts = 0; |
| 3703 | } /* else we keep the current one */ |
| 3704 | } |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3705 | |
| 3706 | cur_endpt = &client->ch_endpts[next_endpt_index]; |
| 3707 | free(cur_endpt_name); |
| 3708 | cur_endpt_name = strdup(cur_endpt->name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3709 | } |
| 3710 | |
| 3711 | cleanup: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3712 | VRB(NULL, "Call Home client \"%s\" thread exit.", data->client_name); |
Michal Vasko | 9550cf1 | 2017-03-21 15:33:58 +0100 | [diff] [blame] | 3713 | free(cur_endpt_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3714 | free(data->client_name); |
| 3715 | free(data); |
| 3716 | return NULL; |
| 3717 | } |
| 3718 | |
| 3719 | API int |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3720 | nc_connect_ch_client_dispatch(const char *client_name, nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb, |
| 3721 | nc_server_ch_session_release_ctx_cb release_ctx_cb, void *ctx_cb_data, nc_server_ch_new_session_cb new_session_cb) |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3722 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3723 | int ret; |
| 3724 | pthread_t tid; |
| 3725 | struct nc_ch_client_thread_arg *arg; |
| 3726 | |
| 3727 | if (!client_name) { |
| 3728 | ERRARG("client_name"); |
| 3729 | return -1; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3730 | } else if (!acquire_ctx_cb) { |
| 3731 | ERRARG("acquire_ctx_cb"); |
| 3732 | return -1; |
| 3733 | } else if (!release_ctx_cb) { |
| 3734 | ERRARG("release_ctx_cb"); |
| 3735 | return -1; |
| 3736 | } else if (!new_session_cb) { |
| 3737 | ERRARG("new_session_cb"); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3738 | return -1; |
| 3739 | } |
| 3740 | |
| 3741 | arg = malloc(sizeof *arg); |
| 3742 | if (!arg) { |
| 3743 | ERRMEM; |
| 3744 | return -1; |
| 3745 | } |
| 3746 | arg->client_name = strdup(client_name); |
| 3747 | if (!arg->client_name) { |
| 3748 | ERRMEM; |
| 3749 | free(arg); |
| 3750 | return -1; |
| 3751 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3752 | arg->acquire_ctx_cb = acquire_ctx_cb; |
| 3753 | arg->release_ctx_cb = release_ctx_cb; |
| 3754 | arg->ctx_cb_data = ctx_cb_data; |
| 3755 | arg->new_session_cb = new_session_cb; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3756 | |
| 3757 | ret = pthread_create(&tid, NULL, nc_ch_client_thread, arg); |
| 3758 | if (ret) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3759 | ERR(NULL, "Creating a new thread failed (%s).", strerror(ret)); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3760 | free(arg->client_name); |
| 3761 | free(arg); |
| 3762 | return -1; |
| 3763 | } |
| 3764 | /* the thread now manages arg */ |
| 3765 | |
| 3766 | pthread_detach(tid); |
| 3767 | |
| 3768 | return 0; |
| 3769 | } |
| 3770 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3771 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3772 | |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 3773 | API time_t |
| 3774 | nc_session_get_start_time(const struct nc_session *session) |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3775 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3776 | if (!session || (session->side != NC_SERVER)) { |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3777 | ERRARG("session"); |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 3778 | return 0; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3779 | } |
| 3780 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3781 | return session->opts.server.session_start; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3782 | } |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3783 | |
| 3784 | API void |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 3785 | nc_session_inc_notif_status(struct nc_session *session) |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3786 | { |
| 3787 | if (!session || (session->side != NC_SERVER)) { |
| 3788 | ERRARG("session"); |
| 3789 | return; |
| 3790 | } |
| 3791 | |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3792 | /* NTF STATUS LOCK */ |
| 3793 | pthread_mutex_lock(&session->opts.server.ntf_status_lock); |
| 3794 | |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 3795 | ++session->opts.server.ntf_status; |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3796 | |
| 3797 | /* NTF STATUS UNLOCK */ |
| 3798 | pthread_mutex_unlock(&session->opts.server.ntf_status_lock); |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 3799 | } |
| 3800 | |
| 3801 | API void |
| 3802 | nc_session_dec_notif_status(struct nc_session *session) |
| 3803 | { |
| 3804 | if (!session || (session->side != NC_SERVER)) { |
| 3805 | ERRARG("session"); |
| 3806 | return; |
| 3807 | } |
| 3808 | |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3809 | /* NTF STATUS LOCK */ |
| 3810 | pthread_mutex_lock(&session->opts.server.ntf_status_lock); |
| 3811 | |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 3812 | if (session->opts.server.ntf_status) { |
| 3813 | --session->opts.server.ntf_status; |
| 3814 | } |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3815 | |
| 3816 | /* NTF STATUS UNLOCK */ |
| 3817 | pthread_mutex_unlock(&session->opts.server.ntf_status_lock); |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3818 | } |
| 3819 | |
| 3820 | API int |
| 3821 | nc_session_get_notif_status(const struct nc_session *session) |
| 3822 | { |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3823 | uint32_t ntf_status; |
| 3824 | |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3825 | if (!session || (session->side != NC_SERVER)) { |
| 3826 | ERRARG("session"); |
| 3827 | return 0; |
| 3828 | } |
| 3829 | |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3830 | /* NTF STATUS LOCK */ |
| 3831 | pthread_mutex_lock(&((struct nc_session *)session)->opts.server.ntf_status_lock); |
| 3832 | |
| 3833 | ntf_status = session->opts.server.ntf_status; |
| 3834 | |
| 3835 | /* NTF STATUS UNLOCK */ |
| 3836 | pthread_mutex_unlock(&((struct nc_session *)session)->opts.server.ntf_status_lock); |
| 3837 | |
| 3838 | return ntf_status; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3839 | } |