2 * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <sys/syslimits.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;
45 catch_sigint(int signo)
50 static const struct got_error *
51 gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
52 char *section, char *tag, int def)
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);
63 static const struct got_error *
64 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
65 char *section, char *tag)
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);
76 static const struct got_error *
77 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
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;
85 if (gitconfig == NULL)
86 return got_error(GOT_ERR_PRIVSEP_MSG);
88 err = got_gitconfig_get_section_list(§ions, gitconfig);
92 TAILQ_FOREACH(node, §ions->fields, link) {
93 if (strncasecmp("remote \"", node->field, 8) != 0)
99 err = got_privsep_send_gitconfig_remotes(ibuf, NULL, 0);
103 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
104 if (remotes == NULL) {
105 err = got_error_from_errno("recallocarray");
110 TAILQ_FOREACH(node, §ions->fields, link) {
111 char *name, *end, *mirror;
113 if (strncasecmp("remote \"", node->field, 8) != 0)
116 name = strdup(node->field + 8);
118 err = got_error_from_errno("strdup");
121 end = strrchr(name, '"');
124 remotes[i].name = name;
126 remotes[i].url = got_gitconfig_get_str(gitconfig,
128 if (remotes[i].url == NULL) {
129 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
133 remotes[i].mirror_references = 0;
134 mirror = got_gitconfig_get_str(gitconfig, node->field,
136 if (mirror != NULL &&
137 (strcasecmp(mirror, "true") == 0 ||
138 strcasecmp(mirror, "on") == 0 ||
139 strcasecmp(mirror, "yes") == 0 ||
140 strcmp(mirror, "1") == 0))
141 remotes[i].mirror_references = 1;
146 err = got_privsep_send_gitconfig_remotes(ibuf, remotes, nremotes);
148 for (i = 0; i < nremotes; i++)
149 free(remotes[i].name);
151 got_gitconfig_free_list(sections);
155 static const struct got_error *
156 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
160 if (gitconfig == NULL)
161 return got_error(GOT_ERR_PRIVSEP_MSG);
163 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
165 return got_privsep_send_gitconfig_str(ibuf, value);
166 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
167 return got_privsep_send_gitconfig_str(ibuf, value);
171 main(int argc, char *argv[])
173 const struct got_error *err = NULL;
176 struct got_gitconfig *gitconfig = NULL;
183 signal(SIGINT, catch_sigint);
185 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
188 /* revoke access to most system calls */
189 if (pledge("stdio recvfd", NULL) == -1) {
190 err = got_error_from_errno("pledge");
191 got_privsep_send_error(&ibuf, err);
199 memset(&imsg, 0, sizeof(imsg));
202 if (sigint_received) {
203 err = got_error(GOT_ERR_CANCELLED);
207 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
209 if (err->code == GOT_ERR_PRIVSEP_PIPE)
214 if (imsg.hdr.type == GOT_IMSG_STOP)
217 switch (imsg.hdr.type) {
218 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
219 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
221 err = got_error(GOT_ERR_PRIVSEP_LEN);
225 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
230 got_gitconfig_close(gitconfig);
231 err = got_gitconfig_open(&gitconfig, imsg.fd);
233 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
234 err = gitconfig_num_request(&ibuf, gitconfig, "core",
235 "repositoryformatversion", 0);
237 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
238 err = gitconfig_str_request(&ibuf, gitconfig, "user",
241 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
242 err = gitconfig_str_request(&ibuf, gitconfig, "user",
245 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
246 err = gitconfig_remotes_request(&ibuf, gitconfig);
248 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
249 err = gitconfig_owner_request(&ibuf, gitconfig);
252 err = got_error(GOT_ERR_PRIVSEP_MSG);
257 if (close(imsg.fd) == -1 && err == NULL)
258 err = got_error_from_errno("close");
268 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
269 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
270 got_privsep_send_error(&ibuf, err);
273 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
274 err = got_error_from_errno("close");