ipif: "include" looks for the file in the directory where "include" appears
[userv-utils] / ucgi / ucgicommon.c
CommitLineData
a33962ba 1/*
ca2efed5 2 * Copyright 1996-2013,2016 Ian Jackson <ijackson@chiark.greenend.org.uk>
9028e234
IJ
3 * Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
4 * Copyright 1999,2003
5 * Chancellor Masters and Scholars of the University of Cambridge
6 * Copyright 2010 Tony Finch <fanf@dotat.at>
93733bee 7 * Copyright 2013,2016 Mark Wooding <mdw@distorted.org.uk>
a33962ba 8 *
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
9028e234 11 * the Free Software Foundation; either version 3 of the License, or
a33962ba 12 * (at your option) any later version.
13 *
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.
18 *
19 * You should have received a copy of the GNU General Public License
9028e234 20 * along with userv-utils; if not, see http://www.gnu.org/licenses/.
a33962ba 21 */
6a580c17 22
aa0bce91 23#include <assert.h>
f7b4be5a
MW
24#include <ctype.h>
25#include <stdarg.h>
6a580c17 26#include <stdio.h>
27#include <string.h>
6aaa2ce3 28#include <errno.h>
6a580c17 29
f601a2c6 30#include <unistd.h>
6a580c17 31
6a580c17 32#include "ucgi.h"
6a580c17 33
34int debugmode= 0;
35
36static void outerror(void) {
37 perror("stdout");
38 exit(debugmode ? 0 : -1);
39}
40
41void syserror(const char *m) {
0cd9d59d
MW
42 if (printf("Content-Type: text/plain\n"
43 "Status: 500\n\n"
6a580c17 44 "ucgi: system call error:\n"
45 "%s: %s\n",
46 m,strerror(errno))==EOF || fflush(stdout)) outerror();
47 exit(0);
48}
49
0cd9d59d
MW
50void error(const char *m, int st) {
51 if (printf("Content-Type: text/plain\n"
52 "Status: %d\n\n"
6a580c17 53 "ucgi: error:\n"
54 "%s\n",
0cd9d59d 55 st, m)==EOF || fflush(stdout)) outerror();
6a580c17 56 exit(0);
57}
58
59void *xmalloc(size_t sz) {
60 void *r;
61
62 r= malloc(sz);
63 if (!r) syserror("malloc failed");
64 return r;
65}
66
44a77f48
MW
67void *xrealloc(void *ptr, size_t sz) {
68 void *r;
69
70 r= realloc(ptr,sz);
71 if (!r) syserror("realloc failed");
72 return r;
73}
74
6a580c17 75void xsetenv(const char *en, const char *ev, int overwrite) {
76 if (setenv(en,ev,overwrite)) syserror("setenv");
77}
f601a2c6 78
540ab05f 79const char **load_filters(unsigned flags, const char *first, ...) {
f7b4be5a
MW
80 va_list ap;
81 const char *name, *p, *q, **v;
82 char *pp;
83 size_t l, n, sz;
84 FILE *fp;
85 char buf[MAX_ENVVAR_NAME];
86
87 D( if (debugmode) printf(";; load_filters...\n"); )
88 va_start(ap, first);
89 for (name= first; name; name= va_arg(ap, const char *)) {
90 fp= fopen(name, "r"); if (fp) goto opened;
91 D( if (debugmode)
92 printf(";; file `%s': %s\n", name, strerror(errno)); )
93 if (errno != ENOENT) syserror("failed to open environment filters");
94 }
95 va_end(ap);
96 if (flags & LOADF_MUST) syserror("failed to open environment filters");
97 D( if (debugmode) printf(";; using default filters\n"); )
98 return 0;
99
100opened:
101 va_end(ap);
102 D( if (debugmode) printf(";; file `%s': OK\n", name); )
103
104 n= 0; sz= 128; v= xmalloc(sz * sizeof(*v));
105 for (;;) {
106 if (!fgets(buf, sizeof(buf), fp)) break;
107 l= strlen(buf);
108 if (buf[l - 1] == '\n') buf[--l]= 0;
109 if (l + 1 == sizeof(buf))
0cd9d59d 110 error("line too long in environment filter file", 500);
f7b4be5a
MW
111 p= buf; q= p + l;
112 while (isspace((unsigned char)*p)) p++;
113 while (q > p && isspace((unsigned char)q[-1])) q--;
114 if (*p == '#' || p == q) continue;
115 l= q - p;
116 pp= xmalloc(l + 1);
117 memcpy(pp, p, l);
118 pp[l]= 0;
119 v[n++]= pp;
120 D( if (debugmode) printf(";; filter: `%s'\n", pp); )
121 if (n >= sz) {
122 sz *= 2;
123 v= xrealloc(v, sz * sizeof(*v));
124 }
125 }
126 if (ferror(fp)) syserror("failed to read environment filters");
127 fclose(fp);
128 return v;
129}
130
92fc834e
MW
131static int envvar_match(unsigned flags, const char *en,
132 const char *const *patv,
aa0bce91 133 const char *const *defaults,
540ab05f 134 const char **ev) {
92fc834e
MW
135 const char *const *patp;
136 const char *q, *pat;
137 int acceptp;
aa0bce91 138 int rc;
92fc834e 139
aa0bce91 140 if (!patv) { patv= defaults; defaults= 0; }
92fc834e
MW
141 for (patp= patv; (pat= *patp); patp++) {
142 q= en;
143 acceptp= 1;
144 if (*pat == '!' && (flags & FILTF_WILDCARD)) { acceptp= 0; pat++; }
aa0bce91
MW
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
0cd9d59d 151 error("unknown pattern directive", 500);
aa0bce91
MW
152 continue;
153 }
92fc834e
MW
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
184match:
185 if (!acceptp) return -1;
186 *ev= q + 1;
187 return +1;
188}
189
f601a2c6
MW
190void filter_environment(unsigned flags, const char *prefix_in,
191 const char *const *patv,
aa0bce91 192 const char *const *defaults,
f601a2c6
MW
193 void (*foundone)(const char *fulln,
194 const char *en, const char *ev,
195 void *p),
540ab05f 196 void *p) {
f601a2c6 197 char *const *ep;
92fc834e 198 const char *en, *ev;
f601a2c6
MW
199 char enbuf[MAX_ENVVAR_NAME];
200 size_t n, pn = strlen(prefix_in);
f601a2c6
MW
201
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"); )
92fc834e 207 continue;
f601a2c6 208 }
aa0bce91 209 if (envvar_match(flags, en + pn, patv, defaults, &ev) > 0) {
92fc834e
MW
210 n= strcspn(en, "=");
211 if (n >= sizeof(enbuf))
0cd9d59d 212 error("environment variable name too long", 500);
92fc834e
MW
213 memcpy(enbuf, en, n);
214 enbuf[n]= 0;
215 D( if (debugmode)
216 printf(";; full = `%s'; tail = `%s'; value = `%s'\n",
217 enbuf, enbuf + pn, ev); )
218 foundone(enbuf, enbuf + pn, ev, p);
f601a2c6 219 }
f601a2c6
MW
220 }
221}