chiark / gitweb /
www-cgi/: Decentralize the whitelist of environment variables.
[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
21#include <stdio.h>
22#include <string.h>
6aaa2ce3 23#include <errno.h>
6a580c17 24
f601a2c6
MW
25#include <unistd.h>
26
6a580c17 27#include "ucgi.h"
28
6a580c17 29int debugmode= 0;
30
31static void outerror(void) {
32 perror("stdout");
33 exit(debugmode ? 0 : -1);
34}
35
36void syserror(const char *m) {
37 if (printf("Content-Type: text/plain\n\n"
38 "ucgi: system call error:\n"
39 "%s: %s\n",
40 m,strerror(errno))==EOF || fflush(stdout)) outerror();
41 exit(0);
42}
43
44void error(const char *m) {
45 if (printf("Content-Type: text/plain\n\n"
46 "ucgi: error:\n"
47 "%s\n",
48 m)==EOF || fflush(stdout)) outerror();
49 exit(0);
50}
51
52void *xmalloc(size_t sz) {
53 void *r;
54
55 r= malloc(sz);
56 if (!r) syserror("malloc failed");
57 return r;
58}
59
44a77f48
MW
60void *xrealloc(void *ptr, size_t sz) {
61 void *r;
62
63 r= realloc(ptr,sz);
64 if (!r) syserror("realloc failed");
65 return r;
66}
67
6a580c17 68void xsetenv(const char *en, const char *ev, int overwrite) {
69 if (setenv(en,ev,overwrite)) syserror("setenv");
70}
f601a2c6
MW
71
72void filter_environment(unsigned flags, const char *prefix_in,
73 const char *const *patv,
74 void (*foundone)(const char *fulln,
75 const char *en, const char *ev,
76 void *p),
77 void *p)
78{
79 char *const *ep;
80 const char *const *patp;
81 const char *en, *ev, *pat, *q;
82 char enbuf[MAX_ENVVAR_NAME];
83 size_t n, pn = strlen(prefix_in);
84 int acceptp;
85
86 D( if (debugmode) printf(";; filter_environment...\n"); )
87 for (ep= environ; (en= *ep); ep++) {
88 D( if (debugmode) printf(";; consider env-var `%s'\n", en); )
89 if (strncmp(en, prefix_in, pn) != 0 || !en[pn]) {
90 D( if (debugmode) printf(";; doesn't match prefix\n"); )
91 goto next_ev;
92 }
93 for (patp= patv; (pat= *patp); patp++) {
94 q= en + pn;
95 acceptp= 1;
96 if (*pat == '!' && (flags & FILTF_WILDCARD)) {
97 acceptp= 0; pat++;
98 }
99 for (;;) {
100 if (!*pat) {
101 if (*q != '=') {
102 D( if (debugmode)
103 printf(";; mismatch `%s' (prefix)\n", *patp); )
104 goto next_pat;
105 }
106 D( if (debugmode) printf(";; matched `%s'\n", *patp); )
107 ev = q + 1;
108 break;
109 } else if (*pat == '*' && (flags & FILTF_WILDCARD)) {
110 q = strchr(q, '=');
111 if (!q) {
112 D( if (debugmode)
113 printf(";; mismatch `%s' (discard: no `=')\n", *patp); )
114 goto next_ev;
115 }
116 D( if (debugmode)
117 printf(";; wildcard match for `%s'\n", *patp); )
118 ev = q + 1;
119 break;
120 } else
121 if (*pat++ != *q++) {
122 D( if (debugmode) printf(";; mismatch `%s'\n", *patp); )
123 goto next_pat;
124 }
125 }
126 if (acceptp) {
127 n= q - en;
128 if (n >= sizeof(enbuf))
129 error("environment variable name too long");
130 memcpy(enbuf, en, n);
131 enbuf[n]= 0;
132 D( if (debugmode)
133 printf(";; full = `%s'; tail = `%s'; value = `%s'\n",
134 enbuf, enbuf + pn, ev); )
135 foundone(enbuf, enbuf + pn, ev, p);
136 } D( else if (debugmode)
137 printf(";; matched negated pattern\n"); )
138 goto next_ev;
139 next_pat:;
140 }
141 next_ev:;
142 }
143}