chiark / gitweb /
www-cgi/: Allow customization of the environment filters.
[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/*
711a0748 6 * Copyright (C) 1998-1999,2003 Ian Jackson
a33962ba 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 */
6a580c17 24
25#include <stdio.h>
26#include <string.h>
27#include <ctype.h>
f7b4be5a 28#include <getopt.h>
6a580c17 29#include <unistd.h>
30#include <sys/types.h>
31#include <sys/wait.h>
32#include <sys/stat.h>
33
34#include "ucgi.h"
35
f7b4be5a 36static const char *const default_envok[]= {
a8e8db26
MW
37 "AUTH_TYPE",
38 "CONTENT_LENGTH",
39 "CONTENT_TYPE",
40 "DOCUMENT_ROOT",
41 "GATEWAY_INTERFACE",
42 "HTTP_ACCEPT",
43 "HTTP_ACCEPT_CHARSET",
44 "HTTP_ACCEPT_ENCODING",
45 "HTTP_ACCEPT_LANGUAGE",
46 "HTTP_CACHE_CONTROL",
47 "HTTP_CONNECTION",
48 "HTTP_CONTENT_ENCODING",
49 "HTTP_COOKIE",
50 "HTTP_DNT",
51 "HTTP_HOST",
52 "HTTP_KEEP_ALIVE",
53 "HTTP_NEGOTIATE",
54 "HTTP_PRAGMA",
55 "HTTP_REFERER",
56 "HTTP_USER_AGENT",
57 "HTTP_VIA",
58 "HTTP_X_FORWARDED_FOR",
59 "HTTPS",
60 "PATH_INFO",
61 "PATH_TRANSLATED",
62 "QUERY_STRING",
63 "REMOTE_ADDR",
64 "REMOTE_HOST",
65 "REMOTE_USER",
66 "REMOTE_IDENT",
67 "REQUEST_METHOD",
68 "REQUEST_URI",
69 "SCRIPT_FILENAME",
70 "SCRIPT_NAME",
71 "SCRIPT_URI",
72 "SCRIPT_URL",
73 "SERVER_ADDR",
74 "SERVER_ADMIN",
75 "SERVER_NAME",
76 "SERVER_PORT",
77 "SERVER_PROTOCOL",
78 "SERVER_SIGNATURE",
79 "SERVER_SOFTWARE",
80 0
81};
82
f601a2c6
MW
83static void setenvar(const char *fulln,
84 const char *en, const char *ep, void *p) {
85 xsetenv(en, ep, 1);
86 unsetenv(fulln);
87}
88
f7b4be5a 89int main(int argc, char **argv) {
f601a2c6
MW
90 char *scriptpath, *newvar;
91 const char *nextslash, *lastslash, *pathi, *ev, *ev2, *scriptdir, *av;
f7b4be5a 92 const char *const *envok;
6a580c17 93 const char **arguments;
f601a2c6 94 size_t scriptdirlen, scriptpathlen, l;
6a580c17 95 struct stat stab;
f7b4be5a
MW
96 int i, r, nargs;
97 const char *filters= 0;
6a580c17 98
99 ev= getenv("USERV_U_DEBUG");
100 if (ev && *ev) debugmode= 1;
101
6a3086f1 102 D( if (debugmode) printf(";;; UCGITARGET\n"); )
6a580c17 103 if (argc > MAX_ARGS) error("too many arguments");
104
f7b4be5a
MW
105 for (;;) {
106 i= getopt(argc, argv, "+e:"); if (i < 0) break;
107 switch (i) {
108 case 'e': filters= optarg; break;
109 default: error("bad command line"); break;
110 }
111 }
112 argc -= optind; argv += optind;
113
114 if (!*argv) error("no script directory argument");
6a580c17 115 ev= getenv("HOME"); if (!ev) error("no HOME env. var");
116 l= strlen(*argv)+strlen(ev);
117 newvar= xmalloc(l+2);
118 sprintf(newvar,"%s/%s",ev,*argv);
119 scriptdir= newvar;
120 scriptdirlen= strlen(scriptdir);
121
f7b4be5a
MW
122 if (filters)
123 envok= load_filters(LOADF_MUST, filters, LF_END);
124 else {
125 envok= load_filters(0,
126 ".userv/ucgitarget.env-filter",
127 "/etc/userv/ucgitarget.env-filter",
128 LF_END);
129 if (!envok) envok= default_envok;
130 }
131
f601a2c6 132 filter_environment(0, "USERV_U_E_", envok, setenvar, 0);
6a580c17 133
134 scriptpath= 0;
135 pathi= getenv("PATH_INFO");
136 if (!pathi) error("PATH_INFO not found");
137 lastslash= pathi;
6a3086f1
MW
138 D( if (debugmode) {
139 printf(";; find script name...\n"
140 ";; PATH_INFO = `%s'\n",
141 pathi);
142 } )
6a580c17 143 for (;;) {
144 if (*lastslash != '/') error("PATH_INFO expected slash not found");
145 if (lastslash[1]=='.' || lastslash[1]=='#' || !lastslash[1]) error("bad char begin");
146 nextslash= strchr(lastslash+1,'/');
147 if (!nextslash) nextslash= lastslash+1+strlen(lastslash+1);
148 if (!nextslash) error("insufficient elements in PATH_INFO");
149 if (nextslash==lastslash+1) error("empty component in PATH_INFO");
150 if (nextslash-pathi > MAX_SCRIPTPATH_LEN) error("PATH_INFO script path too long");
151 scriptpathlen= scriptdirlen+(nextslash-pathi);
152 scriptpath= xrealloc(scriptpath,scriptpathlen+1);
153 strcpy(scriptpath,scriptdir);
154 memcpy(scriptpath+scriptdirlen,pathi,nextslash-pathi);
155 scriptpath[scriptpathlen]= 0;
156 if (scriptpath[scriptpathlen-1]=='~') error("bad char end");
6a3086f1 157 D( if (debugmode) printf(";; try `%s'\n", scriptpath); )
6a580c17 158 r= stat(scriptpath,&stab); if (r) syserror("stat script");
159 if (S_ISREG(stab.st_mode)) break;
1ba0145f 160 if (!S_ISDIR(stab.st_mode)) error("script not directory or file");
6a580c17 161 lastslash= nextslash;
162 }
6a3086f1 163 D( if (debugmode) printf(";; found script: tail = `%s'\n", nextslash); )
6a580c17 164 if (*nextslash) xsetenv("PATH_INFO",nextslash,1);
165 else unsetenv("PATH_INFO");
166
167 newvar= xmalloc(scriptpathlen+strlen(nextslash)+3);
168 sprintf(newvar,"%s%s",scriptpath,nextslash);
169 xsetenv("PATH_TRANSLATED",newvar,1);
170
171 xsetenv("SCRIPT_FILENAME",scriptpath,1);
172
173 ev= getenv("SCRIPT_NAME");
174 if (ev) {
175 ev2= getenv("USER"); if (!ev2) error("no USER variable");
176 newvar= xmalloc(strlen(ev)+2+strlen(ev2)+scriptpathlen-scriptdirlen+2);
177 sprintf(newvar,"%s/~%s%s",ev,ev2,scriptpath+scriptdirlen);
178 xsetenv("SCRIPT_NAME",newvar,1);
179 }
180
181 arguments= xmalloc(sizeof(const char*)*(argc+5));
182 nargs= 0;
183
184 arguments[nargs++]= scriptpath;
185 while ((av= (*++argv))) arguments[nargs++]= av;
186 arguments[nargs++]= 0;
187
6a3086f1
MW
188 D( if (debugmode) {
189 int i;
190
191 printf(";; final environment...\n");
192 for (i = 0; environ[i]; i++)
193 printf(";; %s\n", environ[i]);
194
195 printf(";; final command line...\n");
196 for (i = 0; arguments[i]; i++)
197 printf(";; %s\n", arguments[i]);
198 fflush(stdout);
199 } )
200
6a580c17 201 execvp(scriptpath,(char*const*)arguments);
202 syserror("exec script");
203 return -1;
204}