2 62ee7d94 2023-01-10 thomas * Copyright (c) 2022, 2023 Stefan Sperling <stsp@openbsd.org>
4 62ee7d94 2023-01-10 thomas * Permission to use, copy, modify, and distribute this software for any
5 62ee7d94 2023-01-10 thomas * purpose with or without fee is hereby granted, provided that the above
6 62ee7d94 2023-01-10 thomas * copyright notice and this permission notice appear in all copies.
8 62ee7d94 2023-01-10 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 62ee7d94 2023-01-10 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 62ee7d94 2023-01-10 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 62ee7d94 2023-01-10 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 62ee7d94 2023-01-10 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 62ee7d94 2023-01-10 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 62ee7d94 2023-01-10 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 62ee7d94 2023-01-10 thomas #include <sys/types.h>
18 62ee7d94 2023-01-10 thomas #include <sys/queue.h>
19 62ee7d94 2023-01-10 thomas #include <sys/tree.h>
20 62ee7d94 2023-01-10 thomas #include <sys/socket.h>
21 62ee7d94 2023-01-10 thomas #include <sys/uio.h>
23 62ee7d94 2023-01-10 thomas #include <errno.h>
24 62ee7d94 2023-01-10 thomas #include <event.h>
25 62ee7d94 2023-01-10 thomas #include <limits.h>
26 62ee7d94 2023-01-10 thomas #include <sha1.h>
27 62ee7d94 2023-01-10 thomas #include <signal.h>
28 62ee7d94 2023-01-10 thomas #include <stdint.h>
29 62ee7d94 2023-01-10 thomas #include <stdio.h>
30 62ee7d94 2023-01-10 thomas #include <stdlib.h>
31 62ee7d94 2023-01-10 thomas #include <string.h>
32 62ee7d94 2023-01-10 thomas #include <imsg.h>
33 62ee7d94 2023-01-10 thomas #include <unistd.h>
35 62ee7d94 2023-01-10 thomas #include "got_error.h"
36 62ee7d94 2023-01-10 thomas #include "got_repository.h"
37 62ee7d94 2023-01-10 thomas #include "got_object.h"
38 62ee7d94 2023-01-10 thomas #include "got_path.h"
39 62ee7d94 2023-01-10 thomas #include "got_reference.h"
40 62ee7d94 2023-01-10 thomas #include "got_opentemp.h"
42 62ee7d94 2023-01-10 thomas #include "got_lib_sha1.h"
43 62ee7d94 2023-01-10 thomas #include "got_lib_delta.h"
44 62ee7d94 2023-01-10 thomas #include "got_lib_object.h"
45 62ee7d94 2023-01-10 thomas #include "got_lib_object_cache.h"
46 62ee7d94 2023-01-10 thomas #include "got_lib_pack.h"
47 62ee7d94 2023-01-10 thomas #include "got_lib_repository.h"
48 62ee7d94 2023-01-10 thomas #include "got_lib_gitproto.h"
50 62ee7d94 2023-01-10 thomas #include "gotd.h"
51 62ee7d94 2023-01-10 thomas #include "log.h"
52 62ee7d94 2023-01-10 thomas #include "session.h"
55 62ee7d94 2023-01-10 thomas static struct gotd_session {
56 62ee7d94 2023-01-10 thomas pid_t pid;
57 62ee7d94 2023-01-10 thomas const char *title;
58 62ee7d94 2023-01-10 thomas struct got_repository *repo;
59 62ee7d94 2023-01-10 thomas int *pack_fds;
60 62ee7d94 2023-01-10 thomas int *temp_fds;
61 62ee7d94 2023-01-10 thomas struct gotd_imsgev parent_iev;
62 62ee7d94 2023-01-10 thomas struct timeval request_timeout;
63 62ee7d94 2023-01-10 thomas } gotd_session;
65 62ee7d94 2023-01-10 thomas static struct gotd_session_client {
66 7b1db75e 2023-01-14 thomas enum gotd_session_state state;
67 62ee7d94 2023-01-10 thomas int is_writing;
68 62ee7d94 2023-01-10 thomas struct gotd_client_capability *capabilities;
69 62ee7d94 2023-01-10 thomas size_t ncapa_alloc;
70 62ee7d94 2023-01-10 thomas size_t ncapabilities;
71 62ee7d94 2023-01-10 thomas uint32_t id;
73 62ee7d94 2023-01-10 thomas int delta_cache_fd;
74 62ee7d94 2023-01-10 thomas struct gotd_imsgev iev;
75 62ee7d94 2023-01-10 thomas struct gotd_imsgev repo_child_iev;
76 62ee7d94 2023-01-10 thomas struct event tmo;
77 62ee7d94 2023-01-10 thomas uid_t euid;
78 62ee7d94 2023-01-10 thomas gid_t egid;
79 62ee7d94 2023-01-10 thomas char *packfile_path;
80 62ee7d94 2023-01-10 thomas char *packidx_path;
81 62ee7d94 2023-01-10 thomas int nref_updates;
82 98c7fd82 2023-01-23 thomas int accept_flush_pkt;
83 62ee7d94 2023-01-10 thomas } gotd_session_client;
85 62ee7d94 2023-01-10 thomas void gotd_session_sighdlr(int sig, short event, void *arg);
86 62ee7d94 2023-01-10 thomas static void gotd_session_shutdown(void);
88 62ee7d94 2023-01-10 thomas static void
89 62ee7d94 2023-01-10 thomas disconnect(struct gotd_session_client *client)
91 62ee7d94 2023-01-10 thomas log_debug("uid %d: disconnecting", client->euid);
93 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&gotd_session.parent_iev,
94 62ee7d94 2023-01-10 thomas GOTD_IMSG_DISCONNECT, PROC_SESSION, -1, NULL, 0) == -1)
95 62ee7d94 2023-01-10 thomas log_warn("imsg compose DISCONNECT");
97 62ee7d94 2023-01-10 thomas imsg_clear(&client->repo_child_iev.ibuf);
98 62ee7d94 2023-01-10 thomas event_del(&client->repo_child_iev.ev);
99 62ee7d94 2023-01-10 thomas evtimer_del(&client->tmo);
100 62ee7d94 2023-01-10 thomas close(client->fd);
101 62ee7d94 2023-01-10 thomas if (client->delta_cache_fd != -1)
102 62ee7d94 2023-01-10 thomas close(client->delta_cache_fd);
103 62ee7d94 2023-01-10 thomas if (client->packfile_path) {
104 62ee7d94 2023-01-10 thomas if (unlink(client->packfile_path) == -1 && errno != ENOENT)
105 62ee7d94 2023-01-10 thomas log_warn("unlink %s: ", client->packfile_path);
106 62ee7d94 2023-01-10 thomas free(client->packfile_path);
108 62ee7d94 2023-01-10 thomas if (client->packidx_path) {
109 62ee7d94 2023-01-10 thomas if (unlink(client->packidx_path) == -1 && errno != ENOENT)
110 62ee7d94 2023-01-10 thomas log_warn("unlink %s: ", client->packidx_path);
111 62ee7d94 2023-01-10 thomas free(client->packidx_path);
113 62ee7d94 2023-01-10 thomas free(client->capabilities);
115 62ee7d94 2023-01-10 thomas gotd_session_shutdown();
118 62ee7d94 2023-01-10 thomas static void
119 62ee7d94 2023-01-10 thomas disconnect_on_error(struct gotd_session_client *client,
120 62ee7d94 2023-01-10 thomas const struct got_error *err)
122 62ee7d94 2023-01-10 thomas struct imsgbuf ibuf;
124 62ee7d94 2023-01-10 thomas log_warnx("uid %d: %s", client->euid, err->msg);
125 62ee7d94 2023-01-10 thomas if (err->code != GOT_ERR_EOF) {
126 62ee7d94 2023-01-10 thomas imsg_init(&ibuf, client->fd);
127 62ee7d94 2023-01-10 thomas gotd_imsg_send_error(&ibuf, 0, PROC_SESSION, err);
128 62ee7d94 2023-01-10 thomas imsg_clear(&ibuf);
131 62ee7d94 2023-01-10 thomas disconnect(client);
134 62ee7d94 2023-01-10 thomas static void
135 62ee7d94 2023-01-10 thomas gotd_request_timeout(int fd, short events, void *arg)
137 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = arg;
139 62ee7d94 2023-01-10 thomas log_debug("disconnecting uid %d due to timeout", client->euid);
140 62ee7d94 2023-01-10 thomas disconnect(client);
144 62ee7d94 2023-01-10 thomas gotd_session_sighdlr(int sig, short event, void *arg)
147 62ee7d94 2023-01-10 thomas * Normal signal handler rules don't apply because libevent
148 62ee7d94 2023-01-10 thomas * decouples for us.
151 62ee7d94 2023-01-10 thomas switch (sig) {
152 62ee7d94 2023-01-10 thomas case SIGHUP:
153 62ee7d94 2023-01-10 thomas log_info("%s: ignoring SIGHUP", __func__);
155 62ee7d94 2023-01-10 thomas case SIGUSR1:
156 62ee7d94 2023-01-10 thomas log_info("%s: ignoring SIGUSR1", __func__);
158 62ee7d94 2023-01-10 thomas case SIGTERM:
159 62ee7d94 2023-01-10 thomas case SIGINT:
160 62ee7d94 2023-01-10 thomas gotd_session_shutdown();
161 62ee7d94 2023-01-10 thomas /* NOTREACHED */
164 62ee7d94 2023-01-10 thomas fatalx("unexpected signal");
168 62ee7d94 2023-01-10 thomas static const struct got_error *
169 62ee7d94 2023-01-10 thomas recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
171 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_done idone;
172 62ee7d94 2023-01-10 thomas size_t datalen;
174 62ee7d94 2023-01-10 thomas log_debug("packfile-done received");
176 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
177 62ee7d94 2023-01-10 thomas if (datalen != sizeof(idone))
178 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
179 62ee7d94 2023-01-10 thomas memcpy(&idone, imsg->data, sizeof(idone));
181 62ee7d94 2023-01-10 thomas *client_id = idone.client_id;
182 62ee7d94 2023-01-10 thomas return NULL;
185 62ee7d94 2023-01-10 thomas static const struct got_error *
186 62ee7d94 2023-01-10 thomas recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
188 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_install inst;
189 62ee7d94 2023-01-10 thomas size_t datalen;
191 62ee7d94 2023-01-10 thomas log_debug("packfile-install received");
193 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
194 62ee7d94 2023-01-10 thomas if (datalen != sizeof(inst))
195 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
196 62ee7d94 2023-01-10 thomas memcpy(&inst, imsg->data, sizeof(inst));
198 62ee7d94 2023-01-10 thomas *client_id = inst.client_id;
199 62ee7d94 2023-01-10 thomas return NULL;
202 62ee7d94 2023-01-10 thomas static const struct got_error *
203 62ee7d94 2023-01-10 thomas recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
205 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_updates_start istart;
206 62ee7d94 2023-01-10 thomas size_t datalen;
208 62ee7d94 2023-01-10 thomas log_debug("ref-updates-start received");
210 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
211 62ee7d94 2023-01-10 thomas if (datalen != sizeof(istart))
212 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
213 62ee7d94 2023-01-10 thomas memcpy(&istart, imsg->data, sizeof(istart));
215 62ee7d94 2023-01-10 thomas *client_id = istart.client_id;
216 62ee7d94 2023-01-10 thomas return NULL;
219 62ee7d94 2023-01-10 thomas static const struct got_error *
220 62ee7d94 2023-01-10 thomas recv_ref_update(uint32_t *client_id, struct imsg *imsg)
222 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update iref;
223 62ee7d94 2023-01-10 thomas size_t datalen;
225 62ee7d94 2023-01-10 thomas log_debug("ref-update received");
227 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
228 62ee7d94 2023-01-10 thomas if (datalen < sizeof(iref))
229 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
230 62ee7d94 2023-01-10 thomas memcpy(&iref, imsg->data, sizeof(iref));
232 62ee7d94 2023-01-10 thomas *client_id = iref.client_id;
233 62ee7d94 2023-01-10 thomas return NULL;
236 62ee7d94 2023-01-10 thomas static const struct got_error *
237 62ee7d94 2023-01-10 thomas send_ref_update_ok(struct gotd_session_client *client,
238 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update *iref, const char *refname)
240 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update_ok iok;
241 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &client->iev;
242 62ee7d94 2023-01-10 thomas struct ibuf *wbuf;
243 62ee7d94 2023-01-10 thomas size_t len;
245 62ee7d94 2023-01-10 thomas memset(&iok, 0, sizeof(iok));
246 62ee7d94 2023-01-10 thomas iok.client_id = client->id;
247 62ee7d94 2023-01-10 thomas memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
248 62ee7d94 2023-01-10 thomas memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
249 62ee7d94 2023-01-10 thomas iok.name_len = strlen(refname);
251 62ee7d94 2023-01-10 thomas len = sizeof(iok) + iok.name_len;
252 62ee7d94 2023-01-10 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_OK,
253 62ee7d94 2023-01-10 thomas PROC_SESSION, gotd_session.pid, len);
254 62ee7d94 2023-01-10 thomas if (wbuf == NULL)
255 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_create REF_UPDATE_OK");
257 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
258 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_OK");
259 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, refname, iok.name_len) == -1)
260 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_OK");
262 62ee7d94 2023-01-10 thomas wbuf->fd = -1;
263 62ee7d94 2023-01-10 thomas imsg_close(&iev->ibuf, wbuf);
264 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
265 62ee7d94 2023-01-10 thomas return NULL;
268 62ee7d94 2023-01-10 thomas static void
269 62ee7d94 2023-01-10 thomas send_refs_updated(struct gotd_session_client *client)
271 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
272 62ee7d94 2023-01-10 thomas PROC_SESSION, -1, NULL, 0) == -1)
273 62ee7d94 2023-01-10 thomas log_warn("imsg compose REFS_UPDATED");
276 62ee7d94 2023-01-10 thomas static const struct got_error *
277 62ee7d94 2023-01-10 thomas send_ref_update_ng(struct gotd_session_client *client,
278 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update *iref, const char *refname,
279 62ee7d94 2023-01-10 thomas const char *reason)
281 62ee7d94 2023-01-10 thomas const struct got_error *ng_err;
282 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update_ng ing;
283 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &client->iev;
284 62ee7d94 2023-01-10 thomas struct ibuf *wbuf;
285 62ee7d94 2023-01-10 thomas size_t len;
287 62ee7d94 2023-01-10 thomas memset(&ing, 0, sizeof(ing));
288 62ee7d94 2023-01-10 thomas ing.client_id = client->id;
289 62ee7d94 2023-01-10 thomas memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
290 62ee7d94 2023-01-10 thomas memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
291 62ee7d94 2023-01-10 thomas ing.name_len = strlen(refname);
293 62ee7d94 2023-01-10 thomas ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
294 62ee7d94 2023-01-10 thomas ing.reason_len = strlen(ng_err->msg);
296 62ee7d94 2023-01-10 thomas len = sizeof(ing) + ing.name_len + ing.reason_len;
297 62ee7d94 2023-01-10 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_NG,
298 62ee7d94 2023-01-10 thomas PROC_SESSION, gotd_session.pid, len);
299 62ee7d94 2023-01-10 thomas if (wbuf == NULL)
300 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_create REF_UPDATE_NG");
302 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
303 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
304 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, refname, ing.name_len) == -1)
305 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
306 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
307 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
309 62ee7d94 2023-01-10 thomas wbuf->fd = -1;
310 62ee7d94 2023-01-10 thomas imsg_close(&iev->ibuf, wbuf);
311 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
312 62ee7d94 2023-01-10 thomas return NULL;
315 62ee7d94 2023-01-10 thomas static const struct got_error *
316 62ee7d94 2023-01-10 thomas install_pack(struct gotd_session_client *client, const char *repo_path,
317 62ee7d94 2023-01-10 thomas struct imsg *imsg)
319 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
320 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_install inst;
321 62ee7d94 2023-01-10 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
322 62ee7d94 2023-01-10 thomas size_t datalen;
323 62ee7d94 2023-01-10 thomas char *packfile_path = NULL, *packidx_path = NULL;
325 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
326 62ee7d94 2023-01-10 thomas if (datalen != sizeof(inst))
327 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
328 62ee7d94 2023-01-10 thomas memcpy(&inst, imsg->data, sizeof(inst));
330 62ee7d94 2023-01-10 thomas if (client->packfile_path == NULL)
331 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
332 62ee7d94 2023-01-10 thomas "client has no pack file");
333 62ee7d94 2023-01-10 thomas if (client->packidx_path == NULL)
334 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
335 62ee7d94 2023-01-10 thomas "client has no pack file index");
337 62ee7d94 2023-01-10 thomas if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
338 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_NO_SPACE,
339 62ee7d94 2023-01-10 thomas "could not convert pack file SHA1 to hex");
341 62ee7d94 2023-01-10 thomas if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
342 62ee7d94 2023-01-10 thomas repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
343 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
344 62ee7d94 2023-01-10 thomas goto done;
347 62ee7d94 2023-01-10 thomas if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
348 62ee7d94 2023-01-10 thomas repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
349 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
350 62ee7d94 2023-01-10 thomas goto done;
353 62ee7d94 2023-01-10 thomas if (rename(client->packfile_path, packfile_path) == -1) {
354 62ee7d94 2023-01-10 thomas err = got_error_from_errno3("rename", client->packfile_path,
355 62ee7d94 2023-01-10 thomas packfile_path);
356 62ee7d94 2023-01-10 thomas goto done;
359 62ee7d94 2023-01-10 thomas free(client->packfile_path);
360 62ee7d94 2023-01-10 thomas client->packfile_path = NULL;
362 62ee7d94 2023-01-10 thomas if (rename(client->packidx_path, packidx_path) == -1) {
363 62ee7d94 2023-01-10 thomas err = got_error_from_errno3("rename", client->packidx_path,
364 62ee7d94 2023-01-10 thomas packidx_path);
365 62ee7d94 2023-01-10 thomas goto done;
368 62ee7d94 2023-01-10 thomas free(client->packidx_path);
369 62ee7d94 2023-01-10 thomas client->packidx_path = NULL;
371 62ee7d94 2023-01-10 thomas free(packfile_path);
372 62ee7d94 2023-01-10 thomas free(packidx_path);
373 62ee7d94 2023-01-10 thomas return err;
376 62ee7d94 2023-01-10 thomas static const struct got_error *
377 62ee7d94 2023-01-10 thomas begin_ref_updates(struct gotd_session_client *client, struct imsg *imsg)
379 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_updates_start istart;
380 62ee7d94 2023-01-10 thomas size_t datalen;
382 62ee7d94 2023-01-10 thomas if (client->nref_updates != -1)
383 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
385 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
386 62ee7d94 2023-01-10 thomas if (datalen != sizeof(istart))
387 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
388 62ee7d94 2023-01-10 thomas memcpy(&istart, imsg->data, sizeof(istart));
390 62ee7d94 2023-01-10 thomas if (istart.nref_updates <= 0)
391 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
393 62ee7d94 2023-01-10 thomas client->nref_updates = istart.nref_updates;
394 62ee7d94 2023-01-10 thomas return NULL;
397 62ee7d94 2023-01-10 thomas static const struct got_error *
398 d98779cd 2023-01-19 thomas update_ref(int *shut, struct gotd_session_client *client,
399 d98779cd 2023-01-19 thomas const char *repo_path, struct imsg *imsg)
401 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
402 62ee7d94 2023-01-10 thomas struct got_repository *repo = NULL;
403 62ee7d94 2023-01-10 thomas struct got_reference *ref = NULL;
404 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update iref;
405 62ee7d94 2023-01-10 thomas struct got_object_id old_id, new_id;
406 62ee7d94 2023-01-10 thomas struct got_object_id *id = NULL;
407 62ee7d94 2023-01-10 thomas struct got_object *obj = NULL;
408 62ee7d94 2023-01-10 thomas char *refname = NULL;
409 62ee7d94 2023-01-10 thomas size_t datalen;
410 62ee7d94 2023-01-10 thomas int locked = 0;
412 62ee7d94 2023-01-10 thomas log_debug("update-ref from uid %d", client->euid);
414 62ee7d94 2023-01-10 thomas if (client->nref_updates <= 0)
415 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
417 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
418 62ee7d94 2023-01-10 thomas if (datalen < sizeof(iref))
419 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
420 62ee7d94 2023-01-10 thomas memcpy(&iref, imsg->data, sizeof(iref));
421 62ee7d94 2023-01-10 thomas if (datalen != sizeof(iref) + iref.name_len)
422 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
423 fcbb06bf 2023-01-14 thomas refname = strndup(imsg->data + sizeof(iref), iref.name_len);
424 62ee7d94 2023-01-10 thomas if (refname == NULL)
425 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
427 62ee7d94 2023-01-10 thomas log_debug("updating ref %s for uid %d", refname, client->euid);
429 62ee7d94 2023-01-10 thomas err = got_repo_open(&repo, repo_path, NULL, NULL);
431 62ee7d94 2023-01-10 thomas goto done;
433 62ee7d94 2023-01-10 thomas memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
434 62ee7d94 2023-01-10 thomas memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
435 49563dfb 2023-01-28 thomas err = got_object_open(&obj, repo,
436 49563dfb 2023-01-28 thomas iref.delete_ref ? &old_id : &new_id);
438 62ee7d94 2023-01-10 thomas goto done;
440 62ee7d94 2023-01-10 thomas if (iref.ref_is_new) {
441 62ee7d94 2023-01-10 thomas err = got_ref_open(&ref, repo, refname, 0);
442 62ee7d94 2023-01-10 thomas if (err) {
443 62ee7d94 2023-01-10 thomas if (err->code != GOT_ERR_NOT_REF)
444 62ee7d94 2023-01-10 thomas goto done;
445 62ee7d94 2023-01-10 thomas err = got_ref_alloc(&ref, refname, &new_id);
447 62ee7d94 2023-01-10 thomas goto done;
448 62ee7d94 2023-01-10 thomas err = got_ref_write(ref, repo); /* will lock/unlock */
450 62ee7d94 2023-01-10 thomas goto done;
452 62ee7d94 2023-01-10 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
453 62ee7d94 2023-01-10 thomas "%s has been created by someone else "
454 62ee7d94 2023-01-10 thomas "while transaction was in progress",
455 62ee7d94 2023-01-10 thomas got_ref_get_name(ref));
456 62ee7d94 2023-01-10 thomas goto done;
458 49563dfb 2023-01-28 thomas } else if (iref.delete_ref) {
459 49563dfb 2023-01-28 thomas err = got_ref_open(&ref, repo, refname, 1 /* lock */);
461 49563dfb 2023-01-28 thomas goto done;
462 49563dfb 2023-01-28 thomas locked = 1;
464 49563dfb 2023-01-28 thomas err = got_ref_resolve(&id, repo, ref);
466 49563dfb 2023-01-28 thomas goto done;
468 49563dfb 2023-01-28 thomas if (got_object_id_cmp(id, &old_id) != 0) {
469 49563dfb 2023-01-28 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
470 49563dfb 2023-01-28 thomas "%s has been modified by someone else "
471 49563dfb 2023-01-28 thomas "while transaction was in progress",
472 49563dfb 2023-01-28 thomas got_ref_get_name(ref));
473 49563dfb 2023-01-28 thomas goto done;
476 49563dfb 2023-01-28 thomas err = got_ref_delete(ref, repo);
478 49563dfb 2023-01-28 thomas goto done;
480 49563dfb 2023-01-28 thomas free(id);
481 49563dfb 2023-01-28 thomas id = NULL;
483 62ee7d94 2023-01-10 thomas err = got_ref_open(&ref, repo, refname, 1 /* lock */);
485 62ee7d94 2023-01-10 thomas goto done;
486 62ee7d94 2023-01-10 thomas locked = 1;
488 62ee7d94 2023-01-10 thomas err = got_ref_resolve(&id, repo, ref);
490 62ee7d94 2023-01-10 thomas goto done;
492 62ee7d94 2023-01-10 thomas if (got_object_id_cmp(id, &old_id) != 0) {
493 62ee7d94 2023-01-10 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
494 62ee7d94 2023-01-10 thomas "%s has been modified by someone else "
495 62ee7d94 2023-01-10 thomas "while transaction was in progress",
496 62ee7d94 2023-01-10 thomas got_ref_get_name(ref));
497 62ee7d94 2023-01-10 thomas goto done;
500 62ee7d94 2023-01-10 thomas err = got_ref_change_ref(ref, &new_id);
502 62ee7d94 2023-01-10 thomas goto done;
504 62ee7d94 2023-01-10 thomas err = got_ref_write(ref, repo);
506 62ee7d94 2023-01-10 thomas goto done;
508 62ee7d94 2023-01-10 thomas free(id);
509 62ee7d94 2023-01-10 thomas id = NULL;
512 62ee7d94 2023-01-10 thomas if (err) {
513 62ee7d94 2023-01-10 thomas if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
514 62ee7d94 2023-01-10 thomas err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
515 62ee7d94 2023-01-10 thomas "could not acquire exclusive file lock for %s",
516 62ee7d94 2023-01-10 thomas refname);
518 62ee7d94 2023-01-10 thomas send_ref_update_ng(client, &iref, refname, err->msg);
520 62ee7d94 2023-01-10 thomas send_ref_update_ok(client, &iref, refname);
522 62ee7d94 2023-01-10 thomas if (client->nref_updates > 0) {
523 62ee7d94 2023-01-10 thomas client->nref_updates--;
524 d98779cd 2023-01-19 thomas if (client->nref_updates == 0) {
525 62ee7d94 2023-01-10 thomas send_refs_updated(client);
526 d98779cd 2023-01-19 thomas *shut = 1;
530 62ee7d94 2023-01-10 thomas if (locked) {
531 62ee7d94 2023-01-10 thomas const struct got_error *unlock_err;
532 62ee7d94 2023-01-10 thomas unlock_err = got_ref_unlock(ref);
533 62ee7d94 2023-01-10 thomas if (unlock_err && err == NULL)
534 62ee7d94 2023-01-10 thomas err = unlock_err;
537 62ee7d94 2023-01-10 thomas got_ref_close(ref);
539 62ee7d94 2023-01-10 thomas got_object_close(obj);
540 62ee7d94 2023-01-10 thomas if (repo)
541 62ee7d94 2023-01-10 thomas got_repo_close(repo);
542 62ee7d94 2023-01-10 thomas free(refname);
543 62ee7d94 2023-01-10 thomas free(id);
544 62ee7d94 2023-01-10 thomas return err;
547 62ee7d94 2023-01-10 thomas static void
548 62ee7d94 2023-01-10 thomas session_dispatch_repo_child(int fd, short event, void *arg)
550 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
551 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
552 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
553 62ee7d94 2023-01-10 thomas ssize_t n;
554 62ee7d94 2023-01-10 thomas int shut = 0;
555 62ee7d94 2023-01-10 thomas struct imsg imsg;
557 62ee7d94 2023-01-10 thomas if (event & EV_READ) {
558 62ee7d94 2023-01-10 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
559 62ee7d94 2023-01-10 thomas fatal("imsg_read error");
560 62ee7d94 2023-01-10 thomas if (n == 0) {
561 62ee7d94 2023-01-10 thomas /* Connection closed. */
562 62ee7d94 2023-01-10 thomas shut = 1;
563 62ee7d94 2023-01-10 thomas goto done;
567 62ee7d94 2023-01-10 thomas if (event & EV_WRITE) {
568 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
569 62ee7d94 2023-01-10 thomas if (n == -1 && errno != EAGAIN)
570 62ee7d94 2023-01-10 thomas fatal("msgbuf_write");
571 62ee7d94 2023-01-10 thomas if (n == 0) {
572 62ee7d94 2023-01-10 thomas /* Connection closed. */
573 62ee7d94 2023-01-10 thomas shut = 1;
574 62ee7d94 2023-01-10 thomas goto done;
578 62ee7d94 2023-01-10 thomas for (;;) {
579 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
580 62ee7d94 2023-01-10 thomas uint32_t client_id = 0;
581 62ee7d94 2023-01-10 thomas int do_disconnect = 0;
582 62ee7d94 2023-01-10 thomas int do_ref_updates = 0, do_ref_update = 0;
583 62ee7d94 2023-01-10 thomas int do_packfile_install = 0;
585 62ee7d94 2023-01-10 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
586 62ee7d94 2023-01-10 thomas fatal("%s: imsg_get error", __func__);
587 62ee7d94 2023-01-10 thomas if (n == 0) /* No more messages. */
590 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
591 62ee7d94 2023-01-10 thomas case GOTD_IMSG_ERROR:
592 62ee7d94 2023-01-10 thomas do_disconnect = 1;
593 62ee7d94 2023-01-10 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
595 62ee7d94 2023-01-10 thomas case GOTD_IMSG_PACKFILE_DONE:
596 62ee7d94 2023-01-10 thomas do_disconnect = 1;
597 62ee7d94 2023-01-10 thomas err = recv_packfile_done(&client_id, &imsg);
599 62ee7d94 2023-01-10 thomas case GOTD_IMSG_PACKFILE_INSTALL:
600 62ee7d94 2023-01-10 thomas err = recv_packfile_install(&client_id, &imsg);
601 62ee7d94 2023-01-10 thomas if (err == NULL)
602 62ee7d94 2023-01-10 thomas do_packfile_install = 1;
604 62ee7d94 2023-01-10 thomas case GOTD_IMSG_REF_UPDATES_START:
605 62ee7d94 2023-01-10 thomas err = recv_ref_updates_start(&client_id, &imsg);
606 62ee7d94 2023-01-10 thomas if (err == NULL)
607 62ee7d94 2023-01-10 thomas do_ref_updates = 1;
609 62ee7d94 2023-01-10 thomas case GOTD_IMSG_REF_UPDATE:
610 62ee7d94 2023-01-10 thomas err = recv_ref_update(&client_id, &imsg);
611 62ee7d94 2023-01-10 thomas if (err == NULL)
612 62ee7d94 2023-01-10 thomas do_ref_update = 1;
615 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
619 62ee7d94 2023-01-10 thomas if (do_disconnect) {
621 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
623 62ee7d94 2023-01-10 thomas disconnect(client);
625 62ee7d94 2023-01-10 thomas if (do_packfile_install)
626 62ee7d94 2023-01-10 thomas err = install_pack(client,
627 62ee7d94 2023-01-10 thomas gotd_session.repo->path, &imsg);
628 62ee7d94 2023-01-10 thomas else if (do_ref_updates)
629 62ee7d94 2023-01-10 thomas err = begin_ref_updates(client, &imsg);
630 62ee7d94 2023-01-10 thomas else if (do_ref_update)
631 d98779cd 2023-01-19 thomas err = update_ref(&shut, client,
632 62ee7d94 2023-01-10 thomas gotd_session.repo->path, &imsg);
634 62ee7d94 2023-01-10 thomas log_warnx("uid %d: %s", client->euid, err->msg);
636 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
639 62ee7d94 2023-01-10 thomas if (!shut) {
640 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
642 62ee7d94 2023-01-10 thomas /* This pipe is dead. Remove its event handler */
643 62ee7d94 2023-01-10 thomas event_del(&iev->ev);
644 62ee7d94 2023-01-10 thomas event_loopexit(NULL);
648 62ee7d94 2023-01-10 thomas static const struct got_error *
649 62ee7d94 2023-01-10 thomas recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
651 62ee7d94 2023-01-10 thomas struct gotd_imsg_capabilities icapas;
652 62ee7d94 2023-01-10 thomas size_t datalen;
654 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
655 62ee7d94 2023-01-10 thomas if (datalen != sizeof(icapas))
656 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
657 62ee7d94 2023-01-10 thomas memcpy(&icapas, imsg->data, sizeof(icapas));
659 62ee7d94 2023-01-10 thomas client->ncapa_alloc = icapas.ncapabilities;
660 62ee7d94 2023-01-10 thomas client->capabilities = calloc(client->ncapa_alloc,
661 62ee7d94 2023-01-10 thomas sizeof(*client->capabilities));
662 62ee7d94 2023-01-10 thomas if (client->capabilities == NULL) {
663 62ee7d94 2023-01-10 thomas client->ncapa_alloc = 0;
664 62ee7d94 2023-01-10 thomas return got_error_from_errno("calloc");
667 62ee7d94 2023-01-10 thomas log_debug("expecting %zu capabilities from uid %d",
668 62ee7d94 2023-01-10 thomas client->ncapa_alloc, client->euid);
669 62ee7d94 2023-01-10 thomas return NULL;
672 62ee7d94 2023-01-10 thomas static const struct got_error *
673 62ee7d94 2023-01-10 thomas recv_capability(struct gotd_session_client *client, struct imsg *imsg)
675 62ee7d94 2023-01-10 thomas struct gotd_imsg_capability icapa;
676 62ee7d94 2023-01-10 thomas struct gotd_client_capability *capa;
677 62ee7d94 2023-01-10 thomas size_t datalen;
678 62ee7d94 2023-01-10 thomas char *key, *value = NULL;
680 62ee7d94 2023-01-10 thomas if (client->capabilities == NULL ||
681 62ee7d94 2023-01-10 thomas client->ncapabilities >= client->ncapa_alloc) {
682 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
683 62ee7d94 2023-01-10 thomas "unexpected capability received");
686 62ee7d94 2023-01-10 thomas memset(&icapa, 0, sizeof(icapa));
688 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
689 62ee7d94 2023-01-10 thomas if (datalen < sizeof(icapa))
690 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
691 62ee7d94 2023-01-10 thomas memcpy(&icapa, imsg->data, sizeof(icapa));
693 62ee7d94 2023-01-10 thomas if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
694 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
696 fcbb06bf 2023-01-14 thomas key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
697 62ee7d94 2023-01-10 thomas if (key == NULL)
698 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
699 62ee7d94 2023-01-10 thomas if (icapa.value_len > 0) {
700 fcbb06bf 2023-01-14 thomas value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
701 fcbb06bf 2023-01-14 thomas icapa.value_len);
702 62ee7d94 2023-01-10 thomas if (value == NULL) {
703 62ee7d94 2023-01-10 thomas free(key);
704 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
708 62ee7d94 2023-01-10 thomas capa = &client->capabilities[client->ncapabilities++];
709 62ee7d94 2023-01-10 thomas capa->key = key;
710 62ee7d94 2023-01-10 thomas capa->value = value;
712 62ee7d94 2023-01-10 thomas if (value)
713 62ee7d94 2023-01-10 thomas log_debug("uid %d: capability %s=%s", client->euid, key, value);
715 62ee7d94 2023-01-10 thomas log_debug("uid %d: capability %s", client->euid, key);
717 62ee7d94 2023-01-10 thomas return NULL;
720 62ee7d94 2023-01-10 thomas static const struct got_error *
721 62ee7d94 2023-01-10 thomas ensure_client_is_reading(struct gotd_session_client *client)
723 62ee7d94 2023-01-10 thomas if (client->is_writing) {
724 62ee7d94 2023-01-10 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
725 62ee7d94 2023-01-10 thomas "uid %d made a read-request but is not reading from "
726 62ee7d94 2023-01-10 thomas "a repository", client->euid);
729 62ee7d94 2023-01-10 thomas return NULL;
732 62ee7d94 2023-01-10 thomas static const struct got_error *
733 62ee7d94 2023-01-10 thomas ensure_client_is_writing(struct gotd_session_client *client)
735 62ee7d94 2023-01-10 thomas if (!client->is_writing) {
736 62ee7d94 2023-01-10 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
737 62ee7d94 2023-01-10 thomas "uid %d made a write-request but is not writing to "
738 62ee7d94 2023-01-10 thomas "a repository", client->euid);
741 62ee7d94 2023-01-10 thomas return NULL;
744 62ee7d94 2023-01-10 thomas static const struct got_error *
745 62ee7d94 2023-01-10 thomas forward_want(struct gotd_session_client *client, struct imsg *imsg)
747 62ee7d94 2023-01-10 thomas struct gotd_imsg_want ireq;
748 62ee7d94 2023-01-10 thomas struct gotd_imsg_want iwant;
749 62ee7d94 2023-01-10 thomas size_t datalen;
751 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
752 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ireq))
753 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
755 62ee7d94 2023-01-10 thomas memcpy(&ireq, imsg->data, datalen);
757 62ee7d94 2023-01-10 thomas memset(&iwant, 0, sizeof(iwant));
758 62ee7d94 2023-01-10 thomas memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
759 62ee7d94 2023-01-10 thomas iwant.client_id = client->id;
761 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_WANT,
762 62ee7d94 2023-01-10 thomas PROC_SESSION, -1, &iwant, sizeof(iwant)) == -1)
763 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg compose WANT");
765 62ee7d94 2023-01-10 thomas return NULL;
768 62ee7d94 2023-01-10 thomas static const struct got_error *
769 62ee7d94 2023-01-10 thomas forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
771 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
772 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update ireq;
773 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update *iref = NULL;
774 62ee7d94 2023-01-10 thomas size_t datalen;
776 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
777 62ee7d94 2023-01-10 thomas if (datalen < sizeof(ireq))
778 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
779 62ee7d94 2023-01-10 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
780 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ireq) + ireq.name_len)
781 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
783 62ee7d94 2023-01-10 thomas iref = malloc(datalen);
784 62ee7d94 2023-01-10 thomas if (iref == NULL)
785 62ee7d94 2023-01-10 thomas return got_error_from_errno("malloc");
786 62ee7d94 2023-01-10 thomas memcpy(iref, imsg->data, datalen);
788 62ee7d94 2023-01-10 thomas iref->client_id = client->id;
789 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
790 62ee7d94 2023-01-10 thomas GOTD_IMSG_REF_UPDATE, PROC_SESSION, -1, iref, datalen) == -1)
791 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose REF_UPDATE");
792 62ee7d94 2023-01-10 thomas free(iref);
793 62ee7d94 2023-01-10 thomas return err;
796 62ee7d94 2023-01-10 thomas static const struct got_error *
797 62ee7d94 2023-01-10 thomas forward_have(struct gotd_session_client *client, struct imsg *imsg)
799 62ee7d94 2023-01-10 thomas struct gotd_imsg_have ireq;
800 62ee7d94 2023-01-10 thomas struct gotd_imsg_have ihave;
801 62ee7d94 2023-01-10 thomas size_t datalen;
803 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
804 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ireq))
805 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
807 62ee7d94 2023-01-10 thomas memcpy(&ireq, imsg->data, datalen);
809 62ee7d94 2023-01-10 thomas memset(&ihave, 0, sizeof(ihave));
810 62ee7d94 2023-01-10 thomas memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
811 62ee7d94 2023-01-10 thomas ihave.client_id = client->id;
813 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_HAVE,
814 62ee7d94 2023-01-10 thomas PROC_SESSION, -1, &ihave, sizeof(ihave)) == -1)
815 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg compose HAVE");
817 62ee7d94 2023-01-10 thomas return NULL;
820 62ee7d94 2023-01-10 thomas static int
821 62ee7d94 2023-01-10 thomas client_has_capability(struct gotd_session_client *client, const char *capastr)
823 62ee7d94 2023-01-10 thomas struct gotd_client_capability *capa;
824 62ee7d94 2023-01-10 thomas size_t i;
826 62ee7d94 2023-01-10 thomas if (client->ncapabilities == 0)
827 62ee7d94 2023-01-10 thomas return 0;
829 62ee7d94 2023-01-10 thomas for (i = 0; i < client->ncapabilities; i++) {
830 62ee7d94 2023-01-10 thomas capa = &client->capabilities[i];
831 62ee7d94 2023-01-10 thomas if (strcmp(capa->key, capastr) == 0)
832 62ee7d94 2023-01-10 thomas return 1;
835 62ee7d94 2023-01-10 thomas return 0;
838 62ee7d94 2023-01-10 thomas static const struct got_error *
839 62ee7d94 2023-01-10 thomas recv_packfile(struct gotd_session_client *client)
841 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
842 62ee7d94 2023-01-10 thomas struct gotd_imsg_recv_packfile ipack;
843 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_pipe ipipe;
844 62ee7d94 2023-01-10 thomas struct gotd_imsg_packidx_file ifile;
845 62ee7d94 2023-01-10 thomas char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
846 62ee7d94 2023-01-10 thomas int packfd = -1, idxfd = -1;
847 62ee7d94 2023-01-10 thomas int pipe[2] = { -1, -1 };
849 62ee7d94 2023-01-10 thomas if (client->packfile_path) {
850 62ee7d94 2023-01-10 thomas return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
851 62ee7d94 2023-01-10 thomas "uid %d already has a pack file", client->euid);
854 62ee7d94 2023-01-10 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
855 62ee7d94 2023-01-10 thomas return got_error_from_errno("socketpair");
857 62ee7d94 2023-01-10 thomas memset(&ipipe, 0, sizeof(ipipe));
858 62ee7d94 2023-01-10 thomas ipipe.client_id = client->id;
860 62ee7d94 2023-01-10 thomas /* Send pack pipe end 0 to repo child process. */
861 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
862 62ee7d94 2023-01-10 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION, pipe[0],
863 62ee7d94 2023-01-10 thomas &ipipe, sizeof(ipipe)) == -1) {
864 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
865 62ee7d94 2023-01-10 thomas pipe[0] = -1;
866 62ee7d94 2023-01-10 thomas goto done;
868 62ee7d94 2023-01-10 thomas pipe[0] = -1;
870 62ee7d94 2023-01-10 thomas /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
871 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->iev,
872 62ee7d94 2023-01-10 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION, pipe[1], NULL, 0) == -1)
873 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
874 62ee7d94 2023-01-10 thomas pipe[1] = -1;
876 62ee7d94 2023-01-10 thomas if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
877 62ee7d94 2023-01-10 thomas got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
878 62ee7d94 2023-01-10 thomas client->euid) == -1) {
879 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
880 62ee7d94 2023-01-10 thomas goto done;
883 62ee7d94 2023-01-10 thomas err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
885 62ee7d94 2023-01-10 thomas goto done;
887 62ee7d94 2023-01-10 thomas free(basepath);
888 62ee7d94 2023-01-10 thomas if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
889 62ee7d94 2023-01-10 thomas got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
890 62ee7d94 2023-01-10 thomas client->euid) == -1) {
891 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
892 62ee7d94 2023-01-10 thomas basepath = NULL;
893 62ee7d94 2023-01-10 thomas goto done;
895 62ee7d94 2023-01-10 thomas err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
897 62ee7d94 2023-01-10 thomas goto done;
899 62ee7d94 2023-01-10 thomas memset(&ifile, 0, sizeof(ifile));
900 62ee7d94 2023-01-10 thomas ifile.client_id = client->id;
901 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
902 62ee7d94 2023-01-10 thomas GOTD_IMSG_PACKIDX_FILE, PROC_SESSION,
903 62ee7d94 2023-01-10 thomas idxfd, &ifile, sizeof(ifile)) == -1) {
904 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKIDX_FILE");
905 62ee7d94 2023-01-10 thomas idxfd = -1;
906 62ee7d94 2023-01-10 thomas goto done;
908 62ee7d94 2023-01-10 thomas idxfd = -1;
910 62ee7d94 2023-01-10 thomas memset(&ipack, 0, sizeof(ipack));
911 62ee7d94 2023-01-10 thomas ipack.client_id = client->id;
912 62ee7d94 2023-01-10 thomas if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
913 62ee7d94 2023-01-10 thomas ipack.report_status = 1;
915 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
916 62ee7d94 2023-01-10 thomas GOTD_IMSG_RECV_PACKFILE, PROC_SESSION, packfd,
917 62ee7d94 2023-01-10 thomas &ipack, sizeof(ipack)) == -1) {
918 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose RECV_PACKFILE");
919 62ee7d94 2023-01-10 thomas packfd = -1;
920 62ee7d94 2023-01-10 thomas goto done;
922 62ee7d94 2023-01-10 thomas packfd = -1;
925 62ee7d94 2023-01-10 thomas free(basepath);
926 62ee7d94 2023-01-10 thomas if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
927 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
928 62ee7d94 2023-01-10 thomas if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
929 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
930 62ee7d94 2023-01-10 thomas if (packfd != -1 && close(packfd) == -1 && err == NULL)
931 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
932 62ee7d94 2023-01-10 thomas if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
933 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
934 62ee7d94 2023-01-10 thomas if (err) {
935 62ee7d94 2023-01-10 thomas free(pack_path);
936 62ee7d94 2023-01-10 thomas free(idx_path);
938 62ee7d94 2023-01-10 thomas client->packfile_path = pack_path;
939 62ee7d94 2023-01-10 thomas client->packidx_path = idx_path;
941 62ee7d94 2023-01-10 thomas return err;
944 62ee7d94 2023-01-10 thomas static const struct got_error *
945 62ee7d94 2023-01-10 thomas send_packfile(struct gotd_session_client *client)
947 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
948 62ee7d94 2023-01-10 thomas struct gotd_imsg_send_packfile ipack;
949 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_pipe ipipe;
950 62ee7d94 2023-01-10 thomas int pipe[2];
952 62ee7d94 2023-01-10 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
953 62ee7d94 2023-01-10 thomas return got_error_from_errno("socketpair");
955 62ee7d94 2023-01-10 thomas memset(&ipack, 0, sizeof(ipack));
956 62ee7d94 2023-01-10 thomas memset(&ipipe, 0, sizeof(ipipe));
958 62ee7d94 2023-01-10 thomas ipack.client_id = client->id;
959 62ee7d94 2023-01-10 thomas if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
960 62ee7d94 2023-01-10 thomas ipack.report_progress = 1;
962 62ee7d94 2023-01-10 thomas client->delta_cache_fd = got_opentempfd();
963 62ee7d94 2023-01-10 thomas if (client->delta_cache_fd == -1)
964 62ee7d94 2023-01-10 thomas return got_error_from_errno("got_opentempfd");
966 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
967 62ee7d94 2023-01-10 thomas GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
968 62ee7d94 2023-01-10 thomas &ipack, sizeof(ipack)) == -1) {
969 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose SEND_PACKFILE");
970 62ee7d94 2023-01-10 thomas close(pipe[0]);
971 62ee7d94 2023-01-10 thomas close(pipe[1]);
972 62ee7d94 2023-01-10 thomas return err;
975 62ee7d94 2023-01-10 thomas ipipe.client_id = client->id;
977 62ee7d94 2023-01-10 thomas /* Send pack pipe end 0 to repo child process. */
978 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
979 62ee7d94 2023-01-10 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD,
980 62ee7d94 2023-01-10 thomas pipe[0], &ipipe, sizeof(ipipe)) == -1) {
981 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
982 62ee7d94 2023-01-10 thomas close(pipe[1]);
983 62ee7d94 2023-01-10 thomas return err;
986 62ee7d94 2023-01-10 thomas /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
987 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->iev,
988 62ee7d94 2023-01-10 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
989 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
991 62ee7d94 2023-01-10 thomas return err;
994 62ee7d94 2023-01-10 thomas static void
995 62ee7d94 2023-01-10 thomas session_dispatch_listener(int fd, short events, void *arg)
997 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
998 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
999 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1000 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1001 62ee7d94 2023-01-10 thomas struct imsg imsg;
1002 62ee7d94 2023-01-10 thomas ssize_t n;
1004 62ee7d94 2023-01-10 thomas if (events & EV_WRITE) {
1005 62ee7d94 2023-01-10 thomas while (ibuf->w.queued) {
1006 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
1007 62ee7d94 2023-01-10 thomas if (n == -1 && errno == EPIPE) {
1009 62ee7d94 2023-01-10 thomas * The client has closed its socket.
1010 62ee7d94 2023-01-10 thomas * This can happen when Git clients are
1011 62ee7d94 2023-01-10 thomas * done sending pack file data.
1013 62ee7d94 2023-01-10 thomas msgbuf_clear(&ibuf->w);
1014 62ee7d94 2023-01-10 thomas continue;
1015 62ee7d94 2023-01-10 thomas } else if (n == -1 && errno != EAGAIN) {
1016 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg_flush");
1017 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1020 62ee7d94 2023-01-10 thomas if (n == 0) {
1021 62ee7d94 2023-01-10 thomas /* Connection closed. */
1022 62ee7d94 2023-01-10 thomas err = got_error(GOT_ERR_EOF);
1023 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1029 62ee7d94 2023-01-10 thomas if ((events & EV_READ) == 0)
1032 62ee7d94 2023-01-10 thomas memset(&imsg, 0, sizeof(imsg));
1034 62ee7d94 2023-01-10 thomas while (err == NULL) {
1035 62ee7d94 2023-01-10 thomas err = gotd_imsg_recv(&imsg, ibuf, 0);
1036 62ee7d94 2023-01-10 thomas if (err) {
1037 62ee7d94 2023-01-10 thomas if (err->code == GOT_ERR_PRIVSEP_READ)
1038 62ee7d94 2023-01-10 thomas err = NULL;
1042 62ee7d94 2023-01-10 thomas evtimer_del(&client->tmo);
1044 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
1045 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CAPABILITIES:
1046 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1047 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1048 62ee7d94 2023-01-10 thomas "unexpected capabilities received");
1051 62ee7d94 2023-01-10 thomas log_debug("receiving capabilities from uid %d",
1052 62ee7d94 2023-01-10 thomas client->euid);
1053 62ee7d94 2023-01-10 thomas err = recv_capabilities(client, &imsg);
1055 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CAPABILITY:
1056 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1057 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1058 62ee7d94 2023-01-10 thomas "unexpected capability received");
1061 62ee7d94 2023-01-10 thomas err = recv_capability(client, &imsg);
1062 62ee7d94 2023-01-10 thomas if (err || client->ncapabilities < client->ncapa_alloc)
1064 62ee7d94 2023-01-10 thomas if (!client->is_writing) {
1065 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_WANT;
1066 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1067 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting want-lines",
1068 62ee7d94 2023-01-10 thomas client->euid);
1069 62ee7d94 2023-01-10 thomas } else if (client->is_writing) {
1070 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1071 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1072 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting ref-update-lines",
1073 62ee7d94 2023-01-10 thomas client->euid);
1075 62ee7d94 2023-01-10 thomas fatalx("client %d is both reading and writing",
1076 62ee7d94 2023-01-10 thomas client->euid);
1078 62ee7d94 2023-01-10 thomas case GOTD_IMSG_WANT:
1079 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_WANT) {
1080 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1081 62ee7d94 2023-01-10 thomas "unexpected want-line received");
1084 62ee7d94 2023-01-10 thomas log_debug("received want-line from uid %d",
1085 62ee7d94 2023-01-10 thomas client->euid);
1086 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1087 62ee7d94 2023-01-10 thomas if (err)
1089 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1090 62ee7d94 2023-01-10 thomas err = forward_want(client, &imsg);
1092 62ee7d94 2023-01-10 thomas case GOTD_IMSG_REF_UPDATE:
1093 0445d8ec 2023-01-19 thomas if (client->state != GOTD_STATE_EXPECT_REF_UPDATE &&
1094 0445d8ec 2023-01-19 thomas client->state !=
1095 0445d8ec 2023-01-19 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1096 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1097 62ee7d94 2023-01-10 thomas "unexpected ref-update-line received");
1100 62ee7d94 2023-01-10 thomas log_debug("received ref-update-line from uid %d",
1101 62ee7d94 2023-01-10 thomas client->euid);
1102 62ee7d94 2023-01-10 thomas err = ensure_client_is_writing(client);
1103 62ee7d94 2023-01-10 thomas if (err)
1105 62ee7d94 2023-01-10 thomas err = forward_ref_update(client, &imsg);
1106 62ee7d94 2023-01-10 thomas if (err)
1108 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1109 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1111 62ee7d94 2023-01-10 thomas case GOTD_IMSG_HAVE:
1112 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_HAVE) {
1113 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1114 62ee7d94 2023-01-10 thomas "unexpected have-line received");
1117 62ee7d94 2023-01-10 thomas log_debug("received have-line from uid %d",
1118 62ee7d94 2023-01-10 thomas client->euid);
1119 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1120 62ee7d94 2023-01-10 thomas if (err)
1122 62ee7d94 2023-01-10 thomas err = forward_have(client, &imsg);
1123 62ee7d94 2023-01-10 thomas if (err)
1125 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1127 62ee7d94 2023-01-10 thomas case GOTD_IMSG_FLUSH:
1128 62ee7d94 2023-01-10 thomas if (client->state == GOTD_STATE_EXPECT_WANT ||
1129 62ee7d94 2023-01-10 thomas client->state == GOTD_STATE_EXPECT_HAVE) {
1130 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1131 62ee7d94 2023-01-10 thomas if (err)
1133 62ee7d94 2023-01-10 thomas } else if (client->state ==
1134 62ee7d94 2023-01-10 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1135 62ee7d94 2023-01-10 thomas err = ensure_client_is_writing(client);
1136 62ee7d94 2023-01-10 thomas if (err)
1138 6110f5ef 2023-01-19 thomas } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1139 98c7fd82 2023-01-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1140 98c7fd82 2023-01-23 thomas "unexpected flush-pkt received");
1143 98c7fd82 2023-01-23 thomas if (!client->accept_flush_pkt) {
1144 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1145 62ee7d94 2023-01-10 thomas "unexpected flush-pkt received");
1150 98c7fd82 2023-01-23 thomas * Accept just one flush packet at a time.
1151 98c7fd82 2023-01-23 thomas * Future client state transitions will set this flag
1152 98c7fd82 2023-01-23 thomas * again if another flush packet is expected.
1154 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 0;
1156 62ee7d94 2023-01-10 thomas log_debug("received flush-pkt from uid %d",
1157 62ee7d94 2023-01-10 thomas client->euid);
1158 62ee7d94 2023-01-10 thomas if (client->state == GOTD_STATE_EXPECT_WANT) {
1159 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_HAVE;
1160 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting have-lines",
1161 62ee7d94 2023-01-10 thomas client->euid);
1162 62ee7d94 2023-01-10 thomas } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1163 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_DONE;
1164 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1165 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting 'done'",
1166 62ee7d94 2023-01-10 thomas client->euid);
1167 62ee7d94 2023-01-10 thomas } else if (client->state ==
1168 62ee7d94 2023-01-10 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1169 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_PACKFILE;
1170 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting packfile",
1171 62ee7d94 2023-01-10 thomas client->euid);
1172 62ee7d94 2023-01-10 thomas err = recv_packfile(client);
1173 6110f5ef 2023-01-19 thomas } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1174 62ee7d94 2023-01-10 thomas /* should not happen, see above */
1175 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1176 62ee7d94 2023-01-10 thomas "unexpected client state");
1180 62ee7d94 2023-01-10 thomas case GOTD_IMSG_DONE:
1181 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_HAVE &&
1182 62ee7d94 2023-01-10 thomas client->state != GOTD_STATE_EXPECT_DONE) {
1183 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1184 62ee7d94 2023-01-10 thomas "unexpected flush-pkt received");
1187 62ee7d94 2023-01-10 thomas log_debug("received 'done' from uid %d", client->euid);
1188 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1189 62ee7d94 2023-01-10 thomas if (err)
1191 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_DONE;
1192 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1193 62ee7d94 2023-01-10 thomas err = send_packfile(client);
1195 62ee7d94 2023-01-10 thomas default:
1196 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1197 62ee7d94 2023-01-10 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1201 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
1204 62ee7d94 2023-01-10 thomas if (err) {
1205 62ee7d94 2023-01-10 thomas if (err->code != GOT_ERR_EOF ||
1206 62ee7d94 2023-01-10 thomas client->state != GOTD_STATE_EXPECT_PACKFILE)
1207 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1208 62ee7d94 2023-01-10 thomas } else {
1209 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
1210 62ee7d94 2023-01-10 thomas evtimer_add(&client->tmo, &gotd_session.request_timeout);
1214 62ee7d94 2023-01-10 thomas static const struct got_error *
1215 62ee7d94 2023-01-10 thomas list_refs_request(void)
1217 62ee7d94 2023-01-10 thomas static const struct got_error *err;
1218 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1219 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &client->repo_child_iev;
1220 62ee7d94 2023-01-10 thomas struct gotd_imsg_list_refs_internal ilref;
1223 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1224 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1226 62ee7d94 2023-01-10 thomas memset(&ilref, 0, sizeof(ilref));
1227 62ee7d94 2023-01-10 thomas ilref.client_id = client->id;
1229 62ee7d94 2023-01-10 thomas fd = dup(client->fd);
1230 62ee7d94 2023-01-10 thomas if (fd == -1)
1231 62ee7d94 2023-01-10 thomas return got_error_from_errno("dup");
1233 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1234 62ee7d94 2023-01-10 thomas PROC_SESSION, fd, &ilref, sizeof(ilref)) == -1) {
1235 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1236 62ee7d94 2023-01-10 thomas close(fd);
1237 62ee7d94 2023-01-10 thomas return err;
1240 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1241 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting capabilities", client->euid);
1242 62ee7d94 2023-01-10 thomas return NULL;
1245 62ee7d94 2023-01-10 thomas static const struct got_error *
1246 62ee7d94 2023-01-10 thomas recv_connect(struct imsg *imsg)
1248 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1249 62ee7d94 2023-01-10 thomas struct gotd_imsg_connect iconnect;
1250 62ee7d94 2023-01-10 thomas size_t datalen;
1252 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1253 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1255 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1256 62ee7d94 2023-01-10 thomas if (datalen != sizeof(iconnect))
1257 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1258 62ee7d94 2023-01-10 thomas memcpy(&iconnect, imsg->data, sizeof(iconnect));
1260 62ee7d94 2023-01-10 thomas if (imsg->fd == -1)
1261 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1263 62ee7d94 2023-01-10 thomas client->fd = imsg->fd;
1264 62ee7d94 2023-01-10 thomas client->euid = iconnect.euid;
1265 62ee7d94 2023-01-10 thomas client->egid = iconnect.egid;
1267 62ee7d94 2023-01-10 thomas imsg_init(&client->iev.ibuf, client->fd);
1268 62ee7d94 2023-01-10 thomas client->iev.handler = session_dispatch_listener;
1269 62ee7d94 2023-01-10 thomas client->iev.events = EV_READ;
1270 62ee7d94 2023-01-10 thomas client->iev.handler_arg = NULL;
1271 62ee7d94 2023-01-10 thomas event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1272 62ee7d94 2023-01-10 thomas session_dispatch_listener, &client->iev);
1273 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(&client->iev);
1274 62ee7d94 2023-01-10 thomas evtimer_set(&client->tmo, gotd_request_timeout, client);
1276 62ee7d94 2023-01-10 thomas return NULL;
1279 62ee7d94 2023-01-10 thomas static const struct got_error *
1280 62ee7d94 2023-01-10 thomas recv_repo_child(struct imsg *imsg)
1282 62ee7d94 2023-01-10 thomas struct gotd_imsg_connect_repo_child ichild;
1283 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1284 62ee7d94 2023-01-10 thomas size_t datalen;
1286 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1287 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1289 62ee7d94 2023-01-10 thomas /* We should already have received a pipe to the listener. */
1290 62ee7d94 2023-01-10 thomas if (client->fd == -1)
1291 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1293 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1294 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ichild))
1295 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1297 62ee7d94 2023-01-10 thomas memcpy(&ichild, imsg->data, sizeof(ichild));
1299 62ee7d94 2023-01-10 thomas client->id = ichild.client_id;
1300 62ee7d94 2023-01-10 thomas if (ichild.proc_id == PROC_REPO_WRITE)
1301 62ee7d94 2023-01-10 thomas client->is_writing = 1;
1302 62ee7d94 2023-01-10 thomas else if (ichild.proc_id == PROC_REPO_READ)
1303 62ee7d94 2023-01-10 thomas client->is_writing = 0;
1305 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1306 62ee7d94 2023-01-10 thomas "bad child process type");
1308 62ee7d94 2023-01-10 thomas if (imsg->fd == -1)
1309 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1311 62ee7d94 2023-01-10 thomas imsg_init(&client->repo_child_iev.ibuf, imsg->fd);
1312 62ee7d94 2023-01-10 thomas client->repo_child_iev.handler = session_dispatch_repo_child;
1313 62ee7d94 2023-01-10 thomas client->repo_child_iev.events = EV_READ;
1314 62ee7d94 2023-01-10 thomas client->repo_child_iev.handler_arg = NULL;
1315 62ee7d94 2023-01-10 thomas event_set(&client->repo_child_iev.ev, client->repo_child_iev.ibuf.fd,
1316 62ee7d94 2023-01-10 thomas EV_READ, session_dispatch_repo_child, &client->repo_child_iev);
1317 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(&client->repo_child_iev);
1319 62ee7d94 2023-01-10 thomas /* The "recvfd" pledge promise is no longer needed. */
1320 62ee7d94 2023-01-10 thomas if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1321 62ee7d94 2023-01-10 thomas fatal("pledge");
1323 62ee7d94 2023-01-10 thomas return NULL;
1326 62ee7d94 2023-01-10 thomas static void
1327 62ee7d94 2023-01-10 thomas session_dispatch(int fd, short event, void *arg)
1329 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
1330 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
1331 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1332 62ee7d94 2023-01-10 thomas ssize_t n;
1333 62ee7d94 2023-01-10 thomas int shut = 0;
1334 62ee7d94 2023-01-10 thomas struct imsg imsg;
1336 62ee7d94 2023-01-10 thomas if (event & EV_READ) {
1337 62ee7d94 2023-01-10 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1338 62ee7d94 2023-01-10 thomas fatal("imsg_read error");
1339 62ee7d94 2023-01-10 thomas if (n == 0) {
1340 62ee7d94 2023-01-10 thomas /* Connection closed. */
1341 62ee7d94 2023-01-10 thomas shut = 1;
1342 62ee7d94 2023-01-10 thomas goto done;
1346 62ee7d94 2023-01-10 thomas if (event & EV_WRITE) {
1347 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
1348 62ee7d94 2023-01-10 thomas if (n == -1 && errno != EAGAIN)
1349 62ee7d94 2023-01-10 thomas fatal("msgbuf_write");
1350 62ee7d94 2023-01-10 thomas if (n == 0) {
1351 62ee7d94 2023-01-10 thomas /* Connection closed. */
1352 62ee7d94 2023-01-10 thomas shut = 1;
1353 62ee7d94 2023-01-10 thomas goto done;
1357 62ee7d94 2023-01-10 thomas for (;;) {
1358 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1359 62ee7d94 2023-01-10 thomas uint32_t client_id = 0;
1360 62ee7d94 2023-01-10 thomas int do_disconnect = 0, do_list_refs = 0;
1362 62ee7d94 2023-01-10 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1363 62ee7d94 2023-01-10 thomas fatal("%s: imsg_get error", __func__);
1364 62ee7d94 2023-01-10 thomas if (n == 0) /* No more messages. */
1367 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
1368 62ee7d94 2023-01-10 thomas case GOTD_IMSG_ERROR:
1369 62ee7d94 2023-01-10 thomas do_disconnect = 1;
1370 62ee7d94 2023-01-10 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
1372 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CONNECT:
1373 62ee7d94 2023-01-10 thomas err = recv_connect(&imsg);
1375 62ee7d94 2023-01-10 thomas case GOTD_IMSG_DISCONNECT:
1376 62ee7d94 2023-01-10 thomas do_disconnect = 1;
1378 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CONNECT_REPO_CHILD:
1379 62ee7d94 2023-01-10 thomas err = recv_repo_child(&imsg);
1380 62ee7d94 2023-01-10 thomas if (err)
1382 62ee7d94 2023-01-10 thomas do_list_refs = 1;
1384 62ee7d94 2023-01-10 thomas default:
1385 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1388 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
1390 62ee7d94 2023-01-10 thomas if (do_disconnect) {
1391 62ee7d94 2023-01-10 thomas if (err)
1392 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1394 62ee7d94 2023-01-10 thomas disconnect(client);
1395 62ee7d94 2023-01-10 thomas } else if (do_list_refs)
1396 62ee7d94 2023-01-10 thomas err = list_refs_request();
1398 62ee7d94 2023-01-10 thomas if (err)
1399 62ee7d94 2023-01-10 thomas log_warnx("uid %d: %s", client->euid, err->msg);
1402 62ee7d94 2023-01-10 thomas if (!shut) {
1403 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
1404 62ee7d94 2023-01-10 thomas } else {
1405 62ee7d94 2023-01-10 thomas /* This pipe is dead. Remove its event handler */
1406 62ee7d94 2023-01-10 thomas event_del(&iev->ev);
1407 62ee7d94 2023-01-10 thomas event_loopexit(NULL);
1412 62ee7d94 2023-01-10 thomas session_main(const char *title, const char *repo_path,
1413 62ee7d94 2023-01-10 thomas int *pack_fds, int *temp_fds, struct timeval *request_timeout)
1415 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1416 62ee7d94 2023-01-10 thomas struct event evsigint, evsigterm, evsighup, evsigusr1;
1418 62ee7d94 2023-01-10 thomas gotd_session.title = title;
1419 62ee7d94 2023-01-10 thomas gotd_session.pid = getpid();
1420 62ee7d94 2023-01-10 thomas gotd_session.pack_fds = pack_fds;
1421 62ee7d94 2023-01-10 thomas gotd_session.temp_fds = temp_fds;
1422 62ee7d94 2023-01-10 thomas memcpy(&gotd_session.request_timeout, request_timeout,
1423 62ee7d94 2023-01-10 thomas sizeof(gotd_session.request_timeout));
1425 62ee7d94 2023-01-10 thomas err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1426 62ee7d94 2023-01-10 thomas if (err)
1427 62ee7d94 2023-01-10 thomas goto done;
1428 62ee7d94 2023-01-10 thomas if (!got_repo_is_bare(gotd_session.repo)) {
1429 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1430 62ee7d94 2023-01-10 thomas "bare git repository required");
1431 62ee7d94 2023-01-10 thomas goto done;
1434 62ee7d94 2023-01-10 thomas got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1436 62ee7d94 2023-01-10 thomas signal_set(&evsigint, SIGINT, gotd_session_sighdlr, NULL);
1437 62ee7d94 2023-01-10 thomas signal_set(&evsigterm, SIGTERM, gotd_session_sighdlr, NULL);
1438 62ee7d94 2023-01-10 thomas signal_set(&evsighup, SIGHUP, gotd_session_sighdlr, NULL);
1439 62ee7d94 2023-01-10 thomas signal_set(&evsigusr1, SIGUSR1, gotd_session_sighdlr, NULL);
1440 62ee7d94 2023-01-10 thomas signal(SIGPIPE, SIG_IGN);
1442 62ee7d94 2023-01-10 thomas signal_add(&evsigint, NULL);
1443 62ee7d94 2023-01-10 thomas signal_add(&evsigterm, NULL);
1444 62ee7d94 2023-01-10 thomas signal_add(&evsighup, NULL);
1445 62ee7d94 2023-01-10 thomas signal_add(&evsigusr1, NULL);
1447 62ee7d94 2023-01-10 thomas gotd_session_client.state = GOTD_STATE_EXPECT_LIST_REFS;
1448 62ee7d94 2023-01-10 thomas gotd_session_client.fd = -1;
1449 62ee7d94 2023-01-10 thomas gotd_session_client.nref_updates = -1;
1450 a6f25078 2023-01-10 thomas gotd_session_client.delta_cache_fd = -1;
1451 98c7fd82 2023-01-23 thomas gotd_session_client.accept_flush_pkt = 1;
1453 62ee7d94 2023-01-10 thomas imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1454 62ee7d94 2023-01-10 thomas gotd_session.parent_iev.handler = session_dispatch;
1455 62ee7d94 2023-01-10 thomas gotd_session.parent_iev.events = EV_READ;
1456 62ee7d94 2023-01-10 thomas gotd_session.parent_iev.handler_arg = NULL;
1457 62ee7d94 2023-01-10 thomas event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1458 62ee7d94 2023-01-10 thomas EV_READ, session_dispatch, &gotd_session.parent_iev);
1459 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1460 62ee7d94 2023-01-10 thomas GOTD_IMSG_CLIENT_SESSION_READY, PROC_SESSION, -1, NULL, 0) == -1) {
1461 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1462 62ee7d94 2023-01-10 thomas goto done;
1465 62ee7d94 2023-01-10 thomas event_dispatch();
1467 62ee7d94 2023-01-10 thomas if (err)
1468 62ee7d94 2023-01-10 thomas log_warnx("%s: %s", title, err->msg);
1469 62ee7d94 2023-01-10 thomas gotd_session_shutdown();
1473 62ee7d94 2023-01-10 thomas gotd_session_shutdown(void)
1475 62ee7d94 2023-01-10 thomas log_debug("%s: shutting down", gotd_session.title);
1476 62ee7d94 2023-01-10 thomas if (gotd_session.repo)
1477 62ee7d94 2023-01-10 thomas got_repo_close(gotd_session.repo);
1478 62ee7d94 2023-01-10 thomas got_repo_pack_fds_close(gotd_session.pack_fds);
1479 62ee7d94 2023-01-10 thomas got_repo_temp_fds_close(gotd_session.temp_fds);
1480 62ee7d94 2023-01-10 thomas exit(0);