chiark / gitweb /
www-cgi/: Allow customization of the environment filters.
[userv-utils.git] / www-cgi / ucgicommon.c
index 924ad0e92d46b123c614fc28f39b5a39470e6a59..db8c75d65858932034adae2f5d6ccb0d1f55b8cd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1998-1999 Ian Jackson
+ * Copyright (C) 1998-1999,2003 Ian Jackson
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by
  * $Id$
  */
 
+#include <ctype.h>
+#include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
 
-#include "ucgi.h"
+#include <unistd.h>
 
-const char *const envok[]= {
-  "CONTENT_LENGTH",
-  "CONTENT_TYPE",
-  "DOCUMENT_ROOT",
-  "GATEWAY_INTERFACE",
-  "HTTP_ACCEPT",
-  "HTTP_ACCEPT_ENCODING",
-  "HTTP_ACCEPT_LANGUAGE",
-  "HTTP_CACHE_CONTROL",
-  "HTTP_HOST",
-  "HTTP_NEGOTIATE",
-  "HTTP_PRAGMA",
-  "HTTP_USER_AGENT",
-  "PATH_INFO",
-  "PATH_TRANSLATED",
-  "QUERY_STRING",
-  "REMOTE_ADDR",
-  "REMOTE_HOST",
-  "REMOTE_USER",
-  "REMOTE_IDENT",
-  "REQUEST_METHOD",
-  "SCRIPT_FILENAME",
-  "SCRIPT_NAME",
-  "SCRIPT_URI",
-  "SCRIPT_URL",
-  "SERVER_ADMIN",
-  "SERVER_NAME",
-  "SERVER_PORT",
-  "SERVER_PROTOCOL",
-  "SERVER_SOFTWARE",
-  0
-};
-const int nenvok= sizeof(envok)/sizeof(envok[0]);
+#include "ucgi.h"
 
 int debugmode= 0;
 
@@ -89,6 +59,140 @@ void *xmalloc(size_t sz) {
   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");
+    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;
+}
+
+void filter_environment(unsigned flags, const char *prefix_in,
+                       const char *const *patv,
+                       void (*foundone)(const char *fulln,
+                                        const char *en, const char *ev,
+                                        void *p),
+                       void *p)
+{
+  char *const *ep;
+  const char *const *patp;
+  const char *en, *ev, *pat, *q;
+  char enbuf[MAX_ENVVAR_NAME];
+  size_t n, pn = strlen(prefix_in);
+  int acceptp;
+
+  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"); )
+      goto next_ev;
+    }
+    for (patp= patv; (pat= *patp); patp++) {
+      q= en + pn;
+      acceptp= 1;
+      if (*pat == '!' && (flags & FILTF_WILDCARD)) {
+       acceptp= 0; pat++;
+      }
+      for (;;) {
+       if (!*pat) {
+         if (*q != '=') {
+           D( if (debugmode)
+                printf(";;     mismatch `%s' (prefix)\n", *patp); )
+           goto next_pat;
+         }
+         D( if (debugmode) printf(";;     matched `%s'\n", *patp); )
+         ev = q + 1;
+         break;
+       } else if (*pat == '*' && (flags & FILTF_WILDCARD)) {
+         q = strchr(q, '=');
+         if (!q) {
+           D( if (debugmode)
+                printf(";;     mismatch `%s' (discard: no `=')\n", *patp); )
+           goto next_ev;
+         }
+         D( if (debugmode)
+              printf(";;     wildcard match for `%s'\n", *patp); )
+         ev = q + 1;
+         break;
+       } else
+         if (*pat++ != *q++) {
+           D( if (debugmode) printf(";;     mismatch `%s'\n", *patp); )
+           goto next_pat;
+         }
+      }
+      if (acceptp) {
+       n= q - en;
+       if (n >= sizeof(enbuf))
+         error("environment variable name too long");
+       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);
+      } D( else if (debugmode)
+            printf(";;     matched negated pattern\n"); )
+      goto next_ev;
+    next_pat:;
+    }
+  next_ev:;
+  }
+}