chiark / gitweb /
9780e360f3f17b371f01d13db05ea49edf5a71f7
[userv-utils.git] / www-cgi / ucgitarget.c
1 /*
2  * Usage: as CGI script, but called by userv
3  * environment variables are USERV_U_E_...
4  */
5 /*
6  * Copyright (C) 1998-1999,2003 Ian Jackson
7  *
8  * This is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with userv-utils; if not, write to the Free Software
20  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  *
22  * $Id$
23  */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <sys/stat.h>
32
33 #include "ucgi.h"
34
35 static const char *const envok[]= {
36   "AUTH_TYPE",
37   "CONTENT_LENGTH",
38   "CONTENT_TYPE",
39   "DOCUMENT_ROOT",
40   "GATEWAY_INTERFACE",
41   "HTTP_ACCEPT",
42   "HTTP_ACCEPT_CHARSET",
43   "HTTP_ACCEPT_ENCODING",
44   "HTTP_ACCEPT_LANGUAGE",
45   "HTTP_CACHE_CONTROL",
46   "HTTP_CONNECTION",
47   "HTTP_CONTENT_ENCODING",
48   "HTTP_COOKIE",
49   "HTTP_DNT",
50   "HTTP_HOST",
51   "HTTP_KEEP_ALIVE",
52   "HTTP_NEGOTIATE",
53   "HTTP_PRAGMA",
54   "HTTP_REFERER",
55   "HTTP_USER_AGENT",
56   "HTTP_VIA",
57   "HTTP_X_FORWARDED_FOR",
58   "HTTPS",
59   "PATH_INFO",
60   "PATH_TRANSLATED",
61   "QUERY_STRING",
62   "REMOTE_ADDR",
63   "REMOTE_HOST",
64   "REMOTE_USER",
65   "REMOTE_IDENT",
66   "REQUEST_METHOD",
67   "REQUEST_URI",
68   "SCRIPT_FILENAME",
69   "SCRIPT_NAME",
70   "SCRIPT_URI",
71   "SCRIPT_URL",
72   "SERVER_ADDR",
73   "SERVER_ADMIN",
74   "SERVER_NAME",
75   "SERVER_PORT",
76   "SERVER_PROTOCOL",
77   "SERVER_SIGNATURE",
78   "SERVER_SOFTWARE",
79   0
80 };
81
82 static void setenvar(const char *fulln,
83                      const char *en, const char *ep, void *p) {
84   xsetenv(en, ep, 1);
85   unsetenv(fulln);
86 }
87
88 int main(int argc, const char **argv) {
89   char *scriptpath, *newvar;
90   const char *nextslash, *lastslash, *pathi, *ev, *ev2, *scriptdir, *av;
91   const char **arguments;
92   size_t scriptdirlen, scriptpathlen, l;
93   struct stat stab;
94   int r, nargs;
95
96   ev= getenv("USERV_U_DEBUG");
97   if (ev && *ev) debugmode= 1;
98   
99   D( if (debugmode) printf(";;; UCGITARGET\n"); )
100   if (argc > MAX_ARGS) error("too many arguments");
101
102   if (!*++argv) error("no script directory argument");
103   ev= getenv("HOME"); if (!ev) error("no HOME env. var");
104   l= strlen(*argv)+strlen(ev);
105   newvar= xmalloc(l+2);
106   sprintf(newvar,"%s/%s",ev,*argv);
107   scriptdir= newvar;
108   scriptdirlen= strlen(scriptdir);
109
110   filter_environment(0, "USERV_U_E_", envok, setenvar, 0);
111
112   scriptpath= 0;
113   pathi= getenv("PATH_INFO");
114   if (!pathi) error("PATH_INFO not found");
115   lastslash= pathi;
116   D( if (debugmode) {
117        printf(";; find script name...\n"
118               ";;   PATH_INFO = `%s'\n",
119               pathi);
120   } )
121   for (;;) {
122     if (*lastslash != '/') error("PATH_INFO expected slash not found");
123     if (lastslash[1]=='.' || lastslash[1]=='#' || !lastslash[1]) error("bad char begin");
124     nextslash= strchr(lastslash+1,'/');
125     if (!nextslash) nextslash= lastslash+1+strlen(lastslash+1);
126     if (!nextslash) error("insufficient elements in PATH_INFO");
127     if (nextslash==lastslash+1) error("empty component in PATH_INFO");
128     if (nextslash-pathi > MAX_SCRIPTPATH_LEN) error("PATH_INFO script path too long");
129     scriptpathlen= scriptdirlen+(nextslash-pathi);
130     scriptpath= xrealloc(scriptpath,scriptpathlen+1);
131     strcpy(scriptpath,scriptdir);
132     memcpy(scriptpath+scriptdirlen,pathi,nextslash-pathi);
133     scriptpath[scriptpathlen]= 0;
134     if (scriptpath[scriptpathlen-1]=='~') error("bad char end");
135     D( if (debugmode) printf(";;   try `%s'\n", scriptpath); )
136     r= stat(scriptpath,&stab); if (r) syserror("stat script");
137     if (S_ISREG(stab.st_mode)) break;
138     if (!S_ISDIR(stab.st_mode)) error("script not directory or file");
139     lastslash= nextslash;
140   }
141   D( if (debugmode) printf(";;   found script: tail = `%s'\n", nextslash); )
142   if (*nextslash) xsetenv("PATH_INFO",nextslash,1);
143   else unsetenv("PATH_INFO");
144
145   newvar= xmalloc(scriptpathlen+strlen(nextslash)+3);
146   sprintf(newvar,"%s%s",scriptpath,nextslash);
147   xsetenv("PATH_TRANSLATED",newvar,1);
148
149   xsetenv("SCRIPT_FILENAME",scriptpath,1);
150
151   ev= getenv("SCRIPT_NAME");
152   if (ev) {
153     ev2= getenv("USER"); if (!ev2) error("no USER variable");
154     newvar= xmalloc(strlen(ev)+2+strlen(ev2)+scriptpathlen-scriptdirlen+2);
155     sprintf(newvar,"%s/~%s%s",ev,ev2,scriptpath+scriptdirlen);
156     xsetenv("SCRIPT_NAME",newvar,1);
157   }
158
159   arguments= xmalloc(sizeof(const char*)*(argc+5));
160   nargs= 0;
161   
162   arguments[nargs++]= scriptpath;
163   while ((av= (*++argv))) arguments[nargs++]= av;
164   arguments[nargs++]= 0;
165
166   D( if (debugmode) {
167        int i;
168
169        printf(";; final environment...\n");
170        for (i = 0; environ[i]; i++)
171          printf(";;   %s\n", environ[i]);
172
173        printf(";; final command line...\n");
174        for (i = 0; arguments[i]; i++)
175          printf(";;   %s\n", arguments[i]);
176        fflush(stdout);
177   } )
178
179   execvp(scriptpath,(char*const*)arguments);
180   syserror("exec script");
181   return -1;
182 }