Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/connect.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / connect.c` 1568 lines ยท 40363 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 3 #include "git-compat-util.h" 4 #include "config.h" 5 #include "environment.h" 6 #include "gettext.h" 7 #include "hex.h" 8 #include "pkt-line.h" 9 #include "quote.h" 10 #include "refs.h" 11 #include "run-command.h" 12 #include "remote.h" 13 #include "connect.h" 14 #include "url.h" 15 #include "string-list.h" 16 #include "oid-array.h" 17 #include "path.h" 18 #include "transport.h" 19 #include "trace2.h" 20 #include "strbuf.h" 21 #include "version.h" 22 #include "protocol.h" 23 #include "alias.h" 24 #include "bundle-uri.h" 25 #include "promisor-remote.h" 26 27 static char *server_capabilities_v1; 28 static struct strvec server_capabilities_v2 = STRVEC_INIT; 29 static const char *next_server_feature_value(const char *feature, size_t *len, size_t *offset); 30 31 static int check_ref(const char *name, unsigned int flags) 32 { 33 if (!flags) 34 return 1; 35 36 if (!skip_prefix(name, "refs/", &name)) 37 return 0; 38 39 /* REF_NORMAL means that we don't want the magic fake tag refs */ 40 if ((flags & REF_NORMAL) && check_refname_format(name, 41 REFNAME_ALLOW_ONELEVEL)) 42 return 0; 43 44 /* REF_BRANCHES means that we want regular branch heads */ 45 if ((flags & REF_BRANCHES) && starts_with(name, "heads/")) 46 return 1; 47 48 /* REF_TAGS means that we want tags */ 49 if ((flags & REF_TAGS) && starts_with(name, "tags/")) 50 return 1; 51 52 /* All type bits clear means that we are ok with anything */ 53 return !(flags & ~REF_NORMAL); 54 } 55 56 int check_ref_type(const struct ref *ref, int flags) 57 { 58 return check_ref(ref->name, flags); 59 } 60 61 static NORETURN void die_initial_contact(int unexpected) 62 { 63 /* 64 * A hang-up after seeing some response from the other end 65 * means that it is unexpected, as we know the other end is 66 * willing to talk to us. A hang-up before seeing any 67 * response does not necessarily mean an ACL problem, though. 68 */ 69 if (unexpected) 70 die(_("the remote end hung up upon initial contact")); 71 else 72 die(_("Could not read from remote repository.\n\n" 73 "Please make sure you have the correct access rights\n" 74 "and the repository exists.")); 75 } 76 77 /* Checks if the server supports the capability 'c' */ 78 int server_supports_v2(const char *c) 79 { 80 size_t i; 81 82 for (i = 0; i < server_capabilities_v2.nr; i++) { 83 const char *out; 84 if (skip_prefix(server_capabilities_v2.v[i], c, &out) && 85 (!*out || *out == '=')) 86 return 1; 87 } 88 return 0; 89 } 90 91 void ensure_server_supports_v2(const char *c) 92 { 93 if (!server_supports_v2(c)) 94 die(_("server doesn't support '%s'"), c); 95 } 96 97 int server_feature_v2(const char *c, const char **v) 98 { 99 size_t i; 100 101 for (i = 0; i < server_capabilities_v2.nr; i++) { 102 const char *out; 103 if (skip_prefix(server_capabilities_v2.v[i], c, &out) && 104 (*out == '=')) { 105 *v = out + 1; 106 return 1; 107 } 108 } 109 return 0; 110 } 111 112 int server_supports_feature(const char *c, const char *feature, 113 int die_on_error) 114 { 115 size_t i; 116 117 for (i = 0; i < server_capabilities_v2.nr; i++) { 118 const char *out; 119 if (skip_prefix(server_capabilities_v2.v[i], c, &out) && 120 (!*out || *(out++) == '=')) { 121 if (parse_feature_request(out, feature)) 122 return 1; 123 else 124 break; 125 } 126 } 127 128 if (die_on_error) 129 die(_("server doesn't support feature '%s'"), feature); 130 131 return 0; 132 } 133 134 static void process_capabilities_v2(struct packet_reader *reader) 135 { 136 while (packet_reader_read(reader) == PACKET_READ_NORMAL) 137 strvec_push(&server_capabilities_v2, reader->line); 138 139 if (reader->status != PACKET_READ_FLUSH) 140 die(_("expected flush after capabilities")); 141 } 142 143 enum protocol_version discover_version(struct packet_reader *reader) 144 { 145 enum protocol_version version = protocol_unknown_version; 146 147 /* 148 * Peek the first line of the server's response to 149 * determine the protocol version the server is speaking. 150 */ 151 switch (packet_reader_peek(reader)) { 152 case PACKET_READ_EOF: 153 die_initial_contact(0); 154 case PACKET_READ_FLUSH: 155 case PACKET_READ_DELIM: 156 case PACKET_READ_RESPONSE_END: 157 version = protocol_v0; 158 break; 159 case PACKET_READ_NORMAL: 160 version = determine_protocol_version_client(reader->line); 161 break; 162 } 163 164 switch (version) { 165 case protocol_v2: 166 process_capabilities_v2(reader); 167 break; 168 case protocol_v1: 169 /* Read the peeked version line */ 170 packet_reader_read(reader); 171 break; 172 case protocol_v0: 173 break; 174 case protocol_unknown_version: 175 BUG("unknown protocol version"); 176 } 177 178 trace2_data_intmax("transfer", NULL, "negotiated-version", version); 179 180 return version; 181 } 182 183 static void parse_one_symref_info(struct string_list *symref, const char *val, int len) 184 { 185 char *sym, *target; 186 struct string_list_item *item; 187 188 if (!len) 189 return; /* just "symref" */ 190 /* e.g. "symref=HEAD:refs/heads/master" */ 191 sym = xmemdupz(val, len); 192 target = strchr(sym, ':'); 193 if (!target) 194 /* just "symref=something" */ 195 goto reject; 196 *(target++) = '\0'; 197 if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) || 198 check_refname_format(target, REFNAME_ALLOW_ONELEVEL)) 199 /* "symref=bogus:pair */ 200 goto reject; 201 item = string_list_append_nodup(symref, sym); 202 item->util = target; 203 return; 204 reject: 205 free(sym); 206 return; 207 } 208 209 static void annotate_refs_with_symref_info(struct ref *ref) 210 { 211 struct string_list symref = STRING_LIST_INIT_DUP; 212 size_t offset = 0; 213 214 while (1) { 215 size_t len; 216 const char *val; 217 218 val = next_server_feature_value("symref", &len, &offset); 219 if (!val) 220 break; 221 parse_one_symref_info(&symref, val, len); 222 } 223 string_list_sort(&symref); 224 225 for (; ref; ref = ref->next) { 226 struct string_list_item *item; 227 item = string_list_lookup(&symref, ref->name); 228 if (!item) 229 continue; 230 ref->symref = xstrdup((char *)item->util); 231 } 232 string_list_clear(&symref, 0); 233 } 234 235 static void process_capabilities(struct packet_reader *reader, size_t *linelen) 236 { 237 const char *feat_val; 238 size_t feat_len; 239 const char *line = reader->line; 240 size_t nul_location = strlen(line); 241 if (nul_location == *linelen) 242 return; 243 244 free(server_capabilities_v1); 245 server_capabilities_v1 = xstrdup(line + nul_location + 1); 246 *linelen = nul_location; 247 248 feat_val = server_feature_value("object-format", &feat_len); 249 if (feat_val) { 250 char *hash_name = xstrndup(feat_val, feat_len); 251 int hash_algo = hash_algo_by_name(hash_name); 252 if (hash_algo != GIT_HASH_UNKNOWN) 253 reader->hash_algo = &hash_algos[hash_algo]; 254 free(hash_name); 255 } else { 256 reader->hash_algo = &hash_algos[GIT_HASH_SHA1_LEGACY]; 257 } 258 } 259 260 static int process_dummy_ref(const struct packet_reader *reader) 261 { 262 const char *line = reader->line; 263 struct object_id oid; 264 const char *name; 265 266 if (parse_oid_hex_algop(line, &oid, &name, reader->hash_algo)) 267 return 0; 268 if (*name != ' ') 269 return 0; 270 name++; 271 272 return oideq(reader->hash_algo->null_oid, &oid) && 273 !strcmp(name, "capabilities^{}"); 274 } 275 276 static void check_no_capabilities(const char *line, size_t len) 277 { 278 if (strlen(line) != len) 279 warning(_("ignoring capabilities after first line '%s'"), 280 line + strlen(line)); 281 } 282 283 static int process_ref(const struct packet_reader *reader, size_t len, 284 struct ref ***list, unsigned int flags, 285 struct oid_array *extra_have) 286 { 287 const char *line = reader->line; 288 struct object_id old_oid; 289 const char *name; 290 291 if (parse_oid_hex_algop(line, &old_oid, &name, reader->hash_algo)) 292 return 0; 293 if (*name != ' ') 294 return 0; 295 name++; 296 297 if (extra_have && !strcmp(name, ".have")) { 298 oid_array_append(extra_have, &old_oid); 299 } else if (!strcmp(name, "capabilities^{}")) { 300 die(_("protocol error: unexpected capabilities^{}")); 301 } else if (check_ref(name, flags)) { 302 struct ref *ref = alloc_ref(name); 303 oidcpy(&ref->old_oid, &old_oid); 304 **list = ref; 305 *list = &ref->next; 306 } 307 check_no_capabilities(line, len); 308 return 1; 309 } 310 311 static int process_shallow(const struct packet_reader *reader, size_t len, 312 struct oid_array *shallow_points) 313 { 314 const char *line = reader->line; 315 const char *arg; 316 struct object_id old_oid; 317 318 if (!skip_prefix(line, "shallow ", &arg)) 319 return 0; 320 321 if (get_oid_hex_algop(arg, &old_oid, reader->hash_algo)) 322 die(_("protocol error: expected shallow sha-1, got '%s'"), arg); 323 if (!shallow_points) 324 die(_("repository on the other end cannot be shallow")); 325 oid_array_append(shallow_points, &old_oid); 326 check_no_capabilities(line, len); 327 return 1; 328 } 329 330 enum get_remote_heads_state { 331 EXPECTING_FIRST_REF = 0, 332 EXPECTING_REF, 333 EXPECTING_SHALLOW, 334 EXPECTING_DONE, 335 }; 336 337 /* 338 * Read all the refs from the other end 339 */ 340 struct ref **get_remote_heads(struct packet_reader *reader, 341 struct ref **list, unsigned int flags, 342 struct oid_array *extra_have, 343 struct oid_array *shallow_points) 344 { 345 struct ref **orig_list = list; 346 size_t len = 0; 347 enum get_remote_heads_state state = EXPECTING_FIRST_REF; 348 349 *list = NULL; 350 351 while (state != EXPECTING_DONE) { 352 switch (packet_reader_read(reader)) { 353 case PACKET_READ_EOF: 354 die_initial_contact(1); 355 case PACKET_READ_NORMAL: 356 len = reader->pktlen; 357 break; 358 case PACKET_READ_FLUSH: 359 state = EXPECTING_DONE; 360 break; 361 case PACKET_READ_DELIM: 362 case PACKET_READ_RESPONSE_END: 363 die(_("invalid packet")); 364 } 365 366 switch (state) { 367 case EXPECTING_FIRST_REF: 368 process_capabilities(reader, &len); 369 if (process_dummy_ref(reader)) { 370 state = EXPECTING_SHALLOW; 371 break; 372 } 373 state = EXPECTING_REF; 374 /* fallthrough */ 375 case EXPECTING_REF: 376 if (process_ref(reader, len, &list, flags, extra_have)) 377 break; 378 state = EXPECTING_SHALLOW; 379 /* fallthrough */ 380 case EXPECTING_SHALLOW: 381 if (process_shallow(reader, len, shallow_points)) 382 break; 383 die(_("protocol error: unexpected '%s'"), reader->line); 384 case EXPECTING_DONE: 385 break; 386 } 387 } 388 389 annotate_refs_with_symref_info(*orig_list); 390 391 return list; 392 } 393 394 /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */ 395 static int process_ref_v2(struct packet_reader *reader, struct ref ***list, 396 const char **unborn_head_target) 397 { 398 int ret = 1; 399 size_t i = 0; 400 struct object_id old_oid; 401 struct ref *ref; 402 struct string_list line_sections = STRING_LIST_INIT_DUP; 403 const char *end; 404 const char *line = reader->line; 405 406 /* 407 * Ref lines have a number of fields which are space deliminated. The 408 * first field is the OID of the ref. The second field is the ref 409 * name. Subsequent fields (symref-target and peeled) are optional and 410 * don't have a particular order. 411 */ 412 if (string_list_split(&line_sections, line, " ", -1) < 2) { 413 ret = 0; 414 goto out; 415 } 416 417 if (!strcmp("unborn", line_sections.items[i].string)) { 418 i++; 419 if (unborn_head_target && 420 !strcmp("HEAD", line_sections.items[i++].string)) { 421 /* 422 * Look for the symref target (if any). If found, 423 * return it to the caller. 424 */ 425 for (; i < line_sections.nr; i++) { 426 const char *arg = line_sections.items[i].string; 427 428 if (skip_prefix(arg, "symref-target:", &arg)) { 429 if (!check_refname_format(arg, 0)) 430 *unborn_head_target = xstrdup(arg); 431 else 432 warning(_("remote advertised invalid symref-target '%s', ignoring"), arg); 433 break; 434 } 435 } 436 } 437 goto out; 438 } 439 if (parse_oid_hex_algop(line_sections.items[i++].string, &old_oid, &end, reader->hash_algo) || 440 *end) { 441 ret = 0; 442 goto out; 443 } 444 445 if (check_refname_format(line_sections.items[i].string, 446 REFNAME_ALLOW_ONELEVEL)) { 447 warning(_("remote advertised invalid ref name '%s', ignoring"), 448 line_sections.items[i].string); 449 goto out; 450 } 451 452 ref = alloc_ref(line_sections.items[i++].string); 453 454 memcpy(ref->old_oid.hash, old_oid.hash, reader->hash_algo->rawsz); 455 **list = ref; 456 *list = &ref->next; 457 458 for (; i < line_sections.nr; i++) { 459 const char *arg = line_sections.items[i].string; 460 if (skip_prefix(arg, "symref-target:", &arg)) { 461 if (!check_refname_format(arg, 0)) 462 ref->symref = xstrdup(arg); 463 else 464 warning(_("remote advertised invalid symref-target '%s', ignoring"), arg); 465 } 466 467 if (skip_prefix(arg, "peeled:", &arg)) { 468 struct object_id peeled_oid; 469 char *peeled_name; 470 struct ref *peeled; 471 if (parse_oid_hex_algop(arg, &peeled_oid, &end, 472 reader->hash_algo) || *end) { 473 ret = 0; 474 goto out; 475 } 476 477 peeled_name = xstrfmt("%s^{}", ref->name); 478 peeled = alloc_ref(peeled_name); 479 480 memcpy(peeled->old_oid.hash, peeled_oid.hash, 481 reader->hash_algo->rawsz); 482 **list = peeled; 483 *list = &peeled->next; 484 485 free(peeled_name); 486 } 487 } 488 489 out: 490 string_list_clear(&line_sections, 0); 491 return ret; 492 } 493 494 void check_stateless_delimiter(int stateless_rpc, 495 struct packet_reader *reader, 496 const char *error) 497 { 498 if (!stateless_rpc) 499 return; /* not in stateless mode, no delimiter expected */ 500 if (packet_reader_read(reader) != PACKET_READ_RESPONSE_END) 501 die("%s", error); 502 } 503 504 static void send_capabilities(int fd_out, struct packet_reader *reader) 505 { 506 const char *hash_name; 507 const char *promisor_remote_info; 508 509 if (server_supports_v2("agent")) 510 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized()); 511 512 if (server_feature_v2("object-format", &hash_name)) { 513 int hash_algo = hash_algo_by_name(hash_name); 514 if (hash_algo == GIT_HASH_UNKNOWN) 515 die(_("unknown object format '%s' specified by server"), hash_name); 516 reader->hash_algo = &hash_algos[hash_algo]; 517 packet_write_fmt(fd_out, "object-format=%s", reader->hash_algo->name); 518 } else { 519 reader->hash_algo = &hash_algos[GIT_HASH_SHA1_LEGACY]; 520 } 521 if (server_feature_v2("promisor-remote", &promisor_remote_info)) { 522 char *reply; 523 promisor_remote_reply(promisor_remote_info, &reply); 524 if (reply) { 525 packet_write_fmt(fd_out, "promisor-remote=%s", reply); 526 free(reply); 527 } 528 } 529 } 530 531 int get_remote_bundle_uri(int fd_out, struct packet_reader *reader, 532 struct bundle_list *bundles, int stateless_rpc) 533 { 534 int line_nr = 1, err = 0; 535 536 /* Assert bundle-uri support */ 537 ensure_server_supports_v2("bundle-uri"); 538 539 /* (Re-)send capabilities */ 540 send_capabilities(fd_out, reader); 541 542 /* Send command */ 543 packet_write_fmt(fd_out, "command=bundle-uri\n"); 544 packet_delim(fd_out); 545 546 packet_flush(fd_out); 547 548 /* Process response from server */ 549 while (packet_reader_read(reader) == PACKET_READ_NORMAL) { 550 const char *line = reader->line; 551 line_nr++; 552 553 /* 554 * Do not parse if an error was encountered, but 555 * continue draining the response so no stale data 556 * is left in the reader for subsequent protocol 557 * exchanges. 558 */ 559 if (err) 560 continue; 561 562 if (!bundle_uri_parse_line(bundles, line)) 563 continue; 564 565 err = error(_("error on bundle-uri response line %d: %s"), 566 line_nr, line); 567 } 568 569 if (reader->status != PACKET_READ_FLUSH) 570 return error(_("expected flush after bundle-uri listing")); 571 572 /* 573 * Might die(), but obscure enough that that's OK, e.g. in 574 * serve.c we'll call BUG() on its equivalent (the 575 * PACKET_READ_RESPONSE_END check). 576 */ 577 check_stateless_delimiter(stateless_rpc, reader, 578 _("expected response end packet after ref listing")); 579 580 return err; 581 } 582 583 struct ref **get_remote_refs(int fd_out, struct packet_reader *reader, 584 struct ref **list, int for_push, 585 struct transport_ls_refs_options *transport_options, 586 const struct string_list *server_options, 587 int stateless_rpc) 588 { 589 size_t i; 590 struct strvec *ref_prefixes = transport_options ? 591 &transport_options->ref_prefixes : NULL; 592 const char **unborn_head_target = transport_options ? 593 &transport_options->unborn_head_target : NULL; 594 *list = NULL; 595 596 ensure_server_supports_v2("ls-refs"); 597 packet_write_fmt(fd_out, "command=ls-refs\n"); 598 599 /* Send capabilities */ 600 send_capabilities(fd_out, reader); 601 602 if (server_options && server_options->nr) { 603 ensure_server_supports_v2("server-option"); 604 for (i = 0; i < server_options->nr; i++) 605 packet_write_fmt(fd_out, "server-option=%s", 606 server_options->items[i].string); 607 } 608 609 packet_delim(fd_out); 610 /* When pushing we don't want to request the peeled tags */ 611 if (!for_push) 612 packet_write_fmt(fd_out, "peel\n"); 613 packet_write_fmt(fd_out, "symrefs\n"); 614 if (server_supports_feature("ls-refs", "unborn", 0)) 615 packet_write_fmt(fd_out, "unborn\n"); 616 for (i = 0; ref_prefixes && i < ref_prefixes->nr; i++) { 617 packet_write_fmt(fd_out, "ref-prefix %s\n", 618 ref_prefixes->v[i]); 619 } 620 packet_flush(fd_out); 621 622 /* Process response from server */ 623 while (packet_reader_read(reader) == PACKET_READ_NORMAL) { 624 if (!process_ref_v2(reader, &list, unborn_head_target)) 625 die(_("invalid ls-refs response: %s"), reader->line); 626 } 627 628 if (reader->status != PACKET_READ_FLUSH) 629 die(_("expected flush after ref listing")); 630 631 check_stateless_delimiter(stateless_rpc, reader, 632 _("expected response end packet after ref listing")); 633 634 return list; 635 } 636 637 const char *parse_feature_value(const char *feature_list, const char *feature, size_t *lenp, size_t *offset) 638 { 639 const char *orig_start = feature_list; 640 size_t len; 641 642 if (!feature_list) 643 return NULL; 644 645 len = strlen(feature); 646 if (offset) 647 feature_list += *offset; 648 while (*feature_list) { 649 const char *found = strstr(feature_list, feature); 650 if (!found) 651 return NULL; 652 if (feature_list == found || isspace(found[-1])) { 653 const char *value = found + len; 654 /* feature with no value (e.g., "thin-pack") */ 655 if (!*value || isspace(*value)) { 656 if (lenp) 657 *lenp = 0; 658 if (offset) 659 *offset = found + len - orig_start; 660 return value; 661 } 662 /* feature with a value (e.g., "agent=git/1.2.3-Linux") */ 663 else if (*value == '=') { 664 size_t end; 665 666 value++; 667 end = strcspn(value, " \t\n"); 668 if (lenp) 669 *lenp = end; 670 if (offset) 671 *offset = value + end - orig_start; 672 return value; 673 } 674 /* 675 * otherwise we matched a substring of another feature; 676 * keep looking 677 */ 678 } 679 feature_list = found + 1; 680 } 681 return NULL; 682 } 683 684 int server_supports_hash(const char *desired, int *feature_supported) 685 { 686 size_t offset = 0; 687 size_t len; 688 const char *hash; 689 690 hash = next_server_feature_value("object-format", &len, &offset); 691 if (feature_supported) 692 *feature_supported = !!hash; 693 if (!hash) { 694 hash = hash_algos[GIT_HASH_SHA1_LEGACY].name; 695 len = strlen(hash); 696 } 697 while (hash) { 698 if (!xstrncmpz(desired, hash, len)) 699 return 1; 700 701 hash = next_server_feature_value("object-format", &len, &offset); 702 } 703 return 0; 704 } 705 706 int parse_feature_request(const char *feature_list, const char *feature) 707 { 708 return !!parse_feature_value(feature_list, feature, NULL, NULL); 709 } 710 711 static const char *next_server_feature_value(const char *feature, size_t *len, size_t *offset) 712 { 713 return parse_feature_value(server_capabilities_v1, feature, len, offset); 714 } 715 716 const char *server_feature_value(const char *feature, size_t *len) 717 { 718 return parse_feature_value(server_capabilities_v1, feature, len, NULL); 719 } 720 721 int server_supports(const char *feature) 722 { 723 return !!server_feature_value(feature, NULL); 724 } 725 726 void write_command_and_capabilities(struct strbuf *req_buf, const char *command, 727 const struct string_list *server_options) 728 { 729 const char *hash_name; 730 int advertise_sid = 0; 731 732 repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid); 733 734 ensure_server_supports_v2(command); 735 packet_buf_write(req_buf, "command=%s", command); 736 if (server_supports_v2("agent")) 737 packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized()); 738 if (advertise_sid && server_supports_v2("session-id")) 739 packet_buf_write(req_buf, "session-id=%s", trace2_session_id()); 740 if (server_options && server_options->nr) { 741 ensure_server_supports_v2("server-option"); 742 for (size_t i = 0; i < server_options->nr; i++) 743 packet_buf_write(req_buf, "server-option=%s", 744 server_options->items[i].string); 745 } 746 747 if (server_feature_v2("object-format", &hash_name)) { 748 const unsigned int hash_algo = hash_algo_by_name(hash_name); 749 if (hash_algo_by_ptr(the_hash_algo) != hash_algo) 750 die(_("mismatched algorithms: client %s; server %s"), 751 the_hash_algo->name, hash_name); 752 packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name); 753 } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1_LEGACY) { 754 die(_("the server does not support algorithm '%s'"), 755 the_hash_algo->name); 756 } 757 packet_buf_delim(req_buf); 758 } 759 760 static const char *url_scheme_name(enum url_scheme scheme) 761 { 762 switch (scheme) { 763 case URL_SCHEME_LOCAL: 764 case URL_SCHEME_FILE: 765 return "file"; 766 case URL_SCHEME_SSH: 767 return "ssh"; 768 case URL_SCHEME_GIT: 769 return "git"; 770 default: 771 return "unknown protocol"; 772 } 773 } 774 775 static char *host_end(char **hoststart, int removebrackets) 776 { 777 char *host = *hoststart; 778 char *end; 779 char *start = strstr(host, "@["); 780 if (start) 781 start++; /* Jump over '@' */ 782 else 783 start = host; 784 if (start[0] == '[') { 785 end = strchr(start + 1, ']'); 786 if (end) { 787 if (removebrackets) { 788 *end = 0; 789 memmove(start, start + 1, end - start); 790 end++; 791 } 792 } else 793 end = host; 794 } else 795 end = host; 796 return end; 797 } 798 799 #define STR_(s) # s 800 #define STR(s) STR_(s) 801 802 static void get_host_and_port(char **host, const char **port) 803 { 804 char *colon, *end; 805 end = host_end(host, 1); 806 colon = strchr(end, ':'); 807 if (colon) { 808 long portnr = strtol(colon + 1, &end, 10); 809 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) { 810 *colon = 0; 811 *port = colon + 1; 812 } else if (!colon[1]) { 813 *colon = 0; 814 } 815 } 816 } 817 818 static void enable_keepalive(int sockfd) 819 { 820 int ka = 1; 821 822 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0) 823 error_errno(_("unable to set SO_KEEPALIVE on socket")); 824 } 825 826 #ifndef NO_IPV6 827 828 static const char *ai_name(const struct addrinfo *ai) 829 { 830 static char addr[NI_MAXHOST]; 831 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0, 832 NI_NUMERICHOST) != 0) 833 xsnprintf(addr, sizeof(addr), "(unknown)"); 834 835 return addr; 836 } 837 838 /* 839 * Returns a connected socket() fd, or else die()s. 840 */ 841 static int git_tcp_connect_sock(char *host, int flags) 842 { 843 struct strbuf error_message = STRBUF_INIT; 844 int sockfd = -1; 845 const char *port = STR(DEFAULT_GIT_PORT); 846 struct addrinfo hints, *ai0, *ai; 847 int gai; 848 int cnt = 0; 849 850 get_host_and_port(&host, &port); 851 if (!*port) 852 port = ""; 853 854 memset(&hints, 0, sizeof(hints)); 855 if (flags & CONNECT_IPV4) 856 hints.ai_family = AF_INET; 857 else if (flags & CONNECT_IPV6) 858 hints.ai_family = AF_INET6; 859 hints.ai_socktype = SOCK_STREAM; 860 hints.ai_protocol = IPPROTO_TCP; 861 862 if (flags & CONNECT_VERBOSE) 863 fprintf(stderr, _("Looking up %s ... "), host); 864 865 gai = getaddrinfo(host, port, &hints, &ai); 866 if (gai) 867 die(_("unable to look up %s (port %s) (%s)"), host, port, gai_strerror(gai)); 868 869 if (flags & CONNECT_VERBOSE) 870 /* TRANSLATORS: this is the end of "Looking up %s ... " */ 871 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port); 872 873 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) { 874 sockfd = socket(ai->ai_family, 875 ai->ai_socktype, ai->ai_protocol); 876 if ((sockfd < 0) || 877 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) { 878 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", 879 host, cnt, ai_name(ai), strerror(errno)); 880 if (0 <= sockfd) 881 close(sockfd); 882 sockfd = -1; 883 continue; 884 } 885 if (flags & CONNECT_VERBOSE) 886 fprintf(stderr, "%s ", ai_name(ai)); 887 break; 888 } 889 890 freeaddrinfo(ai0); 891 892 if (sockfd < 0) 893 die(_("unable to connect to %s:\n%s"), host, error_message.buf); 894 895 enable_keepalive(sockfd); 896 897 if (flags & CONNECT_VERBOSE) 898 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */ 899 fprintf_ln(stderr, _("done.")); 900 901 strbuf_release(&error_message); 902 903 return sockfd; 904 } 905 906 #else /* NO_IPV6 */ 907 908 /* 909 * Returns a connected socket() fd, or else die()s. 910 */ 911 static int git_tcp_connect_sock(char *host, int flags) 912 { 913 struct strbuf error_message = STRBUF_INIT; 914 int sockfd = -1; 915 const char *port = STR(DEFAULT_GIT_PORT); 916 char *ep; 917 struct hostent *he; 918 struct sockaddr_in sa; 919 char **ap; 920 unsigned int nport; 921 int cnt; 922 923 get_host_and_port(&host, &port); 924 925 if (flags & CONNECT_VERBOSE) 926 fprintf(stderr, _("Looking up %s ... "), host); 927 928 he = gethostbyname(host); 929 if (!he) 930 die(_("unable to look up %s (%s)"), host, hstrerror(h_errno)); 931 nport = strtoul(port, &ep, 10); 932 if ( ep == port || *ep ) { 933 /* Not numeric */ 934 struct servent *se = getservbyname(port,"tcp"); 935 if ( !se ) 936 die(_("unknown port %s"), port); 937 nport = se->s_port; 938 } 939 940 if (flags & CONNECT_VERBOSE) 941 /* TRANSLATORS: this is the end of "Looking up %s ... " */ 942 fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port); 943 944 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) { 945 memset(&sa, 0, sizeof sa); 946 sa.sin_family = he->h_addrtype; 947 sa.sin_port = htons(nport); 948 memcpy(&sa.sin_addr, *ap, he->h_length); 949 950 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0); 951 if ((sockfd < 0) || 952 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) { 953 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", 954 host, 955 cnt, 956 inet_ntoa(*(struct in_addr *)&sa.sin_addr), 957 strerror(errno)); 958 if (0 <= sockfd) 959 close(sockfd); 960 sockfd = -1; 961 continue; 962 } 963 if (flags & CONNECT_VERBOSE) 964 fprintf(stderr, "%s ", 965 inet_ntoa(*(struct in_addr *)&sa.sin_addr)); 966 break; 967 } 968 969 if (sockfd < 0) 970 die(_("unable to connect to %s:\n%s"), host, error_message.buf); 971 972 enable_keepalive(sockfd); 973 974 if (flags & CONNECT_VERBOSE) 975 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */ 976 fprintf_ln(stderr, _("done.")); 977 978 return sockfd; 979 } 980 981 #endif /* NO_IPV6 */ 982 983 984 /* 985 * Dummy child_process returned by git_connect() if the transport protocol 986 * does not need fork(2). 987 */ 988 static struct child_process no_fork = CHILD_PROCESS_INIT; 989 990 int git_connection_is_socket(struct child_process *conn) 991 { 992 return conn == &no_fork; 993 } 994 995 static struct child_process *git_tcp_connect(int fd[2], char *host, int flags) 996 { 997 int sockfd = git_tcp_connect_sock(host, flags); 998 999 fd[0] = sockfd; 1000 fd[1] = dup(sockfd); 1001 1002 return &no_fork; 1003 } 1004 1005 1006 static char *git_proxy_command; 1007 1008 static int git_proxy_command_options(const char *var, const char *value, 1009 const struct config_context *ctx, void *cb) 1010 { 1011 if (!strcmp(var, "core.gitproxy")) { 1012 const char *for_pos; 1013 int matchlen = -1; 1014 int hostlen; 1015 const char *rhost_name = cb; 1016 int rhost_len = strlen(rhost_name); 1017 1018 if (git_proxy_command) 1019 return 0; 1020 if (!value) 1021 return config_error_nonbool(var); 1022 /* [core] 1023 * ;# matches www.kernel.org as well 1024 * gitproxy = netcatter-1 for kernel.org 1025 * gitproxy = netcatter-2 for sample.xz 1026 * gitproxy = netcatter-default 1027 */ 1028 for_pos = strstr(value, " for "); 1029 if (!for_pos) 1030 /* matches everybody */ 1031 matchlen = strlen(value); 1032 else { 1033 hostlen = strlen(for_pos + 5); 1034 if (rhost_len < hostlen) 1035 matchlen = -1; 1036 else if (!strncmp(for_pos + 5, 1037 rhost_name + rhost_len - hostlen, 1038 hostlen) && 1039 ((rhost_len == hostlen) || 1040 rhost_name[rhost_len - hostlen -1] == '.')) 1041 matchlen = for_pos - value; 1042 else 1043 matchlen = -1; 1044 } 1045 if (0 <= matchlen) { 1046 /* core.gitproxy = none for kernel.org */ 1047 if (matchlen == 4 && 1048 !memcmp(value, "none", 4)) 1049 matchlen = 0; 1050 git_proxy_command = xmemdupz(value, matchlen); 1051 } 1052 return 0; 1053 } 1054 1055 return git_default_config(var, value, ctx, cb); 1056 } 1057 1058 static int git_use_proxy(const char *host) 1059 { 1060 git_proxy_command = getenv("GIT_PROXY_COMMAND"); 1061 repo_config(the_repository, git_proxy_command_options, (void*)host); 1062 return (git_proxy_command && *git_proxy_command); 1063 } 1064 1065 static struct child_process *git_proxy_connect(int fd[2], char *host) 1066 { 1067 const char *port = STR(DEFAULT_GIT_PORT); 1068 struct child_process *proxy; 1069 1070 get_host_and_port(&host, &port); 1071 1072 if (looks_like_command_line_option(host)) 1073 die(_("strange hostname '%s' blocked"), host); 1074 if (looks_like_command_line_option(port)) 1075 die(_("strange port '%s' blocked"), port); 1076 1077 proxy = xmalloc(sizeof(*proxy)); 1078 child_process_init(proxy); 1079 strvec_push(&proxy->args, git_proxy_command); 1080 strvec_push(&proxy->args, host); 1081 strvec_push(&proxy->args, port); 1082 proxy->in = -1; 1083 proxy->out = -1; 1084 if (start_command(proxy)) 1085 die(_("cannot start proxy %s"), git_proxy_command); 1086 fd[0] = proxy->out; /* read from proxy stdout */ 1087 fd[1] = proxy->in; /* write to proxy stdin */ 1088 return proxy; 1089 } 1090 1091 static char *get_port(char *host) 1092 { 1093 char *end; 1094 char *p = strchr(host, ':'); 1095 1096 if (p) { 1097 long port = strtol(p + 1, &end, 10); 1098 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) { 1099 *p = '\0'; 1100 return p+1; 1101 } 1102 } 1103 1104 return NULL; 1105 } 1106 1107 /* 1108 * Extract protocol and relevant parts from the specified connection URL. 1109 * The caller must free() the returned strings. 1110 */ 1111 static enum url_scheme parse_connect_url(const char *url_orig, char **ret_host, 1112 char **ret_path) 1113 { 1114 char *url; 1115 char *host, *path; 1116 char *end; 1117 int separator = '/'; 1118 enum url_scheme scheme = URL_SCHEME_LOCAL; 1119 1120 if (is_url(url_orig)) 1121 url = url_decode(url_orig); 1122 else 1123 url = xstrdup(url_orig); 1124 1125 host = strstr(url, "://"); 1126 if (host) { 1127 *host = '\0'; 1128 scheme = url_get_scheme(url); 1129 if (scheme == URL_SCHEME_UNKNOWN) 1130 die(_("protocol '%s' is not supported"), url); 1131 host += 3; 1132 } else { 1133 host = url; 1134 if (!url_is_local_not_ssh(url)) { 1135 scheme = URL_SCHEME_SSH; 1136 separator = ':'; 1137 } 1138 } 1139 1140 /* 1141 * Don't do destructive transforms as protocol code does 1142 * '[]' unwrapping in get_host_and_port() 1143 */ 1144 end = host_end(&host, 0); 1145 1146 if (scheme == URL_SCHEME_LOCAL) 1147 path = end; 1148 else if (scheme == URL_SCHEME_FILE && *host != '/' && 1149 !has_dos_drive_prefix(host) && 1150 offset_1st_component(host - 2) > 1) 1151 path = host - 2; /* include the leading "//" */ 1152 else if (scheme == URL_SCHEME_FILE && has_dos_drive_prefix(end)) 1153 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */ 1154 else 1155 path = strchr(end, separator); 1156 1157 if (!path || !*path) 1158 die(_("no path specified; see 'git help pull' for valid url syntax")); 1159 1160 /* 1161 * null-terminate hostname and point path to ~ for URL's like this: 1162 * ssh://host.xz/~user/repo 1163 */ 1164 1165 end = path; /* Need to \0 terminate host here */ 1166 if (separator == ':') 1167 path++; /* path starts after ':' */ 1168 if (scheme == URL_SCHEME_GIT || scheme == URL_SCHEME_SSH) { 1169 if (path[1] == '~') 1170 path++; 1171 } 1172 1173 path = xstrdup(path); 1174 *end = '\0'; 1175 1176 *ret_host = xstrdup(host); 1177 *ret_path = path; 1178 free(url); 1179 return scheme; 1180 } 1181 1182 static const char *get_ssh_command(void) 1183 { 1184 const char *ssh; 1185 1186 if ((ssh = getenv("GIT_SSH_COMMAND"))) 1187 return ssh; 1188 1189 if (!repo_config_get_string_tmp(the_repository, "core.sshcommand", &ssh)) 1190 return ssh; 1191 1192 return NULL; 1193 } 1194 1195 enum ssh_variant { 1196 VARIANT_AUTO, 1197 VARIANT_SIMPLE, 1198 VARIANT_SSH, 1199 VARIANT_PLINK, 1200 VARIANT_PUTTY, 1201 VARIANT_TORTOISEPLINK, 1202 }; 1203 1204 static void override_ssh_variant(enum ssh_variant *ssh_variant) 1205 { 1206 const char *variant = getenv("GIT_SSH_VARIANT"); 1207 1208 if (!variant && repo_config_get_string_tmp(the_repository, "ssh.variant", &variant)) 1209 return; 1210 1211 if (!strcmp(variant, "auto")) 1212 *ssh_variant = VARIANT_AUTO; 1213 else if (!strcmp(variant, "plink")) 1214 *ssh_variant = VARIANT_PLINK; 1215 else if (!strcmp(variant, "putty")) 1216 *ssh_variant = VARIANT_PUTTY; 1217 else if (!strcmp(variant, "tortoiseplink")) 1218 *ssh_variant = VARIANT_TORTOISEPLINK; 1219 else if (!strcmp(variant, "simple")) 1220 *ssh_variant = VARIANT_SIMPLE; 1221 else 1222 *ssh_variant = VARIANT_SSH; 1223 } 1224 1225 static enum ssh_variant determine_ssh_variant(const char *ssh_command, 1226 int is_cmdline) 1227 { 1228 enum ssh_variant ssh_variant = VARIANT_AUTO; 1229 const char *variant; 1230 char *p = NULL; 1231 1232 override_ssh_variant(&ssh_variant); 1233 1234 if (ssh_variant != VARIANT_AUTO) 1235 return ssh_variant; 1236 1237 if (!is_cmdline) { 1238 p = xstrdup(ssh_command); 1239 variant = basename(p); 1240 } else { 1241 const char **ssh_argv; 1242 1243 p = xstrdup(ssh_command); 1244 if (split_cmdline(p, &ssh_argv) > 0) { 1245 variant = basename((char *)ssh_argv[0]); 1246 /* 1247 * At this point, variant points into the buffer 1248 * referenced by p, hence we do not need ssh_argv 1249 * any longer. 1250 */ 1251 free(ssh_argv); 1252 } else { 1253 free(p); 1254 return ssh_variant; 1255 } 1256 } 1257 1258 if (!strcasecmp(variant, "ssh") || 1259 !strcasecmp(variant, "ssh.exe")) 1260 ssh_variant = VARIANT_SSH; 1261 else if (!strcasecmp(variant, "plink") || 1262 !strcasecmp(variant, "plink.exe")) 1263 ssh_variant = VARIANT_PLINK; 1264 else if (!strcasecmp(variant, "tortoiseplink") || 1265 !strcasecmp(variant, "tortoiseplink.exe")) 1266 ssh_variant = VARIANT_TORTOISEPLINK; 1267 1268 free(p); 1269 return ssh_variant; 1270 } 1271 1272 /* 1273 * Open a connection using Git's native protocol. 1274 * 1275 * The caller is responsible for freeing hostandport, but this function may 1276 * modify it (for example, to truncate it to remove the port part). 1277 */ 1278 static struct child_process *git_connect_git(int fd[2], char *hostandport, 1279 const char *path, const char *prog, 1280 enum protocol_version version, 1281 int flags) 1282 { 1283 struct child_process *conn; 1284 struct strbuf request = STRBUF_INIT; 1285 /* 1286 * Set up virtual host information based on where we will 1287 * connect, unless the user has overridden us in 1288 * the environment. 1289 */ 1290 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST"); 1291 if (target_host) 1292 target_host = xstrdup(target_host); 1293 else 1294 target_host = xstrdup(hostandport); 1295 1296 transport_check_allowed("git"); 1297 if (strchr(target_host, '\n') || strchr(path, '\n')) 1298 die(_("newline is forbidden in git:// hosts and repo paths")); 1299 1300 /* 1301 * These underlying connection commands die() if they 1302 * cannot connect. 1303 */ 1304 if (git_use_proxy(hostandport)) 1305 conn = git_proxy_connect(fd, hostandport); 1306 else 1307 conn = git_tcp_connect(fd, hostandport, flags); 1308 /* 1309 * Separate original protocol components prog and path 1310 * from extended host header with a NUL byte. 1311 * 1312 * Note: Do not add any other headers here! Doing so 1313 * will cause older git-daemon servers to crash. 1314 */ 1315 strbuf_addf(&request, 1316 "%s %s%chost=%s%c", 1317 prog, path, 0, 1318 target_host, 0); 1319 1320 /* If using a new version put that stuff here after a second null byte */ 1321 if (version > 0) { 1322 strbuf_addch(&request, '\0'); 1323 strbuf_addf(&request, "version=%d%c", 1324 version, '\0'); 1325 } 1326 1327 packet_write(fd[1], request.buf, request.len); 1328 1329 free(target_host); 1330 strbuf_release(&request); 1331 return conn; 1332 } 1333 1334 /* 1335 * Append the appropriate environment variables to `env` and options to 1336 * `args` for running ssh in Git's SSH-tunneled transport. 1337 */ 1338 static void push_ssh_options(struct strvec *args, struct strvec *env, 1339 enum ssh_variant variant, const char *port, 1340 enum protocol_version version, int flags) 1341 { 1342 if (variant == VARIANT_SSH && 1343 version > 0) { 1344 strvec_push(args, "-o"); 1345 strvec_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT); 1346 strvec_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d", 1347 version); 1348 } 1349 1350 if (flags & CONNECT_IPV4) { 1351 switch (variant) { 1352 case VARIANT_AUTO: 1353 BUG("VARIANT_AUTO passed to push_ssh_options"); 1354 case VARIANT_SIMPLE: 1355 die(_("ssh variant 'simple' does not support -4")); 1356 case VARIANT_SSH: 1357 case VARIANT_PLINK: 1358 case VARIANT_PUTTY: 1359 case VARIANT_TORTOISEPLINK: 1360 strvec_push(args, "-4"); 1361 } 1362 } else if (flags & CONNECT_IPV6) { 1363 switch (variant) { 1364 case VARIANT_AUTO: 1365 BUG("VARIANT_AUTO passed to push_ssh_options"); 1366 case VARIANT_SIMPLE: 1367 die(_("ssh variant 'simple' does not support -6")); 1368 case VARIANT_SSH: 1369 case VARIANT_PLINK: 1370 case VARIANT_PUTTY: 1371 case VARIANT_TORTOISEPLINK: 1372 strvec_push(args, "-6"); 1373 } 1374 } 1375 1376 if (variant == VARIANT_TORTOISEPLINK) 1377 strvec_push(args, "-batch"); 1378 1379 if (port) { 1380 switch (variant) { 1381 case VARIANT_AUTO: 1382 BUG("VARIANT_AUTO passed to push_ssh_options"); 1383 case VARIANT_SIMPLE: 1384 die(_("ssh variant 'simple' does not support setting port")); 1385 case VARIANT_SSH: 1386 strvec_push(args, "-p"); 1387 break; 1388 case VARIANT_PLINK: 1389 case VARIANT_PUTTY: 1390 case VARIANT_TORTOISEPLINK: 1391 strvec_push(args, "-P"); 1392 } 1393 1394 strvec_push(args, port); 1395 } 1396 } 1397 1398 /* Prepare a child_process for use by Git's SSH-tunneled transport. */ 1399 static void fill_ssh_args(struct child_process *conn, const char *ssh_host, 1400 const char *port, enum protocol_version version, 1401 int flags) 1402 { 1403 const char *ssh; 1404 enum ssh_variant variant; 1405 1406 if (looks_like_command_line_option(ssh_host)) 1407 die(_("strange hostname '%s' blocked"), ssh_host); 1408 1409 ssh = get_ssh_command(); 1410 if (ssh) { 1411 variant = determine_ssh_variant(ssh, 1); 1412 } else { 1413 /* 1414 * GIT_SSH is the no-shell version of 1415 * GIT_SSH_COMMAND (and must remain so for 1416 * historical compatibility). 1417 */ 1418 conn->use_shell = 0; 1419 1420 ssh = getenv("GIT_SSH"); 1421 if (!ssh) 1422 ssh = "ssh"; 1423 variant = determine_ssh_variant(ssh, 0); 1424 } 1425 1426 if (variant == VARIANT_AUTO) { 1427 struct child_process detect = CHILD_PROCESS_INIT; 1428 1429 detect.use_shell = conn->use_shell; 1430 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1; 1431 1432 strvec_push(&detect.args, ssh); 1433 strvec_push(&detect.args, "-G"); 1434 push_ssh_options(&detect.args, &detect.env, 1435 VARIANT_SSH, port, version, flags); 1436 strvec_push(&detect.args, ssh_host); 1437 1438 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH; 1439 } 1440 1441 strvec_push(&conn->args, ssh); 1442 push_ssh_options(&conn->args, &conn->env, variant, port, version, 1443 flags); 1444 strvec_push(&conn->args, ssh_host); 1445 } 1446 1447 /* 1448 * This returns the dummy child_process `no_fork` if the transport protocol 1449 * does not need fork(2), or a struct child_process object if it does. Once 1450 * done, finish the connection with finish_connect() with the value returned 1451 * from this function (it is safe to call finish_connect() with NULL to 1452 * support the former case). 1453 * 1454 * If it returns, the connect is successful; it just dies on errors (this 1455 * will hopefully be changed in a libification effort, to return NULL when 1456 * the connection failed). 1457 */ 1458 struct child_process *git_connect(int fd[2], const char *url, 1459 enum git_connect_service service, 1460 const char *prog, int flags) 1461 { 1462 char *hostandport, *path; 1463 struct child_process *conn; 1464 enum url_scheme scheme; 1465 enum protocol_version version = get_protocol_version_config(); 1466 1467 /* 1468 * NEEDSWORK: If we are trying to use protocol v2 and we are planning 1469 * to perform any operation that doesn't involve upload-pack (i.e., a 1470 * fetch, ls-remote, etc), then fallback to v0 since we don't know how 1471 * to do anything else (like push or remote archive) via v2. 1472 */ 1473 if (version == protocol_v2 && service != GIT_CONNECT_UPLOAD_PACK) 1474 version = protocol_v0; 1475 1476 /* Without this we cannot rely on waitpid() to tell 1477 * what happened to our children. 1478 */ 1479 signal(SIGCHLD, SIG_DFL); 1480 1481 scheme = parse_connect_url(url, &hostandport, &path); 1482 if ((flags & CONNECT_DIAG_URL) && (scheme != URL_SCHEME_SSH)) { 1483 printf("Diag: url=%s\n", url ? url : "NULL"); 1484 printf("Diag: protocol=%s\n", url_scheme_name(scheme)); 1485 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL"); 1486 printf("Diag: path=%s\n", path ? path : "NULL"); 1487 conn = NULL; 1488 } else if (scheme == URL_SCHEME_GIT) { 1489 conn = git_connect_git(fd, hostandport, path, prog, version, flags); 1490 conn->trace2_child_class = "transport/git"; 1491 } else { 1492 struct strbuf cmd = STRBUF_INIT; 1493 const char *const *var; 1494 1495 conn = xmalloc(sizeof(*conn)); 1496 child_process_init(conn); 1497 1498 if (looks_like_command_line_option(path)) 1499 die(_("strange pathname '%s' blocked"), path); 1500 1501 strbuf_addstr(&cmd, prog); 1502 strbuf_addch(&cmd, ' '); 1503 sq_quote_buf(&cmd, path); 1504 1505 /* remove repo-local variables from the environment */ 1506 for (var = local_repo_env; *var; var++) 1507 strvec_push(&conn->env, *var); 1508 1509 conn->use_shell = 1; 1510 conn->in = conn->out = -1; 1511 if (scheme == URL_SCHEME_SSH) { 1512 char *ssh_host = hostandport; 1513 const char *port = NULL; 1514 transport_check_allowed("ssh"); 1515 get_host_and_port(&ssh_host, &port); 1516 1517 if (!port) 1518 port = get_port(ssh_host); 1519 1520 if (flags & CONNECT_DIAG_URL) { 1521 printf("Diag: url=%s\n", url ? url : "NULL"); 1522 printf("Diag: protocol=%s\n", url_scheme_name(scheme)); 1523 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL"); 1524 printf("Diag: port=%s\n", port ? port : "NONE"); 1525 printf("Diag: path=%s\n", path ? path : "NULL"); 1526 1527 free(hostandport); 1528 free(path); 1529 child_process_clear(conn); 1530 free(conn); 1531 strbuf_release(&cmd); 1532 return NULL; 1533 } 1534 conn->trace2_child_class = "transport/ssh"; 1535 fill_ssh_args(conn, ssh_host, port, version, flags); 1536 } else { 1537 transport_check_allowed("file"); 1538 conn->trace2_child_class = "transport/file"; 1539 if (version > 0) { 1540 strvec_pushf(&conn->env, 1541 GIT_PROTOCOL_ENVIRONMENT "=version=%d", 1542 version); 1543 } 1544 } 1545 strvec_push(&conn->args, cmd.buf); 1546 1547 if (start_command(conn)) 1548 die(_("unable to fork")); 1549 1550 fd[0] = conn->out; /* read from child's stdout */ 1551 fd[1] = conn->in; /* write to child's stdin */ 1552 strbuf_release(&cmd); 1553 } 1554 free(hostandport); 1555 free(path); 1556 return conn; 1557 } 1558 1559 int finish_connect(struct child_process *conn) 1560 { 1561 int code; 1562 if (!conn || git_connection_is_socket(conn)) 1563 return 0; 1564 1565 code = finish_command(conn); 1566 free(conn); 1567 return code; 1568 }