connect.con commit log: properly handle decorations with chained tags (5e1361c)
   1#include "git-compat-util.h"
   2#include "cache.h"
   3#include "pkt-line.h"
   4#include "quote.h"
   5#include "refs.h"
   6#include "run-command.h"
   7#include "remote.h"
   8#include "url.h"
   9#include "string-list.h"
  10
  11static char *server_capabilities;
  12static const char *parse_feature_value(const char *, const char *, int *);
  13
  14static int check_ref(const char *name, int len, unsigned int flags)
  15{
  16        if (!flags)
  17                return 1;
  18
  19        if (len < 5 || memcmp(name, "refs/", 5))
  20                return 0;
  21
  22        /* Skip the "refs/" part */
  23        name += 5;
  24        len -= 5;
  25
  26        /* REF_NORMAL means that we don't want the magic fake tag refs */
  27        if ((flags & REF_NORMAL) && check_refname_format(name, 0))
  28                return 0;
  29
  30        /* REF_HEADS means that we want regular branch heads */
  31        if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
  32                return 1;
  33
  34        /* REF_TAGS means that we want tags */
  35        if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
  36                return 1;
  37
  38        /* All type bits clear means that we are ok with anything */
  39        return !(flags & ~REF_NORMAL);
  40}
  41
  42int check_ref_type(const struct ref *ref, int flags)
  43{
  44        return check_ref(ref->name, strlen(ref->name), flags);
  45}
  46
  47static void add_extra_have(struct extra_have_objects *extra, unsigned char *sha1)
  48{
  49        ALLOC_GROW(extra->array, extra->nr + 1, extra->alloc);
  50        hashcpy(&(extra->array[extra->nr][0]), sha1);
  51        extra->nr++;
  52}
  53
  54static void die_initial_contact(int got_at_least_one_head)
  55{
  56        if (got_at_least_one_head)
  57                die("The remote end hung up upon initial contact");
  58        else
  59                die("Could not read from remote repository.\n\n"
  60                    "Please make sure you have the correct access rights\n"
  61                    "and the repository exists.");
  62}
  63
  64static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
  65{
  66        char *sym, *target;
  67        struct string_list_item *item;
  68
  69        if (!len)
  70                return; /* just "symref" */
  71        /* e.g. "symref=HEAD:refs/heads/master" */
  72        sym = xmalloc(len + 1);
  73        memcpy(sym, val, len);
  74        sym[len] = '\0';
  75        target = strchr(sym, ':');
  76        if (!target)
  77                /* just "symref=something" */
  78                goto reject;
  79        *(target++) = '\0';
  80        if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
  81            check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
  82                /* "symref=bogus:pair */
  83                goto reject;
  84        item = string_list_append(symref, sym);
  85        item->util = target;
  86        return;
  87reject:
  88        free(sym);
  89        return;
  90}
  91
  92static void annotate_refs_with_symref_info(struct ref *ref)
  93{
  94        struct string_list symref = STRING_LIST_INIT_DUP;
  95        const char *feature_list = server_capabilities;
  96
  97        while (feature_list) {
  98                int len;
  99                const char *val;
 100
 101                val = parse_feature_value(feature_list, "symref", &len);
 102                if (!val)
 103                        break;
 104                parse_one_symref_info(&symref, val, len);
 105                feature_list = val + 1;
 106        }
 107        sort_string_list(&symref);
 108
 109        for (; ref; ref = ref->next) {
 110                struct string_list_item *item;
 111                item = string_list_lookup(&symref, ref->name);
 112                if (!item)
 113                        continue;
 114                ref->symref = xstrdup((char *)item->util);
 115        }
 116        string_list_clear(&symref, 0);
 117}
 118
 119/*
 120 * Read all the refs from the other end
 121 */
 122struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
 123                              struct ref **list, unsigned int flags,
 124                              struct extra_have_objects *extra_have)
 125{
 126        struct ref **orig_list = list;
 127        int got_at_least_one_head = 0;
 128
 129        *list = NULL;
 130        for (;;) {
 131                struct ref *ref;
 132                unsigned char old_sha1[20];
 133                char *name;
 134                int len, name_len;
 135                char *buffer = packet_buffer;
 136
 137                len = packet_read(in, &src_buf, &src_len,
 138                                  packet_buffer, sizeof(packet_buffer),
 139                                  PACKET_READ_GENTLE_ON_EOF |
 140                                  PACKET_READ_CHOMP_NEWLINE);
 141                if (len < 0)
 142                        die_initial_contact(got_at_least_one_head);
 143
 144                if (!len)
 145                        break;
 146
 147                if (len > 4 && !prefixcmp(buffer, "ERR "))
 148                        die("remote error: %s", buffer + 4);
 149
 150                if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
 151                        die("protocol error: expected sha/ref, got '%s'", buffer);
 152                name = buffer + 41;
 153
 154                name_len = strlen(name);
 155                if (len != name_len + 41) {
 156                        free(server_capabilities);
 157                        server_capabilities = xstrdup(name + name_len + 1);
 158                }
 159
 160                if (extra_have &&
 161                    name_len == 5 && !memcmp(".have", name, 5)) {
 162                        add_extra_have(extra_have, old_sha1);
 163                        continue;
 164                }
 165
 166                if (!check_ref(name, name_len, flags))
 167                        continue;
 168                ref = alloc_ref(buffer + 41);
 169                hashcpy(ref->old_sha1, old_sha1);
 170                *list = ref;
 171                list = &ref->next;
 172                got_at_least_one_head = 1;
 173        }
 174
 175        annotate_refs_with_symref_info(*orig_list);
 176
 177        return list;
 178}
 179
 180static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
 181{
 182        int len;
 183
 184        if (!feature_list)
 185                return NULL;
 186
 187        len = strlen(feature);
 188        while (*feature_list) {
 189                const char *found = strstr(feature_list, feature);
 190                if (!found)
 191                        return NULL;
 192                if (feature_list == found || isspace(found[-1])) {
 193                        const char *value = found + len;
 194                        /* feature with no value (e.g., "thin-pack") */
 195                        if (!*value || isspace(*value)) {
 196                                if (lenp)
 197                                        *lenp = 0;
 198                                return value;
 199                        }
 200                        /* feature with a value (e.g., "agent=git/1.2.3") */
 201                        else if (*value == '=') {
 202                                value++;
 203                                if (lenp)
 204                                        *lenp = strcspn(value, " \t\n");
 205                                return value;
 206                        }
 207                        /*
 208                         * otherwise we matched a substring of another feature;
 209                         * keep looking
 210                         */
 211                }
 212                feature_list = found + 1;
 213        }
 214        return NULL;
 215}
 216
 217int parse_feature_request(const char *feature_list, const char *feature)
 218{
 219        return !!parse_feature_value(feature_list, feature, NULL);
 220}
 221
 222const char *server_feature_value(const char *feature, int *len)
 223{
 224        return parse_feature_value(server_capabilities, feature, len);
 225}
 226
 227int server_supports(const char *feature)
 228{
 229        return !!server_feature_value(feature, NULL);
 230}
 231
 232enum protocol {
 233        PROTO_LOCAL = 1,
 234        PROTO_SSH,
 235        PROTO_GIT
 236};
 237
 238static enum protocol get_protocol(const char *name)
 239{
 240        if (!strcmp(name, "ssh"))
 241                return PROTO_SSH;
 242        if (!strcmp(name, "git"))
 243                return PROTO_GIT;
 244        if (!strcmp(name, "git+ssh"))
 245                return PROTO_SSH;
 246        if (!strcmp(name, "ssh+git"))
 247                return PROTO_SSH;
 248        if (!strcmp(name, "file"))
 249                return PROTO_LOCAL;
 250        die("I don't handle protocol '%s'", name);
 251}
 252
 253#define STR_(s) # s
 254#define STR(s)  STR_(s)
 255
 256static void get_host_and_port(char **host, const char **port)
 257{
 258        char *colon, *end;
 259
 260        if (*host[0] == '[') {
 261                end = strchr(*host + 1, ']');
 262                if (end) {
 263                        *end = 0;
 264                        end++;
 265                        (*host)++;
 266                } else
 267                        end = *host;
 268        } else
 269                end = *host;
 270        colon = strchr(end, ':');
 271
 272        if (colon) {
 273                *colon = 0;
 274                *port = colon + 1;
 275        }
 276}
 277
 278static void enable_keepalive(int sockfd)
 279{
 280        int ka = 1;
 281
 282        if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
 283                fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
 284                        strerror(errno));
 285}
 286
 287#ifndef NO_IPV6
 288
 289static const char *ai_name(const struct addrinfo *ai)
 290{
 291        static char addr[NI_MAXHOST];
 292        if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
 293                        NI_NUMERICHOST) != 0)
 294                strcpy(addr, "(unknown)");
 295
 296        return addr;
 297}
 298
 299/*
 300 * Returns a connected socket() fd, or else die()s.
 301 */
 302static int git_tcp_connect_sock(char *host, int flags)
 303{
 304        struct strbuf error_message = STRBUF_INIT;
 305        int sockfd = -1;
 306        const char *port = STR(DEFAULT_GIT_PORT);
 307        struct addrinfo hints, *ai0, *ai;
 308        int gai;
 309        int cnt = 0;
 310
 311        get_host_and_port(&host, &port);
 312        if (!*port)
 313                port = "<none>";
 314
 315        memset(&hints, 0, sizeof(hints));
 316        hints.ai_socktype = SOCK_STREAM;
 317        hints.ai_protocol = IPPROTO_TCP;
 318
 319        if (flags & CONNECT_VERBOSE)
 320                fprintf(stderr, "Looking up %s ... ", host);
 321
 322        gai = getaddrinfo(host, port, &hints, &ai);
 323        if (gai)
 324                die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
 325
 326        if (flags & CONNECT_VERBOSE)
 327                fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
 328
 329        for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
 330                sockfd = socket(ai->ai_family,
 331                                ai->ai_socktype, ai->ai_protocol);
 332                if ((sockfd < 0) ||
 333                    (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
 334                        strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
 335                                    host, cnt, ai_name(ai), strerror(errno));
 336                        if (0 <= sockfd)
 337                                close(sockfd);
 338                        sockfd = -1;
 339                        continue;
 340                }
 341                if (flags & CONNECT_VERBOSE)
 342                        fprintf(stderr, "%s ", ai_name(ai));
 343                break;
 344        }
 345
 346        freeaddrinfo(ai0);
 347
 348        if (sockfd < 0)
 349                die("unable to connect to %s:\n%s", host, error_message.buf);
 350
 351        enable_keepalive(sockfd);
 352
 353        if (flags & CONNECT_VERBOSE)
 354                fprintf(stderr, "done.\n");
 355
 356        strbuf_release(&error_message);
 357
 358        return sockfd;
 359}
 360
 361#else /* NO_IPV6 */
 362
 363/*
 364 * Returns a connected socket() fd, or else die()s.
 365 */
 366static int git_tcp_connect_sock(char *host, int flags)
 367{
 368        struct strbuf error_message = STRBUF_INIT;
 369        int sockfd = -1;
 370        const char *port = STR(DEFAULT_GIT_PORT);
 371        char *ep;
 372        struct hostent *he;
 373        struct sockaddr_in sa;
 374        char **ap;
 375        unsigned int nport;
 376        int cnt;
 377
 378        get_host_and_port(&host, &port);
 379
 380        if (flags & CONNECT_VERBOSE)
 381                fprintf(stderr, "Looking up %s ... ", host);
 382
 383        he = gethostbyname(host);
 384        if (!he)
 385                die("Unable to look up %s (%s)", host, hstrerror(h_errno));
 386        nport = strtoul(port, &ep, 10);
 387        if ( ep == port || *ep ) {
 388                /* Not numeric */
 389                struct servent *se = getservbyname(port,"tcp");
 390                if ( !se )
 391                        die("Unknown port %s", port);
 392                nport = se->s_port;
 393        }
 394
 395        if (flags & CONNECT_VERBOSE)
 396                fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
 397
 398        for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
 399                memset(&sa, 0, sizeof sa);
 400                sa.sin_family = he->h_addrtype;
 401                sa.sin_port = htons(nport);
 402                memcpy(&sa.sin_addr, *ap, he->h_length);
 403
 404                sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
 405                if ((sockfd < 0) ||
 406                    connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
 407                        strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
 408                                host,
 409                                cnt,
 410                                inet_ntoa(*(struct in_addr *)&sa.sin_addr),
 411                                strerror(errno));
 412                        if (0 <= sockfd)
 413                                close(sockfd);
 414                        sockfd = -1;
 415                        continue;
 416                }
 417                if (flags & CONNECT_VERBOSE)
 418                        fprintf(stderr, "%s ",
 419                                inet_ntoa(*(struct in_addr *)&sa.sin_addr));
 420                break;
 421        }
 422
 423        if (sockfd < 0)
 424                die("unable to connect to %s:\n%s", host, error_message.buf);
 425
 426        enable_keepalive(sockfd);
 427
 428        if (flags & CONNECT_VERBOSE)
 429                fprintf(stderr, "done.\n");
 430
 431        return sockfd;
 432}
 433
 434#endif /* NO_IPV6 */
 435
 436
 437static void git_tcp_connect(int fd[2], char *host, int flags)
 438{
 439        int sockfd = git_tcp_connect_sock(host, flags);
 440
 441        fd[0] = sockfd;
 442        fd[1] = dup(sockfd);
 443}
 444
 445
 446static char *git_proxy_command;
 447
 448static int git_proxy_command_options(const char *var, const char *value,
 449                void *cb)
 450{
 451        if (!strcmp(var, "core.gitproxy")) {
 452                const char *for_pos;
 453                int matchlen = -1;
 454                int hostlen;
 455                const char *rhost_name = cb;
 456                int rhost_len = strlen(rhost_name);
 457
 458                if (git_proxy_command)
 459                        return 0;
 460                if (!value)
 461                        return config_error_nonbool(var);
 462                /* [core]
 463                 * ;# matches www.kernel.org as well
 464                 * gitproxy = netcatter-1 for kernel.org
 465                 * gitproxy = netcatter-2 for sample.xz
 466                 * gitproxy = netcatter-default
 467                 */
 468                for_pos = strstr(value, " for ");
 469                if (!for_pos)
 470                        /* matches everybody */
 471                        matchlen = strlen(value);
 472                else {
 473                        hostlen = strlen(for_pos + 5);
 474                        if (rhost_len < hostlen)
 475                                matchlen = -1;
 476                        else if (!strncmp(for_pos + 5,
 477                                          rhost_name + rhost_len - hostlen,
 478                                          hostlen) &&
 479                                 ((rhost_len == hostlen) ||
 480                                  rhost_name[rhost_len - hostlen -1] == '.'))
 481                                matchlen = for_pos - value;
 482                        else
 483                                matchlen = -1;
 484                }
 485                if (0 <= matchlen) {
 486                        /* core.gitproxy = none for kernel.org */
 487                        if (matchlen == 4 &&
 488                            !memcmp(value, "none", 4))
 489                                matchlen = 0;
 490                        git_proxy_command = xmemdupz(value, matchlen);
 491                }
 492                return 0;
 493        }
 494
 495        return git_default_config(var, value, cb);
 496}
 497
 498static int git_use_proxy(const char *host)
 499{
 500        git_proxy_command = getenv("GIT_PROXY_COMMAND");
 501        git_config(git_proxy_command_options, (void*)host);
 502        return (git_proxy_command && *git_proxy_command);
 503}
 504
 505static struct child_process *git_proxy_connect(int fd[2], char *host)
 506{
 507        const char *port = STR(DEFAULT_GIT_PORT);
 508        const char **argv;
 509        struct child_process *proxy;
 510
 511        get_host_and_port(&host, &port);
 512
 513        argv = xmalloc(sizeof(*argv) * 4);
 514        argv[0] = git_proxy_command;
 515        argv[1] = host;
 516        argv[2] = port;
 517        argv[3] = NULL;
 518        proxy = xcalloc(1, sizeof(*proxy));
 519        proxy->argv = argv;
 520        proxy->in = -1;
 521        proxy->out = -1;
 522        if (start_command(proxy))
 523                die("cannot start proxy %s", argv[0]);
 524        fd[0] = proxy->out; /* read from proxy stdout */
 525        fd[1] = proxy->in;  /* write to proxy stdin */
 526        return proxy;
 527}
 528
 529#define MAX_CMD_LEN 1024
 530
 531static char *get_port(char *host)
 532{
 533        char *end;
 534        char *p = strchr(host, ':');
 535
 536        if (p) {
 537                long port = strtol(p + 1, &end, 10);
 538                if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
 539                        *p = '\0';
 540                        return p+1;
 541                }
 542        }
 543
 544        return NULL;
 545}
 546
 547static struct child_process no_fork;
 548
 549/*
 550 * This returns a dummy child_process if the transport protocol does not
 551 * need fork(2), or a struct child_process object if it does.  Once done,
 552 * finish the connection with finish_connect() with the value returned from
 553 * this function (it is safe to call finish_connect() with NULL to support
 554 * the former case).
 555 *
 556 * If it returns, the connect is successful; it just dies on errors (this
 557 * will hopefully be changed in a libification effort, to return NULL when
 558 * the connection failed).
 559 */
 560struct child_process *git_connect(int fd[2], const char *url_orig,
 561                                  const char *prog, int flags)
 562{
 563        char *url;
 564        char *host, *path;
 565        char *end;
 566        int c;
 567        struct child_process *conn = &no_fork;
 568        enum protocol protocol = PROTO_LOCAL;
 569        int free_path = 0;
 570        char *port = NULL;
 571        const char **arg;
 572        struct strbuf cmd;
 573
 574        /* Without this we cannot rely on waitpid() to tell
 575         * what happened to our children.
 576         */
 577        signal(SIGCHLD, SIG_DFL);
 578
 579        if (is_url(url_orig))
 580                url = url_decode(url_orig);
 581        else
 582                url = xstrdup(url_orig);
 583
 584        host = strstr(url, "://");
 585        if (host) {
 586                *host = '\0';
 587                protocol = get_protocol(url);
 588                host += 3;
 589                c = '/';
 590        } else {
 591                host = url;
 592                c = ':';
 593        }
 594
 595        /*
 596         * Don't do destructive transforms with git:// as that
 597         * protocol code does '[]' unwrapping of its own.
 598         */
 599        if (host[0] == '[') {
 600                end = strchr(host + 1, ']');
 601                if (end) {
 602                        if (protocol != PROTO_GIT) {
 603                                *end = 0;
 604                                host++;
 605                        }
 606                        end++;
 607                } else
 608                        end = host;
 609        } else
 610                end = host;
 611
 612        path = strchr(end, c);
 613        if (path && !has_dos_drive_prefix(end)) {
 614                if (c == ':') {
 615                        if (path < strchrnul(host, '/')) {
 616                                protocol = PROTO_SSH;
 617                                *path++ = '\0';
 618                        } else /* '/' in the host part, assume local path */
 619                                path = end;
 620                }
 621        } else
 622                path = end;
 623
 624        if (!path || !*path)
 625                die("No path specified. See 'man git-pull' for valid url syntax");
 626
 627        /*
 628         * null-terminate hostname and point path to ~ for URL's like this:
 629         *    ssh://host.xz/~user/repo
 630         */
 631        if (protocol != PROTO_LOCAL && host != url) {
 632                char *ptr = path;
 633                if (path[1] == '~')
 634                        path++;
 635                else {
 636                        path = xstrdup(ptr);
 637                        free_path = 1;
 638                }
 639
 640                *ptr = '\0';
 641        }
 642
 643        /*
 644         * Add support for ssh port: ssh://host.xy:<port>/...
 645         */
 646        if (protocol == PROTO_SSH && host != url)
 647                port = get_port(end);
 648
 649        if (protocol == PROTO_GIT) {
 650                /* These underlying connection commands die() if they
 651                 * cannot connect.
 652                 */
 653                char *target_host = xstrdup(host);
 654                if (git_use_proxy(host))
 655                        conn = git_proxy_connect(fd, host);
 656                else
 657                        git_tcp_connect(fd, host, flags);
 658                /*
 659                 * Separate original protocol components prog and path
 660                 * from extended host header with a NUL byte.
 661                 *
 662                 * Note: Do not add any other headers here!  Doing so
 663                 * will cause older git-daemon servers to crash.
 664                 */
 665                packet_write(fd[1],
 666                             "%s %s%chost=%s%c",
 667                             prog, path, 0,
 668                             target_host, 0);
 669                free(target_host);
 670                free(url);
 671                if (free_path)
 672                        free(path);
 673                return conn;
 674        }
 675
 676        conn = xcalloc(1, sizeof(*conn));
 677
 678        strbuf_init(&cmd, MAX_CMD_LEN);
 679        strbuf_addstr(&cmd, prog);
 680        strbuf_addch(&cmd, ' ');
 681        sq_quote_buf(&cmd, path);
 682        if (cmd.len >= MAX_CMD_LEN)
 683                die("command line too long");
 684
 685        conn->in = conn->out = -1;
 686        conn->argv = arg = xcalloc(7, sizeof(*arg));
 687        if (protocol == PROTO_SSH) {
 688                const char *ssh = getenv("GIT_SSH");
 689                int putty = ssh && strcasestr(ssh, "plink");
 690                if (!ssh) ssh = "ssh";
 691
 692                *arg++ = ssh;
 693                if (putty && !strcasestr(ssh, "tortoiseplink"))
 694                        *arg++ = "-batch";
 695                if (port) {
 696                        /* P is for PuTTY, p is for OpenSSH */
 697                        *arg++ = putty ? "-P" : "-p";
 698                        *arg++ = port;
 699                }
 700                *arg++ = host;
 701        }
 702        else {
 703                /* remove repo-local variables from the environment */
 704                conn->env = local_repo_env;
 705                conn->use_shell = 1;
 706        }
 707        *arg++ = cmd.buf;
 708        *arg = NULL;
 709
 710        if (start_command(conn))
 711                die("unable to fork");
 712
 713        fd[0] = conn->out; /* read from child's stdout */
 714        fd[1] = conn->in;  /* write to child's stdin */
 715        strbuf_release(&cmd);
 716        free(url);
 717        if (free_path)
 718                free(path);
 719        return conn;
 720}
 721
 722int git_connection_is_socket(struct child_process *conn)
 723{
 724        return conn == &no_fork;
 725}
 726
 727int finish_connect(struct child_process *conn)
 728{
 729        int code;
 730        if (!conn || git_connection_is_socket(conn))
 731                return 0;
 732
 733        code = finish_command(conn);
 734        free(conn->argv);
 735        free(conn);
 736        return code;
 737}