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