Blob


1 /*
2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netdb.h>
23 #include <assert.h>
24 #include <err.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include "got_error.h"
31 #include "got_path.h"
33 #include "got_lib_dial.h"
35 #ifndef nitems
36 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
37 #endif
39 #ifndef ssizeof
40 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
41 #endif
43 #ifndef MIN
44 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
45 #endif
47 #ifndef GOT_DIAL_PATH_SSH
48 #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
49 #endif
51 /* IANA assigned */
52 #define GOT_DEFAULT_GIT_PORT 9418
53 #define GOT_DEFAULT_GIT_PORT_STR "9418"
55 const struct got_error *
56 got_dial_apply_unveil(const char *proto)
57 {
58 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
59 if (unveil(GOT_DIAL_PATH_SSH, "x") != 0) {
60 return got_error_from_errno2("unveil",
61 GOT_DIAL_PATH_SSH);
62 }
63 }
65 return NULL;
66 }
68 static int
69 hassuffix(char *base, char *suf)
70 {
71 int nb, ns;
73 nb = strlen(base);
74 ns = strlen(suf);
75 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
76 return 1;
77 return 0;
78 }
80 const struct got_error *
81 got_dial_parse_uri(char **proto, char **host, char **port,
82 char **server_path, char **repo_name, const char *uri)
83 {
84 const struct got_error *err = NULL;
85 char *s, *p, *q;
87 *proto = *host = *port = *server_path = *repo_name = NULL;
89 p = strstr(uri, "://");
90 if (!p) {
91 /* Try parsing Git's "scp" style URL syntax. */
92 *proto = strdup("ssh");
93 if (*proto == NULL) {
94 err = got_error_from_errno("strdup");
95 goto done;
96 }
97 s = (char *)uri;
98 q = strchr(s, ':');
99 if (q == NULL) {
100 err = got_error(GOT_ERR_PARSE_URI);
101 goto done;
103 /* No slashes allowed before first colon. */
104 p = strchr(s, '/');
105 if (p && q > p) {
106 err = got_error(GOT_ERR_PARSE_URI);
107 goto done;
109 *host = strndup(s, q - s);
110 if (*host == NULL) {
111 err = got_error_from_errno("strndup");
112 goto done;
114 if ((*host)[0] == '\0') {
115 err = got_error(GOT_ERR_PARSE_URI);
116 goto done;
118 p = q + 1;
119 } else {
120 *proto = strndup(uri, p - uri);
121 if (*proto == NULL) {
122 err = got_error_from_errno("strndup");
123 goto done;
125 s = p + 3;
127 p = strstr(s, "/");
128 if (p == NULL || strlen(p) == 1) {
129 err = got_error(GOT_ERR_PARSE_URI);
130 goto done;
133 q = memchr(s, ':', p - s);
134 if (q) {
135 *host = strndup(s, q - s);
136 if (*host == NULL) {
137 err = got_error_from_errno("strndup");
138 goto done;
140 if ((*host)[0] == '\0') {
141 err = got_error(GOT_ERR_PARSE_URI);
142 goto done;
144 *port = strndup(q + 1, p - (q + 1));
145 if (*port == NULL) {
146 err = got_error_from_errno("strndup");
147 goto done;
149 if ((*port)[0] == '\0') {
150 err = got_error(GOT_ERR_PARSE_URI);
151 goto done;
153 } else {
154 *host = strndup(s, p - s);
155 if (*host == NULL) {
156 err = got_error_from_errno("strndup");
157 goto done;
159 if ((*host)[0] == '\0') {
160 err = got_error(GOT_ERR_PARSE_URI);
161 goto done;
166 while (p[0] == '/' && p[1] == '/')
167 p++;
168 *server_path = strdup(p);
169 if (*server_path == NULL) {
170 err = got_error_from_errno("strdup");
171 goto done;
173 got_path_strip_trailing_slashes(*server_path);
174 if ((*server_path)[0] == '\0') {
175 err = got_error(GOT_ERR_PARSE_URI);
176 goto done;
179 err = got_path_basename(repo_name, *server_path);
180 if (err)
181 goto done;
182 if (hassuffix(*repo_name, ".git"))
183 (*repo_name)[strlen(*repo_name) - 4] = '\0';
184 if ((*repo_name)[0] == '\0')
185 err = got_error(GOT_ERR_PARSE_URI);
186 done:
187 if (err) {
188 free(*proto);
189 *proto = NULL;
190 free(*host);
191 *host = NULL;
192 free(*port);
193 *port = NULL;
194 free(*server_path);
195 *server_path = NULL;
196 free(*repo_name);
197 *repo_name = NULL;
199 return err;
202 const struct got_error *
203 got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
204 const char *port, const char *path, const char *direction, int verbosity)
206 const struct got_error *error = NULL;
207 int pid, pfd[2];
208 char cmd[64];
209 char *argv[11];
210 int i = 0, j;
212 *newpid = -1;
213 *newfd = -1;
215 argv[i++] = GOT_DIAL_PATH_SSH;
216 if (port != NULL) {
217 argv[i++] = "-p";
218 argv[i++] = (char *)port;
220 if (verbosity == -1) {
221 argv[i++] = "-q";
222 } else {
223 /* ssh(1) allows up to 3 "-v" options. */
224 for (j = 0; j < MIN(3, verbosity); j++)
225 argv[i++] = "-v";
227 argv[i++] = "--";
228 argv[i++] = (char *)host;
229 argv[i++] = (char *)cmd;
230 argv[i++] = (char *)path;
231 argv[i++] = NULL;
232 assert(i <= nitems(argv));
234 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
235 return got_error_from_errno("socketpair");
237 pid = fork();
238 if (pid == -1) {
239 error = got_error_from_errno("fork");
240 close(pfd[0]);
241 close(pfd[1]);
242 return error;
243 } else if (pid == 0) {
244 int n;
245 if (close(pfd[1]) == -1)
246 err(1, "close");
247 if (dup2(pfd[0], 0) == -1)
248 err(1, "dup2");
249 if (dup2(pfd[0], 1) == -1)
250 err(1, "dup2");
251 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
252 if (n < 0 || n >= ssizeof(cmd))
253 err(1, "snprintf");
254 if (execv(GOT_DIAL_PATH_SSH, argv) == -1)
255 err(1, "execv");
256 abort(); /* not reached */
257 } else {
258 if (close(pfd[0]) == -1)
259 return got_error_from_errno("close");
260 *newpid = pid;
261 *newfd = pfd[1];
262 return NULL;
266 const struct got_error *
267 got_dial_git(int *newfd, const char *host, const char *port,
268 const char *path, const char *direction)
270 const struct got_error *err = NULL;
271 struct addrinfo hints, *servinfo, *p;
272 char *cmd = NULL;
273 int fd = -1, len, r, eaicode;
275 *newfd = -1;
277 if (port == NULL)
278 port = GOT_DEFAULT_GIT_PORT_STR;
280 memset(&hints, 0, sizeof hints);
281 hints.ai_family = AF_UNSPEC;
282 hints.ai_socktype = SOCK_STREAM;
283 eaicode = getaddrinfo(host, port, &hints, &servinfo);
284 if (eaicode) {
285 char msg[512];
286 snprintf(msg, sizeof(msg), "%s: %s", host,
287 gai_strerror(eaicode));
288 return got_error_msg(GOT_ERR_ADDRINFO, msg);
291 for (p = servinfo; p != NULL; p = p->ai_next) {
292 if ((fd = socket(p->ai_family, p->ai_socktype,
293 p->ai_protocol)) == -1)
294 continue;
295 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
296 err = NULL;
297 break;
299 err = got_error_from_errno("connect");
300 close(fd);
302 if (p == NULL)
303 goto done;
305 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
306 err = got_error_from_errno("asprintf");
307 goto done;
309 len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
310 r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
311 if (r < 0)
312 err = got_error_from_errno("dprintf");
313 done:
314 free(cmd);
315 if (err) {
316 if (fd != -1)
317 close(fd);
318 } else
319 *newfd = fd;
320 return err;