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