connect.con commit Merge branch 'maint' (7a6a5e4)
   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 <sys/wait.h>
   7#include <sys/socket.h>
   8#include <netinet/in.h>
   9#include <arpa/inet.h>
  10#include <netdb.h>
  11#include <signal.h>
  12
  13static char *server_capabilities;
  14
  15static int check_ref(const char *name, int len, unsigned int flags)
  16{
  17        if (!flags)
  18                return 1;
  19
  20        if (len < 5 || memcmp(name, "refs/", 5))
  21                return 0;
  22
  23        /* Skip the "refs/" part */
  24        name += 5;
  25        len -= 5;
  26
  27        /* REF_NORMAL means that we don't want the magic fake tag refs */
  28        if ((flags & REF_NORMAL) && check_ref_format(name) < 0)
  29                return 0;
  30
  31        /* REF_HEADS means that we want regular branch heads */
  32        if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
  33                return 1;
  34
  35        /* REF_TAGS means that we want tags */
  36        if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
  37                return 1;
  38
  39        /* All type bits clear means that we are ok with anything */
  40        return !(flags & ~REF_NORMAL);
  41}
  42
  43/*
  44 * Read all the refs from the other end
  45 */
  46struct ref **get_remote_heads(int in, struct ref **list,
  47                              int nr_match, char **match,
  48                              unsigned int flags)
  49{
  50        *list = NULL;
  51        for (;;) {
  52                struct ref *ref;
  53                unsigned char old_sha1[20];
  54                static char buffer[1000];
  55                char *name;
  56                int len, name_len;
  57
  58                len = packet_read_line(in, buffer, sizeof(buffer));
  59                if (!len)
  60                        break;
  61                if (buffer[len-1] == '\n')
  62                        buffer[--len] = 0;
  63
  64                if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
  65                        die("protocol error: expected sha/ref, got '%s'", buffer);
  66                name = buffer + 41;
  67
  68                name_len = strlen(name);
  69                if (len != name_len + 41) {
  70                        if (server_capabilities)
  71                                free(server_capabilities);
  72                        server_capabilities = xstrdup(name + name_len + 1);
  73                }
  74
  75                if (!check_ref(name, name_len, flags))
  76                        continue;
  77                if (nr_match && !path_match(name, nr_match, match))
  78                        continue;
  79                ref = xcalloc(1, sizeof(*ref) + len - 40);
  80                hashcpy(ref->old_sha1, old_sha1);
  81                memcpy(ref->name, buffer + 41, len - 40);
  82                *list = ref;
  83                list = &ref->next;
  84        }
  85        return list;
  86}
  87
  88int server_supports(const char *feature)
  89{
  90        return server_capabilities &&
  91                strstr(server_capabilities, feature) != NULL;
  92}
  93
  94int get_ack(int fd, unsigned char *result_sha1)
  95{
  96        static char line[1000];
  97        int len = packet_read_line(fd, line, sizeof(line));
  98
  99        if (!len)
 100                die("git-fetch-pack: expected ACK/NAK, got EOF");
 101        if (line[len-1] == '\n')
 102                line[--len] = 0;
 103        if (!strcmp(line, "NAK"))
 104                return 0;
 105        if (!strncmp(line, "ACK ", 4)) {
 106                if (!get_sha1_hex(line+4, result_sha1)) {
 107                        if (strstr(line+45, "continue"))
 108                                return 2;
 109                        return 1;
 110                }
 111        }
 112        die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
 113}
 114
 115int path_match(const char *path, int nr, char **match)
 116{
 117        int i;
 118        int pathlen = strlen(path);
 119
 120        for (i = 0; i < nr; i++) {
 121                char *s = match[i];
 122                int len = strlen(s);
 123
 124                if (!len || len > pathlen)
 125                        continue;
 126                if (memcmp(path + pathlen - len, s, len))
 127                        continue;
 128                if (pathlen > len && path[pathlen - len - 1] != '/')
 129                        continue;
 130                *s = 0;
 131                return (i + 1);
 132        }
 133        return 0;
 134}
 135
 136struct refspec {
 137        char *src;
 138        char *dst;
 139        char force;
 140};
 141
 142/*
 143 * A:B means fast forward remote B with local A.
 144 * +A:B means overwrite remote B with local A.
 145 * +A is a shorthand for +A:A.
 146 * A is a shorthand for A:A.
 147 */
 148static struct refspec *parse_ref_spec(int nr_refspec, char **refspec)
 149{
 150        int i;
 151        struct refspec *rs = xcalloc(sizeof(*rs), (nr_refspec + 1));
 152        for (i = 0; i < nr_refspec; i++) {
 153                char *sp, *dp, *ep;
 154                sp = refspec[i];
 155                if (*sp == '+') {
 156                        rs[i].force = 1;
 157                        sp++;
 158                }
 159                ep = strchr(sp, ':');
 160                if (ep) {
 161                        dp = ep + 1;
 162                        *ep = 0;
 163                }
 164                else
 165                        dp = sp;
 166                rs[i].src = sp;
 167                rs[i].dst = dp;
 168        }
 169        rs[nr_refspec].src = rs[nr_refspec].dst = NULL;
 170        return rs;
 171}
 172
 173static int count_refspec_match(const char *pattern,
 174                               struct ref *refs,
 175                               struct ref **matched_ref)
 176{
 177        int match;
 178        int patlen = strlen(pattern);
 179
 180        for (match = 0; refs; refs = refs->next) {
 181                char *name = refs->name;
 182                int namelen = strlen(name);
 183                if (namelen < patlen ||
 184                    memcmp(name + namelen - patlen, pattern, patlen))
 185                        continue;
 186                if (namelen != patlen && name[namelen - patlen - 1] != '/')
 187                        continue;
 188                match++;
 189                *matched_ref = refs;
 190        }
 191        return match;
 192}
 193
 194static void link_dst_tail(struct ref *ref, struct ref ***tail)
 195{
 196        **tail = ref;
 197        *tail = &ref->next;
 198        **tail = NULL;
 199}
 200
 201static struct ref *try_explicit_object_name(const char *name)
 202{
 203        unsigned char sha1[20];
 204        struct ref *ref;
 205        int len;
 206        if (get_sha1(name, sha1))
 207                return NULL;
 208        len = strlen(name) + 1;
 209        ref = xcalloc(1, sizeof(*ref) + len);
 210        memcpy(ref->name, name, len);
 211        hashcpy(ref->new_sha1, sha1);
 212        return ref;
 213}
 214
 215static int match_explicit_refs(struct ref *src, struct ref *dst,
 216                               struct ref ***dst_tail, struct refspec *rs)
 217{
 218        int i, errs;
 219        for (i = errs = 0; rs[i].src; i++) {
 220                struct ref *matched_src, *matched_dst;
 221
 222                matched_src = matched_dst = NULL;
 223                switch (count_refspec_match(rs[i].src, src, &matched_src)) {
 224                case 1:
 225                        break;
 226                case 0:
 227                        /* The source could be in the get_sha1() format
 228                         * not a reference name.
 229                         */
 230                        matched_src = try_explicit_object_name(rs[i].src);
 231                        if (matched_src)
 232                                break;
 233                        errs = 1;
 234                        error("src refspec %s does not match any.",
 235                              rs[i].src);
 236                        break;
 237                default:
 238                        errs = 1;
 239                        error("src refspec %s matches more than one.",
 240                              rs[i].src);
 241                        break;
 242                }
 243                switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) {
 244                case 1:
 245                        break;
 246                case 0:
 247                        if (!memcmp(rs[i].dst, "refs/", 5)) {
 248                                int len = strlen(rs[i].dst) + 1;
 249                                matched_dst = xcalloc(1, sizeof(*dst) + len);
 250                                memcpy(matched_dst->name, rs[i].dst, len);
 251                                link_dst_tail(matched_dst, dst_tail);
 252                        }
 253                        else if (!strcmp(rs[i].src, rs[i].dst) &&
 254                                 matched_src) {
 255                                /* pushing "master:master" when
 256                                 * remote does not have master yet.
 257                                 */
 258                                int len = strlen(matched_src->name) + 1;
 259                                matched_dst = xcalloc(1, sizeof(*dst) + len);
 260                                memcpy(matched_dst->name, matched_src->name,
 261                                       len);
 262                                link_dst_tail(matched_dst, dst_tail);
 263                        }
 264                        else {
 265                                errs = 1;
 266                                error("dst refspec %s does not match any "
 267                                      "existing ref on the remote and does "
 268                                      "not start with refs/.", rs[i].dst);
 269                        }
 270                        break;
 271                default:
 272                        errs = 1;
 273                        error("dst refspec %s matches more than one.",
 274                              rs[i].dst);
 275                        break;
 276                }
 277                if (errs)
 278                        continue;
 279                if (matched_dst->peer_ref) {
 280                        errs = 1;
 281                        error("dst ref %s receives from more than one src.",
 282                              matched_dst->name);
 283                }
 284                else {
 285                        matched_dst->peer_ref = matched_src;
 286                        matched_dst->force = rs[i].force;
 287                }
 288        }
 289        return -errs;
 290}
 291
 292static struct ref *find_ref_by_name(struct ref *list, const char *name)
 293{
 294        for ( ; list; list = list->next)
 295                if (!strcmp(list->name, name))
 296                        return list;
 297        return NULL;
 298}
 299
 300int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
 301               int nr_refspec, char **refspec, int all)
 302{
 303        struct refspec *rs = parse_ref_spec(nr_refspec, refspec);
 304
 305        if (nr_refspec)
 306                return match_explicit_refs(src, dst, dst_tail, rs);
 307
 308        /* pick the remainder */
 309        for ( ; src; src = src->next) {
 310                struct ref *dst_peer;
 311                if (src->peer_ref)
 312                        continue;
 313                dst_peer = find_ref_by_name(dst, src->name);
 314                if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
 315                        continue;
 316                if (!dst_peer) {
 317                        /* Create a new one and link it */
 318                        int len = strlen(src->name) + 1;
 319                        dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
 320                        memcpy(dst_peer->name, src->name, len);
 321                        hashcpy(dst_peer->new_sha1, src->new_sha1);
 322                        link_dst_tail(dst_peer, dst_tail);
 323                }
 324                dst_peer->peer_ref = src;
 325        }
 326        return 0;
 327}
 328
 329enum protocol {
 330        PROTO_LOCAL = 1,
 331        PROTO_SSH,
 332        PROTO_GIT,
 333};
 334
 335static enum protocol get_protocol(const char *name)
 336{
 337        if (!strcmp(name, "ssh"))
 338                return PROTO_SSH;
 339        if (!strcmp(name, "git"))
 340                return PROTO_GIT;
 341        if (!strcmp(name, "git+ssh"))
 342                return PROTO_SSH;
 343        if (!strcmp(name, "ssh+git"))
 344                return PROTO_SSH;
 345        die("I don't handle protocol '%s'", name);
 346}
 347
 348#define STR_(s) # s
 349#define STR(s)  STR_(s)
 350
 351#ifndef NO_IPV6
 352
 353/*
 354 * Returns a connected socket() fd, or else die()s.
 355 */
 356static int git_tcp_connect_sock(char *host)
 357{
 358        int sockfd = -1, saved_errno = 0;
 359        char *colon, *end;
 360        const char *port = STR(DEFAULT_GIT_PORT);
 361        struct addrinfo hints, *ai0, *ai;
 362        int gai;
 363
 364        if (host[0] == '[') {
 365                end = strchr(host + 1, ']');
 366                if (end) {
 367                        *end = 0;
 368                        end++;
 369                        host++;
 370                } else
 371                        end = host;
 372        } else
 373                end = host;
 374        colon = strchr(end, ':');
 375
 376        if (colon) {
 377                *colon = 0;
 378                port = colon + 1;
 379        }
 380
 381        memset(&hints, 0, sizeof(hints));
 382        hints.ai_socktype = SOCK_STREAM;
 383        hints.ai_protocol = IPPROTO_TCP;
 384
 385        gai = getaddrinfo(host, port, &hints, &ai);
 386        if (gai)
 387                die("Unable to look up %s (%s)", host, gai_strerror(gai));
 388
 389        for (ai0 = ai; ai; ai = ai->ai_next) {
 390                sockfd = socket(ai->ai_family,
 391                                ai->ai_socktype, ai->ai_protocol);
 392                if (sockfd < 0) {
 393                        saved_errno = errno;
 394                        continue;
 395                }
 396                if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
 397                        saved_errno = errno;
 398                        close(sockfd);
 399                        sockfd = -1;
 400                        continue;
 401                }
 402                break;
 403        }
 404
 405        freeaddrinfo(ai0);
 406
 407        if (sockfd < 0)
 408                die("unable to connect a socket (%s)", strerror(saved_errno));
 409
 410        return sockfd;
 411}
 412
 413#else /* NO_IPV6 */
 414
 415/*
 416 * Returns a connected socket() fd, or else die()s.
 417 */
 418static int git_tcp_connect_sock(char *host)
 419{
 420        int sockfd = -1, saved_errno = 0;
 421        char *colon, *end;
 422        char *port = STR(DEFAULT_GIT_PORT), *ep;
 423        struct hostent *he;
 424        struct sockaddr_in sa;
 425        char **ap;
 426        unsigned int nport;
 427
 428        if (host[0] == '[') {
 429                end = strchr(host + 1, ']');
 430                if (end) {
 431                        *end = 0;
 432                        end++;
 433                        host++;
 434                } else
 435                        end = host;
 436        } else
 437                end = host;
 438        colon = strchr(end, ':');
 439
 440        if (colon) {
 441                *colon = 0;
 442                port = colon + 1;
 443        }
 444
 445        he = gethostbyname(host);
 446        if (!he)
 447                die("Unable to look up %s (%s)", host, hstrerror(h_errno));
 448        nport = strtoul(port, &ep, 10);
 449        if ( ep == port || *ep ) {
 450                /* Not numeric */
 451                struct servent *se = getservbyname(port,"tcp");
 452                if ( !se )
 453                        die("Unknown port %s\n", port);
 454                nport = se->s_port;
 455        }
 456
 457        for (ap = he->h_addr_list; *ap; ap++) {
 458                sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
 459                if (sockfd < 0) {
 460                        saved_errno = errno;
 461                        continue;
 462                }
 463
 464                memset(&sa, 0, sizeof sa);
 465                sa.sin_family = he->h_addrtype;
 466                sa.sin_port = htons(nport);
 467                memcpy(&sa.sin_addr, *ap, he->h_length);
 468
 469                if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
 470                        saved_errno = errno;
 471                        close(sockfd);
 472                        sockfd = -1;
 473                        continue;
 474                }
 475                break;
 476        }
 477
 478        if (sockfd < 0)
 479                die("unable to connect a socket (%s)", strerror(saved_errno));
 480
 481        return sockfd;
 482}
 483
 484#endif /* NO_IPV6 */
 485
 486
 487static void git_tcp_connect(int fd[2], char *host)
 488{
 489        int sockfd = git_tcp_connect_sock(host);
 490
 491        fd[0] = sockfd;
 492        fd[1] = sockfd;
 493}
 494
 495
 496static char *git_proxy_command;
 497static const char *rhost_name;
 498static int rhost_len;
 499
 500static int git_proxy_command_options(const char *var, const char *value)
 501{
 502        if (!strcmp(var, "core.gitproxy")) {
 503                const char *for_pos;
 504                int matchlen = -1;
 505                int hostlen;
 506
 507                if (git_proxy_command)
 508                        return 0;
 509                /* [core]
 510                 * ;# matches www.kernel.org as well
 511                 * gitproxy = netcatter-1 for kernel.org
 512                 * gitproxy = netcatter-2 for sample.xz
 513                 * gitproxy = netcatter-default
 514                 */
 515                for_pos = strstr(value, " for ");
 516                if (!for_pos)
 517                        /* matches everybody */
 518                        matchlen = strlen(value);
 519                else {
 520                        hostlen = strlen(for_pos + 5);
 521                        if (rhost_len < hostlen)
 522                                matchlen = -1;
 523                        else if (!strncmp(for_pos + 5,
 524                                          rhost_name + rhost_len - hostlen,
 525                                          hostlen) &&
 526                                 ((rhost_len == hostlen) ||
 527                                  rhost_name[rhost_len - hostlen -1] == '.'))
 528                                matchlen = for_pos - value;
 529                        else
 530                                matchlen = -1;
 531                }
 532                if (0 <= matchlen) {
 533                        /* core.gitproxy = none for kernel.org */
 534                        if (matchlen == 4 && 
 535                            !memcmp(value, "none", 4))
 536                                matchlen = 0;
 537                        git_proxy_command = xmalloc(matchlen + 1);
 538                        memcpy(git_proxy_command, value, matchlen);
 539                        git_proxy_command[matchlen] = 0;
 540                }
 541                return 0;
 542        }
 543
 544        return git_default_config(var, value);
 545}
 546
 547static int git_use_proxy(const char *host)
 548{
 549        rhost_name = host;
 550        rhost_len = strlen(host);
 551        git_proxy_command = getenv("GIT_PROXY_COMMAND");
 552        git_config(git_proxy_command_options);
 553        rhost_name = NULL;
 554        return (git_proxy_command && *git_proxy_command);
 555}
 556
 557static void git_proxy_connect(int fd[2], char *host)
 558{
 559        const char *port = STR(DEFAULT_GIT_PORT);
 560        char *colon, *end;
 561        int pipefd[2][2];
 562        pid_t pid;
 563
 564        if (host[0] == '[') {
 565                end = strchr(host + 1, ']');
 566                if (end) {
 567                        *end = 0;
 568                        end++;
 569                        host++;
 570                } else
 571                        end = host;
 572        } else
 573                end = host;
 574        colon = strchr(end, ':');
 575
 576        if (colon) {
 577                *colon = 0;
 578                port = colon + 1;
 579        }
 580
 581        if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
 582                die("unable to create pipe pair for communication");
 583        pid = fork();
 584        if (!pid) {
 585                dup2(pipefd[1][0], 0);
 586                dup2(pipefd[0][1], 1);
 587                close(pipefd[0][0]);
 588                close(pipefd[0][1]);
 589                close(pipefd[1][0]);
 590                close(pipefd[1][1]);
 591                execlp(git_proxy_command, git_proxy_command, host, port, NULL);
 592                die("exec failed");
 593        }
 594        if (pid < 0)
 595                die("fork failed");
 596        fd[0] = pipefd[0][0];
 597        fd[1] = pipefd[1][1];
 598        close(pipefd[0][1]);
 599        close(pipefd[1][0]);
 600}
 601
 602#define MAX_CMD_LEN 1024
 603
 604/*
 605 * This returns 0 if the transport protocol does not need fork(2),
 606 * or a process id if it does.  Once done, finish the connection
 607 * with finish_connect() with the value returned from this function
 608 * (it is safe to call finish_connect() with 0 to support the former
 609 * case).
 610 *
 611 * Does not return a negative value on error; it just dies.
 612 */
 613pid_t git_connect(int fd[2], char *url, const char *prog)
 614{
 615        char *host, *path = url;
 616        char *end;
 617        int c;
 618        int pipefd[2][2];
 619        pid_t pid;
 620        enum protocol protocol = PROTO_LOCAL;
 621        int free_path = 0;
 622
 623        /* Without this we cannot rely on waitpid() to tell
 624         * what happened to our children.
 625         */
 626        signal(SIGCHLD, SIG_DFL);
 627
 628        host = strstr(url, "://");
 629        if(host) {
 630                *host = '\0';
 631                protocol = get_protocol(url);
 632                host += 3;
 633                c = '/';
 634        } else {
 635                host = url;
 636                c = ':';
 637        }
 638
 639        if (host[0] == '[') {
 640                end = strchr(host + 1, ']');
 641                if (end) {
 642                        *end = 0;
 643                        end++;
 644                        host++;
 645                } else
 646                        end = host;
 647        } else
 648                end = host;
 649
 650        path = strchr(end, c);
 651        if (c == ':') {
 652                if (path) {
 653                        protocol = PROTO_SSH;
 654                        *path++ = '\0';
 655                } else
 656                        path = host;
 657        }
 658
 659        if (!path || !*path)
 660                die("No path specified. See 'man git-pull' for valid url syntax");
 661
 662        /*
 663         * null-terminate hostname and point path to ~ for URL's like this:
 664         *    ssh://host.xz/~user/repo
 665         */
 666        if (protocol != PROTO_LOCAL && host != url) {
 667                char *ptr = path;
 668                if (path[1] == '~')
 669                        path++;
 670                else {
 671                        path = xstrdup(ptr);
 672                        free_path = 1;
 673                }
 674
 675                *ptr = '\0';
 676        }
 677
 678        if (protocol == PROTO_GIT) {
 679                /* These underlying connection commands die() if they
 680                 * cannot connect.
 681                 */
 682                char *target_host = xstrdup(host);
 683                if (git_use_proxy(host))
 684                        git_proxy_connect(fd, host);
 685                else
 686                        git_tcp_connect(fd, host);
 687                /*
 688                 * Separate original protocol components prog and path
 689                 * from extended components with a NUL byte.
 690                 */
 691                packet_write(fd[1],
 692                             "%s %s%chost=%s%c",
 693                             prog, path, 0,
 694                             target_host, 0);
 695                free(target_host);
 696                if (free_path)
 697                        free(path);
 698                return 0;
 699        }
 700
 701        if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
 702                die("unable to create pipe pair for communication");
 703        pid = fork();
 704        if (pid < 0)
 705                die("unable to fork");
 706        if (!pid) {
 707                char command[MAX_CMD_LEN];
 708                char *posn = command;
 709                int size = MAX_CMD_LEN;
 710                int of = 0;
 711
 712                of |= add_to_string(&posn, &size, prog, 0);
 713                of |= add_to_string(&posn, &size, " ", 0);
 714                of |= add_to_string(&posn, &size, path, 1);
 715
 716                if (of)
 717                        die("command line too long");
 718
 719                dup2(pipefd[1][0], 0);
 720                dup2(pipefd[0][1], 1);
 721                close(pipefd[0][0]);
 722                close(pipefd[0][1]);
 723                close(pipefd[1][0]);
 724                close(pipefd[1][1]);
 725                if (protocol == PROTO_SSH) {
 726                        const char *ssh, *ssh_basename;
 727                        ssh = getenv("GIT_SSH");
 728                        if (!ssh) ssh = "ssh";
 729                        ssh_basename = strrchr(ssh, '/');
 730                        if (!ssh_basename)
 731                                ssh_basename = ssh;
 732                        else
 733                                ssh_basename++;
 734                        execlp(ssh, ssh_basename, host, command, NULL);
 735                }
 736                else {
 737                        unsetenv(ALTERNATE_DB_ENVIRONMENT);
 738                        unsetenv(DB_ENVIRONMENT);
 739                        unsetenv(GIT_DIR_ENVIRONMENT);
 740                        unsetenv(GRAFT_ENVIRONMENT);
 741                        unsetenv(INDEX_ENVIRONMENT);
 742                        execlp("sh", "sh", "-c", command, NULL);
 743                }
 744                die("exec failed");
 745        }
 746        fd[0] = pipefd[0][0];
 747        fd[1] = pipefd[1][1];
 748        close(pipefd[0][1]);
 749        close(pipefd[1][0]);
 750        if (free_path)
 751                free(path);
 752        return pid;
 753}
 754
 755int finish_connect(pid_t pid)
 756{
 757        if (pid == 0)
 758                return 0;
 759
 760        while (waitpid(pid, NULL, 0) < 0) {
 761                if (errno != EINTR)
 762                        return -1;
 763        }
 764        return 0;
 765}