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>
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 send_gitconfig_int(struct imsgbuf *ibuf, int value)
53 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
54 &value, sizeof(value)) == -1)
55 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
57 return got_privsep_flush_imsg(ibuf);
60 static const struct got_error *
61 gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
62 char *section, char *tag, int def)
66 if (gitconfig == NULL)
67 return got_error(GOT_ERR_PRIVSEP_MSG);
69 value = got_gitconfig_get_num(gitconfig, section, tag, def);
70 return send_gitconfig_int(ibuf, value);
73 static const struct got_error *
74 send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
76 size_t len = value ? strlen(value) : 0;
78 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
80 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
82 return got_privsep_flush_imsg(ibuf);
85 static const struct got_error *
86 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
87 char *section, char *tag)
91 if (gitconfig == NULL)
92 return got_error(GOT_ERR_PRIVSEP_MSG);
94 value = got_gitconfig_get_str(gitconfig, section, tag);
95 return send_gitconfig_str(ibuf, value);
98 static const struct got_error *
99 send_gitconfig_remotes(struct imsgbuf *ibuf, struct got_remote_repo *remotes,
102 const struct got_error *err = NULL;
103 struct got_imsg_remotes iremotes;
106 iremotes.nremotes = nremotes;
107 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
108 &iremotes, sizeof(iremotes)) == -1)
109 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
111 err = got_privsep_flush_imsg(ibuf);
116 for (i = 0; i < nremotes; i++) {
117 struct got_imsg_remote iremote;
118 size_t len = sizeof(iremote);
121 iremote.mirror_references = remotes[i].mirror_references;
122 iremote.name_len = strlen(remotes[i].name);
123 len += iremote.name_len;
124 iremote.url_len = strlen(remotes[i].url);
125 len += iremote.url_len;
127 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
129 return got_error_from_errno(
130 "imsg_create GITCONFIG_REMOTE");
132 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
133 err = got_error_from_errno(
134 "imsg_add GITCONFIG_REMOTE");
139 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
140 err = got_error_from_errno(
141 "imsg_add GITCONFIG_REMOTE");
145 if (imsg_add(wbuf, remotes[i].url, iremote.url_len) == -1) {
146 err = got_error_from_errno(
147 "imsg_add GITCONFIG_REMOTE");
153 imsg_close(ibuf, wbuf);
154 err = got_privsep_flush_imsg(ibuf);
163 get_boolean_val(char *val)
165 return (strcasecmp(val, "true") == 0 ||
166 strcasecmp(val, "on") == 0 ||
167 strcasecmp(val, "yes") == 0 ||
168 strcmp(val, "1") == 0);
171 static const struct got_error *
172 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
174 const struct got_error *err = NULL;
175 struct got_gitconfig_list *sections;
176 struct got_gitconfig_list_node *node;
177 struct got_remote_repo *remotes = NULL;
180 if (gitconfig == NULL)
181 return got_error(GOT_ERR_PRIVSEP_MSG);
183 err = got_gitconfig_get_section_list(§ions, gitconfig);
187 TAILQ_FOREACH(node, §ions->fields, link) {
188 if (strncasecmp("remote \"", node->field, 8) != 0)
194 err = send_gitconfig_remotes(ibuf, NULL, 0);
198 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
199 if (remotes == NULL) {
200 err = got_error_from_errno("recallocarray");
205 TAILQ_FOREACH(node, §ions->fields, link) {
206 char *name, *end, *mirror;
208 if (strncasecmp("remote \"", node->field, 8) != 0)
211 name = strdup(node->field + 8);
213 err = got_error_from_errno("strdup");
216 end = strrchr(name, '"');
219 remotes[i].name = name;
221 remotes[i].url = got_gitconfig_get_str(gitconfig,
223 if (remotes[i].url == NULL) {
224 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
228 remotes[i].mirror_references = 0;
229 mirror = got_gitconfig_get_str(gitconfig, node->field,
231 if (mirror != NULL && get_boolean_val(mirror))
232 remotes[i].mirror_references = 1;
237 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
239 for (i = 0; i < nremotes; i++)
240 free(remotes[i].name);
242 got_gitconfig_free_list(sections);
246 static const struct got_error *
247 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
251 if (gitconfig == NULL)
252 return got_error(GOT_ERR_PRIVSEP_MSG);
254 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
256 return send_gitconfig_str(ibuf, value);
257 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
258 return send_gitconfig_str(ibuf, value);
261 static const struct got_error *
262 gitconfig_extensions_request(struct imsgbuf *ibuf,
263 struct got_gitconfig *gitconfig)
265 const struct got_error *err = NULL;
266 struct got_gitconfig_list *tags;
267 struct got_gitconfig_list_node *node;
271 if (gitconfig == NULL)
272 return got_error(GOT_ERR_PRIVSEP_MSG);
274 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
276 return send_gitconfig_int(ibuf, 0);
278 TAILQ_FOREACH(node, &tags->fields, link) {
279 val = got_gitconfig_get_str(gitconfig, "extensions",
281 if (get_boolean_val(val))
285 err = send_gitconfig_int(ibuf, nextensions);
289 TAILQ_FOREACH(node, &tags->fields, link) {
290 val = got_gitconfig_get_str(gitconfig, "extensions",
292 if (get_boolean_val(val)) {
293 err = send_gitconfig_str(ibuf, node->field);
299 got_gitconfig_free_list(tags);
304 main(int argc, char *argv[])
306 const struct got_error *err = NULL;
309 struct got_gitconfig *gitconfig = NULL;
316 signal(SIGINT, catch_sigint);
318 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
321 /* revoke access to most system calls */
322 if (pledge("stdio recvfd", NULL) == -1) {
323 err = got_error_from_errno("pledge");
324 got_privsep_send_error(&ibuf, err);
332 memset(&imsg, 0, sizeof(imsg));
335 if (sigint_received) {
336 err = got_error(GOT_ERR_CANCELLED);
340 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
342 if (err->code == GOT_ERR_PRIVSEP_PIPE)
347 if (imsg.hdr.type == GOT_IMSG_STOP)
350 switch (imsg.hdr.type) {
351 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
352 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
354 err = got_error(GOT_ERR_PRIVSEP_LEN);
358 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
363 got_gitconfig_close(gitconfig);
364 err = got_gitconfig_open(&gitconfig, imsg.fd);
366 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
367 err = gitconfig_num_request(&ibuf, gitconfig, "core",
368 "repositoryformatversion", 0);
370 case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
371 err = gitconfig_extensions_request(&ibuf, gitconfig);
373 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
374 err = gitconfig_str_request(&ibuf, gitconfig, "user",
377 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
378 err = gitconfig_str_request(&ibuf, gitconfig, "user",
381 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
382 err = gitconfig_remotes_request(&ibuf, gitconfig);
384 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
385 err = gitconfig_owner_request(&ibuf, gitconfig);
388 err = got_error(GOT_ERR_PRIVSEP_MSG);
393 if (close(imsg.fd) == -1 && err == NULL)
394 err = got_error_from_errno("close");
404 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
405 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
406 got_privsep_send_error(&ibuf, err);
409 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
410 err = got_error_from_errno("close");