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>
22 #include <stdint.h>
23 #include <imsg.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sha1.h>
30 #include <unistd.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 const char *section, const 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) : 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 const char *section, const 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.fetch_url_len = strlen(remotes[i].fetch_url);
125 len += iremote.fetch_url_len;
126 iremote.send_url_len = strlen(remotes[i].send_url);
127 len += iremote.send_url_len;
129 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
130 if (wbuf == NULL)
131 return got_error_from_errno(
132 "imsg_create GITCONFIG_REMOTE");
134 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1)
135 return got_error_from_errno(
136 "imsg_add GITCONFIG_REMOTE");
138 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1)
139 return got_error_from_errno(
140 "imsg_add GITCONFIG_REMOTE");
141 if (imsg_add(wbuf, remotes[i].fetch_url, iremote.fetch_url_len) == -1)
142 return got_error_from_errno(
143 "imsg_add GITCONFIG_REMOTE");
144 if (imsg_add(wbuf, remotes[i].send_url, iremote.send_url_len) == -1)
145 return got_error_from_errno(
146 "imsg_add GITCONFIG_REMOTE");
148 wbuf->fd = -1;
149 imsg_close(ibuf, wbuf);
150 err = got_privsep_flush_imsg(ibuf);
151 if (err)
152 return err;
155 return NULL;
158 static int
159 get_boolean_val(char *val)
161 return (strcasecmp(val, "true") == 0 ||
162 strcasecmp(val, "on") == 0 ||
163 strcasecmp(val, "yes") == 0 ||
164 strcmp(val, "1") == 0);
167 static const struct got_error *
168 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
170 const struct got_error *err = NULL;
171 struct got_gitconfig_list *sections;
172 struct got_gitconfig_list_node *node;
173 struct got_remote_repo *remotes = NULL;
174 int nremotes = 0, i;
176 if (gitconfig == NULL)
177 return got_error(GOT_ERR_PRIVSEP_MSG);
179 err = got_gitconfig_get_section_list(&sections, gitconfig);
180 if (err)
181 return err;
183 TAILQ_FOREACH(node, &sections->fields, link) {
184 if (strncasecmp("remote \"", node->field, 8) != 0)
185 continue;
186 nremotes++;
189 if (nremotes == 0) {
190 err = send_gitconfig_remotes(ibuf, NULL, 0);
191 goto done;
194 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
195 if (remotes == NULL) {
196 err = got_error_from_errno("recallocarray");
197 goto done;
200 i = 0;
201 TAILQ_FOREACH(node, &sections->fields, link) {
202 char *name, *end, *mirror;
204 if (strncasecmp("remote \"", node->field, 8) != 0)
205 continue;
207 name = strdup(node->field + 8);
208 if (name == NULL) {
209 err = got_error_from_errno("strdup");
210 goto done;
212 end = strrchr(name, '"');
213 if (end)
214 *end = '\0';
215 remotes[i].name = name;
217 remotes[i].fetch_url = got_gitconfig_get_str(gitconfig,
218 node->field, "url");
219 if (remotes[i].fetch_url == NULL) {
220 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
221 goto done;
224 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
225 node->field, "pushurl");
226 if (remotes[i].send_url == NULL)
227 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
228 node->field, "url");
229 if (remotes[i].send_url == NULL) {
230 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
231 goto done;
234 remotes[i].mirror_references = 0;
235 mirror = got_gitconfig_get_str(gitconfig, node->field,
236 "mirror");
237 if (mirror != NULL && get_boolean_val(mirror))
238 remotes[i].mirror_references = 1;
240 i++;
243 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
244 done:
245 for (i = 0; i < nremotes; i++)
246 free(remotes[i].name);
247 free(remotes);
248 got_gitconfig_free_list(sections);
249 return err;
252 static const struct got_error *
253 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
255 char *value;
257 if (gitconfig == NULL)
258 return got_error(GOT_ERR_PRIVSEP_MSG);
260 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
261 if (value)
262 return send_gitconfig_str(ibuf, value);
263 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
264 return send_gitconfig_str(ibuf, value);
267 static const struct got_error *
268 gitconfig_extensions_request(struct imsgbuf *ibuf,
269 struct got_gitconfig *gitconfig)
271 const struct got_error *err = NULL;
272 struct got_gitconfig_list *tags;
273 struct got_gitconfig_list_node *node;
274 int nextensions = 0;
275 char *val;
277 if (gitconfig == NULL)
278 return got_error(GOT_ERR_PRIVSEP_MSG);
280 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
281 if (tags == NULL)
282 return send_gitconfig_int(ibuf, 0);
284 TAILQ_FOREACH(node, &tags->fields, link) {
285 val = got_gitconfig_get_str(gitconfig, "extensions",
286 node->field);
287 if (get_boolean_val(val))
288 nextensions++;
291 err = send_gitconfig_int(ibuf, nextensions);
292 if (err)
293 goto done;
295 TAILQ_FOREACH(node, &tags->fields, link) {
296 val = got_gitconfig_get_str(gitconfig, "extensions",
297 node->field);
298 if (get_boolean_val(val)) {
299 err = send_gitconfig_str(ibuf, node->field);
300 if (err)
301 goto done;
304 done:
305 got_gitconfig_free_list(tags);
306 return err;
309 int
310 main(int argc, char *argv[])
312 const struct got_error *err = NULL;
313 struct imsgbuf ibuf;
314 size_t datalen;
315 struct got_gitconfig *gitconfig = NULL;
316 #if 0
317 static int attached;
319 while (!attached)
320 sleep(1);
321 #endif
322 signal(SIGINT, catch_sigint);
324 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
326 #ifndef PROFILE
327 /* revoke access to most system calls */
328 if (pledge("stdio recvfd", NULL) == -1) {
329 err = got_error_from_errno("pledge");
330 got_privsep_send_error(&ibuf, err);
331 return 1;
333 #endif
335 for (;;) {
336 struct imsg imsg;
338 memset(&imsg, 0, sizeof(imsg));
339 imsg.fd = -1;
341 if (sigint_received) {
342 err = got_error(GOT_ERR_CANCELLED);
343 break;
346 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
347 if (err) {
348 if (err->code == GOT_ERR_PRIVSEP_PIPE)
349 err = NULL;
350 break;
353 if (imsg.hdr.type == GOT_IMSG_STOP)
354 break;
356 switch (imsg.hdr.type) {
357 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
358 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
359 if (datalen != 0) {
360 err = got_error(GOT_ERR_PRIVSEP_LEN);
361 break;
363 if (imsg.fd == -1){
364 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
365 break;
368 if (gitconfig)
369 got_gitconfig_close(gitconfig);
370 err = got_gitconfig_open(&gitconfig, imsg.fd);
371 break;
372 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
373 err = gitconfig_num_request(&ibuf, gitconfig, "core",
374 "repositoryformatversion", 0);
375 break;
376 case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
377 err = gitconfig_extensions_request(&ibuf, gitconfig);
378 break;
379 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
380 err = gitconfig_str_request(&ibuf, gitconfig, "user",
381 "name");
382 break;
383 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
384 err = gitconfig_str_request(&ibuf, gitconfig, "user",
385 "email");
386 break;
387 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
388 err = gitconfig_remotes_request(&ibuf, gitconfig);
389 break;
390 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
391 err = gitconfig_owner_request(&ibuf, gitconfig);
392 break;
393 default:
394 err = got_error(GOT_ERR_PRIVSEP_MSG);
395 break;
398 if (imsg.fd != -1) {
399 if (close(imsg.fd) == -1 && err == NULL)
400 err = got_error_from_errno("close");
403 imsg_free(&imsg);
404 if (err)
405 break;
408 imsg_clear(&ibuf);
409 if (err) {
410 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
411 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
412 got_privsep_send_error(&ibuf, err);
415 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
416 err = got_error_from_errno("close");
417 return err ? 1 : 0;