]> git.cameronkatri.com Git - cgit.git/blob - filter.c
filter: don't forget to reap the auth filter
[cgit.git] / filter.c
1 /* filter.c: filter framework functions
2 *
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9 #include "cgit.h"
10 #include "html.h"
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <dlfcn.h>
17 #include <errno.h>
18 #ifndef NO_LUA
19 #include <lua.h>
20 #include <lualib.h>
21 #include <lauxlib.h>
22 #endif
23
24 static ssize_t (*libc_write)(int fd, const void *buf, size_t count);
25 static ssize_t (*filter_write)(struct cgit_filter *base, const void *buf, size_t count) = NULL;
26 static struct cgit_filter *current_write_filter = NULL;
27
28 static inline void reap_filter(struct cgit_filter *filter)
29 {
30 if (filter && filter->cleanup)
31 filter->cleanup(filter);
32 }
33
34 void cgit_cleanup_filters(void)
35 {
36 int i;
37 reap_filter(ctx.cfg.about_filter);
38 reap_filter(ctx.cfg.commit_filter);
39 reap_filter(ctx.cfg.source_filter);
40 reap_filter(ctx.cfg.email_filter);
41 reap_filter(ctx.cfg.auth_filter);
42 for (i = 0; i < cgit_repolist.count; ++i) {
43 reap_filter(cgit_repolist.repos[i].about_filter);
44 reap_filter(cgit_repolist.repos[i].commit_filter);
45 reap_filter(cgit_repolist.repos[i].source_filter);
46 reap_filter(cgit_repolist.repos[i].email_filter);
47 }
48 }
49
50 void cgit_init_filters(void)
51 {
52 libc_write = dlsym(RTLD_NEXT, "write");
53 if (!libc_write)
54 die("Could not locate libc's write function");
55 }
56
57 ssize_t write(int fd, const void *buf, size_t count)
58 {
59 if (fd != STDOUT_FILENO || !filter_write)
60 return libc_write(fd, buf, count);
61 return filter_write(current_write_filter, buf, count);
62 }
63
64 static inline void hook_write(struct cgit_filter *filter, ssize_t (*new_write)(struct cgit_filter *base, const void *buf, size_t count))
65 {
66 /* We want to avoid buggy nested patterns. */
67 assert(filter_write == NULL);
68 assert(current_write_filter == NULL);
69 current_write_filter = filter;
70 filter_write = new_write;
71 }
72
73 static inline void unhook_write()
74 {
75 assert(filter_write != NULL);
76 assert(current_write_filter != NULL);
77 filter_write = NULL;
78 current_write_filter = NULL;
79 }
80
81 static int open_exec_filter(struct cgit_filter *base, va_list ap)
82 {
83 struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
84 int i;
85
86 for (i = 0; i < filter->base.argument_count; i++)
87 filter->argv[i + 1] = va_arg(ap, char *);
88
89 filter->old_stdout = chk_positive(dup(STDOUT_FILENO),
90 "Unable to duplicate STDOUT");
91 chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess");
92 filter->pid = chk_non_negative(fork(), "Unable to create subprocess");
93 if (filter->pid == 0) {
94 close(filter->pipe_fh[1]);
95 chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO),
96 "Unable to use pipe as STDIN");
97 execvp(filter->cmd, filter->argv);
98 die_errno("Unable to exec subprocess %s", filter->cmd);
99 }
100 close(filter->pipe_fh[0]);
101 chk_non_negative(dup2(filter->pipe_fh[1], STDOUT_FILENO),
102 "Unable to use pipe as STDOUT");
103 close(filter->pipe_fh[1]);
104 return 0;
105 }
106
107 static int close_exec_filter(struct cgit_filter *base)
108 {
109 struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
110 int i, exit_status = 0;
111
112 chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO),
113 "Unable to restore STDOUT");
114 close(filter->old_stdout);
115 if (filter->pid < 0)
116 goto done;
117 waitpid(filter->pid, &exit_status, 0);
118 if (WIFEXITED(exit_status))
119 goto done;
120 die("Subprocess %s exited abnormally", filter->cmd);
121
122 done:
123 for (i = 0; i < filter->base.argument_count; i++)
124 filter->argv[i + 1] = NULL;
125 return WEXITSTATUS(exit_status);
126
127 }
128
129 static void fprintf_exec_filter(struct cgit_filter *base, FILE *f, const char *prefix)
130 {
131 struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
132 fprintf(f, "%sexec:%s\n", prefix, filter->cmd);
133 }
134
135 static void cleanup_exec_filter(struct cgit_filter *base)
136 {
137 struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
138 if (filter->argv) {
139 free(filter->argv);
140 filter->argv = NULL;
141 }
142 if (filter->cmd) {
143 free(filter->cmd);
144 filter->cmd = NULL;
145 }
146 }
147
148 static struct cgit_filter *new_exec_filter(const char *cmd, int argument_count)
149 {
150 struct cgit_exec_filter *f;
151 int args_size = 0;
152
153 f = xmalloc(sizeof(*f));
154 /* We leave argv for now and assign it below. */
155 cgit_exec_filter_init(f, xstrdup(cmd), NULL);
156 f->base.argument_count = argument_count;
157 args_size = (2 + argument_count) * sizeof(char *);
158 f->argv = xmalloc(args_size);
159 memset(f->argv, 0, args_size);
160 f->argv[0] = f->cmd;
161 return &f->base;
162 }
163
164 void cgit_exec_filter_init(struct cgit_exec_filter *filter, char *cmd, char **argv)
165 {
166 memset(filter, 0, sizeof(*filter));
167 filter->base.open = open_exec_filter;
168 filter->base.close = close_exec_filter;
169 filter->base.fprintf = fprintf_exec_filter;
170 filter->base.cleanup = cleanup_exec_filter;
171 filter->cmd = cmd;
172 filter->argv = argv;
173 /* The argument count for open_filter is zero by default, unless called from new_filter, above. */
174 filter->base.argument_count = 0;
175 }
176
177 #ifndef NO_LUA
178 struct lua_filter {
179 struct cgit_filter base;
180 char *script_file;
181 lua_State *lua_state;
182 };
183
184 static void error_lua_filter(struct lua_filter *filter)
185 {
186 die("Lua error in %s: %s", filter->script_file, lua_tostring(filter->lua_state, -1));
187 lua_pop(filter->lua_state, 1);
188 }
189
190 static ssize_t write_lua_filter(struct cgit_filter *base, const void *buf, size_t count)
191 {
192 struct lua_filter *filter = (struct lua_filter *)base;
193
194 lua_getglobal(filter->lua_state, "filter_write");
195 lua_pushlstring(filter->lua_state, buf, count);
196 if (lua_pcall(filter->lua_state, 1, 0, 0)) {
197 error_lua_filter(filter);
198 errno = EIO;
199 return -1;
200 }
201 return count;
202 }
203
204 static inline int hook_lua_filter(lua_State *lua_state, void (*fn)(const char *txt))
205 {
206 const char *str;
207 ssize_t (*save_filter_write)(struct cgit_filter *base, const void *buf, size_t count);
208 struct cgit_filter *save_filter;
209
210 str = lua_tostring(lua_state, 1);
211 if (!str)
212 return 0;
213
214 save_filter_write = filter_write;
215 save_filter = current_write_filter;
216 unhook_write();
217 fn(str);
218 hook_write(save_filter, save_filter_write);
219
220 return 0;
221 }
222
223 static int html_lua_filter(lua_State *lua_state)
224 {
225 return hook_lua_filter(lua_state, html);
226 }
227
228 static int html_txt_lua_filter(lua_State *lua_state)
229 {
230 return hook_lua_filter(lua_state, html_txt);
231 }
232
233 static int html_attr_lua_filter(lua_State *lua_state)
234 {
235 return hook_lua_filter(lua_state, html_attr);
236 }
237
238 static int html_url_path_lua_filter(lua_State *lua_state)
239 {
240 return hook_lua_filter(lua_state, html_url_path);
241 }
242
243 static int html_url_arg_lua_filter(lua_State *lua_state)
244 {
245 return hook_lua_filter(lua_state, html_url_arg);
246 }
247
248 static int html_include_lua_filter(lua_State *lua_state)
249 {
250 return hook_lua_filter(lua_state, (void (*)(const char *))html_include);
251 }
252
253 static void cleanup_lua_filter(struct cgit_filter *base)
254 {
255 struct lua_filter *filter = (struct lua_filter *)base;
256
257 if (!filter->lua_state)
258 return;
259
260 lua_close(filter->lua_state);
261 filter->lua_state = NULL;
262 if (filter->script_file) {
263 free(filter->script_file);
264 filter->script_file = NULL;
265 }
266 }
267
268 static int init_lua_filter(struct lua_filter *filter)
269 {
270 if (filter->lua_state)
271 return 0;
272
273 if (!(filter->lua_state = luaL_newstate()))
274 return 1;
275
276 luaL_openlibs(filter->lua_state);
277
278 lua_pushcfunction(filter->lua_state, html_lua_filter);
279 lua_setglobal(filter->lua_state, "html");
280 lua_pushcfunction(filter->lua_state, html_txt_lua_filter);
281 lua_setglobal(filter->lua_state, "html_txt");
282 lua_pushcfunction(filter->lua_state, html_attr_lua_filter);
283 lua_setglobal(filter->lua_state, "html_attr");
284 lua_pushcfunction(filter->lua_state, html_url_path_lua_filter);
285 lua_setglobal(filter->lua_state, "html_url_path");
286 lua_pushcfunction(filter->lua_state, html_url_arg_lua_filter);
287 lua_setglobal(filter->lua_state, "html_url_arg");
288 lua_pushcfunction(filter->lua_state, html_include_lua_filter);
289 lua_setglobal(filter->lua_state, "html_include");
290
291 if (luaL_dofile(filter->lua_state, filter->script_file)) {
292 error_lua_filter(filter);
293 lua_close(filter->lua_state);
294 filter->lua_state = NULL;
295 return 1;
296 }
297 return 0;
298 }
299
300 static int open_lua_filter(struct cgit_filter *base, va_list ap)
301 {
302 struct lua_filter *filter = (struct lua_filter *)base;
303 int i;
304
305 if (init_lua_filter(filter))
306 return 1;
307
308 hook_write(base, write_lua_filter);
309
310 lua_getglobal(filter->lua_state, "filter_open");
311 for (i = 0; i < filter->base.argument_count; ++i)
312 lua_pushstring(filter->lua_state, va_arg(ap, char *));
313 if (lua_pcall(filter->lua_state, filter->base.argument_count, 0, 0)) {
314 error_lua_filter(filter);
315 return 1;
316 }
317 return 0;
318 }
319
320 static int close_lua_filter(struct cgit_filter *base)
321 {
322 struct lua_filter *filter = (struct lua_filter *)base;
323 int ret = 0;
324
325 lua_getglobal(filter->lua_state, "filter_close");
326 if (lua_pcall(filter->lua_state, 0, 1, 0)) {
327 error_lua_filter(filter);
328 ret = -1;
329 } else {
330 ret = lua_tonumber(filter->lua_state, -1);
331 lua_pop(filter->lua_state, 1);
332 }
333
334 unhook_write();
335 return ret;
336 }
337
338 static void fprintf_lua_filter(struct cgit_filter *base, FILE *f, const char *prefix)
339 {
340 struct lua_filter *filter = (struct lua_filter *)base;
341 fprintf(f, "%slua:%s\n", prefix, filter->script_file);
342 }
343
344
345 static struct cgit_filter *new_lua_filter(const char *cmd, int argument_count)
346 {
347 struct lua_filter *filter;
348
349 filter = xmalloc(sizeof(*filter));
350 memset(filter, 0, sizeof(*filter));
351 filter->base.open = open_lua_filter;
352 filter->base.close = close_lua_filter;
353 filter->base.fprintf = fprintf_lua_filter;
354 filter->base.cleanup = cleanup_lua_filter;
355 filter->base.argument_count = argument_count;
356 filter->script_file = xstrdup(cmd);
357
358 return &filter->base;
359 }
360
361 #endif
362
363
364 int cgit_open_filter(struct cgit_filter *filter, ...)
365 {
366 int result;
367 va_list ap;
368 if (!filter)
369 return 0;
370 va_start(ap, filter);
371 result = filter->open(filter, ap);
372 va_end(ap);
373 return result;
374 }
375
376 int cgit_close_filter(struct cgit_filter *filter)
377 {
378 if (!filter)
379 return 0;
380 return filter->close(filter);
381 }
382
383 void cgit_fprintf_filter(struct cgit_filter *filter, FILE *f, const char *prefix)
384 {
385 filter->fprintf(filter, f, prefix);
386 }
387
388
389
390 static const struct {
391 const char *prefix;
392 struct cgit_filter *(*ctor)(const char *cmd, int argument_count);
393 } filter_specs[] = {
394 { "exec", new_exec_filter },
395 #ifndef NO_LUA
396 { "lua", new_lua_filter },
397 #endif
398 };
399
400 struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype)
401 {
402 char *colon;
403 int i;
404 size_t len;
405 int argument_count;
406
407 if (!cmd || !cmd[0])
408 return NULL;
409
410 colon = strchr(cmd, ':');
411 len = colon - cmd;
412 /*
413 * In case we're running on Windows, don't allow a single letter before
414 * the colon.
415 */
416 if (len == 1)
417 colon = NULL;
418
419 switch (filtertype) {
420 case AUTH:
421 argument_count = 12;
422 break;
423
424 case EMAIL:
425 argument_count = 2;
426 break;
427
428 case SOURCE:
429 case ABOUT:
430 argument_count = 1;
431 break;
432
433 case COMMIT:
434 default:
435 argument_count = 0;
436 break;
437 }
438
439 /* If no prefix is given, exec filter is the default. */
440 if (!colon)
441 return new_exec_filter(cmd, argument_count);
442
443 for (i = 0; i < ARRAY_SIZE(filter_specs); i++) {
444 if (len == strlen(filter_specs[i].prefix) &&
445 !strncmp(filter_specs[i].prefix, cmd, len))
446 return filter_specs[i].ctor(colon + 1, argument_count);
447 }
448
449 die("Invalid filter type: %.*s", (int) len, cmd);
450 }