Blame


1 d65a88a2 2021-09-05 stsp /*
2 d65a88a2 2021-09-05 stsp * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 d65a88a2 2021-09-05 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 d65a88a2 2021-09-05 stsp *
5 d65a88a2 2021-09-05 stsp * Permission to use, copy, modify, and distribute this software for any
6 d65a88a2 2021-09-05 stsp * purpose with or without fee is hereby granted, provided that the above
7 d65a88a2 2021-09-05 stsp * copyright notice and this permission notice appear in all copies.
8 d65a88a2 2021-09-05 stsp *
9 d65a88a2 2021-09-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 d65a88a2 2021-09-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 d65a88a2 2021-09-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 d65a88a2 2021-09-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 d65a88a2 2021-09-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 d65a88a2 2021-09-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 d65a88a2 2021-09-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 d65a88a2 2021-09-05 stsp */
17 4fccd2fe 2023-03-08 thomas
18 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
19 d65a88a2 2021-09-05 stsp
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 d65a88a2 2021-09-05 stsp #include <sys/types.h>
22 d65a88a2 2021-09-05 stsp #include <sys/socket.h>
23 37e7d69e 2024-04-25 thomas.ad #include <sys/uio.h>
24 d65a88a2 2021-09-05 stsp #include <netdb.h>
25 d65a88a2 2021-09-05 stsp
26 c10270f6 2021-09-06 naddy #include <assert.h>
27 d65a88a2 2021-09-05 stsp #include <err.h>
28 a928faa1 2023-03-10 thomas #include <limits.h>
29 37e7d69e 2024-04-25 thomas.ad #include <sha1.h>
30 37e7d69e 2024-04-25 thomas.ad #include <stdint.h>
31 37e7d69e 2024-04-25 thomas.ad #include <limits.h>
32 d65a88a2 2021-09-05 stsp #include <stdio.h>
33 d65a88a2 2021-09-05 stsp #include <stdlib.h>
34 d65a88a2 2021-09-05 stsp #include <string.h>
35 d65a88a2 2021-09-05 stsp #include <unistd.h>
36 37e7d69e 2024-04-25 thomas.ad #include <imsg.h>
37 d65a88a2 2021-09-05 stsp
38 d65a88a2 2021-09-05 stsp #include "got_error.h"
39 5e5da8c4 2021-09-05 stsp #include "got_path.h"
40 37e7d69e 2024-04-25 thomas.ad #include "got_object.h"
41 d65a88a2 2021-09-05 stsp
42 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
43 dd038bc6 2021-09-21 thomas.ad
44 d65a88a2 2021-09-05 stsp #include "got_lib_dial.h"
45 37e7d69e 2024-04-25 thomas.ad #include "got_lib_delta.h"
46 37e7d69e 2024-04-25 thomas.ad #include "got_lib_object.h"
47 37e7d69e 2024-04-25 thomas.ad #include "got_lib_privsep.h"
48 ef20f542 2022-06-26 thomas #include "got_dial.h"
49 c10270f6 2021-09-06 naddy
50 c10270f6 2021-09-06 naddy #ifndef nitems
51 c10270f6 2021-09-06 naddy #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 c10270f6 2021-09-06 naddy #endif
53 d65a88a2 2021-09-05 stsp
54 d65a88a2 2021-09-05 stsp #ifndef ssizeof
55 d65a88a2 2021-09-05 stsp #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
56 d65a88a2 2021-09-05 stsp #endif
57 d65a88a2 2021-09-05 stsp
58 d65a88a2 2021-09-05 stsp #ifndef MIN
59 d65a88a2 2021-09-05 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 d65a88a2 2021-09-05 stsp #endif
61 d65a88a2 2021-09-05 stsp
62 d65a88a2 2021-09-05 stsp #ifndef GOT_DIAL_PATH_SSH
63 d65a88a2 2021-09-05 stsp #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
64 d65a88a2 2021-09-05 stsp #endif
65 d65a88a2 2021-09-05 stsp
66 d65a88a2 2021-09-05 stsp /* IANA assigned */
67 d65a88a2 2021-09-05 stsp #define GOT_DEFAULT_GIT_PORT 9418
68 d65a88a2 2021-09-05 stsp #define GOT_DEFAULT_GIT_PORT_STR "9418"
69 d65a88a2 2021-09-05 stsp
70 d65a88a2 2021-09-05 stsp const struct got_error *
71 d65a88a2 2021-09-05 stsp got_dial_apply_unveil(const char *proto)
72 d65a88a2 2021-09-05 stsp {
73 d65a88a2 2021-09-05 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
74 d65a88a2 2021-09-05 stsp if (unveil(GOT_DIAL_PATH_SSH, "x") != 0) {
75 d65a88a2 2021-09-05 stsp return got_error_from_errno2("unveil",
76 d65a88a2 2021-09-05 stsp GOT_DIAL_PATH_SSH);
77 d65a88a2 2021-09-05 stsp }
78 d65a88a2 2021-09-05 stsp }
79 d65a88a2 2021-09-05 stsp
80 37e7d69e 2024-04-25 thomas.ad if (strstr(proto, "http") != NULL) {
81 09876a9d 2024-04-25 thomas.ad if (unveil(GOT_PATH_PROG_FETCH_HTTP, "x") != 0) {
82 37e7d69e 2024-04-25 thomas.ad return got_error_from_errno2("unveil",
83 09876a9d 2024-04-25 thomas.ad GOT_PATH_PROG_FETCH_HTTP);
84 37e7d69e 2024-04-25 thomas.ad }
85 37e7d69e 2024-04-25 thomas.ad }
86 37e7d69e 2024-04-25 thomas.ad
87 d65a88a2 2021-09-05 stsp return NULL;
88 d65a88a2 2021-09-05 stsp }
89 d65a88a2 2021-09-05 stsp
90 5e5da8c4 2021-09-05 stsp static int
91 0c6f49ba 2022-07-01 thomas hassuffix(const char *base, const char *suf)
92 5e5da8c4 2021-09-05 stsp {
93 5e5da8c4 2021-09-05 stsp int nb, ns;
94 5e5da8c4 2021-09-05 stsp
95 5e5da8c4 2021-09-05 stsp nb = strlen(base);
96 5e5da8c4 2021-09-05 stsp ns = strlen(suf);
97 5e5da8c4 2021-09-05 stsp if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
98 5e5da8c4 2021-09-05 stsp return 1;
99 5e5da8c4 2021-09-05 stsp return 0;
100 5e5da8c4 2021-09-05 stsp }
101 5e5da8c4 2021-09-05 stsp
102 d65a88a2 2021-09-05 stsp const struct got_error *
103 5e5da8c4 2021-09-05 stsp got_dial_parse_uri(char **proto, char **host, char **port,
104 5e5da8c4 2021-09-05 stsp char **server_path, char **repo_name, const char *uri)
105 5e5da8c4 2021-09-05 stsp {
106 5e5da8c4 2021-09-05 stsp const struct got_error *err = NULL;
107 5e5da8c4 2021-09-05 stsp char *s, *p, *q;
108 5e5da8c4 2021-09-05 stsp
109 5e5da8c4 2021-09-05 stsp *proto = *host = *port = *server_path = *repo_name = NULL;
110 5e5da8c4 2021-09-05 stsp
111 5e5da8c4 2021-09-05 stsp p = strstr(uri, "://");
112 5e5da8c4 2021-09-05 stsp if (!p) {
113 5e5da8c4 2021-09-05 stsp /* Try parsing Git's "scp" style URL syntax. */
114 5e5da8c4 2021-09-05 stsp *proto = strdup("ssh");
115 2996af60 2022-03-07 thomas if (*proto == NULL) {
116 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strdup");
117 5e5da8c4 2021-09-05 stsp goto done;
118 5e5da8c4 2021-09-05 stsp }
119 5e5da8c4 2021-09-05 stsp s = (char *)uri;
120 5e5da8c4 2021-09-05 stsp q = strchr(s, ':');
121 5e5da8c4 2021-09-05 stsp if (q == NULL) {
122 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
123 5e5da8c4 2021-09-05 stsp goto done;
124 5e5da8c4 2021-09-05 stsp }
125 5e5da8c4 2021-09-05 stsp /* No slashes allowed before first colon. */
126 5e5da8c4 2021-09-05 stsp p = strchr(s, '/');
127 5e5da8c4 2021-09-05 stsp if (p && q > p) {
128 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
129 5e5da8c4 2021-09-05 stsp goto done;
130 5e5da8c4 2021-09-05 stsp }
131 5e5da8c4 2021-09-05 stsp *host = strndup(s, q - s);
132 5e5da8c4 2021-09-05 stsp if (*host == NULL) {
133 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
134 5e5da8c4 2021-09-05 stsp goto done;
135 5e5da8c4 2021-09-05 stsp }
136 26ec43f5 2022-03-07 thomas if ((*host)[0] == '\0') {
137 26ec43f5 2022-03-07 thomas err = got_error(GOT_ERR_PARSE_URI);
138 26ec43f5 2022-03-07 thomas goto done;
139 26ec43f5 2022-03-07 thomas }
140 5e5da8c4 2021-09-05 stsp p = q + 1;
141 5e5da8c4 2021-09-05 stsp } else {
142 5e5da8c4 2021-09-05 stsp *proto = strndup(uri, p - uri);
143 2996af60 2022-03-07 thomas if (*proto == NULL) {
144 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
145 5e5da8c4 2021-09-05 stsp goto done;
146 5e5da8c4 2021-09-05 stsp }
147 5e5da8c4 2021-09-05 stsp s = p + 3;
148 5e5da8c4 2021-09-05 stsp
149 5e5da8c4 2021-09-05 stsp p = strstr(s, "/");
150 5e5da8c4 2021-09-05 stsp if (p == NULL || strlen(p) == 1) {
151 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
152 5e5da8c4 2021-09-05 stsp goto done;
153 5e5da8c4 2021-09-05 stsp }
154 5e5da8c4 2021-09-05 stsp
155 5e5da8c4 2021-09-05 stsp q = memchr(s, ':', p - s);
156 5e5da8c4 2021-09-05 stsp if (q) {
157 5e5da8c4 2021-09-05 stsp *host = strndup(s, q - s);
158 5e5da8c4 2021-09-05 stsp if (*host == NULL) {
159 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
160 5e5da8c4 2021-09-05 stsp goto done;
161 5e5da8c4 2021-09-05 stsp }
162 26ec43f5 2022-03-07 thomas if ((*host)[0] == '\0') {
163 26ec43f5 2022-03-07 thomas err = got_error(GOT_ERR_PARSE_URI);
164 26ec43f5 2022-03-07 thomas goto done;
165 26ec43f5 2022-03-07 thomas }
166 5e5da8c4 2021-09-05 stsp *port = strndup(q + 1, p - (q + 1));
167 5e5da8c4 2021-09-05 stsp if (*port == NULL) {
168 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
169 5e5da8c4 2021-09-05 stsp goto done;
170 5e5da8c4 2021-09-05 stsp }
171 26ec43f5 2022-03-07 thomas if ((*port)[0] == '\0') {
172 26ec43f5 2022-03-07 thomas err = got_error(GOT_ERR_PARSE_URI);
173 26ec43f5 2022-03-07 thomas goto done;
174 26ec43f5 2022-03-07 thomas }
175 5e5da8c4 2021-09-05 stsp } else {
176 5e5da8c4 2021-09-05 stsp *host = strndup(s, p - s);
177 5e5da8c4 2021-09-05 stsp if (*host == NULL) {
178 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
179 5e5da8c4 2021-09-05 stsp goto done;
180 5e5da8c4 2021-09-05 stsp }
181 26ec43f5 2022-03-07 thomas if ((*host)[0] == '\0') {
182 26ec43f5 2022-03-07 thomas err = got_error(GOT_ERR_PARSE_URI);
183 26ec43f5 2022-03-07 thomas goto done;
184 26ec43f5 2022-03-07 thomas }
185 5e5da8c4 2021-09-05 stsp }
186 5e5da8c4 2021-09-05 stsp }
187 5e5da8c4 2021-09-05 stsp
188 5e5da8c4 2021-09-05 stsp while (p[0] == '/' && p[1] == '/')
189 5e5da8c4 2021-09-05 stsp p++;
190 5e5da8c4 2021-09-05 stsp *server_path = strdup(p);
191 5e5da8c4 2021-09-05 stsp if (*server_path == NULL) {
192 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strdup");
193 5e5da8c4 2021-09-05 stsp goto done;
194 5e5da8c4 2021-09-05 stsp }
195 5e5da8c4 2021-09-05 stsp got_path_strip_trailing_slashes(*server_path);
196 26ec43f5 2022-03-07 thomas if ((*server_path)[0] == '\0') {
197 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
198 5e5da8c4 2021-09-05 stsp goto done;
199 5e5da8c4 2021-09-05 stsp }
200 26ec43f5 2022-03-07 thomas
201 26ec43f5 2022-03-07 thomas err = got_path_basename(repo_name, *server_path);
202 26ec43f5 2022-03-07 thomas if (err)
203 5e5da8c4 2021-09-05 stsp goto done;
204 26ec43f5 2022-03-07 thomas if (hassuffix(*repo_name, ".git"))
205 26ec43f5 2022-03-07 thomas (*repo_name)[strlen(*repo_name) - 4] = '\0';
206 26ec43f5 2022-03-07 thomas if ((*repo_name)[0] == '\0')
207 26ec43f5 2022-03-07 thomas err = got_error(GOT_ERR_PARSE_URI);
208 5e5da8c4 2021-09-05 stsp done:
209 5e5da8c4 2021-09-05 stsp if (err) {
210 5e5da8c4 2021-09-05 stsp free(*proto);
211 5e5da8c4 2021-09-05 stsp *proto = NULL;
212 5e5da8c4 2021-09-05 stsp free(*host);
213 5e5da8c4 2021-09-05 stsp *host = NULL;
214 5e5da8c4 2021-09-05 stsp free(*port);
215 5e5da8c4 2021-09-05 stsp *port = NULL;
216 5e5da8c4 2021-09-05 stsp free(*server_path);
217 5e5da8c4 2021-09-05 stsp *server_path = NULL;
218 5e5da8c4 2021-09-05 stsp free(*repo_name);
219 5e5da8c4 2021-09-05 stsp *repo_name = NULL;
220 5e5da8c4 2021-09-05 stsp }
221 5e5da8c4 2021-09-05 stsp return err;
222 5e5da8c4 2021-09-05 stsp }
223 5e5da8c4 2021-09-05 stsp
224 a928faa1 2023-03-10 thomas /*
225 a928faa1 2023-03-10 thomas * Escape a given path for the shell which will be started by sshd.
226 a928faa1 2023-03-10 thomas * In particular, git-shell is known to require single-quote characters
227 a928faa1 2023-03-10 thomas * around its repository path argument and will refuse to run otherwise.
228 a928faa1 2023-03-10 thomas */
229 a928faa1 2023-03-10 thomas static const struct got_error *
230 a928faa1 2023-03-10 thomas escape_path(char *buf, size_t bufsize, const char *path)
231 a928faa1 2023-03-10 thomas {
232 a928faa1 2023-03-10 thomas const char *p;
233 a928faa1 2023-03-10 thomas char *q;
234 a928faa1 2023-03-10 thomas
235 a928faa1 2023-03-10 thomas p = path;
236 a928faa1 2023-03-10 thomas q = buf;
237 a928faa1 2023-03-10 thomas
238 a928faa1 2023-03-10 thomas if (bufsize > 1)
239 a928faa1 2023-03-10 thomas *q++ = '\'';
240 a928faa1 2023-03-10 thomas
241 a928faa1 2023-03-10 thomas while (*p != '\0' && (q - buf < bufsize)) {
242 a928faa1 2023-03-10 thomas /* git escapes ! too */
243 a928faa1 2023-03-10 thomas if (*p != '\'' && *p != '!') {
244 a928faa1 2023-03-10 thomas *q++ = *p++;
245 a928faa1 2023-03-10 thomas continue;
246 a928faa1 2023-03-10 thomas }
247 a928faa1 2023-03-10 thomas
248 a928faa1 2023-03-10 thomas if (q - buf + 4 >= bufsize)
249 a928faa1 2023-03-10 thomas break;
250 a928faa1 2023-03-10 thomas *q++ = '\'';
251 a928faa1 2023-03-10 thomas *q++ = '\\';
252 a928faa1 2023-03-10 thomas *q++ = *p++;
253 a928faa1 2023-03-10 thomas *q++ = '\'';
254 a928faa1 2023-03-10 thomas }
255 a928faa1 2023-03-10 thomas
256 a928faa1 2023-03-10 thomas if (*p == '\0' && (q - buf + 1 < bufsize)) {
257 a928faa1 2023-03-10 thomas *q++ = '\'';
258 a928faa1 2023-03-10 thomas *q = '\0';
259 a928faa1 2023-03-10 thomas return NULL;
260 a928faa1 2023-03-10 thomas }
261 a928faa1 2023-03-10 thomas
262 a928faa1 2023-03-10 thomas return got_error_fmt(GOT_ERR_NO_SPACE, "overlong path: %s", path);
263 a928faa1 2023-03-10 thomas }
264 a928faa1 2023-03-10 thomas
265 5e5da8c4 2021-09-05 stsp const struct got_error *
266 d65a88a2 2021-09-05 stsp got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
267 5769f9a0 2023-04-22 thomas const char *port, const char *path, const char *command, int verbosity)
268 d65a88a2 2021-09-05 stsp {
269 d65a88a2 2021-09-05 stsp const struct got_error *error = NULL;
270 d65a88a2 2021-09-05 stsp int pid, pfd[2];
271 d65a88a2 2021-09-05 stsp char cmd[64];
272 a928faa1 2023-03-10 thomas char escaped_path[PATH_MAX];
273 0c6f49ba 2022-07-01 thomas const char *argv[11];
274 d65a88a2 2021-09-05 stsp int i = 0, j;
275 d65a88a2 2021-09-05 stsp
276 d65a88a2 2021-09-05 stsp *newpid = -1;
277 d65a88a2 2021-09-05 stsp *newfd = -1;
278 d65a88a2 2021-09-05 stsp
279 a928faa1 2023-03-10 thomas error = escape_path(escaped_path, sizeof(escaped_path), path);
280 a928faa1 2023-03-10 thomas if (error)
281 a928faa1 2023-03-10 thomas return error;
282 a928faa1 2023-03-10 thomas
283 d65a88a2 2021-09-05 stsp argv[i++] = GOT_DIAL_PATH_SSH;
284 d65a88a2 2021-09-05 stsp if (port != NULL) {
285 d65a88a2 2021-09-05 stsp argv[i++] = "-p";
286 d65a88a2 2021-09-05 stsp argv[i++] = (char *)port;
287 d65a88a2 2021-09-05 stsp }
288 d65a88a2 2021-09-05 stsp if (verbosity == -1) {
289 d65a88a2 2021-09-05 stsp argv[i++] = "-q";
290 d65a88a2 2021-09-05 stsp } else {
291 d65a88a2 2021-09-05 stsp /* ssh(1) allows up to 3 "-v" options. */
292 d65a88a2 2021-09-05 stsp for (j = 0; j < MIN(3, verbosity); j++)
293 d65a88a2 2021-09-05 stsp argv[i++] = "-v";
294 d65a88a2 2021-09-05 stsp }
295 d65a88a2 2021-09-05 stsp argv[i++] = "--";
296 d65a88a2 2021-09-05 stsp argv[i++] = (char *)host;
297 d65a88a2 2021-09-05 stsp argv[i++] = (char *)cmd;
298 a928faa1 2023-03-10 thomas argv[i++] = (char *)escaped_path;
299 d65a88a2 2021-09-05 stsp argv[i++] = NULL;
300 c10270f6 2021-09-06 naddy assert(i <= nitems(argv));
301 d65a88a2 2021-09-05 stsp
302 d65a88a2 2021-09-05 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
303 d65a88a2 2021-09-05 stsp return got_error_from_errno("socketpair");
304 d65a88a2 2021-09-05 stsp
305 d65a88a2 2021-09-05 stsp pid = fork();
306 d65a88a2 2021-09-05 stsp if (pid == -1) {
307 d65a88a2 2021-09-05 stsp error = got_error_from_errno("fork");
308 d65a88a2 2021-09-05 stsp close(pfd[0]);
309 d65a88a2 2021-09-05 stsp close(pfd[1]);
310 d65a88a2 2021-09-05 stsp return error;
311 d65a88a2 2021-09-05 stsp } else if (pid == 0) {
312 d65a88a2 2021-09-05 stsp if (close(pfd[1]) == -1)
313 d65a88a2 2021-09-05 stsp err(1, "close");
314 d65a88a2 2021-09-05 stsp if (dup2(pfd[0], 0) == -1)
315 d65a88a2 2021-09-05 stsp err(1, "dup2");
316 d65a88a2 2021-09-05 stsp if (dup2(pfd[0], 1) == -1)
317 d65a88a2 2021-09-05 stsp err(1, "dup2");
318 5769f9a0 2023-04-22 thomas if (strlcpy(cmd, command, sizeof(cmd)) >= sizeof(cmd))
319 d65a88a2 2021-09-05 stsp err(1, "snprintf");
320 0c6f49ba 2022-07-01 thomas if (execv(GOT_DIAL_PATH_SSH, (char *const *)argv) == -1)
321 eb0f0005 2024-03-02 thomas err(1, "execv %s", GOT_DIAL_PATH_SSH);
322 d65a88a2 2021-09-05 stsp abort(); /* not reached */
323 d65a88a2 2021-09-05 stsp } else {
324 d65a88a2 2021-09-05 stsp if (close(pfd[0]) == -1)
325 d65a88a2 2021-09-05 stsp return got_error_from_errno("close");
326 d65a88a2 2021-09-05 stsp *newpid = pid;
327 d65a88a2 2021-09-05 stsp *newfd = pfd[1];
328 d65a88a2 2021-09-05 stsp return NULL;
329 d65a88a2 2021-09-05 stsp }
330 d65a88a2 2021-09-05 stsp }
331 d65a88a2 2021-09-05 stsp
332 d65a88a2 2021-09-05 stsp const struct got_error *
333 d65a88a2 2021-09-05 stsp got_dial_git(int *newfd, const char *host, const char *port,
334 5769f9a0 2023-04-22 thomas const char *path, const char *command)
335 d65a88a2 2021-09-05 stsp {
336 d65a88a2 2021-09-05 stsp const struct got_error *err = NULL;
337 d65a88a2 2021-09-05 stsp struct addrinfo hints, *servinfo, *p;
338 d65a88a2 2021-09-05 stsp char *cmd = NULL;
339 d65a88a2 2021-09-05 stsp int fd = -1, len, r, eaicode;
340 d65a88a2 2021-09-05 stsp
341 d65a88a2 2021-09-05 stsp *newfd = -1;
342 d65a88a2 2021-09-05 stsp
343 d65a88a2 2021-09-05 stsp if (port == NULL)
344 d65a88a2 2021-09-05 stsp port = GOT_DEFAULT_GIT_PORT_STR;
345 d65a88a2 2021-09-05 stsp
346 d65a88a2 2021-09-05 stsp memset(&hints, 0, sizeof hints);
347 d65a88a2 2021-09-05 stsp hints.ai_family = AF_UNSPEC;
348 d65a88a2 2021-09-05 stsp hints.ai_socktype = SOCK_STREAM;
349 d65a88a2 2021-09-05 stsp eaicode = getaddrinfo(host, port, &hints, &servinfo);
350 d65a88a2 2021-09-05 stsp if (eaicode) {
351 d65a88a2 2021-09-05 stsp char msg[512];
352 d65a88a2 2021-09-05 stsp snprintf(msg, sizeof(msg), "%s: %s", host,
353 d65a88a2 2021-09-05 stsp gai_strerror(eaicode));
354 d65a88a2 2021-09-05 stsp return got_error_msg(GOT_ERR_ADDRINFO, msg);
355 d65a88a2 2021-09-05 stsp }
356 d65a88a2 2021-09-05 stsp
357 d65a88a2 2021-09-05 stsp for (p = servinfo; p != NULL; p = p->ai_next) {
358 d65a88a2 2021-09-05 stsp if ((fd = socket(p->ai_family, p->ai_socktype,
359 d65a88a2 2021-09-05 stsp p->ai_protocol)) == -1)
360 d65a88a2 2021-09-05 stsp continue;
361 d65a88a2 2021-09-05 stsp if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
362 d65a88a2 2021-09-05 stsp err = NULL;
363 d65a88a2 2021-09-05 stsp break;
364 d65a88a2 2021-09-05 stsp }
365 d65a88a2 2021-09-05 stsp err = got_error_from_errno("connect");
366 d65a88a2 2021-09-05 stsp close(fd);
367 d65a88a2 2021-09-05 stsp }
368 7ee8c11a 2022-09-05 thomas freeaddrinfo(servinfo);
369 d65a88a2 2021-09-05 stsp if (p == NULL)
370 d65a88a2 2021-09-05 stsp goto done;
371 d65a88a2 2021-09-05 stsp
372 5769f9a0 2023-04-22 thomas if (asprintf(&cmd, "%s %s", command, path) == -1) {
373 d65a88a2 2021-09-05 stsp err = got_error_from_errno("asprintf");
374 d65a88a2 2021-09-05 stsp goto done;
375 d65a88a2 2021-09-05 stsp }
376 d65a88a2 2021-09-05 stsp len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
377 d65a88a2 2021-09-05 stsp r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
378 d65a88a2 2021-09-05 stsp if (r < 0)
379 d65a88a2 2021-09-05 stsp err = got_error_from_errno("dprintf");
380 d65a88a2 2021-09-05 stsp done:
381 d65a88a2 2021-09-05 stsp free(cmd);
382 d65a88a2 2021-09-05 stsp if (err) {
383 d65a88a2 2021-09-05 stsp if (fd != -1)
384 d65a88a2 2021-09-05 stsp close(fd);
385 d65a88a2 2021-09-05 stsp } else
386 d65a88a2 2021-09-05 stsp *newfd = fd;
387 d65a88a2 2021-09-05 stsp return err;
388 37e7d69e 2024-04-25 thomas.ad }
389 37e7d69e 2024-04-25 thomas.ad
390 37e7d69e 2024-04-25 thomas.ad const struct got_error *
391 37e7d69e 2024-04-25 thomas.ad got_dial_http(pid_t *newpid, int *newfd, const char *host,
392 37e7d69e 2024-04-25 thomas.ad const char *port, const char *path, int verbosity, int tls)
393 37e7d69e 2024-04-25 thomas.ad {
394 37e7d69e 2024-04-25 thomas.ad const struct got_error *error = NULL;
395 37e7d69e 2024-04-25 thomas.ad int pid, pfd[2];
396 37e7d69e 2024-04-25 thomas.ad const char *argv[8];
397 37e7d69e 2024-04-25 thomas.ad int i = 0;
398 37e7d69e 2024-04-25 thomas.ad
399 37e7d69e 2024-04-25 thomas.ad *newpid = -1;
400 37e7d69e 2024-04-25 thomas.ad *newfd = -1;
401 37e7d69e 2024-04-25 thomas.ad
402 37e7d69e 2024-04-25 thomas.ad if (!port)
403 37e7d69e 2024-04-25 thomas.ad port = tls ? "443" : "80";
404 37e7d69e 2024-04-25 thomas.ad
405 09876a9d 2024-04-25 thomas.ad argv[i++] = GOT_PATH_PROG_FETCH_HTTP;
406 37e7d69e 2024-04-25 thomas.ad if (verbosity == -1)
407 37e7d69e 2024-04-25 thomas.ad argv[i++] = "-q";
408 37e7d69e 2024-04-25 thomas.ad else if (verbosity > 0)
409 37e7d69e 2024-04-25 thomas.ad argv[i++] = "-v";
410 37e7d69e 2024-04-25 thomas.ad argv[i++] = "--";
411 37e7d69e 2024-04-25 thomas.ad argv[i++] = tls ? "https" : "http";
412 37e7d69e 2024-04-25 thomas.ad argv[i++] = host;
413 37e7d69e 2024-04-25 thomas.ad argv[i++] = port;
414 37e7d69e 2024-04-25 thomas.ad argv[i++] = path;
415 37e7d69e 2024-04-25 thomas.ad argv[i++] = NULL;
416 37e7d69e 2024-04-25 thomas.ad assert(i <= nitems(argv));
417 37e7d69e 2024-04-25 thomas.ad
418 37e7d69e 2024-04-25 thomas.ad if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
419 37e7d69e 2024-04-25 thomas.ad return got_error_from_errno("socketpair");
420 37e7d69e 2024-04-25 thomas.ad
421 37e7d69e 2024-04-25 thomas.ad pid = fork();
422 37e7d69e 2024-04-25 thomas.ad if (pid == -1) {
423 37e7d69e 2024-04-25 thomas.ad error = got_error_from_errno("fork");
424 37e7d69e 2024-04-25 thomas.ad close(pfd[0]);
425 37e7d69e 2024-04-25 thomas.ad close(pfd[1]);
426 37e7d69e 2024-04-25 thomas.ad return error;
427 37e7d69e 2024-04-25 thomas.ad } else if (pid == 0) {
428 37e7d69e 2024-04-25 thomas.ad if (close(pfd[1]) == -1)
429 37e7d69e 2024-04-25 thomas.ad err(1, "close");
430 37e7d69e 2024-04-25 thomas.ad if (dup2(pfd[0], 0) == -1)
431 37e7d69e 2024-04-25 thomas.ad err(1, "dup2");
432 37e7d69e 2024-04-25 thomas.ad if (dup2(pfd[0], 1) == -1)
433 37e7d69e 2024-04-25 thomas.ad err(1, "dup2");
434 09876a9d 2024-04-25 thomas.ad if (execv(GOT_PATH_PROG_FETCH_HTTP, (char *const *)argv) == -1)
435 a1aebf7f 2024-04-25 thomas.ad err(1, "execv %s", GOT_PATH_PROG_FETCH_HTTP);
436 37e7d69e 2024-04-25 thomas.ad abort(); /* not reached */
437 37e7d69e 2024-04-25 thomas.ad } else {
438 37e7d69e 2024-04-25 thomas.ad if (close(pfd[0]) == -1)
439 37e7d69e 2024-04-25 thomas.ad return got_error_from_errno("close");
440 37e7d69e 2024-04-25 thomas.ad *newpid = pid;
441 37e7d69e 2024-04-25 thomas.ad *newfd = pfd[1];
442 37e7d69e 2024-04-25 thomas.ad return NULL;
443 37e7d69e 2024-04-25 thomas.ad }
444 d65a88a2 2021-09-05 stsp }
445 5769f9a0 2023-04-22 thomas
446 5769f9a0 2023-04-22 thomas const struct got_error *
447 5769f9a0 2023-04-22 thomas got_dial_parse_command(char **command, char **repo_path, const char *gitcmd)
448 5769f9a0 2023-04-22 thomas {
449 5769f9a0 2023-04-22 thomas const struct got_error *err = NULL;
450 5769f9a0 2023-04-22 thomas size_t len, cmdlen, pathlen;
451 5769f9a0 2023-04-22 thomas char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
452 5769f9a0 2023-04-22 thomas const char *relpath;
453 5769f9a0 2023-04-22 thomas
454 5769f9a0 2023-04-22 thomas *command = NULL;
455 5769f9a0 2023-04-22 thomas *repo_path = NULL;
456 5769f9a0 2023-04-22 thomas
457 5769f9a0 2023-04-22 thomas len = strlen(gitcmd);
458 5769f9a0 2023-04-22 thomas
459 5769f9a0 2023-04-22 thomas if (len >= strlen(GOT_DIAL_CMD_SEND) &&
460 5769f9a0 2023-04-22 thomas strncmp(gitcmd, GOT_DIAL_CMD_SEND,
461 5769f9a0 2023-04-22 thomas strlen(GOT_DIAL_CMD_SEND)) == 0)
462 5769f9a0 2023-04-22 thomas cmdlen = strlen(GOT_DIAL_CMD_SEND);
463 5769f9a0 2023-04-22 thomas else if (len >= strlen(GOT_DIAL_CMD_FETCH) &&
464 5769f9a0 2023-04-22 thomas strncmp(gitcmd, GOT_DIAL_CMD_FETCH,
465 5769f9a0 2023-04-22 thomas strlen(GOT_DIAL_CMD_FETCH)) == 0)
466 5769f9a0 2023-04-22 thomas cmdlen = strlen(GOT_DIAL_CMD_FETCH);
467 5769f9a0 2023-04-22 thomas else
468 5769f9a0 2023-04-22 thomas return got_error(GOT_ERR_BAD_PACKET);
469 5769f9a0 2023-04-22 thomas
470 5769f9a0 2023-04-22 thomas if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
471 5769f9a0 2023-04-22 thomas return got_error(GOT_ERR_BAD_PACKET);
472 5769f9a0 2023-04-22 thomas
473 5769f9a0 2023-04-22 thomas if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
474 5769f9a0 2023-04-22 thomas return got_error(GOT_ERR_BAD_PATH);
475 5769f9a0 2023-04-22 thomas
476 5769f9a0 2023-04-22 thomas /* Forbid linefeeds in paths, like Git does. */
477 5769f9a0 2023-04-22 thomas if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
478 5769f9a0 2023-04-22 thomas return got_error(GOT_ERR_BAD_PATH);
479 5769f9a0 2023-04-22 thomas
480 5769f9a0 2023-04-22 thomas path0 = strdup(&gitcmd[cmdlen + 1]);
481 5769f9a0 2023-04-22 thomas if (path0 == NULL)
482 5769f9a0 2023-04-22 thomas return got_error_from_errno("strdup");
483 5769f9a0 2023-04-22 thomas path = path0;
484 5769f9a0 2023-04-22 thomas pathlen = strlen(path);
485 5769f9a0 2023-04-22 thomas
486 5769f9a0 2023-04-22 thomas /*
487 5769f9a0 2023-04-22 thomas * Git clients send a shell command.
488 5769f9a0 2023-04-22 thomas * Trim spaces and quotes around the path.
489 5769f9a0 2023-04-22 thomas */
490 5769f9a0 2023-04-22 thomas while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
491 5769f9a0 2023-04-22 thomas path++;
492 5769f9a0 2023-04-22 thomas pathlen--;
493 5769f9a0 2023-04-22 thomas }
494 5769f9a0 2023-04-22 thomas while (pathlen > 0 &&
495 5769f9a0 2023-04-22 thomas (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
496 5769f9a0 2023-04-22 thomas path[pathlen - 1] == ' ')) {
497 5769f9a0 2023-04-22 thomas path[pathlen - 1] = '\0';
498 5769f9a0 2023-04-22 thomas pathlen--;
499 5769f9a0 2023-04-22 thomas }
500 5769f9a0 2023-04-22 thomas
501 5769f9a0 2023-04-22 thomas /* Deny an empty repository path. */
502 5769f9a0 2023-04-22 thomas if (path[0] == '\0' || got_path_is_root_dir(path)) {
503 5769f9a0 2023-04-22 thomas err = got_error(GOT_ERR_NOT_GIT_REPO);
504 5769f9a0 2023-04-22 thomas goto done;
505 5769f9a0 2023-04-22 thomas }
506 5769f9a0 2023-04-22 thomas
507 5769f9a0 2023-04-22 thomas if (asprintf(&abspath, "/%s", path) == -1) {
508 5769f9a0 2023-04-22 thomas err = got_error_from_errno("asprintf");
509 5769f9a0 2023-04-22 thomas goto done;
510 5769f9a0 2023-04-22 thomas }
511 5769f9a0 2023-04-22 thomas pathlen = strlen(abspath);
512 5769f9a0 2023-04-22 thomas canonpath = malloc(pathlen + 1);
513 5769f9a0 2023-04-22 thomas if (canonpath == NULL) {
514 5769f9a0 2023-04-22 thomas err = got_error_from_errno("malloc");
515 5769f9a0 2023-04-22 thomas goto done;
516 5769f9a0 2023-04-22 thomas }
517 5769f9a0 2023-04-22 thomas err = got_canonpath(abspath, canonpath, pathlen + 1);
518 5769f9a0 2023-04-22 thomas if (err)
519 5769f9a0 2023-04-22 thomas goto done;
520 5769f9a0 2023-04-22 thomas
521 5769f9a0 2023-04-22 thomas relpath = canonpath;
522 5769f9a0 2023-04-22 thomas while (relpath[0] == '/')
523 5769f9a0 2023-04-22 thomas relpath++;
524 5769f9a0 2023-04-22 thomas *repo_path = strdup(relpath);
525 5769f9a0 2023-04-22 thomas if (*repo_path == NULL) {
526 5769f9a0 2023-04-22 thomas err = got_error_from_errno("strdup");
527 5769f9a0 2023-04-22 thomas goto done;
528 5769f9a0 2023-04-22 thomas }
529 5769f9a0 2023-04-22 thomas *command = strndup(gitcmd, cmdlen);
530 5769f9a0 2023-04-22 thomas if (*command == NULL)
531 5769f9a0 2023-04-22 thomas err = got_error_from_errno("strndup");
532 5769f9a0 2023-04-22 thomas done:
533 5769f9a0 2023-04-22 thomas free(path0);
534 5769f9a0 2023-04-22 thomas free(abspath);
535 5769f9a0 2023-04-22 thomas free(canonpath);
536 5769f9a0 2023-04-22 thomas if (err) {
537 5769f9a0 2023-04-22 thomas free(*repo_path);
538 5769f9a0 2023-04-22 thomas *repo_path = NULL;
539 5769f9a0 2023-04-22 thomas }
540 5769f9a0 2023-04-22 thomas return err;
541 5769f9a0 2023-04-22 thomas }