Commit | Line | Data |
---|---|---|
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> | |
f7b4be5a | 28 | #include <getopt.h> |
6a580c17 | 29 | #include <unistd.h> |
30 | #include <sys/types.h> | |
31 | #include <sys/wait.h> | |
32 | #include <sys/stat.h> | |
33 | ||
34 | #include "ucgi.h" | |
35 | ||
f7b4be5a | 36 | static const char *const default_envok[]= { |
a8e8db26 MW |
37 | "AUTH_TYPE", |
38 | "CONTENT_LENGTH", | |
39 | "CONTENT_TYPE", | |
40 | "DOCUMENT_ROOT", | |
41 | "GATEWAY_INTERFACE", | |
42 | "HTTP_ACCEPT", | |
43 | "HTTP_ACCEPT_CHARSET", | |
44 | "HTTP_ACCEPT_ENCODING", | |
45 | "HTTP_ACCEPT_LANGUAGE", | |
46 | "HTTP_CACHE_CONTROL", | |
47 | "HTTP_CONNECTION", | |
48 | "HTTP_CONTENT_ENCODING", | |
49 | "HTTP_COOKIE", | |
50 | "HTTP_DNT", | |
51 | "HTTP_HOST", | |
52 | "HTTP_KEEP_ALIVE", | |
53 | "HTTP_NEGOTIATE", | |
54 | "HTTP_PRAGMA", | |
55 | "HTTP_REFERER", | |
56 | "HTTP_USER_AGENT", | |
57 | "HTTP_VIA", | |
58 | "HTTP_X_FORWARDED_FOR", | |
59 | "HTTPS", | |
60 | "PATH_INFO", | |
61 | "PATH_TRANSLATED", | |
62 | "QUERY_STRING", | |
564fbf9b MW |
63 | "REDIRECT_HANDLER", |
64 | "REDIRECT_SCRIPT_URI", | |
65 | "REDIRECT_SCRIPT_URL", | |
66 | "REDIRECT_STATUS", | |
67 | "REDIRECT_URL", | |
a8e8db26 MW |
68 | "REMOTE_ADDR", |
69 | "REMOTE_HOST", | |
70 | "REMOTE_USER", | |
71 | "REMOTE_IDENT", | |
72 | "REQUEST_METHOD", | |
73 | "REQUEST_URI", | |
74 | "SCRIPT_FILENAME", | |
75 | "SCRIPT_NAME", | |
76 | "SCRIPT_URI", | |
77 | "SCRIPT_URL", | |
78 | "SERVER_ADDR", | |
79 | "SERVER_ADMIN", | |
80 | "SERVER_NAME", | |
81 | "SERVER_PORT", | |
82 | "SERVER_PROTOCOL", | |
83 | "SERVER_SIGNATURE", | |
84 | "SERVER_SOFTWARE", | |
564fbf9b MW |
85 | "SSL_CIPHER", |
86 | "SSL_CLIENT_S_DN", | |
87 | "SSL_CLIENT_VERIFY", | |
88 | "SSL_PROTOCOL", | |
a8e8db26 MW |
89 | 0 |
90 | }; | |
91 | ||
f601a2c6 MW |
92 | static void setenvar(const char *fulln, |
93 | const char *en, const char *ep, void *p) { | |
94 | xsetenv(en, ep, 1); | |
95 | unsetenv(fulln); | |
96 | } | |
97 | ||
f7b4be5a | 98 | int main(int argc, char **argv) { |
f601a2c6 MW |
99 | char *scriptpath, *newvar; |
100 | const char *nextslash, *lastslash, *pathi, *ev, *ev2, *scriptdir, *av; | |
f7b4be5a | 101 | const char *const *envok; |
6a580c17 | 102 | const char **arguments; |
f601a2c6 | 103 | size_t scriptdirlen, scriptpathlen, l; |
6a580c17 | 104 | struct stat stab; |
f7b4be5a MW |
105 | int i, r, nargs; |
106 | const char *filters= 0; | |
6a580c17 | 107 | |
108 | ev= getenv("USERV_U_DEBUG"); | |
109 | if (ev && *ev) debugmode= 1; | |
110 | ||
6a3086f1 | 111 | D( if (debugmode) printf(";;; UCGITARGET\n"); ) |
0cd9d59d | 112 | if (argc > MAX_ARGS) error("too many arguments", 500); |
6a580c17 | 113 | |
f7b4be5a MW |
114 | for (;;) { |
115 | i= getopt(argc, argv, "+e:"); if (i < 0) break; | |
116 | switch (i) { | |
117 | case 'e': filters= optarg; break; | |
0cd9d59d | 118 | default: error("bad command line", 500); break; |
f7b4be5a MW |
119 | } |
120 | } | |
121 | argc -= optind; argv += optind; | |
122 | ||
0cd9d59d MW |
123 | if (!*argv) error("no script directory argument", 500); |
124 | ev= getenv("HOME"); if (!ev) error("no HOME env. var", 500); | |
6a580c17 | 125 | l= strlen(*argv)+strlen(ev); |
126 | newvar= xmalloc(l+2); | |
127 | sprintf(newvar,"%s/%s",ev,*argv); | |
128 | scriptdir= newvar; | |
129 | scriptdirlen= strlen(scriptdir); | |
130 | ||
f7b4be5a MW |
131 | if (filters) |
132 | envok= load_filters(LOADF_MUST, filters, LF_END); | |
133 | else { | |
134 | envok= load_filters(0, | |
135 | ".userv/ucgitarget.env-filter", | |
136 | "/etc/userv/ucgitarget.env-filter", | |
137 | LF_END); | |
f7b4be5a MW |
138 | } |
139 | ||
aa0bce91 | 140 | filter_environment(0, "USERV_U_E_", envok, default_envok, setenvar, 0); |
6a580c17 | 141 | |
142 | scriptpath= 0; | |
143 | pathi= getenv("PATH_INFO"); | |
0cd9d59d | 144 | if (!pathi) error("PATH_INFO not found", 500); |
6a580c17 | 145 | lastslash= pathi; |
6a3086f1 MW |
146 | D( if (debugmode) { |
147 | printf(";; find script name...\n" | |
148 | ";; PATH_INFO = `%s'\n", | |
149 | pathi); | |
150 | } ) | |
6a580c17 | 151 | for (;;) { |
0cd9d59d MW |
152 | if (*lastslash != '/') error("PATH_INFO expected slash not found", 400); |
153 | if (lastslash[1]=='.' || lastslash[1]=='#' || !lastslash[1]) | |
154 | error("bad char begin", 400); | |
6a580c17 | 155 | nextslash= strchr(lastslash+1,'/'); |
156 | if (!nextslash) nextslash= lastslash+1+strlen(lastslash+1); | |
0cd9d59d MW |
157 | if (!nextslash) error("insufficient elements in PATH_INFO", 400); |
158 | if (nextslash==lastslash+1) error("empty component in PATH_INFO", 400); | |
159 | if (nextslash-pathi > MAX_SCRIPTPATH_LEN) | |
160 | error("PATH_INFO script path too long", 400); | |
6a580c17 | 161 | scriptpathlen= scriptdirlen+(nextslash-pathi); |
162 | scriptpath= xrealloc(scriptpath,scriptpathlen+1); | |
163 | strcpy(scriptpath,scriptdir); | |
164 | memcpy(scriptpath+scriptdirlen,pathi,nextslash-pathi); | |
165 | scriptpath[scriptpathlen]= 0; | |
0cd9d59d | 166 | if (scriptpath[scriptpathlen-1]=='~') error("bad char end", 400); |
6a3086f1 | 167 | D( if (debugmode) printf(";; try `%s'\n", scriptpath); ) |
6a580c17 | 168 | r= stat(scriptpath,&stab); if (r) syserror("stat script"); |
169 | if (S_ISREG(stab.st_mode)) break; | |
0cd9d59d | 170 | if (!S_ISDIR(stab.st_mode)) error("script not directory or file", 500); |
6a580c17 | 171 | lastslash= nextslash; |
172 | } | |
6a3086f1 | 173 | D( if (debugmode) printf(";; found script: tail = `%s'\n", nextslash); ) |
6a580c17 | 174 | if (*nextslash) xsetenv("PATH_INFO",nextslash,1); |
175 | else unsetenv("PATH_INFO"); | |
176 | ||
177 | newvar= xmalloc(scriptpathlen+strlen(nextslash)+3); | |
178 | sprintf(newvar,"%s%s",scriptpath,nextslash); | |
179 | xsetenv("PATH_TRANSLATED",newvar,1); | |
180 | ||
181 | xsetenv("SCRIPT_FILENAME",scriptpath,1); | |
182 | ||
183 | ev= getenv("SCRIPT_NAME"); | |
184 | if (ev) { | |
0cd9d59d | 185 | ev2= getenv("USER"); if (!ev2) error("no USER variable", 500); |
6a580c17 | 186 | newvar= xmalloc(strlen(ev)+2+strlen(ev2)+scriptpathlen-scriptdirlen+2); |
187 | sprintf(newvar,"%s/~%s%s",ev,ev2,scriptpath+scriptdirlen); | |
188 | xsetenv("SCRIPT_NAME",newvar,1); | |
189 | } | |
190 | ||
191 | arguments= xmalloc(sizeof(const char*)*(argc+5)); | |
192 | nargs= 0; | |
193 | ||
194 | arguments[nargs++]= scriptpath; | |
195 | while ((av= (*++argv))) arguments[nargs++]= av; | |
196 | arguments[nargs++]= 0; | |
197 | ||
6a3086f1 MW |
198 | D( if (debugmode) { |
199 | int i; | |
200 | ||
201 | printf(";; final environment...\n"); | |
202 | for (i = 0; environ[i]; i++) | |
203 | printf(";; %s\n", environ[i]); | |
204 | ||
205 | printf(";; final command line...\n"); | |
206 | for (i = 0; arguments[i]; i++) | |
207 | printf(";; %s\n", arguments[i]); | |
208 | fflush(stdout); | |
209 | } ) | |
210 | ||
6a580c17 | 211 | execvp(scriptpath,(char*const*)arguments); |
212 | syserror("exec script"); | |
213 | return -1; | |
214 | } |