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"
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 gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
52 char *section, char *tag, int def)
53 {
54 int value;
56 if (gitconfig == NULL)
57 return got_error(GOT_ERR_PRIVSEP_MSG);
59 value = got_gitconfig_get_num(gitconfig, section, tag, def);
60 return got_privsep_send_gitconfig_int(ibuf, value);
61 }
63 static const struct got_error *
64 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
65 char *section, char *tag)
66 {
67 char *value;
69 if (gitconfig == NULL)
70 return got_error(GOT_ERR_PRIVSEP_MSG);
72 value = got_gitconfig_get_str(gitconfig, section, tag);
73 return got_privsep_send_gitconfig_str(ibuf, value);
74 }
76 static const struct got_error *
77 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
78 {
79 const struct got_error *err = NULL;
80 struct got_gitconfig_list *sections;
81 struct got_gitconfig_list_node *node;
82 struct got_remote_repo *remotes = NULL;
83 int nremotes = 0, i;
85 if (gitconfig == NULL)
86 return got_error(GOT_ERR_PRIVSEP_MSG);
88 err = got_gitconfig_get_section_list(&sections, gitconfig);
89 if (err)
90 return err;
92 TAILQ_FOREACH(node, &sections->fields, link) {
93 if (strncasecmp("remote \"", node->field, 8) != 0)
94 continue;
95 nremotes++;
96 }
98 if (nremotes == 0) {
99 err = got_privsep_send_gitconfig_remotes(ibuf, NULL, 0);
100 goto done;
103 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
104 if (remotes == NULL) {
105 err = got_error_from_errno("recallocarray");
106 goto done;
109 i = 0;
110 TAILQ_FOREACH(node, &sections->fields, link) {
111 char *name, *end;
113 if (strncasecmp("remote \"", node->field, 8) != 0)
114 continue;
116 name = strdup(node->field + 8);
117 if (name == NULL) {
118 err = got_error_from_errno("strdup");
119 goto done;
121 end = strrchr(name, '"');
122 if (end)
123 *end = '\0';
124 remotes[i].name = name;
126 remotes[i].url = got_gitconfig_get_str(gitconfig,
127 node->field, "url");
128 if (remotes[i].url == NULL) {
129 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
130 goto done;
133 i++;
136 err = got_privsep_send_gitconfig_remotes(ibuf, remotes, nremotes);
137 done:
138 for (i = 0; i < nremotes; i++)
139 free(remotes[i].name);
140 free(remotes);
141 got_gitconfig_free_list(sections);
142 return err;
145 static const struct got_error *
146 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
148 char *value;
150 if (gitconfig == NULL)
151 return got_error(GOT_ERR_PRIVSEP_MSG);
153 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
154 if (value)
155 return got_privsep_send_gitconfig_str(ibuf, value);
156 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
157 return got_privsep_send_gitconfig_str(ibuf, value);
160 int
161 main(int argc, char *argv[])
163 const struct got_error *err = NULL;
164 struct imsgbuf ibuf;
165 size_t datalen;
166 struct got_gitconfig *gitconfig = NULL;
167 #if 0
168 static int attached;
170 while (!attached)
171 sleep(1);
172 #endif
173 signal(SIGINT, catch_sigint);
175 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
177 #ifndef PROFILE
178 /* revoke access to most system calls */
179 if (pledge("stdio recvfd", NULL) == -1) {
180 err = got_error_from_errno("pledge");
181 got_privsep_send_error(&ibuf, err);
182 return 1;
184 #endif
186 for (;;) {
187 struct imsg imsg;
189 memset(&imsg, 0, sizeof(imsg));
190 imsg.fd = -1;
192 if (sigint_received) {
193 err = got_error(GOT_ERR_CANCELLED);
194 break;
197 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
198 if (err) {
199 if (err->code == GOT_ERR_PRIVSEP_PIPE)
200 err = NULL;
201 break;
204 if (imsg.hdr.type == GOT_IMSG_STOP)
205 break;
207 switch (imsg.hdr.type) {
208 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
209 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
210 if (datalen != 0) {
211 err = got_error(GOT_ERR_PRIVSEP_LEN);
212 break;
214 if (imsg.fd == -1){
215 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
216 break;
219 if (gitconfig)
220 got_gitconfig_close(gitconfig);
221 err = got_gitconfig_open(&gitconfig, imsg.fd);
222 break;
223 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
224 err = gitconfig_num_request(&ibuf, gitconfig, "core",
225 "repositoryformatversion", 0);
226 break;
227 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
228 err = gitconfig_str_request(&ibuf, gitconfig, "user",
229 "name");
230 break;
231 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
232 err = gitconfig_str_request(&ibuf, gitconfig, "user",
233 "email");
234 break;
235 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
236 err = gitconfig_remotes_request(&ibuf, gitconfig);
237 break;
238 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
239 err = gitconfig_owner_request(&ibuf, gitconfig);
240 break;
241 default:
242 err = got_error(GOT_ERR_PRIVSEP_MSG);
243 break;
246 if (imsg.fd != -1) {
247 if (close(imsg.fd) == -1 && err == NULL)
248 err = got_error_from_errno("close");
251 imsg_free(&imsg);
252 if (err)
253 break;
256 imsg_clear(&ibuf);
257 if (err) {
258 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
259 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
260 got_privsep_send_error(&ibuf, err);
263 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
264 err = got_error_from_errno("close");
265 return err ? 1 : 0;