2 ae7c1b78 2023-01-10 stsp * Copyright (c) 2022, 2023 Stefan Sperling <stsp@openbsd.org>
4 ae7c1b78 2023-01-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 ae7c1b78 2023-01-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 ae7c1b78 2023-01-10 stsp * copyright notice and this permission notice appear in all copies.
8 ae7c1b78 2023-01-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 ae7c1b78 2023-01-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 ae7c1b78 2023-01-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ae7c1b78 2023-01-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 ae7c1b78 2023-01-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ae7c1b78 2023-01-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 ae7c1b78 2023-01-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 ae7c1b78 2023-01-10 stsp #include <sys/types.h>
18 ae7c1b78 2023-01-10 stsp #include <sys/queue.h>
19 ae7c1b78 2023-01-10 stsp #include <sys/tree.h>
20 ae7c1b78 2023-01-10 stsp #include <sys/socket.h>
21 839bbaae 2023-02-01 op #include <sys/stat.h>
22 ae7c1b78 2023-01-10 stsp #include <sys/uio.h>
24 ae7c1b78 2023-01-10 stsp #include <errno.h>
25 ae7c1b78 2023-01-10 stsp #include <event.h>
26 ae7c1b78 2023-01-10 stsp #include <limits.h>
27 ae7c1b78 2023-01-10 stsp #include <sha1.h>
28 5822e79e 2023-02-23 op #include <sha2.h>
29 ae7c1b78 2023-01-10 stsp #include <signal.h>
30 ae7c1b78 2023-01-10 stsp #include <stdint.h>
31 ae7c1b78 2023-01-10 stsp #include <stdio.h>
32 ae7c1b78 2023-01-10 stsp #include <stdlib.h>
33 ae7c1b78 2023-01-10 stsp #include <string.h>
34 ae7c1b78 2023-01-10 stsp #include <imsg.h>
35 ae7c1b78 2023-01-10 stsp #include <unistd.h>
37 ae7c1b78 2023-01-10 stsp #include "got_error.h"
38 ae7c1b78 2023-01-10 stsp #include "got_repository.h"
39 ae7c1b78 2023-01-10 stsp #include "got_object.h"
40 ae7c1b78 2023-01-10 stsp #include "got_path.h"
41 ae7c1b78 2023-01-10 stsp #include "got_reference.h"
42 ae7c1b78 2023-01-10 stsp #include "got_opentemp.h"
44 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
45 ae7c1b78 2023-01-10 stsp #include "got_lib_delta.h"
46 ae7c1b78 2023-01-10 stsp #include "got_lib_object.h"
47 ae7c1b78 2023-01-10 stsp #include "got_lib_object_cache.h"
48 ae7c1b78 2023-01-10 stsp #include "got_lib_pack.h"
49 ae7c1b78 2023-01-10 stsp #include "got_lib_repository.h"
50 ae7c1b78 2023-01-10 stsp #include "got_lib_gitproto.h"
52 ae7c1b78 2023-01-10 stsp #include "gotd.h"
53 ae7c1b78 2023-01-10 stsp #include "log.h"
54 ae7c1b78 2023-01-10 stsp #include "session.h"
57 ae7c1b78 2023-01-10 stsp static struct gotd_session {
59 ae7c1b78 2023-01-10 stsp const char *title;
60 ae7c1b78 2023-01-10 stsp struct got_repository *repo;
61 ae7c1b78 2023-01-10 stsp int *pack_fds;
62 ae7c1b78 2023-01-10 stsp int *temp_fds;
63 ae7c1b78 2023-01-10 stsp struct gotd_imsgev parent_iev;
64 ae7c1b78 2023-01-10 stsp struct timeval request_timeout;
65 b0614828 2023-06-19 stsp enum gotd_procid proc_id;
66 ae7c1b78 2023-01-10 stsp } gotd_session;
68 ae7c1b78 2023-01-10 stsp static struct gotd_session_client {
69 eac23c30 2023-01-10 stsp enum gotd_session_state state;
70 ae7c1b78 2023-01-10 stsp int is_writing;
71 ae7c1b78 2023-01-10 stsp struct gotd_client_capability *capabilities;
72 ae7c1b78 2023-01-10 stsp size_t ncapa_alloc;
73 ae7c1b78 2023-01-10 stsp size_t ncapabilities;
74 ae7c1b78 2023-01-10 stsp uint32_t id;
76 ae7c1b78 2023-01-10 stsp int delta_cache_fd;
77 ae7c1b78 2023-01-10 stsp struct gotd_imsgev iev;
78 ae7c1b78 2023-01-10 stsp struct gotd_imsgev repo_child_iev;
79 ae7c1b78 2023-01-10 stsp struct event tmo;
82 ae7c1b78 2023-01-10 stsp char *packfile_path;
83 ae7c1b78 2023-01-10 stsp char *packidx_path;
84 ae7c1b78 2023-01-10 stsp int nref_updates;
85 3448a19a 2023-01-21 stsp int accept_flush_pkt;
86 808264b2 2023-08-22 stsp int flush_disconnect;
87 ae7c1b78 2023-01-10 stsp } gotd_session_client;
89 ae7c1b78 2023-01-10 stsp void gotd_session_sighdlr(int sig, short event, void *arg);
90 ae7c1b78 2023-01-10 stsp static void gotd_session_shutdown(void);
93 ae7c1b78 2023-01-10 stsp disconnect(struct gotd_session_client *client)
95 ae7c1b78 2023-01-10 stsp log_debug("uid %d: disconnecting", client->euid);
97 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&gotd_session.parent_iev,
98 b0614828 2023-06-19 stsp GOTD_IMSG_DISCONNECT, gotd_session.proc_id, -1, NULL, 0) == -1)
99 ae7c1b78 2023-01-10 stsp log_warn("imsg compose DISCONNECT");
101 ae7c1b78 2023-01-10 stsp imsg_clear(&client->repo_child_iev.ibuf);
102 ae7c1b78 2023-01-10 stsp event_del(&client->repo_child_iev.ev);
103 ae7c1b78 2023-01-10 stsp evtimer_del(&client->tmo);
104 ae7c1b78 2023-01-10 stsp close(client->fd);
105 ae7c1b78 2023-01-10 stsp if (client->delta_cache_fd != -1)
106 ae7c1b78 2023-01-10 stsp close(client->delta_cache_fd);
107 ae7c1b78 2023-01-10 stsp if (client->packfile_path) {
108 ae7c1b78 2023-01-10 stsp if (unlink(client->packfile_path) == -1 && errno != ENOENT)
109 ae7c1b78 2023-01-10 stsp log_warn("unlink %s: ", client->packfile_path);
110 ae7c1b78 2023-01-10 stsp free(client->packfile_path);
112 ae7c1b78 2023-01-10 stsp if (client->packidx_path) {
113 ae7c1b78 2023-01-10 stsp if (unlink(client->packidx_path) == -1 && errno != ENOENT)
114 ae7c1b78 2023-01-10 stsp log_warn("unlink %s: ", client->packidx_path);
115 ae7c1b78 2023-01-10 stsp free(client->packidx_path);
117 ae7c1b78 2023-01-10 stsp free(client->capabilities);
119 ae7c1b78 2023-01-10 stsp gotd_session_shutdown();
122 ae7c1b78 2023-01-10 stsp static void
123 ae7c1b78 2023-01-10 stsp disconnect_on_error(struct gotd_session_client *client,
124 ae7c1b78 2023-01-10 stsp const struct got_error *err)
126 ae7c1b78 2023-01-10 stsp struct imsgbuf ibuf;
128 ae7c1b78 2023-01-10 stsp if (err->code != GOT_ERR_EOF) {
129 1050403b 2023-08-11 stsp log_warnx("uid %d: %s", client->euid, err->msg);
130 ae7c1b78 2023-01-10 stsp imsg_init(&ibuf, client->fd);
131 b0614828 2023-06-19 stsp gotd_imsg_send_error(&ibuf, 0, gotd_session.proc_id, err);
132 ae7c1b78 2023-01-10 stsp imsg_clear(&ibuf);
135 ae7c1b78 2023-01-10 stsp disconnect(client);
138 ae7c1b78 2023-01-10 stsp static void
139 ae7c1b78 2023-01-10 stsp gotd_request_timeout(int fd, short events, void *arg)
141 ae7c1b78 2023-01-10 stsp struct gotd_session_client *client = arg;
143 ae7c1b78 2023-01-10 stsp log_debug("disconnecting uid %d due to timeout", client->euid);
144 ae7c1b78 2023-01-10 stsp disconnect(client);
148 ae7c1b78 2023-01-10 stsp gotd_session_sighdlr(int sig, short event, void *arg)
151 ae7c1b78 2023-01-10 stsp * Normal signal handler rules don't apply because libevent
152 ae7c1b78 2023-01-10 stsp * decouples for us.
155 ae7c1b78 2023-01-10 stsp switch (sig) {
156 ae7c1b78 2023-01-10 stsp case SIGHUP:
157 ae7c1b78 2023-01-10 stsp log_info("%s: ignoring SIGHUP", __func__);
159 ae7c1b78 2023-01-10 stsp case SIGUSR1:
160 ae7c1b78 2023-01-10 stsp log_info("%s: ignoring SIGUSR1", __func__);
162 ae7c1b78 2023-01-10 stsp case SIGTERM:
163 ae7c1b78 2023-01-10 stsp case SIGINT:
164 ae7c1b78 2023-01-10 stsp gotd_session_shutdown();
165 ae7c1b78 2023-01-10 stsp /* NOTREACHED */
168 ae7c1b78 2023-01-10 stsp fatalx("unexpected signal");
172 ae7c1b78 2023-01-10 stsp static const struct got_error *
173 ae7c1b78 2023-01-10 stsp recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
175 ae7c1b78 2023-01-10 stsp struct gotd_imsg_packfile_done idone;
176 ae7c1b78 2023-01-10 stsp size_t datalen;
178 ae7c1b78 2023-01-10 stsp log_debug("packfile-done received");
180 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
181 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(idone))
182 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
183 ae7c1b78 2023-01-10 stsp memcpy(&idone, imsg->data, sizeof(idone));
185 ae7c1b78 2023-01-10 stsp *client_id = idone.client_id;
186 ae7c1b78 2023-01-10 stsp return NULL;
189 ae7c1b78 2023-01-10 stsp static const struct got_error *
190 ae7c1b78 2023-01-10 stsp recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
192 ae7c1b78 2023-01-10 stsp struct gotd_imsg_packfile_install inst;
193 ae7c1b78 2023-01-10 stsp size_t datalen;
195 ae7c1b78 2023-01-10 stsp log_debug("packfile-install received");
197 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
198 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(inst))
199 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
200 ae7c1b78 2023-01-10 stsp memcpy(&inst, imsg->data, sizeof(inst));
202 ae7c1b78 2023-01-10 stsp *client_id = inst.client_id;
203 ae7c1b78 2023-01-10 stsp return NULL;
206 ae7c1b78 2023-01-10 stsp static const struct got_error *
207 ae7c1b78 2023-01-10 stsp recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
209 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_updates_start istart;
210 ae7c1b78 2023-01-10 stsp size_t datalen;
212 ae7c1b78 2023-01-10 stsp log_debug("ref-updates-start received");
214 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
215 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(istart))
216 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
217 ae7c1b78 2023-01-10 stsp memcpy(&istart, imsg->data, sizeof(istart));
219 ae7c1b78 2023-01-10 stsp *client_id = istart.client_id;
220 ae7c1b78 2023-01-10 stsp return NULL;
223 ae7c1b78 2023-01-10 stsp static const struct got_error *
224 ae7c1b78 2023-01-10 stsp recv_ref_update(uint32_t *client_id, struct imsg *imsg)
226 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_update iref;
227 ae7c1b78 2023-01-10 stsp size_t datalen;
229 ae7c1b78 2023-01-10 stsp log_debug("ref-update received");
231 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
232 ae7c1b78 2023-01-10 stsp if (datalen < sizeof(iref))
233 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
234 ae7c1b78 2023-01-10 stsp memcpy(&iref, imsg->data, sizeof(iref));
236 ae7c1b78 2023-01-10 stsp *client_id = iref.client_id;
237 ae7c1b78 2023-01-10 stsp return NULL;
240 ae7c1b78 2023-01-10 stsp static const struct got_error *
241 ae7c1b78 2023-01-10 stsp send_ref_update_ok(struct gotd_session_client *client,
242 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_update *iref, const char *refname)
244 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_update_ok iok;
245 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = &client->iev;
246 ae7c1b78 2023-01-10 stsp struct ibuf *wbuf;
247 ae7c1b78 2023-01-10 stsp size_t len;
249 ae7c1b78 2023-01-10 stsp memset(&iok, 0, sizeof(iok));
250 ae7c1b78 2023-01-10 stsp iok.client_id = client->id;
251 ae7c1b78 2023-01-10 stsp memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
252 ae7c1b78 2023-01-10 stsp memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
253 ae7c1b78 2023-01-10 stsp iok.name_len = strlen(refname);
255 ae7c1b78 2023-01-10 stsp len = sizeof(iok) + iok.name_len;
256 ae7c1b78 2023-01-10 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_OK,
257 b0614828 2023-06-19 stsp gotd_session.proc_id, gotd_session.pid, len);
258 ae7c1b78 2023-01-10 stsp if (wbuf == NULL)
259 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg_create REF_UPDATE_OK");
261 ae7c1b78 2023-01-10 stsp if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
262 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg_add REF_UPDATE_OK");
263 ae7c1b78 2023-01-10 stsp if (imsg_add(wbuf, refname, iok.name_len) == -1)
264 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg_add REF_UPDATE_OK");
266 ae7c1b78 2023-01-10 stsp wbuf->fd = -1;
267 ae7c1b78 2023-01-10 stsp imsg_close(&iev->ibuf, wbuf);
268 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
269 ae7c1b78 2023-01-10 stsp return NULL;
272 ae7c1b78 2023-01-10 stsp static void
273 ae7c1b78 2023-01-10 stsp send_refs_updated(struct gotd_session_client *client)
275 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
276 b0614828 2023-06-19 stsp gotd_session.proc_id, -1, NULL, 0) == -1)
277 ae7c1b78 2023-01-10 stsp log_warn("imsg compose REFS_UPDATED");
280 ae7c1b78 2023-01-10 stsp static const struct got_error *
281 ae7c1b78 2023-01-10 stsp send_ref_update_ng(struct gotd_session_client *client,
282 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_update *iref, const char *refname,
283 ae7c1b78 2023-01-10 stsp const char *reason)
285 ae7c1b78 2023-01-10 stsp const struct got_error *ng_err;
286 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_update_ng ing;
287 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = &client->iev;
288 ae7c1b78 2023-01-10 stsp struct ibuf *wbuf;
289 ae7c1b78 2023-01-10 stsp size_t len;
291 ae7c1b78 2023-01-10 stsp memset(&ing, 0, sizeof(ing));
292 ae7c1b78 2023-01-10 stsp ing.client_id = client->id;
293 ae7c1b78 2023-01-10 stsp memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
294 ae7c1b78 2023-01-10 stsp memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
295 ae7c1b78 2023-01-10 stsp ing.name_len = strlen(refname);
297 ae7c1b78 2023-01-10 stsp ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
298 ae7c1b78 2023-01-10 stsp ing.reason_len = strlen(ng_err->msg);
300 ae7c1b78 2023-01-10 stsp len = sizeof(ing) + ing.name_len + ing.reason_len;
301 ae7c1b78 2023-01-10 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_NG,
302 b0614828 2023-06-19 stsp gotd_session.proc_id, gotd_session.pid, len);
303 ae7c1b78 2023-01-10 stsp if (wbuf == NULL)
304 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg_create REF_UPDATE_NG");
306 ae7c1b78 2023-01-10 stsp if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
307 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
308 ae7c1b78 2023-01-10 stsp if (imsg_add(wbuf, refname, ing.name_len) == -1)
309 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
310 ae7c1b78 2023-01-10 stsp if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
311 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
313 ae7c1b78 2023-01-10 stsp wbuf->fd = -1;
314 ae7c1b78 2023-01-10 stsp imsg_close(&iev->ibuf, wbuf);
315 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
316 ae7c1b78 2023-01-10 stsp return NULL;
319 ae7c1b78 2023-01-10 stsp static const struct got_error *
320 ae7c1b78 2023-01-10 stsp install_pack(struct gotd_session_client *client, const char *repo_path,
321 ae7c1b78 2023-01-10 stsp struct imsg *imsg)
323 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
324 ae7c1b78 2023-01-10 stsp struct gotd_imsg_packfile_install inst;
325 ae7c1b78 2023-01-10 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
326 ae7c1b78 2023-01-10 stsp size_t datalen;
327 ae7c1b78 2023-01-10 stsp char *packfile_path = NULL, *packidx_path = NULL;
329 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
330 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(inst))
331 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
332 ae7c1b78 2023-01-10 stsp memcpy(&inst, imsg->data, sizeof(inst));
334 ae7c1b78 2023-01-10 stsp if (client->packfile_path == NULL)
335 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
336 ae7c1b78 2023-01-10 stsp "client has no pack file");
337 ae7c1b78 2023-01-10 stsp if (client->packidx_path == NULL)
338 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
339 ae7c1b78 2023-01-10 stsp "client has no pack file index");
341 ae7c1b78 2023-01-10 stsp if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
342 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_NO_SPACE,
343 ae7c1b78 2023-01-10 stsp "could not convert pack file SHA1 to hex");
345 ae7c1b78 2023-01-10 stsp if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
346 ae7c1b78 2023-01-10 stsp repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
347 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("asprintf");
351 ae7c1b78 2023-01-10 stsp if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
352 ae7c1b78 2023-01-10 stsp repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
353 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("asprintf");
357 ae7c1b78 2023-01-10 stsp if (rename(client->packfile_path, packfile_path) == -1) {
358 ae7c1b78 2023-01-10 stsp err = got_error_from_errno3("rename", client->packfile_path,
359 ae7c1b78 2023-01-10 stsp packfile_path);
363 ae7c1b78 2023-01-10 stsp free(client->packfile_path);
364 ae7c1b78 2023-01-10 stsp client->packfile_path = NULL;
366 ae7c1b78 2023-01-10 stsp if (rename(client->packidx_path, packidx_path) == -1) {
367 ae7c1b78 2023-01-10 stsp err = got_error_from_errno3("rename", client->packidx_path,
368 ae7c1b78 2023-01-10 stsp packidx_path);
372 ae7c1b78 2023-01-10 stsp free(client->packidx_path);
373 ae7c1b78 2023-01-10 stsp client->packidx_path = NULL;
375 ae7c1b78 2023-01-10 stsp free(packfile_path);
376 ae7c1b78 2023-01-10 stsp free(packidx_path);
377 ae7c1b78 2023-01-10 stsp return err;
380 ae7c1b78 2023-01-10 stsp static const struct got_error *
381 ae7c1b78 2023-01-10 stsp begin_ref_updates(struct gotd_session_client *client, struct imsg *imsg)
383 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_updates_start istart;
384 ae7c1b78 2023-01-10 stsp size_t datalen;
386 ae7c1b78 2023-01-10 stsp if (client->nref_updates != -1)
387 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
389 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
390 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(istart))
391 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
392 ae7c1b78 2023-01-10 stsp memcpy(&istart, imsg->data, sizeof(istart));
394 ae7c1b78 2023-01-10 stsp if (istart.nref_updates <= 0)
395 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
397 ae7c1b78 2023-01-10 stsp client->nref_updates = istart.nref_updates;
398 ae7c1b78 2023-01-10 stsp return NULL;
401 ae7c1b78 2023-01-10 stsp static const struct got_error *
402 0ff2c315 2023-01-18 stsp update_ref(int *shut, struct gotd_session_client *client,
403 0ff2c315 2023-01-18 stsp const char *repo_path, struct imsg *imsg)
405 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
406 ae7c1b78 2023-01-10 stsp struct got_repository *repo = NULL;
407 ae7c1b78 2023-01-10 stsp struct got_reference *ref = NULL;
408 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_update iref;
409 ae7c1b78 2023-01-10 stsp struct got_object_id old_id, new_id;
410 ae7c1b78 2023-01-10 stsp struct got_object_id *id = NULL;
411 ae7c1b78 2023-01-10 stsp struct got_object *obj = NULL;
412 ae7c1b78 2023-01-10 stsp char *refname = NULL;
413 ae7c1b78 2023-01-10 stsp size_t datalen;
414 ae7c1b78 2023-01-10 stsp int locked = 0;
415 1df1761f 2023-06-16 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
416 1df1761f 2023-06-16 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
418 ae7c1b78 2023-01-10 stsp log_debug("update-ref from uid %d", client->euid);
420 ae7c1b78 2023-01-10 stsp if (client->nref_updates <= 0)
421 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
423 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
424 ae7c1b78 2023-01-10 stsp if (datalen < sizeof(iref))
425 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
426 ae7c1b78 2023-01-10 stsp memcpy(&iref, imsg->data, sizeof(iref));
427 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(iref) + iref.name_len)
428 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
429 00b3e9ae 2023-01-11 op refname = strndup(imsg->data + sizeof(iref), iref.name_len);
430 ae7c1b78 2023-01-10 stsp if (refname == NULL)
431 00b3e9ae 2023-01-11 op return got_error_from_errno("strndup");
433 ae7c1b78 2023-01-10 stsp log_debug("updating ref %s for uid %d", refname, client->euid);
435 ae7c1b78 2023-01-10 stsp err = got_repo_open(&repo, repo_path, NULL, NULL);
439 ae7c1b78 2023-01-10 stsp memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
440 ae7c1b78 2023-01-10 stsp memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
441 9a8e357c 2023-01-28 op err = got_object_open(&obj, repo,
442 9a8e357c 2023-01-28 op iref.delete_ref ? &old_id : &new_id);
446 ae7c1b78 2023-01-10 stsp if (iref.ref_is_new) {
447 ae7c1b78 2023-01-10 stsp err = got_ref_open(&ref, repo, refname, 0);
449 ae7c1b78 2023-01-10 stsp if (err->code != GOT_ERR_NOT_REF)
451 ae7c1b78 2023-01-10 stsp err = got_ref_alloc(&ref, refname, &new_id);
454 ae7c1b78 2023-01-10 stsp err = got_ref_write(ref, repo); /* will lock/unlock */
458 1df1761f 2023-06-16 stsp err = got_ref_resolve(&id, repo, ref);
461 1df1761f 2023-06-16 stsp got_object_id_hex(&new_id, hex1, sizeof(hex1));
462 1df1761f 2023-06-16 stsp got_object_id_hex(id, hex2, sizeof(hex2));
463 ae7c1b78 2023-01-10 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
464 1df1761f 2023-06-16 stsp "Addition %s: %s failed; %s: %s has been "
465 1df1761f 2023-06-16 stsp "created by someone else while transaction "
466 1df1761f 2023-06-16 stsp "was in progress",
467 1df1761f 2023-06-16 stsp got_ref_get_name(ref), hex1,
468 1df1761f 2023-06-16 stsp got_ref_get_name(ref), hex2);
471 9a8e357c 2023-01-28 op } else if (iref.delete_ref) {
472 9a8e357c 2023-01-28 op err = got_ref_open(&ref, repo, refname, 1 /* lock */);
477 9a8e357c 2023-01-28 op err = got_ref_resolve(&id, repo, ref);
481 9a8e357c 2023-01-28 op if (got_object_id_cmp(id, &old_id) != 0) {
482 1df1761f 2023-06-16 stsp got_object_id_hex(&old_id, hex1, sizeof(hex1));
483 1df1761f 2023-06-16 stsp got_object_id_hex(id, hex2, sizeof(hex2));
484 9a8e357c 2023-01-28 op err = got_error_fmt(GOT_ERR_REF_BUSY,
485 1df1761f 2023-06-16 stsp "Deletion %s: %s failed; %s: %s has been "
486 1df1761f 2023-06-16 stsp "created by someone else while transaction "
487 1df1761f 2023-06-16 stsp "was in progress",
488 1df1761f 2023-06-16 stsp got_ref_get_name(ref), hex1,
489 1df1761f 2023-06-16 stsp got_ref_get_name(ref), hex2);
493 9a8e357c 2023-01-28 op err = got_ref_delete(ref, repo);
500 ae7c1b78 2023-01-10 stsp err = got_ref_open(&ref, repo, refname, 1 /* lock */);
503 ae7c1b78 2023-01-10 stsp locked = 1;
505 ae7c1b78 2023-01-10 stsp err = got_ref_resolve(&id, repo, ref);
509 ae7c1b78 2023-01-10 stsp if (got_object_id_cmp(id, &old_id) != 0) {
510 1df1761f 2023-06-16 stsp got_object_id_hex(&old_id, hex1, sizeof(hex1));
511 1df1761f 2023-06-16 stsp got_object_id_hex(id, hex2, sizeof(hex2));
512 ae7c1b78 2023-01-10 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
513 1df1761f 2023-06-16 stsp "Update %s: %s failed; %s: %s has been "
514 1df1761f 2023-06-16 stsp "created by someone else while transaction "
515 1df1761f 2023-06-16 stsp "was in progress",
516 1df1761f 2023-06-16 stsp got_ref_get_name(ref), hex1,
517 1df1761f 2023-06-16 stsp got_ref_get_name(ref), hex2);
521 f8780fee 2023-06-16 stsp if (got_object_id_cmp(&new_id, &old_id) != 0) {
522 f8780fee 2023-06-16 stsp err = got_ref_change_ref(ref, &new_id);
526 f8780fee 2023-06-16 stsp err = got_ref_write(ref, repo);
536 ae7c1b78 2023-01-10 stsp if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
537 ae7c1b78 2023-01-10 stsp err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
538 ae7c1b78 2023-01-10 stsp "could not acquire exclusive file lock for %s",
541 ae7c1b78 2023-01-10 stsp send_ref_update_ng(client, &iref, refname, err->msg);
543 ae7c1b78 2023-01-10 stsp send_ref_update_ok(client, &iref, refname);
545 ae7c1b78 2023-01-10 stsp if (client->nref_updates > 0) {
546 ae7c1b78 2023-01-10 stsp client->nref_updates--;
547 0ff2c315 2023-01-18 stsp if (client->nref_updates == 0) {
548 ae7c1b78 2023-01-10 stsp send_refs_updated(client);
549 808264b2 2023-08-22 stsp client->flush_disconnect = 1;
553 ae7c1b78 2023-01-10 stsp if (locked) {
554 ae7c1b78 2023-01-10 stsp const struct got_error *unlock_err;
555 ae7c1b78 2023-01-10 stsp unlock_err = got_ref_unlock(ref);
556 ae7c1b78 2023-01-10 stsp if (unlock_err && err == NULL)
557 ae7c1b78 2023-01-10 stsp err = unlock_err;
560 ae7c1b78 2023-01-10 stsp got_ref_close(ref);
562 ae7c1b78 2023-01-10 stsp got_object_close(obj);
564 ae7c1b78 2023-01-10 stsp got_repo_close(repo);
565 ae7c1b78 2023-01-10 stsp free(refname);
567 ae7c1b78 2023-01-10 stsp return err;
570 ae7c1b78 2023-01-10 stsp static void
571 ae7c1b78 2023-01-10 stsp session_dispatch_repo_child(int fd, short event, void *arg)
573 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
574 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
575 ae7c1b78 2023-01-10 stsp struct gotd_session_client *client = &gotd_session_client;
577 ae7c1b78 2023-01-10 stsp int shut = 0;
578 ae7c1b78 2023-01-10 stsp struct imsg imsg;
580 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
581 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
582 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
583 ae7c1b78 2023-01-10 stsp if (n == 0) {
584 ae7c1b78 2023-01-10 stsp /* Connection closed. */
590 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
591 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
592 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
593 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
594 ae7c1b78 2023-01-10 stsp if (n == 0) {
595 ae7c1b78 2023-01-10 stsp /* Connection closed. */
602 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
603 ae7c1b78 2023-01-10 stsp uint32_t client_id = 0;
604 ae7c1b78 2023-01-10 stsp int do_disconnect = 0;
605 ae7c1b78 2023-01-10 stsp int do_ref_updates = 0, do_ref_update = 0;
606 ae7c1b78 2023-01-10 stsp int do_packfile_install = 0;
608 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
609 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get error", __func__);
610 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
613 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
614 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_ERROR:
615 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
616 ae7c1b78 2023-01-10 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
618 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_PACKFILE_DONE:
619 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
620 ae7c1b78 2023-01-10 stsp err = recv_packfile_done(&client_id, &imsg);
622 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_PACKFILE_INSTALL:
623 ae7c1b78 2023-01-10 stsp err = recv_packfile_install(&client_id, &imsg);
624 ae7c1b78 2023-01-10 stsp if (err == NULL)
625 ae7c1b78 2023-01-10 stsp do_packfile_install = 1;
627 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_REF_UPDATES_START:
628 ae7c1b78 2023-01-10 stsp err = recv_ref_updates_start(&client_id, &imsg);
629 ae7c1b78 2023-01-10 stsp if (err == NULL)
630 ae7c1b78 2023-01-10 stsp do_ref_updates = 1;
632 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_REF_UPDATE:
633 ae7c1b78 2023-01-10 stsp err = recv_ref_update(&client_id, &imsg);
634 ae7c1b78 2023-01-10 stsp if (err == NULL)
635 ae7c1b78 2023-01-10 stsp do_ref_update = 1;
638 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
642 ae7c1b78 2023-01-10 stsp if (do_disconnect) {
644 ae7c1b78 2023-01-10 stsp disconnect_on_error(client, err);
646 ae7c1b78 2023-01-10 stsp disconnect(client);
648 ae7c1b78 2023-01-10 stsp if (do_packfile_install)
649 ae7c1b78 2023-01-10 stsp err = install_pack(client,
650 ae7c1b78 2023-01-10 stsp gotd_session.repo->path, &imsg);
651 ae7c1b78 2023-01-10 stsp else if (do_ref_updates)
652 ae7c1b78 2023-01-10 stsp err = begin_ref_updates(client, &imsg);
653 ae7c1b78 2023-01-10 stsp else if (do_ref_update)
654 0ff2c315 2023-01-18 stsp err = update_ref(&shut, client,
655 ae7c1b78 2023-01-10 stsp gotd_session.repo->path, &imsg);
657 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
659 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
662 ae7c1b78 2023-01-10 stsp if (!shut) {
663 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
665 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
666 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
667 ae7c1b78 2023-01-10 stsp event_loopexit(NULL);
671 ae7c1b78 2023-01-10 stsp static const struct got_error *
672 ae7c1b78 2023-01-10 stsp recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
674 ae7c1b78 2023-01-10 stsp struct gotd_imsg_capabilities icapas;
675 ae7c1b78 2023-01-10 stsp size_t datalen;
677 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
678 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(icapas))
679 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
680 ae7c1b78 2023-01-10 stsp memcpy(&icapas, imsg->data, sizeof(icapas));
682 ae7c1b78 2023-01-10 stsp client->ncapa_alloc = icapas.ncapabilities;
683 ae7c1b78 2023-01-10 stsp client->capabilities = calloc(client->ncapa_alloc,
684 ae7c1b78 2023-01-10 stsp sizeof(*client->capabilities));
685 ae7c1b78 2023-01-10 stsp if (client->capabilities == NULL) {
686 ae7c1b78 2023-01-10 stsp client->ncapa_alloc = 0;
687 ae7c1b78 2023-01-10 stsp return got_error_from_errno("calloc");
690 ae7c1b78 2023-01-10 stsp log_debug("expecting %zu capabilities from uid %d",
691 ae7c1b78 2023-01-10 stsp client->ncapa_alloc, client->euid);
692 ae7c1b78 2023-01-10 stsp return NULL;
695 ae7c1b78 2023-01-10 stsp static const struct got_error *
696 ae7c1b78 2023-01-10 stsp recv_capability(struct gotd_session_client *client, struct imsg *imsg)
698 ae7c1b78 2023-01-10 stsp struct gotd_imsg_capability icapa;
699 ae7c1b78 2023-01-10 stsp struct gotd_client_capability *capa;
700 ae7c1b78 2023-01-10 stsp size_t datalen;
701 ae7c1b78 2023-01-10 stsp char *key, *value = NULL;
703 ae7c1b78 2023-01-10 stsp if (client->capabilities == NULL ||
704 ae7c1b78 2023-01-10 stsp client->ncapabilities >= client->ncapa_alloc) {
705 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
706 ae7c1b78 2023-01-10 stsp "unexpected capability received");
709 ae7c1b78 2023-01-10 stsp memset(&icapa, 0, sizeof(icapa));
711 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
712 ae7c1b78 2023-01-10 stsp if (datalen < sizeof(icapa))
713 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
714 ae7c1b78 2023-01-10 stsp memcpy(&icapa, imsg->data, sizeof(icapa));
716 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
717 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
719 00b3e9ae 2023-01-11 op key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
720 ae7c1b78 2023-01-10 stsp if (key == NULL)
721 00b3e9ae 2023-01-11 op return got_error_from_errno("strndup");
722 ae7c1b78 2023-01-10 stsp if (icapa.value_len > 0) {
723 00b3e9ae 2023-01-11 op value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
724 00b3e9ae 2023-01-11 op icapa.value_len);
725 ae7c1b78 2023-01-10 stsp if (value == NULL) {
727 00b3e9ae 2023-01-11 op return got_error_from_errno("strndup");
731 ae7c1b78 2023-01-10 stsp capa = &client->capabilities[client->ncapabilities++];
732 ae7c1b78 2023-01-10 stsp capa->key = key;
733 ae7c1b78 2023-01-10 stsp capa->value = value;
736 ae7c1b78 2023-01-10 stsp log_debug("uid %d: capability %s=%s", client->euid, key, value);
738 ae7c1b78 2023-01-10 stsp log_debug("uid %d: capability %s", client->euid, key);
740 ae7c1b78 2023-01-10 stsp return NULL;
743 ae7c1b78 2023-01-10 stsp static const struct got_error *
744 ae7c1b78 2023-01-10 stsp ensure_client_is_reading(struct gotd_session_client *client)
746 ae7c1b78 2023-01-10 stsp if (client->is_writing) {
747 ae7c1b78 2023-01-10 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
748 ae7c1b78 2023-01-10 stsp "uid %d made a read-request but is not reading from "
749 ae7c1b78 2023-01-10 stsp "a repository", client->euid);
752 ae7c1b78 2023-01-10 stsp return NULL;
755 ae7c1b78 2023-01-10 stsp static const struct got_error *
756 ae7c1b78 2023-01-10 stsp ensure_client_is_writing(struct gotd_session_client *client)
758 ae7c1b78 2023-01-10 stsp if (!client->is_writing) {
759 ae7c1b78 2023-01-10 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
760 ae7c1b78 2023-01-10 stsp "uid %d made a write-request but is not writing to "
761 ae7c1b78 2023-01-10 stsp "a repository", client->euid);
764 ae7c1b78 2023-01-10 stsp return NULL;
767 ae7c1b78 2023-01-10 stsp static const struct got_error *
768 ae7c1b78 2023-01-10 stsp forward_want(struct gotd_session_client *client, struct imsg *imsg)
770 ae7c1b78 2023-01-10 stsp struct gotd_imsg_want ireq;
771 ae7c1b78 2023-01-10 stsp struct gotd_imsg_want iwant;
772 ae7c1b78 2023-01-10 stsp size_t datalen;
774 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
775 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(ireq))
776 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
778 ae7c1b78 2023-01-10 stsp memcpy(&ireq, imsg->data, datalen);
780 ae7c1b78 2023-01-10 stsp memset(&iwant, 0, sizeof(iwant));
781 ae7c1b78 2023-01-10 stsp memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
782 ae7c1b78 2023-01-10 stsp iwant.client_id = client->id;
784 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_WANT,
785 b0614828 2023-06-19 stsp gotd_session.proc_id, -1, &iwant, sizeof(iwant)) == -1)
786 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg compose WANT");
788 ae7c1b78 2023-01-10 stsp return NULL;
791 ae7c1b78 2023-01-10 stsp static const struct got_error *
792 ae7c1b78 2023-01-10 stsp forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
794 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
795 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_update ireq;
796 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_update *iref = NULL;
797 ae7c1b78 2023-01-10 stsp size_t datalen;
799 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
800 ae7c1b78 2023-01-10 stsp if (datalen < sizeof(ireq))
801 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
802 ae7c1b78 2023-01-10 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
803 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(ireq) + ireq.name_len)
804 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
806 ae7c1b78 2023-01-10 stsp iref = malloc(datalen);
807 ae7c1b78 2023-01-10 stsp if (iref == NULL)
808 ae7c1b78 2023-01-10 stsp return got_error_from_errno("malloc");
809 ae7c1b78 2023-01-10 stsp memcpy(iref, imsg->data, datalen);
811 ae7c1b78 2023-01-10 stsp iref->client_id = client->id;
812 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->repo_child_iev,
813 b0614828 2023-06-19 stsp GOTD_IMSG_REF_UPDATE, gotd_session.proc_id, -1,
814 b0614828 2023-06-19 stsp iref, datalen) == -1)
815 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose REF_UPDATE");
816 ae7c1b78 2023-01-10 stsp free(iref);
817 ae7c1b78 2023-01-10 stsp return err;
820 ae7c1b78 2023-01-10 stsp static const struct got_error *
821 ae7c1b78 2023-01-10 stsp forward_have(struct gotd_session_client *client, struct imsg *imsg)
823 ae7c1b78 2023-01-10 stsp struct gotd_imsg_have ireq;
824 ae7c1b78 2023-01-10 stsp struct gotd_imsg_have ihave;
825 ae7c1b78 2023-01-10 stsp size_t datalen;
827 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
828 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(ireq))
829 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
831 ae7c1b78 2023-01-10 stsp memcpy(&ireq, imsg->data, datalen);
833 ae7c1b78 2023-01-10 stsp memset(&ihave, 0, sizeof(ihave));
834 ae7c1b78 2023-01-10 stsp memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
835 ae7c1b78 2023-01-10 stsp ihave.client_id = client->id;
837 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_HAVE,
838 b0614828 2023-06-19 stsp gotd_session.proc_id, -1, &ihave, sizeof(ihave)) == -1)
839 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg compose HAVE");
841 ae7c1b78 2023-01-10 stsp return NULL;
845 ae7c1b78 2023-01-10 stsp client_has_capability(struct gotd_session_client *client, const char *capastr)
847 ae7c1b78 2023-01-10 stsp struct gotd_client_capability *capa;
850 ae7c1b78 2023-01-10 stsp if (client->ncapabilities == 0)
853 ae7c1b78 2023-01-10 stsp for (i = 0; i < client->ncapabilities; i++) {
854 ae7c1b78 2023-01-10 stsp capa = &client->capabilities[i];
855 ae7c1b78 2023-01-10 stsp if (strcmp(capa->key, capastr) == 0)
862 ae7c1b78 2023-01-10 stsp static const struct got_error *
863 ae7c1b78 2023-01-10 stsp recv_packfile(struct gotd_session_client *client)
865 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
866 ae7c1b78 2023-01-10 stsp struct gotd_imsg_recv_packfile ipack;
867 ae7c1b78 2023-01-10 stsp struct gotd_imsg_packfile_pipe ipipe;
868 ae7c1b78 2023-01-10 stsp struct gotd_imsg_packidx_file ifile;
869 ae7c1b78 2023-01-10 stsp char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
870 ae7c1b78 2023-01-10 stsp int packfd = -1, idxfd = -1;
871 ae7c1b78 2023-01-10 stsp int pipe[2] = { -1, -1 };
873 ae7c1b78 2023-01-10 stsp if (client->packfile_path) {
874 ae7c1b78 2023-01-10 stsp return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
875 ae7c1b78 2023-01-10 stsp "uid %d already has a pack file", client->euid);
878 ae7c1b78 2023-01-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
879 ae7c1b78 2023-01-10 stsp return got_error_from_errno("socketpair");
881 ae7c1b78 2023-01-10 stsp memset(&ipipe, 0, sizeof(ipipe));
882 ae7c1b78 2023-01-10 stsp ipipe.client_id = client->id;
884 ae7c1b78 2023-01-10 stsp /* Send pack pipe end 0 to repo child process. */
885 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->repo_child_iev,
886 b0614828 2023-06-19 stsp GOTD_IMSG_PACKFILE_PIPE, gotd_session.proc_id, pipe[0],
887 ae7c1b78 2023-01-10 stsp &ipipe, sizeof(ipipe)) == -1) {
888 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
889 ae7c1b78 2023-01-10 stsp pipe[0] = -1;
892 ae7c1b78 2023-01-10 stsp pipe[0] = -1;
894 ae7c1b78 2023-01-10 stsp /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
895 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->iev,
896 b0614828 2023-06-19 stsp GOTD_IMSG_PACKFILE_PIPE, gotd_session.proc_id, pipe[1],
897 b0614828 2023-06-19 stsp NULL, 0) == -1)
898 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
899 ae7c1b78 2023-01-10 stsp pipe[1] = -1;
901 ae7c1b78 2023-01-10 stsp if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
902 ae7c1b78 2023-01-10 stsp got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
903 ae7c1b78 2023-01-10 stsp client->euid) == -1) {
904 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("asprintf");
908 ae7c1b78 2023-01-10 stsp err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
911 839bbaae 2023-02-01 op if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
912 839bbaae 2023-02-01 op err = got_error_from_errno2("fchmod", pack_path);
916 ae7c1b78 2023-01-10 stsp free(basepath);
917 ae7c1b78 2023-01-10 stsp if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
918 ae7c1b78 2023-01-10 stsp got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
919 ae7c1b78 2023-01-10 stsp client->euid) == -1) {
920 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("asprintf");
921 ae7c1b78 2023-01-10 stsp basepath = NULL;
924 ae7c1b78 2023-01-10 stsp err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
927 839bbaae 2023-02-01 op if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
928 839bbaae 2023-02-01 op err = got_error_from_errno2("fchmod", idx_path);
932 ae7c1b78 2023-01-10 stsp memset(&ifile, 0, sizeof(ifile));
933 ae7c1b78 2023-01-10 stsp ifile.client_id = client->id;
934 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->repo_child_iev,
935 b0614828 2023-06-19 stsp GOTD_IMSG_PACKIDX_FILE, gotd_session.proc_id,
936 ae7c1b78 2023-01-10 stsp idxfd, &ifile, sizeof(ifile)) == -1) {
937 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose PACKIDX_FILE");
938 ae7c1b78 2023-01-10 stsp idxfd = -1;
941 ae7c1b78 2023-01-10 stsp idxfd = -1;
943 ae7c1b78 2023-01-10 stsp memset(&ipack, 0, sizeof(ipack));
944 ae7c1b78 2023-01-10 stsp ipack.client_id = client->id;
945 ae7c1b78 2023-01-10 stsp if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
946 ae7c1b78 2023-01-10 stsp ipack.report_status = 1;
948 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->repo_child_iev,
949 b0614828 2023-06-19 stsp GOTD_IMSG_RECV_PACKFILE, gotd_session.proc_id, packfd,
950 ae7c1b78 2023-01-10 stsp &ipack, sizeof(ipack)) == -1) {
951 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose RECV_PACKFILE");
952 ae7c1b78 2023-01-10 stsp packfd = -1;
955 ae7c1b78 2023-01-10 stsp packfd = -1;
958 ae7c1b78 2023-01-10 stsp free(basepath);
959 ae7c1b78 2023-01-10 stsp if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
960 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("close");
961 ae7c1b78 2023-01-10 stsp if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
962 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("close");
963 ae7c1b78 2023-01-10 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
964 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("close");
965 ae7c1b78 2023-01-10 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
966 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("close");
968 ae7c1b78 2023-01-10 stsp free(pack_path);
969 ae7c1b78 2023-01-10 stsp free(idx_path);
971 ae7c1b78 2023-01-10 stsp client->packfile_path = pack_path;
972 ae7c1b78 2023-01-10 stsp client->packidx_path = idx_path;
974 ae7c1b78 2023-01-10 stsp return err;
977 ae7c1b78 2023-01-10 stsp static const struct got_error *
978 ae7c1b78 2023-01-10 stsp send_packfile(struct gotd_session_client *client)
980 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
981 ae7c1b78 2023-01-10 stsp struct gotd_imsg_send_packfile ipack;
982 ae7c1b78 2023-01-10 stsp struct gotd_imsg_packfile_pipe ipipe;
983 ae7c1b78 2023-01-10 stsp int pipe[2];
985 ae7c1b78 2023-01-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
986 ae7c1b78 2023-01-10 stsp return got_error_from_errno("socketpair");
988 ae7c1b78 2023-01-10 stsp memset(&ipack, 0, sizeof(ipack));
989 ae7c1b78 2023-01-10 stsp memset(&ipipe, 0, sizeof(ipipe));
991 ae7c1b78 2023-01-10 stsp ipack.client_id = client->id;
992 ae7c1b78 2023-01-10 stsp if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
993 ae7c1b78 2023-01-10 stsp ipack.report_progress = 1;
995 ae7c1b78 2023-01-10 stsp client->delta_cache_fd = got_opentempfd();
996 ae7c1b78 2023-01-10 stsp if (client->delta_cache_fd == -1)
997 ae7c1b78 2023-01-10 stsp return got_error_from_errno("got_opentempfd");
999 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->repo_child_iev,
1000 ae7c1b78 2023-01-10 stsp GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
1001 ae7c1b78 2023-01-10 stsp &ipack, sizeof(ipack)) == -1) {
1002 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose SEND_PACKFILE");
1003 ae7c1b78 2023-01-10 stsp close(pipe[0]);
1004 ae7c1b78 2023-01-10 stsp close(pipe[1]);
1005 ae7c1b78 2023-01-10 stsp return err;
1008 ae7c1b78 2023-01-10 stsp ipipe.client_id = client->id;
1010 ae7c1b78 2023-01-10 stsp /* Send pack pipe end 0 to repo child process. */
1011 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->repo_child_iev,
1012 ae7c1b78 2023-01-10 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD,
1013 ae7c1b78 2023-01-10 stsp pipe[0], &ipipe, sizeof(ipipe)) == -1) {
1014 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1015 ae7c1b78 2023-01-10 stsp close(pipe[1]);
1016 ae7c1b78 2023-01-10 stsp return err;
1019 ae7c1b78 2023-01-10 stsp /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1020 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->iev,
1021 ae7c1b78 2023-01-10 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
1022 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1024 ae7c1b78 2023-01-10 stsp return err;
1027 ae7c1b78 2023-01-10 stsp static void
1028 3931a8a4 2023-02-03 stsp session_dispatch_client(int fd, short events, void *arg)
1030 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
1031 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
1032 ae7c1b78 2023-01-10 stsp struct gotd_session_client *client = &gotd_session_client;
1033 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1034 ae7c1b78 2023-01-10 stsp struct imsg imsg;
1035 ae7c1b78 2023-01-10 stsp ssize_t n;
1037 ae7c1b78 2023-01-10 stsp if (events & EV_WRITE) {
1038 ae7c1b78 2023-01-10 stsp while (ibuf->w.queued) {
1039 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
1040 ae7c1b78 2023-01-10 stsp if (n == -1 && errno == EPIPE) {
1042 ae7c1b78 2023-01-10 stsp * The client has closed its socket.
1043 ae7c1b78 2023-01-10 stsp * This can happen when Git clients are
1044 ae7c1b78 2023-01-10 stsp * done sending pack file data.
1046 ae7c1b78 2023-01-10 stsp msgbuf_clear(&ibuf->w);
1048 ae7c1b78 2023-01-10 stsp } else if (n == -1 && errno != EAGAIN) {
1049 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg_flush");
1050 ae7c1b78 2023-01-10 stsp disconnect_on_error(client, err);
1053 ae7c1b78 2023-01-10 stsp if (n == 0) {
1054 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1055 ae7c1b78 2023-01-10 stsp err = got_error(GOT_ERR_EOF);
1056 ae7c1b78 2023-01-10 stsp disconnect_on_error(client, err);
1061 808264b2 2023-08-22 stsp if (client->flush_disconnect) {
1062 808264b2 2023-08-22 stsp disconnect(client);
1067 ae7c1b78 2023-01-10 stsp if ((events & EV_READ) == 0)
1070 ae7c1b78 2023-01-10 stsp memset(&imsg, 0, sizeof(imsg));
1072 ae7c1b78 2023-01-10 stsp while (err == NULL) {
1073 ae7c1b78 2023-01-10 stsp err = gotd_imsg_recv(&imsg, ibuf, 0);
1074 ae7c1b78 2023-01-10 stsp if (err) {
1075 ae7c1b78 2023-01-10 stsp if (err->code == GOT_ERR_PRIVSEP_READ)
1076 ae7c1b78 2023-01-10 stsp err = NULL;
1077 baaae615 2023-06-07 stsp else if (err->code == GOT_ERR_EOF &&
1078 baaae615 2023-06-07 stsp client->state == GOTD_STATE_EXPECT_CAPABILITIES) {
1080 baaae615 2023-06-07 stsp * The client has closed its socket before
1081 baaae615 2023-06-07 stsp * sending its capability announcement.
1082 baaae615 2023-06-07 stsp * This can happen when Git clients have
1083 baaae615 2023-06-07 stsp * no ref-updates to send.
1085 baaae615 2023-06-07 stsp disconnect_on_error(client, err);
1091 ae7c1b78 2023-01-10 stsp evtimer_del(&client->tmo);
1093 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
1094 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CAPABILITIES:
1095 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1096 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1097 ae7c1b78 2023-01-10 stsp "unexpected capabilities received");
1100 ae7c1b78 2023-01-10 stsp log_debug("receiving capabilities from uid %d",
1101 ae7c1b78 2023-01-10 stsp client->euid);
1102 ae7c1b78 2023-01-10 stsp err = recv_capabilities(client, &imsg);
1104 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CAPABILITY:
1105 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1106 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1107 ae7c1b78 2023-01-10 stsp "unexpected capability received");
1110 ae7c1b78 2023-01-10 stsp err = recv_capability(client, &imsg);
1111 ae7c1b78 2023-01-10 stsp if (err || client->ncapabilities < client->ncapa_alloc)
1113 ae7c1b78 2023-01-10 stsp if (!client->is_writing) {
1114 ae7c1b78 2023-01-10 stsp client->state = GOTD_STATE_EXPECT_WANT;
1115 3448a19a 2023-01-21 stsp client->accept_flush_pkt = 1;
1116 ae7c1b78 2023-01-10 stsp log_debug("uid %d: expecting want-lines",
1117 ae7c1b78 2023-01-10 stsp client->euid);
1118 ae7c1b78 2023-01-10 stsp } else if (client->is_writing) {
1119 ae7c1b78 2023-01-10 stsp client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1120 3448a19a 2023-01-21 stsp client->accept_flush_pkt = 1;
1121 ae7c1b78 2023-01-10 stsp log_debug("uid %d: expecting ref-update-lines",
1122 ae7c1b78 2023-01-10 stsp client->euid);
1124 ae7c1b78 2023-01-10 stsp fatalx("client %d is both reading and writing",
1125 ae7c1b78 2023-01-10 stsp client->euid);
1127 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_WANT:
1128 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_WANT) {
1129 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1130 ae7c1b78 2023-01-10 stsp "unexpected want-line received");
1133 ae7c1b78 2023-01-10 stsp log_debug("received want-line from uid %d",
1134 ae7c1b78 2023-01-10 stsp client->euid);
1135 ae7c1b78 2023-01-10 stsp err = ensure_client_is_reading(client);
1138 3448a19a 2023-01-21 stsp client->accept_flush_pkt = 1;
1139 ae7c1b78 2023-01-10 stsp err = forward_want(client, &imsg);
1141 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_REF_UPDATE:
1142 6da1c69c 2023-01-18 stsp if (client->state != GOTD_STATE_EXPECT_REF_UPDATE &&
1143 6da1c69c 2023-01-18 stsp client->state !=
1144 6da1c69c 2023-01-18 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1145 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1146 ae7c1b78 2023-01-10 stsp "unexpected ref-update-line received");
1149 ae7c1b78 2023-01-10 stsp log_debug("received ref-update-line from uid %d",
1150 ae7c1b78 2023-01-10 stsp client->euid);
1151 ae7c1b78 2023-01-10 stsp err = ensure_client_is_writing(client);
1154 ae7c1b78 2023-01-10 stsp err = forward_ref_update(client, &imsg);
1157 ae7c1b78 2023-01-10 stsp client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1158 3448a19a 2023-01-21 stsp client->accept_flush_pkt = 1;
1160 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_HAVE:
1161 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_HAVE) {
1162 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1163 ae7c1b78 2023-01-10 stsp "unexpected have-line received");
1166 ae7c1b78 2023-01-10 stsp log_debug("received have-line from uid %d",
1167 ae7c1b78 2023-01-10 stsp client->euid);
1168 ae7c1b78 2023-01-10 stsp err = ensure_client_is_reading(client);
1171 ae7c1b78 2023-01-10 stsp err = forward_have(client, &imsg);
1174 3448a19a 2023-01-21 stsp client->accept_flush_pkt = 1;
1176 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_FLUSH:
1177 ae7c1b78 2023-01-10 stsp if (client->state == GOTD_STATE_EXPECT_WANT ||
1178 ae7c1b78 2023-01-10 stsp client->state == GOTD_STATE_EXPECT_HAVE) {
1179 ae7c1b78 2023-01-10 stsp err = ensure_client_is_reading(client);
1182 ae7c1b78 2023-01-10 stsp } else if (client->state ==
1183 ae7c1b78 2023-01-10 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1184 ae7c1b78 2023-01-10 stsp err = ensure_client_is_writing(client);
1187 f9550d47 2023-01-18 stsp } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1188 3448a19a 2023-01-21 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1189 3448a19a 2023-01-21 stsp "unexpected flush-pkt received");
1192 3448a19a 2023-01-21 stsp if (!client->accept_flush_pkt) {
1193 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1194 ae7c1b78 2023-01-10 stsp "unexpected flush-pkt received");
1199 3448a19a 2023-01-21 stsp * Accept just one flush packet at a time.
1200 3448a19a 2023-01-21 stsp * Future client state transitions will set this flag
1201 3448a19a 2023-01-21 stsp * again if another flush packet is expected.
1203 3448a19a 2023-01-21 stsp client->accept_flush_pkt = 0;
1205 ae7c1b78 2023-01-10 stsp log_debug("received flush-pkt from uid %d",
1206 ae7c1b78 2023-01-10 stsp client->euid);
1207 ae7c1b78 2023-01-10 stsp if (client->state == GOTD_STATE_EXPECT_WANT) {
1208 ae7c1b78 2023-01-10 stsp client->state = GOTD_STATE_EXPECT_HAVE;
1209 ae7c1b78 2023-01-10 stsp log_debug("uid %d: expecting have-lines",
1210 ae7c1b78 2023-01-10 stsp client->euid);
1211 ae7c1b78 2023-01-10 stsp } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1212 ae7c1b78 2023-01-10 stsp client->state = GOTD_STATE_EXPECT_DONE;
1213 3448a19a 2023-01-21 stsp client->accept_flush_pkt = 1;
1214 ae7c1b78 2023-01-10 stsp log_debug("uid %d: expecting 'done'",
1215 ae7c1b78 2023-01-10 stsp client->euid);
1216 ae7c1b78 2023-01-10 stsp } else if (client->state ==
1217 ae7c1b78 2023-01-10 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1218 ae7c1b78 2023-01-10 stsp client->state = GOTD_STATE_EXPECT_PACKFILE;
1219 ae7c1b78 2023-01-10 stsp log_debug("uid %d: expecting packfile",
1220 ae7c1b78 2023-01-10 stsp client->euid);
1221 ae7c1b78 2023-01-10 stsp err = recv_packfile(client);
1222 f9550d47 2023-01-18 stsp } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1223 ae7c1b78 2023-01-10 stsp /* should not happen, see above */
1224 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1225 ae7c1b78 2023-01-10 stsp "unexpected client state");
1229 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_DONE:
1230 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_HAVE &&
1231 ae7c1b78 2023-01-10 stsp client->state != GOTD_STATE_EXPECT_DONE) {
1232 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1233 ae7c1b78 2023-01-10 stsp "unexpected flush-pkt received");
1236 ae7c1b78 2023-01-10 stsp log_debug("received 'done' from uid %d", client->euid);
1237 ae7c1b78 2023-01-10 stsp err = ensure_client_is_reading(client);
1240 ae7c1b78 2023-01-10 stsp client->state = GOTD_STATE_DONE;
1241 3448a19a 2023-01-21 stsp client->accept_flush_pkt = 1;
1242 ae7c1b78 2023-01-10 stsp err = send_packfile(client);
1245 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1246 ae7c1b78 2023-01-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1250 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1253 ae7c1b78 2023-01-10 stsp if (err) {
1254 ae7c1b78 2023-01-10 stsp if (err->code != GOT_ERR_EOF ||
1255 ae7c1b78 2023-01-10 stsp client->state != GOTD_STATE_EXPECT_PACKFILE)
1256 ae7c1b78 2023-01-10 stsp disconnect_on_error(client, err);
1258 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
1259 ae7c1b78 2023-01-10 stsp evtimer_add(&client->tmo, &gotd_session.request_timeout);
1263 ae7c1b78 2023-01-10 stsp static const struct got_error *
1264 ae7c1b78 2023-01-10 stsp list_refs_request(void)
1266 ae7c1b78 2023-01-10 stsp static const struct got_error *err;
1267 ae7c1b78 2023-01-10 stsp struct gotd_session_client *client = &gotd_session_client;
1268 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = &client->repo_child_iev;
1269 ae7c1b78 2023-01-10 stsp struct gotd_imsg_list_refs_internal ilref;
1272 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1273 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1275 ae7c1b78 2023-01-10 stsp memset(&ilref, 0, sizeof(ilref));
1276 ae7c1b78 2023-01-10 stsp ilref.client_id = client->id;
1278 ae7c1b78 2023-01-10 stsp fd = dup(client->fd);
1279 ae7c1b78 2023-01-10 stsp if (fd == -1)
1280 ae7c1b78 2023-01-10 stsp return got_error_from_errno("dup");
1282 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1283 b0614828 2023-06-19 stsp gotd_session.proc_id, fd, &ilref, sizeof(ilref)) == -1) {
1284 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1285 ae7c1b78 2023-01-10 stsp close(fd);
1286 ae7c1b78 2023-01-10 stsp return err;
1289 ae7c1b78 2023-01-10 stsp client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1290 ae7c1b78 2023-01-10 stsp log_debug("uid %d: expecting capabilities", client->euid);
1291 ae7c1b78 2023-01-10 stsp return NULL;
1294 ae7c1b78 2023-01-10 stsp static const struct got_error *
1295 ae7c1b78 2023-01-10 stsp recv_connect(struct imsg *imsg)
1297 ae7c1b78 2023-01-10 stsp struct gotd_session_client *client = &gotd_session_client;
1298 ae7c1b78 2023-01-10 stsp struct gotd_imsg_connect iconnect;
1299 ae7c1b78 2023-01-10 stsp size_t datalen;
1301 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1302 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1304 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1305 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(iconnect))
1306 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1307 ae7c1b78 2023-01-10 stsp memcpy(&iconnect, imsg->data, sizeof(iconnect));
1309 ae7c1b78 2023-01-10 stsp if (imsg->fd == -1)
1310 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1312 ae7c1b78 2023-01-10 stsp client->fd = imsg->fd;
1313 ae7c1b78 2023-01-10 stsp client->euid = iconnect.euid;
1314 ae7c1b78 2023-01-10 stsp client->egid = iconnect.egid;
1316 ae7c1b78 2023-01-10 stsp imsg_init(&client->iev.ibuf, client->fd);
1317 3931a8a4 2023-02-03 stsp client->iev.handler = session_dispatch_client;
1318 ae7c1b78 2023-01-10 stsp client->iev.events = EV_READ;
1319 ae7c1b78 2023-01-10 stsp client->iev.handler_arg = NULL;
1320 ae7c1b78 2023-01-10 stsp event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1321 3931a8a4 2023-02-03 stsp session_dispatch_client, &client->iev);
1322 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(&client->iev);
1323 ae7c1b78 2023-01-10 stsp evtimer_set(&client->tmo, gotd_request_timeout, client);
1325 ae7c1b78 2023-01-10 stsp return NULL;
1328 ae7c1b78 2023-01-10 stsp static const struct got_error *
1329 ae7c1b78 2023-01-10 stsp recv_repo_child(struct imsg *imsg)
1331 ae7c1b78 2023-01-10 stsp struct gotd_imsg_connect_repo_child ichild;
1332 ae7c1b78 2023-01-10 stsp struct gotd_session_client *client = &gotd_session_client;
1333 ae7c1b78 2023-01-10 stsp size_t datalen;
1335 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1336 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1338 ae7c1b78 2023-01-10 stsp /* We should already have received a pipe to the listener. */
1339 ae7c1b78 2023-01-10 stsp if (client->fd == -1)
1340 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1342 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1343 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(ichild))
1344 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1346 ae7c1b78 2023-01-10 stsp memcpy(&ichild, imsg->data, sizeof(ichild));
1348 ae7c1b78 2023-01-10 stsp client->id = ichild.client_id;
1349 ae7c1b78 2023-01-10 stsp if (ichild.proc_id == PROC_REPO_WRITE)
1350 ae7c1b78 2023-01-10 stsp client->is_writing = 1;
1351 ae7c1b78 2023-01-10 stsp else if (ichild.proc_id == PROC_REPO_READ)
1352 ae7c1b78 2023-01-10 stsp client->is_writing = 0;
1354 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1355 ae7c1b78 2023-01-10 stsp "bad child process type");
1357 ae7c1b78 2023-01-10 stsp if (imsg->fd == -1)
1358 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1360 ae7c1b78 2023-01-10 stsp imsg_init(&client->repo_child_iev.ibuf, imsg->fd);
1361 ae7c1b78 2023-01-10 stsp client->repo_child_iev.handler = session_dispatch_repo_child;
1362 ae7c1b78 2023-01-10 stsp client->repo_child_iev.events = EV_READ;
1363 ae7c1b78 2023-01-10 stsp client->repo_child_iev.handler_arg = NULL;
1364 ae7c1b78 2023-01-10 stsp event_set(&client->repo_child_iev.ev, client->repo_child_iev.ibuf.fd,
1365 ae7c1b78 2023-01-10 stsp EV_READ, session_dispatch_repo_child, &client->repo_child_iev);
1366 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(&client->repo_child_iev);
1368 ae7c1b78 2023-01-10 stsp /* The "recvfd" pledge promise is no longer needed. */
1369 ae7c1b78 2023-01-10 stsp if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1370 ae7c1b78 2023-01-10 stsp fatal("pledge");
1372 ae7c1b78 2023-01-10 stsp return NULL;
1375 ae7c1b78 2023-01-10 stsp static void
1376 ae7c1b78 2023-01-10 stsp session_dispatch(int fd, short event, void *arg)
1378 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
1379 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
1380 ae7c1b78 2023-01-10 stsp struct gotd_session_client *client = &gotd_session_client;
1381 ae7c1b78 2023-01-10 stsp ssize_t n;
1382 ae7c1b78 2023-01-10 stsp int shut = 0;
1383 ae7c1b78 2023-01-10 stsp struct imsg imsg;
1385 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
1386 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1387 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
1388 ae7c1b78 2023-01-10 stsp if (n == 0) {
1389 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1391 ae7c1b78 2023-01-10 stsp goto done;
1395 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
1396 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
1397 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
1398 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
1399 ae7c1b78 2023-01-10 stsp if (n == 0) {
1400 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1402 ae7c1b78 2023-01-10 stsp goto done;
1406 ae7c1b78 2023-01-10 stsp for (;;) {
1407 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1408 ae7c1b78 2023-01-10 stsp uint32_t client_id = 0;
1409 ae7c1b78 2023-01-10 stsp int do_disconnect = 0, do_list_refs = 0;
1411 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1412 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get error", __func__);
1413 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
1416 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
1417 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_ERROR:
1418 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1419 ae7c1b78 2023-01-10 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1421 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CONNECT:
1422 ae7c1b78 2023-01-10 stsp err = recv_connect(&imsg);
1424 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_DISCONNECT:
1425 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1427 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CONNECT_REPO_CHILD:
1428 ae7c1b78 2023-01-10 stsp err = recv_repo_child(&imsg);
1431 ae7c1b78 2023-01-10 stsp do_list_refs = 1;
1434 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1437 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1439 ae7c1b78 2023-01-10 stsp if (do_disconnect) {
1441 ae7c1b78 2023-01-10 stsp disconnect_on_error(client, err);
1443 ae7c1b78 2023-01-10 stsp disconnect(client);
1444 ae7c1b78 2023-01-10 stsp } else if (do_list_refs)
1445 ae7c1b78 2023-01-10 stsp err = list_refs_request();
1448 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1451 ae7c1b78 2023-01-10 stsp if (!shut) {
1452 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
1454 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
1455 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
1456 ae7c1b78 2023-01-10 stsp event_loopexit(NULL);
1461 ae7c1b78 2023-01-10 stsp session_main(const char *title, const char *repo_path,
1462 b0614828 2023-06-19 stsp int *pack_fds, int *temp_fds, struct timeval *request_timeout,
1463 b0614828 2023-06-19 stsp enum gotd_procid proc_id)
1465 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1466 ae7c1b78 2023-01-10 stsp struct event evsigint, evsigterm, evsighup, evsigusr1;
1468 ae7c1b78 2023-01-10 stsp gotd_session.title = title;
1469 ae7c1b78 2023-01-10 stsp gotd_session.pid = getpid();
1470 ae7c1b78 2023-01-10 stsp gotd_session.pack_fds = pack_fds;
1471 ae7c1b78 2023-01-10 stsp gotd_session.temp_fds = temp_fds;
1472 ae7c1b78 2023-01-10 stsp memcpy(&gotd_session.request_timeout, request_timeout,
1473 ae7c1b78 2023-01-10 stsp sizeof(gotd_session.request_timeout));
1474 b0614828 2023-06-19 stsp gotd_session.proc_id = proc_id;
1476 ae7c1b78 2023-01-10 stsp err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1478 ae7c1b78 2023-01-10 stsp goto done;
1479 ae7c1b78 2023-01-10 stsp if (!got_repo_is_bare(gotd_session.repo)) {
1480 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1481 ae7c1b78 2023-01-10 stsp "bare git repository required");
1482 ae7c1b78 2023-01-10 stsp goto done;
1485 ae7c1b78 2023-01-10 stsp got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1487 ae7c1b78 2023-01-10 stsp signal_set(&evsigint, SIGINT, gotd_session_sighdlr, NULL);
1488 ae7c1b78 2023-01-10 stsp signal_set(&evsigterm, SIGTERM, gotd_session_sighdlr, NULL);
1489 ae7c1b78 2023-01-10 stsp signal_set(&evsighup, SIGHUP, gotd_session_sighdlr, NULL);
1490 ae7c1b78 2023-01-10 stsp signal_set(&evsigusr1, SIGUSR1, gotd_session_sighdlr, NULL);
1491 ae7c1b78 2023-01-10 stsp signal(SIGPIPE, SIG_IGN);
1493 ae7c1b78 2023-01-10 stsp signal_add(&evsigint, NULL);
1494 ae7c1b78 2023-01-10 stsp signal_add(&evsigterm, NULL);
1495 ae7c1b78 2023-01-10 stsp signal_add(&evsighup, NULL);
1496 ae7c1b78 2023-01-10 stsp signal_add(&evsigusr1, NULL);
1498 ae7c1b78 2023-01-10 stsp gotd_session_client.state = GOTD_STATE_EXPECT_LIST_REFS;
1499 ae7c1b78 2023-01-10 stsp gotd_session_client.fd = -1;
1500 ae7c1b78 2023-01-10 stsp gotd_session_client.nref_updates = -1;
1501 2fc51af0 2023-01-10 stsp gotd_session_client.delta_cache_fd = -1;
1502 3448a19a 2023-01-21 stsp gotd_session_client.accept_flush_pkt = 1;
1504 ae7c1b78 2023-01-10 stsp imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1505 ae7c1b78 2023-01-10 stsp gotd_session.parent_iev.handler = session_dispatch;
1506 ae7c1b78 2023-01-10 stsp gotd_session.parent_iev.events = EV_READ;
1507 ae7c1b78 2023-01-10 stsp gotd_session.parent_iev.handler_arg = NULL;
1508 ae7c1b78 2023-01-10 stsp event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1509 ae7c1b78 2023-01-10 stsp EV_READ, session_dispatch, &gotd_session.parent_iev);
1510 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1511 b0614828 2023-06-19 stsp GOTD_IMSG_CLIENT_SESSION_READY, gotd_session.proc_id,
1512 b0614828 2023-06-19 stsp -1, NULL, 0) == -1) {
1513 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1514 ae7c1b78 2023-01-10 stsp goto done;
1517 ae7c1b78 2023-01-10 stsp event_dispatch();
1520 ae7c1b78 2023-01-10 stsp log_warnx("%s: %s", title, err->msg);
1521 ae7c1b78 2023-01-10 stsp gotd_session_shutdown();
1525 ae7c1b78 2023-01-10 stsp gotd_session_shutdown(void)
1527 2ec74a9e 2023-02-08 op log_debug("shutting down");
1528 ae7c1b78 2023-01-10 stsp if (gotd_session.repo)
1529 ae7c1b78 2023-01-10 stsp got_repo_close(gotd_session.repo);
1530 ae7c1b78 2023-01-10 stsp got_repo_pack_fds_close(gotd_session.pack_fds);
1531 ae7c1b78 2023-01-10 stsp got_repo_temp_fds_close(gotd_session.temp_fds);