chiark / gitweb /
Merge branch 'mdw+fixes'
[userv-utils] / www-cgi / ucgitarget.c
CommitLineData
6a580c17 1/*
2 * Usage: as CGI script, but called by userv
3 * environment variables are USERV_U_E_...
4 */
a33962ba 5/*
9028e234
IJ
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>
a33962ba 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
9028e234 14 * the Free Software Foundation; either version 3 of the License, or
a33962ba 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
9028e234 23 * along with userv-utils; if not, see http://www.gnu.org/licenses/.
a33962ba 24 */
6a580c17 25
26#include <stdio.h>
27#include <string.h>
28#include <ctype.h>
f7b4be5a 29#include <getopt.h>
6a580c17 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
f7b4be5a 37static const char *const default_envok[]= {
a8e8db26
MW
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",
564fbf9b
MW
64 "REDIRECT_HANDLER",
65 "REDIRECT_SCRIPT_URI",
66 "REDIRECT_SCRIPT_URL",
67 "REDIRECT_STATUS",
68 "REDIRECT_URL",
a8e8db26
MW
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",
564fbf9b
MW
86 "SSL_CIPHER",
87 "SSL_CLIENT_S_DN",
88 "SSL_CLIENT_VERIFY",
89 "SSL_PROTOCOL",
a8e8db26
MW
90 0
91};
6a580c17 92
f601a2c6
MW
93static void setenvar(const char *fulln,
94 const char *en, const char *ep, void *p) {
95 xsetenv(en, ep, 1);
96 unsetenv(fulln);
6a580c17 97}
98
f7b4be5a 99int main(int argc, char **argv) {
f601a2c6
MW
100 char *scriptpath, *newvar;
101 const char *nextslash, *lastslash, *pathi, *ev, *ev2, *scriptdir, *av;
f7b4be5a 102 const char *const *envok;
6a580c17 103 const char **arguments;
f601a2c6 104 size_t scriptdirlen, scriptpathlen, l;
6a580c17 105 struct stat stab;
f7b4be5a
MW
106 int i, r, nargs;
107 const char *filters= 0;
6a580c17 108
109 ev= getenv("USERV_U_DEBUG");
110 if (ev && *ev) debugmode= 1;
111
6a3086f1 112 D( if (debugmode) printf(";;; UCGITARGET\n"); )
0cd9d59d 113 if (argc > MAX_ARGS) error("too many arguments", 500);
6a580c17 114
f7b4be5a
MW
115 for (;;) {
116 i= getopt(argc, argv, "+e:"); if (i < 0) break;
117 switch (i) {
118 case 'e': filters= optarg; break;
0cd9d59d 119 default: error("bad command line", 500); break;
f7b4be5a
MW
120 }
121 }
122 argc -= optind; argv += optind;
123
0cd9d59d
MW
124 if (!*argv) error("no script directory argument", 500);
125 ev= getenv("HOME"); if (!ev) error("no HOME env. var", 500);
6a580c17 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
f7b4be5a
MW
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);
6a580c17 139 }
140
aa0bce91 141 filter_environment(0, "USERV_U_E_", envok, default_envok, setenvar, 0);
6a580c17 142
143 scriptpath= 0;
144 pathi= getenv("PATH_INFO");
0cd9d59d 145 if (!pathi) error("PATH_INFO not found", 500);
6a580c17 146 lastslash= pathi;
6a3086f1
MW
147 D( if (debugmode) {
148 printf(";; find script name...\n"
149 ";; PATH_INFO = `%s'\n",
150 pathi);
151 } )
6a580c17 152 for (;;) {
0cd9d59d
MW
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);
6a580c17 156 nextslash= strchr(lastslash+1,'/');
157 if (!nextslash) nextslash= lastslash+1+strlen(lastslash+1);
0cd9d59d
MW
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);
6a580c17 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;
0cd9d59d 167 if (scriptpath[scriptpathlen-1]=='~') error("bad char end", 400);
6a3086f1 168 D( if (debugmode) printf(";; try `%s'\n", scriptpath); )
6a580c17 169 r= stat(scriptpath,&stab); if (r) syserror("stat script");
170 if (S_ISREG(stab.st_mode)) break;
0cd9d59d 171 if (!S_ISDIR(stab.st_mode)) error("script not directory or file", 500);
6a580c17 172 lastslash= nextslash;
173 }
6a3086f1 174 D( if (debugmode) printf(";; found script: tail = `%s'\n", nextslash); )
6a580c17 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) {
0cd9d59d 186 ev2= getenv("USER"); if (!ev2) error("no USER variable", 500);
6a580c17 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
6a3086f1
MW
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
6a580c17 212 execvp(scriptpath,(char*const*)arguments);
213 syserror("exec script");
214 return -1;
215}