2 * Copyright (c) 2022, 2023 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/socket.h>
34 #include "got_error.h"
35 #include "got_repository.h"
36 #include "got_object.h"
38 #include "got_reference.h"
39 #include "got_opentemp.h"
41 #include "got_lib_hash.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_cache.h"
45 #include "got_lib_pack.h"
46 #include "got_lib_repository.h"
47 #include "got_lib_gitproto.h"
54 static struct gotd_session
{
57 struct got_repository
*repo
;
60 struct gotd_imsgev parent_iev
;
61 struct timeval request_timeout
;
64 static struct gotd_session_client
{
65 enum gotd_session_state state
;
67 struct gotd_client_capability
*capabilities
;
73 struct gotd_imsgev iev
;
74 struct gotd_imsgev repo_child_iev
;
82 } gotd_session_client
;
84 void gotd_session_sighdlr(int sig
, short event
, void *arg
);
85 static void gotd_session_shutdown(void);
88 disconnect(struct gotd_session_client
*client
)
90 log_debug("uid %d: disconnecting", client
->euid
);
92 if (gotd_imsg_compose_event(&gotd_session
.parent_iev
,
93 GOTD_IMSG_DISCONNECT
, PROC_SESSION
, -1, NULL
, 0) == -1)
94 log_warn("imsg compose DISCONNECT");
96 imsg_clear(&client
->repo_child_iev
.ibuf
);
97 event_del(&client
->repo_child_iev
.ev
);
98 evtimer_del(&client
->tmo
);
100 if (client
->delta_cache_fd
!= -1)
101 close(client
->delta_cache_fd
);
102 if (client
->packfile_path
) {
103 if (unlink(client
->packfile_path
) == -1 && errno
!= ENOENT
)
104 log_warn("unlink %s: ", client
->packfile_path
);
105 free(client
->packfile_path
);
107 if (client
->packidx_path
) {
108 if (unlink(client
->packidx_path
) == -1 && errno
!= ENOENT
)
109 log_warn("unlink %s: ", client
->packidx_path
);
110 free(client
->packidx_path
);
112 free(client
->capabilities
);
114 gotd_session_shutdown();
118 disconnect_on_error(struct gotd_session_client
*client
,
119 const struct got_error
*err
)
123 log_warnx("uid %d: %s", client
->euid
, err
->msg
);
124 if (err
->code
!= GOT_ERR_EOF
) {
125 imsg_init(&ibuf
, client
->fd
);
126 gotd_imsg_send_error(&ibuf
, 0, PROC_SESSION
, err
);
134 gotd_request_timeout(int fd
, short events
, void *arg
)
136 struct gotd_session_client
*client
= arg
;
138 log_debug("disconnecting uid %d due to timeout", client
->euid
);
143 gotd_session_sighdlr(int sig
, short event
, void *arg
)
146 * Normal signal handler rules don't apply because libevent
152 log_info("%s: ignoring SIGHUP", __func__
);
155 log_info("%s: ignoring SIGUSR1", __func__
);
159 gotd_session_shutdown();
163 fatalx("unexpected signal");
167 static const struct got_error
*
168 recv_packfile_done(uint32_t *client_id
, struct imsg
*imsg
)
170 struct gotd_imsg_packfile_done idone
;
173 log_debug("packfile-done received");
175 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
176 if (datalen
!= sizeof(idone
))
177 return got_error(GOT_ERR_PRIVSEP_LEN
);
178 memcpy(&idone
, imsg
->data
, sizeof(idone
));
180 *client_id
= idone
.client_id
;
184 static const struct got_error
*
185 recv_packfile_install(uint32_t *client_id
, struct imsg
*imsg
)
187 struct gotd_imsg_packfile_install inst
;
190 log_debug("packfile-install received");
192 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
193 if (datalen
!= sizeof(inst
))
194 return got_error(GOT_ERR_PRIVSEP_LEN
);
195 memcpy(&inst
, imsg
->data
, sizeof(inst
));
197 *client_id
= inst
.client_id
;
201 static const struct got_error
*
202 recv_ref_updates_start(uint32_t *client_id
, struct imsg
*imsg
)
204 struct gotd_imsg_ref_updates_start istart
;
207 log_debug("ref-updates-start received");
209 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
210 if (datalen
!= sizeof(istart
))
211 return got_error(GOT_ERR_PRIVSEP_LEN
);
212 memcpy(&istart
, imsg
->data
, sizeof(istart
));
214 *client_id
= istart
.client_id
;
218 static const struct got_error
*
219 recv_ref_update(uint32_t *client_id
, struct imsg
*imsg
)
221 struct gotd_imsg_ref_update iref
;
224 log_debug("ref-update received");
226 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
227 if (datalen
< sizeof(iref
))
228 return got_error(GOT_ERR_PRIVSEP_LEN
);
229 memcpy(&iref
, imsg
->data
, sizeof(iref
));
231 *client_id
= iref
.client_id
;
235 static const struct got_error
*
236 send_ref_update_ok(struct gotd_session_client
*client
,
237 struct gotd_imsg_ref_update
*iref
, const char *refname
)
239 struct gotd_imsg_ref_update_ok iok
;
240 struct gotd_imsgev
*iev
= &client
->iev
;
244 memset(&iok
, 0, sizeof(iok
));
245 iok
.client_id
= client
->id
;
246 memcpy(iok
.old_id
, iref
->old_id
, SHA1_DIGEST_LENGTH
);
247 memcpy(iok
.new_id
, iref
->new_id
, SHA1_DIGEST_LENGTH
);
248 iok
.name_len
= strlen(refname
);
250 len
= sizeof(iok
) + iok
.name_len
;
251 wbuf
= imsg_create(&iev
->ibuf
, GOTD_IMSG_REF_UPDATE_OK
,
252 PROC_SESSION
, gotd_session
.pid
, len
);
254 return got_error_from_errno("imsg_create REF_UPDATE_OK");
256 if (imsg_add(wbuf
, &iok
, sizeof(iok
)) == -1)
257 return got_error_from_errno("imsg_add REF_UPDATE_OK");
258 if (imsg_add(wbuf
, refname
, iok
.name_len
) == -1)
259 return got_error_from_errno("imsg_add REF_UPDATE_OK");
262 imsg_close(&iev
->ibuf
, wbuf
);
263 gotd_imsg_event_add(iev
);
268 send_refs_updated(struct gotd_session_client
*client
)
270 if (gotd_imsg_compose_event(&client
->iev
, GOTD_IMSG_REFS_UPDATED
,
271 PROC_SESSION
, -1, NULL
, 0) == -1)
272 log_warn("imsg compose REFS_UPDATED");
275 static const struct got_error
*
276 send_ref_update_ng(struct gotd_session_client
*client
,
277 struct gotd_imsg_ref_update
*iref
, const char *refname
,
280 const struct got_error
*ng_err
;
281 struct gotd_imsg_ref_update_ng ing
;
282 struct gotd_imsgev
*iev
= &client
->iev
;
286 memset(&ing
, 0, sizeof(ing
));
287 ing
.client_id
= client
->id
;
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
, gotd_session
.pid
, len
);
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");
309 imsg_close(&iev
->ibuf
, wbuf
);
310 gotd_imsg_event_add(iev
);
314 static const struct got_error
*
315 install_pack(struct gotd_session_client
*client
, const char *repo_path
,
318 const struct got_error
*err
= NULL
;
319 struct gotd_imsg_packfile_install inst
;
320 char hex
[SHA1_DIGEST_STRING_LENGTH
];
322 char *packfile_path
= NULL
, *packidx_path
= NULL
;
324 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
325 if (datalen
!= sizeof(inst
))
326 return got_error(GOT_ERR_PRIVSEP_LEN
);
327 memcpy(&inst
, imsg
->data
, sizeof(inst
));
329 if (client
->packfile_path
== NULL
)
330 return got_error_msg(GOT_ERR_BAD_REQUEST
,
331 "client has no pack file");
332 if (client
->packidx_path
== NULL
)
333 return got_error_msg(GOT_ERR_BAD_REQUEST
,
334 "client has no pack file index");
336 if (got_sha1_digest_to_str(inst
.pack_sha1
, hex
, sizeof(hex
)) == NULL
)
337 return got_error_msg(GOT_ERR_NO_SPACE
,
338 "could not convert pack file SHA1 to hex");
340 if (asprintf(&packfile_path
, "/%s/%s/pack-%s.pack",
341 repo_path
, GOT_OBJECTS_PACK_DIR
, hex
) == -1) {
342 err
= got_error_from_errno("asprintf");
346 if (asprintf(&packidx_path
, "/%s/%s/pack-%s.idx",
347 repo_path
, GOT_OBJECTS_PACK_DIR
, hex
) == -1) {
348 err
= got_error_from_errno("asprintf");
352 if (rename(client
->packfile_path
, packfile_path
) == -1) {
353 err
= got_error_from_errno3("rename", client
->packfile_path
,
358 free(client
->packfile_path
);
359 client
->packfile_path
= NULL
;
361 if (rename(client
->packidx_path
, packidx_path
) == -1) {
362 err
= got_error_from_errno3("rename", client
->packidx_path
,
367 free(client
->packidx_path
);
368 client
->packidx_path
= NULL
;
375 static const struct got_error
*
376 begin_ref_updates(struct gotd_session_client
*client
, struct imsg
*imsg
)
378 struct gotd_imsg_ref_updates_start istart
;
381 if (client
->nref_updates
!= -1)
382 return got_error(GOT_ERR_PRIVSEP_MSG
);
384 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
385 if (datalen
!= sizeof(istart
))
386 return got_error(GOT_ERR_PRIVSEP_LEN
);
387 memcpy(&istart
, imsg
->data
, sizeof(istart
));
389 if (istart
.nref_updates
<= 0)
390 return got_error(GOT_ERR_PRIVSEP_MSG
);
392 client
->nref_updates
= istart
.nref_updates
;
396 static const struct got_error
*
397 update_ref(int *shut
, struct gotd_session_client
*client
,
398 const char *repo_path
, struct imsg
*imsg
)
400 const struct got_error
*err
= NULL
;
401 struct got_repository
*repo
= NULL
;
402 struct got_reference
*ref
= NULL
;
403 struct gotd_imsg_ref_update iref
;
404 struct got_object_id old_id
, new_id
;
405 struct got_object_id
*id
= NULL
;
406 struct got_object
*obj
= NULL
;
407 char *refname
= NULL
;
411 log_debug("update-ref from uid %d", client
->euid
);
413 if (client
->nref_updates
<= 0)
414 return got_error(GOT_ERR_PRIVSEP_MSG
);
416 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
417 if (datalen
< sizeof(iref
))
418 return got_error(GOT_ERR_PRIVSEP_LEN
);
419 memcpy(&iref
, imsg
->data
, sizeof(iref
));
420 if (datalen
!= sizeof(iref
) + iref
.name_len
)
421 return got_error(GOT_ERR_PRIVSEP_LEN
);
422 refname
= strndup(imsg
->data
+ sizeof(iref
), iref
.name_len
);
424 return got_error_from_errno("strndup");
426 log_debug("updating ref %s for uid %d", refname
, client
->euid
);
428 err
= got_repo_open(&repo
, repo_path
, NULL
, NULL
);
432 memcpy(old_id
.sha1
, iref
.old_id
, SHA1_DIGEST_LENGTH
);
433 memcpy(new_id
.sha1
, iref
.new_id
, SHA1_DIGEST_LENGTH
);
434 err
= got_object_open(&obj
, repo
,
435 iref
.delete_ref
? &old_id
: &new_id
);
439 if (iref
.ref_is_new
) {
440 err
= got_ref_open(&ref
, repo
, refname
, 0);
442 if (err
->code
!= GOT_ERR_NOT_REF
)
444 err
= got_ref_alloc(&ref
, refname
, &new_id
);
447 err
= got_ref_write(ref
, repo
); /* will lock/unlock */
451 err
= got_error_fmt(GOT_ERR_REF_BUSY
,
452 "%s has been created by someone else "
453 "while transaction was in progress",
454 got_ref_get_name(ref
));
457 } else if (iref
.delete_ref
) {
458 err
= got_ref_open(&ref
, repo
, refname
, 1 /* lock */);
463 err
= got_ref_resolve(&id
, repo
, ref
);
467 if (got_object_id_cmp(id
, &old_id
) != 0) {
468 err
= got_error_fmt(GOT_ERR_REF_BUSY
,
469 "%s has been modified by someone else "
470 "while transaction was in progress",
471 got_ref_get_name(ref
));
475 err
= got_ref_delete(ref
, repo
);
482 err
= got_ref_open(&ref
, repo
, refname
, 1 /* lock */);
487 err
= got_ref_resolve(&id
, repo
, ref
);
491 if (got_object_id_cmp(id
, &old_id
) != 0) {
492 err
= got_error_fmt(GOT_ERR_REF_BUSY
,
493 "%s has been modified by someone else "
494 "while transaction was in progress",
495 got_ref_get_name(ref
));
499 err
= got_ref_change_ref(ref
, &new_id
);
503 err
= got_ref_write(ref
, repo
);
512 if (err
->code
== GOT_ERR_LOCKFILE_TIMEOUT
) {
513 err
= got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT
,
514 "could not acquire exclusive file lock for %s",
517 send_ref_update_ng(client
, &iref
, refname
, err
->msg
);
519 send_ref_update_ok(client
, &iref
, refname
);
521 if (client
->nref_updates
> 0) {
522 client
->nref_updates
--;
523 if (client
->nref_updates
== 0) {
524 send_refs_updated(client
);
530 const struct got_error
*unlock_err
;
531 unlock_err
= got_ref_unlock(ref
);
532 if (unlock_err
&& err
== NULL
)
538 got_object_close(obj
);
540 got_repo_close(repo
);
547 session_dispatch_repo_child(int fd
, short event
, void *arg
)
549 struct gotd_imsgev
*iev
= arg
;
550 struct imsgbuf
*ibuf
= &iev
->ibuf
;
551 struct gotd_session_client
*client
= &gotd_session_client
;
556 if (event
& EV_READ
) {
557 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
558 fatal("imsg_read error");
560 /* Connection closed. */
566 if (event
& EV_WRITE
) {
567 n
= msgbuf_write(&ibuf
->w
);
568 if (n
== -1 && errno
!= EAGAIN
)
569 fatal("msgbuf_write");
571 /* Connection closed. */
578 const struct got_error
*err
= NULL
;
579 uint32_t client_id
= 0;
580 int do_disconnect
= 0;
581 int do_ref_updates
= 0, do_ref_update
= 0;
582 int do_packfile_install
= 0;
584 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
585 fatal("%s: imsg_get error", __func__
);
586 if (n
== 0) /* No more messages. */
589 switch (imsg
.hdr
.type
) {
590 case GOTD_IMSG_ERROR
:
592 err
= gotd_imsg_recv_error(&client_id
, &imsg
);
594 case GOTD_IMSG_PACKFILE_DONE
:
596 err
= recv_packfile_done(&client_id
, &imsg
);
598 case GOTD_IMSG_PACKFILE_INSTALL
:
599 err
= recv_packfile_install(&client_id
, &imsg
);
601 do_packfile_install
= 1;
603 case GOTD_IMSG_REF_UPDATES_START
:
604 err
= recv_ref_updates_start(&client_id
, &imsg
);
608 case GOTD_IMSG_REF_UPDATE
:
609 err
= recv_ref_update(&client_id
, &imsg
);
614 log_debug("unexpected imsg %d", imsg
.hdr
.type
);
620 disconnect_on_error(client
, err
);
624 if (do_packfile_install
)
625 err
= install_pack(client
,
626 gotd_session
.repo
->path
, &imsg
);
627 else if (do_ref_updates
)
628 err
= begin_ref_updates(client
, &imsg
);
629 else if (do_ref_update
)
630 err
= update_ref(&shut
, client
,
631 gotd_session
.repo
->path
, &imsg
);
633 log_warnx("uid %d: %s", client
->euid
, err
->msg
);
639 gotd_imsg_event_add(iev
);
641 /* This pipe is dead. Remove its event handler */
643 event_loopexit(NULL
);
647 static const struct got_error
*
648 recv_capabilities(struct gotd_session_client
*client
, struct imsg
*imsg
)
650 struct gotd_imsg_capabilities icapas
;
653 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
654 if (datalen
!= sizeof(icapas
))
655 return got_error(GOT_ERR_PRIVSEP_LEN
);
656 memcpy(&icapas
, imsg
->data
, sizeof(icapas
));
658 client
->ncapa_alloc
= icapas
.ncapabilities
;
659 client
->capabilities
= calloc(client
->ncapa_alloc
,
660 sizeof(*client
->capabilities
));
661 if (client
->capabilities
== NULL
) {
662 client
->ncapa_alloc
= 0;
663 return got_error_from_errno("calloc");
666 log_debug("expecting %zu capabilities from uid %d",
667 client
->ncapa_alloc
, client
->euid
);
671 static const struct got_error
*
672 recv_capability(struct gotd_session_client
*client
, struct imsg
*imsg
)
674 struct gotd_imsg_capability icapa
;
675 struct gotd_client_capability
*capa
;
677 char *key
, *value
= NULL
;
679 if (client
->capabilities
== NULL
||
680 client
->ncapabilities
>= client
->ncapa_alloc
) {
681 return got_error_msg(GOT_ERR_BAD_REQUEST
,
682 "unexpected capability received");
685 memset(&icapa
, 0, sizeof(icapa
));
687 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
688 if (datalen
< sizeof(icapa
))
689 return got_error(GOT_ERR_PRIVSEP_LEN
);
690 memcpy(&icapa
, imsg
->data
, sizeof(icapa
));
692 if (datalen
!= sizeof(icapa
) + icapa
.key_len
+ icapa
.value_len
)
693 return got_error(GOT_ERR_PRIVSEP_LEN
);
695 key
= strndup(imsg
->data
+ sizeof(icapa
), icapa
.key_len
);
697 return got_error_from_errno("strndup");
698 if (icapa
.value_len
> 0) {
699 value
= strndup(imsg
->data
+ sizeof(icapa
) + icapa
.key_len
,
703 return got_error_from_errno("strndup");
707 capa
= &client
->capabilities
[client
->ncapabilities
++];
712 log_debug("uid %d: capability %s=%s", client
->euid
, key
, value
);
714 log_debug("uid %d: capability %s", client
->euid
, key
);
719 static const struct got_error
*
720 ensure_client_is_reading(struct gotd_session_client
*client
)
722 if (client
->is_writing
) {
723 return got_error_fmt(GOT_ERR_BAD_PACKET
,
724 "uid %d made a read-request but is not reading from "
725 "a repository", client
->euid
);
731 static const struct got_error
*
732 ensure_client_is_writing(struct gotd_session_client
*client
)
734 if (!client
->is_writing
) {
735 return got_error_fmt(GOT_ERR_BAD_PACKET
,
736 "uid %d made a write-request but is not writing to "
737 "a repository", client
->euid
);
743 static const struct got_error
*
744 forward_want(struct gotd_session_client
*client
, struct imsg
*imsg
)
746 struct gotd_imsg_want ireq
;
747 struct gotd_imsg_want iwant
;
750 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
751 if (datalen
!= sizeof(ireq
))
752 return got_error(GOT_ERR_PRIVSEP_LEN
);
754 memcpy(&ireq
, imsg
->data
, datalen
);
756 memset(&iwant
, 0, sizeof(iwant
));
757 memcpy(iwant
.object_id
, ireq
.object_id
, SHA1_DIGEST_LENGTH
);
758 iwant
.client_id
= client
->id
;
760 if (gotd_imsg_compose_event(&client
->repo_child_iev
, GOTD_IMSG_WANT
,
761 PROC_SESSION
, -1, &iwant
, sizeof(iwant
)) == -1)
762 return got_error_from_errno("imsg compose WANT");
767 static const struct got_error
*
768 forward_ref_update(struct gotd_session_client
*client
, struct imsg
*imsg
)
770 const struct got_error
*err
= NULL
;
771 struct gotd_imsg_ref_update ireq
;
772 struct gotd_imsg_ref_update
*iref
= NULL
;
775 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
776 if (datalen
< sizeof(ireq
))
777 return got_error(GOT_ERR_PRIVSEP_LEN
);
778 memcpy(&ireq
, imsg
->data
, sizeof(ireq
));
779 if (datalen
!= sizeof(ireq
) + ireq
.name_len
)
780 return got_error(GOT_ERR_PRIVSEP_LEN
);
782 iref
= malloc(datalen
);
784 return got_error_from_errno("malloc");
785 memcpy(iref
, imsg
->data
, datalen
);
787 iref
->client_id
= client
->id
;
788 if (gotd_imsg_compose_event(&client
->repo_child_iev
,
789 GOTD_IMSG_REF_UPDATE
, PROC_SESSION
, -1, iref
, datalen
) == -1)
790 err
= got_error_from_errno("imsg compose REF_UPDATE");
795 static const struct got_error
*
796 forward_have(struct gotd_session_client
*client
, struct imsg
*imsg
)
798 struct gotd_imsg_have ireq
;
799 struct gotd_imsg_have ihave
;
802 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
803 if (datalen
!= sizeof(ireq
))
804 return got_error(GOT_ERR_PRIVSEP_LEN
);
806 memcpy(&ireq
, imsg
->data
, datalen
);
808 memset(&ihave
, 0, sizeof(ihave
));
809 memcpy(ihave
.object_id
, ireq
.object_id
, SHA1_DIGEST_LENGTH
);
810 ihave
.client_id
= client
->id
;
812 if (gotd_imsg_compose_event(&client
->repo_child_iev
, GOTD_IMSG_HAVE
,
813 PROC_SESSION
, -1, &ihave
, sizeof(ihave
)) == -1)
814 return got_error_from_errno("imsg compose HAVE");
820 client_has_capability(struct gotd_session_client
*client
, const char *capastr
)
822 struct gotd_client_capability
*capa
;
825 if (client
->ncapabilities
== 0)
828 for (i
= 0; i
< client
->ncapabilities
; i
++) {
829 capa
= &client
->capabilities
[i
];
830 if (strcmp(capa
->key
, capastr
) == 0)
837 static const struct got_error
*
838 recv_packfile(struct gotd_session_client
*client
)
840 const struct got_error
*err
= NULL
;
841 struct gotd_imsg_recv_packfile ipack
;
842 struct gotd_imsg_packfile_pipe ipipe
;
843 struct gotd_imsg_packidx_file ifile
;
844 char *basepath
= NULL
, *pack_path
= NULL
, *idx_path
= NULL
;
845 int packfd
= -1, idxfd
= -1;
846 int pipe
[2] = { -1, -1 };
848 if (client
->packfile_path
) {
849 return got_error_fmt(GOT_ERR_PRIVSEP_MSG
,
850 "uid %d already has a pack file", client
->euid
);
853 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, pipe
) == -1)
854 return got_error_from_errno("socketpair");
856 memset(&ipipe
, 0, sizeof(ipipe
));
857 ipipe
.client_id
= client
->id
;
859 /* Send pack pipe end 0 to repo child process. */
860 if (gotd_imsg_compose_event(&client
->repo_child_iev
,
861 GOTD_IMSG_PACKFILE_PIPE
, PROC_SESSION
, pipe
[0],
862 &ipipe
, sizeof(ipipe
)) == -1) {
863 err
= got_error_from_errno("imsg compose PACKFILE_PIPE");
869 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
870 if (gotd_imsg_compose_event(&client
->iev
,
871 GOTD_IMSG_PACKFILE_PIPE
, PROC_SESSION
, pipe
[1], NULL
, 0) == -1)
872 err
= got_error_from_errno("imsg compose PACKFILE_PIPE");
875 if (asprintf(&basepath
, "%s/%s/receiving-from-uid-%d.pack",
876 got_repo_get_path(gotd_session
.repo
), GOT_OBJECTS_PACK_DIR
,
877 client
->euid
) == -1) {
878 err
= got_error_from_errno("asprintf");
882 err
= got_opentemp_named_fd(&pack_path
, &packfd
, basepath
, "");
885 if (fchmod(packfd
, GOT_DEFAULT_PACK_MODE
) == -1) {
886 err
= got_error_from_errno2("fchmod", pack_path
);
891 if (asprintf(&basepath
, "%s/%s/receiving-from-uid-%d.idx",
892 got_repo_get_path(gotd_session
.repo
), GOT_OBJECTS_PACK_DIR
,
893 client
->euid
) == -1) {
894 err
= got_error_from_errno("asprintf");
898 err
= got_opentemp_named_fd(&idx_path
, &idxfd
, basepath
, "");
901 if (fchmod(idxfd
, GOT_DEFAULT_PACK_MODE
) == -1) {
902 err
= got_error_from_errno2("fchmod", idx_path
);
906 memset(&ifile
, 0, sizeof(ifile
));
907 ifile
.client_id
= client
->id
;
908 if (gotd_imsg_compose_event(&client
->repo_child_iev
,
909 GOTD_IMSG_PACKIDX_FILE
, PROC_SESSION
,
910 idxfd
, &ifile
, sizeof(ifile
)) == -1) {
911 err
= got_error_from_errno("imsg compose PACKIDX_FILE");
917 memset(&ipack
, 0, sizeof(ipack
));
918 ipack
.client_id
= client
->id
;
919 if (client_has_capability(client
, GOT_CAPA_REPORT_STATUS
))
920 ipack
.report_status
= 1;
922 if (gotd_imsg_compose_event(&client
->repo_child_iev
,
923 GOTD_IMSG_RECV_PACKFILE
, PROC_SESSION
, packfd
,
924 &ipack
, sizeof(ipack
)) == -1) {
925 err
= got_error_from_errno("imsg compose RECV_PACKFILE");
933 if (pipe
[0] != -1 && close(pipe
[0]) == -1 && err
== NULL
)
934 err
= got_error_from_errno("close");
935 if (pipe
[1] != -1 && close(pipe
[1]) == -1 && err
== NULL
)
936 err
= got_error_from_errno("close");
937 if (packfd
!= -1 && close(packfd
) == -1 && err
== NULL
)
938 err
= got_error_from_errno("close");
939 if (idxfd
!= -1 && close(idxfd
) == -1 && err
== NULL
)
940 err
= got_error_from_errno("close");
945 client
->packfile_path
= pack_path
;
946 client
->packidx_path
= idx_path
;
951 static const struct got_error
*
952 send_packfile(struct gotd_session_client
*client
)
954 const struct got_error
*err
= NULL
;
955 struct gotd_imsg_send_packfile ipack
;
956 struct gotd_imsg_packfile_pipe ipipe
;
959 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, pipe
) == -1)
960 return got_error_from_errno("socketpair");
962 memset(&ipack
, 0, sizeof(ipack
));
963 memset(&ipipe
, 0, sizeof(ipipe
));
965 ipack
.client_id
= client
->id
;
966 if (client_has_capability(client
, GOT_CAPA_SIDE_BAND_64K
))
967 ipack
.report_progress
= 1;
969 client
->delta_cache_fd
= got_opentempfd();
970 if (client
->delta_cache_fd
== -1)
971 return got_error_from_errno("got_opentempfd");
973 if (gotd_imsg_compose_event(&client
->repo_child_iev
,
974 GOTD_IMSG_SEND_PACKFILE
, PROC_GOTD
, client
->delta_cache_fd
,
975 &ipack
, sizeof(ipack
)) == -1) {
976 err
= got_error_from_errno("imsg compose SEND_PACKFILE");
982 ipipe
.client_id
= client
->id
;
984 /* Send pack pipe end 0 to repo child process. */
985 if (gotd_imsg_compose_event(&client
->repo_child_iev
,
986 GOTD_IMSG_PACKFILE_PIPE
, PROC_GOTD
,
987 pipe
[0], &ipipe
, sizeof(ipipe
)) == -1) {
988 err
= got_error_from_errno("imsg compose PACKFILE_PIPE");
993 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
994 if (gotd_imsg_compose_event(&client
->iev
,
995 GOTD_IMSG_PACKFILE_PIPE
, PROC_GOTD
, pipe
[1], NULL
, 0) == -1)
996 err
= got_error_from_errno("imsg compose PACKFILE_PIPE");
1002 session_dispatch_client(int fd
, short events
, void *arg
)
1004 struct gotd_imsgev
*iev
= arg
;
1005 struct imsgbuf
*ibuf
= &iev
->ibuf
;
1006 struct gotd_session_client
*client
= &gotd_session_client
;
1007 const struct got_error
*err
= NULL
;
1011 if (events
& EV_WRITE
) {
1012 while (ibuf
->w
.queued
) {
1013 n
= msgbuf_write(&ibuf
->w
);
1014 if (n
== -1 && errno
== EPIPE
) {
1016 * The client has closed its socket.
1017 * This can happen when Git clients are
1018 * done sending pack file data.
1020 msgbuf_clear(&ibuf
->w
);
1022 } else if (n
== -1 && errno
!= EAGAIN
) {
1023 err
= got_error_from_errno("imsg_flush");
1024 disconnect_on_error(client
, err
);
1028 /* Connection closed. */
1029 err
= got_error(GOT_ERR_EOF
);
1030 disconnect_on_error(client
, err
);
1036 if ((events
& EV_READ
) == 0)
1039 memset(&imsg
, 0, sizeof(imsg
));
1041 while (err
== NULL
) {
1042 err
= gotd_imsg_recv(&imsg
, ibuf
, 0);
1044 if (err
->code
== GOT_ERR_PRIVSEP_READ
)
1049 evtimer_del(&client
->tmo
);
1051 switch (imsg
.hdr
.type
) {
1052 case GOTD_IMSG_CAPABILITIES
:
1053 if (client
->state
!= GOTD_STATE_EXPECT_CAPABILITIES
) {
1054 err
= got_error_msg(GOT_ERR_BAD_REQUEST
,
1055 "unexpected capabilities received");
1058 log_debug("receiving capabilities from uid %d",
1060 err
= recv_capabilities(client
, &imsg
);
1062 case GOTD_IMSG_CAPABILITY
:
1063 if (client
->state
!= GOTD_STATE_EXPECT_CAPABILITIES
) {
1064 err
= got_error_msg(GOT_ERR_BAD_REQUEST
,
1065 "unexpected capability received");
1068 err
= recv_capability(client
, &imsg
);
1069 if (err
|| client
->ncapabilities
< client
->ncapa_alloc
)
1071 if (!client
->is_writing
) {
1072 client
->state
= GOTD_STATE_EXPECT_WANT
;
1073 client
->accept_flush_pkt
= 1;
1074 log_debug("uid %d: expecting want-lines",
1076 } else if (client
->is_writing
) {
1077 client
->state
= GOTD_STATE_EXPECT_REF_UPDATE
;
1078 client
->accept_flush_pkt
= 1;
1079 log_debug("uid %d: expecting ref-update-lines",
1082 fatalx("client %d is both reading and writing",
1085 case GOTD_IMSG_WANT
:
1086 if (client
->state
!= GOTD_STATE_EXPECT_WANT
) {
1087 err
= got_error_msg(GOT_ERR_BAD_REQUEST
,
1088 "unexpected want-line received");
1091 log_debug("received want-line from uid %d",
1093 err
= ensure_client_is_reading(client
);
1096 client
->accept_flush_pkt
= 1;
1097 err
= forward_want(client
, &imsg
);
1099 case GOTD_IMSG_REF_UPDATE
:
1100 if (client
->state
!= GOTD_STATE_EXPECT_REF_UPDATE
&&
1102 GOTD_STATE_EXPECT_MORE_REF_UPDATES
) {
1103 err
= got_error_msg(GOT_ERR_BAD_REQUEST
,
1104 "unexpected ref-update-line received");
1107 log_debug("received ref-update-line from uid %d",
1109 err
= ensure_client_is_writing(client
);
1112 err
= forward_ref_update(client
, &imsg
);
1115 client
->state
= GOTD_STATE_EXPECT_MORE_REF_UPDATES
;
1116 client
->accept_flush_pkt
= 1;
1118 case GOTD_IMSG_HAVE
:
1119 if (client
->state
!= GOTD_STATE_EXPECT_HAVE
) {
1120 err
= got_error_msg(GOT_ERR_BAD_REQUEST
,
1121 "unexpected have-line received");
1124 log_debug("received have-line from uid %d",
1126 err
= ensure_client_is_reading(client
);
1129 err
= forward_have(client
, &imsg
);
1132 client
->accept_flush_pkt
= 1;
1134 case GOTD_IMSG_FLUSH
:
1135 if (client
->state
== GOTD_STATE_EXPECT_WANT
||
1136 client
->state
== GOTD_STATE_EXPECT_HAVE
) {
1137 err
= ensure_client_is_reading(client
);
1140 } else if (client
->state
==
1141 GOTD_STATE_EXPECT_MORE_REF_UPDATES
) {
1142 err
= ensure_client_is_writing(client
);
1145 } else if (client
->state
!= GOTD_STATE_EXPECT_DONE
) {
1146 err
= got_error_msg(GOT_ERR_BAD_REQUEST
,
1147 "unexpected flush-pkt received");
1150 if (!client
->accept_flush_pkt
) {
1151 err
= got_error_msg(GOT_ERR_BAD_REQUEST
,
1152 "unexpected flush-pkt received");
1157 * Accept just one flush packet at a time.
1158 * Future client state transitions will set this flag
1159 * again if another flush packet is expected.
1161 client
->accept_flush_pkt
= 0;
1163 log_debug("received flush-pkt from uid %d",
1165 if (client
->state
== GOTD_STATE_EXPECT_WANT
) {
1166 client
->state
= GOTD_STATE_EXPECT_HAVE
;
1167 log_debug("uid %d: expecting have-lines",
1169 } else if (client
->state
== GOTD_STATE_EXPECT_HAVE
) {
1170 client
->state
= GOTD_STATE_EXPECT_DONE
;
1171 client
->accept_flush_pkt
= 1;
1172 log_debug("uid %d: expecting 'done'",
1174 } else if (client
->state
==
1175 GOTD_STATE_EXPECT_MORE_REF_UPDATES
) {
1176 client
->state
= GOTD_STATE_EXPECT_PACKFILE
;
1177 log_debug("uid %d: expecting packfile",
1179 err
= recv_packfile(client
);
1180 } else if (client
->state
!= GOTD_STATE_EXPECT_DONE
) {
1181 /* should not happen, see above */
1182 err
= got_error_msg(GOT_ERR_BAD_REQUEST
,
1183 "unexpected client state");
1187 case GOTD_IMSG_DONE
:
1188 if (client
->state
!= GOTD_STATE_EXPECT_HAVE
&&
1189 client
->state
!= GOTD_STATE_EXPECT_DONE
) {
1190 err
= got_error_msg(GOT_ERR_BAD_REQUEST
,
1191 "unexpected flush-pkt received");
1194 log_debug("received 'done' from uid %d", client
->euid
);
1195 err
= ensure_client_is_reading(client
);
1198 client
->state
= GOTD_STATE_DONE
;
1199 client
->accept_flush_pkt
= 1;
1200 err
= send_packfile(client
);
1203 log_debug("unexpected imsg %d", imsg
.hdr
.type
);
1204 err
= got_error(GOT_ERR_PRIVSEP_MSG
);
1212 if (err
->code
!= GOT_ERR_EOF
||
1213 client
->state
!= GOTD_STATE_EXPECT_PACKFILE
)
1214 disconnect_on_error(client
, err
);
1216 gotd_imsg_event_add(iev
);
1217 evtimer_add(&client
->tmo
, &gotd_session
.request_timeout
);
1221 static const struct got_error
*
1222 list_refs_request(void)
1224 static const struct got_error
*err
;
1225 struct gotd_session_client
*client
= &gotd_session_client
;
1226 struct gotd_imsgev
*iev
= &client
->repo_child_iev
;
1227 struct gotd_imsg_list_refs_internal ilref
;
1230 if (client
->state
!= GOTD_STATE_EXPECT_LIST_REFS
)
1231 return got_error(GOT_ERR_PRIVSEP_MSG
);
1233 memset(&ilref
, 0, sizeof(ilref
));
1234 ilref
.client_id
= client
->id
;
1236 fd
= dup(client
->fd
);
1238 return got_error_from_errno("dup");
1240 if (gotd_imsg_compose_event(iev
, GOTD_IMSG_LIST_REFS_INTERNAL
,
1241 PROC_SESSION
, fd
, &ilref
, sizeof(ilref
)) == -1) {
1242 err
= got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1247 client
->state
= GOTD_STATE_EXPECT_CAPABILITIES
;
1248 log_debug("uid %d: expecting capabilities", client
->euid
);
1252 static const struct got_error
*
1253 recv_connect(struct imsg
*imsg
)
1255 struct gotd_session_client
*client
= &gotd_session_client
;
1256 struct gotd_imsg_connect iconnect
;
1259 if (client
->state
!= GOTD_STATE_EXPECT_LIST_REFS
)
1260 return got_error(GOT_ERR_PRIVSEP_MSG
);
1262 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
1263 if (datalen
!= sizeof(iconnect
))
1264 return got_error(GOT_ERR_PRIVSEP_LEN
);
1265 memcpy(&iconnect
, imsg
->data
, sizeof(iconnect
));
1268 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
1270 client
->fd
= imsg
->fd
;
1271 client
->euid
= iconnect
.euid
;
1272 client
->egid
= iconnect
.egid
;
1274 imsg_init(&client
->iev
.ibuf
, client
->fd
);
1275 client
->iev
.handler
= session_dispatch_client
;
1276 client
->iev
.events
= EV_READ
;
1277 client
->iev
.handler_arg
= NULL
;
1278 event_set(&client
->iev
.ev
, client
->iev
.ibuf
.fd
, EV_READ
,
1279 session_dispatch_client
, &client
->iev
);
1280 gotd_imsg_event_add(&client
->iev
);
1281 evtimer_set(&client
->tmo
, gotd_request_timeout
, client
);
1286 static const struct got_error
*
1287 recv_repo_child(struct imsg
*imsg
)
1289 struct gotd_imsg_connect_repo_child ichild
;
1290 struct gotd_session_client
*client
= &gotd_session_client
;
1293 if (client
->state
!= GOTD_STATE_EXPECT_LIST_REFS
)
1294 return got_error(GOT_ERR_PRIVSEP_MSG
);
1296 /* We should already have received a pipe to the listener. */
1297 if (client
->fd
== -1)
1298 return got_error(GOT_ERR_PRIVSEP_MSG
);
1300 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
1301 if (datalen
!= sizeof(ichild
))
1302 return got_error(GOT_ERR_PRIVSEP_LEN
);
1304 memcpy(&ichild
, imsg
->data
, sizeof(ichild
));
1306 client
->id
= ichild
.client_id
;
1307 if (ichild
.proc_id
== PROC_REPO_WRITE
)
1308 client
->is_writing
= 1;
1309 else if (ichild
.proc_id
== PROC_REPO_READ
)
1310 client
->is_writing
= 0;
1312 return got_error_msg(GOT_ERR_PRIVSEP_MSG
,
1313 "bad child process type");
1316 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
1318 imsg_init(&client
->repo_child_iev
.ibuf
, imsg
->fd
);
1319 client
->repo_child_iev
.handler
= session_dispatch_repo_child
;
1320 client
->repo_child_iev
.events
= EV_READ
;
1321 client
->repo_child_iev
.handler_arg
= NULL
;
1322 event_set(&client
->repo_child_iev
.ev
, client
->repo_child_iev
.ibuf
.fd
,
1323 EV_READ
, session_dispatch_repo_child
, &client
->repo_child_iev
);
1324 gotd_imsg_event_add(&client
->repo_child_iev
);
1326 /* The "recvfd" pledge promise is no longer needed. */
1327 if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL
) == -1)
1334 session_dispatch(int fd
, short event
, void *arg
)
1336 struct gotd_imsgev
*iev
= arg
;
1337 struct imsgbuf
*ibuf
= &iev
->ibuf
;
1338 struct gotd_session_client
*client
= &gotd_session_client
;
1343 if (event
& EV_READ
) {
1344 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
1345 fatal("imsg_read error");
1347 /* Connection closed. */
1353 if (event
& EV_WRITE
) {
1354 n
= msgbuf_write(&ibuf
->w
);
1355 if (n
== -1 && errno
!= EAGAIN
)
1356 fatal("msgbuf_write");
1358 /* Connection closed. */
1365 const struct got_error
*err
= NULL
;
1366 uint32_t client_id
= 0;
1367 int do_disconnect
= 0, do_list_refs
= 0;
1369 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
1370 fatal("%s: imsg_get error", __func__
);
1371 if (n
== 0) /* No more messages. */
1374 switch (imsg
.hdr
.type
) {
1375 case GOTD_IMSG_ERROR
:
1377 err
= gotd_imsg_recv_error(&client_id
, &imsg
);
1379 case GOTD_IMSG_CONNECT
:
1380 err
= recv_connect(&imsg
);
1382 case GOTD_IMSG_DISCONNECT
:
1385 case GOTD_IMSG_CONNECT_REPO_CHILD
:
1386 err
= recv_repo_child(&imsg
);
1392 log_debug("unexpected imsg %d", imsg
.hdr
.type
);
1397 if (do_disconnect
) {
1399 disconnect_on_error(client
, err
);
1402 } else if (do_list_refs
)
1403 err
= list_refs_request();
1406 log_warnx("uid %d: %s", client
->euid
, err
->msg
);
1410 gotd_imsg_event_add(iev
);
1412 /* This pipe is dead. Remove its event handler */
1413 event_del(&iev
->ev
);
1414 event_loopexit(NULL
);
1419 session_main(const char *title
, const char *repo_path
,
1420 int *pack_fds
, int *temp_fds
, struct timeval
*request_timeout
)
1422 const struct got_error
*err
= NULL
;
1423 struct event evsigint
, evsigterm
, evsighup
, evsigusr1
;
1425 gotd_session
.title
= title
;
1426 gotd_session
.pid
= getpid();
1427 gotd_session
.pack_fds
= pack_fds
;
1428 gotd_session
.temp_fds
= temp_fds
;
1429 memcpy(&gotd_session
.request_timeout
, request_timeout
,
1430 sizeof(gotd_session
.request_timeout
));
1432 err
= got_repo_open(&gotd_session
.repo
, repo_path
, NULL
, pack_fds
);
1435 if (!got_repo_is_bare(gotd_session
.repo
)) {
1436 err
= got_error_msg(GOT_ERR_NOT_GIT_REPO
,
1437 "bare git repository required");
1441 got_repo_temp_fds_set(gotd_session
.repo
, temp_fds
);
1443 signal_set(&evsigint
, SIGINT
, gotd_session_sighdlr
, NULL
);
1444 signal_set(&evsigterm
, SIGTERM
, gotd_session_sighdlr
, NULL
);
1445 signal_set(&evsighup
, SIGHUP
, gotd_session_sighdlr
, NULL
);
1446 signal_set(&evsigusr1
, SIGUSR1
, gotd_session_sighdlr
, NULL
);
1447 signal(SIGPIPE
, SIG_IGN
);
1449 signal_add(&evsigint
, NULL
);
1450 signal_add(&evsigterm
, NULL
);
1451 signal_add(&evsighup
, NULL
);
1452 signal_add(&evsigusr1
, NULL
);
1454 gotd_session_client
.state
= GOTD_STATE_EXPECT_LIST_REFS
;
1455 gotd_session_client
.fd
= -1;
1456 gotd_session_client
.nref_updates
= -1;
1457 gotd_session_client
.delta_cache_fd
= -1;
1458 gotd_session_client
.accept_flush_pkt
= 1;
1460 imsg_init(&gotd_session
.parent_iev
.ibuf
, GOTD_FILENO_MSG_PIPE
);
1461 gotd_session
.parent_iev
.handler
= session_dispatch
;
1462 gotd_session
.parent_iev
.events
= EV_READ
;
1463 gotd_session
.parent_iev
.handler_arg
= NULL
;
1464 event_set(&gotd_session
.parent_iev
.ev
, gotd_session
.parent_iev
.ibuf
.fd
,
1465 EV_READ
, session_dispatch
, &gotd_session
.parent_iev
);
1466 if (gotd_imsg_compose_event(&gotd_session
.parent_iev
,
1467 GOTD_IMSG_CLIENT_SESSION_READY
, PROC_SESSION
, -1, NULL
, 0) == -1) {
1468 err
= got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1475 log_warnx("%s: %s", title
, err
->msg
);
1476 gotd_session_shutdown();
1480 gotd_session_shutdown(void)
1482 log_debug("shutting down");
1483 if (gotd_session
.repo
)
1484 got_repo_close(gotd_session
.repo
);
1485 got_repo_pack_fds_close(gotd_session
.pack_fds
);
1486 got_repo_temp_fds_close(gotd_session
.temp_fds
);