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