chiark / gitweb /
www-cgi: New program for running PHP disasters.
authorMark Wooding <mdw@distorted.org.uk>
Fri, 8 Jul 2011 12:47:51 +0000 (13:47 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 8 Jul 2011 12:49:16 +0000 (13:49 +0100)
www-cgi/Makefile
www-cgi/uphp.c [new file with mode: 0644]

index 8092368..4339e68 100644 (file)
@@ -22,13 +22,14 @@ include ../settings.make
 uslibdir=      $(libdir)/userv/cgi
 uslibcgidir=   $(uslibdir)/cgi
 
-TARGETS=       ucgi ucgitarget
+TARGETS=       ucgi uphp ucgitarget
 
 all:           $(TARGETS)
 
-OBJS=          ucgi.o ucgitarget.o ucgicommon.o
+OBJS=          ucgi.o uphp.o ucgitarget.o ucgicommon.o
 
 ucgi:          ucgi.o ucgicommon.o -lcdb
+uphp:          uphp.o ucgicommon.o -lcdb
 ucgitarget:    ucgitarget.o ucgicommon.o
 
 $(OBJS):       ucgi.h
diff --git a/www-cgi/uphp.c b/www-cgi/uphp.c
new file mode 100644 (file)
index 0000000..ab0f5ab
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+ * Usage: as CGI script
+ */
+/*
+ * 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
+ * the Free Software Foundation; either version 2 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, write to the Free Software
+ * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * $Id: ucgi.c,v 1.3 2003/07/06 20:47:12 ian Exp $
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <cdb.h>
+
+#include "ucgi.h"
+
+int conf = -1;
+
+static char *lookup(int prefix, const char *label, int len)
+{
+  char *p = 0;
+  unsigned nn;
+  int rc;
+
+  if (conf < 0 || !label) return (0);
+  if (len < 0) len = strlen(label);
+  p = xmalloc(len + 2);
+  p[0] = prefix;
+  memcpy(p + 1, label, len);
+  p[len + 1] = 0;
+  if ((rc = cdb_seek(conf, p, len + 1, &nn)) < 0)
+    error("error seeking in `" CONFIG_CDB "'");
+  free(p);
+  if (!rc) return (0);
+  p = xmalloc(nn + 1);
+  if (read(conf, p, nn) < nn)
+    error("error reading `" CONFIG_CDB "'");
+  p[nn] = 0;
+  return (p);
+}
+
+int main(int argc, const char **argv) {
+  char *defarg, *username;
+  const char *pathi, *ev, *en, *av;
+  const char *const *ep;
+  const char **arguments;
+  size_t l;
+  pid_t child, rchild;
+  int nargs, status;
+
+  l= strlen(argv[0]);
+  if (l>6 && !strcmp(argv[0]+l-6,"-debug")) debugmode= 1;
+
+  if (debugmode) {
+    if (fputs("Content-Type: text/plain\n\n",stdout)==EOF || fflush(stdout))
+      syserror("write stdout");
+    if (dup2(1,2)<0) { perror("dup stdout to stderr"); exit(-1); }
+  }
+  
+  if (argc > MAX_ARGS) error("too many arguments");
+
+  if ((conf = open(CONFIG_CDB, O_RDONLY)) < 0 && errno != ENOENT)
+    error("failed to open `" CONFIG_CDB "'");
+
+  if ((username = lookup('H', getenv("HTTP_HOST"), -1)) == 0)
+    error("unknown virtual host");
+  if (!isalpha(username[0])) error("username 1st character is not alphabetic");
+  if ((pathi = getenv("PATH_INFO")) == 0 || pathi[0] != '/')
+    xsetenv("PATH_INFO", "/php", 1);
+  else {
+    defarg = xmalloc(4 + strlen(pathi) + 1);
+    sprintf(defarg, "/php%s", pathi);
+    xsetenv("PATH_INFO", defarg, 1);
+  }
+
+  arguments= xmalloc(sizeof(const char*)*(nenvok+argc+10));
+  nargs= 0;
+  
+  arguments[nargs++]= "userv";
+  if (debugmode) arguments[nargs++]= "-DDEBUG=1";
+  
+  for (ep= envok; (en= *ep); ep++) {
+    ev= getenv(en); if (!ev) continue;
+    l= strlen(ev); if (l > MAX_ENVVAR_VALUE) error("environment variable too long");
+    defarg= xmalloc(strlen(en)+l+6);
+    sprintf(defarg,"-DE_%s=%s",en,ev);
+    arguments[nargs++]= defarg;
+  }
+
+  arguments[nargs++]= username;
+  arguments[nargs++]= "www-cgi";
+  while ((av= (*++argv))) arguments[nargs++]= av;
+  arguments[nargs++]= 0;
+
+  if (debugmode) {
+    child= fork(); if (child==-1) syserror("fork");
+    if (child) {
+      rchild= waitpid(child,&status,0);
+      if (rchild==-1) syserror("waitpid");
+      printf("\nexit status %d %d\n",(status>>8)&0x0ff,status&0x0ff);
+      exit(0);
+    }
+  }
+      
+  execvp("userv",(char*const*)arguments);
+  syserror("exec userv");
+  return -1;
+}