2 * Usage: as CGI script, but called by userv
3 * environment variables are USERV_U_E_...
6 * Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
7 * Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
9 * Chancellor Masters and Scholars of the University of Cambridge
10 * Copyright 2010 Tony Finch <fanf@dotat.at>
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.
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.
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/.
31 #include <sys/types.h>
37 static const char *const default_envok[]= {
44 "HTTP_ACCEPT_CHARSET",
45 "HTTP_ACCEPT_ENCODING",
46 "HTTP_ACCEPT_LANGUAGE",
49 "HTTP_CONTENT_ENCODING",
59 "HTTP_X_FORWARDED_FOR",
65 "REDIRECT_SCRIPT_URI",
66 "REDIRECT_SCRIPT_URL",
93 static void setenvar(const char *fulln,
94 const char *en, const char *ep, void *p) {
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;
107 const char *filters= 0;
109 ev= getenv("USERV_U_DEBUG");
110 if (ev && *ev) debugmode= 1;
112 D( if (debugmode) printf(";;; UCGITARGET\n"); )
113 if (argc > MAX_ARGS) error("too many arguments", 500);
116 i= getopt(argc, argv, "+e:"); if (i < 0) break;
118 case 'e': filters= optarg; break;
119 default: error("bad command line", 500); break;
122 argc -= optind; argv += optind;
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);
130 scriptdirlen= strlen(scriptdir);
133 envok= load_filters(LOADF_MUST, filters, LF_END);
135 envok= load_filters(0,
136 ".userv/ucgitarget.env-filter",
137 "/etc/userv/ucgitarget.env-filter",
141 filter_environment(0, "USERV_U_E_", envok, default_envok, setenvar, 0);
144 pathi= getenv("PATH_INFO");
145 if (!pathi) error("PATH_INFO not found", 500);
148 printf(";; find script name...\n"
149 ";; PATH_INFO = `%s'\n",
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;
174 D( if (debugmode) printf(";; found script: tail = `%s'\n", nextslash); )
175 if (*nextslash) xsetenv("PATH_INFO",nextslash,1);
176 else unsetenv("PATH_INFO");
178 newvar= xmalloc(scriptpathlen+strlen(nextslash)+3);
179 sprintf(newvar,"%s%s",scriptpath,nextslash);
180 xsetenv("PATH_TRANSLATED",newvar,1);
182 xsetenv("SCRIPT_FILENAME",scriptpath,1);
184 ev= getenv("SCRIPT_NAME");
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);
192 arguments= xmalloc(sizeof(const char*)*(argc+5));
195 arguments[nargs++]= scriptpath;
196 while ((av= (*++argv))) arguments[nargs++]= av;
197 arguments[nargs++]= 0;
202 printf(";; final environment...\n");
203 for (i = 0; environ[i]; i++)
204 printf(";; %s\n", environ[i]);
206 printf(";; final command line...\n");
207 for (i = 0; arguments[i]; i++)
208 printf(";; %s\n", arguments[i]);
212 execvp(scriptpath,(char*const*)arguments);
213 syserror("exec script");