chiark / gitweb /
www-cgi/: Allow customization of the environment filters.
[userv-utils.git] / www-cgi / ucgitarget.c
index ebec06b2c19783fb0e2cdc116ed84089b47ce56a..6c579b69ff5aad5ee7ba6f1c0488f43d68b929af 100644 (file)
@@ -3,7 +3,7 @@
  * environment variables are USERV_U_E_...
  */
 /*
- * 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
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
+#include <getopt.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 
 #include "ucgi.h"
 
-static void *xrealloc(void *ptr, size_t sz) {
-  void *r;
+static const char *const default_envok[]= {
+  "AUTH_TYPE",
+  "CONTENT_LENGTH",
+  "CONTENT_TYPE",
+  "DOCUMENT_ROOT",
+  "GATEWAY_INTERFACE",
+  "HTTP_ACCEPT",
+  "HTTP_ACCEPT_CHARSET",
+  "HTTP_ACCEPT_ENCODING",
+  "HTTP_ACCEPT_LANGUAGE",
+  "HTTP_CACHE_CONTROL",
+  "HTTP_CONNECTION",
+  "HTTP_CONTENT_ENCODING",
+  "HTTP_COOKIE",
+  "HTTP_DNT",
+  "HTTP_HOST",
+  "HTTP_KEEP_ALIVE",
+  "HTTP_NEGOTIATE",
+  "HTTP_PRAGMA",
+  "HTTP_REFERER",
+  "HTTP_USER_AGENT",
+  "HTTP_VIA",
+  "HTTP_X_FORWARDED_FOR",
+  "HTTPS",
+  "PATH_INFO",
+  "PATH_TRANSLATED",
+  "QUERY_STRING",
+  "REMOTE_ADDR",
+  "REMOTE_HOST",
+  "REMOTE_USER",
+  "REMOTE_IDENT",
+  "REQUEST_METHOD",
+  "REQUEST_URI",
+  "SCRIPT_FILENAME",
+  "SCRIPT_NAME",
+  "SCRIPT_URI",
+  "SCRIPT_URL",
+  "SERVER_ADDR",
+  "SERVER_ADMIN",
+  "SERVER_NAME",
+  "SERVER_PORT",
+  "SERVER_PROTOCOL",
+  "SERVER_SIGNATURE",
+  "SERVER_SOFTWARE",
+  0
+};
 
-  r= realloc(ptr,sz);
-  if (!r) syserror("realloc failed");
-  return r;
+static void setenvar(const char *fulln,
+                    const char *en, const char *ep, void *p) {
+  xsetenv(en, ep, 1);
+  unsetenv(fulln);
 }
 
-int main(int argc, const char **argv) {
-  char *uservarn, *scriptpath, *newvar;
-  const char *nextslash, *lastslash, *pathi, *ev, *ev2, *en, *scriptdir, *av;
-  const char *const *ep;
+int main(int argc, char **argv) {
+  char *scriptpath, *newvar;
+  const char *nextslash, *lastslash, *pathi, *ev, *ev2, *scriptdir, *av;
+  const char *const *envok;
   const char **arguments;
-  size_t scriptdirlen, scriptpathlen, l, uservarnl;
+  size_t scriptdirlen, scriptpathlen, l;
   struct stat stab;
-  int r, nargs;
+  int i, r, nargs;
+  const char *filters= 0;
 
   ev= getenv("USERV_U_DEBUG");
   if (ev && *ev) debugmode= 1;
   
+  D( if (debugmode) printf(";;; UCGITARGET\n"); )
   if (argc > MAX_ARGS) error("too many arguments");
 
-  if (!*++argv) error("no script directory argument");
+  for (;;) {
+    i= getopt(argc, argv, "+e:"); if (i < 0) break;
+    switch (i) {
+    case 'e': filters= optarg; break;
+    default: error("bad command line"); break;
+    }
+  }
+  argc -= optind; argv += optind;
+
+  if (!*argv) error("no script directory argument");
   ev= getenv("HOME"); if (!ev) error("no HOME env. var");
   l= strlen(*argv)+strlen(ev);
   newvar= xmalloc(l+2);
@@ -62,22 +119,27 @@ int main(int argc, const char **argv) {
   scriptdir= newvar;
   scriptdirlen= strlen(scriptdir);
 
-  uservarn= 0;
-  uservarnl= 0;
-  for (ep= envok; (en= *ep); ep++) {
-    l= strlen(en)+11;
-    if (uservarnl<l) { uservarn= xrealloc(uservarn,l); uservarnl= l; }
-    sprintf(uservarn,"USERV_U_E_%s",en);
-    ev= getenv(uservarn); if (!ev) continue;
-    if (strlen(ev) > MAX_ENVVAR_VALUE) error("environment variable too long");
-    if (setenv(en,ev,1)) syserror("setenv");
-    unsetenv(uservarn);
+  if (filters)
+    envok= load_filters(LOADF_MUST, filters, LF_END);
+  else {
+    envok= load_filters(0,
+                       ".userv/ucgitarget.env-filter",
+                       "/etc/userv/ucgitarget.env-filter",
+                       LF_END);
+    if (!envok) envok= default_envok;
   }
 
+  filter_environment(0, "USERV_U_E_", envok, setenvar, 0);
+
   scriptpath= 0;
   pathi= getenv("PATH_INFO");
   if (!pathi) error("PATH_INFO not found");
   lastslash= pathi;
+  D( if (debugmode) {
+       printf(";; find script name...\n"
+             ";;   PATH_INFO = `%s'\n",
+             pathi);
+  } )
   for (;;) {
     if (*lastslash != '/') error("PATH_INFO expected slash not found");
     if (lastslash[1]=='.' || lastslash[1]=='#' || !lastslash[1]) error("bad char begin");
@@ -92,11 +154,13 @@ int main(int argc, const char **argv) {
     memcpy(scriptpath+scriptdirlen,pathi,nextslash-pathi);
     scriptpath[scriptpathlen]= 0;
     if (scriptpath[scriptpathlen-1]=='~') error("bad char end");
+    D( if (debugmode) printf(";;   try `%s'\n", scriptpath); )
     r= stat(scriptpath,&stab); if (r) syserror("stat script");
     if (S_ISREG(stab.st_mode)) break;
-    if (!S_ISDIR(stab.st_mode)) syserror("script not directory or file");
+    if (!S_ISDIR(stab.st_mode)) error("script not directory or file");
     lastslash= nextslash;
   }
+  D( if (debugmode) printf(";;   found script: tail = `%s'\n", nextslash); )
   if (*nextslash) xsetenv("PATH_INFO",nextslash,1);
   else unsetenv("PATH_INFO");
 
@@ -121,6 +185,19 @@ int main(int argc, const char **argv) {
   while ((av= (*++argv))) arguments[nargs++]= av;
   arguments[nargs++]= 0;
 
+  D( if (debugmode) {
+       int i;
+
+       printf(";; final environment...\n");
+       for (i = 0; environ[i]; i++)
+        printf(";;   %s\n", environ[i]);
+
+       printf(";; final command line...\n");
+       for (i = 0; arguments[i]; i++)
+        printf(";;   %s\n", arguments[i]);
+       fflush(stdout);
+  } )
+
   execvp(scriptpath,(char*const*)arguments);
   syserror("exec script");
   return -1;