chiark / gitweb /
www-cgi/: Allow customization of the environment filters.
[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
f7b4be5a
MW
21#include <ctype.h>
22#include <stdarg.h>
6a580c17 23#include <stdio.h>
24#include <string.h>
6aaa2ce3 25#include <errno.h>
6a580c17 26
f601a2c6
MW
27#include <unistd.h>
28
6a580c17 29#include "ucgi.h"
30
6a580c17 31int debugmode= 0;
32
33static void outerror(void) {
34 perror("stdout");
35 exit(debugmode ? 0 : -1);
36}
37
38void syserror(const char *m) {
39 if (printf("Content-Type: text/plain\n\n"
40 "ucgi: system call error:\n"
41 "%s: %s\n",
42 m,strerror(errno))==EOF || fflush(stdout)) outerror();
43 exit(0);
44}
45
46void error(const char *m) {
47 if (printf("Content-Type: text/plain\n\n"
48 "ucgi: error:\n"
49 "%s\n",
50 m)==EOF || fflush(stdout)) outerror();
51 exit(0);
52}
53
54void *xmalloc(size_t sz) {
55 void *r;
56
57 r= malloc(sz);
58 if (!r) syserror("malloc failed");
59 return r;
60}
61
44a77f48
MW
62void *xrealloc(void *ptr, size_t sz) {
63 void *r;
64
65 r= realloc(ptr,sz);
66 if (!r) syserror("realloc failed");
67 return r;
68}
69
6a580c17 70void xsetenv(const char *en, const char *ev, int overwrite) {
71 if (setenv(en,ev,overwrite)) syserror("setenv");
72}
f601a2c6 73
f7b4be5a
MW
74const char **load_filters(unsigned flags, const char *first, ...)
75{
76 va_list ap;
77 const char *name, *p, *q, **v;
78 char *pp;
79 size_t l, n, sz;
80 FILE *fp;
81 char buf[MAX_ENVVAR_NAME];
82
83 D( if (debugmode) printf(";; load_filters...\n"); )
84 va_start(ap, first);
85 for (name= first; name; name= va_arg(ap, const char *)) {
86 fp= fopen(name, "r"); if (fp) goto opened;
87 D( if (debugmode)
88 printf(";; file `%s': %s\n", name, strerror(errno)); )
89 if (errno != ENOENT) syserror("failed to open environment filters");
90 }
91 va_end(ap);
92 if (flags & LOADF_MUST) syserror("failed to open environment filters");
93 D( if (debugmode) printf(";; using default filters\n"); )
94 return 0;
95
96opened:
97 va_end(ap);
98 D( if (debugmode) printf(";; file `%s': OK\n", name); )
99
100 n= 0; sz= 128; v= xmalloc(sz * sizeof(*v));
101 for (;;) {
102 if (!fgets(buf, sizeof(buf), fp)) break;
103 l= strlen(buf);
104 if (buf[l - 1] == '\n') buf[--l]= 0;
105 if (l + 1 == sizeof(buf))
106 error("line too long in environment filter file");
107 p= buf; q= p + l;
108 while (isspace((unsigned char)*p)) p++;
109 while (q > p && isspace((unsigned char)q[-1])) q--;
110 if (*p == '#' || p == q) continue;
111 l= q - p;
112 pp= xmalloc(l + 1);
113 memcpy(pp, p, l);
114 pp[l]= 0;
115 v[n++]= pp;
116 D( if (debugmode) printf(";; filter: `%s'\n", pp); )
117 if (n >= sz) {
118 sz *= 2;
119 v= xrealloc(v, sz * sizeof(*v));
120 }
121 }
122 if (ferror(fp)) syserror("failed to read environment filters");
123 fclose(fp);
124 return v;
125}
126
f601a2c6
MW
127void filter_environment(unsigned flags, const char *prefix_in,
128 const char *const *patv,
129 void (*foundone)(const char *fulln,
130 const char *en, const char *ev,
131 void *p),
132 void *p)
133{
134 char *const *ep;
135 const char *const *patp;
136 const char *en, *ev, *pat, *q;
137 char enbuf[MAX_ENVVAR_NAME];
138 size_t n, pn = strlen(prefix_in);
139 int acceptp;
140
141 D( if (debugmode) printf(";; filter_environment...\n"); )
142 for (ep= environ; (en= *ep); ep++) {
143 D( if (debugmode) printf(";; consider env-var `%s'\n", en); )
144 if (strncmp(en, prefix_in, pn) != 0 || !en[pn]) {
145 D( if (debugmode) printf(";; doesn't match prefix\n"); )
146 goto next_ev;
147 }
148 for (patp= patv; (pat= *patp); patp++) {
149 q= en + pn;
150 acceptp= 1;
151 if (*pat == '!' && (flags & FILTF_WILDCARD)) {
152 acceptp= 0; pat++;
153 }
154 for (;;) {
155 if (!*pat) {
156 if (*q != '=') {
157 D( if (debugmode)
158 printf(";; mismatch `%s' (prefix)\n", *patp); )
159 goto next_pat;
160 }
161 D( if (debugmode) printf(";; matched `%s'\n", *patp); )
162 ev = q + 1;
163 break;
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 goto next_ev;
170 }
171 D( if (debugmode)
172 printf(";; wildcard match for `%s'\n", *patp); )
173 ev = q + 1;
174 break;
175 } else
176 if (*pat++ != *q++) {
177 D( if (debugmode) printf(";; mismatch `%s'\n", *patp); )
178 goto next_pat;
179 }
180 }
181 if (acceptp) {
182 n= q - en;
183 if (n >= sizeof(enbuf))
184 error("environment variable name too long");
185 memcpy(enbuf, en, n);
186 enbuf[n]= 0;
187 D( if (debugmode)
188 printf(";; full = `%s'; tail = `%s'; value = `%s'\n",
189 enbuf, enbuf + pn, ev); )
190 foundone(enbuf, enbuf + pn, ev, p);
191 } D( else if (debugmode)
192 printf(";; matched negated pattern\n"); )
193 goto next_ev;
194 next_pat:;
195 }
196 next_ev:;
197 }
198}