/[hydra]/hydra/src/boa.c
ViewVC logotype

Contents of /hydra/src/boa.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (show annotations)
Sat Sep 28 10:52:46 2002 UTC (21 years, 6 months ago) by nmav
Branch: MAIN
Changes since 1.10: +5 -1 lines
File MIME type: text/plain
Cleaned up SSL support. Hydra can be now compiled even if gnutls is not available.

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.10 2002/09/28 10:05:00 nmav Exp $*/
26
27 #include "boa.h"
28 #include "ssl.h"
29 #include <sys/resource.h>
30 #ifdef ENABLE_SMP
31 pthread_t father_id;
32 #endif
33
34 extern int ssl_params_refresh;
35
36 /* globals */
37 int backlog = SO_MAXCONN;
38 time_t start_time;
39
40 time_t current_time;
41
42
43 /* static to boa.c */
44 static void fixup_server_root(void);
45 static socket_type create_server_socket( int port, int);
46 static void hic_init(void);
47 static void drop_privs(void);
48 static server_params *smp_init(socket_type server_s[2]);
49 static int sock_opt = 1;
50 static int do_fork = 1;
51 int devnullfd = -1;
52
53 int main(int argc, char **argv)
54 {
55 int c; /* command line arg */
56 socket_type server_s[2] = {{ -1, 0, 0, 0}, { -1, -1, 0, 0 }}; /* boa socket */
57 server_params *params;
58
59 /* set umask to u+rw, u-x, go-rwx */
60 c = umask(~0600);
61 if (c == -1) {
62 perror("umask");
63 exit(1);
64 }
65
66 devnullfd = open("/dev/null", 0);
67
68 /* make STDIN and STDOUT point to /dev/null */
69 if (devnullfd == -1) {
70 DIE("can't open /dev/null");
71 }
72
73 if (dup2(devnullfd, STDIN_FILENO) == -1) {
74 DIE("can't dup2 /dev/null to STDIN_FILENO");
75 }
76
77 if (dup2(devnullfd, STDOUT_FILENO) == -1) {
78 DIE("can't dup2 /dev/null to STDOUT_FILENO");
79 }
80
81 /* but first, update timestamp, because log_error_time uses it */
82 (void) time(&current_time);
83
84 while ((c = getopt(argc, argv, "c:r:d")) != -1) {
85 switch (c) {
86 case 'c':
87 if (server_root)
88 free(server_root);
89 server_root = strdup(optarg);
90 if (!server_root) {
91 perror("strdup (for server_root)");
92 exit(1);
93 }
94 break;
95 case 'r':
96 if (chdir(optarg) == -1) {
97 log_error_time();
98 perror("chdir (to chroot)");
99 exit(1);
100 }
101 if (chroot(optarg) == -1) {
102 log_error_time();
103 perror("chroot");
104 exit(1);
105 }
106 if (chdir("/") == -1) {
107 log_error_time();
108 perror("chdir (after chroot)");
109 exit(1);
110 }
111 break;
112 case 'd':
113 do_fork = 0;
114 break;
115 default:
116 fprintf(stderr, "Usage: %s [-c serverroot] [-r chroot] [-d]\n", argv[0]);
117 exit(1);
118 }
119 }
120
121 fixup_server_root();
122 read_config_files();
123 open_logs();
124
125 if ((boa_ssl >= 2 || boa_ssl == 0) && server_port > 0) {
126 server_s[0] = create_server_socket( server_port, 0);
127 }
128
129 if (boa_ssl != 0 && ssl_port > 0) {
130 server_s[1] = create_server_socket( ssl_port, 1);
131 }
132
133 if (server_s[1].socket == -1 && server_s[0].socket == -1) {
134 log_error_time();
135 fprintf(stderr, "Could not initialize sockets\n");
136 exit(1);
137 }
138
139 init_signals();
140 drop_privs();
141 create_common_env();
142 build_needs_escape();
143
144 #ifdef ENABLE_SSL
145 if (boa_ssl) {
146 initialize_ssl();
147 }
148 #endif
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 hic_init();
193
194 /* spawn the children pool
195 */
196 params = smp_init( server_s);
197
198 /* unblock signals for daddy
199 */
200 unblock_main_signals();
201
202 /* regenerate parameters in that time interval
203 */
204 #ifdef ENABLE_SSL
205 if (boa_ssl) {
206 if (ssl_params_refresh < DEFAULT_SSL_PARAMS_REFRESH) {
207 log_error_time();
208 fprintf(stderr, "SSL parameters will be refreshed every %d minutes\n",
209 DEFAULT_SSL_PARAMS_REFRESH/60);
210 ssl_params_refresh = DEFAULT_SSL_PARAMS_REFRESH;
211 }
212 alarm( ssl_params_refresh);
213 }
214 #endif
215
216 select_loop( params);
217
218 return 0;
219 }
220
221 server_params *global_server_params;
222 int global_server_params_size;
223
224 extern int server_max_threads;
225
226 /* This function will return a server_params pointer. This
227 * pointer is to be used as a pointer to the select loop.
228 */
229 static server_params* smp_init( socket_type server_s[2])
230 {
231 int i;
232 server_params *params;
233
234 #ifdef ENABLE_SMP
235 pthread_t tid;
236 int max_threads = server_max_threads;
237
238 father_id = pthread_self();
239 #else
240 const int max_threads = 1;
241 #endif
242
243 params = malloc( sizeof(server_params) * max_threads);
244 if (params == NULL) {
245 log_error_time();
246 fprintf(stderr,
247 "Could not allocate memory.\n");
248 exit(1);
249 }
250
251 for (i=0;i<max_threads;i++) {
252 params[i].server_s[0] = server_s[0];
253 params[i].server_s[1] = server_s[1];
254 params[i].request_ready = NULL;
255 params[i].request_block = NULL;
256 params[i].request_free = NULL;
257
258 /* for signal handling */
259 params[i].sighup_flag = 0;
260 params[i].sigchld_flag = 0;
261 params[i].sigalrm_flag = 0;
262 params[i].sigusr1_flag = 0;
263 params[i].sigterm_flag = 0;
264
265 params[i].sockbufsize = SOCKETBUF_SIZE;
266
267 params[i].status.requests = 0;
268 params[i].status.errors = 0;
269
270 params[i].total_connections = 0;
271 params[i].max_fd = 0;
272
273 params[i].handle_sigbus = 0;
274 }
275
276 #ifdef ENABLE_SMP
277 params[0].tid = father_id;
278
279 for( i=1;i<max_threads;i++) {
280 if (pthread_create( &tid, NULL, &select_loop, &params[i]) != 0)
281 {
282 log_error_time();
283 fprintf(stderr,
284 "Could not dispatch threads.\n");
285 exit(1);
286 }
287 params[i].tid = tid;
288 }
289 #endif
290
291 if (max_threads > 1) {
292 log_error_time();
293 fprintf(stderr,
294 "%s: Generated pool of %d threads.\n", SERVER_NAME, max_threads);
295 }
296
297 global_server_params_size = max_threads;
298 global_server_params = params;
299
300 return &params[0];
301 }
302
303 void smp_reinit()
304 {
305 #ifdef ENABLE_SMP
306 int i;
307 server_params *params = global_server_params;
308 int max_threads = server_max_threads;
309 #else
310 int max_threads = 1;
311 #endif
312
313 if (global_server_params_size < max_threads) {
314 log_error_time();
315 fprintf(stderr,
316 "Cannot increase threads on runtime.\n");
317 max_threads = global_server_params_size;
318 }
319
320 #ifdef ENABLE_SMP
321 for( i=1;i<max_threads;i++) {
322 pthread_t tid;
323 if (pthread_create( &tid, NULL, &select_loop, &params[i]) != 0)
324 {
325 log_error_time();
326 fprintf(stderr,
327 "Could not dispatch threads.\n");
328 exit(1);
329 }
330 params[i].tid = tid;
331 }
332 #endif
333
334 if (max_threads > 0) {
335 log_error_time();
336 fprintf(stderr,
337 "Regenerated a pool of %d threads.\n", max_threads);
338 }
339
340 return;
341 }
342
343 #ifdef ENABLE_HIC
344
345 pthread_t hic_tid;
346
347 /* This function will return a server_params pointer. This
348 * pointer is to be used as a pointer to the select loop.
349 */
350 static void hic_init()
351 {
352
353 #ifdef ENABLE_SMP
354
355 if (pthread_create( &hic_tid, NULL, &hic_main_loop, NULL) != 0)
356 {
357 log_error_time();
358 fprintf(stderr,
359 "Could not dispatch hic thread.\n");
360 exit(1);
361 }
362
363 log_error_time();
364 fprintf(stderr,
365 "%s: Dispatched HIC main thread.\n", SERVER_NAME);
366
367 #endif
368
369 return;
370 }
371
372 #endif /* ENABLE_HIC */
373
374 static socket_type create_server_socket( int port, int secure)
375 {
376 socket_type server_s;
377
378 server_s.secure = secure;
379 server_s.port = port;
380
381 server_s.socket = socket(SERVER_AF, SOCK_STREAM, IPPROTO_TCP);
382 if (server_s.socket == -1) {
383 DIE("unable to create socket");
384 }
385
386 /* server socket is nonblocking */
387 if (set_nonblock_fd(server_s.socket) == -1) {
388 DIE("fcntl: unable to set server socket to nonblocking");
389 }
390
391 /* close server socket on exec so cgi's can't write to it */
392 if (fcntl(server_s.socket, F_SETFD, 1) == -1) {
393 DIE("can't set close-on-exec on server socket!");
394 }
395
396 /* reuse socket addr */
397 if ((setsockopt(server_s.socket, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt,
398 sizeof (sock_opt))) == -1) {
399 DIE("setsockopt");
400 }
401
402 /* internet family-specific code encapsulated in bind_server() */
403 if (bind_server(server_s.socket, server_ip, port) == -1) {
404 DIE("unable to bind");
405 }
406
407 /* listen: large number just in case your kernel is nicely tweaked */
408 if (listen(server_s.socket, backlog) == -1) {
409 DIE("unable to listen");
410 }
411 return server_s;
412 }
413
414 static void drop_privs(void)
415 {
416 /* give away our privs if we can */
417 if (getuid() == 0) {
418 struct passwd *passwdbuf;
419 passwdbuf = getpwuid(server_uid);
420 if (passwdbuf == NULL) {
421 DIE("getpwuid");
422 }
423 if (initgroups(passwdbuf->pw_name, passwdbuf->pw_gid) == -1) {
424 DIE("initgroups");
425 }
426 if (setgid(server_gid) == -1) {
427 DIE("setgid");
428 }
429 if (setuid(server_uid) == -1) {
430 DIE("setuid");
431 }
432 /* test for failed-but-return-was-successful setuid
433 * http://www.securityportal.com/list-archive/bugtraq/2000/Jun/0101.html
434 */
435 if (setuid(0) != -1) {
436 DIE("icky Linux kernel bug!");
437 }
438 } else {
439 if (server_gid || server_uid) {
440 log_error_time();
441 fprintf(stderr, "Warning: "
442 "Not running as root: no attempt to change"
443 " to uid %d gid %d\n", server_uid, server_gid);
444 }
445 server_gid = getgid();
446 server_uid = getuid();
447 }
448 }
449
450 /*
451 * Name: fixup_server_root
452 *
453 * Description: Makes sure the server root is valid.
454 *
455 */
456
457 static void fixup_server_root()
458 {
459 char *dirbuf;
460
461 if (!server_root) {
462 #ifdef SERVER_ROOT
463 server_root = strdup(SERVER_ROOT);
464 if (!server_root) {
465 perror("strdup (SERVER_ROOT)");
466 exit(1);
467 }
468 #else
469 fputs(SERVER_NAME": don't know where server root is. Please #define "
470 "SERVER_ROOT in defines.h\n"
471 "and recompile, or use the -c command line option to "
472 "specify it.\n", stderr);
473 exit(1);
474 #endif
475 }
476
477 if (chdir(server_root) == -1) {
478 fprintf(stderr, "Could not chdir to \"%s\": aborting\n",
479 server_root);
480 exit(1);
481 }
482
483 dirbuf = normalize_path(server_root);
484 free(server_root);
485 server_root = dirbuf;
486 }
487

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26