Blob


1 /*
2 * Copyright (c) 2018 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_tag_object(struct got_tag_object **tag, FILE *f,
52 struct got_object_id *expected_id)
53 {
54 const struct got_error *err = NULL;
55 struct got_object *obj = 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 /* Skip object header. */
90 len -= obj->hdrlen;
91 err = got_object_parse_tag(tag, p + obj->hdrlen, len);
92 done:
93 free(p);
94 if (obj)
95 got_object_close(obj);
96 return err;
97 }
99 int
100 main(int argc, char *argv[])
102 const struct got_error *err = NULL;
103 struct imsgbuf ibuf;
104 size_t datalen;
106 signal(SIGINT, catch_sigint);
108 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
110 #ifndef PROFILE
111 /* revoke access to most system calls */
112 if (pledge("stdio recvfd", NULL) == -1) {
113 err = got_error_from_errno("pledge");
114 got_privsep_send_error(&ibuf, err);
115 return 1;
117 #endif
119 for (;;) {
120 struct imsg imsg;
121 FILE *f = NULL;
122 struct got_tag_object *tag = NULL;
123 struct got_object_id expected_id;
125 if (sigint_received) {
126 err = got_error(GOT_ERR_CANCELLED);
127 break;
130 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
131 if (err) {
132 if (err->code == GOT_ERR_PRIVSEP_PIPE)
133 err = NULL;
134 break;
137 if (imsg.hdr.type == GOT_IMSG_STOP)
138 break;
140 if (imsg.hdr.type != GOT_IMSG_TAG_REQUEST) {
141 err = got_error(GOT_ERR_PRIVSEP_MSG);
142 goto done;
145 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
146 if (datalen != sizeof(expected_id)) {
147 err = got_error(GOT_ERR_PRIVSEP_LEN);
148 goto done;
150 memcpy(&expected_id, imsg.data, sizeof(expected_id));
152 if (imsg.fd == -1) {
153 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
154 goto done;
157 /* Always assume file offset zero. */
158 f = fdopen(imsg.fd, "rb");
159 if (f == NULL) {
160 err = got_error_from_errno("fdopen");
161 goto done;
164 err = read_tag_object(&tag, f, &expected_id);
165 if (err)
166 goto done;
168 err = got_privsep_send_tag(&ibuf, tag);
169 done:
170 if (f) {
171 if (fclose(f) == EOF && err == NULL)
172 err = got_error_from_errno("fclose");
173 } else if (imsg.fd != -1) {
174 if (close(imsg.fd) == -1 && err == NULL)
175 err = got_error_from_errno("close");
177 imsg_free(&imsg);
178 if (err)
179 break;
182 imsg_clear(&ibuf);
183 if (err) {
184 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
185 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
186 got_privsep_send_error(&ibuf, err);
189 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
190 err = got_error_from_errno("close");
191 return err ? 1 : 0;