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>
21 #include <sys/syslimits.h>
23 #include <stdint.h>
24 #include <imsg.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sha1.h>
31 #include <zlib.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_object.h"
39 #include "got_lib_privsep.h"
40 #include "got_lib_gitconfig.h"
42 static volatile sig_atomic_t sigint_received;
44 static void
45 catch_sigint(int signo)
46 {
47 sigint_received = 1;
48 }
50 static const struct got_error *
51 send_gitconfig_int(struct imsgbuf *ibuf, int value)
52 {
53 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
54 &value, sizeof(value)) == -1)
55 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
57 return got_privsep_flush_imsg(ibuf);
58 }
60 static const struct got_error *
61 gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
62 char *section, char *tag, int def)
63 {
64 int value;
66 if (gitconfig == NULL)
67 return got_error(GOT_ERR_PRIVSEP_MSG);
69 value = got_gitconfig_get_num(gitconfig, section, tag, def);
70 return send_gitconfig_int(ibuf, value);
71 }
73 static const struct got_error *
74 send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
75 {
76 size_t len = value ? strlen(value) + 1 : 0;
78 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
79 value, len) == -1)
80 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
82 return got_privsep_flush_imsg(ibuf);
83 }
85 static const struct got_error *
86 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
87 char *section, char *tag)
88 {
89 char *value;
91 if (gitconfig == NULL)
92 return got_error(GOT_ERR_PRIVSEP_MSG);
94 value = got_gitconfig_get_str(gitconfig, section, tag);
95 return send_gitconfig_str(ibuf, value);
96 }
98 static const struct got_error *
99 send_gitconfig_remotes(struct imsgbuf *ibuf, struct got_remote_repo *remotes,
100 int nremotes)
102 const struct got_error *err = NULL;
103 struct got_imsg_remotes iremotes;
104 int i;
106 iremotes.nremotes = nremotes;
107 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
108 &iremotes, sizeof(iremotes)) == -1)
109 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
111 err = got_privsep_flush_imsg(ibuf);
112 imsg_clear(ibuf);
113 if (err)
114 return err;
116 for (i = 0; i < nremotes; i++) {
117 struct got_imsg_remote iremote;
118 size_t len = sizeof(iremote);
119 struct ibuf *wbuf;
121 iremote.mirror_references = remotes[i].mirror_references;
122 iremote.name_len = strlen(remotes[i].name);
123 len += iremote.name_len;
124 iremote.url_len = strlen(remotes[i].url);
125 len += iremote.url_len;
127 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
128 if (wbuf == NULL)
129 return got_error_from_errno(
130 "imsg_create GITCONFIG_REMOTE");
132 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
133 err = got_error_from_errno(
134 "imsg_add GITCONFIG_REMOTE");
135 ibuf_free(wbuf);
136 return err;
139 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
140 err = got_error_from_errno(
141 "imsg_add GITCONFIG_REMOTE");
142 ibuf_free(wbuf);
143 return err;
145 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
146 err = got_error_from_errno(
147 "imsg_add GITCONFIG_REMOTE");
148 ibuf_free(wbuf);
149 return err;
152 wbuf->fd = -1;
153 imsg_close(ibuf, wbuf);
154 err = got_privsep_flush_imsg(ibuf);
155 if (err)
156 return err;
159 return NULL;
163 static const struct got_error *
164 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
166 const struct got_error *err = NULL;
167 struct got_gitconfig_list *sections;
168 struct got_gitconfig_list_node *node;
169 struct got_remote_repo *remotes = NULL;
170 int nremotes = 0, i;
172 if (gitconfig == NULL)
173 return got_error(GOT_ERR_PRIVSEP_MSG);
175 err = got_gitconfig_get_section_list(&sections, gitconfig);
176 if (err)
177 return err;
179 TAILQ_FOREACH(node, &sections->fields, link) {
180 if (strncasecmp("remote \"", node->field, 8) != 0)
181 continue;
182 nremotes++;
185 if (nremotes == 0) {
186 err = send_gitconfig_remotes(ibuf, NULL, 0);
187 goto done;
190 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
191 if (remotes == NULL) {
192 err = got_error_from_errno("recallocarray");
193 goto done;
196 i = 0;
197 TAILQ_FOREACH(node, &sections->fields, link) {
198 char *name, *end, *mirror;
200 if (strncasecmp("remote \"", node->field, 8) != 0)
201 continue;
203 name = strdup(node->field + 8);
204 if (name == NULL) {
205 err = got_error_from_errno("strdup");
206 goto done;
208 end = strrchr(name, '"');
209 if (end)
210 *end = '\0';
211 remotes[i].name = name;
213 remotes[i].url = got_gitconfig_get_str(gitconfig,
214 node->field, "url");
215 if (remotes[i].url == NULL) {
216 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
217 goto done;
220 remotes[i].mirror_references = 0;
221 mirror = got_gitconfig_get_str(gitconfig, node->field,
222 "mirror");
223 if (mirror != NULL &&
224 (strcasecmp(mirror, "true") == 0 ||
225 strcasecmp(mirror, "on") == 0 ||
226 strcasecmp(mirror, "yes") == 0 ||
227 strcmp(mirror, "1") == 0))
228 remotes[i].mirror_references = 1;
230 i++;
233 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
234 done:
235 for (i = 0; i < nremotes; i++)
236 free(remotes[i].name);
237 free(remotes);
238 got_gitconfig_free_list(sections);
239 return err;
242 static const struct got_error *
243 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
245 char *value;
247 if (gitconfig == NULL)
248 return got_error(GOT_ERR_PRIVSEP_MSG);
250 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
251 if (value)
252 return send_gitconfig_str(ibuf, value);
253 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
254 return send_gitconfig_str(ibuf, value);
257 int
258 main(int argc, char *argv[])
260 const struct got_error *err = NULL;
261 struct imsgbuf ibuf;
262 size_t datalen;
263 struct got_gitconfig *gitconfig = NULL;
264 #if 0
265 static int attached;
267 while (!attached)
268 sleep(1);
269 #endif
270 signal(SIGINT, catch_sigint);
272 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
274 #ifndef PROFILE
275 /* revoke access to most system calls */
276 if (pledge("stdio recvfd", NULL) == -1) {
277 err = got_error_from_errno("pledge");
278 got_privsep_send_error(&ibuf, err);
279 return 1;
281 #endif
283 for (;;) {
284 struct imsg imsg;
286 memset(&imsg, 0, sizeof(imsg));
287 imsg.fd = -1;
289 if (sigint_received) {
290 err = got_error(GOT_ERR_CANCELLED);
291 break;
294 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
295 if (err) {
296 if (err->code == GOT_ERR_PRIVSEP_PIPE)
297 err = NULL;
298 break;
301 if (imsg.hdr.type == GOT_IMSG_STOP)
302 break;
304 switch (imsg.hdr.type) {
305 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
306 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
307 if (datalen != 0) {
308 err = got_error(GOT_ERR_PRIVSEP_LEN);
309 break;
311 if (imsg.fd == -1){
312 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
313 break;
316 if (gitconfig)
317 got_gitconfig_close(gitconfig);
318 err = got_gitconfig_open(&gitconfig, imsg.fd);
319 break;
320 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
321 err = gitconfig_num_request(&ibuf, gitconfig, "core",
322 "repositoryformatversion", 0);
323 break;
324 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
325 err = gitconfig_str_request(&ibuf, gitconfig, "user",
326 "name");
327 break;
328 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
329 err = gitconfig_str_request(&ibuf, gitconfig, "user",
330 "email");
331 break;
332 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
333 err = gitconfig_remotes_request(&ibuf, gitconfig);
334 break;
335 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
336 err = gitconfig_owner_request(&ibuf, gitconfig);
337 break;
338 default:
339 err = got_error(GOT_ERR_PRIVSEP_MSG);
340 break;
343 if (imsg.fd != -1) {
344 if (close(imsg.fd) == -1 && err == NULL)
345 err = got_error_from_errno("close");
348 imsg_free(&imsg);
349 if (err)
350 break;
353 imsg_clear(&ibuf);
354 if (err) {
355 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
356 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
357 got_privsep_send_error(&ibuf, err);
360 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
361 err = got_error_from_errno("close");
362 return err ? 1 : 0;