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

Annotation of /hydra/src/boa.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations)
Mon Sep 23 19:28:41 2002 UTC (21 years, 6 months ago) by nmav
Branch: MAIN
CVS Tags: BOAS_WITH_RANGES_AND_CGI
Changes since 1.2: +3 -4 lines
File MIME type: text/plain
Cleaned up the CGI handling. Now gunzip, indexer are treated like real cgis. Several other cleanups.

1 nmav 1.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 nmav 1.3 /* $Id: boa.c,v 1.2 2002/09/23 17:56:41 nmav Exp $*/
26 nmav 1.1
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 nmav 1.3 socket_type server_s[2] = {{ -1, 0, 0, 0}, { -1, -1, 0, 0 }}; /* boa socket */
59 nmav 1.1 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(&current_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     if (max_connections < 1) {
151     struct rlimit rl;
152    
153     /* has not been set explicitly */
154     c = getrlimit(RLIMIT_NOFILE, &rl);
155     if (c < 0) {
156     perror("getrlimit");
157     exit(1);
158     }
159     max_connections = rl.rlim_cur;
160     }
161    
162     /* background ourself */
163     if (do_fork) {
164     switch(fork()) {
165     case -1:
166     /* error */
167     perror("fork");
168     exit(1);
169     break;
170     case 0:
171     /* child, success */
172     break;
173     default:
174     /* parent, success */
175     exit(0);
176     break;
177     }
178     }
179    
180     /* main loop */
181     timestamp();
182    
183     start_time = current_time;
184    
185     /* Blocks signals that are not supposed to be catched
186     * by the children.
187     */
188     block_main_signals();
189    
190     /* spawn the children pool
191     */
192     params = smp_init( server_s);
193    
194     /* unblock signals for daddy
195     */
196     unblock_main_signals();
197    
198     /* regenerate parameters in that time interval
199     */
200     if (boa_ssl) {
201     alarm( ssl_params_refresh);
202     }
203    
204     select_loop( params);
205    
206     return 0;
207     }
208    
209     server_params *global_server_params;
210     int global_server_params_size;
211    
212     extern int server_max_threads;
213    
214     /* This function will return a server_params pointer. This
215     * pointer is to be used as a pointer to the select loop.
216     */
217     static server_params* smp_init( socket_type server_s[2])
218     {
219     int i;
220     server_params *params;
221    
222     #ifdef ENABLE_SMP
223     pthread_t tid;
224     int max_threads = server_max_threads;
225    
226     father_id = pthread_self();
227     #else
228     const int max_threads = 1;
229     #endif
230    
231     params = malloc( sizeof(server_params) * max_threads);
232     if (params == NULL) {
233     log_error_time();
234     fprintf(stderr,
235     "Could not allocate memory.\n");
236     exit(1);
237     }
238    
239     for (i=0;i<max_threads;i++) {
240     params[i].server_s[0] = server_s[0];
241     params[i].server_s[1] = server_s[1];
242     params[i].request_ready = NULL;
243     params[i].request_block = NULL;
244     params[i].request_free = NULL;
245    
246     /* for signal handling */
247     params[i].sighup_flag = 0;
248     params[i].sigchld_flag = 0;
249     params[i].sigalrm_flag = 0;
250     params[i].sigusr1_flag = 0;
251     params[i].sigterm_flag = 0;
252    
253     params[i].sockbufsize = SOCKETBUF_SIZE;
254    
255     params[i].status.requests = 0;
256     params[i].status.errors = 0;
257    
258     params[i].total_connections = 0;
259     params[i].max_fd = 0;
260    
261     params[i].handle_sigbus = 0;
262     }
263    
264     #ifdef ENABLE_SMP
265     params[0].tid = father_id;
266    
267     for( i=1;i<max_threads;i++) {
268     if (pthread_create( &tid, NULL, &select_loop, &params[i]) != 0)
269     {
270     log_error_time();
271     fprintf(stderr,
272     "Could not dispatch threads.\n");
273     exit(1);
274     }
275     params[i].tid = tid;
276     }
277     #endif
278    
279     if (max_threads > 1) {
280     log_error_time();
281     fprintf(stderr,
282     "Generated pool of %d threads.\n", max_threads);
283     }
284    
285     global_server_params_size = max_threads;
286     global_server_params = params;
287    
288     return &params[0];
289     }
290    
291     void smp_reinit()
292     {
293     #ifdef ENABLE_SMP
294 nmav 1.2 int i;
295     server_params *params = global_server_params;
296     int max_threads = server_max_threads;
297 nmav 1.1 #else
298 nmav 1.2 int max_threads = 1;
299 nmav 1.1 #endif
300    
301 nmav 1.2 if (global_server_params_size < max_threads) {
302 nmav 1.1 log_error_time();
303     fprintf(stderr,
304     "Cannot increase threads on runtime.\n");
305     max_threads = global_server_params_size;
306 nmav 1.2 }
307 nmav 1.1
308     #ifdef ENABLE_SMP
309 nmav 1.2 for( i=1;i<max_threads;i++) {
310 nmav 1.1 pthread_t tid;
311     if (pthread_create( &tid, NULL, &select_loop, &params[i]) != 0)
312     {
313     log_error_time();
314     fprintf(stderr,
315     "Could not dispatch threads.\n");
316     exit(1);
317     }
318     params[i].tid = tid;
319 nmav 1.2 }
320 nmav 1.1 #endif
321    
322 nmav 1.2 if (max_threads > 0) {
323 nmav 1.1 log_error_time();
324     fprintf(stderr,
325     "Regenerated a pool of %d threads.\n", max_threads);
326 nmav 1.2 }
327 nmav 1.1
328 nmav 1.2 return;
329 nmav 1.1 }
330    
331    
332     static socket_type create_server_socket( int port, int secure)
333     {
334     socket_type server_s;
335    
336     server_s.secure = secure;
337 nmav 1.3 server_s.port = port;
338 nmav 1.1
339     server_s.socket = socket(SERVER_AF, SOCK_STREAM, IPPROTO_TCP);
340     if (server_s.socket == -1) {
341     DIE("unable to create socket");
342     }
343    
344     /* server socket is nonblocking */
345     if (set_nonblock_fd(server_s.socket) == -1) {
346     DIE("fcntl: unable to set server socket to nonblocking");
347     }
348    
349     /* close server socket on exec so cgi's can't write to it */
350     if (fcntl(server_s.socket, F_SETFD, 1) == -1) {
351     DIE("can't set close-on-exec on server socket!");
352     }
353    
354     /* reuse socket addr */
355     if ((setsockopt(server_s.socket, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt,
356     sizeof (sock_opt))) == -1) {
357     DIE("setsockopt");
358     }
359    
360     /* internet family-specific code encapsulated in bind_server() */
361     if (bind_server(server_s.socket, server_ip, port) == -1) {
362     DIE("unable to bind");
363     }
364    
365     /* listen: large number just in case your kernel is nicely tweaked */
366     if (listen(server_s.socket, backlog) == -1) {
367     DIE("unable to listen");
368     }
369     return server_s;
370     }
371    
372     static void drop_privs(void)
373     {
374     /* give away our privs if we can */
375     if (getuid() == 0) {
376     struct passwd *passwdbuf;
377     passwdbuf = getpwuid(server_uid);
378     if (passwdbuf == NULL) {
379     DIE("getpwuid");
380     }
381     if (initgroups(passwdbuf->pw_name, passwdbuf->pw_gid) == -1) {
382     DIE("initgroups");
383     }
384     if (setgid(server_gid) == -1) {
385     DIE("setgid");
386     }
387     if (setuid(server_uid) == -1) {
388     DIE("setuid");
389     }
390     /* test for failed-but-return-was-successful setuid
391     * http://www.securityportal.com/list-archive/bugtraq/2000/Jun/0101.html
392     */
393     if (setuid(0) != -1) {
394     DIE("icky Linux kernel bug!");
395     }
396     } else {
397     if (server_gid || server_uid) {
398     log_error_time();
399     fprintf(stderr, "Warning: "
400     "Not running as root: no attempt to change"
401     " to uid %d gid %d\n", server_uid, server_gid);
402     }
403     server_gid = getgid();
404     server_uid = getuid();
405     }
406     }
407    
408     /*
409     * Name: fixup_server_root
410     *
411     * Description: Makes sure the server root is valid.
412     *
413     */
414    
415     static void fixup_server_root()
416     {
417     char *dirbuf;
418    
419     if (!server_root) {
420     #ifdef SERVER_ROOT
421     server_root = strdup(SERVER_ROOT);
422     if (!server_root) {
423     perror("strdup (SERVER_ROOT)");
424     exit(1);
425     }
426     #else
427     fputs("boa: don't know where server root is. Please #define "
428     "SERVER_ROOT in boa.h\n"
429     "and recompile, or use the -c command line option to "
430     "specify it.\n", stderr);
431     exit(1);
432     #endif
433     }
434    
435     if (chdir(server_root) == -1) {
436     fprintf(stderr, "Could not chdir to \"%s\": aborting\n",
437     server_root);
438     exit(1);
439     }
440    
441     dirbuf = normalize_path(server_root);
442     free(server_root);
443     server_root = dirbuf;
444     }
445    

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26