Blob


1 /*
2 * Copyright (c) 2019 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/queue.h>
19 #include <sys/uio.h>
20 #include <sys/time.h>
21 #include <sys/syslimits.h>
23 #include <stdint.h>
24 #include <imsg.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sha1.h>
31 #include <zlib.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_object.h"
39 #include "got_lib_privsep.h"
40 #include "got_lib_gitconfig.h"
42 static volatile sig_atomic_t sigint_received;
44 static void
45 catch_sigint(int signo)
46 {
47 sigint_received = 1;
48 }
50 static const struct got_error *
51 gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
52 char *section, char *tag, int def)
53 {
54 int value;
56 if (gitconfig == NULL)
57 return got_error(GOT_ERR_PRIVSEP_MSG);
59 value = got_gitconfig_get_num(gitconfig, section, tag, def);
60 return got_privsep_send_gitconfig_int(ibuf, value);
61 }
63 static const struct got_error *
64 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
65 char *section, char *tag)
66 {
67 char *value;
69 if (gitconfig == NULL)
70 return got_error(GOT_ERR_PRIVSEP_MSG);
72 value = got_gitconfig_get_str(gitconfig, section, tag);
73 return got_privsep_send_gitconfig_str(ibuf, value);
74 }
76 static const struct got_error *
77 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
78 {
79 const struct got_error *err = NULL;
80 struct got_gitconfig_list *sections;
81 struct got_gitconfig_list_node *node;
82 struct got_remote_repo *remotes = NULL;
83 int nremotes = 0, i;
85 if (gitconfig == NULL)
86 return got_error(GOT_ERR_PRIVSEP_MSG);
88 err = got_gitconfig_get_section_list(&sections, gitconfig);
89 if (err)
90 return err;
92 TAILQ_FOREACH(node, &sections->fields, link) {
93 if (strncasecmp("remote \"", node->field, 8) != 0)
94 continue;
95 nremotes++;
96 }
98 if (nremotes == 0) {
99 err = got_privsep_send_gitconfig_remotes(ibuf, NULL, 0);
100 goto done;
103 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
104 if (remotes == NULL) {
105 err = got_error_from_errno("recallocarray");
106 goto done;
109 i = 0;
110 TAILQ_FOREACH(node, &sections->fields, link) {
111 char *name, *end;
113 if (strncasecmp("remote \"", node->field, 8) != 0)
114 continue;
116 name = strdup(node->field + 8);
117 if (name == NULL) {
118 err = got_error_from_errno("strdup");
119 goto done;
121 end = strrchr(name, '"');
122 if (end)
123 *end = '\0';
124 remotes[i].name = name;
126 remotes[i].url = got_gitconfig_get_str(gitconfig,
127 node->field, "url");
128 if (remotes[i].url == NULL) {
129 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
130 goto done;
133 i++;
136 err = got_privsep_send_gitconfig_remotes(ibuf, remotes, nremotes);
137 done:
138 for (i = 0; i < nremotes; i++)
139 free(remotes[i].name);
140 free(remotes);
141 got_gitconfig_free_list(sections);
142 return err;
145 int
146 main(int argc, char *argv[])
148 const struct got_error *err = NULL;
149 struct imsgbuf ibuf;
150 size_t datalen;
151 struct got_gitconfig *gitconfig = NULL;
152 #if 0
153 static int attached;
155 while (!attached)
156 sleep(1);
157 #endif
158 signal(SIGINT, catch_sigint);
160 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
162 #ifndef PROFILE
163 /* revoke access to most system calls */
164 if (pledge("stdio recvfd", NULL) == -1) {
165 err = got_error_from_errno("pledge");
166 got_privsep_send_error(&ibuf, err);
167 return 1;
169 #endif
171 for (;;) {
172 struct imsg imsg;
174 memset(&imsg, 0, sizeof(imsg));
175 imsg.fd = -1;
177 if (sigint_received) {
178 err = got_error(GOT_ERR_CANCELLED);
179 break;
182 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
183 if (err) {
184 if (err->code == GOT_ERR_PRIVSEP_PIPE)
185 err = NULL;
186 break;
189 if (imsg.hdr.type == GOT_IMSG_STOP)
190 break;
192 switch (imsg.hdr.type) {
193 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
194 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
195 if (datalen != 0) {
196 err = got_error(GOT_ERR_PRIVSEP_LEN);
197 break;
199 if (imsg.fd == -1){
200 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
201 break;
204 if (gitconfig)
205 got_gitconfig_close(gitconfig);
206 err = got_gitconfig_open(&gitconfig, imsg.fd);
207 break;
208 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
209 err = gitconfig_num_request(&ibuf, gitconfig, "core",
210 "repositoryformatversion", 0);
211 break;
212 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
213 err = gitconfig_str_request(&ibuf, gitconfig, "user",
214 "name");
215 break;
216 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
217 err = gitconfig_str_request(&ibuf, gitconfig, "user",
218 "email");
219 break;
220 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
221 err = gitconfig_remotes_request(&ibuf, gitconfig);
222 break;
223 default:
224 err = got_error(GOT_ERR_PRIVSEP_MSG);
225 break;
228 if (imsg.fd != -1) {
229 if (close(imsg.fd) == -1 && err == NULL)
230 err = got_error_from_errno("close");
233 imsg_free(&imsg);
234 if (err)
235 break;
238 imsg_clear(&ibuf);
239 if (err) {
240 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
241 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
242 got_privsep_send_error(&ibuf, err);
245 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
246 err = got_error_from_errno("close");
247 return err ? 1 : 0;