2 * Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
3 * Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
5 * Chancellor Masters and Scholars of the University of Cambridge
6 * Copyright 2010 Tony Finch <fanf@dotat.at>
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
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
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.
18 * You should have received a copy of the GNU General Public License
19 * along with userv-utils; if not, see http://www.gnu.org/licenses/.
35 static void outerror(void) {
37 exit(debugmode ?
0 : -1);
40 void syserror(const char *m
) {
41 if (printf("Content-Type: text/plain\n"
43 "ucgi: system call error:\n"
45 m
,strerror(errno
))==EOF
|| fflush(stdout
)) outerror();
49 void error(const char *m
, int st
) {
50 if (printf("Content-Type: text/plain\n"
54 st
, m
)==EOF
|| fflush(stdout
)) outerror();
58 void *xmalloc(size_t sz
) {
62 if (!r
) syserror("malloc failed");
66 void *xrealloc(void *ptr
, size_t sz
) {
70 if (!r
) syserror("realloc failed");
74 void xsetenv(const char *en
, const char *ev
, int overwrite
) {
75 if (setenv(en
,ev
,overwrite
)) syserror("setenv");
78 const char **load_filters(unsigned flags
, const char *first
, ...) {
80 const char *name
, *p
, *q
, **v
;
84 char buf
[MAX_ENVVAR_NAME
];
86 D( if (debugmode
) printf(";; load_filters...\n"); )
88 for (name
= first
; name
; name
= va_arg(ap
, const char *)) {
89 fp
= fopen(name
, "r"); if (fp
) goto opened
;
91 printf(";; file `%s': %s\n", name
, strerror(errno
)); )
92 if (errno
!= ENOENT
) syserror("failed to open environment filters");
95 if (flags
& LOADF_MUST
) syserror("failed to open environment filters");
96 D( if (debugmode
) printf(";; using default filters\n"); )
101 D( if (debugmode
) printf(";; file `%s': OK\n", name
); )
103 n
= 0; sz
= 128; v
= xmalloc(sz
* sizeof(*v
));
105 if (!fgets(buf
, sizeof(buf
), fp
)) break;
107 if (buf
[l
- 1] == '\n') buf
[--l
]= 0;
108 if (l
+ 1 == sizeof(buf
))
109 error("line too long in environment filter file", 500);
111 while (isspace((unsigned char)*p
)) p
++;
112 while (q
> p
&& isspace((unsigned char)q
[-1])) q
--;
113 if (*p
== '#' || p
== q
) continue;
119 D( if (debugmode
) printf(";; filter: `%s'\n", pp
); )
122 v
= xrealloc(v
, sz
* sizeof(*v
));
125 if (ferror(fp
)) syserror("failed to read environment filters");
130 static int envvar_match(unsigned flags
, const char *en
,
131 const char *const *patv
,
132 const char *const *defaults
,
134 const char *const *patp
;
139 if (!patv
) { patv
= defaults
; defaults
= 0; }
140 for (patp
= patv
; (pat
= *patp
); patp
++) {
143 if (*pat
== '!' && (flags
& FILTF_WILDCARD
)) { acceptp
= 0; pat
++; }
144 else if (*pat
== '?') {
145 if (strcmp(pat
+ 1, "DEFAULTS") == 0) {
147 rc
= envvar_match(flags
, en
, defaults
, 0, ev
);
150 error("unknown pattern directive", 500);
158 printf(";; mismatch `%s' (prefix)\n", *patp
); )
161 D( if (debugmode
) printf(";; matched pattern `%s'\n", *patp
); )
163 } else if (*pat
== '*' && (flags
& FILTF_WILDCARD
)) {
167 printf(";; mismatch `%s' (discard: no `=')\n", *patp
); )
171 printf(";; wildcard match for `%s'\n", *patp
); )
174 if (*pat
++ != *q
++) {
175 D( if (debugmode
) printf(";; mismatch `%s'\n", *patp
); )
184 if (!acceptp
) return -1;
189 void filter_environment(unsigned flags
, const char *prefix_in
,
190 const char *const *patv
,
191 const char *const *defaults
,
192 void (*foundone
)(const char *fulln
,
193 const char *en
, const char *ev
,
198 char enbuf
[MAX_ENVVAR_NAME
];
199 size_t n
, pn
= strlen(prefix_in
);
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"); )
208 if (envvar_match(flags
, en
+ pn
, patv
, defaults
, &ev
) > 0) {
210 if (n
>= sizeof(enbuf
))
211 error("environment variable name too long", 500);
212 memcpy(enbuf
, en
, n
);
215 printf(";; full = `%s'; tail = `%s'; value = `%s'\n",
216 enbuf
, enbuf
+ pn
, ev
); )
217 foundone(enbuf
, enbuf
+ pn
, ev
, p
);