chiark / gitweb /
Separate parsing of templates from expansion.
[disorder] / server / cgi.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
5aff007d 3 * Copyright (C) 2004-2008 Richard Kettlewell
460b9539 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"
18cda350 55#include "unicode.h"
3dddcfa4 56#include "hash.h"
460b9539 57
58struct kvp *cgi_args;
59
60/* options */
61struct column {
62 struct column *next;
63 char *name;
64 int ncolumns;
65 char **columns;
66};
67
3dddcfa4
RK
68/* macros */
69struct cgi_macro {
70 int nargs;
71 char **args;
72 const char *value;
73};
74
75static hash *cgi_macros;
76
c582f1e8
RK
77/** @brief Parse of a template */
78struct cgi_element {
79 /** @brief Next element */
80 struct cgi_element *next;
81
82 /** @brief Element type */
83 int type;
84#define ELEMENT_TEXT 0
85#define ELEMENT_EXPANSION 1
86
87 /** @brief Line number at start of element */
88 int line;
89
90 /** @brief Plain text */
91 char *text;
92
93 /** @brief Expansion name */
94 char *name;
95
96 /** @brief Argument count */
97 int nargs;
98
99 /** @brief Argument values (NOT recursively expanded) */
100 char **args;
101};
102
460b9539 103#define RELIST(x) struct re *x, **x##_tail = &x
104
105static int have_read_options;
106static struct kvp *labels;
107static struct column *columns;
108
109static void include_options(const char *name);
110
111static void cgi_parse_get(void) {
112 const char *q;
113
114 if(!(q = getenv("QUERY_STRING"))) fatal(0, "QUERY_STRING not set");
115 cgi_args = kvp_urldecode(q, strlen(q));
116}
117
118static void cgi_input(char **ptrp, size_t *np) {
119 const char *cl;
120 char *q;
121 size_t n, m = 0;
122 int r;
123
124 if(!(cl = getenv("CONTENT_LENGTH"))) fatal(0, "CONTENT_LENGTH not set");
125 n = atol(cl);
126 q = xmalloc_noptr(n + 1);
127 while(m < n) {
128 r = read(0, q + m, n - m);
129 if(r > 0)
130 m += r;
131 else if(r == 0)
132 fatal(0, "unexpected end of file reading request body");
133 else switch(errno) {
134 case EINTR: break;
135 default: fatal(errno, "error reading request body");
136 }
137 }
138 if(memchr(q, 0, n)) fatal(0, "null character in request body");
139 q[n + 1] = 0;
140 *ptrp = q;
141 if(np) *np = n;
142}
143
144static int cgi_field_callback(const char *name, const char *value,
145 void *u) {
146 char *disposition, *pname, *pvalue;
147 char **namep = u;
148
149 if(!strcmp(name, "content-disposition")) {
150 if(mime_rfc2388_content_disposition(value,
151 &disposition,
152 &pname,
153 &pvalue))
154 fatal(0, "error parsing Content-Disposition field");
155 if(!strcmp(disposition, "form-data")
156 && pname
157 && !strcmp(pname, "name")) {
158 if(*namep)
159 fatal(0, "duplicate Content-Disposition field");
160 *namep = pvalue;
161 }
162 }
163 return 0;
164}
165
166static int cgi_part_callback(const char *s,
167 void attribute((unused)) *u) {
168 char *name = 0;
169 struct kvp *k;
170
171 if(!(s = mime_parse(s, cgi_field_callback, &name)))
172 fatal(0, "error parsing part header");
173 if(!name) fatal(0, "no name found");
174 k = xmalloc(sizeof *k);
175 k->next = cgi_args;
176 k->name = name;
177 k->value = s;
178 cgi_args = k;
179 return 0;
180}
181
182static void cgi_parse_multipart(const char *boundary) {
183 char *q;
184
185 cgi_input(&q, 0);
186 if(mime_multipart(q, cgi_part_callback, boundary, 0))
187 fatal(0, "invalid multipart object");
188}
189
190static void cgi_parse_post(void) {
9bce81d1 191 const char *ct, *boundary;
192 char *q, *type;
460b9539 193 size_t n;
9bce81d1 194 struct kvp *k;
460b9539 195
196 if(!(ct = getenv("CONTENT_TYPE")))
197 ct = "application/x-www-form-urlencoded";
9bce81d1 198 if(mime_content_type(ct, &type, &k))
460b9539 199 fatal(0, "invalid content type '%s'", ct);
200 if(!strcmp(type, "application/x-www-form-urlencoded")) {
201 cgi_input(&q, &n);
202 cgi_args = kvp_urldecode(q, n);
203 return;
204 }
205 if(!strcmp(type, "multipart/form-data")) {
9bce81d1 206 if(!(boundary = kvp_get(k, "boundary")))
207 fatal(0, "no boundary parameter found");
208 cgi_parse_multipart(boundary);
460b9539 209 return;
210 }
211 fatal(0, "unrecognized content type '%s'", type);
212}
213
214void cgi_parse(void) {
215 const char *p;
216 struct kvp *k;
217
218 if(!(p = getenv("REQUEST_METHOD"))) fatal(0, "REQUEST_METHOD not set");
219 if(!strcmp(p, "GET"))
220 cgi_parse_get();
221 else if(!strcmp(p, "POST"))
222 cgi_parse_post();
223 else
224 fatal(0, "unknown request method %s", p);
225 for(k = cgi_args; k; k = k->next)
18cda350
RK
226 if(!utf8_valid(k->name, strlen(k->name))
227 || !utf8_valid(k->value, strlen(k->value)))
460b9539 228 fatal(0, "invalid UTF-8 sequence in cgi argument");
229}
230
231const char *cgi_get(const char *name) {
232 return kvp_get(cgi_args, name);
233}
234
235void cgi_output(cgi_sink *output, const char *fmt, ...) {
236 va_list ap;
237 int n;
238 char *r;
239
240 va_start(ap, fmt);
241 n = byte_vasprintf(&r, fmt, ap);
242 if(n < 0)
243 fatal(errno, "error calling byte_vasprintf");
244 if(output->quote)
245 r = cgi_sgmlquote(r, 0);
246 output->sink->write(output->sink, r, strlen(r));
247 va_end(ap);
248}
249
250void cgi_header(struct sink *output, const char *name, const char *value) {
251 sink_printf(output, "%s: %s\r\n", name, value);
252}
253
254void cgi_body(struct sink *output) {
255 sink_printf(output, "\r\n");
256}
257
258char *cgi_sgmlquote(const char *s, int raw) {
259 uint32_t *ucs, *p, c;
260 char *b, *bp;
261 int n;
262
263 if(!raw) {
caecd4f4 264 if(!(ucs = utf8_to_utf32(s, strlen(s), 0))) exit(EXIT_FAILURE);
460b9539 265 } else {
266 ucs = xmalloc_noptr((strlen(s) + 1) * sizeof(uint32_t));
267 for(n = 0; s[n]; ++n)
268 ucs[n] = (unsigned char)s[n];
269 ucs[n] = 0;
270 }
271
272 n = 1;
273 /* estimate the length we'll need */
274 for(p = ucs; (c = *p); ++p) {
275 switch(c) {
276 default:
277 if(c > 127 || c < 32) {
278 case '"':
279 case '&':
280 case '<':
281 case '>':
282 n += 12;
283 break;
284 } else
285 n++;
286 }
287 }
288 /* format the string */
289 b = bp = xmalloc_noptr(n);
290 for(p = ucs; (c = *p); ++p) {
291 switch(c) {
292 default:
293 if(*p > 127 || *p < 32) {
294 case '"':
295 case '&':
296 case '<':
297 case '>':
298 bp += sprintf(bp, "&#%lu;", (unsigned long)c);
299 break;
300 } else
301 *bp++ = c;
302 }
303 }
304 *bp = 0;
305 return b;
306}
307
308void cgi_attr(struct sink *output, const char *name, const char *value) {
309 if(!value[strspn(value, "abcdefghijklmnopqrstuvwxyz"
310 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
311 "0123456789")])
312 sink_printf(output, "%s=%s", name, value);
313 else
314 sink_printf(output, "%s=\"%s\"", name, cgi_sgmlquote(value, 0));
315}
316
317void cgi_opentag(struct sink *output, const char *name, ...) {
318 va_list ap;
319 const char *n, *v;
320
321 sink_printf(output, "<%s", name);
322 va_start(ap, name);
323 while((n = va_arg(ap, const char *))) {
324 sink_printf(output, " ");
325 v = va_arg(ap, const char *);
326 if(v)
327 cgi_attr(output, n, v);
328 else
329 sink_printf(output, n);
330 }
331 sink_printf(output, ">");
332}
333
334void cgi_closetag(struct sink *output, const char *name) {
335 sink_printf(output, "</%s>", name);
336}
337
338static int template_open(const char *name,
339 const char *ext,
340 const char **filenamep) {
341 const char *dirs[2];
342 int fd = -1, n;
343 char *fullpath;
344
345 dirs[0] = pkgconfdir;
346 dirs[1] = pkgdatadir;
347 if(name[0] == '/') {
348 if((fd = open(name, O_RDONLY)) < 0) fatal(0, "cannot open %s", name);
349 *filenamep = name;
350 } else {
351 for(n = 0; n < config->templates.n + (int)(sizeof dirs / sizeof *dirs); ++n) {
352 byte_xasprintf(&fullpath, "%s/%s%s",
353 n < config->templates.n ? config->templates.s[n]
354 : dirs[n - config->templates.n],
355 name, ext);
356 if((fd = open(fullpath, O_RDONLY)) >= 0) break;
357 }
358 if(fd < 0) error(0, "cannot find %s%s in template path", name, ext);
359 *filenamep = fullpath;
360 }
361 return fd;
362}
363
364static int valid_template_name(const char *name) {
365 if(strchr(name, '/') || name[0] == '.')
366 return 0;
367 return 1;
368}
369
370void cgi_expand(const char *template,
371 const struct cgi_expansion *expansions,
372 size_t nexpansions,
373 cgi_sink *output,
374 void *u) {
375 int fd = -1;
376 int n;
377 off_t m;
378 char *b;
379 struct stat sb;
380
381 if(!valid_template_name(template))
382 fatal(0, "invalid template name '%s'", template);
383 if((fd = template_open(template, ".html", &template)) < 0)
384 exitfn(EXIT_FAILURE);
385 if(fstat(fd, &sb) < 0) fatal(errno, "cannot stat %s", template);
386 m = 0;
387 b = xmalloc_noptr(sb.st_size + 1);
388 while(m < sb.st_size) {
389 n = read(fd, b + m, sb.st_size - m);
390 if(n > 0) m += n;
391 else if(n == 0) fatal(0, "unexpected EOF reading %s", template);
392 else if(errno != EINTR) fatal(errno, "error reading %s", template);
393 }
394 b[sb.st_size] = 0;
395 xclose(fd);
396 cgi_expand_string(template, b, expansions, nexpansions, output, u);
397}
398
c582f1e8
RK
399/** @brief Return a linked list of the parse of @p template */
400static struct cgi_element *cgi_parse_string(const char *name,
401 const char *template) {
402 int braces, line = 1, sline;
460b9539 403 const char *p;
404 struct vector v;
405 struct dynstr d;
c582f1e8
RK
406 struct cgi_element *head = 0, **tailp = &head, *e;
407
460b9539 408 while(*template) {
409 if(*template != '@') {
c582f1e8
RK
410 sline = line;
411 dynstr_init(&d);
412 /* Gather up text without any expansions in. */
413 while(*template && *template != '@') {
414 if(*template == '\n')
415 ++line;
416 dynstr_append(&d, *template++);
460b9539 417 }
c582f1e8
RK
418 dynstr_terminate(&d);
419 e = xmalloc(sizeof *e);
420 e->next = 0;
421 e->line = sline;
422 e->type = ELEMENT_TEXT;
423 e->text = d.vec;
424 *tailp = e;
425 tailp = &e->next;
460b9539 426 continue;
427 }
428 vector_init(&v);
429 braces = 0;
c582f1e8 430 p = template;
460b9539 431 ++template;
432 sline = line;
433 while(*template != '@') {
f6b388d0
RK
434 /* Skip whitespace */
435 while(isspace((unsigned char)*template))
436 ++template;
460b9539 437 dynstr_init(&d);
438 if(*template == '{') {
439 /* bracketed arg */
440 ++template;
441 while(*template && (*template != '}' || braces > 0)) {
442 switch(*template) {
443 case '{': ++braces; break;
444 case '}': --braces; break;
445 case '\n': ++line; break;
446 }
447 dynstr_append(&d, *template++);
448 }
c582f1e8
RK
449 if(!*template) fatal(0, "%s:%d: unterminated expansion '%.*s'",
450 name, sline, (int)(template - p), p);
460b9539 451 ++template;
f6b388d0
RK
452 if(isspace((unsigned char)*template)) {
453 /* We have @{...}<WHITESPACE><SOMETHING> */
454 for(p = template; isspace((unsigned char)*p); ++p)
455 ;
456 /* Now we are looking at <SOMETHING>. If it's "{" then that
457 * must be the next argument. Otherwise we infer that this
458 * is really the end of the expansion. */
459 if(*p != '{')
460 goto finished_expansion;
461 }
460b9539 462 } else {
463 /* unbracketed arg */
460b9539 464 while(*template
465 && *template != '@' && *template != '{' && *template != ':') {
466 if(*template == '\n') ++line;
467 dynstr_append(&d, *template++);
468 }
469 if(*template == ':')
470 ++template;
c582f1e8
RK
471 if(!*template) fatal(0, "%s:%d: unterminated expansion '%.*s'",
472 name, sline, (int)(template - p), p);
460b9539 473 /* trailing whitespace is not significant in unquoted args */
474 while(d.nvec && (isspace((unsigned char)d.vec[d.nvec - 1])))
475 --d.nvec;
476 }
477 dynstr_terminate(&d);
478 vector_append(&v, d.vec);
479 }
480 ++template;
c582f1e8 481 finished_expansion:
460b9539 482 vector_terminate(&v);
483 /* @@ terminates this file */
484 if(v.nvec == 0)
485 break;
c582f1e8
RK
486 e = xmalloc(sizeof *e);
487 e->next = 0;
488 e->line = sline;
489 e->type = ELEMENT_EXPANSION;
490 e->name = v.vec[0];
491 e->nargs = v.nvec - 1;
492 e->args = &v.vec[1];
493 *tailp = e;
494 tailp = &e->next;
495 }
496 return head;
497}
498
499void cgi_expand_string(const char *name,
500 const char *template,
501 const struct cgi_expansion *expansions,
502 size_t nexpansions,
503 cgi_sink *output,
504 void *u) {
505 int n, m;
506 char *argname;
507 struct dynstr d;
508 cgi_sink parameter_output;
509 const struct cgi_macro *macro;
510
511 struct cgi_element *e;
512
513 for(e = cgi_parse_string(name, template); e; e = e->next) {
514 switch(e->type) {
515 case ELEMENT_TEXT:
516 output->sink->write(output->sink, e->text, strlen(e->text));
517 break;
518 case ELEMENT_EXPANSION:
519 if((n = table_find(expansions,
520 offsetof(struct cgi_expansion, name),
521 sizeof (struct cgi_expansion),
522 nexpansions,
523 e->name)) >= 0) {
524 /* We found a built-in */
525 if(e->nargs < expansions[n].minargs)
526 fatal(0, "%s:%d: insufficient arguments to @%s@ (min %d, got %d)",
527 name, e->line, e->name, expansions[n].minargs, e->nargs);
528 if(e->nargs > expansions[n].maxargs)
529 fatal(0, "%s:%d: too many arguments to @%s@ (max %d, got %d)",
530 name, e->line, e->name, expansions[n].maxargs, e->nargs);
531 /* for ordinary expansions, recursively expand the arguments */
532 if(!(expansions[n].flags & EXP_MAGIC)) {
533 for(m = 0; m < e->nargs; ++m) {
534 dynstr_init(&d);
535 byte_xasprintf(&argname, "<%s:%d arg #%d>", name, e->line, m);
536 parameter_output.quote = 0;
537 parameter_output.sink = sink_dynstr(&d);
538 cgi_expand_string(argname, e->args[m],
539 expansions, nexpansions,
540 &parameter_output, u);
541 dynstr_terminate(&d);
542 e->args[m] = d.vec;
543 }
3dddcfa4 544 }
c582f1e8
RK
545 expansions[n].handler(e->nargs, e->args, output, u);
546 } else if(cgi_macros && (macro = hash_find(cgi_macros, e->name))) {
547 /* We found a macro */
548 if(e->nargs != macro->nargs)
549 fatal(0, "%s:%d: wrong number of arguments to @%s@ (need %d, got %d)",
550 name, e->line, e->name, macro->nargs, e->nargs);
551 /* We must substitute in argument values */
552 /* TODO */
553 cgi_expand_string(e->name,
554 macro->value,
555 expansions,
556 nexpansions,
557 output,
558 u);
559 } else {
560 /* Totally undefined */
561 fatal(0, "%s:%d: unknown expansion '%s'", name, e->line, e->name);
460b9539 562 }
c582f1e8 563 break;
460b9539 564 }
460b9539 565 }
566}
567
568char *cgi_makeurl(const char *url, ...) {
569 va_list ap;
570 struct kvp *kvp, *k, **kk = &kvp;
571 struct dynstr d;
572 const char *n, *v;
573
574 dynstr_init(&d);
575 dynstr_append_string(&d, url);
576 va_start(ap, url);
577 while((n = va_arg(ap, const char *))) {
578 v = va_arg(ap, const char *);
579 *kk = k = xmalloc(sizeof *k);
580 kk = &k->next;
581 k->name = n;
582 k->value = v;
583 }
584 *kk = 0;
585 if(kvp) {
586 dynstr_append(&d, '?');
587 dynstr_append_string(&d, kvp_urlencode(kvp, 0));
588 }
589 dynstr_terminate(&d);
590 return d.vec;
591}
592
593void cgi_set_option(const char *name, const char *value) {
594 struct kvp *k = xmalloc(sizeof *k);
595
596 k->next = labels;
597 k->name = name;
598 k->value = value;
599 labels = k;
600}
601
602static void option_label(int attribute((unused)) nvec,
603 char **vec) {
604 cgi_set_option(vec[0], vec[1]);
605}
606
607static void option_include(int attribute((unused)) nvec,
608 char **vec) {
609 include_options(vec[0]);
610}
611
612static void option_columns(int nvec,
613 char **vec) {
614 struct column *c = xmalloc(sizeof *c);
615
616 c->next = columns;
617 c->name = vec[0];
618 c->ncolumns = nvec - 1;
619 c->columns = &vec[1];
620 columns = c;
621}
622
623static struct option {
624 const char *name;
625 int minargs, maxargs;
626 void (*handler)(int nvec, char **vec);
627} options[] = {
628 { "columns", 1, INT_MAX, option_columns },
629 { "include", 1, 1, option_include },
630 { "label", 2, 2, option_label },
631};
632
633struct read_options_state {
634 const char *name;
635 int line;
636};
637
638static void read_options_error(const char *msg,
639 void *u) {
640 struct read_options_state *cs = u;
641
642 error(0, "%s:%d: %s", cs->name, cs->line, msg);
643}
644
645static void include_options(const char *name) {
646 int n, i;
647 int fd;
648 FILE *fp;
649 char **vec, *buffer;
650 struct read_options_state cs;
651
652 if((fd = template_open(name, "", &cs.name)) < 0) return;
653 if(!(fp = fdopen(fd, "r"))) fatal(errno, "error calling fdopen");
654 cs.line = 0;
655 while(!inputline(cs.name, fp, &buffer, '\n')) {
656 ++cs.line;
657 if(!(vec = split(buffer, &n, SPLIT_COMMENTS|SPLIT_QUOTES,
658 read_options_error, &cs)))
659 continue;
660 if(!n) continue;
661 if((i = TABLE_FIND(options, struct option, name, vec[0])) == -1) {
662 error(0, "%s:%d: unknown option '%s'", cs.name, cs.line, vec[0]);
663 continue;
664 }
665 ++vec;
666 --n;
667 if(n < options[i].minargs) {
668 error(0, "%s:%d: too few arguments to '%s'", cs.name, cs.line, vec[-1]);
669 continue;
670 }
671 if(n > options[i].maxargs) {
672 error(0, "%s:%d: too many arguments to '%s'", cs.name, cs.line, vec[-1]);
673 continue;
674 }
675 options[i].handler(n, vec);
676 }
677 fclose(fp);
678}
679
680static void read_options(void) {
681 if(!have_read_options) {
682 have_read_options = 1;
683 include_options("options");
684 }
685}
686
687const char *cgi_label(const char *key) {
688 const char *label;
689
690 read_options();
691 if(!(label = kvp_get(labels, key))) {
5e34540b 692 /* No label found */
693 if(!strncmp(key, "images.", 7)) {
694 static const char *url_static;
695 /* images.X defaults to <url.static>X.png */
696
697 if(!url_static)
698 url_static = cgi_label("url.static");
699 byte_xasprintf((char **)&label, "%s%s.png", url_static, key + 7);
700 } else if((label = strchr(key, '.')))
701 /* X.Y defaults to Y */
460b9539 702 ++label;
703 else
5e34540b 704 /* otherwise default to label name */
460b9539 705 label = key;
706 }
707 return label;
708}
709
8f9616f1
RK
710int cgi_label_exists(const char *key) {
711 read_options();
712 return kvp_get(labels, key) ? 1 : 0;
713}
714
460b9539 715char **cgi_columns(const char *name, int *ncolumns) {
716 struct column *c;
717
718 read_options();
719 for(c = columns; c && strcmp(name, c->name); c = c->next)
720 ;
721 if(c) {
722 if(ncolumns)
723 *ncolumns = c->ncolumns;
724 return c->columns;
725 } else {
726 if(ncolumns)
727 *ncolumns = 0;
728 return 0;
729 }
730}
731
3dddcfa4
RK
732void cgi_define(const char *name,
733 int nargs,
734 char **args,
735 const char *value) {
736 struct cgi_macro m;
737
738 if(!cgi_macros)
739 cgi_macros = hash_new(sizeof(struct cgi_macro));
740 m.nargs = nargs;
741 m.args = args;
742 m.value = value;
743 hash_add(cgi_macros, name, &m, HASH_INSERT_OR_REPLACE);
744}
745
460b9539 746/*
747Local Variables:
748c-basic-offset:2
749comment-column:40
750End:
751*/