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