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