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