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;
145 struct node_branch *branch;
146 int nbranches = 0;
148 branch = repo->branch;
149 while (branch) {
150 branch = branch->next;
151 nbranches++;
154 iremote.nbranches = nbranches;
155 iremote.mirror_references = repo->mirror_references;
157 iremote.name_len = strlen(repo->name);
158 len += iremote.name_len;
160 err = make_repo_url(&url, repo);
161 if (err)
162 break;
163 iremote.url_len = strlen(url);
164 len += iremote.url_len;
166 wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
167 if (wbuf == NULL) {
168 err = got_error_from_errno(
169 "imsg_create GOTCONFIG_REMOTE");
170 break;
173 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
174 err = got_error_from_errno(
175 "imsg_add GOTCONFIG_REMOTE");
176 ibuf_free(wbuf);
177 break;
180 if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
181 err = got_error_from_errno(
182 "imsg_add GOTCONFIG_REMOTE");
183 ibuf_free(wbuf);
184 break;
186 if (imsg_add(wbuf, url, iremote.url_len) == -1) {
187 err = got_error_from_errno(
188 "imsg_add GOTCONFIG_REMOTE");
189 ibuf_free(wbuf);
190 break;
193 wbuf->fd = -1;
194 imsg_close(ibuf, wbuf);
195 err = got_privsep_flush_imsg(ibuf);
196 if (err)
197 break;
199 free(url);
200 url = NULL;
202 branch = repo->branch;
203 while (branch) {
204 err = send_gotconfig_str(ibuf, branch->branch_name);
205 if (err)
206 break;
207 branch = branch->next;
211 free(url);
212 return err;
215 static const struct got_error *
216 validate_config(struct gotconfig *gotconfig)
218 struct gotconfig_remote_repo *repo, *repo2;
219 static char msg[512];
221 TAILQ_FOREACH(repo, &gotconfig->remotes, entry) {
222 if (repo->name == NULL) {
223 return got_error_msg(GOT_ERR_PARSE_CONFIG,
224 "name required for remote repository");
227 TAILQ_FOREACH(repo2, &gotconfig->remotes, entry) {
228 if (repo == repo2 ||
229 strcmp(repo->name, repo2->name) != 0)
230 continue;
231 snprintf(msg, sizeof(msg),
232 "duplicate remote repository name '%s'",
233 repo->name);
234 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
237 if (repo->server == NULL) {
238 snprintf(msg, sizeof(msg),
239 "server required for remote repository \"%s\"",
240 repo->name);
241 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
244 if (repo->protocol == NULL) {
245 snprintf(msg, sizeof(msg),
246 "protocol required for remote repository \"%s\"",
247 repo->name);
248 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
250 if (strcmp(repo->protocol, "ssh") != 0 &&
251 strcmp(repo->protocol, "git+ssh") != 0 &&
252 strcmp(repo->protocol, "git") != 0) {
253 snprintf(msg, sizeof(msg),"unknown protocol \"%s\" "
254 "for remote repository \"%s\"", repo->protocol,
255 repo->name);
256 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
259 if (repo->repository == NULL) {
260 snprintf(msg, sizeof(msg),
261 "repository path required for remote "
262 "repository \"%s\"", repo->name);
263 return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
267 return NULL;
270 int
271 main(int argc, char *argv[])
273 const struct got_error *err = NULL;
274 struct imsgbuf ibuf;
275 struct gotconfig *gotconfig = NULL;
276 size_t datalen;
277 const char *filename = "got.conf";
278 #if 0
279 static int attached;
281 while (!attached)
282 sleep(1);
283 #endif
284 signal(SIGINT, catch_sigint);
286 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
288 #ifndef PROFILE
289 /* revoke access to most system calls */
290 if (pledge("stdio recvfd", NULL) == -1) {
291 err = got_error_from_errno("pledge");
292 got_privsep_send_error(&ibuf, err);
293 return 1;
295 #endif
297 if (argc > 1)
298 filename = argv[1];
300 for (;;) {
301 struct imsg imsg;
303 memset(&imsg, 0, sizeof(imsg));
304 imsg.fd = -1;
306 if (sigint_received) {
307 err = got_error(GOT_ERR_CANCELLED);
308 break;
311 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
312 if (err) {
313 if (err->code == GOT_ERR_PRIVSEP_PIPE)
314 err = NULL;
315 break;
318 if (imsg.hdr.type == GOT_IMSG_STOP)
319 break;
321 switch (imsg.hdr.type) {
322 case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
323 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
324 if (datalen != 0) {
325 err = got_error(GOT_ERR_PRIVSEP_LEN);
326 break;
328 if (imsg.fd == -1){
329 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
330 break;
333 if (gotconfig)
334 gotconfig_free(gotconfig);
335 err = gotconfig_parse(&gotconfig, filename, &imsg.fd);
336 if (err)
337 break;
338 err = validate_config(gotconfig);
339 break;
340 case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
341 if (gotconfig == NULL) {
342 err = got_error(GOT_ERR_PRIVSEP_MSG);
343 break;
345 err = send_gotconfig_str(&ibuf,
346 gotconfig->author ? gotconfig->author : "");
347 break;
348 case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
349 if (gotconfig == NULL) {
350 err = got_error(GOT_ERR_PRIVSEP_MSG);
351 break;
353 err = send_gotconfig_remotes(&ibuf,
354 &gotconfig->remotes, gotconfig->nremotes);
355 break;
356 default:
357 err = got_error(GOT_ERR_PRIVSEP_MSG);
358 break;
361 if (imsg.fd != -1) {
362 if (close(imsg.fd) == -1 && err == NULL)
363 err = got_error_from_errno("close");
366 imsg_free(&imsg);
367 if (err)
368 break;
371 imsg_clear(&ibuf);
372 if (err) {
373 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
374 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
375 got_privsep_send_error(&ibuf, err);
378 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
379 err = got_error_from_errno("close");
380 return err ? 1 : 0;