Blob


1 /*
2 * Copyright (c) 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>
22 #include <stdint.h>
23 #include <imsg.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sha1.h>
30 #include <unistd.h>
31 #include <zlib.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_path.h"
36 #include "got_repository.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_object.h"
40 #include "got_lib_privsep.h"
42 #include "gotconfig.h"
44 /* parse.y */
45 static volatile sig_atomic_t sigint_received;
47 static void
48 catch_sigint(int signo)
49 {
50 sigint_received = 1;
51 }
53 static const struct got_error *
54 make_repo_url(char **url, struct gotconfig_remote_repo *repo)
55 {
56 const struct got_error *err = NULL;
57 char *s = NULL, *p = NULL;
59 *url = NULL;
61 if (asprintf(&s, "%s://", repo->protocol) == -1)
62 return got_error_from_errno("asprintf");
64 if (repo->server) {
65 p = s;
66 s = NULL;
67 if (asprintf(&s, "%s%s", p, repo->server) == -1) {
68 err = got_error_from_errno("asprintf");
69 goto done;
70 }
71 free(p);
72 p = NULL;
73 }
75 if (repo->port) {
76 p = s;
77 s = NULL;
78 if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
79 err = got_error_from_errno("asprintf");
80 goto done;
81 }
82 free(p);
83 p = NULL;
84 }
86 if (repo->repository) {
87 char *repo_path = repo->repository;
88 while (repo_path[0] == '/')
89 repo_path++;
90 p = s;
91 s = NULL;
92 if (asprintf(&s, "%s/%s", p, repo_path) == -1) {
93 err = got_error_from_errno("asprintf");
94 goto done;
95 }
96 free(p);
97 p = NULL;
98 }
100 got_path_strip_trailing_slashes(s);
101 done:
102 if (err) {
103 free(s);
104 free(p);
105 } else
106 *url = s;
107 return err;
110 static const struct got_error *
111 send_gotconfig_str(struct imsgbuf *ibuf, const char *value)
113 size_t len = value ? strlen(value) : 0;
115 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_STR_VAL, 0, 0, -1,
116 value, len) == -1)
117 return got_error_from_errno("imsg_compose GOTCONFIG_STR_VAL");
119 return got_privsep_flush_imsg(ibuf);
122 static const struct got_error *
123 send_gotconfig_remotes(struct imsgbuf *ibuf,
124 struct gotconfig_remote_repo_list *remotes, int nremotes)
126 const struct got_error *err = NULL;
127 struct got_imsg_remotes iremotes;
128 struct gotconfig_remote_repo *repo;
129 char *url = NULL;
131 iremotes.nremotes = nremotes;
132 if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_REMOTES, 0, 0, -1,
133 &iremotes, sizeof(iremotes)) == -1)
134 return got_error_from_errno("imsg_compose GOTCONFIG_REMOTES");
136 err = got_privsep_flush_imsg(ibuf);
137 imsg_clear(ibuf);
138 if (err)
139 return err;
141 TAILQ_FOREACH(repo, remotes, entry) {
142 struct got_imsg_remote iremote;
143 size_t len = sizeof(iremote);
144 struct ibuf *wbuf;
146 iremote.mirror_references = repo->mirror_references;
148 iremote.name_len = strlen(repo->name);
149 len += iremote.name_len;
151 err = make_repo_url(&url, repo);
152 if (err)
153 break;
154 iremote.url_len = strlen(url);
155 len += iremote.url_len;
157 wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
158 if (wbuf == NULL) {
159 err = got_error_from_errno(
160 "imsg_create GOTCONFIG_REMOTE");
161 break;
164 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
165 err = got_error_from_errno(
166 "imsg_add GOTCONFIG_REMOTE");
167 ibuf_free(wbuf);
168 break;
171 if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
172 err = got_error_from_errno(
173 "imsg_add GOTCONFIG_REMOTE");
174 ibuf_free(wbuf);
175 break;
177 if (imsg_add(wbuf, url, iremote.url_len) == -1) {
178 err = got_error_from_errno(
179 "imsg_add GOTCONFIG_REMOTE");
180 ibuf_free(wbuf);
181 break;
184 wbuf->fd = -1;
185 imsg_close(ibuf, wbuf);
186 err = got_privsep_flush_imsg(ibuf);
187 if (err)
188 break;
190 free(url);
191 url = NULL;
194 free(url);
195 return err;
198 static const struct got_error *
199 validate_config(struct gotconfig *gotconfig)
201 struct gotconfig_remote_repo *repo, *repo2;
202 static char msg[512];
204 TAILQ_FOREACH(repo, &gotconfig->remotes, entry) {
205 if (repo->name == NULL) {
206 return got_error_msg(GOT_ERR_PARSE_CONFIG,
207 "name required for remote repository");
210 TAILQ_FOREACH(repo2, &gotconfig->remotes, entry) {
211 if (repo == repo2 ||
212 strcmp(repo->name, repo2->name) != 0)
213 continue;
214 snprintf(msg, sizeof(msg),
215 "duplicate remote repository name '%s'",
216 repo->name);
217 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
220 if (repo->server == NULL) {
221 snprintf(msg, sizeof(msg),
222 "server required for remote repository \"%s\"",
223 repo->name);
224 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
227 if (repo->protocol == NULL) {
228 snprintf(msg, sizeof(msg),
229 "protocol required for remote repository \"%s\"",
230 repo->name);
231 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
233 if (strcmp(repo->protocol, "ssh") != 0 &&
234 strcmp(repo->protocol, "git+ssh") != 0 &&
235 strcmp(repo->protocol, "git") != 0) {
236 snprintf(msg, sizeof(msg),"unknown protocol \"%s\" "
237 "for remote repository \"%s\"", repo->protocol,
238 repo->name);
239 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
242 if (repo->repository == NULL) {
243 snprintf(msg, sizeof(msg),
244 "repository path required for remote "
245 "repository \"%s\"", repo->name);
246 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
250 return NULL;
253 int
254 main(int argc, char *argv[])
256 const struct got_error *err = NULL;
257 struct imsgbuf ibuf;
258 struct gotconfig *gotconfig = NULL;
259 size_t datalen;
260 const char *filename = "got.conf";
261 #if 0
262 static int attached;
264 while (!attached)
265 sleep(1);
266 #endif
267 signal(SIGINT, catch_sigint);
269 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
271 #ifndef PROFILE
272 /* revoke access to most system calls */
273 if (pledge("stdio recvfd", NULL) == -1) {
274 err = got_error_from_errno("pledge");
275 got_privsep_send_error(&ibuf, err);
276 return 1;
278 #endif
280 if (argc > 1)
281 filename = argv[1];
283 for (;;) {
284 struct imsg imsg;
286 memset(&imsg, 0, sizeof(imsg));
287 imsg.fd = -1;
289 if (sigint_received) {
290 err = got_error(GOT_ERR_CANCELLED);
291 break;
294 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
295 if (err) {
296 if (err->code == GOT_ERR_PRIVSEP_PIPE)
297 err = NULL;
298 break;
301 if (imsg.hdr.type == GOT_IMSG_STOP)
302 break;
304 switch (imsg.hdr.type) {
305 case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
306 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
307 if (datalen != 0) {
308 err = got_error(GOT_ERR_PRIVSEP_LEN);
309 break;
311 if (imsg.fd == -1){
312 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
313 break;
316 if (gotconfig)
317 gotconfig_free(gotconfig);
318 err = gotconfig_parse(&gotconfig, filename, &imsg.fd);
319 if (err)
320 break;
321 err = validate_config(gotconfig);
322 break;
323 case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
324 if (gotconfig == NULL) {
325 err = got_error(GOT_ERR_PRIVSEP_MSG);
326 break;
328 err = send_gotconfig_str(&ibuf,
329 gotconfig->author ? gotconfig->author : "");
330 break;
331 case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
332 if (gotconfig == NULL) {
333 err = got_error(GOT_ERR_PRIVSEP_MSG);
334 break;
336 err = send_gotconfig_remotes(&ibuf,
337 &gotconfig->remotes, gotconfig->nremotes);
338 break;
339 default:
340 err = got_error(GOT_ERR_PRIVSEP_MSG);
341 break;
344 if (imsg.fd != -1) {
345 if (close(imsg.fd) == -1 && err == NULL)
346 err = got_error_from_errno("close");
349 imsg_free(&imsg);
350 if (err)
351 break;
354 imsg_clear(&ibuf);
355 if (err) {
356 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
357 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
358 got_privsep_send_error(&ibuf, err);
361 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
362 err = got_error_from_errno("close");
363 return err ? 1 : 0;