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