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>
7 * Copyright 2013,2016 Mark Wooding <mdw@distorted.org.uk>
9 * This is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with userv-utils; if not, see http://www.gnu.org/licenses/.
36 static void outerror(void) {
38 exit(debugmode ?
0 : -1);
41 void syserror(const char *m
) {
42 if (printf("Content-Type: text/plain\n"
44 "ucgi: system call error:\n"
46 m
,strerror(errno
))==EOF
|| fflush(stdout
)) outerror();
50 void error(const char *m
, int st
) {
51 if (printf("Content-Type: text/plain\n"
55 st
, m
)==EOF
|| fflush(stdout
)) outerror();
59 void *xmalloc(size_t sz
) {
63 if (!r
) syserror("malloc failed");
67 void *xrealloc(void *ptr
, size_t sz
) {
71 if (!r
) syserror("realloc failed");
75 void xsetenv(const char *en
, const char *ev
, int overwrite
) {
76 if (setenv(en
,ev
,overwrite
)) syserror("setenv");
79 const char **load_filters(unsigned flags
, const char *first
, ...) {
81 const char *name
, *p
, *q
, **v
;
85 char buf
[MAX_ENVVAR_NAME
];
87 D( if (debugmode
) printf(";; load_filters...\n"); )
89 for (name
= first
; name
; name
= va_arg(ap
, const char *)) {
90 fp
= fopen(name
, "r"); if (fp
) goto opened
;
92 printf(";; file `%s': %s\n", name
, strerror(errno
)); )
93 if (errno
!= ENOENT
) syserror("failed to open environment filters");
96 if (flags
& LOADF_MUST
) syserror("failed to open environment filters");
97 D( if (debugmode
) printf(";; using default filters\n"); )
102 D( if (debugmode
) printf(";; file `%s': OK\n", name
); )
104 n
= 0; sz
= 128; v
= xmalloc(sz
* sizeof(*v
));
106 if (!fgets(buf
, sizeof(buf
), fp
)) break;
108 if (buf
[l
- 1] == '\n') buf
[--l
]= 0;
109 if (l
+ 1 == sizeof(buf
))
110 error("line too long in environment filter file", 500);
112 while (isspace((unsigned char)*p
)) p
++;
113 while (q
> p
&& isspace((unsigned char)q
[-1])) q
--;
114 if (*p
== '#' || p
== q
) continue;
120 D( if (debugmode
) printf(";; filter: `%s'\n", pp
); )
123 v
= xrealloc(v
, sz
* sizeof(*v
));
126 if (ferror(fp
)) syserror("failed to read environment filters");
131 static int envvar_match(unsigned flags
, const char *en
,
132 const char *const *patv
,
133 const char *const *defaults
,
135 const char *const *patp
;
140 if (!patv
) { patv
= defaults
; defaults
= 0; }
141 for (patp
= patv
; (pat
= *patp
); patp
++) {
144 if (*pat
== '!' && (flags
& FILTF_WILDCARD
)) { acceptp
= 0; pat
++; }
145 else if (*pat
== '?') {
146 if (strcmp(pat
+ 1, "DEFAULTS") == 0) {
148 rc
= envvar_match(flags
, en
, defaults
, 0, ev
);
151 error("unknown pattern directive", 500);
159 printf(";; mismatch `%s' (prefix)\n", *patp
); )
162 D( if (debugmode
) printf(";; matched pattern `%s'\n", *patp
); )
164 } else if (*pat
== '*' && (flags
& FILTF_WILDCARD
)) {
168 printf(";; mismatch `%s' (discard: no `=')\n", *patp
); )
172 printf(";; wildcard match for `%s'\n", *patp
); )
175 if (*pat
++ != *q
++) {
176 D( if (debugmode
) printf(";; mismatch `%s'\n", *patp
); )
185 if (!acceptp
) return -1;
190 void filter_environment(unsigned flags
, const char *prefix_in
,
191 const char *const *patv
,
192 const char *const *defaults
,
193 void (*foundone
)(const char *fulln
,
194 const char *en
, const char *ev
,
199 char enbuf
[MAX_ENVVAR_NAME
];
200 size_t n
, pn
= strlen(prefix_in
);
202 D( if (debugmode
) printf(";; filter_environment...\n"); )
203 for (ep
= environ
; (en
= *ep
); ep
++) {
204 D( if (debugmode
) printf(";; consider env-var `%s'\n", en
); )
205 if (strncmp(en
, prefix_in
, pn
) != 0 || !en
[pn
]) {
206 D( if (debugmode
) printf(";; doesn't match prefix\n"); )
209 if (envvar_match(flags
, en
+ pn
, patv
, defaults
, &ev
) > 0) {
211 if (n
>= sizeof(enbuf
))
212 error("environment variable name too long", 500);
213 memcpy(enbuf
, en
, n
);
216 printf(";; full = `%s'; tail = `%s'; value = `%s'\n",
217 enbuf
, enbuf
+ pn
, ev
); )
218 foundone(enbuf
, enbuf
+ pn
, ev
, p
);