1 |
root |
1.1 |
/* libhttpd.c - HTTP protocol library |
2 |
|
|
** |
3 |
|
|
** Copyright © 1995,1998,1999,2000,2001 by Jef Poskanzer <jef@acme.com>. |
4 |
|
|
** All rights reserved. |
5 |
|
|
** |
6 |
|
|
** Redistribution and use in source and binary forms, with or without |
7 |
|
|
** modification, are permitted provided that the following conditions |
8 |
|
|
** are met: |
9 |
|
|
** 1. Redistributions of source code must retain the above copyright |
10 |
|
|
** notice, this list of conditions and the following disclaimer. |
11 |
|
|
** 2. Redistributions in binary form must reproduce the above copyright |
12 |
|
|
** notice, this list of conditions and the following disclaimer in the |
13 |
|
|
** documentation and/or other materials provided with the distribution. |
14 |
|
|
** |
15 |
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
|
|
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
|
|
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
|
|
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
|
|
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
|
|
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
|
|
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
|
|
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
|
|
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
|
|
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
|
|
** SUCH DAMAGE. |
26 |
|
|
*/ |
27 |
|
|
|
28 |
|
|
|
29 |
|
|
#include "config.h" |
30 |
|
|
#include "version.h" |
31 |
|
|
|
32 |
|
|
#ifdef SHOW_SERVER_VERSION |
33 |
|
|
#define EXPOSED_SERVER_SOFTWARE SERVER_SOFTWARE |
34 |
|
|
#else /* SHOW_SERVER_VERSION */ |
35 |
|
|
#define EXPOSED_SERVER_SOFTWARE "thttpd" |
36 |
|
|
#endif /* SHOW_SERVER_VERSION */ |
37 |
|
|
|
38 |
|
|
#include <sys/types.h> |
39 |
|
|
#include <sys/param.h> |
40 |
|
|
#include <sys/wait.h> |
41 |
|
|
#include <sys/stat.h> |
42 |
|
|
|
43 |
|
|
#include <ctype.h> |
44 |
|
|
#include <errno.h> |
45 |
|
|
#include <fcntl.h> |
46 |
|
|
#include <time.h> |
47 |
|
|
#ifdef HAVE_MEMORY_H |
48 |
|
|
#include <memory.h> |
49 |
|
|
#endif /* HAVE_MEMORY_H */ |
50 |
|
|
#include <pwd.h> |
51 |
|
|
#include <signal.h> |
52 |
|
|
#include <stdio.h> |
53 |
|
|
#include <stdlib.h> |
54 |
|
|
#include <string.h> |
55 |
|
|
#include <syslog.h> |
56 |
|
|
#include <unistd.h> |
57 |
|
|
#include <stdarg.h> |
58 |
|
|
|
59 |
|
|
#ifdef HAVE_OSRELDATE_H |
60 |
|
|
#include <osreldate.h> |
61 |
|
|
#endif /* HAVE_OSRELDATE_H */ |
62 |
|
|
|
63 |
|
|
#ifdef HAVE_DIRENT_H |
64 |
|
|
# include <dirent.h> |
65 |
|
|
# define NAMLEN(dirent) strlen((dirent)->d_name) |
66 |
|
|
#else |
67 |
|
|
# define dirent direct |
68 |
|
|
# define NAMLEN(dirent) (dirent)->d_namlen |
69 |
|
|
# ifdef HAVE_SYS_NDIR_H |
70 |
|
|
# include <sys/ndir.h> |
71 |
|
|
# endif |
72 |
|
|
# ifdef HAVE_SYS_DIR_H |
73 |
|
|
# include <sys/dir.h> |
74 |
|
|
# endif |
75 |
|
|
# ifdef HAVE_NDIR_H |
76 |
|
|
# include <ndir.h> |
77 |
|
|
# endif |
78 |
|
|
#endif |
79 |
|
|
|
80 |
|
|
extern char* crypt( const char* key, const char* setting ); |
81 |
|
|
|
82 |
|
|
#include "libhttpd.h" |
83 |
|
|
#include "mmc.h" |
84 |
|
|
#include "timers.h" |
85 |
|
|
#include "match.h" |
86 |
|
|
#include "tdate_parse.h" |
87 |
|
|
|
88 |
|
|
#ifndef STDIN_FILENO |
89 |
|
|
#define STDIN_FILENO 0 |
90 |
|
|
#endif |
91 |
|
|
#ifndef STDOUT_FILENO |
92 |
|
|
#define STDOUT_FILENO 1 |
93 |
|
|
#endif |
94 |
|
|
#ifndef STDERR_FILENO |
95 |
|
|
#define STDERR_FILENO 2 |
96 |
|
|
#endif |
97 |
|
|
|
98 |
|
|
#ifndef max |
99 |
|
|
#define max(a,b) ((a) > (b) ? (a) : (b)) |
100 |
|
|
#endif |
101 |
|
|
#ifndef min |
102 |
|
|
#define min(a,b) ((a) < (b) ? (a) : (b)) |
103 |
|
|
#endif |
104 |
|
|
|
105 |
|
|
|
106 |
|
|
/* Forwards. */ |
107 |
|
|
static void child_reaper( ClientData client_data, struct timeval* nowP ); |
108 |
|
|
static int do_reap( void ); |
109 |
|
|
static void check_options( void ); |
110 |
|
|
static void free_httpd_server( httpd_server* hs ); |
111 |
|
|
static int initialize_listen_socket( httpd_sockaddr* saP ); |
112 |
|
|
static void unlisten( httpd_server* hs ); |
113 |
|
|
static void add_response( httpd_conn* hc, char* str ); |
114 |
|
|
static void send_mime( httpd_conn* hc, int status, char* title, char* encodings, char* extraheads, char* type, int length, time_t mod ); |
115 |
|
|
static void send_response( httpd_conn* hc, int status, char* title, char* extraheads, char* form, char* arg ); |
116 |
|
|
static void send_response_tail( httpd_conn* hc ); |
117 |
|
|
static void defang( char* str, char* dfstr, int dfsize ); |
118 |
|
|
#ifdef ERR_DIR |
119 |
|
|
static int send_err_file( httpd_conn* hc, int status, char* title, char* extraheads, char* filename ); |
120 |
|
|
#endif /* ERR_DIR */ |
121 |
|
|
#ifdef AUTH_FILE |
122 |
|
|
static void send_authenticate( httpd_conn* hc, char* realm ); |
123 |
|
|
static int b64_decode( const char* str, unsigned char* space, int size ); |
124 |
|
|
static int auth_check( httpd_conn* hc, char* dirname ); |
125 |
|
|
static int auth_check2( httpd_conn* hc, char* dirname ); |
126 |
|
|
#endif /* AUTH_FILE */ |
127 |
|
|
static void send_dirredirect( httpd_conn* hc ); |
128 |
|
|
static int hexit( char c ); |
129 |
|
|
static void strdecode( char* to, char* from ); |
130 |
|
|
#ifdef GENERATE_INDEXES |
131 |
|
|
static void strencode( char* to, int tosize, char* from ); |
132 |
|
|
#endif /* GENERATE_INDEXES */ |
133 |
|
|
#ifdef TILDE_MAP_1 |
134 |
|
|
static int tilde_map_1( httpd_conn* hc ); |
135 |
|
|
#endif /* TILDE_MAP_1 */ |
136 |
|
|
#ifdef TILDE_MAP_2 |
137 |
|
|
static int tilde_map_2( httpd_conn* hc ); |
138 |
|
|
#endif /* TILDE_MAP_2 */ |
139 |
|
|
static int vhost_map( httpd_conn* hc ); |
140 |
|
|
static char* expand_symlinks( char* path, char** restP, int no_symlink, int tildemapped ); |
141 |
|
|
static char* bufgets( httpd_conn* hc ); |
142 |
|
|
static void de_dotdot( char* file ); |
143 |
|
|
static void figure_mime( httpd_conn* hc ); |
144 |
|
|
#ifdef CGI_TIMELIMIT |
145 |
|
|
static void cgi_kill2( ClientData client_data, struct timeval* nowP ); |
146 |
|
|
static void cgi_kill( ClientData client_data, struct timeval* nowP ); |
147 |
|
|
#endif /* CGI_TIMELIMIT */ |
148 |
|
|
#ifdef GENERATE_INDEXES |
149 |
|
|
static off_t ls( httpd_conn* hc ); |
150 |
|
|
#endif /* GENERATE_INDEXES */ |
151 |
|
|
static char* build_env( char* fmt, char* arg ); |
152 |
|
|
#ifdef SERVER_NAME_LIST |
153 |
|
|
static char* hostname_map( char* hostname ); |
154 |
|
|
#endif /* SERVER_NAME_LIST */ |
155 |
|
|
static char** make_envp( httpd_conn* hc ); |
156 |
|
|
static char** make_argp( httpd_conn* hc ); |
157 |
|
|
static void cgi_interpose_input( httpd_conn* hc, int wfd ); |
158 |
|
|
static void post_post_garbage_hack( httpd_conn* hc ); |
159 |
|
|
static void cgi_interpose_output( httpd_conn* hc, int rfd ); |
160 |
|
|
static void cgi_child( httpd_conn* hc ); |
161 |
|
|
static off_t cgi( httpd_conn* hc ); |
162 |
|
|
static int really_start_request( httpd_conn* hc, struct timeval* nowP ); |
163 |
|
|
static void make_log_entry( httpd_conn* hc, struct timeval* nowP ); |
164 |
|
|
static int check_referer( httpd_conn* hc ); |
165 |
|
|
static int really_check_referer( httpd_conn* hc ); |
166 |
|
|
static int sockaddr_check( httpd_sockaddr* saP ); |
167 |
|
|
static size_t sockaddr_len( httpd_sockaddr* saP ); |
168 |
|
|
static int my_snprintf( char* str, size_t size, const char* format, ... ); |
169 |
|
|
|
170 |
|
|
|
171 |
|
|
static int reap_time; |
172 |
|
|
|
173 |
|
|
static void |
174 |
|
|
child_reaper( ClientData client_data, struct timeval* nowP ) |
175 |
|
|
{ |
176 |
|
|
int child_count; |
177 |
|
|
static int prev_child_count = 0; |
178 |
|
|
|
179 |
|
|
child_count = do_reap(); |
180 |
|
|
|
181 |
|
|
/* Reschedule reaping, with adaptively changed time. */ |
182 |
|
|
if ( child_count > prev_child_count * 3 / 2 ) |
183 |
|
|
reap_time = max( reap_time / 2, MIN_REAP_TIME ); |
184 |
|
|
else if ( child_count < prev_child_count * 2 / 3 ) |
185 |
|
|
reap_time = min( reap_time * 5 / 4, MAX_REAP_TIME ); |
186 |
|
|
if ( tmr_create( nowP, child_reaper, JunkClientData, reap_time * 1000L, 0 ) == (Timer*) 0 ) |
187 |
|
|
{ |
188 |
|
|
syslog( LOG_CRIT, "tmr_create(child_reaper) failed" ); |
189 |
|
|
exit( 1 ); |
190 |
|
|
} |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
static int |
194 |
|
|
do_reap( void ) |
195 |
|
|
{ |
196 |
|
|
int child_count; |
197 |
|
|
pid_t pid; |
198 |
|
|
int status; |
199 |
|
|
|
200 |
|
|
/* Reap defunct children until there aren't any more. */ |
201 |
|
|
for ( child_count = 0; ; ++child_count ) |
202 |
|
|
{ |
203 |
|
|
#ifdef HAVE_WAITPID |
204 |
|
|
pid = waitpid( (pid_t) -1, &status, WNOHANG ); |
205 |
|
|
#else /* HAVE_WAITPID */ |
206 |
|
|
pid = wait3( &status, WNOHANG, (struct rusage*) 0 ); |
207 |
|
|
#endif /* HAVE_WAITPID */ |
208 |
|
|
if ( (int) pid == 0 ) /* none left */ |
209 |
|
|
break; |
210 |
|
|
if ( (int) pid < 0 ) |
211 |
|
|
{ |
212 |
|
|
if ( errno == EINTR ) /* because of ptrace */ |
213 |
|
|
continue; |
214 |
|
|
/* ECHILD shouldn't happen with the WNOHANG option, but with |
215 |
|
|
** some kernels it does anyway. Ignore it. |
216 |
|
|
*/ |
217 |
|
|
if ( errno != ECHILD ) |
218 |
|
|
syslog( LOG_ERR, "waitpid - %m" ); |
219 |
|
|
break; |
220 |
|
|
} |
221 |
|
|
} |
222 |
|
|
return child_count; |
223 |
|
|
} |
224 |
|
|
|
225 |
|
|
|
226 |
|
|
static void |
227 |
|
|
check_options( void ) |
228 |
|
|
{ |
229 |
|
|
#if defined(TILDE_MAP_1) && defined(TILDE_MAP_2) |
230 |
|
|
syslog( LOG_CRIT, "both TILDE_MAP_1 and TILDE_MAP_2 are defined" ); |
231 |
|
|
exit( 1 ); |
232 |
|
|
#endif /* both */ |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
|
236 |
|
|
static void |
237 |
|
|
free_httpd_server( httpd_server* hs ) |
238 |
|
|
{ |
239 |
|
|
if ( hs->binding_hostname != (char*) 0 ) |
240 |
|
|
free( (void*) hs->binding_hostname ); |
241 |
|
|
if ( hs->cwd != (char*) 0 ) |
242 |
|
|
free( (void*) hs->cwd ); |
243 |
|
|
if ( hs->cgi_pattern != (char*) 0 ) |
244 |
|
|
free( (void*) hs->cgi_pattern ); |
245 |
|
|
if ( hs->charset != (char*) 0 ) |
246 |
|
|
free( (void*) hs->charset ); |
247 |
|
|
if ( hs->url_pattern != (char*) 0 ) |
248 |
|
|
free( (void*) hs->url_pattern ); |
249 |
|
|
if ( hs->local_pattern != (char*) 0 ) |
250 |
|
|
free( (void*) hs->local_pattern ); |
251 |
|
|
free( (void*) hs ); |
252 |
|
|
} |
253 |
|
|
|
254 |
|
|
|
255 |
|
|
httpd_server* |
256 |
|
|
httpd_initialize( |
257 |
|
|
char* hostname, httpd_sockaddr* sa4P, httpd_sockaddr* sa6P, int port, |
258 |
|
|
char* cgi_pattern, char* charset, char* cwd, int no_log, FILE* logfp, |
259 |
|
|
int no_symlink, int vhost, int global_passwd, char* url_pattern, |
260 |
|
|
char* local_pattern, int no_empty_referers ) |
261 |
|
|
{ |
262 |
|
|
httpd_server* hs; |
263 |
|
|
static char ghnbuf[256]; |
264 |
|
|
char* cp; |
265 |
|
|
|
266 |
|
|
check_options(); |
267 |
|
|
|
268 |
|
|
/* Set up child-process reaper. */ |
269 |
|
|
reap_time = min( MIN_REAP_TIME * 4, MAX_REAP_TIME ); |
270 |
|
|
if ( tmr_create( (struct timeval*) 0, child_reaper, JunkClientData, reap_time * 1000L, 0 ) == (Timer*) 0 ) |
271 |
|
|
{ |
272 |
|
|
syslog( LOG_CRIT, "tmr_create(child_reaper) failed" ); |
273 |
|
|
return (httpd_server*) 0; |
274 |
|
|
} |
275 |
|
|
|
276 |
|
|
hs = NEW( httpd_server, 1 ); |
277 |
|
|
if ( hs == (httpd_server*) 0 ) |
278 |
|
|
{ |
279 |
|
|
syslog( LOG_CRIT, "out of memory allocating an httpd_server" ); |
280 |
|
|
return (httpd_server*) 0; |
281 |
|
|
} |
282 |
|
|
|
283 |
|
|
if ( hostname != (char*) 0 ) |
284 |
|
|
{ |
285 |
|
|
hs->binding_hostname = strdup( hostname ); |
286 |
|
|
if ( hs->binding_hostname == (char*) 0 ) |
287 |
|
|
{ |
288 |
|
|
syslog( LOG_CRIT, "out of memory copying hostname" ); |
289 |
|
|
return (httpd_server*) 0; |
290 |
|
|
} |
291 |
|
|
hs->server_hostname = hs->binding_hostname; |
292 |
|
|
} |
293 |
|
|
else |
294 |
|
|
{ |
295 |
|
|
hs->binding_hostname = (char*) 0; |
296 |
|
|
hs->server_hostname = (char*) 0; |
297 |
|
|
if ( gethostname( ghnbuf, sizeof(ghnbuf) ) < 0 ) |
298 |
|
|
ghnbuf[0] = '\0'; |
299 |
|
|
#ifdef SERVER_NAME_LIST |
300 |
|
|
if ( ghnbuf[0] != '\0' ) |
301 |
|
|
hs->server_hostname = hostname_map( ghnbuf ); |
302 |
|
|
#endif /* SERVER_NAME_LIST */ |
303 |
|
|
if ( hs->server_hostname == (char*) 0 ) |
304 |
|
|
{ |
305 |
|
|
#ifdef SERVER_NAME |
306 |
|
|
hs->server_hostname = SERVER_NAME; |
307 |
|
|
#else /* SERVER_NAME */ |
308 |
|
|
if ( ghnbuf[0] != '\0' ) |
309 |
|
|
hs->server_hostname = ghnbuf; |
310 |
|
|
#endif /* SERVER_NAME */ |
311 |
|
|
} |
312 |
|
|
} |
313 |
|
|
|
314 |
|
|
hs->port = port; |
315 |
|
|
if ( cgi_pattern == (char*) 0 ) |
316 |
|
|
hs->cgi_pattern = (char*) 0; |
317 |
|
|
else |
318 |
|
|
{ |
319 |
|
|
/* Nuke any leading slashes. */ |
320 |
|
|
if ( cgi_pattern[0] == '/' ) |
321 |
|
|
++cgi_pattern; |
322 |
|
|
hs->cgi_pattern = strdup( cgi_pattern ); |
323 |
|
|
if ( hs->cgi_pattern == (char*) 0 ) |
324 |
|
|
{ |
325 |
|
|
syslog( LOG_CRIT, "out of memory copying cgi_pattern" ); |
326 |
|
|
return (httpd_server*) 0; |
327 |
|
|
} |
328 |
|
|
/* Nuke any leading slashes in the cgi pattern. */ |
329 |
|
|
while ( ( cp = strstr( hs->cgi_pattern, "|/" ) ) != (char*) 0 ) |
330 |
|
|
(void) strcpy( cp + 1, cp + 2 ); |
331 |
|
|
} |
332 |
|
|
hs->charset = strdup( charset ); |
333 |
|
|
hs->cwd = strdup( cwd ); |
334 |
|
|
if ( hs->cwd == (char*) 0 ) |
335 |
|
|
{ |
336 |
|
|
syslog( LOG_CRIT, "out of memory copying cwd" ); |
337 |
|
|
return (httpd_server*) 0; |
338 |
|
|
} |
339 |
|
|
if ( url_pattern == (char*) 0 ) |
340 |
|
|
hs->url_pattern = (char*) 0; |
341 |
|
|
else |
342 |
|
|
{ |
343 |
|
|
hs->url_pattern = strdup( url_pattern ); |
344 |
|
|
if ( hs->url_pattern == (char*) 0 ) |
345 |
|
|
{ |
346 |
|
|
syslog( LOG_CRIT, "out of memory copying url_pattern" ); |
347 |
|
|
return (httpd_server*) 0; |
348 |
|
|
} |
349 |
|
|
} |
350 |
|
|
if ( local_pattern == (char*) 0 ) |
351 |
|
|
hs->local_pattern = (char*) 0; |
352 |
|
|
else |
353 |
|
|
{ |
354 |
|
|
hs->local_pattern = strdup( local_pattern ); |
355 |
|
|
if ( hs->local_pattern == (char*) 0 ) |
356 |
|
|
{ |
357 |
|
|
syslog( LOG_CRIT, "out of memory copying local_pattern" ); |
358 |
|
|
return (httpd_server*) 0; |
359 |
|
|
} |
360 |
|
|
} |
361 |
|
|
hs->no_log = no_log; |
362 |
|
|
hs->logfp = (FILE*) 0; |
363 |
|
|
httpd_set_logfp( hs, logfp ); |
364 |
|
|
hs->no_symlink = no_symlink; |
365 |
|
|
hs->vhost = vhost; |
366 |
|
|
hs->global_passwd = global_passwd; |
367 |
|
|
hs->no_empty_referers = no_empty_referers; |
368 |
|
|
|
369 |
|
|
/* Initialize listen sockets. Try v6 first because of a Linux peculiarity; |
370 |
|
|
** unlike other systems, it has magical v6 sockets that also listen for v4, |
371 |
|
|
** but if you bind a v4 socket first then the v6 bind fails. |
372 |
|
|
*/ |
373 |
|
|
if ( sa6P == (httpd_sockaddr*) 0 ) |
374 |
|
|
hs->listen6_fd = -1; |
375 |
|
|
else |
376 |
|
|
hs->listen6_fd = initialize_listen_socket( sa6P ); |
377 |
|
|
if ( sa4P == (httpd_sockaddr*) 0 ) |
378 |
|
|
hs->listen4_fd = -1; |
379 |
|
|
else |
380 |
|
|
hs->listen4_fd = initialize_listen_socket( sa4P ); |
381 |
|
|
/* If we didn't get any valid sockets, fail. */ |
382 |
|
|
if ( hs->listen4_fd == -1 && hs->listen6_fd == -1 ) |
383 |
|
|
{ |
384 |
|
|
free_httpd_server( hs ); |
385 |
|
|
return (httpd_server*) 0; |
386 |
|
|
} |
387 |
|
|
|
388 |
|
|
/* Done initializing. */ |
389 |
|
|
if ( hs->binding_hostname == (char*) 0 ) |
390 |
|
|
syslog( LOG_INFO, "%.80s starting on port %d", SERVER_SOFTWARE, hs->port ); |
391 |
|
|
else |
392 |
|
|
syslog( |
393 |
|
|
LOG_INFO, "%.80s starting on %.80s, port %d", SERVER_SOFTWARE, |
394 |
|
|
httpd_ntoa( hs->listen4_fd != -1 ? sa4P : sa6P ), hs->port ); |
395 |
|
|
return hs; |
396 |
|
|
} |
397 |
|
|
|
398 |
|
|
|
399 |
|
|
static int |
400 |
|
|
initialize_listen_socket( httpd_sockaddr* saP ) |
401 |
|
|
{ |
402 |
|
|
int listen_fd; |
403 |
|
|
int on, flags; |
404 |
|
|
|
405 |
|
|
/* Check sockaddr. */ |
406 |
|
|
if ( ! sockaddr_check( saP ) ) |
407 |
|
|
{ |
408 |
|
|
syslog( LOG_CRIT, "unknown sockaddr family on listen socket" ); |
409 |
|
|
return -1; |
410 |
|
|
} |
411 |
|
|
|
412 |
|
|
/* Create socket. */ |
413 |
|
|
listen_fd = socket( saP->sa.sa_family, SOCK_STREAM, 0 ); |
414 |
|
|
if ( listen_fd < 0 ) |
415 |
|
|
{ |
416 |
|
|
syslog( LOG_CRIT, "socket %.80s - %m", httpd_ntoa( saP ) ); |
417 |
|
|
return -1; |
418 |
|
|
} |
419 |
|
|
(void) fcntl( listen_fd, F_SETFD, 1 ); |
420 |
|
|
|
421 |
|
|
/* Allow reuse of local addresses. */ |
422 |
|
|
on = 1; |
423 |
|
|
if ( setsockopt( |
424 |
|
|
listen_fd, SOL_SOCKET, SO_REUSEADDR, (char*) &on, |
425 |
|
|
sizeof(on) ) < 0 ) |
426 |
|
|
syslog( LOG_CRIT, "setsockopt SO_REUSEADDR - %m" ); |
427 |
|
|
|
428 |
|
|
/* Use accept filtering, if available. */ |
429 |
|
|
#ifdef SO_ACCEPTFILTER |
430 |
|
|
{ |
431 |
|
|
#if ( __FreeBSD_version >= 411000 ) |
432 |
|
|
#define ACCEPT_FILTER_NAME "httpready" |
433 |
|
|
#else |
434 |
|
|
#define ACCEPT_FILTER_NAME "dataready" |
435 |
|
|
#endif |
436 |
|
|
struct accept_filter_arg af; |
437 |
|
|
(void) bzero( &af, sizeof(af) ); |
438 |
|
|
(void) strcpy( af.af_name, ACCEPT_FILTER_NAME ); |
439 |
|
|
(void) setsockopt( |
440 |
|
|
listen_fd, SOL_SOCKET, SO_ACCEPTFILTER, (char*) &af, sizeof(af) ); |
441 |
|
|
} |
442 |
|
|
#endif /* SO_ACCEPTFILTER */ |
443 |
|
|
|
444 |
|
|
/* Bind to it. */ |
445 |
|
|
if ( bind( listen_fd, &saP->sa, sockaddr_len( saP ) ) < 0 ) |
446 |
|
|
{ |
447 |
|
|
syslog( |
448 |
|
|
LOG_CRIT, "bind %.80s - %m", httpd_ntoa( saP ) ); |
449 |
|
|
(void) close( listen_fd ); |
450 |
|
|
return -1; |
451 |
|
|
} |
452 |
|
|
|
453 |
|
|
/* Set the listen file descriptor to no-delay mode. */ |
454 |
|
|
flags = fcntl( listen_fd, F_GETFL, 0 ); |
455 |
|
|
if ( flags == -1 ) |
456 |
|
|
{ |
457 |
|
|
syslog( LOG_CRIT, "fcntl F_GETFL - %m" ); |
458 |
|
|
(void) close( listen_fd ); |
459 |
|
|
return -1; |
460 |
|
|
} |
461 |
|
|
if ( fcntl( listen_fd, F_SETFL, flags | O_NDELAY ) < 0 ) |
462 |
|
|
{ |
463 |
|
|
syslog( LOG_CRIT, "fcntl O_NDELAY - %m" ); |
464 |
|
|
(void) close( listen_fd ); |
465 |
|
|
return -1; |
466 |
|
|
} |
467 |
|
|
|
468 |
|
|
/* Start a listen going. */ |
469 |
|
|
if ( listen( listen_fd, LISTEN_BACKLOG ) < 0 ) |
470 |
|
|
{ |
471 |
|
|
syslog( LOG_CRIT, "listen - %m" ); |
472 |
|
|
(void) close( listen_fd ); |
473 |
|
|
return -1; |
474 |
|
|
} |
475 |
|
|
|
476 |
|
|
return listen_fd; |
477 |
|
|
} |
478 |
|
|
|
479 |
|
|
|
480 |
|
|
void |
481 |
|
|
httpd_set_logfp( httpd_server* hs, FILE* logfp ) |
482 |
|
|
{ |
483 |
|
|
if ( hs->logfp != (FILE*) 0 ) |
484 |
|
|
(void) fclose( hs->logfp ); |
485 |
|
|
hs->logfp = logfp; |
486 |
|
|
} |
487 |
|
|
|
488 |
|
|
|
489 |
|
|
void |
490 |
|
|
httpd_terminate( httpd_server* hs ) |
491 |
|
|
{ |
492 |
|
|
unlisten( hs ); |
493 |
|
|
if ( hs->logfp != (FILE*) 0 ) |
494 |
|
|
(void) fclose( hs->logfp ); |
495 |
|
|
free_httpd_server( hs ); |
496 |
|
|
} |
497 |
|
|
|
498 |
|
|
|
499 |
|
|
static void |
500 |
|
|
unlisten( httpd_server* hs ) |
501 |
|
|
{ |
502 |
|
|
if ( hs->listen4_fd != -1 ) |
503 |
|
|
(void) close( hs->listen4_fd ); |
504 |
|
|
if ( hs->listen6_fd != -1 ) |
505 |
|
|
(void) close( hs->listen6_fd ); |
506 |
|
|
} |
507 |
|
|
|
508 |
|
|
|
509 |
|
|
/* Conditional macro to allow two alternate forms for use in the built-in |
510 |
|
|
** error pages. If EXPLICIT_ERROR_PAGES is defined, the second and more |
511 |
|
|
** explicit error form is used; otherwise, the first and more generic |
512 |
|
|
** form is used. |
513 |
|
|
*/ |
514 |
|
|
#ifdef EXPLICIT_ERROR_PAGES |
515 |
|
|
#define ERROR_FORM(a,b) b |
516 |
|
|
#else /* EXPLICIT_ERROR_PAGES */ |
517 |
|
|
#define ERROR_FORM(a,b) a |
518 |
|
|
#endif /* EXPLICIT_ERROR_PAGES */ |
519 |
|
|
|
520 |
|
|
|
521 |
|
|
static char* ok200title = "OK"; |
522 |
|
|
static char* ok206title = "Partial Content"; |
523 |
|
|
|
524 |
|
|
static char* err302title = "Found"; |
525 |
|
|
static char* err302form = "The actual URL is '%.80s'.\n"; |
526 |
|
|
|
527 |
|
|
static char* err304title = "Not Modified"; |
528 |
|
|
|
529 |
|
|
char* httpd_err400title = "Bad Request"; |
530 |
|
|
char* httpd_err400form = |
531 |
|
|
"Your request has bad syntax or is inherently impossible to satisfy.\n"; |
532 |
|
|
|
533 |
|
|
#ifdef AUTH_FILE |
534 |
|
|
static char* err401title = "Unauthorized"; |
535 |
|
|
static char* err401form = |
536 |
|
|
"Authorization required for the URL '%.80s'.\n"; |
537 |
|
|
#endif /* AUTH_FILE */ |
538 |
|
|
|
539 |
|
|
static char* err403title = "Forbidden"; |
540 |
|
|
static char* err403form = |
541 |
|
|
"You do not have permission to get URL '%.80s' from this server.\n"; |
542 |
|
|
|
543 |
|
|
static char* err404title = "Not Found"; |
544 |
|
|
static char* err404form = |
545 |
|
|
"The requested URL '%.80s' was not found on this server.\n"; |
546 |
|
|
|
547 |
|
|
char* httpd_err408title = "Request Timeout"; |
548 |
|
|
char* httpd_err408form = |
549 |
|
|
"No request appeared within a reasonable time period.\n"; |
550 |
|
|
|
551 |
|
|
static char* err500title = "Internal Error"; |
552 |
|
|
static char* err500form = |
553 |
|
|
"There was an unusual problem serving the requested URL '%.80s'.\n"; |
554 |
|
|
|
555 |
|
|
static char* err501title = "Not Implemented"; |
556 |
|
|
static char* err501form = |
557 |
|
|
"The requested method '%.80s' is not implemented by this server.\n"; |
558 |
|
|
|
559 |
|
|
char* httpd_err503title = "Service Temporarily Overloaded"; |
560 |
|
|
char* httpd_err503form = |
561 |
|
|
"The requested URL '%.80s' is temporarily overloaded. Please try again later.\n"; |
562 |
|
|
|
563 |
|
|
|
564 |
|
|
/* Append a string to the buffer waiting to be sent as response. */ |
565 |
|
|
static void |
566 |
|
|
add_response( httpd_conn* hc, char* str ) |
567 |
|
|
{ |
568 |
|
|
int len; |
569 |
|
|
|
570 |
|
|
len = strlen( str ); |
571 |
|
|
httpd_realloc_str( &hc->response, &hc->maxresponse, hc->responselen + len ); |
572 |
|
|
(void) memcpy( &(hc->response[hc->responselen]), str, len ); |
573 |
|
|
hc->responselen += len; |
574 |
|
|
} |
575 |
|
|
|
576 |
|
|
/* Send the buffered response. */ |
577 |
|
|
void |
578 |
|
|
httpd_write_response( httpd_conn* hc ) |
579 |
|
|
{ |
580 |
|
|
/* First turn off NDELAY mode. */ |
581 |
|
|
httpd_clear_ndelay( hc->conn_fd ); |
582 |
|
|
/* And send it, if necessary. */ |
583 |
|
|
if ( hc->responselen > 0 ) |
584 |
|
|
{ |
585 |
|
|
(void) write( hc->conn_fd, hc->response, hc->responselen ); |
586 |
|
|
hc->responselen = 0; |
587 |
|
|
} |
588 |
|
|
} |
589 |
|
|
|
590 |
|
|
|
591 |
|
|
/* Set NDELAY mode on a socket. */ |
592 |
|
|
void |
593 |
|
|
httpd_set_ndelay( int fd ) |
594 |
|
|
{ |
595 |
|
|
int flags, newflags; |
596 |
|
|
|
597 |
|
|
flags = fcntl( fd, F_GETFL, 0 ); |
598 |
|
|
if ( flags != -1 ) |
599 |
|
|
{ |
600 |
|
|
newflags = flags | (int) O_NDELAY; |
601 |
|
|
if ( newflags != flags ) |
602 |
|
|
(void) fcntl( fd, F_SETFL, newflags ); |
603 |
|
|
} |
604 |
|
|
} |
605 |
|
|
|
606 |
|
|
|
607 |
|
|
/* Clear NDELAY mode on a socket. */ |
608 |
|
|
void |
609 |
|
|
httpd_clear_ndelay( int fd ) |
610 |
|
|
{ |
611 |
|
|
int flags, newflags; |
612 |
|
|
|
613 |
|
|
flags = fcntl( fd, F_GETFL, 0 ); |
614 |
|
|
if ( flags != -1 ) |
615 |
|
|
{ |
616 |
|
|
newflags = flags & ~ (int) O_NDELAY; |
617 |
|
|
if ( newflags != flags ) |
618 |
|
|
(void) fcntl( fd, F_SETFL, newflags ); |
619 |
|
|
} |
620 |
|
|
} |
621 |
|
|
|
622 |
|
|
|
623 |
|
|
static void |
624 |
|
|
send_mime( httpd_conn* hc, int status, char* title, char* encodings, char* extraheads, char* type, int length, time_t mod ) |
625 |
|
|
{ |
626 |
|
|
time_t now; |
627 |
|
|
const char* rfc1123fmt = "%a, %d %b %Y %H:%M:%S GMT"; |
628 |
|
|
char nowbuf[100]; |
629 |
|
|
char modbuf[100]; |
630 |
|
|
char fixed_type[500]; |
631 |
|
|
char buf[1000]; |
632 |
|
|
int partial_content; |
633 |
|
|
|
634 |
|
|
hc->status = status; |
635 |
|
|
hc->bytes_to_send = length; |
636 |
|
|
if ( hc->mime_flag ) |
637 |
|
|
{ |
638 |
|
|
if ( status == 200 && hc->got_range && |
639 |
|
|
( hc->end_byte_loc >= hc->init_byte_loc ) && |
640 |
|
|
( ( hc->end_byte_loc != length - 1 ) || |
641 |
|
|
( hc->init_byte_loc != 0 ) ) && |
642 |
|
|
( hc->range_if == (time_t) -1 || |
643 |
|
|
hc->range_if == hc->sb.st_mtime ) ) |
644 |
|
|
{ |
645 |
|
|
partial_content = 1; |
646 |
|
|
hc->status = status = 206; |
647 |
|
|
title = ok206title; |
648 |
|
|
} |
649 |
|
|
else |
650 |
|
|
partial_content = 0; |
651 |
|
|
|
652 |
|
|
now = time( (time_t*) 0 ); |
653 |
|
|
if ( mod == (time_t) 0 ) |
654 |
|
|
mod = now; |
655 |
|
|
(void) strftime( nowbuf, sizeof(nowbuf), rfc1123fmt, gmtime( &now ) ); |
656 |
|
|
(void) strftime( modbuf, sizeof(modbuf), rfc1123fmt, gmtime( &mod ) ); |
657 |
|
|
(void) my_snprintf( |
658 |
|
|
fixed_type, sizeof(fixed_type), type, hc->hs->charset ); |
659 |
|
|
(void) my_snprintf( buf, sizeof(buf), |
660 |
|
|
"%.20s %d %s\r\nServer: %s\r\nContent-Type: %s\r\nDate: %s\r\nLast-Modified: %s\r\nAccept-Ranges: bytes\r\nConnection: close\r\n", |
661 |
|
|
hc->protocol, status, title, EXPOSED_SERVER_SOFTWARE, fixed_type, |
662 |
|
|
nowbuf, modbuf ); |
663 |
|
|
add_response( hc, buf ); |
664 |
|
|
if ( encodings[0] != '\0' ) |
665 |
|
|
{ |
666 |
|
|
(void) my_snprintf( buf, sizeof(buf), |
667 |
|
|
"Content-Encoding: %s\r\n", encodings ); |
668 |
|
|
add_response( hc, buf ); |
669 |
|
|
} |
670 |
|
|
if ( partial_content ) |
671 |
|
|
{ |
672 |
|
|
(void) my_snprintf( buf, sizeof(buf), |
673 |
|
|
"Content-Range: bytes %ld-%ld/%d\r\nContent-Length: %ld\r\n", |
674 |
|
|
(long) hc->init_byte_loc, (long) hc->end_byte_loc, length, |
675 |
|
|
(long) ( hc->end_byte_loc - hc->init_byte_loc + 1 ) ); |
676 |
|
|
add_response( hc, buf ); |
677 |
|
|
} |
678 |
|
|
else if ( length >= 0 ) |
679 |
|
|
{ |
680 |
|
|
(void) my_snprintf( buf, sizeof(buf), |
681 |
|
|
"Content-Length: %d\r\n", length ); |
682 |
|
|
add_response( hc, buf ); |
683 |
|
|
} |
684 |
|
|
if ( extraheads[0] != '\0' ) |
685 |
|
|
add_response( hc, extraheads ); |
686 |
|
|
add_response( hc, "\r\n" ); |
687 |
|
|
} |
688 |
|
|
} |
689 |
|
|
|
690 |
|
|
|
691 |
|
|
static int str_alloc_count = 0; |
692 |
|
|
static long str_alloc_size = 0; |
693 |
|
|
|
694 |
|
|
void |
695 |
|
|
httpd_realloc_str( char** strP, int* maxsizeP, int size ) |
696 |
|
|
{ |
697 |
|
|
if ( *maxsizeP == 0 ) |
698 |
|
|
{ |
699 |
|
|
*maxsizeP = MAX( 200, size ); /* arbitrary */ |
700 |
|
|
*strP = NEW( char, *maxsizeP + 1 ); |
701 |
|
|
++str_alloc_count; |
702 |
|
|
str_alloc_size += *maxsizeP; |
703 |
|
|
} |
704 |
|
|
else if ( size > *maxsizeP ) |
705 |
|
|
{ |
706 |
|
|
str_alloc_size -= *maxsizeP; |
707 |
|
|
*maxsizeP = MAX( *maxsizeP * 2, size * 5 / 4 ); |
708 |
|
|
*strP = RENEW( *strP, char, *maxsizeP + 1 ); |
709 |
|
|
str_alloc_size += *maxsizeP; |
710 |
|
|
} |
711 |
|
|
else |
712 |
|
|
return; |
713 |
|
|
if ( *strP == (char*) 0 ) |
714 |
|
|
{ |
715 |
|
|
syslog( |
716 |
|
|
LOG_ERR, "out of memory reallocating a string to %d bytes", |
717 |
|
|
*maxsizeP ); |
718 |
|
|
exit( 1 ); |
719 |
|
|
} |
720 |
|
|
} |
721 |
|
|
|
722 |
|
|
|
723 |
|
|
static void |
724 |
|
|
send_response( httpd_conn* hc, int status, char* title, char* extraheads, char* form, char* arg ) |
725 |
|
|
{ |
726 |
|
|
char defanged_arg[1000], buf[2000]; |
727 |
|
|
|
728 |
|
|
send_mime( hc, status, title, "", extraheads, "text/html", -1, 0 ); |
729 |
|
|
(void) my_snprintf( buf, sizeof(buf), |
730 |
|
|
"<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>\n<BODY BGCOLOR=\"#cc9999\"><H2>%d %s</H2>\n", |
731 |
|
|
status, title, status, title ); |
732 |
|
|
add_response( hc, buf ); |
733 |
|
|
defang( arg, defanged_arg, sizeof(defanged_arg) ); |
734 |
|
|
(void) my_snprintf( buf, sizeof(buf), form, defanged_arg ); |
735 |
|
|
add_response( hc, buf ); |
736 |
|
|
if ( match( "**MSIE**", hc->useragent ) ) |
737 |
|
|
{ |
738 |
|
|
int n; |
739 |
|
|
add_response( hc, "<!--\n" ); |
740 |
|
|
for ( n = 0; n < 6; ++n ) |
741 |
|
|
add_response( hc, "Padding so that MSIE deigns to show this error instead of its own canned one.\n"); |
742 |
|
|
add_response( hc, "-->\n" ); |
743 |
|
|
} |
744 |
|
|
send_response_tail( hc ); |
745 |
|
|
} |
746 |
|
|
|
747 |
|
|
|
748 |
|
|
static void |
749 |
|
|
send_response_tail( httpd_conn* hc ) |
750 |
|
|
{ |
751 |
|
|
char buf[1000]; |
752 |
|
|
|
753 |
|
|
(void) my_snprintf( buf, sizeof(buf), |
754 |
|
|
"<HR>\n<ADDRESS><A HREF=\"%s\">%s</A></ADDRESS>\n</BODY></HTML>\n", |
755 |
|
|
SERVER_ADDRESS, EXPOSED_SERVER_SOFTWARE ); |
756 |
|
|
add_response( hc, buf ); |
757 |
|
|
} |
758 |
|
|
|
759 |
|
|
|
760 |
|
|
static void |
761 |
|
|
defang( char* str, char* dfstr, int dfsize ) |
762 |
|
|
{ |
763 |
|
|
char* cp1; |
764 |
|
|
char* cp2; |
765 |
|
|
|
766 |
|
|
for ( cp1 = str, cp2 = dfstr; |
767 |
|
|
*cp1 != '\0' && cp2 - dfstr < dfsize - 1; |
768 |
|
|
++cp1, ++cp2 ) |
769 |
|
|
{ |
770 |
|
|
switch ( *cp1 ) |
771 |
|
|
{ |
772 |
|
|
case '<': |
773 |
|
|
*cp2++ = '&'; |
774 |
|
|
*cp2++ = 'l'; |
775 |
|
|
*cp2++ = 't'; |
776 |
|
|
*cp2 = ';'; |
777 |
|
|
break; |
778 |
|
|
case '>': |
779 |
|
|
*cp2++ = '&'; |
780 |
|
|
*cp2++ = 'g'; |
781 |
|
|
*cp2++ = 't'; |
782 |
|
|
*cp2 = ';'; |
783 |
|
|
break; |
784 |
|
|
default: |
785 |
|
|
*cp2 = *cp1; |
786 |
|
|
break; |
787 |
|
|
} |
788 |
|
|
} |
789 |
|
|
*cp2 = '\0'; |
790 |
|
|
} |
791 |
|
|
|
792 |
|
|
|
793 |
|
|
void |
794 |
|
|
httpd_send_err( httpd_conn* hc, int status, char* title, char* extraheads, char* form, char* arg ) |
795 |
|
|
{ |
796 |
|
|
#ifdef ERR_DIR |
797 |
|
|
|
798 |
|
|
char filename[1000]; |
799 |
|
|
|
800 |
|
|
/* Try virtual host error page. */ |
801 |
|
|
if ( hc->hs->vhost && hc->hostdir[0] != '\0' ) |
802 |
|
|
{ |
803 |
|
|
(void) my_snprintf( filename, sizeof(filename), |
804 |
|
|
"%s/%s/err%d.html", hc->hostdir, ERR_DIR, status ); |
805 |
|
|
if ( send_err_file( hc, status, title, extraheads, filename ) ) |
806 |
|
|
return; |
807 |
|
|
} |
808 |
|
|
|
809 |
|
|
/* Try server-wide error page. */ |
810 |
|
|
(void) my_snprintf( filename, sizeof(filename), |
811 |
|
|
"%s/err%d.html", ERR_DIR, status ); |
812 |
|
|
if ( send_err_file( hc, status, title, extraheads, filename ) ) |
813 |
|
|
return; |
814 |
|
|
|
815 |
|
|
/* Fall back on built-in error page. */ |
816 |
|
|
send_response( hc, status, title, extraheads, form, arg ); |
817 |
|
|
|
818 |
|
|
#else /* ERR_DIR */ |
819 |
|
|
|
820 |
|
|
send_response( hc, status, title, extraheads, form, arg ); |
821 |
|
|
|
822 |
|
|
#endif /* ERR_DIR */ |
823 |
|
|
} |
824 |
|
|
|
825 |
root |
1.1.4.1 |
void |
826 |
|
|
httpd_send_err_blocked( httpd_conn* hc ) |
827 |
|
|
{ |
828 |
root |
1.1.4.4 |
char *protocol = hc->protocol; |
829 |
|
|
|
830 |
root |
1.1.4.1 |
#ifdef ERR_DIR |
831 |
|
|
char filename[1000]; |
832 |
root |
1.1.4.5 |
#endif |
833 |
root |
1.1.4.1 |
|
834 |
root |
1.1.4.5 |
hc->protocol = "HTTP/1.0"; |
835 |
|
|
|
836 |
|
|
#ifdef ERR_DIR |
837 |
root |
1.1.4.1 |
/* Try virtual host error page. */ |
838 |
|
|
if ( hc->hs->vhost && hc->hostdir[0] != '\0' ) |
839 |
|
|
{ |
840 |
|
|
(void) my_snprintf( filename, sizeof(filename), |
841 |
|
|
"%s/%s/err403blocked.html", hc->hostdir, ERR_DIR ); |
842 |
|
|
if ( send_err_file( hc, 403, err403title, "", filename ) ) |
843 |
|
|
return; |
844 |
|
|
} |
845 |
|
|
|
846 |
|
|
/* Try server-wide error page. */ |
847 |
|
|
(void) my_snprintf( filename, sizeof(filename), |
848 |
root |
1.1.4.2 |
"%s/err403blocked.html", ERR_DIR ); |
849 |
root |
1.1.4.1 |
if ( send_err_file( hc, 403, err403title, "", filename ) ) |
850 |
|
|
return; |
851 |
|
|
|
852 |
|
|
/* Fall back on built-in error page. */ |
853 |
|
|
send_response( hc, 403, err403title, "", err403form, "" ); |
854 |
|
|
|
855 |
|
|
#else /* ERR_DIR */ |
856 |
|
|
|
857 |
|
|
send_response( hc, 403, err403title, "", err403form, "" ); |
858 |
|
|
|
859 |
|
|
#endif /* ERR_DIR */ |
860 |
root |
1.1.4.4 |
hc->protocol = protocol; |
861 |
root |
1.1.4.1 |
} |
862 |
root |
1.1 |
|
863 |
|
|
#ifdef ERR_DIR |
864 |
|
|
static int |
865 |
|
|
send_err_file( httpd_conn* hc, int status, char* title, char* extraheads, char* filename ) |
866 |
|
|
{ |
867 |
|
|
FILE* fp; |
868 |
|
|
char buf[1000]; |
869 |
|
|
int r; |
870 |
|
|
|
871 |
|
|
fp = fopen( filename, "r" ); |
872 |
|
|
if ( fp == (FILE*) 0 ) |
873 |
|
|
return 0; |
874 |
|
|
send_mime( hc, status, title, "", extraheads, "text/html", -1, 0 ); |
875 |
|
|
for (;;) |
876 |
|
|
{ |
877 |
|
|
r = fread( buf, 1, sizeof(buf) - 1, fp ); |
878 |
|
|
if ( r <= 0 ) |
879 |
|
|
break; |
880 |
|
|
buf[r] = '\0'; |
881 |
|
|
add_response( hc, buf ); |
882 |
|
|
} |
883 |
|
|
(void) fclose( fp ); |
884 |
|
|
|
885 |
|
|
#ifdef ERR_APPEND_SERVER_INFO |
886 |
|
|
send_response_tail( hc ); |
887 |
|
|
#endif /* ERR_APPEND_SERVER_INFO */ |
888 |
|
|
|
889 |
|
|
return 1; |
890 |
|
|
} |
891 |
|
|
#endif /* ERR_DIR */ |
892 |
|
|
|
893 |
|
|
|
894 |
|
|
#ifdef AUTH_FILE |
895 |
|
|
|
896 |
|
|
static void |
897 |
|
|
send_authenticate( httpd_conn* hc, char* realm ) |
898 |
|
|
{ |
899 |
|
|
static char* header; |
900 |
|
|
static int maxheader = 0; |
901 |
|
|
static char headstr[] = "WWW-Authenticate: Basic realm=\""; |
902 |
|
|
|
903 |
|
|
httpd_realloc_str( |
904 |
|
|
&header, &maxheader, sizeof(headstr) + strlen( realm ) + 3 ); |
905 |
|
|
(void) my_snprintf( header, maxheader, "%s%s\"\r\n", headstr, realm ); |
906 |
|
|
httpd_send_err( hc, 401, err401title, header, err401form, hc->encodedurl ); |
907 |
|
|
/* If the request was a POST then there might still be data to be read, |
908 |
|
|
** so we need to do a lingering close. |
909 |
|
|
*/ |
910 |
|
|
if ( hc->method == METHOD_POST ) |
911 |
|
|
hc->should_linger = 1; |
912 |
|
|
} |
913 |
|
|
|
914 |
|
|
|
915 |
|
|
/* Base-64 decoding. This represents binary data as printable ASCII |
916 |
|
|
** characters. Three 8-bit binary bytes are turned into four 6-bit |
917 |
|
|
** values, like so: |
918 |
|
|
** |
919 |
|
|
** [11111111] [22222222] [33333333] |
920 |
|
|
** |
921 |
|
|
** [111111] [112222] [222233] [333333] |
922 |
|
|
** |
923 |
|
|
** Then the 6-bit values are represented using the characters "A-Za-z0-9+/". |
924 |
|
|
*/ |
925 |
|
|
|
926 |
|
|
static int b64_decode_table[256] = { |
927 |
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */ |
928 |
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */ |
929 |
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */ |
930 |
|
|
52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */ |
931 |
|
|
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */ |
932 |
|
|
15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */ |
933 |
|
|
-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */ |
934 |
|
|
41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */ |
935 |
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */ |
936 |
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */ |
937 |
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */ |
938 |
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */ |
939 |
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */ |
940 |
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */ |
941 |
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */ |
942 |
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */ |
943 |
|
|
}; |
944 |
|
|
|
945 |
|
|
/* Do base-64 decoding on a string. Ignore any non-base64 bytes. |
946 |
|
|
** Return the actual number of bytes generated. The decoded size will |
947 |
|
|
** be at most 3/4 the size of the encoded, and may be smaller if there |
948 |
|
|
** are padding characters (blanks, newlines). |
949 |
|
|
*/ |
950 |
|
|
static int |
951 |
|
|
b64_decode( const char* str, unsigned char* space, int size ) |
952 |
|
|
{ |
953 |
|
|
const char* cp; |
954 |
|
|
int space_idx, phase; |
955 |
|
|
int d, prev_d = 0; |
956 |
|
|
unsigned char c; |
957 |
|
|
|
958 |
|
|
space_idx = 0; |
959 |
|
|
phase = 0; |
960 |
|
|
for ( cp = str; *cp != '\0'; ++cp ) |
961 |
|
|
{ |
962 |
|
|
d = b64_decode_table[(int) *cp]; |
963 |
|
|
if ( d != -1 ) |
964 |
|
|
{ |
965 |
|
|
switch ( phase ) |
966 |
|
|
{ |
967 |
|
|
case 0: |
968 |
|
|
++phase; |
969 |
|
|
break; |
970 |
|
|
case 1: |
971 |
|
|
c = ( ( prev_d << 2 ) | ( ( d & 0x30 ) >> 4 ) ); |
972 |
|
|
if ( space_idx < size ) |
973 |
|
|
space[space_idx++] = c; |
974 |
|
|
++phase; |
975 |
|
|
break; |
976 |
|
|
case 2: |
977 |
|
|
c = ( ( ( prev_d & 0xf ) << 4 ) | ( ( d & 0x3c ) >> 2 ) ); |
978 |
|
|
if ( space_idx < size ) |
979 |
|
|
space[space_idx++] = c; |
980 |
|
|
++phase; |
981 |
|
|
break; |
982 |
|
|
case 3: |
983 |
|
|
c = ( ( ( prev_d & 0x03 ) << 6 ) | d ); |
984 |
|
|
if ( space_idx < size ) |
985 |
|
|
space[space_idx++] = c; |
986 |
|
|
phase = 0; |
987 |
|
|
break; |
988 |
|
|
} |
989 |
|
|
prev_d = d; |
990 |
|
|
} |
991 |
|
|
} |
992 |
|
|
return space_idx; |
993 |
|
|
} |
994 |
|
|
|
995 |
|
|
|
996 |
|
|
/* Returns -1 == unauthorized, 0 == no auth file, 1 = authorized. */ |
997 |
|
|
static int |
998 |
|
|
auth_check( httpd_conn* hc, char* dirname ) |
999 |
|
|
{ |
1000 |
|
|
if ( hc->hs->global_passwd ) |
1001 |
|
|
{ |
1002 |
|
|
char* topdir; |
1003 |
|
|
if ( hc->hs->vhost && hc->hostdir[0] != '\0' ) |
1004 |
|
|
topdir = hc->hostdir; |
1005 |
|
|
else |
1006 |
|
|
topdir = "."; |
1007 |
|
|
switch ( auth_check2( hc, topdir ) ) |
1008 |
|
|
{ |
1009 |
|
|
case -1: |
1010 |
|
|
return -1; |
1011 |
|
|
case 1: |
1012 |
|
|
return 1; |
1013 |
|
|
} |
1014 |
|
|
} |
1015 |
|
|
return auth_check2( hc, dirname ); |
1016 |
|
|
} |
1017 |
|
|
|
1018 |
|
|
|
1019 |
|
|
/* Returns -1 == unauthorized, 0 == no auth file, 1 = authorized. */ |
1020 |
|
|
static int |
1021 |
|
|
auth_check2( httpd_conn* hc, char* dirname ) |
1022 |
|
|
{ |
1023 |
|
|
static char* authpath; |
1024 |
|
|
static int maxauthpath = 0; |
1025 |
|
|
struct stat sb; |
1026 |
|
|
char authinfo[500]; |
1027 |
|
|
char* authpass; |
1028 |
|
|
int l; |
1029 |
|
|
FILE* fp; |
1030 |
|
|
char line[500]; |
1031 |
|
|
char* cryp; |
1032 |
|
|
static char* prevauthpath; |
1033 |
|
|
static int maxprevauthpath = 0; |
1034 |
|
|
static time_t prevmtime; |
1035 |
|
|
static char* prevuser; |
1036 |
|
|
static int maxprevuser = 0; |
1037 |
|
|
static char* prevcryp; |
1038 |
|
|
static int maxprevcryp = 0; |
1039 |
|
|
|
1040 |
|
|
/* Construct auth filename. */ |
1041 |
|
|
httpd_realloc_str( |
1042 |
|
|
&authpath, &maxauthpath, strlen( dirname ) + 1 + sizeof(AUTH_FILE) ); |
1043 |
|
|
(void) my_snprintf( authpath, maxauthpath, |
1044 |
|
|
"%s/%s", dirname, AUTH_FILE ); |
1045 |
|
|
|
1046 |
|
|
/* Does this directory have an auth file? */ |
1047 |
|
|
if ( stat( authpath, &sb ) < 0 ) |
1048 |
|
|
/* Nope, let the request go through. */ |
1049 |
|
|
return 0; |
1050 |
|
|
|
1051 |
|
|
/* Does this request contain basic authorization info? */ |
1052 |
|
|
if ( hc->authorization[0] == '\0' || |
1053 |
|
|
strncmp( hc->authorization, "Basic ", 6 ) != 0 ) |
1054 |
|
|
{ |
1055 |
|
|
/* Nope, return a 401 Unauthorized. */ |
1056 |
|
|
send_authenticate( hc, dirname ); |
1057 |
|
|
return -1; |
1058 |
|
|
} |
1059 |
|
|
|
1060 |
|
|
/* Decode it. */ |
1061 |
|
|
l = b64_decode( &(hc->authorization[6]), authinfo, sizeof(authinfo) - 1 ); |
1062 |
|
|
authinfo[l] = '\0'; |
1063 |
|
|
/* Split into user and password. */ |
1064 |
|
|
authpass = strchr( authinfo, ':' ); |
1065 |
|
|
if ( authpass == (char*) 0 ) |
1066 |
|
|
{ |
1067 |
|
|
/* No colon? Bogus auth info. */ |
1068 |
|
|
send_authenticate( hc, dirname ); |
1069 |
|
|
return -1; |
1070 |
|
|
} |
1071 |
|
|
*authpass++ = '\0'; |
1072 |
|
|
|
1073 |
|
|
/* See if we have a cached entry and can use it. */ |
1074 |
|
|
if ( maxprevauthpath != 0 && |
1075 |
|
|
strcmp( authpath, prevauthpath ) == 0 && |
1076 |
|
|
sb.st_mtime == prevmtime && |
1077 |
|
|
strcmp( authinfo, prevuser ) == 0 ) |
1078 |
|
|
{ |
1079 |
|
|
/* Yes. Check against the cached encrypted password. */ |
1080 |
|
|
if ( strcmp( crypt( authpass, prevcryp ), prevcryp ) == 0 ) |
1081 |
|
|
{ |
1082 |
|
|
/* Ok! */ |
1083 |
|
|
httpd_realloc_str( |
1084 |
|
|
&hc->remoteuser, &hc->maxremoteuser, strlen( authinfo ) ); |
1085 |
|
|
(void) strcpy( hc->remoteuser, authinfo ); |
1086 |
|
|
return 1; |
1087 |
|
|
} |
1088 |
|
|
else |
1089 |
|
|
{ |
1090 |
|
|
/* No. */ |
1091 |
|
|
send_authenticate( hc, dirname ); |
1092 |
|
|
return -1; |
1093 |
|
|
} |
1094 |
|
|
} |
1095 |
|
|
|
1096 |
|
|
/* Open the password file. */ |
1097 |
|
|
fp = fopen( authpath, "r" ); |
1098 |
|
|
if ( fp == (FILE*) 0 ) |
1099 |
|
|
{ |
1100 |
|
|
/* The file exists but we can't open it? Disallow access. */ |
1101 |
|
|
syslog( |
1102 |
|
|
LOG_ERR, "%.80s auth file %.80s could not be opened - %m", |
1103 |
|
|
httpd_ntoa( &hc->client_addr ), authpath ); |
1104 |
|
|
httpd_send_err( |
1105 |
|
|
hc, 403, err403title, "", |
1106 |
|
|
ERROR_FORM( err403form, "The requested URL '%.80s' is protected by an authentication file, but the authentication file cannot be opened.\n" ), |
1107 |
|
|
hc->encodedurl ); |
1108 |
|
|
return -1; |
1109 |
|
|
} |
1110 |
|
|
|
1111 |
|
|
/* Read it. */ |
1112 |
|
|
while ( fgets( line, sizeof(line), fp ) != (char*) 0 ) |
1113 |
|
|
{ |
1114 |
|
|
/* Nuke newline. */ |
1115 |
|
|
l = strlen( line ); |
1116 |
|
|
if ( line[l - 1] == '\n' ) |
1117 |
|
|
line[l - 1] = '\0'; |
1118 |
|
|
/* Split into user and encrypted password. */ |
1119 |
|
|
cryp = strchr( line, ':' ); |
1120 |
|
|
if ( cryp == (char*) 0 ) |
1121 |
|
|
continue; |
1122 |
|
|
*cryp++ = '\0'; |
1123 |
|
|
/* Is this the right user? */ |
1124 |
|
|
if ( strcmp( line, authinfo ) == 0 ) |
1125 |
|
|
{ |
1126 |
|
|
/* Yes. */ |
1127 |
|
|
(void) fclose( fp ); |
1128 |
|
|
/* So is the password right? */ |
1129 |
|
|
if ( strcmp( crypt( authpass, cryp ), cryp ) == 0 ) |
1130 |
|
|
{ |
1131 |
|
|
/* Ok! */ |
1132 |
|
|
httpd_realloc_str( |
1133 |
|
|
&hc->remoteuser, &hc->maxremoteuser, strlen( line ) ); |
1134 |
|
|
(void) strcpy( hc->remoteuser, line ); |
1135 |
|
|
/* And cache this user's info for next time. */ |
1136 |
|
|
httpd_realloc_str( |
1137 |
|
|
&prevauthpath, &maxprevauthpath, strlen( authpath ) ); |
1138 |
|
|
(void) strcpy( prevauthpath, authpath ); |
1139 |
|
|
prevmtime = sb.st_mtime; |
1140 |
|
|
httpd_realloc_str( |
1141 |
|
|
&prevuser, &maxprevuser, strlen( authinfo ) ); |
1142 |
|
|
(void) strcpy( prevuser, authinfo ); |
1143 |
|
|
httpd_realloc_str( &prevcryp, &maxprevcryp, strlen( cryp ) ); |
1144 |
|
|
(void) strcpy( prevcryp, cryp ); |
1145 |
|
|
return 1; |
1146 |
|
|
} |
1147 |
|
|
else |
1148 |
|
|
{ |
1149 |
|
|
/* No. */ |
1150 |
|
|
send_authenticate( hc, dirname ); |
1151 |
|
|
return -1; |
1152 |
|
|
} |
1153 |
|
|
} |
1154 |
|
|
} |
1155 |
|
|
|
1156 |
|
|
/* Didn't find that user. Access denied. */ |
1157 |
|
|
(void) fclose( fp ); |
1158 |
|
|
send_authenticate( hc, dirname ); |
1159 |
|
|
return -1; |
1160 |
|
|
} |
1161 |
|
|
|
1162 |
|
|
#endif /* AUTH_FILE */ |
1163 |
|
|
|
1164 |
|
|
|
1165 |
|
|
static void |
1166 |
|
|
send_dirredirect( httpd_conn* hc ) |
1167 |
|
|
{ |
1168 |
|
|
static char* location; |
1169 |
|
|
static char* header; |
1170 |
|
|
static int maxlocation = 0, maxheader = 0; |
1171 |
|
|
static char headstr[] = "Location: "; |
1172 |
|
|
|
1173 |
|
|
httpd_realloc_str( &location, &maxlocation, strlen( hc->encodedurl ) + 1 ); |
1174 |
|
|
(void) my_snprintf( location, maxlocation, |
1175 |
|
|
"%s/", hc->encodedurl ); |
1176 |
|
|
httpd_realloc_str( |
1177 |
|
|
&header, &maxheader, sizeof(headstr) + strlen( location ) ); |
1178 |
|
|
(void) my_snprintf( header, maxheader, |
1179 |
|
|
"%s%s\r\n", headstr, location ); |
1180 |
|
|
send_response( hc, 302, err302title, header, err302form, location ); |
1181 |
|
|
} |
1182 |
|
|
|
1183 |
|
|
|
1184 |
|
|
char* |
1185 |
|
|
httpd_method_str( int method ) |
1186 |
|
|
{ |
1187 |
|
|
switch ( method ) |
1188 |
|
|
{ |
1189 |
|
|
case METHOD_GET: return "GET"; |
1190 |
|
|
case METHOD_HEAD: return "HEAD"; |
1191 |
|
|
case METHOD_POST: return "POST"; |
1192 |
|
|
default: return "UNKNOWN"; |
1193 |
|
|
} |
1194 |
|
|
} |
1195 |
|
|
|
1196 |
|
|
|
1197 |
|
|
static int |
1198 |
|
|
hexit( char c ) |
1199 |
|
|
{ |
1200 |
|
|
if ( c >= '0' && c <= '9' ) |
1201 |
|
|
return c - '0'; |
1202 |
|
|
if ( c >= 'a' && c <= 'f' ) |
1203 |
|
|
return c - 'a' + 10; |
1204 |
|
|
if ( c >= 'A' && c <= 'F' ) |
1205 |
|
|
return c - 'A' + 10; |
1206 |
|
|
return 0; /* shouldn't happen, we're guarded by isxdigit() */ |
1207 |
|
|
} |
1208 |
|
|
|
1209 |
|
|
|
1210 |
|
|
/* Copies and decodes a string. It's ok for from and to to be the |
1211 |
|
|
** same string. |
1212 |
|
|
*/ |
1213 |
|
|
static void |
1214 |
|
|
strdecode( char* to, char* from ) |
1215 |
|
|
{ |
1216 |
|
|
for ( ; *from != '\0'; ++to, ++from ) |
1217 |
|
|
{ |
1218 |
|
|
if ( from[0] == '%' && isxdigit( from[1] ) && isxdigit( from[2] ) ) |
1219 |
|
|
{ |
1220 |
|
|
*to = hexit( from[1] ) * 16 + hexit( from[2] ); |
1221 |
|
|
from += 2; |
1222 |
|
|
} |
1223 |
|
|
else |
1224 |
|
|
*to = *from; |
1225 |
|
|
} |
1226 |
|
|
*to = '\0'; |
1227 |
|
|
} |
1228 |
|
|
|
1229 |
|
|
|
1230 |
|
|
#ifdef GENERATE_INDEXES |
1231 |
|
|
/* Copies and encodes a string. */ |
1232 |
|
|
static void |
1233 |
|
|
strencode( char* to, int tosize, char* from ) |
1234 |
|
|
{ |
1235 |
|
|
int tolen; |
1236 |
|
|
|
1237 |
|
|
for ( tolen = 0; *from != '\0' && tolen + 4 < tosize; ++from ) |
1238 |
|
|
{ |
1239 |
|
|
if ( isalnum(*from) || strchr( "/_.-~", *from ) != (char*) 0 ) |
1240 |
|
|
{ |
1241 |
|
|
*to = *from; |
1242 |
|
|
++to; |
1243 |
|
|
++tolen; |
1244 |
|
|
} |
1245 |
|
|
else |
1246 |
|
|
{ |
1247 |
|
|
(void) sprintf( to, "%%%02x", (int) *from & 0xff ); |
1248 |
|
|
to += 3; |
1249 |
|
|
tolen += 3; |
1250 |
|
|
} |
1251 |
|
|
} |
1252 |
|
|
*to = '\0'; |
1253 |
|
|
} |
1254 |
|
|
#endif /* GENERATE_INDEXES */ |
1255 |
|
|
|
1256 |
|
|
|
1257 |
|
|
#ifdef TILDE_MAP_1 |
1258 |
|
|
/* Map a ~username/whatever URL into <prefix>/username. */ |
1259 |
|
|
static int |
1260 |
|
|
tilde_map_1( httpd_conn* hc ) |
1261 |
|
|
{ |
1262 |
|
|
static char* temp; |
1263 |
|
|
static int maxtemp = 0; |
1264 |
|
|
int len; |
1265 |
|
|
static char* prefix = TILDE_MAP_1; |
1266 |
|
|
|
1267 |
|
|
len = strlen( hc->expnfilename ) - 1; |
1268 |
|
|
httpd_realloc_str( &temp, &maxtemp, len ); |
1269 |
|
|
(void) strcpy( temp, &hc->expnfilename[1] ); |
1270 |
|
|
httpd_realloc_str( |
1271 |
|
|
&hc->expnfilename, &hc->maxexpnfilename, strlen( prefix ) + 1 + len ); |
1272 |
|
|
(void) strcpy( hc->expnfilename, prefix ); |
1273 |
|
|
if ( prefix[0] != '\0' ) |
1274 |
|
|
(void) strcat( hc->expnfilename, "/" ); |
1275 |
|
|
(void) strcat( hc->expnfilename, temp ); |
1276 |
|
|
return 1; |
1277 |
|
|
} |
1278 |
|
|
#endif /* TILDE_MAP_1 */ |
1279 |
|
|
|
1280 |
|
|
#ifdef TILDE_MAP_2 |
1281 |
|
|
/* Map a ~username/whatever URL into <user's homedir>/<postfix>. */ |
1282 |
|
|
static int |
1283 |
|
|
tilde_map_2( httpd_conn* hc ) |
1284 |
|
|
{ |
1285 |
|
|
static char* temp; |
1286 |
|
|
static int maxtemp = 0; |
1287 |
|
|
static char* postfix = TILDE_MAP_2; |
1288 |
|
|
char* cp; |
1289 |
|
|
struct passwd* pw; |
1290 |
|
|
char* alt; |
1291 |
|
|
char* rest; |
1292 |
|
|
|
1293 |
|
|
/* Get the username. */ |
1294 |
|
|
httpd_realloc_str( &temp, &maxtemp, strlen( hc->expnfilename ) - 1 ); |
1295 |
|
|
(void) strcpy( temp, &hc->expnfilename[1] ); |
1296 |
|
|
cp = strchr( temp, '/' ); |
1297 |
|
|
if ( cp != (char*) 0 ) |
1298 |
|
|
*cp++ = '\0'; |
1299 |
|
|
else |
1300 |
|
|
cp = ""; |
1301 |
|
|
|
1302 |
|
|
/* Get the passwd entry. */ |
1303 |
|
|
pw = getpwnam( temp ); |
1304 |
|
|
if ( pw == (struct passwd*) 0 ) |
1305 |
|
|
return 0; |
1306 |
|
|
|
1307 |
|
|
/* Set up altdir. */ |
1308 |
|
|
httpd_realloc_str( |
1309 |
|
|
&hc->altdir, &hc->maxaltdir, |
1310 |
|
|
strlen( pw->pw_dir ) + 1 + strlen( postfix ) ); |
1311 |
|
|
(void) strcpy( hc->altdir, pw->pw_dir ); |
1312 |
|
|
if ( postfix[0] != '\0' ) |
1313 |
|
|
{ |
1314 |
|
|
(void) strcat( hc->altdir, "/" ); |
1315 |
|
|
(void) strcat( hc->altdir, postfix ); |
1316 |
|
|
} |
1317 |
|
|
alt = expand_symlinks( hc->altdir, &rest, 0, 1 ); |
1318 |
|
|
if ( rest[0] != '\0' ) |
1319 |
|
|
return 0; |
1320 |
|
|
httpd_realloc_str( &hc->altdir, &hc->maxaltdir, strlen( alt ) ); |
1321 |
|
|
(void) strcpy( hc->altdir, alt ); |
1322 |
|
|
|
1323 |
|
|
/* And the filename becomes altdir plus the post-~ part of the original. */ |
1324 |
|
|
httpd_realloc_str( |
1325 |
|
|
&hc->expnfilename, &hc->maxexpnfilename, |
1326 |
|
|
strlen( hc->altdir ) + 1 + strlen( cp ) ); |
1327 |
|
|
(void) my_snprintf( hc->expnfilename, hc->maxexpnfilename, |
1328 |
|
|
"%s/%s", hc->altdir, cp ); |
1329 |
|
|
|
1330 |
|
|
/* For this type of tilde mapping, we want to defeat vhost mapping. */ |
1331 |
|
|
hc->tildemapped = 1; |
1332 |
|
|
|
1333 |
|
|
return 1; |
1334 |
|
|
} |
1335 |
|
|
#endif /* TILDE_MAP_2 */ |
1336 |
|
|
|
1337 |
|
|
|
1338 |
|
|
/* Virtual host mapping. */ |
1339 |
|
|
static int |
1340 |
|
|
vhost_map( httpd_conn* hc ) |
1341 |
|
|
{ |
1342 |
|
|
httpd_sockaddr sa; |
1343 |
|
|
int sz; |
1344 |
|
|
static char* tempfilename; |
1345 |
|
|
static int maxtempfilename = 0; |
1346 |
|
|
char* cp1; |
1347 |
|
|
int len; |
1348 |
|
|
#ifdef VHOST_DIRLEVELS |
1349 |
|
|
int i; |
1350 |
|
|
char* cp2; |
1351 |
|
|
#endif /* VHOST_DIRLEVELS */ |
1352 |
|
|
|
1353 |
|
|
/* Figure out the virtual hostname. */ |
1354 |
|
|
if ( hc->reqhost[0] != '\0' ) |
1355 |
|
|
hc->hostname = hc->reqhost; |
1356 |
|
|
else if ( hc->hdrhost[0] != '\0' ) |
1357 |
|
|
hc->hostname = hc->hdrhost; |
1358 |
|
|
else |
1359 |
|
|
{ |
1360 |
|
|
sz = sizeof(sa); |
1361 |
|
|
if ( getsockname( hc->conn_fd, &sa.sa, &sz ) < 0 ) |
1362 |
|
|
{ |
1363 |
|
|
syslog( LOG_ERR, "getsockname - %m" ); |
1364 |
|
|
return 0; |
1365 |
|
|
} |
1366 |
|
|
hc->hostname = httpd_ntoa( &sa ); |
1367 |
|
|
} |
1368 |
|
|
/* Pound it to lower case. */ |
1369 |
|
|
for ( cp1 = hc->hostname; *cp1 != '\0'; ++cp1 ) |
1370 |
|
|
if ( isupper( *cp1 ) ) |
1371 |
|
|
*cp1 = tolower( *cp1 ); |
1372 |
|
|
|
1373 |
|
|
if ( hc->tildemapped ) |
1374 |
|
|
return 1; |
1375 |
|
|
|
1376 |
|
|
/* Figure out the host directory. */ |
1377 |
|
|
#ifdef VHOST_DIRLEVELS |
1378 |
|
|
httpd_realloc_str( |
1379 |
|
|
&hc->hostdir, &hc->maxhostdir, |
1380 |
|
|
strlen( hc->hostname ) + 2 * VHOST_DIRLEVELS ); |
1381 |
|
|
if ( strncmp( hc->hostname, "www.", 4 ) == 0 ) |
1382 |
|
|
cp1 = &hc->hostname[4]; |
1383 |
|
|
else |
1384 |
|
|
cp1 = hc->hostname; |
1385 |
|
|
for ( cp2 = hc->hostdir, i = 0; i < VHOST_DIRLEVELS; ++i ) |
1386 |
|
|
{ |
1387 |
|
|
/* Skip dots in the hostname. If we don't, then we get vhost |
1388 |
|
|
** directories in higher level of filestructure if dot gets |
1389 |
|
|
** involved into path construction. It's `while' used here instead |
1390 |
|
|
** of `if' for it's possible to have a hostname formed with two |
1391 |
|
|
** dots at the end of it. |
1392 |
|
|
*/ |
1393 |
|
|
while ( *cp1 == '.' ) |
1394 |
|
|
++cp1; |
1395 |
|
|
/* Copy a character from the hostname, or '_' if we ran out. */ |
1396 |
|
|
if ( *cp1 != '\0' ) |
1397 |
|
|
*cp2++ = *cp1++; |
1398 |
|
|
else |
1399 |
|
|
*cp2++ = '_'; |
1400 |
|
|
/* Copy a slash. */ |
1401 |
|
|
*cp2++ = '/'; |
1402 |
|
|
} |
1403 |
|
|
(void) strcpy( cp2, hc->hostname ); |
1404 |
|
|
#else /* VHOST_DIRLEVELS */ |
1405 |
|
|
httpd_realloc_str( &hc->hostdir, &hc->maxhostdir, strlen( hc->hostname ) ); |
1406 |
|
|
(void) strcpy( hc->hostdir, hc->hostname ); |
1407 |
|
|
#endif /* VHOST_DIRLEVELS */ |
1408 |
|
|
|
1409 |
|
|
/* Prepend hostdir to the filename. */ |
1410 |
|
|
len = strlen( hc->expnfilename ); |
1411 |
|
|
httpd_realloc_str( &tempfilename, &maxtempfilename, len ); |
1412 |
|
|
(void) strcpy( tempfilename, hc->expnfilename ); |
1413 |
|
|
httpd_realloc_str( |
1414 |
|
|
&hc->expnfilename, &hc->maxexpnfilename, |
1415 |
|
|
strlen( hc->hostdir ) + 1 + len ); |
1416 |
|
|
(void) strcpy( hc->expnfilename, hc->hostdir ); |
1417 |
|
|
(void) strcat( hc->expnfilename, "/" ); |
1418 |
|
|
(void) strcat( hc->expnfilename, tempfilename ); |
1419 |
|
|
return 1; |
1420 |
|
|
} |
1421 |
|
|
|
1422 |
|
|
|
1423 |
|
|
/* Expands all symlinks in the given filename, eliding ..'s and leading /'s. |
1424 |
|
|
** Returns the expanded path (pointer to static string), or (char*) 0 on |
1425 |
|
|
** errors. Also returns, in the string pointed to by restP, any trailing |
1426 |
|
|
** parts of the path that don't exist. |
1427 |
|
|
** |
1428 |
|
|
** This is a fairly nice little routine. It handles any size filenames |
1429 |
|
|
** without excessive mallocs. |
1430 |
|
|
*/ |
1431 |
|
|
static char* |
1432 |
|
|
expand_symlinks( char* path, char** restP, int no_symlink, int tildemapped ) |
1433 |
|
|
{ |
1434 |
|
|
static char* checked; |
1435 |
|
|
static char* rest; |
1436 |
|
|
char link[5000]; |
1437 |
|
|
static int maxchecked = 0, maxrest = 0; |
1438 |
|
|
int checkedlen, restlen, linklen, prevcheckedlen, prevrestlen, nlinks, i; |
1439 |
|
|
char* r; |
1440 |
|
|
char* cp1; |
1441 |
|
|
char* cp2; |
1442 |
|
|
|
1443 |
|
|
if ( no_symlink ) |
1444 |
|
|
{ |
1445 |
|
|
/* If we are chrooted, we can actually skip the symlink-expansion, |
1446 |
|
|
** since it's impossible to get out of the tree. However, we still |
1447 |
|
|
** need to do the pathinfo check, and the existing symlink expansion |
1448 |
|
|
** code is a pretty reasonable way to do this. So, what we do is |
1449 |
|
|
** a single stat() of the whole filename - if it exists, then we |
1450 |
|
|
** return it as is with nothing in restP. If it doesn't exist, we |
1451 |
|
|
** fall through to the existing code. |
1452 |
|
|
** |
1453 |
|
|
** One side-effect of this is that users can't symlink to central |
1454 |
|
|
** approved CGIs any more. The workaround is to use the central |
1455 |
|
|
** URL for the CGI instead of a local symlinked one. |
1456 |
|
|
*/ |
1457 |
|
|
struct stat sb; |
1458 |
|
|
if ( stat( path, &sb ) != -1 ) |
1459 |
|
|
{ |
1460 |
|
|
httpd_realloc_str( &checked, &maxchecked, strlen( path ) ); |
1461 |
|
|
(void) strcpy( checked, path ); |
1462 |
|
|
httpd_realloc_str( &rest, &maxrest, 0 ); |
1463 |
|
|
rest[0] = '\0'; |
1464 |
|
|
*restP = rest; |
1465 |
|
|
return checked; |
1466 |
|
|
} |
1467 |
|
|
} |
1468 |
|
|
|
1469 |
|
|
/* Start out with nothing in checked and the whole filename in rest. */ |
1470 |
|
|
httpd_realloc_str( &checked, &maxchecked, 1 ); |
1471 |
|
|
checked[0] = '\0'; |
1472 |
|
|
checkedlen = 0; |
1473 |
|
|
restlen = strlen( path ); |
1474 |
|
|
httpd_realloc_str( &rest, &maxrest, restlen ); |
1475 |
|
|
(void) strcpy( rest, path ); |
1476 |
|
|
if ( rest[restlen - 1] == '/' ) |
1477 |
|
|
rest[--restlen] = '\0'; /* trim trailing slash */ |
1478 |
|
|
if ( ! tildemapped ) |
1479 |
|
|
/* Remove any leading slashes. */ |
1480 |
|
|
while ( rest[0] == '/' ) |
1481 |
|
|
{ |
1482 |
|
|
(void) strcpy( rest, &(rest[1]) ); |
1483 |
|
|
--restlen; |
1484 |
|
|
} |
1485 |
|
|
r = rest; |
1486 |
|
|
nlinks = 0; |
1487 |
|
|
|
1488 |
|
|
/* While there are still components to check... */ |
1489 |
|
|
while ( restlen > 0 ) |
1490 |
|
|
{ |
1491 |
|
|
/* Save current checkedlen in case we get a symlink. Save current |
1492 |
|
|
** restlen in case we get a non-existant component. |
1493 |
|
|
*/ |
1494 |
|
|
prevcheckedlen = checkedlen; |
1495 |
|
|
prevrestlen = restlen; |
1496 |
|
|
|
1497 |
|
|
/* Grab one component from r and transfer it to checked. */ |
1498 |
|
|
cp1 = strchr( r, '/' ); |
1499 |
|
|
if ( cp1 != (char*) 0 ) |
1500 |
|
|
{ |
1501 |
|
|
i = cp1 - r; |
1502 |
|
|
if ( i == 0 ) |
1503 |
|
|
{ |
1504 |
|
|
/* Special case for absolute paths. */ |
1505 |
|
|
httpd_realloc_str( &checked, &maxchecked, checkedlen + 1 ); |
1506 |
|
|
(void) strncpy( &checked[checkedlen], r, 1 ); |
1507 |
|
|
checkedlen += 1; |
1508 |
|
|
} |
1509 |
|
|
else if ( strncmp( r, "..", MAX( i, 2 ) ) == 0 ) |
1510 |
|
|
{ |
1511 |
|
|
/* Ignore ..'s that go above the start of the path. */ |
1512 |
|
|
if ( checkedlen != 0 ) |
1513 |
|
|
{ |
1514 |
|
|
cp2 = strrchr( checked, '/' ); |
1515 |
|
|
if ( cp2 == (char*) 0 ) |
1516 |
|
|
checkedlen = 0; |
1517 |
|
|
else if ( cp2 == checked ) |
1518 |
|
|
checkedlen = 1; |
1519 |
|
|
else |
1520 |
|
|
checkedlen = cp2 - checked; |
1521 |
|
|
} |
1522 |
|
|
} |
1523 |
|
|
else |
1524 |
|
|
{ |
1525 |
|
|
httpd_realloc_str( &checked, &maxchecked, checkedlen + 1 + i ); |
1526 |
|
|
if ( checkedlen > 0 && checked[checkedlen-1] != '/' ) |
1527 |
|
|
checked[checkedlen++] = '/'; |
1528 |
|
|
(void) strncpy( &checked[checkedlen], r, i ); |
1529 |
|
|
checkedlen += i; |
1530 |
|
|
} |
1531 |
|
|
checked[checkedlen] = '\0'; |
1532 |
|
|
r += i + 1; |
1533 |
|
|
restlen -= i + 1; |
1534 |
|
|
} |
1535 |
|
|
else |
1536 |
|
|
{ |
1537 |
|
|
/* No slashes remaining, r is all one component. */ |
1538 |
|
|
if ( strcmp( r, ".." ) == 0 ) |
1539 |
|
|
{ |
1540 |
|
|
/* Ignore ..'s that go above the start of the path. */ |
1541 |
|
|
if ( checkedlen != 0 ) |
1542 |
|
|
{ |
1543 |
|
|
cp2 = strrchr( checked, '/' ); |
1544 |
|
|
if ( cp2 == (char*) 0 ) |
1545 |
|
|
checkedlen = 0; |
1546 |
|
|
else if ( cp2 == checked ) |
1547 |
|
|
checkedlen = 1; |
1548 |
|
|
else |
1549 |
|
|
checkedlen = cp2 - checked; |
1550 |
|
|
checked[checkedlen] = '\0'; |
1551 |
|
|
} |
1552 |
|
|
} |
1553 |
|
|
else |
1554 |
|
|
{ |
1555 |
|
|
httpd_realloc_str( |
1556 |
|
|
&checked, &maxchecked, checkedlen + 1 + restlen ); |
1557 |
|
|
if ( checkedlen > 0 && checked[checkedlen-1] != '/' ) |
1558 |
|
|
checked[checkedlen++] = '/'; |
1559 |
|
|
(void) strcpy( &checked[checkedlen], r ); |
1560 |
|
|
checkedlen += restlen; |
1561 |
|
|
} |
1562 |
|
|
r += restlen; |
1563 |
|
|
restlen = 0; |
1564 |
|
|
} |
1565 |
|
|
|
1566 |
|
|
/* Try reading the current filename as a symlink */ |
1567 |
|
|
if ( checked[0] == '\0' ) |
1568 |
|
|
continue; |
1569 |
|
|
linklen = readlink( checked, link, sizeof(link) ); |
1570 |
|
|
if ( linklen == -1 ) |
1571 |
|
|
{ |
1572 |
|
|
if ( errno == EINVAL ) |
1573 |
|
|
continue; /* not a symlink */ |
1574 |
|
|
if ( errno == EACCES || errno == ENOENT || errno == ENOTDIR ) |
1575 |
|
|
{ |
1576 |
|
|
/* That last component was bogus. Restore and return. */ |
1577 |
|
|
*restP = r - ( prevrestlen - restlen ); |
1578 |
|
|
if ( prevcheckedlen == 0 ) |
1579 |
|
|
(void) strcpy( checked, "." ); |
1580 |
|
|
else |
1581 |
|
|
checked[prevcheckedlen] = '\0'; |
1582 |
|
|
return checked; |
1583 |
|
|
} |
1584 |
|
|
syslog( LOG_ERR, "readlink %.80s - %m", checked ); |
1585 |
|
|
return (char*) 0; |
1586 |
|
|
} |
1587 |
|
|
++nlinks; |
1588 |
|
|
if ( nlinks > MAX_LINKS ) |
1589 |
|
|
{ |
1590 |
|
|
syslog( LOG_ERR, "too many symlinks in %.80s", path ); |
1591 |
|
|
return (char*) 0; |
1592 |
|
|
} |
1593 |
|
|
link[linklen] = '\0'; |
1594 |
|
|
if ( link[linklen - 1] == '/' ) |
1595 |
|
|
link[--linklen] = '\0'; /* trim trailing slash */ |
1596 |
|
|
|
1597 |
|
|
/* Insert the link contents in front of the rest of the filename. */ |
1598 |
|
|
if ( restlen != 0 ) |
1599 |
|
|
{ |
1600 |
|
|
(void) strcpy( rest, r ); |
1601 |
|
|
httpd_realloc_str( &rest, &maxrest, restlen + linklen + 1 ); |
1602 |
|
|
for ( i = restlen; i >= 0; --i ) |
1603 |
|
|
rest[i + linklen + 1] = rest[i]; |
1604 |
|
|
(void) strcpy( rest, link ); |
1605 |
|
|
rest[linklen] = '/'; |
1606 |
|
|
restlen += linklen + 1; |
1607 |
|
|
r = rest; |
1608 |
|
|
} |
1609 |
|
|
else |
1610 |
|
|
{ |
1611 |
|
|
/* There's nothing left in the filename, so the link contents |
1612 |
|
|
** becomes the rest. |
1613 |
|
|
*/ |
1614 |
|
|
httpd_realloc_str( &rest, &maxrest, linklen ); |
1615 |
|
|
(void) strcpy( rest, link ); |
1616 |
|
|
restlen = linklen; |
1617 |
|
|
r = rest; |
1618 |
|
|
} |
1619 |
|
|
|
1620 |
|
|
if ( rest[0] == '/' ) |
1621 |
|
|
{ |
1622 |
|
|
/* There must have been an absolute symlink - zero out checked. */ |
1623 |
|
|
checked[0] = '\0'; |
1624 |
|
|
checkedlen = 0; |
1625 |
|
|
} |
1626 |
|
|
else |
1627 |
|
|
{ |
1628 |
|
|
/* Re-check this component. */ |
1629 |
|
|
checkedlen = prevcheckedlen; |
1630 |
|
|
checked[checkedlen] = '\0'; |
1631 |
|
|
} |
1632 |
|
|
} |
1633 |
|
|
|
1634 |
|
|
/* Ok. */ |
1635 |
|
|
*restP = r; |
1636 |
|
|
if ( checked[0] == '\0' ) |
1637 |
|
|
(void) strcpy( checked, "." ); |
1638 |
|
|
return checked; |
1639 |
|
|
} |
1640 |
|
|
|
1641 |
|
|
|
1642 |
|
|
int |
1643 |
|
|
httpd_get_conn( httpd_server* hs, int listen_fd, httpd_conn* hc ) |
1644 |
|
|
{ |
1645 |
|
|
httpd_sockaddr sa; |
1646 |
|
|
int sz; |
1647 |
|
|
|
1648 |
|
|
if ( ! hc->initialized ) |
1649 |
|
|
{ |
1650 |
|
|
hc->read_size = 0; |
1651 |
|
|
httpd_realloc_str( &hc->read_buf, &hc->read_size, 500 ); |
1652 |
|
|
hc->maxdecodedurl = |
1653 |
|
|
hc->maxorigfilename = hc->maxexpnfilename = hc->maxencodings = |
1654 |
|
|
hc->maxpathinfo = hc->maxquery = hc->maxaccept = |
1655 |
|
|
hc->maxaccepte = hc->maxreqhost = hc->maxhostdir = |
1656 |
|
|
hc->maxremoteuser = hc->maxresponse = 0; |
1657 |
|
|
#ifdef TILDE_MAP_2 |
1658 |
|
|
hc->maxaltdir = 0; |
1659 |
|
|
#endif /* TILDE_MAP_2 */ |
1660 |
|
|
httpd_realloc_str( &hc->decodedurl, &hc->maxdecodedurl, 1 ); |
1661 |
|
|
httpd_realloc_str( &hc->origfilename, &hc->maxorigfilename, 1 ); |
1662 |
|
|
httpd_realloc_str( &hc->expnfilename, &hc->maxexpnfilename, 0 ); |
1663 |
|
|
httpd_realloc_str( &hc->encodings, &hc->maxencodings, 0 ); |
1664 |
|
|
httpd_realloc_str( &hc->pathinfo, &hc->maxpathinfo, 0 ); |
1665 |
|
|
httpd_realloc_str( &hc->query, &hc->maxquery, 0 ); |
1666 |
|
|
httpd_realloc_str( &hc->accept, &hc->maxaccept, 0 ); |
1667 |
|
|
httpd_realloc_str( &hc->accepte, &hc->maxaccepte, 0 ); |
1668 |
|
|
httpd_realloc_str( &hc->reqhost, &hc->maxreqhost, 0 ); |
1669 |
|
|
httpd_realloc_str( &hc->hostdir, &hc->maxhostdir, 0 ); |
1670 |
|
|
httpd_realloc_str( &hc->remoteuser, &hc->maxremoteuser, 0 ); |
1671 |
|
|
httpd_realloc_str( &hc->response, &hc->maxresponse, 0 ); |
1672 |
|
|
#ifdef TILDE_MAP_2 |
1673 |
|
|
httpd_realloc_str( &hc->altdir, &hc->maxaltdir, 0 ); |
1674 |
|
|
#endif /* TILDE_MAP_2 */ |
1675 |
|
|
hc->initialized = 1; |
1676 |
|
|
} |
1677 |
|
|
|
1678 |
|
|
/* Accept the new connection. */ |
1679 |
|
|
sz = sizeof(sa); |
1680 |
|
|
hc->conn_fd = accept( listen_fd, &sa.sa, &sz ); |
1681 |
|
|
if ( hc->conn_fd < 0 ) |
1682 |
|
|
{ |
1683 |
|
|
if ( errno == EWOULDBLOCK ) |
1684 |
|
|
return GC_NO_MORE; |
1685 |
|
|
syslog( LOG_ERR, "accept - %m" ); |
1686 |
|
|
return GC_FAIL; |
1687 |
|
|
} |
1688 |
|
|
if ( ! sockaddr_check( &sa ) ) |
1689 |
|
|
{ |
1690 |
|
|
syslog( LOG_ERR, "unknown sockaddr family" ); |
1691 |
|
|
return GC_FAIL; |
1692 |
|
|
} |
1693 |
|
|
(void) fcntl( hc->conn_fd, F_SETFD, 1 ); |
1694 |
|
|
hc->hs = hs; |
1695 |
|
|
memset( &hc->client_addr, 0, sizeof(hc->client_addr) ); |
1696 |
|
|
memcpy( &hc->client_addr, &sa, sockaddr_len( &sa ) ); |
1697 |
|
|
hc->read_idx = 0; |
1698 |
|
|
hc->checked_idx = 0; |
1699 |
|
|
hc->checked_state = CHST_FIRSTWORD; |
1700 |
|
|
hc->method = METHOD_UNKNOWN; |
1701 |
|
|
hc->status = 0; |
1702 |
|
|
hc->bytes_to_send = 0; |
1703 |
|
|
hc->bytes_sent = 0; |
1704 |
|
|
hc->encodedurl = ""; |
1705 |
|
|
hc->decodedurl[0] = '\0'; |
1706 |
|
|
hc->protocol = "UNKNOWN"; |
1707 |
|
|
hc->origfilename[0] = '\0'; |
1708 |
|
|
hc->expnfilename[0] = '\0'; |
1709 |
|
|
hc->encodings[0] = '\0'; |
1710 |
|
|
hc->pathinfo[0] = '\0'; |
1711 |
|
|
hc->query[0] = '\0'; |
1712 |
|
|
hc->referer = ""; |
1713 |
|
|
hc->useragent = ""; |
1714 |
|
|
hc->accept[0] = '\0'; |
1715 |
|
|
hc->accepte[0] = '\0'; |
1716 |
|
|
hc->acceptl = ""; |
1717 |
|
|
hc->cookie = ""; |
1718 |
|
|
hc->contenttype = ""; |
1719 |
|
|
hc->reqhost[0] = '\0'; |
1720 |
|
|
hc->hdrhost = ""; |
1721 |
|
|
hc->hostdir[0] = '\0'; |
1722 |
|
|
hc->authorization = ""; |
1723 |
|
|
hc->remoteuser[0] = '\0'; |
1724 |
|
|
hc->response[0] = '\0'; |
1725 |
|
|
#ifdef TILDE_MAP_2 |
1726 |
|
|
hc->altdir[0] = '\0'; |
1727 |
|
|
#endif /* TILDE_MAP_2 */ |
1728 |
|
|
hc->responselen = 0; |
1729 |
|
|
hc->if_modified_since = (time_t) -1; |
1730 |
|
|
hc->range_if = (time_t) -1; |
1731 |
|
|
hc->contentlength = -1; |
1732 |
|
|
hc->type = ""; |
1733 |
|
|
hc->hostname = (char*) 0; |
1734 |
|
|
hc->mime_flag = 1; |
1735 |
|
|
hc->one_one = 0; |
1736 |
|
|
hc->got_range = 0; |
1737 |
|
|
hc->tildemapped = 0; |
1738 |
|
|
hc->init_byte_loc = 0; |
1739 |
|
|
hc->end_byte_loc = -1; |
1740 |
|
|
hc->keep_alive = 0; |
1741 |
|
|
hc->should_linger = 0; |
1742 |
|
|
hc->file_address = (char*) 0; |
1743 |
|
|
return GC_OK; |
1744 |
|
|
} |
1745 |
|
|
|
1746 |
|
|
|
1747 |
|
|
/* Checks hc->read_buf to see whether a complete request has been read so far; |
1748 |
|
|
** either the first line has two words (an HTTP/0.9 request), or the first |
1749 |
|
|
** line has three words and there's a blank line present. |
1750 |
|
|
** |
1751 |
|
|
** hc->read_idx is how much has been read in; hc->checked_idx is how much we |
1752 |
|
|
** have checked so far; and hc->checked_state is the current state of the |
1753 |
|
|
** finite state machine. |
1754 |
|
|
*/ |
1755 |
|
|
int |
1756 |
|
|
httpd_got_request( httpd_conn* hc ) |
1757 |
|
|
{ |
1758 |
|
|
char c; |
1759 |
|
|
|
1760 |
|
|
for ( ; hc->checked_idx < hc->read_idx; ++hc->checked_idx ) |
1761 |
|
|
{ |
1762 |
|
|
c = hc->read_buf[hc->checked_idx]; |
1763 |
|
|
switch ( hc->checked_state ) |
1764 |
|
|
{ |
1765 |
|
|
case CHST_FIRSTWORD: |
1766 |
|
|
switch ( c ) |
1767 |
|
|
{ |
1768 |
|
|
case ' ': case '\t': |
1769 |
|
|
hc->checked_state = CHST_FIRSTWS; |
1770 |
|
|
break; |
1771 |
|
|
case '\n': case '\r': |
1772 |
|
|
hc->checked_state = CHST_BOGUS; |
1773 |
|
|
return GR_BAD_REQUEST; |
1774 |
|
|
} |
1775 |
|
|
break; |
1776 |
|
|
case CHST_FIRSTWS: |
1777 |
|
|
switch ( c ) |
1778 |
|
|
{ |
1779 |
|
|
case ' ': case '\t': |
1780 |
|
|
break; |
1781 |
|
|
case '\n': case '\r': |
1782 |
|
|
hc->checked_state = CHST_BOGUS; |
1783 |
|
|
return GR_BAD_REQUEST; |
1784 |
|
|
default: |
1785 |
|
|
hc->checked_state = CHST_SECONDWORD; |
1786 |
|
|
break; |
1787 |
|
|
} |
1788 |
|
|
break; |
1789 |
|
|
case CHST_SECONDWORD: |
1790 |
|
|
switch ( c ) |
1791 |
|
|
{ |
1792 |
|
|
case ' ': case '\t': |
1793 |
|
|
hc->checked_state = CHST_SECONDWS; |
1794 |
|
|
break; |
1795 |
|
|
case '\n': case '\r': |
1796 |
|
|
/* The first line has only two words - an HTTP/0.9 request. */ |
1797 |
|
|
return GR_GOT_REQUEST; |
1798 |
|
|
} |
1799 |
|
|
break; |
1800 |
|
|
case CHST_SECONDWS: |
1801 |
|
|
switch ( c ) |
1802 |
|
|
{ |
1803 |
|
|
case ' ': case '\t': |
1804 |
|
|
break; |
1805 |
|
|
case '\n': case '\r': |
1806 |
|
|
hc->checked_state = CHST_BOGUS; |
1807 |
|
|
return GR_BAD_REQUEST; |
1808 |
|
|
default: |
1809 |
|
|
hc->checked_state = CHST_THIRDWORD; |
1810 |
|
|
break; |
1811 |
|
|
} |
1812 |
|
|
break; |
1813 |
|
|
case CHST_THIRDWORD: |
1814 |
|
|
switch ( c ) |
1815 |
|
|
{ |
1816 |
|
|
case ' ': case '\t': |
1817 |
|
|
hc->checked_state = CHST_THIRDWS; |
1818 |
|
|
break; |
1819 |
|
|
case '\n': |
1820 |
|
|
hc->checked_state = CHST_LF; |
1821 |
|
|
break; |
1822 |
|
|
case '\r': |
1823 |
|
|
hc->checked_state = CHST_CR; |
1824 |
|
|
break; |
1825 |
|
|
} |
1826 |
|
|
break; |
1827 |
|
|
case CHST_THIRDWS: |
1828 |
|
|
switch ( c ) |
1829 |
|
|
{ |
1830 |
|
|
case ' ': case '\t': |
1831 |
|
|
break; |
1832 |
|
|
case '\n': |
1833 |
|
|
hc->checked_state = CHST_LF; |
1834 |
|
|
break; |
1835 |
|
|
case '\r': |
1836 |
|
|
hc->checked_state = CHST_CR; |
1837 |
|
|
break; |
1838 |
|
|
default: |
1839 |
|
|
hc->checked_state = CHST_BOGUS; |
1840 |
|
|
return GR_BAD_REQUEST; |
1841 |
|
|
} |
1842 |
|
|
break; |
1843 |
|
|
case CHST_LINE: |
1844 |
|
|
switch ( c ) |
1845 |
|
|
{ |
1846 |
|
|
case '\n': |
1847 |
|
|
hc->checked_state = CHST_LF; |
1848 |
|
|
break; |
1849 |
|
|
case '\r': |
1850 |
|
|
hc->checked_state = CHST_CR; |
1851 |
|
|
break; |
1852 |
|
|
} |
1853 |
|
|
break; |
1854 |
|
|
case CHST_LF: |
1855 |
|
|
switch ( c ) |
1856 |
|
|
{ |
1857 |
|
|
case '\n': |
1858 |
|
|
/* Two newlines in a row - a blank line - end of request. */ |
1859 |
|
|
return GR_GOT_REQUEST; |
1860 |
|
|
case '\r': |
1861 |
|
|
hc->checked_state = CHST_CR; |
1862 |
|
|
break; |
1863 |
|
|
default: |
1864 |
|
|
hc->checked_state = CHST_LINE; |
1865 |
|
|
break; |
1866 |
|
|
} |
1867 |
|
|
break; |
1868 |
|
|
case CHST_CR: |
1869 |
|
|
switch ( c ) |
1870 |
|
|
{ |
1871 |
|
|
case '\n': |
1872 |
|
|
hc->checked_state = CHST_CRLF; |
1873 |
|
|
break; |
1874 |
|
|
case '\r': |
1875 |
|
|
/* Two returns in a row - end of request. */ |
1876 |
|
|
return GR_GOT_REQUEST; |
1877 |
|
|
default: |
1878 |
|
|
hc->checked_state = CHST_LINE; |
1879 |
|
|
break; |
1880 |
|
|
} |
1881 |
|
|
break; |
1882 |
|
|
case CHST_CRLF: |
1883 |
|
|
switch ( c ) |
1884 |
|
|
{ |
1885 |
|
|
case '\n': |
1886 |
|
|
/* Two newlines in a row - end of request. */ |
1887 |
|
|
return GR_GOT_REQUEST; |
1888 |
|
|
case '\r': |
1889 |
|
|
hc->checked_state = CHST_CRLFCR; |
1890 |
|
|
break; |
1891 |
|
|
default: |
1892 |
|
|
hc->checked_state = CHST_LINE; |
1893 |
|
|
break; |
1894 |
|
|
} |
1895 |
|
|
break; |
1896 |
|
|
case CHST_CRLFCR: |
1897 |
|
|
switch ( c ) |
1898 |
|
|
{ |
1899 |
|
|
case '\n': case '\r': |
1900 |
|
|
/* Two CRLFs or two CRs in a row - end of request. */ |
1901 |
|
|
return GR_GOT_REQUEST; |
1902 |
|
|
default: |
1903 |
|
|
hc->checked_state = CHST_LINE; |
1904 |
|
|
break; |
1905 |
|
|
} |
1906 |
|
|
break; |
1907 |
|
|
case CHST_BOGUS: |
1908 |
|
|
return GR_BAD_REQUEST; |
1909 |
|
|
} |
1910 |
|
|
} |
1911 |
|
|
return GR_NO_REQUEST; |
1912 |
|
|
} |
1913 |
|
|
|
1914 |
|
|
|
1915 |
|
|
int |
1916 |
|
|
httpd_parse_request( httpd_conn* hc ) |
1917 |
|
|
{ |
1918 |
|
|
char* buf; |
1919 |
|
|
char* method_str; |
1920 |
|
|
char* url; |
1921 |
|
|
char* protocol; |
1922 |
|
|
char* reqhost; |
1923 |
|
|
char* eol; |
1924 |
|
|
char* cp; |
1925 |
|
|
char* pi; |
1926 |
|
|
|
1927 |
|
|
hc->checked_idx = 0; /* reset */ |
1928 |
|
|
method_str = bufgets( hc ); |
1929 |
|
|
url = strpbrk( method_str, " \t\n\r" ); |
1930 |
|
|
if ( url == (char*) 0 ) |
1931 |
|
|
{ |
1932 |
|
|
httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" ); |
1933 |
|
|
return -1; |
1934 |
|
|
} |
1935 |
|
|
*url++ = '\0'; |
1936 |
|
|
url += strspn( url, " \t\n\r" ); |
1937 |
|
|
protocol = strpbrk( url, " \t\n\r" ); |
1938 |
|
|
if ( protocol == (char*) 0 ) |
1939 |
|
|
{ |
1940 |
|
|
protocol = "HTTP/0.9"; |
1941 |
|
|
hc->mime_flag = 0; |
1942 |
|
|
} |
1943 |
|
|
else |
1944 |
|
|
{ |
1945 |
|
|
*protocol++ = '\0'; |
1946 |
|
|
protocol += strspn( protocol, " \t\n\r" ); |
1947 |
|
|
if ( *protocol != '\0' ) |
1948 |
|
|
{ |
1949 |
|
|
eol = strpbrk( protocol, " \t\n\r" ); |
1950 |
|
|
if ( eol != (char*) 0 ) |
1951 |
|
|
*eol = '\0'; |
1952 |
|
|
if ( strcasecmp( protocol, "HTTP/1.0" ) != 0 ) |
1953 |
|
|
hc->one_one = 1; |
1954 |
|
|
} |
1955 |
|
|
} |
1956 |
|
|
/* Check for HTTP/1.1 absolute URL. */ |
1957 |
|
|
if ( strncasecmp( url, "http://", 7 ) == 0 ) |
1958 |
|
|
{ |
1959 |
|
|
if ( ! hc->one_one ) |
1960 |
|
|
{ |
1961 |
|
|
httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" ); |
1962 |
|
|
return -1; |
1963 |
|
|
} |
1964 |
|
|
reqhost = url + 7; |
1965 |
|
|
url = strchr( reqhost, '/' ); |
1966 |
|
|
if ( url == (char*) 0 ) |
1967 |
|
|
{ |
1968 |
|
|
httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" ); |
1969 |
|
|
return -1; |
1970 |
|
|
} |
1971 |
|
|
*url = '\0'; |
1972 |
|
|
httpd_realloc_str( &hc->reqhost, &hc->maxreqhost, strlen( reqhost ) ); |
1973 |
|
|
(void) strcpy( hc->reqhost, reqhost ); |
1974 |
|
|
*url = '/'; |
1975 |
|
|
} |
1976 |
|
|
|
1977 |
|
|
if ( strcasecmp( method_str, httpd_method_str( METHOD_GET ) ) == 0 ) |
1978 |
|
|
hc->method = METHOD_GET; |
1979 |
|
|
else if ( strcasecmp( method_str, httpd_method_str( METHOD_HEAD ) ) == 0 ) |
1980 |
|
|
hc->method = METHOD_HEAD; |
1981 |
|
|
else if ( strcasecmp( method_str, httpd_method_str( METHOD_POST ) ) == 0 ) |
1982 |
|
|
hc->method = METHOD_POST; |
1983 |
|
|
else |
1984 |
|
|
{ |
1985 |
|
|
httpd_send_err( hc, 501, err501title, "", err501form, method_str ); |
1986 |
|
|
return -1; |
1987 |
|
|
} |
1988 |
|
|
|
1989 |
|
|
hc->encodedurl = url; |
1990 |
|
|
httpd_realloc_str( |
1991 |
|
|
&hc->decodedurl, &hc->maxdecodedurl, strlen( hc->encodedurl ) ); |
1992 |
|
|
strdecode( hc->decodedurl, hc->encodedurl ); |
1993 |
|
|
|
1994 |
|
|
de_dotdot( hc->decodedurl ); |
1995 |
|
|
if ( hc->decodedurl[0] != '/' || hc->decodedurl[1] == '/' || |
1996 |
|
|
( hc->decodedurl[1] == '.' && hc->decodedurl[2] == '.' && |
1997 |
|
|
( hc->decodedurl[3] == '\0' || hc->decodedurl[3] == '/' ) ) ) |
1998 |
|
|
{ |
1999 |
|
|
httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" ); |
2000 |
|
|
return -1; |
2001 |
|
|
} |
2002 |
|
|
|
2003 |
|
|
hc->protocol = protocol; |
2004 |
|
|
|
2005 |
|
|
httpd_realloc_str( |
2006 |
|
|
&hc->origfilename, &hc->maxorigfilename, strlen( hc->decodedurl ) ); |
2007 |
|
|
(void) strcpy( hc->origfilename, &hc->decodedurl[1] ); |
2008 |
|
|
/* Special case for top-level URL. */ |
2009 |
|
|
if ( hc->origfilename[0] == '\0' ) |
2010 |
|
|
(void) strcpy( hc->origfilename, "." ); |
2011 |
|
|
|
2012 |
|
|
/* Extract query string from encoded URL. */ |
2013 |
|
|
cp = strchr( hc->encodedurl, '?' ); |
2014 |
|
|
if ( cp != (char*) 0 ) |
2015 |
|
|
{ |
2016 |
|
|
++cp; |
2017 |
|
|
httpd_realloc_str( &hc->query, &hc->maxquery, strlen( cp ) ); |
2018 |
|
|
(void) strcpy( hc->query, cp ); |
2019 |
|
|
} |
2020 |
|
|
/* And remove query from filename. */ |
2021 |
|
|
cp = strchr( hc->origfilename, '?' ); |
2022 |
|
|
if ( cp != (char*) 0 ) |
2023 |
|
|
*cp = '\0'; |
2024 |
|
|
|
2025 |
|
|
if ( hc->mime_flag ) |
2026 |
|
|
{ |
2027 |
|
|
/* Read the MIME headers. */ |
2028 |
|
|
while ( ( buf = bufgets( hc ) ) != (char*) 0 ) |
2029 |
|
|
{ |
2030 |
|
|
if ( buf[0] == '\0' ) |
2031 |
|
|
break; |
2032 |
|
|
if ( strncasecmp( buf, "Referer:", 8 ) == 0 ) |
2033 |
|
|
{ |
2034 |
|
|
cp = &buf[8]; |
2035 |
|
|
cp += strspn( cp, " \t" ); |
2036 |
|
|
hc->referer = cp; |
2037 |
|
|
} |
2038 |
|
|
else if ( strncasecmp( buf, "User-Agent:", 11 ) == 0 ) |
2039 |
|
|
{ |
2040 |
|
|
cp = &buf[11]; |
2041 |
|
|
cp += strspn( cp, " \t" ); |
2042 |
|
|
hc->useragent = cp; |
2043 |
|
|
} |
2044 |
|
|
else if ( strncasecmp( buf, "Host:", 5 ) == 0 ) |
2045 |
|
|
{ |
2046 |
|
|
cp = &buf[5]; |
2047 |
|
|
cp += strspn( cp, " \t" ); |
2048 |
|
|
hc->hdrhost = cp; |
2049 |
|
|
cp = strchr( hc->hdrhost, ':' ); |
2050 |
|
|
if ( cp != (char*) 0 ) |
2051 |
|
|
*cp = '\0'; |
2052 |
|
|
} |
2053 |
|
|
else if ( strncasecmp( buf, "Accept:", 7 ) == 0 ) |
2054 |
|
|
{ |
2055 |
|
|
cp = &buf[7]; |
2056 |
|
|
cp += strspn( cp, " \t" ); |
2057 |
|
|
if ( hc->accept[0] != '\0' ) |
2058 |
|
|
{ |
2059 |
|
|
if ( strlen( hc->accept ) > 5000 ) |
2060 |
|
|
{ |
2061 |
|
|
syslog( |
2062 |
|
|
LOG_ERR, "%.80s way too much Accept: data", |
2063 |
|
|
httpd_ntoa( &hc->client_addr ) ); |
2064 |
|
|
continue; |
2065 |
|
|
} |
2066 |
|
|
httpd_realloc_str( |
2067 |
|
|
&hc->accept, &hc->maxaccept, |
2068 |
|
|
strlen( hc->accept ) + 2 + strlen( cp ) ); |
2069 |
|
|
(void) strcat( hc->accept, ", " ); |
2070 |
|
|
} |
2071 |
|
|
else |
2072 |
|
|
httpd_realloc_str( |
2073 |
|
|
&hc->accept, &hc->maxaccept, strlen( cp ) ); |
2074 |
|
|
(void) strcat( hc->accept, cp ); |
2075 |
|
|
} |
2076 |
|
|
else if ( strncasecmp( buf, "Accept-Encoding:", 16 ) == 0 ) |
2077 |
|
|
{ |
2078 |
|
|
cp = &buf[16]; |
2079 |
|
|
cp += strspn( cp, " \t" ); |
2080 |
|
|
if ( hc->accepte[0] != '\0' ) |
2081 |
|
|
{ |
2082 |
|
|
if ( strlen( hc->accepte ) > 5000 ) |
2083 |
|
|
{ |
2084 |
|
|
syslog( |
2085 |
|
|
LOG_ERR, "%.80s way too much Accept-Encoding: data", |
2086 |
|
|
httpd_ntoa( &hc->client_addr ) ); |
2087 |
|
|
continue; |
2088 |
|
|
} |
2089 |
|
|
httpd_realloc_str( |
2090 |
|
|
&hc->accepte, &hc->maxaccepte, |
2091 |
|
|
strlen( hc->accepte ) + 2 + strlen( cp ) ); |
2092 |
|
|
(void) strcat( hc->accepte, ", " ); |
2093 |
|
|
} |
2094 |
|
|
else |
2095 |
|
|
httpd_realloc_str( |
2096 |
|
|
&hc->accepte, &hc->maxaccepte, strlen( cp ) ); |
2097 |
|
|
(void) strcpy( hc->accepte, cp ); |
2098 |
|
|
} |
2099 |
|
|
else if ( strncasecmp( buf, "Accept-Language:", 16 ) == 0 ) |
2100 |
|
|
{ |
2101 |
|
|
cp = &buf[16]; |
2102 |
|
|
cp += strspn( cp, " \t" ); |
2103 |
|
|
hc->acceptl = cp; |
2104 |
|
|
} |
2105 |
|
|
else if ( strncasecmp( buf, "If-Modified-Since:", 18 ) == 0 ) |
2106 |
|
|
{ |
2107 |
|
|
cp = &buf[18]; |
2108 |
|
|
hc->if_modified_since = tdate_parse( cp ); |
2109 |
|
|
if ( hc->if_modified_since == (time_t) -1 ) |
2110 |
|
|
syslog( LOG_DEBUG, "unparsable time: %.80s", cp ); |
2111 |
|
|
} |
2112 |
|
|
else if ( strncasecmp( buf, "Cookie:", 7 ) == 0 ) |
2113 |
|
|
{ |
2114 |
|
|
cp = &buf[7]; |
2115 |
|
|
cp += strspn( cp, " \t" ); |
2116 |
|
|
hc->cookie = cp; |
2117 |
|
|
} |
2118 |
|
|
else if ( strncasecmp( buf, "Range:", 6 ) == 0 ) |
2119 |
|
|
{ |
2120 |
|
|
/* Only support %d- and %d-%d, not %d-%d,%d-%d or -%d. */ |
2121 |
|
|
if ( strchr( buf, ',' ) == (char*) 0 ) |
2122 |
|
|
{ |
2123 |
|
|
char* cp_dash; |
2124 |
|
|
cp = strpbrk( buf, "=" ); |
2125 |
|
|
if ( cp != (char*) 0 ) |
2126 |
|
|
{ |
2127 |
|
|
cp_dash = strchr( cp + 1, '-' ); |
2128 |
|
|
if ( cp_dash != (char*) 0 && cp_dash != cp + 1 ) |
2129 |
|
|
{ |
2130 |
|
|
*cp_dash = '\0'; |
2131 |
|
|
hc->got_range = 1; |
2132 |
|
|
hc->init_byte_loc = atol( cp + 1 ); |
2133 |
|
|
if ( isdigit( (int) cp_dash[1] ) ) |
2134 |
|
|
hc->end_byte_loc = atol( cp_dash + 1 ); |
2135 |
|
|
} |
2136 |
|
|
} |
2137 |
|
|
} |
2138 |
|
|
} |
2139 |
|
|
else if ( strncasecmp( buf, "Range-If:", 9 ) == 0 || |
2140 |
|
|
strncasecmp( buf, "If-Range:", 9 ) == 0 ) |
2141 |
|
|
{ |
2142 |
|
|
cp = &buf[9]; |
2143 |
|
|
hc->range_if = tdate_parse( cp ); |
2144 |
|
|
if ( hc->range_if == (time_t) -1 ) |
2145 |
|
|
syslog( LOG_DEBUG, "unparsable time: %.80s", cp ); |
2146 |
|
|
} |
2147 |
|
|
else if ( strncasecmp( buf, "Content-Type:", 13 ) == 0 ) |
2148 |
|
|
{ |
2149 |
|
|
cp = &buf[13]; |
2150 |
|
|
cp += strspn( cp, " \t" ); |
2151 |
|
|
hc->contenttype = cp; |
2152 |
|
|
} |
2153 |
|
|
else if ( strncasecmp( buf, "Content-Length:", 15 ) == 0 ) |
2154 |
|
|
{ |
2155 |
|
|
cp = &buf[15]; |
2156 |
|
|
hc->contentlength = atol( cp ); |
2157 |
|
|
} |
2158 |
|
|
else if ( strncasecmp( buf, "Authorization:", 14 ) == 0 ) |
2159 |
|
|
{ |
2160 |
|
|
cp = &buf[14]; |
2161 |
|
|
cp += strspn( cp, " \t" ); |
2162 |
|
|
hc->authorization = cp; |
2163 |
|
|
} |
2164 |
|
|
else if ( strncasecmp( buf, "Connection:", 11 ) == 0 ) |
2165 |
|
|
{ |
2166 |
|
|
cp = &buf[11]; |
2167 |
|
|
cp += strspn( cp, " \t" ); |
2168 |
|
|
if ( strcasecmp( cp, "keep-alive" ) == 0 ) |
2169 |
|
|
hc->keep_alive = 1; |
2170 |
|
|
} |
2171 |
|
|
#ifdef LOG_UNKNOWN_HEADERS |
2172 |
|
|
else if ( strncasecmp( buf, "Accept-Charset:", 15 ) == 0 || |
2173 |
|
|
strncasecmp( buf, "Accept-Language:", 16 ) == 0 || |
2174 |
|
|
strncasecmp( buf, "Agent:", 6 ) == 0 || |
2175 |
|
|
strncasecmp( buf, "Cache-Control:", 14 ) == 0 || |
2176 |
|
|
strncasecmp( buf, "Cache-Info:", 11 ) == 0 || |
2177 |
|
|
strncasecmp( buf, "Charge-To:", 10 ) == 0 || |
2178 |
|
|
strncasecmp( buf, "Client-IP:", 10 ) == 0 || |
2179 |
|
|
strncasecmp( buf, "Date:", 5 ) == 0 || |
2180 |
|
|
strncasecmp( buf, "Extension:", 10 ) == 0 || |
2181 |
|
|
strncasecmp( buf, "Forwarded:", 10 ) == 0 || |
2182 |
|
|
strncasecmp( buf, "From:", 5 ) == 0 || |
2183 |
|
|
strncasecmp( buf, "HTTP-Version:", 13 ) == 0 || |
2184 |
|
|
strncasecmp( buf, "Max-Forwards:", 13 ) == 0 || |
2185 |
|
|
strncasecmp( buf, "Message-Id:", 11 ) == 0 || |
2186 |
|
|
strncasecmp( buf, "MIME-Version:", 13 ) == 0 || |
2187 |
|
|
strncasecmp( buf, "Negotiate:", 10 ) == 0 || |
2188 |
|
|
strncasecmp( buf, "Pragma:", 7 ) == 0 || |
2189 |
|
|
strncasecmp( buf, "Proxy-Agent:", 12 ) == 0 || |
2190 |
|
|
strncasecmp( buf, "Proxy-Connection:", 17 ) == 0 || |
2191 |
|
|
strncasecmp( buf, "Security-Scheme:", 16 ) == 0 || |
2192 |
|
|
strncasecmp( buf, "Session-Id:", 11 ) == 0 || |
2193 |
|
|
strncasecmp( buf, "UA-Color:", 9 ) == 0 || |
2194 |
|
|
strncasecmp( buf, "UA-CPU:", 7 ) == 0 || |
2195 |
|
|
strncasecmp( buf, "UA-Disp:", 8 ) == 0 || |
2196 |
|
|
strncasecmp( buf, "UA-OS:", 6 ) == 0 || |
2197 |
|
|
strncasecmp( buf, "UA-Pixels:", 10 ) == 0 || |
2198 |
|
|
strncasecmp( buf, "User:", 5 ) == 0 || |
2199 |
|
|
strncasecmp( buf, "Via:", 4 ) == 0 || |
2200 |
|
|
strncasecmp( buf, "X-", 2 ) == 0 ) |
2201 |
|
|
; /* ignore */ |
2202 |
|
|
else |
2203 |
|
|
syslog( LOG_DEBUG, "unknown request header: %.80s", buf ); |
2204 |
|
|
#endif /* LOG_UNKNOWN_HEADERS */ |
2205 |
|
|
} |
2206 |
|
|
} |
2207 |
|
|
|
2208 |
|
|
if ( hc->one_one ) |
2209 |
|
|
{ |
2210 |
|
|
/* Check that HTTP/1.1 requests specify a host, as required. */ |
2211 |
|
|
if ( hc->reqhost[0] == '\0' && hc->hdrhost[0] == '\0' ) |
2212 |
|
|
{ |
2213 |
|
|
httpd_send_err( hc, 400, httpd_err400title, "", httpd_err400form, "" ); |
2214 |
|
|
return -1; |
2215 |
|
|
} |
2216 |
|
|
|
2217 |
|
|
/* If the client wants to do keep-alives, it might also be doing |
2218 |
|
|
** pipelining. There's no way for us to tell. Since we don't |
2219 |
|
|
** implement keep-alives yet, if we close such a connection there |
2220 |
|
|
** might be unread pipelined requests waiting. So, we have to |
2221 |
|
|
** do a lingering close. |
2222 |
|
|
*/ |
2223 |
|
|
if ( hc->keep_alive ) |
2224 |
|
|
hc->should_linger = 1; |
2225 |
|
|
} |
2226 |
|
|
|
2227 |
|
|
/* Ok, the request has been parsed. Now we resolve stuff that |
2228 |
|
|
** may require the entire request. |
2229 |
|
|
*/ |
2230 |
|
|
|
2231 |
|
|
/* Copy original filename to expanded filename. */ |
2232 |
|
|
httpd_realloc_str( |
2233 |
|
|
&hc->expnfilename, &hc->maxexpnfilename, strlen( hc->origfilename ) ); |
2234 |
|
|
(void) strcpy( hc->expnfilename, hc->origfilename ); |
2235 |
|
|
|
2236 |
|
|
/* Tilde mapping. */ |
2237 |
|
|
if ( hc->expnfilename[0] == '~' ) |
2238 |
|
|
{ |
2239 |
|
|
#ifdef TILDE_MAP_1 |
2240 |
|
|
if ( ! tilde_map_1( hc ) ) |
2241 |
|
|
{ |
2242 |
|
|
httpd_send_err( hc, 404, err404title, "", err404form, hc->encodedurl ); |
2243 |
|
|
return -1; |
2244 |
|
|
} |
2245 |
|
|
#endif /* TILDE_MAP_1 */ |
2246 |
|
|
#ifdef TILDE_MAP_2 |
2247 |
|
|
if ( ! tilde_map_2( hc ) ) |
2248 |
|
|
{ |
2249 |
|
|
httpd_send_err( hc, 404, err404title, "", err404form, hc->encodedurl ); |
2250 |
|
|
return -1; |
2251 |
|
|
} |
2252 |
|
|
#endif /* TILDE_MAP_2 */ |
2253 |
|
|
} |
2254 |
|
|
|
2255 |
|
|
/* Virtual host mapping. */ |
2256 |
|
|
if ( hc->hs->vhost ) |
2257 |
|
|
if ( ! vhost_map( hc ) ) |
2258 |
|
|
{ |
2259 |
|
|
httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl ); |
2260 |
|
|
return -1; |
2261 |
|
|
} |
2262 |
|
|
|
2263 |
|
|
/* Expand all symbolic links in the filename. This also gives us |
2264 |
|
|
** any trailing non-existing components, for pathinfo. |
2265 |
|
|
*/ |
2266 |
|
|
cp = expand_symlinks( hc->expnfilename, &pi, hc->hs->no_symlink, hc->tildemapped ); |
2267 |
|
|
if ( cp == (char*) 0 ) |
2268 |
|
|
{ |
2269 |
|
|
httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl ); |
2270 |
|
|
return -1; |
2271 |
|
|
} |
2272 |
|
|
httpd_realloc_str( &hc->expnfilename, &hc->maxexpnfilename, strlen( cp ) ); |
2273 |
|
|
(void) strcpy( hc->expnfilename, cp ); |
2274 |
|
|
httpd_realloc_str( &hc->pathinfo, &hc->maxpathinfo, strlen( pi ) ); |
2275 |
|
|
(void) strcpy( hc->pathinfo, pi ); |
2276 |
|
|
|
2277 |
|
|
/* Remove pathinfo stuff from the original filename too. */ |
2278 |
|
|
if ( hc->pathinfo[0] != '\0' ) |
2279 |
|
|
{ |
2280 |
|
|
int i; |
2281 |
|
|
i = strlen( hc->origfilename ) - strlen( hc->pathinfo ); |
2282 |
|
|
if ( i > 0 && strcmp( &hc->origfilename[i], hc->pathinfo ) == 0 ) |
2283 |
|
|
hc->origfilename[i - 1] = '\0'; |
2284 |
|
|
} |
2285 |
|
|
|
2286 |
|
|
/* If the expanded filename is an absolute path, check that it's still |
2287 |
|
|
** within the current directory or the alternate directory. |
2288 |
|
|
*/ |
2289 |
|
|
if ( hc->expnfilename[0] == '/' ) |
2290 |
|
|
{ |
2291 |
|
|
if ( strncmp( |
2292 |
|
|
hc->expnfilename, hc->hs->cwd, strlen( hc->hs->cwd ) ) == 0 ) |
2293 |
|
|
{ |
2294 |
|
|
/* Elide the current directory. */ |
2295 |
|
|
(void) strcpy( |
2296 |
|
|
hc->expnfilename, &hc->expnfilename[strlen( hc->hs->cwd )] ); |
2297 |
|
|
} |
2298 |
|
|
#ifdef TILDE_MAP_2 |
2299 |
|
|
else if ( hc->altdir[0] != '\0' && |
2300 |
|
|
( strncmp( |
2301 |
|
|
hc->expnfilename, hc->altdir, |
2302 |
|
|
strlen( hc->altdir ) ) == 0 && |
2303 |
|
|
( hc->expnfilename[strlen( hc->altdir )] == '\0' || |
2304 |
|
|
hc->expnfilename[strlen( hc->altdir )] == '/' ) ) ) |
2305 |
|
|
{} |
2306 |
|
|
#endif /* TILDE_MAP_2 */ |
2307 |
|
|
else |
2308 |
|
|
{ |
2309 |
|
|
syslog( |
2310 |
|
|
LOG_NOTICE, "%.80s URL \"%.80s\" goes outside the web tree", |
2311 |
|
|
httpd_ntoa( &hc->client_addr ), hc->encodedurl ); |
2312 |
|
|
httpd_send_err( |
2313 |
|
|
hc, 403, err403title, "", |
2314 |
|
|
ERROR_FORM( err403form, "The requested URL '%.80s' resolves to a file outside the permitted web server directory tree.\n" ), |
2315 |
|
|
hc->encodedurl ); |
2316 |
|
|
return -1; |
2317 |
|
|
} |
2318 |
|
|
} |
2319 |
|
|
|
2320 |
|
|
return 0; |
2321 |
|
|
} |
2322 |
|
|
|
2323 |
|
|
|
2324 |
|
|
static char* |
2325 |
|
|
bufgets( httpd_conn* hc ) |
2326 |
|
|
{ |
2327 |
|
|
int i; |
2328 |
|
|
char c; |
2329 |
|
|
|
2330 |
|
|
for ( i = hc->checked_idx; hc->checked_idx < hc->read_idx; ++hc->checked_idx ) |
2331 |
|
|
{ |
2332 |
|
|
c = hc->read_buf[hc->checked_idx]; |
2333 |
|
|
if ( c == '\n' || c == '\r' ) |
2334 |
|
|
{ |
2335 |
|
|
hc->read_buf[hc->checked_idx] = '\0'; |
2336 |
|
|
++hc->checked_idx; |
2337 |
|
|
if ( c == '\r' && hc->checked_idx < hc->read_idx && |
2338 |
|
|
hc->read_buf[hc->checked_idx] == '\n' ) |
2339 |
|
|
{ |
2340 |
|
|
hc->read_buf[hc->checked_idx] = '\0'; |
2341 |
|
|
++hc->checked_idx; |
2342 |
|
|
} |
2343 |
|
|
return &(hc->read_buf[i]); |
2344 |
|
|
} |
2345 |
|
|
} |
2346 |
|
|
return (char*) 0; |
2347 |
|
|
} |
2348 |
|
|
|
2349 |
|
|
|
2350 |
|
|
static void |
2351 |
|
|
de_dotdot( char* file ) |
2352 |
|
|
{ |
2353 |
|
|
char* cp; |
2354 |
|
|
char* cp2; |
2355 |
|
|
int l; |
2356 |
|
|
|
2357 |
|
|
/* Collapse any multiple / sequences. */ |
2358 |
|
|
while ( ( cp = strstr( file, "//") ) != (char*) 0 ) |
2359 |
|
|
{ |
2360 |
|
|
for ( cp2 = cp + 2; *cp2 == '/'; ++cp2 ) |
2361 |
|
|
continue; |
2362 |
|
|
(void) strcpy( cp + 1, cp2 ); |
2363 |
|
|
} |
2364 |
|
|
|
2365 |
|
|
/* Elide any xxx/../ sequences. */ |
2366 |
|
|
while ( ( cp = strstr( file, "/../" ) ) != (char*) 0 ) |
2367 |
|
|
{ |
2368 |
|
|
for ( cp2 = cp - 1; cp2 >= file && *cp2 != '/'; --cp2 ) |
2369 |
|
|
continue; |
2370 |
|
|
if ( cp2 < file ) |
2371 |
|
|
break; |
2372 |
|
|
(void) strcpy( cp2, cp + 3 ); |
2373 |
|
|
} |
2374 |
|
|
|
2375 |
|
|
/* Also elide any xxx/.. at the end. */ |
2376 |
|
|
while ( ( l = strlen( file ) ) > 3 && |
2377 |
|
|
strcmp( ( cp = file + l - 3 ), "/.." ) == 0 ) |
2378 |
|
|
{ |
2379 |
|
|
for ( cp2 = cp - 1; cp2 >= file && *cp2 != '/'; --cp2 ) |
2380 |
|
|
continue; |
2381 |
|
|
if ( cp2 < file ) |
2382 |
|
|
break; |
2383 |
|
|
*cp2 = '\0'; |
2384 |
|
|
} |
2385 |
|
|
} |
2386 |
|
|
|
2387 |
|
|
|
2388 |
|
|
void |
2389 |
|
|
httpd_close_conn( httpd_conn* hc, struct timeval* nowP ) |
2390 |
|
|
{ |
2391 |
|
|
make_log_entry( hc, nowP ); |
2392 |
|
|
|
2393 |
|
|
if ( hc->file_address != (char*) 0 ) |
2394 |
|
|
{ |
2395 |
|
|
mmc_unmap( hc->file_address, &(hc->sb), nowP ); |
2396 |
|
|
hc->file_address = (char*) 0; |
2397 |
|
|
} |
2398 |
|
|
if ( hc->conn_fd >= 0 ) |
2399 |
|
|
{ |
2400 |
|
|
(void) close( hc->conn_fd ); |
2401 |
|
|
hc->conn_fd = -1; |
2402 |
|
|
} |
2403 |
|
|
} |
2404 |
|
|
|
2405 |
|
|
void |
2406 |
|
|
httpd_destroy_conn( httpd_conn* hc ) |
2407 |
|
|
{ |
2408 |
|
|
if ( hc->initialized ) |
2409 |
|
|
{ |
2410 |
|
|
free( (void*) hc->read_buf ); |
2411 |
|
|
free( (void*) hc->decodedurl ); |
2412 |
|
|
free( (void*) hc->origfilename ); |
2413 |
|
|
free( (void*) hc->expnfilename ); |
2414 |
|
|
free( (void*) hc->encodings ); |
2415 |
|
|
free( (void*) hc->pathinfo ); |
2416 |
|
|
free( (void*) hc->query ); |
2417 |
|
|
free( (void*) hc->accept ); |
2418 |
|
|
free( (void*) hc->accepte ); |
2419 |
|
|
free( (void*) hc->reqhost ); |
2420 |
|
|
free( (void*) hc->hostdir ); |
2421 |
|
|
free( (void*) hc->remoteuser ); |
2422 |
|
|
free( (void*) hc->response ); |
2423 |
|
|
#ifdef TILDE_MAP_2 |
2424 |
|
|
free( (void*) hc->altdir ); |
2425 |
|
|
#endif /* TILDE_MAP_2 */ |
2426 |
|
|
hc->initialized = 0; |
2427 |
|
|
} |
2428 |
|
|
} |
2429 |
|
|
|
2430 |
|
|
|
2431 |
|
|
/* Figures out MIME encodings and type based on the filename. Multiple |
2432 |
|
|
** encodings are separated by semicolons. |
2433 |
|
|
*/ |
2434 |
|
|
static void |
2435 |
|
|
figure_mime( httpd_conn* hc ) |
2436 |
|
|
{ |
2437 |
|
|
int i, j, k, l; |
2438 |
|
|
int got_enc; |
2439 |
|
|
struct table { |
2440 |
|
|
char* ext; |
2441 |
|
|
char* val; |
2442 |
|
|
}; |
2443 |
|
|
static struct table enc_tab[] = { |
2444 |
|
|
#include "mime_encodings.h" |
2445 |
|
|
}; |
2446 |
|
|
static struct table typ_tab[] = { |
2447 |
|
|
#include "mime_types.h" |
2448 |
|
|
}; |
2449 |
|
|
|
2450 |
|
|
/* Look at the extensions on hc->expnfilename from the back forwards. */ |
2451 |
|
|
i = strlen( hc->expnfilename ); |
2452 |
|
|
for (;;) |
2453 |
|
|
{ |
2454 |
|
|
j = i; |
2455 |
|
|
for (;;) |
2456 |
|
|
{ |
2457 |
|
|
--i; |
2458 |
|
|
if ( i <= 0 ) |
2459 |
|
|
{ |
2460 |
|
|
/* No extensions left. */ |
2461 |
|
|
hc->type = "text/plain; charset=%s"; |
2462 |
|
|
return; |
2463 |
|
|
} |
2464 |
|
|
if ( hc->expnfilename[i] == '.' ) |
2465 |
|
|
break; |
2466 |
|
|
} |
2467 |
|
|
/* Found an extension. */ |
2468 |
|
|
got_enc = 0; |
2469 |
|
|
for ( k = 0; k < sizeof(enc_tab)/sizeof(*enc_tab); ++k ) |
2470 |
|
|
{ |
2471 |
|
|
l = strlen( enc_tab[k].ext ); |
2472 |
|
|
if ( l == j - i - 1 && |
2473 |
|
|
strncasecmp( &hc->expnfilename[i+1], enc_tab[k].ext, l ) == 0 ) |
2474 |
|
|
{ |
2475 |
|
|
httpd_realloc_str( |
2476 |
|
|
&hc->encodings, &hc->maxencodings, |
2477 |
|
|
strlen( enc_tab[k].val ) + 1 ); |
2478 |
|
|
if ( hc->encodings[0] != '\0' ) |
2479 |
|
|
(void) strcat( hc->encodings, ";" ); |
2480 |
|
|
(void) strcat( hc->encodings, enc_tab[k].val ); |
2481 |
|
|
got_enc = 1; |
2482 |
|
|
} |
2483 |
|
|
} |
2484 |
|
|
if ( ! got_enc ) |
2485 |
|
|
{ |
2486 |
|
|
/* No encoding extension found - time to try type extensions. */ |
2487 |
|
|
for ( k = 0; k < sizeof(typ_tab)/sizeof(*typ_tab); ++k ) |
2488 |
|
|
{ |
2489 |
|
|
l = strlen( typ_tab[k].ext ); |
2490 |
|
|
if ( l == j - i - 1 && |
2491 |
|
|
strncasecmp( |
2492 |
|
|
&hc->expnfilename[i+1], typ_tab[k].ext, l ) == 0 ) |
2493 |
|
|
{ |
2494 |
|
|
hc->type = typ_tab[k].val; |
2495 |
|
|
return; |
2496 |
|
|
} |
2497 |
|
|
} |
2498 |
|
|
/* No recognized type extension found - return default. */ |
2499 |
|
|
hc->type = "text/plain; charset=%s"; |
2500 |
|
|
return; |
2501 |
|
|
} |
2502 |
|
|
} |
2503 |
|
|
} |
2504 |
|
|
|
2505 |
|
|
|
2506 |
|
|
#ifdef CGI_TIMELIMIT |
2507 |
|
|
static void |
2508 |
|
|
cgi_kill2( ClientData client_data, struct timeval* nowP ) |
2509 |
|
|
{ |
2510 |
|
|
pid_t pid; |
2511 |
|
|
|
2512 |
|
|
/* Before trying to kill the CGI process, reap any zombie processes. |
2513 |
|
|
** That may get rid of the CGI process. |
2514 |
|
|
*/ |
2515 |
|
|
(void) do_reap(); |
2516 |
|
|
|
2517 |
|
|
pid = (pid_t) client_data.i; |
2518 |
|
|
if ( kill( pid, SIGKILL ) == 0 ) |
2519 |
|
|
syslog( LOG_ERR, "hard-killed CGI process %d", pid ); |
2520 |
|
|
} |
2521 |
|
|
|
2522 |
|
|
static void |
2523 |
|
|
cgi_kill( ClientData client_data, struct timeval* nowP ) |
2524 |
|
|
{ |
2525 |
|
|
pid_t pid; |
2526 |
|
|
|
2527 |
|
|
/* Before trying to kill the CGI process, reap any zombie processes. |
2528 |
|
|
** That may get rid of the CGI process. |
2529 |
|
|
*/ |
2530 |
|
|
(void) do_reap(); |
2531 |
|
|
|
2532 |
|
|
pid = (pid_t) client_data.i; |
2533 |
|
|
if ( kill( pid, SIGINT ) == 0 ) |
2534 |
|
|
{ |
2535 |
|
|
syslog( LOG_ERR, "killed CGI process %d", pid ); |
2536 |
|
|
/* In case this isn't enough, schedule an uncatchable kill. */ |
2537 |
|
|
if ( tmr_create( nowP, cgi_kill2, client_data, 5 * 1000L, 0 ) == (Timer*) 0 ) |
2538 |
|
|
{ |
2539 |
|
|
syslog( LOG_CRIT, "tmr_create(cgi_kill2) failed" ); |
2540 |
|
|
exit( 1 ); |
2541 |
|
|
} |
2542 |
|
|
} |
2543 |
|
|
} |
2544 |
|
|
#endif /* CGI_TIMELIMIT */ |
2545 |
|
|
|
2546 |
|
|
|
2547 |
|
|
#ifdef GENERATE_INDEXES |
2548 |
|
|
|
2549 |
|
|
/* qsort comparison routine - declared old-style on purpose, for portability. */ |
2550 |
|
|
static int |
2551 |
|
|
name_compare( a, b ) |
2552 |
|
|
char** a; |
2553 |
|
|
char** b; |
2554 |
|
|
{ |
2555 |
|
|
return strcmp( *a, *b ); |
2556 |
|
|
} |
2557 |
|
|
|
2558 |
|
|
|
2559 |
|
|
static off_t |
2560 |
|
|
ls( httpd_conn* hc ) |
2561 |
|
|
{ |
2562 |
|
|
DIR* dirp; |
2563 |
|
|
struct dirent* de; |
2564 |
|
|
int namlen; |
2565 |
|
|
static int maxnames = 0; |
2566 |
|
|
int nnames; |
2567 |
|
|
static char* names; |
2568 |
|
|
static char** nameptrs; |
2569 |
|
|
static char* name; |
2570 |
|
|
static int maxname = 0; |
2571 |
|
|
static char* rname; |
2572 |
|
|
static int maxrname = 0; |
2573 |
|
|
static char* encrname; |
2574 |
|
|
static int maxencrname = 0; |
2575 |
|
|
FILE* fp; |
2576 |
|
|
int i, r; |
2577 |
|
|
struct stat sb; |
2578 |
|
|
struct stat lsb; |
2579 |
|
|
char modestr[20]; |
2580 |
|
|
char* linkprefix; |
2581 |
|
|
char link[MAXPATHLEN]; |
2582 |
|
|
int linklen; |
2583 |
|
|
char* fileclass; |
2584 |
|
|
time_t now; |
2585 |
|
|
char* timestr; |
2586 |
|
|
ClientData client_data; |
2587 |
|
|
|
2588 |
|
|
dirp = opendir( hc->expnfilename ); |
2589 |
|
|
if ( dirp == (DIR*) 0 ) |
2590 |
|
|
{ |
2591 |
|
|
syslog( LOG_ERR, "opendir %.80s - %m", hc->expnfilename ); |
2592 |
|
|
httpd_send_err( hc, 404, err404title, "", err404form, hc->encodedurl ); |
2593 |
|
|
return -1; |
2594 |
|
|
} |
2595 |
|
|
|
2596 |
|
|
send_mime( hc, 200, ok200title, "", "", "text/html", -1, hc->sb.st_mtime ); |
2597 |
|
|
if ( hc->method == METHOD_HEAD ) |
2598 |
|
|
closedir( dirp ); |
2599 |
|
|
else if ( hc->method == METHOD_GET ) |
2600 |
|
|
{ |
2601 |
|
|
httpd_write_response( hc ); |
2602 |
|
|
r = fork( ); |
2603 |
|
|
if ( r < 0 ) |
2604 |
|
|
{ |
2605 |
|
|
syslog( LOG_ERR, "fork - %m" ); |
2606 |
|
|
httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl ); |
2607 |
|
|
return -1; |
2608 |
|
|
} |
2609 |
|
|
if ( r == 0 ) |
2610 |
|
|
{ |
2611 |
|
|
/* Child process. */ |
2612 |
|
|
unlisten( hc->hs ); |
2613 |
|
|
|
2614 |
|
|
#ifdef CGI_NICE |
2615 |
|
|
/* Set priority. */ |
2616 |
|
|
(void) nice( CGI_NICE ); |
2617 |
|
|
#endif /* CGI_NICE */ |
2618 |
|
|
|
2619 |
|
|
/* Open a stdio stream so that we can use fprintf, which is more |
2620 |
|
|
** efficient than a bunch of separate write()s. We don't have |
2621 |
|
|
** to worry about double closes or file descriptor leaks cause |
2622 |
|
|
** we're in a subprocess. |
2623 |
|
|
*/ |
2624 |
|
|
fp = fdopen( hc->conn_fd, "w" ); |
2625 |
|
|
if ( fp == (FILE*) 0 ) |
2626 |
|
|
{ |
2627 |
|
|
syslog( LOG_ERR, "fdopen - %m" ); |
2628 |
|
|
httpd_send_err( |
2629 |
|
|
hc, 500, err500title, "", err500form, hc->encodedurl ); |
2630 |
|
|
closedir( dirp ); |
2631 |
|
|
exit( 1 ); |
2632 |
|
|
} |
2633 |
|
|
|
2634 |
|
|
(void) fprintf( fp, "\ |
2635 |
|
|
<HTML><HEAD><TITLE>Index of %.80s</TITLE></HEAD>\n\ |
2636 |
|
|
<BODY BGCOLOR=\"#99cc99\">\n\ |
2637 |
|
|
<H2>Index of %.80s</H2>\n\ |
2638 |
|
|
<PRE>\n\ |
2639 |
|
|
mode links bytes last-changed name\n\ |
2640 |
|
|
<HR>", |
2641 |
|
|
hc->encodedurl, hc->encodedurl ); |
2642 |
|
|
|
2643 |
|
|
/* Read in names. */ |
2644 |
|
|
nnames = 0; |
2645 |
|
|
while ( ( de = readdir( dirp ) ) != 0 ) /* dirent or direct */ |
2646 |
|
|
{ |
2647 |
|
|
if ( nnames >= maxnames ) |
2648 |
|
|
{ |
2649 |
|
|
if ( maxnames == 0 ) |
2650 |
|
|
{ |
2651 |
|
|
maxnames = 100; |
2652 |
|
|
names = NEW( char, maxnames * MAXPATHLEN ); |
2653 |
|
|
nameptrs = NEW( char*, maxnames ); |
2654 |
|
|
} |
2655 |
|
|
else |
2656 |
|
|
{ |
2657 |
|
|
maxnames *= 2; |
2658 |
|
|
names = RENEW( names, char, maxnames * MAXPATHLEN ); |
2659 |
|
|
nameptrs = RENEW( nameptrs, char*, maxnames ); |
2660 |
|
|
} |
2661 |
|
|
if ( names == (char*) 0 || nameptrs == (char**) 0 ) |
2662 |
|
|
{ |
2663 |
|
|
syslog( LOG_ERR, "out of memory reallocating directory names" ); |
2664 |
|
|
exit( 1 ); |
2665 |
|
|
} |
2666 |
|
|
for ( i = 0; i < maxnames; ++i ) |
2667 |
|
|
nameptrs[i] = &names[i * MAXPATHLEN]; |
2668 |
|
|
} |
2669 |
|
|
namlen = NAMLEN(de); |
2670 |
|
|
(void) strncpy( nameptrs[nnames], de->d_name, namlen ); |
2671 |
|
|
nameptrs[nnames][namlen] = '\0'; |
2672 |
|
|
++nnames; |
2673 |
|
|
} |
2674 |
|
|
closedir( dirp ); |
2675 |
|
|
|
2676 |
|
|
/* Sort the names. */ |
2677 |
|
|
qsort( nameptrs, nnames, sizeof(*nameptrs), name_compare ); |
2678 |
|
|
|
2679 |
|
|
/* Generate output. */ |
2680 |
|
|
for ( i = 0; i < nnames; ++i ) |
2681 |
|
|
{ |
2682 |
|
|
httpd_realloc_str( |
2683 |
|
|
&name, &maxname, |
2684 |
|
|
strlen( hc->expnfilename ) + 1 + strlen( nameptrs[i] ) ); |
2685 |
|
|
httpd_realloc_str( |
2686 |
|
|
&rname, &maxrname, |
2687 |
|
|
strlen( hc->origfilename ) + 1 + strlen( nameptrs[i] ) ); |
2688 |
|
|
if ( hc->expnfilename[0] == '\0' || |
2689 |
|
|
strcmp( hc->expnfilename, "." ) == 0 ) |
2690 |
|
|
{ |
2691 |
|
|
(void) strcpy( name, nameptrs[i] ); |
2692 |
|
|
(void) strcpy( rname, nameptrs[i] ); |
2693 |
|
|
} |
2694 |
|
|
else |
2695 |
|
|
{ |
2696 |
|
|
(void) my_snprintf( name, maxname, |
2697 |
|
|
"%s/%s", hc->expnfilename, nameptrs[i] ); |
2698 |
|
|
if ( strcmp( hc->origfilename, "." ) == 0 ) |
2699 |
|
|
(void) my_snprintf( rname, maxrname, |
2700 |
|
|
"%s", nameptrs[i] ); |
2701 |
|
|
else |
2702 |
|
|
(void) my_snprintf( rname, maxrname, |
2703 |
|
|
"%s%s", hc->origfilename, nameptrs[i] ); |
2704 |
|
|
} |
2705 |
|
|
httpd_realloc_str( |
2706 |
|
|
&encrname, &maxencrname, 3 * strlen( rname ) + 1 ); |
2707 |
|
|
strencode( encrname, maxencrname, rname ); |
2708 |
|
|
|
2709 |
|
|
if ( stat( name, &sb ) < 0 || lstat( name, &lsb ) < 0 ) |
2710 |
|
|
continue; |
2711 |
|
|
|
2712 |
|
|
linkprefix = ""; |
2713 |
|
|
link[0] = '\0'; |
2714 |
|
|
/* Break down mode word. First the file type. */ |
2715 |
|
|
switch ( lsb.st_mode & S_IFMT ) |
2716 |
|
|
{ |
2717 |
|
|
case S_IFIFO: modestr[0] = 'p'; break; |
2718 |
|
|
case S_IFCHR: modestr[0] = 'c'; break; |
2719 |
|
|
case S_IFDIR: modestr[0] = 'd'; break; |
2720 |
|
|
case S_IFBLK: modestr[0] = 'b'; break; |
2721 |
|
|
case S_IFREG: modestr[0] = '-'; break; |
2722 |
|
|
case S_IFSOCK: modestr[0] = 's'; break; |
2723 |
|
|
case S_IFLNK: modestr[0] = 'l'; |
2724 |
|
|
linklen = readlink( name, link, sizeof(link) ); |
2725 |
|
|
if ( linklen != -1 ) |
2726 |
|
|
{ |
2727 |
|
|
link[linklen] = '\0'; |
2728 |
|
|
linkprefix = " -> "; |
2729 |
|
|
} |
2730 |
|
|
break; |
2731 |
|
|
default: modestr[0] = '?'; break; |
2732 |
|
|
} |
2733 |
|
|
/* Now the world permissions. Owner and group permissions |
2734 |
|
|
** are not of interest to web clients. |
2735 |
|
|
*/ |
2736 |
|
|
modestr[1] = ( lsb.st_mode & S_IROTH ) ? 'r' : '-'; |
2737 |
|
|
modestr[2] = ( lsb.st_mode & S_IWOTH ) ? 'w' : '-'; |
2738 |
|
|
modestr[3] = ( lsb.st_mode & S_IXOTH ) ? 'x' : '-'; |
2739 |
|
|
modestr[4] = '\0'; |
2740 |
|
|
|
2741 |
|
|
/* We also leave out the owner and group name, they are |
2742 |
|
|
** also not of interest to web clients. Plus if we're |
2743 |
|
|
** running under chroot(), they would require a copy |
2744 |
|
|
** of /etc/passwd and /etc/group, which we want to avoid. |
2745 |
|
|
*/ |
2746 |
|
|
|
2747 |
|
|
/* Get time string. */ |
2748 |
|
|
now = time( (time_t*) 0 ); |
2749 |
|
|
timestr = ctime( &lsb.st_mtime ); |
2750 |
|
|
timestr[ 0] = timestr[ 4]; |
2751 |
|
|
timestr[ 1] = timestr[ 5]; |
2752 |
|
|
timestr[ 2] = timestr[ 6]; |
2753 |
|
|
timestr[ 3] = ' '; |
2754 |
|
|
timestr[ 4] = timestr[ 8]; |
2755 |
|
|
timestr[ 5] = timestr[ 9]; |
2756 |
|
|
timestr[ 6] = ' '; |
2757 |
|
|
if ( now - lsb.st_mtime > 60*60*24*182 ) /* 1/2 year */ |
2758 |
|
|
{ |
2759 |
|
|
timestr[ 7] = ' '; |
2760 |
|
|
timestr[ 8] = timestr[20]; |
2761 |
|
|
timestr[ 9] = timestr[21]; |
2762 |
|
|
timestr[10] = timestr[22]; |
2763 |
|
|
timestr[11] = timestr[23]; |
2764 |
|
|
} |
2765 |
|
|
else |
2766 |
|
|
{ |
2767 |
|
|
timestr[ 7] = timestr[11]; |
2768 |
|
|
timestr[ 8] = timestr[12]; |
2769 |
|
|
timestr[ 9] = ':'; |
2770 |
|
|
timestr[10] = timestr[14]; |
2771 |
|
|
timestr[11] = timestr[15]; |
2772 |
|
|
} |
2773 |
|
|
timestr[12] = '\0'; |
2774 |
|
|
|
2775 |
|
|
/* The ls -F file class. */ |
2776 |
|
|
switch ( sb.st_mode & S_IFMT ) |
2777 |
|
|
{ |
2778 |
|
|
case S_IFDIR: fileclass = "/"; break; |
2779 |
|
|
case S_IFSOCK: fileclass = "="; break; |
2780 |
|
|
case S_IFLNK: fileclass = "@"; break; |
2781 |
|
|
default: |
2782 |
|
|
fileclass = ( sb.st_mode & S_IXOTH ) ? "*" : ""; |
2783 |
|
|
break; |
2784 |
|
|
} |
2785 |
|
|
|
2786 |
|
|
/* And print. */ |
2787 |
|
|
(void) fprintf( fp, |
2788 |
|
|
"%s %3ld %8ld %s <A HREF=\"/%.500s%s\">%.80s</A>%s%s%s\n", |
2789 |
|
|
modestr, (long) lsb.st_nlink, (long) lsb.st_size, timestr, |
2790 |
|
|
encrname, S_ISDIR(sb.st_mode) ? "/" : "", |
2791 |
|
|
nameptrs[i], linkprefix, link, fileclass ); |
2792 |
|
|
} |
2793 |
|
|
|
2794 |
|
|
(void) fprintf( fp, "</PRE></BODY></HTML>\n" ); |
2795 |
|
|
(void) fclose( fp ); |
2796 |
|
|
exit( 0 ); |
2797 |
|
|
} |
2798 |
|
|
|
2799 |
|
|
/* Parent process. */ |
2800 |
|
|
closedir( dirp ); |
2801 |
|
|
syslog( LOG_INFO, "spawned indexing process %d for directory '%.200s'", r, hc->expnfilename ); |
2802 |
|
|
#ifdef CGI_TIMELIMIT |
2803 |
|
|
/* Schedule a kill for the child process, in case it runs too long */ |
2804 |
|
|
client_data.i = r; |
2805 |
|
|
if ( tmr_create( (struct timeval*) 0, cgi_kill, client_data, CGI_TIMELIMIT * 1000L, 0 ) == (Timer*) 0 ) |
2806 |
|
|
{ |
2807 |
|
|
syslog( LOG_CRIT, "tmr_create(cgi_kill) failed" ); |
2808 |
|
|
exit( 1 ); |
2809 |
|
|
} |
2810 |
|
|
#endif /* CGI_TIMELIMIT */ |
2811 |
|
|
hc->status = 200; |
2812 |
|
|
hc->bytes_sent = CGI_BYTECOUNT; |
2813 |
|
|
hc->should_linger = 0; |
2814 |
|
|
} |
2815 |
|
|
else |
2816 |
|
|
{ |
2817 |
|
|
httpd_send_err( |
2818 |
|
|
hc, 501, err501title, "", err501form, httpd_method_str( hc->method ) ); |
2819 |
|
|
return -1; |
2820 |
|
|
} |
2821 |
|
|
|
2822 |
|
|
return 0; |
2823 |
|
|
} |
2824 |
|
|
|
2825 |
|
|
#endif /* GENERATE_INDEXES */ |
2826 |
|
|
|
2827 |
|
|
|
2828 |
|
|
static char* |
2829 |
|
|
build_env( char* fmt, char* arg ) |
2830 |
|
|
{ |
2831 |
|
|
char* cp; |
2832 |
|
|
int size; |
2833 |
|
|
static char* buf; |
2834 |
|
|
static int maxbuf = 0; |
2835 |
|
|
|
2836 |
|
|
size = strlen( fmt ) + strlen( arg ); |
2837 |
|
|
if ( size > maxbuf ) |
2838 |
|
|
httpd_realloc_str( &buf, &maxbuf, size ); |
2839 |
|
|
(void) my_snprintf( buf, maxbuf, |
2840 |
|
|
fmt, arg ); |
2841 |
|
|
cp = strdup( buf ); |
2842 |
|
|
if ( cp == (char*) 0 ) |
2843 |
|
|
{ |
2844 |
|
|
syslog( LOG_ERR, "out of memory copying environment variable" ); |
2845 |
|
|
exit( 1 ); |
2846 |
|
|
} |
2847 |
|
|
return cp; |
2848 |
|
|
} |
2849 |
|
|
|
2850 |
|
|
|
2851 |
|
|
#ifdef SERVER_NAME_LIST |
2852 |
|
|
static char* |
2853 |
|
|
hostname_map( char* hostname ) |
2854 |
|
|
{ |
2855 |
|
|
int len, n; |
2856 |
|
|
static char* list[] = { SERVER_NAME_LIST }; |
2857 |
|
|
|
2858 |
|
|
len = strlen( hostname ); |
2859 |
|
|
for ( n = sizeof(list) / sizeof(*list) - 1; n >= 0; --n ) |
2860 |
|
|
if ( strncasecmp( hostname, list[n], len ) == 0 ) |
2861 |
|
|
if ( list[n][len] == '/' ) /* check in case of a substring match */ |
2862 |
|
|
return &list[n][len + 1]; |
2863 |
|
|
return (char*) 0; |
2864 |
|
|
} |
2865 |
|
|
#endif /* SERVER_NAME_LIST */ |
2866 |
|
|
|
2867 |
|
|
|
2868 |
|
|
/* Set up environment variables. Be real careful here to avoid |
2869 |
|
|
** letting malicious clients overrun a buffer. We don't have |
2870 |
|
|
** to worry about freeing stuff since we're a sub-process. |
2871 |
|
|
*/ |
2872 |
|
|
static char** |
2873 |
|
|
make_envp( httpd_conn* hc ) |
2874 |
|
|
{ |
2875 |
|
|
static char* envp[50]; |
2876 |
|
|
int envn; |
2877 |
|
|
char* cp; |
2878 |
|
|
char buf[256]; |
2879 |
|
|
|
2880 |
|
|
envn = 0; |
2881 |
|
|
envp[envn++] = build_env( "PATH=%s", CGI_PATH ); |
2882 |
|
|
#ifdef CGI_LD_LIBRARY_PATH |
2883 |
|
|
envp[envn++] = build_env( "LD_LIBRARY_PATH=%s", CGI_LD_LIBRARY_PATH ); |
2884 |
|
|
#endif /* CGI_LD_LIBRARY_PATH */ |
2885 |
|
|
envp[envn++] = build_env( "SERVER_SOFTWARE=%s", SERVER_SOFTWARE ); |
2886 |
|
|
/* If vhosting, use that server-name here. */ |
2887 |
|
|
if ( hc->hs->vhost && hc->hostname != (char*) 0 ) |
2888 |
|
|
cp = hc->hostname; |
2889 |
|
|
else |
2890 |
|
|
cp = hc->hs->server_hostname; |
2891 |
|
|
if ( cp != (char*) 0 ) |
2892 |
|
|
envp[envn++] = build_env( "SERVER_NAME=%s", cp ); |
2893 |
|
|
envp[envn++] = "GATEWAY_INTERFACE=CGI/1.1"; |
2894 |
|
|
envp[envn++] = build_env("SERVER_PROTOCOL=%s", hc->protocol); |
2895 |
|
|
(void) my_snprintf( buf, sizeof(buf), |
2896 |
|
|
"%d", hc->hs->port ); |
2897 |
|
|
envp[envn++] = build_env( "SERVER_PORT=%s", buf ); |
2898 |
|
|
envp[envn++] = build_env( |
2899 |
|
|
"REQUEST_METHOD=%s", httpd_method_str( hc->method ) ); |
2900 |
|
|
if ( hc->pathinfo[0] != '\0' ) |
2901 |
|
|
{ |
2902 |
|
|
char* cp2; |
2903 |
|
|
int l; |
2904 |
|
|
envp[envn++] = build_env( "PATH_INFO=/%s", hc->pathinfo ); |
2905 |
|
|
l = strlen( hc->hs->cwd ) + strlen( hc->pathinfo ) + 1; |
2906 |
|
|
cp2 = NEW( char, l ); |
2907 |
|
|
if ( cp2 != (char*) 0 ) |
2908 |
|
|
{ |
2909 |
|
|
(void) my_snprintf( cp2, l, |
2910 |
|
|
"%s%s", hc->hs->cwd, hc->pathinfo ); |
2911 |
|
|
envp[envn++] = build_env( "PATH_TRANSLATED=%s", cp2 ); |
2912 |
|
|
} |
2913 |
|
|
} |
2914 |
|
|
envp[envn++] = build_env( |
2915 |
|
|
"SCRIPT_NAME=/%s", strcmp( hc->origfilename, "." ) == 0 ? |
2916 |
|
|
"" : hc->origfilename ); |
2917 |
|
|
if ( hc->query[0] != '\0') |
2918 |
|
|
envp[envn++] = build_env( "QUERY_STRING=%s", hc->query ); |
2919 |
|
|
envp[envn++] = build_env( |
2920 |
|
|
"REMOTE_ADDR=%s", httpd_ntoa( &hc->client_addr ) ); |
2921 |
|
|
if ( hc->referer[0] != '\0' ) |
2922 |
|
|
envp[envn++] = build_env( "HTTP_REFERER=%s", hc->referer ); |
2923 |
|
|
if ( hc->useragent[0] != '\0' ) |
2924 |
|
|
envp[envn++] = build_env( "HTTP_USER_AGENT=%s", hc->useragent ); |
2925 |
|
|
if ( hc->accept[0] != '\0' ) |
2926 |
|
|
envp[envn++] = build_env( "HTTP_ACCEPT=%s", hc->accept ); |
2927 |
|
|
if ( hc->accepte[0] != '\0' ) |
2928 |
|
|
envp[envn++] = build_env( "HTTP_ACCEPT_ENCODING=%s", hc->accepte ); |
2929 |
|
|
if ( hc->acceptl[0] != '\0' ) |
2930 |
|
|
envp[envn++] = build_env( "HTTP_ACCEPT_LANGUAGE=%s", hc->acceptl ); |
2931 |
|
|
if ( hc->cookie[0] != '\0' ) |
2932 |
|
|
envp[envn++] = build_env( "HTTP_COOKIE=%s", hc->cookie ); |
2933 |
|
|
if ( hc->contenttype[0] != '\0' ) |
2934 |
|
|
envp[envn++] = build_env( "CONTENT_TYPE=%s", hc->contenttype ); |
2935 |
|
|
if ( hc->hdrhost[0] != '\0' ) |
2936 |
|
|
envp[envn++] = build_env( "HTTP_HOST=%s", hc->hdrhost ); |
2937 |
|
|
if ( hc->contentlength != -1 ) |
2938 |
|
|
{ |
2939 |
|
|
(void) my_snprintf( buf, sizeof(buf), |
2940 |
|
|
"%ld", (long) hc->contentlength ); |
2941 |
|
|
envp[envn++] = build_env( "CONTENT_LENGTH=%s", buf ); |
2942 |
|
|
} |
2943 |
|
|
if ( hc->remoteuser[0] != '\0' ) |
2944 |
|
|
envp[envn++] = build_env( "REMOTE_USER=%s", hc->remoteuser ); |
2945 |
|
|
if ( hc->authorization[0] == '\0' ) |
2946 |
|
|
envp[envn++] = build_env( "AUTH_TYPE=%s", "Basic" ); |
2947 |
|
|
/* We only support Basic auth at the moment. */ |
2948 |
|
|
if ( getenv( "TZ" ) != (char*) 0 ) |
2949 |
|
|
envp[envn++] = build_env( "TZ=%s", getenv( "TZ" ) ); |
2950 |
|
|
envp[envn++] = build_env( "CGI_PATTERN=%s", hc->hs->cgi_pattern ); |
2951 |
|
|
|
2952 |
|
|
envp[envn] = (char*) 0; |
2953 |
|
|
return envp; |
2954 |
|
|
} |
2955 |
|
|
|
2956 |
|
|
|
2957 |
|
|
/* Set up argument vector. Again, we don't have to worry about freeing stuff |
2958 |
|
|
** since we're a sub-process. This gets done after make_envp() because we |
2959 |
|
|
** scribble on hc->query. |
2960 |
|
|
*/ |
2961 |
|
|
static char** |
2962 |
|
|
make_argp( httpd_conn* hc ) |
2963 |
|
|
{ |
2964 |
|
|
char** argp; |
2965 |
|
|
int argn; |
2966 |
|
|
char* cp1; |
2967 |
|
|
char* cp2; |
2968 |
|
|
|
2969 |
|
|
/* By allocating an arg slot for every character in the query, plus |
2970 |
|
|
** one for the filename and one for the NULL, we are guaranteed to |
2971 |
|
|
** have enough. We could actually use strlen/2. |
2972 |
|
|
*/ |
2973 |
|
|
argp = NEW( char*, strlen( hc->query ) + 2 ); |
2974 |
|
|
if ( argp == (char**) 0 ) |
2975 |
|
|
return (char**) 0; |
2976 |
|
|
|
2977 |
|
|
argp[0] = strrchr( hc->expnfilename, '/' ); |
2978 |
|
|
if ( argp[0] != (char*) 0 ) |
2979 |
|
|
++argp[0]; |
2980 |
|
|
else |
2981 |
|
|
argp[0] = hc->expnfilename; |
2982 |
|
|
|
2983 |
|
|
argn = 1; |
2984 |
|
|
/* According to the CGI spec at http://hoohoo.ncsa.uiuc.edu/cgi/cl.html, |
2985 |
|
|
** "The server should search the query information for a non-encoded = |
2986 |
|
|
** character to determine if the command line is to be used, if it finds |
2987 |
|
|
** one, the command line is not to be used." |
2988 |
|
|
*/ |
2989 |
|
|
if ( strchr( hc->query, '=' ) == (char*) 0 ) |
2990 |
|
|
{ |
2991 |
|
|
for ( cp1 = cp2 = hc->query; *cp2 != '\0'; ++cp2 ) |
2992 |
|
|
{ |
2993 |
|
|
if ( *cp2 == '+' ) |
2994 |
|
|
{ |
2995 |
|
|
*cp2 = '\0'; |
2996 |
|
|
strdecode( cp1, cp1 ); |
2997 |
|
|
argp[argn++] = cp1; |
2998 |
|
|
cp1 = cp2 + 1; |
2999 |
|
|
} |
3000 |
|
|
} |
3001 |
|
|
if ( cp2 != cp1 ) |
3002 |
|
|
{ |
3003 |
|
|
strdecode( cp1, cp1 ); |
3004 |
|
|
argp[argn++] = cp1; |
3005 |
|
|
} |
3006 |
|
|
} |
3007 |
|
|
|
3008 |
|
|
argp[argn] = (char*) 0; |
3009 |
|
|
return argp; |
3010 |
|
|
} |
3011 |
|
|
|
3012 |
|
|
|
3013 |
|
|
/* This routine is used only for POST requests. It reads the data |
3014 |
|
|
** from the request and sends it to the child process. The only reason |
3015 |
|
|
** we need to do it this way instead of just letting the child read |
3016 |
|
|
** directly is that we have already read part of the data into our |
3017 |
|
|
** buffer. |
3018 |
|
|
*/ |
3019 |
|
|
static void |
3020 |
|
|
cgi_interpose_input( httpd_conn* hc, int wfd ) |
3021 |
|
|
{ |
3022 |
|
|
int c, r; |
3023 |
|
|
char buf[1024]; |
3024 |
|
|
|
3025 |
|
|
c = hc->read_idx - hc->checked_idx; |
3026 |
|
|
if ( c > 0 ) |
3027 |
|
|
{ |
3028 |
|
|
if ( write( wfd, &(hc->read_buf[hc->checked_idx]), c ) != c ) |
3029 |
|
|
return; |
3030 |
|
|
} |
3031 |
|
|
while ( c < hc->contentlength ) |
3032 |
|
|
{ |
3033 |
|
|
r = read( hc->conn_fd, buf, MIN( sizeof(buf), hc->contentlength - c ) ); |
3034 |
|
|
if ( r == 0 ) |
3035 |
|
|
sleep( 1 ); |
3036 |
|
|
else if ( r < 0 ) |
3037 |
|
|
{ |
3038 |
|
|
if ( errno == EAGAIN ) |
3039 |
|
|
sleep( 1 ); |
3040 |
|
|
else |
3041 |
|
|
return; |
3042 |
|
|
} |
3043 |
|
|
else |
3044 |
|
|
{ |
3045 |
|
|
if ( write( wfd, buf, r ) != r ) |
3046 |
|
|
return; |
3047 |
|
|
c += r; |
3048 |
|
|
} |
3049 |
|
|
} |
3050 |
|
|
post_post_garbage_hack( hc ); |
3051 |
|
|
} |
3052 |
|
|
|
3053 |
|
|
|
3054 |
|
|
/* Special hack to deal with broken browsers that send a LF or CRLF |
3055 |
|
|
** after POST data, causing TCP resets - we just read and discard up |
3056 |
|
|
** to 2 bytes. Unfortunately this doesn't fix the problem for CGIs |
3057 |
|
|
** which avoid the interposer process due to their POST data being |
3058 |
|
|
** short. Creating an interposer process for all POST CGIs is |
3059 |
|
|
** unacceptably expensive. The eventual fix will come when interposing |
3060 |
|
|
** gets integrated into the main loop as a tasklet instead of a process. |
3061 |
|
|
*/ |
3062 |
|
|
static void |
3063 |
|
|
post_post_garbage_hack( httpd_conn* hc ) |
3064 |
|
|
{ |
3065 |
|
|
char buf[2]; |
3066 |
|
|
int r; |
3067 |
|
|
|
3068 |
|
|
r = recv( hc->conn_fd, buf, sizeof(buf), MSG_PEEK ); |
3069 |
|
|
if ( r > 0 ) |
3070 |
|
|
(void) read( hc->conn_fd, buf, r ); |
3071 |
|
|
} |
3072 |
|
|
|
3073 |
|
|
|
3074 |
|
|
/* This routine is used for parsed-header CGIs. The idea here is that the |
3075 |
|
|
** CGI can return special headers such as "Status:" and "Location:" which |
3076 |
|
|
** change the return status of the response. Since the return status has to |
3077 |
|
|
** be the very first line written out, we have to accumulate all the headers |
3078 |
|
|
** and check for the special ones before writing the status. Then we write |
3079 |
|
|
** out the saved headers and proceed to echo the rest of the response. |
3080 |
|
|
*/ |
3081 |
|
|
static void |
3082 |
|
|
cgi_interpose_output( httpd_conn* hc, int rfd ) |
3083 |
|
|
{ |
3084 |
|
|
int r; |
3085 |
|
|
char buf[1024]; |
3086 |
|
|
int headers_size, headers_len; |
3087 |
|
|
char* headers; |
3088 |
|
|
char* br; |
3089 |
|
|
int status; |
3090 |
|
|
char* title; |
3091 |
|
|
char* cp; |
3092 |
|
|
|
3093 |
|
|
/* Slurp in all headers. */ |
3094 |
|
|
headers_size = 0; |
3095 |
|
|
httpd_realloc_str( &headers, &headers_size, 500 ); |
3096 |
|
|
headers_len = 0; |
3097 |
|
|
for (;;) |
3098 |
|
|
{ |
3099 |
|
|
r = read( rfd, buf, sizeof(buf) ); |
3100 |
|
|
if ( r <= 0 ) |
3101 |
|
|
{ |
3102 |
|
|
br = &(headers[headers_len]); |
3103 |
|
|
break; |
3104 |
|
|
} |
3105 |
|
|
httpd_realloc_str( &headers, &headers_size, headers_len + r ); |
3106 |
|
|
(void) memcpy( &(headers[headers_len]), buf, r ); |
3107 |
|
|
headers_len += r; |
3108 |
|
|
headers[headers_len] = '\0'; |
3109 |
|
|
if ( ( br = strstr( headers, "\r\n\r\n" ) ) != (char*) 0 || |
3110 |
|
|
( br = strstr( headers, "\n\n" ) ) != (char*) 0 ) |
3111 |
|
|
break; |
3112 |
|
|
} |
3113 |
|
|
|
3114 |
|
|
/* Figure out the status. */ |
3115 |
|
|
status = 200; |
3116 |
|
|
if ( ( cp = strstr( headers, "Status:" ) ) != (char*) 0 && |
3117 |
|
|
cp < br && |
3118 |
|
|
( cp == headers || *(cp-1) == '\n' ) ) |
3119 |
|
|
{ |
3120 |
|
|
cp += 7; |
3121 |
|
|
cp += strspn( cp, " \t" ); |
3122 |
|
|
status = atoi( cp ); |
3123 |
|
|
} |
3124 |
|
|
if ( ( cp = strstr( headers, "Location:" ) ) != (char*) 0 && |
3125 |
|
|
cp < br && |
3126 |
|
|
( cp == headers || *(cp-1) == '\n' ) ) |
3127 |
|
|
status = 302; |
3128 |
|
|
|
3129 |
|
|
/* Write the status line. */ |
3130 |
|
|
switch ( status ) |
3131 |
|
|
{ |
3132 |
|
|
case 200: title = ok200title; break; |
3133 |
|
|
case 302: title = err302title; break; |
3134 |
|
|
case 304: title = err304title; break; |
3135 |
|
|
case 400: title = httpd_err400title; break; |
3136 |
|
|
#ifdef AUTH_FILE |
3137 |
|
|
case 401: title = err401title; break; |
3138 |
|
|
#endif /* AUTH_FILE */ |
3139 |
|
|
case 403: title = err403title; break; |
3140 |
|
|
case 404: title = err404title; break; |
3141 |
|
|
case 408: title = httpd_err408title; break; |
3142 |
|
|
case 500: title = err500title; break; |
3143 |
|
|
case 501: title = err501title; break; |
3144 |
|
|
case 503: title = httpd_err503title; break; |
3145 |
|
|
default: title = "Something"; break; |
3146 |
|
|
} |
3147 |
|
|
(void) my_snprintf( buf, sizeof(buf), "HTTP/1.0 %d %s\r\n", status, title ); |
3148 |
|
|
(void) write( hc->conn_fd, buf, strlen( buf ) ); |
3149 |
|
|
|
3150 |
|
|
/* Write the saved headers. */ |
3151 |
|
|
(void) write( hc->conn_fd, headers, headers_len ); |
3152 |
|
|
|
3153 |
|
|
/* Echo the rest of the output. */ |
3154 |
|
|
for (;;) |
3155 |
|
|
{ |
3156 |
|
|
r = read( rfd, buf, sizeof(buf) ); |
3157 |
|
|
if ( r <= 0 ) |
3158 |
|
|
return; |
3159 |
|
|
if ( write( hc->conn_fd, buf, r ) != r ) |
3160 |
|
|
return; |
3161 |
|
|
} |
3162 |
|
|
} |
3163 |
|
|
|
3164 |
|
|
|
3165 |
|
|
/* CGI child process. */ |
3166 |
|
|
static void |
3167 |
|
|
cgi_child( httpd_conn* hc ) |
3168 |
|
|
{ |
3169 |
|
|
int r; |
3170 |
|
|
char** argp; |
3171 |
|
|
char** envp; |
3172 |
|
|
char* binary; |
3173 |
|
|
char* directory; |
3174 |
|
|
|
3175 |
|
|
/* Unset close-on-exec flag for this socket. This actually shouldn't |
3176 |
|
|
** be necessary, according to POSIX a dup()'d file descriptor does |
3177 |
|
|
** *not* inherit the close-on-exec flag, its flag is always clear. |
3178 |
|
|
** However, Linux messes this up and does copy the flag to the |
3179 |
|
|
** dup()'d descriptor, so we have to clear it. This could be |
3180 |
|
|
** ifdeffed for Linux only. |
3181 |
|
|
*/ |
3182 |
|
|
(void) fcntl( hc->conn_fd, F_SETFD, 0 ); |
3183 |
|
|
|
3184 |
|
|
/* Close the syslog descriptor so that the CGI program can't |
3185 |
|
|
** mess with it. All other open descriptors should be either |
3186 |
|
|
** the listen socket(s), sockets from accept(), or the file-logging |
3187 |
|
|
** fd, and all of those are set to close-on-exec, so we don't |
3188 |
|
|
** have to close anything else. |
3189 |
|
|
*/ |
3190 |
|
|
closelog(); |
3191 |
|
|
|
3192 |
|
|
/* If the socket happens to be using one of the stdin/stdout/stderr |
3193 |
|
|
** descriptors, move it to another descriptor so that the dup2 calls |
3194 |
|
|
** below don't screw things up. |
3195 |
|
|
*/ |
3196 |
|
|
if ( hc->conn_fd == STDIN_FILENO || hc->conn_fd == STDOUT_FILENO || hc->conn_fd == STDERR_FILENO ) |
3197 |
|
|
{ |
3198 |
|
|
int newfd = dup( hc->conn_fd ); |
3199 |
|
|
if ( newfd >= 0 ) |
3200 |
|
|
hc->conn_fd = newfd; |
3201 |
|
|
/* If the dup fails, shrug. We'll just take our chances. |
3202 |
|
|
** Shouldn't happen though. |
3203 |
|
|
** |
3204 |
|
|
** If the dup happens to produce an fd that is still one of |
3205 |
|
|
** the standard ones, we should be ok - I think it can be |
3206 |
|
|
** fd 2, stderr, but can never show up as 0 or 1 since at |
3207 |
|
|
** least two file descriptors are always in use. Because |
3208 |
|
|
** of the order in which we dup2 things below - stderr is |
3209 |
|
|
** always done last - it's actually ok for the socket to |
3210 |
|
|
** be fd 2. It'll just get dup2'd onto itself. |
3211 |
|
|
*/ |
3212 |
|
|
} |
3213 |
|
|
|
3214 |
|
|
/* Make the environment vector. */ |
3215 |
|
|
envp = make_envp( hc ); |
3216 |
|
|
|
3217 |
|
|
/* Make the argument vector. */ |
3218 |
|
|
argp = make_argp( hc ); |
3219 |
|
|
|
3220 |
|
|
/* Set up stdin. For POSTs we may have to set up a pipe from an |
3221 |
|
|
** interposer process, depending on if we've read some of the data |
3222 |
|
|
** into our buffer. |
3223 |
|
|
*/ |
3224 |
|
|
if ( hc->method == METHOD_POST && hc->read_idx > hc->checked_idx ) |
3225 |
|
|
{ |
3226 |
|
|
int p[2]; |
3227 |
|
|
|
3228 |
|
|
if ( pipe( p ) < 0 ) |
3229 |
|
|
{ |
3230 |
|
|
syslog( LOG_ERR, "pipe - %m" ); |
3231 |
|
|
httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl ); |
3232 |
|
|
exit( 1 ); |
3233 |
|
|
} |
3234 |
|
|
r = fork( ); |
3235 |
|
|
if ( r < 0 ) |
3236 |
|
|
{ |
3237 |
|
|
syslog( LOG_ERR, "fork - %m" ); |
3238 |
|
|
httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl ); |
3239 |
|
|
exit( 1 ); |
3240 |
|
|
} |
3241 |
|
|
if ( r == 0 ) |
3242 |
|
|
{ |
3243 |
|
|
/* Interposer process. */ |
3244 |
|
|
(void) close( p[0] ); |
3245 |
|
|
cgi_interpose_input( hc, p[1] ); |
3246 |
|
|
exit( 0 ); |
3247 |
|
|
} |
3248 |
|
|
(void) close( p[1] ); |
3249 |
|
|
(void) dup2( p[0], STDIN_FILENO ); |
3250 |
|
|
(void) close( p[0] ); |
3251 |
|
|
} |
3252 |
|
|
else |
3253 |
|
|
{ |
3254 |
|
|
/* Otherwise, the request socket is stdin. */ |
3255 |
|
|
(void) dup2( hc->conn_fd, STDIN_FILENO ); |
3256 |
|
|
} |
3257 |
|
|
|
3258 |
|
|
/* Set up stdout/stderr. If we're doing CGI header parsing, |
3259 |
|
|
** we need an output interposer too. |
3260 |
|
|
*/ |
3261 |
|
|
if ( strncmp( argp[0], "nph-", 4 ) != 0 && hc->mime_flag ) |
3262 |
|
|
{ |
3263 |
|
|
int p[2]; |
3264 |
|
|
|
3265 |
|
|
if ( pipe( p ) < 0 ) |
3266 |
|
|
{ |
3267 |
|
|
syslog( LOG_ERR, "pipe - %m" ); |
3268 |
|
|
httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl ); |
3269 |
|
|
exit( 1 ); |
3270 |
|
|
} |
3271 |
|
|
r = fork( ); |
3272 |
|
|
if ( r < 0 ) |
3273 |
|
|
{ |
3274 |
|
|
syslog( LOG_ERR, "fork - %m" ); |
3275 |
|
|
httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl ); |
3276 |
|
|
exit( 1 ); |
3277 |
|
|
} |
3278 |
|
|
if ( r == 0 ) |
3279 |
|
|
{ |
3280 |
|
|
/* Interposer process. */ |
3281 |
|
|
(void) close( p[1] ); |
3282 |
|
|
cgi_interpose_output( hc, p[0] ); |
3283 |
|
|
exit( 0 ); |
3284 |
|
|
} |
3285 |
|
|
(void) close( p[0] ); |
3286 |
|
|
(void) dup2( p[1], STDOUT_FILENO ); |
3287 |
|
|
(void) dup2( p[1], STDERR_FILENO ); |
3288 |
|
|
(void) close( p[1] ); |
3289 |
|
|
} |
3290 |
|
|
else |
3291 |
|
|
{ |
3292 |
|
|
/* Otherwise, the request socket is stdout/stderr. */ |
3293 |
|
|
(void) dup2( hc->conn_fd, STDOUT_FILENO ); |
3294 |
|
|
(void) dup2( hc->conn_fd, STDERR_FILENO ); |
3295 |
|
|
} |
3296 |
|
|
|
3297 |
|
|
/* At this point we would like to set close-on-exec again for hc->conn_fd |
3298 |
|
|
** (see previous comments on Linux's broken behavior re: close-on-exec |
3299 |
|
|
** and dup.) Unfortunately there seems to be another Linux problem, or |
3300 |
|
|
** perhaps a different aspect of the same problem - if we do this |
3301 |
|
|
** close-on-exec in Linux, the socket stays open but stderr gets |
3302 |
|
|
** closed - the last fd duped from the socket. What a mess. So we'll |
3303 |
|
|
** just leave the socket as is, which under other OSs means an extra |
3304 |
|
|
** file descriptor gets passed to the child process. Since the child |
3305 |
|
|
** probably already has that file open via stdin stdout and/or stderr, |
3306 |
|
|
** this is not a problem. |
3307 |
|
|
*/ |
3308 |
|
|
/* (void) fcntl( hc->conn_fd, F_SETFD, 1 ); */ |
3309 |
|
|
|
3310 |
|
|
#ifdef CGI_NICE |
3311 |
|
|
/* Set priority. */ |
3312 |
|
|
(void) nice( CGI_NICE ); |
3313 |
|
|
#endif /* CGI_NICE */ |
3314 |
|
|
|
3315 |
|
|
/* Split the program into directory and binary, so we can chdir() |
3316 |
|
|
** to the program's own directory. This isn't in the CGI 1.1 |
3317 |
|
|
** spec, but it's what other HTTP servers do. |
3318 |
|
|
*/ |
3319 |
|
|
directory = strdup( hc->expnfilename ); |
3320 |
|
|
if ( directory == (char*) 0 ) |
3321 |
|
|
binary = hc->expnfilename; /* ignore errors */ |
3322 |
|
|
else |
3323 |
|
|
{ |
3324 |
|
|
binary = strrchr( directory, '/' ); |
3325 |
|
|
if ( binary == (char*) 0 ) |
3326 |
|
|
binary = hc->expnfilename; |
3327 |
|
|
else |
3328 |
|
|
{ |
3329 |
|
|
*binary++ = '\0'; |
3330 |
|
|
(void) chdir( directory ); /* ignore errors */ |
3331 |
|
|
} |
3332 |
|
|
} |
3333 |
|
|
|
3334 |
|
|
/* Default behavior for SIGPIPE. */ |
3335 |
|
|
(void) signal( SIGPIPE, SIG_DFL ); |
3336 |
|
|
|
3337 |
|
|
/* Run the program. */ |
3338 |
|
|
(void) execve( binary, argp, envp ); |
3339 |
|
|
|
3340 |
|
|
/* Something went wrong. */ |
3341 |
|
|
syslog( LOG_ERR, "execve %.80s - %m", hc->expnfilename ); |
3342 |
|
|
httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl ); |
3343 |
|
|
exit( 1 ); |
3344 |
|
|
} |
3345 |
|
|
|
3346 |
|
|
|
3347 |
|
|
static off_t |
3348 |
|
|
cgi( httpd_conn* hc ) |
3349 |
|
|
{ |
3350 |
|
|
int r; |
3351 |
|
|
ClientData client_data; |
3352 |
|
|
|
3353 |
|
|
if ( hc->method == METHOD_GET || hc->method == METHOD_POST ) |
3354 |
|
|
{ |
3355 |
|
|
httpd_clear_ndelay( hc->conn_fd ); |
3356 |
|
|
r = fork( ); |
3357 |
|
|
if ( r < 0 ) |
3358 |
|
|
{ |
3359 |
|
|
syslog( LOG_ERR, "fork - %m" ); |
3360 |
|
|
httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl ); |
3361 |
|
|
return -1; |
3362 |
|
|
} |
3363 |
|
|
if ( r == 0 ) |
3364 |
|
|
{ |
3365 |
|
|
unlisten( hc->hs ); |
3366 |
|
|
cgi_child( hc ); |
3367 |
|
|
} |
3368 |
|
|
|
3369 |
|
|
/* Parent process. */ |
3370 |
|
|
syslog( LOG_INFO, "spawned CGI process %d for file '%.200s'", r, hc->expnfilename ); |
3371 |
|
|
#ifdef CGI_TIMELIMIT |
3372 |
|
|
/* Schedule a kill for the child process, in case it runs too long */ |
3373 |
|
|
client_data.i = r; |
3374 |
|
|
if ( tmr_create( (struct timeval*) 0, cgi_kill, client_data, CGI_TIMELIMIT * 1000L, 0 ) == (Timer*) 0 ) |
3375 |
|
|
{ |
3376 |
|
|
syslog( LOG_CRIT, "tmr_create(cgi_kill) failed" ); |
3377 |
|
|
exit( 1 ); |
3378 |
|
|
} |
3379 |
|
|
#endif /* CGI_TIMELIMIT */ |
3380 |
|
|
hc->status = 200; |
3381 |
|
|
hc->bytes_sent = CGI_BYTECOUNT; |
3382 |
|
|
hc->should_linger = 0; |
3383 |
|
|
} |
3384 |
|
|
else |
3385 |
|
|
{ |
3386 |
|
|
httpd_send_err( |
3387 |
|
|
hc, 501, err501title, "", err501form, httpd_method_str( hc->method ) ); |
3388 |
|
|
return -1; |
3389 |
|
|
} |
3390 |
|
|
|
3391 |
|
|
return 0; |
3392 |
|
|
} |
3393 |
|
|
|
3394 |
|
|
|
3395 |
|
|
static int |
3396 |
|
|
really_start_request( httpd_conn* hc, struct timeval* nowP ) |
3397 |
|
|
{ |
3398 |
|
|
static char* indexname; |
3399 |
|
|
static int maxindexname = 0; |
3400 |
|
|
static const char* index_names[] = { INDEX_NAMES }; |
3401 |
|
|
int i; |
3402 |
|
|
#ifdef AUTH_FILE |
3403 |
|
|
static char* dirname; |
3404 |
|
|
static int maxdirname = 0; |
3405 |
|
|
#endif /* AUTH_FILE */ |
3406 |
|
|
int expnlen, indxlen; |
3407 |
|
|
char* cp; |
3408 |
|
|
char* pi; |
3409 |
|
|
|
3410 |
|
|
expnlen = strlen( hc->expnfilename ); |
3411 |
|
|
|
3412 |
|
|
if ( hc->method != METHOD_GET && hc->method != METHOD_HEAD && |
3413 |
|
|
hc->method != METHOD_POST ) |
3414 |
|
|
{ |
3415 |
|
|
httpd_send_err( |
3416 |
|
|
hc, 501, err501title, "", err501form, httpd_method_str( hc->method ) ); |
3417 |
|
|
return -1; |
3418 |
|
|
} |
3419 |
|
|
|
3420 |
|
|
/* Stat the file. */ |
3421 |
|
|
if ( stat( hc->expnfilename, &hc->sb ) < 0 ) |
3422 |
|
|
{ |
3423 |
|
|
httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl ); |
3424 |
|
|
return -1; |
3425 |
|
|
} |
3426 |
|
|
|
3427 |
|
|
/* Is it world-readable or world-executable? We check explicitly instead |
3428 |
|
|
** of just trying to open it, so that no one ever gets surprised by |
3429 |
|
|
** a file that's not set world-readable and yet somehow is |
3430 |
|
|
** readable by the HTTP server and therefore the *whole* world. |
3431 |
|
|
*/ |
3432 |
|
|
if ( ! ( hc->sb.st_mode & ( S_IROTH | S_IXOTH ) ) ) |
3433 |
|
|
{ |
3434 |
|
|
syslog( |
3435 |
|
|
LOG_INFO, |
3436 |
|
|
"%.80s URL \"%.80s\" resolves to a non world-readable file", |
3437 |
|
|
httpd_ntoa( &hc->client_addr ), hc->encodedurl ); |
3438 |
|
|
httpd_send_err( |
3439 |
|
|
hc, 403, err403title, "", |
3440 |
|
|
ERROR_FORM( err403form, "The requested URL '%.80s' resolves to a file that is not world-readable.\n" ), |
3441 |
|
|
hc->encodedurl ); |
3442 |
|
|
return -1; |
3443 |
|
|
} |
3444 |
|
|
|
3445 |
|
|
/* Is it a directory? */ |
3446 |
|
|
if ( S_ISDIR(hc->sb.st_mode) ) |
3447 |
|
|
{ |
3448 |
|
|
/* If there's pathinfo, it's just a non-existent file. */ |
3449 |
|
|
if ( hc->pathinfo[0] != '\0' ) |
3450 |
|
|
{ |
3451 |
|
|
httpd_send_err( hc, 404, err404title, "", err404form, hc->encodedurl ); |
3452 |
|
|
return -1; |
3453 |
|
|
} |
3454 |
|
|
|
3455 |
|
|
/* Special handling for directory URLs that don't end in a slash. |
3456 |
|
|
** We send back an explicit redirect with the slash, because |
3457 |
|
|
** otherwise many clients can't build relative URLs properly. |
3458 |
|
|
*/ |
3459 |
|
|
if ( hc->decodedurl[strlen( hc->decodedurl ) - 1] != '/' ) |
3460 |
|
|
{ |
3461 |
|
|
send_dirredirect( hc ); |
3462 |
|
|
return -1; |
3463 |
|
|
} |
3464 |
|
|
|
3465 |
|
|
/* Check for an index file. */ |
3466 |
|
|
for ( i = 0; i < sizeof(index_names) / sizeof(char*); ++i ) |
3467 |
|
|
{ |
3468 |
|
|
httpd_realloc_str( |
3469 |
|
|
&indexname, &maxindexname, |
3470 |
|
|
expnlen + 1 + strlen( index_names[i] ) ); |
3471 |
|
|
(void) strcpy( indexname, hc->expnfilename ); |
3472 |
|
|
indxlen = strlen( indexname ); |
3473 |
|
|
if ( indxlen == 0 || indexname[indxlen - 1] != '/' ) |
3474 |
|
|
(void) strcat( indexname, "/" ); |
3475 |
|
|
if ( strcmp( indexname, "./" ) == 0 ) |
3476 |
|
|
indexname[0] = '\0'; |
3477 |
|
|
(void) strcat( indexname, index_names[i] ); |
3478 |
|
|
if ( stat( indexname, &hc->sb ) >= 0 ) |
3479 |
|
|
goto got_one; |
3480 |
|
|
} |
3481 |
|
|
|
3482 |
|
|
/* Nope, no index file, so it's an actual directory request. */ |
3483 |
|
|
#ifdef GENERATE_INDEXES |
3484 |
|
|
/* Directories must be readable for indexing. */ |
3485 |
|
|
if ( ! ( hc->sb.st_mode & S_IROTH ) ) |
3486 |
|
|
{ |
3487 |
|
|
syslog( |
3488 |
|
|
LOG_INFO, |
3489 |
|
|
"%.80s URL \"%.80s\" tried to index a directory with indexing disabled", |
3490 |
|
|
httpd_ntoa( &hc->client_addr ), hc->encodedurl ); |
3491 |
|
|
httpd_send_err( |
3492 |
|
|
hc, 403, err403title, "", |
3493 |
|
|
ERROR_FORM( err403form, "The requested URL '%.80s' resolves to a directory that has indexing disabled.\n" ), |
3494 |
|
|
hc->encodedurl ); |
3495 |
|
|
return -1; |
3496 |
|
|
} |
3497 |
|
|
#ifdef AUTH_FILE |
3498 |
|
|
/* Check authorization for this directory. */ |
3499 |
|
|
if ( auth_check( hc, hc->expnfilename ) == -1 ) |
3500 |
|
|
return -1; |
3501 |
|
|
#endif /* AUTH_FILE */ |
3502 |
|
|
/* Referer check. */ |
3503 |
|
|
if ( ! check_referer( hc ) ) |
3504 |
|
|
return -1; |
3505 |
|
|
/* Ok, generate an index. */ |
3506 |
|
|
return ls( hc ); |
3507 |
|
|
#else /* GENERATE_INDEXES */ |
3508 |
|
|
syslog( |
3509 |
|
|
LOG_INFO, "%.80s URL \"%.80s\" tried to index a directory", |
3510 |
|
|
httpd_ntoa( &hc->client_addr ), hc->encodedurl ); |
3511 |
|
|
httpd_send_err( |
3512 |
|
|
hc, 403, err403title, "", |
3513 |
|
|
ERROR_FORM( err403form, "The requested URL '%.80s' is a directory, and directory indexing is disabled on this server.\n" ), |
3514 |
|
|
hc->encodedurl ); |
3515 |
|
|
return -1; |
3516 |
|
|
#endif /* GENERATE_INDEXES */ |
3517 |
|
|
|
3518 |
|
|
got_one: ; |
3519 |
|
|
/* Got an index file. Expand symlinks again. More pathinfo means |
3520 |
|
|
** something went wrong. |
3521 |
|
|
*/ |
3522 |
|
|
cp = expand_symlinks( indexname, &pi, hc->hs->no_symlink, hc->tildemapped ); |
3523 |
|
|
if ( cp == (char*) 0 || pi[0] != '\0' ) |
3524 |
|
|
{ |
3525 |
|
|
httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl ); |
3526 |
|
|
return -1; |
3527 |
|
|
} |
3528 |
|
|
expnlen = strlen( cp ); |
3529 |
|
|
httpd_realloc_str( &hc->expnfilename, &hc->maxexpnfilename, expnlen ); |
3530 |
|
|
(void) strcpy( hc->expnfilename, cp ); |
3531 |
|
|
|
3532 |
|
|
/* Now, is the index version world-readable or world-executable? */ |
3533 |
|
|
if ( ! ( hc->sb.st_mode & ( S_IROTH | S_IXOTH ) ) ) |
3534 |
|
|
{ |
3535 |
|
|
syslog( |
3536 |
|
|
LOG_INFO, |
3537 |
|
|
"%.80s URL \"%.80s\" resolves to a non-world-readable index file", |
3538 |
|
|
httpd_ntoa( &hc->client_addr ), hc->encodedurl ); |
3539 |
|
|
httpd_send_err( |
3540 |
|
|
hc, 403, err403title, "", |
3541 |
|
|
ERROR_FORM( err403form, "The requested URL '%.80s' resolves to an index file that is not world-readable.\n" ), |
3542 |
|
|
hc->encodedurl ); |
3543 |
|
|
return -1; |
3544 |
|
|
} |
3545 |
|
|
} |
3546 |
|
|
|
3547 |
|
|
#ifdef AUTH_FILE |
3548 |
|
|
/* Check authorization for this directory. */ |
3549 |
|
|
httpd_realloc_str( &dirname, &maxdirname, expnlen ); |
3550 |
|
|
(void) strcpy( dirname, hc->expnfilename ); |
3551 |
|
|
cp = strrchr( dirname, '/' ); |
3552 |
|
|
if ( cp == (char*) 0 ) |
3553 |
|
|
(void) strcpy( dirname, "." ); |
3554 |
|
|
else |
3555 |
|
|
*cp = '\0'; |
3556 |
|
|
if ( auth_check( hc, dirname ) == -1 ) |
3557 |
|
|
return -1; |
3558 |
|
|
|
3559 |
|
|
/* Check if the filename is the AUTH_FILE itself - that's verboten. */ |
3560 |
|
|
if ( expnlen == sizeof(AUTH_FILE) - 1 ) |
3561 |
|
|
{ |
3562 |
|
|
if ( strcmp( hc->expnfilename, AUTH_FILE ) == 0 ) |
3563 |
|
|
{ |
3564 |
|
|
syslog( |
3565 |
|
|
LOG_NOTICE, |
3566 |
|
|
"%.80s URL \"%.80s\" tried to retrieve an auth file", |
3567 |
|
|
httpd_ntoa( &hc->client_addr ), hc->encodedurl ); |
3568 |
|
|
httpd_send_err( |
3569 |
|
|
hc, 403, err403title, "", |
3570 |
|
|
ERROR_FORM( err403form, "The requested URL '%.80s' is an authorization file, retrieving it is not permitted.\n" ), |
3571 |
|
|
hc->encodedurl ); |
3572 |
|
|
return -1; |
3573 |
|
|
} |
3574 |
|
|
} |
3575 |
|
|
else if ( expnlen >= sizeof(AUTH_FILE) && |
3576 |
|
|
strcmp( &(hc->expnfilename[expnlen - sizeof(AUTH_FILE) + 1]), AUTH_FILE ) == 0 && |
3577 |
|
|
hc->expnfilename[expnlen - sizeof(AUTH_FILE)] == '/' ) |
3578 |
|
|
{ |
3579 |
|
|
syslog( |
3580 |
|
|
LOG_NOTICE, |
3581 |
|
|
"%.80s URL \"%.80s\" tried to retrieve an auth file", |
3582 |
|
|
httpd_ntoa( &hc->client_addr ), hc->encodedurl ); |
3583 |
|
|
httpd_send_err( |
3584 |
|
|
hc, 403, err403title, "", |
3585 |
|
|
ERROR_FORM( err403form, "The requested URL '%.80s' is an authorization file, retrieving it is not permitted.\n" ), |
3586 |
|
|
hc->encodedurl ); |
3587 |
|
|
return -1; |
3588 |
|
|
} |
3589 |
|
|
#endif /* AUTH_FILE */ |
3590 |
|
|
|
3591 |
|
|
/* Referer check. */ |
3592 |
|
|
if ( ! check_referer( hc ) ) |
3593 |
|
|
return -1; |
3594 |
|
|
|
3595 |
|
|
/* Is it world-executable and in the CGI area? */ |
3596 |
|
|
if ( hc->hs->cgi_pattern != (char*) 0 && |
3597 |
|
|
( hc->sb.st_mode & S_IXOTH ) && |
3598 |
|
|
match( hc->hs->cgi_pattern, hc->expnfilename ) ) |
3599 |
|
|
return cgi( hc ); |
3600 |
|
|
|
3601 |
|
|
/* It's not CGI. If it's executable or there's pathinfo, someone's |
3602 |
|
|
** trying to either serve or run a non-CGI file as CGI. Either case |
3603 |
|
|
** is prohibited. |
3604 |
|
|
*/ |
3605 |
|
|
if ( hc->sb.st_mode & S_IXOTH ) |
3606 |
|
|
{ |
3607 |
|
|
syslog( |
3608 |
|
|
LOG_NOTICE, "%.80s URL \"%.80s\" is executable but isn't CGI", |
3609 |
|
|
httpd_ntoa( &hc->client_addr ), hc->encodedurl ); |
3610 |
|
|
httpd_send_err( |
3611 |
|
|
hc, 403, err403title, "", |
3612 |
|
|
ERROR_FORM( err403form, "The requested URL '%.80s' resolves to a file which is marked executable but is not a CGI file; retrieving it is forbidden.\n" ), |
3613 |
|
|
hc->encodedurl ); |
3614 |
|
|
return -1; |
3615 |
|
|
} |
3616 |
|
|
if ( hc->pathinfo[0] != '\0' ) |
3617 |
|
|
{ |
3618 |
|
|
syslog( |
3619 |
|
|
LOG_INFO, "%.80s URL \"%.80s\" has pathinfo but isn't CGI", |
3620 |
|
|
httpd_ntoa( &hc->client_addr ), hc->encodedurl ); |
3621 |
|
|
httpd_send_err( |
3622 |
|
|
hc, 403, err403title, "", |
3623 |
|
|
ERROR_FORM( err403form, "The requested URL '%.80s' resolves to a file plus CGI-style pathinfo, but the file is not a valid CGI file.\n" ), |
3624 |
|
|
hc->encodedurl ); |
3625 |
|
|
return -1; |
3626 |
|
|
} |
3627 |
|
|
|
3628 |
|
|
/* Fill in end_byte_loc, if necessary. */ |
3629 |
|
|
if ( hc->got_range && |
3630 |
|
|
( hc->end_byte_loc == -1 || hc->end_byte_loc >= hc->sb.st_size ) ) |
3631 |
|
|
hc->end_byte_loc = hc->sb.st_size - 1; |
3632 |
|
|
|
3633 |
|
|
figure_mime( hc ); |
3634 |
|
|
|
3635 |
|
|
if ( hc->method == METHOD_HEAD ) |
3636 |
|
|
{ |
3637 |
|
|
send_mime( |
3638 |
|
|
hc, 200, ok200title, hc->encodings, "", hc->type, hc->sb.st_size, |
3639 |
|
|
hc->sb.st_mtime ); |
3640 |
|
|
} |
3641 |
|
|
else if ( hc->if_modified_since != (time_t) -1 && |
3642 |
|
|
hc->if_modified_since >= hc->sb.st_mtime ) |
3643 |
|
|
{ |
3644 |
|
|
hc->method = METHOD_HEAD; |
3645 |
|
|
send_mime( |
3646 |
|
|
hc, 304, err304title, hc->encodings, "", hc->type, hc->sb.st_size, |
3647 |
|
|
hc->sb.st_mtime ); |
3648 |
|
|
} |
3649 |
|
|
else |
3650 |
|
|
{ |
3651 |
|
|
hc->file_address = mmc_map( hc->expnfilename, &(hc->sb), nowP ); |
3652 |
|
|
if ( hc->file_address == (char*) 0 ) |
3653 |
|
|
{ |
3654 |
|
|
httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl ); |
3655 |
|
|
return -1; |
3656 |
|
|
} |
3657 |
|
|
send_mime( |
3658 |
|
|
hc, 200, ok200title, hc->encodings, "", hc->type, hc->sb.st_size, |
3659 |
|
|
hc->sb.st_mtime ); |
3660 |
|
|
} |
3661 |
|
|
|
3662 |
|
|
return 0; |
3663 |
|
|
} |
3664 |
|
|
|
3665 |
|
|
|
3666 |
|
|
int |
3667 |
|
|
httpd_start_request( httpd_conn* hc, struct timeval* nowP ) |
3668 |
|
|
{ |
3669 |
|
|
int r; |
3670 |
|
|
|
3671 |
|
|
/* Really start the request. */ |
3672 |
|
|
r = really_start_request( hc, nowP ); |
3673 |
|
|
|
3674 |
|
|
/* And return the status. */ |
3675 |
|
|
return r; |
3676 |
|
|
} |
3677 |
|
|
|
3678 |
|
|
|
3679 |
|
|
static void |
3680 |
|
|
make_log_entry( httpd_conn* hc, struct timeval* nowP ) |
3681 |
|
|
{ |
3682 |
|
|
char* ru; |
3683 |
|
|
char url[305]; |
3684 |
|
|
char bytes[40]; |
3685 |
|
|
|
3686 |
|
|
if ( hc->hs->no_log ) |
3687 |
|
|
return; |
3688 |
|
|
|
3689 |
root |
1.1.4.3 |
/* don't log UNKNOWN protocol requests (blocks etc..) */ |
3690 |
|
|
if ( !strcmp (hc->protocol, "UNKNOWN") ) |
3691 |
|
|
return; |
3692 |
|
|
|
3693 |
root |
1.1 |
/* This is straight CERN Combined Log Format - the only tweak |
3694 |
|
|
** being that if we're using syslog() we leave out the date, because |
3695 |
|
|
** syslogd puts it in. The included syslogtocern script turns the |
3696 |
|
|
** results into true CERN format. |
3697 |
|
|
*/ |
3698 |
|
|
|
3699 |
|
|
/* Format remote user. */ |
3700 |
|
|
if ( hc->remoteuser[0] != '\0' ) |
3701 |
|
|
ru = hc->remoteuser; |
3702 |
|
|
else |
3703 |
|
|
ru = "-"; |
3704 |
|
|
/* If we're vhosting, prepend the hostname to the url. This is |
3705 |
|
|
** a little weird, perhaps writing separate log files for |
3706 |
|
|
** each vhost would make more sense. |
3707 |
|
|
*/ |
3708 |
|
|
if ( hc->hs->vhost && ! hc->tildemapped ) |
3709 |
|
|
(void) my_snprintf( url, sizeof(url), |
3710 |
|
|
"/%.100s%.200s", |
3711 |
|
|
hc->hostname == (char*) 0 ? hc->hs->server_hostname : hc->hostname, |
3712 |
|
|
hc->encodedurl ); |
3713 |
|
|
else |
3714 |
|
|
(void) my_snprintf( url, sizeof(url), |
3715 |
|
|
"%.200s", hc->encodedurl ); |
3716 |
|
|
/* Format the bytes. */ |
3717 |
|
|
if ( (long) hc->bytes_sent >= 0 ) |
3718 |
|
|
(void) my_snprintf( bytes, sizeof(bytes), |
3719 |
|
|
"%ld", (long) hc->bytes_sent ); |
3720 |
|
|
else |
3721 |
|
|
(void) strcpy( bytes, "-" ); |
3722 |
|
|
|
3723 |
|
|
/* Logfile or syslog? */ |
3724 |
|
|
if ( hc->hs->logfp != (FILE*) 0 ) |
3725 |
|
|
{ |
3726 |
|
|
time_t now; |
3727 |
|
|
struct tm* t; |
3728 |
|
|
const char* cernfmt_nozone = "%d/%b/%Y:%H:%M:%S"; |
3729 |
|
|
char date_nozone[100]; |
3730 |
|
|
int zone; |
3731 |
|
|
char sign; |
3732 |
|
|
char date[100]; |
3733 |
|
|
|
3734 |
|
|
/* Get the current time, if necessary. */ |
3735 |
|
|
if ( nowP != (struct timeval*) 0 ) |
3736 |
|
|
now = nowP->tv_sec; |
3737 |
|
|
else |
3738 |
|
|
now = time( (time_t*) 0 ); |
3739 |
|
|
/* Format the time, forcing a numeric timezone (some log analyzers |
3740 |
|
|
** are stoooopid about this). |
3741 |
|
|
*/ |
3742 |
|
|
t = localtime( &now ); |
3743 |
|
|
(void) strftime( date_nozone, sizeof(date_nozone), cernfmt_nozone, t ); |
3744 |
|
|
#ifdef HAVE_TM_GMTOFF |
3745 |
|
|
zone = t->tm_gmtoff / 60L; |
3746 |
|
|
#else |
3747 |
|
|
zone = -timezone / 60L; |
3748 |
|
|
/* Probably have to add something about daylight time here. */ |
3749 |
|
|
#endif |
3750 |
|
|
if ( zone >= 0 ) |
3751 |
|
|
sign = '+'; |
3752 |
|
|
else |
3753 |
|
|
{ |
3754 |
|
|
sign = '-'; |
3755 |
|
|
zone = -zone; |
3756 |
|
|
} |
3757 |
|
|
zone = ( zone / 60 ) * 100 + zone % 60; |
3758 |
|
|
(void) my_snprintf( date, sizeof(date), |
3759 |
|
|
"%s %c%04d", date_nozone, sign, zone ); |
3760 |
|
|
/* And write the log entry. */ |
3761 |
|
|
(void) fprintf( hc->hs->logfp, |
3762 |
|
|
"%.80s - %.80s [%s] \"%.80s %.300s %.80s\" %d %s \"%.200s\" \"%.80s\"\n", |
3763 |
|
|
httpd_ntoa( &hc->client_addr ), ru, date, |
3764 |
|
|
httpd_method_str( hc->method ), url, hc->protocol, |
3765 |
|
|
hc->status, bytes, hc->referer, hc->useragent ); |
3766 |
|
|
(void) fflush( hc->hs->logfp ); /* don't need to flush every time */ |
3767 |
|
|
} |
3768 |
|
|
else |
3769 |
|
|
syslog( LOG_INFO, |
3770 |
|
|
"%.80s - %.80s \"%.80s %.200s %.80s\" %d %s \"%.200s\" \"%.80s\"", |
3771 |
|
|
httpd_ntoa( &hc->client_addr ), ru, |
3772 |
|
|
httpd_method_str( hc->method ), url, hc->protocol, |
3773 |
|
|
hc->status, bytes, hc->referer, hc->useragent ); |
3774 |
|
|
} |
3775 |
|
|
|
3776 |
|
|
|
3777 |
|
|
/* Returns 1 if ok to serve the url, 0 if not. */ |
3778 |
|
|
static int |
3779 |
|
|
check_referer( httpd_conn* hc ) |
3780 |
|
|
{ |
3781 |
|
|
int r; |
3782 |
|
|
|
3783 |
|
|
/* Are we doing referer checking at all? */ |
3784 |
|
|
if ( hc->hs->url_pattern == (char*) 0 ) |
3785 |
|
|
return 1; |
3786 |
|
|
|
3787 |
|
|
r = really_check_referer( hc ); |
3788 |
|
|
|
3789 |
|
|
if ( ! r ) |
3790 |
|
|
{ |
3791 |
|
|
syslog( |
3792 |
|
|
LOG_INFO, "%.80s non-local referer \"%.80s\" \"%.80s\"", |
3793 |
|
|
httpd_ntoa( &hc->client_addr ), hc->encodedurl, |
3794 |
|
|
hc->referer ); |
3795 |
|
|
httpd_send_err( |
3796 |
|
|
hc, 403, err403title, "", |
3797 |
|
|
ERROR_FORM( err403form, "You must supply a local referer to get URL '%.80s' from this server.\n" ), |
3798 |
|
|
hc->encodedurl ); |
3799 |
|
|
} |
3800 |
|
|
return r; |
3801 |
|
|
} |
3802 |
|
|
|
3803 |
|
|
|
3804 |
|
|
/* Returns 1 if ok to serve the url, 0 if not. */ |
3805 |
|
|
static int |
3806 |
|
|
really_check_referer( httpd_conn* hc ) |
3807 |
|
|
{ |
3808 |
|
|
httpd_server* hs; |
3809 |
|
|
char* cp1; |
3810 |
|
|
char* cp2; |
3811 |
|
|
char* cp3; |
3812 |
|
|
static char* refhost = (char*) 0; |
3813 |
|
|
static int refhost_size = 0; |
3814 |
|
|
char *lp; |
3815 |
|
|
|
3816 |
|
|
hs = hc->hs; |
3817 |
|
|
|
3818 |
|
|
/* Check for an empty referer. */ |
3819 |
|
|
if ( hc->referer == (char*) 0 || hc->referer[0] == '\0' || |
3820 |
|
|
( cp1 = strstr( hc->referer, "//" ) ) == (char*) 0 ) |
3821 |
|
|
{ |
3822 |
|
|
/* Disallow if we require a referer and the url matches. */ |
3823 |
|
|
if ( hs->no_empty_referers && match( hs->url_pattern, hc->decodedurl ) ) |
3824 |
|
|
return 0; |
3825 |
|
|
/* Otherwise ok. */ |
3826 |
|
|
return 1; |
3827 |
|
|
} |
3828 |
|
|
|
3829 |
|
|
/* Extract referer host. */ |
3830 |
|
|
cp1 += 2; |
3831 |
|
|
for ( cp2 = cp1; *cp2 != '/' && *cp2 != ':' && *cp2 != '\0'; ++cp2 ) |
3832 |
|
|
continue; |
3833 |
|
|
httpd_realloc_str( &refhost, &refhost_size, cp2 - cp1 ); |
3834 |
|
|
for ( cp3 = refhost; cp1 < cp2; ++cp1, ++cp3 ) |
3835 |
|
|
if ( isupper(*cp1) ) |
3836 |
|
|
*cp3 = tolower(*cp1); |
3837 |
|
|
else |
3838 |
|
|
*cp3 = *cp1; |
3839 |
|
|
*cp3 = '\0'; |
3840 |
|
|
|
3841 |
|
|
/* Local pattern? */ |
3842 |
|
|
if ( hs->local_pattern != (char*) 0 ) |
3843 |
|
|
lp = hs->local_pattern; |
3844 |
|
|
else |
3845 |
|
|
{ |
3846 |
|
|
/* No local pattern. What's our hostname? */ |
3847 |
|
|
if ( ! hs->vhost ) |
3848 |
|
|
{ |
3849 |
|
|
/* Not vhosting, use the server name. */ |
3850 |
|
|
lp = hs->server_hostname; |
3851 |
|
|
if ( lp == (char*) 0 ) |
3852 |
|
|
/* Couldn't figure out local hostname - give up. */ |
3853 |
|
|
return 1; |
3854 |
|
|
} |
3855 |
|
|
else |
3856 |
|
|
{ |
3857 |
|
|
/* We are vhosting, use the hostname on this connection. */ |
3858 |
|
|
lp = hc->hostname; |
3859 |
|
|
if ( lp == (char*) 0 ) |
3860 |
|
|
/* Oops, no hostname. Maybe it's an old browser that |
3861 |
|
|
** doesn't send a Host: header. We could figure out |
3862 |
|
|
** the default hostname for this IP address, but it's |
3863 |
|
|
** not worth it for the few requests like this. |
3864 |
|
|
*/ |
3865 |
|
|
return 1; |
3866 |
|
|
} |
3867 |
|
|
} |
3868 |
|
|
|
3869 |
|
|
/* If the referer host doesn't match the local host pattern, and |
3870 |
|
|
** the URL does match the url pattern, it's an illegal reference. |
3871 |
|
|
*/ |
3872 |
|
|
if ( ! match( lp, refhost ) && match( hs->url_pattern, hc->decodedurl ) ) |
3873 |
|
|
return 0; |
3874 |
|
|
/* Otherwise ok. */ |
3875 |
|
|
return 1; |
3876 |
|
|
} |
3877 |
|
|
|
3878 |
|
|
|
3879 |
|
|
char* |
3880 |
|
|
httpd_ntoa( httpd_sockaddr* saP ) |
3881 |
|
|
{ |
3882 |
|
|
#ifdef HAVE_GETNAMEINFO |
3883 |
|
|
static char str[200]; |
3884 |
|
|
|
3885 |
|
|
if ( getnameinfo( &saP->sa, sockaddr_len( saP ), str, sizeof(str), 0, 0, NI_NUMERICHOST ) != 0 ) |
3886 |
|
|
{ |
3887 |
|
|
str[0] = '?'; |
3888 |
|
|
str[1] = '\0'; |
3889 |
|
|
} |
3890 |
|
|
return str; |
3891 |
|
|
|
3892 |
|
|
#else /* HAVE_GETNAMEINFO */ |
3893 |
|
|
|
3894 |
|
|
return inet_ntoa( saP->sa_in.sin_addr ); |
3895 |
|
|
|
3896 |
|
|
#endif /* HAVE_GETNAMEINFO */ |
3897 |
|
|
} |
3898 |
|
|
|
3899 |
|
|
|
3900 |
|
|
static int |
3901 |
|
|
sockaddr_check( httpd_sockaddr* saP ) |
3902 |
|
|
{ |
3903 |
|
|
switch ( saP->sa.sa_family ) |
3904 |
|
|
{ |
3905 |
|
|
case AF_INET: return 1; |
3906 |
|
|
#if defined(AF_INET6) && defined(HAVE_SOCKADDR_IN6) |
3907 |
|
|
case AF_INET6: return 1; |
3908 |
|
|
#endif /* AF_INET6 && HAVE_SOCKADDR_IN6 */ |
3909 |
|
|
default: |
3910 |
|
|
return 0; |
3911 |
|
|
} |
3912 |
|
|
} |
3913 |
|
|
|
3914 |
|
|
|
3915 |
|
|
static size_t |
3916 |
|
|
sockaddr_len( httpd_sockaddr* saP ) |
3917 |
|
|
{ |
3918 |
|
|
switch ( saP->sa.sa_family ) |
3919 |
|
|
{ |
3920 |
|
|
case AF_INET: return sizeof(struct sockaddr_in); |
3921 |
|
|
#if defined(AF_INET6) && defined(HAVE_SOCKADDR_IN6) |
3922 |
|
|
case AF_INET6: return sizeof(struct sockaddr_in6); |
3923 |
|
|
#endif /* AF_INET6 && HAVE_SOCKADDR_IN6 */ |
3924 |
|
|
default: |
3925 |
|
|
return 0; /* shouldn't happen */ |
3926 |
|
|
} |
3927 |
|
|
} |
3928 |
|
|
|
3929 |
|
|
|
3930 |
|
|
/* Some systems don't have snprintf(), so we make our own that uses |
3931 |
|
|
** either vsnprintf() or vsprintf(). If your system doesn't have |
3932 |
|
|
** vsnprintf(), it is probably vulnerable to buffer overruns. |
3933 |
|
|
** Upgrade! |
3934 |
|
|
*/ |
3935 |
|
|
static int |
3936 |
|
|
my_snprintf( char* str, size_t size, const char* format, ... ) |
3937 |
|
|
{ |
3938 |
|
|
va_list ap; |
3939 |
|
|
int r; |
3940 |
|
|
|
3941 |
|
|
va_start( ap, format ); |
3942 |
|
|
#ifdef HAVE_VSNPRINTF |
3943 |
|
|
r = vsnprintf( str, size, format, ap ); |
3944 |
|
|
#else /* HAVE_VSNPRINTF */ |
3945 |
|
|
r = vsprintf( str, format, ap ); |
3946 |
|
|
#endif /* HAVE_VSNPRINTF */ |
3947 |
|
|
va_end( ap ); |
3948 |
|
|
return r; |
3949 |
|
|
} |
3950 |
|
|
|
3951 |
|
|
|
3952 |
|
|
/* Generate debugging statistics syslog message. */ |
3953 |
|
|
void |
3954 |
|
|
httpd_logstats( long secs ) |
3955 |
|
|
{ |
3956 |
|
|
syslog( LOG_NOTICE, |
3957 |
|
|
" libhttpd - %d strings allocated, %ld bytes (%g bytes/str)", |
3958 |
|
|
str_alloc_count, str_alloc_size, |
3959 |
|
|
(float) str_alloc_size / str_alloc_count ); |
3960 |
|
|
} |