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