]> git.ipfire.org Git - thirdparty/git.git/blame - connect.c
Move refspec parser from connect.c and cache.h to remote.{c,h}
[thirdparty/git.git] / connect.c
CommitLineData
731043fd 1#include "git-compat-util.h"
f7192598 2#include "cache.h"
41cb7488 3#include "pkt-line.h"
b10d0ec7 4#include "quote.h"
6abf5c0c 5#include "refs.h"
15a1c012 6#include "run-command.h"
6b62816c 7#include "remote.h"
f7192598 8
96f1e58f 9static char *server_capabilities;
211b5f9e 10
2718ff09
LT
11static int check_ref(const char *name, int len, unsigned int flags)
12{
13 if (!flags)
14 return 1;
15
c41e20b3 16 if (len < 5 || memcmp(name, "refs/", 5))
2718ff09
LT
17 return 0;
18
19 /* Skip the "refs/" part */
20 name += 5;
21 len -= 5;
22
23 /* REF_NORMAL means that we don't want the magic fake tag refs */
24 if ((flags & REF_NORMAL) && check_ref_format(name) < 0)
25 return 0;
26
27 /* REF_HEADS means that we want regular branch heads */
28 if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
29 return 1;
30
31 /* REF_TAGS means that we want tags */
32 if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
33 return 1;
34
35 /* All type bits clear means that we are ok with anything */
36 return !(flags & ~REF_NORMAL);
37}
38
d1c133f5
LT
39/*
40 * Read all the refs from the other end
41 */
1a7141ff 42struct ref **get_remote_heads(int in, struct ref **list,
2718ff09
LT
43 int nr_match, char **match,
44 unsigned int flags)
d1c133f5
LT
45{
46 *list = NULL;
47 for (;;) {
48 struct ref *ref;
49 unsigned char old_sha1[20];
50 static char buffer[1000];
51 char *name;
211b5f9e 52 int len, name_len;
d1c133f5
LT
53
54 len = packet_read_line(in, buffer, sizeof(buffer));
55 if (!len)
56 break;
57 if (buffer[len-1] == '\n')
58 buffer[--len] = 0;
59
60 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
61 die("protocol error: expected sha/ref, got '%s'", buffer);
62 name = buffer + 41;
1a7141ff 63
211b5f9e
JS
64 name_len = strlen(name);
65 if (len != name_len + 41) {
66 if (server_capabilities)
67 free(server_capabilities);
9befac47 68 server_capabilities = xstrdup(name + name_len + 1);
211b5f9e
JS
69 }
70
2718ff09 71 if (!check_ref(name, name_len, flags))
cfee10a7 72 continue;
d1c133f5
LT
73 if (nr_match && !path_match(name, nr_match, match))
74 continue;
f88395ac 75 ref = xcalloc(1, sizeof(*ref) + len - 40);
e702496e 76 hashcpy(ref->old_sha1, old_sha1);
d1c133f5 77 memcpy(ref->name, buffer + 41, len - 40);
d1c133f5
LT
78 *list = ref;
79 list = &ref->next;
80 }
81 return list;
82}
83
211b5f9e
JS
84int server_supports(const char *feature)
85{
1f5881bb
JS
86 return server_capabilities &&
87 strstr(server_capabilities, feature) != NULL;
211b5f9e
JS
88}
89
41cb7488
LT
90int get_ack(int fd, unsigned char *result_sha1)
91{
92 static char line[1000];
93 int len = packet_read_line(fd, line, sizeof(line));
94
95 if (!len)
96 die("git-fetch-pack: expected ACK/NAK, got EOF");
97 if (line[len-1] == '\n')
98 line[--len] = 0;
99 if (!strcmp(line, "NAK"))
100 return 0;
cc44c765 101 if (!prefixcmp(line, "ACK ")) {
c4c86f07
JS
102 if (!get_sha1_hex(line+4, result_sha1)) {
103 if (strstr(line+45, "continue"))
104 return 2;
41cb7488 105 return 1;
c4c86f07 106 }
41cb7488
LT
107 }
108 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
109}
110
013e7c7f
LT
111int path_match(const char *path, int nr, char **match)
112{
113 int i;
114 int pathlen = strlen(path);
115
116 for (i = 0; i < nr; i++) {
117 char *s = match[i];
118 int len = strlen(s);
119
120 if (!len || len > pathlen)
121 continue;
122 if (memcmp(path + pathlen - len, s, len))
123 continue;
124 if (pathlen > len && path[pathlen - len - 1] != '/')
125 continue;
126 *s = 0;
9546010b 127 return (i + 1);
013e7c7f
LT
128 }
129 return 0;
130}
131
2386d658
LT
132enum protocol {
133 PROTO_LOCAL = 1,
134 PROTO_SSH,
135 PROTO_GIT,
136};
137
138static enum protocol get_protocol(const char *name)
139{
140 if (!strcmp(name, "ssh"))
141 return PROTO_SSH;
142 if (!strcmp(name, "git"))
143 return PROTO_GIT;
c05186cc
LT
144 if (!strcmp(name, "git+ssh"))
145 return PROTO_SSH;
146 if (!strcmp(name, "ssh+git"))
147 return PROTO_SSH;
2386d658
LT
148 die("I don't handle protocol '%s'", name);
149}
150
5ba88448
YH
151#define STR_(s) # s
152#define STR(s) STR_(s)
2386d658 153
49744d63 154#ifndef NO_IPV6
4c505f71 155
5ad312be
JL
156/*
157 * Returns a connected socket() fd, or else die()s.
158 */
7841ce79 159static int git_tcp_connect_sock(char *host, int flags)
2386d658 160{
ac3bc6c1 161 int sockfd = -1, saved_errno = 0;
5ba88448 162 char *colon, *end;
554fe20d 163 const char *port = STR(DEFAULT_GIT_PORT);
5ba88448
YH
164 struct addrinfo hints, *ai0, *ai;
165 int gai;
166
167 if (host[0] == '[') {
168 end = strchr(host + 1, ']');
169 if (end) {
170 *end = 0;
171 end++;
172 host++;
173 } else
174 end = host;
175 } else
176 end = host;
177 colon = strchr(end, ':');
178
ce6f8e7e
LT
179 if (colon) {
180 *colon = 0;
5ba88448 181 port = colon + 1;
608d48b2
LT
182 if (!*port)
183 port = "<none>";
ce6f8e7e 184 }
5ba88448
YH
185
186 memset(&hints, 0, sizeof(hints));
187 hints.ai_socktype = SOCK_STREAM;
188 hints.ai_protocol = IPPROTO_TCP;
189
7841ce79
MT
190 if (flags & CONNECT_VERBOSE)
191 fprintf(stderr, "Looking up %s ... ", host);
192
5ba88448
YH
193 gai = getaddrinfo(host, port, &hints, &ai);
194 if (gai)
608d48b2 195 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
5ba88448 196
7841ce79
MT
197 if (flags & CONNECT_VERBOSE)
198 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
199
5ba88448 200 for (ai0 = ai; ai; ai = ai->ai_next) {
5ad312be
JL
201 sockfd = socket(ai->ai_family,
202 ai->ai_socktype, ai->ai_protocol);
ac3bc6c1
PB
203 if (sockfd < 0) {
204 saved_errno = errno;
5ba88448 205 continue;
ac3bc6c1 206 }
5ba88448 207 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
ac3bc6c1 208 saved_errno = errno;
5ba88448
YH
209 close(sockfd);
210 sockfd = -1;
211 continue;
2386d658 212 }
5ba88448 213 break;
2386d658
LT
214 }
215
5ba88448 216 freeaddrinfo(ai0);
2386d658 217
2386d658 218 if (sockfd < 0)
ac3bc6c1 219 die("unable to connect a socket (%s)", strerror(saved_errno));
5ba88448 220
7841ce79
MT
221 if (flags & CONNECT_VERBOSE)
222 fprintf(stderr, "done.\n");
223
5ad312be 224 return sockfd;
2386d658
LT
225}
226
49744d63 227#else /* NO_IPV6 */
4c505f71 228
5ad312be
JL
229/*
230 * Returns a connected socket() fd, or else die()s.
231 */
7841ce79 232static int git_tcp_connect_sock(char *host, int flags)
4c505f71 233{
ac3bc6c1 234 int sockfd = -1, saved_errno = 0;
4c505f71
PA
235 char *colon, *end;
236 char *port = STR(DEFAULT_GIT_PORT), *ep;
237 struct hostent *he;
238 struct sockaddr_in sa;
239 char **ap;
240 unsigned int nport;
241
242 if (host[0] == '[') {
243 end = strchr(host + 1, ']');
244 if (end) {
245 *end = 0;
246 end++;
247 host++;
248 } else
249 end = host;
250 } else
251 end = host;
252 colon = strchr(end, ':');
253
254 if (colon) {
255 *colon = 0;
256 port = colon + 1;
257 }
258
7841ce79
MT
259 if (flags & CONNECT_VERBOSE)
260 fprintf(stderr, "Looking up %s ... ", host);
261
4c505f71
PA
262 he = gethostbyname(host);
263 if (!he)
264 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
265 nport = strtoul(port, &ep, 10);
266 if ( ep == port || *ep ) {
267 /* Not numeric */
268 struct servent *se = getservbyname(port,"tcp");
269 if ( !se )
270 die("Unknown port %s\n", port);
271 nport = se->s_port;
272 }
273
7841ce79
MT
274 if (flags & CONNECT_VERBOSE)
275 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
276
4c505f71
PA
277 for (ap = he->h_addr_list; *ap; ap++) {
278 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
ac3bc6c1
PB
279 if (sockfd < 0) {
280 saved_errno = errno;
4c505f71 281 continue;
ac3bc6c1 282 }
4c505f71
PA
283
284 memset(&sa, 0, sizeof sa);
285 sa.sin_family = he->h_addrtype;
6573faff 286 sa.sin_port = htons(nport);
c6164218 287 memcpy(&sa.sin_addr, *ap, he->h_length);
4c505f71
PA
288
289 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
ac3bc6c1 290 saved_errno = errno;
4c505f71
PA
291 close(sockfd);
292 sockfd = -1;
293 continue;
294 }
295 break;
296 }
297
298 if (sockfd < 0)
ac3bc6c1 299 die("unable to connect a socket (%s)", strerror(saved_errno));
4c505f71 300
7841ce79
MT
301 if (flags & CONNECT_VERBOSE)
302 fprintf(stderr, "done.\n");
303
5ad312be
JL
304 return sockfd;
305}
306
307#endif /* NO_IPV6 */
308
309
7841ce79 310static void git_tcp_connect(int fd[2], char *host, int flags)
5ad312be 311{
7841ce79 312 int sockfd = git_tcp_connect_sock(host, flags);
5ad312be 313
4c505f71 314 fd[0] = sockfd;
ec587fde 315 fd[1] = dup(sockfd);
4c505f71
PA
316}
317
4c505f71 318
96f1e58f
DR
319static char *git_proxy_command;
320static const char *rhost_name;
e814bc4d 321static int rhost_len;
f8014776
PC
322
323static int git_proxy_command_options(const char *var, const char *value)
324{
e814bc4d 325 if (!strcmp(var, "core.gitproxy")) {
c3df8568
YH
326 const char *for_pos;
327 int matchlen = -1;
328 int hostlen;
329
e814bc4d 330 if (git_proxy_command)
f8014776 331 return 0;
e814bc4d
JH
332 /* [core]
333 * ;# matches www.kernel.org as well
334 * gitproxy = netcatter-1 for kernel.org
335 * gitproxy = netcatter-2 for sample.xz
336 * gitproxy = netcatter-default
337 */
c3df8568 338 for_pos = strstr(value, " for ");
e814bc4d
JH
339 if (!for_pos)
340 /* matches everybody */
341 matchlen = strlen(value);
342 else {
343 hostlen = strlen(for_pos + 5);
344 if (rhost_len < hostlen)
345 matchlen = -1;
346 else if (!strncmp(for_pos + 5,
347 rhost_name + rhost_len - hostlen,
348 hostlen) &&
349 ((rhost_len == hostlen) ||
350 rhost_name[rhost_len - hostlen -1] == '.'))
351 matchlen = for_pos - value;
352 else
353 matchlen = -1;
354 }
355 if (0 <= matchlen) {
356 /* core.gitproxy = none for kernel.org */
357 if (matchlen == 4 &&
358 !memcmp(value, "none", 4))
359 matchlen = 0;
360 git_proxy_command = xmalloc(matchlen + 1);
361 memcpy(git_proxy_command, value, matchlen);
362 git_proxy_command[matchlen] = 0;
f8014776 363 }
e814bc4d 364 return 0;
f8014776
PC
365 }
366
367 return git_default_config(var, value);
368}
369
e814bc4d 370static int git_use_proxy(const char *host)
f8014776 371{
e814bc4d
JH
372 rhost_name = host;
373 rhost_len = strlen(host);
f8014776
PC
374 git_proxy_command = getenv("GIT_PROXY_COMMAND");
375 git_config(git_proxy_command_options);
e814bc4d
JH
376 rhost_name = NULL;
377 return (git_proxy_command && *git_proxy_command);
f8014776
PC
378}
379
c78963d2 380static void git_proxy_connect(int fd[2], char *host)
f8014776 381{
554fe20d 382 const char *port = STR(DEFAULT_GIT_PORT);
f8014776 383 char *colon, *end;
15a1c012
SP
384 const char *argv[4];
385 struct child_process proxy;
f8014776
PC
386
387 if (host[0] == '[') {
388 end = strchr(host + 1, ']');
389 if (end) {
390 *end = 0;
391 end++;
392 host++;
393 } else
394 end = host;
395 } else
396 end = host;
397 colon = strchr(end, ':');
398
399 if (colon) {
400 *colon = 0;
401 port = colon + 1;
402 }
403
15a1c012
SP
404 argv[0] = git_proxy_command;
405 argv[1] = host;
406 argv[2] = port;
407 argv[3] = NULL;
408 memset(&proxy, 0, sizeof(proxy));
409 proxy.argv = argv;
410 proxy.in = -1;
411 proxy.out = -1;
412 if (start_command(&proxy))
413 die("cannot start proxy %s", argv[0]);
414 fd[0] = proxy.out; /* read from proxy stdout */
415 fd[1] = proxy.in; /* write to proxy stdin */
f8014776
PC
416}
417
0f503d77
CC
418#define MAX_CMD_LEN 1024
419
f7192598 420/*
f42a5c4e
FBH
421 * This returns 0 if the transport protocol does not need fork(2),
422 * or a process id if it does. Once done, finish the connection
423 * with finish_connect() with the value returned from this function
424 * (it is safe to call finish_connect() with 0 to support the former
425 * case).
426 *
427 * Does not return a negative value on error; it just dies.
f7192598 428 */
7841ce79 429pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
f7192598 430{
faea9ccb 431 char *host, *path = url;
356bece0
YH
432 char *end;
433 int c;
f7192598
LT
434 int pipefd[2][2];
435 pid_t pid;
faea9ccb 436 enum protocol protocol = PROTO_LOCAL;
da2a95b2 437 int free_path = 0;
faea9ccb 438
f0b7367c
JH
439 /* Without this we cannot rely on waitpid() to tell
440 * what happened to our children.
441 */
442 signal(SIGCHLD, SIG_DFL);
443
faea9ccb
AE
444 host = strstr(url, "://");
445 if(host) {
446 *host = '\0';
447 protocol = get_protocol(url);
448 host += 3;
356bece0
YH
449 c = '/';
450 } else {
f7192598 451 host = url;
356bece0
YH
452 c = ':';
453 }
454
455 if (host[0] == '[') {
456 end = strchr(host + 1, ']');
457 if (end) {
458 *end = 0;
459 end++;
460 host++;
461 } else
462 end = host;
463 } else
464 end = host;
465
466 path = strchr(end, c);
467 if (c == ':') {
468 if (path) {
faea9ccb 469 protocol = PROTO_SSH;
356bece0
YH
470 *path++ = '\0';
471 } else
472 path = host;
f7192598 473 }
2386d658 474
faea9ccb
AE
475 if (!path || !*path)
476 die("No path specified. See 'man git-pull' for valid url syntax");
477
478 /*
479 * null-terminate hostname and point path to ~ for URL's like this:
480 * ssh://host.xz/~user/repo
481 */
482 if (protocol != PROTO_LOCAL && host != url) {
483 char *ptr = path;
484 if (path[1] == '~')
485 path++;
da2a95b2 486 else {
9befac47 487 path = xstrdup(ptr);
da2a95b2
SH
488 free_path = 1;
489 }
faea9ccb
AE
490
491 *ptr = '\0';
492 }
493
f8014776 494 if (protocol == PROTO_GIT) {
5ad312be
JL
495 /* These underlying connection commands die() if they
496 * cannot connect.
497 */
9befac47 498 char *target_host = xstrdup(host);
e814bc4d 499 if (git_use_proxy(host))
c78963d2 500 git_proxy_connect(fd, host);
da2a95b2 501 else
7841ce79 502 git_tcp_connect(fd, host, flags);
5ad312be
JL
503 /*
504 * Separate original protocol components prog and path
505 * from extended components with a NUL byte.
506 */
507 packet_write(fd[1],
508 "%s %s%chost=%s%c",
509 prog, path, 0,
510 target_host, 0);
511 free(target_host);
da2a95b2
SH
512 if (free_path)
513 free(path);
5ad312be 514 return 0;
f8014776 515 }
2386d658 516
f7192598
LT
517 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
518 die("unable to create pipe pair for communication");
519 pid = fork();
c9bc159d
PD
520 if (pid < 0)
521 die("unable to fork");
f7192598 522 if (!pid) {
0f503d77
CC
523 char command[MAX_CMD_LEN];
524 char *posn = command;
525 int size = MAX_CMD_LEN;
526 int of = 0;
527
528 of |= add_to_string(&posn, &size, prog, 0);
529 of |= add_to_string(&posn, &size, " ", 0);
530 of |= add_to_string(&posn, &size, path, 1);
531
532 if (of)
533 die("command line too long");
534
f7192598
LT
535 dup2(pipefd[1][0], 0);
536 dup2(pipefd[0][1], 1);
537 close(pipefd[0][0]);
538 close(pipefd[0][1]);
539 close(pipefd[1][0]);
540 close(pipefd[1][1]);
4852f723 541 if (protocol == PROTO_SSH) {
c7c81b3a
JR
542 const char *ssh, *ssh_basename;
543 ssh = getenv("GIT_SSH");
544 if (!ssh) ssh = "ssh";
545 ssh_basename = strrchr(ssh, '/');
4852f723
MS
546 if (!ssh_basename)
547 ssh_basename = ssh;
548 else
549 ssh_basename++;
550 execlp(ssh, ssh_basename, host, command, NULL);
551 }
016fb48b
MD
552 else {
553 unsetenv(ALTERNATE_DB_ENVIRONMENT);
554 unsetenv(DB_ENVIRONMENT);
555 unsetenv(GIT_DIR_ENVIRONMENT);
556 unsetenv(GRAFT_ENVIRONMENT);
557 unsetenv(INDEX_ENVIRONMENT);
f7192598 558 execlp("sh", "sh", "-c", command, NULL);
016fb48b 559 }
f7192598 560 die("exec failed");
016fb48b 561 }
f7192598
LT
562 fd[0] = pipefd[0][0];
563 fd[1] = pipefd[1][1];
564 close(pipefd[0][1]);
565 close(pipefd[1][0]);
da2a95b2
SH
566 if (free_path)
567 free(path);
f7192598
LT
568 return pid;
569}
570
571int finish_connect(pid_t pid)
572{
f42a5c4e
FBH
573 if (pid == 0)
574 return 0;
575
53e1a761 576 while (waitpid(pid, NULL, 0) < 0) {
f7192598 577 if (errno != EINTR)
53e1a761 578 return -1;
f7192598 579 }
53e1a761 580 return 0;
f7192598 581}