ucgi/: Return useful status codes when things go wrong.
[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) {
0cd9d59d
MW
40 if (printf("Content-Type: text/plain\n"
41 "Status: 500\n\n"
6a580c17 42 "ucgi: system call error:\n"
43 "%s: %s\n",
44 m,strerror(errno))==EOF || fflush(stdout)) outerror();
45 exit(0);
46}
47
0cd9d59d
MW
48void error(const char *m, int st) {
49 if (printf("Content-Type: text/plain\n"
50 "Status: %d\n\n"
6a580c17 51 "ucgi: error:\n"
52 "%s\n",
0cd9d59d 53 st, m)==EOF || fflush(stdout)) outerror();
6a580c17 54 exit(0);
55}
56
57void *xmalloc(size_t sz) {
58 void *r;
59
60 r= malloc(sz);
61 if (!r) syserror("malloc failed");
62 return r;
63}
64
44a77f48
MW
65void *xrealloc(void *ptr, size_t sz) {
66 void *r;
67
68 r= realloc(ptr,sz);
69 if (!r) syserror("realloc failed");
70 return r;
71}
72
6a580c17 73void xsetenv(const char *en, const char *ev, int overwrite) {
74 if (setenv(en,ev,overwrite)) syserror("setenv");
75}
f601a2c6 76
f7b4be5a
MW
77const char **load_filters(unsigned flags, const char *first, ...)
78{
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
99opened:
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
130static int envvar_match(unsigned flags, const char *en,
131 const char *const *patv,
aa0bce91 132 const char *const *defaults,
92fc834e
MW
133 const char **ev)
134{
135 const char *const *patp;
136 const char *q, *pat;
137 int acceptp;
aa0bce91 138 int rc;
92fc834e 139
aa0bce91 140 if (!patv) { patv= defaults; defaults= 0; }
92fc834e
MW
141 for (patp= patv; (pat= *patp); patp++) {
142 q= en;
143 acceptp= 1;
144 if (*pat == '!' && (flags & FILTF_WILDCARD)) { acceptp= 0; pat++; }
aa0bce91
MW
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
0cd9d59d 151 error("unknown pattern directive", 500);
aa0bce91
MW
152 continue;
153 }
92fc834e
MW
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
184match:
185 if (!acceptp) return -1;
186 *ev= q + 1;
187 return +1;
188}
189
f601a2c6
MW
190void filter_environment(unsigned flags, const char *prefix_in,
191 const char *const *patv,
aa0bce91 192 const char *const *defaults,
f601a2c6
MW
193 void (*foundone)(const char *fulln,
194 const char *en, const char *ev,
195 void *p),
196 void *p)
197{
198 char *const *ep;
92fc834e 199 const char *en, *ev;
f601a2c6
MW
200 char enbuf[MAX_ENVVAR_NAME];
201 size_t n, pn = strlen(prefix_in);
f601a2c6
MW
202
203 D( if (debugmode) printf(";; filter_environment...\n"); )
204 for (ep= environ; (en= *ep); ep++) {
205 D( if (debugmode) printf(";; consider env-var `%s'\n", en); )
206 if (strncmp(en, prefix_in, pn) != 0 || !en[pn]) {
207 D( if (debugmode) printf(";; doesn't match prefix\n"); )
92fc834e 208 continue;
f601a2c6 209 }
aa0bce91 210 if (envvar_match(flags, en + pn, patv, defaults, &ev) > 0) {
92fc834e
MW
211 n= strcspn(en, "=");
212 if (n >= sizeof(enbuf))
0cd9d59d 213 error("environment variable name too long", 500);
92fc834e
MW
214 memcpy(enbuf, en, n);
215 enbuf[n]= 0;
216 D( if (debugmode)
217 printf(";; full = `%s'; tail = `%s'; value = `%s'\n",
218 enbuf, enbuf + pn, ev); )
219 foundone(enbuf, enbuf + pn, ev, p);
f601a2c6 220 }
f601a2c6
MW
221 }
222}