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 ***extnames, char ***extvals,
51 int *nextensions, 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 (extnames)
61 *extnames = NULL;
62 if (extvals)
63 *extvals = 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;
75 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
76 if (fd == -1) {
77 if (errno == ENOENT)
78 return NULL;
79 return got_error_from_errno2("open", gitconfig_path);
80 }
82 ibuf = calloc(1, sizeof(*ibuf));
83 if (ibuf == NULL) {
84 err = got_error_from_errno("calloc");
85 goto done;
86 }
88 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
89 err = got_error_from_errno("socketpair");
90 goto done;
91 }
93 pid = fork();
94 if (pid == -1) {
95 err = got_error_from_errno("fork");
96 goto done;
97 } else if (pid == 0) {
98 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
99 gitconfig_path);
100 /* not reached */
103 if (close(imsg_fds[1]) == -1) {
104 err = got_error_from_errno("close");
105 goto done;
107 imsg_fds[1] = -1;
108 imsg_init(ibuf, imsg_fds[0]);
110 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
111 if (err)
112 goto done;
113 fd = -1;
115 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
116 if (err)
117 goto done;
119 err = got_privsep_recv_gitconfig_int(
120 gitconfig_repository_format_version, ibuf);
121 if (err)
122 goto done;
124 if (extnames && extvals && nextensions) {
125 err = got_privsep_send_gitconfig_repository_extensions_req(
126 ibuf);
127 if (err)
128 goto done;
129 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
130 if (err)
131 goto done;
132 if (*nextensions > 0) {
133 int i;
134 *extnames = calloc(*nextensions, sizeof(char *));
135 if (*extnames == NULL) {
136 err = got_error_from_errno("calloc");
137 goto done;
139 *extvals = calloc(*nextensions, sizeof(char *));
140 if (*extvals == NULL) {
141 err = got_error_from_errno("calloc");
142 goto done;
144 for (i = 0; i < *nextensions; i++) {
145 char *ext, *val;
146 err = got_privsep_recv_gitconfig_pair(&ext,
147 &val, ibuf);
148 if (err)
149 goto done;
150 (*extnames)[i] = ext;
151 (*extvals)[i] = val;
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;