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 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) : 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;
162 static int
163 get_boolean_val(char *val)
165 return (strcasecmp(val, "true") == 0 ||
166 strcasecmp(val, "on") == 0 ||
167 strcasecmp(val, "yes") == 0 ||
168 strcmp(val, "1") == 0);
171 static const struct got_error *
172 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
174 const struct got_error *err = NULL;
175 struct got_gitconfig_list *sections;
176 struct got_gitconfig_list_node *node;
177 struct got_remote_repo *remotes = NULL;
178 int nremotes = 0, i;
180 if (gitconfig == NULL)
181 return got_error(GOT_ERR_PRIVSEP_MSG);
183 err = got_gitconfig_get_section_list(&sections, gitconfig);
184 if (err)
185 return err;
187 TAILQ_FOREACH(node, &sections->fields, link) {
188 if (strncasecmp("remote \"", node->field, 8) != 0)
189 continue;
190 nremotes++;
193 if (nremotes == 0) {
194 err = send_gitconfig_remotes(ibuf, NULL, 0);
195 goto done;
198 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
199 if (remotes == NULL) {
200 err = got_error_from_errno("recallocarray");
201 goto done;
204 i = 0;
205 TAILQ_FOREACH(node, &sections->fields, link) {
206 char *name, *end, *mirror;
208 if (strncasecmp("remote \"", node->field, 8) != 0)
209 continue;
211 name = strdup(node->field + 8);
212 if (name == NULL) {
213 err = got_error_from_errno("strdup");
214 goto done;
216 end = strrchr(name, '"');
217 if (end)
218 *end = '\0';
219 remotes[i].name = name;
221 remotes[i].url = got_gitconfig_get_str(gitconfig,
222 node->field, "url");
223 if (remotes[i].url == NULL) {
224 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
225 goto done;
228 remotes[i].mirror_references = 0;
229 mirror = got_gitconfig_get_str(gitconfig, node->field,
230 "mirror");
231 if (mirror != NULL && get_boolean_val(mirror))
232 remotes[i].mirror_references = 1;
234 i++;
237 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
238 done:
239 for (i = 0; i < nremotes; i++)
240 free(remotes[i].name);
241 free(remotes);
242 got_gitconfig_free_list(sections);
243 return err;
246 static const struct got_error *
247 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
249 char *value;
251 if (gitconfig == NULL)
252 return got_error(GOT_ERR_PRIVSEP_MSG);
254 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
255 if (value)
256 return send_gitconfig_str(ibuf, value);
257 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
258 return send_gitconfig_str(ibuf, value);
261 static const struct got_error *
262 gitconfig_extensions_request(struct imsgbuf *ibuf,
263 struct got_gitconfig *gitconfig)
265 const struct got_error *err = NULL;
266 struct got_gitconfig_list *tags;
267 struct got_gitconfig_list_node *node;
268 int nextensions = 0;
269 char *val;
271 if (gitconfig == NULL)
272 return got_error(GOT_ERR_PRIVSEP_MSG);
274 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
275 if (tags == NULL)
276 return send_gitconfig_int(ibuf, 0);
278 TAILQ_FOREACH(node, &tags->fields, link) {
279 val = got_gitconfig_get_str(gitconfig, "extensions",
280 node->field);
281 if (get_boolean_val(val))
282 nextensions++;
285 err = send_gitconfig_int(ibuf, nextensions);
286 if (err)
287 goto done;
289 TAILQ_FOREACH(node, &tags->fields, link) {
290 val = got_gitconfig_get_str(gitconfig, "extensions",
291 node->field);
292 if (get_boolean_val(val)) {
293 err = send_gitconfig_str(ibuf, node->field);
294 if (err)
295 goto done;
298 done:
299 got_gitconfig_free_list(tags);
300 return err;
303 int
304 main(int argc, char *argv[])
306 const struct got_error *err = NULL;
307 struct imsgbuf ibuf;
308 size_t datalen;
309 struct got_gitconfig *gitconfig = NULL;
310 #if 0
311 static int attached;
313 while (!attached)
314 sleep(1);
315 #endif
316 signal(SIGINT, catch_sigint);
318 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
320 #ifndef PROFILE
321 /* revoke access to most system calls */
322 if (pledge("stdio recvfd", NULL) == -1) {
323 err = got_error_from_errno("pledge");
324 got_privsep_send_error(&ibuf, err);
325 return 1;
327 #endif
329 for (;;) {
330 struct imsg imsg;
332 memset(&imsg, 0, sizeof(imsg));
333 imsg.fd = -1;
335 if (sigint_received) {
336 err = got_error(GOT_ERR_CANCELLED);
337 break;
340 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
341 if (err) {
342 if (err->code == GOT_ERR_PRIVSEP_PIPE)
343 err = NULL;
344 break;
347 if (imsg.hdr.type == GOT_IMSG_STOP)
348 break;
350 switch (imsg.hdr.type) {
351 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
352 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
353 if (datalen != 0) {
354 err = got_error(GOT_ERR_PRIVSEP_LEN);
355 break;
357 if (imsg.fd == -1){
358 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
359 break;
362 if (gitconfig)
363 got_gitconfig_close(gitconfig);
364 err = got_gitconfig_open(&gitconfig, imsg.fd);
365 break;
366 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
367 err = gitconfig_num_request(&ibuf, gitconfig, "core",
368 "repositoryformatversion", 0);
369 break;
370 case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
371 err = gitconfig_extensions_request(&ibuf, gitconfig);
372 break;
373 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
374 err = gitconfig_str_request(&ibuf, gitconfig, "user",
375 "name");
376 break;
377 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
378 err = gitconfig_str_request(&ibuf, gitconfig, "user",
379 "email");
380 break;
381 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
382 err = gitconfig_remotes_request(&ibuf, gitconfig);
383 break;
384 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
385 err = gitconfig_owner_request(&ibuf, gitconfig);
386 break;
387 default:
388 err = got_error(GOT_ERR_PRIVSEP_MSG);
389 break;
392 if (imsg.fd != -1) {
393 if (close(imsg.fd) == -1 && err == NULL)
394 err = got_error_from_errno("close");
397 imsg_free(&imsg);
398 if (err)
399 break;
402 imsg_clear(&ibuf);
403 if (err) {
404 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
405 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
406 got_privsep_send_error(&ibuf, err);
409 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
410 err = got_error_from_errno("close");
411 return err ? 1 : 0;