1 #include "git-compat-util.h"
4 #include "environment.h"
8 #include "repository.h"
14 #include "run-command.h"
15 #include "string-list.h"
19 #include "object-store-ll.h"
23 #include "write-or-die.h"
25 static const char content_type
[] = "Content-Type";
26 static const char content_length
[] = "Content-Length";
27 static const char last_modified
[] = "Last-Modified";
28 static int getanyfile
= 1;
29 static unsigned long max_request_buffer
= 10 * 1024 * 1024;
31 static struct string_list
*query_params
;
35 const char *config_name
;
36 unsigned buffer_input
: 1;
40 static struct rpc_service rpc_service
[] = {
41 { "upload-pack", "uploadpack", 1, 1 },
42 { "receive-pack", "receivepack", 0, -1 },
45 static struct string_list
*get_parameters(void)
48 const char *query
= getenv("QUERY_STRING");
50 CALLOC_ARRAY(query_params
, 1);
51 while (query
&& *query
) {
52 char *name
= url_decode_parameter_name(&query
);
53 char *value
= url_decode_parameter_value(&query
);
54 struct string_list_item
*i
;
56 i
= string_list_lookup(query_params
, name
);
58 i
= string_list_insert(query_params
, name
);
67 static const char *get_parameter(const char *name
)
69 struct string_list_item
*i
;
70 i
= string_list_lookup(get_parameters(), name
);
71 return i
? i
->util
: NULL
;
74 __attribute__((format (printf
, 2, 3)))
75 static void format_write(int fd
, const char *fmt
, ...)
77 static char buffer
[1024];
83 n
= vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
85 if (n
>= sizeof(buffer
))
86 die("protocol error: impossibly long line");
88 write_or_die(fd
, buffer
, n
);
91 static void http_status(struct strbuf
*hdr
, unsigned code
, const char *msg
)
93 strbuf_addf(hdr
, "Status: %u %s\r\n", code
, msg
);
96 static void hdr_str(struct strbuf
*hdr
, const char *name
, const char *value
)
98 strbuf_addf(hdr
, "%s: %s\r\n", name
, value
);
101 static void hdr_int(struct strbuf
*hdr
, const char *name
, uintmax_t value
)
103 strbuf_addf(hdr
, "%s: %" PRIuMAX
"\r\n", name
, value
);
106 static void hdr_date(struct strbuf
*hdr
, const char *name
, timestamp_t when
)
108 const char *value
= show_date(when
, 0, DATE_MODE(RFC2822
));
109 hdr_str(hdr
, name
, value
);
112 static void hdr_nocache(struct strbuf
*hdr
)
114 hdr_str(hdr
, "Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
115 hdr_str(hdr
, "Pragma", "no-cache");
116 hdr_str(hdr
, "Cache-Control", "no-cache, max-age=0, must-revalidate");
119 static void hdr_cache_forever(struct strbuf
*hdr
)
121 timestamp_t now
= time(NULL
);
122 hdr_date(hdr
, "Date", now
);
123 hdr_date(hdr
, "Expires", now
+ 31536000);
124 hdr_str(hdr
, "Cache-Control", "public, max-age=31536000");
127 static void end_headers(struct strbuf
*hdr
)
129 strbuf_add(hdr
, "\r\n", 2);
130 write_or_die(1, hdr
->buf
, hdr
->len
);
134 __attribute__((format (printf
, 2, 3)))
135 static NORETURN
void not_found(struct strbuf
*hdr
, const char *err
, ...)
139 http_status(hdr
, 404, "Not Found");
143 va_start(params
, err
);
145 vfprintf(stderr
, err
, params
);
150 __attribute__((format (printf
, 2, 3)))
151 static NORETURN
void forbidden(struct strbuf
*hdr
, const char *err
, ...)
155 http_status(hdr
, 403, "Forbidden");
159 va_start(params
, err
);
161 vfprintf(stderr
, err
, params
);
166 static void select_getanyfile(struct strbuf
*hdr
)
169 forbidden(hdr
, "Unsupported service: getanyfile");
172 static void send_strbuf(struct strbuf
*hdr
,
173 const char *type
, struct strbuf
*buf
)
175 hdr_int(hdr
, content_length
, buf
->len
);
176 hdr_str(hdr
, content_type
, type
);
178 write_or_die(1, buf
->buf
, buf
->len
);
181 static void send_local_file(struct strbuf
*hdr
, const char *the_type
,
184 char *p
= git_pathdup("%s", name
);
185 size_t buf_alloc
= 8192;
186 char *buf
= xmalloc(buf_alloc
);
190 fd
= open(p
, O_RDONLY
);
192 not_found(hdr
, "Cannot open '%s': %s", p
, strerror(errno
));
193 if (fstat(fd
, &sb
) < 0)
194 die_errno("Cannot stat '%s'", p
);
196 hdr_int(hdr
, content_length
, sb
.st_size
);
197 hdr_str(hdr
, content_type
, the_type
);
198 hdr_date(hdr
, last_modified
, sb
.st_mtime
);
202 ssize_t n
= xread(fd
, buf
, buf_alloc
);
204 die_errno("Cannot read '%s'", p
);
207 write_or_die(1, buf
, n
);
214 static void get_text_file(struct strbuf
*hdr
, char *name
)
216 select_getanyfile(hdr
);
218 send_local_file(hdr
, "text/plain", name
);
221 static void get_loose_object(struct strbuf
*hdr
, char *name
)
223 select_getanyfile(hdr
);
224 hdr_cache_forever(hdr
);
225 send_local_file(hdr
, "application/x-git-loose-object", name
);
228 static void get_pack_file(struct strbuf
*hdr
, char *name
)
230 select_getanyfile(hdr
);
231 hdr_cache_forever(hdr
);
232 send_local_file(hdr
, "application/x-git-packed-objects", name
);
235 static void get_idx_file(struct strbuf
*hdr
, char *name
)
237 select_getanyfile(hdr
);
238 hdr_cache_forever(hdr
);
239 send_local_file(hdr
, "application/x-git-packed-objects-toc", name
);
242 static void http_config(void)
245 struct strbuf var
= STRBUF_INIT
;
247 git_config_get_bool("http.getanyfile", &getanyfile
);
248 git_config_get_ulong("http.maxrequestbuffer", &max_request_buffer
);
250 for (i
= 0; i
< ARRAY_SIZE(rpc_service
); i
++) {
251 struct rpc_service
*svc
= &rpc_service
[i
];
252 strbuf_addf(&var
, "http.%s", svc
->config_name
);
253 if (!git_config_get_bool(var
.buf
, &value
))
254 svc
->enabled
= value
;
258 strbuf_release(&var
);
261 static struct rpc_service
*select_service(struct strbuf
*hdr
, const char *name
)
263 const char *svc_name
;
264 struct rpc_service
*svc
= NULL
;
267 if (!skip_prefix(name
, "git-", &svc_name
))
268 forbidden(hdr
, "Unsupported service: '%s'", name
);
270 for (i
= 0; i
< ARRAY_SIZE(rpc_service
); i
++) {
271 struct rpc_service
*s
= &rpc_service
[i
];
272 if (!strcmp(s
->name
, svc_name
)) {
279 forbidden(hdr
, "Unsupported service: '%s'", name
);
281 if (svc
->enabled
< 0) {
282 const char *user
= getenv("REMOTE_USER");
283 svc
->enabled
= (user
&& *user
) ? 1 : 0;
286 forbidden(hdr
, "Service not enabled: '%s'", svc
->name
);
290 static void write_to_child(int out
, const unsigned char *buf
, ssize_t len
, const char *prog_name
)
292 if (write_in_full(out
, buf
, len
) < 0)
293 die("unable to write to '%s'", prog_name
);
297 * This is basically strbuf_read(), except that if we
298 * hit max_request_buffer we die (we'd rather reject a
299 * maliciously large request than chew up infinite memory).
301 static ssize_t
read_request_eof(int fd
, unsigned char **out
)
303 size_t len
= 0, alloc
= 8192;
304 unsigned char *buf
= xmalloc(alloc
);
306 if (max_request_buffer
< alloc
)
307 max_request_buffer
= alloc
;
312 cnt
= read_in_full(fd
, buf
+ len
, alloc
- len
);
318 /* partial read from read_in_full means we hit EOF */
325 /* otherwise, grow and try again (if we can) */
326 if (alloc
== max_request_buffer
)
327 die("request was larger than our maximum size (%lu);"
328 " try setting GIT_HTTP_MAX_REQUEST_BUFFER",
331 alloc
= alloc_nr(alloc
);
332 if (alloc
> max_request_buffer
)
333 alloc
= max_request_buffer
;
334 REALLOC_ARRAY(buf
, alloc
);
338 static ssize_t
read_request_fixed_len(int fd
, ssize_t req_len
, unsigned char **out
)
340 unsigned char *buf
= NULL
;
343 if (max_request_buffer
< req_len
) {
344 die("request was larger than our maximum size (%lu): "
345 "%" PRIuMAX
"; try setting GIT_HTTP_MAX_REQUEST_BUFFER",
346 max_request_buffer
, (uintmax_t)req_len
);
349 buf
= xmalloc(req_len
);
350 cnt
= read_in_full(fd
, buf
, req_len
);
359 static ssize_t
get_content_length(void)
362 const char *str
= getenv("CONTENT_LENGTH");
364 if (str
&& *str
&& !git_parse_ssize_t(str
, &val
))
365 die("failed to parse CONTENT_LENGTH: %s", str
);
369 static ssize_t
read_request(int fd
, unsigned char **out
, ssize_t req_len
)
372 return read_request_eof(fd
, out
);
374 return read_request_fixed_len(fd
, req_len
, out
);
377 static void inflate_request(const char *prog_name
, int out
, int buffer_input
, ssize_t req_len
)
380 unsigned char *full_request
= NULL
;
381 unsigned char in_buf
[8192];
382 unsigned char out_buf
[8192];
383 unsigned long cnt
= 0;
384 int req_len_defined
= req_len
>= 0;
385 size_t req_remaining_len
= req_len
;
387 memset(&stream
, 0, sizeof(stream
));
388 git_inflate_init_gzip_only(&stream
);
395 n
= 0; /* nothing left to read */
397 n
= read_request(0, &full_request
, req_len
);
398 stream
.next_in
= full_request
;
401 if (req_len_defined
&& req_remaining_len
<= sizeof(in_buf
))
402 buffer_len
= req_remaining_len
;
404 buffer_len
= sizeof(in_buf
);
405 n
= xread(0, in_buf
, buffer_len
);
406 stream
.next_in
= in_buf
;
407 if (req_len_defined
&& n
> 0)
408 req_remaining_len
-= n
;
412 die("request ended in the middle of the gzip stream");
415 while (0 < stream
.avail_in
) {
418 stream
.next_out
= out_buf
;
419 stream
.avail_out
= sizeof(out_buf
);
421 ret
= git_inflate(&stream
, Z_NO_FLUSH
);
422 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
423 die("zlib error inflating request, result %d", ret
);
425 n
= stream
.total_out
- cnt
;
426 write_to_child(out
, out_buf
, stream
.total_out
- cnt
, prog_name
);
427 cnt
= stream
.total_out
;
429 if (ret
== Z_STREAM_END
)
435 git_inflate_end(&stream
);
440 static void copy_request(const char *prog_name
, int out
, ssize_t req_len
)
443 ssize_t n
= read_request(0, &buf
, req_len
);
445 die_errno("error reading request body");
446 write_to_child(out
, buf
, n
, prog_name
);
451 static void pipe_fixed_length(const char *prog_name
, int out
, size_t req_len
)
453 unsigned char buf
[8192];
454 size_t remaining_len
= req_len
;
456 while (remaining_len
> 0) {
457 size_t chunk_length
= remaining_len
> sizeof(buf
) ? sizeof(buf
) : remaining_len
;
458 ssize_t n
= xread(0, buf
, chunk_length
);
460 die_errno("Reading request failed");
461 write_to_child(out
, buf
, n
, prog_name
);
468 static void run_service(const char **argv
, int buffer_input
)
470 const char *encoding
= getenv("HTTP_CONTENT_ENCODING");
471 const char *user
= getenv("REMOTE_USER");
472 const char *host
= getenv("REMOTE_ADDR");
473 int gzipped_request
= 0;
474 struct child_process cld
= CHILD_PROCESS_INIT
;
475 ssize_t req_len
= get_content_length();
477 if (encoding
&& (!strcmp(encoding
, "gzip") || !strcmp(encoding
, "x-gzip")))
485 if (!getenv("GIT_COMMITTER_NAME"))
486 strvec_pushf(&cld
.env
, "GIT_COMMITTER_NAME=%s", user
);
487 if (!getenv("GIT_COMMITTER_EMAIL"))
488 strvec_pushf(&cld
.env
,
489 "GIT_COMMITTER_EMAIL=%s@http.%s", user
, host
);
491 strvec_pushv(&cld
.args
, argv
);
492 if (buffer_input
|| gzipped_request
|| req_len
>= 0)
495 cld
.clean_on_exit
= 1;
496 cld
.wait_after_clean
= 1;
497 if (start_command(&cld
))
502 inflate_request(argv
[0], cld
.in
, buffer_input
, req_len
);
503 else if (buffer_input
)
504 copy_request(argv
[0], cld
.in
, req_len
);
505 else if (req_len
>= 0)
506 pipe_fixed_length(argv
[0], cld
.in
, req_len
);
510 if (finish_command(&cld
))
514 static int show_text_ref(const char *name
, const struct object_id
*oid
,
515 int flag UNUSED
, void *cb_data
)
517 const char *name_nons
= strip_namespace(name
);
518 struct strbuf
*buf
= cb_data
;
519 struct object
*o
= parse_object(the_repository
, oid
);
523 strbuf_addf(buf
, "%s\t%s\n", oid_to_hex(oid
), name_nons
);
524 if (o
->type
== OBJ_TAG
) {
525 o
= deref_tag(the_repository
, o
, name
, 0);
528 strbuf_addf(buf
, "%s\t%s^{}\n", oid_to_hex(&o
->oid
),
534 static void get_info_refs(struct strbuf
*hdr
, char *arg UNUSED
)
536 const char *service_name
= get_parameter("service");
537 struct strbuf buf
= STRBUF_INIT
;
542 const char *argv
[] = {NULL
/* service name */,
543 "--http-backend-info-refs",
545 struct rpc_service
*svc
= select_service(hdr
, service_name
);
547 strbuf_addf(&buf
, "application/x-git-%s-advertisement",
549 hdr_str(hdr
, content_type
, buf
.buf
);
553 if (determine_protocol_version_server() != protocol_v2
) {
554 packet_write_fmt(1, "# service=git-%s\n", svc
->name
);
559 run_service(argv
, 0);
562 select_getanyfile(hdr
);
563 for_each_namespaced_ref(show_text_ref
, &buf
);
564 send_strbuf(hdr
, "text/plain", &buf
);
566 strbuf_release(&buf
);
569 static int show_head_ref(const char *refname
, const struct object_id
*oid
,
570 int flag
, void *cb_data
)
572 struct strbuf
*buf
= cb_data
;
574 if (flag
& REF_ISSYMREF
) {
575 const char *target
= resolve_ref_unsafe(refname
,
580 strbuf_addf(buf
, "ref: %s\n", strip_namespace(target
));
582 strbuf_addf(buf
, "%s\n", oid_to_hex(oid
));
588 static void get_head(struct strbuf
*hdr
, char *arg UNUSED
)
590 struct strbuf buf
= STRBUF_INIT
;
592 select_getanyfile(hdr
);
593 head_ref_namespaced(show_head_ref
, &buf
);
594 send_strbuf(hdr
, "text/plain", &buf
);
595 strbuf_release(&buf
);
598 static void get_info_packs(struct strbuf
*hdr
, char *arg UNUSED
)
600 size_t objdirlen
= strlen(get_object_directory());
601 struct strbuf buf
= STRBUF_INIT
;
602 struct packed_git
*p
;
605 select_getanyfile(hdr
);
606 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
611 strbuf_grow(&buf
, cnt
* 53 + 2);
612 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
614 strbuf_addf(&buf
, "P %s\n", p
->pack_name
+ objdirlen
+ 6);
616 strbuf_addch(&buf
, '\n');
619 send_strbuf(hdr
, "text/plain; charset=utf-8", &buf
);
620 strbuf_release(&buf
);
623 static void check_content_type(struct strbuf
*hdr
, const char *accepted_type
)
625 const char *actual_type
= getenv("CONTENT_TYPE");
630 if (strcmp(actual_type
, accepted_type
)) {
631 http_status(hdr
, 415, "Unsupported Media Type");
635 "Expected POST with Content-Type '%s',"
636 " but received '%s' instead.\n",
637 accepted_type
, actual_type
);
642 static void service_rpc(struct strbuf
*hdr
, char *service_name
)
644 const char *argv
[] = {NULL
, "--stateless-rpc", ".", NULL
};
645 struct rpc_service
*svc
= select_service(hdr
, service_name
);
646 struct strbuf buf
= STRBUF_INIT
;
649 strbuf_addf(&buf
, "application/x-git-%s-request", svc
->name
);
650 check_content_type(hdr
, buf
.buf
);
655 strbuf_addf(&buf
, "application/x-git-%s-result", svc
->name
);
656 hdr_str(hdr
, content_type
, buf
.buf
);
661 run_service(argv
, svc
->buffer_input
);
662 strbuf_release(&buf
);
666 static NORETURN
void die_webcgi(const char *err
, va_list params
)
669 struct strbuf hdr
= STRBUF_INIT
;
670 report_fn die_message_fn
= get_die_message_routine();
672 die_message_fn(err
, params
);
674 http_status(&hdr
, 500, "Internal Server Error");
678 exit(0); /* we successfully reported a failure ;-) */
681 static int die_webcgi_recursing(void)
686 static char* getdir(void)
688 struct strbuf buf
= STRBUF_INIT
;
689 char *pathinfo
= getenv("PATH_INFO");
690 char *root
= getenv("GIT_PROJECT_ROOT");
691 char *path
= getenv("PATH_TRANSLATED");
694 if (!pathinfo
|| !*pathinfo
)
695 die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
696 if (daemon_avoid_alias(pathinfo
))
697 die("'%s': aliased", pathinfo
);
698 end_url_with_slash(&buf
, root
);
699 if (pathinfo
[0] == '/')
701 strbuf_addstr(&buf
, pathinfo
);
702 return strbuf_detach(&buf
, NULL
);
703 } else if (path
&& *path
) {
704 return xstrdup(path
);
706 die("No GIT_PROJECT_ROOT or PATH_TRANSLATED from server");
710 static struct service_cmd
{
713 void (*imp
)(struct strbuf
*, char *);
715 {"GET", "/HEAD$", get_head
},
716 {"GET", "/info/refs$", get_info_refs
},
717 {"GET", "/objects/info/alternates$", get_text_file
},
718 {"GET", "/objects/info/http-alternates$", get_text_file
},
719 {"GET", "/objects/info/packs$", get_info_packs
},
720 {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{38}$", get_loose_object
},
721 {"GET", "/objects/[0-9a-f]{2}/[0-9a-f]{62}$", get_loose_object
},
722 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.pack$", get_pack_file
},
723 {"GET", "/objects/pack/pack-[0-9a-f]{64}\\.pack$", get_pack_file
},
724 {"GET", "/objects/pack/pack-[0-9a-f]{40}\\.idx$", get_idx_file
},
725 {"GET", "/objects/pack/pack-[0-9a-f]{64}\\.idx$", get_idx_file
},
727 {"POST", "/git-upload-pack$", service_rpc
},
728 {"POST", "/git-receive-pack$", service_rpc
}
731 static int bad_request(struct strbuf
*hdr
, const struct service_cmd
*c
)
733 const char *proto
= getenv("SERVER_PROTOCOL");
735 if (proto
&& !strcmp(proto
, "HTTP/1.1")) {
736 http_status(hdr
, 405, "Method Not Allowed");
737 hdr_str(hdr
, "Allow",
738 !strcmp(c
->method
, "GET") ? "GET, HEAD" : c
->method
);
740 http_status(hdr
, 400, "Bad Request");
746 int cmd_main(int argc UNUSED
, const char **argv UNUSED
)
748 char *method
= getenv("REQUEST_METHOD");
749 const char *proto_header
;
751 struct service_cmd
*cmd
= NULL
;
752 char *cmd_arg
= NULL
;
754 struct strbuf hdr
= STRBUF_INIT
;
756 set_die_routine(die_webcgi
);
757 set_die_is_recursing_routine(die_webcgi_recursing
);
760 die("No REQUEST_METHOD from server");
761 if (!strcmp(method
, "HEAD"))
765 for (i
= 0; i
< ARRAY_SIZE(services
); i
++) {
766 struct service_cmd
*c
= &services
[i
];
771 if (regcomp(&re
, c
->pattern
, REG_EXTENDED
))
772 die("Bogus regex in service table: %s", c
->pattern
);
773 ret
= regexec(&re
, dir
, 1, out
, 0);
779 if (strcmp(method
, c
->method
))
780 return bad_request(&hdr
, c
);
783 n
= out
[0].rm_eo
- out
[0].rm_so
;
784 cmd_arg
= xmemdupz(dir
+ out
[0].rm_so
+ 1, n
- 1);
785 dir
[out
[0].rm_so
] = 0;
791 not_found(&hdr
, "Request not supported: '%s'", dir
);
794 if (!enter_repo(dir
, 0))
795 not_found(&hdr
, "Not a git repository: '%s'", dir
);
796 if (!getenv("GIT_HTTP_EXPORT_ALL") &&
797 access("git-daemon-export-ok", F_OK
) )
798 not_found(&hdr
, "Repository not exported: '%s'", dir
);
802 max_request_buffer
= git_env_ulong("GIT_HTTP_MAX_REQUEST_BUFFER",
804 proto_header
= getenv("HTTP_GIT_PROTOCOL");
806 setenv(GIT_PROTOCOL_ENVIRONMENT
, proto_header
, 0);
808 cmd
->imp(&hdr
, cmd_arg
);