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