portable: configure: use config.h
[got-portable.git] / lib / dial.c
blobdc7a448c999c4b5077313b0e944478a315474dbf
1 /*
2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
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.
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.
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_compat.h"
35 #include "got_lib_dial.h"
36 #include "got_dial.h"
38 #ifndef nitems
39 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
40 #endif
42 #ifndef ssizeof
43 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
44 #endif
46 #ifndef MIN
47 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
48 #endif
50 #ifndef GOT_DIAL_PATH_SSH
51 #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
52 #endif
54 /* IANA assigned */
55 #define GOT_DEFAULT_GIT_PORT 9418
56 #define GOT_DEFAULT_GIT_PORT_STR "9418"
58 const struct got_error *
59 got_dial_apply_unveil(const char *proto)
61 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
62 if (unveil(GOT_DIAL_PATH_SSH, "x") != 0) {
63 return got_error_from_errno2("unveil",
64 GOT_DIAL_PATH_SSH);
68 return NULL;
71 static int
72 hassuffix(const char *base, const char *suf)
74 int nb, ns;
76 nb = strlen(base);
77 ns = strlen(suf);
78 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
79 return 1;
80 return 0;
83 const struct got_error *
84 got_dial_parse_uri(char **proto, char **host, char **port,
85 char **server_path, char **repo_name, const char *uri)
87 const struct got_error *err = NULL;
88 char *s, *p, *q;
90 *proto = *host = *port = *server_path = *repo_name = NULL;
92 p = strstr(uri, "://");
93 if (!p) {
94 /* Try parsing Git's "scp" style URL syntax. */
95 *proto = strdup("ssh");
96 if (*proto == NULL) {
97 err = got_error_from_errno("strdup");
98 goto done;
100 s = (char *)uri;
101 q = strchr(s, ':');
102 if (q == NULL) {
103 err = got_error(GOT_ERR_PARSE_URI);
104 goto done;
106 /* No slashes allowed before first colon. */
107 p = strchr(s, '/');
108 if (p && q > p) {
109 err = got_error(GOT_ERR_PARSE_URI);
110 goto done;
112 *host = strndup(s, q - s);
113 if (*host == NULL) {
114 err = got_error_from_errno("strndup");
115 goto done;
117 if ((*host)[0] == '\0') {
118 err = got_error(GOT_ERR_PARSE_URI);
119 goto done;
121 p = q + 1;
122 } else {
123 *proto = strndup(uri, p - uri);
124 if (*proto == NULL) {
125 err = got_error_from_errno("strndup");
126 goto done;
128 s = p + 3;
130 p = strstr(s, "/");
131 if (p == NULL || strlen(p) == 1) {
132 err = got_error(GOT_ERR_PARSE_URI);
133 goto done;
136 q = memchr(s, ':', p - s);
137 if (q) {
138 *host = strndup(s, q - s);
139 if (*host == NULL) {
140 err = got_error_from_errno("strndup");
141 goto done;
143 if ((*host)[0] == '\0') {
144 err = got_error(GOT_ERR_PARSE_URI);
145 goto done;
147 *port = strndup(q + 1, p - (q + 1));
148 if (*port == NULL) {
149 err = got_error_from_errno("strndup");
150 goto done;
152 if ((*port)[0] == '\0') {
153 err = got_error(GOT_ERR_PARSE_URI);
154 goto done;
156 } else {
157 *host = strndup(s, p - s);
158 if (*host == NULL) {
159 err = got_error_from_errno("strndup");
160 goto done;
162 if ((*host)[0] == '\0') {
163 err = got_error(GOT_ERR_PARSE_URI);
164 goto done;
169 while (p[0] == '/' && p[1] == '/')
170 p++;
171 *server_path = strdup(p);
172 if (*server_path == NULL) {
173 err = got_error_from_errno("strdup");
174 goto done;
176 got_path_strip_trailing_slashes(*server_path);
177 if ((*server_path)[0] == '\0') {
178 err = got_error(GOT_ERR_PARSE_URI);
179 goto done;
182 err = got_path_basename(repo_name, *server_path);
183 if (err)
184 goto done;
185 if (hassuffix(*repo_name, ".git"))
186 (*repo_name)[strlen(*repo_name) - 4] = '\0';
187 if ((*repo_name)[0] == '\0')
188 err = got_error(GOT_ERR_PARSE_URI);
189 done:
190 if (err) {
191 free(*proto);
192 *proto = NULL;
193 free(*host);
194 *host = NULL;
195 free(*port);
196 *port = NULL;
197 free(*server_path);
198 *server_path = NULL;
199 free(*repo_name);
200 *repo_name = NULL;
202 return err;
205 const struct got_error *
206 got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
207 const char *port, const char *path, const char *direction, int verbosity)
209 const struct got_error *error = NULL;
210 int pid, pfd[2];
211 char cmd[64];
212 const char *argv[11];
213 int i = 0, j;
215 *newpid = -1;
216 *newfd = -1;
218 argv[i++] = GOT_DIAL_PATH_SSH;
219 if (port != NULL) {
220 argv[i++] = "-p";
221 argv[i++] = (char *)port;
223 if (verbosity == -1) {
224 argv[i++] = "-q";
225 } else {
226 /* ssh(1) allows up to 3 "-v" options. */
227 for (j = 0; j < MIN(3, verbosity); j++)
228 argv[i++] = "-v";
230 argv[i++] = "--";
231 argv[i++] = (char *)host;
232 argv[i++] = (char *)cmd;
233 argv[i++] = (char *)path;
234 argv[i++] = NULL;
235 assert(i <= nitems(argv));
237 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
238 return got_error_from_errno("socketpair");
240 pid = fork();
241 if (pid == -1) {
242 error = got_error_from_errno("fork");
243 close(pfd[0]);
244 close(pfd[1]);
245 return error;
246 } else if (pid == 0) {
247 int n;
248 if (close(pfd[1]) == -1)
249 err(1, "close");
250 if (dup2(pfd[0], 0) == -1)
251 err(1, "dup2");
252 if (dup2(pfd[0], 1) == -1)
253 err(1, "dup2");
254 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
255 if (n < 0 || n >= ssizeof(cmd))
256 err(1, "snprintf");
257 if (execv(GOT_DIAL_PATH_SSH, (char *const *)argv) == -1)
258 err(1, "execv");
259 abort(); /* not reached */
260 } else {
261 if (close(pfd[0]) == -1)
262 return got_error_from_errno("close");
263 *newpid = pid;
264 *newfd = pfd[1];
265 return NULL;
269 const struct got_error *
270 got_dial_git(int *newfd, const char *host, const char *port,
271 const char *path, const char *direction)
273 const struct got_error *err = NULL;
274 struct addrinfo hints, *servinfo, *p;
275 char *cmd = NULL;
276 int fd = -1, len, r, eaicode;
278 *newfd = -1;
280 if (port == NULL)
281 port = GOT_DEFAULT_GIT_PORT_STR;
283 memset(&hints, 0, sizeof hints);
284 hints.ai_family = AF_UNSPEC;
285 hints.ai_socktype = SOCK_STREAM;
286 eaicode = getaddrinfo(host, port, &hints, &servinfo);
287 if (eaicode) {
288 char msg[512];
289 snprintf(msg, sizeof(msg), "%s: %s", host,
290 gai_strerror(eaicode));
291 return got_error_msg(GOT_ERR_ADDRINFO, msg);
294 for (p = servinfo; p != NULL; p = p->ai_next) {
295 if ((fd = socket(p->ai_family, p->ai_socktype,
296 p->ai_protocol)) == -1)
297 continue;
298 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
299 err = NULL;
300 break;
302 err = got_error_from_errno("connect");
303 close(fd);
305 freeaddrinfo(servinfo);
306 if (p == NULL)
307 goto done;
309 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
310 err = got_error_from_errno("asprintf");
311 goto done;
313 len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
314 r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
315 if (r < 0)
316 err = got_error_from_errno("dprintf");
317 done:
318 free(cmd);
319 if (err) {
320 if (fd != -1)
321 close(fd);
322 } else
323 *newfd = fd;
324 return err;