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/uio.h>
19 #include <sys/time.h>
21 #include <stdint.h>
22 #include <limits.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <zlib.h>
30 #include "got_compat.h"
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_repository.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_object.h"
38 #include "got_lib_privsep.h"
39 #include "got_lib_gitconfig.h"
41 static volatile sig_atomic_t sigint_received;
43 static void
44 catch_sigint(int signo)
45 {
46 sigint_received = 1;
47 }
49 static const struct got_error *
50 send_gitconfig_int(struct imsgbuf *ibuf, int value)
51 {
52 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
53 &value, sizeof(value)) == -1)
54 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
56 return got_privsep_flush_imsg(ibuf);
57 }
59 static const struct got_error *
60 gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
61 const char *section, const char *tag, int def)
62 {
63 int value;
65 if (gitconfig == NULL)
66 return got_error(GOT_ERR_PRIVSEP_MSG);
68 value = got_gitconfig_get_num(gitconfig, section, tag, def);
69 return send_gitconfig_int(ibuf, value);
70 }
72 static const struct got_error *
73 send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
74 {
75 size_t len = value ? strlen(value) : 0;
77 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
78 value, len) == -1)
79 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
81 return got_privsep_flush_imsg(ibuf);
82 }
84 static const struct got_error *
85 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
86 const char *section, const char *tag)
87 {
88 char *value;
90 if (gitconfig == NULL)
91 return got_error(GOT_ERR_PRIVSEP_MSG);
93 value = got_gitconfig_get_str(gitconfig, section, tag);
94 return send_gitconfig_str(ibuf, value);
95 }
97 static const struct got_error *
98 send_gitconfig_remotes(struct imsgbuf *ibuf, struct got_remote_repo *remotes,
99 int nremotes)
101 const struct got_error *err = NULL;
102 struct got_imsg_remotes iremotes;
103 int i;
105 iremotes.nremotes = nremotes;
106 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
107 &iremotes, sizeof(iremotes)) == -1)
108 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
110 err = got_privsep_flush_imsg(ibuf);
111 imsg_clear(ibuf);
112 if (err)
113 return err;
115 for (i = 0; i < nremotes; i++) {
116 struct got_imsg_remote iremote;
117 size_t len = sizeof(iremote);
118 struct ibuf *wbuf;
120 iremote.mirror_references = remotes[i].mirror_references;
121 iremote.name_len = strlen(remotes[i].name);
122 len += iremote.name_len;
123 iremote.fetch_url_len = strlen(remotes[i].fetch_url);
124 len += iremote.fetch_url_len;
125 iremote.send_url_len = strlen(remotes[i].send_url);
126 len += iremote.send_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].fetch_url, iremote.fetch_url_len) == -1) {
147 err = got_error_from_errno(
148 "imsg_add GITCONFIG_REMOTE");
149 ibuf_free(wbuf);
150 return err;
152 if (imsg_add(wbuf, remotes[i].send_url, iremote.send_url_len) == -1) {
153 err = got_error_from_errno(
154 "imsg_add GITCONFIG_REMOTE");
155 ibuf_free(wbuf);
156 return err;
159 wbuf->fd = -1;
160 imsg_close(ibuf, wbuf);
161 err = got_privsep_flush_imsg(ibuf);
162 if (err)
163 return err;
166 return NULL;
169 static int
170 get_boolean_val(char *val)
172 return (strcasecmp(val, "true") == 0 ||
173 strcasecmp(val, "on") == 0 ||
174 strcasecmp(val, "yes") == 0 ||
175 strcmp(val, "1") == 0);
178 static const struct got_error *
179 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
181 const struct got_error *err = NULL;
182 struct got_gitconfig_list *sections;
183 struct got_gitconfig_list_node *node;
184 struct got_remote_repo *remotes = NULL;
185 int nremotes = 0, i;
187 if (gitconfig == NULL)
188 return got_error(GOT_ERR_PRIVSEP_MSG);
190 err = got_gitconfig_get_section_list(&sections, gitconfig);
191 if (err)
192 return err;
194 TAILQ_FOREACH(node, &sections->fields, link) {
195 if (strncasecmp("remote \"", node->field, 8) != 0)
196 continue;
197 nremotes++;
200 if (nremotes == 0) {
201 err = send_gitconfig_remotes(ibuf, NULL, 0);
202 goto done;
205 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
206 if (remotes == NULL) {
207 err = got_error_from_errno("recallocarray");
208 goto done;
211 i = 0;
212 TAILQ_FOREACH(node, &sections->fields, link) {
213 char *name, *end, *mirror;
215 if (strncasecmp("remote \"", node->field, 8) != 0)
216 continue;
218 name = strdup(node->field + 8);
219 if (name == NULL) {
220 err = got_error_from_errno("strdup");
221 goto done;
223 end = strrchr(name, '"');
224 if (end)
225 *end = '\0';
226 remotes[i].name = name;
228 remotes[i].fetch_url = got_gitconfig_get_str(gitconfig,
229 node->field, "url");
230 if (remotes[i].fetch_url == NULL) {
231 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
232 goto done;
235 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
236 node->field, "pushurl");
237 if (remotes[i].send_url == NULL)
238 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
239 node->field, "url");
240 if (remotes[i].send_url == NULL) {
241 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
242 goto done;
245 remotes[i].mirror_references = 0;
246 mirror = got_gitconfig_get_str(gitconfig, node->field,
247 "mirror");
248 if (mirror != NULL && get_boolean_val(mirror))
249 remotes[i].mirror_references = 1;
251 i++;
254 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
255 done:
256 for (i = 0; i < nremotes; i++)
257 free(remotes[i].name);
258 free(remotes);
259 got_gitconfig_free_list(sections);
260 return err;
263 static const struct got_error *
264 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
266 char *value;
268 if (gitconfig == NULL)
269 return got_error(GOT_ERR_PRIVSEP_MSG);
271 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
272 if (value)
273 return send_gitconfig_str(ibuf, value);
274 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
275 return send_gitconfig_str(ibuf, value);
278 static const struct got_error *
279 gitconfig_extensions_request(struct imsgbuf *ibuf,
280 struct got_gitconfig *gitconfig)
282 const struct got_error *err = NULL;
283 struct got_gitconfig_list *tags;
284 struct got_gitconfig_list_node *node;
285 int nextensions = 0;
286 char *val;
288 if (gitconfig == NULL)
289 return got_error(GOT_ERR_PRIVSEP_MSG);
291 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
292 if (tags == NULL)
293 return send_gitconfig_int(ibuf, 0);
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 nextensions++;
302 err = send_gitconfig_int(ibuf, nextensions);
303 if (err)
304 goto done;
306 TAILQ_FOREACH(node, &tags->fields, link) {
307 val = got_gitconfig_get_str(gitconfig, "extensions",
308 node->field);
309 if (get_boolean_val(val)) {
310 err = send_gitconfig_str(ibuf, node->field);
311 if (err)
312 goto done;
315 done:
316 got_gitconfig_free_list(tags);
317 return err;
320 int
321 main(int argc, char *argv[])
323 const struct got_error *err = NULL;
324 struct imsgbuf ibuf;
325 size_t datalen;
326 struct got_gitconfig *gitconfig = NULL;
327 #if 0
328 static int attached;
330 while (!attached)
331 sleep(1);
332 #endif
333 signal(SIGINT, catch_sigint);
335 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
337 #ifndef PROFILE
338 /* revoke access to most system calls */
339 if (pledge("stdio recvfd", NULL) == -1) {
340 err = got_error_from_errno("pledge");
341 got_privsep_send_error(&ibuf, err);
342 return 1;
344 #endif
346 for (;;) {
347 struct imsg imsg;
349 memset(&imsg, 0, sizeof(imsg));
350 imsg.fd = -1;
352 if (sigint_received) {
353 err = got_error(GOT_ERR_CANCELLED);
354 break;
357 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
358 if (err) {
359 if (err->code == GOT_ERR_PRIVSEP_PIPE)
360 err = NULL;
361 break;
364 if (imsg.hdr.type == GOT_IMSG_STOP)
365 break;
367 switch (imsg.hdr.type) {
368 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
369 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
370 if (datalen != 0) {
371 err = got_error(GOT_ERR_PRIVSEP_LEN);
372 break;
374 if (imsg.fd == -1){
375 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
376 break;
379 if (gitconfig)
380 got_gitconfig_close(gitconfig);
381 err = got_gitconfig_open(&gitconfig, imsg.fd);
382 break;
383 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
384 err = gitconfig_num_request(&ibuf, gitconfig, "core",
385 "repositoryformatversion", 0);
386 break;
387 case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
388 err = gitconfig_extensions_request(&ibuf, gitconfig);
389 break;
390 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
391 err = gitconfig_str_request(&ibuf, gitconfig, "user",
392 "name");
393 break;
394 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
395 err = gitconfig_str_request(&ibuf, gitconfig, "user",
396 "email");
397 break;
398 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
399 err = gitconfig_remotes_request(&ibuf, gitconfig);
400 break;
401 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
402 err = gitconfig_owner_request(&ibuf, gitconfig);
403 break;
404 default:
405 err = got_error(GOT_ERR_PRIVSEP_MSG);
406 break;
409 if (imsg.fd != -1) {
410 if (close(imsg.fd) == -1 && err == NULL)
411 err = got_error_from_errno("close");
414 imsg_free(&imsg);
415 if (err)
416 break;
419 imsg_clear(&ibuf);
420 if (err) {
421 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
422 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
423 got_privsep_send_error(&ibuf, err);
426 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
427 err = got_error_from_errno("close");
428 return err ? 1 : 0;