2 aba9c984 2019-09-08 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
4 aba9c984 2019-09-08 stsp * Permission to use, copy, modify, and distribute this software for any
5 aba9c984 2019-09-08 stsp * purpose with or without fee is hereby granted, provided that the above
6 aba9c984 2019-09-08 stsp * copyright notice and this permission notice appear in all copies.
8 aba9c984 2019-09-08 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 aba9c984 2019-09-08 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 aba9c984 2019-09-08 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 aba9c984 2019-09-08 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 aba9c984 2019-09-08 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 aba9c984 2019-09-08 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 aba9c984 2019-09-08 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 aba9c984 2019-09-08 stsp #include <sys/types.h>
18 aba9c984 2019-09-08 stsp #include <sys/queue.h>
19 aba9c984 2019-09-08 stsp #include <sys/uio.h>
20 aba9c984 2019-09-08 stsp #include <sys/time.h>
22 aba9c984 2019-09-08 stsp #include <stdint.h>
23 aba9c984 2019-09-08 stsp #include <imsg.h>
24 aba9c984 2019-09-08 stsp #include <limits.h>
25 aba9c984 2019-09-08 stsp #include <signal.h>
26 aba9c984 2019-09-08 stsp #include <stdio.h>
27 aba9c984 2019-09-08 stsp #include <stdlib.h>
28 aba9c984 2019-09-08 stsp #include <string.h>
29 aba9c984 2019-09-08 stsp #include <sha1.h>
30 69c6accf 2023-02-04 op #include <sha2.h>
31 81a12da5 2020-09-09 naddy #include <unistd.h>
32 aba9c984 2019-09-08 stsp #include <zlib.h>
34 aba9c984 2019-09-08 stsp #include "got_error.h"
35 aba9c984 2019-09-08 stsp #include "got_object.h"
36 cd95becd 2019-11-29 stsp #include "got_repository.h"
38 aba9c984 2019-09-08 stsp #include "got_lib_delta.h"
39 aba9c984 2019-09-08 stsp #include "got_lib_object.h"
40 aba9c984 2019-09-08 stsp #include "got_lib_privsep.h"
41 aba9c984 2019-09-08 stsp #include "got_lib_gitconfig.h"
43 aba9c984 2019-09-08 stsp static volatile sig_atomic_t sigint_received;
46 aba9c984 2019-09-08 stsp catch_sigint(int signo)
48 aba9c984 2019-09-08 stsp sigint_received = 1;
51 aba9c984 2019-09-08 stsp static const struct got_error *
52 e70bf110 2020-03-22 stsp send_gitconfig_int(struct imsgbuf *ibuf, int value)
54 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
55 e70bf110 2020-03-22 stsp &value, sizeof(value)) == -1)
56 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
58 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
61 e70bf110 2020-03-22 stsp static const struct got_error *
62 aba9c984 2019-09-08 stsp gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
63 fda3525e 2021-09-25 stsp const char *section, const char *tag, int def)
67 aba9c984 2019-09-08 stsp if (gitconfig == NULL)
68 aba9c984 2019-09-08 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
70 aba9c984 2019-09-08 stsp value = got_gitconfig_get_num(gitconfig, section, tag, def);
71 e70bf110 2020-03-22 stsp return send_gitconfig_int(ibuf, value);
74 aba9c984 2019-09-08 stsp static const struct got_error *
75 e70bf110 2020-03-22 stsp send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
77 6c13dcd2 2020-09-18 stsp size_t len = value ? strlen(value) : 0;
79 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
80 e70bf110 2020-03-22 stsp value, len) == -1)
81 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
83 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
86 e70bf110 2020-03-22 stsp static const struct got_error *
87 aba9c984 2019-09-08 stsp gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
88 fda3525e 2021-09-25 stsp const char *section, const char *tag)
90 aba9c984 2019-09-08 stsp char *value;
92 aba9c984 2019-09-08 stsp if (gitconfig == NULL)
93 aba9c984 2019-09-08 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
95 aba9c984 2019-09-08 stsp value = got_gitconfig_get_str(gitconfig, section, tag);
96 e70bf110 2020-03-22 stsp return send_gitconfig_str(ibuf, value);
99 cd95becd 2019-11-29 stsp static const struct got_error *
100 e70bf110 2020-03-22 stsp send_gitconfig_remotes(struct imsgbuf *ibuf, struct got_remote_repo *remotes,
101 e70bf110 2020-03-22 stsp int nremotes)
103 e70bf110 2020-03-22 stsp const struct got_error *err = NULL;
104 e70bf110 2020-03-22 stsp struct got_imsg_remotes iremotes;
107 e70bf110 2020-03-22 stsp iremotes.nremotes = nremotes;
108 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
109 e70bf110 2020-03-22 stsp &iremotes, sizeof(iremotes)) == -1)
110 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
112 e70bf110 2020-03-22 stsp err = got_privsep_flush_imsg(ibuf);
113 e70bf110 2020-03-22 stsp imsg_clear(ibuf);
115 e70bf110 2020-03-22 stsp return err;
117 e70bf110 2020-03-22 stsp for (i = 0; i < nremotes; i++) {
118 e70bf110 2020-03-22 stsp struct got_imsg_remote iremote;
119 e70bf110 2020-03-22 stsp size_t len = sizeof(iremote);
120 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
122 e70bf110 2020-03-22 stsp iremote.mirror_references = remotes[i].mirror_references;
123 e70bf110 2020-03-22 stsp iremote.name_len = strlen(remotes[i].name);
124 e70bf110 2020-03-22 stsp len += iremote.name_len;
125 6480c871 2021-08-30 stsp iremote.fetch_url_len = strlen(remotes[i].fetch_url);
126 6480c871 2021-08-30 stsp len += iremote.fetch_url_len;
127 6480c871 2021-08-30 stsp iremote.send_url_len = strlen(remotes[i].send_url);
128 6480c871 2021-08-30 stsp len += iremote.send_url_len;
130 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
131 e70bf110 2020-03-22 stsp if (wbuf == NULL)
132 e70bf110 2020-03-22 stsp return got_error_from_errno(
133 e70bf110 2020-03-22 stsp "imsg_create GITCONFIG_REMOTE");
135 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1)
136 1453347d 2022-05-19 stsp return got_error_from_errno(
137 e70bf110 2020-03-22 stsp "imsg_add GITCONFIG_REMOTE");
139 1453347d 2022-05-19 stsp if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1)
140 1453347d 2022-05-19 stsp return got_error_from_errno(
141 e70bf110 2020-03-22 stsp "imsg_add GITCONFIG_REMOTE");
142 1453347d 2022-05-19 stsp if (imsg_add(wbuf, remotes[i].fetch_url, iremote.fetch_url_len) == -1)
143 1453347d 2022-05-19 stsp return got_error_from_errno(
144 6480c871 2021-08-30 stsp "imsg_add GITCONFIG_REMOTE");
145 1453347d 2022-05-19 stsp if (imsg_add(wbuf, remotes[i].send_url, iremote.send_url_len) == -1)
146 1453347d 2022-05-19 stsp return got_error_from_errno(
147 1453347d 2022-05-19 stsp "imsg_add GITCONFIG_REMOTE");
149 e70bf110 2020-03-22 stsp wbuf->fd = -1;
150 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
151 e70bf110 2020-03-22 stsp err = got_privsep_flush_imsg(ibuf);
153 e70bf110 2020-03-22 stsp return err;
156 e70bf110 2020-03-22 stsp return NULL;
160 20b7abb3 2020-10-22 stsp get_boolean_val(char *val)
162 20b7abb3 2020-10-22 stsp return (strcasecmp(val, "true") == 0 ||
163 20b7abb3 2020-10-22 stsp strcasecmp(val, "on") == 0 ||
164 20b7abb3 2020-10-22 stsp strcasecmp(val, "yes") == 0 ||
165 20b7abb3 2020-10-22 stsp strcmp(val, "1") == 0);
168 e70bf110 2020-03-22 stsp static const struct got_error *
169 cd95becd 2019-11-29 stsp gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
171 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
172 cd95becd 2019-11-29 stsp struct got_gitconfig_list *sections;
173 cd95becd 2019-11-29 stsp struct got_gitconfig_list_node *node;
174 cd95becd 2019-11-29 stsp struct got_remote_repo *remotes = NULL;
175 cd95becd 2019-11-29 stsp int nremotes = 0, i;
177 cd95becd 2019-11-29 stsp if (gitconfig == NULL)
178 cd95becd 2019-11-29 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
180 cd95becd 2019-11-29 stsp err = got_gitconfig_get_section_list(§ions, gitconfig);
182 cd95becd 2019-11-29 stsp return err;
184 cd95becd 2019-11-29 stsp TAILQ_FOREACH(node, §ions->fields, link) {
185 cd95becd 2019-11-29 stsp if (strncasecmp("remote \"", node->field, 8) != 0)
187 cd95becd 2019-11-29 stsp nremotes++;
190 cd95becd 2019-11-29 stsp if (nremotes == 0) {
191 e70bf110 2020-03-22 stsp err = send_gitconfig_remotes(ibuf, NULL, 0);
195 cd95becd 2019-11-29 stsp remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
196 cd95becd 2019-11-29 stsp if (remotes == NULL) {
197 cd95becd 2019-11-29 stsp err = got_error_from_errno("recallocarray");
202 cd95becd 2019-11-29 stsp TAILQ_FOREACH(node, §ions->fields, link) {
203 469dd726 2020-03-20 stsp char *name, *end, *mirror;
205 cd95becd 2019-11-29 stsp if (strncasecmp("remote \"", node->field, 8) != 0)
208 cd95becd 2019-11-29 stsp name = strdup(node->field + 8);
209 cd95becd 2019-11-29 stsp if (name == NULL) {
210 cd95becd 2019-11-29 stsp err = got_error_from_errno("strdup");
213 cd95becd 2019-11-29 stsp end = strrchr(name, '"');
215 cd95becd 2019-11-29 stsp *end = '\0';
216 cd95becd 2019-11-29 stsp remotes[i].name = name;
218 6480c871 2021-08-30 stsp remotes[i].fetch_url = got_gitconfig_get_str(gitconfig,
219 cd95becd 2019-11-29 stsp node->field, "url");
220 6480c871 2021-08-30 stsp if (remotes[i].fetch_url == NULL) {
221 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
225 6480c871 2021-08-30 stsp remotes[i].send_url = got_gitconfig_get_str(gitconfig,
226 6480c871 2021-08-30 stsp node->field, "pushurl");
227 6480c871 2021-08-30 stsp if (remotes[i].send_url == NULL)
228 6480c871 2021-08-30 stsp remotes[i].send_url = got_gitconfig_get_str(gitconfig,
229 6480c871 2021-08-30 stsp node->field, "url");
230 6480c871 2021-08-30 stsp if (remotes[i].send_url == NULL) {
231 6480c871 2021-08-30 stsp err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
235 469dd726 2020-03-20 stsp remotes[i].mirror_references = 0;
236 469dd726 2020-03-20 stsp mirror = got_gitconfig_get_str(gitconfig, node->field,
238 20b7abb3 2020-10-22 stsp if (mirror != NULL && get_boolean_val(mirror))
239 469dd726 2020-03-20 stsp remotes[i].mirror_references = 1;
244 e70bf110 2020-03-22 stsp err = send_gitconfig_remotes(ibuf, remotes, nremotes);
246 cd95becd 2019-11-29 stsp for (i = 0; i < nremotes; i++)
247 cd95becd 2019-11-29 stsp free(remotes[i].name);
248 cd95becd 2019-11-29 stsp free(remotes);
249 cd95becd 2019-11-29 stsp got_gitconfig_free_list(sections);
250 cd95becd 2019-11-29 stsp return err;
253 9a1cc63f 2020-02-03 stsp static const struct got_error *
254 9a1cc63f 2020-02-03 stsp gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
256 9a1cc63f 2020-02-03 stsp char *value;
258 9a1cc63f 2020-02-03 stsp if (gitconfig == NULL)
259 9a1cc63f 2020-02-03 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
261 9a1cc63f 2020-02-03 stsp value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
263 e70bf110 2020-03-22 stsp return send_gitconfig_str(ibuf, value);
264 9a1cc63f 2020-02-03 stsp value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
265 e70bf110 2020-03-22 stsp return send_gitconfig_str(ibuf, value);
268 20b7abb3 2020-10-22 stsp static const struct got_error *
269 20b7abb3 2020-10-22 stsp gitconfig_extensions_request(struct imsgbuf *ibuf,
270 20b7abb3 2020-10-22 stsp struct got_gitconfig *gitconfig)
272 20b7abb3 2020-10-22 stsp const struct got_error *err = NULL;
273 20b7abb3 2020-10-22 stsp struct got_gitconfig_list *tags;
274 20b7abb3 2020-10-22 stsp struct got_gitconfig_list_node *node;
275 20b7abb3 2020-10-22 stsp int nextensions = 0;
278 20b7abb3 2020-10-22 stsp if (gitconfig == NULL)
279 20b7abb3 2020-10-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
281 20b7abb3 2020-10-22 stsp tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
282 20b7abb3 2020-10-22 stsp if (tags == NULL)
283 20b7abb3 2020-10-22 stsp return send_gitconfig_int(ibuf, 0);
285 20b7abb3 2020-10-22 stsp TAILQ_FOREACH(node, &tags->fields, link) {
286 20b7abb3 2020-10-22 stsp val = got_gitconfig_get_str(gitconfig, "extensions",
287 20b7abb3 2020-10-22 stsp node->field);
288 01f02e60 2023-02-04 op if (get_boolean_val(val) ||
289 01f02e60 2023-02-04 op !strcmp(node->field, "objectformat"))
290 20b7abb3 2020-10-22 stsp nextensions++;
293 20b7abb3 2020-10-22 stsp err = send_gitconfig_int(ibuf, nextensions);
297 20b7abb3 2020-10-22 stsp TAILQ_FOREACH(node, &tags->fields, link) {
298 20b7abb3 2020-10-22 stsp val = got_gitconfig_get_str(gitconfig, "extensions",
299 20b7abb3 2020-10-22 stsp node->field);
300 01f02e60 2023-02-04 op if (get_boolean_val(val) ||
301 01f02e60 2023-02-04 op !strcmp(node->field, "objectformat")) {
302 01f02e60 2023-02-04 op /* HACK for objectformat: send 'sha256' */
303 01f02e60 2023-02-04 op err = send_gitconfig_str(ibuf, val);
309 20b7abb3 2020-10-22 stsp got_gitconfig_free_list(tags);
310 20b7abb3 2020-10-22 stsp return err;
314 aba9c984 2019-09-08 stsp main(int argc, char *argv[])
316 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
317 aba9c984 2019-09-08 stsp struct imsgbuf ibuf;
318 aba9c984 2019-09-08 stsp size_t datalen;
319 aba9c984 2019-09-08 stsp struct got_gitconfig *gitconfig = NULL;
321 aba9c984 2019-09-08 stsp static int attached;
323 aba9c984 2019-09-08 stsp while (!attached)
326 aba9c984 2019-09-08 stsp signal(SIGINT, catch_sigint);
328 aba9c984 2019-09-08 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
330 aba9c984 2019-09-08 stsp #ifndef PROFILE
331 aba9c984 2019-09-08 stsp /* revoke access to most system calls */
332 aba9c984 2019-09-08 stsp if (pledge("stdio recvfd", NULL) == -1) {
333 aba9c984 2019-09-08 stsp err = got_error_from_errno("pledge");
334 aba9c984 2019-09-08 stsp got_privsep_send_error(&ibuf, err);
340 aba9c984 2019-09-08 stsp struct imsg imsg;
342 aba9c984 2019-09-08 stsp memset(&imsg, 0, sizeof(imsg));
343 aba9c984 2019-09-08 stsp imsg.fd = -1;
345 aba9c984 2019-09-08 stsp if (sigint_received) {
346 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_CANCELLED);
350 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
352 aba9c984 2019-09-08 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
353 aba9c984 2019-09-08 stsp err = NULL;
357 aba9c984 2019-09-08 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
360 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
361 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
362 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
363 aba9c984 2019-09-08 stsp if (datalen != 0) {
364 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
367 aba9c984 2019-09-08 stsp if (imsg.fd == -1){
368 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
372 aba9c984 2019-09-08 stsp if (gitconfig)
373 aba9c984 2019-09-08 stsp got_gitconfig_close(gitconfig);
374 aba9c984 2019-09-08 stsp err = got_gitconfig_open(&gitconfig, imsg.fd);
376 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
377 aba9c984 2019-09-08 stsp err = gitconfig_num_request(&ibuf, gitconfig, "core",
378 aba9c984 2019-09-08 stsp "repositoryformatversion", 0);
380 20b7abb3 2020-10-22 stsp case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
381 20b7abb3 2020-10-22 stsp err = gitconfig_extensions_request(&ibuf, gitconfig);
383 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
384 aba9c984 2019-09-08 stsp err = gitconfig_str_request(&ibuf, gitconfig, "user",
387 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
388 aba9c984 2019-09-08 stsp err = gitconfig_str_request(&ibuf, gitconfig, "user",
391 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
392 cd95becd 2019-11-29 stsp err = gitconfig_remotes_request(&ibuf, gitconfig);
394 9a1cc63f 2020-02-03 stsp case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
395 9a1cc63f 2020-02-03 stsp err = gitconfig_owner_request(&ibuf, gitconfig);
398 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
402 aba9c984 2019-09-08 stsp if (imsg.fd != -1) {
403 aba9c984 2019-09-08 stsp if (close(imsg.fd) == -1 && err == NULL)
404 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
407 aba9c984 2019-09-08 stsp imsg_free(&imsg);
412 aba9c984 2019-09-08 stsp imsg_clear(&ibuf);
414 aba9c984 2019-09-08 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
415 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
416 aba9c984 2019-09-08 stsp got_privsep_send_error(&ibuf, err);
419 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
420 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
421 aba9c984 2019-09-08 stsp return err ? 1 : 0;