chiark / gitweb /
4d371877911d5e2381717a34892939ba3b4ed73c
[userv-utils.git] / www-cgi / ucgicommon.c
1 /*
2  * Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
3  * Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
4  * Copyright 1999,2003
5  *    Chancellor Masters and Scholars of the University of Cambridge
6  * Copyright 2010 Tony Finch <fanf@dotat.at>
7  *
8  * This is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with userv-utils; if not, see http://www.gnu.org/licenses/.
20  */
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include <unistd.h>
30
31 #include "ucgi.h"
32
33 int debugmode= 0;
34
35 static void outerror(void) {
36   perror("stdout");
37   exit(debugmode ? 0 : -1);
38 }
39
40 void syserror(const char *m) {
41   if (printf("Content-Type: text/plain\n"
42              "Status: 500\n\n"
43              "ucgi: system call error:\n"
44              "%s: %s\n",
45              m,strerror(errno))==EOF || fflush(stdout)) outerror();
46   exit(0);
47 }
48
49 void error(const char *m, int st) {
50   if (printf("Content-Type: text/plain\n"
51              "Status: %d\n\n"
52              "ucgi: error:\n"
53              "%s\n",
54              st, m)==EOF || fflush(stdout)) outerror();
55   exit(0);
56 }
57
58 void *xmalloc(size_t sz) {
59   void *r;
60
61   r= malloc(sz);
62   if (!r) syserror("malloc failed");
63   return r;
64 }
65
66 void *xrealloc(void *ptr, size_t sz) {
67   void *r;
68
69   r= realloc(ptr,sz);
70   if (!r) syserror("realloc failed");
71   return r;
72 }
73
74 void xsetenv(const char *en, const char *ev, int overwrite) {
75   if (setenv(en,ev,overwrite)) syserror("setenv");
76 }
77
78 const char **load_filters(unsigned flags, const char *first, ...) {
79   va_list ap;
80   const char *name, *p, *q, **v;
81   char *pp;
82   size_t l, n, sz;
83   FILE *fp;
84   char buf[MAX_ENVVAR_NAME];
85
86   D( if (debugmode) printf(";; load_filters...\n"); )
87   va_start(ap, first);
88   for (name= first; name; name= va_arg(ap, const char *)) {
89     fp= fopen(name, "r"); if (fp) goto opened;
90     D( if (debugmode)
91          printf(";;   file `%s': %s\n", name, strerror(errno)); )
92     if (errno != ENOENT) syserror("failed to open environment filters");
93   }
94   va_end(ap);
95   if (flags & LOADF_MUST) syserror("failed to open environment filters");
96   D( if (debugmode) printf(";;   using default filters\n"); )
97   return 0;
98
99 opened:
100   va_end(ap);
101   D( if (debugmode) printf(";;   file `%s': OK\n", name); )
102
103   n= 0; sz= 128; v= xmalloc(sz * sizeof(*v));
104   for (;;) {
105     if (!fgets(buf, sizeof(buf), fp)) break;
106     l= strlen(buf);
107     if (buf[l - 1] == '\n') buf[--l]= 0;
108     if (l + 1 == sizeof(buf))
109       error("line too long in environment filter file", 500);
110     p= buf; q= p + l;
111     while (isspace((unsigned char)*p)) p++;
112     while (q > p && isspace((unsigned char)q[-1])) q--;
113     if (*p == '#' || p == q) continue;
114     l= q - p;
115     pp= xmalloc(l + 1);
116     memcpy(pp, p, l);
117     pp[l]= 0;
118     v[n++]= pp;
119     D( if (debugmode) printf(";;   filter: `%s'\n", pp); )
120     if (n >= sz) {
121       sz *= 2;
122       v= xrealloc(v, sz * sizeof(*v));
123     }
124   }
125   if (ferror(fp)) syserror("failed to read environment filters");
126   fclose(fp);
127   return v;
128 }
129
130 static int envvar_match(unsigned flags, const char *en,
131                         const char *const *patv,
132                         const char *const *defaults,
133                         const char **ev) {
134   const char *const *patp;
135   const char *q, *pat;
136   int acceptp;
137   int rc;
138
139   if (!patv) { patv= defaults; defaults= 0; }
140   for (patp= patv; (pat= *patp); patp++) {
141     q= en;
142     acceptp= 1;
143     if (*pat == '!' && (flags & FILTF_WILDCARD)) { acceptp= 0; pat++; }
144     else if (*pat == '?') {
145       if (strcmp(pat + 1, "DEFAULTS") == 0) {
146         assert(defaults);
147         rc= envvar_match(flags, en, defaults, 0, ev);
148         if (rc) return rc;
149       } else
150         error("unknown pattern directive", 500);
151       continue;
152     }
153
154     for (;;) {
155       if (!*pat) {
156         if (*q != '=') {
157           D( if (debugmode)
158                printf(";;     mismatch `%s' (prefix)\n", *patp); )
159           break;
160         }
161         D( if (debugmode) printf(";;     matched pattern `%s'\n", *patp); )
162         goto match;
163       } else if (*pat == '*' && (flags & FILTF_WILDCARD)) {
164         q = strchr(q, '=');
165         if (!q) {
166           D( if (debugmode)
167                printf(";;     mismatch `%s' (discard: no `=')\n", *patp); )
168           return -1;
169         }
170         D( if (debugmode)
171              printf(";;     wildcard match for `%s'\n", *patp); )
172         goto match;
173       } else {
174         if (*pat++ != *q++) {
175           D( if (debugmode) printf(";;     mismatch `%s'\n", *patp); )
176           break;
177         }
178       }
179     }
180   }
181   return 0;
182
183 match:
184   if (!acceptp) return -1;
185   *ev= q + 1;
186   return +1;
187 }
188
189 void filter_environment(unsigned flags, const char *prefix_in,
190                         const char *const *patv,
191                         const char *const *defaults,
192                         void (*foundone)(const char *fulln,
193                                          const char *en, const char *ev,
194                                          void *p),
195                         void *p) {
196   char *const *ep;
197   const char *en, *ev;
198   char enbuf[MAX_ENVVAR_NAME];
199   size_t n, pn = strlen(prefix_in);
200
201   D( if (debugmode) printf(";; filter_environment...\n"); )
202   for (ep= environ; (en= *ep); ep++) {
203     D( if (debugmode) printf(";;   consider env-var `%s'\n", en); )
204     if (strncmp(en, prefix_in, pn) != 0 || !en[pn]) {
205       D( if (debugmode) printf(";;     doesn't match prefix\n"); )
206       continue;
207     }
208     if (envvar_match(flags, en + pn, patv, defaults, &ev) > 0) {
209       n= strcspn(en, "=");
210       if (n >= sizeof(enbuf))
211         error("environment variable name too long", 500);
212       memcpy(enbuf, en, n);
213       enbuf[n]= 0;
214       D( if (debugmode)
215            printf(";;     full = `%s'; tail = `%s'; value = `%s'\n",
216                   enbuf, enbuf + pn, ev); )
217       foundone(enbuf, enbuf + pn, ev, p);
218     }
219   }
220 }