chiark / gitweb /
www-cgi/ucgicommon.c: Split matching out from `filter_environment'.
[userv-utils.git] / www-cgi / ucgicommon.c
1 /*
2  * Copyright (C) 1998-1999,2003 Ian Jackson
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  */
20
21 #include <ctype.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include <unistd.h>
28
29 #include "ucgi.h"
30
31 int debugmode= 0;
32
33 static void outerror(void) {
34   perror("stdout");
35   exit(debugmode ? 0 : -1);
36 }
37
38 void syserror(const char *m) {
39   if (printf("Content-Type: text/plain\n\n"
40              "ucgi: system call error:\n"
41              "%s: %s\n",
42              m,strerror(errno))==EOF || fflush(stdout)) outerror();
43   exit(0);
44 }
45
46 void error(const char *m) {
47   if (printf("Content-Type: text/plain\n\n"
48              "ucgi: error:\n"
49              "%s\n",
50              m)==EOF || fflush(stdout)) outerror();
51   exit(0);
52 }
53
54 void *xmalloc(size_t sz) {
55   void *r;
56
57   r= malloc(sz);
58   if (!r) syserror("malloc failed");
59   return r;
60 }
61
62 void *xrealloc(void *ptr, size_t sz) {
63   void *r;
64
65   r= realloc(ptr,sz);
66   if (!r) syserror("realloc failed");
67   return r;
68 }
69
70 void xsetenv(const char *en, const char *ev, int overwrite) {
71   if (setenv(en,ev,overwrite)) syserror("setenv");
72 }
73
74 const char **load_filters(unsigned flags, const char *first, ...)
75 {
76   va_list ap;
77   const char *name, *p, *q, **v;
78   char *pp;
79   size_t l, n, sz;
80   FILE *fp;
81   char buf[MAX_ENVVAR_NAME];
82
83   D( if (debugmode) printf(";; load_filters...\n"); )
84   va_start(ap, first);
85   for (name= first; name; name= va_arg(ap, const char *)) {
86     fp= fopen(name, "r"); if (fp) goto opened;
87     D( if (debugmode)
88          printf(";;   file `%s': %s\n", name, strerror(errno)); )
89     if (errno != ENOENT) syserror("failed to open environment filters");
90   }
91   va_end(ap);
92   if (flags & LOADF_MUST) syserror("failed to open environment filters");
93   D( if (debugmode) printf(";;   using default filters\n"); )
94   return 0;
95
96 opened:
97   va_end(ap);
98   D( if (debugmode) printf(";;   file `%s': OK\n", name); )
99
100   n= 0; sz= 128; v= xmalloc(sz * sizeof(*v));
101   for (;;) {
102     if (!fgets(buf, sizeof(buf), fp)) break;
103     l= strlen(buf);
104     if (buf[l - 1] == '\n') buf[--l]= 0;
105     if (l + 1 == sizeof(buf))
106       error("line too long in environment filter file");
107     p= buf; q= p + l;
108     while (isspace((unsigned char)*p)) p++;
109     while (q > p && isspace((unsigned char)q[-1])) q--;
110     if (*p == '#' || p == q) continue;
111     l= q - p;
112     pp= xmalloc(l + 1);
113     memcpy(pp, p, l);
114     pp[l]= 0;
115     v[n++]= pp;
116     D( if (debugmode) printf(";;   filter: `%s'\n", pp); )
117     if (n >= sz) {
118       sz *= 2;
119       v= xrealloc(v, sz * sizeof(*v));
120     }
121   }
122   if (ferror(fp)) syserror("failed to read environment filters");
123   fclose(fp);
124   return v;
125 }
126
127 static int envvar_match(unsigned flags, const char *en,
128                         const char *const *patv,
129                         const char **ev)
130 {
131   const char *const *patp;
132   const char *q, *pat;
133   int acceptp;
134
135   for (patp= patv; (pat= *patp); patp++) {
136     q= en;
137     acceptp= 1;
138     if (*pat == '!' && (flags & FILTF_WILDCARD)) { acceptp= 0; pat++; }
139
140     for (;;) {
141       if (!*pat) {
142         if (*q != '=') {
143           D( if (debugmode)
144                printf(";;     mismatch `%s' (prefix)\n", *patp); )
145           break;
146         }
147         D( if (debugmode) printf(";;     matched pattern `%s'\n", *patp); )
148         goto match;
149       } else if (*pat == '*' && (flags & FILTF_WILDCARD)) {
150         q = strchr(q, '=');
151         if (!q) {
152           D( if (debugmode)
153                printf(";;     mismatch `%s' (discard: no `=')\n", *patp); )
154           return -1;
155         }
156         D( if (debugmode)
157              printf(";;     wildcard match for `%s'\n", *patp); )
158         goto match;
159       } else {
160         if (*pat++ != *q++) {
161           D( if (debugmode) printf(";;     mismatch `%s'\n", *patp); )
162           break;
163         }
164       }
165     }
166   }
167   return 0;
168
169 match:
170   if (!acceptp) return -1;
171   *ev= q + 1;
172   return +1;
173 }
174
175 void filter_environment(unsigned flags, const char *prefix_in,
176                         const char *const *patv,
177                         void (*foundone)(const char *fulln,
178                                          const char *en, const char *ev,
179                                          void *p),
180                         void *p)
181 {
182   char *const *ep;
183   const char *en, *ev;
184   char enbuf[MAX_ENVVAR_NAME];
185   size_t n, pn = strlen(prefix_in);
186
187   D( if (debugmode) printf(";; filter_environment...\n"); )
188   for (ep= environ; (en= *ep); ep++) {
189     D( if (debugmode) printf(";;   consider env-var `%s'\n", en); )
190     if (strncmp(en, prefix_in, pn) != 0 || !en[pn]) {
191       D( if (debugmode) printf(";;     doesn't match prefix\n"); )
192       continue;
193     }
194     if (envvar_match(flags, en + pn, patv, &ev) > 0) {
195       n= strcspn(en, "=");
196       if (n >= sizeof(enbuf))
197         error("environment variable name too long");
198       memcpy(enbuf, en, n);
199       enbuf[n]= 0;
200       D( if (debugmode)
201            printf(";;     full = `%s'; tail = `%s'; value = `%s'\n",
202                   enbuf, enbuf + pn, ev); )
203       foundone(enbuf, enbuf + pn, ev, p);
204     }
205   }
206 }