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.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 err = got_error_from_errno(
136 "imsg_add GITCONFIG_REMOTE");
137 ibuf_free(wbuf);
138 return err;
141 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
142 err = got_error_from_errno(
143 "imsg_add GITCONFIG_REMOTE");
144 ibuf_free(wbuf);
145 return err;
147 if (imsg_add(wbuf, remotes[i].fetch_url, iremote.fetch_url_len) == -1) {
148 err = got_error_from_errno(
149 "imsg_add GITCONFIG_REMOTE");
150 ibuf_free(wbuf);
151 return err;
153 if (imsg_add(wbuf, remotes[i].send_url, iremote.send_url_len) == -1) {
154 err = got_error_from_errno(
155 "imsg_add GITCONFIG_REMOTE");
156 ibuf_free(wbuf);
157 return err;
160 wbuf->fd = -1;
161 imsg_close(ibuf, wbuf);
162 err = got_privsep_flush_imsg(ibuf);
163 if (err)
164 return err;
167 return NULL;
170 static int
171 get_boolean_val(char *val)
173 return (strcasecmp(val, "true") == 0 ||
174 strcasecmp(val, "on") == 0 ||
175 strcasecmp(val, "yes") == 0 ||
176 strcmp(val, "1") == 0);
179 static const struct got_error *
180 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
182 const struct got_error *err = NULL;
183 struct got_gitconfig_list *sections;
184 struct got_gitconfig_list_node *node;
185 struct got_remote_repo *remotes = NULL;
186 int nremotes = 0, i;
188 if (gitconfig == NULL)
189 return got_error(GOT_ERR_PRIVSEP_MSG);
191 err = got_gitconfig_get_section_list(&sections, gitconfig);
192 if (err)
193 return err;
195 TAILQ_FOREACH(node, &sections->fields, link) {
196 if (strncasecmp("remote \"", node->field, 8) != 0)
197 continue;
198 nremotes++;
201 if (nremotes == 0) {
202 err = send_gitconfig_remotes(ibuf, NULL, 0);
203 goto done;
206 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
207 if (remotes == NULL) {
208 err = got_error_from_errno("recallocarray");
209 goto done;
212 i = 0;
213 TAILQ_FOREACH(node, &sections->fields, link) {
214 char *name, *end, *mirror;
216 if (strncasecmp("remote \"", node->field, 8) != 0)
217 continue;
219 name = strdup(node->field + 8);
220 if (name == NULL) {
221 err = got_error_from_errno("strdup");
222 goto done;
224 end = strrchr(name, '"');
225 if (end)
226 *end = '\0';
227 remotes[i].name = name;
229 remotes[i].fetch_url = got_gitconfig_get_str(gitconfig,
230 node->field, "url");
231 if (remotes[i].fetch_url == NULL) {
232 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
233 goto done;
236 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
237 node->field, "pushurl");
238 if (remotes[i].send_url == NULL)
239 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
240 node->field, "url");
241 if (remotes[i].send_url == NULL) {
242 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
243 goto done;
246 remotes[i].mirror_references = 0;
247 mirror = got_gitconfig_get_str(gitconfig, node->field,
248 "mirror");
249 if (mirror != NULL && get_boolean_val(mirror))
250 remotes[i].mirror_references = 1;
252 i++;
255 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
256 done:
257 for (i = 0; i < nremotes; i++)
258 free(remotes[i].name);
259 free(remotes);
260 got_gitconfig_free_list(sections);
261 return err;
264 static const struct got_error *
265 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
267 char *value;
269 if (gitconfig == NULL)
270 return got_error(GOT_ERR_PRIVSEP_MSG);
272 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
273 if (value)
274 return send_gitconfig_str(ibuf, value);
275 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
276 return send_gitconfig_str(ibuf, value);
279 static const struct got_error *
280 gitconfig_extensions_request(struct imsgbuf *ibuf,
281 struct got_gitconfig *gitconfig)
283 const struct got_error *err = NULL;
284 struct got_gitconfig_list *tags;
285 struct got_gitconfig_list_node *node;
286 int nextensions = 0;
287 char *val;
289 if (gitconfig == NULL)
290 return got_error(GOT_ERR_PRIVSEP_MSG);
292 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
293 if (tags == NULL)
294 return send_gitconfig_int(ibuf, 0);
296 TAILQ_FOREACH(node, &tags->fields, link) {
297 val = got_gitconfig_get_str(gitconfig, "extensions",
298 node->field);
299 if (get_boolean_val(val))
300 nextensions++;
303 err = send_gitconfig_int(ibuf, nextensions);
304 if (err)
305 goto done;
307 TAILQ_FOREACH(node, &tags->fields, link) {
308 val = got_gitconfig_get_str(gitconfig, "extensions",
309 node->field);
310 if (get_boolean_val(val)) {
311 err = send_gitconfig_str(ibuf, node->field);
312 if (err)
313 goto done;
316 done:
317 got_gitconfig_free_list(tags);
318 return err;
321 int
322 main(int argc, char *argv[])
324 const struct got_error *err = NULL;
325 struct imsgbuf ibuf;
326 size_t datalen;
327 struct got_gitconfig *gitconfig = NULL;
328 #if 0
329 static int attached;
331 while (!attached)
332 sleep(1);
333 #endif
334 signal(SIGINT, catch_sigint);
336 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
338 #ifndef PROFILE
339 /* revoke access to most system calls */
340 if (pledge("stdio recvfd", NULL) == -1) {
341 err = got_error_from_errno("pledge");
342 got_privsep_send_error(&ibuf, err);
343 return 1;
345 #endif
347 for (;;) {
348 struct imsg imsg;
350 memset(&imsg, 0, sizeof(imsg));
351 imsg.fd = -1;
353 if (sigint_received) {
354 err = got_error(GOT_ERR_CANCELLED);
355 break;
358 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
359 if (err) {
360 if (err->code == GOT_ERR_PRIVSEP_PIPE)
361 err = NULL;
362 break;
365 if (imsg.hdr.type == GOT_IMSG_STOP)
366 break;
368 switch (imsg.hdr.type) {
369 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
370 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
371 if (datalen != 0) {
372 err = got_error(GOT_ERR_PRIVSEP_LEN);
373 break;
375 if (imsg.fd == -1){
376 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
377 break;
380 if (gitconfig)
381 got_gitconfig_close(gitconfig);
382 err = got_gitconfig_open(&gitconfig, imsg.fd);
383 break;
384 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
385 err = gitconfig_num_request(&ibuf, gitconfig, "core",
386 "repositoryformatversion", 0);
387 break;
388 case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
389 err = gitconfig_extensions_request(&ibuf, gitconfig);
390 break;
391 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
392 err = gitconfig_str_request(&ibuf, gitconfig, "user",
393 "name");
394 break;
395 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
396 err = gitconfig_str_request(&ibuf, gitconfig, "user",
397 "email");
398 break;
399 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
400 err = gitconfig_remotes_request(&ibuf, gitconfig);
401 break;
402 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
403 err = gitconfig_owner_request(&ibuf, gitconfig);
404 break;
405 default:
406 err = got_error(GOT_ERR_PRIVSEP_MSG);
407 break;
410 if (imsg.fd != -1) {
411 if (close(imsg.fd) == -1 && err == NULL)
412 err = got_error_from_errno("close");
415 imsg_free(&imsg);
416 if (err)
417 break;
420 imsg_clear(&ibuf);
421 if (err) {
422 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
423 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
424 got_privsep_send_error(&ibuf, err);
427 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
428 err = got_error_from_errno("close");
429 return err ? 1 : 0;