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