diff options
| author | 2007-02-03 23:02:55 (JST) | |
|---|---|---|
| committer | 2007-02-05 05:47:46 (JST) | |
| commit | ce1c7336e5b3e3ebe8f8c9029c405aedec98c208 (patch) | |
| tree | b51a59a9552b32519cf694c0f5dc68c5a739069c | |
| parent | ebd7b0fbc378e9beca0b275c5cd9150c930bde56 (diff) | |
| download | cgit-ce1c7336e5b3e3ebe8f8c9029c405aedec98c208.zip cgit-ce1c7336e5b3e3ebe8f8c9029c405aedec98c208.tar.gz | |
Read repo-info from /etc/cgitrc
This makes cgit read all repo-info from the configfile, instead of scanning for
possible git-dirs below a common root path. This is primarily done to get
better security (separate physical path from logical repo-name).
In /etc/cgitrc each repo is registered with the following keys:
repo.url
repo.name
repo.path
repo.desc
repo.owner
Note:
*Required keys are repo.url and repo.path, all others are optional
*Each occurrence of repo.url starts a new repository registration
*Default value for repo.name is taken from repo.url
*The value of repo.url cannot contain characters with special meaning for
urls (i.e. one of /?%&), while repo.name can contain anything.
Example:
repo.url=cgit-pub
repo.name=cgit/public
repo.path=/pub/git/cgit
repo.desc=My public cgit repo
repo.owner=Lars Hjemli
repo.url=cgit-priv
repo.name=cgit/private
repo.path=/home/larsh/src/cgit/.git
repo.desc=My private cgit repo
repo.owner=Lars Hjemli
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
| -rw-r--r-- | cgit.c | 64 | ||||
| -rw-r--r-- | cgit.h | 18 | ||||
| -rw-r--r-- | cgitrc | 13 | ||||
| -rw-r--r-- | shared.c | 41 | ||||
| -rw-r--r-- | ui-repolist.c | 39 |
5 files changed, 120 insertions, 55 deletions
| @@ -10,18 +10,45 @@ | |||
| 10 | 10 | ||
| 11 | const char cgit_version[] = CGIT_VERSION; | 11 | const char cgit_version[] = CGIT_VERSION; |
| 12 | 12 | ||
| 13 | static void cgit_prepare_cache(struct cacheitem *item) | 13 | |
| 14 | static struct repoinfo *cgit_get_repoinfo(char *url) | ||
| 15 | { | ||
| 16 | int i; | ||
| 17 | struct repoinfo *repo; | ||
| 18 | |||
| 19 | for (i=0; i<cgit_repolist.count; i++) { | ||
| 20 | repo = &cgit_repolist.repos[i]; | ||
| 21 | if (!strcmp(repo->url, url)) | ||
| 22 | return repo; | ||
| 23 | } | ||
| 24 | return NULL; | ||
| 25 | } | ||
| 26 | |||
| 27 | |||
| 28 | static int cgit_prepare_cache(struct cacheitem *item) | ||
| 14 | { | 29 | { |
| 15 | if (!cgit_query_repo) { | 30 | if (!cgit_query_repo) { |
| 16 | item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); | 31 | item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); |
| 17 | item->ttl = cgit_cache_root_ttl; | 32 | item->ttl = cgit_cache_root_ttl; |
| 18 | } else if (!cgit_query_page) { | 33 | return 1; |
| 34 | } | ||
| 35 | cgit_repo = cgit_get_repoinfo(cgit_query_repo); | ||
| 36 | if (!cgit_repo) { | ||
| 37 | char *title = fmt("%s - %s", cgit_root_title, "Bad request"); | ||
| 38 | cgit_print_docstart(title, item); | ||
| 39 | cgit_print_pageheader(title, 0); | ||
| 40 | cgit_print_error(fmt("Unknown repo: %s", cgit_query_repo)); | ||
| 41 | cgit_print_docend(); | ||
| 42 | return 0; | ||
| 43 | } | ||
| 44 | |||
| 45 | if (!cgit_query_page) { | ||
| 19 | item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root, | 46 | item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root, |
| 20 | cgit_query_repo)); | 47 | cgit_repo->url)); |
| 21 | item->ttl = cgit_cache_repo_ttl; | 48 | item->ttl = cgit_cache_repo_ttl; |
| 22 | } else { | 49 | } else { |
| 23 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, | 50 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, |
| 24 | cgit_query_repo, cgit_query_page, | 51 | cgit_repo->url, cgit_query_page, |
| 25 | cache_safe_filename(cgit_querystring))); | 52 | cache_safe_filename(cgit_querystring))); |
| 26 | if (cgit_query_has_symref) | 53 | if (cgit_query_has_symref) |
| 27 | item->ttl = cgit_cache_dynamic_ttl; | 54 | item->ttl = cgit_cache_dynamic_ttl; |
| @@ -30,13 +57,16 @@ static void cgit_prepare_cache(struct cacheitem *item) | |||
| 30 | else | 57 | else |
| 31 | item->ttl = cgit_cache_repo_ttl; | 58 | item->ttl = cgit_cache_repo_ttl; |
| 32 | } | 59 | } |
| 60 | return 1; | ||
| 33 | } | 61 | } |
| 34 | 62 | ||
| 35 | static void cgit_print_repo_page(struct cacheitem *item) | 63 | static void cgit_print_repo_page(struct cacheitem *item) |
| 36 | { | 64 | { |
| 37 | if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || | 65 | char *title; |
| 38 | cgit_read_config("info/cgit", cgit_repo_config_cb)) { | 66 | int show_search; |
| 39 | char *title = fmt("%s - %s", cgit_root_title, "Bad request"); | 67 | |
| 68 | if (chdir(cgit_repo->path)) { | ||
| 69 | title = fmt("%s - %s", cgit_root_title, "Bad request"); | ||
| 40 | cgit_print_docstart(title, item); | 70 | cgit_print_docstart(title, item); |
| 41 | cgit_print_pageheader(title, 0); | 71 | cgit_print_pageheader(title, 0); |
| 42 | cgit_print_error(fmt("Unable to scan repository: %s", | 72 | cgit_print_error(fmt("Unable to scan repository: %s", |
| @@ -44,9 +74,10 @@ static void cgit_print_repo_page(struct cacheitem *item) | |||
| 44 | cgit_print_docend(); | 74 | cgit_print_docend(); |
| 45 | return; | 75 | return; |
| 46 | } | 76 | } |
| 47 | setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1); | 77 | |
| 48 | char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); | 78 | title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); |
| 49 | int show_search = 0; | 79 | show_search = 0; |
| 80 | setenv("GIT_DIR", cgit_repo->path, 1); | ||
| 50 | if (cgit_query_page && !strcmp(cgit_query_page, "log")) | 81 | if (cgit_query_page && !strcmp(cgit_query_page, "log")) |
| 51 | show_search = 1; | 82 | show_search = 1; |
| 52 | cgit_print_docstart(title, item); | 83 | cgit_print_docstart(title, item); |
| @@ -131,9 +162,6 @@ static void cgit_parse_args(int argc, const char **argv) | |||
| 131 | int i; | 162 | int i; |
| 132 | 163 | ||
| 133 | for (i = 1; i < argc; i++) { | 164 | for (i = 1; i < argc; i++) { |
| 134 | if (!strncmp(argv[i], "--root=", 7)) { | ||
| 135 | cgit_root = xstrdup(argv[i]+7); | ||
| 136 | } | ||
| 137 | if (!strncmp(argv[i], "--cache=", 8)) { | 165 | if (!strncmp(argv[i], "--cache=", 8)) { |
| 138 | cgit_cache_root = xstrdup(argv[i]+8); | 166 | cgit_cache_root = xstrdup(argv[i]+8); |
| 139 | } | 167 | } |
| @@ -167,13 +195,19 @@ int main(int argc, const char **argv) | |||
| 167 | { | 195 | { |
| 168 | struct cacheitem item; | 196 | struct cacheitem item; |
| 169 | 197 | ||
| 198 | htmlfd = STDOUT_FILENO; | ||
| 199 | item.st.st_mtime = time(NULL); | ||
| 200 | cgit_repolist.length = 0; | ||
| 201 | cgit_repolist.count = 0; | ||
| 202 | cgit_repolist.repos = NULL; | ||
| 203 | |||
| 170 | cgit_read_config("/etc/cgitrc", cgit_global_config_cb); | 204 | cgit_read_config("/etc/cgitrc", cgit_global_config_cb); |
| 171 | if (getenv("QUERY_STRING")) | 205 | if (getenv("QUERY_STRING")) |
| 172 | cgit_querystring = xstrdup(getenv("QUERY_STRING")); | 206 | cgit_querystring = xstrdup(getenv("QUERY_STRING")); |
| 173 | cgit_parse_args(argc, argv); | 207 | cgit_parse_args(argc, argv); |
| 174 | cgit_parse_query(cgit_querystring, cgit_querystring_cb); | 208 | cgit_parse_query(cgit_querystring, cgit_querystring_cb); |
| 175 | 209 | if (!cgit_prepare_cache(&item)) | |
| 176 | cgit_prepare_cache(&item); | 210 | return 0; |
| 177 | if (cgit_nocache) { | 211 | if (cgit_nocache) { |
| 178 | item.fd = STDOUT_FILENO; | 212 | item.fd = STDOUT_FILENO; |
| 179 | cgit_fill_cache(&item); | 213 | cgit_fill_cache(&item); |
| @@ -15,6 +15,20 @@ struct cacheitem { | |||
| 15 | int fd; | 15 | int fd; |
| 16 | }; | 16 | }; |
| 17 | 17 | ||
| 18 | struct repoinfo { | ||
| 19 | char *url; | ||
| 20 | char *name; | ||
| 21 | char *path; | ||
| 22 | char *desc; | ||
| 23 | char *owner; | ||
| 24 | }; | ||
| 25 | |||
| 26 | struct repolist { | ||
| 27 | int length; | ||
| 28 | int count; | ||
| 29 | struct repoinfo *repos; | ||
| 30 | }; | ||
| 31 | |||
| 18 | struct commitinfo { | 32 | struct commitinfo { |
| 19 | struct commit *commit; | 33 | struct commit *commit; |
| 20 | char *author; | 34 | char *author; |
| @@ -36,7 +50,9 @@ struct taginfo { | |||
| 36 | 50 | ||
| 37 | extern const char cgit_version[]; | 51 | extern const char cgit_version[]; |
| 38 | 52 | ||
| 39 | extern char *cgit_root; | 53 | extern struct repolist cgit_repolist; |
| 54 | extern struct repoinfo *cgit_repo; | ||
| 55 | |||
| 40 | extern char *cgit_root_title; | 56 | extern char *cgit_root_title; |
| 41 | extern char *cgit_css; | 57 | extern char *cgit_css; |
| 42 | extern char *cgit_logo; | 58 | extern char *cgit_logo; |
| @@ -3,10 +3,6 @@ | |||
| 3 | ## | 3 | ## |
| 4 | 4 | ||
| 5 | 5 | ||
| 6 | ## root folder for git repos | ||
| 7 | #root=/usr/src/git | ||
| 8 | |||
| 9 | |||
| 10 | ## base for virtual urls. If specified, rewrite rules must be added to | 6 | ## base for virtual urls. If specified, rewrite rules must be added to |
| 11 | ## httpd.conf. Possible rules for /git/ when cgit.cgi is accessed as /cgit.cgi: | 7 | ## httpd.conf. Possible rules for /git/ when cgit.cgi is accessed as /cgit.cgi: |
| 12 | ## | 8 | ## |
| @@ -61,3 +57,12 @@ | |||
| 61 | 57 | ||
| 62 | ## ttl for static pages (addressed by SHA-1) | 58 | ## ttl for static pages (addressed by SHA-1) |
| 63 | #cache-static-ttl=-1 | 59 | #cache-static-ttl=-1 |
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | ## Example repository entry | ||
| 64 | #repo.url=cgit | ||
| 65 | #repo.name=cgit | ||
| 66 | #repo.desc=the caching cgi for git | ||
| 67 | #repo.path=/pub/git/cgit | ||
| 68 | #repo.owner=Lars Hjemli | ||
| @@ -8,7 +8,9 @@ | |||
| 8 | 8 | ||
| 9 | #include "cgit.h" | 9 | #include "cgit.h" |
| 10 | 10 | ||
| 11 | char *cgit_root = "/usr/src/git"; | 11 | struct repolist cgit_repolist; |
| 12 | struct repoinfo *cgit_repo; | ||
| 13 | |||
| 12 | char *cgit_root_title = "Git repository browser"; | 14 | char *cgit_root_title = "Git repository browser"; |
| 13 | char *cgit_css = "/cgit.css"; | 15 | char *cgit_css = "/cgit.css"; |
| 14 | char *cgit_logo = "/git-logo.png"; | 16 | char *cgit_logo = "/git-logo.png"; |
| @@ -46,11 +48,32 @@ int cgit_query_ofs = 0; | |||
| 46 | 48 | ||
| 47 | int htmlfd = 0; | 49 | int htmlfd = 0; |
| 48 | 50 | ||
| 51 | struct repoinfo *add_repo(const char *url) | ||
| 52 | { | ||
| 53 | struct repoinfo *ret; | ||
| 54 | |||
| 55 | if (++cgit_repolist.count > cgit_repolist.length) { | ||
| 56 | if (cgit_repolist.length == 0) | ||
| 57 | cgit_repolist.length = 8; | ||
| 58 | else | ||
| 59 | cgit_repolist.length *= 2; | ||
| 60 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, | ||
| 61 | cgit_repolist.length * | ||
| 62 | sizeof(struct repoinfo)); | ||
| 63 | } | ||
| 64 | |||
| 65 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; | ||
| 66 | ret->url = xstrdup(url); | ||
| 67 | ret->name = ret->url; | ||
| 68 | ret->path = NULL; | ||
| 69 | ret->desc = NULL; | ||
| 70 | ret->owner = NULL; | ||
| 71 | return ret; | ||
| 72 | } | ||
| 73 | |||
| 49 | void cgit_global_config_cb(const char *name, const char *value) | 74 | void cgit_global_config_cb(const char *name, const char *value) |
| 50 | { | 75 | { |
| 51 | if (!strcmp(name, "root")) | 76 | if (!strcmp(name, "root-title")) |
| 52 | cgit_root = xstrdup(value); | ||
| 53 | else if (!strcmp(name, "root-title")) | ||
| 54 | cgit_root_title = xstrdup(value); | 77 | cgit_root_title = xstrdup(value); |
| 55 | else if (!strcmp(name, "css")) | 78 | else if (!strcmp(name, "css")) |
| 56 | cgit_css = xstrdup(value); | 79 | cgit_css = xstrdup(value); |
| @@ -74,6 +97,16 @@ void cgit_global_config_cb(const char *name, const char *value) | |||
| 74 | cgit_cache_dynamic_ttl = atoi(value); | 97 | cgit_cache_dynamic_ttl = atoi(value); |
| 75 | else if (!strcmp(name, "max-message-length")) | 98 | else if (!strcmp(name, "max-message-length")) |
| 76 | cgit_max_msg_len = atoi(value); | 99 | cgit_max_msg_len = atoi(value); |
| 100 | else if (!strcmp(name, "repo.url")) | ||
| 101 | cgit_repo = add_repo(value); | ||
| 102 | else if (!strcmp(name, "repo.name")) | ||
| 103 | cgit_repo->name = xstrdup(value); | ||
| 104 | else if (cgit_repo && !strcmp(name, "repo.path")) | ||
| 105 | cgit_repo->path = xstrdup(value); | ||
| 106 | else if (cgit_repo && !strcmp(name, "repo.desc")) | ||
| 107 | cgit_repo->desc = xstrdup(value); | ||
| 108 | else if (cgit_repo && !strcmp(name, "repo.owner")) | ||
| 109 | cgit_repo->owner = xstrdup(value); | ||
| 77 | } | 110 | } |
| 78 | 111 | ||
| 79 | void cgit_repo_config_cb(const char *name, const char *value) | 112 | void cgit_repo_config_cb(const char *name, const char *value) |
diff --git a/ui-repolist.c b/ui-repolist.c index bd4af59..011ec95 100644 --- a/ui-repolist.c +++ b/ui-repolist.c | |||
| @@ -10,54 +10,31 @@ | |||
| 10 | 10 | ||
| 11 | void cgit_print_repolist(struct cacheitem *item) | 11 | void cgit_print_repolist(struct cacheitem *item) |
| 12 | { | 12 | { |
| 13 | DIR *d; | 13 | struct repoinfo *repo; |
| 14 | struct dirent *de; | 14 | int i; |
| 15 | struct stat st; | ||
| 16 | char *name; | ||
| 17 | 15 | ||
| 18 | chdir(cgit_root); | ||
| 19 | cgit_print_docstart(cgit_root_title, item); | 16 | cgit_print_docstart(cgit_root_title, item); |
| 20 | cgit_print_pageheader(cgit_root_title, 0); | 17 | cgit_print_pageheader(cgit_root_title, 0); |
| 21 | 18 | ||
| 22 | if (!(d = opendir("."))) { | ||
| 23 | cgit_print_error(fmt("Unable to scan repository directory: %s", | ||
| 24 | strerror(errno))); | ||
| 25 | cgit_print_docend(); | ||
| 26 | return; | ||
| 27 | } | ||
| 28 | |||
| 29 | html("<h2>Repositories</h2>\n"); | 19 | html("<h2>Repositories</h2>\n"); |
| 30 | html("<table class='list nowrap'>"); | 20 | html("<table class='list nowrap'>"); |
| 31 | html("<tr class='nohover'>" | 21 | html("<tr class='nohover'>" |
| 32 | "<th class='left'>Name</th>" | 22 | "<th class='left'>Name</th>" |
| 33 | "<th class='left'>Description</th>" | 23 | "<th class='left'>Description</th>" |
| 34 | "<th class='left'>Owner</th></tr>\n"); | 24 | "<th class='left'>Owner</th></tr>\n"); |
| 35 | while ((de = readdir(d)) != NULL) { | ||
| 36 | if (de->d_name[0] == '.') | ||
| 37 | continue; | ||
| 38 | if (stat(de->d_name, &st) < 0) | ||
| 39 | continue; | ||
| 40 | if (!S_ISDIR(st.st_mode)) | ||
| 41 | continue; | ||
| 42 | |||
| 43 | cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL; | ||
| 44 | name = fmt("%s/info/cgit", de->d_name); | ||
| 45 | if (cgit_read_config(name, cgit_repo_config_cb)) | ||
| 46 | continue; | ||
| 47 | 25 | ||
| 26 | for (i=0; i<cgit_repolist.count; i++) { | ||
| 27 | repo = &cgit_repolist.repos[i]; | ||
| 48 | html("<tr><td>"); | 28 | html("<tr><td>"); |
| 49 | html_link_open(cgit_repourl(de->d_name), NULL, NULL); | 29 | html_link_open(cgit_repourl(repo->url), NULL, NULL); |
| 50 | html_txt(cgit_repo_name); | 30 | html_txt(repo->name); |
| 51 | html_link_close(); | 31 | html_link_close(); |
| 52 | html("</td><td>"); | 32 | html("</td><td>"); |
| 53 | html_txt(cgit_repo_desc); | 33 | html_txt(repo->desc); |
| 54 | html("</td><td>"); | 34 | html("</td><td>"); |
| 55 | html_txt(cgit_repo_owner); | 35 | html_txt(repo->owner); |
| 56 | html("</td></tr>\n"); | 36 | html("</td></tr>\n"); |
| 57 | } | 37 | } |
| 58 | closedir(d); | ||
| 59 | html("</table>"); | 38 | html("</table>"); |
| 60 | cgit_print_docend(); | 39 | cgit_print_docend(); |
| 61 | } | 40 | } |
| 62 | |||
| 63 | |||
