portable: remove sockaddr-linux code
[got-portable.git] / lib / read_gitconfig_privsep.c
blobe216540720a3f3a01b5d352278ae176d5785ea0f
1 /*
2 * Copyright (c) 2019, 2022 Stefan Sperling <stsp@openbsd.org>
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.
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.
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_compat.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 ***extnames, char ***extvals,
50 int *nextensions, const char *gitconfig_path)
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 (extnames)
60 *extnames = NULL;
61 if (extvals)
62 *extvals = NULL;
63 if (nextensions)
64 *nextensions = 0;
65 *gitconfig_author_name = NULL;
66 *gitconfig_author_email = NULL;
67 if (remotes)
68 *remotes = NULL;
69 if (nremotes)
70 *nremotes = 0;
71 if (gitconfig_owner)
72 *gitconfig_owner = NULL;
74 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
75 if (fd == -1) {
76 if (errno == ENOENT)
77 return NULL;
78 return got_error_from_errno2("open", gitconfig_path);
81 ibuf = calloc(1, sizeof(*ibuf));
82 if (ibuf == NULL) {
83 err = got_error_from_errno("calloc");
84 goto done;
87 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
88 err = got_error_from_errno("socketpair");
89 goto done;
92 pid = fork();
93 if (pid == -1) {
94 err = got_error_from_errno("fork");
95 goto done;
96 } else if (pid == 0) {
97 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
98 gitconfig_path);
99 /* not reached */
102 if (close(imsg_fds[1]) == -1) {
103 err = got_error_from_errno("close");
104 goto done;
106 imsg_fds[1] = -1;
107 imsg_init(ibuf, imsg_fds[0]);
109 err = got_privsep_send_gitconfig_parse_req(ibuf, fd);
110 if (err)
111 goto done;
112 fd = -1;
114 err = got_privsep_send_gitconfig_repository_format_version_req(ibuf);
115 if (err)
116 goto done;
118 err = got_privsep_recv_gitconfig_int(
119 gitconfig_repository_format_version, ibuf);
120 if (err)
121 goto done;
123 if (extnames && extvals && nextensions) {
124 err = got_privsep_send_gitconfig_repository_extensions_req(
125 ibuf);
126 if (err)
127 goto done;
128 err = got_privsep_recv_gitconfig_int(nextensions, ibuf);
129 if (err)
130 goto done;
131 if (*nextensions > 0) {
132 int i;
133 *extnames = calloc(*nextensions, sizeof(char *));
134 if (*extnames == NULL) {
135 err = got_error_from_errno("calloc");
136 goto done;
138 *extvals = calloc(*nextensions, sizeof(char *));
139 if (*extvals == NULL) {
140 err = got_error_from_errno("calloc");
141 goto done;
143 for (i = 0; i < *nextensions; i++) {
144 char *ext, *val;
145 err = got_privsep_recv_gitconfig_pair(&ext,
146 &val, ibuf);
147 if (err)
148 goto done;
149 (*extnames)[i] = ext;
150 (*extvals)[i] = val;
155 err = got_privsep_send_gitconfig_author_name_req(ibuf);
156 if (err)
157 goto done;
159 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf);
160 if (err)
161 goto done;
163 err = got_privsep_send_gitconfig_author_email_req(ibuf);
164 if (err)
165 goto done;
167 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf);
168 if (err)
169 goto done;
171 if (remotes && nremotes) {
172 err = got_privsep_send_gitconfig_remotes_req(ibuf);
173 if (err)
174 goto done;
176 err = got_privsep_recv_gitconfig_remotes(remotes,
177 nremotes, ibuf);
178 if (err)
179 goto done;
182 if (gitconfig_owner) {
183 err = got_privsep_send_gitconfig_owner_req(ibuf);
184 if (err)
185 goto done;
186 err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf);
187 if (err)
188 goto done;
191 err = got_privsep_send_stop(imsg_fds[0]);
192 child_err = got_privsep_wait_for_child(pid);
193 if (child_err && err == NULL)
194 err = child_err;
195 done:
196 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
197 err = got_error_from_errno("close");
198 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
199 err = got_error_from_errno("close");
200 if (fd != -1 && close(fd) == -1 && err == NULL)
201 err = got_error_from_errno2("close", gitconfig_path);
202 free(ibuf);
203 return err;