chiark / gitweb /
Found in davenant:/usr/local/src/misc
[userv-utils.git] / www-cgi / ucgi.c
1 /*
2  * Usage: as CGI script
3  */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <ctype.h>
8 #include <unistd.h>
9 #include <sys/types.h>
10 #include <sys/wait.h>
11
12 #include "ucgi.h"
13
14 int main(int argc, const char **argv) {
15   char *defarg, *username;
16   const char *slash2, *pathi, *ev, *en, *av;
17   const char *const *ep;
18   const char **arguments;
19   size_t usernamelen, l;
20   pid_t child, rchild;
21   int nargs, status;
22
23   l= strlen(argv[0]);
24   if (l>6 && !strcmp(argv[0]+l-6,"-debug")) debugmode= 1;
25
26   if (debugmode) {
27     if (fputs("Content-Type: text/plain\n\n",stdout)==EOF || fflush(stdout))
28       syserror("write stdout");
29     if (dup2(1,2)<0) { perror("dup stdout to stderr"); exit(-1); }
30   }
31   
32   if (argc > MAX_ARGS) error("too many arguments");
33
34   pathi= getenv("PATH_INFO");
35   if (!pathi) error("PATH_INFO not found");
36   if (pathi[0] != '/' || pathi[1] != '~') error("PATH_INFO must start with /~");
37   slash2= strchr(pathi+2,'/'); if (!slash2) error("PATH_INFO must have more than one /");
38   usernamelen= slash2-(pathi+2);
39   if (usernamelen > MAX_USERNAME_LEN) error("PATH_INFO username too long");
40   username= xmalloc(usernamelen+1);
41   memcpy(username,pathi+2,usernamelen); username[usernamelen]= 0;
42   if (!isalpha(username[0])) error("username 1st character is not alphabetic");
43   xsetenv("PATH_INFO",slash2,1);
44   
45   arguments= xmalloc(sizeof(const char*)*(nenvok+argc+10));
46   nargs= 0;
47   
48   arguments[nargs++]= "userv";
49   if (debugmode) arguments[nargs++]= "-DDEBUG=1";
50   
51   for (ep= envok; (en= *ep); ep++) {
52     ev= getenv(en); if (!ev) continue;
53     l= strlen(ev); if (l > MAX_ENVVAR_VALUE) error("environment variable too long");
54     defarg= xmalloc(strlen(en)+l+6);
55     sprintf(defarg,"-DE_%s=%s",en,ev);
56     arguments[nargs++]= defarg;
57   }
58
59   arguments[nargs++]= username;
60   arguments[nargs++]= "www-cgi";
61   while ((av= (*++argv))) arguments[nargs++]= av;
62   arguments[nargs++]= 0;
63
64   if (debugmode) {
65     child= fork(); if (child==-1) syserror("fork");
66     if (child) {
67       rchild= waitpid(child,&status,0);
68       if (rchild==-1) syserror("waitpid");
69       printf("\nexit status %d %d\n",(status>>8)&0x0ff,status&0x0ff);
70       exit(0);
71     }
72   }
73       
74   execvp("userv",(char*const*)arguments);
75   syserror("exec userv");
76   return -1;
77 }