2 6a800804 2022-10-13 stsp * Copyright (c) 2019, 2022 Stefan Sperling <stsp@openbsd.org>
4 6a800804 2022-10-13 stsp * Permission to use, copy, modify, and distribute this software for any
5 6a800804 2022-10-13 stsp * purpose with or without fee is hereby granted, provided that the above
6 6a800804 2022-10-13 stsp * copyright notice and this permission notice appear in all copies.
8 6a800804 2022-10-13 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 6a800804 2022-10-13 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 6a800804 2022-10-13 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 6a800804 2022-10-13 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 6a800804 2022-10-13 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 6a800804 2022-10-13 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 6a800804 2022-10-13 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 6a800804 2022-10-13 stsp #include <sys/types.h>
18 6a800804 2022-10-13 stsp #include <sys/tree.h>
19 6a800804 2022-10-13 stsp #include <sys/socket.h>
20 6a800804 2022-10-13 stsp #include <sys/queue.h>
21 6a800804 2022-10-13 stsp #include <sys/uio.h>
23 6a800804 2022-10-13 stsp #include <errno.h>
24 6a800804 2022-10-13 stsp #include <fcntl.h>
25 6a800804 2022-10-13 stsp #include <limits.h>
26 6a800804 2022-10-13 stsp #include <sha1.h>
27 6a800804 2022-10-13 stsp #include <stdio.h>
28 6a800804 2022-10-13 stsp #include <stdlib.h>
29 6a800804 2022-10-13 stsp #include <stdint.h>
30 6a800804 2022-10-13 stsp #include <imsg.h>
31 6a800804 2022-10-13 stsp #include <unistd.h>
33 6a800804 2022-10-13 stsp #include "got_error.h"
34 6a800804 2022-10-13 stsp #include "got_object.h"
35 6a800804 2022-10-13 stsp #include "got_repository.h"
36 6a800804 2022-10-13 stsp #include "got_path.h"
38 6a800804 2022-10-13 stsp #include "got_lib_delta.h"
39 6a800804 2022-10-13 stsp #include "got_lib_object.h"
40 6a800804 2022-10-13 stsp #include "got_lib_object_cache.h"
41 6a800804 2022-10-13 stsp #include "got_lib_privsep.h"
42 6a800804 2022-10-13 stsp #include "got_lib_pack.h"
43 6a800804 2022-10-13 stsp #include "got_lib_repository.h"
45 6a800804 2022-10-13 stsp const struct got_error *
46 6a800804 2022-10-13 stsp got_repo_read_gitconfig(int *gitconfig_repository_format_version,
47 6a800804 2022-10-13 stsp char **gitconfig_author_name, char **gitconfig_author_email,
48 6a800804 2022-10-13 stsp struct got_remote_repo **remotes, int *nremotes,
49 6a800804 2022-10-13 stsp char **gitconfig_owner, char ***extensions, int *nextensions,
50 6a800804 2022-10-13 stsp const char *gitconfig_path)
52 6a800804 2022-10-13 stsp const struct got_error *err = NULL, *child_err = NULL;
53 6a800804 2022-10-13 stsp int fd = -1;
54 6a800804 2022-10-13 stsp int imsg_fds[2] = { -1, -1 };
56 6a800804 2022-10-13 stsp struct imsgbuf *ibuf;
58 6a800804 2022-10-13 stsp *gitconfig_repository_format_version = 0;
59 6a800804 2022-10-13 stsp if (extensions)
60 6a800804 2022-10-13 stsp *extensions = NULL;
61 6a800804 2022-10-13 stsp if (nextensions)
62 6a800804 2022-10-13 stsp *nextensions = 0;
63 6a800804 2022-10-13 stsp *gitconfig_author_name = NULL;
64 6a800804 2022-10-13 stsp *gitconfig_author_email = NULL;
65 6a800804 2022-10-13 stsp if (remotes)
66 6a800804 2022-10-13 stsp *remotes = NULL;
67 6a800804 2022-10-13 stsp if (nremotes)
68 6a800804 2022-10-13 stsp *nremotes = 0;
69 6a800804 2022-10-13 stsp if (gitconfig_owner)
70 6a800804 2022-10-13 stsp *gitconfig_owner = NULL;
72 6a800804 2022-10-13 stsp fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
73 6a800804 2022-10-13 stsp if (fd == -1) {
74 6a800804 2022-10-13 stsp if (errno == ENOENT)
75 6a800804 2022-10-13 stsp return NULL;
76 6a800804 2022-10-13 stsp return got_error_from_errno2("open", gitconfig_path);
79 6a800804 2022-10-13 stsp ibuf = calloc(1, sizeof(*ibuf));
80 6a800804 2022-10-13 stsp if (ibuf == NULL) {
81 6a800804 2022-10-13 stsp err = got_error_from_errno("calloc");
85 6a800804 2022-10-13 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
86 6a800804 2022-10-13 stsp err = got_error_from_errno("socketpair");
90 6a800804 2022-10-13 stsp pid = fork();
91 6a800804 2022-10-13 stsp if (pid == -1) {
92 6a800804 2022-10-13 stsp err = got_error_from_errno("fork");
94 6a800804 2022-10-13 stsp } else if (pid == 0) {
95 6a800804 2022-10-13 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
96 6a800804 2022-10-13 stsp gitconfig_path);
97 6a800804 2022-10-13 stsp /* not reached */
100 6a800804 2022-10-13 stsp if (close(imsg_fds[1]) == -1) {
101 6a800804 2022-10-13 stsp err = got_error_from_errno("close");
104 6a800804 2022-10-13 stsp imsg_fds[1] = -1;
105 6a800804 2022-10-13 stsp imsg_init(ibuf, imsg_fds[0]);
107 6a800804 2022-10-13 stsp err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
112 6a800804 2022-10-13 stsp err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
116 6a800804 2022-10-13 stsp err = got_privsep_recv_gitconfig_int(
117 6a800804 2022-10-13 stsp gitconfig_repository_format_version, ibuf);
121 6a800804 2022-10-13 stsp if (extensions && nextensions) {
122 6a800804 2022-10-13 stsp err = got_privsep_send_gitconfig_repository_extensions_req(
126 6a800804 2022-10-13 stsp err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
129 6a800804 2022-10-13 stsp if (*nextensions > 0) {
131 6a800804 2022-10-13 stsp *extensions = calloc(*nextensions, sizeof(char *));
132 6a800804 2022-10-13 stsp if (*extensions == NULL) {
133 6a800804 2022-10-13 stsp err = got_error_from_errno("calloc");
136 6a800804 2022-10-13 stsp for (i = 0; i < *nextensions; i++) {
138 6a800804 2022-10-13 stsp err = got_privsep_recv_gitconfig_str(&ext,
142 6a800804 2022-10-13 stsp (*extensions)[i] = ext;
147 6a800804 2022-10-13 stsp err = got_privsep_send_gitconfig_author_name_req(ibuf);
151 6a800804 2022-10-13 stsp err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
155 6a800804 2022-10-13 stsp err = got_privsep_send_gitconfig_author_email_req(ibuf);
159 6a800804 2022-10-13 stsp err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
163 6a800804 2022-10-13 stsp if (remotes && nremotes) {
164 6a800804 2022-10-13 stsp err = got_privsep_send_gitconfig_remotes_req(ibuf);
168 6a800804 2022-10-13 stsp err = got_privsep_recv_gitconfig_remotes(remotes,
169 6a800804 2022-10-13 stsp nremotes, ibuf);
174 6a800804 2022-10-13 stsp if (gitconfig_owner) {
175 6a800804 2022-10-13 stsp err = got_privsep_send_gitconfig_owner_req(ibuf);
178 6a800804 2022-10-13 stsp err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
183 6a800804 2022-10-13 stsp err = got_privsep_send_stop(imsg_fds[0]);
184 6a800804 2022-10-13 stsp child_err = got_privsep_wait_for_child(pid);
185 6a800804 2022-10-13 stsp if (child_err && err == NULL)
186 6a800804 2022-10-13 stsp err = child_err;
188 6a800804 2022-10-13 stsp if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
189 6a800804 2022-10-13 stsp err = got_error_from_errno("close");
190 6a800804 2022-10-13 stsp if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
191 6a800804 2022-10-13 stsp err = got_error_from_errno("close");
192 6a800804 2022-10-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
193 6a800804 2022-10-13 stsp err = got_error_from_errno2("close", gitconfig_path);
194 6a800804 2022-10-13 stsp free(ibuf);
195 6a800804 2022-10-13 stsp return err;