Commit | Line | Data |
---|---|---|
a33962ba | 1 | /* |
9028e234 IJ |
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> | |
a33962ba | 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 | |
9028e234 | 10 | * the Free Software Foundation; either version 3 of the License, or |
a33962ba | 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 | |
9028e234 | 19 | * along with userv-utils; if not, see http://www.gnu.org/licenses/. |
a33962ba | 20 | */ |
6a580c17 | 21 | |
aa0bce91 | 22 | #include <assert.h> |
f7b4be5a MW |
23 | #include <ctype.h> |
24 | #include <stdarg.h> | |
6a580c17 | 25 | #include <stdio.h> |
26 | #include <string.h> | |
6aaa2ce3 | 27 | #include <errno.h> |
6a580c17 | 28 | |
f601a2c6 | 29 | #include <unistd.h> |
6a580c17 | 30 | |
6a580c17 | 31 | #include "ucgi.h" |
6a580c17 | 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) { | |
0cd9d59d MW |
41 | if (printf("Content-Type: text/plain\n" |
42 | "Status: 500\n\n" | |
6a580c17 | 43 | "ucgi: system call error:\n" |
44 | "%s: %s\n", | |
45 | m,strerror(errno))==EOF || fflush(stdout)) outerror(); | |
46 | exit(0); | |
47 | } | |
48 | ||
0cd9d59d MW |
49 | void error(const char *m, int st) { |
50 | if (printf("Content-Type: text/plain\n" | |
51 | "Status: %d\n\n" | |
6a580c17 | 52 | "ucgi: error:\n" |
53 | "%s\n", | |
0cd9d59d | 54 | st, m)==EOF || fflush(stdout)) outerror(); |
6a580c17 | 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 | ||
44a77f48 MW |
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 | ||
6a580c17 | 74 | void xsetenv(const char *en, const char *ev, int overwrite) { |
75 | if (setenv(en,ev,overwrite)) syserror("setenv"); | |
76 | } | |
f601a2c6 | 77 | |
540ab05f | 78 | const char **load_filters(unsigned flags, const char *first, ...) { |
f7b4be5a MW |
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)) | |
0cd9d59d | 109 | error("line too long in environment filter file", 500); |
f7b4be5a MW |
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 | ||
92fc834e MW |
130 | static int envvar_match(unsigned flags, const char *en, |
131 | const char *const *patv, | |
aa0bce91 | 132 | const char *const *defaults, |
540ab05f | 133 | const char **ev) { |
92fc834e MW |
134 | const char *const *patp; |
135 | const char *q, *pat; | |
136 | int acceptp; | |
aa0bce91 | 137 | int rc; |
92fc834e | 138 | |
aa0bce91 | 139 | if (!patv) { patv= defaults; defaults= 0; } |
92fc834e MW |
140 | for (patp= patv; (pat= *patp); patp++) { |
141 | q= en; | |
142 | acceptp= 1; | |
143 | if (*pat == '!' && (flags & FILTF_WILDCARD)) { acceptp= 0; pat++; } | |
aa0bce91 MW |
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 | |
0cd9d59d | 150 | error("unknown pattern directive", 500); |
aa0bce91 MW |
151 | continue; |
152 | } | |
92fc834e MW |
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 | ||
f601a2c6 MW |
189 | void filter_environment(unsigned flags, const char *prefix_in, |
190 | const char *const *patv, | |
aa0bce91 | 191 | const char *const *defaults, |
f601a2c6 MW |
192 | void (*foundone)(const char *fulln, |
193 | const char *en, const char *ev, | |
194 | void *p), | |
540ab05f | 195 | void *p) { |
f601a2c6 | 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)) | |
0cd9d59d | 211 | error("environment variable name too long", 500); |
92fc834e MW |
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 | } |