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