http-backend: Use http.getanyfile to disable dumb HTTP serving

Some repository owners may wish to enable smart HTTP, but disallow
dumb content serving.  Disallowing dumb serving might be because
the owners want to rely upon reachability to control which objects
clients may access from the repository, or they just want to
encourage clients to use the more bandwidth efficient transport.

If http.getanyfile is set to false the backend CGI will return with
'403 Forbidden' when an object file is accessed by a dumb client.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Shawn O. Pearce 2009-11-04 17:16:37 -08:00 коммит произвёл Junio C Hamano
Родитель 7da4e2280c
Коммит 5abb013b3d
2 изменённых файлов: 36 добавлений и 6 удалений

Просмотреть файл

@ -29,6 +29,14 @@ SERVICES
These services can be enabled/disabled using the per-repository These services can be enabled/disabled using the per-repository
configuration file: configuration file:
http.getanyfile::
This serves older Git clients which are unable to use the
upload pack service. When enabled, clients are able to read
any file within the repository, including objects that are
no longer reachable from a branch but are still present.
It is enabled by default, but a repository can disable it
by setting this configuration item to `false`.
http.uploadpack:: http.uploadpack::
This serves 'git-fetch-pack' and 'git-ls-remote' clients. This serves 'git-fetch-pack' and 'git-ls-remote' clients.
It is enabled by default, but a repository can disable it It is enabled by default, but a repository can disable it

Просмотреть файл

@ -10,6 +10,7 @@
static const char content_type[] = "Content-Type"; static const char content_type[] = "Content-Type";
static const char content_length[] = "Content-Length"; static const char content_length[] = "Content-Length";
static const char last_modified[] = "Last-Modified"; static const char last_modified[] = "Last-Modified";
static int getanyfile = 1;
static struct string_list *query_params; static struct string_list *query_params;
@ -194,6 +195,12 @@ static NORETURN void forbidden(const char *err, ...)
exit(0); exit(0);
} }
static void select_getanyfile(void)
{
if (!getanyfile)
forbidden("Unsupported service: getanyfile");
}
static void send_strbuf(const char *type, struct strbuf *buf) static void send_strbuf(const char *type, struct strbuf *buf)
{ {
hdr_int(content_length, buf->len); hdr_int(content_length, buf->len);
@ -238,37 +245,50 @@ static void send_file(const char *the_type, const char *name)
static void get_text_file(char *name) static void get_text_file(char *name)
{ {
select_getanyfile();
hdr_nocache(); hdr_nocache();
send_file("text/plain", name); send_file("text/plain", name);
} }
static void get_loose_object(char *name) static void get_loose_object(char *name)
{ {
select_getanyfile();
hdr_cache_forever(); hdr_cache_forever();
send_file("application/x-git-loose-object", name); send_file("application/x-git-loose-object", name);
} }
static void get_pack_file(char *name) static void get_pack_file(char *name)
{ {
select_getanyfile();
hdr_cache_forever(); hdr_cache_forever();
send_file("application/x-git-packed-objects", name); send_file("application/x-git-packed-objects", name);
} }
static void get_idx_file(char *name) static void get_idx_file(char *name)
{ {
select_getanyfile();
hdr_cache_forever(); hdr_cache_forever();
send_file("application/x-git-packed-objects-toc", name); send_file("application/x-git-packed-objects-toc", name);
} }
static int http_config(const char *var, const char *value, void *cb) static int http_config(const char *var, const char *value, void *cb)
{ {
struct rpc_service *svc = cb; if (!strcmp(var, "http.getanyfile")) {
getanyfile = git_config_bool(var, value);
return 0;
}
if (!prefixcmp(var, "http.") && if (!prefixcmp(var, "http.")) {
!strcmp(var + 5, svc->config_name)) { int i;
for (i = 0; i < ARRAY_SIZE(rpc_service); i++) {
struct rpc_service *svc = &rpc_service[i];
if (!strcmp(var + 5, svc->config_name)) {
svc->enabled = git_config_bool(var, value); svc->enabled = git_config_bool(var, value);
return 0; return 0;
} }
}
}
/* we are not interested in parsing any other configuration here */ /* we are not interested in parsing any other configuration here */
return 0; return 0;
@ -293,7 +313,6 @@ static struct rpc_service *select_service(const char *name)
if (!svc) if (!svc)
forbidden("Unsupported service: '%s'", name); forbidden("Unsupported service: '%s'", name);
git_config(http_config, svc);
if (svc->enabled < 0) { if (svc->enabled < 0) {
const char *user = getenv("REMOTE_USER"); const char *user = getenv("REMOTE_USER");
svc->enabled = (user && *user) ? 1 : 0; svc->enabled = (user && *user) ? 1 : 0;
@ -442,6 +461,7 @@ static void get_info_refs(char *arg)
run_service(argv); run_service(argv);
} else { } else {
select_getanyfile();
for_each_ref(show_text_ref, &buf); for_each_ref(show_text_ref, &buf);
send_strbuf("text/plain", &buf); send_strbuf("text/plain", &buf);
} }
@ -455,6 +475,7 @@ static void get_info_packs(char *arg)
struct packed_git *p; struct packed_git *p;
size_t cnt = 0; size_t cnt = 0;
select_getanyfile();
prepare_packed_git(); prepare_packed_git();
for (p = packed_git; p; p = p->next) { for (p = packed_git; p; p = p->next) {
if (p->pack_local) if (p->pack_local)
@ -621,6 +642,7 @@ int main(int argc, char **argv)
if (!enter_repo(dir, 0)) if (!enter_repo(dir, 0))
not_found("Not a git repository: '%s'", dir); not_found("Not a git repository: '%s'", dir);
git_config(http_config, NULL);
cmd->imp(cmd_arg); cmd->imp(cmd_arg);
return 0; return 0;
} }