Blame


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