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 <zlib.h>
33 #include "got_error.h"
34 #include "got_object.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 gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
51 char *section, char *tag, int def)
52 {
53 int value;
55 if (gitconfig == NULL)
56 return got_error(GOT_ERR_PRIVSEP_MSG);
58 value = got_gitconfig_get_num(gitconfig, section, tag, def);
59 return got_privsep_send_gitconfig_int(ibuf, value);
60 }
62 static const struct got_error *
63 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
64 char *section, char *tag)
65 {
66 char *value;
68 if (gitconfig == NULL)
69 return got_error(GOT_ERR_PRIVSEP_MSG);
71 value = got_gitconfig_get_str(gitconfig, section, tag);
72 return got_privsep_send_gitconfig_str(ibuf, value);
73 }
75 int
76 main(int argc, char *argv[])
77 {
78 const struct got_error *err = NULL;
79 struct imsgbuf ibuf;
80 size_t datalen;
81 struct got_gitconfig *gitconfig = NULL;
82 #if 0
83 static int attached;
85 while (!attached)
86 sleep(1);
87 #endif
88 signal(SIGINT, catch_sigint);
90 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
92 #ifndef PROFILE
93 /* revoke access to most system calls */
94 if (pledge("stdio recvfd", NULL) == -1) {
95 err = got_error_from_errno("pledge");
96 got_privsep_send_error(&ibuf, err);
97 return 1;
98 }
99 #endif
101 for (;;) {
102 struct imsg imsg;
104 memset(&imsg, 0, sizeof(imsg));
105 imsg.fd = -1;
107 if (sigint_received) {
108 err = got_error(GOT_ERR_CANCELLED);
109 break;
112 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
113 if (err) {
114 if (err->code == GOT_ERR_PRIVSEP_PIPE)
115 err = NULL;
116 break;
119 if (imsg.hdr.type == GOT_IMSG_STOP)
120 break;
122 switch (imsg.hdr.type) {
123 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
124 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
125 if (datalen != 0) {
126 err = got_error(GOT_ERR_PRIVSEP_LEN);
127 break;
129 if (imsg.fd == -1){
130 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
131 break;
134 if (gitconfig)
135 got_gitconfig_close(gitconfig);
136 err = got_gitconfig_open(&gitconfig, imsg.fd);
137 break;
138 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
139 err = gitconfig_num_request(&ibuf, gitconfig, "core",
140 "repositoryformatversion", 0);
141 break;
142 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
143 err = gitconfig_str_request(&ibuf, gitconfig, "user",
144 "name");
145 break;
146 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
147 err = gitconfig_str_request(&ibuf, gitconfig, "user",
148 "email");
149 break;
150 default:
151 err = got_error(GOT_ERR_PRIVSEP_MSG);
152 break;
155 if (imsg.fd != -1) {
156 if (close(imsg.fd) == -1 && err == NULL)
157 err = got_error_from_errno("close");
160 imsg_free(&imsg);
161 if (err)
162 break;
165 imsg_clear(&ibuf);
166 if (err) {
167 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
168 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
169 got_privsep_send_error(&ibuf, err);
172 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
173 err = got_error_from_errno("close");
174 return err ? 1 : 0;