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