Blob


1 /*
2 * Copyright (c) 2019, 2022 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/time.h>
18 #include <sys/types.h>
19 #include <sys/tree.h>
20 #include <sys/socket.h>
21 #include <sys/queue.h>
22 #include <sys/uio.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <sha1.h>
28 #include <sha2.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <imsg.h>
33 #include <unistd.h>
35 #include <string.h>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_repository.h"
40 #include "got_path.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_cache.h"
45 #include "got_lib_privsep.h"
46 #include "got_lib_pack.h"
47 #include "got_lib_repository.h"
49 const struct got_error *
50 got_repo_read_gitconfig(int *gitconfig_repository_format_version,
51 char **gitconfig_author_name, char **gitconfig_author_email,
52 struct got_remote_repo **remotes, int *nremotes,
53 char **gitconfig_owner, char **object_format,
54 char ***extensions, int *nextensions,
55 const char *gitconfig_path)
56 {
57 const struct got_error *err = NULL, *child_err = NULL;
58 int fd = -1;
59 int imsg_fds[2] = { -1, -1 };
60 pid_t pid;
61 struct imsgbuf *ibuf;
63 *gitconfig_repository_format_version = 0;
64 if (extensions)
65 *extensions = NULL;
66 if (nextensions)
67 *nextensions = 0;
68 *gitconfig_author_name = NULL;
69 *gitconfig_author_email = NULL;
70 if (remotes)
71 *remotes = NULL;
72 if (nremotes)
73 *nremotes = 0;
74 if (gitconfig_owner)
75 *gitconfig_owner = NULL;
76 if (object_format)
77 *object_format = NULL;
79 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
80 if (fd == -1) {
81 if (errno == ENOENT)
82 return NULL;
83 return got_error_from_errno2("open", gitconfig_path);
84 }
86 ibuf = calloc(1, sizeof(*ibuf));
87 if (ibuf == NULL) {
88 err = got_error_from_errno("calloc");
89 goto done;
90 }
92 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
93 err = got_error_from_errno("socketpair");
94 goto done;
95 }
97 pid = fork();
98 if (pid == -1) {
99 err = got_error_from_errno("fork");
100 goto done;
101 } else if (pid == 0) {
102 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
103 gitconfig_path);
104 /* not reached */
107 if (close(imsg_fds[1]) == -1) {
108 err = got_error_from_errno("close");
109 goto done;
111 imsg_fds[1] = -1;
112 imsg_init(ibuf, imsg_fds[0]);
114 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
115 if (err)
116 goto done;
117 fd = -1;
119 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
120 if (err)
121 goto done;
123 err = got_privsep_recv_gitconfig_int(
124 gitconfig_repository_format_version, ibuf);
125 if (err)
126 goto done;
128 if (extensions && nextensions) {
129 err = got_privsep_send_gitconfig_repository_extensions_req(
130 ibuf);
131 if (err)
132 goto done;
133 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
134 if (err)
135 goto done;
136 if (*nextensions > 0) {
137 int i;
138 *extensions = calloc(*nextensions, sizeof(char *));
139 if (*extensions == NULL) {
140 err = got_error_from_errno("calloc");
141 goto done;
143 for (i = 0; i < *nextensions; i++) {
144 char *ext;
145 err = got_privsep_recv_gitconfig_str(&ext,
146 ibuf);
147 if (err)
148 goto done;
149 if (!strcmp(ext, "sha256"))
150 *object_format = ext;
151 (*extensions)[i] = ext;
156 err = got_privsep_send_gitconfig_author_name_req(ibuf);
157 if (err)
158 goto done;
160 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
161 if (err)
162 goto done;
164 err = got_privsep_send_gitconfig_author_email_req(ibuf);
165 if (err)
166 goto done;
168 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
169 if (err)
170 goto done;
172 if (remotes && nremotes) {
173 err = got_privsep_send_gitconfig_remotes_req(ibuf);
174 if (err)
175 goto done;
177 err = got_privsep_recv_gitconfig_remotes(remotes,
178 nremotes, ibuf);
179 if (err)
180 goto done;
183 if (gitconfig_owner) {
184 err = got_privsep_send_gitconfig_owner_req(ibuf);
185 if (err)
186 goto done;
187 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
188 if (err)
189 goto done;
192 err = got_privsep_send_stop(imsg_fds[0]);
193 child_err = got_privsep_wait_for_child(pid);
194 if (child_err && err == NULL)
195 err = child_err;
196 done:
197 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
198 err = got_error_from_errno("close");
199 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
200 err = got_error_from_errno("close");
201 if (fd != -1 && close(fd) == -1 && err == NULL)
202 err = got_error_from_errno2("close", gitconfig_path);
203 free(ibuf);
204 return err;