Blame


1 aba9c984 2019-09-08 stsp /*
2 aba9c984 2019-09-08 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
3 aba9c984 2019-09-08 stsp *
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.
7 aba9c984 2019-09-08 stsp *
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.
15 aba9c984 2019-09-08 stsp */
16 aba9c984 2019-09-08 stsp
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>
22 aba9c984 2019-09-08 stsp
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>
32 aba9c984 2019-09-08 stsp
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"
36 aba9c984 2019-09-08 stsp
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"
41 aba9c984 2019-09-08 stsp
42 aba9c984 2019-09-08 stsp static volatile sig_atomic_t sigint_received;
43 aba9c984 2019-09-08 stsp
44 aba9c984 2019-09-08 stsp static void
45 aba9c984 2019-09-08 stsp catch_sigint(int signo)
46 aba9c984 2019-09-08 stsp {
47 aba9c984 2019-09-08 stsp sigint_received = 1;
48 aba9c984 2019-09-08 stsp }
49 aba9c984 2019-09-08 stsp
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)
53 aba9c984 2019-09-08 stsp {
54 aba9c984 2019-09-08 stsp int value;
55 aba9c984 2019-09-08 stsp
56 aba9c984 2019-09-08 stsp if (gitconfig == NULL)
57 aba9c984 2019-09-08 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
58 aba9c984 2019-09-08 stsp
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);
61 aba9c984 2019-09-08 stsp }
62 aba9c984 2019-09-08 stsp
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)
66 aba9c984 2019-09-08 stsp {
67 aba9c984 2019-09-08 stsp char *value;
68 aba9c984 2019-09-08 stsp
69 aba9c984 2019-09-08 stsp if (gitconfig == NULL)
70 aba9c984 2019-09-08 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
71 aba9c984 2019-09-08 stsp
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);
74 aba9c984 2019-09-08 stsp }
75 aba9c984 2019-09-08 stsp
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)
78 cd95becd 2019-11-29 stsp {
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;
84 cd95becd 2019-11-29 stsp
85 cd95becd 2019-11-29 stsp if (gitconfig == NULL)
86 cd95becd 2019-11-29 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
87 cd95becd 2019-11-29 stsp
88 cd95becd 2019-11-29 stsp err = got_gitconfig_get_section_list(&sections, gitconfig);
89 cd95becd 2019-11-29 stsp if (err)
90 cd95becd 2019-11-29 stsp return err;
91 cd95becd 2019-11-29 stsp
92 cd95becd 2019-11-29 stsp TAILQ_FOREACH(node, &sections->fields, link) {
93 cd95becd 2019-11-29 stsp if (strncasecmp("remote \"", node->field, 8) != 0)
94 cd95becd 2019-11-29 stsp continue;
95 cd95becd 2019-11-29 stsp nremotes++;
96 cd95becd 2019-11-29 stsp }
97 cd95becd 2019-11-29 stsp
98 cd95becd 2019-11-29 stsp if (nremotes == 0) {
99 cd95becd 2019-11-29 stsp err = got_privsep_send_gitconfig_remotes(ibuf, NULL, 0);
100 cd95becd 2019-11-29 stsp goto done;
101 cd95becd 2019-11-29 stsp }
102 cd95becd 2019-11-29 stsp
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");
106 cd95becd 2019-11-29 stsp goto done;
107 cd95becd 2019-11-29 stsp }
108 cd95becd 2019-11-29 stsp
109 cd95becd 2019-11-29 stsp i = 0;
110 cd95becd 2019-11-29 stsp TAILQ_FOREACH(node, &sections->fields, link) {
111 469dd726 2020-03-20 stsp char *name, *end, *mirror;
112 cd95becd 2019-11-29 stsp
113 cd95becd 2019-11-29 stsp if (strncasecmp("remote \"", node->field, 8) != 0)
114 cd95becd 2019-11-29 stsp continue;
115 cd95becd 2019-11-29 stsp
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");
119 cd95becd 2019-11-29 stsp goto done;
120 cd95becd 2019-11-29 stsp }
121 cd95becd 2019-11-29 stsp end = strrchr(name, '"');
122 cd95becd 2019-11-29 stsp if (end)
123 cd95becd 2019-11-29 stsp *end = '\0';
124 cd95becd 2019-11-29 stsp remotes[i].name = name;
125 cd95becd 2019-11-29 stsp
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);
130 cd95becd 2019-11-29 stsp goto done;
131 cd95becd 2019-11-29 stsp }
132 469dd726 2020-03-20 stsp
133 469dd726 2020-03-20 stsp remotes[i].mirror_references = 0;
134 469dd726 2020-03-20 stsp mirror = got_gitconfig_get_str(gitconfig, node->field,
135 469dd726 2020-03-20 stsp "mirror");
136 469dd726 2020-03-20 stsp if (mirror != NULL &&
137 469dd726 2020-03-20 stsp (strcasecmp(mirror, "true") == 0 ||
138 469dd726 2020-03-20 stsp strcasecmp(mirror, "on") == 0 ||
139 469dd726 2020-03-20 stsp strcasecmp(mirror, "yes") == 0 ||
140 469dd726 2020-03-20 stsp strcmp(mirror, "1") == 0))
141 469dd726 2020-03-20 stsp remotes[i].mirror_references = 1;
142 cd95becd 2019-11-29 stsp
143 cd95becd 2019-11-29 stsp i++;
144 cd95becd 2019-11-29 stsp }
145 cd95becd 2019-11-29 stsp
146 cd95becd 2019-11-29 stsp err = got_privsep_send_gitconfig_remotes(ibuf, remotes, nremotes);
147 cd95becd 2019-11-29 stsp done:
148 cd95becd 2019-11-29 stsp for (i = 0; i < nremotes; i++)
149 cd95becd 2019-11-29 stsp free(remotes[i].name);
150 cd95becd 2019-11-29 stsp free(remotes);
151 cd95becd 2019-11-29 stsp got_gitconfig_free_list(sections);
152 cd95becd 2019-11-29 stsp return err;
153 9a1cc63f 2020-02-03 stsp }
154 9a1cc63f 2020-02-03 stsp
155 9a1cc63f 2020-02-03 stsp static const struct got_error *
156 9a1cc63f 2020-02-03 stsp gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
157 9a1cc63f 2020-02-03 stsp {
158 9a1cc63f 2020-02-03 stsp char *value;
159 9a1cc63f 2020-02-03 stsp
160 9a1cc63f 2020-02-03 stsp if (gitconfig == NULL)
161 9a1cc63f 2020-02-03 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
162 9a1cc63f 2020-02-03 stsp
163 9a1cc63f 2020-02-03 stsp value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
164 9a1cc63f 2020-02-03 stsp if (value)
165 9a1cc63f 2020-02-03 stsp return got_privsep_send_gitconfig_str(ibuf, value);
166 9a1cc63f 2020-02-03 stsp value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
167 9a1cc63f 2020-02-03 stsp return got_privsep_send_gitconfig_str(ibuf, value);
168 cd95becd 2019-11-29 stsp }
169 cd95becd 2019-11-29 stsp
170 aba9c984 2019-09-08 stsp int
171 aba9c984 2019-09-08 stsp main(int argc, char *argv[])
172 aba9c984 2019-09-08 stsp {
173 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
174 aba9c984 2019-09-08 stsp struct imsgbuf ibuf;
175 aba9c984 2019-09-08 stsp size_t datalen;
176 aba9c984 2019-09-08 stsp struct got_gitconfig *gitconfig = NULL;
177 aba9c984 2019-09-08 stsp #if 0
178 aba9c984 2019-09-08 stsp static int attached;
179 aba9c984 2019-09-08 stsp
180 aba9c984 2019-09-08 stsp while (!attached)
181 aba9c984 2019-09-08 stsp sleep(1);
182 aba9c984 2019-09-08 stsp #endif
183 aba9c984 2019-09-08 stsp signal(SIGINT, catch_sigint);
184 aba9c984 2019-09-08 stsp
185 aba9c984 2019-09-08 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
186 aba9c984 2019-09-08 stsp
187 aba9c984 2019-09-08 stsp #ifndef PROFILE
188 aba9c984 2019-09-08 stsp /* revoke access to most system calls */
189 aba9c984 2019-09-08 stsp if (pledge("stdio recvfd", NULL) == -1) {
190 aba9c984 2019-09-08 stsp err = got_error_from_errno("pledge");
191 aba9c984 2019-09-08 stsp got_privsep_send_error(&ibuf, err);
192 aba9c984 2019-09-08 stsp return 1;
193 aba9c984 2019-09-08 stsp }
194 aba9c984 2019-09-08 stsp #endif
195 aba9c984 2019-09-08 stsp
196 aba9c984 2019-09-08 stsp for (;;) {
197 aba9c984 2019-09-08 stsp struct imsg imsg;
198 aba9c984 2019-09-08 stsp
199 aba9c984 2019-09-08 stsp memset(&imsg, 0, sizeof(imsg));
200 aba9c984 2019-09-08 stsp imsg.fd = -1;
201 aba9c984 2019-09-08 stsp
202 aba9c984 2019-09-08 stsp if (sigint_received) {
203 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_CANCELLED);
204 aba9c984 2019-09-08 stsp break;
205 aba9c984 2019-09-08 stsp }
206 aba9c984 2019-09-08 stsp
207 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
208 aba9c984 2019-09-08 stsp if (err) {
209 aba9c984 2019-09-08 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
210 aba9c984 2019-09-08 stsp err = NULL;
211 aba9c984 2019-09-08 stsp break;
212 aba9c984 2019-09-08 stsp }
213 aba9c984 2019-09-08 stsp
214 aba9c984 2019-09-08 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
215 aba9c984 2019-09-08 stsp break;
216 aba9c984 2019-09-08 stsp
217 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
218 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
219 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
220 aba9c984 2019-09-08 stsp if (datalen != 0) {
221 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
222 aba9c984 2019-09-08 stsp break;
223 aba9c984 2019-09-08 stsp }
224 aba9c984 2019-09-08 stsp if (imsg.fd == -1){
225 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
226 aba9c984 2019-09-08 stsp break;
227 aba9c984 2019-09-08 stsp }
228 aba9c984 2019-09-08 stsp
229 aba9c984 2019-09-08 stsp if (gitconfig)
230 aba9c984 2019-09-08 stsp got_gitconfig_close(gitconfig);
231 aba9c984 2019-09-08 stsp err = got_gitconfig_open(&gitconfig, imsg.fd);
232 aba9c984 2019-09-08 stsp break;
233 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
234 aba9c984 2019-09-08 stsp err = gitconfig_num_request(&ibuf, gitconfig, "core",
235 aba9c984 2019-09-08 stsp "repositoryformatversion", 0);
236 aba9c984 2019-09-08 stsp break;
237 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
238 aba9c984 2019-09-08 stsp err = gitconfig_str_request(&ibuf, gitconfig, "user",
239 aba9c984 2019-09-08 stsp "name");
240 aba9c984 2019-09-08 stsp break;
241 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
242 aba9c984 2019-09-08 stsp err = gitconfig_str_request(&ibuf, gitconfig, "user",
243 aba9c984 2019-09-08 stsp "email");
244 aba9c984 2019-09-08 stsp break;
245 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
246 cd95becd 2019-11-29 stsp err = gitconfig_remotes_request(&ibuf, gitconfig);
247 cd95becd 2019-11-29 stsp break;
248 9a1cc63f 2020-02-03 stsp case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
249 9a1cc63f 2020-02-03 stsp err = gitconfig_owner_request(&ibuf, gitconfig);
250 9a1cc63f 2020-02-03 stsp break;
251 aba9c984 2019-09-08 stsp default:
252 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
253 aba9c984 2019-09-08 stsp break;
254 aba9c984 2019-09-08 stsp }
255 aba9c984 2019-09-08 stsp
256 aba9c984 2019-09-08 stsp if (imsg.fd != -1) {
257 aba9c984 2019-09-08 stsp if (close(imsg.fd) == -1 && err == NULL)
258 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
259 aba9c984 2019-09-08 stsp }
260 aba9c984 2019-09-08 stsp
261 aba9c984 2019-09-08 stsp imsg_free(&imsg);
262 aba9c984 2019-09-08 stsp if (err)
263 aba9c984 2019-09-08 stsp break;
264 aba9c984 2019-09-08 stsp }
265 aba9c984 2019-09-08 stsp
266 aba9c984 2019-09-08 stsp imsg_clear(&ibuf);
267 aba9c984 2019-09-08 stsp if (err) {
268 aba9c984 2019-09-08 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
269 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
270 aba9c984 2019-09-08 stsp got_privsep_send_error(&ibuf, err);
271 aba9c984 2019-09-08 stsp }
272 aba9c984 2019-09-08 stsp }
273 aba9c984 2019-09-08 stsp if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
274 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
275 aba9c984 2019-09-08 stsp return err ? 1 : 0;
276 aba9c984 2019-09-08 stsp }