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/tree.h>
19 #include <sys/socket.h>
20 #include <sys/queue.h>
21 #include <sys/uio.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <sha1.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <imsg.h>
31 #include <unistd.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
36 #include "got_path.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_object.h"
40 #include "got_lib_object_cache.h"
41 #include "got_lib_privsep.h"
42 #include "got_lib_pack.h"
43 #include "got_lib_repository.h"
45 const struct got_error *
46 got_repo_read_gitconfig(int *gitconfig_repository_format_version,
47 char **gitconfig_author_name, char **gitconfig_author_email,
48 struct got_remote_repo **remotes, int *nremotes,
49 char **gitconfig_owner, char ***extensions, int *nextensions,
50 const char *gitconfig_path)
51 {
52 const struct got_error *err = NULL, *child_err = NULL;
53 int fd = -1;
54 int imsg_fds[2] = { -1, -1 };
55 pid_t pid;
56 struct imsgbuf *ibuf;
58 *gitconfig_repository_format_version = 0;
59 if (extensions)
60 *extensions = NULL;
61 if (nextensions)
62 *nextensions = 0;
63 *gitconfig_author_name = NULL;
64 *gitconfig_author_email = NULL;
65 if (remotes)
66 *remotes = NULL;
67 if (nremotes)
68 *nremotes = 0;
69 if (gitconfig_owner)
70 *gitconfig_owner = NULL;
72 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
73 if (fd == -1) {
74 if (errno == ENOENT)
75 return NULL;
76 return got_error_from_errno2("open", gitconfig_path);
77 }
79 ibuf = calloc(1, sizeof(*ibuf));
80 if (ibuf == NULL) {
81 err = got_error_from_errno("calloc");
82 goto done;
83 }
85 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
86 err = got_error_from_errno("socketpair");
87 goto done;
88 }
90 pid = fork();
91 if (pid == -1) {
92 err = got_error_from_errno("fork");
93 goto done;
94 } else if (pid == 0) {
95 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
96 gitconfig_path);
97 /* not reached */
98 }
100 if (close(imsg_fds[1]) == -1) {
101 err = got_error_from_errno("close");
102 goto done;
104 imsg_fds[1] = -1;
105 imsg_init(ibuf, imsg_fds[0]);
107 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
108 if (err)
109 goto done;
110 fd = -1;
112 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
113 if (err)
114 goto done;
116 err = got_privsep_recv_gitconfig_int(
117 gitconfig_repository_format_version, ibuf);
118 if (err)
119 goto done;
121 if (extensions && nextensions) {
122 err = got_privsep_send_gitconfig_repository_extensions_req(
123 ibuf);
124 if (err)
125 goto done;
126 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
127 if (err)
128 goto done;
129 if (*nextensions > 0) {
130 int i;
131 *extensions = calloc(*nextensions, sizeof(char *));
132 if (*extensions == NULL) {
133 err = got_error_from_errno("calloc");
134 goto done;
136 for (i = 0; i < *nextensions; i++) {
137 char *ext;
138 err = got_privsep_recv_gitconfig_str(&ext,
139 ibuf);
140 if (err)
141 goto done;
142 (*extensions)[i] = ext;
147 err = got_privsep_send_gitconfig_author_name_req(ibuf);
148 if (err)
149 goto done;
151 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
152 if (err)
153 goto done;
155 err = got_privsep_send_gitconfig_author_email_req(ibuf);
156 if (err)
157 goto done;
159 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
160 if (err)
161 goto done;
163 if (remotes && nremotes) {
164 err = got_privsep_send_gitconfig_remotes_req(ibuf);
165 if (err)
166 goto done;
168 err = got_privsep_recv_gitconfig_remotes(remotes,
169 nremotes, ibuf);
170 if (err)
171 goto done;
174 if (gitconfig_owner) {
175 err = got_privsep_send_gitconfig_owner_req(ibuf);
176 if (err)
177 goto done;
178 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
179 if (err)
180 goto done;
183 err = got_privsep_send_stop(imsg_fds[0]);
184 child_err = got_privsep_wait_for_child(pid);
185 if (child_err && err == NULL)
186 err = child_err;
187 done:
188 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
189 err = got_error_from_errno("close");
190 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
191 err = got_error_from_errno("close");
192 if (fd != -1 && close(fd) == -1 && err == NULL)
193 err = got_error_from_errno2("close", gitconfig_path);
194 free(ibuf);
195 return err;