]> git.cameronkatri.com Git - cgit.git/blob - ui-snapshot.c
ui-snapshot: do not access $HOME
[cgit.git] / ui-snapshot.c
1 /* ui-snapshot.c: generate snapshot of a commit
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 * Copyright (C) 2012 Jason A. Donenfeld <Jason@zx2c4.com>
5 *
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
8 */
9
10 #include "cgit.h"
11 #include "ui-snapshot.h"
12 #include "html.h"
13 #include "ui-shared.h"
14
15 static int write_archive_type(const char *format, const char *hex, const char *prefix)
16 {
17 struct argv_array argv = ARGV_ARRAY_INIT;
18 const char **nargv;
19 char *user_home, *xdg_home;
20 int result;
21 argv_array_push(&argv, "snapshot");
22 argv_array_push(&argv, format);
23 if (prefix) {
24 struct strbuf buf = STRBUF_INIT;
25 strbuf_addstr(&buf, prefix);
26 strbuf_addch(&buf, '/');
27 argv_array_push(&argv, "--prefix");
28 argv_array_push(&argv, buf.buf);
29 strbuf_release(&buf);
30 }
31 argv_array_push(&argv, hex);
32 /*
33 * Now we need to copy the pointers to arguments into a new
34 * structure because write_archive will rearrange its arguments
35 * which may result in duplicated/missing entries causing leaks
36 * or double-frees in argv_array_clear.
37 */
38 nargv = xmalloc(sizeof(char *) * (argv.argc + 1));
39 /* argv_array guarantees a trailing NULL entry. */
40 memcpy(nargv, argv.argv, sizeof(char *) * (argv.argc + 1));
41
42 user_home = getenv("HOME");
43 xdg_home = getenv("XDG_CONFIG_HOME");
44 unsetenv("HOME");
45 unsetenv("XDG_CONFIG_HOME");
46 result = write_archive(argv.argc, nargv, NULL, 1, NULL, 0);
47 if (user_home)
48 setenv("HOME", user_home, 1);
49 if (xdg_home)
50 setenv("XDG_CONFIG_HOME", xdg_home, 1);
51 argv_array_clear(&argv);
52 free(nargv);
53 return result;
54 }
55
56 static int write_tar_archive(const char *hex, const char *prefix)
57 {
58 return write_archive_type("--format=tar", hex, prefix);
59 }
60
61 static int write_zip_archive(const char *hex, const char *prefix)
62 {
63 return write_archive_type("--format=zip", hex, prefix);
64 }
65
66 static int write_compressed_tar_archive(const char *hex,
67 const char *prefix,
68 char *filter_argv[])
69 {
70 int rv;
71 struct cgit_filter f;
72
73 f.cmd = filter_argv[0];
74 f.argv = filter_argv;
75 cgit_open_filter(&f);
76 rv = write_tar_archive(hex, prefix);
77 cgit_close_filter(&f);
78 return rv;
79 }
80
81 static int write_tar_gzip_archive(const char *hex, const char *prefix)
82 {
83 char *argv[] = { "gzip", "-n", NULL };
84 return write_compressed_tar_archive(hex, prefix, argv);
85 }
86
87 static int write_tar_bzip2_archive(const char *hex, const char *prefix)
88 {
89 char *argv[] = { "bzip2", NULL };
90 return write_compressed_tar_archive(hex, prefix, argv);
91 }
92
93 static int write_tar_xz_archive(const char *hex, const char *prefix)
94 {
95 char *argv[] = { "xz", NULL };
96 return write_compressed_tar_archive(hex, prefix, argv);
97 }
98
99 const struct cgit_snapshot_format cgit_snapshot_formats[] = {
100 { ".zip", "application/x-zip", write_zip_archive, 0x01 },
101 { ".tar.gz", "application/x-gzip", write_tar_gzip_archive, 0x02 },
102 { ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive, 0x04 },
103 { ".tar", "application/x-tar", write_tar_archive, 0x08 },
104 { ".tar.xz", "application/x-xz", write_tar_xz_archive, 0x10 },
105 { NULL }
106 };
107
108 static const struct cgit_snapshot_format *get_format(const char *filename)
109 {
110 const struct cgit_snapshot_format *fmt;
111 int fl, sl;
112
113 fl = strlen(filename);
114 for (fmt = cgit_snapshot_formats; fmt->suffix; fmt++) {
115 sl = strlen(fmt->suffix);
116 if (sl >= fl)
117 continue;
118 if (!strcmp(fmt->suffix, filename + fl - sl))
119 return fmt;
120 }
121 return NULL;
122 }
123
124 static int make_snapshot(const struct cgit_snapshot_format *format,
125 const char *hex, const char *prefix,
126 const char *filename)
127 {
128 unsigned char sha1[20];
129
130 if (get_sha1(hex, sha1)) {
131 cgit_print_error("Bad object id: %s", hex);
132 return 1;
133 }
134 if (!lookup_commit_reference(sha1)) {
135 cgit_print_error("Not a commit reference: %s", hex);
136 return 1;
137 }
138 ctx.page.mimetype = xstrdup(format->mimetype);
139 ctx.page.filename = xstrdup(filename);
140 cgit_print_http_headers(&ctx);
141 format->write_func(hex, prefix);
142 return 0;
143 }
144
145 /* Try to guess the requested revision from the requested snapshot name.
146 * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
147 * "cgit-0.7.2". If this is a valid commit object name we've got a winner.
148 * Otherwise, if the snapshot name has a prefix matching the result from
149 * repo_basename(), we strip the basename and any following '-' and '_'
150 * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
151 * more. If this still isn't a valid commit object name, we check if pre-
152 * pending a 'v' or a 'V' to the remaining snapshot name ("0.7.2" ->
153 * "v0.7.2") gives us something valid.
154 */
155 static const char *get_ref_from_filename(const char *url, const char *filename,
156 const struct cgit_snapshot_format *format)
157 {
158 const char *reponame;
159 unsigned char sha1[20];
160 struct strbuf snapshot = STRBUF_INIT;
161 int result = 1;
162
163 strbuf_addstr(&snapshot, filename);
164 strbuf_setlen(&snapshot, snapshot.len - strlen(format->suffix));
165
166 if (get_sha1(snapshot.buf, sha1) == 0)
167 goto out;
168
169 reponame = cgit_repobasename(url);
170 if (prefixcmp(snapshot.buf, reponame) == 0) {
171 const char *new_start = snapshot.buf;
172 new_start += strlen(reponame);
173 while (new_start && (*new_start == '-' || *new_start == '_'))
174 new_start++;
175 strbuf_splice(&snapshot, 0, new_start - snapshot.buf, "", 0);
176 }
177
178 if (get_sha1(snapshot.buf, sha1) == 0)
179 goto out;
180
181 strbuf_insert(&snapshot, 0, "v", 1);
182 if (get_sha1(snapshot.buf, sha1) == 0)
183 goto out;
184
185 strbuf_splice(&snapshot, 0, 1, "V", 1);
186 if (get_sha1(snapshot.buf, sha1) == 0)
187 goto out;
188
189 result = 0;
190 strbuf_release(&snapshot);
191
192 out:
193 return result ? strbuf_detach(&snapshot, NULL) : NULL;
194 }
195
196 __attribute__((format (printf, 1, 2)))
197 static void show_error(char *fmt, ...)
198 {
199 va_list ap;
200
201 ctx.page.mimetype = "text/html";
202 cgit_print_http_headers(&ctx);
203 cgit_print_docstart(&ctx);
204 cgit_print_pageheader(&ctx);
205 va_start(ap, fmt);
206 cgit_vprint_error(fmt, ap);
207 va_end(ap);
208 cgit_print_docend();
209 }
210
211 void cgit_print_snapshot(const char *head, const char *hex,
212 const char *filename, int snapshots, int dwim)
213 {
214 const struct cgit_snapshot_format* f;
215 char *prefix = NULL;
216
217 if (!filename) {
218 show_error("No snapshot name specified");
219 return;
220 }
221
222 f = get_format(filename);
223 if (!f) {
224 show_error("Unsupported snapshot format: %s", filename);
225 return;
226 }
227
228 if (!hex && dwim) {
229 hex = get_ref_from_filename(ctx.repo->url, filename, f);
230 if (hex == NULL) {
231 html_status(404, "Not found", 0);
232 return;
233 }
234 prefix = xstrdup(filename);
235 prefix[strlen(filename) - strlen(f->suffix)] = '\0';
236 }
237
238 if (!hex)
239 hex = head;
240
241 if (!prefix)
242 prefix = xstrdup(cgit_repobasename(ctx.repo->url));
243
244 make_snapshot(f, hex, prefix, filename);
245 free(prefix);
246 }