2 * Copyright (C) 1998-1999,2003 Ian Jackson
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.
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.
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.
29 const char *const envok
[]= {
36 "HTTP_ACCEPT_CHARSET",
37 "HTTP_ACCEPT_ENCODING",
38 "HTTP_ACCEPT_LANGUAGE",
41 "HTTP_CONTENT_ENCODING",
51 "HTTP_X_FORWARDED_FOR",
75 const int nenvok
= sizeof(envok
)/sizeof(envok
[0]);
79 static void outerror(void) {
81 exit(debugmode ?
0 : -1);
84 void syserror(const char *m
) {
85 if (printf("Content-Type: text/plain\n\n"
86 "ucgi: system call error:\n"
88 m
,strerror(errno
))==EOF
|| fflush(stdout
)) outerror();
92 void error(const char *m
) {
93 if (printf("Content-Type: text/plain\n\n"
96 m
)==EOF
|| fflush(stdout
)) outerror();
100 void *xmalloc(size_t sz
) {
104 if (!r
) syserror("malloc failed");
108 void *xrealloc(void *ptr
, size_t sz
) {
112 if (!r
) syserror("realloc failed");
116 void xsetenv(const char *en
, const char *ev
, int overwrite
) {
117 if (setenv(en
,ev
,overwrite
)) syserror("setenv");
120 void filter_environment(unsigned flags
, const char *prefix_in
,
121 const char *const *patv
,
122 void (*foundone
)(const char *fulln
,
123 const char *en
, const char *ev
,
128 const char *const *patp
;
129 const char *en
, *ev
, *pat
, *q
;
130 char enbuf
[MAX_ENVVAR_NAME
];
131 size_t n
, pn
= strlen(prefix_in
);
134 D( if (debugmode
) printf(";; filter_environment...\n"); )
135 for (ep
= environ
; (en
= *ep
); ep
++) {
136 D( if (debugmode
) printf(";; consider env-var `%s'\n", en
); )
137 if (strncmp(en
, prefix_in
, pn
) != 0 || !en
[pn
]) {
138 D( if (debugmode
) printf(";; doesn't match prefix\n"); )
141 for (patp
= patv
; (pat
= *patp
); patp
++) {
144 if (*pat
== '!' && (flags
& FILTF_WILDCARD
)) {
151 printf(";; mismatch `%s' (prefix)\n", *patp
); )
154 D( if (debugmode
) printf(";; matched `%s'\n", *patp
); )
157 } else if (*pat
== '*' && (flags
& FILTF_WILDCARD
)) {
161 printf(";; mismatch `%s' (discard: no `=')\n", *patp
); )
165 printf(";; wildcard match for `%s'\n", *patp
); )
169 if (*pat
++ != *q
++) {
170 D( if (debugmode
) printf(";; mismatch `%s'\n", *patp
); )
176 if (n
>= sizeof(enbuf
))
177 error("environment variable name too long");
178 memcpy(enbuf
, en
, n
);
181 printf(";; full = `%s'; tail = `%s'; value = `%s'\n",
182 enbuf
, enbuf
+ pn
, ev
); )
183 foundone(enbuf
, enbuf
+ pn
, ev
, p
);
184 } D( else if (debugmode
)
185 printf(";; matched negated pattern\n"); )