]> git.cameronkatri.com Git - cgit.git/blob - cgit.c
Fix cache algorithm loophole
[cgit.git] / cgit.c
1 /* cgit.c: cgi for the git scm
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9 #include "cgit.h"
10
11 const char cgit_version[] = CGIT_VERSION;
12
13 const char cgit_doctype[] =
14 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
15 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
16
17 const char cgit_error[] =
18 "<div class='error'>%s</div>";
19
20 const char cgit_lib_error[] =
21 "<div class='error'>%s: %s</div>";
22
23 int htmlfd = 0;
24
25 char *cgit_root = "/usr/src/git";
26 char *cgit_root_title = "Git repository browser";
27 char *cgit_css = "/cgit.css";
28 char *cgit_logo = "/git-logo.png";
29 char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/";
30 char *cgit_virtual_root = NULL;
31
32 char *cgit_cache_root = "/var/cache/cgit";
33
34 int cgit_cache_root_ttl = 5;
35 int cgit_cache_repo_ttl = 5;
36 int cgit_cache_dynamic_ttl = 5;
37 int cgit_cache_static_ttl = -1;
38 int cgit_cache_max_create_time = 5;
39
40 char *cgit_repo_name = NULL;
41 char *cgit_repo_desc = NULL;
42 char *cgit_repo_owner = NULL;
43
44 int cgit_query_has_symref = 0;
45 int cgit_query_has_sha1 = 0;
46
47 char *cgit_querystring = NULL;
48 char *cgit_query_repo = NULL;
49 char *cgit_query_page = NULL;
50 char *cgit_query_head = NULL;
51 char *cgit_query_sha1 = NULL;
52
53 struct cacheitem cacheitem;
54
55 int cgit_parse_query(char *txt, configfn fn)
56 {
57 char *t, *value = NULL, c;
58
59 if (!txt)
60 return 0;
61
62 t = txt = xstrdup(txt);
63
64 while((c=*t) != '\0') {
65 if (c=='=') {
66 *t = '\0';
67 value = t+1;
68 } else if (c=='&') {
69 *t = '\0';
70 (*fn)(txt, value);
71 txt = t+1;
72 value = NULL;
73 }
74 t++;
75 }
76 if (t!=txt)
77 (*fn)(txt, value);
78 return 0;
79 }
80
81 void cgit_global_config_cb(const char *name, const char *value)
82 {
83 if (!strcmp(name, "root"))
84 cgit_root = xstrdup(value);
85 else if (!strcmp(name, "root-title"))
86 cgit_root_title = xstrdup(value);
87 else if (!strcmp(name, "css"))
88 cgit_css = xstrdup(value);
89 else if (!strcmp(name, "logo"))
90 cgit_logo = xstrdup(value);
91 else if (!strcmp(name, "logo-link"))
92 cgit_logo_link = xstrdup(value);
93 else if (!strcmp(name, "virtual-root"))
94 cgit_virtual_root = xstrdup(value);
95 }
96
97 void cgit_repo_config_cb(const char *name, const char *value)
98 {
99 if (!strcmp(name, "name"))
100 cgit_repo_name = xstrdup(value);
101 else if (!strcmp(name, "desc"))
102 cgit_repo_desc = xstrdup(value);
103 else if (!strcmp(name, "owner"))
104 cgit_repo_owner = xstrdup(value);
105 }
106
107 void cgit_querystring_cb(const char *name, const char *value)
108 {
109 if (!strcmp(name,"r"))
110 cgit_query_repo = xstrdup(value);
111 else if (!strcmp(name, "p"))
112 cgit_query_page = xstrdup(value);
113 else if (!strcmp(name, "h")) {
114 cgit_query_head = xstrdup(value);
115 cgit_query_has_symref = 1;
116 } else if (!strcmp(name, "id")) {
117 cgit_query_sha1 = xstrdup(value);
118 cgit_query_has_sha1 = 1;
119 }
120 }
121
122 char *cgit_repourl(const char *reponame)
123 {
124 if (cgit_virtual_root) {
125 return fmt("%s/%s/", cgit_virtual_root, reponame);
126 } else {
127 return fmt("?r=%s", reponame);
128 }
129 }
130
131 char *cgit_pageurl(const char *reponame, const char *pagename,
132 const char *query)
133 {
134 if (cgit_virtual_root) {
135 return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame,
136 pagename, query);
137 } else {
138 return fmt("?r=%s&p=%s&%s", reponame, pagename, query);
139 }
140 }
141
142 static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1,
143 int flags, void *cb_data)
144 {
145 struct commit *commit;
146 char buf[256], *url;
147
148 commit = lookup_commit(sha1);
149 if (commit && !parse_commit(commit)){
150 html("<tr><td>");
151 url = cgit_pageurl(cgit_query_repo, "log",
152 fmt("h=%s", refname));
153 html_link_open(url, NULL, NULL);
154 strncpy(buf, refname, sizeof(buf));
155 html_txt(buf);
156 html_link_close();
157 html("</td><td>");
158 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, buf,
159 sizeof(buf), 0, NULL, NULL, 0);
160 html_txt(buf);
161 html("</td></tr>\n");
162 } else {
163 html("<tr><td>");
164 html_txt(buf);
165 html("</td><td>");
166 htmlf("*** bad ref %s", sha1_to_hex(sha1));
167 html("</td></tr>\n");
168 }
169 return 0;
170 }
171
172 /* Sun, 06 Nov 1994 08:49:37 GMT */
173 static char *http_date(time_t t)
174 {
175 static char day[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
176 static char month[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
177 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
178 struct tm *tm = gmtime(&t);
179 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
180 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
181 tm->tm_hour, tm->tm_min, tm->tm_sec);
182 }
183
184 static int ttl_seconds(int ttl)
185 {
186 if (ttl<0)
187 return 60 * 60 * 24 * 365;
188 else
189 return ttl * 60;
190 }
191
192 static void cgit_print_docstart(char *title)
193 {
194 html("Content-Type: text/html; charset=utf-8\n");
195 htmlf("Last-Modified: %s\n", http_date(cacheitem.st.st_mtime));
196 htmlf("Expires: %s\n", http_date(cacheitem.st.st_mtime +
197 ttl_seconds(cacheitem.ttl)));
198 html("\n");
199 html(cgit_doctype);
200 html("<html>\n");
201 html("<head>\n");
202 html("<title>");
203 html_txt(title);
204 html("</title>\n");
205 htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version);
206 html("<link rel='stylesheet' type='text/css' href='");
207 html_attr(cgit_css);
208 html("'/>\n");
209 html("</head>\n");
210 html("<body>\n");
211 }
212
213 static void cgit_print_docend()
214 {
215 html("</body>\n</html>\n");
216 }
217
218 static void cgit_print_pageheader(char *title)
219 {
220 html("<div id='header'>");
221 htmlf("<a href='%s'>", cgit_logo_link);
222 htmlf("<img id='logo' src='%s'/>\n", cgit_logo);
223 htmlf("</a>");
224 html_txt(title);
225 html("</div>");
226 }
227
228 static void cgit_print_repolist()
229 {
230 DIR *d;
231 struct dirent *de;
232 struct stat st;
233 char *name;
234
235 chdir(cgit_root);
236 cgit_print_docstart(cgit_root_title);
237 cgit_print_pageheader(cgit_root_title);
238
239 if (!(d = opendir("."))) {
240 htmlf(cgit_lib_error, "Unable to scan repository directory",
241 strerror(errno));
242 cgit_print_docend();
243 return;
244 }
245
246 html("<h2>Repositories</h2>\n");
247 html("<table class='list'>");
248 html("<tr><th>Name</th><th>Description</th><th>Owner</th></tr>\n");
249 while ((de = readdir(d)) != NULL) {
250 if (de->d_name[0] == '.')
251 continue;
252 if (stat(de->d_name, &st) < 0)
253 continue;
254 if (!S_ISDIR(st.st_mode))
255 continue;
256
257 cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL;
258 name = fmt("%s/info/cgit", de->d_name);
259 if (cgit_read_config(name, cgit_repo_config_cb))
260 continue;
261
262 html("<tr><td>");
263 html_link_open(cgit_repourl(de->d_name), NULL, NULL);
264 html_txt(cgit_repo_name);
265 html_link_close();
266 html("</td><td>");
267 html_txt(cgit_repo_desc);
268 html("</td><td>");
269 html_txt(cgit_repo_owner);
270 html("</td></tr>\n");
271 }
272 closedir(d);
273 html("</table>");
274 cgit_print_docend();
275 }
276
277 static void cgit_print_branches()
278 {
279 html("<table class='list'>");
280 html("<tr><th>Branch name</th><th>Head commit</th></tr>\n");
281 for_each_branch_ref(cgit_print_branch_cb, NULL);
282 html("</table>");
283 }
284
285 static int get_one_line(char *txt)
286 {
287 char *t;
288
289 for(t=txt; *t != '\n' && t != '\0'; t++)
290 ;
291 *t = '\0';
292 return t-txt-1;
293 }
294
295 static void cgit_print_commit_shortlog(struct commit *commit)
296 {
297 char *h, *t, *p;
298 char *tree = NULL, *author = NULL, *subject = NULL;
299 int len;
300 time_t sec;
301 struct tm *time;
302 char buf[32];
303
304 h = t = commit->buffer;
305
306 if (strncmp(h, "tree ", 5))
307 die("Bad commit format: %s",
308 sha1_to_hex(commit->object.sha1));
309
310 len = get_one_line(h);
311 tree = h+5;
312 h += len + 2;
313
314 while (!strncmp(h, "parent ", 7))
315 h += get_one_line(h) + 2;
316
317 if (!strncmp(h, "author ", 7)) {
318 author = h+7;
319 h += get_one_line(h) + 2;
320 t = author;
321 while(t!=h && *t!='<')
322 t++;
323 *t='\0';
324 p = t;
325 while(--t!=author && *t==' ')
326 *t='\0';
327 while(++p!=h && *p!='>')
328 ;
329 while(++p!=h && !isdigit(*p))
330 ;
331
332 t = p;
333 while(++p && isdigit(*p))
334 ;
335 *p = '\0';
336 sec = atoi(t);
337 time = gmtime(&sec);
338 }
339
340 while((len = get_one_line(h)) > 0)
341 h += len+2;
342
343 h++;
344 len = get_one_line(h);
345
346 subject = h;
347
348 html("<tr><td>");
349 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time);
350 html_txt(buf);
351 html("</td><td>");
352 char *qry = fmt("id=%s", sha1_to_hex(commit->object.sha1));
353 char *url = cgit_pageurl(cgit_query_repo, "view", qry);
354 html_link_open(url, NULL, NULL);
355 html_txt(subject);
356 html_link_close();
357 html("</td><td>");
358 html_txt(author);
359 html("</td></tr>\n");
360 }
361
362 static void cgit_print_log(const char *tip, int ofs, int cnt)
363 {
364 struct rev_info rev;
365 struct commit *commit;
366 const char *argv[2] = {NULL, tip};
367 int n = 0;
368
369 init_revisions(&rev, NULL);
370 rev.abbrev = DEFAULT_ABBREV;
371 rev.commit_format = CMIT_FMT_DEFAULT;
372 rev.verbose_header = 1;
373 rev.show_root_diff = 0;
374 setup_revisions(2, argv, &rev, NULL);
375 prepare_revision_walk(&rev);
376
377 html("<h2>Log</h2>");
378 html("<table class='list'>");
379 html("<tr><th>Date</th><th>Message</th><th>Author</th></tr>\n");
380 while ((commit = get_revision(&rev)) != NULL && n++ < 100) {
381 cgit_print_commit_shortlog(commit);
382 free(commit->buffer);
383 commit->buffer = NULL;
384 free_commit_list(commit->parents);
385 commit->parents = NULL;
386 }
387 html("</table>\n");
388 }
389
390 static void cgit_print_repo_summary()
391 {
392 html("<h2>");
393 html_txt("Repo summary page");
394 html("</h2>");
395 cgit_print_branches();
396 }
397
398 static void cgit_print_object(char *hex)
399 {
400 unsigned char sha1[20];
401 //struct object *object;
402 char type[20];
403 unsigned char *buf;
404 unsigned long size;
405
406 if (get_sha1_hex(hex, sha1)){
407 htmlf(cgit_error, "Bad hex value");
408 return;
409 }
410
411 if (sha1_object_info(sha1, type, NULL)){
412 htmlf(cgit_error, "Bad object name");
413 return;
414 }
415
416 buf = read_sha1_file(sha1, type, &size);
417 if (!buf) {
418 htmlf(cgit_error, "Error reading object");
419 return;
420 }
421
422 buf[size] = '\0';
423 html("<h2>Object view</h2>");
424 htmlf("sha1=%s<br/>type=%s<br/>size=%i<br/>", hex, type, size);
425 html("<pre>");
426 html_txt(buf);
427 html("</pre>");
428 }
429
430 static void cgit_print_repo_page()
431 {
432 if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) ||
433 cgit_read_config("info/cgit", cgit_repo_config_cb)) {
434 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
435 cgit_print_docstart(title);
436 cgit_print_pageheader(title);
437 htmlf(cgit_lib_error, "Unable to scan repository",
438 strerror(errno));
439 cgit_print_docend();
440 return;
441 }
442 setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1);
443 char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc);
444 cgit_print_docstart(title);
445 cgit_print_pageheader(title);
446 if (!cgit_query_page)
447 cgit_print_repo_summary();
448 else if (!strcmp(cgit_query_page, "log")) {
449 cgit_print_log(cgit_query_head, 0, 100);
450 } else if (!strcmp(cgit_query_page, "view")) {
451 cgit_print_object(cgit_query_sha1);
452 }
453 cgit_print_docend();
454 }
455
456 static void cgit_fill_cache(struct cacheitem *item)
457 {
458 htmlfd = item->fd;
459 item->st.st_mtime = time(NULL);
460 if (cgit_query_repo)
461 cgit_print_repo_page();
462 else
463 cgit_print_repolist();
464 }
465
466 static void cgit_refresh_cache(struct cacheitem *item)
467 {
468 cache_prepare(item);
469 top:
470 if (!cache_exist(item)) {
471 if (!cache_lock(item)) {
472 sched_yield();
473 goto top;
474 }
475 if (!cache_exist(item))
476 cgit_fill_cache(item);
477 cache_unlock(item);
478 } else if (cache_expired(item) && cache_lock(item)) {
479 if (cache_expired(item))
480 cgit_fill_cache(item);
481 cache_unlock(item);
482 }
483 }
484
485 static void cgit_print_cache(struct cacheitem *item)
486 {
487 static char buf[4096];
488 ssize_t i;
489
490 int fd = open(item->name, O_RDONLY);
491 if (fd<0)
492 die("Unable to open cached file %s", item->name);
493
494 while((i=read(fd, buf, sizeof(buf))) > 0)
495 write(STDOUT_FILENO, buf, i);
496
497 close(fd);
498 }
499
500 int main(int argc, const char **argv)
501 {
502 cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
503 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
504 cgit_parse_query(cgit_querystring, cgit_querystring_cb);
505 cgit_refresh_cache(&cacheitem);
506 cgit_print_cache(&cacheitem);
507 return 0;
508 }