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