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