b65817434bfc59e18b42db32b07b47875f4ed095
2 * Usage: as CGI script, but called by userv
3 * environment variables are USERV_U_E_...
6 * Copyright 1996-2013 Ian Jackson <ijackson@chiark.greenend.org.uk>
7 * Copyright 1998 David Damerell <damerell@chiark.greenend.org.uk>
9 * Chancellor Masters and Scholars of the University of Cambridge
10 * Copyright 2010 Tony Finch <fanf@dotat.at>
11 * Copyright 2013,2016 Mark Wooding <mdw@distorted.org.uk>
13 * This is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with userv-utils; if not, see http://www.gnu.org/licenses/.
32 #include <sys/types.h>
38 static const char *const default_envok
[]= {
45 "HTTP_ACCEPT_CHARSET",
46 "HTTP_ACCEPT_ENCODING",
47 "HTTP_ACCEPT_LANGUAGE",
50 "HTTP_CONTENT_ENCODING",
60 "HTTP_X_FORWARDED_FOR",
66 "REDIRECT_SCRIPT_URI",
67 "REDIRECT_SCRIPT_URL",
94 static void setenvar(const char *fulln
,
95 const char *en
, const char *ep
, void *p
) {
100 int main(int argc
, char **argv
) {
101 char *scriptpath
, *newvar
;
102 const char *nextslash
, *lastslash
, *pathi
, *ev
, *ev2
, *scriptdir
, *av
;
103 const char *const *envok
;
104 const char **arguments
;
105 size_t scriptdirlen
, scriptpathlen
, l
;
108 const char *filters
= 0;
110 ev
= getenv("USERV_U_DEBUG");
111 if (ev
&& *ev
) debugmode
= 1;
113 D( if (debugmode
) printf(";;; UCGITARGET\n"); )
114 if (argc
> MAX_ARGS
) error("too many arguments", 500);
117 i
= getopt(argc
, argv
, "+e:"); if (i
< 0) break;
119 case 'e': filters
= optarg
; break;
120 default: error("bad command line", 500); break;
123 argc
-= optind
; argv
+= optind
;
125 if (!*argv
) error("no script directory argument", 500);
126 ev
= getenv("HOME"); if (!ev
) error("no HOME env. var", 500);
127 l
= strlen(*argv
)+strlen(ev
);
128 newvar
= xmalloc(l
+2);
129 sprintf(newvar
,"%s/%s",ev
,*argv
);
131 scriptdirlen
= strlen(scriptdir
);
134 envok
= load_filters(LOADF_MUST
, filters
, LF_END
);
136 envok
= load_filters(0,
137 ".userv/ucgitarget.env-filter",
138 "/etc/userv/ucgitarget.env-filter",
142 filter_environment(0, "USERV_U_E_", envok
, default_envok
, setenvar
, 0);
145 pathi
= getenv("PATH_INFO");
146 if (!pathi
) error("PATH_INFO not found", 500);
149 printf(";; find script name...\n"
150 ";; PATH_INFO = `%s'\n",
154 if (*lastslash
!= '/') error("PATH_INFO expected slash not found", 400);
155 if (lastslash
[1]=='.' || lastslash
[1]=='#' || !lastslash
[1])
156 error("bad char begin", 400);
157 nextslash
= strchr(lastslash
+1,'/');
158 if (!nextslash
) nextslash
= lastslash
+1+strlen(lastslash
+1);
159 if (!nextslash
) error("insufficient elements in PATH_INFO", 400);
160 if (nextslash
==lastslash
+1) error("empty component in PATH_INFO", 400);
161 if (nextslash
-pathi
> MAX_SCRIPTPATH_LEN
)
162 error("PATH_INFO script path too long", 400);
163 scriptpathlen
= scriptdirlen
+(nextslash
-pathi
);
164 scriptpath
= xrealloc(scriptpath
,scriptpathlen
+1);
165 strcpy(scriptpath
,scriptdir
);
166 memcpy(scriptpath
+scriptdirlen
,pathi
,nextslash
-pathi
);
167 scriptpath
[scriptpathlen
]= 0;
168 if (scriptpath
[scriptpathlen
-1]=='~') error("bad char end", 400);
169 D( if (debugmode
) printf(";; try `%s'\n", scriptpath
); )
170 r
= stat(scriptpath
,&stab
); if (r
) syserror("stat script");
171 if (S_ISREG(stab
.st_mode
)) break;
172 if (!S_ISDIR(stab
.st_mode
)) error("script not directory or file", 500);
173 lastslash
= nextslash
;
175 D( if (debugmode
) printf(";; found script: tail = `%s'\n", nextslash
); )
176 if (*nextslash
) xsetenv("PATH_INFO",nextslash
,1);
177 else unsetenv("PATH_INFO");
179 newvar
= xmalloc(scriptpathlen
+strlen(nextslash
)+3);
180 sprintf(newvar
,"%s%s",scriptpath
,nextslash
);
181 xsetenv("PATH_TRANSLATED",newvar
,1);
183 xsetenv("SCRIPT_FILENAME",scriptpath
,1);
185 ev
= getenv("SCRIPT_NAME");
187 ev2
= getenv("USER"); if (!ev2
) error("no USER variable", 500);
188 newvar
= xmalloc(strlen(ev
)+2+strlen(ev2
)+scriptpathlen
-scriptdirlen
+2);
189 sprintf(newvar
,"%s/~%s%s",ev
,ev2
,scriptpath
+scriptdirlen
);
190 xsetenv("SCRIPT_NAME",newvar
,1);
193 arguments
= xmalloc(sizeof(const char*)*(argc
+5));
196 arguments
[nargs
++]= scriptpath
;
197 while ((av
= (*++argv
))) arguments
[nargs
++]= av
;
198 arguments
[nargs
++]= 0;
203 printf(";; final environment...\n");
204 for (i
= 0; environ
[i
]; i
++)
205 printf(";; %s\n", environ
[i
]);
207 printf(";; final command line...\n");
208 for (i
= 0; arguments
[i
]; i
++)
209 printf(";; %s\n", arguments
[i
]);
213 execvp(scriptpath
,(char*const*)arguments
);
214 syserror("exec script");