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