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>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_repository.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_object.h"
40 #include "got_lib_privsep.h"
41 #include "got_lib_gitconfig.h"
43 static volatile sig_atomic_t sigint_received;
46 catch_sigint(int signo)
51 static const struct got_error *
52 send_gitconfig_int(struct imsgbuf *ibuf, int value)
54 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
55 &value, sizeof(value)) == -1)
56 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
58 return got_privsep_flush_imsg(ibuf);
61 static const struct got_error *
62 gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
63 char *section, char *tag, int def)
67 if (gitconfig == NULL)
68 return got_error(GOT_ERR_PRIVSEP_MSG);
70 value = got_gitconfig_get_num(gitconfig, section, tag, def);
71 return send_gitconfig_int(ibuf, value);
74 static const struct got_error *
75 send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
77 size_t len = value ? strlen(value) + 1 : 0;
79 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
81 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
83 return got_privsep_flush_imsg(ibuf);
86 static const struct got_error *
87 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
88 char *section, char *tag)
92 if (gitconfig == NULL)
93 return got_error(GOT_ERR_PRIVSEP_MSG);
95 value = got_gitconfig_get_str(gitconfig, section, tag);
96 return send_gitconfig_str(ibuf, value);
99 static const struct got_error *
100 send_gitconfig_remotes(struct imsgbuf *ibuf, struct got_remote_repo *remotes,
103 const struct got_error *err = NULL;
104 struct got_imsg_remotes iremotes;
107 iremotes.nremotes = nremotes;
108 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
109 &iremotes, sizeof(iremotes)) == -1)
110 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
112 err = got_privsep_flush_imsg(ibuf);
117 for (i = 0; i < nremotes; i++) {
118 struct got_imsg_remote iremote;
119 size_t len = sizeof(iremote);
122 iremote.mirror_references = remotes[i].mirror_references;
123 iremote.name_len = strlen(remotes[i].name);
124 len += iremote.name_len;
125 iremote.url_len = strlen(remotes[i].url);
126 len += iremote.url_len;
128 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
130 return got_error_from_errno(
131 "imsg_create GITCONFIG_REMOTE");
133 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
134 err = got_error_from_errno(
135 "imsg_add GITCONFIG_REMOTE");
140 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
141 err = got_error_from_errno(
142 "imsg_add GITCONFIG_REMOTE");
146 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
147 err = got_error_from_errno(
148 "imsg_add GITCONFIG_REMOTE");
154 imsg_close(ibuf, wbuf);
155 err = got_privsep_flush_imsg(ibuf);
164 static const struct got_error *
165 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
167 const struct got_error *err = NULL;
168 struct got_gitconfig_list *sections;
169 struct got_gitconfig_list_node *node;
170 struct got_remote_repo *remotes = NULL;
173 if (gitconfig == NULL)
174 return got_error(GOT_ERR_PRIVSEP_MSG);
176 err = got_gitconfig_get_section_list(§ions, gitconfig);
180 TAILQ_FOREACH(node, §ions->fields, link) {
181 if (strncasecmp("remote \"", node->field, 8) != 0)
187 err = send_gitconfig_remotes(ibuf, NULL, 0);
191 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
192 if (remotes == NULL) {
193 err = got_error_from_errno("recallocarray");
198 TAILQ_FOREACH(node, §ions->fields, link) {
199 char *name, *end, *mirror;
201 if (strncasecmp("remote \"", node->field, 8) != 0)
204 name = strdup(node->field + 8);
206 err = got_error_from_errno("strdup");
209 end = strrchr(name, '"');
212 remotes[i].name = name;
214 remotes[i].url = got_gitconfig_get_str(gitconfig,
216 if (remotes[i].url == NULL) {
217 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
221 remotes[i].mirror_references = 0;
222 mirror = got_gitconfig_get_str(gitconfig, node->field,
224 if (mirror != NULL &&
225 (strcasecmp(mirror, "true") == 0 ||
226 strcasecmp(mirror, "on") == 0 ||
227 strcasecmp(mirror, "yes") == 0 ||
228 strcmp(mirror, "1") == 0))
229 remotes[i].mirror_references = 1;
234 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
236 for (i = 0; i < nremotes; i++)
237 free(remotes[i].name);
239 got_gitconfig_free_list(sections);
243 static const struct got_error *
244 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
248 if (gitconfig == NULL)
249 return got_error(GOT_ERR_PRIVSEP_MSG);
251 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
253 return send_gitconfig_str(ibuf, value);
254 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
255 return send_gitconfig_str(ibuf, value);
259 main(int argc, char *argv[])
261 const struct got_error *err = NULL;
264 struct got_gitconfig *gitconfig = NULL;
271 signal(SIGINT, catch_sigint);
273 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
276 /* revoke access to most system calls */
277 if (pledge("stdio recvfd", NULL) == -1) {
278 err = got_error_from_errno("pledge");
279 got_privsep_send_error(&ibuf, err);
287 memset(&imsg, 0, sizeof(imsg));
290 if (sigint_received) {
291 err = got_error(GOT_ERR_CANCELLED);
295 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
297 if (err->code == GOT_ERR_PRIVSEP_PIPE)
302 if (imsg.hdr.type == GOT_IMSG_STOP)
305 switch (imsg.hdr.type) {
306 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
307 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
309 err = got_error(GOT_ERR_PRIVSEP_LEN);
313 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
318 got_gitconfig_close(gitconfig);
319 err = got_gitconfig_open(&gitconfig, imsg.fd);
321 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
322 err = gitconfig_num_request(&ibuf, gitconfig, "core",
323 "repositoryformatversion", 0);
325 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
326 err = gitconfig_str_request(&ibuf, gitconfig, "user",
329 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
330 err = gitconfig_str_request(&ibuf, gitconfig, "user",
333 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
334 err = gitconfig_remotes_request(&ibuf, gitconfig);
336 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
337 err = gitconfig_owner_request(&ibuf, gitconfig);
340 err = got_error(GOT_ERR_PRIVSEP_MSG);
345 if (close(imsg.fd) == -1 && err == NULL)
346 err = got_error_from_errno("close");
356 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
357 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
358 got_privsep_send_error(&ibuf, err);
361 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
362 err = got_error_from_errno("close");