Blame


1 aba9c984 2019-09-08 stsp /*
2 aba9c984 2019-09-08 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
3 aba9c984 2019-09-08 stsp *
4 aba9c984 2019-09-08 stsp * Permission to use, copy, modify, and distribute this software for any
5 aba9c984 2019-09-08 stsp * purpose with or without fee is hereby granted, provided that the above
6 aba9c984 2019-09-08 stsp * copyright notice and this permission notice appear in all copies.
7 aba9c984 2019-09-08 stsp *
8 aba9c984 2019-09-08 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 aba9c984 2019-09-08 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 aba9c984 2019-09-08 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 aba9c984 2019-09-08 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 aba9c984 2019-09-08 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 aba9c984 2019-09-08 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 aba9c984 2019-09-08 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 aba9c984 2019-09-08 stsp */
16 aba9c984 2019-09-08 stsp
17 aba9c984 2019-09-08 stsp #include <sys/types.h>
18 aba9c984 2019-09-08 stsp #include <sys/queue.h>
19 aba9c984 2019-09-08 stsp #include <sys/uio.h>
20 aba9c984 2019-09-08 stsp #include <sys/time.h>
21 aba9c984 2019-09-08 stsp #include <sys/syslimits.h>
22 aba9c984 2019-09-08 stsp
23 aba9c984 2019-09-08 stsp #include <stdint.h>
24 aba9c984 2019-09-08 stsp #include <imsg.h>
25 aba9c984 2019-09-08 stsp #include <limits.h>
26 aba9c984 2019-09-08 stsp #include <signal.h>
27 aba9c984 2019-09-08 stsp #include <stdio.h>
28 aba9c984 2019-09-08 stsp #include <stdlib.h>
29 aba9c984 2019-09-08 stsp #include <string.h>
30 aba9c984 2019-09-08 stsp #include <sha1.h>
31 aba9c984 2019-09-08 stsp #include <zlib.h>
32 aba9c984 2019-09-08 stsp
33 aba9c984 2019-09-08 stsp #include "got_error.h"
34 aba9c984 2019-09-08 stsp #include "got_object.h"
35 aba9c984 2019-09-08 stsp
36 aba9c984 2019-09-08 stsp #include "got_lib_delta.h"
37 aba9c984 2019-09-08 stsp #include "got_lib_object.h"
38 aba9c984 2019-09-08 stsp #include "got_lib_privsep.h"
39 aba9c984 2019-09-08 stsp #include "got_lib_gitconfig.h"
40 aba9c984 2019-09-08 stsp
41 aba9c984 2019-09-08 stsp static volatile sig_atomic_t sigint_received;
42 aba9c984 2019-09-08 stsp
43 aba9c984 2019-09-08 stsp static void
44 aba9c984 2019-09-08 stsp catch_sigint(int signo)
45 aba9c984 2019-09-08 stsp {
46 aba9c984 2019-09-08 stsp sigint_received = 1;
47 aba9c984 2019-09-08 stsp }
48 aba9c984 2019-09-08 stsp
49 aba9c984 2019-09-08 stsp static const struct got_error *
50 aba9c984 2019-09-08 stsp gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
51 aba9c984 2019-09-08 stsp char *section, char *tag, int def)
52 aba9c984 2019-09-08 stsp {
53 aba9c984 2019-09-08 stsp int value;
54 aba9c984 2019-09-08 stsp
55 aba9c984 2019-09-08 stsp if (gitconfig == NULL)
56 aba9c984 2019-09-08 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
57 aba9c984 2019-09-08 stsp
58 aba9c984 2019-09-08 stsp value = got_gitconfig_get_num(gitconfig, section, tag, def);
59 aba9c984 2019-09-08 stsp return got_privsep_send_gitconfig_int(ibuf, value);
60 aba9c984 2019-09-08 stsp }
61 aba9c984 2019-09-08 stsp
62 aba9c984 2019-09-08 stsp static const struct got_error *
63 aba9c984 2019-09-08 stsp gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
64 aba9c984 2019-09-08 stsp char *section, char *tag)
65 aba9c984 2019-09-08 stsp {
66 aba9c984 2019-09-08 stsp char *value;
67 aba9c984 2019-09-08 stsp
68 aba9c984 2019-09-08 stsp if (gitconfig == NULL)
69 aba9c984 2019-09-08 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
70 aba9c984 2019-09-08 stsp
71 aba9c984 2019-09-08 stsp value = got_gitconfig_get_str(gitconfig, section, tag);
72 aba9c984 2019-09-08 stsp return got_privsep_send_gitconfig_str(ibuf, value);
73 aba9c984 2019-09-08 stsp }
74 aba9c984 2019-09-08 stsp
75 aba9c984 2019-09-08 stsp int
76 aba9c984 2019-09-08 stsp main(int argc, char *argv[])
77 aba9c984 2019-09-08 stsp {
78 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
79 aba9c984 2019-09-08 stsp struct imsgbuf ibuf;
80 aba9c984 2019-09-08 stsp size_t datalen;
81 aba9c984 2019-09-08 stsp struct got_gitconfig *gitconfig = NULL;
82 aba9c984 2019-09-08 stsp #if 0
83 aba9c984 2019-09-08 stsp static int attached;
84 aba9c984 2019-09-08 stsp
85 aba9c984 2019-09-08 stsp while (!attached)
86 aba9c984 2019-09-08 stsp sleep(1);
87 aba9c984 2019-09-08 stsp #endif
88 aba9c984 2019-09-08 stsp signal(SIGINT, catch_sigint);
89 aba9c984 2019-09-08 stsp
90 aba9c984 2019-09-08 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
91 aba9c984 2019-09-08 stsp
92 aba9c984 2019-09-08 stsp #ifndef PROFILE
93 aba9c984 2019-09-08 stsp /* revoke access to most system calls */
94 aba9c984 2019-09-08 stsp if (pledge("stdio recvfd", NULL) == -1) {
95 aba9c984 2019-09-08 stsp err = got_error_from_errno("pledge");
96 aba9c984 2019-09-08 stsp got_privsep_send_error(&ibuf, err);
97 aba9c984 2019-09-08 stsp return 1;
98 aba9c984 2019-09-08 stsp }
99 aba9c984 2019-09-08 stsp #endif
100 aba9c984 2019-09-08 stsp
101 aba9c984 2019-09-08 stsp for (;;) {
102 aba9c984 2019-09-08 stsp struct imsg imsg;
103 aba9c984 2019-09-08 stsp
104 aba9c984 2019-09-08 stsp memset(&imsg, 0, sizeof(imsg));
105 aba9c984 2019-09-08 stsp imsg.fd = -1;
106 aba9c984 2019-09-08 stsp
107 aba9c984 2019-09-08 stsp if (sigint_received) {
108 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_CANCELLED);
109 aba9c984 2019-09-08 stsp break;
110 aba9c984 2019-09-08 stsp }
111 aba9c984 2019-09-08 stsp
112 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
113 aba9c984 2019-09-08 stsp if (err) {
114 aba9c984 2019-09-08 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
115 aba9c984 2019-09-08 stsp err = NULL;
116 aba9c984 2019-09-08 stsp break;
117 aba9c984 2019-09-08 stsp }
118 aba9c984 2019-09-08 stsp
119 aba9c984 2019-09-08 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
120 aba9c984 2019-09-08 stsp break;
121 aba9c984 2019-09-08 stsp
122 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
123 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
124 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
125 aba9c984 2019-09-08 stsp if (datalen != 0) {
126 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
127 aba9c984 2019-09-08 stsp break;
128 aba9c984 2019-09-08 stsp }
129 aba9c984 2019-09-08 stsp if (imsg.fd == -1){
130 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
131 aba9c984 2019-09-08 stsp break;
132 aba9c984 2019-09-08 stsp }
133 aba9c984 2019-09-08 stsp
134 aba9c984 2019-09-08 stsp if (gitconfig)
135 aba9c984 2019-09-08 stsp got_gitconfig_close(gitconfig);
136 aba9c984 2019-09-08 stsp err = got_gitconfig_open(&gitconfig, imsg.fd);
137 aba9c984 2019-09-08 stsp break;
138 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
139 aba9c984 2019-09-08 stsp err = gitconfig_num_request(&ibuf, gitconfig, "core",
140 aba9c984 2019-09-08 stsp "repositoryformatversion", 0);
141 aba9c984 2019-09-08 stsp break;
142 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
143 aba9c984 2019-09-08 stsp err = gitconfig_str_request(&ibuf, gitconfig, "user",
144 aba9c984 2019-09-08 stsp "name");
145 aba9c984 2019-09-08 stsp break;
146 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
147 aba9c984 2019-09-08 stsp err = gitconfig_str_request(&ibuf, gitconfig, "user",
148 aba9c984 2019-09-08 stsp "email");
149 aba9c984 2019-09-08 stsp break;
150 aba9c984 2019-09-08 stsp default:
151 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
152 aba9c984 2019-09-08 stsp break;
153 aba9c984 2019-09-08 stsp }
154 aba9c984 2019-09-08 stsp
155 aba9c984 2019-09-08 stsp if (imsg.fd != -1) {
156 aba9c984 2019-09-08 stsp if (close(imsg.fd) == -1 && err == NULL)
157 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
158 aba9c984 2019-09-08 stsp }
159 aba9c984 2019-09-08 stsp
160 aba9c984 2019-09-08 stsp imsg_free(&imsg);
161 aba9c984 2019-09-08 stsp if (err)
162 aba9c984 2019-09-08 stsp break;
163 aba9c984 2019-09-08 stsp }
164 aba9c984 2019-09-08 stsp
165 aba9c984 2019-09-08 stsp imsg_clear(&ibuf);
166 aba9c984 2019-09-08 stsp if (err) {
167 aba9c984 2019-09-08 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
168 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
169 aba9c984 2019-09-08 stsp got_privsep_send_error(&ibuf, err);
170 aba9c984 2019-09-08 stsp }
171 aba9c984 2019-09-08 stsp }
172 aba9c984 2019-09-08 stsp if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
173 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
174 aba9c984 2019-09-08 stsp return err ? 1 : 0;
175 aba9c984 2019-09-08 stsp }