2 * Usage: as CGI script, but called by userv
3 * environment variables are USERV_U_E_...
6 * Copyright (C) 1998-1999,2003 Ian Jackson
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.
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.
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.
30 #include <sys/types.h>
36 static const char *const default_envok[]= {
43 "HTTP_ACCEPT_CHARSET",
44 "HTTP_ACCEPT_ENCODING",
45 "HTTP_ACCEPT_LANGUAGE",
48 "HTTP_CONTENT_ENCODING",
58 "HTTP_X_FORWARDED_FOR",
64 "REDIRECT_SCRIPT_URI",
65 "REDIRECT_SCRIPT_URL",
92 static void setenvar(const char *fulln,
93 const char *en, const char *ep, void *p) {
98 int main(int argc, char **argv) {
99 char *scriptpath, *newvar;
100 const char *nextslash, *lastslash, *pathi, *ev, *ev2, *scriptdir, *av;
101 const char *const *envok;
102 const char **arguments;
103 size_t scriptdirlen, scriptpathlen, l;
106 const char *filters= 0;
108 ev= getenv("USERV_U_DEBUG");
109 if (ev && *ev) debugmode= 1;
111 D( if (debugmode) printf(";;; UCGITARGET\n"); )
112 if (argc > MAX_ARGS) error("too many arguments");
115 i= getopt(argc, argv, "+e:"); if (i < 0) break;
117 case 'e': filters= optarg; break;
118 default: error("bad command line"); break;
121 argc -= optind; argv += optind;
123 if (!*argv) error("no script directory argument");
124 ev= getenv("HOME"); if (!ev) error("no HOME env. var");
125 l= strlen(*argv)+strlen(ev);
126 newvar= xmalloc(l+2);
127 sprintf(newvar,"%s/%s",ev,*argv);
129 scriptdirlen= strlen(scriptdir);
132 envok= load_filters(LOADF_MUST, filters, LF_END);
134 envok= load_filters(0,
135 ".userv/ucgitarget.env-filter",
136 "/etc/userv/ucgitarget.env-filter",
138 if (!envok) envok= default_envok;
141 filter_environment(0, "USERV_U_E_", envok, setenvar, 0);
144 pathi= getenv("PATH_INFO");
145 if (!pathi) error("PATH_INFO not found");
148 printf(";; find script name...\n"
149 ";; PATH_INFO = `%s'\n",
153 if (*lastslash != '/') error("PATH_INFO expected slash not found");
154 if (lastslash[1]=='.' || lastslash[1]=='#' || !lastslash[1]) error("bad char begin");
155 nextslash= strchr(lastslash+1,'/');
156 if (!nextslash) nextslash= lastslash+1+strlen(lastslash+1);
157 if (!nextslash) error("insufficient elements in PATH_INFO");
158 if (nextslash==lastslash+1) error("empty component in PATH_INFO");
159 if (nextslash-pathi > MAX_SCRIPTPATH_LEN) error("PATH_INFO script path too long");
160 scriptpathlen= scriptdirlen+(nextslash-pathi);
161 scriptpath= xrealloc(scriptpath,scriptpathlen+1);
162 strcpy(scriptpath,scriptdir);
163 memcpy(scriptpath+scriptdirlen,pathi,nextslash-pathi);
164 scriptpath[scriptpathlen]= 0;
165 if (scriptpath[scriptpathlen-1]=='~') error("bad char end");
166 D( if (debugmode) printf(";; try `%s'\n", scriptpath); )
167 r= stat(scriptpath,&stab); if (r) syserror("stat script");
168 if (S_ISREG(stab.st_mode)) break;
169 if (!S_ISDIR(stab.st_mode)) error("script not directory or file");
170 lastslash= nextslash;
172 D( if (debugmode) printf(";; found script: tail = `%s'\n", nextslash); )
173 if (*nextslash) xsetenv("PATH_INFO",nextslash,1);
174 else unsetenv("PATH_INFO");
176 newvar= xmalloc(scriptpathlen+strlen(nextslash)+3);
177 sprintf(newvar,"%s%s",scriptpath,nextslash);
178 xsetenv("PATH_TRANSLATED",newvar,1);
180 xsetenv("SCRIPT_FILENAME",scriptpath,1);
182 ev= getenv("SCRIPT_NAME");
184 ev2= getenv("USER"); if (!ev2) error("no USER variable");
185 newvar= xmalloc(strlen(ev)+2+strlen(ev2)+scriptpathlen-scriptdirlen+2);
186 sprintf(newvar,"%s/~%s%s",ev,ev2,scriptpath+scriptdirlen);
187 xsetenv("SCRIPT_NAME",newvar,1);
190 arguments= xmalloc(sizeof(const char*)*(argc+5));
193 arguments[nargs++]= scriptpath;
194 while ((av= (*++argv))) arguments[nargs++]= av;
195 arguments[nargs++]= 0;
200 printf(";; final environment...\n");
201 for (i = 0; environ[i]; i++)
202 printf(";; %s\n", environ[i]);
204 printf(";; final command line...\n");
205 for (i = 0; arguments[i]; i++)
206 printf(";; %s\n", arguments[i]);
210 execvp(scriptpath,(char*const*)arguments);
211 syserror("exec script");