]> git.ipfire.org Git - thirdparty/git.git/blame - connect.c
filter-branch: fix variable export logic
[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
4577370e
DB
39int check_ref_type(const struct ref *ref, int flags)
40{
41 return check_ref(ref->name, strlen(ref->name), flags);
42}
43
d1c133f5
LT
44/*
45 * Read all the refs from the other end
46 */
1a7141ff 47struct ref **get_remote_heads(int in, struct ref **list,
2718ff09
LT
48 int nr_match, char **match,
49 unsigned int flags)
d1c133f5
LT
50{
51 *list = NULL;
52 for (;;) {
53 struct ref *ref;
54 unsigned char old_sha1[20];
55 static char buffer[1000];
56 char *name;
211b5f9e 57 int len, name_len;
d1c133f5
LT
58
59 len = packet_read_line(in, buffer, sizeof(buffer));
60 if (!len)
61 break;
62 if (buffer[len-1] == '\n')
63 buffer[--len] = 0;
64
65 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
66 die("protocol error: expected sha/ref, got '%s'", buffer);
67 name = buffer + 41;
1a7141ff 68
211b5f9e
JS
69 name_len = strlen(name);
70 if (len != name_len + 41) {
8e0f7003 71 free(server_capabilities);
9befac47 72 server_capabilities = xstrdup(name + name_len + 1);
211b5f9e
JS
73 }
74
2718ff09 75 if (!check_ref(name, name_len, flags))
cfee10a7 76 continue;
d1c133f5
LT
77 if (nr_match && !path_match(name, nr_match, match))
78 continue;
90446a00 79 ref = alloc_ref(name_len + 1);
e702496e 80 hashcpy(ref->old_sha1, old_sha1);
90446a00 81 memcpy(ref->name, buffer + 41, name_len + 1);
d1c133f5
LT
82 *list = ref;
83 list = &ref->next;
84 }
85 return list;
86}
87
211b5f9e
JS
88int server_supports(const char *feature)
89{
1f5881bb
JS
90 return server_capabilities &&
91 strstr(server_capabilities, feature) != NULL;
211b5f9e
JS
92}
93
41cb7488
LT
94int get_ack(int fd, unsigned char *result_sha1)
95{
96 static char line[1000];
97 int len = packet_read_line(fd, line, sizeof(line));
98
99 if (!len)
100 die("git-fetch-pack: expected ACK/NAK, got EOF");
101 if (line[len-1] == '\n')
102 line[--len] = 0;
103 if (!strcmp(line, "NAK"))
104 return 0;
cc44c765 105 if (!prefixcmp(line, "ACK ")) {
c4c86f07
JS
106 if (!get_sha1_hex(line+4, result_sha1)) {
107 if (strstr(line+45, "continue"))
108 return 2;
41cb7488 109 return 1;
c4c86f07 110 }
41cb7488
LT
111 }
112 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
113}
114
013e7c7f
LT
115int path_match(const char *path, int nr, char **match)
116{
117 int i;
118 int pathlen = strlen(path);
119
120 for (i = 0; i < nr; i++) {
121 char *s = match[i];
122 int len = strlen(s);
123
124 if (!len || len > pathlen)
125 continue;
126 if (memcmp(path + pathlen - len, s, len))
127 continue;
128 if (pathlen > len && path[pathlen - len - 1] != '/')
129 continue;
130 *s = 0;
9546010b 131 return (i + 1);
013e7c7f
LT
132 }
133 return 0;
134}
135
2386d658
LT
136enum protocol {
137 PROTO_LOCAL = 1,
138 PROTO_SSH,
139 PROTO_GIT,
140};
141
142static enum protocol get_protocol(const char *name)
143{
144 if (!strcmp(name, "ssh"))
145 return PROTO_SSH;
146 if (!strcmp(name, "git"))
147 return PROTO_GIT;
c05186cc
LT
148 if (!strcmp(name, "git+ssh"))
149 return PROTO_SSH;
150 if (!strcmp(name, "ssh+git"))
151 return PROTO_SSH;
72a4f4b6
LT
152 if (!strcmp(name, "file"))
153 return PROTO_LOCAL;
2386d658
LT
154 die("I don't handle protocol '%s'", name);
155}
156
5ba88448
YH
157#define STR_(s) # s
158#define STR(s) STR_(s)
2386d658 159
49744d63 160#ifndef NO_IPV6
4c505f71 161
ba505322
AR
162static const char *ai_name(const struct addrinfo *ai)
163{
164 static char addr[INET_ADDRSTRLEN];
165 if ( AF_INET == ai->ai_family ) {
166 struct sockaddr_in *in;
167 in = (struct sockaddr_in *)ai->ai_addr;
168 inet_ntop(ai->ai_family, &in->sin_addr, addr, sizeof(addr));
169 } else if ( AF_INET6 == ai->ai_family ) {
170 struct sockaddr_in6 *in;
171 in = (struct sockaddr_in6 *)ai->ai_addr;
172 inet_ntop(ai->ai_family, &in->sin6_addr, addr, sizeof(addr));
173 } else {
174 strcpy(addr, "(unknown)");
175 }
176 return addr;
177}
178
5ad312be
JL
179/*
180 * Returns a connected socket() fd, or else die()s.
181 */
7841ce79 182static int git_tcp_connect_sock(char *host, int flags)
2386d658 183{
ac3bc6c1 184 int sockfd = -1, saved_errno = 0;
5ba88448 185 char *colon, *end;
554fe20d 186 const char *port = STR(DEFAULT_GIT_PORT);
5ba88448
YH
187 struct addrinfo hints, *ai0, *ai;
188 int gai;
ba505322 189 int cnt = 0;
5ba88448
YH
190
191 if (host[0] == '[') {
192 end = strchr(host + 1, ']');
193 if (end) {
194 *end = 0;
195 end++;
196 host++;
197 } else
198 end = host;
199 } else
200 end = host;
201 colon = strchr(end, ':');
202
ce6f8e7e
LT
203 if (colon) {
204 *colon = 0;
5ba88448 205 port = colon + 1;
608d48b2
LT
206 if (!*port)
207 port = "<none>";
ce6f8e7e 208 }
5ba88448
YH
209
210 memset(&hints, 0, sizeof(hints));
211 hints.ai_socktype = SOCK_STREAM;
212 hints.ai_protocol = IPPROTO_TCP;
213
7841ce79
MT
214 if (flags & CONNECT_VERBOSE)
215 fprintf(stderr, "Looking up %s ... ", host);
216
5ba88448
YH
217 gai = getaddrinfo(host, port, &hints, &ai);
218 if (gai)
608d48b2 219 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
5ba88448 220
7841ce79
MT
221 if (flags & CONNECT_VERBOSE)
222 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
223
5ba88448 224 for (ai0 = ai; ai; ai = ai->ai_next) {
5ad312be
JL
225 sockfd = socket(ai->ai_family,
226 ai->ai_socktype, ai->ai_protocol);
ac3bc6c1
PB
227 if (sockfd < 0) {
228 saved_errno = errno;
5ba88448 229 continue;
ac3bc6c1 230 }
5ba88448 231 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
ac3bc6c1 232 saved_errno = errno;
7cbf2f24 233 fprintf(stderr, "%s[%d: %s]: errno=%s\n",
ba505322
AR
234 host,
235 cnt,
236 ai_name(ai),
ba505322 237 strerror(saved_errno));
5ba88448
YH
238 close(sockfd);
239 sockfd = -1;
240 continue;
2386d658 241 }
ba505322
AR
242 if (flags & CONNECT_VERBOSE)
243 fprintf(stderr, "%s ", ai_name(ai));
5ba88448 244 break;
2386d658
LT
245 }
246
5ba88448 247 freeaddrinfo(ai0);
2386d658 248
2386d658 249 if (sockfd < 0)
ac3bc6c1 250 die("unable to connect a socket (%s)", strerror(saved_errno));
5ba88448 251
7841ce79
MT
252 if (flags & CONNECT_VERBOSE)
253 fprintf(stderr, "done.\n");
254
5ad312be 255 return sockfd;
2386d658
LT
256}
257
49744d63 258#else /* NO_IPV6 */
4c505f71 259
5ad312be
JL
260/*
261 * Returns a connected socket() fd, or else die()s.
262 */
7841ce79 263static int git_tcp_connect_sock(char *host, int flags)
4c505f71 264{
ac3bc6c1 265 int sockfd = -1, saved_errno = 0;
4c505f71
PA
266 char *colon, *end;
267 char *port = STR(DEFAULT_GIT_PORT), *ep;
268 struct hostent *he;
269 struct sockaddr_in sa;
270 char **ap;
271 unsigned int nport;
ba505322 272 int cnt;
4c505f71
PA
273
274 if (host[0] == '[') {
275 end = strchr(host + 1, ']');
276 if (end) {
277 *end = 0;
278 end++;
279 host++;
280 } else
281 end = host;
282 } else
283 end = host;
284 colon = strchr(end, ':');
285
286 if (colon) {
287 *colon = 0;
288 port = colon + 1;
289 }
290
7841ce79
MT
291 if (flags & CONNECT_VERBOSE)
292 fprintf(stderr, "Looking up %s ... ", host);
293
4c505f71
PA
294 he = gethostbyname(host);
295 if (!he)
296 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
297 nport = strtoul(port, &ep, 10);
298 if ( ep == port || *ep ) {
299 /* Not numeric */
300 struct servent *se = getservbyname(port,"tcp");
301 if ( !se )
302 die("Unknown port %s\n", port);
303 nport = se->s_port;
304 }
305
7841ce79
MT
306 if (flags & CONNECT_VERBOSE)
307 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
308
ba505322 309 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
4c505f71 310 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
ac3bc6c1
PB
311 if (sockfd < 0) {
312 saved_errno = errno;
4c505f71 313 continue;
ac3bc6c1 314 }
4c505f71
PA
315
316 memset(&sa, 0, sizeof sa);
317 sa.sin_family = he->h_addrtype;
6573faff 318 sa.sin_port = htons(nport);
c6164218 319 memcpy(&sa.sin_addr, *ap, he->h_length);
4c505f71
PA
320
321 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
ac3bc6c1 322 saved_errno = errno;
7cbf2f24 323 fprintf(stderr, "%s[%d: %s]: errno=%s\n",
ba505322
AR
324 host,
325 cnt,
326 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
ba505322 327 strerror(saved_errno));
4c505f71
PA
328 close(sockfd);
329 sockfd = -1;
330 continue;
331 }
ba505322
AR
332 if (flags & CONNECT_VERBOSE)
333 fprintf(stderr, "%s ",
334 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
4c505f71
PA
335 break;
336 }
337
338 if (sockfd < 0)
ac3bc6c1 339 die("unable to connect a socket (%s)", strerror(saved_errno));
4c505f71 340
7841ce79
MT
341 if (flags & CONNECT_VERBOSE)
342 fprintf(stderr, "done.\n");
343
5ad312be
JL
344 return sockfd;
345}
346
347#endif /* NO_IPV6 */
348
349
7841ce79 350static void git_tcp_connect(int fd[2], char *host, int flags)
5ad312be 351{
7841ce79 352 int sockfd = git_tcp_connect_sock(host, flags);
5ad312be 353
4c505f71 354 fd[0] = sockfd;
ec587fde 355 fd[1] = dup(sockfd);
4c505f71
PA
356}
357
4c505f71 358
96f1e58f
DR
359static char *git_proxy_command;
360static const char *rhost_name;
e814bc4d 361static int rhost_len;
f8014776
PC
362
363static int git_proxy_command_options(const char *var, const char *value)
364{
e814bc4d 365 if (!strcmp(var, "core.gitproxy")) {
c3df8568
YH
366 const char *for_pos;
367 int matchlen = -1;
368 int hostlen;
369
e814bc4d 370 if (git_proxy_command)
f8014776 371 return 0;
c64b9ad0
JH
372 if (!value)
373 return config_error_nonbool(var);
e814bc4d
JH
374 /* [core]
375 * ;# matches www.kernel.org as well
376 * gitproxy = netcatter-1 for kernel.org
377 * gitproxy = netcatter-2 for sample.xz
378 * gitproxy = netcatter-default
379 */
c3df8568 380 for_pos = strstr(value, " for ");
e814bc4d
JH
381 if (!for_pos)
382 /* matches everybody */
383 matchlen = strlen(value);
384 else {
385 hostlen = strlen(for_pos + 5);
386 if (rhost_len < hostlen)
387 matchlen = -1;
388 else if (!strncmp(for_pos + 5,
389 rhost_name + rhost_len - hostlen,
390 hostlen) &&
391 ((rhost_len == hostlen) ||
392 rhost_name[rhost_len - hostlen -1] == '.'))
393 matchlen = for_pos - value;
394 else
395 matchlen = -1;
396 }
397 if (0 <= matchlen) {
398 /* core.gitproxy = none for kernel.org */
a6080a0a 399 if (matchlen == 4 &&
e814bc4d
JH
400 !memcmp(value, "none", 4))
401 matchlen = 0;
182af834 402 git_proxy_command = xmemdupz(value, matchlen);
f8014776 403 }
e814bc4d 404 return 0;
f8014776
PC
405 }
406
407 return git_default_config(var, value);
408}
409
e814bc4d 410static int git_use_proxy(const char *host)
f8014776 411{
e814bc4d
JH
412 rhost_name = host;
413 rhost_len = strlen(host);
f8014776
PC
414 git_proxy_command = getenv("GIT_PROXY_COMMAND");
415 git_config(git_proxy_command_options);
e814bc4d
JH
416 rhost_name = NULL;
417 return (git_proxy_command && *git_proxy_command);
f8014776
PC
418}
419
c78963d2 420static void git_proxy_connect(int fd[2], char *host)
f8014776 421{
554fe20d 422 const char *port = STR(DEFAULT_GIT_PORT);
f8014776 423 char *colon, *end;
15a1c012
SP
424 const char *argv[4];
425 struct child_process proxy;
f8014776
PC
426
427 if (host[0] == '[') {
428 end = strchr(host + 1, ']');
429 if (end) {
430 *end = 0;
431 end++;
432 host++;
433 } else
434 end = host;
435 } else
436 end = host;
437 colon = strchr(end, ':');
438
439 if (colon) {
440 *colon = 0;
441 port = colon + 1;
442 }
443
15a1c012
SP
444 argv[0] = git_proxy_command;
445 argv[1] = host;
446 argv[2] = port;
447 argv[3] = NULL;
448 memset(&proxy, 0, sizeof(proxy));
449 proxy.argv = argv;
450 proxy.in = -1;
451 proxy.out = -1;
452 if (start_command(&proxy))
453 die("cannot start proxy %s", argv[0]);
454 fd[0] = proxy.out; /* read from proxy stdout */
455 fd[1] = proxy.in; /* write to proxy stdin */
f8014776
PC
456}
457
0f503d77
CC
458#define MAX_CMD_LEN 1024
459
2e776665
LT
460char *get_port(char *host)
461{
462 char *end;
463 char *p = strchr(host, ':');
464
465 if (p) {
466 strtol(p+1, &end, 10);
467 if (*end == '\0') {
468 *p = '\0';
469 return p+1;
470 }
471 }
472
473 return NULL;
474}
475
fb32c917
JS
476static struct child_process no_fork;
477
f7192598 478/*
fb32c917
JS
479 * This returns a dummy child_process if the transport protocol does not
480 * need fork(2), or a struct child_process object if it does. Once done,
481 * finish the connection with finish_connect() with the value returned from
482 * this function (it is safe to call finish_connect() with NULL to support
483 * the former case).
f42a5c4e 484 *
fb32c917
JS
485 * If it returns, the connect is successful; it just dies on errors (this
486 * will hopefully be changed in a libification effort, to return NULL when
487 * the connection failed).
f7192598 488 */
4577370e 489struct child_process *git_connect(int fd[2], const char *url_orig,
98158e9c 490 const char *prog, int flags)
f7192598 491{
4577370e 492 char *url = xstrdup(url_orig);
faea9ccb 493 char *host, *path = url;
356bece0
YH
494 char *end;
495 int c;
98158e9c 496 struct child_process *conn;
faea9ccb 497 enum protocol protocol = PROTO_LOCAL;
da2a95b2 498 int free_path = 0;
2e776665 499 char *port = NULL;
f364cb88
JS
500 const char **arg;
501 struct strbuf cmd;
faea9ccb 502
f0b7367c
JH
503 /* Without this we cannot rely on waitpid() to tell
504 * what happened to our children.
505 */
506 signal(SIGCHLD, SIG_DFL);
507
faea9ccb
AE
508 host = strstr(url, "://");
509 if(host) {
510 *host = '\0';
511 protocol = get_protocol(url);
512 host += 3;
356bece0
YH
513 c = '/';
514 } else {
f7192598 515 host = url;
356bece0
YH
516 c = ':';
517 }
518
519 if (host[0] == '[') {
520 end = strchr(host + 1, ']');
521 if (end) {
522 *end = 0;
523 end++;
524 host++;
525 } else
526 end = host;
527 } else
528 end = host;
529
530 path = strchr(end, c);
72a4f4b6
LT
531 if (path) {
532 if (c == ':') {
faea9ccb 533 protocol = PROTO_SSH;
356bece0 534 *path++ = '\0';
72a4f4b6
LT
535 }
536 } else
537 path = end;
2386d658 538
faea9ccb
AE
539 if (!path || !*path)
540 die("No path specified. See 'man git-pull' for valid url syntax");
541
542 /*
543 * null-terminate hostname and point path to ~ for URL's like this:
544 * ssh://host.xz/~user/repo
545 */
546 if (protocol != PROTO_LOCAL && host != url) {
547 char *ptr = path;
548 if (path[1] == '~')
549 path++;
da2a95b2 550 else {
9befac47 551 path = xstrdup(ptr);
da2a95b2
SH
552 free_path = 1;
553 }
faea9ccb
AE
554
555 *ptr = '\0';
556 }
557
2e776665
LT
558 /*
559 * Add support for ssh port: ssh://host.xy:<port>/...
560 */
561 if (protocol == PROTO_SSH && host != url)
562 port = get_port(host);
563
f8014776 564 if (protocol == PROTO_GIT) {
5ad312be
JL
565 /* These underlying connection commands die() if they
566 * cannot connect.
567 */
9befac47 568 char *target_host = xstrdup(host);
e814bc4d 569 if (git_use_proxy(host))
c78963d2 570 git_proxy_connect(fd, host);
da2a95b2 571 else
7841ce79 572 git_tcp_connect(fd, host, flags);
5ad312be
JL
573 /*
574 * Separate original protocol components prog and path
575 * from extended components with a NUL byte.
576 */
577 packet_write(fd[1],
578 "%s %s%chost=%s%c",
579 prog, path, 0,
580 target_host, 0);
581 free(target_host);
4577370e 582 free(url);
da2a95b2
SH
583 if (free_path)
584 free(path);
fb32c917 585 return &no_fork;
f8014776 586 }
2386d658 587
98158e9c 588 conn = xcalloc(1, sizeof(*conn));
2e776665 589
f364cb88
JS
590 strbuf_init(&cmd, MAX_CMD_LEN);
591 strbuf_addstr(&cmd, prog);
592 strbuf_addch(&cmd, ' ');
593 sq_quote_buf(&cmd, path);
594 if (cmd.len >= MAX_CMD_LEN)
595 die("command line too long");
596
597 conn->in = conn->out = -1;
598 conn->argv = arg = xcalloc(6, sizeof(*arg));
599 if (protocol == PROTO_SSH) {
600 const char *ssh = getenv("GIT_SSH");
601 if (!ssh) ssh = "ssh";
602
603 *arg++ = ssh;
604 if (port) {
605 *arg++ = "-p";
606 *arg++ = port;
4852f723 607 }
f364cb88
JS
608 *arg++ = host;
609 }
610 else {
611 /* remove these from the environment */
612 const char *env[] = {
613 ALTERNATE_DB_ENVIRONMENT,
614 DB_ENVIRONMENT,
615 GIT_DIR_ENVIRONMENT,
616 GIT_WORK_TREE_ENVIRONMENT,
617 GRAFT_ENVIRONMENT,
618 INDEX_ENVIRONMENT,
619 NULL
620 };
621 conn->env = env;
622 *arg++ = "sh";
623 *arg++ = "-c";
016fb48b 624 }
f364cb88
JS
625 *arg++ = cmd.buf;
626 *arg = NULL;
627
628 if (start_command(conn))
629 die("unable to fork");
630
631 fd[0] = conn->out; /* read from child's stdout */
632 fd[1] = conn->in; /* write to child's stdin */
633 strbuf_release(&cmd);
4577370e 634 free(url);
da2a95b2
SH
635 if (free_path)
636 free(path);
98158e9c 637 return conn;
f7192598
LT
638}
639
98158e9c 640int finish_connect(struct child_process *conn)
f7192598 641{
f364cb88 642 int code;
fb32c917 643 if (!conn || conn == &no_fork)
f42a5c4e
FBH
644 return 0;
645
f364cb88
JS
646 code = finish_command(conn);
647 free(conn->argv);
98158e9c 648 free(conn);
f364cb88 649 return code;
f7192598 650}