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 <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 char *section, 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) + 1 : 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 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
88 char *section, char *tag)
89 {
90 char *value;
92 if (gitconfig == NULL)
93 return got_error(GOT_ERR_PRIVSEP_MSG);
95 value = got_gitconfig_get_str(gitconfig, section, tag);
96 return send_gitconfig_str(ibuf, value);
97 }
99 static const struct got_error *
100 send_gitconfig_remotes(struct imsgbuf *ibuf, struct got_remote_repo *remotes,
101 int nremotes)
103 const struct got_error *err = NULL;
104 struct got_imsg_remotes iremotes;
105 int i;
107 iremotes.nremotes = nremotes;
108 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
109 &iremotes, sizeof(iremotes)) == -1)
110 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
112 err = got_privsep_flush_imsg(ibuf);
113 imsg_clear(ibuf);
114 if (err)
115 return err;
117 for (i = 0; i < nremotes; i++) {
118 struct got_imsg_remote iremote;
119 size_t len = sizeof(iremote);
120 struct ibuf *wbuf;
122 iremote.mirror_references = remotes[i].mirror_references;
123 iremote.name_len = strlen(remotes[i].name);
124 len += iremote.name_len;
125 iremote.url_len = strlen(remotes[i].url);
126 len += iremote.url_len;
128 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
129 if (wbuf == NULL)
130 return got_error_from_errno(
131 "imsg_create GITCONFIG_REMOTE");
133 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
134 err = got_error_from_errno(
135 "imsg_add GITCONFIG_REMOTE");
136 ibuf_free(wbuf);
137 return err;
140 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
141 err = got_error_from_errno(
142 "imsg_add GITCONFIG_REMOTE");
143 ibuf_free(wbuf);
144 return err;
146 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
147 err = got_error_from_errno(
148 "imsg_add GITCONFIG_REMOTE");
149 ibuf_free(wbuf);
150 return err;
153 wbuf->fd = -1;
154 imsg_close(ibuf, wbuf);
155 err = got_privsep_flush_imsg(ibuf);
156 if (err)
157 return err;
160 return NULL;
164 static const struct got_error *
165 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
167 const struct got_error *err = NULL;
168 struct got_gitconfig_list *sections;
169 struct got_gitconfig_list_node *node;
170 struct got_remote_repo *remotes = NULL;
171 int nremotes = 0, i;
173 if (gitconfig == NULL)
174 return got_error(GOT_ERR_PRIVSEP_MSG);
176 err = got_gitconfig_get_section_list(&sections, gitconfig);
177 if (err)
178 return err;
180 TAILQ_FOREACH(node, &sections->fields, link) {
181 if (strncasecmp("remote \"", node->field, 8) != 0)
182 continue;
183 nremotes++;
186 if (nremotes == 0) {
187 err = send_gitconfig_remotes(ibuf, NULL, 0);
188 goto done;
191 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
192 if (remotes == NULL) {
193 err = got_error_from_errno("recallocarray");
194 goto done;
197 i = 0;
198 TAILQ_FOREACH(node, &sections->fields, link) {
199 char *name, *end, *mirror;
201 if (strncasecmp("remote \"", node->field, 8) != 0)
202 continue;
204 name = strdup(node->field + 8);
205 if (name == NULL) {
206 err = got_error_from_errno("strdup");
207 goto done;
209 end = strrchr(name, '"');
210 if (end)
211 *end = '\0';
212 remotes[i].name = name;
214 remotes[i].url = got_gitconfig_get_str(gitconfig,
215 node->field, "url");
216 if (remotes[i].url == NULL) {
217 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
218 goto done;
221 remotes[i].mirror_references = 0;
222 mirror = got_gitconfig_get_str(gitconfig, node->field,
223 "mirror");
224 if (mirror != NULL &&
225 (strcasecmp(mirror, "true") == 0 ||
226 strcasecmp(mirror, "on") == 0 ||
227 strcasecmp(mirror, "yes") == 0 ||
228 strcmp(mirror, "1") == 0))
229 remotes[i].mirror_references = 1;
231 i++;
234 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
235 done:
236 for (i = 0; i < nremotes; i++)
237 free(remotes[i].name);
238 free(remotes);
239 got_gitconfig_free_list(sections);
240 return err;
243 static const struct got_error *
244 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
246 char *value;
248 if (gitconfig == NULL)
249 return got_error(GOT_ERR_PRIVSEP_MSG);
251 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
252 if (value)
253 return send_gitconfig_str(ibuf, value);
254 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
255 return send_gitconfig_str(ibuf, value);
258 int
259 main(int argc, char *argv[])
261 const struct got_error *err = NULL;
262 struct imsgbuf ibuf;
263 size_t datalen;
264 struct got_gitconfig *gitconfig = NULL;
265 #if 0
266 static int attached;
268 while (!attached)
269 sleep(1);
270 #endif
271 signal(SIGINT, catch_sigint);
273 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
275 #ifndef PROFILE
276 /* revoke access to most system calls */
277 if (pledge("stdio recvfd", NULL) == -1) {
278 err = got_error_from_errno("pledge");
279 got_privsep_send_error(&ibuf, err);
280 return 1;
282 #endif
284 for (;;) {
285 struct imsg imsg;
287 memset(&imsg, 0, sizeof(imsg));
288 imsg.fd = -1;
290 if (sigint_received) {
291 err = got_error(GOT_ERR_CANCELLED);
292 break;
295 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
296 if (err) {
297 if (err->code == GOT_ERR_PRIVSEP_PIPE)
298 err = NULL;
299 break;
302 if (imsg.hdr.type == GOT_IMSG_STOP)
303 break;
305 switch (imsg.hdr.type) {
306 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
307 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
308 if (datalen != 0) {
309 err = got_error(GOT_ERR_PRIVSEP_LEN);
310 break;
312 if (imsg.fd == -1){
313 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
314 break;
317 if (gitconfig)
318 got_gitconfig_close(gitconfig);
319 err = got_gitconfig_open(&gitconfig, imsg.fd);
320 break;
321 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
322 err = gitconfig_num_request(&ibuf, gitconfig, "core",
323 "repositoryformatversion", 0);
324 break;
325 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
326 err = gitconfig_str_request(&ibuf, gitconfig, "user",
327 "name");
328 break;
329 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
330 err = gitconfig_str_request(&ibuf, gitconfig, "user",
331 "email");
332 break;
333 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
334 err = gitconfig_remotes_request(&ibuf, gitconfig);
335 break;
336 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
337 err = gitconfig_owner_request(&ibuf, gitconfig);
338 break;
339 default:
340 err = got_error(GOT_ERR_PRIVSEP_MSG);
341 break;
344 if (imsg.fd != -1) {
345 if (close(imsg.fd) == -1 && err == NULL)
346 err = got_error_from_errno("close");
349 imsg_free(&imsg);
350 if (err)
351 break;
354 imsg_clear(&ibuf);
355 if (err) {
356 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
357 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
358 got_privsep_send_error(&ibuf, err);
361 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
362 err = got_error_from_errno("close");
363 return err ? 1 : 0;