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 wbuf->fd = -1;
112 imsg_close(ibuf, wbuf);
113 return got_privsep_flush_imsg(ibuf);
116 static const struct got_error *
117 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
118 const char *section, const char *tag)
120 char *value;
122 if (gitconfig == NULL)
123 return got_error(GOT_ERR_PRIVSEP_MSG);
125 value = got_gitconfig_get_str(gitconfig, section, tag);
126 return send_gitconfig_str(ibuf, value);
129 static const struct got_error *
130 send_gitconfig_remotes(struct imsgbuf *ibuf, struct got_remote_repo *remotes,
131 int nremotes)
133 const struct got_error *err = NULL;
134 struct got_imsg_remotes iremotes;
135 int i;
137 iremotes.nremotes = nremotes;
138 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
139 &iremotes, sizeof(iremotes)) == -1)
140 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
142 err = got_privsep_flush_imsg(ibuf);
143 imsg_clear(ibuf);
144 if (err)
145 return err;
147 for (i = 0; i < nremotes; i++) {
148 struct got_imsg_remote iremote;
149 size_t len = sizeof(iremote);
150 struct ibuf *wbuf;
152 iremote.mirror_references = remotes[i].mirror_references;
153 iremote.name_len = strlen(remotes[i].name);
154 len += iremote.name_len;
155 iremote.fetch_url_len = strlen(remotes[i].fetch_url);
156 len += iremote.fetch_url_len;
157 iremote.send_url_len = strlen(remotes[i].send_url);
158 len += iremote.send_url_len;
160 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
161 if (wbuf == NULL)
162 return got_error_from_errno(
163 "imsg_create GITCONFIG_REMOTE");
165 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1)
166 return got_error_from_errno(
167 "imsg_add GITCONFIG_REMOTE");
169 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1)
170 return got_error_from_errno(
171 "imsg_add GITCONFIG_REMOTE");
172 if (imsg_add(wbuf, remotes[i].fetch_url, iremote.fetch_url_len) == -1)
173 return got_error_from_errno(
174 "imsg_add GITCONFIG_REMOTE");
175 if (imsg_add(wbuf, remotes[i].send_url, iremote.send_url_len) == -1)
176 return got_error_from_errno(
177 "imsg_add GITCONFIG_REMOTE");
179 wbuf->fd = -1;
180 imsg_close(ibuf, wbuf);
181 err = got_privsep_flush_imsg(ibuf);
182 if (err)
183 return err;
186 return NULL;
189 static int
190 get_boolean_val(char *val)
192 return (strcasecmp(val, "true") == 0 ||
193 strcasecmp(val, "on") == 0 ||
194 strcasecmp(val, "yes") == 0 ||
195 strcmp(val, "1") == 0);
198 static int
199 skip_node(struct got_gitconfig *gitconfig, struct got_gitconfig_list_node *node)
201 /*
202 * Skip config nodes which do not describe remotes, and remotes
203 * which do not have a fetch URL defined (as used by git-annex).
204 */
205 return (strncasecmp("remote \"", node->field, 8) != 0 ||
206 got_gitconfig_get_str(gitconfig, node->field, "url") == NULL);
209 static const struct got_error *
210 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
212 const struct got_error *err = NULL;
213 struct got_gitconfig_list *sections;
214 struct got_gitconfig_list_node *node;
215 struct got_remote_repo *remotes = NULL;
216 int nremotes = 0, i;
218 if (gitconfig == NULL)
219 return got_error(GOT_ERR_PRIVSEP_MSG);
221 err = got_gitconfig_get_section_list(&sections, gitconfig);
222 if (err)
223 return err;
225 TAILQ_FOREACH(node, &sections->fields, link) {
226 if (skip_node(gitconfig, node))
227 continue;
228 nremotes++;
231 if (nremotes == 0) {
232 err = send_gitconfig_remotes(ibuf, NULL, 0);
233 goto done;
236 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
237 if (remotes == NULL) {
238 err = got_error_from_errno("recallocarray");
239 goto done;
242 i = 0;
243 TAILQ_FOREACH(node, &sections->fields, link) {
244 char *name, *end, *mirror;
246 if (skip_node(gitconfig, node))
247 continue;
249 name = strdup(node->field + 8);
250 if (name == NULL) {
251 err = got_error_from_errno("strdup");
252 goto done;
254 end = strrchr(name, '"');
255 if (end)
256 *end = '\0';
257 remotes[i].name = name;
259 remotes[i].fetch_url = got_gitconfig_get_str(gitconfig,
260 node->field, "url");
262 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
263 node->field, "pushurl");
264 if (remotes[i].send_url == NULL)
265 remotes[i].send_url = remotes[i].fetch_url;
267 remotes[i].mirror_references = 0;
268 mirror = got_gitconfig_get_str(gitconfig, node->field,
269 "mirror");
270 if (mirror != NULL && get_boolean_val(mirror))
271 remotes[i].mirror_references = 1;
273 i++;
276 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
277 done:
278 for (i = 0; i < nremotes; i++)
279 free(remotes[i].name);
280 free(remotes);
281 got_gitconfig_free_list(sections);
282 return err;
285 static const struct got_error *
286 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
288 char *value;
290 if (gitconfig == NULL)
291 return got_error(GOT_ERR_PRIVSEP_MSG);
293 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
294 if (value)
295 return send_gitconfig_str(ibuf, value);
296 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
297 return send_gitconfig_str(ibuf, value);
300 static const struct got_error *
301 gitconfig_extensions_request(struct imsgbuf *ibuf,
302 struct got_gitconfig *gitconfig)
304 const struct got_error *err = NULL;
305 struct got_gitconfig_list *tags;
306 struct got_gitconfig_list_node *node;
307 int nextensions = 0;
308 char *val;
310 if (gitconfig == NULL)
311 return got_error(GOT_ERR_PRIVSEP_MSG);
313 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
314 if (tags == NULL)
315 return send_gitconfig_int(ibuf, 0);
317 TAILQ_FOREACH(node, &tags->fields, link)
318 nextensions++;
320 err = send_gitconfig_int(ibuf, nextensions);
321 if (err)
322 goto done;
324 TAILQ_FOREACH(node, &tags->fields, link) {
325 val = got_gitconfig_get_str(gitconfig, "extensions",
326 node->field);
327 err = send_gitconfig_pair(ibuf, node->field, val);
328 if (err)
329 goto done;
331 done:
332 got_gitconfig_free_list(tags);
333 return err;
336 int
337 main(int argc, char *argv[])
339 const struct got_error *err = NULL;
340 struct imsgbuf ibuf;
341 size_t datalen;
342 struct got_gitconfig *gitconfig = NULL;
343 #if 0
344 static int attached;
346 while (!attached)
347 sleep(1);
348 #endif
349 signal(SIGINT, catch_sigint);
351 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
353 #ifndef PROFILE
354 /* revoke access to most system calls */
355 if (pledge("stdio recvfd", NULL) == -1) {
356 err = got_error_from_errno("pledge");
357 got_privsep_send_error(&ibuf, err);
358 return 1;
360 #endif
362 for (;;) {
363 struct imsg imsg;
365 memset(&imsg, 0, sizeof(imsg));
366 imsg.fd = -1;
368 if (sigint_received) {
369 err = got_error(GOT_ERR_CANCELLED);
370 break;
373 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
374 if (err) {
375 if (err->code == GOT_ERR_PRIVSEP_PIPE)
376 err = NULL;
377 break;
380 if (imsg.hdr.type == GOT_IMSG_STOP)
381 break;
383 switch (imsg.hdr.type) {
384 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
385 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
386 if (datalen != 0) {
387 err = got_error(GOT_ERR_PRIVSEP_LEN);
388 break;
390 if (imsg.fd == -1){
391 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
392 break;
395 if (gitconfig)
396 got_gitconfig_close(gitconfig);
397 err = got_gitconfig_open(&gitconfig, imsg.fd);
398 break;
399 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
400 err = gitconfig_num_request(&ibuf, gitconfig, "core",
401 "repositoryformatversion", 0);
402 break;
403 case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
404 err = gitconfig_extensions_request(&ibuf, gitconfig);
405 break;
406 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
407 err = gitconfig_str_request(&ibuf, gitconfig, "user",
408 "name");
409 break;
410 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
411 err = gitconfig_str_request(&ibuf, gitconfig, "user",
412 "email");
413 break;
414 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
415 err = gitconfig_remotes_request(&ibuf, gitconfig);
416 break;
417 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
418 err = gitconfig_owner_request(&ibuf, gitconfig);
419 break;
420 default:
421 err = got_error(GOT_ERR_PRIVSEP_MSG);
422 break;
425 if (imsg.fd != -1) {
426 if (close(imsg.fd) == -1 && err == NULL)
427 err = got_error_from_errno("close");
430 imsg_free(&imsg);
431 if (err)
432 break;
435 imsg_clear(&ibuf);
436 if (err) {
437 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
438 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
439 got_privsep_send_error(&ibuf, err);
442 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
443 err = got_error_from_errno("close");
444 return err ? 1 : 0;