http-backend.con commit do not write null sha1s to on-disk index (4337b58)
   1#include "cache.h"
   2#include "refs.h"
   3#include "pkt-line.h"
   4#include "object.h"
   5#include "tag.h"
   6#include "exec_cmd.h"
   7#include "run-command.h"
   8#include "string-list.h"
   9#include "url.h"
  10
  11static const char content_type[] = "Content-Type";
  12static const char content_length[] = "Content-Length";
  13static const char last_modified[] = "Last-Modified";
  14static int getanyfile = 1;
  15
  16static struct string_list *query_params;
  17
  18struct rpc_service {
  19        const char *name;
  20        const char *config_name;
  21        signed enabled : 2;
  22};
  23
  24static struct rpc_service rpc_service[] = {
  25        { "upload-pack", "uploadpack", 1 },
  26        { "receive-pack", "receivepack", -1 },
  27};
  28
  29static struct string_list *get_parameters(void)
  30{
  31        if (!query_params) {
  32                const char *query = getenv("QUERY_STRING");
  33
  34                query_params = xcalloc(1, sizeof(*query_params));
  35                while (query && *query) {
  36                        char *name = url_decode_parameter_name(&query);
  37                        char *value = url_decode_parameter_value(&query);
  38                        struct string_list_item *i;
  39
  40                        i = string_list_lookup(query_params, name);
  41                        if (!i)
  42                                i = string_list_insert(query_params, name);
  43                        else
  44                                free(i->util);
  45                        i->util = value;
  46                }
  47        }
  48        return query_params;
  49}
  50
  51static const char *get_parameter(const char *name)
  52{
  53        struct string_list_item *i;
  54        i = string_list_lookup(get_parameters(), name);
  55        return i ? i->util : NULL;
  56}
  57
  58__attribute__((format (printf, 2, 3)))
  59static void format_write(int fd, const char *fmt, ...)
  60{
  61        static char buffer[1024];
  62
  63        va_list args;
  64        unsigned n;
  65
  66        va_start(args, fmt);
  67        n = vsnprintf(buffer, sizeof(buffer), fmt, args);
  68        va_end(args);
  69        if (n >= sizeof(buffer))
  70                die("protocol error: impossibly long line");
  71
  72        safe_write(fd, buffer, n);
  73}
  74
  75static void http_status(unsigned code, const char *msg)
  76{
  77        format_write(1, "Status: %u %s\r\n", code, msg);
  78}
  79
  80static void hdr_str(const char *name, const char *value)
  81{
  82        format_write(1, "%s: %s\r\n", name, value);
  83}
  84
  85static void hdr_int(const char *name, uintmax_t value)
  86{
  87        format_write(1, "%s: %" PRIuMAX "\r\n", name, value);
  88}
  89
  90static void hdr_date(const char *name, unsigned long when)
  91{
  92        const char *value = show_date(when, 0, DATE_RFC2822);
  93        hdr_str(name, value);
  94}
  95
  96static void hdr_nocache(void)
  97{
  98        hdr_str("Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
  99        hdr_str("Pragma", "no-cache");
 100        hdr_str("Cache-Control", "no-cache, max-age=0, must-revalidate");
 101}
 102
 103static void hdr_cache_forever(void)
 104{
 105        unsigned long now = time(NULL);
 106        hdr_date("Date", now);
 107        hdr_date("Expires", now + 31536000);
 108        hdr_str("Cache-Control", "public, max-age=31536000");
 109}
 110
 111static void end_headers(void)
 112{
 113        safe_write(1, "\r\n", 2);
 114}
 115
 116__attribute__((format (printf, 1, 2)))
 117static NORETURN void not_found(const char *err, ...)
 118{
 119        va_list params;
 120
 121        http_status(404, "Not Found");
 122        hdr_nocache();
 123        end_headers();
 124
 125        va_start(params, err);
 126        if (err && *err)
 127                vfprintf(stderr, err, params);
 128        va_end(params);
 129        exit(0);
 130}
 131
 132__attribute__((format (printf, 1, 2)))
 133static NORETURN void forbidden(const char *err, ...)
 134{
 135        va_list params;
 136
 137        http_status(403, "Forbidden");
 138        hdr_nocache();
 139        end_headers();
 140
 141        va_start(params, err);
 142        if (err && *err)
 143                vfprintf(stderr, err, params);
 144        va_end(params);
 145        exit(0);
 146}
 147
 148static void select_getanyfile(void)
 149{
 150        if (!getanyfile)
 151                forbidden("Unsupported service: getanyfile");
 152}
 153
 154static void send_strbuf(const char *type, struct strbuf *buf)
 155{
 156        hdr_int(content_length, buf->len);
 157        hdr_str(content_type, type);
 158        end_headers();
 159        safe_write(1, buf->buf, buf->len);
 160}
 161
 162static void send_local_file(const char *the_type, const char *name)
 163{
 164        const char *p = git_path("%s", name);
 165        size_t buf_alloc = 8192;
 166        char *buf = xmalloc(buf_alloc);
 167        int fd;
 168        struct stat sb;
 169
 170        fd = open(p, O_RDONLY);
 171        if (fd < 0)
 172                not_found("Cannot open '%s': %s", p, strerror(errno));
 173        if (fstat(fd, &sb) < 0)
 174                die_errno("Cannot stat '%s'", p);
 175
 176        hdr_int(content_length, sb.st_size);
 177        hdr_str(content_type, the_type);
 178        hdr_date(last_modified, sb.st_mtime);
 179        end_headers();
 180
 181        for (;;) {
 182                ssize_t n = xread(fd, buf, buf_alloc);
 183                if (n < 0)
 184                        die_errno("Cannot read '%s'", p);
 185                if (!n)
 186                        break;
 187                safe_write(1, buf, n);
 188        }
 189        close(fd);
 190        free(buf);
 191}
 192
 193static void get_text_file(char *name)
 194{
 195        select_getanyfile();
 196        hdr_nocache();
 197        send_local_file("text/plain", name);
 198}
 199
 200static void get_loose_object(char *name)
 201{
 202        select_getanyfile();
 203        hdr_cache_forever();
 204        send_local_file("application/x-git-loose-object", name);
 205}
 206
 207static void get_pack_file(char *name)
 208{
 209        select_getanyfile();
 210        hdr_cache_forever();
 211        send_local_file("application/x-git-packed-objects", name);
 212}
 213
 214static void get_idx_file(char *name)
 215{
 216        select_getanyfile();
 217        hdr_cache_forever();
 218        send_local_file("application/x-git-packed-objects-toc", name);
 219}
 220
 221static int http_config(const char *var, const char *value, void *cb)
 222{
 223        if (!strcmp(var, "http.getanyfile")) {
 224                getanyfile = git_config_bool(var, value);
 225                return 0;
 226        }
 227
 228        if (!prefixcmp(var, "http.")) {
 229                int i;
 230
 231                for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
 232                        struct rpc_service *svc = &rpc_service[i];
 233                        if (!strcmp(var + 5, svc->config_name)) {
 234                                svc->enabled = git_config_bool(var, value);
 235                                return 0;
 236                        }
 237                }
 238        }
 239
 240        /* we are not interested in parsing any other configuration here */
 241        return 0;
 242}
 243
 244static struct rpc_service *select_service(const char *name)
 245{
 246        struct rpc_service *svc = NULL;
 247        int i;
 248
 249        if (prefixcmp(name, "git-"))
 250                forbidden("Unsupported service: '%s'", name);
 251
 252        for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
 253                struct rpc_service *s = &rpc_service[i];
 254                if (!strcmp(s->name, name + 4)) {
 255                        svc = s;
 256                        break;
 257                }
 258        }
 259
 260        if (!svc)
 261                forbidden("Unsupported service: '%s'", name);
 262
 263        if (svc->enabled < 0) {
 264                const char *user = getenv("REMOTE_USER");
 265                svc->enabled = (user && *user) ? 1 : 0;
 266        }
 267        if (!svc->enabled)
 268                forbidden("Service not enabled: '%s'", svc->name);
 269        return svc;
 270}
 271
 272static void inflate_request(const char *prog_name, int out)
 273{
 274        git_zstream stream;
 275        unsigned char in_buf[8192];
 276        unsigned char out_buf[8192];
 277        unsigned long cnt = 0;
 278
 279        memset(&stream, 0, sizeof(stream));
 280        git_inflate_init_gzip_only(&stream);
 281
 282        while (1) {
 283                ssize_t n = xread(0, in_buf, sizeof(in_buf));
 284                if (n <= 0)
 285                        die("request ended in the middle of the gzip stream");
 286
 287                stream.next_in = in_buf;
 288                stream.avail_in = n;
 289
 290                while (0 < stream.avail_in) {
 291                        int ret;
 292
 293                        stream.next_out = out_buf;
 294                        stream.avail_out = sizeof(out_buf);
 295
 296                        ret = git_inflate(&stream, Z_NO_FLUSH);
 297                        if (ret != Z_OK && ret != Z_STREAM_END)
 298                                die("zlib error inflating request, result %d", ret);
 299
 300                        n = stream.total_out - cnt;
 301                        if (write_in_full(out, out_buf, n) != n)
 302                                die("%s aborted reading request", prog_name);
 303                        cnt += n;
 304
 305                        if (ret == Z_STREAM_END)
 306                                goto done;
 307                }
 308        }
 309
 310done:
 311        git_inflate_end(&stream);
 312        close(out);
 313}
 314
 315static void run_service(const char **argv)
 316{
 317        const char *encoding = getenv("HTTP_CONTENT_ENCODING");
 318        const char *user = getenv("REMOTE_USER");
 319        const char *host = getenv("REMOTE_ADDR");
 320        char *env[3];
 321        struct strbuf buf = STRBUF_INIT;
 322        int gzipped_request = 0;
 323        struct child_process cld;
 324
 325        if (encoding && !strcmp(encoding, "gzip"))
 326                gzipped_request = 1;
 327        else if (encoding && !strcmp(encoding, "x-gzip"))
 328                gzipped_request = 1;
 329
 330        if (!user || !*user)
 331                user = "anonymous";
 332        if (!host || !*host)
 333                host = "(none)";
 334
 335        memset(&env, 0, sizeof(env));
 336        strbuf_addf(&buf, "GIT_COMMITTER_NAME=%s", user);
 337        env[0] = strbuf_detach(&buf, NULL);
 338
 339        strbuf_addf(&buf, "GIT_COMMITTER_EMAIL=%s@http.%s", user, host);
 340        env[1] = strbuf_detach(&buf, NULL);
 341        env[2] = NULL;
 342
 343        memset(&cld, 0, sizeof(cld));
 344        cld.argv = argv;
 345        cld.env = (const char *const *)env;
 346        if (gzipped_request)
 347                cld.in = -1;
 348        cld.git_cmd = 1;
 349        if (start_command(&cld))
 350                exit(1);
 351
 352        close(1);
 353        if (gzipped_request)
 354                inflate_request(argv[0], cld.in);
 355        else
 356                close(0);
 357
 358        if (finish_command(&cld))
 359                exit(1);
 360        free(env[0]);
 361        free(env[1]);
 362        strbuf_release(&buf);
 363}
 364
 365static int show_text_ref(const char *name, const unsigned char *sha1,
 366        int flag, void *cb_data)
 367{
 368        struct strbuf *buf = cb_data;
 369        struct object *o = parse_object(sha1);
 370        if (!o)
 371                return 0;
 372
 373        strbuf_addf(buf, "%s\t%s\n", sha1_to_hex(sha1), name);
 374        if (o->type == OBJ_TAG) {
 375                o = deref_tag(o, name, 0);
 376                if (!o)
 377                        return 0;
 378                strbuf_addf(buf, "%s\t%s^{}\n", sha1_to_hex(o->sha1), name);
 379        }
 380        return 0;
 381}
 382
 383static void get_info_refs(char *arg)
 384{
 385        const char *service_name = get_parameter("service");
 386        struct strbuf buf = STRBUF_INIT;
 387
 388        hdr_nocache();
 389
 390        if (service_name) {
 391                const char *argv[] = {NULL /* service name */,
 392                        "--stateless-rpc", "--advertise-refs",
 393                        ".", NULL};
 394                struct rpc_service *svc = select_service(service_name);
 395
 396                strbuf_addf(&buf, "application/x-git-%s-advertisement",
 397                        svc->name);
 398                hdr_str(content_type, buf.buf);
 399                end_headers();
 400
 401                packet_write(1, "# service=git-%s\n", svc->name);
 402                packet_flush(1);
 403
 404                argv[0] = svc->name;
 405                run_service(argv);
 406
 407        } else {
 408                select_getanyfile();
 409                for_each_ref(show_text_ref, &buf);
 410                send_strbuf("text/plain", &buf);
 411        }
 412        strbuf_release(&buf);
 413}
 414
 415static void get_info_packs(char *arg)
 416{
 417        size_t objdirlen = strlen(get_object_directory());
 418        struct strbuf buf = STRBUF_INIT;
 419        struct packed_git *p;
 420        size_t cnt = 0;
 421
 422        select_getanyfile();
 423        prepare_packed_git();
 424        for (p = packed_git; p; p = p->next) {
 425                if (p->pack_local)
 426                        cnt++;
 427        }
 428
 429        strbuf_grow(&buf, cnt * 53 + 2);
 430        for (p = packed_git; p; p = p->next) {
 431                if (p->pack_local)
 432                        strbuf_addf(&buf, "P %s\n", p->pack_name + objdirlen + 6);
 433        }
 434        strbuf_addch(&buf, '\n');
 435
 436        hdr_nocache();
 437        send_strbuf("text/plain; charset=utf-8", &buf);
 438        strbuf_release(&buf);
 439}
 440
 441static void check_content_type(const char *accepted_type)
 442{
 443        const char *actual_type = getenv("CONTENT_TYPE");
 444
 445        if (!actual_type)
 446                actual_type = "";
 447
 448        if (strcmp(actual_type, accepted_type)) {
 449                http_status(415, "Unsupported Media Type");
 450                hdr_nocache();
 451                end_headers();
 452                format_write(1,
 453                        "Expected POST with Content-Type '%s',"
 454                        " but received '%s' instead.\n",
 455                        accepted_type, actual_type);
 456                exit(0);
 457        }
 458}
 459
 460static void service_rpc(char *service_name)
 461{
 462        const char *argv[] = {NULL, "--stateless-rpc", ".", NULL};
 463        struct rpc_service *svc = select_service(service_name);
 464        struct strbuf buf = STRBUF_INIT;
 465
 466        strbuf_reset(&buf);
 467        strbuf_addf(&buf, "application/x-git-%s-request", svc->name);
 468        check_content_type(buf.buf);
 469
 470        hdr_nocache();
 471
 472        strbuf_reset(&buf);
 473        strbuf_addf(&buf, "application/x-git-%s-result", svc->name);
 474        hdr_str(content_type, buf.buf);
 475
 476        end_headers();
 477
 478        argv[0] = svc->name;
 479        run_service(argv);
 480        strbuf_release(&buf);
 481}
 482
 483static NORETURN void die_webcgi(const char *err, va_list params)
 484{
 485        static int dead;
 486
 487        if (!dead) {
 488                dead = 1;
 489                http_status(500, "Internal Server Error");
 490                hdr_nocache();
 491                end_headers();
 492
 493                vreportf("fatal: ", err, params);
 494        }
 495        exit(0); /* we successfully reported a failure ;-) */
 496}
 497
 498static char* getdir(void)
 499{
 500        struct strbuf buf = STRBUF_INIT;
 501        char *pathinfo = getenv("PATH_INFO");
 502        char *root = getenv("GIT_PROJECT_ROOT");
 503        char *path = getenv("PATH_TRANSLATED");
 504
 505        if (root && *root) {
 506                if (!pathinfo || !*pathinfo)
 507                        die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
 508                if (daemon_avoid_alias(pathinfo))
 509                        die("'%s': aliased", pathinfo);
 510                end_url_with_slash(&buf, root);
 511                if (pathinfo[0] == '/')
 512                        pathinfo++;
 513                strbuf_addstr(&buf, pathinfo);
 514                return strbuf_detach(&buf, NULL);
 515        } else if (path && *path) {
 516                return xstrdup(path);
 517        } else
 518                die("No GIT_PROJECT_ROOT or PATH_TRANSLATED from server");
 519        return NULL;
 520}
 521
 522static struct service_cmd {
 523        const char *method;
 524        const char *pattern;
 525        void (*imp)(char *);
 526} services[] = {
 527        {"GET", "/HEAD$", get_text_file},
 528        {"GET", "/info/refs$", get_info_refs},
 529        {"GET", "/objects/info/alternates$", get_text_file},
 530        {"GET", "/objects/info/http-alternates$", get_text_file},
 531        {"GET", "/objects/info/packs$", get_info_packs},
 532        {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{38}$", get_loose_object},
 533        {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.pack$", get_pack_file},
 534        {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.idx$", get_idx_file},
 535
 536        {"POST", "/git-upload-pack$", service_rpc},
 537        {"POST", "/git-receive-pack$", service_rpc}
 538};
 539
 540int main(int argc, char **argv)
 541{
 542        char *method = getenv("REQUEST_METHOD");
 543        char *dir;
 544        struct service_cmd *cmd = NULL;
 545        char *cmd_arg = NULL;
 546        int i;
 547
 548        git_setup_gettext();
 549
 550        git_extract_argv0_path(argv[0]);
 551        set_die_routine(die_webcgi);
 552
 553        if (!method)
 554                die("No REQUEST_METHOD from server");
 555        if (!strcmp(method, "HEAD"))
 556                method = "GET";
 557        dir = getdir();
 558
 559        for (i = 0; i < ARRAY_SIZE(services); i++) {
 560                struct service_cmd *c = &services[i];
 561                regex_t re;
 562                regmatch_t out[1];
 563
 564                if (regcomp(&re, c->pattern, REG_EXTENDED))
 565                        die("Bogus regex in service table: %s", c->pattern);
 566                if (!regexec(&re, dir, 1, out, 0)) {
 567                        size_t n;
 568
 569                        if (strcmp(method, c->method)) {
 570                                const char *proto = getenv("SERVER_PROTOCOL");
 571                                if (proto && !strcmp(proto, "HTTP/1.1"))
 572                                        http_status(405, "Method Not Allowed");
 573                                else
 574                                        http_status(400, "Bad Request");
 575                                hdr_nocache();
 576                                end_headers();
 577                                return 0;
 578                        }
 579
 580                        cmd = c;
 581                        n = out[0].rm_eo - out[0].rm_so;
 582                        cmd_arg = xmalloc(n);
 583                        memcpy(cmd_arg, dir + out[0].rm_so + 1, n-1);
 584                        cmd_arg[n-1] = '\0';
 585                        dir[out[0].rm_so] = 0;
 586                        break;
 587                }
 588                regfree(&re);
 589        }
 590
 591        if (!cmd)
 592                not_found("Request not supported: '%s'", dir);
 593
 594        setup_path();
 595        if (!enter_repo(dir, 0))
 596                not_found("Not a git repository: '%s'", dir);
 597        if (!getenv("GIT_HTTP_EXPORT_ALL") &&
 598            access("git-daemon-export-ok", F_OK) )
 599                not_found("Repository not exported: '%s'", dir);
 600
 601        git_config(http_config, NULL);
 602        cmd->imp(cmd_arg);
 603        return 0;
 604}