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