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>
30 #include "got_compat.h"
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_repository.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_object.h"
38 #include "got_lib_privsep.h"
39 #include "got_lib_gitconfig.h"
41 static volatile sig_atomic_t sigint_received;
44 catch_sigint(int signo)
49 static const struct got_error *
50 send_gitconfig_int(struct imsgbuf *ibuf, int value)
52 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
53 &value, sizeof(value)) == -1)
54 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
56 return got_privsep_flush_imsg(ibuf);
59 static const struct got_error *
60 gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
61 const char *section, const char *tag, int def)
65 if (gitconfig == NULL)
66 return got_error(GOT_ERR_PRIVSEP_MSG);
68 value = got_gitconfig_get_num(gitconfig, section, tag, def);
69 return send_gitconfig_int(ibuf, value);
72 static const struct got_error *
73 send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
75 size_t len = value ? strlen(value) : 0;
77 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
79 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
81 return got_privsep_flush_imsg(ibuf);
84 static const struct got_error *
85 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
86 const char *section, const char *tag)
90 if (gitconfig == NULL)
91 return got_error(GOT_ERR_PRIVSEP_MSG);
93 value = got_gitconfig_get_str(gitconfig, section, tag);
94 return send_gitconfig_str(ibuf, value);
97 static const struct got_error *
98 send_gitconfig_remotes(struct imsgbuf *ibuf, struct got_remote_repo *remotes,
101 const struct got_error *err = NULL;
102 struct got_imsg_remotes iremotes;
105 iremotes.nremotes = nremotes;
106 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
107 &iremotes, sizeof(iremotes)) == -1)
108 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
110 err = got_privsep_flush_imsg(ibuf);
115 for (i = 0; i < nremotes; i++) {
116 struct got_imsg_remote iremote;
117 size_t len = sizeof(iremote);
120 iremote.mirror_references = remotes[i].mirror_references;
121 iremote.name_len = strlen(remotes[i].name);
122 len += iremote.name_len;
123 iremote.fetch_url_len = strlen(remotes[i].fetch_url);
124 len += iremote.fetch_url_len;
125 iremote.send_url_len = strlen(remotes[i].send_url);
126 len += iremote.send_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].fetch_url, iremote.fetch_url_len) == -1) {
147 err = got_error_from_errno(
148 "imsg_add GITCONFIG_REMOTE");
152 if (imsg_add(wbuf, remotes[i].send_url, iremote.send_url_len) == -1) {
153 err = got_error_from_errno(
154 "imsg_add GITCONFIG_REMOTE");
160 imsg_close(ibuf, wbuf);
161 err = got_privsep_flush_imsg(ibuf);
170 get_boolean_val(char *val)
172 return (strcasecmp(val, "true") == 0 ||
173 strcasecmp(val, "on") == 0 ||
174 strcasecmp(val, "yes") == 0 ||
175 strcmp(val, "1") == 0);
178 static const struct got_error *
179 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
181 const struct got_error *err = NULL;
182 struct got_gitconfig_list *sections;
183 struct got_gitconfig_list_node *node;
184 struct got_remote_repo *remotes = NULL;
187 if (gitconfig == NULL)
188 return got_error(GOT_ERR_PRIVSEP_MSG);
190 err = got_gitconfig_get_section_list(§ions, gitconfig);
194 TAILQ_FOREACH(node, §ions->fields, link) {
195 if (strncasecmp("remote \"", node->field, 8) != 0)
201 err = send_gitconfig_remotes(ibuf, NULL, 0);
205 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
206 if (remotes == NULL) {
207 err = got_error_from_errno("recallocarray");
212 TAILQ_FOREACH(node, §ions->fields, link) {
213 char *name, *end, *mirror;
215 if (strncasecmp("remote \"", node->field, 8) != 0)
218 name = strdup(node->field + 8);
220 err = got_error_from_errno("strdup");
223 end = strrchr(name, '"');
226 remotes[i].name = name;
228 remotes[i].fetch_url = got_gitconfig_get_str(gitconfig,
230 if (remotes[i].fetch_url == NULL) {
231 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
235 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
236 node->field, "pushurl");
237 if (remotes[i].send_url == NULL)
238 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
240 if (remotes[i].send_url == NULL) {
241 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
245 remotes[i].mirror_references = 0;
246 mirror = got_gitconfig_get_str(gitconfig, node->field,
248 if (mirror != NULL && get_boolean_val(mirror))
249 remotes[i].mirror_references = 1;
254 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
256 for (i = 0; i < nremotes; i++)
257 free(remotes[i].name);
259 got_gitconfig_free_list(sections);
263 static const struct got_error *
264 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
268 if (gitconfig == NULL)
269 return got_error(GOT_ERR_PRIVSEP_MSG);
271 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
273 return send_gitconfig_str(ibuf, value);
274 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
275 return send_gitconfig_str(ibuf, value);
278 static const struct got_error *
279 gitconfig_extensions_request(struct imsgbuf *ibuf,
280 struct got_gitconfig *gitconfig)
282 const struct got_error *err = NULL;
283 struct got_gitconfig_list *tags;
284 struct got_gitconfig_list_node *node;
288 if (gitconfig == NULL)
289 return got_error(GOT_ERR_PRIVSEP_MSG);
291 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
293 return send_gitconfig_int(ibuf, 0);
295 TAILQ_FOREACH(node, &tags->fields, link) {
296 val = got_gitconfig_get_str(gitconfig, "extensions",
298 if (get_boolean_val(val))
302 err = send_gitconfig_int(ibuf, nextensions);
306 TAILQ_FOREACH(node, &tags->fields, link) {
307 val = got_gitconfig_get_str(gitconfig, "extensions",
309 if (get_boolean_val(val)) {
310 err = send_gitconfig_str(ibuf, node->field);
316 got_gitconfig_free_list(tags);
321 main(int argc, char *argv[])
323 const struct got_error *err = NULL;
326 struct got_gitconfig *gitconfig = NULL;
333 signal(SIGINT, catch_sigint);
335 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
338 /* revoke access to most system calls */
339 if (pledge("stdio recvfd", NULL) == -1) {
340 err = got_error_from_errno("pledge");
341 got_privsep_send_error(&ibuf, err);
345 /* revoke fs access */
346 if (landlock_no_fs() == -1) {
347 err = got_error_from_errno("landlock_no_fs");
348 got_privsep_send_error(&ibuf, err);
356 memset(&imsg, 0, sizeof(imsg));
359 if (sigint_received) {
360 err = got_error(GOT_ERR_CANCELLED);
364 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
366 if (err->code == GOT_ERR_PRIVSEP_PIPE)
371 if (imsg.hdr.type == GOT_IMSG_STOP)
374 switch (imsg.hdr.type) {
375 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
376 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
378 err = got_error(GOT_ERR_PRIVSEP_LEN);
382 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
387 got_gitconfig_close(gitconfig);
388 err = got_gitconfig_open(&gitconfig, imsg.fd);
390 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
391 err = gitconfig_num_request(&ibuf, gitconfig, "core",
392 "repositoryformatversion", 0);
394 case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
395 err = gitconfig_extensions_request(&ibuf, gitconfig);
397 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
398 err = gitconfig_str_request(&ibuf, gitconfig, "user",
401 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
402 err = gitconfig_str_request(&ibuf, gitconfig, "user",
405 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
406 err = gitconfig_remotes_request(&ibuf, gitconfig);
408 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
409 err = gitconfig_owner_request(&ibuf, gitconfig);
412 err = got_error(GOT_ERR_PRIVSEP_MSG);
417 if (close(imsg.fd) == -1 && err == NULL)
418 err = got_error_from_errno("close");
428 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
429 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
430 got_privsep_send_error(&ibuf, err);
433 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
434 err = got_error_from_errno("close");