Blame


1 2b3d32a1 2022-12-30 thomas /*
2 2b3d32a1 2022-12-30 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 2b3d32a1 2022-12-30 thomas *
4 2b3d32a1 2022-12-30 thomas * Permission to use, copy, modify, and distribute this software for any
5 2b3d32a1 2022-12-30 thomas * purpose with or without fee is hereby granted, provided that the above
6 2b3d32a1 2022-12-30 thomas * copyright notice and this permission notice appear in all copies.
7 2b3d32a1 2022-12-30 thomas *
8 2b3d32a1 2022-12-30 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 2b3d32a1 2022-12-30 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 2b3d32a1 2022-12-30 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 2b3d32a1 2022-12-30 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 2b3d32a1 2022-12-30 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 2b3d32a1 2022-12-30 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 2b3d32a1 2022-12-30 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 2b3d32a1 2022-12-30 thomas */
16 2b3d32a1 2022-12-30 thomas
17 2b3d32a1 2022-12-30 thomas #include <sys/types.h>
18 2b3d32a1 2022-12-30 thomas #include <sys/queue.h>
19 2b3d32a1 2022-12-30 thomas #include <sys/socket.h>
20 2b3d32a1 2022-12-30 thomas #include <sys/uio.h>
21 2b3d32a1 2022-12-30 thomas
22 2b3d32a1 2022-12-30 thomas #include <errno.h>
23 2b3d32a1 2022-12-30 thomas #include <event.h>
24 2b3d32a1 2022-12-30 thomas #include <stdint.h>
25 2b3d32a1 2022-12-30 thomas #include <stdio.h>
26 2b3d32a1 2022-12-30 thomas #include <stdlib.h>
27 2b3d32a1 2022-12-30 thomas #include <string.h>
28 2b3d32a1 2022-12-30 thomas #include <imsg.h>
29 2b3d32a1 2022-12-30 thomas #include <limits.h>
30 2b3d32a1 2022-12-30 thomas #include <signal.h>
31 2b3d32a1 2022-12-30 thomas #include <unistd.h>
32 2b3d32a1 2022-12-30 thomas
33 2b3d32a1 2022-12-30 thomas #include "got_error.h"
34 6d7eb4f7 2023-04-04 thomas #include "got_path.h"
35 2b3d32a1 2022-12-30 thomas
36 621b8de0 2023-08-23 thomas #include "got_compat.h"
37 621b8de0 2023-08-23 thomas
38 2b3d32a1 2022-12-30 thomas #include "gotd.h"
39 2b3d32a1 2022-12-30 thomas #include "log.h"
40 2b3d32a1 2022-12-30 thomas #include "listen.h"
41 2b3d32a1 2022-12-30 thomas
42 2b3d32a1 2022-12-30 thomas #ifndef nitems
43 2b3d32a1 2022-12-30 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
44 2b3d32a1 2022-12-30 thomas #endif
45 2b3d32a1 2022-12-30 thomas
46 2b3d32a1 2022-12-30 thomas struct gotd_listen_client {
47 2b3d32a1 2022-12-30 thomas STAILQ_ENTRY(gotd_listen_client) entry;
48 2b3d32a1 2022-12-30 thomas uint32_t id;
49 2b3d32a1 2022-12-30 thomas int fd;
50 ba63ab46 2023-01-02 thomas uid_t euid;
51 2b3d32a1 2022-12-30 thomas };
52 2b3d32a1 2022-12-30 thomas STAILQ_HEAD(gotd_listen_clients, gotd_listen_client);
53 2b3d32a1 2022-12-30 thomas
54 2b3d32a1 2022-12-30 thomas static struct gotd_listen_clients gotd_listen_clients[GOTD_CLIENT_TABLE_SIZE];
55 2b3d32a1 2022-12-30 thomas static SIPHASH_KEY clients_hash_key;
56 2b3d32a1 2022-12-30 thomas static volatile int listen_client_cnt;
57 2b3d32a1 2022-12-30 thomas static int inflight;
58 2b3d32a1 2022-12-30 thomas
59 ba63ab46 2023-01-02 thomas struct gotd_uid_connection_counter {
60 ba63ab46 2023-01-02 thomas STAILQ_ENTRY(gotd_uid_connection_counter) entry;
61 ba63ab46 2023-01-02 thomas uid_t euid;
62 ba63ab46 2023-01-02 thomas int nconnections;
63 ba63ab46 2023-01-02 thomas };
64 ba63ab46 2023-01-02 thomas STAILQ_HEAD(gotd_client_uids, gotd_uid_connection_counter);
65 ba63ab46 2023-01-02 thomas static struct gotd_client_uids gotd_client_uids[GOTD_CLIENT_TABLE_SIZE];
66 ba63ab46 2023-01-02 thomas static SIPHASH_KEY uid_hash_key;
67 ba63ab46 2023-01-02 thomas
68 2b3d32a1 2022-12-30 thomas static struct {
69 2b3d32a1 2022-12-30 thomas pid_t pid;
70 2b3d32a1 2022-12-30 thomas const char *title;
71 2b3d32a1 2022-12-30 thomas int fd;
72 2b3d32a1 2022-12-30 thomas struct gotd_imsgev iev;
73 2b3d32a1 2022-12-30 thomas struct gotd_imsgev pause;
74 0781db0e 2023-01-06 thomas struct gotd_uid_connection_limit *connection_limits;
75 0781db0e 2023-01-06 thomas size_t nconnection_limits;
76 2b3d32a1 2022-12-30 thomas } gotd_listen;
77 2b3d32a1 2022-12-30 thomas
78 2b3d32a1 2022-12-30 thomas static int inflight;
79 2b3d32a1 2022-12-30 thomas
80 2b3d32a1 2022-12-30 thomas static void listen_shutdown(void);
81 2b3d32a1 2022-12-30 thomas
82 2b3d32a1 2022-12-30 thomas static void
83 2b3d32a1 2022-12-30 thomas listen_sighdlr(int sig, short event, void *arg)
84 2b3d32a1 2022-12-30 thomas {
85 2b3d32a1 2022-12-30 thomas /*
86 2b3d32a1 2022-12-30 thomas * Normal signal handler rules don't apply because libevent
87 2b3d32a1 2022-12-30 thomas * decouples for us.
88 2b3d32a1 2022-12-30 thomas */
89 2b3d32a1 2022-12-30 thomas
90 2b3d32a1 2022-12-30 thomas switch (sig) {
91 2b3d32a1 2022-12-30 thomas case SIGHUP:
92 2b3d32a1 2022-12-30 thomas break;
93 2b3d32a1 2022-12-30 thomas case SIGUSR1:
94 2b3d32a1 2022-12-30 thomas break;
95 2b3d32a1 2022-12-30 thomas case SIGTERM:
96 2b3d32a1 2022-12-30 thomas case SIGINT:
97 2b3d32a1 2022-12-30 thomas listen_shutdown();
98 2b3d32a1 2022-12-30 thomas /* NOTREACHED */
99 2b3d32a1 2022-12-30 thomas break;
100 2b3d32a1 2022-12-30 thomas default:
101 2b3d32a1 2022-12-30 thomas fatalx("unexpected signal");
102 2b3d32a1 2022-12-30 thomas }
103 2b3d32a1 2022-12-30 thomas }
104 2b3d32a1 2022-12-30 thomas
105 2b3d32a1 2022-12-30 thomas static uint64_t
106 2b3d32a1 2022-12-30 thomas client_hash(uint32_t client_id)
107 2b3d32a1 2022-12-30 thomas {
108 2b3d32a1 2022-12-30 thomas return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
109 2b3d32a1 2022-12-30 thomas }
110 2b3d32a1 2022-12-30 thomas
111 2b3d32a1 2022-12-30 thomas static void
112 2b3d32a1 2022-12-30 thomas add_client(struct gotd_listen_client *client)
113 2b3d32a1 2022-12-30 thomas {
114 2b3d32a1 2022-12-30 thomas uint64_t slot = client_hash(client->id) % nitems(gotd_listen_clients);
115 2b3d32a1 2022-12-30 thomas STAILQ_INSERT_HEAD(&gotd_listen_clients[slot], client, entry);
116 2b3d32a1 2022-12-30 thomas listen_client_cnt++;
117 2b3d32a1 2022-12-30 thomas }
118 2b3d32a1 2022-12-30 thomas
119 2b3d32a1 2022-12-30 thomas static struct gotd_listen_client *
120 2b3d32a1 2022-12-30 thomas find_client(uint32_t client_id)
121 2b3d32a1 2022-12-30 thomas {
122 2b3d32a1 2022-12-30 thomas uint64_t slot;
123 2b3d32a1 2022-12-30 thomas struct gotd_listen_client *c;
124 2b3d32a1 2022-12-30 thomas
125 2b3d32a1 2022-12-30 thomas slot = client_hash(client_id) % nitems(gotd_listen_clients);
126 2b3d32a1 2022-12-30 thomas STAILQ_FOREACH(c, &gotd_listen_clients[slot], entry) {
127 2b3d32a1 2022-12-30 thomas if (c->id == client_id)
128 2b3d32a1 2022-12-30 thomas return c;
129 2b3d32a1 2022-12-30 thomas }
130 2b3d32a1 2022-12-30 thomas
131 2b3d32a1 2022-12-30 thomas return NULL;
132 2b3d32a1 2022-12-30 thomas }
133 2b3d32a1 2022-12-30 thomas
134 2b3d32a1 2022-12-30 thomas static uint32_t
135 2b3d32a1 2022-12-30 thomas get_client_id(void)
136 2b3d32a1 2022-12-30 thomas {
137 2b3d32a1 2022-12-30 thomas int duplicate = 0;
138 2b3d32a1 2022-12-30 thomas uint32_t id;
139 2b3d32a1 2022-12-30 thomas
140 2b3d32a1 2022-12-30 thomas do {
141 2b3d32a1 2022-12-30 thomas id = arc4random();
142 2b3d32a1 2022-12-30 thomas duplicate = (find_client(id) != NULL);
143 2b3d32a1 2022-12-30 thomas } while (duplicate || id == 0);
144 2b3d32a1 2022-12-30 thomas
145 2b3d32a1 2022-12-30 thomas return id;
146 ba63ab46 2023-01-02 thomas }
147 ba63ab46 2023-01-02 thomas
148 ba63ab46 2023-01-02 thomas static uint64_t
149 ba63ab46 2023-01-02 thomas uid_hash(uid_t euid)
150 ba63ab46 2023-01-02 thomas {
151 ba63ab46 2023-01-02 thomas return SipHash24(&uid_hash_key, &euid, sizeof(euid));
152 ba63ab46 2023-01-02 thomas }
153 ba63ab46 2023-01-02 thomas
154 ba63ab46 2023-01-02 thomas static void
155 ba63ab46 2023-01-02 thomas add_uid_connection_counter(struct gotd_uid_connection_counter *counter)
156 ba63ab46 2023-01-02 thomas {
157 ba63ab46 2023-01-02 thomas uint64_t slot = uid_hash(counter->euid) % nitems(gotd_client_uids);
158 ba63ab46 2023-01-02 thomas STAILQ_INSERT_HEAD(&gotd_client_uids[slot], counter, entry);
159 ba63ab46 2023-01-02 thomas }
160 ba63ab46 2023-01-02 thomas
161 ba63ab46 2023-01-02 thomas static void
162 ba63ab46 2023-01-02 thomas remove_uid_connection_counter(struct gotd_uid_connection_counter *counter)
163 ba63ab46 2023-01-02 thomas {
164 ba63ab46 2023-01-02 thomas uint64_t slot = uid_hash(counter->euid) % nitems(gotd_client_uids);
165 ba63ab46 2023-01-02 thomas STAILQ_REMOVE(&gotd_client_uids[slot], counter,
166 ba63ab46 2023-01-02 thomas gotd_uid_connection_counter, entry);
167 ba63ab46 2023-01-02 thomas }
168 ba63ab46 2023-01-02 thomas
169 ba63ab46 2023-01-02 thomas static struct gotd_uid_connection_counter *
170 ba63ab46 2023-01-02 thomas find_uid_connection_counter(uid_t euid)
171 ba63ab46 2023-01-02 thomas {
172 ba63ab46 2023-01-02 thomas uint64_t slot;
173 ba63ab46 2023-01-02 thomas struct gotd_uid_connection_counter *c;
174 ba63ab46 2023-01-02 thomas
175 ba63ab46 2023-01-02 thomas slot = uid_hash(euid) % nitems(gotd_client_uids);
176 ba63ab46 2023-01-02 thomas STAILQ_FOREACH(c, &gotd_client_uids[slot], entry) {
177 ba63ab46 2023-01-02 thomas if (c->euid == euid)
178 ba63ab46 2023-01-02 thomas return c;
179 ba63ab46 2023-01-02 thomas }
180 ba63ab46 2023-01-02 thomas
181 ba63ab46 2023-01-02 thomas return NULL;
182 2b3d32a1 2022-12-30 thomas }
183 2b3d32a1 2022-12-30 thomas
184 2b3d32a1 2022-12-30 thomas static const struct got_error *
185 2b3d32a1 2022-12-30 thomas disconnect(struct gotd_listen_client *client)
186 2b3d32a1 2022-12-30 thomas {
187 ba63ab46 2023-01-02 thomas struct gotd_uid_connection_counter *counter;
188 2b3d32a1 2022-12-30 thomas uint64_t slot;
189 2b3d32a1 2022-12-30 thomas int client_fd;
190 2b3d32a1 2022-12-30 thomas
191 2b3d32a1 2022-12-30 thomas log_debug("client on fd %d disconnecting", client->fd);
192 2b3d32a1 2022-12-30 thomas
193 2b3d32a1 2022-12-30 thomas slot = client_hash(client->id) % nitems(gotd_listen_clients);
194 2b3d32a1 2022-12-30 thomas STAILQ_REMOVE(&gotd_listen_clients[slot], client,
195 2b3d32a1 2022-12-30 thomas gotd_listen_client, entry);
196 ba63ab46 2023-01-02 thomas
197 ba63ab46 2023-01-02 thomas counter = find_uid_connection_counter(client->euid);
198 ba63ab46 2023-01-02 thomas if (counter) {
199 ba63ab46 2023-01-02 thomas if (counter->nconnections > 0)
200 ba63ab46 2023-01-02 thomas counter->nconnections--;
201 ba63ab46 2023-01-02 thomas if (counter->nconnections == 0) {
202 ba63ab46 2023-01-02 thomas remove_uid_connection_counter(counter);
203 ba63ab46 2023-01-02 thomas free(counter);
204 ba63ab46 2023-01-02 thomas }
205 ba63ab46 2023-01-02 thomas }
206 ba63ab46 2023-01-02 thomas
207 2b3d32a1 2022-12-30 thomas client_fd = client->fd;
208 2b3d32a1 2022-12-30 thomas free(client);
209 2b3d32a1 2022-12-30 thomas inflight--;
210 2b3d32a1 2022-12-30 thomas listen_client_cnt--;
211 2b3d32a1 2022-12-30 thomas if (close(client_fd) == -1)
212 2b3d32a1 2022-12-30 thomas return got_error_from_errno("close");
213 2b3d32a1 2022-12-30 thomas
214 2b3d32a1 2022-12-30 thomas return NULL;
215 2b3d32a1 2022-12-30 thomas }
216 2b3d32a1 2022-12-30 thomas
217 2b3d32a1 2022-12-30 thomas static int
218 2b3d32a1 2022-12-30 thomas accept_reserve(int fd, struct sockaddr *addr, socklen_t *addrlen,
219 2b3d32a1 2022-12-30 thomas int reserve, volatile int *counter)
220 2b3d32a1 2022-12-30 thomas {
221 2b3d32a1 2022-12-30 thomas int ret;
222 621b8de0 2023-08-23 thomas int sock_flags = SOCK_NONBLOCK;
223 2b3d32a1 2022-12-30 thomas
224 621b8de0 2023-08-23 thomas #ifdef SOCK_CLOEXEC
225 621b8de0 2023-08-23 thomas sock_flags |= SOCK_CLOEXEC;
226 621b8de0 2023-08-23 thomas #endif
227 621b8de0 2023-08-23 thomas
228 2b3d32a1 2022-12-30 thomas if (getdtablecount() + reserve +
229 2b3d32a1 2022-12-30 thomas ((*counter + 1) * GOTD_FD_NEEDED) >= getdtablesize()) {
230 2b3d32a1 2022-12-30 thomas log_debug("inflight fds exceeded");
231 2b3d32a1 2022-12-30 thomas errno = EMFILE;
232 2b3d32a1 2022-12-30 thomas return -1;
233 2b3d32a1 2022-12-30 thomas }
234 621b8de0 2023-08-23 thomas #ifdef __APPLE__
235 621b8de0 2023-08-23 thomas /* TA: silence warning from GCC. */
236 621b8de0 2023-08-23 thomas (void)sock_flags;
237 621b8de0 2023-08-23 thomas ret = accept(fd, addr, addrlen);
238 621b8de0 2023-08-23 thomas #else
239 621b8de0 2023-08-23 thomas ret = accept4(fd, addr, addrlen, sock_flags);
240 621b8de0 2023-08-23 thomas #endif
241 2b3d32a1 2022-12-30 thomas
242 621b8de0 2023-08-23 thomas if (ret > -1) {
243 2b3d32a1 2022-12-30 thomas (*counter)++;
244 2b3d32a1 2022-12-30 thomas }
245 2b3d32a1 2022-12-30 thomas
246 2b3d32a1 2022-12-30 thomas return ret;
247 2b3d32a1 2022-12-30 thomas }
248 2b3d32a1 2022-12-30 thomas
249 2b3d32a1 2022-12-30 thomas static void
250 2b3d32a1 2022-12-30 thomas gotd_accept_paused(int fd, short event, void *arg)
251 2b3d32a1 2022-12-30 thomas {
252 2b3d32a1 2022-12-30 thomas event_add(&gotd_listen.iev.ev, NULL);
253 2b3d32a1 2022-12-30 thomas }
254 2b3d32a1 2022-12-30 thomas
255 2b3d32a1 2022-12-30 thomas static void
256 2b3d32a1 2022-12-30 thomas gotd_accept(int fd, short event, void *arg)
257 2b3d32a1 2022-12-30 thomas {
258 2b3d32a1 2022-12-30 thomas struct gotd_imsgev *iev = arg;
259 2b3d32a1 2022-12-30 thomas struct sockaddr_storage ss;
260 2b3d32a1 2022-12-30 thomas struct timeval backoff;
261 2b3d32a1 2022-12-30 thomas socklen_t len;
262 2b3d32a1 2022-12-30 thomas int s = -1;
263 2b3d32a1 2022-12-30 thomas struct gotd_listen_client *client = NULL;
264 ba63ab46 2023-01-02 thomas struct gotd_uid_connection_counter *counter = NULL;
265 2b3d32a1 2022-12-30 thomas struct gotd_imsg_connect iconn;
266 0bcde4c8 2022-12-30 thomas uid_t euid;
267 0bcde4c8 2022-12-30 thomas gid_t egid;
268 2b3d32a1 2022-12-30 thomas
269 2b3d32a1 2022-12-30 thomas backoff.tv_sec = 1;
270 2b3d32a1 2022-12-30 thomas backoff.tv_usec = 0;
271 2b3d32a1 2022-12-30 thomas
272 2b3d32a1 2022-12-30 thomas if (event_add(&gotd_listen.iev.ev, NULL) == -1) {
273 2b3d32a1 2022-12-30 thomas log_warn("event_add");
274 2b3d32a1 2022-12-30 thomas return;
275 2b3d32a1 2022-12-30 thomas }
276 2b3d32a1 2022-12-30 thomas if (event & EV_TIMEOUT)
277 2b3d32a1 2022-12-30 thomas return;
278 2b3d32a1 2022-12-30 thomas
279 2b3d32a1 2022-12-30 thomas len = sizeof(ss);
280 2b3d32a1 2022-12-30 thomas
281 2b3d32a1 2022-12-30 thomas /* Other backoff conditions apart from EMFILE/ENFILE? */
282 2b3d32a1 2022-12-30 thomas s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
283 2b3d32a1 2022-12-30 thomas &inflight);
284 2b3d32a1 2022-12-30 thomas if (s == -1) {
285 2b3d32a1 2022-12-30 thomas switch (errno) {
286 2b3d32a1 2022-12-30 thomas case EINTR:
287 2b3d32a1 2022-12-30 thomas case EWOULDBLOCK:
288 2b3d32a1 2022-12-30 thomas case ECONNABORTED:
289 2b3d32a1 2022-12-30 thomas return;
290 2b3d32a1 2022-12-30 thomas case EMFILE:
291 2b3d32a1 2022-12-30 thomas case ENFILE:
292 2b3d32a1 2022-12-30 thomas event_del(&gotd_listen.iev.ev);
293 2b3d32a1 2022-12-30 thomas evtimer_add(&gotd_listen.pause.ev, &backoff);
294 2b3d32a1 2022-12-30 thomas return;
295 2b3d32a1 2022-12-30 thomas default:
296 2b3d32a1 2022-12-30 thomas log_warn("accept");
297 2b3d32a1 2022-12-30 thomas return;
298 2b3d32a1 2022-12-30 thomas }
299 2b3d32a1 2022-12-30 thomas }
300 2b3d32a1 2022-12-30 thomas
301 2b3d32a1 2022-12-30 thomas if (listen_client_cnt >= GOTD_MAXCLIENTS)
302 0bcde4c8 2022-12-30 thomas goto err;
303 0bcde4c8 2022-12-30 thomas
304 0bcde4c8 2022-12-30 thomas if (getpeereid(s, &euid, &egid) == -1) {
305 0bcde4c8 2022-12-30 thomas log_warn("getpeerid");
306 2b3d32a1 2022-12-30 thomas goto err;
307 0bcde4c8 2022-12-30 thomas }
308 2b3d32a1 2022-12-30 thomas
309 ba63ab46 2023-01-02 thomas counter = find_uid_connection_counter(euid);
310 ba63ab46 2023-01-02 thomas if (counter == NULL) {
311 ba63ab46 2023-01-02 thomas counter = calloc(1, sizeof(*counter));
312 ba63ab46 2023-01-02 thomas if (counter == NULL) {
313 ba63ab46 2023-01-02 thomas log_warn("%s: calloc", __func__);
314 ba63ab46 2023-01-02 thomas goto err;
315 ba63ab46 2023-01-02 thomas }
316 ba63ab46 2023-01-02 thomas counter->euid = euid;
317 ba63ab46 2023-01-02 thomas counter->nconnections = 1;
318 ba63ab46 2023-01-02 thomas add_uid_connection_counter(counter);
319 ba63ab46 2023-01-02 thomas } else {
320 0781db0e 2023-01-06 thomas int max_connections = GOTD_MAX_CONN_PER_UID;
321 0781db0e 2023-01-06 thomas struct gotd_uid_connection_limit *limit;
322 0781db0e 2023-01-06 thomas
323 0781db0e 2023-01-06 thomas limit = gotd_find_uid_connection_limit(
324 0781db0e 2023-01-06 thomas gotd_listen.connection_limits,
325 0781db0e 2023-01-06 thomas gotd_listen.nconnection_limits, euid);
326 0781db0e 2023-01-06 thomas if (limit)
327 0781db0e 2023-01-06 thomas max_connections = limit->max_connections;
328 0781db0e 2023-01-06 thomas
329 0781db0e 2023-01-06 thomas if (counter->nconnections >= max_connections) {
330 ba63ab46 2023-01-02 thomas log_warnx("maximum connections exceeded for uid %d",
331 ba63ab46 2023-01-02 thomas euid);
332 ba63ab46 2023-01-02 thomas goto err;
333 ba63ab46 2023-01-02 thomas }
334 ba63ab46 2023-01-02 thomas counter->nconnections++;
335 ba63ab46 2023-01-02 thomas }
336 ba63ab46 2023-01-02 thomas
337 2b3d32a1 2022-12-30 thomas client = calloc(1, sizeof(*client));
338 2b3d32a1 2022-12-30 thomas if (client == NULL) {
339 2b3d32a1 2022-12-30 thomas log_warn("%s: calloc", __func__);
340 2b3d32a1 2022-12-30 thomas goto err;
341 2b3d32a1 2022-12-30 thomas }
342 2b3d32a1 2022-12-30 thomas client->id = get_client_id();
343 2b3d32a1 2022-12-30 thomas client->fd = s;
344 ba63ab46 2023-01-02 thomas client->euid = euid;
345 2b3d32a1 2022-12-30 thomas s = -1;
346 2b3d32a1 2022-12-30 thomas add_client(client);
347 0bcde4c8 2022-12-30 thomas log_debug("%s: new client connected on fd %d uid %d gid %d", __func__,
348 0bcde4c8 2022-12-30 thomas client->fd, euid, egid);
349 2b3d32a1 2022-12-30 thomas
350 2b3d32a1 2022-12-30 thomas memset(&iconn, 0, sizeof(iconn));
351 2b3d32a1 2022-12-30 thomas iconn.client_id = client->id;
352 0bcde4c8 2022-12-30 thomas iconn.euid = euid;
353 0bcde4c8 2022-12-30 thomas iconn.egid = egid;
354 2b3d32a1 2022-12-30 thomas s = dup(client->fd);
355 2b3d32a1 2022-12-30 thomas if (s == -1) {
356 2b3d32a1 2022-12-30 thomas log_warn("%s: dup", __func__);
357 2b3d32a1 2022-12-30 thomas goto err;
358 2b3d32a1 2022-12-30 thomas }
359 2b3d32a1 2022-12-30 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_CONNECT, PROC_LISTEN, s,
360 2b3d32a1 2022-12-30 thomas &iconn, sizeof(iconn)) == -1) {
361 2b3d32a1 2022-12-30 thomas log_warn("imsg compose CONNECT");
362 2b3d32a1 2022-12-30 thomas goto err;
363 2b3d32a1 2022-12-30 thomas }
364 2b3d32a1 2022-12-30 thomas
365 2b3d32a1 2022-12-30 thomas return;
366 2b3d32a1 2022-12-30 thomas err:
367 2b3d32a1 2022-12-30 thomas inflight--;
368 2b3d32a1 2022-12-30 thomas if (client)
369 2b3d32a1 2022-12-30 thomas disconnect(client);
370 2b3d32a1 2022-12-30 thomas if (s != -1)
371 2b3d32a1 2022-12-30 thomas close(s);
372 2b3d32a1 2022-12-30 thomas }
373 2b3d32a1 2022-12-30 thomas
374 2b3d32a1 2022-12-30 thomas static const struct got_error *
375 2b3d32a1 2022-12-30 thomas recv_disconnect(struct imsg *imsg)
376 2b3d32a1 2022-12-30 thomas {
377 2b3d32a1 2022-12-30 thomas struct gotd_imsg_disconnect idisconnect;
378 2b3d32a1 2022-12-30 thomas size_t datalen;
379 2b3d32a1 2022-12-30 thomas struct gotd_listen_client *client = NULL;
380 2b3d32a1 2022-12-30 thomas
381 2b3d32a1 2022-12-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
382 2b3d32a1 2022-12-30 thomas if (datalen != sizeof(idisconnect))
383 2b3d32a1 2022-12-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
384 2b3d32a1 2022-12-30 thomas memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
385 2b3d32a1 2022-12-30 thomas
386 2b3d32a1 2022-12-30 thomas log_debug("client disconnecting");
387 2b3d32a1 2022-12-30 thomas
388 2b3d32a1 2022-12-30 thomas client = find_client(idisconnect.client_id);
389 2b3d32a1 2022-12-30 thomas if (client == NULL)
390 2b3d32a1 2022-12-30 thomas return got_error(GOT_ERR_CLIENT_ID);
391 2b3d32a1 2022-12-30 thomas
392 2b3d32a1 2022-12-30 thomas return disconnect(client);
393 2b3d32a1 2022-12-30 thomas }
394 2b3d32a1 2022-12-30 thomas
395 2b3d32a1 2022-12-30 thomas static void
396 2b3d32a1 2022-12-30 thomas listen_dispatch(int fd, short event, void *arg)
397 2b3d32a1 2022-12-30 thomas {
398 2b3d32a1 2022-12-30 thomas const struct got_error *err = NULL;
399 2b3d32a1 2022-12-30 thomas struct gotd_imsgev *iev = arg;
400 2b3d32a1 2022-12-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
401 2b3d32a1 2022-12-30 thomas struct imsg imsg;
402 2b3d32a1 2022-12-30 thomas ssize_t n;
403 2b3d32a1 2022-12-30 thomas int shut = 0;
404 2b3d32a1 2022-12-30 thomas
405 2b3d32a1 2022-12-30 thomas if (event & EV_READ) {
406 2b3d32a1 2022-12-30 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
407 2b3d32a1 2022-12-30 thomas fatal("imsg_read error");
408 2b3d32a1 2022-12-30 thomas if (n == 0) /* Connection closed. */
409 2b3d32a1 2022-12-30 thomas shut = 1;
410 2b3d32a1 2022-12-30 thomas }
411 2b3d32a1 2022-12-30 thomas
412 2b3d32a1 2022-12-30 thomas if (event & EV_WRITE) {
413 2b3d32a1 2022-12-30 thomas n = msgbuf_write(&ibuf->w);
414 2b3d32a1 2022-12-30 thomas if (n == -1 && errno != EAGAIN)
415 2b3d32a1 2022-12-30 thomas fatal("msgbuf_write");
416 2b3d32a1 2022-12-30 thomas if (n == 0) /* Connection closed. */
417 2b3d32a1 2022-12-30 thomas shut = 1;
418 2b3d32a1 2022-12-30 thomas }
419 2b3d32a1 2022-12-30 thomas
420 2b3d32a1 2022-12-30 thomas for (;;) {
421 2b3d32a1 2022-12-30 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
422 2b3d32a1 2022-12-30 thomas fatal("%s: imsg_get", __func__);
423 2b3d32a1 2022-12-30 thomas if (n == 0) /* No more messages. */
424 2b3d32a1 2022-12-30 thomas break;
425 2b3d32a1 2022-12-30 thomas
426 2b3d32a1 2022-12-30 thomas switch (imsg.hdr.type) {
427 2b3d32a1 2022-12-30 thomas case GOTD_IMSG_DISCONNECT:
428 2b3d32a1 2022-12-30 thomas err = recv_disconnect(&imsg);
429 2b3d32a1 2022-12-30 thomas if (err)
430 5330ab76 2023-02-17 thomas log_warnx("disconnect: %s", err->msg);
431 2b3d32a1 2022-12-30 thomas break;
432 2b3d32a1 2022-12-30 thomas default:
433 5330ab76 2023-02-17 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
434 2b3d32a1 2022-12-30 thomas break;
435 2b3d32a1 2022-12-30 thomas }
436 2b3d32a1 2022-12-30 thomas
437 2b3d32a1 2022-12-30 thomas imsg_free(&imsg);
438 2b3d32a1 2022-12-30 thomas }
439 2b3d32a1 2022-12-30 thomas
440 2b3d32a1 2022-12-30 thomas if (!shut) {
441 2b3d32a1 2022-12-30 thomas gotd_imsg_event_add(iev);
442 2b3d32a1 2022-12-30 thomas } else {
443 2b3d32a1 2022-12-30 thomas /* This pipe is dead. Remove its event handler */
444 2b3d32a1 2022-12-30 thomas event_del(&iev->ev);
445 2b3d32a1 2022-12-30 thomas event_loopexit(NULL);
446 2b3d32a1 2022-12-30 thomas }
447 2b3d32a1 2022-12-30 thomas }
448 2b3d32a1 2022-12-30 thomas
449 2b3d32a1 2022-12-30 thomas void
450 0781db0e 2023-01-06 thomas listen_main(const char *title, int gotd_socket,
451 0781db0e 2023-01-06 thomas struct gotd_uid_connection_limit *connection_limits,
452 0781db0e 2023-01-06 thomas size_t nconnection_limits)
453 2b3d32a1 2022-12-30 thomas {
454 2b3d32a1 2022-12-30 thomas struct gotd_imsgev iev;
455 2b3d32a1 2022-12-30 thomas struct event evsigint, evsigterm, evsighup, evsigusr1;
456 56409302 2023-01-02 thomas
457 56409302 2023-01-02 thomas arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
458 ba63ab46 2023-01-02 thomas arc4random_buf(&uid_hash_key, sizeof(uid_hash_key));
459 2b3d32a1 2022-12-30 thomas
460 2b3d32a1 2022-12-30 thomas gotd_listen.title = title;
461 2b3d32a1 2022-12-30 thomas gotd_listen.pid = getpid();
462 2b3d32a1 2022-12-30 thomas gotd_listen.fd = gotd_socket;
463 0781db0e 2023-01-06 thomas gotd_listen.connection_limits = connection_limits;
464 0781db0e 2023-01-06 thomas gotd_listen.nconnection_limits = nconnection_limits;
465 2b3d32a1 2022-12-30 thomas
466 2b3d32a1 2022-12-30 thomas signal_set(&evsigint, SIGINT, listen_sighdlr, NULL);
467 2b3d32a1 2022-12-30 thomas signal_set(&evsigterm, SIGTERM, listen_sighdlr, NULL);
468 2b3d32a1 2022-12-30 thomas signal_set(&evsighup, SIGHUP, listen_sighdlr, NULL);
469 2b3d32a1 2022-12-30 thomas signal_set(&evsigusr1, SIGUSR1, listen_sighdlr, NULL);
470 2b3d32a1 2022-12-30 thomas signal(SIGPIPE, SIG_IGN);
471 2b3d32a1 2022-12-30 thomas
472 2b3d32a1 2022-12-30 thomas signal_add(&evsigint, NULL);
473 2b3d32a1 2022-12-30 thomas signal_add(&evsigterm, NULL);
474 2b3d32a1 2022-12-30 thomas signal_add(&evsighup, NULL);
475 2b3d32a1 2022-12-30 thomas signal_add(&evsigusr1, NULL);
476 2b3d32a1 2022-12-30 thomas
477 2b3d32a1 2022-12-30 thomas imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
478 2b3d32a1 2022-12-30 thomas iev.handler = listen_dispatch;
479 2b3d32a1 2022-12-30 thomas iev.events = EV_READ;
480 2b3d32a1 2022-12-30 thomas iev.handler_arg = NULL;
481 2b3d32a1 2022-12-30 thomas event_set(&iev.ev, iev.ibuf.fd, EV_READ, listen_dispatch, &iev);
482 2b3d32a1 2022-12-30 thomas if (event_add(&iev.ev, NULL) == -1)
483 2b3d32a1 2022-12-30 thomas fatalx("event add");
484 2b3d32a1 2022-12-30 thomas
485 2b3d32a1 2022-12-30 thomas event_set(&gotd_listen.iev.ev, gotd_listen.fd, EV_READ | EV_PERSIST,
486 2b3d32a1 2022-12-30 thomas gotd_accept, &iev);
487 2b3d32a1 2022-12-30 thomas if (event_add(&gotd_listen.iev.ev, NULL))
488 2b3d32a1 2022-12-30 thomas fatalx("event add");
489 2b3d32a1 2022-12-30 thomas evtimer_set(&gotd_listen.pause.ev, gotd_accept_paused, NULL);
490 2b3d32a1 2022-12-30 thomas
491 2b3d32a1 2022-12-30 thomas event_dispatch();
492 2b3d32a1 2022-12-30 thomas
493 2b3d32a1 2022-12-30 thomas listen_shutdown();
494 2b3d32a1 2022-12-30 thomas }
495 2b3d32a1 2022-12-30 thomas
496 2b3d32a1 2022-12-30 thomas static void
497 2b3d32a1 2022-12-30 thomas listen_shutdown(void)
498 2b3d32a1 2022-12-30 thomas {
499 5330ab76 2023-02-17 thomas log_debug("shutting down");
500 2b3d32a1 2022-12-30 thomas
501 0781db0e 2023-01-06 thomas free(gotd_listen.connection_limits);
502 2b3d32a1 2022-12-30 thomas if (gotd_listen.fd != -1)
503 2b3d32a1 2022-12-30 thomas close(gotd_listen.fd);
504 2b3d32a1 2022-12-30 thomas
505 2b3d32a1 2022-12-30 thomas exit(0);
506 2b3d32a1 2022-12-30 thomas }