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 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"
36 d348087d 2022-10-13 thomas
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"
43 d348087d 2022-10-13 thomas
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)
50 d348087d 2022-10-13 thomas {
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;
56 d348087d 2022-10-13 thomas
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;
72 d348087d 2022-10-13 thomas
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);
78 d348087d 2022-10-13 thomas }
79 d348087d 2022-10-13 thomas
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;
84 d348087d 2022-10-13 thomas }
85 d348087d 2022-10-13 thomas
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;
89 d348087d 2022-10-13 thomas }
90 d348087d 2022-10-13 thomas
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 */
99 d348087d 2022-10-13 thomas }
100 d348087d 2022-10-13 thomas
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;
104 d348087d 2022-10-13 thomas }
105 d348087d 2022-10-13 thomas imsg_fds[1] = -1;
106 d348087d 2022-10-13 thomas imsg_init(ibuf, imsg_fds[0]);
107 d348087d 2022-10-13 thomas
108 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
109 d348087d 2022-10-13 thomas if (err)
110 d348087d 2022-10-13 thomas goto done;
111 d348087d 2022-10-13 thomas fd = -1;
112 d348087d 2022-10-13 thomas
113 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
114 d348087d 2022-10-13 thomas if (err)
115 d348087d 2022-10-13 thomas goto done;
116 d348087d 2022-10-13 thomas
117 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_int(
118 d348087d 2022-10-13 thomas gitconfig_repository_format_version, ibuf);
119 d348087d 2022-10-13 thomas if (err)
120 d348087d 2022-10-13 thomas goto done;
121 d348087d 2022-10-13 thomas
122 b091c2cd 2023-02-07 thomas if (extnames && extvals && nextensions) {
123 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_repository_extensions_req(
124 d348087d 2022-10-13 thomas ibuf);
125 d348087d 2022-10-13 thomas if (err)
126 d348087d 2022-10-13 thomas goto done;
127 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
128 d348087d 2022-10-13 thomas if (err)
129 d348087d 2022-10-13 thomas goto done;
130 d348087d 2022-10-13 thomas if (*nextensions > 0) {
131 d348087d 2022-10-13 thomas int i;
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;
136 d348087d 2022-10-13 thomas }
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;
141 b091c2cd 2023-02-07 thomas }
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);
146 d348087d 2022-10-13 thomas if (err)
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;
150 d348087d 2022-10-13 thomas }
151 d348087d 2022-10-13 thomas }
152 d348087d 2022-10-13 thomas }
153 d348087d 2022-10-13 thomas
154 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_author_name_req(ibuf);
155 d348087d 2022-10-13 thomas if (err)
156 d348087d 2022-10-13 thomas goto done;
157 d348087d 2022-10-13 thomas
158 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
159 d348087d 2022-10-13 thomas if (err)
160 d348087d 2022-10-13 thomas goto done;
161 d348087d 2022-10-13 thomas
162 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_author_email_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_str(gitconfig_author_email, ibuf);
167 d348087d 2022-10-13 thomas if (err)
168 d348087d 2022-10-13 thomas goto done;
169 d348087d 2022-10-13 thomas
170 d348087d 2022-10-13 thomas if (remotes && nremotes) {
171 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_remotes_req(ibuf);
172 d348087d 2022-10-13 thomas if (err)
173 d348087d 2022-10-13 thomas goto done;
174 d348087d 2022-10-13 thomas
175 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_remotes(remotes,
176 d348087d 2022-10-13 thomas nremotes, 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 if (gitconfig_owner) {
182 d348087d 2022-10-13 thomas err = got_privsep_send_gitconfig_owner_req(ibuf);
183 d348087d 2022-10-13 thomas if (err)
184 d348087d 2022-10-13 thomas goto done;
185 d348087d 2022-10-13 thomas err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
186 d348087d 2022-10-13 thomas if (err)
187 d348087d 2022-10-13 thomas goto done;
188 d348087d 2022-10-13 thomas }
189 d348087d 2022-10-13 thomas
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;
194 d348087d 2022-10-13 thomas done:
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;
203 d348087d 2022-10-13 thomas }