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;
86 int n;
88 *proto = *host = *port = *server_path = *repo_name = NULL;
90 p = strstr(uri, "://");
91 if (!p) {
92 /* Try parsing Git's "scp" style URL syntax. */
93 *proto = strdup("ssh");
94 if (proto == NULL) {
95 err = got_error_from_errno("strdup");
96 goto done;
97 }
98 s = (char *)uri;
99 q = strchr(s, ':');
100 if (q == NULL) {
101 err = got_error(GOT_ERR_PARSE_URI);
102 goto done;
104 /* No slashes allowed before first colon. */
105 p = strchr(s, '/');
106 if (p && q > p) {
107 err = got_error(GOT_ERR_PARSE_URI);
108 goto done;
110 *host = strndup(s, q - s);
111 if (*host == NULL) {
112 err = got_error_from_errno("strndup");
113 goto done;
115 p = q + 1;
116 } else {
117 *proto = strndup(uri, p - uri);
118 if (proto == NULL) {
119 err = got_error_from_errno("strndup");
120 goto done;
122 s = p + 3;
124 p = strstr(s, "/");
125 if (p == NULL || strlen(p) == 1) {
126 err = got_error(GOT_ERR_PARSE_URI);
127 goto done;
130 q = memchr(s, ':', p - s);
131 if (q) {
132 *host = strndup(s, q - s);
133 if (*host == NULL) {
134 err = got_error_from_errno("strndup");
135 goto done;
137 *port = strndup(q + 1, p - (q + 1));
138 if (*port == NULL) {
139 err = got_error_from_errno("strndup");
140 goto done;
142 } else {
143 *host = strndup(s, p - s);
144 if (*host == NULL) {
145 err = got_error_from_errno("strndup");
146 goto done;
151 while (p[0] == '/' && p[1] == '/')
152 p++;
153 *server_path = strdup(p);
154 if (*server_path == NULL) {
155 err = got_error_from_errno("strdup");
156 goto done;
158 got_path_strip_trailing_slashes(*server_path);
160 p = strrchr(p, '/');
161 if (!p || strlen(p) <= 1) {
162 err = got_error(GOT_ERR_PARSE_URI);
163 goto done;
165 p++;
166 n = strlen(p);
167 if (n == 0) {
168 err = got_error(GOT_ERR_PARSE_URI);
169 goto done;
171 if (hassuffix(p, ".git"))
172 n -= 4;
173 *repo_name = strndup(p, (p + n) - p);
174 if (*repo_name == NULL) {
175 err = got_error_from_errno("strndup");
176 goto done;
178 done:
179 if (err) {
180 free(*proto);
181 *proto = NULL;
182 free(*host);
183 *host = NULL;
184 free(*port);
185 *port = NULL;
186 free(*server_path);
187 *server_path = NULL;
188 free(*repo_name);
189 *repo_name = NULL;
191 return err;
194 const struct got_error *
195 got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
196 const char *port, const char *path, const char *direction, int verbosity)
198 const struct got_error *error = NULL;
199 int pid, pfd[2];
200 char cmd[64];
201 char *argv[11];
202 int i = 0, j;
204 *newpid = -1;
205 *newfd = -1;
207 argv[i++] = GOT_DIAL_PATH_SSH;
208 if (port != NULL) {
209 argv[i++] = "-p";
210 argv[i++] = (char *)port;
212 if (verbosity == -1) {
213 argv[i++] = "-q";
214 } else {
215 /* ssh(1) allows up to 3 "-v" options. */
216 for (j = 0; j < MIN(3, verbosity); j++)
217 argv[i++] = "-v";
219 argv[i++] = "--";
220 argv[i++] = (char *)host;
221 argv[i++] = (char *)cmd;
222 argv[i++] = (char *)path;
223 argv[i++] = NULL;
224 assert(i <= nitems(argv));
226 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
227 return got_error_from_errno("socketpair");
229 pid = fork();
230 if (pid == -1) {
231 error = got_error_from_errno("fork");
232 close(pfd[0]);
233 close(pfd[1]);
234 return error;
235 } else if (pid == 0) {
236 int n;
237 if (close(pfd[1]) == -1)
238 err(1, "close");
239 if (dup2(pfd[0], 0) == -1)
240 err(1, "dup2");
241 if (dup2(pfd[0], 1) == -1)
242 err(1, "dup2");
243 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
244 if (n < 0 || n >= ssizeof(cmd))
245 err(1, "snprintf");
246 if (execv(GOT_DIAL_PATH_SSH, argv) == -1)
247 err(1, "execv");
248 abort(); /* not reached */
249 } else {
250 if (close(pfd[0]) == -1)
251 return got_error_from_errno("close");
252 *newpid = pid;
253 *newfd = pfd[1];
254 return NULL;
258 const struct got_error *
259 got_dial_git(int *newfd, const char *host, const char *port,
260 const char *path, const char *direction)
262 const struct got_error *err = NULL;
263 struct addrinfo hints, *servinfo, *p;
264 char *cmd = NULL;
265 int fd = -1, len, r, eaicode;
267 *newfd = -1;
269 if (port == NULL)
270 port = GOT_DEFAULT_GIT_PORT_STR;
272 memset(&hints, 0, sizeof hints);
273 hints.ai_family = AF_UNSPEC;
274 hints.ai_socktype = SOCK_STREAM;
275 eaicode = getaddrinfo(host, port, &hints, &servinfo);
276 if (eaicode) {
277 char msg[512];
278 snprintf(msg, sizeof(msg), "%s: %s", host,
279 gai_strerror(eaicode));
280 return got_error_msg(GOT_ERR_ADDRINFO, msg);
283 for (p = servinfo; p != NULL; p = p->ai_next) {
284 if ((fd = socket(p->ai_family, p->ai_socktype,
285 p->ai_protocol)) == -1)
286 continue;
287 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
288 err = NULL;
289 break;
291 err = got_error_from_errno("connect");
292 close(fd);
294 if (p == NULL)
295 goto done;
297 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
298 err = got_error_from_errno("asprintf");
299 goto done;
301 len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
302 r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
303 if (r < 0)
304 err = got_error_from_errno("dprintf");
305 done:
306 free(cmd);
307 if (err) {
308 if (fd != -1)
309 close(fd);
310 } else
311 *newfd = fd;
312 return err;