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