1 |
/* |
2 |
* Boa, an http server |
3 |
* Copyright (C) 1995 Paul Phillips <paulp@go2net.com> |
4 |
* Some changes Copyright (C) 1996 Charles F. Randall <crandall@goldsys.com> |
5 |
* Some changes Copyright (C) 1996 Larry Doolittle <ldoolitt@boa.org> |
6 |
* Some changes Copyright (C) 1996-2002 Jon Nelson <jnelson@boa.org> |
7 |
* Portions Copyright (C) 2002 Nikos Mavroyanopoulos <nmav@gnutls.org> |
8 |
* |
9 |
* This program is free software; you can redistribute it and/or modify |
10 |
* it under the terms of the GNU General Public License as published by |
11 |
* the Free Software Foundation; either version 1, or (at your option) |
12 |
* any later version. |
13 |
* |
14 |
* This program is distributed in the hope that it will be useful, |
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
* GNU General Public License for more details. |
18 |
* |
19 |
* You should have received a copy of the GNU General Public License |
20 |
* along with this program; if not, write to the Free Software |
21 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
22 |
* |
23 |
*/ |
24 |
|
25 |
/* $Id: boa.c,v 1.5 2002/09/25 10:33:19 nmav Exp $*/ |
26 |
|
27 |
#include "boa.h" |
28 |
#include "ssl.h" |
29 |
#include <sys/resource.h> |
30 |
#ifdef ENABLE_SMP |
31 |
# include <pthread.h> |
32 |
|
33 |
pthread_t father_id; |
34 |
|
35 |
#endif |
36 |
|
37 |
extern int ssl_params_refresh; |
38 |
|
39 |
/* globals */ |
40 |
int backlog = SO_MAXCONN; |
41 |
time_t start_time; |
42 |
|
43 |
time_t current_time; |
44 |
|
45 |
|
46 |
/* static to boa.c */ |
47 |
static void fixup_server_root(void); |
48 |
static socket_type create_server_socket( int port, int); |
49 |
static void drop_privs(void); |
50 |
static server_params *smp_init(socket_type server_s[2]); |
51 |
static int sock_opt = 1; |
52 |
static int do_fork = 1; |
53 |
int devnullfd = -1; |
54 |
|
55 |
int main(int argc, char **argv) |
56 |
{ |
57 |
int c; /* command line arg */ |
58 |
socket_type server_s[2] = {{ -1, 0, 0, 0}, { -1, -1, 0, 0 }}; /* boa socket */ |
59 |
server_params *params; |
60 |
|
61 |
/* set umask to u+rw, u-x, go-rwx */ |
62 |
c = umask(~0600); |
63 |
if (c == -1) { |
64 |
perror("umask"); |
65 |
exit(1); |
66 |
} |
67 |
|
68 |
devnullfd = open("/dev/null", 0); |
69 |
|
70 |
/* make STDIN and STDOUT point to /dev/null */ |
71 |
if (devnullfd == -1) { |
72 |
DIE("can't open /dev/null"); |
73 |
} |
74 |
|
75 |
if (dup2(devnullfd, STDIN_FILENO) == -1) { |
76 |
DIE("can't dup2 /dev/null to STDIN_FILENO"); |
77 |
} |
78 |
|
79 |
if (dup2(devnullfd, STDOUT_FILENO) == -1) { |
80 |
DIE("can't dup2 /dev/null to STDOUT_FILENO"); |
81 |
} |
82 |
|
83 |
/* but first, update timestamp, because log_error_time uses it */ |
84 |
(void) time(¤t_time); |
85 |
|
86 |
while ((c = getopt(argc, argv, "c:r:d")) != -1) { |
87 |
switch (c) { |
88 |
case 'c': |
89 |
if (server_root) |
90 |
free(server_root); |
91 |
server_root = strdup(optarg); |
92 |
if (!server_root) { |
93 |
perror("strdup (for server_root)"); |
94 |
exit(1); |
95 |
} |
96 |
break; |
97 |
case 'r': |
98 |
if (chdir(optarg) == -1) { |
99 |
log_error_time(); |
100 |
perror("chdir (to chroot)"); |
101 |
exit(1); |
102 |
} |
103 |
if (chroot(optarg) == -1) { |
104 |
log_error_time(); |
105 |
perror("chroot"); |
106 |
exit(1); |
107 |
} |
108 |
if (chdir("/") == -1) { |
109 |
log_error_time(); |
110 |
perror("chdir (after chroot)"); |
111 |
exit(1); |
112 |
} |
113 |
break; |
114 |
case 'd': |
115 |
do_fork = 0; |
116 |
break; |
117 |
default: |
118 |
fprintf(stderr, "Usage: %s [-c serverroot] [-r chroot] [-d]\n", argv[0]); |
119 |
exit(1); |
120 |
} |
121 |
} |
122 |
|
123 |
fixup_server_root(); |
124 |
read_config_files(); |
125 |
open_logs(); |
126 |
|
127 |
if ((boa_ssl >= 2 || boa_ssl == 0) && server_port > 0) { |
128 |
server_s[0] = create_server_socket( server_port, 0); |
129 |
} |
130 |
|
131 |
if (boa_ssl != 0 && ssl_port > 0) { |
132 |
server_s[1] = create_server_socket( ssl_port, 1); |
133 |
} |
134 |
|
135 |
if (server_s[1].socket == -1 && server_s[0].socket == -1) { |
136 |
log_error_time(); |
137 |
fprintf(stderr, "Could not initialize sockets\n"); |
138 |
exit(1); |
139 |
} |
140 |
|
141 |
init_signals(); |
142 |
drop_privs(); |
143 |
create_common_env(); |
144 |
build_needs_escape(); |
145 |
|
146 |
if (boa_ssl) { |
147 |
initialize_ssl(); |
148 |
} |
149 |
|
150 |
initialize_mmap(); |
151 |
|
152 |
if (max_connections < 1) { |
153 |
struct rlimit rl; |
154 |
|
155 |
/* has not been set explicitly */ |
156 |
c = getrlimit(RLIMIT_NOFILE, &rl); |
157 |
if (c < 0) { |
158 |
perror("getrlimit"); |
159 |
exit(1); |
160 |
} |
161 |
max_connections = rl.rlim_cur; |
162 |
} |
163 |
|
164 |
/* background ourself */ |
165 |
if (do_fork) { |
166 |
switch(fork()) { |
167 |
case -1: |
168 |
/* error */ |
169 |
perror("fork"); |
170 |
exit(1); |
171 |
break; |
172 |
case 0: |
173 |
/* child, success */ |
174 |
break; |
175 |
default: |
176 |
/* parent, success */ |
177 |
exit(0); |
178 |
break; |
179 |
} |
180 |
} |
181 |
|
182 |
/* main loop */ |
183 |
timestamp(); |
184 |
|
185 |
start_time = current_time; |
186 |
|
187 |
/* Blocks signals that are not supposed to be catched |
188 |
* by the children. |
189 |
*/ |
190 |
block_main_signals(); |
191 |
|
192 |
/* spawn the children pool |
193 |
*/ |
194 |
params = smp_init( server_s); |
195 |
|
196 |
/* unblock signals for daddy |
197 |
*/ |
198 |
unblock_main_signals(); |
199 |
|
200 |
/* regenerate parameters in that time interval |
201 |
*/ |
202 |
if (boa_ssl) { |
203 |
if (ssl_params_refresh < DEFAULT_SSL_PARAMS_REFRESH) { |
204 |
log_error_time(); |
205 |
fprintf(stderr, "SSL parameters will be refreshed every %d minutes\n", |
206 |
DEFAULT_SSL_PARAMS_REFRESH/60); |
207 |
ssl_params_refresh = DEFAULT_SSL_PARAMS_REFRESH; |
208 |
} |
209 |
alarm( ssl_params_refresh); |
210 |
} |
211 |
|
212 |
select_loop( params); |
213 |
|
214 |
return 0; |
215 |
} |
216 |
|
217 |
server_params *global_server_params; |
218 |
int global_server_params_size; |
219 |
|
220 |
extern int server_max_threads; |
221 |
|
222 |
/* This function will return a server_params pointer. This |
223 |
* pointer is to be used as a pointer to the select loop. |
224 |
*/ |
225 |
static server_params* smp_init( socket_type server_s[2]) |
226 |
{ |
227 |
int i; |
228 |
server_params *params; |
229 |
|
230 |
#ifdef ENABLE_SMP |
231 |
pthread_t tid; |
232 |
int max_threads = server_max_threads; |
233 |
|
234 |
father_id = pthread_self(); |
235 |
#else |
236 |
const int max_threads = 1; |
237 |
#endif |
238 |
|
239 |
params = malloc( sizeof(server_params) * max_threads); |
240 |
if (params == NULL) { |
241 |
log_error_time(); |
242 |
fprintf(stderr, |
243 |
"Could not allocate memory.\n"); |
244 |
exit(1); |
245 |
} |
246 |
|
247 |
for (i=0;i<max_threads;i++) { |
248 |
params[i].server_s[0] = server_s[0]; |
249 |
params[i].server_s[1] = server_s[1]; |
250 |
params[i].request_ready = NULL; |
251 |
params[i].request_block = NULL; |
252 |
params[i].request_free = NULL; |
253 |
|
254 |
/* for signal handling */ |
255 |
params[i].sighup_flag = 0; |
256 |
params[i].sigchld_flag = 0; |
257 |
params[i].sigalrm_flag = 0; |
258 |
params[i].sigusr1_flag = 0; |
259 |
params[i].sigterm_flag = 0; |
260 |
|
261 |
params[i].sockbufsize = SOCKETBUF_SIZE; |
262 |
|
263 |
params[i].status.requests = 0; |
264 |
params[i].status.errors = 0; |
265 |
|
266 |
params[i].total_connections = 0; |
267 |
params[i].max_fd = 0; |
268 |
|
269 |
params[i].handle_sigbus = 0; |
270 |
} |
271 |
|
272 |
#ifdef ENABLE_SMP |
273 |
params[0].tid = father_id; |
274 |
|
275 |
for( i=1;i<max_threads;i++) { |
276 |
if (pthread_create( &tid, NULL, &select_loop, ¶ms[i]) != 0) |
277 |
{ |
278 |
log_error_time(); |
279 |
fprintf(stderr, |
280 |
"Could not dispatch threads.\n"); |
281 |
exit(1); |
282 |
} |
283 |
params[i].tid = tid; |
284 |
} |
285 |
#endif |
286 |
|
287 |
if (max_threads > 1) { |
288 |
log_error_time(); |
289 |
fprintf(stderr, |
290 |
"Generated pool of %d threads.\n", max_threads); |
291 |
} |
292 |
|
293 |
global_server_params_size = max_threads; |
294 |
global_server_params = params; |
295 |
|
296 |
return ¶ms[0]; |
297 |
} |
298 |
|
299 |
void smp_reinit() |
300 |
{ |
301 |
#ifdef ENABLE_SMP |
302 |
int i; |
303 |
server_params *params = global_server_params; |
304 |
int max_threads = server_max_threads; |
305 |
#else |
306 |
int max_threads = 1; |
307 |
#endif |
308 |
|
309 |
if (global_server_params_size < max_threads) { |
310 |
log_error_time(); |
311 |
fprintf(stderr, |
312 |
"Cannot increase threads on runtime.\n"); |
313 |
max_threads = global_server_params_size; |
314 |
} |
315 |
|
316 |
#ifdef ENABLE_SMP |
317 |
for( i=1;i<max_threads;i++) { |
318 |
pthread_t tid; |
319 |
if (pthread_create( &tid, NULL, &select_loop, ¶ms[i]) != 0) |
320 |
{ |
321 |
log_error_time(); |
322 |
fprintf(stderr, |
323 |
"Could not dispatch threads.\n"); |
324 |
exit(1); |
325 |
} |
326 |
params[i].tid = tid; |
327 |
} |
328 |
#endif |
329 |
|
330 |
if (max_threads > 0) { |
331 |
log_error_time(); |
332 |
fprintf(stderr, |
333 |
"Regenerated a pool of %d threads.\n", max_threads); |
334 |
} |
335 |
|
336 |
return; |
337 |
} |
338 |
|
339 |
|
340 |
static socket_type create_server_socket( int port, int secure) |
341 |
{ |
342 |
socket_type server_s; |
343 |
|
344 |
server_s.secure = secure; |
345 |
server_s.port = port; |
346 |
|
347 |
server_s.socket = socket(SERVER_AF, SOCK_STREAM, IPPROTO_TCP); |
348 |
if (server_s.socket == -1) { |
349 |
DIE("unable to create socket"); |
350 |
} |
351 |
|
352 |
/* server socket is nonblocking */ |
353 |
if (set_nonblock_fd(server_s.socket) == -1) { |
354 |
DIE("fcntl: unable to set server socket to nonblocking"); |
355 |
} |
356 |
|
357 |
/* close server socket on exec so cgi's can't write to it */ |
358 |
if (fcntl(server_s.socket, F_SETFD, 1) == -1) { |
359 |
DIE("can't set close-on-exec on server socket!"); |
360 |
} |
361 |
|
362 |
/* reuse socket addr */ |
363 |
if ((setsockopt(server_s.socket, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt, |
364 |
sizeof (sock_opt))) == -1) { |
365 |
DIE("setsockopt"); |
366 |
} |
367 |
|
368 |
/* internet family-specific code encapsulated in bind_server() */ |
369 |
if (bind_server(server_s.socket, server_ip, port) == -1) { |
370 |
DIE("unable to bind"); |
371 |
} |
372 |
|
373 |
/* listen: large number just in case your kernel is nicely tweaked */ |
374 |
if (listen(server_s.socket, backlog) == -1) { |
375 |
DIE("unable to listen"); |
376 |
} |
377 |
return server_s; |
378 |
} |
379 |
|
380 |
static void drop_privs(void) |
381 |
{ |
382 |
/* give away our privs if we can */ |
383 |
if (getuid() == 0) { |
384 |
struct passwd *passwdbuf; |
385 |
passwdbuf = getpwuid(server_uid); |
386 |
if (passwdbuf == NULL) { |
387 |
DIE("getpwuid"); |
388 |
} |
389 |
if (initgroups(passwdbuf->pw_name, passwdbuf->pw_gid) == -1) { |
390 |
DIE("initgroups"); |
391 |
} |
392 |
if (setgid(server_gid) == -1) { |
393 |
DIE("setgid"); |
394 |
} |
395 |
if (setuid(server_uid) == -1) { |
396 |
DIE("setuid"); |
397 |
} |
398 |
/* test for failed-but-return-was-successful setuid |
399 |
* http://www.securityportal.com/list-archive/bugtraq/2000/Jun/0101.html |
400 |
*/ |
401 |
if (setuid(0) != -1) { |
402 |
DIE("icky Linux kernel bug!"); |
403 |
} |
404 |
} else { |
405 |
if (server_gid || server_uid) { |
406 |
log_error_time(); |
407 |
fprintf(stderr, "Warning: " |
408 |
"Not running as root: no attempt to change" |
409 |
" to uid %d gid %d\n", server_uid, server_gid); |
410 |
} |
411 |
server_gid = getgid(); |
412 |
server_uid = getuid(); |
413 |
} |
414 |
} |
415 |
|
416 |
/* |
417 |
* Name: fixup_server_root |
418 |
* |
419 |
* Description: Makes sure the server root is valid. |
420 |
* |
421 |
*/ |
422 |
|
423 |
static void fixup_server_root() |
424 |
{ |
425 |
char *dirbuf; |
426 |
|
427 |
if (!server_root) { |
428 |
#ifdef SERVER_ROOT |
429 |
server_root = strdup(SERVER_ROOT); |
430 |
if (!server_root) { |
431 |
perror("strdup (SERVER_ROOT)"); |
432 |
exit(1); |
433 |
} |
434 |
#else |
435 |
fputs(SERVER_NAME": don't know where server root is. Please #define " |
436 |
"SERVER_ROOT in defines.h\n" |
437 |
"and recompile, or use the -c command line option to " |
438 |
"specify it.\n", stderr); |
439 |
exit(1); |
440 |
#endif |
441 |
} |
442 |
|
443 |
if (chdir(server_root) == -1) { |
444 |
fprintf(stderr, "Could not chdir to \"%s\": aborting\n", |
445 |
server_root); |
446 |
exit(1); |
447 |
} |
448 |
|
449 |
dirbuf = normalize_path(server_root); |
450 |
free(server_root); |
451 |
server_root = dirbuf; |
452 |
} |
453 |
|