]> gitweb.ps.run Git - ps-cgit/blobdiff - cgit.c
syntax-highlighting.sh: Fix command injection.
[ps-cgit] / cgit.c
diff --git a/cgit.c b/cgit.c
index 5d6e4886dd2fe2b31df0fc0124f0e53d9115ce9d..a97ed69653b36d696c0fd564bfbc4be55f99f2b0 100644 (file)
--- a/cgit.c
+++ b/cgit.c
@@ -1,7 +1,7 @@
 /* cgit.c: cgi for the git scm
  *
  * Copyright (C) 2006 Lars Hjemli
- * Copyright (C) 2010 Jason A. Donenfeld <Jason@zx2c4.com>
+ * Copyright (C) 2010, 2012 Jason A. Donenfeld <Jason@zx2c4.com>
  *
  * Licensed under GNU General Public License v2
  *   (see COPYING for full license text)
@@ -29,6 +29,7 @@ void add_mimetype(const char *name, const char *value)
 struct cgit_filter *new_filter(const char *cmd, filter_type filtertype)
 {
        struct cgit_filter *f;
+       int args_size = 0;
        int extra_args;
 
        if (!cmd || !cmd[0])
@@ -48,9 +49,10 @@ struct cgit_filter *new_filter(const char *cmd, filter_type filtertype)
 
        f = xmalloc(sizeof(struct cgit_filter));
        f->cmd = xstrdup(cmd);
-       f->argv = xmalloc((2 + extra_args) * sizeof(char *));
+       args_size = (2 + extra_args) * sizeof(char *);
+       f->argv = xmalloc(args_size);
+       memset(f->argv, 0, args_size);
        f->argv[0] = f->cmd;
-       f->argv[1] = NULL;
        return f;
 }
 
@@ -58,6 +60,8 @@ static void process_cached_repolist(const char *path);
 
 void repo_config(struct cgit_repo *repo, const char *name, const char *value)
 {
+       struct string_list_item *item;
+
        if (!strcmp(name, "name"))
                repo->name = xstrdup(value);
        else if (!strcmp(name, "clone-url"))
@@ -71,20 +75,28 @@ void repo_config(struct cgit_repo *repo, const char *name, const char *value)
        else if (!strcmp(name, "snapshots"))
                repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value);
        else if (!strcmp(name, "enable-commit-graph"))
-               repo->enable_commit_graph = ctx.cfg.enable_commit_graph * atoi(value);
+               repo->enable_commit_graph = atoi(value);
        else if (!strcmp(name, "enable-log-filecount"))
-               repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
+               repo->enable_log_filecount = atoi(value);
        else if (!strcmp(name, "enable-log-linecount"))
-               repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
+               repo->enable_log_linecount = atoi(value);
        else if (!strcmp(name, "enable-remote-branches"))
                repo->enable_remote_branches = atoi(value);
        else if (!strcmp(name, "enable-subject-links"))
                repo->enable_subject_links = atoi(value);
-       else if (!strcmp(name, "max-stats"))
+       else if (!strcmp(name, "commit-sort")) {
+               if (!strcmp(value, "date"))
+                       repo->commit_sort = 1;
+               if (!strcmp(value, "topo"))
+                       repo->commit_sort = 2;
+       } else if (!strcmp(name, "max-stats"))
                repo->max_stats = cgit_find_stats_period(value, NULL);
        else if (!strcmp(name, "module-link"))
                repo->module_link= xstrdup(value);
-       else if (!strcmp(name, "section"))
+       else if (!prefixcmp(name, "module-link.")) {
+               item = string_list_append(&repo->submodules, name + 12);
+               item->util = xstrdup(value);
+       } else if (!strcmp(name, "section"))
                repo->section = xstrdup(value);
        else if (!strcmp(name, "readme") && value != NULL)
                repo->readme = xstrdup(value);
@@ -156,8 +168,8 @@ void config_cb(const char *name, const char *value)
                ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
        else if (!strcmp(name, "enable-filter-overrides"))
                ctx.cfg.enable_filter_overrides = atoi(value);
-       else if (!strcmp(name, "enable-gitweb-owner"))
-               ctx.cfg.enable_gitweb_owner = atoi(value);
+       else if (!strcmp(name, "enable-http-clone"))
+               ctx.cfg.enable_http_clone = atoi(value);
        else if (!strcmp(name, "enable-index-links"))
                ctx.cfg.enable_index_links = atoi(value);
        else if (!strcmp(name, "enable-commit-graph"))
@@ -172,6 +184,8 @@ void config_cb(const char *name, const char *value)
                ctx.cfg.enable_subject_links = atoi(value);
        else if (!strcmp(name, "enable-tree-linenumbers"))
                ctx.cfg.enable_tree_linenumbers = atoi(value);
+       else if (!strcmp(name, "enable-git-config"))
+               ctx.cfg.enable_git_config = atoi(value);
        else if (!strcmp(name, "max-stats"))
                ctx.cfg.max_stats = cgit_find_stats_period(value, NULL);
        else if (!strcmp(name, "cache-size"))
@@ -188,6 +202,8 @@ void config_cb(const char *name, const char *value)
                ctx.cfg.cache_static_ttl = atoi(value);
        else if (!strcmp(name, "cache-dynamic-ttl"))
                ctx.cfg.cache_dynamic_ttl = atoi(value);
+       else if (!strcmp(name, "case-sensitive-sort"))
+               ctx.cfg.case_sensitive_sort = atoi(value);
        else if (!strcmp(name, "about-filter"))
                ctx.cfg.about_filter = new_filter(value, ABOUT);
        else if (!strcmp(name, "commit-filter"))
@@ -220,6 +236,10 @@ void config_cb(const char *name, const char *value)
                ctx.cfg.scan_hidden_path = atoi(value);
        else if (!strcmp(name, "section-from-path"))
                ctx.cfg.section_from_path = atoi(value);
+       else if (!strcmp(name, "repository-sort"))
+               ctx.cfg.repository_sort = xstrdup(value);
+       else if (!strcmp(name, "section-sort"))
+               ctx.cfg.section_sort = atoi(value);
        else if (!strcmp(name, "source-filter"))
                ctx.cfg.source_filter = new_filter(value, SOURCE);
        else if (!strcmp(name, "summary-log"))
@@ -232,6 +252,8 @@ void config_cb(const char *name, const char *value)
                ctx.cfg.ssdiff = atoi(value);
        else if (!strcmp(name, "agefile"))
                ctx.cfg.agefile = xstrdup(value);
+       else if (!strcmp(name, "mimetype-file"))
+               ctx.cfg.mimetype_file = xstrdup(value);
        else if (!strcmp(name, "renamelimit"))
                ctx.cfg.renamelimit = atoi(value);
        else if (!strcmp(name, "remove-suffix"))
@@ -240,9 +262,16 @@ void config_cb(const char *name, const char *value)
                ctx.cfg.robots = xstrdup(value);
        else if (!strcmp(name, "clone-prefix"))
                ctx.cfg.clone_prefix = xstrdup(value);
+       else if (!strcmp(name, "clone-url"))
+               ctx.cfg.clone_url = xstrdup(value);
        else if (!strcmp(name, "local-time"))
                ctx.cfg.local_time = atoi(value);
-       else if (!prefixcmp(name, "mimetype."))
+       else if (!strcmp(name, "commit-sort")) {
+               if (!strcmp(value, "date"))
+                       ctx.cfg.commit_sort = 1;
+               if (!strcmp(value, "topo"))
+                       ctx.cfg.commit_sort = 2;
+       } else if (!prefixcmp(name, "mimetype."))
                add_mimetype(name + 9, value);
        else if (!strcmp(name, "include"))
                parse_configfile(expand_macros(value), config_cb);
@@ -292,6 +321,7 @@ static void querystring_cb(const char *name, const char *value)
                ctx.qry.period = xstrdup(value);
        } else if (!strcmp(name, "ss")) {
                ctx.qry.ssdiff = atoi(value);
+               ctx.qry.has_ssdiff = 1;
        } else if (!strcmp(name, "all")) {
                ctx.qry.show_all = atoi(value);
        } else if (!strcmp(name, "context")) {
@@ -319,11 +349,13 @@ static void prepare_context(struct cgit_context *ctx)
        ctx->cfg.cache_root_ttl = 5;
        ctx->cfg.cache_scanrc_ttl = 15;
        ctx->cfg.cache_static_ttl = -1;
+       ctx->cfg.case_sensitive_sort = 1;
        ctx->cfg.css = "/cgit.css";
        ctx->cfg.logo = "/cgit.png";
        ctx->cfg.local_time = 0;
-       ctx->cfg.enable_gitweb_owner = 1;
+       ctx->cfg.enable_http_clone = 1;
        ctx->cfg.enable_tree_linenumbers = 1;
+       ctx->cfg.enable_git_config = 0;
        ctx->cfg.max_repo_count = 50;
        ctx->cfg.max_commit_count = 50;
        ctx->cfg.max_lock_attempts = 5;
@@ -331,7 +363,6 @@ static void prepare_context(struct cgit_context *ctx)
        ctx->cfg.max_repodesc_len = 80;
        ctx->cfg.max_blob_size = 0;
        ctx->cfg.max_stats = 0;
-       ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
        ctx->cfg.project_list = NULL;
        ctx->cfg.renamelimit = -1;
        ctx->cfg.remove_suffix = 0;
@@ -341,6 +372,8 @@ static void prepare_context(struct cgit_context *ctx)
        ctx->cfg.scan_hidden_path = 0;
        ctx->cfg.script_name = CGIT_SCRIPT_NAME;
        ctx->cfg.section = "";
+       ctx->cfg.repository_sort = "name";
+       ctx->cfg.section_sort = 1;
        ctx->cfg.summary_branches = 10;
        ctx->cfg.summary_log = 10;
        ctx->cfg.summary_tags = 10;
@@ -409,18 +442,33 @@ char *find_default_branch(struct cgit_repo *repo)
        return ref;
 }
 
+static char *guess_defbranch(const char *repo_path)
+{
+       const char *ref;
+       unsigned char sha1[20];
+
+       ref = resolve_ref("HEAD", sha1, 0, NULL);
+       if (!ref || prefixcmp(ref, "refs/heads/"))
+               return "master";
+       return xstrdup(ref + 11);
+}
+
 static int prepare_repo_cmd(struct cgit_context *ctx)
 {
        char *tmp;
        unsigned char sha1[20];
        int nongit = 0;
+       int rc;
 
        setenv("GIT_DIR", ctx->repo->path, 1);
        setup_git_directory_gently(&nongit);
        if (nongit) {
+               rc = errno;
                ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
                                      "config error");
-               tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
+               tmp = fmt("Failed to open %s: %s",
+                         ctx->repo->name,
+                         rc ? strerror(rc) : "Not a valid git repository");
                ctx->repo = NULL;
                cgit_print_http_headers(ctx);
                cgit_print_docstart(ctx);
@@ -431,10 +479,12 @@ static int prepare_repo_cmd(struct cgit_context *ctx)
        }
        ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
 
+       if (!ctx->repo->defbranch)
+               ctx->repo->defbranch = guess_defbranch(ctx->repo->path);
+
        if (!ctx->qry.head) {
                ctx->qry.nohead = 1;
                ctx->qry.head = find_default_branch(ctx->repo);
-               ctx->repo->defbranch = ctx->qry.head;
        }
 
        if (!ctx->qry.head) {
@@ -450,7 +500,7 @@ static int prepare_repo_cmd(struct cgit_context *ctx)
                tmp = xstrdup(ctx->qry.head);
                ctx->qry.head = ctx->repo->defbranch;
                ctx->page.status = 404;
-               ctx->page.statusmsg = "not found";
+               ctx->page.statusmsg = "Not found";
                cgit_print_http_headers(ctx);
                cgit_print_docstart(ctx);
                cgit_print_pageheader(ctx);
@@ -458,6 +508,8 @@ static int prepare_repo_cmd(struct cgit_context *ctx)
                cgit_print_docend();
                return 1;
        }
+       sort_string_list(&ctx->repo->submodules);
+       cgit_prepare_repo_env(ctx->repo);
        return 0;
 }
 
@@ -469,6 +521,8 @@ static void process_request(void *cbdata)
        cmd = cgit_get_cmd(ctx);
        if (!cmd) {
                ctx->page.title = "cgit error";
+               ctx->page.status = 404;
+               ctx->page.statusmsg = "Not found";
                cgit_print_http_headers(ctx);
                cgit_print_docstart(ctx);
                cgit_print_pageheader(ctx);
@@ -477,6 +531,11 @@ static void process_request(void *cbdata)
                return;
        }
 
+       if (!ctx->cfg.enable_http_clone && cmd->is_clone) {
+               html_status(404, "Not found", 0);
+               return;
+       }
+
        /* If cmd->want_vpath is set, assume ctx->qry.path contains a "virtual"
         * in-project path limit to be made available at ctx->qry.vpath.
         * Otherwise, no path limit is in effect (ctx->qry.vpath = NULL).
@@ -768,8 +827,11 @@ int main(int argc, const char **argv)
         * that virtual-root equals SCRIPT_NAME, minus any possibly
         * trailing slashes.
         */
-       if (!ctx.cfg.virtual_root)
+       if (!ctx.cfg.virtual_root && ctx.cfg.script_name) {
                ctx.cfg.virtual_root = trim_end(ctx.cfg.script_name, '/');
+               if (!ctx.cfg.virtual_root)
+                       ctx.cfg.virtual_root = "";
+        }
 
        /* If no url parameter is specified on the querystring, lets
         * use PATH_INFO as url. This allows cgit to work with virtual