Blame


1 d348087d 2022-10-13 thomas /*
2 d348087d 2022-10-13 thomas * Copyright (c) 2019, 2022 Stefan Sperling <stsp@openbsd.org>
3 d348087d 2022-10-13 thomas *
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.
7 d348087d 2022-10-13 thomas *
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.
15 d348087d 2022-10-13 thomas */
16 d348087d 2022-10-13 thomas
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>
22 d348087d 2022-10-13 thomas
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>
31 d348087d 2022-10-13 thomas
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"
37 d348087d 2022-10-13 thomas
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"
44 d348087d 2022-10-13 thomas
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)
51 d348087d 2022-10-13 thomas {
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;
57 d348087d 2022-10-13 thomas
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;
73 d348087d 2022-10-13 thomas
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);
79 d348087d 2022-10-13 thomas }
80 d348087d 2022-10-13 thomas
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;
85 d348087d 2022-10-13 thomas }
86 d348087d 2022-10-13 thomas
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;
90 d348087d 2022-10-13 thomas }
91 d348087d 2022-10-13 thomas
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 */
100 d348087d 2022-10-13 thomas }
101 d348087d 2022-10-13 thomas
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;
105 d348087d 2022-10-13 thomas }
106 d348087d 2022-10-13 thomas imsg_fds[1] = -1;
107 d348087d 2022-10-13 thomas imsg_init(ibuf, imsg_fds[0]);
108 d348087d 2022-10-13 thomas
109 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
110 d348087d 2022-10-13 thomas if (err)
111 d348087d 2022-10-13 thomas goto done;
112 d348087d 2022-10-13 thomas fd = -1;
113 d348087d 2022-10-13 thomas
114 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
115 d348087d 2022-10-13 thomas if (err)
116 d348087d 2022-10-13 thomas goto done;
117 d348087d 2022-10-13 thomas
118 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_int(
119 d348087d 2022-10-13 thomas gitconfig_repository_format_version, ibuf);
120 d348087d 2022-10-13 thomas if (err)
121 d348087d 2022-10-13 thomas goto done;
122 d348087d 2022-10-13 thomas
123 b091c2cd 2023-02-07 thomas if (extnames && extvals && nextensions) {
124 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_repository_extensions_req(
125 d348087d 2022-10-13 thomas ibuf);
126 d348087d 2022-10-13 thomas if (err)
127 d348087d 2022-10-13 thomas goto done;
128 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
129 d348087d 2022-10-13 thomas if (err)
130 d348087d 2022-10-13 thomas goto done;
131 d348087d 2022-10-13 thomas if (*nextensions > 0) {
132 d348087d 2022-10-13 thomas int i;
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;
137 d348087d 2022-10-13 thomas }
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;
142 b091c2cd 2023-02-07 thomas }
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);
147 d348087d 2022-10-13 thomas if (err)
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;
151 d348087d 2022-10-13 thomas }
152 d348087d 2022-10-13 thomas }
153 d348087d 2022-10-13 thomas }
154 d348087d 2022-10-13 thomas
155 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_author_name_req(ibuf);
156 d348087d 2022-10-13 thomas if (err)
157 d348087d 2022-10-13 thomas goto done;
158 d348087d 2022-10-13 thomas
159 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
160 d348087d 2022-10-13 thomas if (err)
161 d348087d 2022-10-13 thomas goto done;
162 d348087d 2022-10-13 thomas
163 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_author_email_req(ibuf);
164 d348087d 2022-10-13 thomas if (err)
165 d348087d 2022-10-13 thomas goto done;
166 d348087d 2022-10-13 thomas
167 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
168 d348087d 2022-10-13 thomas if (err)
169 d348087d 2022-10-13 thomas goto done;
170 d348087d 2022-10-13 thomas
171 d348087d 2022-10-13 thomas if (remotes && nremotes) {
172 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_remotes_req(ibuf);
173 d348087d 2022-10-13 thomas if (err)
174 d348087d 2022-10-13 thomas goto done;
175 d348087d 2022-10-13 thomas
176 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_remotes(remotes,
177 d348087d 2022-10-13 thomas nremotes, ibuf);
178 d348087d 2022-10-13 thomas if (err)
179 d348087d 2022-10-13 thomas goto done;
180 d348087d 2022-10-13 thomas }
181 d348087d 2022-10-13 thomas
182 d348087d 2022-10-13 thomas if (gitconfig_owner) {
183 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_owner_req(ibuf);
184 d348087d 2022-10-13 thomas if (err)
185 d348087d 2022-10-13 thomas goto done;
186 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
187 d348087d 2022-10-13 thomas if (err)
188 d348087d 2022-10-13 thomas goto done;
189 d348087d 2022-10-13 thomas }
190 d348087d 2022-10-13 thomas
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;
195 d348087d 2022-10-13 thomas done:
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;
204 d348087d 2022-10-13 thomas }