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/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 <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <imsg.h>
30 #include <unistd.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_repository.h"
35 #include "got_path.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_object.h"
39 #include "got_lib_object_cache.h"
40 #include "got_lib_privsep.h"
41 #include "got_lib_pack.h"
42 #include "got_lib_repository.h"
44 const struct got_error *
45 got_repo_read_gitconfig(int *gitconfig_repository_format_version,
46 char **gitconfig_author_name, char **gitconfig_author_email,
47 struct got_remote_repo **remotes, int *nremotes,
48 char **gitconfig_owner, char ***extensions, int *nextensions,
49 const char *gitconfig_path)
50 {
51 const struct got_error *err = NULL, *child_err = NULL;
52 int fd = -1;
53 int imsg_fds[2] = { -1, -1 };
54 pid_t pid;
55 struct imsgbuf *ibuf;
57 *gitconfig_repository_format_version = 0;
58 if (extensions)
59 *extensions = NULL;
60 if (nextensions)
61 *nextensions = 0;
62 *gitconfig_author_name = NULL;
63 *gitconfig_author_email = NULL;
64 if (remotes)
65 *remotes = NULL;
66 if (nremotes)
67 *nremotes = 0;
68 if (gitconfig_owner)
69 *gitconfig_owner = NULL;
71 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
72 if (fd == -1) {
73 if (errno == ENOENT)
74 return NULL;
75 return got_error_from_errno2("open", gitconfig_path);
76 }
78 ibuf = calloc(1, sizeof(*ibuf));
79 if (ibuf == NULL) {
80 err = got_error_from_errno("calloc");
81 goto done;
82 }
84 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
85 err = got_error_from_errno("socketpair");
86 goto done;
87 }
89 pid = fork();
90 if (pid == -1) {
91 err = got_error_from_errno("fork");
92 goto done;
93 } else if (pid == 0) {
94 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
95 gitconfig_path);
96 /* not reached */
97 }
99 if (close(imsg_fds[1]) == -1) {
100 err = got_error_from_errno("close");
101 goto done;
103 imsg_fds[1] = -1;
104 imsg_init(ibuf, imsg_fds[0]);
106 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
107 if (err)
108 goto done;
109 fd = -1;
111 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
112 if (err)
113 goto done;
115 err = got_privsep_recv_gitconfig_int(
116 gitconfig_repository_format_version, ibuf);
117 if (err)
118 goto done;
120 if (extensions && nextensions) {
121 err = got_privsep_send_gitconfig_repository_extensions_req(
122 ibuf);
123 if (err)
124 goto done;
125 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
126 if (err)
127 goto done;
128 if (*nextensions > 0) {
129 int i;
130 *extensions = calloc(*nextensions, sizeof(char *));
131 if (*extensions == NULL) {
132 err = got_error_from_errno("calloc");
133 goto done;
135 for (i = 0; i < *nextensions; i++) {
136 char *ext;
137 err = got_privsep_recv_gitconfig_str(&ext,
138 ibuf);
139 if (err)
140 goto done;
141 (*extensions)[i] = ext;
146 err = got_privsep_send_gitconfig_author_name_req(ibuf);
147 if (err)
148 goto done;
150 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
151 if (err)
152 goto done;
154 err = got_privsep_send_gitconfig_author_email_req(ibuf);
155 if (err)
156 goto done;
158 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
159 if (err)
160 goto done;
162 if (remotes && nremotes) {
163 err = got_privsep_send_gitconfig_remotes_req(ibuf);
164 if (err)
165 goto done;
167 err = got_privsep_recv_gitconfig_remotes(remotes,
168 nremotes, ibuf);
169 if (err)
170 goto done;
173 if (gitconfig_owner) {
174 err = got_privsep_send_gitconfig_owner_req(ibuf);
175 if (err)
176 goto done;
177 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
178 if (err)
179 goto done;
182 err = got_privsep_send_stop(imsg_fds[0]);
183 child_err = got_privsep_wait_for_child(pid);
184 if (child_err && err == NULL)
185 err = child_err;
186 done:
187 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
188 err = got_error_from_errno("close");
189 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
190 err = got_error_from_errno("close");
191 if (fd != -1 && close(fd) == -1 && err == NULL)
192 err = got_error_from_errno2("close", gitconfig_path);
193 free(ibuf);
194 return err;