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>
21 aba9c984 2019-09-08 stsp #include <sys/syslimits.h>
23 aba9c984 2019-09-08 stsp #include <stdint.h>
24 aba9c984 2019-09-08 stsp #include <imsg.h>
25 aba9c984 2019-09-08 stsp #include <limits.h>
26 aba9c984 2019-09-08 stsp #include <signal.h>
27 aba9c984 2019-09-08 stsp #include <stdio.h>
28 aba9c984 2019-09-08 stsp #include <stdlib.h>
29 aba9c984 2019-09-08 stsp #include <string.h>
30 aba9c984 2019-09-08 stsp #include <sha1.h>
31 aba9c984 2019-09-08 stsp #include <zlib.h>
33 aba9c984 2019-09-08 stsp #include "got_error.h"
34 aba9c984 2019-09-08 stsp #include "got_object.h"
35 cd95becd 2019-11-29 stsp #include "got_repository.h"
37 aba9c984 2019-09-08 stsp #include "got_lib_delta.h"
38 aba9c984 2019-09-08 stsp #include "got_lib_object.h"
39 aba9c984 2019-09-08 stsp #include "got_lib_privsep.h"
40 aba9c984 2019-09-08 stsp #include "got_lib_gitconfig.h"
42 aba9c984 2019-09-08 stsp static volatile sig_atomic_t sigint_received;
45 aba9c984 2019-09-08 stsp catch_sigint(int signo)
47 aba9c984 2019-09-08 stsp sigint_received = 1;
50 aba9c984 2019-09-08 stsp static const struct got_error *
51 aba9c984 2019-09-08 stsp gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
52 aba9c984 2019-09-08 stsp char *section, char *tag, int def)
56 aba9c984 2019-09-08 stsp if (gitconfig == NULL)
57 aba9c984 2019-09-08 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
59 aba9c984 2019-09-08 stsp value = got_gitconfig_get_num(gitconfig, section, tag, def);
60 aba9c984 2019-09-08 stsp return got_privsep_send_gitconfig_int(ibuf, value);
63 aba9c984 2019-09-08 stsp static const struct got_error *
64 aba9c984 2019-09-08 stsp gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
65 aba9c984 2019-09-08 stsp char *section, char *tag)
67 aba9c984 2019-09-08 stsp char *value;
69 aba9c984 2019-09-08 stsp if (gitconfig == NULL)
70 aba9c984 2019-09-08 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
72 aba9c984 2019-09-08 stsp value = got_gitconfig_get_str(gitconfig, section, tag);
73 aba9c984 2019-09-08 stsp return got_privsep_send_gitconfig_str(ibuf, value);
76 cd95becd 2019-11-29 stsp static const struct got_error *
77 cd95becd 2019-11-29 stsp gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
79 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
80 cd95becd 2019-11-29 stsp struct got_gitconfig_list *sections;
81 cd95becd 2019-11-29 stsp struct got_gitconfig_list_node *node;
82 cd95becd 2019-11-29 stsp struct got_remote_repo *remotes = NULL;
83 cd95becd 2019-11-29 stsp int nremotes = 0, i;
85 cd95becd 2019-11-29 stsp if (gitconfig == NULL)
86 cd95becd 2019-11-29 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
88 cd95becd 2019-11-29 stsp err = got_gitconfig_get_section_list(§ions, gitconfig);
92 cd95becd 2019-11-29 stsp TAILQ_FOREACH(node, §ions->fields, link) {
93 cd95becd 2019-11-29 stsp if (strncasecmp("remote \"", node->field, 8) != 0)
98 cd95becd 2019-11-29 stsp if (nremotes == 0) {
99 cd95becd 2019-11-29 stsp err = got_privsep_send_gitconfig_remotes(ibuf, NULL, 0);
103 cd95becd 2019-11-29 stsp remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
104 cd95becd 2019-11-29 stsp if (remotes == NULL) {
105 cd95becd 2019-11-29 stsp err = got_error_from_errno("recallocarray");
110 cd95becd 2019-11-29 stsp TAILQ_FOREACH(node, §ions->fields, link) {
111 cd95becd 2019-11-29 stsp char *name, *end;
113 cd95becd 2019-11-29 stsp if (strncasecmp("remote \"", node->field, 8) != 0)
116 cd95becd 2019-11-29 stsp name = strdup(node->field + 8);
117 cd95becd 2019-11-29 stsp if (name == NULL) {
118 cd95becd 2019-11-29 stsp err = got_error_from_errno("strdup");
121 cd95becd 2019-11-29 stsp end = strrchr(name, '"');
123 cd95becd 2019-11-29 stsp *end = '\0';
124 cd95becd 2019-11-29 stsp remotes[i].name = name;
126 cd95becd 2019-11-29 stsp remotes[i].url = got_gitconfig_get_str(gitconfig,
127 cd95becd 2019-11-29 stsp node->field, "url");
128 cd95becd 2019-11-29 stsp if (remotes[i].url == NULL) {
129 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
136 cd95becd 2019-11-29 stsp err = got_privsep_send_gitconfig_remotes(ibuf, remotes, nremotes);
138 cd95becd 2019-11-29 stsp for (i = 0; i < nremotes; i++)
139 cd95becd 2019-11-29 stsp free(remotes[i].name);
140 cd95becd 2019-11-29 stsp free(remotes);
141 cd95becd 2019-11-29 stsp got_gitconfig_free_list(sections);
142 cd95becd 2019-11-29 stsp return err;
145 9a1cc63f 2020-02-03 stsp static const struct got_error *
146 9a1cc63f 2020-02-03 stsp gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
148 9a1cc63f 2020-02-03 stsp char *value;
150 9a1cc63f 2020-02-03 stsp if (gitconfig == NULL)
151 9a1cc63f 2020-02-03 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
153 9a1cc63f 2020-02-03 stsp value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
155 9a1cc63f 2020-02-03 stsp return got_privsep_send_gitconfig_str(ibuf, value);
156 9a1cc63f 2020-02-03 stsp value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
157 9a1cc63f 2020-02-03 stsp return got_privsep_send_gitconfig_str(ibuf, value);
161 aba9c984 2019-09-08 stsp main(int argc, char *argv[])
163 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
164 aba9c984 2019-09-08 stsp struct imsgbuf ibuf;
165 aba9c984 2019-09-08 stsp size_t datalen;
166 aba9c984 2019-09-08 stsp struct got_gitconfig *gitconfig = NULL;
168 aba9c984 2019-09-08 stsp static int attached;
170 aba9c984 2019-09-08 stsp while (!attached)
173 aba9c984 2019-09-08 stsp signal(SIGINT, catch_sigint);
175 aba9c984 2019-09-08 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
177 aba9c984 2019-09-08 stsp #ifndef PROFILE
178 aba9c984 2019-09-08 stsp /* revoke access to most system calls */
179 aba9c984 2019-09-08 stsp if (pledge("stdio recvfd", NULL) == -1) {
180 aba9c984 2019-09-08 stsp err = got_error_from_errno("pledge");
181 aba9c984 2019-09-08 stsp got_privsep_send_error(&ibuf, err);
187 aba9c984 2019-09-08 stsp struct imsg imsg;
189 aba9c984 2019-09-08 stsp memset(&imsg, 0, sizeof(imsg));
190 aba9c984 2019-09-08 stsp imsg.fd = -1;
192 aba9c984 2019-09-08 stsp if (sigint_received) {
193 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_CANCELLED);
197 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
199 aba9c984 2019-09-08 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
200 aba9c984 2019-09-08 stsp err = NULL;
204 aba9c984 2019-09-08 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
207 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
208 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
209 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
210 aba9c984 2019-09-08 stsp if (datalen != 0) {
211 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
214 aba9c984 2019-09-08 stsp if (imsg.fd == -1){
215 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
219 aba9c984 2019-09-08 stsp if (gitconfig)
220 aba9c984 2019-09-08 stsp got_gitconfig_close(gitconfig);
221 aba9c984 2019-09-08 stsp err = got_gitconfig_open(&gitconfig, imsg.fd);
223 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
224 aba9c984 2019-09-08 stsp err = gitconfig_num_request(&ibuf, gitconfig, "core",
225 aba9c984 2019-09-08 stsp "repositoryformatversion", 0);
227 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
228 aba9c984 2019-09-08 stsp err = gitconfig_str_request(&ibuf, gitconfig, "user",
231 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
232 aba9c984 2019-09-08 stsp err = gitconfig_str_request(&ibuf, gitconfig, "user",
235 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
236 cd95becd 2019-11-29 stsp err = gitconfig_remotes_request(&ibuf, gitconfig);
238 9a1cc63f 2020-02-03 stsp case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
239 9a1cc63f 2020-02-03 stsp err = gitconfig_owner_request(&ibuf, gitconfig);
242 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
246 aba9c984 2019-09-08 stsp if (imsg.fd != -1) {
247 aba9c984 2019-09-08 stsp if (close(imsg.fd) == -1 && err == NULL)
248 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
251 aba9c984 2019-09-08 stsp imsg_free(&imsg);
256 aba9c984 2019-09-08 stsp imsg_clear(&ibuf);
258 aba9c984 2019-09-08 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
259 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
260 aba9c984 2019-09-08 stsp got_privsep_send_error(&ibuf, err);
263 aba9c984 2019-09-08 stsp if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
264 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
265 aba9c984 2019-09-08 stsp return err ? 1 : 0;