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 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
33 d348087d 2022-10-13 thomas #include "got_error.h"
34 d348087d 2022-10-13 thomas #include "got_object.h"
35 d348087d 2022-10-13 thomas #include "got_repository.h"
36 d348087d 2022-10-13 thomas #include "got_path.h"
38 d348087d 2022-10-13 thomas #include "got_lib_delta.h"
39 d348087d 2022-10-13 thomas #include "got_lib_object.h"
40 d348087d 2022-10-13 thomas #include "got_lib_object_cache.h"
41 d348087d 2022-10-13 thomas #include "got_lib_privsep.h"
42 d348087d 2022-10-13 thomas #include "got_lib_pack.h"
43 d348087d 2022-10-13 thomas #include "got_lib_repository.h"
45 d348087d 2022-10-13 thomas const struct got_error *
46 d348087d 2022-10-13 thomas got_repo_read_gitconfig(int *gitconfig_repository_format_version,
47 d348087d 2022-10-13 thomas char **gitconfig_author_name, char **gitconfig_author_email,
48 d348087d 2022-10-13 thomas struct got_remote_repo **remotes, int *nremotes,
49 b091c2cd 2023-02-07 thomas char **gitconfig_owner, char ***extnames, char ***extvals,
50 b091c2cd 2023-02-07 thomas int *nextensions, const char *gitconfig_path)
52 d348087d 2022-10-13 thomas const struct got_error *err = NULL, *child_err = NULL;
53 d348087d 2022-10-13 thomas int fd = -1;
54 d348087d 2022-10-13 thomas int imsg_fds[2] = { -1, -1 };
55 d348087d 2022-10-13 thomas pid_t pid;
56 d348087d 2022-10-13 thomas struct imsgbuf *ibuf;
58 d348087d 2022-10-13 thomas *gitconfig_repository_format_version = 0;
59 b091c2cd 2023-02-07 thomas if (extnames)
60 b091c2cd 2023-02-07 thomas *extnames = NULL;
61 b091c2cd 2023-02-07 thomas if (extvals)
62 b091c2cd 2023-02-07 thomas *extvals = NULL;
63 d348087d 2022-10-13 thomas if (nextensions)
64 d348087d 2022-10-13 thomas *nextensions = 0;
65 d348087d 2022-10-13 thomas *gitconfig_author_name = NULL;
66 d348087d 2022-10-13 thomas *gitconfig_author_email = NULL;
67 d348087d 2022-10-13 thomas if (remotes)
68 d348087d 2022-10-13 thomas *remotes = NULL;
69 d348087d 2022-10-13 thomas if (nremotes)
70 d348087d 2022-10-13 thomas *nremotes = 0;
71 d348087d 2022-10-13 thomas if (gitconfig_owner)
72 d348087d 2022-10-13 thomas *gitconfig_owner = NULL;
74 d348087d 2022-10-13 thomas fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
75 d348087d 2022-10-13 thomas if (fd == -1) {
76 d348087d 2022-10-13 thomas if (errno == ENOENT)
77 d348087d 2022-10-13 thomas return NULL;
78 d348087d 2022-10-13 thomas return got_error_from_errno2("open", gitconfig_path);
81 d348087d 2022-10-13 thomas ibuf = calloc(1, sizeof(*ibuf));
82 d348087d 2022-10-13 thomas if (ibuf == NULL) {
83 d348087d 2022-10-13 thomas err = got_error_from_errno("calloc");
84 d348087d 2022-10-13 thomas goto done;
87 d348087d 2022-10-13 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
88 d348087d 2022-10-13 thomas err = got_error_from_errno("socketpair");
89 d348087d 2022-10-13 thomas goto done;
92 d348087d 2022-10-13 thomas pid = fork();
93 d348087d 2022-10-13 thomas if (pid == -1) {
94 d348087d 2022-10-13 thomas err = got_error_from_errno("fork");
95 d348087d 2022-10-13 thomas goto done;
96 d348087d 2022-10-13 thomas } else if (pid == 0) {
97 d348087d 2022-10-13 thomas got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
98 d348087d 2022-10-13 thomas gitconfig_path);
99 d348087d 2022-10-13 thomas /* not reached */
102 d348087d 2022-10-13 thomas if (close(imsg_fds[1]) == -1) {
103 d348087d 2022-10-13 thomas err = got_error_from_errno("close");
104 d348087d 2022-10-13 thomas goto done;
106 d348087d 2022-10-13 thomas imsg_fds[1] = -1;
107 d348087d 2022-10-13 thomas imsg_init(ibuf, imsg_fds[0]);
109 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
111 d348087d 2022-10-13 thomas goto done;
114 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
116 d348087d 2022-10-13 thomas goto done;
118 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_int(
119 d348087d 2022-10-13 thomas gitconfig_repository_format_version, ibuf);
121 d348087d 2022-10-13 thomas goto done;
123 b091c2cd 2023-02-07 thomas if (extnames && extvals && nextensions) {
124 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_repository_extensions_req(
127 d348087d 2022-10-13 thomas goto done;
128 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
130 d348087d 2022-10-13 thomas goto done;
131 d348087d 2022-10-13 thomas if (*nextensions > 0) {
133 b091c2cd 2023-02-07 thomas *extnames = calloc(*nextensions, sizeof(char *));
134 b091c2cd 2023-02-07 thomas if (*extnames == NULL) {
135 d348087d 2022-10-13 thomas err = got_error_from_errno("calloc");
136 d348087d 2022-10-13 thomas goto done;
138 b091c2cd 2023-02-07 thomas *extvals = calloc(*nextensions, sizeof(char *));
139 b091c2cd 2023-02-07 thomas if (*extvals == NULL) {
140 b091c2cd 2023-02-07 thomas err = got_error_from_errno("calloc");
141 b091c2cd 2023-02-07 thomas goto done;
143 d348087d 2022-10-13 thomas for (i = 0; i < *nextensions; i++) {
144 b091c2cd 2023-02-07 thomas char *ext, *val;
145 b091c2cd 2023-02-07 thomas err = got_privsep_recv_gitconfig_pair(&ext,
146 b091c2cd 2023-02-07 thomas &val, ibuf);
148 d348087d 2022-10-13 thomas goto done;
149 b091c2cd 2023-02-07 thomas (*extnames)[i] = ext;
150 b091c2cd 2023-02-07 thomas (*extvals)[i] = val;
155 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_author_name_req(ibuf);
157 d348087d 2022-10-13 thomas goto done;
159 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
161 d348087d 2022-10-13 thomas goto done;
163 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_author_email_req(ibuf);
165 d348087d 2022-10-13 thomas goto done;
167 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
169 d348087d 2022-10-13 thomas goto done;
171 d348087d 2022-10-13 thomas if (remotes && nremotes) {
172 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_remotes_req(ibuf);
174 d348087d 2022-10-13 thomas goto done;
176 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_remotes(remotes,
177 d348087d 2022-10-13 thomas nremotes, ibuf);
179 d348087d 2022-10-13 thomas goto done;
182 d348087d 2022-10-13 thomas if (gitconfig_owner) {
183 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_owner_req(ibuf);
185 d348087d 2022-10-13 thomas goto done;
186 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
188 d348087d 2022-10-13 thomas goto done;
191 d348087d 2022-10-13 thomas err = got_privsep_send_stop(imsg_fds[0]);
192 d348087d 2022-10-13 thomas child_err = got_privsep_wait_for_child(pid);
193 d348087d 2022-10-13 thomas if (child_err && err == NULL)
194 d348087d 2022-10-13 thomas err = child_err;
196 d348087d 2022-10-13 thomas if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
197 d348087d 2022-10-13 thomas err = got_error_from_errno("close");
198 d348087d 2022-10-13 thomas if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
199 d348087d 2022-10-13 thomas err = got_error_from_errno("close");
200 d348087d 2022-10-13 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
201 d348087d 2022-10-13 thomas err = got_error_from_errno2("close", gitconfig_path);
202 d348087d 2022-10-13 thomas free(ibuf);
203 d348087d 2022-10-13 thomas return err;