author | Lars Hjemli <hjemli@gmail.com> | 2007-02-03 14:02:55 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2007-02-04 20:47:46 (UTC) |
commit | ce1c7336e5b3e3ebe8f8c9029c405aedec98c208 (patch) (unidiff) | |
tree | b51a59a9552b32519cf694c0f5dc68c5a739069c | |
parent | ebd7b0fbc378e9beca0b275c5cd9150c930bde56 (diff) | |
download | cgit-ce1c7336e5b3e3ebe8f8c9029c405aedec98c208.zip cgit-ce1c7336e5b3e3ebe8f8c9029c405aedec98c208.tar.gz cgit-ce1c7336e5b3e3ebe8f8c9029c405aedec98c208.tar.bz2 |
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
@@ -12,3 +12,18 @@ 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 | { |
@@ -17,5 +32,17 @@ static void cgit_prepare_cache(struct cacheitem *item) | |||
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; |
@@ -23,3 +50,3 @@ static void cgit_prepare_cache(struct cacheitem *item) | |||
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))); |
@@ -32,2 +59,3 @@ static void cgit_prepare_cache(struct cacheitem *item) | |||
32 | } | 59 | } |
60 | return 1; | ||
33 | } | 61 | } |
@@ -36,5 +64,7 @@ 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); |
@@ -46,5 +76,6 @@ static void cgit_print_repo_page(struct cacheitem *item) | |||
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")) |
@@ -133,5 +164,2 @@ static void cgit_parse_args(int argc, const char **argv) | |||
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)) { |
@@ -169,2 +197,8 @@ int main(int argc, const char **argv) | |||
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); |
@@ -174,4 +208,4 @@ int main(int argc, const char **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) { |
@@ -17,2 +17,16 @@ struct cacheitem { | |||
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 { |
@@ -38,3 +52,5 @@ 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; |
@@ -5,6 +5,2 @@ | |||
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 |
@@ -63 +59,10 @@ | |||
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 | ||
@@ -10,3 +10,5 @@ | |||
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"; |
@@ -48,7 +50,28 @@ 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); |
@@ -76,2 +99,12 @@ void cgit_global_config_cb(const char *name, const char *value) | |||
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 | } |
diff --git a/ui-repolist.c b/ui-repolist.c index bd4af59..011ec95 100644 --- a/ui-repolist.c +++ b/ui-repolist.c | |||
@@ -12,8 +12,5 @@ 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); |
@@ -21,9 +18,2 @@ void cgit_print_repolist(struct cacheitem *item) | |||
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"); |
@@ -34,26 +24,15 @@ void cgit_print_repolist(struct cacheitem *item) | |||
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>"); |
@@ -61,3 +40 @@ void cgit_print_repolist(struct cacheitem *item) | |||
61 | } | } | |
62 | |||
63 | |||