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