| 1 | /* |
| 2 | * Usage: as CGI script, but called by userv |
| 3 | * environment variables are USERV_U_E_... |
| 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <string.h> |
| 8 | #include <ctype.h> |
| 9 | #include <unistd.h> |
| 10 | #include <sys/types.h> |
| 11 | #include <sys/wait.h> |
| 12 | #include <sys/stat.h> |
| 13 | |
| 14 | #include "ucgi.h" |
| 15 | |
| 16 | static void *xrealloc(void *ptr, size_t sz) { |
| 17 | void *r; |
| 18 | |
| 19 | r= realloc(ptr,sz); |
| 20 | if (!r) syserror("realloc failed"); |
| 21 | return r; |
| 22 | } |
| 23 | |
| 24 | int main(int argc, const char **argv) { |
| 25 | char *uservarn, *scriptpath, *newvar; |
| 26 | const char *nextslash, *lastslash, *pathi, *ev, *ev2, *en, *scriptdir, *av; |
| 27 | const char *const *ep; |
| 28 | const char **arguments; |
| 29 | size_t scriptdirlen, scriptpathlen, l, uservarnl; |
| 30 | struct stat stab; |
| 31 | int r, nargs; |
| 32 | |
| 33 | ev= getenv("USERV_U_DEBUG"); |
| 34 | if (ev && *ev) debugmode= 1; |
| 35 | |
| 36 | if (argc > MAX_ARGS) error("too many arguments"); |
| 37 | |
| 38 | if (!*++argv) error("no script directory argument"); |
| 39 | ev= getenv("HOME"); if (!ev) error("no HOME env. var"); |
| 40 | l= strlen(*argv)+strlen(ev); |
| 41 | newvar= xmalloc(l+2); |
| 42 | sprintf(newvar,"%s/%s",ev,*argv); |
| 43 | scriptdir= newvar; |
| 44 | scriptdirlen= strlen(scriptdir); |
| 45 | |
| 46 | uservarn= 0; |
| 47 | uservarnl= 0; |
| 48 | for (ep= envok; (en= *ep); ep++) { |
| 49 | l= strlen(en)+11; |
| 50 | if (uservarnl<l) { uservarn= xrealloc(uservarn,l); uservarnl= l; } |
| 51 | sprintf(uservarn,"USERV_U_E_%s",en); |
| 52 | ev= getenv(uservarn); if (!ev) continue; |
| 53 | if (strlen(ev) > MAX_ENVVAR_VALUE) error("environment variable too long"); |
| 54 | if (setenv(en,ev,1)) syserror("setenv"); |
| 55 | unsetenv(uservarn); |
| 56 | } |
| 57 | |
| 58 | scriptpath= 0; |
| 59 | pathi= getenv("PATH_INFO"); |
| 60 | if (!pathi) error("PATH_INFO not found"); |
| 61 | lastslash= pathi; |
| 62 | for (;;) { |
| 63 | if (*lastslash != '/') error("PATH_INFO expected slash not found"); |
| 64 | if (lastslash[1]=='.' || lastslash[1]=='#' || !lastslash[1]) error("bad char begin"); |
| 65 | nextslash= strchr(lastslash+1,'/'); |
| 66 | if (!nextslash) nextslash= lastslash+1+strlen(lastslash+1); |
| 67 | if (!nextslash) error("insufficient elements in PATH_INFO"); |
| 68 | if (nextslash==lastslash+1) error("empty component in PATH_INFO"); |
| 69 | if (nextslash-pathi > MAX_SCRIPTPATH_LEN) error("PATH_INFO script path too long"); |
| 70 | scriptpathlen= scriptdirlen+(nextslash-pathi); |
| 71 | scriptpath= xrealloc(scriptpath,scriptpathlen+1); |
| 72 | strcpy(scriptpath,scriptdir); |
| 73 | memcpy(scriptpath+scriptdirlen,pathi,nextslash-pathi); |
| 74 | scriptpath[scriptpathlen]= 0; |
| 75 | if (scriptpath[scriptpathlen-1]=='~') error("bad char end"); |
| 76 | r= stat(scriptpath,&stab); if (r) syserror("stat script"); |
| 77 | if (S_ISREG(stab.st_mode)) break; |
| 78 | if (!S_ISDIR(stab.st_mode)) syserror("script not directory or file"); |
| 79 | lastslash= nextslash; |
| 80 | } |
| 81 | if (*nextslash) xsetenv("PATH_INFO",nextslash,1); |
| 82 | else unsetenv("PATH_INFO"); |
| 83 | |
| 84 | newvar= xmalloc(scriptpathlen+strlen(nextslash)+3); |
| 85 | sprintf(newvar,"%s%s",scriptpath,nextslash); |
| 86 | xsetenv("PATH_TRANSLATED",newvar,1); |
| 87 | |
| 88 | xsetenv("SCRIPT_FILENAME",scriptpath,1); |
| 89 | |
| 90 | ev= getenv("SCRIPT_NAME"); |
| 91 | if (ev) { |
| 92 | ev2= getenv("USER"); if (!ev2) error("no USER variable"); |
| 93 | newvar= xmalloc(strlen(ev)+2+strlen(ev2)+scriptpathlen-scriptdirlen+2); |
| 94 | sprintf(newvar,"%s/~%s%s",ev,ev2,scriptpath+scriptdirlen); |
| 95 | xsetenv("SCRIPT_NAME",newvar,1); |
| 96 | } |
| 97 | |
| 98 | arguments= xmalloc(sizeof(const char*)*(argc+5)); |
| 99 | nargs= 0; |
| 100 | |
| 101 | arguments[nargs++]= scriptpath; |
| 102 | while ((av= (*++argv))) arguments[nargs++]= av; |
| 103 | arguments[nargs++]= 0; |
| 104 | |
| 105 | execvp(scriptpath,(char*const*)arguments); |
| 106 | syserror("exec script"); |
| 107 | return -1; |
| 108 | } |