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/queue.h>
19 #include <sys/uio.h>
20 #include <sys/time.h>
21 #include <sys/syslimits.h>
23 #include <stdint.h>
24 #include <imsg.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sha1.h>
31 #include <unistd.h>
32 #include <zlib.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_path.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_inflate.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_parse.h"
42 #include "got_lib_privsep.h"
44 static volatile sig_atomic_t sigint_received;
46 static void
47 catch_sigint(int signo)
48 {
49 sigint_received = 1;
50 }
52 static const struct got_error *
53 read_tree_object(struct got_pathlist_head *entries, int *nentries,
54 uint8_t **p, FILE *f)
55 {
56 const struct got_error *err = NULL;
57 struct got_object *obj;
58 size_t len;
60 err = got_inflate_to_mem(p, &len, NULL, f);
61 if (err)
62 return err;
64 err = got_object_parse_header(&obj, *p, len);
65 if (err)
66 return err;
68 if (len < obj->hdrlen + obj->size) {
69 err = got_error(GOT_ERR_BAD_OBJ_DATA);
70 goto done;
71 }
73 /* Skip object header. */
74 len -= obj->hdrlen;
75 err = got_object_parse_tree(entries, nentries, *p + obj->hdrlen, len);
76 done:
77 got_object_close(obj);
78 return err;
79 }
81 int
82 main(int argc, char *argv[])
83 {
84 const struct got_error *err = NULL;
85 struct imsgbuf ibuf;
87 signal(SIGINT, catch_sigint);
89 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
91 #ifndef PROFILE
92 /* revoke access to most system calls */
93 if (pledge("stdio recvfd", NULL) == -1) {
94 err = got_error_from_errno("pledge");
95 got_privsep_send_error(&ibuf, err);
96 return 1;
97 }
98 #endif
100 for (;;) {
101 struct imsg imsg;
102 FILE *f = NULL;
103 struct got_pathlist_head entries;
104 int nentries = 0;
105 uint8_t *buf = NULL;
107 TAILQ_INIT(&entries);
109 if (sigint_received) {
110 err = got_error(GOT_ERR_CANCELLED);
111 break;
114 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
115 if (err) {
116 if (err->code == GOT_ERR_PRIVSEP_PIPE)
117 err = NULL;
118 break;
121 if (imsg.hdr.type == GOT_IMSG_STOP)
122 break;
124 if (imsg.hdr.type != GOT_IMSG_TREE_REQUEST) {
125 err = got_error(GOT_ERR_PRIVSEP_MSG);
126 goto done;
129 if (imsg.fd == -1) {
130 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
131 goto done;
134 /* Always assume file offset zero. */
135 f = fdopen(imsg.fd, "rb");
136 if (f == NULL) {
137 err = got_error_from_errno("fdopen");
138 goto done;
141 err = read_tree_object(&entries, &nentries, &buf, f);
142 if (err)
143 goto done;
145 err = got_privsep_send_tree(&ibuf, &entries, nentries);
146 done:
147 got_object_parsed_tree_entries_free(&entries);
148 free(buf);
149 if (f) {
150 if (fclose(f) != 0 && err == NULL)
151 err = got_error_from_errno("fclose");
152 } else if (imsg.fd != -1) {
153 if (close(imsg.fd) != 0 && err == NULL)
154 err = got_error_from_errno("close");
156 imsg_free(&imsg);
157 if (err)
158 break;
161 imsg_clear(&ibuf);
162 if (err) {
163 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
164 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
165 got_privsep_send_error(&ibuf, err);
168 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
169 err = got_error_from_errno("close");
170 return err ? 1 : 0;