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