chiark / gitweb /
Merge branch 'mdw+fixes'
[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 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
7  * Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
8  * Copyright 1999,2003
9  *    Chancellor Masters and Scholars of the University of Cambridge
10  * Copyright 2010 Tony Finch <fanf@dotat.at>
11  *
12  * This is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with userv-utils; if not, see http://www.gnu.org/licenses/.
24  */
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <getopt.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <sys/stat.h>
34
35 #include "ucgi.h"
36
37 static const char *const default_envok[]= {
38   "AUTH_TYPE",
39   "CONTENT_LENGTH",
40   "CONTENT_TYPE",
41   "DOCUMENT_ROOT",
42   "GATEWAY_INTERFACE",
43   "HTTP_ACCEPT",
44   "HTTP_ACCEPT_CHARSET",
45   "HTTP_ACCEPT_ENCODING",
46   "HTTP_ACCEPT_LANGUAGE",
47   "HTTP_CACHE_CONTROL",
48   "HTTP_CONNECTION",
49   "HTTP_CONTENT_ENCODING",
50   "HTTP_COOKIE",
51   "HTTP_DNT",
52   "HTTP_HOST",
53   "HTTP_KEEP_ALIVE",
54   "HTTP_NEGOTIATE",
55   "HTTP_PRAGMA",
56   "HTTP_REFERER",
57   "HTTP_USER_AGENT",
58   "HTTP_VIA",
59   "HTTP_X_FORWARDED_FOR",
60   "HTTPS",
61   "PATH_INFO",
62   "PATH_TRANSLATED",
63   "QUERY_STRING",
64   "REDIRECT_HANDLER",
65   "REDIRECT_SCRIPT_URI",
66   "REDIRECT_SCRIPT_URL",
67   "REDIRECT_STATUS",
68   "REDIRECT_URL",
69   "REMOTE_ADDR",
70   "REMOTE_HOST",
71   "REMOTE_USER",
72   "REMOTE_IDENT",
73   "REQUEST_METHOD",
74   "REQUEST_URI",
75   "SCRIPT_FILENAME",
76   "SCRIPT_NAME",
77   "SCRIPT_URI",
78   "SCRIPT_URL",
79   "SERVER_ADDR",
80   "SERVER_ADMIN",
81   "SERVER_NAME",
82   "SERVER_PORT",
83   "SERVER_PROTOCOL",
84   "SERVER_SIGNATURE",
85   "SERVER_SOFTWARE",
86   "SSL_CIPHER",
87   "SSL_CLIENT_S_DN",
88   "SSL_CLIENT_VERIFY",
89   "SSL_PROTOCOL",
90   0
91 };
92
93 static void setenvar(const char *fulln,
94                      const char *en, const char *ep, void *p) {
95   xsetenv(en, ep, 1);
96   unsetenv(fulln);
97 }
98
99 int main(int argc, char **argv) {
100   char *scriptpath, *newvar;
101   const char *nextslash, *lastslash, *pathi, *ev, *ev2, *scriptdir, *av;
102   const char *const *envok;
103   const char **arguments;
104   size_t scriptdirlen, scriptpathlen, l;
105   struct stat stab;
106   int i, r, nargs;
107   const char *filters= 0;
108
109   ev= getenv("USERV_U_DEBUG");
110   if (ev && *ev) debugmode= 1;
111   
112   D( if (debugmode) printf(";;; UCGITARGET\n"); )
113   if (argc > MAX_ARGS) error("too many arguments", 500);
114
115   for (;;) {
116     i= getopt(argc, argv, "+e:"); if (i < 0) break;
117     switch (i) {
118     case 'e': filters= optarg; break;
119     default: error("bad command line", 500); break;
120     }
121   }
122   argc -= optind; argv += optind;
123
124   if (!*argv) error("no script directory argument", 500);
125   ev= getenv("HOME"); if (!ev) error("no HOME env. var", 500);
126   l= strlen(*argv)+strlen(ev);
127   newvar= xmalloc(l+2);
128   sprintf(newvar,"%s/%s",ev,*argv);
129   scriptdir= newvar;
130   scriptdirlen= strlen(scriptdir);
131
132   if (filters)
133     envok= load_filters(LOADF_MUST, filters, LF_END);
134   else {
135     envok= load_filters(0,
136                         ".userv/ucgitarget.env-filter",
137                         "/etc/userv/ucgitarget.env-filter",
138                         LF_END);
139   }
140
141   filter_environment(0, "USERV_U_E_", envok, default_envok, setenvar, 0);
142
143   scriptpath= 0;
144   pathi= getenv("PATH_INFO");
145   if (!pathi) error("PATH_INFO not found", 500);
146   lastslash= pathi;
147   D( if (debugmode) {
148        printf(";; find script name...\n"
149               ";;   PATH_INFO = `%s'\n",
150               pathi);
151   } )
152   for (;;) {
153     if (*lastslash != '/') error("PATH_INFO expected slash not found", 400);
154     if (lastslash[1]=='.' || lastslash[1]=='#' || !lastslash[1])
155       error("bad char begin", 400);
156     nextslash= strchr(lastslash+1,'/');
157     if (!nextslash) nextslash= lastslash+1+strlen(lastslash+1);
158     if (!nextslash) error("insufficient elements in PATH_INFO", 400);
159     if (nextslash==lastslash+1) error("empty component in PATH_INFO", 400);
160     if (nextslash-pathi > MAX_SCRIPTPATH_LEN)
161       error("PATH_INFO script path too long", 400);
162     scriptpathlen= scriptdirlen+(nextslash-pathi);
163     scriptpath= xrealloc(scriptpath,scriptpathlen+1);
164     strcpy(scriptpath,scriptdir);
165     memcpy(scriptpath+scriptdirlen,pathi,nextslash-pathi);
166     scriptpath[scriptpathlen]= 0;
167     if (scriptpath[scriptpathlen-1]=='~') error("bad char end", 400);
168     D( if (debugmode) printf(";;   try `%s'\n", scriptpath); )
169     r= stat(scriptpath,&stab); if (r) syserror("stat script");
170     if (S_ISREG(stab.st_mode)) break;
171     if (!S_ISDIR(stab.st_mode)) error("script not directory or file", 500);
172     lastslash= nextslash;
173   }
174   D( if (debugmode) printf(";;   found script: tail = `%s'\n", nextslash); )
175   if (*nextslash) xsetenv("PATH_INFO",nextslash,1);
176   else unsetenv("PATH_INFO");
177
178   newvar= xmalloc(scriptpathlen+strlen(nextslash)+3);
179   sprintf(newvar,"%s%s",scriptpath,nextslash);
180   xsetenv("PATH_TRANSLATED",newvar,1);
181
182   xsetenv("SCRIPT_FILENAME",scriptpath,1);
183
184   ev= getenv("SCRIPT_NAME");
185   if (ev) {
186     ev2= getenv("USER"); if (!ev2) error("no USER variable", 500);
187     newvar= xmalloc(strlen(ev)+2+strlen(ev2)+scriptpathlen-scriptdirlen+2);
188     sprintf(newvar,"%s/~%s%s",ev,ev2,scriptpath+scriptdirlen);
189     xsetenv("SCRIPT_NAME",newvar,1);
190   }
191
192   arguments= xmalloc(sizeof(const char*)*(argc+5));
193   nargs= 0;
194   
195   arguments[nargs++]= scriptpath;
196   while ((av= (*++argv))) arguments[nargs++]= av;
197   arguments[nargs++]= 0;
198
199   D( if (debugmode) {
200        int i;
201
202        printf(";; final environment...\n");
203        for (i = 0; environ[i]; i++)
204          printf(";;   %s\n", environ[i]);
205
206        printf(";; final command line...\n");
207        for (i = 0; arguments[i]; i++)
208          printf(";;   %s\n", arguments[i]);
209        fflush(stdout);
210   } )
211
212   execvp(scriptpath,(char*const*)arguments);
213   syserror("exec script");
214   return -1;
215 }