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