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);
146 main(int argc, char *argv[])
148 const struct got_error *err = NULL;
151 struct got_gitconfig *gitconfig = NULL;
158 signal(SIGINT, catch_sigint);
160 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
163 /* revoke access to most system calls */
164 if (pledge("stdio recvfd", NULL) == -1) {
165 err = got_error_from_errno("pledge");
166 got_privsep_send_error(&ibuf, err);
174 memset(&imsg, 0, sizeof(imsg));
177 if (sigint_received) {
178 err = got_error(GOT_ERR_CANCELLED);
182 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
184 if (err->code == GOT_ERR_PRIVSEP_PIPE)
189 if (imsg.hdr.type == GOT_IMSG_STOP)
192 switch (imsg.hdr.type) {
193 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
194 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
196 err = got_error(GOT_ERR_PRIVSEP_LEN);
200 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
205 got_gitconfig_close(gitconfig);
206 err = got_gitconfig_open(&gitconfig, imsg.fd);
208 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
209 err = gitconfig_num_request(&ibuf, gitconfig, "core",
210 "repositoryformatversion", 0);
212 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
213 err = gitconfig_str_request(&ibuf, gitconfig, "user",
216 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
217 err = gitconfig_str_request(&ibuf, gitconfig, "user",
220 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
221 err = gitconfig_remotes_request(&ibuf, gitconfig);
224 err = got_error(GOT_ERR_PRIVSEP_MSG);
229 if (close(imsg.fd) == -1 && err == NULL)
230 err = got_error_from_errno("close");
240 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
241 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
242 got_privsep_send_error(&ibuf, err);
245 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
246 err = got_error_from_errno("close");