]> git.armaanb.net Git - stagit.git/blob - stagit-index.c
check path truncation
[stagit.git] / stagit-index.c
1 #include <sys/stat.h>
2
3 #include <err.h>
4 #include <errno.h>
5 #include <inttypes.h>
6 #include <libgen.h>
7 #include <limits.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12
13 #include <git2.h>
14
15 #include "compat.h"
16 #include "config.h"
17
18 static git_repository *repo;
19
20 static const char *relpath = "";
21 static const char *repodir;
22
23 static char description[255] = "Repositories";
24 static char name[255];
25 static char owner[255];
26
27 FILE *
28 efopen(const char *name, const char *flags)
29 {
30         FILE *fp;
31
32         if (!(fp = fopen(name, flags)))
33                 err(1, "fopen");
34
35         return fp;
36 }
37
38 /* Escape characters below as HTML 2.0 / XML 1.0. */
39 void
40 xmlencode(FILE *fp, const char *s, size_t len)
41 {
42         size_t i;
43
44         for (i = 0; *s && i < len; s++, i++) {
45                 switch(*s) {
46                 case '<':  fputs("&lt;",   fp); break;
47                 case '>':  fputs("&gt;",   fp); break;
48                 case '\'': fputs("&apos;", fp); break;
49                 case '&':  fputs("&amp;",  fp); break;
50                 case '"':  fputs("&quot;", fp); break;
51                 default:   fputc(*s, fp);
52                 }
53         }
54 }
55
56 /* Some implementations of basename(3) return a pointer to a static
57  * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX).
58  * This is a wrapper function that is compatible with both versions.
59  * The program will error out if basename(3) failed, this can only happen
60  * with the OpenBSD version. */
61 char *
62 xbasename(const char *path)
63 {
64         char *p, *b;
65
66         if (!(p = strdup(path)))
67                 err(1, "strdup");
68         if (!(b = basename(p)))
69                 err(1, "basename");
70         if (!(b = strdup(b)))
71                 err(1, "strdup");
72         free(p);
73
74         return b;
75 }
76
77 void
78 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
79 {
80         struct tm *intm;
81         time_t t;
82         char out[32];
83
84         t = (time_t) intime->time + (intime->offset * 60);
85         intm = gmtime(&t);
86         strftime(out, sizeof(out), fmt, intm);
87         fputs(out, fp);
88 }
89
90 void
91 printtimeshort(FILE *fp, const git_time *intime)
92 {
93         printtimeformat(fp, intime, "%Y-%m-%d %H:%M");
94 }
95
96 int
97 writeheader(FILE *fp)
98 {
99         fputs("<!DOCTYPE HTML>"
100                 "<html dir=\"ltr\" lang=\"en\">\n<head>\n"
101                 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
102                 "<meta http-equiv=\"Content-Language\" content=\"en\" />\n<title>", fp);
103         xmlencode(fp, description, strlen(description));
104         fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
105         fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
106         fputs("</head>\n<body>\n", fp);
107         fprintf(fp, "<table>\n<tr><td><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></td>\n"
108                 "<td><h1>%s</h1><span class=\"desc\">%s</span></td></tr><tr><td></td><td>\n",
109                 relpath, name, description);
110         fputs("</td></tr>\n</table>\n<hr/>\n<div id=\"content\">\n"
111               "<table id=\"index\"><thead>\n"
112               "<tr><td>Name</td><td>Description</td><td>Owner</td><td>Last commit</td></tr>"
113               "</thead><tbody>\n", fp);
114
115         return 0;
116 }
117
118 int
119 writefooter(FILE *fp)
120 {
121         return !fputs("</tbody>\n</table>\n</div>\n</body>\n</html>\n", fp);
122 }
123
124 int
125 writelog(FILE *fp)
126 {
127         char *stripped_name = NULL, *p;
128         git_commit *commit = NULL;
129         const git_signature *author;
130         git_revwalk *w = NULL;
131         git_oid id;
132         int ret = 0;
133
134         git_revwalk_new(&w, repo);
135         git_revwalk_push_head(w);
136         git_revwalk_sorting(w, GIT_SORT_TIME);
137         git_revwalk_simplify_first_parent(w);
138
139         if (git_revwalk_next(&id, w) ||
140             git_commit_lookup(&commit, repo, &id)) {
141                 ret = -1;
142                 goto err;
143         }
144
145         author = git_commit_author(commit);
146
147         /* strip .git suffix */
148         if (!(stripped_name = strdup(name)))
149                 err(1, "strdup");
150         if ((p = strrchr(stripped_name, '.')))
151                 if (!strcmp(p, ".git"))
152                         *p = '\0';
153
154         fputs("<tr><td><a href=\"", fp);
155         xmlencode(fp, stripped_name, strlen(stripped_name));
156         fputs("/log.html\">", fp);
157         xmlencode(fp, stripped_name, strlen(stripped_name));
158         fputs("</a></td><td>", fp);
159         xmlencode(fp, description, strlen(description));
160         fputs("</td><td>", fp);
161         xmlencode(fp, owner, strlen(owner));
162         fputs("</td><td>", fp);
163         if (author)
164                 printtimeshort(fp, &(author->when));
165         fputs("</td></tr>", fp);
166
167         git_commit_free(commit);
168 err:
169         git_revwalk_free(w);
170         free(stripped_name);
171
172         return ret;
173 }
174
175 int
176 main(int argc, char *argv[])
177 {
178         const git_error *e = NULL;
179         FILE *fp;
180         char path[PATH_MAX], *p;
181         int i, r, ret = 0;
182
183         if (argc < 2) {
184                 fprintf(stderr, "%s [repodir...]\n", argv[0]);
185                 return 1;
186         }
187         git_libgit2_init();
188
189         writeheader(stdout);
190
191         for (i = 1; i < argc; i++) {
192                 repodir = argv[i];
193
194                 if (git_repository_open_ext(&repo, repodir,
195                     GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
196                         e = giterr_last();
197                         fprintf(stderr, "%s: %s\n", argv[0], e->message);
198                         ret = 1;
199                         continue;
200                 }
201
202                 /* use directory name as name, truncation of name is no problem. */
203                 p = xbasename(repodir);
204                 snprintf(name, sizeof(name), "%s", p);
205                 free(p);
206
207                 /* read description or .git/description */
208                 description[0] = '\0';
209                 r = snprintf(path, sizeof(path), "%s%s%s",
210                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
211                 if (r == -1 || (size_t)r >= sizeof(path))
212                         errx(1, "path truncated: '%s%s%s'",
213                                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description");
214                 if (!(fp = fopen(path, "r"))) {
215                         r = snprintf(path, sizeof(path), "%s%s%s",
216                                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
217                         if (r == -1 || (size_t)r >= sizeof(path))
218                                 errx(1, "path truncated: '%s%s%s'",
219                                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description");
220                         fp = fopen(path, "r");
221                 }
222                 if (fp) {
223                         if (!fgets(description, sizeof(description), fp))
224                                 description[0] = '\0';
225                         fclose(fp);
226                 }
227
228                 /* read owner or .git/owner */
229                 owner[0] = '\0';
230                 r = snprintf(path, sizeof(path), "%s%s%s",
231                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "owner");
232                 if (r == -1 || (size_t)r >= sizeof(path))
233                         errx(1, "path truncated: '%s%s%s'",
234                                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "owner");
235                 if (!(fp = fopen(path, "r"))) {
236                         r = snprintf(path, sizeof(path), "%s%s%s",
237                                 repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/owner");
238                         if (r == -1 || (size_t)r >= sizeof(path))
239                                 errx(1, "path truncated: '%s%s%s'",
240                                         repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/owner");
241                         fp = fopen(path, "r");
242                 }
243                 if (fp) {
244                         if (!fgets(owner, sizeof(owner), fp))
245                                 owner[0] = '\0';
246                         owner[strcspn(owner, "\n")] = '\0';
247                         fclose(fp);
248                 }
249                 writelog(stdout);
250         }
251         writefooter(stdout);
252
253         /* cleanup */
254         git_repository_free(repo);
255         git_libgit2_shutdown();
256
257         return ret;
258 }