Blame


1 3efd8e31 2022-10-23 thomas /*
2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 3efd8e31 2022-10-23 thomas *
4 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
5 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
6 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
7 3efd8e31 2022-10-23 thomas *
8 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 3efd8e31 2022-10-23 thomas */
16 3efd8e31 2022-10-23 thomas
17 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
18 3efd8e31 2022-10-23 thomas #include <sys/tree.h>
19 3efd8e31 2022-10-23 thomas #include <sys/time.h>
20 3efd8e31 2022-10-23 thomas #include <sys/types.h>
21 3efd8e31 2022-10-23 thomas #include <sys/stat.h>
22 3efd8e31 2022-10-23 thomas #include <sys/socket.h>
23 3efd8e31 2022-10-23 thomas #include <sys/un.h>
24 3efd8e31 2022-10-23 thomas #include <sys/wait.h>
25 3efd8e31 2022-10-23 thomas
26 3efd8e31 2022-10-23 thomas #include <fcntl.h>
27 3efd8e31 2022-10-23 thomas #include <err.h>
28 3efd8e31 2022-10-23 thomas #include <errno.h>
29 3efd8e31 2022-10-23 thomas #include <event.h>
30 3efd8e31 2022-10-23 thomas #include <limits.h>
31 3efd8e31 2022-10-23 thomas #include <pwd.h>
32 3efd8e31 2022-10-23 thomas #include <grp.h>
33 3efd8e31 2022-10-23 thomas #include <imsg.h>
34 3efd8e31 2022-10-23 thomas #include <sha1.h>
35 3efd8e31 2022-10-23 thomas #include <signal.h>
36 3efd8e31 2022-10-23 thomas #include <siphash.h>
37 3efd8e31 2022-10-23 thomas #include <stdarg.h>
38 3efd8e31 2022-10-23 thomas #include <stdio.h>
39 3efd8e31 2022-10-23 thomas #include <stdlib.h>
40 3efd8e31 2022-10-23 thomas #include <string.h>
41 3efd8e31 2022-10-23 thomas #include <syslog.h>
42 3efd8e31 2022-10-23 thomas #include <unistd.h>
43 3efd8e31 2022-10-23 thomas
44 3efd8e31 2022-10-23 thomas #include "got_error.h"
45 3efd8e31 2022-10-23 thomas #include "got_opentemp.h"
46 3efd8e31 2022-10-23 thomas #include "got_path.h"
47 3efd8e31 2022-10-23 thomas #include "got_repository.h"
48 3efd8e31 2022-10-23 thomas #include "got_object.h"
49 3efd8e31 2022-10-23 thomas #include "got_reference.h"
50 3efd8e31 2022-10-23 thomas
51 3efd8e31 2022-10-23 thomas #include "got_lib_delta.h"
52 3efd8e31 2022-10-23 thomas #include "got_lib_object.h"
53 3efd8e31 2022-10-23 thomas #include "got_lib_object_cache.h"
54 3efd8e31 2022-10-23 thomas #include "got_lib_sha1.h"
55 3efd8e31 2022-10-23 thomas #include "got_lib_gitproto.h"
56 3efd8e31 2022-10-23 thomas #include "got_lib_pack.h"
57 3efd8e31 2022-10-23 thomas #include "got_lib_repository.h"
58 3efd8e31 2022-10-23 thomas
59 3efd8e31 2022-10-23 thomas #include "gotd.h"
60 3efd8e31 2022-10-23 thomas #include "log.h"
61 2b3d32a1 2022-12-30 thomas #include "listen.h"
62 729a7e24 2022-11-17 thomas #include "auth.h"
63 3efd8e31 2022-10-23 thomas #include "repo_read.h"
64 3efd8e31 2022-10-23 thomas #include "repo_write.h"
65 3efd8e31 2022-10-23 thomas
66 3efd8e31 2022-10-23 thomas #ifndef nitems
67 3efd8e31 2022-10-23 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 3efd8e31 2022-10-23 thomas #endif
69 3efd8e31 2022-10-23 thomas
70 3efd8e31 2022-10-23 thomas struct gotd_client {
71 3efd8e31 2022-10-23 thomas STAILQ_ENTRY(gotd_client) entry;
72 3efd8e31 2022-10-23 thomas enum gotd_client_state state;
73 3efd8e31 2022-10-23 thomas struct gotd_client_capability *capabilities;
74 3efd8e31 2022-10-23 thomas size_t ncapa_alloc;
75 3efd8e31 2022-10-23 thomas size_t ncapabilities;
76 3efd8e31 2022-10-23 thomas uint32_t id;
77 3efd8e31 2022-10-23 thomas int fd;
78 3efd8e31 2022-10-23 thomas int delta_cache_fd;
79 3efd8e31 2022-10-23 thomas struct gotd_imsgev iev;
80 3efd8e31 2022-10-23 thomas struct event tmo;
81 3efd8e31 2022-10-23 thomas uid_t euid;
82 3efd8e31 2022-10-23 thomas gid_t egid;
83 3efd8e31 2022-10-23 thomas struct gotd_child_proc *repo_read;
84 3efd8e31 2022-10-23 thomas struct gotd_child_proc *repo_write;
85 c669c489 2022-12-30 thomas struct gotd_child_proc *auth;
86 c669c489 2022-12-30 thomas int required_auth;
87 3efd8e31 2022-10-23 thomas char *packfile_path;
88 3efd8e31 2022-10-23 thomas char *packidx_path;
89 3efd8e31 2022-10-23 thomas int nref_updates;
90 3efd8e31 2022-10-23 thomas };
91 3efd8e31 2022-10-23 thomas STAILQ_HEAD(gotd_clients, gotd_client);
92 3efd8e31 2022-10-23 thomas
93 3efd8e31 2022-10-23 thomas static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
94 3efd8e31 2022-10-23 thomas static SIPHASH_KEY clients_hash_key;
95 3efd8e31 2022-10-23 thomas volatile int client_cnt;
96 3efd8e31 2022-10-23 thomas static struct timeval timeout = { 3600, 0 };
97 95ef3f8a 2022-12-30 thomas static struct timeval auth_timeout = { 5, 0 };
98 3efd8e31 2022-10-23 thomas static struct gotd gotd;
99 3efd8e31 2022-10-23 thomas
100 3efd8e31 2022-10-23 thomas void gotd_sighdlr(int sig, short event, void *arg);
101 c902213d 2022-10-29 thomas static void gotd_shutdown(void);
102 85b37c72 2022-12-30 thomas static const struct got_error *start_repo_child(struct gotd_client *,
103 85b37c72 2022-12-30 thomas enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
104 c669c489 2022-12-30 thomas static const struct got_error *start_auth_child(struct gotd_client *, int,
105 c669c489 2022-12-30 thomas struct gotd_repo *, char *, const char *, int, int);
106 85b37c72 2022-12-30 thomas static void kill_proc(struct gotd_child_proc *, int);
107 3efd8e31 2022-10-23 thomas
108 3efd8e31 2022-10-23 thomas __dead static void
109 3efd8e31 2022-10-23 thomas usage()
110 3efd8e31 2022-10-23 thomas {
111 5ac853dc 2022-10-24 thomas fprintf(stderr, "usage: %s [-dv] [-f config-file]\n", getprogname());
112 5ac853dc 2022-10-24 thomas exit(1);
113 3efd8e31 2022-10-23 thomas }
114 3efd8e31 2022-10-23 thomas
115 3efd8e31 2022-10-23 thomas static int
116 3efd8e31 2022-10-23 thomas unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
117 3efd8e31 2022-10-23 thomas {
118 3efd8e31 2022-10-23 thomas struct sockaddr_un sun;
119 3efd8e31 2022-10-23 thomas int fd = -1;
120 3efd8e31 2022-10-23 thomas mode_t old_umask, mode;
121 3efd8e31 2022-10-23 thomas
122 3efd8e31 2022-10-23 thomas fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
123 3efd8e31 2022-10-23 thomas if (fd == -1) {
124 3efd8e31 2022-10-23 thomas log_warn("socket");
125 3efd8e31 2022-10-23 thomas return -1;
126 3efd8e31 2022-10-23 thomas }
127 3efd8e31 2022-10-23 thomas
128 3efd8e31 2022-10-23 thomas sun.sun_family = AF_UNIX;
129 3efd8e31 2022-10-23 thomas if (strlcpy(sun.sun_path, unix_socket_path,
130 3efd8e31 2022-10-23 thomas sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
131 3efd8e31 2022-10-23 thomas log_warnx("%s: name too long", unix_socket_path);
132 3efd8e31 2022-10-23 thomas close(fd);
133 3efd8e31 2022-10-23 thomas return -1;
134 3efd8e31 2022-10-23 thomas }
135 3efd8e31 2022-10-23 thomas
136 3efd8e31 2022-10-23 thomas if (unlink(unix_socket_path) == -1) {
137 3efd8e31 2022-10-23 thomas if (errno != ENOENT) {
138 3efd8e31 2022-10-23 thomas log_warn("unlink %s", unix_socket_path);
139 3efd8e31 2022-10-23 thomas close(fd);
140 3efd8e31 2022-10-23 thomas return -1;
141 3efd8e31 2022-10-23 thomas }
142 3efd8e31 2022-10-23 thomas }
143 3efd8e31 2022-10-23 thomas
144 3efd8e31 2022-10-23 thomas old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
145 3efd8e31 2022-10-23 thomas mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
146 3efd8e31 2022-10-23 thomas
147 3efd8e31 2022-10-23 thomas if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
148 3efd8e31 2022-10-23 thomas log_warn("bind: %s", unix_socket_path);
149 3efd8e31 2022-10-23 thomas close(fd);
150 3efd8e31 2022-10-23 thomas umask(old_umask);
151 3efd8e31 2022-10-23 thomas return -1;
152 3efd8e31 2022-10-23 thomas }
153 3efd8e31 2022-10-23 thomas
154 3efd8e31 2022-10-23 thomas umask(old_umask);
155 3efd8e31 2022-10-23 thomas
156 3efd8e31 2022-10-23 thomas if (chmod(unix_socket_path, mode) == -1) {
157 3efd8e31 2022-10-23 thomas log_warn("chmod %o %s", mode, unix_socket_path);
158 3efd8e31 2022-10-23 thomas close(fd);
159 3efd8e31 2022-10-23 thomas unlink(unix_socket_path);
160 3efd8e31 2022-10-23 thomas return -1;
161 3efd8e31 2022-10-23 thomas }
162 3efd8e31 2022-10-23 thomas
163 3efd8e31 2022-10-23 thomas if (chown(unix_socket_path, uid, gid) == -1) {
164 3efd8e31 2022-10-23 thomas log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
165 3efd8e31 2022-10-23 thomas close(fd);
166 3efd8e31 2022-10-23 thomas unlink(unix_socket_path);
167 3efd8e31 2022-10-23 thomas return -1;
168 3efd8e31 2022-10-23 thomas }
169 3efd8e31 2022-10-23 thomas
170 3efd8e31 2022-10-23 thomas if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
171 3efd8e31 2022-10-23 thomas log_warn("listen");
172 3efd8e31 2022-10-23 thomas close(fd);
173 3efd8e31 2022-10-23 thomas unlink(unix_socket_path);
174 3efd8e31 2022-10-23 thomas return -1;
175 3efd8e31 2022-10-23 thomas }
176 3efd8e31 2022-10-23 thomas
177 3efd8e31 2022-10-23 thomas return fd;
178 3efd8e31 2022-10-23 thomas }
179 3efd8e31 2022-10-23 thomas
180 3efd8e31 2022-10-23 thomas static struct group *
181 3efd8e31 2022-10-23 thomas match_group(gid_t *groups, int ngroups, const char *unix_group_name)
182 3efd8e31 2022-10-23 thomas {
183 3efd8e31 2022-10-23 thomas struct group *gr;
184 3efd8e31 2022-10-23 thomas int i;
185 3efd8e31 2022-10-23 thomas
186 3efd8e31 2022-10-23 thomas for (i = 0; i < ngroups; i++) {
187 3efd8e31 2022-10-23 thomas gr = getgrgid(groups[i]);
188 3efd8e31 2022-10-23 thomas if (gr == NULL) {
189 3efd8e31 2022-10-23 thomas log_warn("getgrgid %d", groups[i]);
190 3efd8e31 2022-10-23 thomas continue;
191 3efd8e31 2022-10-23 thomas }
192 3efd8e31 2022-10-23 thomas if (strcmp(gr->gr_name, unix_group_name) == 0)
193 3efd8e31 2022-10-23 thomas return gr;
194 3efd8e31 2022-10-23 thomas }
195 3efd8e31 2022-10-23 thomas
196 3efd8e31 2022-10-23 thomas return NULL;
197 3efd8e31 2022-10-23 thomas }
198 3efd8e31 2022-10-23 thomas
199 3efd8e31 2022-10-23 thomas static uint64_t
200 3efd8e31 2022-10-23 thomas client_hash(uint32_t client_id)
201 3efd8e31 2022-10-23 thomas {
202 3efd8e31 2022-10-23 thomas return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
203 3efd8e31 2022-10-23 thomas }
204 3efd8e31 2022-10-23 thomas
205 3efd8e31 2022-10-23 thomas static void
206 3efd8e31 2022-10-23 thomas add_client(struct gotd_client *client)
207 3efd8e31 2022-10-23 thomas {
208 3efd8e31 2022-10-23 thomas uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
209 3efd8e31 2022-10-23 thomas STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
210 3efd8e31 2022-10-23 thomas client_cnt++;
211 3efd8e31 2022-10-23 thomas }
212 3efd8e31 2022-10-23 thomas
213 3efd8e31 2022-10-23 thomas static struct gotd_client *
214 3efd8e31 2022-10-23 thomas find_client(uint32_t client_id)
215 3efd8e31 2022-10-23 thomas {
216 3efd8e31 2022-10-23 thomas uint64_t slot;
217 3efd8e31 2022-10-23 thomas struct gotd_client *c;
218 3efd8e31 2022-10-23 thomas
219 3efd8e31 2022-10-23 thomas slot = client_hash(client_id) % nitems(gotd_clients);
220 3efd8e31 2022-10-23 thomas STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
221 3efd8e31 2022-10-23 thomas if (c->id == client_id)
222 3efd8e31 2022-10-23 thomas return c;
223 3efd8e31 2022-10-23 thomas }
224 3efd8e31 2022-10-23 thomas
225 3efd8e31 2022-10-23 thomas return NULL;
226 3efd8e31 2022-10-23 thomas }
227 3efd8e31 2022-10-23 thomas
228 3efd8e31 2022-10-23 thomas static struct gotd_child_proc *
229 3efd8e31 2022-10-23 thomas get_client_proc(struct gotd_client *client)
230 3efd8e31 2022-10-23 thomas {
231 3efd8e31 2022-10-23 thomas if (client->repo_read && client->repo_write) {
232 3efd8e31 2022-10-23 thomas fatalx("uid %d is reading and writing in the same session",
233 3efd8e31 2022-10-23 thomas client->euid);
234 3efd8e31 2022-10-23 thomas /* NOTREACHED */
235 3efd8e31 2022-10-23 thomas }
236 3efd8e31 2022-10-23 thomas
237 3efd8e31 2022-10-23 thomas if (client->repo_read)
238 3efd8e31 2022-10-23 thomas return client->repo_read;
239 3efd8e31 2022-10-23 thomas else if (client->repo_write)
240 3efd8e31 2022-10-23 thomas return client->repo_write;
241 85b37c72 2022-12-30 thomas
242 85b37c72 2022-12-30 thomas return NULL;
243 85b37c72 2022-12-30 thomas }
244 85b37c72 2022-12-30 thomas
245 85b37c72 2022-12-30 thomas static struct gotd_client *
246 85b37c72 2022-12-30 thomas find_client_by_proc_fd(int fd)
247 85b37c72 2022-12-30 thomas {
248 85b37c72 2022-12-30 thomas uint64_t slot;
249 85b37c72 2022-12-30 thomas
250 85b37c72 2022-12-30 thomas for (slot = 0; slot < nitems(gotd_clients); slot++) {
251 85b37c72 2022-12-30 thomas struct gotd_client *c;
252 85b37c72 2022-12-30 thomas
253 85b37c72 2022-12-30 thomas STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
254 85b37c72 2022-12-30 thomas struct gotd_child_proc *proc = get_client_proc(c);
255 85b37c72 2022-12-30 thomas if (proc && proc->iev.ibuf.fd == fd)
256 85b37c72 2022-12-30 thomas return c;
257 c669c489 2022-12-30 thomas if (c->auth && c->auth->iev.ibuf.fd == fd)
258 c669c489 2022-12-30 thomas return c;
259 85b37c72 2022-12-30 thomas }
260 85b37c72 2022-12-30 thomas }
261 c902213d 2022-10-29 thomas
262 3efd8e31 2022-10-23 thomas return NULL;
263 3efd8e31 2022-10-23 thomas }
264 3efd8e31 2022-10-23 thomas
265 3efd8e31 2022-10-23 thomas static int
266 3efd8e31 2022-10-23 thomas client_is_reading(struct gotd_client *client)
267 3efd8e31 2022-10-23 thomas {
268 3efd8e31 2022-10-23 thomas return client->repo_read != NULL;
269 3efd8e31 2022-10-23 thomas }
270 3efd8e31 2022-10-23 thomas
271 3efd8e31 2022-10-23 thomas static int
272 3efd8e31 2022-10-23 thomas client_is_writing(struct gotd_client *client)
273 3efd8e31 2022-10-23 thomas {
274 3efd8e31 2022-10-23 thomas return client->repo_write != NULL;
275 3efd8e31 2022-10-23 thomas }
276 3efd8e31 2022-10-23 thomas
277 3efd8e31 2022-10-23 thomas static const struct got_error *
278 3efd8e31 2022-10-23 thomas ensure_client_is_reading(struct gotd_client *client)
279 3efd8e31 2022-10-23 thomas {
280 3efd8e31 2022-10-23 thomas if (!client_is_reading(client)) {
281 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
282 3efd8e31 2022-10-23 thomas "uid %d made a read-request but is not reading from "
283 3efd8e31 2022-10-23 thomas "a repository", client->euid);
284 3efd8e31 2022-10-23 thomas }
285 3efd8e31 2022-10-23 thomas
286 3efd8e31 2022-10-23 thomas return NULL;
287 3efd8e31 2022-10-23 thomas }
288 3efd8e31 2022-10-23 thomas
289 3efd8e31 2022-10-23 thomas static const struct got_error *
290 3efd8e31 2022-10-23 thomas ensure_client_is_writing(struct gotd_client *client)
291 3efd8e31 2022-10-23 thomas {
292 3efd8e31 2022-10-23 thomas if (!client_is_writing(client)) {
293 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
294 3efd8e31 2022-10-23 thomas "uid %d made a write-request but is not writing to "
295 3efd8e31 2022-10-23 thomas "a repository", client->euid);
296 3efd8e31 2022-10-23 thomas }
297 3efd8e31 2022-10-23 thomas
298 3efd8e31 2022-10-23 thomas return NULL;
299 3efd8e31 2022-10-23 thomas }
300 3efd8e31 2022-10-23 thomas
301 3efd8e31 2022-10-23 thomas static const struct got_error *
302 3efd8e31 2022-10-23 thomas ensure_client_is_not_writing(struct gotd_client *client)
303 3efd8e31 2022-10-23 thomas {
304 3efd8e31 2022-10-23 thomas if (client_is_writing(client)) {
305 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
306 3efd8e31 2022-10-23 thomas "uid %d made a read-request but is writing to "
307 3efd8e31 2022-10-23 thomas "a repository", client->euid);
308 3efd8e31 2022-10-23 thomas }
309 3efd8e31 2022-10-23 thomas
310 3efd8e31 2022-10-23 thomas return NULL;
311 3efd8e31 2022-10-23 thomas }
312 3efd8e31 2022-10-23 thomas
313 3efd8e31 2022-10-23 thomas static const struct got_error *
314 3efd8e31 2022-10-23 thomas ensure_client_is_not_reading(struct gotd_client *client)
315 3efd8e31 2022-10-23 thomas {
316 3efd8e31 2022-10-23 thomas if (client_is_reading(client)) {
317 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
318 3efd8e31 2022-10-23 thomas "uid %d made a write-request but is reading from "
319 3efd8e31 2022-10-23 thomas "a repository", client->euid);
320 3efd8e31 2022-10-23 thomas }
321 3efd8e31 2022-10-23 thomas
322 3efd8e31 2022-10-23 thomas return NULL;
323 85b37c72 2022-12-30 thomas }
324 85b37c72 2022-12-30 thomas
325 85b37c72 2022-12-30 thomas static void
326 c669c489 2022-12-30 thomas wait_for_child(pid_t child_pid)
327 85b37c72 2022-12-30 thomas {
328 85b37c72 2022-12-30 thomas pid_t pid;
329 85b37c72 2022-12-30 thomas int status;
330 85b37c72 2022-12-30 thomas
331 c669c489 2022-12-30 thomas log_debug("waiting for child PID %ld to terminate",
332 c669c489 2022-12-30 thomas (long)child_pid);
333 85b37c72 2022-12-30 thomas
334 85b37c72 2022-12-30 thomas do {
335 c669c489 2022-12-30 thomas pid = waitpid(child_pid, &status, WNOHANG);
336 85b37c72 2022-12-30 thomas if (pid == -1) {
337 85b37c72 2022-12-30 thomas if (errno != EINTR && errno != ECHILD)
338 85b37c72 2022-12-30 thomas fatal("wait");
339 85b37c72 2022-12-30 thomas } else if (WIFSIGNALED(status)) {
340 85b37c72 2022-12-30 thomas log_warnx("child PID %ld terminated; signal %d",
341 85b37c72 2022-12-30 thomas (long)pid, WTERMSIG(status));
342 46ecc01f 2022-12-30 thomas }
343 85b37c72 2022-12-30 thomas } while (pid != -1 || (pid == -1 && errno == EINTR));
344 3efd8e31 2022-10-23 thomas }
345 3efd8e31 2022-10-23 thomas
346 3efd8e31 2022-10-23 thomas static void
347 c669c489 2022-12-30 thomas kill_auth_proc(struct gotd_client *client)
348 c669c489 2022-12-30 thomas {
349 c669c489 2022-12-30 thomas struct gotd_child_proc *proc;
350 c669c489 2022-12-30 thomas
351 c669c489 2022-12-30 thomas if (client->auth == NULL)
352 c669c489 2022-12-30 thomas return;
353 c669c489 2022-12-30 thomas
354 c669c489 2022-12-30 thomas proc = client->auth;
355 c669c489 2022-12-30 thomas client->auth = NULL;
356 c669c489 2022-12-30 thomas
357 c669c489 2022-12-30 thomas event_del(&proc->iev.ev);
358 c669c489 2022-12-30 thomas msgbuf_clear(&proc->iev.ibuf.w);
359 c669c489 2022-12-30 thomas close(proc->iev.ibuf.fd);
360 c669c489 2022-12-30 thomas kill_proc(proc, 0);
361 c669c489 2022-12-30 thomas wait_for_child(proc->pid);
362 c669c489 2022-12-30 thomas free(proc);
363 c669c489 2022-12-30 thomas }
364 c669c489 2022-12-30 thomas
365 c669c489 2022-12-30 thomas static void
366 3efd8e31 2022-10-23 thomas disconnect(struct gotd_client *client)
367 3efd8e31 2022-10-23 thomas {
368 3efd8e31 2022-10-23 thomas struct gotd_imsg_disconnect idisconnect;
369 3efd8e31 2022-10-23 thomas struct gotd_child_proc *proc = get_client_proc(client);
370 85b37c72 2022-12-30 thomas struct gotd_child_proc *listen_proc = &gotd.listen_proc;
371 3efd8e31 2022-10-23 thomas uint64_t slot;
372 3efd8e31 2022-10-23 thomas
373 3efd8e31 2022-10-23 thomas log_debug("uid %d: disconnecting", client->euid);
374 c669c489 2022-12-30 thomas
375 c669c489 2022-12-30 thomas kill_auth_proc(client);
376 3efd8e31 2022-10-23 thomas
377 3efd8e31 2022-10-23 thomas idisconnect.client_id = client->id;
378 c902213d 2022-10-29 thomas if (proc) {
379 c902213d 2022-10-29 thomas if (gotd_imsg_compose_event(&proc->iev,
380 c902213d 2022-10-29 thomas GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
381 c902213d 2022-10-29 thomas &idisconnect, sizeof(idisconnect)) == -1)
382 c902213d 2022-10-29 thomas log_warn("imsg compose DISCONNECT");
383 85b37c72 2022-12-30 thomas
384 85b37c72 2022-12-30 thomas msgbuf_clear(&proc->iev.ibuf.w);
385 85b37c72 2022-12-30 thomas close(proc->iev.ibuf.fd);
386 85b37c72 2022-12-30 thomas kill_proc(proc, 0);
387 c669c489 2022-12-30 thomas wait_for_child(proc->pid);
388 85b37c72 2022-12-30 thomas free(proc);
389 85b37c72 2022-12-30 thomas proc = NULL;
390 c902213d 2022-10-29 thomas }
391 2b3d32a1 2022-12-30 thomas
392 2b3d32a1 2022-12-30 thomas if (gotd_imsg_compose_event(&listen_proc->iev,
393 2b3d32a1 2022-12-30 thomas GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
394 2b3d32a1 2022-12-30 thomas &idisconnect, sizeof(idisconnect)) == -1)
395 2b3d32a1 2022-12-30 thomas log_warn("imsg compose DISCONNECT");
396 2b3d32a1 2022-12-30 thomas
397 3efd8e31 2022-10-23 thomas slot = client_hash(client->id) % nitems(gotd_clients);
398 3efd8e31 2022-10-23 thomas STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
399 3efd8e31 2022-10-23 thomas imsg_clear(&client->iev.ibuf);
400 3efd8e31 2022-10-23 thomas event_del(&client->iev.ev);
401 3efd8e31 2022-10-23 thomas evtimer_del(&client->tmo);
402 3efd8e31 2022-10-23 thomas close(client->fd);
403 3efd8e31 2022-10-23 thomas if (client->delta_cache_fd != -1)
404 3efd8e31 2022-10-23 thomas close(client->delta_cache_fd);
405 3efd8e31 2022-10-23 thomas if (client->packfile_path) {
406 3efd8e31 2022-10-23 thomas if (unlink(client->packfile_path) == -1 && errno != ENOENT)
407 3efd8e31 2022-10-23 thomas log_warn("unlink %s: ", client->packfile_path);
408 3efd8e31 2022-10-23 thomas free(client->packfile_path);
409 3efd8e31 2022-10-23 thomas }
410 3efd8e31 2022-10-23 thomas if (client->packidx_path) {
411 3efd8e31 2022-10-23 thomas if (unlink(client->packidx_path) == -1 && errno != ENOENT)
412 3efd8e31 2022-10-23 thomas log_warn("unlink %s: ", client->packidx_path);
413 3efd8e31 2022-10-23 thomas free(client->packidx_path);
414 3efd8e31 2022-10-23 thomas }
415 3efd8e31 2022-10-23 thomas free(client->capabilities);
416 3efd8e31 2022-10-23 thomas free(client);
417 3efd8e31 2022-10-23 thomas client_cnt--;
418 3efd8e31 2022-10-23 thomas }
419 3efd8e31 2022-10-23 thomas
420 3efd8e31 2022-10-23 thomas static void
421 3efd8e31 2022-10-23 thomas disconnect_on_error(struct gotd_client *client, const struct got_error *err)
422 3efd8e31 2022-10-23 thomas {
423 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
424 3efd8e31 2022-10-23 thomas
425 3efd8e31 2022-10-23 thomas log_warnx("uid %d: %s", client->euid, err->msg);
426 3efd8e31 2022-10-23 thomas if (err->code != GOT_ERR_EOF) {
427 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, client->fd);
428 3efd8e31 2022-10-23 thomas gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
429 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
430 3efd8e31 2022-10-23 thomas }
431 3efd8e31 2022-10-23 thomas disconnect(client);
432 c902213d 2022-10-29 thomas }
433 c902213d 2022-10-29 thomas
434 c902213d 2022-10-29 thomas static const struct got_error *
435 c902213d 2022-10-29 thomas send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
436 c902213d 2022-10-29 thomas {
437 c902213d 2022-10-29 thomas const struct got_error *err = NULL;
438 c902213d 2022-10-29 thomas struct gotd_imsg_info_repo irepo;
439 c902213d 2022-10-29 thomas
440 c902213d 2022-10-29 thomas memset(&irepo, 0, sizeof(irepo));
441 c902213d 2022-10-29 thomas
442 c902213d 2022-10-29 thomas if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
443 c902213d 2022-10-29 thomas >= sizeof(irepo.repo_name))
444 c902213d 2022-10-29 thomas return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
445 c902213d 2022-10-29 thomas if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
446 c902213d 2022-10-29 thomas >= sizeof(irepo.repo_path))
447 c902213d 2022-10-29 thomas return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
448 c902213d 2022-10-29 thomas
449 c902213d 2022-10-29 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
450 c902213d 2022-10-29 thomas &irepo, sizeof(irepo)) == -1) {
451 c902213d 2022-10-29 thomas err = got_error_from_errno("imsg compose INFO_REPO");
452 c902213d 2022-10-29 thomas if (err)
453 c902213d 2022-10-29 thomas return err;
454 c902213d 2022-10-29 thomas }
455 c902213d 2022-10-29 thomas
456 c902213d 2022-10-29 thomas return NULL;
457 3efd8e31 2022-10-23 thomas }
458 3efd8e31 2022-10-23 thomas
459 c902213d 2022-10-29 thomas static const struct got_error *
460 c902213d 2022-10-29 thomas send_capability(struct gotd_client_capability *capa, struct gotd_imsgev* iev)
461 c902213d 2022-10-29 thomas {
462 c902213d 2022-10-29 thomas const struct got_error *err = NULL;
463 c902213d 2022-10-29 thomas struct gotd_imsg_capability icapa;
464 c902213d 2022-10-29 thomas size_t len;
465 c902213d 2022-10-29 thomas struct ibuf *wbuf;
466 c902213d 2022-10-29 thomas
467 c902213d 2022-10-29 thomas memset(&icapa, 0, sizeof(icapa));
468 c902213d 2022-10-29 thomas
469 c902213d 2022-10-29 thomas icapa.key_len = strlen(capa->key);
470 c902213d 2022-10-29 thomas len = sizeof(icapa) + icapa.key_len;
471 c902213d 2022-10-29 thomas if (capa->value) {
472 c902213d 2022-10-29 thomas icapa.value_len = strlen(capa->value);
473 c902213d 2022-10-29 thomas len += icapa.value_len;
474 c902213d 2022-10-29 thomas }
475 c902213d 2022-10-29 thomas
476 c902213d 2022-10-29 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
477 c902213d 2022-10-29 thomas if (wbuf == NULL) {
478 c902213d 2022-10-29 thomas err = got_error_from_errno("imsg_create CAPABILITY");
479 c902213d 2022-10-29 thomas return err;
480 c902213d 2022-10-29 thomas }
481 c902213d 2022-10-29 thomas
482 c902213d 2022-10-29 thomas if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
483 c902213d 2022-10-29 thomas return got_error_from_errno("imsg_add CAPABILITY");
484 c902213d 2022-10-29 thomas if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
485 c902213d 2022-10-29 thomas return got_error_from_errno("imsg_add CAPABILITY");
486 c902213d 2022-10-29 thomas if (capa->value) {
487 c902213d 2022-10-29 thomas if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
488 c902213d 2022-10-29 thomas return got_error_from_errno("imsg_add CAPABILITY");
489 c902213d 2022-10-29 thomas }
490 c902213d 2022-10-29 thomas
491 c902213d 2022-10-29 thomas wbuf->fd = -1;
492 c902213d 2022-10-29 thomas imsg_close(&iev->ibuf, wbuf);
493 c902213d 2022-10-29 thomas
494 c902213d 2022-10-29 thomas gotd_imsg_event_add(iev);
495 c902213d 2022-10-29 thomas
496 c902213d 2022-10-29 thomas return NULL;
497 c902213d 2022-10-29 thomas }
498 c902213d 2022-10-29 thomas
499 c902213d 2022-10-29 thomas static const struct got_error *
500 c902213d 2022-10-29 thomas send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
501 c902213d 2022-10-29 thomas {
502 c902213d 2022-10-29 thomas const struct got_error *err = NULL;
503 c902213d 2022-10-29 thomas struct gotd_imsg_info_client iclient;
504 c902213d 2022-10-29 thomas struct gotd_child_proc *proc;
505 c902213d 2022-10-29 thomas size_t i;
506 c902213d 2022-10-29 thomas
507 c902213d 2022-10-29 thomas memset(&iclient, 0, sizeof(iclient));
508 c902213d 2022-10-29 thomas iclient.euid = client->euid;
509 c902213d 2022-10-29 thomas iclient.egid = client->egid;
510 c902213d 2022-10-29 thomas
511 c902213d 2022-10-29 thomas proc = get_client_proc(client);
512 c902213d 2022-10-29 thomas if (proc) {
513 414e37cb 2022-12-30 thomas if (strlcpy(iclient.repo_name, proc->repo_path,
514 c902213d 2022-10-29 thomas sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
515 c902213d 2022-10-29 thomas return got_error_msg(GOT_ERR_NO_SPACE,
516 c902213d 2022-10-29 thomas "repo name too long");
517 c902213d 2022-10-29 thomas }
518 c902213d 2022-10-29 thomas if (client_is_writing(client))
519 c902213d 2022-10-29 thomas iclient.is_writing = 1;
520 c902213d 2022-10-29 thomas }
521 c902213d 2022-10-29 thomas
522 c902213d 2022-10-29 thomas iclient.state = client->state;
523 c902213d 2022-10-29 thomas iclient.ncapabilities = client->ncapabilities;
524 c902213d 2022-10-29 thomas
525 c902213d 2022-10-29 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
526 c902213d 2022-10-29 thomas &iclient, sizeof(iclient)) == -1) {
527 c902213d 2022-10-29 thomas err = got_error_from_errno("imsg compose INFO_CLIENT");
528 c902213d 2022-10-29 thomas if (err)
529 c902213d 2022-10-29 thomas return err;
530 c902213d 2022-10-29 thomas }
531 c902213d 2022-10-29 thomas
532 c902213d 2022-10-29 thomas for (i = 0; i < client->ncapabilities; i++) {
533 c902213d 2022-10-29 thomas struct gotd_client_capability *capa;
534 c902213d 2022-10-29 thomas capa = &client->capabilities[i];
535 c902213d 2022-10-29 thomas err = send_capability(capa, iev);
536 c902213d 2022-10-29 thomas if (err)
537 c902213d 2022-10-29 thomas return err;
538 c902213d 2022-10-29 thomas }
539 c902213d 2022-10-29 thomas
540 c902213d 2022-10-29 thomas return NULL;
541 c902213d 2022-10-29 thomas }
542 c902213d 2022-10-29 thomas
543 c902213d 2022-10-29 thomas static const struct got_error *
544 c902213d 2022-10-29 thomas send_info(struct gotd_client *client)
545 c902213d 2022-10-29 thomas {
546 c902213d 2022-10-29 thomas const struct got_error *err = NULL;
547 c902213d 2022-10-29 thomas struct gotd_imsg_info info;
548 c902213d 2022-10-29 thomas uint64_t slot;
549 c902213d 2022-10-29 thomas struct gotd_repo *repo;
550 c902213d 2022-10-29 thomas
551 c902213d 2022-10-29 thomas info.pid = gotd.pid;
552 c902213d 2022-10-29 thomas info.verbosity = gotd.verbosity;
553 c902213d 2022-10-29 thomas info.nrepos = gotd.nrepos;
554 c902213d 2022-10-29 thomas info.nclients = client_cnt - 1;
555 c902213d 2022-10-29 thomas
556 c902213d 2022-10-29 thomas if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
557 c902213d 2022-10-29 thomas &info, sizeof(info)) == -1) {
558 c902213d 2022-10-29 thomas err = got_error_from_errno("imsg compose INFO");
559 c902213d 2022-10-29 thomas if (err)
560 c902213d 2022-10-29 thomas return err;
561 c902213d 2022-10-29 thomas }
562 c902213d 2022-10-29 thomas
563 c902213d 2022-10-29 thomas TAILQ_FOREACH(repo, &gotd.repos, entry) {
564 c902213d 2022-10-29 thomas err = send_repo_info(&client->iev, repo);
565 c902213d 2022-10-29 thomas if (err)
566 c902213d 2022-10-29 thomas return err;
567 c902213d 2022-10-29 thomas }
568 c902213d 2022-10-29 thomas
569 c902213d 2022-10-29 thomas for (slot = 0; slot < nitems(gotd_clients); slot++) {
570 c902213d 2022-10-29 thomas struct gotd_client *c;
571 c902213d 2022-10-29 thomas STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
572 c902213d 2022-10-29 thomas if (c->id == client->id)
573 c902213d 2022-10-29 thomas continue;
574 c902213d 2022-10-29 thomas err = send_client_info(&client->iev, c);
575 c902213d 2022-10-29 thomas if (err)
576 c902213d 2022-10-29 thomas return err;
577 c902213d 2022-10-29 thomas }
578 c902213d 2022-10-29 thomas }
579 c902213d 2022-10-29 thomas
580 c902213d 2022-10-29 thomas return NULL;
581 c902213d 2022-10-29 thomas }
582 c902213d 2022-10-29 thomas
583 c902213d 2022-10-29 thomas static const struct got_error *
584 c902213d 2022-10-29 thomas stop_gotd(struct gotd_client *client)
585 c902213d 2022-10-29 thomas {
586 c902213d 2022-10-29 thomas
587 c902213d 2022-10-29 thomas if (client->euid != 0)
588 c902213d 2022-10-29 thomas return got_error_set_errno(EPERM, "stop");
589 c902213d 2022-10-29 thomas
590 c902213d 2022-10-29 thomas gotd_shutdown();
591 c902213d 2022-10-29 thomas /* NOTREACHED */
592 729a7e24 2022-11-17 thomas return NULL;
593 729a7e24 2022-11-17 thomas }
594 729a7e24 2022-11-17 thomas
595 729a7e24 2022-11-17 thomas static struct gotd_repo *
596 729a7e24 2022-11-17 thomas find_repo_by_name(const char *repo_name)
597 729a7e24 2022-11-17 thomas {
598 729a7e24 2022-11-17 thomas struct gotd_repo *repo;
599 729a7e24 2022-11-17 thomas size_t namelen;
600 729a7e24 2022-11-17 thomas
601 729a7e24 2022-11-17 thomas TAILQ_FOREACH(repo, &gotd.repos, entry) {
602 729a7e24 2022-11-17 thomas namelen = strlen(repo->name);
603 729a7e24 2022-11-17 thomas if (strncmp(repo->name, repo_name, namelen) != 0)
604 729a7e24 2022-11-17 thomas continue;
605 729a7e24 2022-11-17 thomas if (repo_name[namelen] == '\0' ||
606 729a7e24 2022-11-17 thomas strcmp(&repo_name[namelen], ".git") == 0)
607 729a7e24 2022-11-17 thomas return repo;
608 3efd8e31 2022-10-23 thomas }
609 3efd8e31 2022-10-23 thomas
610 3efd8e31 2022-10-23 thomas return NULL;
611 3efd8e31 2022-10-23 thomas }
612 3efd8e31 2022-10-23 thomas
613 3efd8e31 2022-10-23 thomas static const struct got_error *
614 85b37c72 2022-12-30 thomas start_client_session(struct gotd_client *client, struct imsg *imsg)
615 3efd8e31 2022-10-23 thomas {
616 3efd8e31 2022-10-23 thomas const struct got_error *err;
617 3efd8e31 2022-10-23 thomas struct gotd_imsg_list_refs ireq;
618 729a7e24 2022-11-17 thomas struct gotd_repo *repo = NULL;
619 3efd8e31 2022-10-23 thomas size_t datalen;
620 3efd8e31 2022-10-23 thomas
621 3efd8e31 2022-10-23 thomas log_debug("list-refs request from uid %d", client->euid);
622 3efd8e31 2022-10-23 thomas
623 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
624 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
625 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
626 3efd8e31 2022-10-23 thomas
627 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, datalen);
628 3efd8e31 2022-10-23 thomas
629 3efd8e31 2022-10-23 thomas if (ireq.client_is_reading) {
630 3efd8e31 2022-10-23 thomas err = ensure_client_is_not_writing(client);
631 3efd8e31 2022-10-23 thomas if (err)
632 3efd8e31 2022-10-23 thomas return err;
633 729a7e24 2022-11-17 thomas repo = find_repo_by_name(ireq.repo_name);
634 729a7e24 2022-11-17 thomas if (repo == NULL)
635 729a7e24 2022-11-17 thomas return got_error(GOT_ERR_NOT_GIT_REPO);
636 c669c489 2022-12-30 thomas err = start_auth_child(client, GOTD_AUTH_READ, repo,
637 85b37c72 2022-12-30 thomas gotd.argv0, gotd.confpath, gotd.daemonize,
638 85b37c72 2022-12-30 thomas gotd.verbosity);
639 85b37c72 2022-12-30 thomas if (err)
640 85b37c72 2022-12-30 thomas return err;
641 3efd8e31 2022-10-23 thomas } else {
642 3efd8e31 2022-10-23 thomas err = ensure_client_is_not_reading(client);
643 729a7e24 2022-11-17 thomas if (err)
644 729a7e24 2022-11-17 thomas return err;
645 729a7e24 2022-11-17 thomas repo = find_repo_by_name(ireq.repo_name);
646 729a7e24 2022-11-17 thomas if (repo == NULL)
647 729a7e24 2022-11-17 thomas return got_error(GOT_ERR_NOT_GIT_REPO);
648 c669c489 2022-12-30 thomas err = start_auth_child(client,
649 c669c489 2022-12-30 thomas GOTD_AUTH_READ | GOTD_AUTH_WRITE,
650 c669c489 2022-12-30 thomas repo, gotd.argv0, gotd.confpath, gotd.daemonize,
651 85b37c72 2022-12-30 thomas gotd.verbosity);
652 85b37c72 2022-12-30 thomas if (err)
653 85b37c72 2022-12-30 thomas return err;
654 3efd8e31 2022-10-23 thomas }
655 3efd8e31 2022-10-23 thomas
656 c669c489 2022-12-30 thomas /* Flow continues upon authentication successs/failure or timeout. */
657 3efd8e31 2022-10-23 thomas return NULL;
658 3efd8e31 2022-10-23 thomas }
659 3efd8e31 2022-10-23 thomas
660 3efd8e31 2022-10-23 thomas static const struct got_error *
661 3efd8e31 2022-10-23 thomas forward_want(struct gotd_client *client, struct imsg *imsg)
662 3efd8e31 2022-10-23 thomas {
663 3efd8e31 2022-10-23 thomas struct gotd_imsg_want ireq;
664 3efd8e31 2022-10-23 thomas struct gotd_imsg_want iwant;
665 3efd8e31 2022-10-23 thomas size_t datalen;
666 3efd8e31 2022-10-23 thomas
667 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
668 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
669 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
670 3efd8e31 2022-10-23 thomas
671 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, datalen);
672 3efd8e31 2022-10-23 thomas
673 3efd8e31 2022-10-23 thomas memset(&iwant, 0, sizeof(iwant));
674 3efd8e31 2022-10-23 thomas memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
675 3efd8e31 2022-10-23 thomas iwant.client_id = client->id;
676 3efd8e31 2022-10-23 thomas
677 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_WANT,
678 3efd8e31 2022-10-23 thomas PROC_GOTD, -1, &iwant, sizeof(iwant)) == -1)
679 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg compose WANT");
680 3efd8e31 2022-10-23 thomas
681 3efd8e31 2022-10-23 thomas return NULL;
682 3efd8e31 2022-10-23 thomas }
683 3efd8e31 2022-10-23 thomas
684 3efd8e31 2022-10-23 thomas static const struct got_error *
685 3efd8e31 2022-10-23 thomas forward_ref_update(struct gotd_client *client, struct imsg *imsg)
686 3efd8e31 2022-10-23 thomas {
687 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
688 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update ireq;
689 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update *iref = NULL;
690 3efd8e31 2022-10-23 thomas size_t datalen;
691 3efd8e31 2022-10-23 thomas
692 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
693 3efd8e31 2022-10-23 thomas if (datalen < sizeof(ireq))
694 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
695 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
696 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq) + ireq.name_len)
697 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
698 3efd8e31 2022-10-23 thomas
699 3efd8e31 2022-10-23 thomas iref = malloc(datalen);
700 3efd8e31 2022-10-23 thomas if (iref == NULL)
701 3efd8e31 2022-10-23 thomas return got_error_from_errno("malloc");
702 3efd8e31 2022-10-23 thomas memcpy(iref, imsg->data, datalen);
703 3efd8e31 2022-10-23 thomas
704 3efd8e31 2022-10-23 thomas iref->client_id = client->id;
705 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_write->iev,
706 3efd8e31 2022-10-23 thomas GOTD_IMSG_REF_UPDATE, PROC_GOTD, -1, iref, datalen) == -1)
707 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose REF_UPDATE");
708 3efd8e31 2022-10-23 thomas free(iref);
709 3efd8e31 2022-10-23 thomas return err;
710 3efd8e31 2022-10-23 thomas }
711 3efd8e31 2022-10-23 thomas
712 3efd8e31 2022-10-23 thomas static const struct got_error *
713 3efd8e31 2022-10-23 thomas forward_have(struct gotd_client *client, struct imsg *imsg)
714 3efd8e31 2022-10-23 thomas {
715 3efd8e31 2022-10-23 thomas struct gotd_imsg_have ireq;
716 3efd8e31 2022-10-23 thomas struct gotd_imsg_have ihave;
717 3efd8e31 2022-10-23 thomas size_t datalen;
718 3efd8e31 2022-10-23 thomas
719 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
720 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
721 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
722 3efd8e31 2022-10-23 thomas
723 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, datalen);
724 3efd8e31 2022-10-23 thomas
725 3efd8e31 2022-10-23 thomas memset(&ihave, 0, sizeof(ihave));
726 3efd8e31 2022-10-23 thomas memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
727 3efd8e31 2022-10-23 thomas ihave.client_id = client->id;
728 3efd8e31 2022-10-23 thomas
729 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_HAVE,
730 3efd8e31 2022-10-23 thomas PROC_GOTD, -1, &ihave, sizeof(ihave)) == -1)
731 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg compose HAVE");
732 3efd8e31 2022-10-23 thomas
733 3efd8e31 2022-10-23 thomas return NULL;
734 3efd8e31 2022-10-23 thomas }
735 3efd8e31 2022-10-23 thomas
736 3efd8e31 2022-10-23 thomas static int
737 3efd8e31 2022-10-23 thomas client_has_capability(struct gotd_client *client, const char *capastr)
738 3efd8e31 2022-10-23 thomas {
739 3efd8e31 2022-10-23 thomas struct gotd_client_capability *capa;
740 3efd8e31 2022-10-23 thomas size_t i;
741 3efd8e31 2022-10-23 thomas
742 3efd8e31 2022-10-23 thomas if (client->ncapabilities == 0)
743 3efd8e31 2022-10-23 thomas return 0;
744 3efd8e31 2022-10-23 thomas
745 3efd8e31 2022-10-23 thomas for (i = 0; i < client->ncapabilities; i++) {
746 3efd8e31 2022-10-23 thomas capa = &client->capabilities[i];
747 3efd8e31 2022-10-23 thomas if (strcmp(capa->key, capastr) == 0)
748 3efd8e31 2022-10-23 thomas return 1;
749 3efd8e31 2022-10-23 thomas }
750 3efd8e31 2022-10-23 thomas
751 3efd8e31 2022-10-23 thomas return 0;
752 3efd8e31 2022-10-23 thomas }
753 3efd8e31 2022-10-23 thomas
754 3efd8e31 2022-10-23 thomas static const struct got_error *
755 3efd8e31 2022-10-23 thomas recv_capabilities(struct gotd_client *client, struct imsg *imsg)
756 3efd8e31 2022-10-23 thomas {
757 3efd8e31 2022-10-23 thomas struct gotd_imsg_capabilities icapas;
758 3efd8e31 2022-10-23 thomas size_t datalen;
759 3efd8e31 2022-10-23 thomas
760 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
761 3efd8e31 2022-10-23 thomas if (datalen != sizeof(icapas))
762 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
763 3efd8e31 2022-10-23 thomas memcpy(&icapas, imsg->data, sizeof(icapas));
764 3efd8e31 2022-10-23 thomas
765 3efd8e31 2022-10-23 thomas client->ncapa_alloc = icapas.ncapabilities;
766 3efd8e31 2022-10-23 thomas client->capabilities = calloc(client->ncapa_alloc,
767 3efd8e31 2022-10-23 thomas sizeof(*client->capabilities));
768 3efd8e31 2022-10-23 thomas if (client->capabilities == NULL) {
769 3efd8e31 2022-10-23 thomas client->ncapa_alloc = 0;
770 3efd8e31 2022-10-23 thomas return got_error_from_errno("calloc");
771 3efd8e31 2022-10-23 thomas }
772 3efd8e31 2022-10-23 thomas
773 3efd8e31 2022-10-23 thomas log_debug("expecting %zu capabilities from uid %d",
774 3efd8e31 2022-10-23 thomas client->ncapa_alloc, client->euid);
775 3efd8e31 2022-10-23 thomas return NULL;
776 3efd8e31 2022-10-23 thomas }
777 3efd8e31 2022-10-23 thomas
778 3efd8e31 2022-10-23 thomas static const struct got_error *
779 3efd8e31 2022-10-23 thomas recv_capability(struct gotd_client *client, struct imsg *imsg)
780 3efd8e31 2022-10-23 thomas {
781 3efd8e31 2022-10-23 thomas struct gotd_imsg_capability icapa;
782 3efd8e31 2022-10-23 thomas struct gotd_client_capability *capa;
783 3efd8e31 2022-10-23 thomas size_t datalen;
784 3efd8e31 2022-10-23 thomas char *key, *value = NULL;
785 3efd8e31 2022-10-23 thomas
786 3efd8e31 2022-10-23 thomas if (client->capabilities == NULL ||
787 3efd8e31 2022-10-23 thomas client->ncapabilities >= client->ncapa_alloc) {
788 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
789 3efd8e31 2022-10-23 thomas "unexpected capability received");
790 3efd8e31 2022-10-23 thomas }
791 3efd8e31 2022-10-23 thomas
792 3efd8e31 2022-10-23 thomas memset(&icapa, 0, sizeof(icapa));
793 3efd8e31 2022-10-23 thomas
794 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
795 3efd8e31 2022-10-23 thomas if (datalen < sizeof(icapa))
796 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
797 3efd8e31 2022-10-23 thomas memcpy(&icapa, imsg->data, sizeof(icapa));
798 3efd8e31 2022-10-23 thomas
799 3efd8e31 2022-10-23 thomas if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
800 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
801 3efd8e31 2022-10-23 thomas
802 3efd8e31 2022-10-23 thomas key = malloc(icapa.key_len + 1);
803 3efd8e31 2022-10-23 thomas if (key == NULL)
804 3efd8e31 2022-10-23 thomas return got_error_from_errno("malloc");
805 3efd8e31 2022-10-23 thomas if (icapa.value_len > 0) {
806 3efd8e31 2022-10-23 thomas value = malloc(icapa.value_len + 1);
807 3efd8e31 2022-10-23 thomas if (value == NULL) {
808 3efd8e31 2022-10-23 thomas free(key);
809 3efd8e31 2022-10-23 thomas return got_error_from_errno("malloc");
810 3efd8e31 2022-10-23 thomas }
811 3efd8e31 2022-10-23 thomas }
812 3efd8e31 2022-10-23 thomas
813 3efd8e31 2022-10-23 thomas memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
814 3efd8e31 2022-10-23 thomas key[icapa.key_len] = '\0';
815 3efd8e31 2022-10-23 thomas if (value) {
816 3efd8e31 2022-10-23 thomas memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
817 3efd8e31 2022-10-23 thomas icapa.value_len);
818 3efd8e31 2022-10-23 thomas value[icapa.value_len] = '\0';
819 3efd8e31 2022-10-23 thomas }
820 3efd8e31 2022-10-23 thomas
821 3efd8e31 2022-10-23 thomas capa = &client->capabilities[client->ncapabilities++];
822 3efd8e31 2022-10-23 thomas capa->key = key;
823 3efd8e31 2022-10-23 thomas capa->value = value;
824 3efd8e31 2022-10-23 thomas
825 3efd8e31 2022-10-23 thomas if (value)
826 3efd8e31 2022-10-23 thomas log_debug("uid %d: capability %s=%s", client->euid, key, value);
827 3efd8e31 2022-10-23 thomas else
828 3efd8e31 2022-10-23 thomas log_debug("uid %d: capability %s", client->euid, key);
829 3efd8e31 2022-10-23 thomas
830 3efd8e31 2022-10-23 thomas return NULL;
831 3efd8e31 2022-10-23 thomas }
832 3efd8e31 2022-10-23 thomas
833 3efd8e31 2022-10-23 thomas static const struct got_error *
834 3efd8e31 2022-10-23 thomas send_packfile(struct gotd_client *client)
835 3efd8e31 2022-10-23 thomas {
836 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
837 3efd8e31 2022-10-23 thomas struct gotd_imsg_send_packfile ipack;
838 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_pipe ipipe;
839 3efd8e31 2022-10-23 thomas int pipe[2];
840 3efd8e31 2022-10-23 thomas
841 3efd8e31 2022-10-23 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
842 3efd8e31 2022-10-23 thomas return got_error_from_errno("socketpair");
843 3efd8e31 2022-10-23 thomas
844 3efd8e31 2022-10-23 thomas memset(&ipack, 0, sizeof(ipack));
845 3efd8e31 2022-10-23 thomas memset(&ipipe, 0, sizeof(ipipe));
846 3efd8e31 2022-10-23 thomas
847 3efd8e31 2022-10-23 thomas ipack.client_id = client->id;
848 3efd8e31 2022-10-23 thomas if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
849 3efd8e31 2022-10-23 thomas ipack.report_progress = 1;
850 3efd8e31 2022-10-23 thomas
851 3efd8e31 2022-10-23 thomas client->delta_cache_fd = got_opentempfd();
852 3efd8e31 2022-10-23 thomas if (client->delta_cache_fd == -1)
853 3efd8e31 2022-10-23 thomas return got_error_from_errno("got_opentempfd");
854 3efd8e31 2022-10-23 thomas
855 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_read->iev,
856 3efd8e31 2022-10-23 thomas GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
857 3efd8e31 2022-10-23 thomas &ipack, sizeof(ipack)) == -1) {
858 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose SEND_PACKFILE");
859 3efd8e31 2022-10-23 thomas close(pipe[0]);
860 3efd8e31 2022-10-23 thomas close(pipe[1]);
861 3efd8e31 2022-10-23 thomas return err;
862 3efd8e31 2022-10-23 thomas }
863 3efd8e31 2022-10-23 thomas
864 3efd8e31 2022-10-23 thomas ipipe.client_id = client->id;
865 3efd8e31 2022-10-23 thomas
866 cc4cc677 2022-10-28 thomas /* Send pack pipe end 0 to repo_read. */
867 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_read->iev,
868 3efd8e31 2022-10-23 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
869 3efd8e31 2022-10-23 thomas &ipipe, sizeof(ipipe)) == -1) {
870 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
871 3efd8e31 2022-10-23 thomas close(pipe[1]);
872 3efd8e31 2022-10-23 thomas return err;
873 3efd8e31 2022-10-23 thomas }
874 3efd8e31 2022-10-23 thomas
875 cc4cc677 2022-10-28 thomas /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
876 cc4cc677 2022-10-28 thomas if (gotd_imsg_compose_event(&client->iev,
877 cc4cc677 2022-10-28 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
878 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
879 3efd8e31 2022-10-23 thomas
880 3efd8e31 2022-10-23 thomas return err;
881 3efd8e31 2022-10-23 thomas }
882 3efd8e31 2022-10-23 thomas
883 3efd8e31 2022-10-23 thomas static const struct got_error *
884 3efd8e31 2022-10-23 thomas recv_packfile(struct gotd_client *client)
885 3efd8e31 2022-10-23 thomas {
886 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
887 3efd8e31 2022-10-23 thomas struct gotd_imsg_recv_packfile ipack;
888 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_pipe ipipe;
889 3efd8e31 2022-10-23 thomas struct gotd_imsg_packidx_file ifile;
890 3efd8e31 2022-10-23 thomas char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
891 3efd8e31 2022-10-23 thomas int packfd = -1, idxfd = -1;
892 3efd8e31 2022-10-23 thomas int pipe[2] = { -1, -1 };
893 3efd8e31 2022-10-23 thomas
894 3efd8e31 2022-10-23 thomas if (client->packfile_path) {
895 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
896 3efd8e31 2022-10-23 thomas "uid %d already has a pack file", client->euid);
897 3efd8e31 2022-10-23 thomas }
898 3efd8e31 2022-10-23 thomas
899 3efd8e31 2022-10-23 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
900 3efd8e31 2022-10-23 thomas return got_error_from_errno("socketpair");
901 3efd8e31 2022-10-23 thomas
902 3efd8e31 2022-10-23 thomas memset(&ipipe, 0, sizeof(ipipe));
903 3efd8e31 2022-10-23 thomas ipipe.client_id = client->id;
904 3efd8e31 2022-10-23 thomas
905 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_write->iev,
906 3efd8e31 2022-10-23 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
907 3efd8e31 2022-10-23 thomas &ipipe, sizeof(ipipe)) == -1) {
908 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
909 3efd8e31 2022-10-23 thomas pipe[0] = -1;
910 3efd8e31 2022-10-23 thomas goto done;
911 3efd8e31 2022-10-23 thomas }
912 3efd8e31 2022-10-23 thomas pipe[0] = -1;
913 3efd8e31 2022-10-23 thomas
914 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_write->iev,
915 3efd8e31 2022-10-23 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1],
916 3efd8e31 2022-10-23 thomas &ipipe, sizeof(ipipe)) == -1)
917 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
918 3efd8e31 2022-10-23 thomas pipe[1] = -1;
919 3efd8e31 2022-10-23 thomas
920 3efd8e31 2022-10-23 thomas if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
921 414e37cb 2022-12-30 thomas client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
922 3efd8e31 2022-10-23 thomas client->euid) == -1) {
923 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
924 3efd8e31 2022-10-23 thomas goto done;
925 3efd8e31 2022-10-23 thomas }
926 3efd8e31 2022-10-23 thomas
927 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
928 3efd8e31 2022-10-23 thomas if (err)
929 3efd8e31 2022-10-23 thomas goto done;
930 3efd8e31 2022-10-23 thomas
931 3efd8e31 2022-10-23 thomas free(basepath);
932 3efd8e31 2022-10-23 thomas if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
933 414e37cb 2022-12-30 thomas client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
934 3efd8e31 2022-10-23 thomas client->euid) == -1) {
935 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
936 3efd8e31 2022-10-23 thomas basepath = NULL;
937 3efd8e31 2022-10-23 thomas goto done;
938 3efd8e31 2022-10-23 thomas }
939 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
940 3efd8e31 2022-10-23 thomas if (err)
941 3efd8e31 2022-10-23 thomas goto done;
942 3efd8e31 2022-10-23 thomas
943 3efd8e31 2022-10-23 thomas memset(&ifile, 0, sizeof(ifile));
944 3efd8e31 2022-10-23 thomas ifile.client_id = client->id;
945 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_write->iev,
946 3efd8e31 2022-10-23 thomas GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
947 3efd8e31 2022-10-23 thomas &ifile, sizeof(ifile)) == -1) {
948 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose PACKIDX_FILE");
949 3efd8e31 2022-10-23 thomas idxfd = -1;
950 3efd8e31 2022-10-23 thomas goto done;
951 3efd8e31 2022-10-23 thomas }
952 3efd8e31 2022-10-23 thomas idxfd = -1;
953 3efd8e31 2022-10-23 thomas
954 3efd8e31 2022-10-23 thomas memset(&ipack, 0, sizeof(ipack));
955 3efd8e31 2022-10-23 thomas ipack.client_id = client->id;
956 3efd8e31 2022-10-23 thomas if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
957 3efd8e31 2022-10-23 thomas ipack.report_status = 1;
958 3efd8e31 2022-10-23 thomas
959 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_write->iev,
960 3efd8e31 2022-10-23 thomas GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
961 3efd8e31 2022-10-23 thomas &ipack, sizeof(ipack)) == -1) {
962 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose RECV_PACKFILE");
963 3efd8e31 2022-10-23 thomas packfd = -1;
964 3efd8e31 2022-10-23 thomas goto done;
965 3efd8e31 2022-10-23 thomas }
966 3efd8e31 2022-10-23 thomas packfd = -1;
967 3efd8e31 2022-10-23 thomas
968 3efd8e31 2022-10-23 thomas done:
969 3efd8e31 2022-10-23 thomas free(basepath);
970 3efd8e31 2022-10-23 thomas if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
971 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
972 3efd8e31 2022-10-23 thomas if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
973 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
974 3efd8e31 2022-10-23 thomas if (packfd != -1 && close(packfd) == -1 && err == NULL)
975 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
976 3efd8e31 2022-10-23 thomas if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
977 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
978 3efd8e31 2022-10-23 thomas if (err) {
979 3efd8e31 2022-10-23 thomas free(pack_path);
980 3efd8e31 2022-10-23 thomas free(idx_path);
981 3efd8e31 2022-10-23 thomas } else {
982 3efd8e31 2022-10-23 thomas client->packfile_path = pack_path;
983 3efd8e31 2022-10-23 thomas client->packidx_path = idx_path;
984 3efd8e31 2022-10-23 thomas }
985 3efd8e31 2022-10-23 thomas return err;
986 3efd8e31 2022-10-23 thomas }
987 3efd8e31 2022-10-23 thomas
988 3efd8e31 2022-10-23 thomas static void
989 3efd8e31 2022-10-23 thomas gotd_request(int fd, short events, void *arg)
990 3efd8e31 2022-10-23 thomas {
991 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev = arg;
992 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf = &iev->ibuf;
993 3efd8e31 2022-10-23 thomas struct gotd_client *client = iev->handler_arg;
994 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
995 3efd8e31 2022-10-23 thomas struct imsg imsg;
996 3efd8e31 2022-10-23 thomas ssize_t n;
997 3efd8e31 2022-10-23 thomas
998 3efd8e31 2022-10-23 thomas if (events & EV_WRITE) {
999 3efd8e31 2022-10-23 thomas while (ibuf->w.queued) {
1000 3efd8e31 2022-10-23 thomas n = msgbuf_write(&ibuf->w);
1001 3efd8e31 2022-10-23 thomas if (n == -1 && errno == EPIPE) {
1002 3efd8e31 2022-10-23 thomas /*
1003 3efd8e31 2022-10-23 thomas * The client has closed its socket.
1004 3efd8e31 2022-10-23 thomas * This can happen when Git clients are
1005 3efd8e31 2022-10-23 thomas * done sending pack file data.
1006 16373356 2023-01-02 thomas */
1007 3efd8e31 2022-10-23 thomas msgbuf_clear(&ibuf->w);
1008 3efd8e31 2022-10-23 thomas continue;
1009 3efd8e31 2022-10-23 thomas } else if (n == -1 && errno != EAGAIN) {
1010 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_flush");
1011 3efd8e31 2022-10-23 thomas disconnect_on_error(client, err);
1012 3efd8e31 2022-10-23 thomas return;
1013 3efd8e31 2022-10-23 thomas }
1014 3efd8e31 2022-10-23 thomas if (n == 0) {
1015 3efd8e31 2022-10-23 thomas /* Connection closed. */
1016 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_EOF);
1017 3efd8e31 2022-10-23 thomas disconnect_on_error(client, err);
1018 3efd8e31 2022-10-23 thomas return;
1019 3efd8e31 2022-10-23 thomas }
1020 3efd8e31 2022-10-23 thomas }
1021 c902213d 2022-10-29 thomas
1022 c902213d 2022-10-29 thomas /* Disconnect gotctl(8) now that messages have been sent. */
1023 c902213d 2022-10-29 thomas if (!client_is_reading(client) && !client_is_writing(client)) {
1024 c902213d 2022-10-29 thomas disconnect(client);
1025 c902213d 2022-10-29 thomas return;
1026 c902213d 2022-10-29 thomas }
1027 3efd8e31 2022-10-23 thomas }
1028 3efd8e31 2022-10-23 thomas
1029 3efd8e31 2022-10-23 thomas if ((events & EV_READ) == 0)
1030 3efd8e31 2022-10-23 thomas return;
1031 3efd8e31 2022-10-23 thomas
1032 3efd8e31 2022-10-23 thomas memset(&imsg, 0, sizeof(imsg));
1033 3efd8e31 2022-10-23 thomas
1034 3efd8e31 2022-10-23 thomas while (err == NULL) {
1035 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv(&imsg, ibuf, 0);
1036 3efd8e31 2022-10-23 thomas if (err) {
1037 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_PRIVSEP_READ)
1038 3efd8e31 2022-10-23 thomas err = NULL;
1039 3efd8e31 2022-10-23 thomas break;
1040 3efd8e31 2022-10-23 thomas }
1041 3efd8e31 2022-10-23 thomas
1042 3efd8e31 2022-10-23 thomas evtimer_del(&client->tmo);
1043 3efd8e31 2022-10-23 thomas
1044 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
1045 c902213d 2022-10-29 thomas case GOTD_IMSG_INFO:
1046 c902213d 2022-10-29 thomas err = send_info(client);
1047 c902213d 2022-10-29 thomas break;
1048 c902213d 2022-10-29 thomas case GOTD_IMSG_STOP:
1049 c902213d 2022-10-29 thomas err = stop_gotd(client);
1050 c902213d 2022-10-29 thomas break;
1051 3efd8e31 2022-10-23 thomas case GOTD_IMSG_LIST_REFS:
1052 3efd8e31 2022-10-23 thomas if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1053 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1054 3efd8e31 2022-10-23 thomas "unexpected list-refs request received");
1055 3efd8e31 2022-10-23 thomas break;
1056 3efd8e31 2022-10-23 thomas }
1057 85b37c72 2022-12-30 thomas err = start_client_session(client, &imsg);
1058 3efd8e31 2022-10-23 thomas if (err)
1059 3efd8e31 2022-10-23 thomas break;
1060 3efd8e31 2022-10-23 thomas break;
1061 3efd8e31 2022-10-23 thomas case GOTD_IMSG_CAPABILITIES:
1062 3efd8e31 2022-10-23 thomas if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1063 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1064 3efd8e31 2022-10-23 thomas "unexpected capabilities received");
1065 3efd8e31 2022-10-23 thomas break;
1066 3efd8e31 2022-10-23 thomas }
1067 3efd8e31 2022-10-23 thomas log_debug("receiving capabilities from uid %d",
1068 3efd8e31 2022-10-23 thomas client->euid);
1069 3efd8e31 2022-10-23 thomas err = recv_capabilities(client, &imsg);
1070 3efd8e31 2022-10-23 thomas break;
1071 3efd8e31 2022-10-23 thomas case GOTD_IMSG_CAPABILITY:
1072 3efd8e31 2022-10-23 thomas if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1073 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1074 3efd8e31 2022-10-23 thomas "unexpected capability received");
1075 3efd8e31 2022-10-23 thomas break;
1076 3efd8e31 2022-10-23 thomas }
1077 3efd8e31 2022-10-23 thomas err = recv_capability(client, &imsg);
1078 3efd8e31 2022-10-23 thomas if (err || client->ncapabilities < client->ncapa_alloc)
1079 3efd8e31 2022-10-23 thomas break;
1080 3efd8e31 2022-10-23 thomas if (client_is_reading(client)) {
1081 3efd8e31 2022-10-23 thomas client->state = GOTD_STATE_EXPECT_WANT;
1082 3efd8e31 2022-10-23 thomas log_debug("uid %d: expecting want-lines",
1083 3efd8e31 2022-10-23 thomas client->euid);
1084 3efd8e31 2022-10-23 thomas } else if (client_is_writing(client)) {
1085 3efd8e31 2022-10-23 thomas client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1086 3efd8e31 2022-10-23 thomas log_debug("uid %d: expecting ref-update-lines",
1087 3efd8e31 2022-10-23 thomas client->euid);
1088 3efd8e31 2022-10-23 thomas } else
1089 3efd8e31 2022-10-23 thomas fatalx("client %d is both reading and writing",
1090 3efd8e31 2022-10-23 thomas client->euid);
1091 3efd8e31 2022-10-23 thomas break;
1092 3efd8e31 2022-10-23 thomas case GOTD_IMSG_WANT:
1093 3efd8e31 2022-10-23 thomas if (client->state != GOTD_STATE_EXPECT_WANT) {
1094 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1095 3efd8e31 2022-10-23 thomas "unexpected want-line received");
1096 3efd8e31 2022-10-23 thomas break;
1097 3efd8e31 2022-10-23 thomas }
1098 3efd8e31 2022-10-23 thomas log_debug("received want-line from uid %d",
1099 3efd8e31 2022-10-23 thomas client->euid);
1100 3efd8e31 2022-10-23 thomas err = ensure_client_is_reading(client);
1101 3efd8e31 2022-10-23 thomas if (err)
1102 3efd8e31 2022-10-23 thomas break;
1103 3efd8e31 2022-10-23 thomas err = forward_want(client, &imsg);
1104 3efd8e31 2022-10-23 thomas break;
1105 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATE:
1106 3efd8e31 2022-10-23 thomas if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1107 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1108 3efd8e31 2022-10-23 thomas "unexpected ref-update-line received");
1109 3efd8e31 2022-10-23 thomas break;
1110 3efd8e31 2022-10-23 thomas }
1111 3efd8e31 2022-10-23 thomas log_debug("received ref-update-line from uid %d",
1112 3efd8e31 2022-10-23 thomas client->euid);
1113 3efd8e31 2022-10-23 thomas err = ensure_client_is_writing(client);
1114 3efd8e31 2022-10-23 thomas if (err)
1115 3efd8e31 2022-10-23 thomas break;
1116 3efd8e31 2022-10-23 thomas err = forward_ref_update(client, &imsg);
1117 3efd8e31 2022-10-23 thomas if (err)
1118 3efd8e31 2022-10-23 thomas break;
1119 3efd8e31 2022-10-23 thomas client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1120 3efd8e31 2022-10-23 thomas break;
1121 3efd8e31 2022-10-23 thomas case GOTD_IMSG_HAVE:
1122 3efd8e31 2022-10-23 thomas if (client->state != GOTD_STATE_EXPECT_HAVE) {
1123 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1124 3efd8e31 2022-10-23 thomas "unexpected have-line received");
1125 3efd8e31 2022-10-23 thomas break;
1126 3efd8e31 2022-10-23 thomas }
1127 3efd8e31 2022-10-23 thomas log_debug("received have-line from uid %d",
1128 3efd8e31 2022-10-23 thomas client->euid);
1129 3efd8e31 2022-10-23 thomas err = ensure_client_is_reading(client);
1130 3efd8e31 2022-10-23 thomas if (err)
1131 3efd8e31 2022-10-23 thomas break;
1132 3efd8e31 2022-10-23 thomas err = forward_have(client, &imsg);
1133 3efd8e31 2022-10-23 thomas if (err)
1134 3efd8e31 2022-10-23 thomas break;
1135 3efd8e31 2022-10-23 thomas break;
1136 3efd8e31 2022-10-23 thomas case GOTD_IMSG_FLUSH:
1137 3efd8e31 2022-10-23 thomas if (client->state == GOTD_STATE_EXPECT_WANT ||
1138 3efd8e31 2022-10-23 thomas client->state == GOTD_STATE_EXPECT_HAVE) {
1139 3efd8e31 2022-10-23 thomas err = ensure_client_is_reading(client);
1140 3efd8e31 2022-10-23 thomas if (err)
1141 3efd8e31 2022-10-23 thomas break;
1142 3efd8e31 2022-10-23 thomas } else if (client->state ==
1143 3efd8e31 2022-10-23 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1144 3efd8e31 2022-10-23 thomas err = ensure_client_is_writing(client);
1145 3efd8e31 2022-10-23 thomas if (err)
1146 3efd8e31 2022-10-23 thomas break;
1147 3efd8e31 2022-10-23 thomas } else {
1148 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1149 3efd8e31 2022-10-23 thomas "unexpected flush-pkt received");
1150 3efd8e31 2022-10-23 thomas break;
1151 3efd8e31 2022-10-23 thomas }
1152 3efd8e31 2022-10-23 thomas log_debug("received flush-pkt from uid %d",
1153 3efd8e31 2022-10-23 thomas client->euid);
1154 3efd8e31 2022-10-23 thomas if (client->state == GOTD_STATE_EXPECT_WANT) {
1155 3efd8e31 2022-10-23 thomas client->state = GOTD_STATE_EXPECT_HAVE;
1156 3efd8e31 2022-10-23 thomas log_debug("uid %d: expecting have-lines",
1157 3efd8e31 2022-10-23 thomas client->euid);
1158 3efd8e31 2022-10-23 thomas } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1159 3efd8e31 2022-10-23 thomas client->state = GOTD_STATE_EXPECT_DONE;
1160 3efd8e31 2022-10-23 thomas log_debug("uid %d: expecting 'done'",
1161 3efd8e31 2022-10-23 thomas client->euid);
1162 3efd8e31 2022-10-23 thomas } else if (client->state ==
1163 3efd8e31 2022-10-23 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1164 3efd8e31 2022-10-23 thomas client->state = GOTD_STATE_EXPECT_PACKFILE;
1165 3efd8e31 2022-10-23 thomas log_debug("uid %d: expecting packfile",
1166 3efd8e31 2022-10-23 thomas client->euid);
1167 3efd8e31 2022-10-23 thomas err = recv_packfile(client);
1168 3efd8e31 2022-10-23 thomas } else {
1169 3efd8e31 2022-10-23 thomas /* should not happen, see above */
1170 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1171 3efd8e31 2022-10-23 thomas "unexpected client state");
1172 3efd8e31 2022-10-23 thomas break;
1173 3efd8e31 2022-10-23 thomas }
1174 3efd8e31 2022-10-23 thomas break;
1175 3efd8e31 2022-10-23 thomas case GOTD_IMSG_DONE:
1176 3efd8e31 2022-10-23 thomas if (client->state != GOTD_STATE_EXPECT_HAVE &&
1177 3efd8e31 2022-10-23 thomas client->state != GOTD_STATE_EXPECT_DONE) {
1178 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1179 3efd8e31 2022-10-23 thomas "unexpected flush-pkt received");
1180 3efd8e31 2022-10-23 thomas break;
1181 3efd8e31 2022-10-23 thomas }
1182 3efd8e31 2022-10-23 thomas log_debug("received 'done' from uid %d", client->euid);
1183 3efd8e31 2022-10-23 thomas err = ensure_client_is_reading(client);
1184 3efd8e31 2022-10-23 thomas if (err)
1185 3efd8e31 2022-10-23 thomas break;
1186 3efd8e31 2022-10-23 thomas client->state = GOTD_STATE_DONE;
1187 3efd8e31 2022-10-23 thomas err = send_packfile(client);
1188 3efd8e31 2022-10-23 thomas break;
1189 3efd8e31 2022-10-23 thomas default:
1190 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1191 3efd8e31 2022-10-23 thomas break;
1192 3efd8e31 2022-10-23 thomas }
1193 3efd8e31 2022-10-23 thomas
1194 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
1195 3efd8e31 2022-10-23 thomas }
1196 3efd8e31 2022-10-23 thomas
1197 3efd8e31 2022-10-23 thomas if (err) {
1198 3efd8e31 2022-10-23 thomas if (err->code != GOT_ERR_EOF ||
1199 3efd8e31 2022-10-23 thomas client->state != GOTD_STATE_EXPECT_PACKFILE)
1200 3efd8e31 2022-10-23 thomas disconnect_on_error(client, err);
1201 3efd8e31 2022-10-23 thomas } else {
1202 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(&client->iev);
1203 95ef3f8a 2022-12-30 thomas if (client->state == GOTD_STATE_EXPECT_LIST_REFS)
1204 95ef3f8a 2022-12-30 thomas evtimer_add(&client->tmo, &auth_timeout);
1205 95ef3f8a 2022-12-30 thomas else
1206 95ef3f8a 2022-12-30 thomas evtimer_add(&client->tmo, &timeout);
1207 3efd8e31 2022-10-23 thomas }
1208 3efd8e31 2022-10-23 thomas }
1209 3efd8e31 2022-10-23 thomas
1210 3efd8e31 2022-10-23 thomas static void
1211 3efd8e31 2022-10-23 thomas gotd_request_timeout(int fd, short events, void *arg)
1212 3efd8e31 2022-10-23 thomas {
1213 3efd8e31 2022-10-23 thomas struct gotd_client *client = arg;
1214 3efd8e31 2022-10-23 thomas
1215 3efd8e31 2022-10-23 thomas log_debug("disconnecting uid %d due to timeout", client->euid);
1216 3efd8e31 2022-10-23 thomas disconnect(client);
1217 3efd8e31 2022-10-23 thomas }
1218 3efd8e31 2022-10-23 thomas
1219 2b3d32a1 2022-12-30 thomas static const struct got_error *
1220 2b3d32a1 2022-12-30 thomas recv_connect(uint32_t *client_id, struct imsg *imsg)
1221 3efd8e31 2022-10-23 thomas {
1222 2b3d32a1 2022-12-30 thomas const struct got_error *err = NULL;
1223 2b3d32a1 2022-12-30 thomas struct gotd_imsg_connect iconnect;
1224 2b3d32a1 2022-12-30 thomas size_t datalen;
1225 3efd8e31 2022-10-23 thomas int s = -1;
1226 3efd8e31 2022-10-23 thomas struct gotd_client *client = NULL;
1227 3efd8e31 2022-10-23 thomas
1228 2b3d32a1 2022-12-30 thomas *client_id = 0;
1229 3efd8e31 2022-10-23 thomas
1230 2b3d32a1 2022-12-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1231 2b3d32a1 2022-12-30 thomas if (datalen != sizeof(iconnect))
1232 2b3d32a1 2022-12-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1233 2b3d32a1 2022-12-30 thomas memcpy(&iconnect, imsg->data, sizeof(iconnect));
1234 3efd8e31 2022-10-23 thomas
1235 2b3d32a1 2022-12-30 thomas s = imsg->fd;
1236 3efd8e31 2022-10-23 thomas if (s == -1) {
1237 2b3d32a1 2022-12-30 thomas err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1238 2b3d32a1 2022-12-30 thomas goto done;
1239 3efd8e31 2022-10-23 thomas }
1240 3efd8e31 2022-10-23 thomas
1241 2b3d32a1 2022-12-30 thomas if (find_client(iconnect.client_id)) {
1242 2b3d32a1 2022-12-30 thomas err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
1243 2b3d32a1 2022-12-30 thomas goto done;
1244 2b3d32a1 2022-12-30 thomas }
1245 3efd8e31 2022-10-23 thomas
1246 3efd8e31 2022-10-23 thomas client = calloc(1, sizeof(*client));
1247 3efd8e31 2022-10-23 thomas if (client == NULL) {
1248 2b3d32a1 2022-12-30 thomas err = got_error_from_errno("calloc");
1249 2b3d32a1 2022-12-30 thomas goto done;
1250 3efd8e31 2022-10-23 thomas }
1251 3efd8e31 2022-10-23 thomas
1252 2b3d32a1 2022-12-30 thomas *client_id = iconnect.client_id;
1253 2b3d32a1 2022-12-30 thomas
1254 3efd8e31 2022-10-23 thomas client->state = GOTD_STATE_EXPECT_LIST_REFS;
1255 2b3d32a1 2022-12-30 thomas client->id = iconnect.client_id;
1256 3efd8e31 2022-10-23 thomas client->fd = s;
1257 3efd8e31 2022-10-23 thomas s = -1;
1258 3efd8e31 2022-10-23 thomas client->delta_cache_fd = -1;
1259 0bcde4c8 2022-12-30 thomas /* The auth process will verify UID/GID for us. */
1260 0bcde4c8 2022-12-30 thomas client->euid = iconnect.euid;
1261 0bcde4c8 2022-12-30 thomas client->egid = iconnect.egid;
1262 3efd8e31 2022-10-23 thomas client->nref_updates = -1;
1263 3efd8e31 2022-10-23 thomas
1264 3efd8e31 2022-10-23 thomas imsg_init(&client->iev.ibuf, client->fd);
1265 3efd8e31 2022-10-23 thomas client->iev.handler = gotd_request;
1266 3efd8e31 2022-10-23 thomas client->iev.events = EV_READ;
1267 3efd8e31 2022-10-23 thomas client->iev.handler_arg = client;
1268 3efd8e31 2022-10-23 thomas
1269 3efd8e31 2022-10-23 thomas event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1270 3efd8e31 2022-10-23 thomas &client->iev);
1271 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(&client->iev);
1272 3efd8e31 2022-10-23 thomas
1273 3efd8e31 2022-10-23 thomas evtimer_set(&client->tmo, gotd_request_timeout, client);
1274 3efd8e31 2022-10-23 thomas
1275 3efd8e31 2022-10-23 thomas add_client(client);
1276 3efd8e31 2022-10-23 thomas log_debug("%s: new client uid %d connected on fd %d", __func__,
1277 3efd8e31 2022-10-23 thomas client->euid, client->fd);
1278 2b3d32a1 2022-12-30 thomas done:
1279 2b3d32a1 2022-12-30 thomas if (err) {
1280 85b37c72 2022-12-30 thomas struct gotd_child_proc *listen_proc = &gotd.listen_proc;
1281 2b3d32a1 2022-12-30 thomas struct gotd_imsg_disconnect idisconnect;
1282 3efd8e31 2022-10-23 thomas
1283 2b3d32a1 2022-12-30 thomas idisconnect.client_id = client->id;
1284 2b3d32a1 2022-12-30 thomas if (gotd_imsg_compose_event(&listen_proc->iev,
1285 2b3d32a1 2022-12-30 thomas GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
1286 2b3d32a1 2022-12-30 thomas &idisconnect, sizeof(idisconnect)) == -1)
1287 2b3d32a1 2022-12-30 thomas log_warn("imsg compose DISCONNECT");
1288 2b3d32a1 2022-12-30 thomas
1289 2b3d32a1 2022-12-30 thomas if (s != -1)
1290 2b3d32a1 2022-12-30 thomas close(s);
1291 2b3d32a1 2022-12-30 thomas }
1292 2b3d32a1 2022-12-30 thomas
1293 2b3d32a1 2022-12-30 thomas return err;
1294 3efd8e31 2022-10-23 thomas }
1295 3efd8e31 2022-10-23 thomas
1296 3efd8e31 2022-10-23 thomas static const char *gotd_proc_names[PROC_MAX] = {
1297 3efd8e31 2022-10-23 thomas "parent",
1298 2b3d32a1 2022-12-30 thomas "listen",
1299 c669c489 2022-12-30 thomas "auth",
1300 3efd8e31 2022-10-23 thomas "repo_read",
1301 3efd8e31 2022-10-23 thomas "repo_write"
1302 3efd8e31 2022-10-23 thomas };
1303 3efd8e31 2022-10-23 thomas
1304 3efd8e31 2022-10-23 thomas static void
1305 3efd8e31 2022-10-23 thomas kill_proc(struct gotd_child_proc *proc, int fatal)
1306 3efd8e31 2022-10-23 thomas {
1307 3efd8e31 2022-10-23 thomas if (fatal) {
1308 3efd8e31 2022-10-23 thomas log_warnx("sending SIGKILL to PID %d", proc->pid);
1309 3efd8e31 2022-10-23 thomas kill(proc->pid, SIGKILL);
1310 3efd8e31 2022-10-23 thomas } else
1311 3efd8e31 2022-10-23 thomas kill(proc->pid, SIGTERM);
1312 3efd8e31 2022-10-23 thomas }
1313 3efd8e31 2022-10-23 thomas
1314 3efd8e31 2022-10-23 thomas static void
1315 3efd8e31 2022-10-23 thomas gotd_shutdown(void)
1316 3efd8e31 2022-10-23 thomas {
1317 3efd8e31 2022-10-23 thomas struct gotd_child_proc *proc;
1318 85b37c72 2022-12-30 thomas uint64_t slot;
1319 3efd8e31 2022-10-23 thomas
1320 85b37c72 2022-12-30 thomas for (slot = 0; slot < nitems(gotd_clients); slot++) {
1321 85b37c72 2022-12-30 thomas struct gotd_client *c, *tmp;
1322 85b37c72 2022-12-30 thomas
1323 85b37c72 2022-12-30 thomas STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
1324 85b37c72 2022-12-30 thomas disconnect(c);
1325 3efd8e31 2022-10-23 thomas }
1326 3efd8e31 2022-10-23 thomas
1327 85b37c72 2022-12-30 thomas proc = &gotd.listen_proc;
1328 85b37c72 2022-12-30 thomas msgbuf_clear(&proc->iev.ibuf.w);
1329 85b37c72 2022-12-30 thomas close(proc->iev.ibuf.fd);
1330 85b37c72 2022-12-30 thomas kill_proc(proc, 0);
1331 c669c489 2022-12-30 thomas wait_for_child(proc->pid);
1332 3efd8e31 2022-10-23 thomas
1333 3efd8e31 2022-10-23 thomas log_info("terminating");
1334 3efd8e31 2022-10-23 thomas exit(0);
1335 3efd8e31 2022-10-23 thomas }
1336 3efd8e31 2022-10-23 thomas
1337 3efd8e31 2022-10-23 thomas void
1338 3efd8e31 2022-10-23 thomas gotd_sighdlr(int sig, short event, void *arg)
1339 3efd8e31 2022-10-23 thomas {
1340 3efd8e31 2022-10-23 thomas /*
1341 3efd8e31 2022-10-23 thomas * Normal signal handler rules don't apply because libevent
1342 3efd8e31 2022-10-23 thomas * decouples for us.
1343 3efd8e31 2022-10-23 thomas */
1344 3efd8e31 2022-10-23 thomas
1345 3efd8e31 2022-10-23 thomas switch (sig) {
1346 3efd8e31 2022-10-23 thomas case SIGHUP:
1347 3efd8e31 2022-10-23 thomas log_info("%s: ignoring SIGHUP", __func__);
1348 3efd8e31 2022-10-23 thomas break;
1349 3efd8e31 2022-10-23 thomas case SIGUSR1:
1350 3efd8e31 2022-10-23 thomas log_info("%s: ignoring SIGUSR1", __func__);
1351 3efd8e31 2022-10-23 thomas break;
1352 3efd8e31 2022-10-23 thomas case SIGTERM:
1353 3efd8e31 2022-10-23 thomas case SIGINT:
1354 3efd8e31 2022-10-23 thomas gotd_shutdown();
1355 3efd8e31 2022-10-23 thomas log_warnx("gotd terminating");
1356 3efd8e31 2022-10-23 thomas exit(0);
1357 3efd8e31 2022-10-23 thomas break;
1358 3efd8e31 2022-10-23 thomas default:
1359 3efd8e31 2022-10-23 thomas fatalx("unexpected signal");
1360 3efd8e31 2022-10-23 thomas }
1361 3efd8e31 2022-10-23 thomas }
1362 3efd8e31 2022-10-23 thomas
1363 3efd8e31 2022-10-23 thomas static const struct got_error *
1364 3efd8e31 2022-10-23 thomas ensure_proc_is_reading(struct gotd_client *client,
1365 3efd8e31 2022-10-23 thomas struct gotd_child_proc *proc)
1366 3efd8e31 2022-10-23 thomas {
1367 3efd8e31 2022-10-23 thomas if (!client_is_reading(client)) {
1368 3efd8e31 2022-10-23 thomas kill_proc(proc, 1);
1369 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
1370 3efd8e31 2022-10-23 thomas "PID %d handled a read-request for uid %d but this "
1371 3efd8e31 2022-10-23 thomas "user is not reading from a repository", proc->pid,
1372 3efd8e31 2022-10-23 thomas client->euid);
1373 3efd8e31 2022-10-23 thomas }
1374 3efd8e31 2022-10-23 thomas
1375 3efd8e31 2022-10-23 thomas return NULL;
1376 3efd8e31 2022-10-23 thomas }
1377 3efd8e31 2022-10-23 thomas
1378 3efd8e31 2022-10-23 thomas static const struct got_error *
1379 3efd8e31 2022-10-23 thomas ensure_proc_is_writing(struct gotd_client *client,
1380 3efd8e31 2022-10-23 thomas struct gotd_child_proc *proc)
1381 3efd8e31 2022-10-23 thomas {
1382 3efd8e31 2022-10-23 thomas if (!client_is_writing(client)) {
1383 3efd8e31 2022-10-23 thomas kill_proc(proc, 1);
1384 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
1385 3efd8e31 2022-10-23 thomas "PID %d handled a write-request for uid %d but this "
1386 3efd8e31 2022-10-23 thomas "user is not writing to a repository", proc->pid,
1387 3efd8e31 2022-10-23 thomas client->euid);
1388 3efd8e31 2022-10-23 thomas }
1389 3efd8e31 2022-10-23 thomas
1390 3efd8e31 2022-10-23 thomas return NULL;
1391 3efd8e31 2022-10-23 thomas }
1392 3efd8e31 2022-10-23 thomas
1393 3efd8e31 2022-10-23 thomas static int
1394 3efd8e31 2022-10-23 thomas verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1395 3efd8e31 2022-10-23 thomas struct imsg *imsg)
1396 3efd8e31 2022-10-23 thomas {
1397 3efd8e31 2022-10-23 thomas const struct got_error *err;
1398 3efd8e31 2022-10-23 thomas struct gotd_child_proc *client_proc;
1399 3efd8e31 2022-10-23 thomas int ret = 0;
1400 3efd8e31 2022-10-23 thomas
1401 2b3d32a1 2022-12-30 thomas if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
1402 2b3d32a1 2022-12-30 thomas client_proc = get_client_proc(client);
1403 2b3d32a1 2022-12-30 thomas if (client_proc == NULL)
1404 2b3d32a1 2022-12-30 thomas fatalx("no process found for uid %d", client->euid);
1405 2b3d32a1 2022-12-30 thomas if (proc->pid != client_proc->pid) {
1406 2b3d32a1 2022-12-30 thomas kill_proc(proc, 1);
1407 2b3d32a1 2022-12-30 thomas log_warnx("received message from PID %d for uid %d, "
1408 2b3d32a1 2022-12-30 thomas "while PID %d is the process serving this user",
1409 2b3d32a1 2022-12-30 thomas proc->pid, client->euid, client_proc->pid);
1410 2b3d32a1 2022-12-30 thomas return 0;
1411 2b3d32a1 2022-12-30 thomas }
1412 3efd8e31 2022-10-23 thomas }
1413 3efd8e31 2022-10-23 thomas
1414 3efd8e31 2022-10-23 thomas switch (imsg->hdr.type) {
1415 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
1416 3efd8e31 2022-10-23 thomas ret = 1;
1417 3efd8e31 2022-10-23 thomas break;
1418 2b3d32a1 2022-12-30 thomas case GOTD_IMSG_CONNECT:
1419 2b3d32a1 2022-12-30 thomas if (proc->type != PROC_LISTEN) {
1420 2b3d32a1 2022-12-30 thomas err = got_error_fmt(GOT_ERR_BAD_PACKET,
1421 2b3d32a1 2022-12-30 thomas "new connection for uid %d from PID %d "
1422 2b3d32a1 2022-12-30 thomas "which is not the listen process",
1423 c669c489 2022-12-30 thomas proc->pid, client->euid);
1424 c669c489 2022-12-30 thomas } else
1425 c669c489 2022-12-30 thomas ret = 1;
1426 c669c489 2022-12-30 thomas break;
1427 c669c489 2022-12-30 thomas case GOTD_IMSG_ACCESS_GRANTED:
1428 c669c489 2022-12-30 thomas if (proc->type != PROC_AUTH) {
1429 c669c489 2022-12-30 thomas err = got_error_fmt(GOT_ERR_BAD_PACKET,
1430 c669c489 2022-12-30 thomas "authentication of uid %d from PID %d "
1431 c669c489 2022-12-30 thomas "which is not the auth process",
1432 2b3d32a1 2022-12-30 thomas proc->pid, client->euid);
1433 2b3d32a1 2022-12-30 thomas } else
1434 2b3d32a1 2022-12-30 thomas ret = 1;
1435 2b3d32a1 2022-12-30 thomas break;
1436 85b37c72 2022-12-30 thomas case GOTD_IMSG_REPO_CHILD_READY:
1437 85b37c72 2022-12-30 thomas if (proc->type != PROC_REPO_READ &&
1438 85b37c72 2022-12-30 thomas proc->type != PROC_REPO_WRITE) {
1439 85b37c72 2022-12-30 thomas err = got_error_fmt(GOT_ERR_BAD_PACKET,
1440 85b37c72 2022-12-30 thomas "unexpected \"ready\" signal from PID %d",
1441 85b37c72 2022-12-30 thomas proc->pid);
1442 85b37c72 2022-12-30 thomas } else
1443 85b37c72 2022-12-30 thomas ret = 1;
1444 85b37c72 2022-12-30 thomas break;
1445 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_DONE:
1446 3efd8e31 2022-10-23 thomas err = ensure_proc_is_reading(client, proc);
1447 3efd8e31 2022-10-23 thomas if (err)
1448 3efd8e31 2022-10-23 thomas log_warnx("uid %d: %s", client->euid, err->msg);
1449 3efd8e31 2022-10-23 thomas else
1450 3efd8e31 2022-10-23 thomas ret = 1;
1451 3efd8e31 2022-10-23 thomas break;
1452 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_INSTALL:
1453 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATES_START:
1454 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATE:
1455 3efd8e31 2022-10-23 thomas err = ensure_proc_is_writing(client, proc);
1456 3efd8e31 2022-10-23 thomas if (err)
1457 3efd8e31 2022-10-23 thomas log_warnx("uid %d: %s", client->euid, err->msg);
1458 3efd8e31 2022-10-23 thomas else
1459 3efd8e31 2022-10-23 thomas ret = 1;
1460 3efd8e31 2022-10-23 thomas break;
1461 3efd8e31 2022-10-23 thomas default:
1462 3efd8e31 2022-10-23 thomas log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1463 3efd8e31 2022-10-23 thomas break;
1464 3efd8e31 2022-10-23 thomas }
1465 3efd8e31 2022-10-23 thomas
1466 3efd8e31 2022-10-23 thomas return ret;
1467 3efd8e31 2022-10-23 thomas }
1468 3efd8e31 2022-10-23 thomas
1469 3efd8e31 2022-10-23 thomas static const struct got_error *
1470 85b37c72 2022-12-30 thomas list_refs_request(struct gotd_client *client, struct gotd_imsgev *iev)
1471 85b37c72 2022-12-30 thomas {
1472 85b37c72 2022-12-30 thomas static const struct got_error *err;
1473 85b37c72 2022-12-30 thomas struct gotd_imsg_list_refs_internal ilref;
1474 85b37c72 2022-12-30 thomas int fd;
1475 85b37c72 2022-12-30 thomas
1476 85b37c72 2022-12-30 thomas memset(&ilref, 0, sizeof(ilref));
1477 85b37c72 2022-12-30 thomas ilref.client_id = client->id;
1478 85b37c72 2022-12-30 thomas
1479 85b37c72 2022-12-30 thomas fd = dup(client->fd);
1480 85b37c72 2022-12-30 thomas if (fd == -1)
1481 85b37c72 2022-12-30 thomas return got_error_from_errno("dup");
1482 85b37c72 2022-12-30 thomas
1483 85b37c72 2022-12-30 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1484 85b37c72 2022-12-30 thomas PROC_GOTD, fd, &ilref, sizeof(ilref)) == -1) {
1485 85b37c72 2022-12-30 thomas err = got_error_from_errno("imsg compose WANT");
1486 85b37c72 2022-12-30 thomas close(fd);
1487 85b37c72 2022-12-30 thomas return err;
1488 85b37c72 2022-12-30 thomas }
1489 85b37c72 2022-12-30 thomas
1490 85b37c72 2022-12-30 thomas client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1491 85b37c72 2022-12-30 thomas log_debug("uid %d: expecting capabilities", client->euid);
1492 85b37c72 2022-12-30 thomas return NULL;
1493 85b37c72 2022-12-30 thomas }
1494 85b37c72 2022-12-30 thomas
1495 85b37c72 2022-12-30 thomas static const struct got_error *
1496 3efd8e31 2022-10-23 thomas recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1497 3efd8e31 2022-10-23 thomas {
1498 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_done idone;
1499 3efd8e31 2022-10-23 thomas size_t datalen;
1500 3efd8e31 2022-10-23 thomas
1501 3efd8e31 2022-10-23 thomas log_debug("packfile-done received");
1502 3efd8e31 2022-10-23 thomas
1503 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1504 3efd8e31 2022-10-23 thomas if (datalen != sizeof(idone))
1505 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1506 3efd8e31 2022-10-23 thomas memcpy(&idone, imsg->data, sizeof(idone));
1507 3efd8e31 2022-10-23 thomas
1508 3efd8e31 2022-10-23 thomas *client_id = idone.client_id;
1509 3efd8e31 2022-10-23 thomas return NULL;
1510 3efd8e31 2022-10-23 thomas }
1511 3efd8e31 2022-10-23 thomas
1512 3efd8e31 2022-10-23 thomas static const struct got_error *
1513 3efd8e31 2022-10-23 thomas recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1514 3efd8e31 2022-10-23 thomas {
1515 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_install inst;
1516 3efd8e31 2022-10-23 thomas size_t datalen;
1517 3efd8e31 2022-10-23 thomas
1518 3efd8e31 2022-10-23 thomas log_debug("packfile-install received");
1519 3efd8e31 2022-10-23 thomas
1520 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1521 3efd8e31 2022-10-23 thomas if (datalen != sizeof(inst))
1522 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1523 3efd8e31 2022-10-23 thomas memcpy(&inst, imsg->data, sizeof(inst));
1524 3efd8e31 2022-10-23 thomas
1525 3efd8e31 2022-10-23 thomas *client_id = inst.client_id;
1526 3efd8e31 2022-10-23 thomas return NULL;
1527 3efd8e31 2022-10-23 thomas }
1528 3efd8e31 2022-10-23 thomas
1529 3efd8e31 2022-10-23 thomas static const struct got_error *
1530 3efd8e31 2022-10-23 thomas recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1531 3efd8e31 2022-10-23 thomas {
1532 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_updates_start istart;
1533 3efd8e31 2022-10-23 thomas size_t datalen;
1534 3efd8e31 2022-10-23 thomas
1535 3efd8e31 2022-10-23 thomas log_debug("ref-updates-start received");
1536 3efd8e31 2022-10-23 thomas
1537 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1538 3efd8e31 2022-10-23 thomas if (datalen != sizeof(istart))
1539 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1540 3efd8e31 2022-10-23 thomas memcpy(&istart, imsg->data, sizeof(istart));
1541 3efd8e31 2022-10-23 thomas
1542 3efd8e31 2022-10-23 thomas *client_id = istart.client_id;
1543 3efd8e31 2022-10-23 thomas return NULL;
1544 3efd8e31 2022-10-23 thomas }
1545 3efd8e31 2022-10-23 thomas
1546 3efd8e31 2022-10-23 thomas static const struct got_error *
1547 3efd8e31 2022-10-23 thomas recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1548 3efd8e31 2022-10-23 thomas {
1549 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update iref;
1550 3efd8e31 2022-10-23 thomas size_t datalen;
1551 3efd8e31 2022-10-23 thomas
1552 3efd8e31 2022-10-23 thomas log_debug("ref-update received");
1553 3efd8e31 2022-10-23 thomas
1554 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1555 3efd8e31 2022-10-23 thomas if (datalen < sizeof(iref))
1556 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1557 3efd8e31 2022-10-23 thomas memcpy(&iref, imsg->data, sizeof(iref));
1558 3efd8e31 2022-10-23 thomas
1559 3efd8e31 2022-10-23 thomas *client_id = iref.client_id;
1560 3efd8e31 2022-10-23 thomas return NULL;
1561 3efd8e31 2022-10-23 thomas }
1562 3efd8e31 2022-10-23 thomas
1563 3efd8e31 2022-10-23 thomas static const struct got_error *
1564 3efd8e31 2022-10-23 thomas send_ref_update_ok(struct gotd_client *client,
1565 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update *iref, const char *refname)
1566 3efd8e31 2022-10-23 thomas {
1567 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update_ok iok;
1568 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
1569 3efd8e31 2022-10-23 thomas size_t len;
1570 3efd8e31 2022-10-23 thomas
1571 3efd8e31 2022-10-23 thomas memset(&iok, 0, sizeof(iok));
1572 3efd8e31 2022-10-23 thomas iok.client_id = client->id;
1573 3efd8e31 2022-10-23 thomas memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1574 3efd8e31 2022-10-23 thomas memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1575 3efd8e31 2022-10-23 thomas iok.name_len = strlen(refname);
1576 3efd8e31 2022-10-23 thomas
1577 3efd8e31 2022-10-23 thomas len = sizeof(iok) + iok.name_len;
1578 3efd8e31 2022-10-23 thomas wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1579 3efd8e31 2022-10-23 thomas PROC_GOTD, gotd.pid, len);
1580 3efd8e31 2022-10-23 thomas if (wbuf == NULL)
1581 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_create REF_UPDATE_OK");
1582 3efd8e31 2022-10-23 thomas
1583 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1584 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE_OK");
1585 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, iok.name_len) == -1)
1586 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE_OK");
1587 3efd8e31 2022-10-23 thomas
1588 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
1589 3efd8e31 2022-10-23 thomas imsg_close(&client->iev.ibuf, wbuf);
1590 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(&client->iev);
1591 3efd8e31 2022-10-23 thomas return NULL;
1592 3efd8e31 2022-10-23 thomas }
1593 3efd8e31 2022-10-23 thomas
1594 3efd8e31 2022-10-23 thomas static void
1595 3efd8e31 2022-10-23 thomas send_refs_updated(struct gotd_client *client)
1596 3efd8e31 2022-10-23 thomas {
1597 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->iev,
1598 3efd8e31 2022-10-23 thomas GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1599 3efd8e31 2022-10-23 thomas log_warn("imsg compose REFS_UPDATED");
1600 3efd8e31 2022-10-23 thomas }
1601 3efd8e31 2022-10-23 thomas
1602 3efd8e31 2022-10-23 thomas static const struct got_error *
1603 3efd8e31 2022-10-23 thomas send_ref_update_ng(struct gotd_client *client,
1604 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update *iref, const char *refname,
1605 3efd8e31 2022-10-23 thomas const char *reason)
1606 3efd8e31 2022-10-23 thomas {
1607 3efd8e31 2022-10-23 thomas const struct got_error *ng_err;
1608 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update_ng ing;
1609 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
1610 3efd8e31 2022-10-23 thomas size_t len;
1611 3efd8e31 2022-10-23 thomas
1612 3efd8e31 2022-10-23 thomas memset(&ing, 0, sizeof(ing));
1613 3efd8e31 2022-10-23 thomas ing.client_id = client->id;
1614 3efd8e31 2022-10-23 thomas memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1615 3efd8e31 2022-10-23 thomas memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1616 3efd8e31 2022-10-23 thomas ing.name_len = strlen(refname);
1617 3efd8e31 2022-10-23 thomas
1618 3efd8e31 2022-10-23 thomas ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1619 3efd8e31 2022-10-23 thomas ing.reason_len = strlen(ng_err->msg);
1620 3efd8e31 2022-10-23 thomas
1621 3efd8e31 2022-10-23 thomas len = sizeof(ing) + ing.name_len + ing.reason_len;
1622 3efd8e31 2022-10-23 thomas wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1623 3efd8e31 2022-10-23 thomas PROC_GOTD, gotd.pid, len);
1624 3efd8e31 2022-10-23 thomas if (wbuf == NULL)
1625 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_create REF_UPDATE_NG");
1626 3efd8e31 2022-10-23 thomas
1627 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1628 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
1629 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, ing.name_len) == -1)
1630 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
1631 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1632 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
1633 3efd8e31 2022-10-23 thomas
1634 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
1635 3efd8e31 2022-10-23 thomas imsg_close(&client->iev.ibuf, wbuf);
1636 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(&client->iev);
1637 3efd8e31 2022-10-23 thomas return NULL;
1638 3efd8e31 2022-10-23 thomas }
1639 3efd8e31 2022-10-23 thomas
1640 3efd8e31 2022-10-23 thomas static const struct got_error *
1641 3efd8e31 2022-10-23 thomas install_pack(struct gotd_client *client, const char *repo_path,
1642 3efd8e31 2022-10-23 thomas struct imsg *imsg)
1643 3efd8e31 2022-10-23 thomas {
1644 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1645 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_install inst;
1646 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
1647 3efd8e31 2022-10-23 thomas size_t datalen;
1648 3efd8e31 2022-10-23 thomas char *packfile_path = NULL, *packidx_path = NULL;
1649 3efd8e31 2022-10-23 thomas
1650 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1651 3efd8e31 2022-10-23 thomas if (datalen != sizeof(inst))
1652 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1653 3efd8e31 2022-10-23 thomas memcpy(&inst, imsg->data, sizeof(inst));
1654 3efd8e31 2022-10-23 thomas
1655 3efd8e31 2022-10-23 thomas if (client->packfile_path == NULL)
1656 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
1657 3efd8e31 2022-10-23 thomas "client has no pack file");
1658 3efd8e31 2022-10-23 thomas if (client->packidx_path == NULL)
1659 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
1660 3efd8e31 2022-10-23 thomas "client has no pack file index");
1661 3efd8e31 2022-10-23 thomas
1662 3efd8e31 2022-10-23 thomas if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1663 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_NO_SPACE,
1664 3efd8e31 2022-10-23 thomas "could not convert pack file SHA1 to hex");
1665 3efd8e31 2022-10-23 thomas
1666 3efd8e31 2022-10-23 thomas if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1667 3efd8e31 2022-10-23 thomas repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1668 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
1669 3efd8e31 2022-10-23 thomas goto done;
1670 3efd8e31 2022-10-23 thomas }
1671 3efd8e31 2022-10-23 thomas
1672 3efd8e31 2022-10-23 thomas if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1673 3efd8e31 2022-10-23 thomas repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1674 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
1675 3efd8e31 2022-10-23 thomas goto done;
1676 3efd8e31 2022-10-23 thomas }
1677 3efd8e31 2022-10-23 thomas
1678 3efd8e31 2022-10-23 thomas if (rename(client->packfile_path, packfile_path) == -1) {
1679 3efd8e31 2022-10-23 thomas err = got_error_from_errno3("rename", client->packfile_path,
1680 3efd8e31 2022-10-23 thomas packfile_path);
1681 3efd8e31 2022-10-23 thomas goto done;
1682 3efd8e31 2022-10-23 thomas }
1683 3efd8e31 2022-10-23 thomas
1684 3efd8e31 2022-10-23 thomas free(client->packfile_path);
1685 3efd8e31 2022-10-23 thomas client->packfile_path = NULL;
1686 3efd8e31 2022-10-23 thomas
1687 3efd8e31 2022-10-23 thomas if (rename(client->packidx_path, packidx_path) == -1) {
1688 3efd8e31 2022-10-23 thomas err = got_error_from_errno3("rename", client->packidx_path,
1689 3efd8e31 2022-10-23 thomas packidx_path);
1690 3efd8e31 2022-10-23 thomas goto done;
1691 3efd8e31 2022-10-23 thomas }
1692 3efd8e31 2022-10-23 thomas
1693 3efd8e31 2022-10-23 thomas free(client->packidx_path);
1694 3efd8e31 2022-10-23 thomas client->packidx_path = NULL;
1695 3efd8e31 2022-10-23 thomas done:
1696 3efd8e31 2022-10-23 thomas free(packfile_path);
1697 3efd8e31 2022-10-23 thomas free(packidx_path);
1698 3efd8e31 2022-10-23 thomas return err;
1699 3efd8e31 2022-10-23 thomas }
1700 3efd8e31 2022-10-23 thomas
1701 3efd8e31 2022-10-23 thomas static const struct got_error *
1702 3efd8e31 2022-10-23 thomas begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1703 3efd8e31 2022-10-23 thomas {
1704 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_updates_start istart;
1705 3efd8e31 2022-10-23 thomas size_t datalen;
1706 3efd8e31 2022-10-23 thomas
1707 3efd8e31 2022-10-23 thomas if (client->nref_updates != -1)
1708 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1709 3efd8e31 2022-10-23 thomas
1710 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1711 3efd8e31 2022-10-23 thomas if (datalen != sizeof(istart))
1712 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1713 3efd8e31 2022-10-23 thomas memcpy(&istart, imsg->data, sizeof(istart));
1714 3efd8e31 2022-10-23 thomas
1715 3efd8e31 2022-10-23 thomas if (istart.nref_updates <= 0)
1716 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1717 3efd8e31 2022-10-23 thomas
1718 3efd8e31 2022-10-23 thomas client->nref_updates = istart.nref_updates;
1719 3efd8e31 2022-10-23 thomas return NULL;
1720 3efd8e31 2022-10-23 thomas }
1721 3efd8e31 2022-10-23 thomas
1722 3efd8e31 2022-10-23 thomas static const struct got_error *
1723 3efd8e31 2022-10-23 thomas update_ref(struct gotd_client *client, const char *repo_path,
1724 3efd8e31 2022-10-23 thomas struct imsg *imsg)
1725 3efd8e31 2022-10-23 thomas {
1726 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1727 3efd8e31 2022-10-23 thomas struct got_repository *repo = NULL;
1728 3efd8e31 2022-10-23 thomas struct got_reference *ref = NULL;
1729 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update iref;
1730 3efd8e31 2022-10-23 thomas struct got_object_id old_id, new_id;
1731 3efd8e31 2022-10-23 thomas struct got_object_id *id = NULL;
1732 3efd8e31 2022-10-23 thomas struct got_object *obj = NULL;
1733 3efd8e31 2022-10-23 thomas char *refname = NULL;
1734 3efd8e31 2022-10-23 thomas size_t datalen;
1735 3efd8e31 2022-10-23 thomas int locked = 0;
1736 3efd8e31 2022-10-23 thomas
1737 3efd8e31 2022-10-23 thomas log_debug("update-ref from uid %d", client->euid);
1738 3efd8e31 2022-10-23 thomas
1739 3efd8e31 2022-10-23 thomas if (client->nref_updates <= 0)
1740 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1741 3efd8e31 2022-10-23 thomas
1742 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1743 3efd8e31 2022-10-23 thomas if (datalen < sizeof(iref))
1744 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1745 3efd8e31 2022-10-23 thomas memcpy(&iref, imsg->data, sizeof(iref));
1746 3efd8e31 2022-10-23 thomas if (datalen != sizeof(iref) + iref.name_len)
1747 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1748 3efd8e31 2022-10-23 thomas refname = malloc(iref.name_len + 1);
1749 3efd8e31 2022-10-23 thomas if (refname == NULL)
1750 3efd8e31 2022-10-23 thomas return got_error_from_errno("malloc");
1751 3efd8e31 2022-10-23 thomas memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1752 3efd8e31 2022-10-23 thomas refname[iref.name_len] = '\0';
1753 3efd8e31 2022-10-23 thomas
1754 3efd8e31 2022-10-23 thomas log_debug("updating ref %s for uid %d", refname, client->euid);
1755 3efd8e31 2022-10-23 thomas
1756 3efd8e31 2022-10-23 thomas err = got_repo_open(&repo, repo_path, NULL, NULL);
1757 3efd8e31 2022-10-23 thomas if (err)
1758 3efd8e31 2022-10-23 thomas goto done;
1759 3efd8e31 2022-10-23 thomas
1760 3efd8e31 2022-10-23 thomas memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1761 3efd8e31 2022-10-23 thomas memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1762 3efd8e31 2022-10-23 thomas err = got_object_open(&obj, repo, &new_id);
1763 3efd8e31 2022-10-23 thomas if (err)
1764 3efd8e31 2022-10-23 thomas goto done;
1765 3efd8e31 2022-10-23 thomas
1766 3efd8e31 2022-10-23 thomas if (iref.ref_is_new) {
1767 3efd8e31 2022-10-23 thomas err = got_ref_open(&ref, repo, refname, 0);
1768 3efd8e31 2022-10-23 thomas if (err) {
1769 3efd8e31 2022-10-23 thomas if (err->code != GOT_ERR_NOT_REF)
1770 3efd8e31 2022-10-23 thomas goto done;
1771 3efd8e31 2022-10-23 thomas err = got_ref_alloc(&ref, refname, &new_id);
1772 3efd8e31 2022-10-23 thomas if (err)
1773 3efd8e31 2022-10-23 thomas goto done;
1774 3efd8e31 2022-10-23 thomas err = got_ref_write(ref, repo); /* will lock/unlock */
1775 3efd8e31 2022-10-23 thomas if (err)
1776 3efd8e31 2022-10-23 thomas goto done;
1777 3efd8e31 2022-10-23 thomas } else {
1778 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
1779 3efd8e31 2022-10-23 thomas "%s has been created by someone else "
1780 3efd8e31 2022-10-23 thomas "while transaction was in progress",
1781 3efd8e31 2022-10-23 thomas got_ref_get_name(ref));
1782 3efd8e31 2022-10-23 thomas goto done;
1783 3efd8e31 2022-10-23 thomas }
1784 3efd8e31 2022-10-23 thomas } else {
1785 3efd8e31 2022-10-23 thomas err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1786 3efd8e31 2022-10-23 thomas if (err)
1787 3efd8e31 2022-10-23 thomas goto done;
1788 3efd8e31 2022-10-23 thomas locked = 1;
1789 3efd8e31 2022-10-23 thomas
1790 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&id, repo, ref);
1791 3efd8e31 2022-10-23 thomas if (err)
1792 3efd8e31 2022-10-23 thomas goto done;
1793 3efd8e31 2022-10-23 thomas
1794 3efd8e31 2022-10-23 thomas if (got_object_id_cmp(id, &old_id) != 0) {
1795 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
1796 3efd8e31 2022-10-23 thomas "%s has been modified by someone else "
1797 3efd8e31 2022-10-23 thomas "while transaction was in progress",
1798 3efd8e31 2022-10-23 thomas got_ref_get_name(ref));
1799 3efd8e31 2022-10-23 thomas goto done;
1800 3efd8e31 2022-10-23 thomas }
1801 3efd8e31 2022-10-23 thomas
1802 3efd8e31 2022-10-23 thomas err = got_ref_change_ref(ref, &new_id);
1803 3efd8e31 2022-10-23 thomas if (err)
1804 3efd8e31 2022-10-23 thomas goto done;
1805 3efd8e31 2022-10-23 thomas
1806 3efd8e31 2022-10-23 thomas err = got_ref_write(ref, repo);
1807 3efd8e31 2022-10-23 thomas if (err)
1808 3efd8e31 2022-10-23 thomas goto done;
1809 3efd8e31 2022-10-23 thomas
1810 3efd8e31 2022-10-23 thomas free(id);
1811 3efd8e31 2022-10-23 thomas id = NULL;
1812 3efd8e31 2022-10-23 thomas }
1813 3efd8e31 2022-10-23 thomas done:
1814 3efd8e31 2022-10-23 thomas if (err) {
1815 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1816 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1817 3efd8e31 2022-10-23 thomas "could not acquire exclusive file lock for %s",
1818 3efd8e31 2022-10-23 thomas refname);
1819 3efd8e31 2022-10-23 thomas }
1820 3efd8e31 2022-10-23 thomas send_ref_update_ng(client, &iref, refname, err->msg);
1821 3efd8e31 2022-10-23 thomas } else
1822 3efd8e31 2022-10-23 thomas send_ref_update_ok(client, &iref, refname);
1823 3efd8e31 2022-10-23 thomas
1824 3efd8e31 2022-10-23 thomas if (client->nref_updates > 0) {
1825 3efd8e31 2022-10-23 thomas client->nref_updates--;
1826 3efd8e31 2022-10-23 thomas if (client->nref_updates == 0)
1827 3efd8e31 2022-10-23 thomas send_refs_updated(client);
1828 3efd8e31 2022-10-23 thomas
1829 3efd8e31 2022-10-23 thomas }
1830 3efd8e31 2022-10-23 thomas if (locked) {
1831 3efd8e31 2022-10-23 thomas const struct got_error *unlock_err;
1832 3efd8e31 2022-10-23 thomas unlock_err = got_ref_unlock(ref);
1833 3efd8e31 2022-10-23 thomas if (unlock_err && err == NULL)
1834 3efd8e31 2022-10-23 thomas err = unlock_err;
1835 3efd8e31 2022-10-23 thomas }
1836 3efd8e31 2022-10-23 thomas if (ref)
1837 3efd8e31 2022-10-23 thomas got_ref_close(ref);
1838 3efd8e31 2022-10-23 thomas if (obj)
1839 3efd8e31 2022-10-23 thomas got_object_close(obj);
1840 3efd8e31 2022-10-23 thomas if (repo)
1841 3efd8e31 2022-10-23 thomas got_repo_close(repo);
1842 3efd8e31 2022-10-23 thomas free(refname);
1843 3efd8e31 2022-10-23 thomas free(id);
1844 3efd8e31 2022-10-23 thomas return err;
1845 3efd8e31 2022-10-23 thomas }
1846 3efd8e31 2022-10-23 thomas
1847 3efd8e31 2022-10-23 thomas static void
1848 85b37c72 2022-12-30 thomas gotd_dispatch_listener(int fd, short event, void *arg)
1849 3efd8e31 2022-10-23 thomas {
1850 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev = arg;
1851 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf = &iev->ibuf;
1852 85b37c72 2022-12-30 thomas struct gotd_child_proc *proc = &gotd.listen_proc;
1853 85b37c72 2022-12-30 thomas ssize_t n;
1854 85b37c72 2022-12-30 thomas int shut = 0;
1855 85b37c72 2022-12-30 thomas struct imsg imsg;
1856 85b37c72 2022-12-30 thomas
1857 85b37c72 2022-12-30 thomas if (proc->iev.ibuf.fd != fd)
1858 85b37c72 2022-12-30 thomas fatalx("%s: unexpected fd %d", __func__, fd);
1859 85b37c72 2022-12-30 thomas
1860 85b37c72 2022-12-30 thomas if (event & EV_READ) {
1861 85b37c72 2022-12-30 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1862 85b37c72 2022-12-30 thomas fatal("imsg_read error");
1863 85b37c72 2022-12-30 thomas if (n == 0) {
1864 85b37c72 2022-12-30 thomas /* Connection closed. */
1865 85b37c72 2022-12-30 thomas shut = 1;
1866 85b37c72 2022-12-30 thomas goto done;
1867 85b37c72 2022-12-30 thomas }
1868 85b37c72 2022-12-30 thomas }
1869 85b37c72 2022-12-30 thomas
1870 85b37c72 2022-12-30 thomas if (event & EV_WRITE) {
1871 85b37c72 2022-12-30 thomas n = msgbuf_write(&ibuf->w);
1872 85b37c72 2022-12-30 thomas if (n == -1 && errno != EAGAIN)
1873 85b37c72 2022-12-30 thomas fatal("msgbuf_write");
1874 85b37c72 2022-12-30 thomas if (n == 0) {
1875 85b37c72 2022-12-30 thomas /* Connection closed. */
1876 85b37c72 2022-12-30 thomas shut = 1;
1877 85b37c72 2022-12-30 thomas goto done;
1878 85b37c72 2022-12-30 thomas }
1879 85b37c72 2022-12-30 thomas }
1880 85b37c72 2022-12-30 thomas
1881 85b37c72 2022-12-30 thomas for (;;) {
1882 85b37c72 2022-12-30 thomas const struct got_error *err = NULL;
1883 85b37c72 2022-12-30 thomas struct gotd_client *client = NULL;
1884 85b37c72 2022-12-30 thomas uint32_t client_id = 0;
1885 85b37c72 2022-12-30 thomas int do_disconnect = 0;
1886 85b37c72 2022-12-30 thomas
1887 85b37c72 2022-12-30 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1888 85b37c72 2022-12-30 thomas fatal("%s: imsg_get error", __func__);
1889 85b37c72 2022-12-30 thomas if (n == 0) /* No more messages. */
1890 85b37c72 2022-12-30 thomas break;
1891 85b37c72 2022-12-30 thomas
1892 85b37c72 2022-12-30 thomas switch (imsg.hdr.type) {
1893 85b37c72 2022-12-30 thomas case GOTD_IMSG_ERROR:
1894 85b37c72 2022-12-30 thomas do_disconnect = 1;
1895 85b37c72 2022-12-30 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
1896 85b37c72 2022-12-30 thomas break;
1897 85b37c72 2022-12-30 thomas case GOTD_IMSG_CONNECT:
1898 85b37c72 2022-12-30 thomas err = recv_connect(&client_id, &imsg);
1899 85b37c72 2022-12-30 thomas break;
1900 85b37c72 2022-12-30 thomas default:
1901 85b37c72 2022-12-30 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1902 85b37c72 2022-12-30 thomas break;
1903 85b37c72 2022-12-30 thomas }
1904 85b37c72 2022-12-30 thomas
1905 85b37c72 2022-12-30 thomas client = find_client(client_id);
1906 85b37c72 2022-12-30 thomas if (client == NULL) {
1907 85b37c72 2022-12-30 thomas log_warnx("%s: client not found", __func__);
1908 85b37c72 2022-12-30 thomas imsg_free(&imsg);
1909 85b37c72 2022-12-30 thomas continue;
1910 85b37c72 2022-12-30 thomas }
1911 85b37c72 2022-12-30 thomas
1912 85b37c72 2022-12-30 thomas if (err)
1913 85b37c72 2022-12-30 thomas log_warnx("uid %d: %s", client->euid, err->msg);
1914 85b37c72 2022-12-30 thomas
1915 85b37c72 2022-12-30 thomas if (do_disconnect) {
1916 85b37c72 2022-12-30 thomas if (err)
1917 85b37c72 2022-12-30 thomas disconnect_on_error(client, err);
1918 85b37c72 2022-12-30 thomas else
1919 85b37c72 2022-12-30 thomas disconnect(client);
1920 85b37c72 2022-12-30 thomas }
1921 85b37c72 2022-12-30 thomas
1922 85b37c72 2022-12-30 thomas imsg_free(&imsg);
1923 85b37c72 2022-12-30 thomas }
1924 85b37c72 2022-12-30 thomas done:
1925 85b37c72 2022-12-30 thomas if (!shut) {
1926 85b37c72 2022-12-30 thomas gotd_imsg_event_add(iev);
1927 85b37c72 2022-12-30 thomas } else {
1928 85b37c72 2022-12-30 thomas /* This pipe is dead. Remove its event handler */
1929 85b37c72 2022-12-30 thomas event_del(&iev->ev);
1930 85b37c72 2022-12-30 thomas event_loopexit(NULL);
1931 85b37c72 2022-12-30 thomas }
1932 85b37c72 2022-12-30 thomas }
1933 85b37c72 2022-12-30 thomas
1934 85b37c72 2022-12-30 thomas static void
1935 c669c489 2022-12-30 thomas gotd_dispatch_auth_child(int fd, short event, void *arg)
1936 c669c489 2022-12-30 thomas {
1937 c669c489 2022-12-30 thomas const struct got_error *err = NULL;
1938 c669c489 2022-12-30 thomas struct gotd_imsgev *iev = arg;
1939 c669c489 2022-12-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
1940 c669c489 2022-12-30 thomas struct gotd_client *client;
1941 c669c489 2022-12-30 thomas struct gotd_repo *repo = NULL;
1942 c669c489 2022-12-30 thomas ssize_t n;
1943 c669c489 2022-12-30 thomas int shut = 0;
1944 c669c489 2022-12-30 thomas struct imsg imsg;
1945 c669c489 2022-12-30 thomas uint32_t client_id = 0;
1946 c669c489 2022-12-30 thomas int do_disconnect = 0;
1947 c669c489 2022-12-30 thomas enum gotd_procid proc_type;
1948 c669c489 2022-12-30 thomas
1949 c669c489 2022-12-30 thomas client = find_client_by_proc_fd(fd);
1950 c669c489 2022-12-30 thomas if (client == NULL)
1951 c669c489 2022-12-30 thomas fatalx("cannot find client for fd %d", fd);
1952 c669c489 2022-12-30 thomas
1953 c669c489 2022-12-30 thomas if (client->auth == NULL)
1954 c669c489 2022-12-30 thomas fatalx("cannot find auth child process for fd %d", fd);
1955 c669c489 2022-12-30 thomas
1956 c669c489 2022-12-30 thomas if (event & EV_READ) {
1957 c669c489 2022-12-30 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1958 c669c489 2022-12-30 thomas fatal("imsg_read error");
1959 c669c489 2022-12-30 thomas if (n == 0) {
1960 c669c489 2022-12-30 thomas /* Connection closed. */
1961 c669c489 2022-12-30 thomas shut = 1;
1962 c669c489 2022-12-30 thomas goto done;
1963 c669c489 2022-12-30 thomas }
1964 c669c489 2022-12-30 thomas }
1965 c669c489 2022-12-30 thomas
1966 c669c489 2022-12-30 thomas if (event & EV_WRITE) {
1967 c669c489 2022-12-30 thomas n = msgbuf_write(&ibuf->w);
1968 c669c489 2022-12-30 thomas if (n == -1 && errno != EAGAIN)
1969 c669c489 2022-12-30 thomas fatal("msgbuf_write");
1970 c669c489 2022-12-30 thomas if (n == 0) {
1971 c669c489 2022-12-30 thomas /* Connection closed. */
1972 c669c489 2022-12-30 thomas shut = 1;
1973 c669c489 2022-12-30 thomas }
1974 c669c489 2022-12-30 thomas goto done;
1975 c669c489 2022-12-30 thomas }
1976 c669c489 2022-12-30 thomas
1977 c669c489 2022-12-30 thomas if (client->auth->iev.ibuf.fd != fd)
1978 c669c489 2022-12-30 thomas fatalx("%s: unexpected fd %d", __func__, fd);
1979 c669c489 2022-12-30 thomas
1980 c669c489 2022-12-30 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1981 c669c489 2022-12-30 thomas fatal("%s: imsg_get error", __func__);
1982 c669c489 2022-12-30 thomas if (n == 0) /* No more messages. */
1983 c669c489 2022-12-30 thomas return;
1984 c669c489 2022-12-30 thomas
1985 c669c489 2022-12-30 thomas evtimer_del(&client->tmo);
1986 c669c489 2022-12-30 thomas
1987 c669c489 2022-12-30 thomas switch (imsg.hdr.type) {
1988 c669c489 2022-12-30 thomas case GOTD_IMSG_ERROR:
1989 c669c489 2022-12-30 thomas do_disconnect = 1;
1990 c669c489 2022-12-30 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
1991 c669c489 2022-12-30 thomas break;
1992 c669c489 2022-12-30 thomas case GOTD_IMSG_ACCESS_GRANTED:
1993 c669c489 2022-12-30 thomas break;
1994 c669c489 2022-12-30 thomas default:
1995 c669c489 2022-12-30 thomas do_disconnect = 1;
1996 c669c489 2022-12-30 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1997 c669c489 2022-12-30 thomas break;
1998 c669c489 2022-12-30 thomas }
1999 c669c489 2022-12-30 thomas
2000 c669c489 2022-12-30 thomas if (!verify_imsg_src(client, client->auth, &imsg)) {
2001 c669c489 2022-12-30 thomas do_disconnect = 1;
2002 c669c489 2022-12-30 thomas log_debug("dropping imsg type %d from PID %d",
2003 c669c489 2022-12-30 thomas imsg.hdr.type, client->auth->pid);
2004 c669c489 2022-12-30 thomas }
2005 c669c489 2022-12-30 thomas imsg_free(&imsg);
2006 c669c489 2022-12-30 thomas
2007 c669c489 2022-12-30 thomas if (do_disconnect) {
2008 c669c489 2022-12-30 thomas if (err)
2009 c669c489 2022-12-30 thomas disconnect_on_error(client, err);
2010 c669c489 2022-12-30 thomas else
2011 c669c489 2022-12-30 thomas disconnect(client);
2012 c669c489 2022-12-30 thomas goto done;
2013 c669c489 2022-12-30 thomas }
2014 c669c489 2022-12-30 thomas
2015 c669c489 2022-12-30 thomas repo = find_repo_by_name(client->auth->repo_name);
2016 c669c489 2022-12-30 thomas if (repo == NULL) {
2017 c669c489 2022-12-30 thomas err = got_error(GOT_ERR_NOT_GIT_REPO);
2018 c669c489 2022-12-30 thomas goto done;
2019 c669c489 2022-12-30 thomas }
2020 c669c489 2022-12-30 thomas kill_auth_proc(client);
2021 c669c489 2022-12-30 thomas
2022 c669c489 2022-12-30 thomas log_info("authenticated uid %d for repository %s\n",
2023 c669c489 2022-12-30 thomas client->euid, repo->name);
2024 c669c489 2022-12-30 thomas
2025 c669c489 2022-12-30 thomas if (client->required_auth & GOTD_AUTH_WRITE)
2026 c669c489 2022-12-30 thomas proc_type = PROC_REPO_WRITE;
2027 c669c489 2022-12-30 thomas else
2028 c669c489 2022-12-30 thomas proc_type = PROC_REPO_READ;
2029 c669c489 2022-12-30 thomas
2030 46ecc01f 2022-12-30 thomas err = start_repo_child(client, proc_type, repo, gotd.argv0,
2031 46ecc01f 2022-12-30 thomas gotd.confpath, gotd.daemonize, gotd.verbosity);
2032 c669c489 2022-12-30 thomas done:
2033 c669c489 2022-12-30 thomas if (err)
2034 c669c489 2022-12-30 thomas log_warnx("uid %d: %s", client->euid, err->msg);
2035 c669c489 2022-12-30 thomas
2036 c669c489 2022-12-30 thomas /* We might have killed the auth process by now. */
2037 c669c489 2022-12-30 thomas if (client->auth != NULL) {
2038 c669c489 2022-12-30 thomas if (!shut) {
2039 c669c489 2022-12-30 thomas gotd_imsg_event_add(iev);
2040 c669c489 2022-12-30 thomas } else {
2041 c669c489 2022-12-30 thomas /* This pipe is dead. Remove its event handler */
2042 c669c489 2022-12-30 thomas event_del(&iev->ev);
2043 c669c489 2022-12-30 thomas }
2044 c669c489 2022-12-30 thomas }
2045 c669c489 2022-12-30 thomas }
2046 c669c489 2022-12-30 thomas
2047 c669c489 2022-12-30 thomas static void
2048 85b37c72 2022-12-30 thomas gotd_dispatch_repo_child(int fd, short event, void *arg)
2049 85b37c72 2022-12-30 thomas {
2050 85b37c72 2022-12-30 thomas struct gotd_imsgev *iev = arg;
2051 85b37c72 2022-12-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
2052 3efd8e31 2022-10-23 thomas struct gotd_child_proc *proc = NULL;
2053 85b37c72 2022-12-30 thomas struct gotd_client *client = NULL;
2054 3efd8e31 2022-10-23 thomas ssize_t n;
2055 3efd8e31 2022-10-23 thomas int shut = 0;
2056 3efd8e31 2022-10-23 thomas struct imsg imsg;
2057 3efd8e31 2022-10-23 thomas
2058 3efd8e31 2022-10-23 thomas if (event & EV_READ) {
2059 3efd8e31 2022-10-23 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2060 3efd8e31 2022-10-23 thomas fatal("imsg_read error");
2061 3efd8e31 2022-10-23 thomas if (n == 0) {
2062 3efd8e31 2022-10-23 thomas /* Connection closed. */
2063 3efd8e31 2022-10-23 thomas shut = 1;
2064 3efd8e31 2022-10-23 thomas goto done;
2065 3efd8e31 2022-10-23 thomas }
2066 3efd8e31 2022-10-23 thomas }
2067 3efd8e31 2022-10-23 thomas
2068 3efd8e31 2022-10-23 thomas if (event & EV_WRITE) {
2069 3efd8e31 2022-10-23 thomas n = msgbuf_write(&ibuf->w);
2070 3efd8e31 2022-10-23 thomas if (n == -1 && errno != EAGAIN)
2071 3efd8e31 2022-10-23 thomas fatal("msgbuf_write");
2072 3efd8e31 2022-10-23 thomas if (n == 0) {
2073 3efd8e31 2022-10-23 thomas /* Connection closed. */
2074 3efd8e31 2022-10-23 thomas shut = 1;
2075 3efd8e31 2022-10-23 thomas goto done;
2076 3efd8e31 2022-10-23 thomas }
2077 3efd8e31 2022-10-23 thomas }
2078 3efd8e31 2022-10-23 thomas
2079 85b37c72 2022-12-30 thomas client = find_client_by_proc_fd(fd);
2080 85b37c72 2022-12-30 thomas if (client == NULL)
2081 85b37c72 2022-12-30 thomas fatalx("cannot find client for fd %d", fd);
2082 85b37c72 2022-12-30 thomas
2083 85b37c72 2022-12-30 thomas proc = get_client_proc(client);
2084 3efd8e31 2022-10-23 thomas if (proc == NULL)
2085 3efd8e31 2022-10-23 thomas fatalx("cannot find child process for fd %d", fd);
2086 3efd8e31 2022-10-23 thomas
2087 3efd8e31 2022-10-23 thomas for (;;) {
2088 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
2089 3efd8e31 2022-10-23 thomas uint32_t client_id = 0;
2090 3efd8e31 2022-10-23 thomas int do_disconnect = 0;
2091 85b37c72 2022-12-30 thomas int do_list_refs = 0, do_ref_updates = 0, do_ref_update = 0;
2092 3efd8e31 2022-10-23 thomas int do_packfile_install = 0;
2093 3efd8e31 2022-10-23 thomas
2094 3efd8e31 2022-10-23 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
2095 3efd8e31 2022-10-23 thomas fatal("%s: imsg_get error", __func__);
2096 3efd8e31 2022-10-23 thomas if (n == 0) /* No more messages. */
2097 3efd8e31 2022-10-23 thomas break;
2098 3efd8e31 2022-10-23 thomas
2099 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
2100 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
2101 3efd8e31 2022-10-23 thomas do_disconnect = 1;
2102 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
2103 3efd8e31 2022-10-23 thomas break;
2104 85b37c72 2022-12-30 thomas case GOTD_IMSG_REPO_CHILD_READY:
2105 85b37c72 2022-12-30 thomas do_list_refs = 1;
2106 2b3d32a1 2022-12-30 thomas break;
2107 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_DONE:
2108 3efd8e31 2022-10-23 thomas do_disconnect = 1;
2109 3efd8e31 2022-10-23 thomas err = recv_packfile_done(&client_id, &imsg);
2110 3efd8e31 2022-10-23 thomas break;
2111 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_INSTALL:
2112 3efd8e31 2022-10-23 thomas err = recv_packfile_install(&client_id, &imsg);
2113 3efd8e31 2022-10-23 thomas if (err == NULL)
2114 3efd8e31 2022-10-23 thomas do_packfile_install = 1;
2115 3efd8e31 2022-10-23 thomas break;
2116 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATES_START:
2117 3efd8e31 2022-10-23 thomas err = recv_ref_updates_start(&client_id, &imsg);
2118 3efd8e31 2022-10-23 thomas if (err == NULL)
2119 3efd8e31 2022-10-23 thomas do_ref_updates = 1;
2120 3efd8e31 2022-10-23 thomas break;
2121 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATE:
2122 3efd8e31 2022-10-23 thomas err = recv_ref_update(&client_id, &imsg);
2123 3efd8e31 2022-10-23 thomas if (err == NULL)
2124 3efd8e31 2022-10-23 thomas do_ref_update = 1;
2125 3efd8e31 2022-10-23 thomas break;
2126 3efd8e31 2022-10-23 thomas default:
2127 3efd8e31 2022-10-23 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
2128 3efd8e31 2022-10-23 thomas break;
2129 3efd8e31 2022-10-23 thomas }
2130 3efd8e31 2022-10-23 thomas
2131 3efd8e31 2022-10-23 thomas if (!verify_imsg_src(client, proc, &imsg)) {
2132 3efd8e31 2022-10-23 thomas log_debug("dropping imsg type %d from PID %d",
2133 3efd8e31 2022-10-23 thomas imsg.hdr.type, proc->pid);
2134 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
2135 3efd8e31 2022-10-23 thomas continue;
2136 3efd8e31 2022-10-23 thomas }
2137 3efd8e31 2022-10-23 thomas if (err)
2138 3efd8e31 2022-10-23 thomas log_warnx("uid %d: %s", client->euid, err->msg);
2139 3efd8e31 2022-10-23 thomas
2140 3efd8e31 2022-10-23 thomas if (do_disconnect) {
2141 3efd8e31 2022-10-23 thomas if (err)
2142 3efd8e31 2022-10-23 thomas disconnect_on_error(client, err);
2143 3efd8e31 2022-10-23 thomas else
2144 3efd8e31 2022-10-23 thomas disconnect(client);
2145 965fcba6 2022-11-04 thomas } else {
2146 85b37c72 2022-12-30 thomas if (do_list_refs)
2147 85b37c72 2022-12-30 thomas err = list_refs_request(client, iev);
2148 85b37c72 2022-12-30 thomas else if (do_packfile_install)
2149 414e37cb 2022-12-30 thomas err = install_pack(client, proc->repo_path,
2150 965fcba6 2022-11-04 thomas &imsg);
2151 965fcba6 2022-11-04 thomas else if (do_ref_updates)
2152 965fcba6 2022-11-04 thomas err = begin_ref_updates(client, &imsg);
2153 965fcba6 2022-11-04 thomas else if (do_ref_update)
2154 414e37cb 2022-12-30 thomas err = update_ref(client, proc->repo_path,
2155 965fcba6 2022-11-04 thomas &imsg);
2156 965fcba6 2022-11-04 thomas if (err)
2157 965fcba6 2022-11-04 thomas log_warnx("uid %d: %s", client->euid, err->msg);
2158 965fcba6 2022-11-04 thomas }
2159 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
2160 3efd8e31 2022-10-23 thomas }
2161 3efd8e31 2022-10-23 thomas done:
2162 3efd8e31 2022-10-23 thomas if (!shut) {
2163 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(iev);
2164 3efd8e31 2022-10-23 thomas } else {
2165 3efd8e31 2022-10-23 thomas /* This pipe is dead. Remove its event handler */
2166 3efd8e31 2022-10-23 thomas event_del(&iev->ev);
2167 3efd8e31 2022-10-23 thomas event_loopexit(NULL);
2168 3efd8e31 2022-10-23 thomas }
2169 3efd8e31 2022-10-23 thomas }
2170 3efd8e31 2022-10-23 thomas
2171 3efd8e31 2022-10-23 thomas static pid_t
2172 414e37cb 2022-12-30 thomas start_child(enum gotd_procid proc_id, const char *repo_path,
2173 832b8374 2022-10-31 thomas char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
2174 3efd8e31 2022-10-23 thomas {
2175 832b8374 2022-10-31 thomas char *argv[11];
2176 3efd8e31 2022-10-23 thomas int argc = 0;
2177 3efd8e31 2022-10-23 thomas pid_t pid;
2178 3efd8e31 2022-10-23 thomas
2179 3efd8e31 2022-10-23 thomas switch (pid = fork()) {
2180 3efd8e31 2022-10-23 thomas case -1:
2181 3efd8e31 2022-10-23 thomas fatal("cannot fork");
2182 3efd8e31 2022-10-23 thomas case 0:
2183 3efd8e31 2022-10-23 thomas break;
2184 3efd8e31 2022-10-23 thomas default:
2185 3efd8e31 2022-10-23 thomas close(fd);
2186 3efd8e31 2022-10-23 thomas return pid;
2187 3efd8e31 2022-10-23 thomas }
2188 3efd8e31 2022-10-23 thomas
2189 bb3a6ce9 2022-11-17 thomas if (fd != GOTD_FILENO_MSG_PIPE) {
2190 bb3a6ce9 2022-11-17 thomas if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
2191 3efd8e31 2022-10-23 thomas fatal("cannot setup imsg fd");
2192 3efd8e31 2022-10-23 thomas } else if (fcntl(fd, F_SETFD, 0) == -1)
2193 3efd8e31 2022-10-23 thomas fatal("cannot setup imsg fd");
2194 3efd8e31 2022-10-23 thomas
2195 3efd8e31 2022-10-23 thomas argv[argc++] = argv0;
2196 3efd8e31 2022-10-23 thomas switch (proc_id) {
2197 2b3d32a1 2022-12-30 thomas case PROC_LISTEN:
2198 2b3d32a1 2022-12-30 thomas argv[argc++] = (char *)"-L";
2199 2b3d32a1 2022-12-30 thomas break;
2200 c669c489 2022-12-30 thomas case PROC_AUTH:
2201 c669c489 2022-12-30 thomas argv[argc++] = (char *)"-A";
2202 c669c489 2022-12-30 thomas break;
2203 3efd8e31 2022-10-23 thomas case PROC_REPO_READ:
2204 3efd8e31 2022-10-23 thomas argv[argc++] = (char *)"-R";
2205 3efd8e31 2022-10-23 thomas break;
2206 3efd8e31 2022-10-23 thomas case PROC_REPO_WRITE:
2207 3efd8e31 2022-10-23 thomas argv[argc++] = (char *)"-W";
2208 3efd8e31 2022-10-23 thomas break;
2209 3efd8e31 2022-10-23 thomas default:
2210 3efd8e31 2022-10-23 thomas fatalx("invalid process id %d", proc_id);
2211 3efd8e31 2022-10-23 thomas }
2212 3efd8e31 2022-10-23 thomas
2213 832b8374 2022-10-31 thomas argv[argc++] = (char *)"-f";
2214 832b8374 2022-10-31 thomas argv[argc++] = (char *)confpath;
2215 832b8374 2022-10-31 thomas
2216 414e37cb 2022-12-30 thomas if (repo_path) {
2217 2b3d32a1 2022-12-30 thomas argv[argc++] = (char *)"-P";
2218 414e37cb 2022-12-30 thomas argv[argc++] = (char *)repo_path;
2219 2b3d32a1 2022-12-30 thomas }
2220 3efd8e31 2022-10-23 thomas
2221 3efd8e31 2022-10-23 thomas if (!daemonize)
2222 3efd8e31 2022-10-23 thomas argv[argc++] = (char *)"-d";
2223 3efd8e31 2022-10-23 thomas if (verbosity > 0)
2224 3efd8e31 2022-10-23 thomas argv[argc++] = (char *)"-v";
2225 3efd8e31 2022-10-23 thomas if (verbosity > 1)
2226 3efd8e31 2022-10-23 thomas argv[argc++] = (char *)"-v";
2227 3efd8e31 2022-10-23 thomas argv[argc++] = NULL;
2228 3efd8e31 2022-10-23 thomas
2229 3efd8e31 2022-10-23 thomas execvp(argv0, argv);
2230 3efd8e31 2022-10-23 thomas fatal("execvp");
2231 3efd8e31 2022-10-23 thomas }
2232 3efd8e31 2022-10-23 thomas
2233 3efd8e31 2022-10-23 thomas static void
2234 2b3d32a1 2022-12-30 thomas start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
2235 2b3d32a1 2022-12-30 thomas {
2236 85b37c72 2022-12-30 thomas struct gotd_child_proc *proc = &gotd.listen_proc;
2237 2b3d32a1 2022-12-30 thomas
2238 2b3d32a1 2022-12-30 thomas proc->type = PROC_LISTEN;
2239 2b3d32a1 2022-12-30 thomas
2240 2b3d32a1 2022-12-30 thomas if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2241 2b3d32a1 2022-12-30 thomas PF_UNSPEC, proc->pipe) == -1)
2242 2b3d32a1 2022-12-30 thomas fatal("socketpair");
2243 2b3d32a1 2022-12-30 thomas
2244 2b3d32a1 2022-12-30 thomas proc->pid = start_child(proc->type, NULL, argv0, confpath,
2245 2b3d32a1 2022-12-30 thomas proc->pipe[1], daemonize, verbosity);
2246 2b3d32a1 2022-12-30 thomas imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2247 85b37c72 2022-12-30 thomas proc->iev.handler = gotd_dispatch_listener;
2248 2b3d32a1 2022-12-30 thomas proc->iev.events = EV_READ;
2249 2b3d32a1 2022-12-30 thomas proc->iev.handler_arg = NULL;
2250 2b3d32a1 2022-12-30 thomas }
2251 2b3d32a1 2022-12-30 thomas
2252 85b37c72 2022-12-30 thomas static const struct got_error *
2253 85b37c72 2022-12-30 thomas start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
2254 85b37c72 2022-12-30 thomas struct gotd_repo *repo, char *argv0, const char *confpath,
2255 832b8374 2022-10-31 thomas int daemonize, int verbosity)
2256 3efd8e31 2022-10-23 thomas {
2257 3efd8e31 2022-10-23 thomas struct gotd_child_proc *proc;
2258 3efd8e31 2022-10-23 thomas
2259 85b37c72 2022-12-30 thomas if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
2260 85b37c72 2022-12-30 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
2261 46ecc01f 2022-12-30 thomas
2262 85b37c72 2022-12-30 thomas proc = calloc(1, sizeof(*proc));
2263 85b37c72 2022-12-30 thomas if (proc == NULL)
2264 85b37c72 2022-12-30 thomas return got_error_from_errno("calloc");
2265 3efd8e31 2022-10-23 thomas
2266 85b37c72 2022-12-30 thomas proc->type = proc_type;
2267 85b37c72 2022-12-30 thomas if (strlcpy(proc->repo_name, repo->name,
2268 85b37c72 2022-12-30 thomas sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2269 85b37c72 2022-12-30 thomas fatalx("repository name too long: %s", repo->name);
2270 85b37c72 2022-12-30 thomas log_debug("starting %s for repository %s",
2271 85b37c72 2022-12-30 thomas proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
2272 85b37c72 2022-12-30 thomas if (realpath(repo->path, proc->repo_path) == NULL)
2273 85b37c72 2022-12-30 thomas fatal("%s", repo->path);
2274 85b37c72 2022-12-30 thomas if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2275 85b37c72 2022-12-30 thomas PF_UNSPEC, proc->pipe) == -1)
2276 85b37c72 2022-12-30 thomas fatal("socketpair");
2277 85b37c72 2022-12-30 thomas proc->pid = start_child(proc->type, proc->repo_path, argv0,
2278 85b37c72 2022-12-30 thomas confpath, proc->pipe[1], daemonize, verbosity);
2279 85b37c72 2022-12-30 thomas imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2280 85b37c72 2022-12-30 thomas log_debug("proc %s %s is on fd %d",
2281 85b37c72 2022-12-30 thomas gotd_proc_names[proc->type], proc->repo_path,
2282 85b37c72 2022-12-30 thomas proc->pipe[0]);
2283 85b37c72 2022-12-30 thomas proc->iev.handler = gotd_dispatch_repo_child;
2284 85b37c72 2022-12-30 thomas proc->iev.events = EV_READ;
2285 85b37c72 2022-12-30 thomas proc->iev.handler_arg = NULL;
2286 85b37c72 2022-12-30 thomas event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2287 85b37c72 2022-12-30 thomas gotd_dispatch_repo_child, &proc->iev);
2288 85b37c72 2022-12-30 thomas gotd_imsg_event_add(&proc->iev);
2289 85b37c72 2022-12-30 thomas
2290 85b37c72 2022-12-30 thomas if (proc->type == PROC_REPO_READ)
2291 85b37c72 2022-12-30 thomas client->repo_read = proc;
2292 85b37c72 2022-12-30 thomas else
2293 85b37c72 2022-12-30 thomas client->repo_write = proc;
2294 c669c489 2022-12-30 thomas
2295 c669c489 2022-12-30 thomas return NULL;
2296 c669c489 2022-12-30 thomas }
2297 c669c489 2022-12-30 thomas
2298 c669c489 2022-12-30 thomas static const struct got_error *
2299 c669c489 2022-12-30 thomas start_auth_child(struct gotd_client *client, int required_auth,
2300 c669c489 2022-12-30 thomas struct gotd_repo *repo, char *argv0, const char *confpath,
2301 c669c489 2022-12-30 thomas int daemonize, int verbosity)
2302 c669c489 2022-12-30 thomas {
2303 0bcde4c8 2022-12-30 thomas const struct got_error *err = NULL;
2304 c669c489 2022-12-30 thomas struct gotd_child_proc *proc;
2305 c669c489 2022-12-30 thomas struct gotd_imsg_auth iauth;
2306 0bcde4c8 2022-12-30 thomas int fd;
2307 c669c489 2022-12-30 thomas
2308 c669c489 2022-12-30 thomas memset(&iauth, 0, sizeof(iauth));
2309 0bcde4c8 2022-12-30 thomas
2310 0bcde4c8 2022-12-30 thomas fd = dup(client->fd);
2311 0bcde4c8 2022-12-30 thomas if (fd == -1)
2312 0bcde4c8 2022-12-30 thomas return got_error_from_errno("dup");
2313 c669c489 2022-12-30 thomas
2314 c669c489 2022-12-30 thomas proc = calloc(1, sizeof(*proc));
2315 0bcde4c8 2022-12-30 thomas if (proc == NULL) {
2316 0bcde4c8 2022-12-30 thomas err = got_error_from_errno("calloc");
2317 0bcde4c8 2022-12-30 thomas close(fd);
2318 0bcde4c8 2022-12-30 thomas return err;
2319 0bcde4c8 2022-12-30 thomas }
2320 c669c489 2022-12-30 thomas
2321 c669c489 2022-12-30 thomas proc->type = PROC_AUTH;
2322 c669c489 2022-12-30 thomas if (strlcpy(proc->repo_name, repo->name,
2323 c669c489 2022-12-30 thomas sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2324 c669c489 2022-12-30 thomas fatalx("repository name too long: %s", repo->name);
2325 c669c489 2022-12-30 thomas log_debug("starting auth for uid %d repository %s",
2326 c669c489 2022-12-30 thomas client->euid, repo->name);
2327 c669c489 2022-12-30 thomas if (realpath(repo->path, proc->repo_path) == NULL)
2328 c669c489 2022-12-30 thomas fatal("%s", repo->path);
2329 c669c489 2022-12-30 thomas if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2330 c669c489 2022-12-30 thomas PF_UNSPEC, proc->pipe) == -1)
2331 c669c489 2022-12-30 thomas fatal("socketpair");
2332 c669c489 2022-12-30 thomas proc->pid = start_child(proc->type, proc->repo_path, argv0,
2333 c669c489 2022-12-30 thomas confpath, proc->pipe[1], daemonize, verbosity);
2334 c669c489 2022-12-30 thomas imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2335 c669c489 2022-12-30 thomas log_debug("proc %s %s is on fd %d",
2336 c669c489 2022-12-30 thomas gotd_proc_names[proc->type], proc->repo_path,
2337 c669c489 2022-12-30 thomas proc->pipe[0]);
2338 c669c489 2022-12-30 thomas proc->iev.handler = gotd_dispatch_auth_child;
2339 c669c489 2022-12-30 thomas proc->iev.events = EV_READ;
2340 c669c489 2022-12-30 thomas proc->iev.handler_arg = NULL;
2341 c669c489 2022-12-30 thomas event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2342 c669c489 2022-12-30 thomas gotd_dispatch_auth_child, &proc->iev);
2343 c669c489 2022-12-30 thomas gotd_imsg_event_add(&proc->iev);
2344 c669c489 2022-12-30 thomas
2345 c669c489 2022-12-30 thomas iauth.euid = client->euid;
2346 c669c489 2022-12-30 thomas iauth.egid = client->egid;
2347 c669c489 2022-12-30 thomas iauth.required_auth = required_auth;
2348 c669c489 2022-12-30 thomas iauth.client_id = client->id;
2349 c669c489 2022-12-30 thomas if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
2350 0bcde4c8 2022-12-30 thomas PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
2351 c669c489 2022-12-30 thomas log_warn("imsg compose AUTHENTICATE");
2352 0bcde4c8 2022-12-30 thomas close(fd);
2353 0bcde4c8 2022-12-30 thomas /* Let the auth_timeout handler tidy up. */
2354 0bcde4c8 2022-12-30 thomas }
2355 85b37c72 2022-12-30 thomas
2356 c669c489 2022-12-30 thomas client->auth = proc;
2357 c669c489 2022-12-30 thomas client->required_auth = required_auth;
2358 85b37c72 2022-12-30 thomas return NULL;
2359 414e37cb 2022-12-30 thomas }
2360 414e37cb 2022-12-30 thomas
2361 414e37cb 2022-12-30 thomas static void
2362 414e37cb 2022-12-30 thomas apply_unveil_repo_readonly(const char *repo_path)
2363 414e37cb 2022-12-30 thomas {
2364 414e37cb 2022-12-30 thomas if (unveil(repo_path, "r") == -1)
2365 414e37cb 2022-12-30 thomas fatal("unveil %s", repo_path);
2366 b942ab08 2022-12-30 thomas
2367 b942ab08 2022-12-30 thomas if (unveil(NULL, NULL) == -1)
2368 b942ab08 2022-12-30 thomas fatal("unveil");
2369 b942ab08 2022-12-30 thomas }
2370 b942ab08 2022-12-30 thomas
2371 b942ab08 2022-12-30 thomas static void
2372 b942ab08 2022-12-30 thomas apply_unveil_none(void)
2373 b942ab08 2022-12-30 thomas {
2374 b942ab08 2022-12-30 thomas if (unveil("/", "") == -1)
2375 b942ab08 2022-12-30 thomas fatal("unveil");
2376 414e37cb 2022-12-30 thomas
2377 414e37cb 2022-12-30 thomas if (unveil(NULL, NULL) == -1)
2378 414e37cb 2022-12-30 thomas fatal("unveil");
2379 3efd8e31 2022-10-23 thomas }
2380 3efd8e31 2022-10-23 thomas
2381 3efd8e31 2022-10-23 thomas static void
2382 3efd8e31 2022-10-23 thomas apply_unveil(void)
2383 3efd8e31 2022-10-23 thomas {
2384 3efd8e31 2022-10-23 thomas struct gotd_repo *repo;
2385 3efd8e31 2022-10-23 thomas
2386 85b37c72 2022-12-30 thomas if (unveil(gotd.argv0, "x") == -1)
2387 85b37c72 2022-12-30 thomas fatal("unveil %s", gotd.argv0);
2388 85b37c72 2022-12-30 thomas
2389 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(repo, &gotd.repos, entry) {
2390 3efd8e31 2022-10-23 thomas if (unveil(repo->path, "rwc") == -1)
2391 3efd8e31 2022-10-23 thomas fatal("unveil %s", repo->path);
2392 3efd8e31 2022-10-23 thomas }
2393 3efd8e31 2022-10-23 thomas
2394 3efd8e31 2022-10-23 thomas if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2395 3efd8e31 2022-10-23 thomas fatal("unveil %s", GOT_TMPDIR_STR);
2396 3efd8e31 2022-10-23 thomas
2397 3efd8e31 2022-10-23 thomas if (unveil(NULL, NULL) == -1)
2398 3efd8e31 2022-10-23 thomas fatal("unveil");
2399 3efd8e31 2022-10-23 thomas }
2400 3efd8e31 2022-10-23 thomas
2401 3efd8e31 2022-10-23 thomas int
2402 3efd8e31 2022-10-23 thomas main(int argc, char **argv)
2403 3efd8e31 2022-10-23 thomas {
2404 3efd8e31 2022-10-23 thomas const struct got_error *error = NULL;
2405 3efd8e31 2022-10-23 thomas int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2406 3efd8e31 2022-10-23 thomas const char *confpath = GOTD_CONF_PATH;
2407 3efd8e31 2022-10-23 thomas char *argv0 = argv[0];
2408 3efd8e31 2022-10-23 thomas char title[2048];
2409 ff260661 2022-11-17 thomas gid_t groups[NGROUPS_MAX];
2410 729a7e24 2022-11-17 thomas int ngroups = NGROUPS_MAX;
2411 3efd8e31 2022-10-23 thomas struct passwd *pw = NULL;
2412 3efd8e31 2022-10-23 thomas struct group *gr = NULL;
2413 3efd8e31 2022-10-23 thomas char *repo_path = NULL;
2414 3efd8e31 2022-10-23 thomas enum gotd_procid proc_id = PROC_GOTD;
2415 3efd8e31 2022-10-23 thomas struct event evsigint, evsigterm, evsighup, evsigusr1;
2416 3efd8e31 2022-10-23 thomas int *pack_fds = NULL, *temp_fds = NULL;
2417 3efd8e31 2022-10-23 thomas
2418 3efd8e31 2022-10-23 thomas log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2419 3efd8e31 2022-10-23 thomas
2420 c669c489 2022-12-30 thomas while ((ch = getopt(argc, argv, "Adf:LnP:RvW")) != -1) {
2421 3efd8e31 2022-10-23 thomas switch (ch) {
2422 c669c489 2022-12-30 thomas case 'A':
2423 c669c489 2022-12-30 thomas proc_id = PROC_AUTH;
2424 c669c489 2022-12-30 thomas break;
2425 3efd8e31 2022-10-23 thomas case 'd':
2426 3efd8e31 2022-10-23 thomas daemonize = 0;
2427 3efd8e31 2022-10-23 thomas break;
2428 3efd8e31 2022-10-23 thomas case 'f':
2429 3efd8e31 2022-10-23 thomas confpath = optarg;
2430 3efd8e31 2022-10-23 thomas break;
2431 2b3d32a1 2022-12-30 thomas case 'L':
2432 2b3d32a1 2022-12-30 thomas proc_id = PROC_LISTEN;
2433 2b3d32a1 2022-12-30 thomas break;
2434 3efd8e31 2022-10-23 thomas case 'n':
2435 3efd8e31 2022-10-23 thomas noaction = 1;
2436 3efd8e31 2022-10-23 thomas break;
2437 f7065961 2022-10-27 thomas case 'P':
2438 f7065961 2022-10-27 thomas repo_path = realpath(optarg, NULL);
2439 f7065961 2022-10-27 thomas if (repo_path == NULL)
2440 f7065961 2022-10-27 thomas fatal("realpath '%s'", optarg);
2441 3efd8e31 2022-10-23 thomas break;
2442 3efd8e31 2022-10-23 thomas case 'R':
2443 3efd8e31 2022-10-23 thomas proc_id = PROC_REPO_READ;
2444 3efd8e31 2022-10-23 thomas break;
2445 f7065961 2022-10-27 thomas case 'v':
2446 f7065961 2022-10-27 thomas if (verbosity < 3)
2447 f7065961 2022-10-27 thomas verbosity++;
2448 f7065961 2022-10-27 thomas break;
2449 3efd8e31 2022-10-23 thomas case 'W':
2450 3efd8e31 2022-10-23 thomas proc_id = PROC_REPO_WRITE;
2451 3efd8e31 2022-10-23 thomas break;
2452 3efd8e31 2022-10-23 thomas default:
2453 3efd8e31 2022-10-23 thomas usage();
2454 3efd8e31 2022-10-23 thomas }
2455 3efd8e31 2022-10-23 thomas }
2456 3efd8e31 2022-10-23 thomas
2457 3efd8e31 2022-10-23 thomas argc -= optind;
2458 3efd8e31 2022-10-23 thomas argv += optind;
2459 3efd8e31 2022-10-23 thomas
2460 3efd8e31 2022-10-23 thomas if (argc != 0)
2461 3efd8e31 2022-10-23 thomas usage();
2462 3efd8e31 2022-10-23 thomas
2463 85b37c72 2022-12-30 thomas /* Require an absolute path in argv[0] for reliable re-exec. */
2464 85b37c72 2022-12-30 thomas if (!got_path_is_absolute(argv0))
2465 85b37c72 2022-12-30 thomas fatalx("bad path \"%s\": must be an absolute path", argv0);
2466 85b37c72 2022-12-30 thomas
2467 85b37c72 2022-12-30 thomas if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
2468 3efd8e31 2022-10-23 thomas fatalx("need root privileges");
2469 3efd8e31 2022-10-23 thomas
2470 3efd8e31 2022-10-23 thomas log_init(daemonize ? 0 : 1, LOG_DAEMON);
2471 3efd8e31 2022-10-23 thomas log_setverbose(verbosity);
2472 3efd8e31 2022-10-23 thomas
2473 3efd8e31 2022-10-23 thomas if (parse_config(confpath, proc_id, &gotd) != 0)
2474 3efd8e31 2022-10-23 thomas return 1;
2475 3efd8e31 2022-10-23 thomas
2476 85b37c72 2022-12-30 thomas gotd.argv0 = argv0;
2477 85b37c72 2022-12-30 thomas gotd.daemonize = daemonize;
2478 85b37c72 2022-12-30 thomas gotd.verbosity = verbosity;
2479 85b37c72 2022-12-30 thomas gotd.confpath = confpath;
2480 85b37c72 2022-12-30 thomas
2481 3efd8e31 2022-10-23 thomas if (proc_id == PROC_GOTD &&
2482 3efd8e31 2022-10-23 thomas (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2483 3efd8e31 2022-10-23 thomas fatalx("no repository defined in configuration file");
2484 3efd8e31 2022-10-23 thomas
2485 3efd8e31 2022-10-23 thomas pw = getpwnam(gotd.user_name);
2486 3efd8e31 2022-10-23 thomas if (pw == NULL)
2487 3e7c54e1 2022-12-30 thomas fatalx("user %s not found", gotd.user_name);
2488 3efd8e31 2022-10-23 thomas
2489 3efd8e31 2022-10-23 thomas if (pw->pw_uid == 0) {
2490 3efd8e31 2022-10-23 thomas fatalx("cannot run %s as %s: the user running %s "
2491 3efd8e31 2022-10-23 thomas "must not be the superuser",
2492 3efd8e31 2022-10-23 thomas getprogname(), pw->pw_name, getprogname());
2493 3efd8e31 2022-10-23 thomas }
2494 3efd8e31 2022-10-23 thomas
2495 ff260661 2022-11-17 thomas if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
2496 3efd8e31 2022-10-23 thomas log_warnx("group membership list truncated");
2497 3efd8e31 2022-10-23 thomas
2498 ff260661 2022-11-17 thomas gr = match_group(groups, ngroups, gotd.unix_group_name);
2499 3efd8e31 2022-10-23 thomas if (gr == NULL) {
2500 3efd8e31 2022-10-23 thomas fatalx("cannot start %s: the user running %s "
2501 3efd8e31 2022-10-23 thomas "must be a secondary member of group %s",
2502 3efd8e31 2022-10-23 thomas getprogname(), getprogname(), gotd.unix_group_name);
2503 3efd8e31 2022-10-23 thomas }
2504 3efd8e31 2022-10-23 thomas if (gr->gr_gid == pw->pw_gid) {
2505 3efd8e31 2022-10-23 thomas fatalx("cannot start %s: the user running %s "
2506 3efd8e31 2022-10-23 thomas "must be a secondary member of group %s, but "
2507 3efd8e31 2022-10-23 thomas "%s is the user's primary group",
2508 3efd8e31 2022-10-23 thomas getprogname(), getprogname(), gotd.unix_group_name,
2509 3efd8e31 2022-10-23 thomas gotd.unix_group_name);
2510 3efd8e31 2022-10-23 thomas }
2511 3efd8e31 2022-10-23 thomas
2512 2b3d32a1 2022-12-30 thomas if (proc_id == PROC_LISTEN &&
2513 3efd8e31 2022-10-23 thomas !got_path_is_absolute(gotd.unix_socket_path))
2514 3efd8e31 2022-10-23 thomas fatalx("bad unix socket path \"%s\": must be an absolute path",
2515 3efd8e31 2022-10-23 thomas gotd.unix_socket_path);
2516 3efd8e31 2022-10-23 thomas
2517 3efd8e31 2022-10-23 thomas if (noaction)
2518 3efd8e31 2022-10-23 thomas return 0;
2519 3efd8e31 2022-10-23 thomas
2520 1eec6e4e 2022-12-06 thomas if (proc_id == PROC_GOTD) {
2521 2b3d32a1 2022-12-30 thomas gotd.pid = getpid();
2522 2b3d32a1 2022-12-30 thomas snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2523 2b3d32a1 2022-12-30 thomas start_listener(argv0, confpath, daemonize, verbosity);
2524 2b3d32a1 2022-12-30 thomas arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2525 2b3d32a1 2022-12-30 thomas if (daemonize && daemon(1, 0) == -1)
2526 2b3d32a1 2022-12-30 thomas fatal("daemon");
2527 2b3d32a1 2022-12-30 thomas } else if (proc_id == PROC_LISTEN) {
2528 2b3d32a1 2022-12-30 thomas snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2529 1eec6e4e 2022-12-06 thomas if (verbosity) {
2530 1eec6e4e 2022-12-06 thomas log_info("socket: %s", gotd.unix_socket_path);
2531 1eec6e4e 2022-12-06 thomas log_info("user: %s", pw->pw_name);
2532 1eec6e4e 2022-12-06 thomas log_info("secondary group: %s", gr->gr_name);
2533 1eec6e4e 2022-12-06 thomas }
2534 3efd8e31 2022-10-23 thomas
2535 3efd8e31 2022-10-23 thomas fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2536 3efd8e31 2022-10-23 thomas gr->gr_gid);
2537 3efd8e31 2022-10-23 thomas if (fd == -1) {
2538 3efd8e31 2022-10-23 thomas fatal("cannot listen on unix socket %s",
2539 3efd8e31 2022-10-23 thomas gotd.unix_socket_path);
2540 3efd8e31 2022-10-23 thomas }
2541 414e37cb 2022-12-30 thomas if (daemonize && daemon(0, 0) == -1)
2542 3efd8e31 2022-10-23 thomas fatal("daemon");
2543 c669c489 2022-12-30 thomas } else if (proc_id == PROC_AUTH) {
2544 c669c489 2022-12-30 thomas snprintf(title, sizeof(title), "%s %s",
2545 c669c489 2022-12-30 thomas gotd_proc_names[proc_id], repo_path);
2546 c669c489 2022-12-30 thomas if (daemonize && daemon(0, 0) == -1)
2547 c669c489 2022-12-30 thomas fatal("daemon");
2548 3efd8e31 2022-10-23 thomas } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2549 3efd8e31 2022-10-23 thomas error = got_repo_pack_fds_open(&pack_fds);
2550 3efd8e31 2022-10-23 thomas if (error != NULL)
2551 3efd8e31 2022-10-23 thomas fatalx("cannot open pack tempfiles: %s", error->msg);
2552 3efd8e31 2022-10-23 thomas error = got_repo_temp_fds_open(&temp_fds);
2553 3efd8e31 2022-10-23 thomas if (error != NULL)
2554 3efd8e31 2022-10-23 thomas fatalx("cannot open pack tempfiles: %s", error->msg);
2555 3efd8e31 2022-10-23 thomas if (repo_path == NULL)
2556 3efd8e31 2022-10-23 thomas fatalx("repository path not specified");
2557 3efd8e31 2022-10-23 thomas snprintf(title, sizeof(title), "%s %s",
2558 3efd8e31 2022-10-23 thomas gotd_proc_names[proc_id], repo_path);
2559 414e37cb 2022-12-30 thomas if (daemonize && daemon(0, 0) == -1)
2560 3efd8e31 2022-10-23 thomas fatal("daemon");
2561 3efd8e31 2022-10-23 thomas } else
2562 3efd8e31 2022-10-23 thomas fatal("invalid process id %d", proc_id);
2563 3efd8e31 2022-10-23 thomas
2564 3efd8e31 2022-10-23 thomas setproctitle("%s", title);
2565 3efd8e31 2022-10-23 thomas log_procinit(title);
2566 3efd8e31 2022-10-23 thomas
2567 3efd8e31 2022-10-23 thomas /* Drop root privileges. */
2568 3efd8e31 2022-10-23 thomas if (setgid(pw->pw_gid) == -1)
2569 3efd8e31 2022-10-23 thomas fatal("setgid %d failed", pw->pw_gid);
2570 3efd8e31 2022-10-23 thomas if (setuid(pw->pw_uid) == -1)
2571 3efd8e31 2022-10-23 thomas fatal("setuid %d failed", pw->pw_uid);
2572 3efd8e31 2022-10-23 thomas
2573 3efd8e31 2022-10-23 thomas event_init();
2574 3efd8e31 2022-10-23 thomas
2575 3efd8e31 2022-10-23 thomas switch (proc_id) {
2576 3efd8e31 2022-10-23 thomas case PROC_GOTD:
2577 3efd8e31 2022-10-23 thomas #ifndef PROFILE
2578 c669c489 2022-12-30 thomas if (pledge("stdio rpath wpath cpath proc exec "
2579 0bcde4c8 2022-12-30 thomas "sendfd recvfd fattr flock unveil", NULL) == -1)
2580 3efd8e31 2022-10-23 thomas err(1, "pledge");
2581 3efd8e31 2022-10-23 thomas #endif
2582 3efd8e31 2022-10-23 thomas break;
2583 2b3d32a1 2022-12-30 thomas case PROC_LISTEN:
2584 2b3d32a1 2022-12-30 thomas #ifndef PROFILE
2585 2b3d32a1 2022-12-30 thomas if (pledge("stdio sendfd unix", NULL) == -1)
2586 2b3d32a1 2022-12-30 thomas err(1, "pledge");
2587 2b3d32a1 2022-12-30 thomas #endif
2588 2b3d32a1 2022-12-30 thomas listen_main(title, fd);
2589 2b3d32a1 2022-12-30 thomas /* NOTREACHED */
2590 2b3d32a1 2022-12-30 thomas break;
2591 c669c489 2022-12-30 thomas case PROC_AUTH:
2592 c669c489 2022-12-30 thomas #ifndef PROFILE
2593 b942ab08 2022-12-30 thomas if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
2594 c669c489 2022-12-30 thomas err(1, "pledge");
2595 c669c489 2022-12-30 thomas #endif
2596 b942ab08 2022-12-30 thomas /*
2597 b942ab08 2022-12-30 thomas * We need the "unix" pledge promise for getpeername(2) only.
2598 b942ab08 2022-12-30 thomas * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2599 b942ab08 2022-12-30 thomas * filesystem access via unveil(2). Access to password database
2600 b942ab08 2022-12-30 thomas * files will still work since "getpw" bypasses unveil(2).
2601 b942ab08 2022-12-30 thomas */
2602 b942ab08 2022-12-30 thomas apply_unveil_none();
2603 b942ab08 2022-12-30 thomas
2604 c669c489 2022-12-30 thomas auth_main(title, &gotd.repos, repo_path);
2605 c669c489 2022-12-30 thomas /* NOTREACHED */
2606 c669c489 2022-12-30 thomas break;
2607 3efd8e31 2022-10-23 thomas case PROC_REPO_READ:
2608 3efd8e31 2022-10-23 thomas #ifndef PROFILE
2609 414e37cb 2022-12-30 thomas if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2610 3efd8e31 2022-10-23 thomas err(1, "pledge");
2611 3efd8e31 2022-10-23 thomas #endif
2612 414e37cb 2022-12-30 thomas apply_unveil_repo_readonly(repo_path);
2613 414e37cb 2022-12-30 thomas repo_read_main(title, repo_path, pack_fds, temp_fds);
2614 3efd8e31 2022-10-23 thomas /* NOTREACHED */
2615 3efd8e31 2022-10-23 thomas exit(0);
2616 3efd8e31 2022-10-23 thomas case PROC_REPO_WRITE:
2617 3efd8e31 2022-10-23 thomas #ifndef PROFILE
2618 414e37cb 2022-12-30 thomas if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2619 3efd8e31 2022-10-23 thomas err(1, "pledge");
2620 3efd8e31 2022-10-23 thomas #endif
2621 414e37cb 2022-12-30 thomas apply_unveil_repo_readonly(repo_path);
2622 414e37cb 2022-12-30 thomas repo_write_main(title, repo_path, pack_fds, temp_fds);
2623 3efd8e31 2022-10-23 thomas /* NOTREACHED */
2624 3efd8e31 2022-10-23 thomas exit(0);
2625 3efd8e31 2022-10-23 thomas default:
2626 3efd8e31 2022-10-23 thomas fatal("invalid process id %d", proc_id);
2627 3efd8e31 2022-10-23 thomas }
2628 3efd8e31 2022-10-23 thomas
2629 3efd8e31 2022-10-23 thomas if (proc_id != PROC_GOTD)
2630 3efd8e31 2022-10-23 thomas fatal("invalid process id %d", proc_id);
2631 3efd8e31 2022-10-23 thomas
2632 3efd8e31 2022-10-23 thomas apply_unveil();
2633 3efd8e31 2022-10-23 thomas
2634 3efd8e31 2022-10-23 thomas signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2635 3efd8e31 2022-10-23 thomas signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2636 3efd8e31 2022-10-23 thomas signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2637 3efd8e31 2022-10-23 thomas signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2638 3efd8e31 2022-10-23 thomas signal(SIGPIPE, SIG_IGN);
2639 3efd8e31 2022-10-23 thomas
2640 3efd8e31 2022-10-23 thomas signal_add(&evsigint, NULL);
2641 3efd8e31 2022-10-23 thomas signal_add(&evsigterm, NULL);
2642 3efd8e31 2022-10-23 thomas signal_add(&evsighup, NULL);
2643 3efd8e31 2022-10-23 thomas signal_add(&evsigusr1, NULL);
2644 3efd8e31 2022-10-23 thomas
2645 85b37c72 2022-12-30 thomas gotd_imsg_event_add(&gotd.listen_proc.iev);
2646 3efd8e31 2022-10-23 thomas
2647 3efd8e31 2022-10-23 thomas event_dispatch();
2648 3efd8e31 2022-10-23 thomas
2649 3efd8e31 2022-10-23 thomas if (pack_fds)
2650 3efd8e31 2022-10-23 thomas got_repo_pack_fds_close(pack_fds);
2651 3efd8e31 2022-10-23 thomas free(repo_path);
2652 3efd8e31 2022-10-23 thomas return 0;
2653 3efd8e31 2022-10-23 thomas }