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.
29 #include <sys/types.h>
35 static void *xrealloc(void *ptr, size_t sz) {
39 if (!r) syserror("realloc failed");
43 int main(int argc, const char **argv) {
44 char *uservarn, *scriptpath, *newvar;
45 const char *nextslash, *lastslash, *pathi, *ev, *ev2, *en, *scriptdir, *av;
46 const char *const *ep;
47 const char **arguments;
48 size_t scriptdirlen, scriptpathlen, l, uservarnl;
52 ev= getenv("USERV_U_DEBUG");
53 if (ev && *ev) debugmode= 1;
55 if (argc > MAX_ARGS) error("too many arguments");
57 if (!*++argv) error("no script directory argument");
58 ev= getenv("HOME"); if (!ev) error("no HOME env. var");
59 l= strlen(*argv)+strlen(ev);
61 sprintf(newvar,"%s/%s",ev,*argv);
63 scriptdirlen= strlen(scriptdir);
67 for (ep= envok; (en= *ep); ep++) {
69 if (uservarnl<l) { uservarn= xrealloc(uservarn,l); uservarnl= l; }
70 sprintf(uservarn,"USERV_U_E_%s",en);
71 ev= getenv(uservarn); if (!ev) continue;
72 if (strlen(ev) > MAX_ENVVAR_VALUE) error("environment variable too long");
73 if (setenv(en,ev,1)) syserror("setenv");
78 pathi= getenv("PATH_INFO");
79 if (!pathi) error("PATH_INFO not found");
82 if (*lastslash != '/') error("PATH_INFO expected slash not found");
83 if (lastslash[1]=='.' || lastslash[1]=='#' || !lastslash[1]) error("bad char begin");
84 nextslash= strchr(lastslash+1,'/');
85 if (!nextslash) nextslash= lastslash+1+strlen(lastslash+1);
86 if (!nextslash) error("insufficient elements in PATH_INFO");
87 if (nextslash==lastslash+1) error("empty component in PATH_INFO");
88 if (nextslash-pathi > MAX_SCRIPTPATH_LEN) error("PATH_INFO script path too long");
89 scriptpathlen= scriptdirlen+(nextslash-pathi);
90 scriptpath= xrealloc(scriptpath,scriptpathlen+1);
91 strcpy(scriptpath,scriptdir);
92 memcpy(scriptpath+scriptdirlen,pathi,nextslash-pathi);
93 scriptpath[scriptpathlen]= 0;
94 if (scriptpath[scriptpathlen-1]=='~') error("bad char end");
95 r= stat(scriptpath,&stab); if (r) syserror("stat script");
96 if (S_ISREG(stab.st_mode)) break;
97 if (!S_ISDIR(stab.st_mode)) syserror("script not directory or file");
100 if (*nextslash) xsetenv("PATH_INFO",nextslash,1);
101 else unsetenv("PATH_INFO");
103 newvar= xmalloc(scriptpathlen+strlen(nextslash)+3);
104 sprintf(newvar,"%s%s",scriptpath,nextslash);
105 xsetenv("PATH_TRANSLATED",newvar,1);
107 xsetenv("SCRIPT_FILENAME",scriptpath,1);
109 ev= getenv("SCRIPT_NAME");
111 ev2= getenv("USER"); if (!ev2) error("no USER variable");
112 newvar= xmalloc(strlen(ev)+2+strlen(ev2)+scriptpathlen-scriptdirlen+2);
113 sprintf(newvar,"%s/~%s%s",ev,ev2,scriptpath+scriptdirlen);
114 xsetenv("SCRIPT_NAME",newvar,1);
117 arguments= xmalloc(sizeof(const char*)*(argc+5));
120 arguments[nargs++]= scriptpath;
121 while ((av= (*++argv))) arguments[nargs++]= av;
122 arguments[nargs++]= 0;
124 execvp(scriptpath,(char*const*)arguments);
125 syserror("exec script");