]> git.ipfire.org Git - thirdparty/git.git/blame - connect.c
fetch-pack: perform a fetch using v2
[thirdparty/git.git] / connect.c
CommitLineData
731043fd 1#include "git-compat-util.h"
f7192598 2#include "cache.h"
b2141fc1 3#include "config.h"
41cb7488 4#include "pkt-line.h"
b10d0ec7 5#include "quote.h"
6abf5c0c 6#include "refs.h"
15a1c012 7#include "run-command.h"
6b62816c 8#include "remote.h"
47a59185 9#include "connect.h"
9d2e9420 10#include "url.h"
a45b5f05 11#include "string-list.h"
13eb4626 12#include "sha1-array.h"
a5adaced 13#include "transport.h"
0cd83283 14#include "strbuf.h"
e52449b6 15#include "version.h"
2609043d 16#include "protocol.h"
f7192598 17
e52449b6
BW
18static char *server_capabilities_v1;
19static struct argv_array server_capabilities_v2 = ARGV_ARRAY_INIT;
5d54cffc 20static const char *parse_feature_value(const char *, const char *, int *);
211b5f9e 21
be0b3f82 22static int check_ref(const char *name, unsigned int flags)
2718ff09
LT
23{
24 if (!flags)
25 return 1;
26
be0b3f82 27 if (!skip_prefix(name, "refs/", &name))
2718ff09
LT
28 return 0;
29
2718ff09 30 /* REF_NORMAL means that we don't want the magic fake tag refs */
8d9c5010 31 if ((flags & REF_NORMAL) && check_refname_format(name, 0))
2718ff09
LT
32 return 0;
33
34 /* REF_HEADS means that we want regular branch heads */
be0b3f82 35 if ((flags & REF_HEADS) && starts_with(name, "heads/"))
2718ff09
LT
36 return 1;
37
38 /* REF_TAGS means that we want tags */
be0b3f82 39 if ((flags & REF_TAGS) && starts_with(name, "tags/"))
2718ff09
LT
40 return 1;
41
42 /* All type bits clear means that we are ok with anything */
43 return !(flags & ~REF_NORMAL);
44}
45
4577370e
DB
46int check_ref_type(const struct ref *ref, int flags)
47{
be0b3f82 48 return check_ref(ref->name, flags);
4577370e
DB
49}
50
55e4f936 51static void die_initial_contact(int unexpected)
46284dd1 52{
7e3e479b
BW
53 /*
54 * A hang-up after seeing some response from the other end
55 * means that it is unexpected, as we know the other end is
56 * willing to talk to us. A hang-up before seeing any
57 * response does not necessarily mean an ACL problem, though.
58 */
55e4f936 59 if (unexpected)
f2b93b38 60 die(_("The remote end hung up upon initial contact"));
46284dd1 61 else
f2b93b38
VA
62 die(_("Could not read from remote repository.\n\n"
63 "Please make sure you have the correct access rights\n"
64 "and the repository exists."));
46284dd1
HV
65}
66
e52449b6
BW
67/* Checks if the server supports the capability 'c' */
68int server_supports_v2(const char *c, int die_on_error)
69{
70 int i;
71
72 for (i = 0; i < server_capabilities_v2.argc; i++) {
73 const char *out;
74 if (skip_prefix(server_capabilities_v2.argv[i], c, &out) &&
75 (!*out || *out == '='))
76 return 1;
77 }
78
79 if (die_on_error)
80 die("server doesn't support '%s'", c);
81
82 return 0;
83}
84
85static void process_capabilities_v2(struct packet_reader *reader)
86{
87 while (packet_reader_read(reader) == PACKET_READ_NORMAL)
88 argv_array_push(&server_capabilities_v2, reader->line);
89
90 if (reader->status != PACKET_READ_FLUSH)
91 die("expected flush after capabilities");
92}
93
ad6ac124 94enum protocol_version discover_version(struct packet_reader *reader)
7e3e479b
BW
95{
96 enum protocol_version version = protocol_unknown_version;
97
98 /*
99 * Peek the first line of the server's response to
100 * determine the protocol version the server is speaking.
101 */
102 switch (packet_reader_peek(reader)) {
103 case PACKET_READ_EOF:
104 die_initial_contact(0);
105 case PACKET_READ_FLUSH:
106 case PACKET_READ_DELIM:
107 version = protocol_v0;
108 break;
109 case PACKET_READ_NORMAL:
110 version = determine_protocol_version_client(reader->line);
111 break;
112 }
113
114 switch (version) {
8f6982b4 115 case protocol_v2:
e52449b6 116 process_capabilities_v2(reader);
8f6982b4 117 break;
7e3e479b
BW
118 case protocol_v1:
119 /* Read the peeked version line */
120 packet_reader_read(reader);
121 break;
122 case protocol_v0:
123 break;
124 case protocol_unknown_version:
125 BUG("unknown protocol version");
126 }
127
128 return version;
129}
130
a45b5f05
JH
131static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
132{
133 char *sym, *target;
134 struct string_list_item *item;
135
136 if (!len)
137 return; /* just "symref" */
138 /* e.g. "symref=HEAD:refs/heads/master" */
5c0b13f8 139 sym = xmemdupz(val, len);
a45b5f05
JH
140 target = strchr(sym, ':');
141 if (!target)
142 /* just "symref=something" */
143 goto reject;
144 *(target++) = '\0';
145 if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
146 check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
147 /* "symref=bogus:pair */
148 goto reject;
ef4fe561 149 item = string_list_append_nodup(symref, sym);
a45b5f05
JH
150 item->util = target;
151 return;
152reject:
153 free(sym);
154 return;
155}
156
157static void annotate_refs_with_symref_info(struct ref *ref)
158{
159 struct string_list symref = STRING_LIST_INIT_DUP;
e52449b6 160 const char *feature_list = server_capabilities_v1;
a45b5f05
JH
161
162 while (feature_list) {
163 int len;
164 const char *val;
165
166 val = parse_feature_value(feature_list, "symref", &len);
167 if (!val)
168 break;
169 parse_one_symref_info(&symref, val, len);
170 feature_list = val + 1;
171 }
3383e199 172 string_list_sort(&symref);
a45b5f05
JH
173
174 for (; ref; ref = ref->next) {
175 struct string_list_item *item;
176 item = string_list_lookup(&symref, ref->name);
177 if (!item)
178 continue;
179 ref->symref = xstrdup((char *)item->util);
180 }
181 string_list_clear(&symref, 0);
182}
183
7e3e479b 184static void process_capabilities(const char *line, int *len)
2609043d 185{
7e3e479b 186 int nul_location = strlen(line);
0cd83283
JT
187 if (nul_location == *len)
188 return;
e52449b6 189 server_capabilities_v1 = xstrdup(line + nul_location + 1);
0cd83283
JT
190 *len = nul_location;
191}
192
7e3e479b 193static int process_dummy_ref(const char *line)
0cd83283
JT
194{
195 struct object_id oid;
196 const char *name;
197
7e3e479b 198 if (parse_oid_hex(line, &oid, &name))
0cd83283
JT
199 return 0;
200 if (*name != ' ')
201 return 0;
202 name++;
203
204 return !oidcmp(&null_oid, &oid) && !strcmp(name, "capabilities^{}");
205}
206
7e3e479b 207static void check_no_capabilities(const char *line, int len)
0cd83283 208{
7e3e479b 209 if (strlen(line) != len)
0cd83283 210 warning("Ignoring capabilities after first line '%s'",
7e3e479b 211 line + strlen(line));
0cd83283
JT
212}
213
7e3e479b
BW
214static int process_ref(const char *line, int len, struct ref ***list,
215 unsigned int flags, struct oid_array *extra_have)
0cd83283
JT
216{
217 struct object_id old_oid;
218 const char *name;
219
7e3e479b 220 if (parse_oid_hex(line, &old_oid, &name))
0cd83283
JT
221 return 0;
222 if (*name != ' ')
223 return 0;
224 name++;
225
226 if (extra_have && !strcmp(name, ".have")) {
227 oid_array_append(extra_have, &old_oid);
228 } else if (!strcmp(name, "capabilities^{}")) {
229 die("protocol error: unexpected capabilities^{}");
230 } else if (check_ref(name, flags)) {
231 struct ref *ref = alloc_ref(name);
232 oidcpy(&ref->old_oid, &old_oid);
233 **list = ref;
234 *list = &ref->next;
235 }
7e3e479b 236 check_no_capabilities(line, len);
0cd83283
JT
237 return 1;
238}
239
7e3e479b
BW
240static int process_shallow(const char *line, int len,
241 struct oid_array *shallow_points)
0cd83283
JT
242{
243 const char *arg;
244 struct object_id old_oid;
245
7e3e479b 246 if (!skip_prefix(line, "shallow ", &arg))
0cd83283
JT
247 return 0;
248
249 if (get_oid_hex(arg, &old_oid))
250 die("protocol error: expected shallow sha-1, got '%s'", arg);
251 if (!shallow_points)
252 die("repository on the other end cannot be shallow");
253 oid_array_append(shallow_points, &old_oid);
7e3e479b 254 check_no_capabilities(line, len);
0cd83283
JT
255 return 1;
256}
257
7e3e479b
BW
258enum get_remote_heads_state {
259 EXPECTING_FIRST_REF = 0,
260 EXPECTING_REF,
261 EXPECTING_SHALLOW,
262 EXPECTING_DONE,
263};
264
d1c133f5
LT
265/*
266 * Read all the refs from the other end
267 */
ad6ac124 268struct ref **get_remote_heads(struct packet_reader *reader,
85edf4f5 269 struct ref **list, unsigned int flags,
910650d2 270 struct oid_array *extra_have,
271 struct oid_array *shallow_points)
d1c133f5 272{
a45b5f05 273 struct ref **orig_list = list;
7e3e479b
BW
274 int len = 0;
275 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
7e3e479b 276 const char *arg;
55e4f936 277
d1c133f5 278 *list = NULL;
1a7141ff 279
7e3e479b 280 while (state != EXPECTING_DONE) {
ad6ac124 281 switch (packet_reader_read(reader)) {
7e3e479b
BW
282 case PACKET_READ_EOF:
283 die_initial_contact(1);
284 case PACKET_READ_NORMAL:
ad6ac124
BW
285 len = reader->pktlen;
286 if (len > 4 && skip_prefix(reader->line, "ERR ", &arg))
7e3e479b
BW
287 die("remote error: %s", arg);
288 break;
289 case PACKET_READ_FLUSH:
290 state = EXPECTING_DONE;
291 break;
292 case PACKET_READ_DELIM:
293 die("invalid packet");
294 }
295
0cd83283
JT
296 switch (state) {
297 case EXPECTING_FIRST_REF:
ad6ac124
BW
298 process_capabilities(reader->line, &len);
299 if (process_dummy_ref(reader->line)) {
0cd83283
JT
300 state = EXPECTING_SHALLOW;
301 break;
302 }
303 state = EXPECTING_REF;
304 /* fallthrough */
305 case EXPECTING_REF:
ad6ac124 306 if (process_ref(reader->line, len, &list, flags, extra_have))
0cd83283
JT
307 break;
308 state = EXPECTING_SHALLOW;
309 /* fallthrough */
310 case EXPECTING_SHALLOW:
ad6ac124 311 if (process_shallow(reader->line, len, shallow_points))
0cd83283 312 break;
ad6ac124 313 die("protocol error: unexpected '%s'", reader->line);
7e3e479b
BW
314 case EXPECTING_DONE:
315 break;
211b5f9e 316 }
d1c133f5 317 }
a45b5f05
JH
318
319 annotate_refs_with_symref_info(*orig_list);
320
d1c133f5
LT
321 return list;
322}
323
e52449b6
BW
324/* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
325static int process_ref_v2(const char *line, struct ref ***list)
326{
327 int ret = 1;
328 int i = 0;
329 struct object_id old_oid;
330 struct ref *ref;
331 struct string_list line_sections = STRING_LIST_INIT_DUP;
332 const char *end;
333
334 /*
335 * Ref lines have a number of fields which are space deliminated. The
336 * first field is the OID of the ref. The second field is the ref
337 * name. Subsequent fields (symref-target and peeled) are optional and
338 * don't have a particular order.
339 */
340 if (string_list_split(&line_sections, line, ' ', -1) < 2) {
341 ret = 0;
342 goto out;
343 }
344
345 if (parse_oid_hex(line_sections.items[i++].string, &old_oid, &end) ||
346 *end) {
347 ret = 0;
348 goto out;
349 }
350
351 ref = alloc_ref(line_sections.items[i++].string);
352
353 oidcpy(&ref->old_oid, &old_oid);
354 **list = ref;
355 *list = &ref->next;
356
357 for (; i < line_sections.nr; i++) {
358 const char *arg = line_sections.items[i].string;
359 if (skip_prefix(arg, "symref-target:", &arg))
360 ref->symref = xstrdup(arg);
361
362 if (skip_prefix(arg, "peeled:", &arg)) {
363 struct object_id peeled_oid;
364 char *peeled_name;
365 struct ref *peeled;
366 if (parse_oid_hex(arg, &peeled_oid, &end) || *end) {
367 ret = 0;
368 goto out;
369 }
370
371 peeled_name = xstrfmt("%s^{}", ref->name);
372 peeled = alloc_ref(peeled_name);
373
374 oidcpy(&peeled->old_oid, &peeled_oid);
375 **list = peeled;
376 *list = &peeled->next;
377
378 free(peeled_name);
379 }
380 }
381
382out:
383 string_list_clear(&line_sections, 0);
384 return ret;
385}
386
387struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
388 struct ref **list, int for_push,
389 const struct argv_array *ref_prefixes)
390{
391 int i;
392 *list = NULL;
393
394 if (server_supports_v2("ls-refs", 1))
395 packet_write_fmt(fd_out, "command=ls-refs\n");
396
397 if (server_supports_v2("agent", 0))
398 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
399
400 packet_delim(fd_out);
401 /* When pushing we don't want to request the peeled tags */
402 if (!for_push)
403 packet_write_fmt(fd_out, "peel\n");
404 packet_write_fmt(fd_out, "symrefs\n");
405 for (i = 0; ref_prefixes && i < ref_prefixes->argc; i++) {
406 packet_write_fmt(fd_out, "ref-prefix %s\n",
407 ref_prefixes->argv[i]);
408 }
409 packet_flush(fd_out);
410
411 /* Process response from server */
412 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
413 if (!process_ref_v2(reader->line, &list))
414 die("invalid ls-refs response: %s", reader->line);
415 }
416
417 if (reader->status != PACKET_READ_FLUSH)
418 die("expected flush after ref listing");
419
420 return list;
421}
422
5d54cffc 423static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
f47182c8
JH
424{
425 int len;
426
427 if (!feature_list)
428 return NULL;
429
430 len = strlen(feature);
431 while (*feature_list) {
432 const char *found = strstr(feature_list, feature);
433 if (!found)
434 return NULL;
94427108
JK
435 if (feature_list == found || isspace(found[-1])) {
436 const char *value = found + len;
437 /* feature with no value (e.g., "thin-pack") */
438 if (!*value || isspace(*value)) {
439 if (lenp)
440 *lenp = 0;
441 return value;
442 }
443 /* feature with a value (e.g., "agent=git/1.2.3") */
444 else if (*value == '=') {
445 value++;
446 if (lenp)
447 *lenp = strcspn(value, " \t\n");
448 return value;
449 }
450 /*
451 * otherwise we matched a substring of another feature;
452 * keep looking
453 */
454 }
f47182c8
JH
455 feature_list = found + 1;
456 }
457 return NULL;
211b5f9e
JS
458}
459
94427108
JK
460int parse_feature_request(const char *feature_list, const char *feature)
461{
462 return !!parse_feature_value(feature_list, feature, NULL);
463}
464
465const char *server_feature_value(const char *feature, int *len)
466{
e52449b6 467 return parse_feature_value(server_capabilities_v1, feature, len);
94427108
JK
468}
469
470int server_supports(const char *feature)
471{
472 return !!server_feature_value(feature, NULL);
473}
474
2386d658
LT
475enum protocol {
476 PROTO_LOCAL = 1,
c59ab2e5 477 PROTO_FILE,
2386d658 478 PROTO_SSH,
4b05548f 479 PROTO_GIT
2386d658
LT
480};
481
c59ab2e5
TB
482int url_is_local_not_ssh(const char *url)
483{
484 const char *colon = strchr(url, ':');
485 const char *slash = strchr(url, '/');
486 return !colon || (slash && slash < colon) ||
487 has_dos_drive_prefix(url);
488}
489
5610b7c0
TB
490static const char *prot_name(enum protocol protocol)
491{
492 switch (protocol) {
493 case PROTO_LOCAL:
c59ab2e5 494 case PROTO_FILE:
5610b7c0
TB
495 return "file";
496 case PROTO_SSH:
497 return "ssh";
498 case PROTO_GIT:
499 return "git";
500 default:
83e6bda3 501 return "unknown protocol";
5610b7c0
TB
502 }
503}
504
2386d658
LT
505static enum protocol get_protocol(const char *name)
506{
507 if (!strcmp(name, "ssh"))
508 return PROTO_SSH;
509 if (!strcmp(name, "git"))
510 return PROTO_GIT;
07c7782c 511 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
c05186cc 512 return PROTO_SSH;
07c7782c 513 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
c05186cc 514 return PROTO_SSH;
72a4f4b6 515 if (!strcmp(name, "file"))
c59ab2e5 516 return PROTO_FILE;
2386d658
LT
517 die("I don't handle protocol '%s'", name);
518}
519
86ceb337
TB
520static char *host_end(char **hoststart, int removebrackets)
521{
522 char *host = *hoststart;
523 char *end;
524 char *start = strstr(host, "@[");
525 if (start)
526 start++; /* Jump over '@' */
527 else
528 start = host;
529 if (start[0] == '[') {
530 end = strchr(start + 1, ']');
531 if (end) {
532 if (removebrackets) {
533 *end = 0;
534 memmove(start, start + 1, end - start);
535 end++;
536 }
537 } else
538 end = host;
539 } else
540 end = host;
541 return end;
542}
543
5ba88448
YH
544#define STR_(s) # s
545#define STR(s) STR_(s)
2386d658 546
72a534da
ML
547static void get_host_and_port(char **host, const char **port)
548{
549 char *colon, *end;
86ceb337 550 end = host_end(host, 1);
72a534da 551 colon = strchr(end, ':');
72a534da 552 if (colon) {
86ceb337
TB
553 long portnr = strtol(colon + 1, &end, 10);
554 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
555 *colon = 0;
556 *port = colon + 1;
6b6c5f7a
TB
557 } else if (!colon[1]) {
558 *colon = 0;
86ceb337 559 }
72a534da
ML
560 }
561}
562
e47a8583
EW
563static void enable_keepalive(int sockfd)
564{
565 int ka = 1;
566
567 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
568 fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
569 strerror(errno));
570}
571
49744d63 572#ifndef NO_IPV6
4c505f71 573
ba505322
AR
574static const char *ai_name(const struct addrinfo *ai)
575{
785a9857
BK
576 static char addr[NI_MAXHOST];
577 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
578 NI_NUMERICHOST) != 0)
5096d490 579 xsnprintf(addr, sizeof(addr), "(unknown)");
785a9857 580
ba505322
AR
581 return addr;
582}
583
5ad312be
JL
584/*
585 * Returns a connected socket() fd, or else die()s.
586 */
7841ce79 587static int git_tcp_connect_sock(char *host, int flags)
2386d658 588{
63a995b6
DZ
589 struct strbuf error_message = STRBUF_INIT;
590 int sockfd = -1;
554fe20d 591 const char *port = STR(DEFAULT_GIT_PORT);
5ba88448
YH
592 struct addrinfo hints, *ai0, *ai;
593 int gai;
ba505322 594 int cnt = 0;
5ba88448 595
72a534da
ML
596 get_host_and_port(&host, &port);
597 if (!*port)
598 port = "<none>";
5ba88448
YH
599
600 memset(&hints, 0, sizeof(hints));
c915f11e
EW
601 if (flags & CONNECT_IPV4)
602 hints.ai_family = AF_INET;
603 else if (flags & CONNECT_IPV6)
604 hints.ai_family = AF_INET6;
5ba88448
YH
605 hints.ai_socktype = SOCK_STREAM;
606 hints.ai_protocol = IPPROTO_TCP;
607
7841ce79
MT
608 if (flags & CONNECT_VERBOSE)
609 fprintf(stderr, "Looking up %s ... ", host);
610
5ba88448
YH
611 gai = getaddrinfo(host, port, &hints, &ai);
612 if (gai)
608d48b2 613 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
5ba88448 614
7841ce79
MT
615 if (flags & CONNECT_VERBOSE)
616 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
617
e08afecd 618 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
5ad312be
JL
619 sockfd = socket(ai->ai_family,
620 ai->ai_socktype, ai->ai_protocol);
63a995b6
DZ
621 if ((sockfd < 0) ||
622 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
623 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
624 host, cnt, ai_name(ai), strerror(errno));
625 if (0 <= sockfd)
626 close(sockfd);
5ba88448
YH
627 sockfd = -1;
628 continue;
2386d658 629 }
ba505322
AR
630 if (flags & CONNECT_VERBOSE)
631 fprintf(stderr, "%s ", ai_name(ai));
5ba88448 632 break;
2386d658
LT
633 }
634
5ba88448 635 freeaddrinfo(ai0);
2386d658 636
2386d658 637 if (sockfd < 0)
63a995b6 638 die("unable to connect to %s:\n%s", host, error_message.buf);
5ba88448 639
e47a8583
EW
640 enable_keepalive(sockfd);
641
7841ce79
MT
642 if (flags & CONNECT_VERBOSE)
643 fprintf(stderr, "done.\n");
644
63a995b6
DZ
645 strbuf_release(&error_message);
646
5ad312be 647 return sockfd;
2386d658
LT
648}
649
49744d63 650#else /* NO_IPV6 */
4c505f71 651
5ad312be
JL
652/*
653 * Returns a connected socket() fd, or else die()s.
654 */
7841ce79 655static int git_tcp_connect_sock(char *host, int flags)
4c505f71 656{
7203a2d1
EFL
657 struct strbuf error_message = STRBUF_INIT;
658 int sockfd = -1;
72a534da
ML
659 const char *port = STR(DEFAULT_GIT_PORT);
660 char *ep;
4c505f71
PA
661 struct hostent *he;
662 struct sockaddr_in sa;
663 char **ap;
664 unsigned int nport;
ba505322 665 int cnt;
4c505f71 666
72a534da 667 get_host_and_port(&host, &port);
4c505f71 668
7841ce79
MT
669 if (flags & CONNECT_VERBOSE)
670 fprintf(stderr, "Looking up %s ... ", host);
671
4c505f71
PA
672 he = gethostbyname(host);
673 if (!he)
674 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
675 nport = strtoul(port, &ep, 10);
676 if ( ep == port || *ep ) {
677 /* Not numeric */
678 struct servent *se = getservbyname(port,"tcp");
679 if ( !se )
d7530708 680 die("Unknown port %s", port);
4c505f71
PA
681 nport = se->s_port;
682 }
683
7841ce79
MT
684 if (flags & CONNECT_VERBOSE)
685 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
686
ba505322 687 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
4c505f71
PA
688 memset(&sa, 0, sizeof sa);
689 sa.sin_family = he->h_addrtype;
6573faff 690 sa.sin_port = htons(nport);
c6164218 691 memcpy(&sa.sin_addr, *ap, he->h_length);
4c505f71 692
7203a2d1
EFL
693 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
694 if ((sockfd < 0) ||
695 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
696 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
ba505322
AR
697 host,
698 cnt,
699 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
7203a2d1
EFL
700 strerror(errno));
701 if (0 <= sockfd)
702 close(sockfd);
4c505f71
PA
703 sockfd = -1;
704 continue;
705 }
ba505322
AR
706 if (flags & CONNECT_VERBOSE)
707 fprintf(stderr, "%s ",
708 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
4c505f71
PA
709 break;
710 }
711
712 if (sockfd < 0)
7203a2d1 713 die("unable to connect to %s:\n%s", host, error_message.buf);
4c505f71 714
e47a8583
EW
715 enable_keepalive(sockfd);
716
7841ce79
MT
717 if (flags & CONNECT_VERBOSE)
718 fprintf(stderr, "done.\n");
719
5ad312be
JL
720 return sockfd;
721}
722
723#endif /* NO_IPV6 */
724
725
8e349780
JN
726/*
727 * Dummy child_process returned by git_connect() if the transport protocol
728 * does not need fork(2).
729 */
730static struct child_process no_fork = CHILD_PROCESS_INIT;
731
732int git_connection_is_socket(struct child_process *conn)
733{
734 return conn == &no_fork;
735}
736
737static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
5ad312be 738{
7841ce79 739 int sockfd = git_tcp_connect_sock(host, flags);
5ad312be 740
4c505f71 741 fd[0] = sockfd;
ec587fde 742 fd[1] = dup(sockfd);
8e349780
JN
743
744 return &no_fork;
4c505f71
PA
745}
746
4c505f71 747
96f1e58f 748static char *git_proxy_command;
f8014776 749
ef90d6d4
JS
750static int git_proxy_command_options(const char *var, const char *value,
751 void *cb)
f8014776 752{
e814bc4d 753 if (!strcmp(var, "core.gitproxy")) {
c3df8568
YH
754 const char *for_pos;
755 int matchlen = -1;
756 int hostlen;
15112c95
EFL
757 const char *rhost_name = cb;
758 int rhost_len = strlen(rhost_name);
c3df8568 759
e814bc4d 760 if (git_proxy_command)
f8014776 761 return 0;
c64b9ad0
JH
762 if (!value)
763 return config_error_nonbool(var);
e814bc4d
JH
764 /* [core]
765 * ;# matches www.kernel.org as well
766 * gitproxy = netcatter-1 for kernel.org
767 * gitproxy = netcatter-2 for sample.xz
768 * gitproxy = netcatter-default
769 */
c3df8568 770 for_pos = strstr(value, " for ");
e814bc4d
JH
771 if (!for_pos)
772 /* matches everybody */
773 matchlen = strlen(value);
774 else {
775 hostlen = strlen(for_pos + 5);
776 if (rhost_len < hostlen)
777 matchlen = -1;
778 else if (!strncmp(for_pos + 5,
779 rhost_name + rhost_len - hostlen,
780 hostlen) &&
781 ((rhost_len == hostlen) ||
782 rhost_name[rhost_len - hostlen -1] == '.'))
783 matchlen = for_pos - value;
784 else
785 matchlen = -1;
786 }
787 if (0 <= matchlen) {
788 /* core.gitproxy = none for kernel.org */
a6080a0a 789 if (matchlen == 4 &&
e814bc4d
JH
790 !memcmp(value, "none", 4))
791 matchlen = 0;
182af834 792 git_proxy_command = xmemdupz(value, matchlen);
f8014776 793 }
e814bc4d 794 return 0;
f8014776
PC
795 }
796
ef90d6d4 797 return git_default_config(var, value, cb);
f8014776
PC
798}
799
e814bc4d 800static int git_use_proxy(const char *host)
f8014776
PC
801{
802 git_proxy_command = getenv("GIT_PROXY_COMMAND");
15112c95 803 git_config(git_proxy_command_options, (void*)host);
e814bc4d 804 return (git_proxy_command && *git_proxy_command);
f8014776
PC
805}
806
5cbf8246 807static struct child_process *git_proxy_connect(int fd[2], char *host)
f8014776 808{
554fe20d 809 const char *port = STR(DEFAULT_GIT_PORT);
5cbf8246 810 struct child_process *proxy;
f8014776 811
72a534da 812 get_host_and_port(&host, &port);
f8014776 813
3be4cf09
JK
814 if (looks_like_command_line_option(host))
815 die("strange hostname '%s' blocked", host);
816 if (looks_like_command_line_option(port))
817 die("strange port '%s' blocked", port);
818
483bbd4e
RS
819 proxy = xmalloc(sizeof(*proxy));
820 child_process_init(proxy);
1823bea1
JK
821 argv_array_push(&proxy->args, git_proxy_command);
822 argv_array_push(&proxy->args, host);
823 argv_array_push(&proxy->args, port);
5cbf8246
JK
824 proxy->in = -1;
825 proxy->out = -1;
826 if (start_command(proxy))
1823bea1 827 die("cannot start proxy %s", git_proxy_command);
5cbf8246
JK
828 fd[0] = proxy->out; /* read from proxy stdout */
829 fd[1] = proxy->in; /* write to proxy stdin */
830 return proxy;
f8014776
PC
831}
832
86ceb337 833static char *get_port(char *host)
2e776665
LT
834{
835 char *end;
86ceb337
TB
836 char *p = strchr(host, ':');
837
2e776665 838 if (p) {
8f148253
RS
839 long port = strtol(p + 1, &end, 10);
840 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
86ceb337
TB
841 *p = '\0';
842 return p+1;
2e776665
LT
843 }
844 }
845
846 return NULL;
847}
848
f7192598 849/*
cabc3c12
JS
850 * Extract protocol and relevant parts from the specified connection URL.
851 * The caller must free() the returned strings.
f7192598 852 */
cabc3c12 853static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
83b05875 854 char **ret_path)
f7192598 855{
9d2e9420 856 char *url;
8e76bf3f 857 char *host, *path;
356bece0 858 char *end;
c59ab2e5 859 int separator = '/';
faea9ccb 860 enum protocol protocol = PROTO_LOCAL;
f0b7367c 861
9d2e9420
JK
862 if (is_url(url_orig))
863 url = url_decode(url_orig);
864 else
865 url = xstrdup(url_orig);
866
faea9ccb 867 host = strstr(url, "://");
eeefa7c9 868 if (host) {
faea9ccb
AE
869 *host = '\0';
870 protocol = get_protocol(url);
871 host += 3;
356bece0 872 } else {
f7192598 873 host = url;
c59ab2e5
TB
874 if (!url_is_local_not_ssh(url)) {
875 protocol = PROTO_SSH;
876 separator = ':';
877 }
356bece0
YH
878 }
879
9aa5053d 880 /*
83b05875
TB
881 * Don't do destructive transforms as protocol code does
882 * '[]' unwrapping in get_host_and_port()
9aa5053d 883 */
86ceb337 884 end = host_end(&host, 0);
356bece0 885
c59ab2e5 886 if (protocol == PROTO_LOCAL)
72a4f4b6 887 path = end;
c59ab2e5
TB
888 else if (protocol == PROTO_FILE && has_dos_drive_prefix(end))
889 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
890 else
891 path = strchr(end, separator);
2386d658 892
faea9ccb
AE
893 if (!path || !*path)
894 die("No path specified. See 'man git-pull' for valid url syntax");
895
896 /*
897 * null-terminate hostname and point path to ~ for URL's like this:
898 * ssh://host.xz/~user/repo
899 */
c59ab2e5
TB
900
901 end = path; /* Need to \0 terminate host here */
902 if (separator == ':')
903 path++; /* path starts after ':' */
904 if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
faea9ccb
AE
905 if (path[1] == '~')
906 path++;
faea9ccb
AE
907 }
908
c59ab2e5
TB
909 path = xstrdup(path);
910 *end = '\0';
911
cabc3c12 912 *ret_host = xstrdup(host);
c59ab2e5 913 *ret_path = path;
cabc3c12
JS
914 free(url);
915 return protocol;
916}
917
3c8ede3f
NTND
918static const char *get_ssh_command(void)
919{
920 const char *ssh;
921
922 if ((ssh = getenv("GIT_SSH_COMMAND")))
923 return ssh;
924
925 if (!git_config_get_string_const("core.sshcommand", &ssh))
926 return ssh;
927
928 return NULL;
929}
930
94b8ae5a 931enum ssh_variant {
0da0e49b 932 VARIANT_AUTO,
94b8ae5a
BW
933 VARIANT_SIMPLE,
934 VARIANT_SSH,
935 VARIANT_PLINK,
936 VARIANT_PUTTY,
937 VARIANT_TORTOISEPLINK,
938};
939
0da0e49b 940static void override_ssh_variant(enum ssh_variant *ssh_variant)
e2824e47 941{
94b8ae5a 942 const char *variant = getenv("GIT_SSH_VARIANT");
486c8e8c 943
94b8ae5a 944 if (!variant && git_config_get_string_const("ssh.variant", &variant))
0da0e49b 945 return;
486c8e8c 946
0da0e49b
JN
947 if (!strcmp(variant, "auto"))
948 *ssh_variant = VARIANT_AUTO;
949 else if (!strcmp(variant, "plink"))
94b8ae5a
BW
950 *ssh_variant = VARIANT_PLINK;
951 else if (!strcmp(variant, "putty"))
952 *ssh_variant = VARIANT_PUTTY;
953 else if (!strcmp(variant, "tortoiseplink"))
954 *ssh_variant = VARIANT_TORTOISEPLINK;
955 else if (!strcmp(variant, "simple"))
956 *ssh_variant = VARIANT_SIMPLE;
957 else
958 *ssh_variant = VARIANT_SSH;
486c8e8c
JH
959}
960
94b8ae5a
BW
961static enum ssh_variant determine_ssh_variant(const char *ssh_command,
962 int is_cmdline)
486c8e8c 963{
0da0e49b 964 enum ssh_variant ssh_variant = VARIANT_AUTO;
486c8e8c 965 const char *variant;
e2824e47
JS
966 char *p = NULL;
967
0da0e49b
JN
968 override_ssh_variant(&ssh_variant);
969
970 if (ssh_variant != VARIANT_AUTO)
94b8ae5a 971 return ssh_variant;
486c8e8c
JH
972
973 if (!is_cmdline) {
e2824e47
JS
974 p = xstrdup(ssh_command);
975 variant = basename(p);
976 } else {
977 const char **ssh_argv;
978
979 p = xstrdup(ssh_command);
22e5ae5c 980 if (split_cmdline(p, &ssh_argv) > 0) {
e2824e47
JS
981 variant = basename((char *)ssh_argv[0]);
982 /*
983 * At this point, variant points into the buffer
984 * referenced by p, hence we do not need ssh_argv
985 * any longer.
986 */
987 free(ssh_argv);
5d2993b6
JK
988 } else {
989 free(p);
94b8ae5a 990 return ssh_variant;
5d2993b6 991 }
e2824e47
JS
992 }
993
94b8ae5a
BW
994 if (!strcasecmp(variant, "ssh") ||
995 !strcasecmp(variant, "ssh.exe"))
996 ssh_variant = VARIANT_SSH;
997 else if (!strcasecmp(variant, "plink") ||
998 !strcasecmp(variant, "plink.exe"))
999 ssh_variant = VARIANT_PLINK;
e2824e47 1000 else if (!strcasecmp(variant, "tortoiseplink") ||
94b8ae5a
BW
1001 !strcasecmp(variant, "tortoiseplink.exe"))
1002 ssh_variant = VARIANT_TORTOISEPLINK;
1003
e2824e47 1004 free(p);
94b8ae5a 1005 return ssh_variant;
e2824e47
JS
1006}
1007
2ac67cb6
JN
1008/*
1009 * Open a connection using Git's native protocol.
1010 *
1011 * The caller is responsible for freeing hostandport, but this function may
1012 * modify it (for example, to truncate it to remove the port part).
1013 */
1014static struct child_process *git_connect_git(int fd[2], char *hostandport,
1015 const char *path, const char *prog,
1016 int flags)
1017{
1018 struct child_process *conn;
1019 struct strbuf request = STRBUF_INIT;
1020 /*
1021 * Set up virtual host information based on where we will
1022 * connect, unless the user has overridden us in
1023 * the environment.
1024 */
1025 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1026 if (target_host)
1027 target_host = xstrdup(target_host);
1028 else
1029 target_host = xstrdup(hostandport);
1030
1031 transport_check_allowed("git");
1032
233cd282
JN
1033 /*
1034 * These underlying connection commands die() if they
2ac67cb6
JN
1035 * cannot connect.
1036 */
1037 if (git_use_proxy(hostandport))
1038 conn = git_proxy_connect(fd, hostandport);
1039 else
1040 conn = git_tcp_connect(fd, hostandport, flags);
1041 /*
1042 * Separate original protocol components prog and path
1043 * from extended host header with a NUL byte.
1044 *
1045 * Note: Do not add any other headers here! Doing so
1046 * will cause older git-daemon servers to crash.
1047 */
1048 strbuf_addf(&request,
1049 "%s %s%chost=%s%c",
1050 prog, path, 0,
1051 target_host, 0);
1052
1053 /* If using a new version put that stuff here after a second null byte */
1054 if (get_protocol_version_config() > 0) {
1055 strbuf_addch(&request, '\0');
1056 strbuf_addf(&request, "version=%d%c",
1057 get_protocol_version_config(), '\0');
1058 }
1059
1060 packet_write(fd[1], request.buf, request.len);
1061
1062 free(target_host);
1063 strbuf_release(&request);
1064 return conn;
1065}
1066
957e2ad2
JN
1067/*
1068 * Append the appropriate environment variables to `env` and options to
1069 * `args` for running ssh in Git's SSH-tunneled transport.
1070 */
1071static void push_ssh_options(struct argv_array *args, struct argv_array *env,
1072 enum ssh_variant variant, const char *port,
1073 int flags)
1074{
1075 if (variant == VARIANT_SSH &&
1076 get_protocol_version_config() > 0) {
1077 argv_array_push(args, "-o");
1078 argv_array_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1079 argv_array_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1080 get_protocol_version_config());
1081 }
1082
a3f5b66f
JN
1083 if (flags & CONNECT_IPV4) {
1084 switch (variant) {
1085 case VARIANT_AUTO:
1086 BUG("VARIANT_AUTO passed to push_ssh_options");
1087 case VARIANT_SIMPLE:
1088 die("ssh variant 'simple' does not support -4");
1089 case VARIANT_SSH:
1090 case VARIANT_PLINK:
1091 case VARIANT_PUTTY:
1092 case VARIANT_TORTOISEPLINK:
957e2ad2 1093 argv_array_push(args, "-4");
a3f5b66f
JN
1094 }
1095 } else if (flags & CONNECT_IPV6) {
1096 switch (variant) {
1097 case VARIANT_AUTO:
1098 BUG("VARIANT_AUTO passed to push_ssh_options");
1099 case VARIANT_SIMPLE:
1100 die("ssh variant 'simple' does not support -6");
1101 case VARIANT_SSH:
1102 case VARIANT_PLINK:
1103 case VARIANT_PUTTY:
1104 case VARIANT_TORTOISEPLINK:
957e2ad2 1105 argv_array_push(args, "-6");
a3f5b66f 1106 }
957e2ad2
JN
1107 }
1108
1109 if (variant == VARIANT_TORTOISEPLINK)
1110 argv_array_push(args, "-batch");
1111
3fa5e0d0
JN
1112 if (port) {
1113 switch (variant) {
1114 case VARIANT_AUTO:
1115 BUG("VARIANT_AUTO passed to push_ssh_options");
1116 case VARIANT_SIMPLE:
1117 die("ssh variant 'simple' does not support setting port");
1118 case VARIANT_SSH:
957e2ad2 1119 argv_array_push(args, "-p");
3fa5e0d0
JN
1120 break;
1121 case VARIANT_PLINK:
1122 case VARIANT_PUTTY:
1123 case VARIANT_TORTOISEPLINK:
957e2ad2 1124 argv_array_push(args, "-P");
3fa5e0d0 1125 }
957e2ad2
JN
1126
1127 argv_array_push(args, port);
1128 }
1129}
1130
fce54ce4
JN
1131/* Prepare a child_process for use by Git's SSH-tunneled transport. */
1132static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
1133 const char *port, int flags)
1134{
1135 const char *ssh;
1136 enum ssh_variant variant;
1137
1138 if (looks_like_command_line_option(ssh_host))
1139 die("strange hostname '%s' blocked", ssh_host);
1140
1141 ssh = get_ssh_command();
1142 if (ssh) {
1143 variant = determine_ssh_variant(ssh, 1);
1144 } else {
1145 /*
1146 * GIT_SSH is the no-shell version of
1147 * GIT_SSH_COMMAND (and must remain so for
1148 * historical compatibility).
1149 */
1150 conn->use_shell = 0;
1151
1152 ssh = getenv("GIT_SSH");
1153 if (!ssh)
1154 ssh = "ssh";
1155 variant = determine_ssh_variant(ssh, 0);
1156 }
1157
0da0e49b
JN
1158 if (variant == VARIANT_AUTO) {
1159 struct child_process detect = CHILD_PROCESS_INIT;
1160
1161 detect.use_shell = conn->use_shell;
1162 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1163
1164 argv_array_push(&detect.args, ssh);
1165 argv_array_push(&detect.args, "-G");
1166 push_ssh_options(&detect.args, &detect.env_array,
1167 VARIANT_SSH, port, flags);
1168 argv_array_push(&detect.args, ssh_host);
1169
1170 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1171 }
1172
fce54ce4 1173 argv_array_push(&conn->args, ssh);
957e2ad2 1174 push_ssh_options(&conn->args, &conn->env_array, variant, port, flags);
fce54ce4
JN
1175 argv_array_push(&conn->args, ssh_host);
1176}
1177
cabc3c12 1178/*
8e349780
JN
1179 * This returns the dummy child_process `no_fork` if the transport protocol
1180 * does not need fork(2), or a struct child_process object if it does. Once
1181 * done, finish the connection with finish_connect() with the value returned
1182 * from this function (it is safe to call finish_connect() with NULL to
1183 * support the former case).
cabc3c12
JS
1184 *
1185 * If it returns, the connect is successful; it just dies on errors (this
1186 * will hopefully be changed in a libification effort, to return NULL when
1187 * the connection failed).
1188 */
1189struct child_process *git_connect(int fd[2], const char *url,
1190 const char *prog, int flags)
1191{
a2036d7e 1192 char *hostandport, *path;
8e349780 1193 struct child_process *conn;
cabc3c12 1194 enum protocol protocol;
cabc3c12
JS
1195
1196 /* Without this we cannot rely on waitpid() to tell
1197 * what happened to our children.
2e776665 1198 */
cabc3c12 1199 signal(SIGCHLD, SIG_DFL);
2e776665 1200
a2036d7e 1201 protocol = parse_connect_url(url, &hostandport, &path);
3f55ccab 1202 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
5610b7c0
TB
1203 printf("Diag: url=%s\n", url ? url : "NULL");
1204 printf("Diag: protocol=%s\n", prot_name(protocol));
a2036d7e 1205 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
5610b7c0 1206 printf("Diag: path=%s\n", path ? path : "NULL");
a2036d7e
TB
1207 conn = NULL;
1208 } else if (protocol == PROTO_GIT) {
2ac67cb6 1209 conn = git_connect_git(fd, hostandport, path, prog, flags);
a2036d7e 1210 } else {
f1399291 1211 struct strbuf cmd = STRBUF_INIT;
0c2f0d27 1212 const char *const *var;
f1399291 1213
483bbd4e
RS
1214 conn = xmalloc(sizeof(*conn));
1215 child_process_init(conn);
a2036d7e 1216
aeeb2d49
JK
1217 if (looks_like_command_line_option(path))
1218 die("strange pathname '%s' blocked", path);
1219
a2036d7e
TB
1220 strbuf_addstr(&cmd, prog);
1221 strbuf_addch(&cmd, ' ');
1222 sq_quote_buf(&cmd, path);
1223
aab40438 1224 /* remove repo-local variables from the environment */
0c2f0d27
BW
1225 for (var = local_repo_env; *var; var++)
1226 argv_array_push(&conn->env_array, *var);
1227
a48b409f 1228 conn->use_shell = 1;
a2036d7e 1229 conn->in = conn->out = -1;
a2036d7e 1230 if (protocol == PROTO_SSH) {
a2036d7e
TB
1231 char *ssh_host = hostandport;
1232 const char *port = NULL;
a5adaced 1233 transport_check_allowed("ssh");
a2036d7e 1234 get_host_and_port(&ssh_host, &port);
a2036d7e 1235
86ceb337
TB
1236 if (!port)
1237 port = get_port(ssh_host);
42da4840 1238
3f55ccab
TB
1239 if (flags & CONNECT_DIAG_URL) {
1240 printf("Diag: url=%s\n", url ? url : "NULL");
1241 printf("Diag: protocol=%s\n", prot_name(protocol));
1242 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
1243 printf("Diag: port=%s\n", port ? port : "NONE");
1244 printf("Diag: path=%s\n", path ? path : "NULL");
a2036d7e 1245
3f55ccab
TB
1246 free(hostandport);
1247 free(path);
04f20c04 1248 free(conn);
f1399291 1249 strbuf_release(&cmd);
3f55ccab 1250 return NULL;
37ee646e 1251 }
fce54ce4 1252 fill_ssh_args(conn, ssh_host, port, flags);
c049b61d 1253 } else {
a5adaced 1254 transport_check_allowed("file");
0c2f0d27
BW
1255 if (get_protocol_version_config() > 0) {
1256 argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
1257 get_protocol_version_config());
1258 }
4852f723 1259 }
1823bea1 1260 argv_array_push(&conn->args, cmd.buf);
f364cb88 1261
a2036d7e
TB
1262 if (start_command(conn))
1263 die("unable to fork");
f364cb88 1264
a2036d7e
TB
1265 fd[0] = conn->out; /* read from child's stdout */
1266 fd[1] = conn->in; /* write to child's stdin */
1267 strbuf_release(&cmd);
1268 }
1269 free(hostandport);
cabc3c12 1270 free(path);
98158e9c 1271 return conn;
f7192598
LT
1272}
1273
98158e9c 1274int finish_connect(struct child_process *conn)
f7192598 1275{
f364cb88 1276 int code;
7ffe853b 1277 if (!conn || git_connection_is_socket(conn))
f42a5c4e
FBH
1278 return 0;
1279
f364cb88 1280 code = finish_command(conn);
98158e9c 1281 free(conn);
f364cb88 1282 return code;
f7192598 1283}