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