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 const struct got_error *
199 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
201 const struct got_error *err = NULL;
202 struct got_gitconfig_list *sections;
203 struct got_gitconfig_list_node *node;
204 struct got_remote_repo *remotes = NULL;
205 int nremotes = 0, i;
207 if (gitconfig == NULL)
208 return got_error(GOT_ERR_PRIVSEP_MSG);
210 err = got_gitconfig_get_section_list(&sections, gitconfig);
211 if (err)
212 return err;
214 TAILQ_FOREACH(node, &sections->fields, link) {
215 if (strncasecmp("remote \"", node->field, 8) != 0)
216 continue;
217 nremotes++;
220 if (nremotes == 0) {
221 err = send_gitconfig_remotes(ibuf, NULL, 0);
222 goto done;
225 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
226 if (remotes == NULL) {
227 err = got_error_from_errno("recallocarray");
228 goto done;
231 i = 0;
232 TAILQ_FOREACH(node, &sections->fields, link) {
233 char *name, *end, *mirror;
235 if (strncasecmp("remote \"", node->field, 8) != 0)
236 continue;
238 name = strdup(node->field + 8);
239 if (name == NULL) {
240 err = got_error_from_errno("strdup");
241 goto done;
243 end = strrchr(name, '"');
244 if (end)
245 *end = '\0';
246 remotes[i].name = name;
248 remotes[i].fetch_url = got_gitconfig_get_str(gitconfig,
249 node->field, "url");
250 if (remotes[i].fetch_url == NULL) {
251 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
252 goto done;
255 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
256 node->field, "pushurl");
257 if (remotes[i].send_url == NULL)
258 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
259 node->field, "url");
260 if (remotes[i].send_url == NULL) {
261 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
262 goto done;
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;
363 memset(&imsg, 0, sizeof(imsg));
364 imsg.fd = -1;
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 if (imsg.fd == -1){
389 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
390 break;
393 if (gitconfig)
394 got_gitconfig_close(gitconfig);
395 err = got_gitconfig_open(&gitconfig, imsg.fd);
396 break;
397 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
398 err = gitconfig_num_request(&ibuf, gitconfig, "core",
399 "repositoryformatversion", 0);
400 break;
401 case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
402 err = gitconfig_extensions_request(&ibuf, gitconfig);
403 break;
404 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
405 err = gitconfig_str_request(&ibuf, gitconfig, "user",
406 "name");
407 break;
408 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
409 err = gitconfig_str_request(&ibuf, gitconfig, "user",
410 "email");
411 break;
412 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
413 err = gitconfig_remotes_request(&ibuf, gitconfig);
414 break;
415 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
416 err = gitconfig_owner_request(&ibuf, gitconfig);
417 break;
418 default:
419 err = got_error(GOT_ERR_PRIVSEP_MSG);
420 break;
423 if (imsg.fd != -1) {
424 if (close(imsg.fd) == -1 && err == NULL)
425 err = got_error_from_errno("close");
428 imsg_free(&imsg);
429 if (err)
430 break;
433 imsg_clear(&ibuf);
434 if (err) {
435 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
436 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
437 got_privsep_send_error(&ibuf, err);
440 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
441 err = got_error_from_errno("close");
442 return err ? 1 : 0;