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