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 "got_compat.h"
19 #include <sys/types.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 <signal.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <imsg.h>
33 #include <unistd.h>
35 #include "got_compat.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;
500 struct ibuf *wbuf;
502 memset(&inotify, 0, sizeof(inotify));
504 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
505 if (datalen < sizeof(icontent))
506 return got_error(GOT_ERR_PRIVSEP_LEN);
507 memcpy(&icontent, imsg->data, sizeof(icontent));
508 if (datalen != sizeof(icontent) + icontent.refname_len)
509 return got_error(GOT_ERR_PRIVSEP_LEN);
510 refname = strndup(imsg->data + sizeof(icontent), icontent.refname_len);
511 if (refname == NULL)
512 return got_error_from_errno("strndup");
514 notif = STAILQ_FIRST(&notifications);
515 if (notif == NULL)
516 return got_error(GOT_ERR_PRIVSEP_MSG);
518 STAILQ_REMOVE(&notifications, notif, gotd_session_notif, entry);
520 if (notif->action != icontent.action || notif->fd == -1 ||
521 strcmp(notif->refname, refname) != 0) {
522 err = got_error(GOT_ERR_PRIVSEP_MSG);
523 goto done;
525 if (notif->action == GOTD_NOTIF_ACTION_CREATED) {
526 if (memcmp(notif->new_id.sha1, icontent.new_id,
527 SHA1_DIGEST_LENGTH) != 0) {
528 err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
529 "received notification content for unknown event");
530 goto done;
532 } else if (notif->action == GOTD_NOTIF_ACTION_REMOVED) {
533 if (memcmp(notif->old_id.sha1, icontent.old_id,
534 SHA1_DIGEST_LENGTH) != 0) {
535 err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
536 "received notification content for unknown event");
537 goto done;
539 } else if (memcmp(notif->old_id.sha1, icontent.old_id,
540 SHA1_DIGEST_LENGTH) != 0 ||
541 memcmp(notif->new_id.sha1, icontent.new_id,
542 SHA1_DIGEST_LENGTH) != 0) {
543 err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
544 "received notification content for unknown event");
545 goto done;
548 switch (notif->action) {
549 case GOTD_NOTIF_ACTION_CREATED:
550 action = "created";
551 break;
552 case GOTD_NOTIF_ACTION_REMOVED:
553 action = "removed";
554 break;
555 case GOTD_NOTIF_ACTION_CHANGED:
556 action = "changed";
557 break;
558 default:
559 err = got_error(GOT_ERR_PRIVSEP_MSG);
560 goto done;
563 strlcpy(inotify.repo_name, gotd_session.repo_cfg->name,
564 sizeof(inotify.repo_name));
566 snprintf(inotify.subject_line, sizeof(inotify.subject_line),
567 "%s: %s %s %s", gotd_session.repo_cfg->name,
568 client->username, action, notif->refname);
570 inotify.username_len = strlen(client->username);
571 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_NOTIFY,
572 PROC_SESSION_WRITE, gotd_session.pid,
573 sizeof(inotify) + inotify.username_len);
574 if (wbuf == NULL) {
575 err = got_error_from_errno("imsg_create NOTIFY");
576 goto done;
578 if (imsg_add(wbuf, &inotify, sizeof(inotify)) == -1) {
579 err = got_error_from_errno("imsg_add NOTIFY");
580 goto done;
582 if (imsg_add(wbuf, client->username, inotify.username_len) == -1) {
583 err = got_error_from_errno("imsg_add NOTIFY");
584 goto done;
587 ibuf_fd_set(wbuf, notif->fd);
588 notif->fd = -1;
590 imsg_close(&iev->ibuf, wbuf);
591 gotd_imsg_event_add(iev);
592 done:
593 if (notif->fd != -1)
594 close(notif->fd);
595 free(notif);
596 free(refname);
597 return err;
600 /* Request notification content from REPO_WRITE process. */
601 static const struct got_error *
602 request_notification(struct gotd_session_notif *notif)
604 const struct got_error *err = NULL;
605 struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
606 struct gotd_imsg_notification_content icontent;
607 struct ibuf *wbuf;
608 size_t len;
609 int fd;
611 fd = got_opentempfd();
612 if (fd == -1)
613 return got_error_from_errno("got_opentemp");
615 memset(&icontent, 0, sizeof(icontent));
617 icontent.action = notif->action;
618 memcpy(&icontent.old_id, &notif->old_id, sizeof(notif->old_id));
619 memcpy(&icontent.new_id, &notif->new_id, sizeof(notif->new_id));
620 icontent.refname_len = strlen(notif->refname);
622 len = sizeof(icontent) + icontent.refname_len;
623 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_NOTIFY,
624 PROC_SESSION_WRITE, gotd_session.pid, len);
625 if (wbuf == NULL) {
626 err = got_error_from_errno("imsg_create NOTIFY");
627 goto done;
629 if (imsg_add(wbuf, &icontent, sizeof(icontent)) == -1) {
630 err = got_error_from_errno("imsg_add NOTIFY");
631 goto done;
633 if (imsg_add(wbuf, notif->refname, icontent.refname_len) == -1) {
634 err = got_error_from_errno("imsg_add NOTIFY");
635 goto done;
638 notif->fd = dup(fd);
639 if (notif->fd == -1) {
640 err = got_error_from_errno("dup");
641 goto done;
644 ibuf_fd_set(wbuf, fd);
645 fd = -1;
647 imsg_close(&iev->ibuf, wbuf);
648 gotd_imsg_event_add(iev);
649 done:
650 if (err && fd != -1)
651 close(fd);
652 return err;
655 static const struct got_error *
656 update_ref(int *shut, struct gotd_session_client *client,
657 const char *repo_path, struct imsg *imsg)
659 const struct got_error *err = NULL;
660 struct got_repository *repo = gotd_session.repo;
661 struct got_reference *ref = NULL;
662 struct gotd_imsg_ref_update iref;
663 struct got_object_id old_id, new_id;
664 struct gotd_session_notif *notif;
665 struct got_object_id *id = NULL;
666 char *refname = NULL;
667 size_t datalen;
668 int locked = 0;
669 char hex1[SHA1_DIGEST_STRING_LENGTH];
670 char hex2[SHA1_DIGEST_STRING_LENGTH];
672 log_debug("update-ref from uid %d", client->euid);
674 if (client->nref_updates <= 0)
675 return got_error(GOT_ERR_PRIVSEP_MSG);
677 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
678 if (datalen < sizeof(iref))
679 return got_error(GOT_ERR_PRIVSEP_LEN);
680 memcpy(&iref, imsg->data, sizeof(iref));
681 if (datalen != sizeof(iref) + iref.name_len)
682 return got_error(GOT_ERR_PRIVSEP_LEN);
683 refname = strndup(imsg->data + sizeof(iref), iref.name_len);
684 if (refname == NULL)
685 return got_error_from_errno("strndup");
687 log_debug("updating ref %s for uid %d", refname, client->euid);
689 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
690 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
691 err = got_repo_find_object_id(iref.delete_ref ? &old_id : &new_id,
692 repo);
693 if (err)
694 goto done;
696 if (iref.ref_is_new) {
697 err = got_ref_open(&ref, repo, refname, 0);
698 if (err) {
699 if (err->code != GOT_ERR_NOT_REF)
700 goto done;
701 err = got_ref_alloc(&ref, refname, &new_id);
702 if (err)
703 goto done;
704 err = got_ref_write(ref, repo); /* will lock/unlock */
705 if (err)
706 goto done;
707 err = queue_notification(NULL, &new_id, repo, ref);
708 if (err)
709 goto done;
710 } else {
711 err = got_ref_resolve(&id, repo, ref);
712 if (err)
713 goto done;
714 got_object_id_hex(&new_id, hex1, sizeof(hex1));
715 got_object_id_hex(id, hex2, sizeof(hex2));
716 err = got_error_fmt(GOT_ERR_REF_BUSY,
717 "Addition %s: %s failed; %s: %s has been "
718 "created by someone else while transaction "
719 "was in progress",
720 got_ref_get_name(ref), hex1,
721 got_ref_get_name(ref), hex2);
722 goto done;
724 } else if (iref.delete_ref) {
725 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
726 if (err)
727 goto done;
728 locked = 1;
730 err = got_ref_resolve(&id, repo, ref);
731 if (err)
732 goto done;
734 if (got_object_id_cmp(id, &old_id) != 0) {
735 got_object_id_hex(&old_id, hex1, sizeof(hex1));
736 got_object_id_hex(id, hex2, sizeof(hex2));
737 err = got_error_fmt(GOT_ERR_REF_BUSY,
738 "Deletion %s: %s failed; %s: %s has been "
739 "created by someone else while transaction "
740 "was in progress",
741 got_ref_get_name(ref), hex1,
742 got_ref_get_name(ref), hex2);
743 goto done;
746 err = got_ref_delete(ref, repo);
747 if (err)
748 goto done;
749 err = queue_notification(&old_id, NULL, repo, ref);
750 if (err)
751 goto done;
752 free(id);
753 id = NULL;
754 } else {
755 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
756 if (err)
757 goto done;
758 locked = 1;
760 err = got_ref_resolve(&id, repo, ref);
761 if (err)
762 goto done;
764 if (got_object_id_cmp(id, &old_id) != 0) {
765 got_object_id_hex(&old_id, hex1, sizeof(hex1));
766 got_object_id_hex(id, hex2, sizeof(hex2));
767 err = got_error_fmt(GOT_ERR_REF_BUSY,
768 "Update %s: %s failed; %s: %s has been "
769 "created by someone else while transaction "
770 "was in progress",
771 got_ref_get_name(ref), hex1,
772 got_ref_get_name(ref), hex2);
773 goto done;
776 if (got_object_id_cmp(&new_id, &old_id) != 0) {
777 err = got_ref_change_ref(ref, &new_id);
778 if (err)
779 goto done;
780 err = got_ref_write(ref, repo);
781 if (err)
782 goto done;
783 err = queue_notification(&old_id, &new_id, repo, ref);
784 if (err)
785 goto done;
788 free(id);
789 id = NULL;
791 done:
792 if (err) {
793 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
794 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
795 "could not acquire exclusive file lock for %s",
796 refname);
798 send_ref_update_ng(client, &iref, refname, err->msg);
799 } else
800 send_ref_update_ok(client, &iref, refname);
802 if (client->nref_updates > 0) {
803 client->nref_updates--;
804 if (client->nref_updates == 0) {
805 send_refs_updated(client);
806 notif = STAILQ_FIRST(&notifications);
807 if (notif) {
808 gotd_session.state = GOTD_STATE_NOTIFY;
809 err = request_notification(notif);
810 if (err) {
811 log_warn("could not send notification: "
812 "%s", err->msg);
813 client->flush_disconnect = 1;
815 } else
816 client->flush_disconnect = 1;
820 if (locked) {
821 const struct got_error *unlock_err;
822 unlock_err = got_ref_unlock(ref);
823 if (unlock_err && err == NULL)
824 err = unlock_err;
826 if (ref)
827 got_ref_close(ref);
828 free(refname);
829 free(id);
830 return err;
833 static const struct got_error *
834 recv_notification_content(struct imsg *imsg)
836 struct gotd_imsg_notification_content inotif;
837 size_t datalen;
839 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
840 if (datalen < sizeof(inotif))
841 return got_error(GOT_ERR_PRIVSEP_LEN);
842 memcpy(&inotif, imsg->data, sizeof(inotif));
844 return NULL;
847 static void
848 session_dispatch_repo_child(int fd, short event, void *arg)
850 struct gotd_imsgev *iev = arg;
851 struct imsgbuf *ibuf = &iev->ibuf;
852 struct gotd_session_client *client = &gotd_session_client;
853 ssize_t n;
854 int shut = 0;
855 struct imsg imsg;
857 if (event & EV_READ) {
858 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
859 fatal("imsg_read error");
860 if (n == 0) {
861 /* Connection closed. */
862 shut = 1;
863 goto done;
867 if (event & EV_WRITE) {
868 n = msgbuf_write(&ibuf->w);
869 if (n == -1 && errno != EAGAIN)
870 fatal("msgbuf_write");
871 if (n == 0) {
872 /* Connection closed. */
873 shut = 1;
874 goto done;
878 for (;;) {
879 const struct got_error *err = NULL;
880 uint32_t client_id = 0;
881 int do_disconnect = 0;
882 int do_ref_updates = 0, do_ref_update = 0;
883 int do_packfile_install = 0, do_notify = 0;
885 if ((n = imsg_get(ibuf, &imsg)) == -1)
886 fatal("%s: imsg_get error", __func__);
887 if (n == 0) /* No more messages. */
888 break;
890 switch (imsg.hdr.type) {
891 case GOTD_IMSG_ERROR:
892 do_disconnect = 1;
893 err = gotd_imsg_recv_error(&client_id, &imsg);
894 break;
895 case GOTD_IMSG_PACKFILE_INSTALL:
896 err = recv_packfile_install(&imsg);
897 if (err == NULL)
898 do_packfile_install = 1;
899 break;
900 case GOTD_IMSG_REF_UPDATES_START:
901 err = recv_ref_updates_start(&imsg);
902 if (err == NULL)
903 do_ref_updates = 1;
904 break;
905 case GOTD_IMSG_REF_UPDATE:
906 err = recv_ref_update(&imsg);
907 if (err == NULL)
908 do_ref_update = 1;
909 break;
910 case GOTD_IMSG_NOTIFY:
911 err = recv_notification_content(&imsg);
912 if (err == NULL)
913 do_notify = 1;
914 break;
915 default:
916 log_debug("unexpected imsg %d", imsg.hdr.type);
917 break;
920 if (do_disconnect || err) {
921 if (err)
922 disconnect_on_error(client, err);
923 else
924 disconnect(client);
925 } else {
926 struct gotd_session_notif *notif;
928 if (do_packfile_install)
929 err = install_pack(client,
930 gotd_session.repo->path, &imsg);
931 else if (do_ref_updates)
932 err = begin_ref_updates(client, &imsg);
933 else if (do_ref_update)
934 err = update_ref(&shut, client,
935 gotd_session.repo->path, &imsg);
936 else if (do_notify)
937 err = forward_notification(client, &imsg);
938 if (err)
939 log_warnx("uid %d: %s", client->euid, err->msg);
941 notif = STAILQ_FIRST(&notifications);
942 if (notif && do_notify) {
943 /* Request content for next notification. */
944 err = request_notification(notif);
945 if (err) {
946 log_warn("could not send notification: "
947 "%s", err->msg);
948 shut = 1;
952 imsg_free(&imsg);
954 done:
955 if (!shut) {
956 gotd_imsg_event_add(iev);
957 } else {
958 /* This pipe is dead. Remove its event handler */
959 event_del(&iev->ev);
960 event_loopexit(NULL);
964 static const struct got_error *
965 recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
967 struct gotd_imsg_capabilities icapas;
968 size_t datalen;
970 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
971 if (datalen != sizeof(icapas))
972 return got_error(GOT_ERR_PRIVSEP_LEN);
973 memcpy(&icapas, imsg->data, sizeof(icapas));
975 client->ncapa_alloc = icapas.ncapabilities;
976 client->capabilities = calloc(client->ncapa_alloc,
977 sizeof(*client->capabilities));
978 if (client->capabilities == NULL) {
979 client->ncapa_alloc = 0;
980 return got_error_from_errno("calloc");
983 log_debug("expecting %zu capabilities from uid %d",
984 client->ncapa_alloc, client->euid);
985 return NULL;
988 static const struct got_error *
989 recv_capability(struct gotd_session_client *client, struct imsg *imsg)
991 struct gotd_imsg_capability icapa;
992 struct gotd_client_capability *capa;
993 size_t datalen;
994 char *key, *value = NULL;
996 if (client->capabilities == NULL ||
997 client->ncapabilities >= client->ncapa_alloc) {
998 return got_error_msg(GOT_ERR_BAD_REQUEST,
999 "unexpected capability received");
1002 memset(&icapa, 0, sizeof(icapa));
1004 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1005 if (datalen < sizeof(icapa))
1006 return got_error(GOT_ERR_PRIVSEP_LEN);
1007 memcpy(&icapa, imsg->data, sizeof(icapa));
1009 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
1010 return got_error(GOT_ERR_PRIVSEP_LEN);
1012 key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
1013 if (key == NULL)
1014 return got_error_from_errno("strndup");
1015 if (icapa.value_len > 0) {
1016 value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
1017 icapa.value_len);
1018 if (value == NULL) {
1019 free(key);
1020 return got_error_from_errno("strndup");
1024 capa = &client->capabilities[client->ncapabilities++];
1025 capa->key = key;
1026 capa->value = value;
1028 if (value)
1029 log_debug("uid %d: capability %s=%s", client->euid, key, value);
1030 else
1031 log_debug("uid %d: capability %s", client->euid, key);
1033 return NULL;
1036 static const struct got_error *
1037 forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
1039 const struct got_error *err = NULL;
1040 struct gotd_imsg_ref_update ireq;
1041 struct gotd_imsg_ref_update *iref = NULL;
1042 size_t datalen;
1044 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1045 if (datalen < sizeof(ireq))
1046 return got_error(GOT_ERR_PRIVSEP_LEN);
1047 memcpy(&ireq, imsg->data, sizeof(ireq));
1048 if (datalen != sizeof(ireq) + ireq.name_len)
1049 return got_error(GOT_ERR_PRIVSEP_LEN);
1051 iref = malloc(datalen);
1052 if (iref == NULL)
1053 return got_error_from_errno("malloc");
1054 memcpy(iref, imsg->data, datalen);
1056 if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1057 GOTD_IMSG_REF_UPDATE, PROC_SESSION_WRITE, -1,
1058 iref, datalen) == -1)
1059 err = got_error_from_errno("imsg compose REF_UPDATE");
1060 free(iref);
1061 return err;
1064 static int
1065 client_has_capability(struct gotd_session_client *client, const char *capastr)
1067 struct gotd_client_capability *capa;
1068 size_t i;
1070 if (client->ncapabilities == 0)
1071 return 0;
1073 for (i = 0; i < client->ncapabilities; i++) {
1074 capa = &client->capabilities[i];
1075 if (strcmp(capa->key, capastr) == 0)
1076 return 1;
1079 return 0;
1082 static const struct got_error *
1083 recv_packfile(struct gotd_session_client *client)
1085 const struct got_error *err = NULL;
1086 struct gotd_imsg_recv_packfile ipack;
1087 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
1088 int packfd = -1, idxfd = -1;
1089 int pipe[2] = { -1, -1 };
1091 if (client->packfile_path) {
1092 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
1093 "uid %d already has a pack file", client->euid);
1096 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
1097 return got_error_from_errno("socketpair");
1099 /* Send pack pipe end 0 to repo child process. */
1100 if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1101 GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION_WRITE, pipe[0],
1102 NULL, 0) == -1) {
1103 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1104 pipe[0] = -1;
1105 goto done;
1107 pipe[0] = -1;
1109 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1110 if (gotd_imsg_compose_event(&client->iev,
1111 GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION_WRITE, pipe[1],
1112 NULL, 0) == -1)
1113 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1114 pipe[1] = -1;
1116 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
1117 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1118 client->euid) == -1) {
1119 err = got_error_from_errno("asprintf");
1120 goto done;
1123 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
1124 if (err)
1125 goto done;
1126 if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
1127 err = got_error_from_errno2("fchmod", pack_path);
1128 goto done;
1131 free(basepath);
1132 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
1133 got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1134 client->euid) == -1) {
1135 err = got_error_from_errno("asprintf");
1136 basepath = NULL;
1137 goto done;
1139 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
1140 if (err)
1141 goto done;
1142 if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
1143 err = got_error_from_errno2("fchmod", idx_path);
1144 goto done;
1147 if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1148 GOTD_IMSG_PACKIDX_FILE, PROC_SESSION_WRITE,
1149 idxfd, NULL, 0) == -1) {
1150 err = got_error_from_errno("imsg compose PACKIDX_FILE");
1151 idxfd = -1;
1152 goto done;
1154 idxfd = -1;
1156 memset(&ipack, 0, sizeof(ipack));
1157 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
1158 ipack.report_status = 1;
1160 if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1161 GOTD_IMSG_RECV_PACKFILE, PROC_SESSION_WRITE, packfd,
1162 &ipack, sizeof(ipack)) == -1) {
1163 err = got_error_from_errno("imsg compose RECV_PACKFILE");
1164 packfd = -1;
1165 goto done;
1167 packfd = -1;
1169 done:
1170 free(basepath);
1171 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
1172 err = got_error_from_errno("close");
1173 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
1174 err = got_error_from_errno("close");
1175 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1176 err = got_error_from_errno("close");
1177 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1178 err = got_error_from_errno("close");
1179 if (err) {
1180 free(pack_path);
1181 free(idx_path);
1182 } else {
1183 client->packfile_path = pack_path;
1184 client->packidx_path = idx_path;
1186 return err;
1189 static void
1190 session_dispatch_client(int fd, short events, void *arg)
1192 struct gotd_imsgev *iev = arg;
1193 struct imsgbuf *ibuf = &iev->ibuf;
1194 struct gotd_session_client *client = &gotd_session_client;
1195 const struct got_error *err = NULL;
1196 struct imsg imsg;
1197 ssize_t n;
1199 if (events & EV_WRITE) {
1200 while (ibuf->w.queued) {
1201 n = msgbuf_write(&ibuf->w);
1202 if (n == -1 && errno == EPIPE) {
1204 * The client has closed its socket.
1205 * This can happen when Git clients are
1206 * done sending pack file data.
1208 msgbuf_clear(&ibuf->w);
1209 continue;
1210 } else if (n == -1 && errno != EAGAIN) {
1211 err = got_error_from_errno("imsg_flush");
1212 disconnect_on_error(client, err);
1213 return;
1215 if (n == 0) {
1216 /* Connection closed. */
1217 err = got_error(GOT_ERR_EOF);
1218 disconnect_on_error(client, err);
1219 return;
1223 if (client->flush_disconnect) {
1224 disconnect(client);
1225 return;
1229 if ((events & EV_READ) == 0)
1230 return;
1232 memset(&imsg, 0, sizeof(imsg));
1234 while (err == NULL) {
1235 err = gotd_imsg_recv(&imsg, ibuf, 0);
1236 if (err) {
1237 if (err->code == GOT_ERR_PRIVSEP_READ)
1238 err = NULL;
1239 else if (err->code == GOT_ERR_EOF &&
1240 gotd_session.state ==
1241 GOTD_STATE_EXPECT_CAPABILITIES) {
1243 * The client has closed its socket before
1244 * sending its capability announcement.
1245 * This can happen when Git clients have
1246 * no ref-updates to send.
1248 disconnect_on_error(client, err);
1249 return;
1251 break;
1254 evtimer_del(&client->tmo);
1256 switch (imsg.hdr.type) {
1257 case GOTD_IMSG_CAPABILITIES:
1258 if (gotd_session.state !=
1259 GOTD_STATE_EXPECT_CAPABILITIES) {
1260 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1261 "unexpected capabilities received");
1262 break;
1264 log_debug("receiving capabilities from uid %d",
1265 client->euid);
1266 err = recv_capabilities(client, &imsg);
1267 break;
1268 case GOTD_IMSG_CAPABILITY:
1269 if (gotd_session.state != GOTD_STATE_EXPECT_CAPABILITIES) {
1270 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1271 "unexpected capability received");
1272 break;
1274 err = recv_capability(client, &imsg);
1275 if (err || client->ncapabilities < client->ncapa_alloc)
1276 break;
1277 gotd_session.state = GOTD_STATE_EXPECT_REF_UPDATE;
1278 client->accept_flush_pkt = 1;
1279 log_debug("uid %d: expecting ref-update-lines",
1280 client->euid);
1281 break;
1282 case GOTD_IMSG_REF_UPDATE:
1283 if (gotd_session.state != GOTD_STATE_EXPECT_REF_UPDATE &&
1284 gotd_session.state !=
1285 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1286 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1287 "unexpected ref-update-line received");
1288 break;
1290 log_debug("received ref-update-line from uid %d",
1291 client->euid);
1292 err = forward_ref_update(client, &imsg);
1293 if (err)
1294 break;
1295 gotd_session.state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1296 client->accept_flush_pkt = 1;
1297 break;
1298 case GOTD_IMSG_FLUSH:
1299 if (gotd_session.state !=
1300 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1301 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1302 "unexpected flush-pkt received");
1303 break;
1305 if (!client->accept_flush_pkt) {
1306 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1307 "unexpected flush-pkt received");
1308 break;
1312 * Accept just one flush packet at a time.
1313 * Future client state transitions will set this flag
1314 * again if another flush packet is expected.
1316 client->accept_flush_pkt = 0;
1318 log_debug("received flush-pkt from uid %d",
1319 client->euid);
1320 if (gotd_session.state ==
1321 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1322 gotd_session.state = GOTD_STATE_EXPECT_PACKFILE;
1323 log_debug("uid %d: expecting packfile",
1324 client->euid);
1325 err = recv_packfile(client);
1326 } else {
1327 /* should not happen, see above */
1328 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1329 "unexpected client state");
1330 break;
1332 break;
1333 default:
1334 log_debug("unexpected imsg %d", imsg.hdr.type);
1335 err = got_error(GOT_ERR_PRIVSEP_MSG);
1336 break;
1339 imsg_free(&imsg);
1342 if (err) {
1343 if (err->code != GOT_ERR_EOF ||
1344 gotd_session.state != GOTD_STATE_EXPECT_PACKFILE)
1345 disconnect_on_error(client, err);
1346 } else {
1347 gotd_imsg_event_add(iev);
1348 evtimer_add(&client->tmo, &gotd_session.request_timeout);
1352 static const struct got_error *
1353 list_refs_request(void)
1355 static const struct got_error *err;
1356 struct gotd_session_client *client = &gotd_session_client;
1357 struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
1358 int fd;
1360 if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1361 return got_error(GOT_ERR_PRIVSEP_MSG);
1363 fd = dup(client->fd);
1364 if (fd == -1)
1365 return got_error_from_errno("dup");
1367 if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1368 PROC_SESSION_WRITE, fd, NULL, 0) == -1) {
1369 err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1370 close(fd);
1371 return err;
1374 gotd_session.state = GOTD_STATE_EXPECT_CAPABILITIES;
1375 log_debug("uid %d: expecting capabilities", client->euid);
1376 return NULL;
1379 static const struct got_error *
1380 recv_connect(struct imsg *imsg)
1382 struct gotd_session_client *client = &gotd_session_client;
1383 struct gotd_imsg_connect iconnect;
1384 size_t datalen;
1386 if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1387 return got_error(GOT_ERR_PRIVSEP_MSG);
1389 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1390 if (datalen < sizeof(iconnect))
1391 return got_error(GOT_ERR_PRIVSEP_LEN);
1392 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1393 if (iconnect.username_len == 0 ||
1394 datalen != sizeof(iconnect) + iconnect.username_len)
1395 return got_error(GOT_ERR_PRIVSEP_LEN);
1397 client->euid = iconnect.euid;
1398 client->egid = iconnect.egid;
1399 client->fd = imsg_get_fd(imsg);
1400 if (client->fd == -1)
1401 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1403 client->username = strndup(imsg->data + sizeof(iconnect),
1404 iconnect.username_len);
1405 if (client->username == NULL)
1406 return got_error_from_errno("strndup");
1408 imsg_init(&client->iev.ibuf, client->fd);
1409 client->iev.handler = session_dispatch_client;
1410 client->iev.events = EV_READ;
1411 client->iev.handler_arg = NULL;
1412 event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1413 session_dispatch_client, &client->iev);
1414 gotd_imsg_event_add(&client->iev);
1415 evtimer_set(&client->tmo, gotd_request_timeout, client);
1417 return NULL;
1420 static void
1421 session_dispatch_notifier(int fd, short event, void *arg)
1423 const struct got_error *err;
1424 struct gotd_session_client *client = &gotd_session_client;
1425 struct gotd_imsgev *iev = arg;
1426 struct imsgbuf *ibuf = &iev->ibuf;
1427 ssize_t n;
1428 int shut = 0;
1429 struct imsg imsg;
1430 struct gotd_session_notif *notif;
1432 if (event & EV_READ) {
1433 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1434 fatal("imsg_read error");
1435 if (n == 0) {
1436 /* Connection closed. */
1437 shut = 1;
1438 goto done;
1442 if (event & EV_WRITE) {
1443 n = msgbuf_write(&ibuf->w);
1444 if (n == -1 && errno != EAGAIN)
1445 fatal("msgbuf_write");
1446 if (n == 0) {
1447 /* Connection closed. */
1448 shut = 1;
1449 goto done;
1453 for (;;) {
1454 if ((n = imsg_get(ibuf, &imsg)) == -1)
1455 fatal("%s: imsg_get error", __func__);
1456 if (n == 0) /* No more messages. */
1457 break;
1459 switch (imsg.hdr.type) {
1460 case GOTD_IMSG_NOTIFICATION_SENT:
1461 if (gotd_session.state != GOTD_STATE_NOTIFY) {
1462 log_warn("unexpected imsg %d", imsg.hdr.type);
1463 break;
1465 notif = STAILQ_FIRST(&notifications);
1466 if (notif == NULL) {
1467 disconnect(client);
1468 break; /* NOTREACHED */
1470 /* Request content for the next notification. */
1471 err = request_notification(notif);
1472 if (err) {
1473 log_warn("could not send notification: %s",
1474 err->msg);
1475 disconnect(client);
1477 break;
1478 default:
1479 log_debug("unexpected imsg %d", imsg.hdr.type);
1480 break;
1483 imsg_free(&imsg);
1485 done:
1486 if (!shut) {
1487 gotd_imsg_event_add(iev);
1488 } else {
1489 /* This pipe is dead. Remove its event handler */
1490 event_del(&iev->ev);
1491 imsg_clear(&iev->ibuf);
1492 imsg_init(&iev->ibuf, -1);
1496 static const struct got_error *
1497 recv_notifier(struct imsg *imsg)
1499 struct gotd_imsgev *iev = &gotd_session.notifier_iev;
1500 struct gotd_session_client *client = &gotd_session_client;
1501 size_t datalen;
1502 int fd;
1504 if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1505 return got_error(GOT_ERR_PRIVSEP_MSG);
1507 /* We should already have received a pipe to the listener. */
1508 if (client->fd == -1)
1509 return got_error(GOT_ERR_PRIVSEP_MSG);
1511 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1512 if (datalen != 0)
1513 return got_error(GOT_ERR_PRIVSEP_LEN);
1515 fd = imsg_get_fd(imsg);
1516 if (fd == -1)
1517 return NULL; /* notifications unused */
1519 imsg_init(&iev->ibuf, fd);
1520 iev->handler = session_dispatch_notifier;
1521 iev->events = EV_READ;
1522 iev->handler_arg = NULL;
1523 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1524 session_dispatch_notifier, iev);
1525 gotd_imsg_event_add(iev);
1527 return NULL;
1530 static const struct got_error *
1531 recv_repo_child(struct imsg *imsg)
1533 struct gotd_imsg_connect_repo_child ichild;
1534 struct gotd_session_client *client = &gotd_session_client;
1535 size_t datalen;
1536 int fd;
1538 if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1539 return got_error(GOT_ERR_PRIVSEP_MSG);
1541 /* We should already have received a pipe to the listener. */
1542 if (client->fd == -1)
1543 return got_error(GOT_ERR_PRIVSEP_MSG);
1545 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1546 if (datalen != sizeof(ichild))
1547 return got_error(GOT_ERR_PRIVSEP_LEN);
1549 memcpy(&ichild, imsg->data, sizeof(ichild));
1551 if (ichild.proc_id != PROC_REPO_WRITE)
1552 return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1553 "bad child process type");
1555 fd = imsg_get_fd(imsg);
1556 if (fd == -1)
1557 return got_error(GOT_ERR_PRIVSEP_NO_FD);
1559 imsg_init(&gotd_session.repo_child_iev.ibuf, fd);
1560 gotd_session.repo_child_iev.handler = session_dispatch_repo_child;
1561 gotd_session.repo_child_iev.events = EV_READ;
1562 gotd_session.repo_child_iev.handler_arg = NULL;
1563 event_set(&gotd_session.repo_child_iev.ev,
1564 gotd_session.repo_child_iev.ibuf.fd, EV_READ,
1565 session_dispatch_repo_child, &gotd_session.repo_child_iev);
1566 gotd_imsg_event_add(&gotd_session.repo_child_iev);
1568 /* The "recvfd" pledge promise is no longer needed. */
1569 if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1570 fatal("pledge");
1572 return NULL;
1575 static void
1576 session_dispatch(int fd, short event, void *arg)
1578 struct gotd_imsgev *iev = arg;
1579 struct imsgbuf *ibuf = &iev->ibuf;
1580 struct gotd_session_client *client = &gotd_session_client;
1581 ssize_t n;
1582 int shut = 0;
1583 struct imsg imsg;
1585 if (event & EV_READ) {
1586 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1587 fatal("imsg_read error");
1588 if (n == 0) {
1589 /* Connection closed. */
1590 shut = 1;
1591 goto done;
1595 if (event & EV_WRITE) {
1596 n = msgbuf_write(&ibuf->w);
1597 if (n == -1 && errno != EAGAIN)
1598 fatal("msgbuf_write");
1599 if (n == 0) {
1600 /* Connection closed. */
1601 shut = 1;
1602 goto done;
1606 for (;;) {
1607 const struct got_error *err = NULL;
1608 uint32_t client_id = 0;
1609 int do_disconnect = 0, do_list_refs = 0;
1611 if ((n = imsg_get(ibuf, &imsg)) == -1)
1612 fatal("%s: imsg_get error", __func__);
1613 if (n == 0) /* No more messages. */
1614 break;
1616 switch (imsg.hdr.type) {
1617 case GOTD_IMSG_ERROR:
1618 do_disconnect = 1;
1619 err = gotd_imsg_recv_error(&client_id, &imsg);
1620 break;
1621 case GOTD_IMSG_CONNECT:
1622 err = recv_connect(&imsg);
1623 break;
1624 case GOTD_IMSG_DISCONNECT:
1625 do_disconnect = 1;
1626 break;
1627 case GOTD_IMSG_CONNECT_NOTIFIER:
1628 err = recv_notifier(&imsg);
1629 break;
1630 case GOTD_IMSG_CONNECT_REPO_CHILD:
1631 err = recv_repo_child(&imsg);
1632 if (err)
1633 break;
1634 do_list_refs = 1;
1635 break;
1636 default:
1637 log_debug("unexpected imsg %d", imsg.hdr.type);
1638 break;
1640 imsg_free(&imsg);
1642 if (do_disconnect) {
1643 if (err)
1644 disconnect_on_error(client, err);
1645 else
1646 disconnect(client);
1647 } else if (do_list_refs)
1648 err = list_refs_request();
1650 if (err)
1651 log_warnx("uid %d: %s", client->euid, err->msg);
1653 done:
1654 if (!shut) {
1655 gotd_imsg_event_add(iev);
1656 } else {
1657 /* This pipe is dead. Remove its event handler */
1658 event_del(&iev->ev);
1659 event_loopexit(NULL);
1663 void
1664 session_write_main(const char *title, const char *repo_path,
1665 int *pack_fds, int *temp_fds, struct timeval *request_timeout,
1666 struct gotd_repo *repo_cfg)
1668 const struct got_error *err = NULL;
1669 struct event evsigint, evsigterm, evsighup, evsigusr1;
1671 STAILQ_INIT(&notifications);
1673 gotd_session.title = title;
1674 gotd_session.pid = getpid();
1675 gotd_session.pack_fds = pack_fds;
1676 gotd_session.temp_fds = temp_fds;
1677 memcpy(&gotd_session.request_timeout, request_timeout,
1678 sizeof(gotd_session.request_timeout));
1679 gotd_session.repo_cfg = repo_cfg;
1681 imsg_init(&gotd_session.notifier_iev.ibuf, -1);
1683 err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1684 if (err)
1685 goto done;
1686 if (!got_repo_is_bare(gotd_session.repo)) {
1687 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1688 "bare git repository required");
1689 goto done;
1692 got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1694 signal_set(&evsigint, SIGINT, session_write_sighdlr, NULL);
1695 signal_set(&evsigterm, SIGTERM, session_write_sighdlr, NULL);
1696 signal_set(&evsighup, SIGHUP, session_write_sighdlr, NULL);
1697 signal_set(&evsigusr1, SIGUSR1, session_write_sighdlr, NULL);
1698 signal(SIGPIPE, SIG_IGN);
1700 signal_add(&evsigint, NULL);
1701 signal_add(&evsigterm, NULL);
1702 signal_add(&evsighup, NULL);
1703 signal_add(&evsigusr1, NULL);
1705 gotd_session.state = GOTD_STATE_EXPECT_LIST_REFS;
1707 gotd_session_client.fd = -1;
1708 gotd_session_client.nref_updates = -1;
1709 gotd_session_client.delta_cache_fd = -1;
1710 gotd_session_client.accept_flush_pkt = 1;
1712 imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1713 gotd_session.parent_iev.handler = session_dispatch;
1714 gotd_session.parent_iev.events = EV_READ;
1715 gotd_session.parent_iev.handler_arg = NULL;
1716 event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1717 EV_READ, session_dispatch, &gotd_session.parent_iev);
1718 if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1719 GOTD_IMSG_CLIENT_SESSION_READY, PROC_SESSION_WRITE,
1720 -1, NULL, 0) == -1) {
1721 err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1722 goto done;
1725 event_dispatch();
1726 done:
1727 if (err)
1728 log_warnx("%s: %s", title, err->msg);
1729 session_write_shutdown();
1732 static void
1733 session_write_shutdown(void)
1735 struct gotd_session_notif *notif;
1737 log_debug("%s: shutting down", gotd_session.title);
1739 while (!STAILQ_EMPTY(&notifications)) {
1740 notif = STAILQ_FIRST(&notifications);
1741 STAILQ_REMOVE_HEAD(&notifications, entry);
1742 if (notif->fd != -1)
1743 close(notif->fd);
1744 free(notif->refname);
1745 free(notif);
1748 if (gotd_session.repo)
1749 got_repo_close(gotd_session.repo);
1750 got_repo_pack_fds_close(gotd_session.pack_fds);
1751 got_repo_temp_fds_close(gotd_session.temp_fds);
1752 free(gotd_session_client.username);
1753 exit(0);