Blob


1 /*
2 * Copyright (c) 2019 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 <limits.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <zlib.h>
31 #include "got_compat.h"
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_object.h"
39 #include "got_lib_privsep.h"
40 #include "got_lib_gitconfig.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 send_gitconfig_int(struct imsgbuf *ibuf, int value)
52 {
53 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
54 &value, sizeof(value)) == -1)
55 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
57 return got_privsep_flush_imsg(ibuf);
58 }
60 static const struct got_error *
61 gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
62 const char *section, const char *tag, int def)
63 {
64 int value;
66 if (gitconfig == NULL)
67 return got_error(GOT_ERR_PRIVSEP_MSG);
69 value = got_gitconfig_get_num(gitconfig, section, tag, def);
70 return send_gitconfig_int(ibuf, value);
71 }
73 static const struct got_error *
74 send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
75 {
76 size_t len = value ? strlen(value) : 0;
78 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
79 value, len) == -1)
80 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
82 return got_privsep_flush_imsg(ibuf);
83 }
85 static const struct got_error *
86 send_gitconfig_pair(struct imsgbuf *ibuf, const char *key, const char *val)
87 {
88 struct ibuf *wbuf;
89 size_t klen = key ? strlen(key) : 0;
90 size_t vlen = val ? strlen(val) : 0;
91 size_t tot = sizeof(klen) + sizeof(vlen) + klen + vlen;
93 if (tot > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
94 return got_error(GOT_ERR_NO_SPACE);
96 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_PAIR, 0, 0, tot);
97 if (wbuf == NULL)
98 return got_error_from_errno("imsg_create GITCONFIG_PAIR");
100 /* Keep in sync with got_imsg_gitconfig_pair */
101 if (imsg_add(wbuf, &klen, sizeof(klen)) == -1)
102 return got_error_from_errno("imsg_add GITCONFIG_PAIR");
103 if (imsg_add(wbuf, &vlen, sizeof(vlen)) == -1)
104 return got_error_from_errno("imsg_add GITCONFIG_PAIR");
105 if (imsg_add(wbuf, key, klen) == -1)
106 return got_error_from_errno("imsg_add GITCONFIG_PAIR");
107 if (imsg_add(wbuf, val, vlen) == -1)
108 return got_error_from_errno("imsg_add GITCONFIG_PAIR");
110 imsg_close(ibuf, wbuf);
111 return got_privsep_flush_imsg(ibuf);
114 static const struct got_error *
115 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
116 const char *section, const char *tag)
118 char *value;
120 if (gitconfig == NULL)
121 return got_error(GOT_ERR_PRIVSEP_MSG);
123 value = got_gitconfig_get_str(gitconfig, section, tag);
124 return send_gitconfig_str(ibuf, value);
127 static const struct got_error *
128 send_gitconfig_remotes(struct imsgbuf *ibuf, struct got_remote_repo *remotes,
129 int nremotes)
131 const struct got_error *err = NULL;
132 struct got_imsg_remotes iremotes;
133 int i;
135 iremotes.nremotes = nremotes;
136 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
137 &iremotes, sizeof(iremotes)) == -1)
138 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
140 err = got_privsep_flush_imsg(ibuf);
141 imsg_clear(ibuf);
142 if (err)
143 return err;
145 for (i = 0; i < nremotes; i++) {
146 struct got_imsg_remote iremote;
147 size_t len = sizeof(iremote);
148 struct ibuf *wbuf;
150 iremote.mirror_references = remotes[i].mirror_references;
151 iremote.name_len = strlen(remotes[i].name);
152 len += iremote.name_len;
153 iremote.fetch_url_len = strlen(remotes[i].fetch_url);
154 len += iremote.fetch_url_len;
155 iremote.send_url_len = strlen(remotes[i].send_url);
156 len += iremote.send_url_len;
158 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
159 if (wbuf == NULL)
160 return got_error_from_errno(
161 "imsg_create GITCONFIG_REMOTE");
163 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1)
164 return got_error_from_errno(
165 "imsg_add GITCONFIG_REMOTE");
167 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1)
168 return got_error_from_errno(
169 "imsg_add GITCONFIG_REMOTE");
170 if (imsg_add(wbuf, remotes[i].fetch_url, iremote.fetch_url_len) == -1)
171 return got_error_from_errno(
172 "imsg_add GITCONFIG_REMOTE");
173 if (imsg_add(wbuf, remotes[i].send_url, iremote.send_url_len) == -1)
174 return got_error_from_errno(
175 "imsg_add GITCONFIG_REMOTE");
177 imsg_close(ibuf, wbuf);
178 err = got_privsep_flush_imsg(ibuf);
179 if (err)
180 return err;
183 return NULL;
186 static int
187 get_boolean_val(char *val)
189 return (strcasecmp(val, "true") == 0 ||
190 strcasecmp(val, "on") == 0 ||
191 strcasecmp(val, "yes") == 0 ||
192 strcmp(val, "1") == 0);
195 static int
196 skip_node(struct got_gitconfig *gitconfig, struct got_gitconfig_list_node *node)
198 /*
199 * Skip config nodes which do not describe remotes, and remotes
200 * which do not have a fetch URL defined (as used by git-annex).
201 */
202 return (strncasecmp("remote \"", node->field, 8) != 0 ||
203 got_gitconfig_get_str(gitconfig, node->field, "url") == NULL);
206 static const struct got_error *
207 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
209 const struct got_error *err = NULL;
210 struct got_gitconfig_list *sections;
211 struct got_gitconfig_list_node *node;
212 struct got_remote_repo *remotes = NULL;
213 int nremotes = 0, i;
215 if (gitconfig == NULL)
216 return got_error(GOT_ERR_PRIVSEP_MSG);
218 err = got_gitconfig_get_section_list(&sections, gitconfig);
219 if (err)
220 return err;
222 TAILQ_FOREACH(node, &sections->fields, link) {
223 if (skip_node(gitconfig, node))
224 continue;
225 nremotes++;
228 if (nremotes == 0) {
229 err = send_gitconfig_remotes(ibuf, NULL, 0);
230 goto done;
233 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
234 if (remotes == NULL) {
235 err = got_error_from_errno("recallocarray");
236 goto done;
239 i = 0;
240 TAILQ_FOREACH(node, &sections->fields, link) {
241 char *name, *end, *mirror;
243 if (skip_node(gitconfig, node))
244 continue;
246 name = strdup(node->field + 8);
247 if (name == NULL) {
248 err = got_error_from_errno("strdup");
249 goto done;
251 end = strrchr(name, '"');
252 if (end)
253 *end = '\0';
254 remotes[i].name = name;
256 remotes[i].fetch_url = got_gitconfig_get_str(gitconfig,
257 node->field, "url");
259 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
260 node->field, "pushurl");
261 if (remotes[i].send_url == NULL)
262 remotes[i].send_url = remotes[i].fetch_url;
264 remotes[i].mirror_references = 0;
265 mirror = got_gitconfig_get_str(gitconfig, node->field,
266 "mirror");
267 if (mirror != NULL && get_boolean_val(mirror))
268 remotes[i].mirror_references = 1;
270 i++;
273 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
274 done:
275 for (i = 0; i < nremotes; i++)
276 free(remotes[i].name);
277 free(remotes);
278 got_gitconfig_free_list(sections);
279 return err;
282 static const struct got_error *
283 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
285 char *value;
287 if (gitconfig == NULL)
288 return got_error(GOT_ERR_PRIVSEP_MSG);
290 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
291 if (value)
292 return send_gitconfig_str(ibuf, value);
293 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
294 return send_gitconfig_str(ibuf, value);
297 static const struct got_error *
298 gitconfig_extensions_request(struct imsgbuf *ibuf,
299 struct got_gitconfig *gitconfig)
301 const struct got_error *err = NULL;
302 struct got_gitconfig_list *tags;
303 struct got_gitconfig_list_node *node;
304 int nextensions = 0;
305 char *val;
307 if (gitconfig == NULL)
308 return got_error(GOT_ERR_PRIVSEP_MSG);
310 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
311 if (tags == NULL)
312 return send_gitconfig_int(ibuf, 0);
314 TAILQ_FOREACH(node, &tags->fields, link)
315 nextensions++;
317 err = send_gitconfig_int(ibuf, nextensions);
318 if (err)
319 goto done;
321 TAILQ_FOREACH(node, &tags->fields, link) {
322 val = got_gitconfig_get_str(gitconfig, "extensions",
323 node->field);
324 err = send_gitconfig_pair(ibuf, node->field, val);
325 if (err)
326 goto done;
328 done:
329 got_gitconfig_free_list(tags);
330 return err;
333 int
334 main(int argc, char *argv[])
336 const struct got_error *err = NULL;
337 struct imsgbuf ibuf;
338 size_t datalen;
339 struct got_gitconfig *gitconfig = NULL;
340 #if 0
341 static int attached;
343 while (!attached)
344 sleep(1);
345 #endif
346 signal(SIGINT, catch_sigint);
348 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
350 #ifndef PROFILE
351 /* revoke access to most system calls */
352 if (pledge("stdio recvfd", NULL) == -1) {
353 err = got_error_from_errno("pledge");
354 got_privsep_send_error(&ibuf, err);
355 return 1;
358 /* revoke fs access */
359 if (landlock_no_fs() == -1) {
360 err = got_error_from_errno("landlock_no_fs");
361 got_privsep_send_error(&ibuf, err);
362 return 1;
364 if (cap_enter() == -1) {
365 err = got_error_from_errno("cap_enter");
366 got_privsep_send_error(&ibuf, err);
367 return 1;
369 #endif
371 for (;;) {
372 struct imsg imsg;
373 int fd = -1;
375 memset(&imsg, 0, sizeof(imsg));
377 if (sigint_received) {
378 err = got_error(GOT_ERR_CANCELLED);
379 break;
382 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
383 if (err) {
384 if (err->code == GOT_ERR_PRIVSEP_PIPE)
385 err = NULL;
386 break;
389 if (imsg.hdr.type == GOT_IMSG_STOP)
390 break;
392 switch (imsg.hdr.type) {
393 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
394 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
395 if (datalen != 0) {
396 err = got_error(GOT_ERR_PRIVSEP_LEN);
397 break;
399 fd = imsg_get_fd(&imsg);
400 if (fd == -1) {
401 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
402 break;
405 if (gitconfig)
406 got_gitconfig_close(gitconfig);
407 err = got_gitconfig_open(&gitconfig, fd);
408 break;
409 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
410 err = gitconfig_num_request(&ibuf, gitconfig, "core",
411 "repositoryformatversion", 0);
412 break;
413 case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
414 err = gitconfig_extensions_request(&ibuf, gitconfig);
415 break;
416 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
417 err = gitconfig_str_request(&ibuf, gitconfig, "user",
418 "name");
419 break;
420 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
421 err = gitconfig_str_request(&ibuf, gitconfig, "user",
422 "email");
423 break;
424 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
425 err = gitconfig_remotes_request(&ibuf, gitconfig);
426 break;
427 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
428 err = gitconfig_owner_request(&ibuf, gitconfig);
429 break;
430 default:
431 err = got_error(GOT_ERR_PRIVSEP_MSG);
432 break;
435 if (fd != -1) {
436 if (close(fd) == -1 && err == NULL)
437 err = got_error_from_errno("close");
440 imsg_free(&imsg);
441 if (err)
442 break;
445 imsg_clear(&ibuf);
446 if (err) {
447 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
448 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
449 got_privsep_send_error(&ibuf, err);
452 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
453 err = got_error_from_errno("close");
454 return err ? 1 : 0;