summaryrefslogtreecommitdiff
path: root/ucgi/ucgicommon.c
blob: c3eb4b3e2bae64a33d2c32e23709aafe55f62811 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * Copyright 1996-2013,2016 Ian Jackson <ijackson@chiark.greenend.org.uk>
 * Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
 * Copyright 1999,2003
 *    Chancellor Masters and Scholars of the University of Cambridge
 * Copyright 2010 Tony Finch <fanf@dotat.at>
 * Copyright 2013,2016 Mark Wooding <mdw@distorted.org.uk>
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with userv-utils; if not, see http://www.gnu.org/licenses/.
 */

#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <unistd.h>

#include "ucgi.h"

int debugmode= 0;

static void outerror(void) {
  perror("stdout");
  exit(debugmode ? 0 : -1);
}

void syserror(const char *m) {
  if (printf("Content-Type: text/plain\n"
	     "Status: 500\n\n"
	     "ucgi: system call error:\n"
	     "%s: %s\n",
	     m,strerror(errno))==EOF || fflush(stdout)) outerror();
  exit(0);
}

void error(const char *m, int st) {
  if (printf("Content-Type: text/plain\n"
	     "Status: %d\n\n"
	     "ucgi: error:\n"
	     "%s\n",
	     st, m)==EOF || fflush(stdout)) outerror();
  exit(0);
}

void *xmalloc(size_t sz) {
  void *r;

  r= malloc(sz);
  if (!r) syserror("malloc failed");
  return r;
}

void *xrealloc(void *ptr, size_t sz) {
  void *r;

  r= realloc(ptr,sz);
  if (!r) syserror("realloc failed");
  return r;
}

void xsetenv(const char *en, const char *ev, int overwrite) {
  if (setenv(en,ev,overwrite)) syserror("setenv");
}

const char **load_filters(unsigned flags, const char *first, ...) {
  va_list ap;
  const char *name, *p, *q, **v;
  char *pp;
  size_t l, n, sz;
  FILE *fp;
  char buf[MAX_ENVVAR_NAME];

  D( if (debugmode) printf(";; load_filters...\n"); )
  va_start(ap, first);
  for (name= first; name; name= va_arg(ap, const char *)) {
    fp= fopen(name, "r"); if (fp) goto opened;
    D( if (debugmode)
	 printf(";;   file `%s': %s\n", name, strerror(errno)); )
    if (errno != ENOENT) syserror("failed to open environment filters");
  }
  va_end(ap);
  if (flags & LOADF_MUST) syserror("failed to open environment filters");
  D( if (debugmode) printf(";;   using default filters\n"); )
  return 0;

opened:
  va_end(ap);
  D( if (debugmode) printf(";;   file `%s': OK\n", name); )

  n= 0; sz= 128; v= xmalloc(sz * sizeof(*v));
  for (;;) {
    if (!fgets(buf, sizeof(buf), fp)) break;
    l= strlen(buf);
    if (buf[l - 1] == '\n') buf[--l]= 0;
    if (l + 1 == sizeof(buf))
      error("line too long in environment filter file", 500);
    p= buf; q= p + l;
    while (isspace((unsigned char)*p)) p++;
    while (q > p && isspace((unsigned char)q[-1])) q--;
    if (*p == '#' || p == q) continue;
    l= q - p;
    pp= xmalloc(l + 1);
    memcpy(pp, p, l);
    pp[l]= 0;
    v[n++]= pp;
    D( if (debugmode) printf(";;   filter: `%s'\n", pp); )
    if (n >= sz) {
      sz *= 2;
      v= xrealloc(v, sz * sizeof(*v));
    }
  }
  if (ferror(fp)) syserror("failed to read environment filters");
  fclose(fp);
  return v;
}

static int envvar_match(unsigned flags, const char *en,
			const char *const *patv,
			const char *const *defaults,
			const char **ev) {
  const char *const *patp;
  const char *q, *pat;
  int acceptp;
  int rc;

  if (!patv) { patv= defaults; defaults= 0; }
  for (patp= patv; (pat= *patp); patp++) {
    q= en;
    acceptp= 1;
    if (*pat == '!' && (flags & FILTF_WILDCARD)) { acceptp= 0; pat++; }
    else if (*pat == '?') {
      if (strcmp(pat + 1, "DEFAULTS") == 0) {
	assert(defaults);
	rc= envvar_match(flags, en, defaults, 0, ev);
	if (rc) return rc;
      } else
	error("unknown pattern directive", 500);
      continue;
    }

    for (;;) {
      if (!*pat) {
	if (*q != '=') {
	  D( if (debugmode)
	       printf(";;     mismatch `%s' (prefix)\n", *patp); )
	  break;
	}
	D( if (debugmode) printf(";;     matched pattern `%s'\n", *patp); )
	goto match;
      } else if (*pat == '*' && (flags & FILTF_WILDCARD)) {
	q = strchr(q, '=');
	if (!q) {
	  D( if (debugmode)
	       printf(";;     mismatch `%s' (discard: no `=')\n", *patp); )
	  return -1;
	}
	D( if (debugmode)
	     printf(";;     wildcard match for `%s'\n", *patp); )
	goto match;
      } else {
	if (*pat++ != *q++) {
	  D( if (debugmode) printf(";;     mismatch `%s'\n", *patp); )
	  break;
	}
      }
    }
  }
  return 0;

match:
  if (!acceptp) return -1;
  *ev= q + 1;
  return +1;
}

void filter_environment(unsigned flags, const char *prefix_in,
			const char *const *patv,
			const char *const *defaults,
			void (*foundone)(const char *fulln,
					 const char *en, const char *ev,
					 void *p),
			void *p) {
  char *const *ep;
  const char *en, *ev;
  char enbuf[MAX_ENVVAR_NAME];
  size_t n, pn = strlen(prefix_in);

  D( if (debugmode) printf(";; filter_environment...\n"); )
  for (ep= environ; (en= *ep); ep++) {
    D( if (debugmode) printf(";;   consider env-var `%s'\n", en); )
    if (strncmp(en, prefix_in, pn) != 0 || !en[pn]) {
      D( if (debugmode) printf(";;     doesn't match prefix\n"); )
      continue;
    }
    if (envvar_match(flags, en + pn, patv, defaults, &ev) > 0) {
      n= strcspn(en, "=");
      if (n >= sizeof(enbuf))
	error("environment variable name too long", 500);
      memcpy(enbuf, en, n);
      enbuf[n]= 0;
      D( if (debugmode)
	   printf(";;     full = `%s'; tail = `%s'; value = `%s'\n",
		  enbuf, enbuf + pn, ev); )
      foundone(enbuf, enbuf + pn, ev, p);
    }
  }
}