chiark / gitweb /
a2ebe64a4b0e18d9247216bfd71c4b6965d5069e
[userv-utils.git] / www-cgi / ucgi.c
1 /*
2  * Usage: as CGI script
3  */
4 /*
5  * Copyright (C) 1998-1999,2003 Ian Jackson
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with userv-utils; if not, write to the Free Software
19  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  *
21  * $Id$
22  */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30
31 #include "ucgi.h"
32
33 static const char *const default_envok[] = {
34   "AUTH_TYPE",
35   "CONTENT_TYPE",
36   "CONTENT_LENGTH",
37   "DOCUMENT_ROOT",
38   "GATEWAY_INTERFACE",
39   "HTTP_*",
40   "HTTPS",
41   "PATH_INFO",
42   "PATH_TRANSLATED",
43   "QUERY_STRING",
44   "REMOTE_*",
45   "REQUEST_METHOD",
46   "REQUEST_URI",
47   "SCRIPT_*",
48   "SERVER_*",
49   0
50 };
51
52 struct buildargs {
53   const char **v;
54   int n, max;
55 };
56
57 static void addarg(struct buildargs *args, const char *a) {
58   if (args->n > args->max) error("too many arguments");
59   args->v[args->n++]= a;
60 }
61
62 static void add_userv_var(const char *fulln,
63                           const char *en, const char *ev, void *p) {
64   struct buildargs *args= p;
65   size_t l;
66   char *a;
67
68   l= strlen(ev); if (l > MAX_ENVVAR_VALUE) error("environment variable too long");
69   a= xmalloc(strlen(en)+l+6);
70   sprintf(a,"-DE_%s=%s",en,ev);
71   addarg(args, a);
72 }
73
74 int main(int argc, const char **argv) {
75   char *username;
76   const char *slash2, *pathi, *ev, *av;
77   const char *const *envok;
78   size_t usernamelen, l;
79   struct buildargs args;
80   pid_t child, rchild;
81   int status;
82
83   l= strlen(argv[0]);
84   if (l>6 && !strcmp(argv[0]+l-6,"-debug")) debugmode= 1;
85
86   if (debugmode) {
87     if (fputs("Content-Type: text/plain\n\n",stdout)==EOF || fflush(stdout))
88       syserror("write stdout");
89     if (dup2(1,2)<0) { perror("dup stdout to stderr"); exit(-1); }
90     D( printf(";;; UCGI\n"); )
91   }
92   
93   if (argc > MAX_ARGS) error("too many arguments");
94
95   ev= getenv("UCGI_ENV_FILTER");
96   if (ev)
97     envok= load_filters(LOADF_MUST, ev, LF_END);
98   else {
99     envok= load_filters(0, "/etc/userv/ucgi.env-filter", LF_END);
100     if (!envok) envok= default_envok;
101   }
102
103   pathi= getenv("PATH_INFO");
104   if (!pathi) error("PATH_INFO not found");
105   D( if (debugmode) {
106        printf(";; find user name...\n"
107               ";;   initial PATH_INFO = `%s'\n",
108               pathi);
109   } )
110   if (pathi[0] != '/' || pathi[1] != '~') error("PATH_INFO must start with /~");
111   slash2= strchr(pathi+2,'/'); if (!slash2) error("PATH_INFO must have more than one /");
112   usernamelen= slash2-(pathi+2);
113   if (usernamelen > MAX_USERNAME_LEN) error("PATH_INFO username too long");
114   username= xmalloc(usernamelen+1);
115   memcpy(username,pathi+2,usernamelen); username[usernamelen]= 0;
116   D( if (debugmode)
117        printf(";;   user = `%s'; tail = `%s'\n", username, slash2); )
118   if (!isalpha(username[0])) error("username 1st character is not alphabetic");
119   xsetenv("PATH_INFO",slash2,1);
120
121   args.n= 0; args.max= argc + MAX_ENVVARS + 10;
122   args.v= xmalloc(args.max * sizeof(*args.v));
123   
124   addarg(&args, "userv");
125   if (debugmode) addarg(&args, "-DDEBUG=1");
126
127   filter_environment(FILTF_WILDCARD, "", envok, add_userv_var, &args);
128
129   addarg(&args, username);
130   addarg(&args, "www-cgi");
131   while ((av= (*++argv))) addarg(&args, av);
132   addarg(&args, 0);
133
134   if (debugmode) {
135     D( fflush(stdout); )
136     child= fork(); if (child==-1) syserror("fork");
137     if (child) {
138       rchild= waitpid(child,&status,0);
139       if (rchild==-1) syserror("waitpid");
140       printf("\nexit status %d %d\n",(status>>8)&0x0ff,status&0x0ff);
141       exit(0);
142     }
143   }
144       
145   D( if (debugmode) {
146        int i;
147
148        printf(";; final command line...\n");
149        for (i = 0; args.v[i]; i++)
150          printf(";;   %s\n", args.v[i]);
151        fflush(stdout);
152   } )
153
154   execvp("userv",(char*const*)args.v);
155   syserror("exec userv");
156   return -1;
157 }