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/queue.h>
19 #include <sys/uio.h>
20 #include <sys/time.h>
21 #include <sys/limits.h>
22 #include <sys/syslimits.h>
24 #include <stdint.h>
25 #include <imsg.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sha1.h>
31 #include <zlib.h>
33 #include "got_error.h"
34 #include "got_object.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_inflate.h"
38 #include "got_lib_object.h"
39 #include "got_lib_object_parse.h"
40 #include "got_lib_privsep.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, struct got_object *obj,
52 FILE *f)
53 {
54 const struct got_error *err = NULL;
55 size_t len;
56 uint8_t *p;
58 if (obj->flags & GOT_OBJ_FLAG_PACKED)
59 err = got_read_file_to_mem(&p, &len, f);
60 else
61 err = got_inflate_to_mem(&p, &len, f);
62 if (err)
63 return err;
65 if (len < obj->hdrlen + obj->size) {
66 err = got_error(GOT_ERR_BAD_OBJ_DATA);
67 goto done;
68 }
70 /* Skip object header. */
71 len -= obj->hdrlen;
72 err = got_object_parse_commit(commit, p + obj->hdrlen, len);
73 free(p);
74 done:
75 return err;
76 }
78 int
79 main(int argc, char *argv[])
80 {
81 const struct got_error *err = NULL;
82 struct got_commit_object *commit = NULL;
83 struct imsgbuf ibuf;
84 size_t datalen;
86 signal(SIGINT, catch_sigint);
88 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
90 #ifndef PROFILE
91 /* revoke access to most system calls */
92 if (pledge("stdio recvfd", NULL) == -1) {
93 err = got_error_from_errno();
94 got_privsep_send_error(&ibuf, err);
95 return 1;
96 }
97 #endif
99 while (1) {
100 struct imsg imsg;
101 struct got_imsg_object iobj;
102 FILE *f = NULL;
103 struct got_object *obj = NULL;
105 if (sigint_received) {
106 err = got_error(GOT_ERR_CANCELLED);
107 break;
110 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
111 if (err) {
112 if (err->code == GOT_ERR_PRIVSEP_PIPE)
113 err = NULL;
114 break;
117 if (imsg.hdr.type == GOT_IMSG_STOP)
118 break;
120 if (imsg.hdr.type != GOT_IMSG_COMMIT_REQUEST) {
121 err = got_error(GOT_ERR_PRIVSEP_MSG);
122 goto done;
125 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
126 if (datalen != sizeof(iobj)) {
127 err = got_error(GOT_ERR_PRIVSEP_LEN);
128 goto done;
131 memcpy(&iobj, imsg.data, sizeof(iobj));
132 if (iobj.type != GOT_OBJ_TYPE_COMMIT) {
133 err = got_error(GOT_ERR_OBJ_TYPE);
134 goto done;
137 if (imsg.fd == -1) {
138 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
139 goto done;
142 obj = calloc(1, sizeof(*obj));
143 if (obj == NULL) {
144 err = got_error_from_errno();
145 goto done;
147 obj->type = iobj.type;
148 obj->hdrlen = iobj.hdrlen;
149 obj->size = iobj.size;
151 /* Always assume file offset zero. */
152 f = fdopen(imsg.fd, "rb");
153 if (f == NULL) {
154 err = got_error_from_errno();
155 goto done;
158 err = read_commit_object(&commit, obj, f);
159 if (err)
160 goto done;
162 err = got_privsep_send_commit(&ibuf, commit);
163 done:
164 if (f)
165 fclose(f);
166 else if (imsg.fd != -1)
167 close(imsg.fd);
168 imsg_free(&imsg);
169 if (obj)
170 got_object_close(obj);
171 if (err)
172 break;
175 imsg_clear(&ibuf);
176 if (err) {
177 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
178 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
179 got_privsep_send_error(&ibuf, err);
182 close(GOT_IMSG_FD_CHILD);
183 return err ? 1 : 0;