chiark / gitweb /
23d102055a7ec0c75fca6c3a1fd5aebbaf2e9d20
[disorder] / server / cgi.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004-2008 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20
21 #include <config.h>
22 #include "types.h"
23
24 #include <string.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30 #include <stddef.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <pcre.h>
34 #include <limits.h>
35 #include <fnmatch.h>
36 #include <ctype.h>
37
38 #include "mem.h"
39 #include "log.h"
40 #include "hex.h"
41 #include "charset.h"
42 #include "configuration.h"
43 #include "table.h"
44 #include "syscalls.h"
45 #include "kvp.h"
46 #include "vector.h"
47 #include "split.h"
48 #include "inputline.h"
49 #include "regsub.h"
50 #include "defs.h"
51 #include "sink.h"
52 #include "cgi.h"
53 #include "printf.h"
54 #include "mime.h"
55 #include "unicode.h"
56
57 struct kvp *cgi_args;
58
59 /* options */
60 struct column {
61   struct column *next;
62   char *name;
63   int ncolumns;
64   char **columns;
65 };
66
67 #define RELIST(x) struct re *x, **x##_tail = &x
68
69 static int have_read_options;
70 static struct kvp *labels;
71 static struct column *columns;
72
73 static void include_options(const char *name);
74
75 static void cgi_parse_get(void) {
76   const char *q;
77
78   if(!(q = getenv("QUERY_STRING"))) fatal(0, "QUERY_STRING not set");
79   cgi_args = kvp_urldecode(q, strlen(q));
80 }
81
82 static void cgi_input(char **ptrp, size_t *np) {
83   const char *cl;
84   char *q;
85   size_t n, m = 0;
86   int r;
87
88   if(!(cl = getenv("CONTENT_LENGTH"))) fatal(0, "CONTENT_LENGTH not set");
89   n = atol(cl);
90   q = xmalloc_noptr(n + 1);
91   while(m < n) {
92     r = read(0, q + m, n - m);
93     if(r > 0)
94       m += r;
95     else if(r == 0)
96       fatal(0, "unexpected end of file reading request body");
97     else switch(errno) {
98     case EINTR: break;
99     default: fatal(errno, "error reading request body");
100     }
101   }
102   if(memchr(q, 0, n)) fatal(0, "null character in request body");
103   q[n + 1] = 0;
104   *ptrp = q;
105   if(np) *np = n;
106 }
107
108 static int cgi_field_callback(const char *name, const char *value,
109                               void *u) {
110   char *disposition, *pname, *pvalue;
111   char **namep = u;
112
113   if(!strcmp(name, "content-disposition")) {
114     if(mime_rfc2388_content_disposition(value,
115                                         &disposition,
116                                         &pname,
117                                         &pvalue))
118       fatal(0, "error parsing Content-Disposition field");
119     if(!strcmp(disposition, "form-data")
120        && pname
121        && !strcmp(pname, "name")) {
122       if(*namep)
123         fatal(0, "duplicate Content-Disposition field");
124       *namep = pvalue;
125     }
126   }
127   return 0;
128 }
129
130 static int cgi_part_callback(const char *s,
131                              void attribute((unused)) *u) {
132   char *name = 0;
133   struct kvp *k;
134   
135   if(!(s = mime_parse(s, cgi_field_callback, &name)))
136     fatal(0, "error parsing part header");
137   if(!name) fatal(0, "no name found");
138   k = xmalloc(sizeof *k);
139   k->next = cgi_args;
140   k->name = name;
141   k->value = s;
142   cgi_args = k;
143   return 0;
144 }
145
146 static void cgi_parse_multipart(const char *boundary) {
147   char *q;
148   
149   cgi_input(&q, 0);
150   if(mime_multipart(q, cgi_part_callback, boundary, 0))
151     fatal(0, "invalid multipart object");
152 }
153
154 static void cgi_parse_post(void) {
155   const char *ct, *boundary;
156   char *q, *type;
157   size_t n;
158   struct kvp *k;
159
160   if(!(ct = getenv("CONTENT_TYPE")))
161     ct = "application/x-www-form-urlencoded";
162   if(mime_content_type(ct, &type, &k))
163     fatal(0, "invalid content type '%s'", ct);
164   if(!strcmp(type, "application/x-www-form-urlencoded")) {
165     cgi_input(&q, &n);
166     cgi_args = kvp_urldecode(q, n);
167     return;
168   }
169   if(!strcmp(type, "multipart/form-data")) {
170     if(!(boundary = kvp_get(k, "boundary")))
171       fatal(0, "no boundary parameter found");
172     cgi_parse_multipart(boundary);
173     return;
174   }
175   fatal(0, "unrecognized content type '%s'", type);
176 }
177
178 void cgi_parse(void) {
179   const char *p;
180   struct kvp *k;
181
182   if(!(p = getenv("REQUEST_METHOD"))) fatal(0, "REQUEST_METHOD not set");
183   if(!strcmp(p, "GET"))
184     cgi_parse_get();
185   else if(!strcmp(p, "POST"))
186     cgi_parse_post();
187   else
188     fatal(0, "unknown request method %s", p);
189   for(k = cgi_args; k; k = k->next)
190     if(!utf8_valid(k->name, strlen(k->name))
191        || !utf8_valid(k->value, strlen(k->value)))
192       fatal(0, "invalid UTF-8 sequence in cgi argument");
193 }
194
195 const char *cgi_get(const char *name) {
196   return kvp_get(cgi_args, name);
197 }
198
199 void cgi_output(cgi_sink *output, const char *fmt, ...) {
200   va_list ap;
201   int n;
202   char *r;
203
204   va_start(ap, fmt);
205   n = byte_vasprintf(&r, fmt, ap);
206   if(n < 0)
207     fatal(errno, "error calling byte_vasprintf");
208   if(output->quote)
209     r = cgi_sgmlquote(r, 0);
210   output->sink->write(output->sink, r, strlen(r));
211   va_end(ap);
212 }
213
214 void cgi_header(struct sink *output, const char *name, const char *value) {
215   sink_printf(output, "%s: %s\r\n", name, value);
216 }
217
218 void cgi_body(struct sink *output) {
219   sink_printf(output, "\r\n");
220 }
221
222 char *cgi_sgmlquote(const char *s, int raw) {
223   uint32_t *ucs, *p, c;
224   char *b, *bp;
225   int n;
226
227   if(!raw) {
228     if(!(ucs = utf8_to_utf32(s, strlen(s), 0))) exit(EXIT_FAILURE);
229   } else {
230     ucs = xmalloc_noptr((strlen(s) + 1) * sizeof(uint32_t));
231     for(n = 0; s[n]; ++n)
232       ucs[n] = (unsigned char)s[n];
233     ucs[n] = 0;
234   }
235
236   n = 1;
237   /* estimate the length we'll need */
238   for(p = ucs; (c = *p); ++p) {
239     switch(c) {
240     default:
241       if(c > 127 || c < 32) {
242       case '"':
243       case '&':
244       case '<':
245       case '>':
246         n += 12;
247         break;
248       } else
249         n++;
250     }
251   }
252   /* format the string */
253   b = bp = xmalloc_noptr(n);
254   for(p = ucs; (c = *p); ++p) {
255     switch(c) {
256     default:
257       if(*p > 127 || *p < 32) {
258       case '"':
259       case '&':
260       case '<':
261       case '>':
262         bp += sprintf(bp, "&#%lu;", (unsigned long)c);
263         break;
264       } else
265         *bp++ = c;
266     }
267   }
268   *bp = 0;
269   return b;
270 }
271
272 void cgi_attr(struct sink *output, const char *name, const char *value) {
273   if(!value[strspn(value, "abcdefghijklmnopqrstuvwxyz"
274                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
275                    "0123456789")])
276     sink_printf(output, "%s=%s", name, value);
277   else
278     sink_printf(output, "%s=\"%s\"", name, cgi_sgmlquote(value, 0));
279 }
280
281 void cgi_opentag(struct sink *output, const char *name, ...) {
282   va_list ap;
283   const char *n, *v;
284    
285   sink_printf(output, "<%s", name);
286   va_start(ap, name);
287   while((n = va_arg(ap, const char *))) {
288     sink_printf(output, " ");
289     v = va_arg(ap, const char *);
290     if(v)
291       cgi_attr(output, n, v);
292     else
293       sink_printf(output, n);
294   }
295   sink_printf(output, ">");
296 }
297
298 void cgi_closetag(struct sink *output, const char *name) {
299   sink_printf(output, "</%s>", name);
300 }
301
302 static int template_open(const char *name,
303                          const char *ext,
304                          const char **filenamep) {
305   const char *dirs[2];
306   int fd = -1, n;
307   char *fullpath;
308
309   dirs[0] = pkgconfdir;
310   dirs[1] = pkgdatadir;
311   if(name[0] == '/') {
312     if((fd = open(name, O_RDONLY)) < 0) fatal(0, "cannot open %s", name);
313     *filenamep = name;
314   } else {
315     for(n = 0; n < config->templates.n + (int)(sizeof dirs / sizeof *dirs); ++n) {
316       byte_xasprintf(&fullpath, "%s/%s%s",
317                      n < config->templates.n ? config->templates.s[n]
318                                              : dirs[n - config->templates.n],
319                      name, ext);
320       if((fd = open(fullpath, O_RDONLY)) >= 0) break;
321     }
322     if(fd < 0) error(0, "cannot find %s%s in template path", name, ext);
323     *filenamep = fullpath;
324   }
325   return fd;
326 }
327
328 static int valid_template_name(const char *name) {
329   if(strchr(name, '/') || name[0] == '.')
330     return 0;
331   return 1;
332 }
333
334 void cgi_expand(const char *template,
335                 const struct cgi_expansion *expansions,
336                 size_t nexpansions,
337                 cgi_sink *output,
338                 void *u) {
339   int fd = -1;
340   int n;
341   off_t m;
342   char *b;
343   struct stat sb;
344
345   if(!valid_template_name(template))
346     fatal(0, "invalid template name '%s'", template);
347   if((fd = template_open(template, ".html", &template)) < 0)
348     exitfn(EXIT_FAILURE);
349   if(fstat(fd, &sb) < 0) fatal(errno, "cannot stat %s", template);
350   m = 0;
351   b = xmalloc_noptr(sb.st_size + 1);
352   while(m < sb.st_size) {
353     n = read(fd, b + m, sb.st_size - m);
354     if(n > 0) m += n;
355     else if(n == 0) fatal(0, "unexpected EOF reading %s", template);
356     else if(errno != EINTR) fatal(errno, "error reading %s", template);
357   }
358   b[sb.st_size] = 0;
359   xclose(fd);
360   cgi_expand_string(template, b, expansions, nexpansions, output, u);
361 }
362
363 void cgi_expand_string(const char *name,
364                        const char *template,
365                        const struct cgi_expansion *expansions,
366                        size_t nexpansions,
367                        cgi_sink *output,
368                        void *u) {
369   int braces, n, m, line = 1, sline;
370   char *argname;
371   const char *p;
372   struct vector v;
373   struct dynstr d;
374   cgi_sink parameter_output;
375   
376   while(*template) {
377     if(*template != '@') {
378       p = template;
379       while(*p && *p != '@') {
380         if(*p == '\n') ++line;
381         ++p;
382       }
383       output->sink->write(output->sink, template, p - template);
384       template = p;
385       continue;
386     }
387     vector_init(&v);
388     braces = 0;
389     ++template;
390     sline = line;
391     while(*template != '@') {
392       /* Skip whitespace */
393       while(isspace((unsigned char)*template))
394         ++template;
395       dynstr_init(&d);
396       if(*template == '{') {
397         /* bracketed arg */
398         ++template;
399         while(*template && (*template != '}' || braces > 0)) {
400           switch(*template) {
401           case '{': ++braces; break;
402           case '}': --braces; break;
403           case '\n': ++line; break;
404           }
405           dynstr_append(&d, *template++);
406         }
407         if(!*template) fatal(0, "%s:%d: unterminated expansion", name, sline);
408         ++template;
409         if(isspace((unsigned char)*template)) {
410           /* We have @{...}<WHITESPACE><SOMETHING> */
411           for(p = template; isspace((unsigned char)*p); ++p)
412             ;
413           /* Now we are looking at <SOMETHING>.  If it's "{" then that
414            * must be the next argument.  Otherwise we infer that this
415            * is really the end of the expansion. */
416           if(*p != '{')
417             goto finished_expansion;
418         }
419       } else {
420         /* unbracketed arg */
421         while(*template
422               && *template != '@' && *template != '{' && *template != ':') {
423           if(*template == '\n') ++line;
424           dynstr_append(&d, *template++);
425         }
426         if(*template == ':')
427           ++template;
428         if(!*template) fatal(0, "%s:%d: unterminated expansion", name, sline);
429         /* trailing whitespace is not significant in unquoted args */
430         while(d.nvec && (isspace((unsigned char)d.vec[d.nvec - 1])))
431           --d.nvec;
432       }
433       dynstr_terminate(&d);
434       vector_append(&v, d.vec);
435     }
436     ++template;
437 finished_expansion:
438     vector_terminate(&v);
439     /* @@ terminates this file */
440     if(v.nvec == 0)
441       break;
442     if((n = table_find(expansions,
443                        offsetof(struct cgi_expansion, name),
444                        sizeof (struct cgi_expansion),
445                        nexpansions,
446                        v.vec[0])) < 0)
447       fatal(0, "%s:%d: unknown expansion '%s'", name, line, v.vec[0]);
448     if(v.nvec - 1 < expansions[n].minargs)
449       fatal(0, "%s:%d: insufficient arguments to @%s@ (min %d, got %d)",
450             name, line, v.vec[0], expansions[n].minargs, v.nvec - 1);
451     if(v.nvec - 1 > expansions[n].maxargs)
452       fatal(0, "%s:%d: too many arguments to @%s@ (max %d, got %d)",
453             name, line, v.vec[0], expansions[n].maxargs, v.nvec - 1);
454     /* for ordinary expansions, recursively expand the arguments */
455     if(!(expansions[n].flags & EXP_MAGIC)) {
456       for(m = 1; m < v.nvec; ++m) {
457         dynstr_init(&d);
458         byte_xasprintf(&argname, "<%s:%d arg #%d>", name, sline, m);
459         parameter_output.quote = 0;
460         parameter_output.sink = sink_dynstr(&d);
461         cgi_expand_string(argname, v.vec[m],
462                           expansions, nexpansions,
463                           &parameter_output, u);
464         dynstr_terminate(&d);
465         v.vec[m] = d.vec;
466       }
467     }
468     expansions[n].handler(v.nvec - 1, v.vec + 1, output, u);
469   }
470 }
471
472 char *cgi_makeurl(const char *url, ...) {
473   va_list ap;
474   struct kvp *kvp, *k, **kk = &kvp;
475   struct dynstr d;
476   const char *n, *v;
477   
478   dynstr_init(&d);
479   dynstr_append_string(&d, url);
480   va_start(ap, url);
481   while((n = va_arg(ap, const char *))) {
482     v = va_arg(ap, const char *);
483     *kk = k = xmalloc(sizeof *k);
484     kk = &k->next;
485     k->name = n;
486     k->value = v;
487   }
488   *kk = 0;
489   if(kvp) {
490     dynstr_append(&d, '?');
491     dynstr_append_string(&d, kvp_urlencode(kvp, 0));
492   }
493   dynstr_terminate(&d);
494   return d.vec;
495 }
496
497 void cgi_set_option(const char *name, const char *value) {
498   struct kvp *k = xmalloc(sizeof *k);
499
500   k->next = labels;
501   k->name = name;
502   k->value = value;
503   labels = k;
504 }
505
506 static void option_label(int attribute((unused)) nvec,
507                          char **vec) {
508   cgi_set_option(vec[0], vec[1]);
509 }
510
511 static void option_include(int attribute((unused)) nvec,
512                            char **vec) {
513   include_options(vec[0]);
514 }
515
516 static void option_columns(int nvec,
517                             char **vec) {
518   struct column *c = xmalloc(sizeof *c);
519   
520   c->next = columns;
521   c->name = vec[0];
522   c->ncolumns = nvec - 1;
523   c->columns = &vec[1];
524   columns = c;
525 }
526
527 static struct option {
528   const char *name;
529   int minargs, maxargs;
530   void (*handler)(int nvec, char **vec);
531 } options[] = {
532   { "columns", 1, INT_MAX, option_columns },
533   { "include", 1, 1, option_include },
534   { "label", 2, 2, option_label },
535 };
536
537 struct read_options_state {
538   const char *name;
539   int line;
540 };
541
542 static void read_options_error(const char *msg,
543                                void *u) {
544   struct read_options_state *cs = u;
545   
546   error(0, "%s:%d: %s", cs->name, cs->line, msg);
547 }
548
549 static void include_options(const char *name) {
550   int n, i;
551   int fd;
552   FILE *fp;
553   char **vec, *buffer;
554   struct read_options_state cs;
555
556   if((fd = template_open(name, "", &cs.name)) < 0) return;
557   if(!(fp = fdopen(fd, "r"))) fatal(errno, "error calling fdopen");
558   cs.line = 0;
559   while(!inputline(cs.name, fp, &buffer, '\n')) {
560     ++cs.line;
561     if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES,
562                      read_options_error, &cs)))
563       continue;
564     if(!n) continue;
565     if((i = TABLE_FIND(options, struct option, name, vec[0])) == -1) {
566       error(0, "%s:%d: unknown option '%s'", cs.name, cs.line, vec[0]);
567       continue;
568     }
569     ++vec;
570     --n;
571     if(n < options[i].minargs) {
572       error(0, "%s:%d: too few arguments to '%s'", cs.name, cs.line, vec[-1]);
573       continue;
574     }
575     if(n > options[i].maxargs) {
576       error(0, "%s:%d: too many arguments to '%s'", cs.name, cs.line, vec[-1]);
577       continue;
578     }
579     options[i].handler(n, vec);
580   }
581   fclose(fp);
582 }
583
584 static void read_options(void) {
585   if(!have_read_options) {
586     have_read_options = 1;
587     include_options("options");
588   }
589 }
590
591 const char *cgi_label(const char *key) {
592   const char *label;
593
594   read_options();
595   if(!(label = kvp_get(labels, key))) {
596     /* No label found */
597     if(!strncmp(key, "images.", 7)) {
598       static const char *url_static;
599       /* images.X defaults to <url.static>X.png */
600
601       if(!url_static)
602         url_static = cgi_label("url.static");
603       byte_xasprintf((char **)&label, "%s%s.png", url_static, key + 7);
604     } else if((label = strchr(key, '.')))
605       /* X.Y defaults to Y */
606       ++label;
607     else
608       /* otherwise default to label name */
609       label = key;
610   }
611   return label;
612 }
613
614 int cgi_label_exists(const char *key) {
615   read_options();
616   return kvp_get(labels, key) ? 1 : 0;
617 }
618
619 char **cgi_columns(const char *name, int *ncolumns) {
620   struct column *c;
621
622   read_options();
623   for(c = columns; c && strcmp(name, c->name); c = c->next)
624     ;
625   if(c) {
626     if(ncolumns)
627       *ncolumns = c->ncolumns;
628     return c->columns;
629   } else {
630     if(ncolumns)
631       *ncolumns = 0;
632     return 0;
633   }
634 }
635
636 /*
637 Local Variables:
638 c-basic-offset:2
639 comment-column:40
640 End:
641 */