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 d65a88a2 2021-09-05 stsp #include <netdb.h>
24 d65a88a2 2021-09-05 stsp
25 c10270f6 2021-09-06 naddy #include <assert.h>
26 d65a88a2 2021-09-05 stsp #include <err.h>
27 d65a88a2 2021-09-05 stsp #include <stdio.h>
28 d65a88a2 2021-09-05 stsp #include <stdlib.h>
29 d65a88a2 2021-09-05 stsp #include <string.h>
30 d65a88a2 2021-09-05 stsp #include <unistd.h>
31 d65a88a2 2021-09-05 stsp
32 d65a88a2 2021-09-05 stsp #include "got_error.h"
33 5e5da8c4 2021-09-05 stsp #include "got_path.h"
34 d65a88a2 2021-09-05 stsp
35 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
36 dd038bc6 2021-09-21 thomas.ad
37 d65a88a2 2021-09-05 stsp #include "got_lib_dial.h"
38 ef20f542 2022-06-26 thomas #include "got_dial.h"
39 c10270f6 2021-09-06 naddy
40 c10270f6 2021-09-06 naddy #ifndef nitems
41 c10270f6 2021-09-06 naddy #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
42 c10270f6 2021-09-06 naddy #endif
43 d65a88a2 2021-09-05 stsp
44 d65a88a2 2021-09-05 stsp #ifndef ssizeof
45 d65a88a2 2021-09-05 stsp #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
46 d65a88a2 2021-09-05 stsp #endif
47 d65a88a2 2021-09-05 stsp
48 d65a88a2 2021-09-05 stsp #ifndef MIN
49 d65a88a2 2021-09-05 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 d65a88a2 2021-09-05 stsp #endif
51 d65a88a2 2021-09-05 stsp
52 d65a88a2 2021-09-05 stsp #ifndef GOT_DIAL_PATH_SSH
53 d65a88a2 2021-09-05 stsp #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
54 d65a88a2 2021-09-05 stsp #endif
55 d65a88a2 2021-09-05 stsp
56 d65a88a2 2021-09-05 stsp /* IANA assigned */
57 d65a88a2 2021-09-05 stsp #define GOT_DEFAULT_GIT_PORT 9418
58 d65a88a2 2021-09-05 stsp #define GOT_DEFAULT_GIT_PORT_STR "9418"
59 d65a88a2 2021-09-05 stsp
60 d65a88a2 2021-09-05 stsp const struct got_error *
61 d65a88a2 2021-09-05 stsp got_dial_apply_unveil(const char *proto)
62 d65a88a2 2021-09-05 stsp {
63 d65a88a2 2021-09-05 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
64 d65a88a2 2021-09-05 stsp if (unveil(GOT_DIAL_PATH_SSH, "x") != 0) {
65 d65a88a2 2021-09-05 stsp return got_error_from_errno2("unveil",
66 d65a88a2 2021-09-05 stsp GOT_DIAL_PATH_SSH);
67 d65a88a2 2021-09-05 stsp }
68 d65a88a2 2021-09-05 stsp }
69 d65a88a2 2021-09-05 stsp
70 d65a88a2 2021-09-05 stsp return NULL;
71 d65a88a2 2021-09-05 stsp }
72 d65a88a2 2021-09-05 stsp
73 5e5da8c4 2021-09-05 stsp static int
74 0c6f49ba 2022-07-01 thomas hassuffix(const char *base, const char *suf)
75 5e5da8c4 2021-09-05 stsp {
76 5e5da8c4 2021-09-05 stsp int nb, ns;
77 5e5da8c4 2021-09-05 stsp
78 5e5da8c4 2021-09-05 stsp nb = strlen(base);
79 5e5da8c4 2021-09-05 stsp ns = strlen(suf);
80 5e5da8c4 2021-09-05 stsp if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
81 5e5da8c4 2021-09-05 stsp return 1;
82 5e5da8c4 2021-09-05 stsp return 0;
83 5e5da8c4 2021-09-05 stsp }
84 5e5da8c4 2021-09-05 stsp
85 d65a88a2 2021-09-05 stsp const struct got_error *
86 5e5da8c4 2021-09-05 stsp got_dial_parse_uri(char **proto, char **host, char **port,
87 5e5da8c4 2021-09-05 stsp char **server_path, char **repo_name, const char *uri)
88 5e5da8c4 2021-09-05 stsp {
89 5e5da8c4 2021-09-05 stsp const struct got_error *err = NULL;
90 5e5da8c4 2021-09-05 stsp char *s, *p, *q;
91 5e5da8c4 2021-09-05 stsp
92 5e5da8c4 2021-09-05 stsp *proto = *host = *port = *server_path = *repo_name = NULL;
93 5e5da8c4 2021-09-05 stsp
94 5e5da8c4 2021-09-05 stsp p = strstr(uri, "://");
95 5e5da8c4 2021-09-05 stsp if (!p) {
96 5e5da8c4 2021-09-05 stsp /* Try parsing Git's "scp" style URL syntax. */
97 5e5da8c4 2021-09-05 stsp *proto = strdup("ssh");
98 2996af60 2022-03-07 thomas if (*proto == NULL) {
99 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strdup");
100 5e5da8c4 2021-09-05 stsp goto done;
101 5e5da8c4 2021-09-05 stsp }
102 5e5da8c4 2021-09-05 stsp s = (char *)uri;
103 5e5da8c4 2021-09-05 stsp q = strchr(s, ':');
104 5e5da8c4 2021-09-05 stsp if (q == NULL) {
105 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
106 5e5da8c4 2021-09-05 stsp goto done;
107 5e5da8c4 2021-09-05 stsp }
108 5e5da8c4 2021-09-05 stsp /* No slashes allowed before first colon. */
109 5e5da8c4 2021-09-05 stsp p = strchr(s, '/');
110 5e5da8c4 2021-09-05 stsp if (p && q > p) {
111 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
112 5e5da8c4 2021-09-05 stsp goto done;
113 5e5da8c4 2021-09-05 stsp }
114 5e5da8c4 2021-09-05 stsp *host = strndup(s, q - s);
115 5e5da8c4 2021-09-05 stsp if (*host == NULL) {
116 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
117 5e5da8c4 2021-09-05 stsp goto done;
118 5e5da8c4 2021-09-05 stsp }
119 26ec43f5 2022-03-07 thomas if ((*host)[0] == '\0') {
120 26ec43f5 2022-03-07 thomas err = got_error(GOT_ERR_PARSE_URI);
121 26ec43f5 2022-03-07 thomas goto done;
122 26ec43f5 2022-03-07 thomas }
123 5e5da8c4 2021-09-05 stsp p = q + 1;
124 5e5da8c4 2021-09-05 stsp } else {
125 5e5da8c4 2021-09-05 stsp *proto = strndup(uri, p - uri);
126 2996af60 2022-03-07 thomas if (*proto == NULL) {
127 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
128 5e5da8c4 2021-09-05 stsp goto done;
129 5e5da8c4 2021-09-05 stsp }
130 5e5da8c4 2021-09-05 stsp s = p + 3;
131 5e5da8c4 2021-09-05 stsp
132 5e5da8c4 2021-09-05 stsp p = strstr(s, "/");
133 5e5da8c4 2021-09-05 stsp if (p == NULL || strlen(p) == 1) {
134 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
135 5e5da8c4 2021-09-05 stsp goto done;
136 5e5da8c4 2021-09-05 stsp }
137 5e5da8c4 2021-09-05 stsp
138 5e5da8c4 2021-09-05 stsp q = memchr(s, ':', p - s);
139 5e5da8c4 2021-09-05 stsp if (q) {
140 5e5da8c4 2021-09-05 stsp *host = strndup(s, q - s);
141 5e5da8c4 2021-09-05 stsp if (*host == NULL) {
142 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
143 5e5da8c4 2021-09-05 stsp goto done;
144 5e5da8c4 2021-09-05 stsp }
145 26ec43f5 2022-03-07 thomas if ((*host)[0] == '\0') {
146 26ec43f5 2022-03-07 thomas err = got_error(GOT_ERR_PARSE_URI);
147 26ec43f5 2022-03-07 thomas goto done;
148 26ec43f5 2022-03-07 thomas }
149 5e5da8c4 2021-09-05 stsp *port = strndup(q + 1, p - (q + 1));
150 5e5da8c4 2021-09-05 stsp if (*port == NULL) {
151 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
152 5e5da8c4 2021-09-05 stsp goto done;
153 5e5da8c4 2021-09-05 stsp }
154 26ec43f5 2022-03-07 thomas if ((*port)[0] == '\0') {
155 26ec43f5 2022-03-07 thomas err = got_error(GOT_ERR_PARSE_URI);
156 26ec43f5 2022-03-07 thomas goto done;
157 26ec43f5 2022-03-07 thomas }
158 5e5da8c4 2021-09-05 stsp } else {
159 5e5da8c4 2021-09-05 stsp *host = strndup(s, p - s);
160 5e5da8c4 2021-09-05 stsp if (*host == NULL) {
161 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
162 5e5da8c4 2021-09-05 stsp goto done;
163 5e5da8c4 2021-09-05 stsp }
164 26ec43f5 2022-03-07 thomas if ((*host)[0] == '\0') {
165 26ec43f5 2022-03-07 thomas err = got_error(GOT_ERR_PARSE_URI);
166 26ec43f5 2022-03-07 thomas goto done;
167 26ec43f5 2022-03-07 thomas }
168 5e5da8c4 2021-09-05 stsp }
169 5e5da8c4 2021-09-05 stsp }
170 5e5da8c4 2021-09-05 stsp
171 5e5da8c4 2021-09-05 stsp while (p[0] == '/' && p[1] == '/')
172 5e5da8c4 2021-09-05 stsp p++;
173 5e5da8c4 2021-09-05 stsp *server_path = strdup(p);
174 5e5da8c4 2021-09-05 stsp if (*server_path == NULL) {
175 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strdup");
176 5e5da8c4 2021-09-05 stsp goto done;
177 5e5da8c4 2021-09-05 stsp }
178 5e5da8c4 2021-09-05 stsp got_path_strip_trailing_slashes(*server_path);
179 26ec43f5 2022-03-07 thomas if ((*server_path)[0] == '\0') {
180 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
181 5e5da8c4 2021-09-05 stsp goto done;
182 5e5da8c4 2021-09-05 stsp }
183 26ec43f5 2022-03-07 thomas
184 26ec43f5 2022-03-07 thomas err = got_path_basename(repo_name, *server_path);
185 26ec43f5 2022-03-07 thomas if (err)
186 5e5da8c4 2021-09-05 stsp goto done;
187 26ec43f5 2022-03-07 thomas if (hassuffix(*repo_name, ".git"))
188 26ec43f5 2022-03-07 thomas (*repo_name)[strlen(*repo_name) - 4] = '\0';
189 26ec43f5 2022-03-07 thomas if ((*repo_name)[0] == '\0')
190 26ec43f5 2022-03-07 thomas err = got_error(GOT_ERR_PARSE_URI);
191 5e5da8c4 2021-09-05 stsp done:
192 5e5da8c4 2021-09-05 stsp if (err) {
193 5e5da8c4 2021-09-05 stsp free(*proto);
194 5e5da8c4 2021-09-05 stsp *proto = NULL;
195 5e5da8c4 2021-09-05 stsp free(*host);
196 5e5da8c4 2021-09-05 stsp *host = NULL;
197 5e5da8c4 2021-09-05 stsp free(*port);
198 5e5da8c4 2021-09-05 stsp *port = NULL;
199 5e5da8c4 2021-09-05 stsp free(*server_path);
200 5e5da8c4 2021-09-05 stsp *server_path = NULL;
201 5e5da8c4 2021-09-05 stsp free(*repo_name);
202 5e5da8c4 2021-09-05 stsp *repo_name = NULL;
203 5e5da8c4 2021-09-05 stsp }
204 5e5da8c4 2021-09-05 stsp return err;
205 5e5da8c4 2021-09-05 stsp }
206 5e5da8c4 2021-09-05 stsp
207 5e5da8c4 2021-09-05 stsp const struct got_error *
208 d65a88a2 2021-09-05 stsp got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
209 d65a88a2 2021-09-05 stsp const char *port, const char *path, const char *direction, int verbosity)
210 d65a88a2 2021-09-05 stsp {
211 d65a88a2 2021-09-05 stsp const struct got_error *error = NULL;
212 d65a88a2 2021-09-05 stsp int pid, pfd[2];
213 d65a88a2 2021-09-05 stsp char cmd[64];
214 0c6f49ba 2022-07-01 thomas const char *argv[11];
215 d65a88a2 2021-09-05 stsp int i = 0, j;
216 d65a88a2 2021-09-05 stsp
217 d65a88a2 2021-09-05 stsp *newpid = -1;
218 d65a88a2 2021-09-05 stsp *newfd = -1;
219 d65a88a2 2021-09-05 stsp
220 d65a88a2 2021-09-05 stsp argv[i++] = GOT_DIAL_PATH_SSH;
221 d65a88a2 2021-09-05 stsp if (port != NULL) {
222 d65a88a2 2021-09-05 stsp argv[i++] = "-p";
223 d65a88a2 2021-09-05 stsp argv[i++] = (char *)port;
224 d65a88a2 2021-09-05 stsp }
225 d65a88a2 2021-09-05 stsp if (verbosity == -1) {
226 d65a88a2 2021-09-05 stsp argv[i++] = "-q";
227 d65a88a2 2021-09-05 stsp } else {
228 d65a88a2 2021-09-05 stsp /* ssh(1) allows up to 3 "-v" options. */
229 d65a88a2 2021-09-05 stsp for (j = 0; j < MIN(3, verbosity); j++)
230 d65a88a2 2021-09-05 stsp argv[i++] = "-v";
231 d65a88a2 2021-09-05 stsp }
232 d65a88a2 2021-09-05 stsp argv[i++] = "--";
233 d65a88a2 2021-09-05 stsp argv[i++] = (char *)host;
234 d65a88a2 2021-09-05 stsp argv[i++] = (char *)cmd;
235 d65a88a2 2021-09-05 stsp argv[i++] = (char *)path;
236 d65a88a2 2021-09-05 stsp argv[i++] = NULL;
237 c10270f6 2021-09-06 naddy assert(i <= nitems(argv));
238 d65a88a2 2021-09-05 stsp
239 d65a88a2 2021-09-05 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
240 d65a88a2 2021-09-05 stsp return got_error_from_errno("socketpair");
241 d65a88a2 2021-09-05 stsp
242 d65a88a2 2021-09-05 stsp pid = fork();
243 d65a88a2 2021-09-05 stsp if (pid == -1) {
244 d65a88a2 2021-09-05 stsp error = got_error_from_errno("fork");
245 d65a88a2 2021-09-05 stsp close(pfd[0]);
246 d65a88a2 2021-09-05 stsp close(pfd[1]);
247 d65a88a2 2021-09-05 stsp return error;
248 d65a88a2 2021-09-05 stsp } else if (pid == 0) {
249 d65a88a2 2021-09-05 stsp int n;
250 d65a88a2 2021-09-05 stsp if (close(pfd[1]) == -1)
251 d65a88a2 2021-09-05 stsp err(1, "close");
252 d65a88a2 2021-09-05 stsp if (dup2(pfd[0], 0) == -1)
253 d65a88a2 2021-09-05 stsp err(1, "dup2");
254 d65a88a2 2021-09-05 stsp if (dup2(pfd[0], 1) == -1)
255 d65a88a2 2021-09-05 stsp err(1, "dup2");
256 d65a88a2 2021-09-05 stsp n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
257 d65a88a2 2021-09-05 stsp if (n < 0 || n >= ssizeof(cmd))
258 d65a88a2 2021-09-05 stsp err(1, "snprintf");
259 0c6f49ba 2022-07-01 thomas if (execv(GOT_DIAL_PATH_SSH, (char *const *)argv) == -1)
260 d65a88a2 2021-09-05 stsp err(1, "execv");
261 d65a88a2 2021-09-05 stsp abort(); /* not reached */
262 d65a88a2 2021-09-05 stsp } else {
263 d65a88a2 2021-09-05 stsp if (close(pfd[0]) == -1)
264 d65a88a2 2021-09-05 stsp return got_error_from_errno("close");
265 d65a88a2 2021-09-05 stsp *newpid = pid;
266 d65a88a2 2021-09-05 stsp *newfd = pfd[1];
267 d65a88a2 2021-09-05 stsp return NULL;
268 d65a88a2 2021-09-05 stsp }
269 d65a88a2 2021-09-05 stsp }
270 d65a88a2 2021-09-05 stsp
271 d65a88a2 2021-09-05 stsp const struct got_error *
272 d65a88a2 2021-09-05 stsp got_dial_git(int *newfd, const char *host, const char *port,
273 d65a88a2 2021-09-05 stsp const char *path, const char *direction)
274 d65a88a2 2021-09-05 stsp {
275 d65a88a2 2021-09-05 stsp const struct got_error *err = NULL;
276 d65a88a2 2021-09-05 stsp struct addrinfo hints, *servinfo, *p;
277 d65a88a2 2021-09-05 stsp char *cmd = NULL;
278 d65a88a2 2021-09-05 stsp int fd = -1, len, r, eaicode;
279 d65a88a2 2021-09-05 stsp
280 d65a88a2 2021-09-05 stsp *newfd = -1;
281 d65a88a2 2021-09-05 stsp
282 d65a88a2 2021-09-05 stsp if (port == NULL)
283 d65a88a2 2021-09-05 stsp port = GOT_DEFAULT_GIT_PORT_STR;
284 d65a88a2 2021-09-05 stsp
285 d65a88a2 2021-09-05 stsp memset(&hints, 0, sizeof hints);
286 d65a88a2 2021-09-05 stsp hints.ai_family = AF_UNSPEC;
287 d65a88a2 2021-09-05 stsp hints.ai_socktype = SOCK_STREAM;
288 d65a88a2 2021-09-05 stsp eaicode = getaddrinfo(host, port, &hints, &servinfo);
289 d65a88a2 2021-09-05 stsp if (eaicode) {
290 d65a88a2 2021-09-05 stsp char msg[512];
291 d65a88a2 2021-09-05 stsp snprintf(msg, sizeof(msg), "%s: %s", host,
292 d65a88a2 2021-09-05 stsp gai_strerror(eaicode));
293 d65a88a2 2021-09-05 stsp return got_error_msg(GOT_ERR_ADDRINFO, msg);
294 d65a88a2 2021-09-05 stsp }
295 d65a88a2 2021-09-05 stsp
296 d65a88a2 2021-09-05 stsp for (p = servinfo; p != NULL; p = p->ai_next) {
297 d65a88a2 2021-09-05 stsp if ((fd = socket(p->ai_family, p->ai_socktype,
298 d65a88a2 2021-09-05 stsp p->ai_protocol)) == -1)
299 d65a88a2 2021-09-05 stsp continue;
300 d65a88a2 2021-09-05 stsp if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
301 d65a88a2 2021-09-05 stsp err = NULL;
302 d65a88a2 2021-09-05 stsp break;
303 d65a88a2 2021-09-05 stsp }
304 d65a88a2 2021-09-05 stsp err = got_error_from_errno("connect");
305 d65a88a2 2021-09-05 stsp close(fd);
306 d65a88a2 2021-09-05 stsp }
307 7ee8c11a 2022-09-05 thomas freeaddrinfo(servinfo);
308 d65a88a2 2021-09-05 stsp if (p == NULL)
309 d65a88a2 2021-09-05 stsp goto done;
310 d65a88a2 2021-09-05 stsp
311 d65a88a2 2021-09-05 stsp if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
312 d65a88a2 2021-09-05 stsp err = got_error_from_errno("asprintf");
313 d65a88a2 2021-09-05 stsp goto done;
314 d65a88a2 2021-09-05 stsp }
315 d65a88a2 2021-09-05 stsp len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
316 d65a88a2 2021-09-05 stsp r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
317 d65a88a2 2021-09-05 stsp if (r < 0)
318 d65a88a2 2021-09-05 stsp err = got_error_from_errno("dprintf");
319 d65a88a2 2021-09-05 stsp done:
320 d65a88a2 2021-09-05 stsp free(cmd);
321 d65a88a2 2021-09-05 stsp if (err) {
322 d65a88a2 2021-09-05 stsp if (fd != -1)
323 d65a88a2 2021-09-05 stsp close(fd);
324 d65a88a2 2021-09-05 stsp } else
325 d65a88a2 2021-09-05 stsp *newfd = fd;
326 d65a88a2 2021-09-05 stsp return err;
327 d65a88a2 2021-09-05 stsp }