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 <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 <sha2.h>
31 #include <unistd.h>
32 #include <zlib.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_repository.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_object.h"
40 #include "got_lib_privsep.h"
41 #include "got_lib_gitconfig.h"
43 static volatile sig_atomic_t sigint_received;
45 static void
46 catch_sigint(int signo)
47 {
48 sigint_received = 1;
49 }
51 static const struct got_error *
52 send_gitconfig_int(struct imsgbuf *ibuf, int value)
53 {
54 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
55 &value, sizeof(value)) == -1)
56 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
58 return got_privsep_flush_imsg(ibuf);
59 }
61 static const struct got_error *
62 gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
63 const char *section, const char *tag, int def)
64 {
65 int value;
67 if (gitconfig == NULL)
68 return got_error(GOT_ERR_PRIVSEP_MSG);
70 value = got_gitconfig_get_num(gitconfig, section, tag, def);
71 return send_gitconfig_int(ibuf, value);
72 }
74 static const struct got_error *
75 send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
76 {
77 size_t len = value ? strlen(value) : 0;
79 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
80 value, len) == -1)
81 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
83 return got_privsep_flush_imsg(ibuf);
84 }
86 static const struct got_error *
87 send_gitconfig_pair(struct imsgbuf *ibuf, const char *key, const char *val)
88 {
89 struct ibuf *wbuf;
90 size_t klen = key ? strlen(key) : 0;
91 size_t vlen = val ? strlen(val) : 0;
92 size_t tot = sizeof(klen) + sizeof(vlen) + klen + vlen;
94 if (tot > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
95 return got_error(GOT_ERR_NO_SPACE);
97 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_PAIR, 0, 0, tot);
98 if (wbuf == NULL)
99 return got_error_from_errno("imsg_create GITCONFIG_PAIR");
101 /* Keep in sync with got_imsg_gitconfig_pair */
102 if (imsg_add(wbuf, &klen, sizeof(klen)) == -1)
103 return got_error_from_errno("imsg_add GITCONFIG_PAIR");
104 if (imsg_add(wbuf, &vlen, sizeof(vlen)) == -1)
105 return got_error_from_errno("imsg_add GITCONFIG_PAIR");
106 if (imsg_add(wbuf, key, klen) == -1)
107 return got_error_from_errno("imsg_add GITCONFIG_PAIR");
108 if (imsg_add(wbuf, val, vlen) == -1)
109 return got_error_from_errno("imsg_add GITCONFIG_PAIR");
111 imsg_close(ibuf, wbuf);
112 return got_privsep_flush_imsg(ibuf);
115 static const struct got_error *
116 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
117 const char *section, const char *tag)
119 char *value;
121 if (gitconfig == NULL)
122 return got_error(GOT_ERR_PRIVSEP_MSG);
124 value = got_gitconfig_get_str(gitconfig, section, tag);
125 return send_gitconfig_str(ibuf, value);
128 static const struct got_error *
129 send_gitconfig_remotes(struct imsgbuf *ibuf, struct got_remote_repo *remotes,
130 int nremotes)
132 const struct got_error *err = NULL;
133 struct got_imsg_remotes iremotes;
134 int i;
136 iremotes.nremotes = nremotes;
137 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
138 &iremotes, sizeof(iremotes)) == -1)
139 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
141 err = got_privsep_flush_imsg(ibuf);
142 imsg_clear(ibuf);
143 if (err)
144 return err;
146 for (i = 0; i < nremotes; i++) {
147 struct got_imsg_remote iremote;
148 size_t len = sizeof(iremote);
149 struct ibuf *wbuf;
151 iremote.mirror_references = remotes[i].mirror_references;
152 iremote.name_len = strlen(remotes[i].name);
153 len += iremote.name_len;
154 iremote.fetch_url_len = strlen(remotes[i].fetch_url);
155 len += iremote.fetch_url_len;
156 iremote.send_url_len = strlen(remotes[i].send_url);
157 len += iremote.send_url_len;
159 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
160 if (wbuf == NULL)
161 return got_error_from_errno(
162 "imsg_create GITCONFIG_REMOTE");
164 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1)
165 return got_error_from_errno(
166 "imsg_add GITCONFIG_REMOTE");
168 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1)
169 return got_error_from_errno(
170 "imsg_add GITCONFIG_REMOTE");
171 if (imsg_add(wbuf, remotes[i].fetch_url, iremote.fetch_url_len) == -1)
172 return got_error_from_errno(
173 "imsg_add GITCONFIG_REMOTE");
174 if (imsg_add(wbuf, remotes[i].send_url, iremote.send_url_len) == -1)
175 return got_error_from_errno(
176 "imsg_add GITCONFIG_REMOTE");
178 imsg_close(ibuf, wbuf);
179 err = got_privsep_flush_imsg(ibuf);
180 if (err)
181 return err;
184 return NULL;
187 static int
188 get_boolean_val(char *val)
190 return (strcasecmp(val, "true") == 0 ||
191 strcasecmp(val, "on") == 0 ||
192 strcasecmp(val, "yes") == 0 ||
193 strcmp(val, "1") == 0);
196 static int
197 skip_node(struct got_gitconfig *gitconfig, struct got_gitconfig_list_node *node)
199 /*
200 * Skip config nodes which do not describe remotes, and remotes
201 * which do not have a fetch URL defined (as used by git-annex).
202 */
203 return (strncasecmp("remote \"", node->field, 8) != 0 ||
204 got_gitconfig_get_str(gitconfig, node->field, "url") == NULL);
207 static const struct got_error *
208 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
210 const struct got_error *err = NULL;
211 struct got_gitconfig_list *sections;
212 struct got_gitconfig_list_node *node;
213 struct got_remote_repo *remotes = NULL;
214 int nremotes = 0, i;
216 if (gitconfig == NULL)
217 return got_error(GOT_ERR_PRIVSEP_MSG);
219 err = got_gitconfig_get_section_list(&sections, gitconfig);
220 if (err)
221 return err;
223 TAILQ_FOREACH(node, &sections->fields, link) {
224 if (skip_node(gitconfig, node))
225 continue;
226 nremotes++;
229 if (nremotes == 0) {
230 err = send_gitconfig_remotes(ibuf, NULL, 0);
231 goto done;
234 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
235 if (remotes == NULL) {
236 err = got_error_from_errno("recallocarray");
237 goto done;
240 i = 0;
241 TAILQ_FOREACH(node, &sections->fields, link) {
242 char *name, *end, *mirror;
244 if (skip_node(gitconfig, node))
245 continue;
247 name = strdup(node->field + 8);
248 if (name == NULL) {
249 err = got_error_from_errno("strdup");
250 goto done;
252 end = strrchr(name, '"');
253 if (end)
254 *end = '\0';
255 remotes[i].name = name;
257 remotes[i].fetch_url = got_gitconfig_get_str(gitconfig,
258 node->field, "url");
260 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
261 node->field, "pushurl");
262 if (remotes[i].send_url == NULL)
263 remotes[i].send_url = remotes[i].fetch_url;
265 remotes[i].mirror_references = 0;
266 mirror = got_gitconfig_get_str(gitconfig, node->field,
267 "mirror");
268 if (mirror != NULL && get_boolean_val(mirror))
269 remotes[i].mirror_references = 1;
271 i++;
274 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
275 done:
276 for (i = 0; i < nremotes; i++)
277 free(remotes[i].name);
278 free(remotes);
279 got_gitconfig_free_list(sections);
280 return err;
283 static const struct got_error *
284 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
286 char *value;
288 if (gitconfig == NULL)
289 return got_error(GOT_ERR_PRIVSEP_MSG);
291 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
292 if (value)
293 return send_gitconfig_str(ibuf, value);
294 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
295 return send_gitconfig_str(ibuf, value);
298 static const struct got_error *
299 gitconfig_extensions_request(struct imsgbuf *ibuf,
300 struct got_gitconfig *gitconfig)
302 const struct got_error *err = NULL;
303 struct got_gitconfig_list *tags;
304 struct got_gitconfig_list_node *node;
305 int nextensions = 0;
306 char *val;
308 if (gitconfig == NULL)
309 return got_error(GOT_ERR_PRIVSEP_MSG);
311 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
312 if (tags == NULL)
313 return send_gitconfig_int(ibuf, 0);
315 TAILQ_FOREACH(node, &tags->fields, link)
316 nextensions++;
318 err = send_gitconfig_int(ibuf, nextensions);
319 if (err)
320 goto done;
322 TAILQ_FOREACH(node, &tags->fields, link) {
323 val = got_gitconfig_get_str(gitconfig, "extensions",
324 node->field);
325 err = send_gitconfig_pair(ibuf, node->field, val);
326 if (err)
327 goto done;
329 done:
330 got_gitconfig_free_list(tags);
331 return err;
334 int
335 main(int argc, char *argv[])
337 const struct got_error *err = NULL;
338 struct imsgbuf ibuf;
339 size_t datalen;
340 struct got_gitconfig *gitconfig = NULL;
341 #if 0
342 static int attached;
344 while (!attached)
345 sleep(1);
346 #endif
347 signal(SIGINT, catch_sigint);
349 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
351 #ifndef PROFILE
352 /* revoke access to most system calls */
353 if (pledge("stdio recvfd", NULL) == -1) {
354 err = got_error_from_errno("pledge");
355 got_privsep_send_error(&ibuf, err);
356 return 1;
358 #endif
360 for (;;) {
361 struct imsg imsg;
362 int fd = -1;
364 memset(&imsg, 0, sizeof(imsg));
366 if (sigint_received) {
367 err = got_error(GOT_ERR_CANCELLED);
368 break;
371 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
372 if (err) {
373 if (err->code == GOT_ERR_PRIVSEP_PIPE)
374 err = NULL;
375 break;
378 if (imsg.hdr.type == GOT_IMSG_STOP)
379 break;
381 switch (imsg.hdr.type) {
382 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
383 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
384 if (datalen != 0) {
385 err = got_error(GOT_ERR_PRIVSEP_LEN);
386 break;
388 fd = imsg_get_fd(&imsg);
389 if (fd == -1) {
390 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
391 break;
394 if (gitconfig)
395 got_gitconfig_close(gitconfig);
396 err = got_gitconfig_open(&gitconfig, fd);
397 break;
398 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
399 err = gitconfig_num_request(&ibuf, gitconfig, "core",
400 "repositoryformatversion", 0);
401 break;
402 case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
403 err = gitconfig_extensions_request(&ibuf, gitconfig);
404 break;
405 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
406 err = gitconfig_str_request(&ibuf, gitconfig, "user",
407 "name");
408 break;
409 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
410 err = gitconfig_str_request(&ibuf, gitconfig, "user",
411 "email");
412 break;
413 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
414 err = gitconfig_remotes_request(&ibuf, gitconfig);
415 break;
416 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
417 err = gitconfig_owner_request(&ibuf, gitconfig);
418 break;
419 default:
420 err = got_error(GOT_ERR_PRIVSEP_MSG);
421 break;
424 if (fd != -1) {
425 if (close(fd) == -1 && err == NULL)
426 err = got_error_from_errno("close");
429 imsg_free(&imsg);
430 if (err)
431 break;
434 imsg_clear(&ibuf);
435 if (err) {
436 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
437 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
438 got_privsep_send_error(&ibuf, err);
441 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
442 err = got_error_from_errno("close");
443 return err ? 1 : 0;