2 * Usage: as CGI script, but called by userv
3 * environment variables are USERV_U_E_...
10 #include <sys/types.h>
16 void *xrealloc(void *ptr, size_t sz) {
20 if (!r) syserror("realloc failed");
24 int main(int argc, const char **argv) {
25 char *uservar, *scriptpath;
26 const char *nextslash, *lastslash, *pathi, *ev, *en, *scriptdir;
27 const char *const *ep;
28 const char **arguments;
29 size_t scriptdirlen, scriptpathlen, l, uservarnl;
32 ev= getenv("USERV_U_DEBUG");
33 if (ev && *ev) debugmode= 1;
35 if (argc > MAX_ARGS) error("too many arguments");
38 if (!scriptdir) error("no argv[1] (script directory)");
39 scriptdirlen= strlen(scriptdir);
41 for (ep= envok; (en= *ep); ep++) {
43 if (uservarnl<l) { uservarn= xrealloc(uservarn,l); uservarnl= l; }
44 sprintf(uservarn,"USERV_U_E_%s",en);
45 ev= getenv(uservarn); if (!ev) continue;
46 if (strlen(ev) > MAX_ENVVAR_VALUE) error("environment variable too long");
47 if (setenv(en,ev,1)) syserror("setenv");
48 if (unsetenv(uservarn)) syserror("unsetenv");
51 pathi= getenv("PATH_INFO");
52 if (!pathi) error("PATH_INFO not found");
53 if (pathi[0] != '/') error("PATH_INFO must start with /");
56 nextslash= strchr(lastslash+1,'/');
57 if (!nextslash) error("insufficient slashes in PATH_INFO beyond directory");
58 if (nextslash[1]=='.' || nextslash[1]=='#' || !nextslash[1]) error("bad char begin");
59 if (nextslash-pathi > MAX_SCRIPTPATH_LEN) error("PATH_INFO script dir too long");
60 scriptpathlen= scriptdirlen+(nextslash-pathi);
61 scriptpath= xrealloc(scriptpath,scriptpathlen);
62 memcpy(scriptpath,pathi,scriptpathlen); scriptpath[scriptpathlen]= 0;
63 if (scriptpath[scriptpathlen-1]=='~') error("bad char end");
64 r= stat(scriptpath,&stab); if (r) syserror("stat script");
65 if (S_ISREG(stab.st_mode)) break;
66 if (!S_ISDIR(stab.st_mode)) syserror("script not directory or file");
69 if (setenv("PATH_INFO",nextslash,1)) syserror("setenv PATH_INFO");
71 arguments= xmalloc(sizeof(const char*)*(argc+5));
74 arguments[nargs++]= scriptpath;
75 while ((av= (*++argv))) arguments[nargs++]= av;
76 arguments[nargs++]= 0;
78 execvp(scriptpath,(char*const*)arguments);
79 syserror("exec script");