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