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 "got_error.h"
36 #include "got_object.h"
37 #include "got_repository.h"
38 #include "got_path.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_object.h"
42 #include "got_lib_object_cache.h"
43 #include "got_lib_privsep.h"
44 #include "got_lib_pack.h"
45 #include "got_lib_repository.h"
47 const struct got_error *
48 got_repo_read_gitconfig(int *gitconfig_repository_format_version,
49 char **gitconfig_author_name, char **gitconfig_author_email,
50 struct got_remote_repo **remotes, int *nremotes,
51 char **gitconfig_owner, char **object_format,
52 char ***extensions, int *nextensions,
53 const char *gitconfig_path)
54 {
55 const struct got_error *err = NULL, *child_err = NULL;
56 int fd = -1;
57 int imsg_fds[2] = { -1, -1 };
58 pid_t pid;
59 struct imsgbuf *ibuf;
61 *gitconfig_repository_format_version = 0;
62 if (extensions)
63 *extensions = NULL;
64 if (nextensions)
65 *nextensions = 0;
66 *gitconfig_author_name = NULL;
67 *gitconfig_author_email = NULL;
68 if (remotes)
69 *remotes = NULL;
70 if (nremotes)
71 *nremotes = 0;
72 if (gitconfig_owner)
73 *gitconfig_owner = NULL;
74 if (object_format)
75 *object_format = NULL;
77 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
78 if (fd == -1) {
79 if (errno == ENOENT)
80 return NULL;
81 return got_error_from_errno2("open", gitconfig_path);
82 }
84 ibuf = calloc(1, sizeof(*ibuf));
85 if (ibuf == NULL) {
86 err = got_error_from_errno("calloc");
87 goto done;
88 }
90 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
91 err = got_error_from_errno("socketpair");
92 goto done;
93 }
95 pid = fork();
96 if (pid == -1) {
97 err = got_error_from_errno("fork");
98 goto done;
99 } else if (pid == 0) {
100 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
101 gitconfig_path);
102 /* not reached */
105 if (close(imsg_fds[1]) == -1) {
106 err = got_error_from_errno("close");
107 goto done;
109 imsg_fds[1] = -1;
110 imsg_init(ibuf, imsg_fds[0]);
112 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
113 if (err)
114 goto done;
115 fd = -1;
117 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
118 if (err)
119 goto done;
121 err = got_privsep_recv_gitconfig_int(
122 gitconfig_repository_format_version, ibuf);
123 if (err)
124 goto done;
126 if (extensions && nextensions) {
127 err = got_privsep_send_gitconfig_repository_extensions_req(
128 ibuf);
129 if (err)
130 goto done;
131 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
132 if (err)
133 goto done;
134 if (*nextensions > 0) {
135 int i;
136 *extensions = calloc(*nextensions, sizeof(char *));
137 if (*extensions == NULL) {
138 err = got_error_from_errno("calloc");
139 goto done;
141 for (i = 0; i < *nextensions; i++) {
142 char *ext;
143 err = got_privsep_recv_gitconfig_str(&ext,
144 ibuf);
145 if (err)
146 goto done;
147 (*extensions)[i] = ext;
152 err = got_privsep_send_gitconfig_author_name_req(ibuf);
153 if (err)
154 goto done;
156 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
157 if (err)
158 goto done;
160 err = got_privsep_send_gitconfig_author_email_req(ibuf);
161 if (err)
162 goto done;
164 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
165 if (err)
166 goto done;
168 if (remotes && nremotes) {
169 err = got_privsep_send_gitconfig_remotes_req(ibuf);
170 if (err)
171 goto done;
173 err = got_privsep_recv_gitconfig_remotes(remotes,
174 nremotes, ibuf);
175 if (err)
176 goto done;
179 if (gitconfig_owner) {
180 err = got_privsep_send_gitconfig_owner_req(ibuf);
181 if (err)
182 goto done;
183 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
184 if (err)
185 goto done;
188 err = got_privsep_send_stop(imsg_fds[0]);
189 child_err = got_privsep_wait_for_child(pid);
190 if (child_err && err == NULL)
191 err = child_err;
192 done:
193 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
194 err = got_error_from_errno("close");
195 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
196 err = got_error_from_errno("close");
197 if (fd != -1 && close(fd) == -1 && err == NULL)
198 err = got_error_from_errno2("close", gitconfig_path);
199 free(ibuf);
200 return err;