chiark / gitweb /
www-cgi/: Centralize environment variable filtering.
[userv-utils] / 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 <stdio.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #include <unistd.h>
26
27 #include "ucgi.h"
28
29 const char *const envok[]= {
30   "AUTH_TYPE",
31   "CONTENT_LENGTH",
32   "CONTENT_TYPE",
33   "DOCUMENT_ROOT",
34   "GATEWAY_INTERFACE",
35   "HTTP_ACCEPT",
36   "HTTP_ACCEPT_CHARSET",
37   "HTTP_ACCEPT_ENCODING",
38   "HTTP_ACCEPT_LANGUAGE",
39   "HTTP_CACHE_CONTROL",
40   "HTTP_CONNECTION",
41   "HTTP_CONTENT_ENCODING",
42   "HTTP_COOKIE",
43   "HTTP_DNT",
44   "HTTP_HOST",
45   "HTTP_KEEP_ALIVE",
46   "HTTP_NEGOTIATE",
47   "HTTP_PRAGMA",
48   "HTTP_REFERER",
49   "HTTP_USER_AGENT",
50   "HTTP_VIA",
51   "HTTP_X_FORWARDED_FOR",
52   "HTTPS",
53   "PATH_INFO",
54   "PATH_TRANSLATED",
55   "QUERY_STRING",
56   "REMOTE_ADDR",
57   "REMOTE_HOST",
58   "REMOTE_USER",
59   "REMOTE_IDENT",
60   "REQUEST_METHOD",
61   "REQUEST_URI",
62   "SCRIPT_FILENAME",
63   "SCRIPT_NAME",
64   "SCRIPT_URI",
65   "SCRIPT_URL",
66   "SERVER_ADDR",
67   "SERVER_ADMIN",
68   "SERVER_NAME",
69   "SERVER_PORT",
70   "SERVER_PROTOCOL",
71   "SERVER_SIGNATURE",
72   "SERVER_SOFTWARE",
73   0
74 };
75 const int nenvok= sizeof(envok)/sizeof(envok[0]);
76
77 int debugmode= 0;
78
79 static void outerror(void) {
80   perror("stdout");
81   exit(debugmode ? 0 : -1);
82 }
83
84 void syserror(const char *m) {
85   if (printf("Content-Type: text/plain\n\n"
86              "ucgi: system call error:\n"
87              "%s: %s\n",
88              m,strerror(errno))==EOF || fflush(stdout)) outerror();
89   exit(0);
90 }
91
92 void error(const char *m) {
93   if (printf("Content-Type: text/plain\n\n"
94              "ucgi: error:\n"
95              "%s\n",
96              m)==EOF || fflush(stdout)) outerror();
97   exit(0);
98 }
99
100 void *xmalloc(size_t sz) {
101   void *r;
102
103   r= malloc(sz);
104   if (!r) syserror("malloc failed");
105   return r;
106 }
107
108 void *xrealloc(void *ptr, size_t sz) {
109   void *r;
110
111   r= realloc(ptr,sz);
112   if (!r) syserror("realloc failed");
113   return r;
114 }
115
116 void xsetenv(const char *en, const char *ev, int overwrite) {
117   if (setenv(en,ev,overwrite)) syserror("setenv");
118 }
119
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,
124                                          void *p),
125                         void *p)
126 {
127   char *const *ep;
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);
132   int acceptp;
133
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"); )
139       goto next_ev;
140     }
141     for (patp= patv; (pat= *patp); patp++) {
142       q= en + pn;
143       acceptp= 1;
144       if (*pat == '!' && (flags & FILTF_WILDCARD)) {
145         acceptp= 0; pat++;
146       }
147       for (;;) {
148         if (!*pat) {
149           if (*q != '=') {
150             D( if (debugmode)
151                  printf(";;     mismatch `%s' (prefix)\n", *patp); )
152             goto next_pat;
153           }
154           D( if (debugmode) printf(";;     matched `%s'\n", *patp); )
155           ev = q + 1;
156           break;
157         } else if (*pat == '*' && (flags & FILTF_WILDCARD)) {
158           q = strchr(q, '=');
159           if (!q) {
160             D( if (debugmode)
161                  printf(";;     mismatch `%s' (discard: no `=')\n", *patp); )
162             goto next_ev;
163           }
164           D( if (debugmode)
165                printf(";;     wildcard match for `%s'\n", *patp); )
166           ev = q + 1;
167           break;
168         } else
169           if (*pat++ != *q++) {
170             D( if (debugmode) printf(";;     mismatch `%s'\n", *patp); )
171             goto next_pat;
172           }
173       }
174       if (acceptp) {
175         n= q - en;
176         if (n >= sizeof(enbuf))
177           error("environment variable name too long");
178         memcpy(enbuf, en, n);
179         enbuf[n]= 0;
180         D( if (debugmode)
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"); )
186       goto next_ev;
187     next_pat:;
188     }
189   next_ev:;
190   }
191 }