Blame


1 257add31 2020-09-09 stsp /*
2 257add31 2020-09-09 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
3 257add31 2020-09-09 stsp *
4 257add31 2020-09-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 257add31 2020-09-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 257add31 2020-09-09 stsp * copyright notice and this permission notice appear in all copies.
7 257add31 2020-09-09 stsp *
8 257add31 2020-09-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 257add31 2020-09-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 257add31 2020-09-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 257add31 2020-09-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 257add31 2020-09-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 257add31 2020-09-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 257add31 2020-09-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 257add31 2020-09-09 stsp */
16 257add31 2020-09-09 stsp
17 257add31 2020-09-09 stsp #include <sys/types.h>
18 257add31 2020-09-09 stsp #include <sys/queue.h>
19 257add31 2020-09-09 stsp #include <sys/uio.h>
20 257add31 2020-09-09 stsp #include <sys/time.h>
21 257add31 2020-09-09 stsp
22 257add31 2020-09-09 stsp #include <stdint.h>
23 257add31 2020-09-09 stsp #include <imsg.h>
24 257add31 2020-09-09 stsp #include <limits.h>
25 257add31 2020-09-09 stsp #include <signal.h>
26 257add31 2020-09-09 stsp #include <stdio.h>
27 257add31 2020-09-09 stsp #include <stdlib.h>
28 257add31 2020-09-09 stsp #include <string.h>
29 257add31 2020-09-09 stsp #include <sha1.h>
30 e12e0e21 2020-09-14 naddy #include <unistd.h>
31 257add31 2020-09-09 stsp #include <zlib.h>
32 257add31 2020-09-09 stsp
33 257add31 2020-09-09 stsp #include "got_error.h"
34 257add31 2020-09-09 stsp #include "got_object.h"
35 257add31 2020-09-09 stsp #include "got_repository.h"
36 257add31 2020-09-09 stsp
37 257add31 2020-09-09 stsp #include "got_lib_delta.h"
38 257add31 2020-09-09 stsp #include "got_lib_object.h"
39 257add31 2020-09-09 stsp #include "got_lib_privsep.h"
40 257add31 2020-09-09 stsp
41 257add31 2020-09-09 stsp #include "gotconfig.h"
42 257add31 2020-09-09 stsp
43 257add31 2020-09-09 stsp /* parse.y */
44 257add31 2020-09-09 stsp static volatile sig_atomic_t sigint_received;
45 257add31 2020-09-09 stsp
46 257add31 2020-09-09 stsp static void
47 257add31 2020-09-09 stsp catch_sigint(int signo)
48 257add31 2020-09-09 stsp {
49 257add31 2020-09-09 stsp sigint_received = 1;
50 257add31 2020-09-09 stsp }
51 257add31 2020-09-09 stsp
52 257add31 2020-09-09 stsp static const struct got_error *
53 257add31 2020-09-09 stsp make_repo_url(char **url, struct gotconfig_remote_repo *repo)
54 257add31 2020-09-09 stsp {
55 257add31 2020-09-09 stsp const struct got_error *err = NULL;
56 257add31 2020-09-09 stsp char *s = NULL, *p = NULL;
57 257add31 2020-09-09 stsp
58 257add31 2020-09-09 stsp *url = NULL;
59 257add31 2020-09-09 stsp
60 257add31 2020-09-09 stsp if (asprintf(&s, "%s://", repo->protocol) == -1)
61 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
62 257add31 2020-09-09 stsp
63 257add31 2020-09-09 stsp if (repo->server) {
64 257add31 2020-09-09 stsp p = s;
65 257add31 2020-09-09 stsp s = NULL;
66 257add31 2020-09-09 stsp if (asprintf(&s, "%s%s", p, repo->server) == -1) {
67 257add31 2020-09-09 stsp err = got_error_from_errno("asprintf");
68 257add31 2020-09-09 stsp goto done;
69 257add31 2020-09-09 stsp }
70 257add31 2020-09-09 stsp free(p);
71 257add31 2020-09-09 stsp p = NULL;
72 257add31 2020-09-09 stsp }
73 257add31 2020-09-09 stsp
74 257add31 2020-09-09 stsp if (repo->port) {
75 257add31 2020-09-09 stsp p = s;
76 257add31 2020-09-09 stsp s = NULL;
77 257add31 2020-09-09 stsp if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
78 257add31 2020-09-09 stsp err = got_error_from_errno("asprintf");
79 257add31 2020-09-09 stsp goto done;
80 257add31 2020-09-09 stsp }
81 257add31 2020-09-09 stsp free(p);
82 257add31 2020-09-09 stsp p = NULL;
83 257add31 2020-09-09 stsp }
84 257add31 2020-09-09 stsp
85 257add31 2020-09-09 stsp if (repo->repository) {
86 257add31 2020-09-09 stsp p = s;
87 257add31 2020-09-09 stsp s = NULL;
88 257add31 2020-09-09 stsp if (asprintf(&s, "%s/%s", p, repo->repository) == -1) {
89 257add31 2020-09-09 stsp err = got_error_from_errno("asprintf");
90 257add31 2020-09-09 stsp goto done;
91 257add31 2020-09-09 stsp }
92 257add31 2020-09-09 stsp free(p);
93 257add31 2020-09-09 stsp p = NULL;
94 257add31 2020-09-09 stsp }
95 257add31 2020-09-09 stsp done:
96 257add31 2020-09-09 stsp if (err) {
97 257add31 2020-09-09 stsp free(s);
98 257add31 2020-09-09 stsp free(p);
99 257add31 2020-09-09 stsp } else
100 257add31 2020-09-09 stsp *url = s;
101 257add31 2020-09-09 stsp return err;
102 257add31 2020-09-09 stsp }
103 257add31 2020-09-09 stsp
104 257add31 2020-09-09 stsp static const struct got_error *
105 257add31 2020-09-09 stsp send_gotconfig_str(struct imsgbuf *ibuf, const char *value)
106 257add31 2020-09-09 stsp {
107 be96c417 2020-09-17 stsp size_t len = value ? strlen(value) : 0;
108 257add31 2020-09-09 stsp
109 257add31 2020-09-09 stsp if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_STR_VAL, 0, 0, -1,
110 257add31 2020-09-09 stsp value, len) == -1)
111 257add31 2020-09-09 stsp return got_error_from_errno("imsg_compose GOTCONFIG_STR_VAL");
112 257add31 2020-09-09 stsp
113 257add31 2020-09-09 stsp return got_privsep_flush_imsg(ibuf);
114 257add31 2020-09-09 stsp }
115 257add31 2020-09-09 stsp
116 257add31 2020-09-09 stsp static const struct got_error *
117 257add31 2020-09-09 stsp send_gotconfig_remotes(struct imsgbuf *ibuf,
118 257add31 2020-09-09 stsp struct gotconfig_remote_repo_list *remotes, int nremotes)
119 257add31 2020-09-09 stsp {
120 257add31 2020-09-09 stsp const struct got_error *err = NULL;
121 257add31 2020-09-09 stsp struct got_imsg_remotes iremotes;
122 257add31 2020-09-09 stsp struct gotconfig_remote_repo *repo;
123 257add31 2020-09-09 stsp char *url = NULL;
124 257add31 2020-09-09 stsp
125 257add31 2020-09-09 stsp iremotes.nremotes = nremotes;
126 257add31 2020-09-09 stsp if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_REMOTES, 0, 0, -1,
127 257add31 2020-09-09 stsp &iremotes, sizeof(iremotes)) == -1)
128 257add31 2020-09-09 stsp return got_error_from_errno("imsg_compose GOTCONFIG_REMOTES");
129 257add31 2020-09-09 stsp
130 257add31 2020-09-09 stsp err = got_privsep_flush_imsg(ibuf);
131 257add31 2020-09-09 stsp imsg_clear(ibuf);
132 257add31 2020-09-09 stsp if (err)
133 257add31 2020-09-09 stsp return err;
134 257add31 2020-09-09 stsp
135 257add31 2020-09-09 stsp TAILQ_FOREACH(repo, remotes, entry) {
136 257add31 2020-09-09 stsp struct got_imsg_remote iremote;
137 257add31 2020-09-09 stsp size_t len = sizeof(iremote);
138 257add31 2020-09-09 stsp struct ibuf *wbuf;
139 257add31 2020-09-09 stsp
140 257add31 2020-09-09 stsp iremote.mirror_references = repo->mirror_references;
141 257add31 2020-09-09 stsp
142 257add31 2020-09-09 stsp iremote.name_len = strlen(repo->name);
143 257add31 2020-09-09 stsp len += iremote.name_len;
144 257add31 2020-09-09 stsp
145 257add31 2020-09-09 stsp err = make_repo_url(&url, repo);
146 257add31 2020-09-09 stsp if (err)
147 257add31 2020-09-09 stsp break;
148 257add31 2020-09-09 stsp iremote.url_len = strlen(url);
149 257add31 2020-09-09 stsp len += iremote.url_len;
150 257add31 2020-09-09 stsp
151 257add31 2020-09-09 stsp wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
152 257add31 2020-09-09 stsp if (wbuf == NULL) {
153 257add31 2020-09-09 stsp err = got_error_from_errno(
154 257add31 2020-09-09 stsp "imsg_create GOTCONFIG_REMOTE");
155 257add31 2020-09-09 stsp break;
156 257add31 2020-09-09 stsp }
157 257add31 2020-09-09 stsp
158 257add31 2020-09-09 stsp if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
159 257add31 2020-09-09 stsp err = got_error_from_errno(
160 257add31 2020-09-09 stsp "imsg_add GOTCONFIG_REMOTE");
161 257add31 2020-09-09 stsp ibuf_free(wbuf);
162 257add31 2020-09-09 stsp break;
163 257add31 2020-09-09 stsp }
164 257add31 2020-09-09 stsp
165 257add31 2020-09-09 stsp if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
166 257add31 2020-09-09 stsp err = got_error_from_errno(
167 257add31 2020-09-09 stsp "imsg_add GOTCONFIG_REMOTE");
168 257add31 2020-09-09 stsp ibuf_free(wbuf);
169 257add31 2020-09-09 stsp break;
170 257add31 2020-09-09 stsp }
171 257add31 2020-09-09 stsp if (imsg_add(wbuf, url, iremote.url_len) == -1) {
172 257add31 2020-09-09 stsp err = got_error_from_errno(
173 257add31 2020-09-09 stsp "imsg_add GOTCONFIG_REMOTE");
174 257add31 2020-09-09 stsp ibuf_free(wbuf);
175 257add31 2020-09-09 stsp break;
176 257add31 2020-09-09 stsp }
177 257add31 2020-09-09 stsp
178 257add31 2020-09-09 stsp wbuf->fd = -1;
179 257add31 2020-09-09 stsp imsg_close(ibuf, wbuf);
180 257add31 2020-09-09 stsp err = got_privsep_flush_imsg(ibuf);
181 257add31 2020-09-09 stsp if (err)
182 257add31 2020-09-09 stsp break;
183 257add31 2020-09-09 stsp
184 257add31 2020-09-09 stsp free(url);
185 257add31 2020-09-09 stsp url = NULL;
186 257add31 2020-09-09 stsp }
187 257add31 2020-09-09 stsp
188 257add31 2020-09-09 stsp free(url);
189 257add31 2020-09-09 stsp return err;
190 257add31 2020-09-09 stsp }
191 257add31 2020-09-09 stsp
192 257add31 2020-09-09 stsp static const struct got_error *
193 257add31 2020-09-09 stsp validate_config(struct gotconfig *gotconfig)
194 257add31 2020-09-09 stsp {
195 257add31 2020-09-09 stsp struct gotconfig_remote_repo *repo, *repo2;
196 257add31 2020-09-09 stsp static char msg[512];
197 257add31 2020-09-09 stsp
198 257add31 2020-09-09 stsp TAILQ_FOREACH(repo, &gotconfig->remotes, entry) {
199 257add31 2020-09-09 stsp if (repo->name == NULL) {
200 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG,
201 257add31 2020-09-09 stsp "name required for remote repository");
202 257add31 2020-09-09 stsp }
203 257add31 2020-09-09 stsp
204 257add31 2020-09-09 stsp TAILQ_FOREACH(repo2, &gotconfig->remotes, entry) {
205 257add31 2020-09-09 stsp if (repo == repo2 ||
206 257add31 2020-09-09 stsp strcmp(repo->name, repo2->name) != 0)
207 257add31 2020-09-09 stsp continue;
208 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),
209 257add31 2020-09-09 stsp "duplicate remote repository name '%s'",
210 257add31 2020-09-09 stsp repo->name);
211 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
212 257add31 2020-09-09 stsp }
213 257add31 2020-09-09 stsp
214 257add31 2020-09-09 stsp if (repo->server == NULL) {
215 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),
216 257add31 2020-09-09 stsp "server required for remote repository \"%s\"",
217 257add31 2020-09-09 stsp repo->name);
218 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
219 257add31 2020-09-09 stsp }
220 257add31 2020-09-09 stsp
221 257add31 2020-09-09 stsp if (repo->protocol == NULL) {
222 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),
223 257add31 2020-09-09 stsp "protocol required for remote repository \"%s\"",
224 257add31 2020-09-09 stsp repo->name);
225 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
226 257add31 2020-09-09 stsp }
227 257add31 2020-09-09 stsp if (strcmp(repo->protocol, "ssh") != 0 &&
228 257add31 2020-09-09 stsp strcmp(repo->protocol, "git+ssh") != 0 &&
229 257add31 2020-09-09 stsp strcmp(repo->protocol, "git") != 0) {
230 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),"unknown protocol \"%s\" "
231 257add31 2020-09-09 stsp "for remote repository \"%s\"", repo->protocol,
232 257add31 2020-09-09 stsp repo->name);
233 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
234 257add31 2020-09-09 stsp }
235 257add31 2020-09-09 stsp
236 257add31 2020-09-09 stsp if (repo->repository == NULL) {
237 257add31 2020-09-09 stsp snprintf(msg, sizeof(msg),
238 257add31 2020-09-09 stsp "repository path required for remote "
239 257add31 2020-09-09 stsp "repository \"%s\"", repo->name);
240 257add31 2020-09-09 stsp return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
241 257add31 2020-09-09 stsp }
242 257add31 2020-09-09 stsp }
243 257add31 2020-09-09 stsp
244 257add31 2020-09-09 stsp return NULL;
245 257add31 2020-09-09 stsp }
246 257add31 2020-09-09 stsp
247 257add31 2020-09-09 stsp int
248 257add31 2020-09-09 stsp main(int argc, char *argv[])
249 257add31 2020-09-09 stsp {
250 257add31 2020-09-09 stsp const struct got_error *err = NULL;
251 257add31 2020-09-09 stsp struct imsgbuf ibuf;
252 53dfa00d 2020-09-10 stsp struct gotconfig *gotconfig = NULL;
253 257add31 2020-09-09 stsp size_t datalen;
254 257add31 2020-09-09 stsp const char *filename = "got.conf";
255 257add31 2020-09-09 stsp #if 0
256 257add31 2020-09-09 stsp static int attached;
257 257add31 2020-09-09 stsp
258 257add31 2020-09-09 stsp while (!attached)
259 257add31 2020-09-09 stsp sleep(1);
260 257add31 2020-09-09 stsp #endif
261 257add31 2020-09-09 stsp signal(SIGINT, catch_sigint);
262 257add31 2020-09-09 stsp
263 257add31 2020-09-09 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
264 257add31 2020-09-09 stsp
265 257add31 2020-09-09 stsp #ifndef PROFILE
266 257add31 2020-09-09 stsp /* revoke access to most system calls */
267 257add31 2020-09-09 stsp if (pledge("stdio recvfd", NULL) == -1) {
268 257add31 2020-09-09 stsp err = got_error_from_errno("pledge");
269 257add31 2020-09-09 stsp got_privsep_send_error(&ibuf, err);
270 257add31 2020-09-09 stsp return 1;
271 257add31 2020-09-09 stsp }
272 257add31 2020-09-09 stsp #endif
273 257add31 2020-09-09 stsp
274 257add31 2020-09-09 stsp if (argc > 1)
275 257add31 2020-09-09 stsp filename = argv[1];
276 257add31 2020-09-09 stsp
277 257add31 2020-09-09 stsp for (;;) {
278 257add31 2020-09-09 stsp struct imsg imsg;
279 257add31 2020-09-09 stsp
280 257add31 2020-09-09 stsp memset(&imsg, 0, sizeof(imsg));
281 257add31 2020-09-09 stsp imsg.fd = -1;
282 257add31 2020-09-09 stsp
283 257add31 2020-09-09 stsp if (sigint_received) {
284 257add31 2020-09-09 stsp err = got_error(GOT_ERR_CANCELLED);
285 257add31 2020-09-09 stsp break;
286 257add31 2020-09-09 stsp }
287 257add31 2020-09-09 stsp
288 257add31 2020-09-09 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
289 257add31 2020-09-09 stsp if (err) {
290 257add31 2020-09-09 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
291 257add31 2020-09-09 stsp err = NULL;
292 257add31 2020-09-09 stsp break;
293 257add31 2020-09-09 stsp }
294 257add31 2020-09-09 stsp
295 257add31 2020-09-09 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
296 257add31 2020-09-09 stsp break;
297 257add31 2020-09-09 stsp
298 257add31 2020-09-09 stsp switch (imsg.hdr.type) {
299 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
300 257add31 2020-09-09 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
301 257add31 2020-09-09 stsp if (datalen != 0) {
302 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
303 257add31 2020-09-09 stsp break;
304 257add31 2020-09-09 stsp }
305 257add31 2020-09-09 stsp if (imsg.fd == -1){
306 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
307 257add31 2020-09-09 stsp break;
308 257add31 2020-09-09 stsp }
309 257add31 2020-09-09 stsp
310 257add31 2020-09-09 stsp if (gotconfig)
311 257add31 2020-09-09 stsp gotconfig_free(gotconfig);
312 257add31 2020-09-09 stsp err = gotconfig_parse(&gotconfig, filename, &imsg.fd);
313 257add31 2020-09-09 stsp if (err)
314 257add31 2020-09-09 stsp break;
315 257add31 2020-09-09 stsp err = validate_config(gotconfig);
316 257add31 2020-09-09 stsp break;
317 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
318 257add31 2020-09-09 stsp if (gotconfig == NULL) {
319 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
320 257add31 2020-09-09 stsp break;
321 257add31 2020-09-09 stsp }
322 257add31 2020-09-09 stsp err = send_gotconfig_str(&ibuf,
323 257add31 2020-09-09 stsp gotconfig->author ? gotconfig->author : "");
324 257add31 2020-09-09 stsp break;
325 257add31 2020-09-09 stsp case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
326 257add31 2020-09-09 stsp if (gotconfig == NULL) {
327 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
328 257add31 2020-09-09 stsp break;
329 257add31 2020-09-09 stsp }
330 257add31 2020-09-09 stsp err = send_gotconfig_remotes(&ibuf,
331 257add31 2020-09-09 stsp &gotconfig->remotes, gotconfig->nremotes);
332 257add31 2020-09-09 stsp break;
333 257add31 2020-09-09 stsp default:
334 257add31 2020-09-09 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
335 257add31 2020-09-09 stsp break;
336 257add31 2020-09-09 stsp }
337 257add31 2020-09-09 stsp
338 257add31 2020-09-09 stsp if (imsg.fd != -1) {
339 257add31 2020-09-09 stsp if (close(imsg.fd) == -1 && err == NULL)
340 257add31 2020-09-09 stsp err = got_error_from_errno("close");
341 257add31 2020-09-09 stsp }
342 257add31 2020-09-09 stsp
343 257add31 2020-09-09 stsp imsg_free(&imsg);
344 257add31 2020-09-09 stsp if (err)
345 257add31 2020-09-09 stsp break;
346 257add31 2020-09-09 stsp }
347 257add31 2020-09-09 stsp
348 257add31 2020-09-09 stsp imsg_clear(&ibuf);
349 257add31 2020-09-09 stsp if (err) {
350 257add31 2020-09-09 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
351 257add31 2020-09-09 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
352 257add31 2020-09-09 stsp got_privsep_send_error(&ibuf, err);
353 257add31 2020-09-09 stsp }
354 257add31 2020-09-09 stsp }
355 257add31 2020-09-09 stsp if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
356 257add31 2020-09-09 stsp err = got_error_from_errno("close");
357 257add31 2020-09-09 stsp return err ? 1 : 0;
358 257add31 2020-09-09 stsp }