| 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 | *unborn_head_target = xstrdup(arg); |
| 430 | break; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | goto out; |
| 435 | } |
| 436 | if (parse_oid_hex_algop(line_sections.items[i++].string, &old_oid, &end, reader->hash_algo) || |
| 437 | *end) { |
| 438 | ret = 0; |
| 439 | goto out; |
| 440 | } |
| 441 | |
| 442 | ref = alloc_ref(line_sections.items[i++].string); |
| 443 | |
| 444 | memcpy(ref->old_oid.hash, old_oid.hash, reader->hash_algo->rawsz); |
| 445 | **list = ref; |
| 446 | *list = &ref->next; |
| 447 | |
| 448 | for (; i < line_sections.nr; i++) { |
| 449 | const char *arg = line_sections.items[i].string; |
| 450 | if (skip_prefix(arg, "symref-target:", &arg)) |
| 451 | ref->symref = xstrdup(arg); |
| 452 | |
| 453 | if (skip_prefix(arg, "peeled:", &arg)) { |
| 454 | struct object_id peeled_oid; |
| 455 | char *peeled_name; |
| 456 | struct ref *peeled; |
| 457 | if (parse_oid_hex_algop(arg, &peeled_oid, &end, |
| 458 | reader->hash_algo) || *end) { |
| 459 | ret = 0; |
| 460 | goto out; |
| 461 | } |
| 462 | |
| 463 | peeled_name = xstrfmt("%s^{}", ref->name); |
| 464 | peeled = alloc_ref(peeled_name); |
| 465 | |
| 466 | memcpy(peeled->old_oid.hash, peeled_oid.hash, |
| 467 | reader->hash_algo->rawsz); |
| 468 | **list = peeled; |
| 469 | *list = &peeled->next; |
| 470 | |
| 471 | free(peeled_name); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | out: |
| 476 | string_list_clear(&line_sections, 0); |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | void check_stateless_delimiter(int stateless_rpc, |
| 481 | struct packet_reader *reader, |
| 482 | const char *error) |
| 483 | { |
| 484 | if (!stateless_rpc) |
| 485 | return; /* not in stateless mode, no delimiter expected */ |
| 486 | if (packet_reader_read(reader) != PACKET_READ_RESPONSE_END) |
| 487 | die("%s", error); |
| 488 | } |
| 489 | |
| 490 | static void send_capabilities(int fd_out, struct packet_reader *reader) |
| 491 | { |
| 492 | const char *hash_name; |
| 493 | const char *promisor_remote_info; |
| 494 | |
| 495 | if (server_supports_v2("agent")) |
| 496 | packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized()); |
| 497 | |
| 498 | if (server_feature_v2("object-format", &hash_name)) { |
| 499 | int hash_algo = hash_algo_by_name(hash_name); |
| 500 | if (hash_algo == GIT_HASH_UNKNOWN) |
| 501 | die(_("unknown object format '%s' specified by server"), hash_name); |
| 502 | reader->hash_algo = &hash_algos[hash_algo]; |
| 503 | packet_write_fmt(fd_out, "object-format=%s", reader->hash_algo->name); |
| 504 | } else { |
| 505 | reader->hash_algo = &hash_algos[GIT_HASH_SHA1_LEGACY]; |
| 506 | } |
| 507 | if (server_feature_v2("promisor-remote", &promisor_remote_info)) { |
| 508 | char *reply; |
| 509 | promisor_remote_reply(promisor_remote_info, &reply); |
| 510 | if (reply) { |
| 511 | packet_write_fmt(fd_out, "promisor-remote=%s", reply); |
| 512 | free(reply); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | int get_remote_bundle_uri(int fd_out, struct packet_reader *reader, |
| 518 | struct bundle_list *bundles, int stateless_rpc) |
| 519 | { |
| 520 | int line_nr = 1; |
| 521 | |
| 522 | /* Assert bundle-uri support */ |
| 523 | ensure_server_supports_v2("bundle-uri"); |
| 524 | |
| 525 | /* (Re-)send capabilities */ |
| 526 | send_capabilities(fd_out, reader); |
| 527 | |
| 528 | /* Send command */ |
| 529 | packet_write_fmt(fd_out, "command=bundle-uri\n"); |
| 530 | packet_delim(fd_out); |
| 531 | |
| 532 | packet_flush(fd_out); |
| 533 | |
| 534 | /* Process response from server */ |
| 535 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
| 536 | const char *line = reader->line; |
| 537 | line_nr++; |
| 538 | |
| 539 | if (!bundle_uri_parse_line(bundles, line)) |
| 540 | continue; |
| 541 | |
| 542 | return error(_("error on bundle-uri response line %d: %s"), |
| 543 | line_nr, line); |
| 544 | } |
| 545 | |
| 546 | if (reader->status != PACKET_READ_FLUSH) |
| 547 | return error(_("expected flush after bundle-uri listing")); |
| 548 | |
| 549 | /* |
| 550 | * Might die(), but obscure enough that that's OK, e.g. in |
| 551 | * serve.c we'll call BUG() on its equivalent (the |
| 552 | * PACKET_READ_RESPONSE_END check). |
| 553 | */ |
| 554 | check_stateless_delimiter(stateless_rpc, reader, |
| 555 | _("expected response end packet after ref listing")); |
| 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | struct ref **get_remote_refs(int fd_out, struct packet_reader *reader, |
| 561 | struct ref **list, int for_push, |
| 562 | struct transport_ls_refs_options *transport_options, |
| 563 | const struct string_list *server_options, |
| 564 | int stateless_rpc) |
| 565 | { |
| 566 | size_t i; |
| 567 | struct strvec *ref_prefixes = transport_options ? |
| 568 | &transport_options->ref_prefixes : NULL; |
| 569 | const char **unborn_head_target = transport_options ? |
| 570 | &transport_options->unborn_head_target : NULL; |
| 571 | *list = NULL; |
| 572 | |
| 573 | ensure_server_supports_v2("ls-refs"); |
| 574 | packet_write_fmt(fd_out, "command=ls-refs\n"); |
| 575 | |
| 576 | /* Send capabilities */ |
| 577 | send_capabilities(fd_out, reader); |
| 578 | |
| 579 | if (server_options && server_options->nr) { |
| 580 | ensure_server_supports_v2("server-option"); |
| 581 | for (i = 0; i < server_options->nr; i++) |
| 582 | packet_write_fmt(fd_out, "server-option=%s", |
| 583 | server_options->items[i].string); |
| 584 | } |
| 585 | |
| 586 | packet_delim(fd_out); |
| 587 | /* When pushing we don't want to request the peeled tags */ |
| 588 | if (!for_push) |
| 589 | packet_write_fmt(fd_out, "peel\n"); |
| 590 | packet_write_fmt(fd_out, "symrefs\n"); |
| 591 | if (server_supports_feature("ls-refs", "unborn", 0)) |
| 592 | packet_write_fmt(fd_out, "unborn\n"); |
| 593 | for (i = 0; ref_prefixes && i < ref_prefixes->nr; i++) { |
| 594 | packet_write_fmt(fd_out, "ref-prefix %s\n", |
| 595 | ref_prefixes->v[i]); |
| 596 | } |
| 597 | packet_flush(fd_out); |
| 598 | |
| 599 | /* Process response from server */ |
| 600 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
| 601 | if (!process_ref_v2(reader, &list, unborn_head_target)) |
| 602 | die(_("invalid ls-refs response: %s"), reader->line); |
| 603 | } |
| 604 | |
| 605 | if (reader->status != PACKET_READ_FLUSH) |
| 606 | die(_("expected flush after ref listing")); |
| 607 | |
| 608 | check_stateless_delimiter(stateless_rpc, reader, |
| 609 | _("expected response end packet after ref listing")); |
| 610 | |
| 611 | return list; |
| 612 | } |
| 613 | |
| 614 | const char *parse_feature_value(const char *feature_list, const char *feature, size_t *lenp, size_t *offset) |
| 615 | { |
| 616 | const char *orig_start = feature_list; |
| 617 | size_t len; |
| 618 | |
| 619 | if (!feature_list) |
| 620 | return NULL; |
| 621 | |
| 622 | len = strlen(feature); |
| 623 | if (offset) |
| 624 | feature_list += *offset; |
| 625 | while (*feature_list) { |
| 626 | const char *found = strstr(feature_list, feature); |
| 627 | if (!found) |
| 628 | return NULL; |
| 629 | if (feature_list == found || isspace(found[-1])) { |
| 630 | const char *value = found + len; |
| 631 | /* feature with no value (e.g., "thin-pack") */ |
| 632 | if (!*value || isspace(*value)) { |
| 633 | if (lenp) |
| 634 | *lenp = 0; |
| 635 | if (offset) |
| 636 | *offset = found + len - orig_start; |
| 637 | return value; |
| 638 | } |
| 639 | /* feature with a value (e.g., "agent=git/1.2.3-Linux") */ |
| 640 | else if (*value == '=') { |
| 641 | size_t end; |
| 642 | |
| 643 | value++; |
| 644 | end = strcspn(value, " \t\n"); |
| 645 | if (lenp) |
| 646 | *lenp = end; |
| 647 | if (offset) |
| 648 | *offset = value + end - orig_start; |
| 649 | return value; |
| 650 | } |
| 651 | /* |
| 652 | * otherwise we matched a substring of another feature; |
| 653 | * keep looking |
| 654 | */ |
| 655 | } |
| 656 | feature_list = found + 1; |
| 657 | } |
| 658 | return NULL; |
| 659 | } |
| 660 | |
| 661 | int server_supports_hash(const char *desired, int *feature_supported) |
| 662 | { |
| 663 | size_t offset = 0; |
| 664 | size_t len; |
| 665 | const char *hash; |
| 666 | |
| 667 | hash = next_server_feature_value("object-format", &len, &offset); |
| 668 | if (feature_supported) |
| 669 | *feature_supported = !!hash; |
| 670 | if (!hash) { |
| 671 | hash = hash_algos[GIT_HASH_SHA1_LEGACY].name; |
| 672 | len = strlen(hash); |
| 673 | } |
| 674 | while (hash) { |
| 675 | if (!xstrncmpz(desired, hash, len)) |
| 676 | return 1; |
| 677 | |
| 678 | hash = next_server_feature_value("object-format", &len, &offset); |
| 679 | } |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | int parse_feature_request(const char *feature_list, const char *feature) |
| 684 | { |
| 685 | return !!parse_feature_value(feature_list, feature, NULL, NULL); |
| 686 | } |
| 687 | |
| 688 | static const char *next_server_feature_value(const char *feature, size_t *len, size_t *offset) |
| 689 | { |
| 690 | return parse_feature_value(server_capabilities_v1, feature, len, offset); |
| 691 | } |
| 692 | |
| 693 | const char *server_feature_value(const char *feature, size_t *len) |
| 694 | { |
| 695 | return parse_feature_value(server_capabilities_v1, feature, len, NULL); |
| 696 | } |
| 697 | |
| 698 | int server_supports(const char *feature) |
| 699 | { |
| 700 | return !!server_feature_value(feature, NULL); |
| 701 | } |
| 702 | |
| 703 | static const char *url_scheme_name(enum url_scheme scheme) |
| 704 | { |
| 705 | switch (scheme) { |
| 706 | case URL_SCHEME_LOCAL: |
| 707 | case URL_SCHEME_FILE: |
| 708 | return "file"; |
| 709 | case URL_SCHEME_SSH: |
| 710 | return "ssh"; |
| 711 | case URL_SCHEME_GIT: |
| 712 | return "git"; |
| 713 | default: |
| 714 | return "unknown protocol"; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | static char *host_end(char **hoststart, int removebrackets) |
| 719 | { |
| 720 | char *host = *hoststart; |
| 721 | char *end; |
| 722 | char *start = strstr(host, "@["); |
| 723 | if (start) |
| 724 | start++; /* Jump over '@' */ |
| 725 | else |
| 726 | start = host; |
| 727 | if (start[0] == '[') { |
| 728 | end = strchr(start + 1, ']'); |
| 729 | if (end) { |
| 730 | if (removebrackets) { |
| 731 | *end = 0; |
| 732 | memmove(start, start + 1, end - start); |
| 733 | end++; |
| 734 | } |
| 735 | } else |
| 736 | end = host; |
| 737 | } else |
| 738 | end = host; |
| 739 | return end; |
| 740 | } |
| 741 | |
| 742 | #define STR_(s) # s |
| 743 | #define STR(s) STR_(s) |
| 744 | |
| 745 | static void get_host_and_port(char **host, const char **port) |
| 746 | { |
| 747 | char *colon, *end; |
| 748 | end = host_end(host, 1); |
| 749 | colon = strchr(end, ':'); |
| 750 | if (colon) { |
| 751 | long portnr = strtol(colon + 1, &end, 10); |
| 752 | if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) { |
| 753 | *colon = 0; |
| 754 | *port = colon + 1; |
| 755 | } else if (!colon[1]) { |
| 756 | *colon = 0; |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | static void enable_keepalive(int sockfd) |
| 762 | { |
| 763 | int ka = 1; |
| 764 | |
| 765 | if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0) |
| 766 | error_errno(_("unable to set SO_KEEPALIVE on socket")); |
| 767 | } |
| 768 | |
| 769 | #ifndef NO_IPV6 |
| 770 | |
| 771 | static const char *ai_name(const struct addrinfo *ai) |
| 772 | { |
| 773 | static char addr[NI_MAXHOST]; |
| 774 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0, |
| 775 | NI_NUMERICHOST) != 0) |
| 776 | xsnprintf(addr, sizeof(addr), "(unknown)"); |
| 777 | |
| 778 | return addr; |
| 779 | } |
| 780 | |
| 781 | /* |
| 782 | * Returns a connected socket() fd, or else die()s. |
| 783 | */ |
| 784 | static int git_tcp_connect_sock(char *host, int flags) |
| 785 | { |
| 786 | struct strbuf error_message = STRBUF_INIT; |
| 787 | int sockfd = -1; |
| 788 | const char *port = STR(DEFAULT_GIT_PORT); |
| 789 | struct addrinfo hints, *ai0, *ai; |
| 790 | int gai; |
| 791 | int cnt = 0; |
| 792 | |
| 793 | get_host_and_port(&host, &port); |
| 794 | if (!*port) |
| 795 | port = "<none>"; |
| 796 | |
| 797 | memset(&hints, 0, sizeof(hints)); |
| 798 | if (flags & CONNECT_IPV4) |
| 799 | hints.ai_family = AF_INET; |
| 800 | else if (flags & CONNECT_IPV6) |
| 801 | hints.ai_family = AF_INET6; |
| 802 | hints.ai_socktype = SOCK_STREAM; |
| 803 | hints.ai_protocol = IPPROTO_TCP; |
| 804 | |
| 805 | if (flags & CONNECT_VERBOSE) |
| 806 | fprintf(stderr, _("Looking up %s ... "), host); |
| 807 | |
| 808 | gai = getaddrinfo(host, port, &hints, &ai); |
| 809 | if (gai) |
| 810 | die(_("unable to look up %s (port %s) (%s)"), host, port, gai_strerror(gai)); |
| 811 | |
| 812 | if (flags & CONNECT_VERBOSE) |
| 813 | /* TRANSLATORS: this is the end of "Looking up %s ... " */ |
| 814 | fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port); |
| 815 | |
| 816 | for (ai0 = ai; ai; ai = ai->ai_next, cnt++) { |
| 817 | sockfd = socket(ai->ai_family, |
| 818 | ai->ai_socktype, ai->ai_protocol); |
| 819 | if ((sockfd < 0) || |
| 820 | (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) { |
| 821 | strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", |
| 822 | host, cnt, ai_name(ai), strerror(errno)); |
| 823 | if (0 <= sockfd) |
| 824 | close(sockfd); |
| 825 | sockfd = -1; |
| 826 | continue; |
| 827 | } |
| 828 | if (flags & CONNECT_VERBOSE) |
| 829 | fprintf(stderr, "%s ", ai_name(ai)); |
| 830 | break; |
| 831 | } |
| 832 | |
| 833 | freeaddrinfo(ai0); |
| 834 | |
| 835 | if (sockfd < 0) |
| 836 | die(_("unable to connect to %s:\n%s"), host, error_message.buf); |
| 837 | |
| 838 | enable_keepalive(sockfd); |
| 839 | |
| 840 | if (flags & CONNECT_VERBOSE) |
| 841 | /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */ |
| 842 | fprintf_ln(stderr, _("done.")); |
| 843 | |
| 844 | strbuf_release(&error_message); |
| 845 | |
| 846 | return sockfd; |
| 847 | } |
| 848 | |
| 849 | #else /* NO_IPV6 */ |
| 850 | |
| 851 | /* |
| 852 | * Returns a connected socket() fd, or else die()s. |
| 853 | */ |
| 854 | static int git_tcp_connect_sock(char *host, int flags) |
| 855 | { |
| 856 | struct strbuf error_message = STRBUF_INIT; |
| 857 | int sockfd = -1; |
| 858 | const char *port = STR(DEFAULT_GIT_PORT); |
| 859 | char *ep; |
| 860 | struct hostent *he; |
| 861 | struct sockaddr_in sa; |
| 862 | char **ap; |
| 863 | unsigned int nport; |
| 864 | int cnt; |
| 865 | |
| 866 | get_host_and_port(&host, &port); |
| 867 | |
| 868 | if (flags & CONNECT_VERBOSE) |
| 869 | fprintf(stderr, _("Looking up %s ... "), host); |
| 870 | |
| 871 | he = gethostbyname(host); |
| 872 | if (!he) |
| 873 | die(_("unable to look up %s (%s)"), host, hstrerror(h_errno)); |
| 874 | nport = strtoul(port, &ep, 10); |
| 875 | if ( ep == port || *ep ) { |
| 876 | /* Not numeric */ |
| 877 | struct servent *se = getservbyname(port,"tcp"); |
| 878 | if ( !se ) |
| 879 | die(_("unknown port %s"), port); |
| 880 | nport = se->s_port; |
| 881 | } |
| 882 | |
| 883 | if (flags & CONNECT_VERBOSE) |
| 884 | /* TRANSLATORS: this is the end of "Looking up %s ... " */ |
| 885 | fprintf(stderr, _("done.\nConnecting to %s (port %s) ... "), host, port); |
| 886 | |
| 887 | for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) { |
| 888 | memset(&sa, 0, sizeof sa); |
| 889 | sa.sin_family = he->h_addrtype; |
| 890 | sa.sin_port = htons(nport); |
| 891 | memcpy(&sa.sin_addr, *ap, he->h_length); |
| 892 | |
| 893 | sockfd = socket(he->h_addrtype, SOCK_STREAM, 0); |
| 894 | if ((sockfd < 0) || |
| 895 | connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) { |
| 896 | strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n", |
| 897 | host, |
| 898 | cnt, |
| 899 | inet_ntoa(*(struct in_addr *)&sa.sin_addr), |
| 900 | strerror(errno)); |
| 901 | if (0 <= sockfd) |
| 902 | close(sockfd); |
| 903 | sockfd = -1; |
| 904 | continue; |
| 905 | } |
| 906 | if (flags & CONNECT_VERBOSE) |
| 907 | fprintf(stderr, "%s ", |
| 908 | inet_ntoa(*(struct in_addr *)&sa.sin_addr)); |
| 909 | break; |
| 910 | } |
| 911 | |
| 912 | if (sockfd < 0) |
| 913 | die(_("unable to connect to %s:\n%s"), host, error_message.buf); |
| 914 | |
| 915 | enable_keepalive(sockfd); |
| 916 | |
| 917 | if (flags & CONNECT_VERBOSE) |
| 918 | /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */ |
| 919 | fprintf_ln(stderr, _("done.")); |
| 920 | |
| 921 | return sockfd; |
| 922 | } |
| 923 | |
| 924 | #endif /* NO_IPV6 */ |
| 925 | |
| 926 | |
| 927 | /* |
| 928 | * Dummy child_process returned by git_connect() if the transport protocol |
| 929 | * does not need fork(2). |
| 930 | */ |
| 931 | static struct child_process no_fork = CHILD_PROCESS_INIT; |
| 932 | |
| 933 | int git_connection_is_socket(struct child_process *conn) |
| 934 | { |
| 935 | return conn == &no_fork; |
| 936 | } |
| 937 | |
| 938 | static struct child_process *git_tcp_connect(int fd[2], char *host, int flags) |
| 939 | { |
| 940 | int sockfd = git_tcp_connect_sock(host, flags); |
| 941 | |
| 942 | fd[0] = sockfd; |
| 943 | fd[1] = dup(sockfd); |
| 944 | |
| 945 | return &no_fork; |
| 946 | } |
| 947 | |
| 948 | |
| 949 | static char *git_proxy_command; |
| 950 | |
| 951 | static int git_proxy_command_options(const char *var, const char *value, |
| 952 | const struct config_context *ctx, void *cb) |
| 953 | { |
| 954 | if (!strcmp(var, "core.gitproxy")) { |
| 955 | const char *for_pos; |
| 956 | int matchlen = -1; |
| 957 | int hostlen; |
| 958 | const char *rhost_name = cb; |
| 959 | int rhost_len = strlen(rhost_name); |
| 960 | |
| 961 | if (git_proxy_command) |
| 962 | return 0; |
| 963 | if (!value) |
| 964 | return config_error_nonbool(var); |
| 965 | /* [core] |
| 966 | * ;# matches www.kernel.org as well |
| 967 | * gitproxy = netcatter-1 for kernel.org |
| 968 | * gitproxy = netcatter-2 for sample.xz |
| 969 | * gitproxy = netcatter-default |
| 970 | */ |
| 971 | for_pos = strstr(value, " for "); |
| 972 | if (!for_pos) |
| 973 | /* matches everybody */ |
| 974 | matchlen = strlen(value); |
| 975 | else { |
| 976 | hostlen = strlen(for_pos + 5); |
| 977 | if (rhost_len < hostlen) |
| 978 | matchlen = -1; |
| 979 | else if (!strncmp(for_pos + 5, |
| 980 | rhost_name + rhost_len - hostlen, |
| 981 | hostlen) && |
| 982 | ((rhost_len == hostlen) || |
| 983 | rhost_name[rhost_len - hostlen -1] == '.')) |
| 984 | matchlen = for_pos - value; |
| 985 | else |
| 986 | matchlen = -1; |
| 987 | } |
| 988 | if (0 <= matchlen) { |
| 989 | /* core.gitproxy = none for kernel.org */ |
| 990 | if (matchlen == 4 && |
| 991 | !memcmp(value, "none", 4)) |
| 992 | matchlen = 0; |
| 993 | git_proxy_command = xmemdupz(value, matchlen); |
| 994 | } |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | return git_default_config(var, value, ctx, cb); |
| 999 | } |
| 1000 | |
| 1001 | static int git_use_proxy(const char *host) |
| 1002 | { |
| 1003 | git_proxy_command = getenv("GIT_PROXY_COMMAND"); |
| 1004 | repo_config(the_repository, git_proxy_command_options, (void*)host); |
| 1005 | return (git_proxy_command && *git_proxy_command); |
| 1006 | } |
| 1007 | |
| 1008 | static struct child_process *git_proxy_connect(int fd[2], char *host) |
| 1009 | { |
| 1010 | const char *port = STR(DEFAULT_GIT_PORT); |
| 1011 | struct child_process *proxy; |
| 1012 | |
| 1013 | get_host_and_port(&host, &port); |
| 1014 | |
| 1015 | if (looks_like_command_line_option(host)) |
| 1016 | die(_("strange hostname '%s' blocked"), host); |
| 1017 | if (looks_like_command_line_option(port)) |
| 1018 | die(_("strange port '%s' blocked"), port); |
| 1019 | |
| 1020 | proxy = xmalloc(sizeof(*proxy)); |
| 1021 | child_process_init(proxy); |
| 1022 | strvec_push(&proxy->args, git_proxy_command); |
| 1023 | strvec_push(&proxy->args, host); |
| 1024 | strvec_push(&proxy->args, port); |
| 1025 | proxy->in = -1; |
| 1026 | proxy->out = -1; |
| 1027 | if (start_command(proxy)) |
| 1028 | die(_("cannot start proxy %s"), git_proxy_command); |
| 1029 | fd[0] = proxy->out; /* read from proxy stdout */ |
| 1030 | fd[1] = proxy->in; /* write to proxy stdin */ |
| 1031 | return proxy; |
| 1032 | } |
| 1033 | |
| 1034 | static char *get_port(char *host) |
| 1035 | { |
| 1036 | char *end; |
| 1037 | char *p = strchr(host, ':'); |
| 1038 | |
| 1039 | if (p) { |
| 1040 | long port = strtol(p + 1, &end, 10); |
| 1041 | if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) { |
| 1042 | *p = '\0'; |
| 1043 | return p+1; |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | return NULL; |
| 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * Extract protocol and relevant parts from the specified connection URL. |
| 1052 | * The caller must free() the returned strings. |
| 1053 | */ |
| 1054 | static enum url_scheme parse_connect_url(const char *url_orig, char **ret_host, |
| 1055 | char **ret_path) |
| 1056 | { |
| 1057 | char *url; |
| 1058 | char *host, *path; |
| 1059 | char *end; |
| 1060 | int separator = '/'; |
| 1061 | enum url_scheme scheme = URL_SCHEME_LOCAL; |
| 1062 | |
| 1063 | if (is_url(url_orig)) |
| 1064 | url = url_decode(url_orig); |
| 1065 | else |
| 1066 | url = xstrdup(url_orig); |
| 1067 | |
| 1068 | host = strstr(url, "://"); |
| 1069 | if (host) { |
| 1070 | *host = '\0'; |
| 1071 | scheme = url_get_scheme(url); |
| 1072 | if (scheme == URL_SCHEME_UNKNOWN) |
| 1073 | die(_("protocol '%s' is not supported"), url); |
| 1074 | host += 3; |
| 1075 | } else { |
| 1076 | host = url; |
| 1077 | if (!url_is_local_not_ssh(url)) { |
| 1078 | scheme = URL_SCHEME_SSH; |
| 1079 | separator = ':'; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | /* |
| 1084 | * Don't do destructive transforms as protocol code does |
| 1085 | * '[]' unwrapping in get_host_and_port() |
| 1086 | */ |
| 1087 | end = host_end(&host, 0); |
| 1088 | |
| 1089 | if (scheme == URL_SCHEME_LOCAL) |
| 1090 | path = end; |
| 1091 | else if (scheme == URL_SCHEME_FILE && *host != '/' && |
| 1092 | !has_dos_drive_prefix(host) && |
| 1093 | offset_1st_component(host - 2) > 1) |
| 1094 | path = host - 2; /* include the leading "//" */ |
| 1095 | else if (scheme == URL_SCHEME_FILE && has_dos_drive_prefix(end)) |
| 1096 | path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */ |
| 1097 | else |
| 1098 | path = strchr(end, separator); |
| 1099 | |
| 1100 | if (!path || !*path) |
| 1101 | die(_("no path specified; see 'git help pull' for valid url syntax")); |
| 1102 | |
| 1103 | /* |
| 1104 | * null-terminate hostname and point path to ~ for URL's like this: |
| 1105 | * ssh://host.xz/~user/repo |
| 1106 | */ |
| 1107 | |
| 1108 | end = path; /* Need to \0 terminate host here */ |
| 1109 | if (separator == ':') |
| 1110 | path++; /* path starts after ':' */ |
| 1111 | if (scheme == URL_SCHEME_GIT || scheme == URL_SCHEME_SSH) { |
| 1112 | if (path[1] == '~') |
| 1113 | path++; |
| 1114 | } |
| 1115 | |
| 1116 | path = xstrdup(path); |
| 1117 | *end = '\0'; |
| 1118 | |
| 1119 | *ret_host = xstrdup(host); |
| 1120 | *ret_path = path; |
| 1121 | free(url); |
| 1122 | return scheme; |
| 1123 | } |
| 1124 | |
| 1125 | static const char *get_ssh_command(void) |
| 1126 | { |
| 1127 | const char *ssh; |
| 1128 | |
| 1129 | if ((ssh = getenv("GIT_SSH_COMMAND"))) |
| 1130 | return ssh; |
| 1131 | |
| 1132 | if (!repo_config_get_string_tmp(the_repository, "core.sshcommand", &ssh)) |
| 1133 | return ssh; |
| 1134 | |
| 1135 | return NULL; |
| 1136 | } |
| 1137 | |
| 1138 | enum ssh_variant { |
| 1139 | VARIANT_AUTO, |
| 1140 | VARIANT_SIMPLE, |
| 1141 | VARIANT_SSH, |
| 1142 | VARIANT_PLINK, |
| 1143 | VARIANT_PUTTY, |
| 1144 | VARIANT_TORTOISEPLINK, |
| 1145 | }; |
| 1146 | |
| 1147 | static void override_ssh_variant(enum ssh_variant *ssh_variant) |
| 1148 | { |
| 1149 | const char *variant = getenv("GIT_SSH_VARIANT"); |
| 1150 | |
| 1151 | if (!variant && repo_config_get_string_tmp(the_repository, "ssh.variant", &variant)) |
| 1152 | return; |
| 1153 | |
| 1154 | if (!strcmp(variant, "auto")) |
| 1155 | *ssh_variant = VARIANT_AUTO; |
| 1156 | else if (!strcmp(variant, "plink")) |
| 1157 | *ssh_variant = VARIANT_PLINK; |
| 1158 | else if (!strcmp(variant, "putty")) |
| 1159 | *ssh_variant = VARIANT_PUTTY; |
| 1160 | else if (!strcmp(variant, "tortoiseplink")) |
| 1161 | *ssh_variant = VARIANT_TORTOISEPLINK; |
| 1162 | else if (!strcmp(variant, "simple")) |
| 1163 | *ssh_variant = VARIANT_SIMPLE; |
| 1164 | else |
| 1165 | *ssh_variant = VARIANT_SSH; |
| 1166 | } |
| 1167 | |
| 1168 | static enum ssh_variant determine_ssh_variant(const char *ssh_command, |
| 1169 | int is_cmdline) |
| 1170 | { |
| 1171 | enum ssh_variant ssh_variant = VARIANT_AUTO; |
| 1172 | const char *variant; |
| 1173 | char *p = NULL; |
| 1174 | |
| 1175 | override_ssh_variant(&ssh_variant); |
| 1176 | |
| 1177 | if (ssh_variant != VARIANT_AUTO) |
| 1178 | return ssh_variant; |
| 1179 | |
| 1180 | if (!is_cmdline) { |
| 1181 | p = xstrdup(ssh_command); |
| 1182 | variant = basename(p); |
| 1183 | } else { |
| 1184 | const char **ssh_argv; |
| 1185 | |
| 1186 | p = xstrdup(ssh_command); |
| 1187 | if (split_cmdline(p, &ssh_argv) > 0) { |
| 1188 | variant = basename((char *)ssh_argv[0]); |
| 1189 | /* |
| 1190 | * At this point, variant points into the buffer |
| 1191 | * referenced by p, hence we do not need ssh_argv |
| 1192 | * any longer. |
| 1193 | */ |
| 1194 | free(ssh_argv); |
| 1195 | } else { |
| 1196 | free(p); |
| 1197 | return ssh_variant; |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | if (!strcasecmp(variant, "ssh") || |
| 1202 | !strcasecmp(variant, "ssh.exe")) |
| 1203 | ssh_variant = VARIANT_SSH; |
| 1204 | else if (!strcasecmp(variant, "plink") || |
| 1205 | !strcasecmp(variant, "plink.exe")) |
| 1206 | ssh_variant = VARIANT_PLINK; |
| 1207 | else if (!strcasecmp(variant, "tortoiseplink") || |
| 1208 | !strcasecmp(variant, "tortoiseplink.exe")) |
| 1209 | ssh_variant = VARIANT_TORTOISEPLINK; |
| 1210 | |
| 1211 | free(p); |
| 1212 | return ssh_variant; |
| 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | * Open a connection using Git's native protocol. |
| 1217 | * |
| 1218 | * The caller is responsible for freeing hostandport, but this function may |
| 1219 | * modify it (for example, to truncate it to remove the port part). |
| 1220 | */ |
| 1221 | static struct child_process *git_connect_git(int fd[2], char *hostandport, |
| 1222 | const char *path, const char *prog, |
| 1223 | enum protocol_version version, |
| 1224 | int flags) |
| 1225 | { |
| 1226 | struct child_process *conn; |
| 1227 | struct strbuf request = STRBUF_INIT; |
| 1228 | /* |
| 1229 | * Set up virtual host information based on where we will |
| 1230 | * connect, unless the user has overridden us in |
| 1231 | * the environment. |
| 1232 | */ |
| 1233 | char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST"); |
| 1234 | if (target_host) |
| 1235 | target_host = xstrdup(target_host); |
| 1236 | else |
| 1237 | target_host = xstrdup(hostandport); |
| 1238 | |
| 1239 | transport_check_allowed("git"); |
| 1240 | if (strchr(target_host, '\n') || strchr(path, '\n')) |
| 1241 | die(_("newline is forbidden in git:// hosts and repo paths")); |
| 1242 | |
| 1243 | /* |
| 1244 | * These underlying connection commands die() if they |
| 1245 | * cannot connect. |
| 1246 | */ |
| 1247 | if (git_use_proxy(hostandport)) |
| 1248 | conn = git_proxy_connect(fd, hostandport); |
| 1249 | else |
| 1250 | conn = git_tcp_connect(fd, hostandport, flags); |
| 1251 | /* |
| 1252 | * Separate original protocol components prog and path |
| 1253 | * from extended host header with a NUL byte. |
| 1254 | * |
| 1255 | * Note: Do not add any other headers here! Doing so |
| 1256 | * will cause older git-daemon servers to crash. |
| 1257 | */ |
| 1258 | strbuf_addf(&request, |
| 1259 | "%s %s%chost=%s%c", |
| 1260 | prog, path, 0, |
| 1261 | target_host, 0); |
| 1262 | |
| 1263 | /* If using a new version put that stuff here after a second null byte */ |
| 1264 | if (version > 0) { |
| 1265 | strbuf_addch(&request, '\0'); |
| 1266 | strbuf_addf(&request, "version=%d%c", |
| 1267 | version, '\0'); |
| 1268 | } |
| 1269 | |
| 1270 | packet_write(fd[1], request.buf, request.len); |
| 1271 | |
| 1272 | free(target_host); |
| 1273 | strbuf_release(&request); |
| 1274 | return conn; |
| 1275 | } |
| 1276 | |
| 1277 | /* |
| 1278 | * Append the appropriate environment variables to `env` and options to |
| 1279 | * `args` for running ssh in Git's SSH-tunneled transport. |
| 1280 | */ |
| 1281 | static void push_ssh_options(struct strvec *args, struct strvec *env, |
| 1282 | enum ssh_variant variant, const char *port, |
| 1283 | enum protocol_version version, int flags) |
| 1284 | { |
| 1285 | if (variant == VARIANT_SSH && |
| 1286 | version > 0) { |
| 1287 | strvec_push(args, "-o"); |
| 1288 | strvec_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT); |
| 1289 | strvec_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d", |
| 1290 | version); |
| 1291 | } |
| 1292 | |
| 1293 | if (flags & CONNECT_IPV4) { |
| 1294 | switch (variant) { |
| 1295 | case VARIANT_AUTO: |
| 1296 | BUG("VARIANT_AUTO passed to push_ssh_options"); |
| 1297 | case VARIANT_SIMPLE: |
| 1298 | die(_("ssh variant 'simple' does not support -4")); |
| 1299 | case VARIANT_SSH: |
| 1300 | case VARIANT_PLINK: |
| 1301 | case VARIANT_PUTTY: |
| 1302 | case VARIANT_TORTOISEPLINK: |
| 1303 | strvec_push(args, "-4"); |
| 1304 | } |
| 1305 | } else if (flags & CONNECT_IPV6) { |
| 1306 | switch (variant) { |
| 1307 | case VARIANT_AUTO: |
| 1308 | BUG("VARIANT_AUTO passed to push_ssh_options"); |
| 1309 | case VARIANT_SIMPLE: |
| 1310 | die(_("ssh variant 'simple' does not support -6")); |
| 1311 | case VARIANT_SSH: |
| 1312 | case VARIANT_PLINK: |
| 1313 | case VARIANT_PUTTY: |
| 1314 | case VARIANT_TORTOISEPLINK: |
| 1315 | strvec_push(args, "-6"); |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | if (variant == VARIANT_TORTOISEPLINK) |
| 1320 | strvec_push(args, "-batch"); |
| 1321 | |
| 1322 | if (port) { |
| 1323 | switch (variant) { |
| 1324 | case VARIANT_AUTO: |
| 1325 | BUG("VARIANT_AUTO passed to push_ssh_options"); |
| 1326 | case VARIANT_SIMPLE: |
| 1327 | die(_("ssh variant 'simple' does not support setting port")); |
| 1328 | case VARIANT_SSH: |
| 1329 | strvec_push(args, "-p"); |
| 1330 | break; |
| 1331 | case VARIANT_PLINK: |
| 1332 | case VARIANT_PUTTY: |
| 1333 | case VARIANT_TORTOISEPLINK: |
| 1334 | strvec_push(args, "-P"); |
| 1335 | } |
| 1336 | |
| 1337 | strvec_push(args, port); |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | /* Prepare a child_process for use by Git's SSH-tunneled transport. */ |
| 1342 | static void fill_ssh_args(struct child_process *conn, const char *ssh_host, |
| 1343 | const char *port, enum protocol_version version, |
| 1344 | int flags) |
| 1345 | { |
| 1346 | const char *ssh; |
| 1347 | enum ssh_variant variant; |
| 1348 | |
| 1349 | if (looks_like_command_line_option(ssh_host)) |
| 1350 | die(_("strange hostname '%s' blocked"), ssh_host); |
| 1351 | |
| 1352 | ssh = get_ssh_command(); |
| 1353 | if (ssh) { |
| 1354 | variant = determine_ssh_variant(ssh, 1); |
| 1355 | } else { |
| 1356 | /* |
| 1357 | * GIT_SSH is the no-shell version of |
| 1358 | * GIT_SSH_COMMAND (and must remain so for |
| 1359 | * historical compatibility). |
| 1360 | */ |
| 1361 | conn->use_shell = 0; |
| 1362 | |
| 1363 | ssh = getenv("GIT_SSH"); |
| 1364 | if (!ssh) |
| 1365 | ssh = "ssh"; |
| 1366 | variant = determine_ssh_variant(ssh, 0); |
| 1367 | } |
| 1368 | |
| 1369 | if (variant == VARIANT_AUTO) { |
| 1370 | struct child_process detect = CHILD_PROCESS_INIT; |
| 1371 | |
| 1372 | detect.use_shell = conn->use_shell; |
| 1373 | detect.no_stdin = detect.no_stdout = detect.no_stderr = 1; |
| 1374 | |
| 1375 | strvec_push(&detect.args, ssh); |
| 1376 | strvec_push(&detect.args, "-G"); |
| 1377 | push_ssh_options(&detect.args, &detect.env, |
| 1378 | VARIANT_SSH, port, version, flags); |
| 1379 | strvec_push(&detect.args, ssh_host); |
| 1380 | |
| 1381 | variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH; |
| 1382 | } |
| 1383 | |
| 1384 | strvec_push(&conn->args, ssh); |
| 1385 | push_ssh_options(&conn->args, &conn->env, variant, port, version, |
| 1386 | flags); |
| 1387 | strvec_push(&conn->args, ssh_host); |
| 1388 | } |
| 1389 | |
| 1390 | /* |
| 1391 | * This returns the dummy child_process `no_fork` if the transport protocol |
| 1392 | * does not need fork(2), or a struct child_process object if it does. Once |
| 1393 | * done, finish the connection with finish_connect() with the value returned |
| 1394 | * from this function (it is safe to call finish_connect() with NULL to |
| 1395 | * support the former case). |
| 1396 | * |
| 1397 | * If it returns, the connect is successful; it just dies on errors (this |
| 1398 | * will hopefully be changed in a libification effort, to return NULL when |
| 1399 | * the connection failed). |
| 1400 | */ |
| 1401 | struct child_process *git_connect(int fd[2], const char *url, |
| 1402 | enum git_connect_service service, |
| 1403 | const char *prog, int flags) |
| 1404 | { |
| 1405 | char *hostandport, *path; |
| 1406 | struct child_process *conn; |
| 1407 | enum url_scheme scheme; |
| 1408 | enum protocol_version version = get_protocol_version_config(); |
| 1409 | |
| 1410 | /* |
| 1411 | * NEEDSWORK: If we are trying to use protocol v2 and we are planning |
| 1412 | * to perform any operation that doesn't involve upload-pack (i.e., a |
| 1413 | * fetch, ls-remote, etc), then fallback to v0 since we don't know how |
| 1414 | * to do anything else (like push or remote archive) via v2. |
| 1415 | */ |
| 1416 | if (version == protocol_v2 && service != GIT_CONNECT_UPLOAD_PACK) |
| 1417 | version = protocol_v0; |
| 1418 | |
| 1419 | /* Without this we cannot rely on waitpid() to tell |
| 1420 | * what happened to our children. |
| 1421 | */ |
| 1422 | signal(SIGCHLD, SIG_DFL); |
| 1423 | |
| 1424 | scheme = parse_connect_url(url, &hostandport, &path); |
| 1425 | if ((flags & CONNECT_DIAG_URL) && (scheme != URL_SCHEME_SSH)) { |
| 1426 | printf("Diag: url=%s\n", url ? url : "NULL"); |
| 1427 | printf("Diag: protocol=%s\n", url_scheme_name(scheme)); |
| 1428 | printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL"); |
| 1429 | printf("Diag: path=%s\n", path ? path : "NULL"); |
| 1430 | conn = NULL; |
| 1431 | } else if (scheme == URL_SCHEME_GIT) { |
| 1432 | conn = git_connect_git(fd, hostandport, path, prog, version, flags); |
| 1433 | conn->trace2_child_class = "transport/git"; |
| 1434 | } else { |
| 1435 | struct strbuf cmd = STRBUF_INIT; |
| 1436 | const char *const *var; |
| 1437 | |
| 1438 | conn = xmalloc(sizeof(*conn)); |
| 1439 | child_process_init(conn); |
| 1440 | |
| 1441 | if (looks_like_command_line_option(path)) |
| 1442 | die(_("strange pathname '%s' blocked"), path); |
| 1443 | |
| 1444 | strbuf_addstr(&cmd, prog); |
| 1445 | strbuf_addch(&cmd, ' '); |
| 1446 | sq_quote_buf(&cmd, path); |
| 1447 | |
| 1448 | /* remove repo-local variables from the environment */ |
| 1449 | for (var = local_repo_env; *var; var++) |
| 1450 | strvec_push(&conn->env, *var); |
| 1451 | |
| 1452 | conn->use_shell = 1; |
| 1453 | conn->in = conn->out = -1; |
| 1454 | if (scheme == URL_SCHEME_SSH) { |
| 1455 | char *ssh_host = hostandport; |
| 1456 | const char *port = NULL; |
| 1457 | transport_check_allowed("ssh"); |
| 1458 | get_host_and_port(&ssh_host, &port); |
| 1459 | |
| 1460 | if (!port) |
| 1461 | port = get_port(ssh_host); |
| 1462 | |
| 1463 | if (flags & CONNECT_DIAG_URL) { |
| 1464 | printf("Diag: url=%s\n", url ? url : "NULL"); |
| 1465 | printf("Diag: protocol=%s\n", url_scheme_name(scheme)); |
| 1466 | printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL"); |
| 1467 | printf("Diag: port=%s\n", port ? port : "NONE"); |
| 1468 | printf("Diag: path=%s\n", path ? path : "NULL"); |
| 1469 | |
| 1470 | free(hostandport); |
| 1471 | free(path); |
| 1472 | child_process_clear(conn); |
| 1473 | free(conn); |
| 1474 | strbuf_release(&cmd); |
| 1475 | return NULL; |
| 1476 | } |
| 1477 | conn->trace2_child_class = "transport/ssh"; |
| 1478 | fill_ssh_args(conn, ssh_host, port, version, flags); |
| 1479 | } else { |
| 1480 | transport_check_allowed("file"); |
| 1481 | conn->trace2_child_class = "transport/file"; |
| 1482 | if (version > 0) { |
| 1483 | strvec_pushf(&conn->env, |
| 1484 | GIT_PROTOCOL_ENVIRONMENT "=version=%d", |
| 1485 | version); |
| 1486 | } |
| 1487 | } |
| 1488 | strvec_push(&conn->args, cmd.buf); |
| 1489 | |
| 1490 | if (start_command(conn)) |
| 1491 | die(_("unable to fork")); |
| 1492 | |
| 1493 | fd[0] = conn->out; /* read from child's stdout */ |
| 1494 | fd[1] = conn->in; /* write to child's stdin */ |
| 1495 | strbuf_release(&cmd); |
| 1496 | } |
| 1497 | free(hostandport); |
| 1498 | free(path); |
| 1499 | return conn; |
| 1500 | } |
| 1501 | |
| 1502 | int finish_connect(struct child_process *conn) |
| 1503 | { |
| 1504 | int code; |
| 1505 | if (!conn || git_connection_is_socket(conn)) |
| 1506 | return 0; |
| 1507 | |
| 1508 | code = finish_command(conn); |
| 1509 | free(conn); |
| 1510 | return code; |
| 1511 | } |