Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 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/uio.h>
19 #include <sys/time.h>
21 #include <stdint.h>
22 #include <limits.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <zlib.h>
30 #include "got_compat.h"
32 #include "got_error.h"
33 #include "got_object.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_inflate.h"
37 #include "got_lib_object.h"
38 #include "got_lib_object_parse.h"
39 #include "got_lib_privsep.h"
40 #include "got_lib_sha1.h"
42 static volatile sig_atomic_t sigint_received;
44 static void
45 catch_sigint(int signo)
46 {
47 sigint_received = 1;
48 }
50 static const struct got_error *
51 read_commit_object(struct got_commit_object **commit, FILE *f,
52 struct got_object_id *expected_id)
53 {
54 struct got_object *obj = NULL;
55 const struct got_error *err = NULL;
56 size_t len;
57 uint8_t *p;
58 struct got_inflate_checksum csum;
59 SHA1_CTX sha1_ctx;
60 struct got_object_id id;
62 SHA1Init(&sha1_ctx);
63 memset(&csum, 0, sizeof(csum));
64 csum.output_sha1 = &sha1_ctx;
66 err = got_inflate_to_mem(&p, &len, NULL, &csum, f);
67 if (err)
68 return err;
70 SHA1Final(id.sha1, &sha1_ctx);
71 if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
72 char buf[SHA1_DIGEST_STRING_LENGTH];
73 err = got_error_fmt(GOT_ERR_OBJ_CSUM,
74 "checksum failure for object %s",
75 got_sha1_digest_to_str(expected_id->sha1, buf,
76 sizeof(buf)));
77 goto done;
78 }
80 err = got_object_parse_header(&obj, p, len);
81 if (err)
82 goto done;
84 if (len < obj->hdrlen + obj->size) {
85 err = got_error(GOT_ERR_BAD_OBJ_DATA);
86 goto done;
87 }
89 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
90 err = got_error(GOT_ERR_OBJ_TYPE);
91 goto done;
92 }
94 /* Skip object header. */
95 len -= obj->hdrlen;
96 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
97 done:
98 free(p);
99 if (obj)
100 got_object_close(obj);
101 return err;
104 int
105 main(int argc, char *argv[])
107 const struct got_error *err = NULL;
108 struct imsgbuf ibuf;
109 size_t datalen;
111 signal(SIGINT, catch_sigint);
113 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
115 #ifndef PROFILE
116 /* revoke access to most system calls */
117 if (pledge("stdio recvfd", NULL) == -1) {
118 err = got_error_from_errno("pledge");
119 got_privsep_send_error(&ibuf, err);
120 return 1;
122 #endif
124 for (;;) {
125 struct imsg imsg;
126 FILE *f = NULL;
127 struct got_commit_object *commit = NULL;
128 struct got_object_id expected_id;
130 if (sigint_received) {
131 err = got_error(GOT_ERR_CANCELLED);
132 break;
135 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
136 if (err) {
137 if (err->code == GOT_ERR_PRIVSEP_PIPE)
138 err = NULL;
139 break;
142 if (imsg.hdr.type == GOT_IMSG_STOP)
143 break;
145 if (imsg.hdr.type != GOT_IMSG_COMMIT_REQUEST) {
146 err = got_error(GOT_ERR_PRIVSEP_MSG);
147 goto done;
150 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
151 if (datalen != sizeof(expected_id)) {
152 err = got_error(GOT_ERR_PRIVSEP_LEN);
153 goto done;
155 memcpy(&expected_id, imsg.data, sizeof(expected_id));
157 if (imsg.fd == -1) {
158 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
159 goto done;
162 /* Always assume file offset zero. */
163 f = fdopen(imsg.fd, "rb");
164 if (f == NULL) {
165 err = got_error_from_errno("fdopen");
166 goto done;
169 err = read_commit_object(&commit, f, &expected_id);
170 if (err)
171 goto done;
173 err = got_privsep_send_commit(&ibuf, commit);
174 got_object_commit_close(commit);
175 done:
176 if (f) {
177 if (fclose(f) == EOF && err == NULL)
178 err = got_error_from_errno("fclose");
179 } else if (imsg.fd != -1) {
180 if (close(imsg.fd) == -1 && err == NULL)
181 err = got_error_from_errno("close");
183 imsg_free(&imsg);
184 if (err)
185 break;
188 imsg_clear(&ibuf);
189 if (err) {
190 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
191 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
192 got_privsep_send_error(&ibuf, err);
195 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
196 err = got_error_from_errno("close");
197 return err ? 1 : 0;