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