Blob


1 /*
2 * Copyright (c) 2022, 2023 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/uio.h>
24 #include <errno.h>
25 #include <event.h>
26 #include <limits.h>
27 #include <sha1.h>
28 #include <sha2.h>
29 #include <signal.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <imsg.h>
35 #include <unistd.h>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_reference.h"
42 #include "got_opentemp.h"
44 #include "got_lib_hash.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_pack.h"
49 #include "got_lib_repository.h"
50 #include "got_lib_gitproto.h"
52 #include "gotd.h"
53 #include "log.h"
54 #include "session_write.h"
56 struct gotd_session_notif {
57 STAILQ_ENTRY(gotd_session_notif) entry;
58 int fd;
59 enum gotd_notification_action action;
60 char *refname;
61 struct got_object_id old_id;
62 struct got_object_id new_id;
63 };
64 STAILQ_HEAD(gotd_session_notifications, gotd_session_notif) notifications;
66 enum gotd_session_write_state {
67 GOTD_STATE_EXPECT_LIST_REFS,
68 GOTD_STATE_EXPECT_CAPABILITIES,
69 GOTD_STATE_EXPECT_REF_UPDATE,
70 GOTD_STATE_EXPECT_MORE_REF_UPDATES,
71 GOTD_STATE_EXPECT_PACKFILE,
72 GOTD_STATE_NOTIFY,
73 };
75 static struct gotd_session_write {
76 pid_t pid;
77 const char *title;
78 struct got_repository *repo;
79 struct gotd_repo *repo_cfg;
80 int *pack_fds;
81 int *temp_fds;
82 struct gotd_imsgev parent_iev;
83 struct gotd_imsgev notifier_iev;
84 struct timeval request_timeout;
85 enum gotd_session_write_state state;
86 struct gotd_imsgev repo_child_iev;
87 } gotd_session;
89 static struct gotd_session_client {
90 struct gotd_client_capability *capabilities;
91 size_t ncapa_alloc;
92 size_t ncapabilities;
93 uint32_t id;
94 int fd;
95 int delta_cache_fd;
96 struct gotd_imsgev iev;
97 struct event tmo;
98 uid_t euid;
99 gid_t egid;
100 char *username;
101 char *packfile_path;
102 char *packidx_path;
103 int nref_updates;
104 int accept_flush_pkt;
105 int flush_disconnect;
106 } gotd_session_client;
108 static void session_write_shutdown(void);
110 static void
111 disconnect(struct gotd_session_client *client)
113 log_debug("uid %d: disconnecting", client->euid);
115 if (gotd_imsg_compose_event(&gotd_session.parent_iev,
116 GOTD_IMSG_DISCONNECT, PROC_SESSION_WRITE, -1, NULL, 0) == -1)
117 log_warn("imsg compose DISCONNECT");
119 imsg_clear(&gotd_session.repo_child_iev.ibuf);
120 event_del(&gotd_session.repo_child_iev.ev);
121 evtimer_del(&client->tmo);
122 close(client->fd);
123 if (client->delta_cache_fd != -1)
124 close(client->delta_cache_fd);
125 if (client->packfile_path) {
126 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
127 log_warn("unlink %s: ", client->packfile_path);
128 free(client->packfile_path);
130 if (client->packidx_path) {
131 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
132 log_warn("unlink %s: ", client->packidx_path);
133 free(client->packidx_path);
135 free(client->capabilities);
137 session_write_shutdown();
140 static void
141 disconnect_on_error(struct gotd_session_client *client,
142 const struct got_error *err)
144 struct imsgbuf ibuf;
146 if (err->code != GOT_ERR_EOF) {
147 log_warnx("uid %d: %s", client->euid, err->msg);
148 imsg_init(&ibuf, client->fd);
149 gotd_imsg_send_error(&ibuf, 0, PROC_SESSION_WRITE, err);
150 imsg_clear(&ibuf);
153 disconnect(client);
156 static void
157 gotd_request_timeout(int fd, short events, void *arg)
159 struct gotd_session_client *client = arg;
161 log_debug("disconnecting uid %d due to timeout", client->euid);
162 disconnect(client);
165 static void
166 session_write_sighdlr(int sig, short event, void *arg)
168 /*
169 * Normal signal handler rules don't apply because libevent
170 * decouples for us.
171 */
173 switch (sig) {
174 case SIGHUP:
175 log_info("%s: ignoring SIGHUP", __func__);
176 break;
177 case SIGUSR1:
178 log_info("%s: ignoring SIGUSR1", __func__);
179 break;
180 case SIGTERM:
181 case SIGINT:
182 session_write_shutdown();
183 /* NOTREACHED */
184 break;
185 default:
186 fatalx("unexpected signal");
190 static const struct got_error *
191 recv_packfile_install(struct imsg *imsg)
193 struct gotd_imsg_packfile_install inst;
194 size_t datalen;
196 log_debug("packfile-install received");
198 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
199 if (datalen != sizeof(inst))
200 return got_error(GOT_ERR_PRIVSEP_LEN);
201 memcpy(&inst, imsg->data, sizeof(inst));
203 return NULL;
206 static const struct got_error *
207 recv_ref_updates_start(struct imsg *imsg)
209 struct gotd_imsg_ref_updates_start istart;
210 size_t datalen;
212 log_debug("ref-updates-start received");
214 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
215 if (datalen != sizeof(istart))
216 return got_error(GOT_ERR_PRIVSEP_LEN);
217 memcpy(&istart, imsg->data, sizeof(istart));
219 return NULL;
222 static const struct got_error *
223 recv_ref_update(struct imsg *imsg)
225 struct gotd_imsg_ref_update iref;
226 size_t datalen;
228 log_debug("ref-update received");
230 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
231 if (datalen < sizeof(iref))
232 return got_error(GOT_ERR_PRIVSEP_LEN);
233 memcpy(&iref, imsg->data, sizeof(iref));
235 return NULL;
238 static const struct got_error *
239 send_ref_update_ok(struct gotd_session_client *client,
240 struct gotd_imsg_ref_update *iref, const char *refname)
242 struct gotd_imsg_ref_update_ok iok;
243 struct gotd_imsgev *iev = &client->iev;
244 struct ibuf *wbuf;
245 size_t len;
247 memset(&iok, 0, sizeof(iok));
248 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
249 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
250 iok.name_len = strlen(refname);
252 len = sizeof(iok) + iok.name_len;
253 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_OK,
254 PROC_SESSION_WRITE, gotd_session.pid, len);
255 if (wbuf == NULL)
256 return got_error_from_errno("imsg_create REF_UPDATE_OK");
258 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
259 return got_error_from_errno("imsg_add REF_UPDATE_OK");
260 if (imsg_add(wbuf, refname, iok.name_len) == -1)
261 return got_error_from_errno("imsg_add REF_UPDATE_OK");
263 imsg_close(&iev->ibuf, wbuf);
264 gotd_imsg_event_add(iev);
265 return NULL;
268 static void
269 send_refs_updated(struct gotd_session_client *client)
271 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
272 PROC_SESSION_WRITE, -1, NULL, 0) == -1)
273 log_warn("imsg compose REFS_UPDATED");
276 static const struct got_error *
277 send_ref_update_ng(struct gotd_session_client *client,
278 struct gotd_imsg_ref_update *iref, const char *refname,
279 const char *reason)
281 const struct got_error *ng_err;
282 struct gotd_imsg_ref_update_ng ing;
283 struct gotd_imsgev *iev = &client->iev;
284 struct ibuf *wbuf;
285 size_t len;
287 memset(&ing, 0, sizeof(ing));
288 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
289 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
290 ing.name_len = strlen(refname);
292 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
293 ing.reason_len = strlen(ng_err->msg);
295 len = sizeof(ing) + ing.name_len + ing.reason_len;
296 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_NG,
297 PROC_SESSION_WRITE, gotd_session.pid, len);
298 if (wbuf == NULL)
299 return got_error_from_errno("imsg_create REF_UPDATE_NG");
301 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
302 return got_error_from_errno("imsg_add REF_UPDATE_NG");
303 if (imsg_add(wbuf, refname, ing.name_len) == -1)
304 return got_error_from_errno("imsg_add REF_UPDATE_NG");
305 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
306 return got_error_from_errno("imsg_add REF_UPDATE_NG");
308 imsg_close(&iev->ibuf, wbuf);
309 gotd_imsg_event_add(iev);
310 return NULL;
313 static const struct got_error *
314 install_pack(struct gotd_session_client *client, const char *repo_path,
315 struct imsg *imsg)
317 const struct got_error *err = NULL;
318 struct gotd_imsg_packfile_install inst;
319 char hex[SHA1_DIGEST_STRING_LENGTH];
320 size_t datalen;
321 char *packfile_path = NULL, *packidx_path = NULL;
323 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
324 if (datalen != sizeof(inst))
325 return got_error(GOT_ERR_PRIVSEP_LEN);
326 memcpy(&inst, imsg->data, sizeof(inst));
328 if (client->packfile_path == NULL)
329 return got_error_msg(GOT_ERR_BAD_REQUEST,
330 "client has no pack file");
331 if (client->packidx_path == NULL)
332 return got_error_msg(GOT_ERR_BAD_REQUEST,
333 "client has no pack file index");
335 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
336 return got_error_msg(GOT_ERR_NO_SPACE,
337 "could not convert pack file SHA1 to hex");
339 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
340 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
341 err = got_error_from_errno("asprintf");
342 goto done;
345 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
346 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
347 err = got_error_from_errno("asprintf");
348 goto done;
351 if (rename(client->packfile_path, packfile_path) == -1) {
352 err = got_error_from_errno3("rename", client->packfile_path,
353 packfile_path);
354 goto done;
357 free(client->packfile_path);
358 client->packfile_path = NULL;
360 if (rename(client->packidx_path, packidx_path) == -1) {
361 err = got_error_from_errno3("rename", client->packidx_path,
362 packidx_path);
363 goto done;
366 /* Ensure we re-read the pack index list upon next access. */
367 gotd_session.repo->pack_path_mtime.tv_sec = 0;
368 gotd_session.repo->pack_path_mtime.tv_nsec = 0;
370 free(client->packidx_path);
371 client->packidx_path = NULL;
372 done:
373 free(packfile_path);
374 free(packidx_path);
375 return err;
378 static const struct got_error *
379 begin_ref_updates(struct gotd_session_client *client, struct imsg *imsg)
381 struct gotd_imsg_ref_updates_start istart;
382 size_t datalen;
384 if (client->nref_updates != -1)
385 return got_error(GOT_ERR_PRIVSEP_MSG);
387 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
388 if (datalen != sizeof(istart))
389 return got_error(GOT_ERR_PRIVSEP_LEN);
390 memcpy(&istart, imsg->data, sizeof(istart));
392 if (istart.nref_updates <= 0)
393 return got_error(GOT_ERR_PRIVSEP_MSG);
395 client->nref_updates = istart.nref_updates;
396 return NULL;
399 static const struct got_error *
400 validate_namespace(const char *namespace)
402 size_t len = strlen(namespace);
404 if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
405 namespace[len - 1] != '/') {
406 return got_error_fmt(GOT_ERR_BAD_REF_NAME,
407 "reference namespace '%s'", namespace);
410 return NULL;
413 static const struct got_error *
414 queue_notification(struct got_object_id *old_id, struct got_object_id *new_id,
415 struct got_repository *repo, struct got_reference *ref)
417 const struct got_error *err = NULL;
418 struct gotd_repo *repo_cfg = gotd_session.repo_cfg;
419 struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
420 struct got_pathlist_entry *pe;
421 struct gotd_session_notif *notif;
423 if (iev->ibuf.fd == -1 ||
424 STAILQ_EMPTY(&repo_cfg->notification_targets))
425 return NULL; /* notifications unused */
427 TAILQ_FOREACH(pe, &repo_cfg->notification_refs, entry) {
428 const char *refname = pe->path;
429 if (strcmp(got_ref_get_name(ref), refname) == 0)
430 break;
432 if (pe == NULL) {
433 TAILQ_FOREACH(pe, &repo_cfg->notification_ref_namespaces,
434 entry) {
435 const char *namespace = pe->path;
437 err = validate_namespace(namespace);
438 if (err)
439 return err;
440 if (strncmp(namespace, got_ref_get_name(ref),
441 strlen(namespace)) == 0)
442 break;
446 /*
447 * If a branch or a reference namespace was specified in the
448 * configuration file then only send notifications if a match
449 * was found.
450 */
451 if (pe == NULL && (!TAILQ_EMPTY(&repo_cfg->notification_refs) ||
452 !TAILQ_EMPTY(&repo_cfg->notification_ref_namespaces)))
453 return NULL;
455 notif = calloc(1, sizeof(*notif));
456 if (notif == NULL)
457 return got_error_from_errno("calloc");
459 notif->fd = -1;
461 if (old_id == NULL)
462 notif->action = GOTD_NOTIF_ACTION_CREATED;
463 else if (new_id == NULL)
464 notif->action = GOTD_NOTIF_ACTION_REMOVED;
465 else
466 notif->action = GOTD_NOTIF_ACTION_CHANGED;
468 if (old_id != NULL)
469 memcpy(&notif->old_id, old_id, sizeof(notif->old_id));
470 if (new_id != NULL)
471 memcpy(&notif->new_id, new_id, sizeof(notif->new_id));
473 notif->refname = strdup(got_ref_get_name(ref));
474 if (notif->refname == NULL) {
475 err = got_error_from_errno("strdup");
476 goto done;
479 STAILQ_INSERT_TAIL(&notifications, notif, entry);
480 done:
481 if (err && notif) {
482 free(notif->refname);
483 free(notif);
485 return err;
488 /* Forward notification content to the NOTIFY process. */
489 static const struct got_error *
490 forward_notification(struct gotd_session_client *client, struct imsg *imsg)
492 const struct got_error *err = NULL;
493 struct gotd_imsgev *iev = &gotd_session.notifier_iev;
494 struct gotd_session_notif *notif;
495 struct gotd_imsg_notification_content icontent;
496 char *refname = NULL;
497 size_t datalen;
498 struct gotd_imsg_notify inotify;
499 const char *action;
501 memset(&inotify, 0, sizeof(inotify));
503 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
504 if (datalen < sizeof(icontent))
505 return got_error(GOT_ERR_PRIVSEP_LEN);
506 memcpy(&icontent, imsg->data, sizeof(icontent));
507 if (datalen != sizeof(icontent) + icontent.refname_len)
508 return got_error(GOT_ERR_PRIVSEP_LEN);
509 refname = strndup(imsg->data + sizeof(icontent), icontent.refname_len);
510 if (refname == NULL)
511 return got_error_from_errno("strndup");
513 notif = STAILQ_FIRST(&notifications);
514 if (notif == NULL)
515 return got_error(GOT_ERR_PRIVSEP_MSG);
517 STAILQ_REMOVE(&notifications, notif, gotd_session_notif, entry);
519 if (notif->action != icontent.action || notif->fd == -1 ||
520 strcmp(notif->refname, refname) != 0) {
521 err = got_error(GOT_ERR_PRIVSEP_MSG);
522 goto done;
524 if (notif->action == GOTD_NOTIF_ACTION_CREATED) {
525 if (memcmp(notif->new_id.sha1, icontent.new_id,
526 SHA1_DIGEST_LENGTH) != 0) {
527 err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
528 "received notification content for unknown event");
529 goto done;
531 } else if (notif->action == GOTD_NOTIF_ACTION_REMOVED) {
532 if (memcmp(notif->old_id.sha1, icontent.old_id,
533 SHA1_DIGEST_LENGTH) != 0) {
534 err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
535 "received notification content for unknown event");
536 goto done;
538 } else if (memcmp(notif->old_id.sha1, icontent.old_id,
539 SHA1_DIGEST_LENGTH) != 0 ||
540 memcmp(notif->new_id.sha1, icontent.new_id,
541 SHA1_DIGEST_LENGTH) != 0) {
542 err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
543 "received notification content for unknown event");
544 goto done;
547 switch (notif->action) {
548 case GOTD_NOTIF_ACTION_CREATED:
549 action = "created";
550 break;
551 case GOTD_NOTIF_ACTION_REMOVED:
552 action = "removed";
553 break;
554 case GOTD_NOTIF_ACTION_CHANGED:
555 action = "changed";
556 break;
557 default:
558 err = got_error(GOT_ERR_PRIVSEP_MSG);
559 goto done;
562 strlcpy(inotify.repo_name, gotd_session.repo_cfg->name,
563 sizeof(inotify.repo_name));
565 snprintf(inotify.subject_line, sizeof(inotify.subject_line),
566 "%s: %s %s %s", gotd_session.repo_cfg->name,
567 client->username, action, notif->refname);
569 if (gotd_imsg_compose_event(iev, GOTD_IMSG_NOTIFY,
570 PROC_SESSION_WRITE, notif->fd, &inotify, sizeof(inotify))
571 == -1) {
572 err = got_error_from_errno("imsg compose NOTIFY");
573 goto done;
575 notif->fd = -1;
576 done:
577 if (notif->fd != -1)
578 close(notif->fd);
579 free(notif);
580 free(refname);
581 return err;
584 /* Request notification content from REPO_WRITE process. */
585 static const struct got_error *
586 request_notification(struct gotd_session_notif *notif)
588 const struct got_error *err = NULL;
589 struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
590 struct gotd_imsg_notification_content icontent;
591 struct ibuf *wbuf;
592 size_t len;
593 int fd;
595 fd = got_opentempfd();
596 if (fd == -1)
597 return got_error_from_errno("got_opentemp");
599 memset(&icontent, 0, sizeof(icontent));
601 icontent.action = notif->action;
602 memcpy(&icontent.old_id, &notif->old_id, sizeof(notif->old_id));
603 memcpy(&icontent.new_id, &notif->new_id, sizeof(notif->new_id));
604 icontent.refname_len = strlen(notif->refname);
606 len = sizeof(icontent) + icontent.refname_len;
607 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_NOTIFY,
608 PROC_SESSION_WRITE, gotd_session.pid, len);
609 if (wbuf == NULL) {
610 err = got_error_from_errno("imsg_create NOTIFY");
611 goto done;
613 if (imsg_add(wbuf, &icontent, sizeof(icontent)) == -1) {
614 err = got_error_from_errno("imsg_add NOTIFY");
615 goto done;
617 if (imsg_add(wbuf, notif->refname, icontent.refname_len) == -1) {
618 err = got_error_from_errno("imsg_add NOTIFY");
619 goto done;
622 notif->fd = dup(fd);
623 if (notif->fd == -1) {
624 err = got_error_from_errno("dup");
625 goto done;
628 ibuf_fd_set(wbuf, fd);
629 fd = -1;
631 imsg_close(&iev->ibuf, wbuf);
632 gotd_imsg_event_add(iev);
633 done:
634 if (err && fd != -1)
635 close(fd);
636 return err;
639 static const struct got_error *
640 update_ref(int *shut, struct gotd_session_client *client,
641 const char *repo_path, struct imsg *imsg)
643 const struct got_error *err = NULL;
644 struct got_repository *repo = gotd_session.repo;
645 struct got_reference *ref = NULL;
646 struct gotd_imsg_ref_update iref;
647 struct got_object_id old_id, new_id;
648 struct gotd_session_notif *notif;
649 struct got_object_id *id = NULL;
650 char *refname = NULL;
651 size_t datalen;
652 int locked = 0;
653 char hex1[SHA1_DIGEST_STRING_LENGTH];
654 char hex2[SHA1_DIGEST_STRING_LENGTH];
656 log_debug("update-ref from uid %d", client->euid);
658 if (client->nref_updates <= 0)
659 return got_error(GOT_ERR_PRIVSEP_MSG);
661 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
662 if (datalen < sizeof(iref))
663 return got_error(GOT_ERR_PRIVSEP_LEN);
664 memcpy(&iref, imsg->data, sizeof(iref));
665 if (datalen != sizeof(iref) + iref.name_len)
666 return got_error(GOT_ERR_PRIVSEP_LEN);
667 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
668 if (refname == NULL)
669 return got_error_from_errno("strndup");
671 log_debug("updating ref %s for uid %d", refname, client->euid);
673 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
674 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
675 err = got_repo_find_object_id(iref.delete_ref ? &old_id : &new_id,
676 repo);
677 if (err)
678 goto done;
680 if (iref.ref_is_new) {
681 err = got_ref_open(&ref, repo, refname, 0);
682 if (err) {
683 if (err->code != GOT_ERR_NOT_REF)
684 goto done;
685 err = got_ref_alloc(&ref, refname, &new_id);
686 if (err)
687 goto done;
688 err = got_ref_write(ref, repo); /* will lock/unlock */
689 if (err)
690 goto done;
691 err = queue_notification(NULL, &new_id, repo, ref);
692 if (err)
693 goto done;
694 } else {
695 err = got_ref_resolve(&id, repo, ref);
696 if (err)
697 goto done;
698 got_object_id_hex(&new_id, hex1, sizeof(hex1));
699 got_object_id_hex(id, hex2, sizeof(hex2));
700 err = got_error_fmt(GOT_ERR_REF_BUSY,
701 "Addition %s: %s failed; %s: %s has been "
702 "created by someone else while transaction "
703 "was in progress",
704 got_ref_get_name(ref), hex1,
705 got_ref_get_name(ref), hex2);
706 goto done;
708 } else if (iref.delete_ref) {
709 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
710 if (err)
711 goto done;
712 locked = 1;
714 err = got_ref_resolve(&id, repo, ref);
715 if (err)
716 goto done;
718 if (got_object_id_cmp(id, &old_id) != 0) {
719 got_object_id_hex(&old_id, hex1, sizeof(hex1));
720 got_object_id_hex(id, hex2, sizeof(hex2));
721 err = got_error_fmt(GOT_ERR_REF_BUSY,
722 "Deletion %s: %s failed; %s: %s has been "
723 "created by someone else while transaction "
724 "was in progress",
725 got_ref_get_name(ref), hex1,
726 got_ref_get_name(ref), hex2);
727 goto done;
730 err = got_ref_delete(ref, repo);
731 if (err)
732 goto done;
733 err = queue_notification(&old_id, NULL, repo, ref);
734 if (err)
735 goto done;
736 free(id);
737 id = NULL;
738 } else {
739 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
740 if (err)
741 goto done;
742 locked = 1;
744 err = got_ref_resolve(&id, repo, ref);
745 if (err)
746 goto done;
748 if (got_object_id_cmp(id, &old_id) != 0) {
749 got_object_id_hex(&old_id, hex1, sizeof(hex1));
750 got_object_id_hex(id, hex2, sizeof(hex2));
751 err = got_error_fmt(GOT_ERR_REF_BUSY,
752 "Update %s: %s failed; %s: %s has been "
753 "created by someone else while transaction "
754 "was in progress",
755 got_ref_get_name(ref), hex1,
756 got_ref_get_name(ref), hex2);
757 goto done;
760 if (got_object_id_cmp(&new_id, &old_id) != 0) {
761 err = got_ref_change_ref(ref, &new_id);
762 if (err)
763 goto done;
764 err = got_ref_write(ref, repo);
765 if (err)
766 goto done;
767 err = queue_notification(&old_id, &new_id, repo, ref);
768 if (err)
769 goto done;
772 free(id);
773 id = NULL;
775 done:
776 if (err) {
777 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
778 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
779 "could not acquire exclusive file lock for %s",
780 refname);
782 send_ref_update_ng(client, &iref, refname, err->msg);
783 } else
784 send_ref_update_ok(client, &iref, refname);
786 if (client->nref_updates > 0) {
787 client->nref_updates--;
788 if (client->nref_updates == 0) {
789 send_refs_updated(client);
790 notif = STAILQ_FIRST(&notifications);
791 if (notif) {
792 gotd_session.state = GOTD_STATE_NOTIFY;
793 err = request_notification(notif);
794 if (err) {
795 log_warn("could not send notification: "
796 "%s", err->msg);
797 client->flush_disconnect = 1;
799 } else
800 client->flush_disconnect = 1;
804 if (locked) {
805 const struct got_error *unlock_err;
806 unlock_err = got_ref_unlock(ref);
807 if (unlock_err && err == NULL)
808 err = unlock_err;
810 if (ref)
811 got_ref_close(ref);
812 free(refname);
813 free(id);
814 return err;
817 static const struct got_error *
818 recv_notification_content(struct imsg *imsg)
820 struct gotd_imsg_notification_content inotif;
821 size_t datalen;
823 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
824 if (datalen < sizeof(inotif))
825 return got_error(GOT_ERR_PRIVSEP_LEN);
826 memcpy(&inotif, imsg->data, sizeof(inotif));
828 return NULL;
831 static void
832 session_dispatch_repo_child(int fd, short event, void *arg)
834 struct gotd_imsgev *iev = arg;
835 struct imsgbuf *ibuf = &iev->ibuf;
836 struct gotd_session_client *client = &gotd_session_client;
837 ssize_t n;
838 int shut = 0;
839 struct imsg imsg;
841 if (event & EV_READ) {
842 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
843 fatal("imsg_read error");
844 if (n == 0) {
845 /* Connection closed. */
846 shut = 1;
847 goto done;
851 if (event & EV_WRITE) {
852 n = msgbuf_write(&ibuf->w);
853 if (n == -1 && errno != EAGAIN)
854 fatal("msgbuf_write");
855 if (n == 0) {
856 /* Connection closed. */
857 shut = 1;
858 goto done;
862 for (;;) {
863 const struct got_error *err = NULL;
864 uint32_t client_id = 0;
865 int do_disconnect = 0;
866 int do_ref_updates = 0, do_ref_update = 0;
867 int do_packfile_install = 0, do_notify = 0;
869 if ((n = imsg_get(ibuf, &imsg)) == -1)
870 fatal("%s: imsg_get error", __func__);
871 if (n == 0) /* No more messages. */
872 break;
874 switch (imsg.hdr.type) {
875 case GOTD_IMSG_ERROR:
876 do_disconnect = 1;
877 err = gotd_imsg_recv_error(&client_id, &imsg);
878 break;
879 case GOTD_IMSG_PACKFILE_INSTALL:
880 err = recv_packfile_install(&imsg);
881 if (err == NULL)
882 do_packfile_install = 1;
883 break;
884 case GOTD_IMSG_REF_UPDATES_START:
885 err = recv_ref_updates_start(&imsg);
886 if (err == NULL)
887 do_ref_updates = 1;
888 break;
889 case GOTD_IMSG_REF_UPDATE:
890 err = recv_ref_update(&imsg);
891 if (err == NULL)
892 do_ref_update = 1;
893 break;
894 case GOTD_IMSG_NOTIFY:
895 err = recv_notification_content(&imsg);
896 if (err == NULL)
897 do_notify = 1;
898 break;
899 default:
900 log_debug("unexpected imsg %d", imsg.hdr.type);
901 break;
904 if (do_disconnect) {
905 if (err)
906 disconnect_on_error(client, err);
907 else
908 disconnect(client);
909 } else {
910 struct gotd_session_notif *notif;
912 if (do_packfile_install)
913 err = install_pack(client,
914 gotd_session.repo->path, &imsg);
915 else if (do_ref_updates)
916 err = begin_ref_updates(client, &imsg);
917 else if (do_ref_update)
918 err = update_ref(&shut, client,
919 gotd_session.repo->path, &imsg);
920 else if (do_notify)
921 err = forward_notification(client, &imsg);
922 if (err)
923 log_warnx("uid %d: %s", client->euid, err->msg);
925 notif = STAILQ_FIRST(&notifications);
926 if (notif && do_notify) {
927 /* Request content for next notification. */
928 err = request_notification(notif);
929 if (err) {
930 log_warn("could not send notification: "
931 "%s", err->msg);
932 shut = 1;
936 imsg_free(&imsg);
938 done:
939 if (!shut) {
940 gotd_imsg_event_add(iev);
941 } else {
942 /* This pipe is dead. Remove its event handler */
943 event_del(&iev->ev);
944 event_loopexit(NULL);
948 static const struct got_error *
949 recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
951 struct gotd_imsg_capabilities icapas;
952 size_t datalen;
954 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
955 if (datalen != sizeof(icapas))
956 return got_error(GOT_ERR_PRIVSEP_LEN);
957 memcpy(&icapas, imsg->data, sizeof(icapas));
959 client->ncapa_alloc = icapas.ncapabilities;
960 client->capabilities = calloc(client->ncapa_alloc,
961 sizeof(*client->capabilities));
962 if (client->capabilities == NULL) {
963 client->ncapa_alloc = 0;
964 return got_error_from_errno("calloc");
967 log_debug("expecting %zu capabilities from uid %d",
968 client->ncapa_alloc, client->euid);
969 return NULL;
972 static const struct got_error *
973 recv_capability(struct gotd_session_client *client, struct imsg *imsg)
975 struct gotd_imsg_capability icapa;
976 struct gotd_client_capability *capa;
977 size_t datalen;
978 char *key, *value = NULL;
980 if (client->capabilities == NULL ||
981 client->ncapabilities >= client->ncapa_alloc) {
982 return got_error_msg(GOT_ERR_BAD_REQUEST,
983 "unexpected capability received");
986 memset(&icapa, 0, sizeof(icapa));
988 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
989 if (datalen < sizeof(icapa))
990 return got_error(GOT_ERR_PRIVSEP_LEN);
991 memcpy(&icapa, imsg->data, sizeof(icapa));
993 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
994 return got_error(GOT_ERR_PRIVSEP_LEN);
996 key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
997 if (key == NULL)
998 return got_error_from_errno("strndup");
999 if (icapa.value_len > 0) {
1000 value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
1001 icapa.value_len);
1002 if (value == NULL) {
1003 free(key);
1004 return got_error_from_errno("strndup");
1008 capa = &client->capabilities[client->ncapabilities++];
1009 capa->key = key;
1010 capa->value = value;
1012 if (value)
1013 log_debug("uid %d: capability %s=%s", client->euid, key, value);
1014 else
1015 log_debug("uid %d: capability %s", client->euid, key);
1017 return NULL;
1020 static const struct got_error *
1021 forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
1023 const struct got_error *err = NULL;
1024 struct gotd_imsg_ref_update ireq;
1025 struct gotd_imsg_ref_update *iref = NULL;
1026 size_t datalen;
1028 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1029 if (datalen < sizeof(ireq))
1030 return got_error(GOT_ERR_PRIVSEP_LEN);
1031 memcpy(&ireq, imsg->data, sizeof(ireq));
1032 if (datalen != sizeof(ireq) + ireq.name_len)
1033 return got_error(GOT_ERR_PRIVSEP_LEN);
1035 iref = malloc(datalen);
1036 if (iref == NULL)
1037 return got_error_from_errno("malloc");
1038 memcpy(iref, imsg->data, datalen);
1040 if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1041 GOTD_IMSG_REF_UPDATE, PROC_SESSION_WRITE, -1,
1042 iref, datalen) == -1)
1043 err = got_error_from_errno("imsg compose REF_UPDATE");
1044 free(iref);
1045 return err;
1048 static int
1049 client_has_capability(struct gotd_session_client *client, const char *capastr)
1051 struct gotd_client_capability *capa;
1052 size_t i;
1054 if (client->ncapabilities == 0)
1055 return 0;
1057 for (i = 0; i < client->ncapabilities; i++) {
1058 capa = &client->capabilities[i];
1059 if (strcmp(capa->key, capastr) == 0)
1060 return 1;
1063 return 0;
1066 static const struct got_error *
1067 recv_packfile(struct gotd_session_client *client)
1069 const struct got_error *err = NULL;
1070 struct gotd_imsg_recv_packfile ipack;
1071 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
1072 int packfd = -1, idxfd = -1;
1073 int pipe[2] = { -1, -1 };
1075 if (client->packfile_path) {
1076 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
1077 "uid %d already has a pack file", client->euid);
1080 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
1081 return got_error_from_errno("socketpair");
1083 /* Send pack pipe end 0 to repo child process. */
1084 if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1085 GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION_WRITE, pipe[0],
1086 NULL, 0) == -1) {
1087 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1088 pipe[0] = -1;
1089 goto done;
1091 pipe[0] = -1;
1093 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1094 if (gotd_imsg_compose_event(&client->iev,
1095 GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION_WRITE, pipe[1],
1096 NULL, 0) == -1)
1097 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1098 pipe[1] = -1;
1100 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
1101 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1102 client->euid) == -1) {
1103 err = got_error_from_errno("asprintf");
1104 goto done;
1107 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
1108 if (err)
1109 goto done;
1110 if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
1111 err = got_error_from_errno2("fchmod", pack_path);
1112 goto done;
1115 free(basepath);
1116 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
1117 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1118 client->euid) == -1) {
1119 err = got_error_from_errno("asprintf");
1120 basepath = NULL;
1121 goto done;
1123 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
1124 if (err)
1125 goto done;
1126 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
1127 err = got_error_from_errno2("fchmod", idx_path);
1128 goto done;
1131 if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1132 GOTD_IMSG_PACKIDX_FILE, PROC_SESSION_WRITE,
1133 idxfd, NULL, 0) == -1) {
1134 err = got_error_from_errno("imsg compose PACKIDX_FILE");
1135 idxfd = -1;
1136 goto done;
1138 idxfd = -1;
1140 memset(&ipack, 0, sizeof(ipack));
1141 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
1142 ipack.report_status = 1;
1144 if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1145 GOTD_IMSG_RECV_PACKFILE, PROC_SESSION_WRITE, packfd,
1146 &ipack, sizeof(ipack)) == -1) {
1147 err = got_error_from_errno("imsg compose RECV_PACKFILE");
1148 packfd = -1;
1149 goto done;
1151 packfd = -1;
1153 done:
1154 free(basepath);
1155 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
1156 err = got_error_from_errno("close");
1157 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
1158 err = got_error_from_errno("close");
1159 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1160 err = got_error_from_errno("close");
1161 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1162 err = got_error_from_errno("close");
1163 if (err) {
1164 free(pack_path);
1165 free(idx_path);
1166 } else {
1167 client->packfile_path = pack_path;
1168 client->packidx_path = idx_path;
1170 return err;
1173 static void
1174 session_dispatch_client(int fd, short events, void *arg)
1176 struct gotd_imsgev *iev = arg;
1177 struct imsgbuf *ibuf = &iev->ibuf;
1178 struct gotd_session_client *client = &gotd_session_client;
1179 const struct got_error *err = NULL;
1180 struct imsg imsg;
1181 ssize_t n;
1183 if (events & EV_WRITE) {
1184 while (ibuf->w.queued) {
1185 n = msgbuf_write(&ibuf->w);
1186 if (n == -1 && errno == EPIPE) {
1188 * The client has closed its socket.
1189 * This can happen when Git clients are
1190 * done sending pack file data.
1192 msgbuf_clear(&ibuf->w);
1193 continue;
1194 } else if (n == -1 && errno != EAGAIN) {
1195 err = got_error_from_errno("imsg_flush");
1196 disconnect_on_error(client, err);
1197 return;
1199 if (n == 0) {
1200 /* Connection closed. */
1201 err = got_error(GOT_ERR_EOF);
1202 disconnect_on_error(client, err);
1203 return;
1207 if (client->flush_disconnect) {
1208 disconnect(client);
1209 return;
1213 if ((events & EV_READ) == 0)
1214 return;
1216 memset(&imsg, 0, sizeof(imsg));
1218 while (err == NULL) {
1219 err = gotd_imsg_recv(&imsg, ibuf, 0);
1220 if (err) {
1221 if (err->code == GOT_ERR_PRIVSEP_READ)
1222 err = NULL;
1223 else if (err->code == GOT_ERR_EOF &&
1224 gotd_session.state ==
1225 GOTD_STATE_EXPECT_CAPABILITIES) {
1227 * The client has closed its socket before
1228 * sending its capability announcement.
1229 * This can happen when Git clients have
1230 * no ref-updates to send.
1232 disconnect_on_error(client, err);
1233 return;
1235 break;
1238 evtimer_del(&client->tmo);
1240 switch (imsg.hdr.type) {
1241 case GOTD_IMSG_CAPABILITIES:
1242 if (gotd_session.state !=
1243 GOTD_STATE_EXPECT_CAPABILITIES) {
1244 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1245 "unexpected capabilities received");
1246 break;
1248 log_debug("receiving capabilities from uid %d",
1249 client->euid);
1250 err = recv_capabilities(client, &imsg);
1251 break;
1252 case GOTD_IMSG_CAPABILITY:
1253 if (gotd_session.state != GOTD_STATE_EXPECT_CAPABILITIES) {
1254 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1255 "unexpected capability received");
1256 break;
1258 err = recv_capability(client, &imsg);
1259 if (err || client->ncapabilities < client->ncapa_alloc)
1260 break;
1261 gotd_session.state = GOTD_STATE_EXPECT_REF_UPDATE;
1262 client->accept_flush_pkt = 1;
1263 log_debug("uid %d: expecting ref-update-lines",
1264 client->euid);
1265 break;
1266 case GOTD_IMSG_REF_UPDATE:
1267 if (gotd_session.state != GOTD_STATE_EXPECT_REF_UPDATE &&
1268 gotd_session.state !=
1269 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1270 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1271 "unexpected ref-update-line received");
1272 break;
1274 log_debug("received ref-update-line from uid %d",
1275 client->euid);
1276 err = forward_ref_update(client, &imsg);
1277 if (err)
1278 break;
1279 gotd_session.state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1280 client->accept_flush_pkt = 1;
1281 break;
1282 case GOTD_IMSG_FLUSH:
1283 if (gotd_session.state !=
1284 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1285 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1286 "unexpected flush-pkt received");
1287 break;
1289 if (!client->accept_flush_pkt) {
1290 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1291 "unexpected flush-pkt received");
1292 break;
1296 * Accept just one flush packet at a time.
1297 * Future client state transitions will set this flag
1298 * again if another flush packet is expected.
1300 client->accept_flush_pkt = 0;
1302 log_debug("received flush-pkt from uid %d",
1303 client->euid);
1304 if (gotd_session.state ==
1305 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1306 gotd_session.state = GOTD_STATE_EXPECT_PACKFILE;
1307 log_debug("uid %d: expecting packfile",
1308 client->euid);
1309 err = recv_packfile(client);
1310 } else {
1311 /* should not happen, see above */
1312 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1313 "unexpected client state");
1314 break;
1316 break;
1317 default:
1318 log_debug("unexpected imsg %d", imsg.hdr.type);
1319 err = got_error(GOT_ERR_PRIVSEP_MSG);
1320 break;
1323 imsg_free(&imsg);
1326 if (err) {
1327 if (err->code != GOT_ERR_EOF ||
1328 gotd_session.state != GOTD_STATE_EXPECT_PACKFILE)
1329 disconnect_on_error(client, err);
1330 } else {
1331 gotd_imsg_event_add(iev);
1332 evtimer_add(&client->tmo, &gotd_session.request_timeout);
1336 static const struct got_error *
1337 list_refs_request(void)
1339 static const struct got_error *err;
1340 struct gotd_session_client *client = &gotd_session_client;
1341 struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
1342 int fd;
1344 if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1345 return got_error(GOT_ERR_PRIVSEP_MSG);
1347 fd = dup(client->fd);
1348 if (fd == -1)
1349 return got_error_from_errno("dup");
1351 if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1352 PROC_SESSION_WRITE, fd, NULL, 0) == -1) {
1353 err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1354 close(fd);
1355 return err;
1358 gotd_session.state = GOTD_STATE_EXPECT_CAPABILITIES;
1359 log_debug("uid %d: expecting capabilities", client->euid);
1360 return NULL;
1363 static const struct got_error *
1364 recv_connect(struct imsg *imsg)
1366 struct gotd_session_client *client = &gotd_session_client;
1367 struct gotd_imsg_connect iconnect;
1368 size_t datalen;
1370 if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1371 return got_error(GOT_ERR_PRIVSEP_MSG);
1373 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1374 if (datalen < sizeof(iconnect))
1375 return got_error(GOT_ERR_PRIVSEP_LEN);
1376 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1377 if (iconnect.username_len == 0 ||
1378 datalen != sizeof(iconnect) + iconnect.username_len)
1379 return got_error(GOT_ERR_PRIVSEP_LEN);
1381 client->euid = iconnect.euid;
1382 client->egid = iconnect.egid;
1383 client->fd = imsg_get_fd(imsg);
1384 if (client->fd == -1)
1385 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1387 client->username = strndup(imsg->data + sizeof(iconnect),
1388 iconnect.username_len);
1389 if (client->username == NULL)
1390 return got_error_from_errno("strndup");
1392 imsg_init(&client->iev.ibuf, client->fd);
1393 client->iev.handler = session_dispatch_client;
1394 client->iev.events = EV_READ;
1395 client->iev.handler_arg = NULL;
1396 event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1397 session_dispatch_client, &client->iev);
1398 gotd_imsg_event_add(&client->iev);
1399 evtimer_set(&client->tmo, gotd_request_timeout, client);
1401 return NULL;
1404 static void
1405 session_dispatch_notifier(int fd, short event, void *arg)
1407 const struct got_error *err;
1408 struct gotd_session_client *client = &gotd_session_client;
1409 struct gotd_imsgev *iev = arg;
1410 struct imsgbuf *ibuf = &iev->ibuf;
1411 ssize_t n;
1412 int shut = 0;
1413 struct imsg imsg;
1414 struct gotd_session_notif *notif;
1416 if (event & EV_READ) {
1417 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1418 fatal("imsg_read error");
1419 if (n == 0) {
1420 /* Connection closed. */
1421 shut = 1;
1422 goto done;
1426 if (event & EV_WRITE) {
1427 n = msgbuf_write(&ibuf->w);
1428 if (n == -1 && errno != EAGAIN)
1429 fatal("msgbuf_write");
1430 if (n == 0) {
1431 /* Connection closed. */
1432 shut = 1;
1433 goto done;
1437 for (;;) {
1438 if ((n = imsg_get(ibuf, &imsg)) == -1)
1439 fatal("%s: imsg_get error", __func__);
1440 if (n == 0) /* No more messages. */
1441 break;
1443 switch (imsg.hdr.type) {
1444 case GOTD_IMSG_NOTIFICATION_SENT:
1445 if (gotd_session.state != GOTD_STATE_NOTIFY) {
1446 log_warn("unexpected imsg %d", imsg.hdr.type);
1447 break;
1449 notif = STAILQ_FIRST(&notifications);
1450 if (notif == NULL) {
1451 disconnect(client);
1452 break; /* NOTREACHED */
1454 /* Request content for the next notification. */
1455 err = request_notification(notif);
1456 if (err) {
1457 log_warn("could not send notification: %s",
1458 err->msg);
1459 disconnect(client);
1461 break;
1462 default:
1463 log_debug("unexpected imsg %d", imsg.hdr.type);
1464 break;
1467 imsg_free(&imsg);
1469 done:
1470 if (!shut) {
1471 gotd_imsg_event_add(iev);
1472 } else {
1473 /* This pipe is dead. Remove its event handler */
1474 event_del(&iev->ev);
1475 imsg_clear(&iev->ibuf);
1476 imsg_init(&iev->ibuf, -1);
1480 static const struct got_error *
1481 recv_notifier(struct imsg *imsg)
1483 struct gotd_imsgev *iev = &gotd_session.notifier_iev;
1484 struct gotd_session_client *client = &gotd_session_client;
1485 size_t datalen;
1486 int fd;
1488 if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1489 return got_error(GOT_ERR_PRIVSEP_MSG);
1491 /* We should already have received a pipe to the listener. */
1492 if (client->fd == -1)
1493 return got_error(GOT_ERR_PRIVSEP_MSG);
1495 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1496 if (datalen != 0)
1497 return got_error(GOT_ERR_PRIVSEP_LEN);
1499 fd = imsg_get_fd(imsg);
1500 if (fd == -1)
1501 return NULL; /* notifications unused */
1503 imsg_init(&iev->ibuf, fd);
1504 iev->handler = session_dispatch_notifier;
1505 iev->events = EV_READ;
1506 iev->handler_arg = NULL;
1507 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1508 session_dispatch_notifier, iev);
1509 gotd_imsg_event_add(iev);
1511 return NULL;
1514 static const struct got_error *
1515 recv_repo_child(struct imsg *imsg)
1517 struct gotd_imsg_connect_repo_child ichild;
1518 struct gotd_session_client *client = &gotd_session_client;
1519 size_t datalen;
1520 int fd;
1522 if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1523 return got_error(GOT_ERR_PRIVSEP_MSG);
1525 /* We should already have received a pipe to the listener. */
1526 if (client->fd == -1)
1527 return got_error(GOT_ERR_PRIVSEP_MSG);
1529 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1530 if (datalen != sizeof(ichild))
1531 return got_error(GOT_ERR_PRIVSEP_LEN);
1533 memcpy(&ichild, imsg->data, sizeof(ichild));
1535 if (ichild.proc_id != PROC_REPO_WRITE)
1536 return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1537 "bad child process type");
1539 fd = imsg_get_fd(imsg);
1540 if (fd == -1)
1541 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1543 imsg_init(&gotd_session.repo_child_iev.ibuf, fd);
1544 gotd_session.repo_child_iev.handler = session_dispatch_repo_child;
1545 gotd_session.repo_child_iev.events = EV_READ;
1546 gotd_session.repo_child_iev.handler_arg = NULL;
1547 event_set(&gotd_session.repo_child_iev.ev,
1548 gotd_session.repo_child_iev.ibuf.fd, EV_READ,
1549 session_dispatch_repo_child, &gotd_session.repo_child_iev);
1550 gotd_imsg_event_add(&gotd_session.repo_child_iev);
1552 /* The "recvfd" pledge promise is no longer needed. */
1553 if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1554 fatal("pledge");
1556 return NULL;
1559 static void
1560 session_dispatch(int fd, short event, void *arg)
1562 struct gotd_imsgev *iev = arg;
1563 struct imsgbuf *ibuf = &iev->ibuf;
1564 struct gotd_session_client *client = &gotd_session_client;
1565 ssize_t n;
1566 int shut = 0;
1567 struct imsg imsg;
1569 if (event & EV_READ) {
1570 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1571 fatal("imsg_read error");
1572 if (n == 0) {
1573 /* Connection closed. */
1574 shut = 1;
1575 goto done;
1579 if (event & EV_WRITE) {
1580 n = msgbuf_write(&ibuf->w);
1581 if (n == -1 && errno != EAGAIN)
1582 fatal("msgbuf_write");
1583 if (n == 0) {
1584 /* Connection closed. */
1585 shut = 1;
1586 goto done;
1590 for (;;) {
1591 const struct got_error *err = NULL;
1592 uint32_t client_id = 0;
1593 int do_disconnect = 0, do_list_refs = 0;
1595 if ((n = imsg_get(ibuf, &imsg)) == -1)
1596 fatal("%s: imsg_get error", __func__);
1597 if (n == 0) /* No more messages. */
1598 break;
1600 switch (imsg.hdr.type) {
1601 case GOTD_IMSG_ERROR:
1602 do_disconnect = 1;
1603 err = gotd_imsg_recv_error(&client_id, &imsg);
1604 break;
1605 case GOTD_IMSG_CONNECT:
1606 err = recv_connect(&imsg);
1607 break;
1608 case GOTD_IMSG_DISCONNECT:
1609 do_disconnect = 1;
1610 break;
1611 case GOTD_IMSG_CONNECT_NOTIFIER:
1612 err = recv_notifier(&imsg);
1613 break;
1614 case GOTD_IMSG_CONNECT_REPO_CHILD:
1615 err = recv_repo_child(&imsg);
1616 if (err)
1617 break;
1618 do_list_refs = 1;
1619 break;
1620 default:
1621 log_debug("unexpected imsg %d", imsg.hdr.type);
1622 break;
1624 imsg_free(&imsg);
1626 if (do_disconnect) {
1627 if (err)
1628 disconnect_on_error(client, err);
1629 else
1630 disconnect(client);
1631 } else if (do_list_refs)
1632 err = list_refs_request();
1634 if (err)
1635 log_warnx("uid %d: %s", client->euid, err->msg);
1637 done:
1638 if (!shut) {
1639 gotd_imsg_event_add(iev);
1640 } else {
1641 /* This pipe is dead. Remove its event handler */
1642 event_del(&iev->ev);
1643 event_loopexit(NULL);
1647 void
1648 session_write_main(const char *title, const char *repo_path,
1649 int *pack_fds, int *temp_fds, struct timeval *request_timeout,
1650 struct gotd_repo *repo_cfg)
1652 const struct got_error *err = NULL;
1653 struct event evsigint, evsigterm, evsighup, evsigusr1;
1655 STAILQ_INIT(&notifications);
1657 gotd_session.title = title;
1658 gotd_session.pid = getpid();
1659 gotd_session.pack_fds = pack_fds;
1660 gotd_session.temp_fds = temp_fds;
1661 memcpy(&gotd_session.request_timeout, request_timeout,
1662 sizeof(gotd_session.request_timeout));
1663 gotd_session.repo_cfg = repo_cfg;
1665 imsg_init(&gotd_session.notifier_iev.ibuf, -1);
1667 err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1668 if (err)
1669 goto done;
1670 if (!got_repo_is_bare(gotd_session.repo)) {
1671 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1672 "bare git repository required");
1673 goto done;
1676 got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1678 signal_set(&evsigint, SIGINT, session_write_sighdlr, NULL);
1679 signal_set(&evsigterm, SIGTERM, session_write_sighdlr, NULL);
1680 signal_set(&evsighup, SIGHUP, session_write_sighdlr, NULL);
1681 signal_set(&evsigusr1, SIGUSR1, session_write_sighdlr, NULL);
1682 signal(SIGPIPE, SIG_IGN);
1684 signal_add(&evsigint, NULL);
1685 signal_add(&evsigterm, NULL);
1686 signal_add(&evsighup, NULL);
1687 signal_add(&evsigusr1, NULL);
1689 gotd_session.state = GOTD_STATE_EXPECT_LIST_REFS;
1691 gotd_session_client.fd = -1;
1692 gotd_session_client.nref_updates = -1;
1693 gotd_session_client.delta_cache_fd = -1;
1694 gotd_session_client.accept_flush_pkt = 1;
1696 imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1697 gotd_session.parent_iev.handler = session_dispatch;
1698 gotd_session.parent_iev.events = EV_READ;
1699 gotd_session.parent_iev.handler_arg = NULL;
1700 event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1701 EV_READ, session_dispatch, &gotd_session.parent_iev);
1702 if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1703 GOTD_IMSG_CLIENT_SESSION_READY, PROC_SESSION_WRITE,
1704 -1, NULL, 0) == -1) {
1705 err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1706 goto done;
1709 event_dispatch();
1710 done:
1711 if (err)
1712 log_warnx("%s: %s", title, err->msg);
1713 session_write_shutdown();
1716 static void
1717 session_write_shutdown(void)
1719 struct gotd_session_notif *notif;
1721 log_debug("%s: shutting down", gotd_session.title);
1723 while (!STAILQ_EMPTY(&notifications)) {
1724 notif = STAILQ_FIRST(&notifications);
1725 STAILQ_REMOVE_HEAD(&notifications, entry);
1726 if (notif->fd != -1)
1727 close(notif->fd);
1728 free(notif->refname);
1729 free(notif);
1732 if (gotd_session.repo)
1733 got_repo_close(gotd_session.repo);
1734 got_repo_pack_fds_close(gotd_session.pack_fds);
1735 got_repo_temp_fds_close(gotd_session.temp_fds);
1736 free(gotd_session_client.username);
1737 exit(0);