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