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