chiark / gitweb /
www-cgi/: Centralize environment variable filtering.
[userv-utils] / www-cgi / ucgitarget.c
CommitLineData
6a580c17 1/*
2 * Usage: as CGI script, but called by userv
3 * environment variables are USERV_U_E_...
4 */
a33962ba 5/*
711a0748 6 * Copyright (C) 1998-1999,2003 Ian Jackson
a33962ba 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 */
6a580c17 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
f601a2c6
MW
35static void setenvar(const char *fulln,
36 const char *en, const char *ep, void *p) {
37 xsetenv(en, ep, 1);
38 unsetenv(fulln);
39}
40
6a580c17 41int main(int argc, const char **argv) {
f601a2c6
MW
42 char *scriptpath, *newvar;
43 const char *nextslash, *lastslash, *pathi, *ev, *ev2, *scriptdir, *av;
6a580c17 44 const char **arguments;
f601a2c6 45 size_t scriptdirlen, scriptpathlen, l;
6a580c17 46 struct stat stab;
47 int r, nargs;
48
49 ev= getenv("USERV_U_DEBUG");
50 if (ev && *ev) debugmode= 1;
51
6a3086f1 52 D( if (debugmode) printf(";;; UCGITARGET\n"); )
6a580c17 53 if (argc > MAX_ARGS) error("too many arguments");
54
55 if (!*++argv) error("no script directory argument");
56 ev= getenv("HOME"); if (!ev) error("no HOME env. var");
57 l= strlen(*argv)+strlen(ev);
58 newvar= xmalloc(l+2);
59 sprintf(newvar,"%s/%s",ev,*argv);
60 scriptdir= newvar;
61 scriptdirlen= strlen(scriptdir);
62
f601a2c6 63 filter_environment(0, "USERV_U_E_", envok, setenvar, 0);
6a580c17 64
65 scriptpath= 0;
66 pathi= getenv("PATH_INFO");
67 if (!pathi) error("PATH_INFO not found");
68 lastslash= pathi;
6a3086f1
MW
69 D( if (debugmode) {
70 printf(";; find script name...\n"
71 ";; PATH_INFO = `%s'\n",
72 pathi);
73 } )
6a580c17 74 for (;;) {
75 if (*lastslash != '/') error("PATH_INFO expected slash not found");
76 if (lastslash[1]=='.' || lastslash[1]=='#' || !lastslash[1]) error("bad char begin");
77 nextslash= strchr(lastslash+1,'/');
78 if (!nextslash) nextslash= lastslash+1+strlen(lastslash+1);
79 if (!nextslash) error("insufficient elements in PATH_INFO");
80 if (nextslash==lastslash+1) error("empty component in PATH_INFO");
81 if (nextslash-pathi > MAX_SCRIPTPATH_LEN) error("PATH_INFO script path too long");
82 scriptpathlen= scriptdirlen+(nextslash-pathi);
83 scriptpath= xrealloc(scriptpath,scriptpathlen+1);
84 strcpy(scriptpath,scriptdir);
85 memcpy(scriptpath+scriptdirlen,pathi,nextslash-pathi);
86 scriptpath[scriptpathlen]= 0;
87 if (scriptpath[scriptpathlen-1]=='~') error("bad char end");
6a3086f1 88 D( if (debugmode) printf(";; try `%s'\n", scriptpath); )
6a580c17 89 r= stat(scriptpath,&stab); if (r) syserror("stat script");
90 if (S_ISREG(stab.st_mode)) break;
1ba0145f 91 if (!S_ISDIR(stab.st_mode)) error("script not directory or file");
6a580c17 92 lastslash= nextslash;
93 }
6a3086f1 94 D( if (debugmode) printf(";; found script: tail = `%s'\n", nextslash); )
6a580c17 95 if (*nextslash) xsetenv("PATH_INFO",nextslash,1);
96 else unsetenv("PATH_INFO");
97
98 newvar= xmalloc(scriptpathlen+strlen(nextslash)+3);
99 sprintf(newvar,"%s%s",scriptpath,nextslash);
100 xsetenv("PATH_TRANSLATED",newvar,1);
101
102 xsetenv("SCRIPT_FILENAME",scriptpath,1);
103
104 ev= getenv("SCRIPT_NAME");
105 if (ev) {
106 ev2= getenv("USER"); if (!ev2) error("no USER variable");
107 newvar= xmalloc(strlen(ev)+2+strlen(ev2)+scriptpathlen-scriptdirlen+2);
108 sprintf(newvar,"%s/~%s%s",ev,ev2,scriptpath+scriptdirlen);
109 xsetenv("SCRIPT_NAME",newvar,1);
110 }
111
112 arguments= xmalloc(sizeof(const char*)*(argc+5));
113 nargs= 0;
114
115 arguments[nargs++]= scriptpath;
116 while ((av= (*++argv))) arguments[nargs++]= av;
117 arguments[nargs++]= 0;
118
6a3086f1
MW
119 D( if (debugmode) {
120 int i;
121
122 printf(";; final environment...\n");
123 for (i = 0; environ[i]; i++)
124 printf(";; %s\n", environ[i]);
125
126 printf(";; final command line...\n");
127 for (i = 0; arguments[i]; i++)
128 printf(";; %s\n", arguments[i]);
129 fflush(stdout);
130 } )
131
6a580c17 132 execvp(scriptpath,(char*const*)arguments);
133 syserror("exec script");
134 return -1;
135}