2 d348087d 2022-10-13 thomas * Copyright (c) 2019, 2022 Stefan Sperling <stsp@openbsd.org>
4 d348087d 2022-10-13 thomas * Permission to use, copy, modify, and distribute this software for any
5 d348087d 2022-10-13 thomas * purpose with or without fee is hereby granted, provided that the above
6 d348087d 2022-10-13 thomas * copyright notice and this permission notice appear in all copies.
8 d348087d 2022-10-13 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d348087d 2022-10-13 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d348087d 2022-10-13 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d348087d 2022-10-13 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d348087d 2022-10-13 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d348087d 2022-10-13 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d348087d 2022-10-13 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 9b251092 2022-11-08 thomas #include <sys/time.h>
18 d348087d 2022-10-13 thomas #include <sys/types.h>
19 d348087d 2022-10-13 thomas #include <sys/socket.h>
20 d348087d 2022-10-13 thomas #include <sys/queue.h>
21 d348087d 2022-10-13 thomas #include <sys/uio.h>
23 d348087d 2022-10-13 thomas #include <errno.h>
24 d348087d 2022-10-13 thomas #include <fcntl.h>
25 d348087d 2022-10-13 thomas #include <limits.h>
26 d348087d 2022-10-13 thomas #include <stdio.h>
27 d348087d 2022-10-13 thomas #include <stdlib.h>
28 d348087d 2022-10-13 thomas #include <stdint.h>
29 d348087d 2022-10-13 thomas #include <imsg.h>
30 d348087d 2022-10-13 thomas #include <unistd.h>
32 d348087d 2022-10-13 thomas #include "got_error.h"
33 d348087d 2022-10-13 thomas #include "got_object.h"
34 d348087d 2022-10-13 thomas #include "got_repository.h"
35 d348087d 2022-10-13 thomas #include "got_path.h"
37 d348087d 2022-10-13 thomas #include "got_lib_delta.h"
38 d348087d 2022-10-13 thomas #include "got_lib_object.h"
39 d348087d 2022-10-13 thomas #include "got_lib_object_cache.h"
40 d348087d 2022-10-13 thomas #include "got_lib_privsep.h"
41 d348087d 2022-10-13 thomas #include "got_lib_pack.h"
42 d348087d 2022-10-13 thomas #include "got_lib_repository.h"
44 d348087d 2022-10-13 thomas const struct got_error *
45 d348087d 2022-10-13 thomas got_repo_read_gitconfig(int *gitconfig_repository_format_version,
46 d348087d 2022-10-13 thomas char **gitconfig_author_name, char **gitconfig_author_email,
47 d348087d 2022-10-13 thomas struct got_remote_repo **remotes, int *nremotes,
48 b091c2cd 2023-02-07 thomas char **gitconfig_owner, char ***extnames, char ***extvals,
49 b091c2cd 2023-02-07 thomas int *nextensions, const char *gitconfig_path)
51 d348087d 2022-10-13 thomas const struct got_error *err = NULL, *child_err = NULL;
52 d348087d 2022-10-13 thomas int fd = -1;
53 d348087d 2022-10-13 thomas int imsg_fds[2] = { -1, -1 };
54 d348087d 2022-10-13 thomas pid_t pid;
55 d348087d 2022-10-13 thomas struct imsgbuf *ibuf;
57 d348087d 2022-10-13 thomas *gitconfig_repository_format_version = 0;
58 b091c2cd 2023-02-07 thomas if (extnames)
59 b091c2cd 2023-02-07 thomas *extnames = NULL;
60 b091c2cd 2023-02-07 thomas if (extvals)
61 b091c2cd 2023-02-07 thomas *extvals = NULL;
62 d348087d 2022-10-13 thomas if (nextensions)
63 d348087d 2022-10-13 thomas *nextensions = 0;
64 d348087d 2022-10-13 thomas *gitconfig_author_name = NULL;
65 d348087d 2022-10-13 thomas *gitconfig_author_email = NULL;
66 d348087d 2022-10-13 thomas if (remotes)
67 d348087d 2022-10-13 thomas *remotes = NULL;
68 d348087d 2022-10-13 thomas if (nremotes)
69 d348087d 2022-10-13 thomas *nremotes = 0;
70 d348087d 2022-10-13 thomas if (gitconfig_owner)
71 d348087d 2022-10-13 thomas *gitconfig_owner = NULL;
73 d348087d 2022-10-13 thomas fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
74 d348087d 2022-10-13 thomas if (fd == -1) {
75 d348087d 2022-10-13 thomas if (errno == ENOENT)
76 d348087d 2022-10-13 thomas return NULL;
77 d348087d 2022-10-13 thomas return got_error_from_errno2("open", gitconfig_path);
80 d348087d 2022-10-13 thomas ibuf = calloc(1, sizeof(*ibuf));
81 d348087d 2022-10-13 thomas if (ibuf == NULL) {
82 d348087d 2022-10-13 thomas err = got_error_from_errno("calloc");
83 d348087d 2022-10-13 thomas goto done;
86 d348087d 2022-10-13 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
87 d348087d 2022-10-13 thomas err = got_error_from_errno("socketpair");
88 d348087d 2022-10-13 thomas goto done;
91 d348087d 2022-10-13 thomas pid = fork();
92 d348087d 2022-10-13 thomas if (pid == -1) {
93 d348087d 2022-10-13 thomas err = got_error_from_errno("fork");
94 d348087d 2022-10-13 thomas goto done;
95 d348087d 2022-10-13 thomas } else if (pid == 0) {
96 d348087d 2022-10-13 thomas got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
97 d348087d 2022-10-13 thomas gitconfig_path);
98 d348087d 2022-10-13 thomas /* not reached */
101 d348087d 2022-10-13 thomas if (close(imsg_fds[1]) == -1) {
102 d348087d 2022-10-13 thomas err = got_error_from_errno("close");
103 d348087d 2022-10-13 thomas goto done;
105 d348087d 2022-10-13 thomas imsg_fds[1] = -1;
106 d348087d 2022-10-13 thomas imsg_init(ibuf, imsg_fds[0]);
108 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
110 d348087d 2022-10-13 thomas goto done;
113 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
115 d348087d 2022-10-13 thomas goto done;
117 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_int(
118 d348087d 2022-10-13 thomas gitconfig_repository_format_version, ibuf);
120 d348087d 2022-10-13 thomas goto done;
122 b091c2cd 2023-02-07 thomas if (extnames && extvals && nextensions) {
123 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_repository_extensions_req(
126 d348087d 2022-10-13 thomas goto done;
127 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
129 d348087d 2022-10-13 thomas goto done;
130 d348087d 2022-10-13 thomas if (*nextensions > 0) {
132 b091c2cd 2023-02-07 thomas *extnames = calloc(*nextensions, sizeof(char *));
133 b091c2cd 2023-02-07 thomas if (*extnames == NULL) {
134 d348087d 2022-10-13 thomas err = got_error_from_errno("calloc");
135 d348087d 2022-10-13 thomas goto done;
137 b091c2cd 2023-02-07 thomas *extvals = calloc(*nextensions, sizeof(char *));
138 b091c2cd 2023-02-07 thomas if (*extvals == NULL) {
139 b091c2cd 2023-02-07 thomas err = got_error_from_errno("calloc");
140 b091c2cd 2023-02-07 thomas goto done;
142 d348087d 2022-10-13 thomas for (i = 0; i < *nextensions; i++) {
143 b091c2cd 2023-02-07 thomas char *ext, *val;
144 b091c2cd 2023-02-07 thomas err = got_privsep_recv_gitconfig_pair(&ext,
145 b091c2cd 2023-02-07 thomas &val, ibuf);
147 d348087d 2022-10-13 thomas goto done;
148 b091c2cd 2023-02-07 thomas (*extnames)[i] = ext;
149 b091c2cd 2023-02-07 thomas (*extvals)[i] = val;
154 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_author_name_req(ibuf);
156 d348087d 2022-10-13 thomas goto done;
158 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
160 d348087d 2022-10-13 thomas goto done;
162 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_author_email_req(ibuf);
164 d348087d 2022-10-13 thomas goto done;
166 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
168 d348087d 2022-10-13 thomas goto done;
170 d348087d 2022-10-13 thomas if (remotes && nremotes) {
171 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_remotes_req(ibuf);
173 d348087d 2022-10-13 thomas goto done;
175 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_remotes(remotes,
176 d348087d 2022-10-13 thomas nremotes, ibuf);
178 d348087d 2022-10-13 thomas goto done;
181 d348087d 2022-10-13 thomas if (gitconfig_owner) {
182 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_owner_req(ibuf);
184 d348087d 2022-10-13 thomas goto done;
185 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
187 d348087d 2022-10-13 thomas goto done;
190 d348087d 2022-10-13 thomas err = got_privsep_send_stop(imsg_fds[0]);
191 d348087d 2022-10-13 thomas child_err = got_privsep_wait_for_child(pid);
192 d348087d 2022-10-13 thomas if (child_err && err == NULL)
193 d348087d 2022-10-13 thomas err = child_err;
195 d348087d 2022-10-13 thomas if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
196 d348087d 2022-10-13 thomas err = got_error_from_errno("close");
197 d348087d 2022-10-13 thomas if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
198 d348087d 2022-10-13 thomas err = got_error_from_errno("close");
199 d348087d 2022-10-13 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
200 d348087d 2022-10-13 thomas err = got_error_from_errno2("close", gitconfig_path);
201 d348087d 2022-10-13 thomas free(ibuf);
202 d348087d 2022-10-13 thomas return err;