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) {
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);
136 err = got_privsep_send_gitconfig_remotes(ibuf, remotes, nremotes);
138 for (i = 0; i < nremotes; i++)
139 free(remotes[i].name);
141 got_gitconfig_free_list(sections);
145 static const struct got_error *
146 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
150 if (gitconfig == NULL)
151 return got_error(GOT_ERR_PRIVSEP_MSG);
153 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
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);
161 main(int argc, char *argv[])
163 const struct got_error *err = NULL;
166 struct got_gitconfig *gitconfig = NULL;
173 signal(SIGINT, catch_sigint);
175 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
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);
189 memset(&imsg, 0, sizeof(imsg));
192 if (sigint_received) {
193 err = got_error(GOT_ERR_CANCELLED);
197 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
199 if (err->code == GOT_ERR_PRIVSEP_PIPE)
204 if (imsg.hdr.type == GOT_IMSG_STOP)
207 switch (imsg.hdr.type) {
208 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
209 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
211 err = got_error(GOT_ERR_PRIVSEP_LEN);
215 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
220 got_gitconfig_close(gitconfig);
221 err = got_gitconfig_open(&gitconfig, imsg.fd);
223 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
224 err = gitconfig_num_request(&ibuf, gitconfig, "core",
225 "repositoryformatversion", 0);
227 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
228 err = gitconfig_str_request(&ibuf, gitconfig, "user",
231 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
232 err = gitconfig_str_request(&ibuf, gitconfig, "user",
235 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
236 err = gitconfig_remotes_request(&ibuf, gitconfig);
238 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
239 err = gitconfig_owner_request(&ibuf, gitconfig);
242 err = got_error(GOT_ERR_PRIVSEP_MSG);
247 if (close(imsg.fd) == -1 && err == NULL)
248 err = got_error_from_errno("close");
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");