Blame


1 6a800804 2022-10-13 stsp /*
2 6a800804 2022-10-13 stsp * Copyright (c) 2019, 2022 Stefan Sperling <stsp@openbsd.org>
3 6a800804 2022-10-13 stsp *
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.
7 6a800804 2022-10-13 stsp *
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.
15 6a800804 2022-10-13 stsp */
16 6a800804 2022-10-13 stsp
17 1b3e8e86 2022-11-08 stsp #include <sys/time.h>
18 6a800804 2022-10-13 stsp #include <sys/types.h>
19 6a800804 2022-10-13 stsp #include <sys/tree.h>
20 6a800804 2022-10-13 stsp #include <sys/socket.h>
21 6a800804 2022-10-13 stsp #include <sys/queue.h>
22 6a800804 2022-10-13 stsp #include <sys/uio.h>
23 6a800804 2022-10-13 stsp
24 6a800804 2022-10-13 stsp #include <errno.h>
25 6a800804 2022-10-13 stsp #include <fcntl.h>
26 6a800804 2022-10-13 stsp #include <limits.h>
27 6a800804 2022-10-13 stsp #include <sha1.h>
28 5822e79e 2023-02-23 op #include <sha2.h>
29 6a800804 2022-10-13 stsp #include <stdio.h>
30 6a800804 2022-10-13 stsp #include <stdlib.h>
31 6a800804 2022-10-13 stsp #include <stdint.h>
32 6a800804 2022-10-13 stsp #include <imsg.h>
33 6a800804 2022-10-13 stsp #include <unistd.h>
34 6a800804 2022-10-13 stsp
35 6a800804 2022-10-13 stsp #include "got_error.h"
36 6a800804 2022-10-13 stsp #include "got_object.h"
37 6a800804 2022-10-13 stsp #include "got_repository.h"
38 6a800804 2022-10-13 stsp #include "got_path.h"
39 6a800804 2022-10-13 stsp
40 6a800804 2022-10-13 stsp #include "got_lib_delta.h"
41 6a800804 2022-10-13 stsp #include "got_lib_object.h"
42 6a800804 2022-10-13 stsp #include "got_lib_object_cache.h"
43 6a800804 2022-10-13 stsp #include "got_lib_privsep.h"
44 6a800804 2022-10-13 stsp #include "got_lib_pack.h"
45 6a800804 2022-10-13 stsp #include "got_lib_repository.h"
46 6a800804 2022-10-13 stsp
47 6a800804 2022-10-13 stsp const struct got_error *
48 6a800804 2022-10-13 stsp got_repo_read_gitconfig(int *gitconfig_repository_format_version,
49 6a800804 2022-10-13 stsp char **gitconfig_author_name, char **gitconfig_author_email,
50 6a800804 2022-10-13 stsp struct got_remote_repo **remotes, int *nremotes,
51 27749ea2 2023-02-05 op char **gitconfig_owner, char ***extnames, char ***extvals,
52 27749ea2 2023-02-05 op int *nextensions, const char *gitconfig_path)
53 6a800804 2022-10-13 stsp {
54 6a800804 2022-10-13 stsp const struct got_error *err = NULL, *child_err = NULL;
55 6a800804 2022-10-13 stsp int fd = -1;
56 6a800804 2022-10-13 stsp int imsg_fds[2] = { -1, -1 };
57 6a800804 2022-10-13 stsp pid_t pid;
58 6a800804 2022-10-13 stsp struct imsgbuf *ibuf;
59 6a800804 2022-10-13 stsp
60 6a800804 2022-10-13 stsp *gitconfig_repository_format_version = 0;
61 27749ea2 2023-02-05 op if (extnames)
62 27749ea2 2023-02-05 op *extnames = NULL;
63 27749ea2 2023-02-05 op if (extvals)
64 27749ea2 2023-02-05 op *extvals = NULL;
65 6a800804 2022-10-13 stsp if (nextensions)
66 6a800804 2022-10-13 stsp *nextensions = 0;
67 6a800804 2022-10-13 stsp *gitconfig_author_name = NULL;
68 6a800804 2022-10-13 stsp *gitconfig_author_email = NULL;
69 6a800804 2022-10-13 stsp if (remotes)
70 6a800804 2022-10-13 stsp *remotes = NULL;
71 6a800804 2022-10-13 stsp if (nremotes)
72 6a800804 2022-10-13 stsp *nremotes = 0;
73 6a800804 2022-10-13 stsp if (gitconfig_owner)
74 6a800804 2022-10-13 stsp *gitconfig_owner = NULL;
75 6a800804 2022-10-13 stsp
76 6a800804 2022-10-13 stsp fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
77 6a800804 2022-10-13 stsp if (fd == -1) {
78 6a800804 2022-10-13 stsp if (errno == ENOENT)
79 6a800804 2022-10-13 stsp return NULL;
80 6a800804 2022-10-13 stsp return got_error_from_errno2("open", gitconfig_path);
81 6a800804 2022-10-13 stsp }
82 6a800804 2022-10-13 stsp
83 6a800804 2022-10-13 stsp ibuf = calloc(1, sizeof(*ibuf));
84 6a800804 2022-10-13 stsp if (ibuf == NULL) {
85 6a800804 2022-10-13 stsp err = got_error_from_errno("calloc");
86 6a800804 2022-10-13 stsp goto done;
87 6a800804 2022-10-13 stsp }
88 6a800804 2022-10-13 stsp
89 6a800804 2022-10-13 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
90 6a800804 2022-10-13 stsp err = got_error_from_errno("socketpair");
91 6a800804 2022-10-13 stsp goto done;
92 6a800804 2022-10-13 stsp }
93 6a800804 2022-10-13 stsp
94 6a800804 2022-10-13 stsp pid = fork();
95 6a800804 2022-10-13 stsp if (pid == -1) {
96 6a800804 2022-10-13 stsp err = got_error_from_errno("fork");
97 6a800804 2022-10-13 stsp goto done;
98 6a800804 2022-10-13 stsp } else if (pid == 0) {
99 6a800804 2022-10-13 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
100 6a800804 2022-10-13 stsp gitconfig_path);
101 6a800804 2022-10-13 stsp /* not reached */
102 6a800804 2022-10-13 stsp }
103 6a800804 2022-10-13 stsp
104 6a800804 2022-10-13 stsp if (close(imsg_fds[1]) == -1) {
105 6a800804 2022-10-13 stsp err = got_error_from_errno("close");
106 6a800804 2022-10-13 stsp goto done;
107 6a800804 2022-10-13 stsp }
108 6a800804 2022-10-13 stsp imsg_fds[1] = -1;
109 6a800804 2022-10-13 stsp imsg_init(ibuf, imsg_fds[0]);
110 6a800804 2022-10-13 stsp
111 6a800804 2022-10-13 stsp err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
112 6a800804 2022-10-13 stsp if (err)
113 6a800804 2022-10-13 stsp goto done;
114 6a800804 2022-10-13 stsp fd = -1;
115 6a800804 2022-10-13 stsp
116 6a800804 2022-10-13 stsp err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
117 6a800804 2022-10-13 stsp if (err)
118 6a800804 2022-10-13 stsp goto done;
119 6a800804 2022-10-13 stsp
120 6a800804 2022-10-13 stsp err = got_privsep_recv_gitconfig_int(
121 6a800804 2022-10-13 stsp gitconfig_repository_format_version, ibuf);
122 6a800804 2022-10-13 stsp if (err)
123 6a800804 2022-10-13 stsp goto done;
124 6a800804 2022-10-13 stsp
125 27749ea2 2023-02-05 op if (extnames && extvals && nextensions) {
126 6a800804 2022-10-13 stsp err = got_privsep_send_gitconfig_repository_extensions_req(
127 6a800804 2022-10-13 stsp ibuf);
128 6a800804 2022-10-13 stsp if (err)
129 6a800804 2022-10-13 stsp goto done;
130 6a800804 2022-10-13 stsp err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
131 6a800804 2022-10-13 stsp if (err)
132 6a800804 2022-10-13 stsp goto done;
133 6a800804 2022-10-13 stsp if (*nextensions > 0) {
134 6a800804 2022-10-13 stsp int i;
135 27749ea2 2023-02-05 op *extnames = calloc(*nextensions, sizeof(char *));
136 27749ea2 2023-02-05 op if (*extnames == NULL) {
137 6a800804 2022-10-13 stsp err = got_error_from_errno("calloc");
138 6a800804 2022-10-13 stsp goto done;
139 6a800804 2022-10-13 stsp }
140 27749ea2 2023-02-05 op *extvals = calloc(*nextensions, sizeof(char *));
141 27749ea2 2023-02-05 op if (*extvals == NULL) {
142 27749ea2 2023-02-05 op err = got_error_from_errno("calloc");
143 27749ea2 2023-02-05 op goto done;
144 27749ea2 2023-02-05 op }
145 6a800804 2022-10-13 stsp for (i = 0; i < *nextensions; i++) {
146 27749ea2 2023-02-05 op char *ext, *val;
147 27749ea2 2023-02-05 op err = got_privsep_recv_gitconfig_pair(&ext,
148 27749ea2 2023-02-05 op &val, ibuf);
149 6a800804 2022-10-13 stsp if (err)
150 6a800804 2022-10-13 stsp goto done;
151 27749ea2 2023-02-05 op (*extnames)[i] = ext;
152 27749ea2 2023-02-05 op (*extvals)[i] = val;
153 6a800804 2022-10-13 stsp }
154 6a800804 2022-10-13 stsp }
155 6a800804 2022-10-13 stsp }
156 6a800804 2022-10-13 stsp
157 6a800804 2022-10-13 stsp err = got_privsep_send_gitconfig_author_name_req(ibuf);
158 6a800804 2022-10-13 stsp if (err)
159 6a800804 2022-10-13 stsp goto done;
160 6a800804 2022-10-13 stsp
161 6a800804 2022-10-13 stsp err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
162 6a800804 2022-10-13 stsp if (err)
163 6a800804 2022-10-13 stsp goto done;
164 6a800804 2022-10-13 stsp
165 6a800804 2022-10-13 stsp err = got_privsep_send_gitconfig_author_email_req(ibuf);
166 6a800804 2022-10-13 stsp if (err)
167 6a800804 2022-10-13 stsp goto done;
168 6a800804 2022-10-13 stsp
169 6a800804 2022-10-13 stsp err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
170 6a800804 2022-10-13 stsp if (err)
171 6a800804 2022-10-13 stsp goto done;
172 6a800804 2022-10-13 stsp
173 6a800804 2022-10-13 stsp if (remotes && nremotes) {
174 6a800804 2022-10-13 stsp err = got_privsep_send_gitconfig_remotes_req(ibuf);
175 6a800804 2022-10-13 stsp if (err)
176 6a800804 2022-10-13 stsp goto done;
177 6a800804 2022-10-13 stsp
178 6a800804 2022-10-13 stsp err = got_privsep_recv_gitconfig_remotes(remotes,
179 6a800804 2022-10-13 stsp nremotes, ibuf);
180 6a800804 2022-10-13 stsp if (err)
181 6a800804 2022-10-13 stsp goto done;
182 6a800804 2022-10-13 stsp }
183 6a800804 2022-10-13 stsp
184 6a800804 2022-10-13 stsp if (gitconfig_owner) {
185 6a800804 2022-10-13 stsp err = got_privsep_send_gitconfig_owner_req(ibuf);
186 6a800804 2022-10-13 stsp if (err)
187 6a800804 2022-10-13 stsp goto done;
188 6a800804 2022-10-13 stsp err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
189 6a800804 2022-10-13 stsp if (err)
190 6a800804 2022-10-13 stsp goto done;
191 6a800804 2022-10-13 stsp }
192 6a800804 2022-10-13 stsp
193 6a800804 2022-10-13 stsp err = got_privsep_send_stop(imsg_fds[0]);
194 6a800804 2022-10-13 stsp child_err = got_privsep_wait_for_child(pid);
195 6a800804 2022-10-13 stsp if (child_err && err == NULL)
196 6a800804 2022-10-13 stsp err = child_err;
197 6a800804 2022-10-13 stsp done:
198 6a800804 2022-10-13 stsp if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
199 6a800804 2022-10-13 stsp err = got_error_from_errno("close");
200 6a800804 2022-10-13 stsp if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
201 6a800804 2022-10-13 stsp err = got_error_from_errno("close");
202 6a800804 2022-10-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
203 6a800804 2022-10-13 stsp err = got_error_from_errno2("close", gitconfig_path);
204 6a800804 2022-10-13 stsp free(ibuf);
205 6a800804 2022-10-13 stsp return err;
206 6a800804 2022-10-13 stsp }