This repository has been archived by the owner on Dec 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
mini_httpd.c
3701 lines (3346 loc) · 89.7 KB
/
mini_httpd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* mini_httpd - small HTTP server
**
** Copyright © 1999,2000 by Jef Poskanzer <jef@mail.acme.com>.
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
*/
#include "version.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <limits.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <time.h>
#include <pwd.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <ctype.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <dirent.h>
#include "port.h"
#include "match.h"
#include "tdate_parse.h"
#ifdef HAVE_SENDFILE
# ifdef HAVE_LINUX_SENDFILE
# include <sys/sendfile.h>
# else /* HAVE_LINUX_SENDFILE */
# include <sys/uio.h>
# endif /* HAVE_LINUX_SENDFILE */
#endif /* HAVE_SENDFILE */
#ifdef USE_SSL
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif /* USE_SSL */
extern char* crypt( const char* key, const char* setting );
#if defined(AF_INET6) && defined(IN6_IS_ADDR_V4MAPPED)
#define USE_IPV6
#endif
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif
#ifndef SHUT_WR
#define SHUT_WR 1
#endif
#ifndef SIZE_T_MAX
#define SIZE_T_MAX 2147483647L
#endif
#ifndef HAVE_INT64T
typedef long long int64_t;
#endif
#ifdef __CYGWIN__
#define timezone _timezone
#endif
#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
#ifndef ERR_DIR
#define ERR_DIR "errors"
#endif /* ERR_DIR */
#ifndef DEFAULT_HTTP_PORT
#define DEFAULT_HTTP_PORT 80
#endif /* DEFAULT_HTTP_PORT */
#ifdef USE_SSL
#ifndef DEFAULT_HTTPS_PORT
#define DEFAULT_HTTPS_PORT 443
#endif /* DEFAULT_HTTPS_PORT */
#ifndef DEFAULT_CERTFILE
#define DEFAULT_CERTFILE "mini_httpd.pem"
#endif /* DEFAULT_CERTFILE */
#endif /* USE_SSL */
#ifndef DEFAULT_USER
#define DEFAULT_USER "nobody"
#endif /* DEFAULT_USER */
#ifndef CGI_NICE
#define CGI_NICE 10
#endif /* CGI_NICE */
#ifndef CGI_PATH
#define CGI_PATH "/usr/local/bin:/usr/ucb:/bin:/usr/bin"
#endif /* CGI_PATH */
#ifndef CGI_LD_LIBRARY_PATH
#define CGI_LD_LIBRARY_PATH "/usr/local/lib:/usr/lib"
#endif /* CGI_LD_LIBRARY_PATH */
#ifndef AUTH_FILE
#define AUTH_FILE ".htpasswd"
#endif /* AUTH_FILE */
#ifndef READ_TIMEOUT
#define READ_TIMEOUT 60
#endif /* READ_TIMEOUT */
#ifndef WRITE_TIMEOUT
#define WRITE_TIMEOUT 300
#endif /* WRITE_TIMEOUT */
#ifndef DEFAULT_CHARSET
#define DEFAULT_CHARSET "iso-8859-1"
#endif /* DEFAULT_CHARSET */
#define METHOD_UNKNOWN 0
#define METHOD_GET 1
#define METHOD_HEAD 2
#define METHOD_POST 3
/* A multi-family sockaddr. */
typedef union {
struct sockaddr sa;
struct sockaddr_in sa_in;
#ifdef USE_IPV6
struct sockaddr_in6 sa_in6;
struct sockaddr_storage sa_stor;
#endif /* USE_IPV6 */
} usockaddr;
extern char** environ;
static char* argv0;
static int debug;
static unsigned short port;
static char* dir;
static char* data_dir;
static int do_chroot;
static int vhost;
static char* user;
static char* cgi_pattern;
static char* url_pattern;
static int no_empty_referers;
static char* local_pattern;
static char* hostname;
static char hostname_buf[500];
static char* logfile;
static char* errfile;
static char* pidfile;
static int cgipassenv;
static int tcp_nopush;
static char* charset;
static char* p3p;
static char* xrealip;
static int max_age;
static FILE* logfp;
static FILE* errfp;
static int listen4_fd, listen6_fd;
#ifdef USE_SSL
static int do_ssl;
static char* certfile;
static char* cipher;
static SSL_CTX* ssl_ctx;
#endif /* USE_SSL */
static char cwd[MAXPATHLEN];
static int got_hup;
/* Request variables. */
static int conn_fd;
#ifdef USE_SSL
static SSL* ssl;
#endif /* USE_SSL */
static usockaddr client_addr;
static char* request;
static size_t request_size, request_len, request_idx;
static int method;
static char* path;
static char* file;
static char* pathinfo;
struct stat sb;
static char* query;
static char* protocol;
static int status;
static off_t bytes;
static char* req_hostname;
static char* authorization;
static size_t content_length;
static char* content_type;
static char* cookie;
static char* host;
static time_t if_modified_since;
static char* referer;
static char* useragent;
static char* remote_addr;
static char* remoteuser;
/* Forwards. */
static void usage( void );
static void read_config( char* filename );
static void value_required( char* name, char* value );
static void no_value_required( char* name, char* value );
static int initialize_listen_socket( usockaddr* usaP );
static void handle_request( void );
static void de_dotdot( char* file );
static int get_pathinfo( void );
static void do_file( void );
static void do_dir( void );
#ifdef HAVE_SCANDIR
static char* file_details( const char* dir, const char* name );
static void strencode( char* to, size_t tosize, const char* from );
#endif /* HAVE_SCANDIR */
static void do_cgi( void );
static void cgi_interpose_input( int wfd );
static void post_post_garbage_hack( void );
static void cgi_interpose_output( int rfd, int parse_headers );
static char** make_argp( void );
static char** make_envp( void );
static char* build_env( char* fmt, char* arg );
static void auth_check( char* dirname );
static void send_authenticate( char* realm );
static char* virtual_file( char* file );
static void send_error( int s, char* title, char* extra_header, char* text );
static void send_error_body( int s, char* title, char* text );
static int send_error_file( char* filename );
static void send_error_tail( void );
static void add_headers( int s, char* title, char* extra_header, char* me, char* mt, off_t b, time_t mod );
static void start_request( void );
static void add_to_request( char* str, size_t len );
static char* get_request_line( void );
static void start_response( void );
static void add_to_response( char* str, size_t len );
static void send_response( void );
static void send_via_write( int fd, off_t size );
static ssize_t my_read( char* buf, size_t size );
static ssize_t my_write( char* buf, size_t size );
#ifdef HAVE_SENDFILE
static int my_sendfile( int fd, int socket, off_t offset, size_t nbytes );
#endif /* HAVE_SENDFILE */
static void add_to_buf( char** bufP, size_t* bufsizeP, size_t* buflenP, char* str, size_t len );
static void make_log_entry( void );
static void check_referer( void );
static int really_check_referer( void );
static char* get_method_str( int m );
static void init_mime( void );
static const char* figure_mime( char* name, char* me, size_t me_size );
static void handle_sigterm( int sig );
static void handle_sighup( int sig );
static void handle_sigchld( int sig );
static void re_open_logfile( void );
static void handle_read_timeout( int sig );
static void handle_write_timeout( int sig );
static void lookup_hostname( usockaddr* usa4P, size_t sa4_len, int* gotv4P, usockaddr* usa6P, size_t sa6_len, int* gotv6P );
static char* ntoa( usockaddr* usaP );
static int sockaddr_check( usockaddr* usaP );
static size_t sockaddr_len( usockaddr* usaP );
static void strdecode( char* to, char* from );
static int hexit( char c );
static int b64_decode( const char* str, unsigned char* space, int size );
static void set_ndelay( int fd );
static void clear_ndelay( int fd );
static void* e_malloc( size_t size );
static void* e_realloc( void* optr, size_t size );
static char* e_strdup( char* ostr );
#ifdef NO_SNPRINTF
static int snprintf( char* str, size_t size, const char* format, ... );
#endif /* NO_SNPRINTF */
int
main( int argc, char** argv )
{
int argn;
struct passwd* pwd;
uid_t uid = 32767;
gid_t gid = 32767;
usockaddr host_addr4;
usockaddr host_addr6;
int gotv4, gotv6;
fd_set lfdset;
int maxfd;
usockaddr usa;
int sz, r;
char* cp;
/* Parse args. */
argv0 = argv[0];
debug = 0;
port = 0;
dir = (char*) 0;
data_dir = (char*) 0;
do_chroot = 0;
vhost = 0;
cgi_pattern = (char*) 0;
url_pattern = (char*) 0;
no_empty_referers = 0;
local_pattern = (char*) 0;
charset = DEFAULT_CHARSET;
p3p = (char*) 0;
xrealip = (char*) 0;
max_age = -1;
user = DEFAULT_USER;
hostname = (char*) 0;
logfile = (char*) 0;
errfile = (char*) 0;
pidfile = (char*) 0;
cgipassenv = 0;
tcp_nopush = 0;
logfp = (FILE*) 0;
errfp = (FILE*) 0;
#ifdef USE_SSL
do_ssl = 0;
certfile = DEFAULT_CERTFILE;
cipher = (char*) 0;
#endif /* USE_SSL */
argn = 1;
while ( argn < argc && argv[argn][0] == '-' )
{
if ( strcmp( argv[argn], "-V" ) == 0 )
{
(void) printf( "%s\n", SERVER_SOFTWARE );
exit( 0 );
}
else if ( strcmp( argv[argn], "-C" ) == 0 && argn + 1 < argc )
{
++argn;
read_config( argv[argn] );
}
else if ( strcmp( argv[argn], "-D" ) == 0 )
debug = 1;
#ifdef USE_SSL
else if ( strcmp( argv[argn], "-S" ) == 0 )
do_ssl = 1;
else if ( strcmp( argv[argn], "-E" ) == 0 && argn + 1 < argc )
{
++argn;
certfile = argv[argn];
}
else if ( strcmp( argv[argn], "-Y" ) == 0 && argn + 1 < argc )
{
++argn;
cipher = argv[argn];
}
#endif /* USE_SSL */
else if ( strcmp( argv[argn], "-p" ) == 0 && argn + 1 < argc )
{
++argn;
port = (unsigned short) atoi( argv[argn] );
}
else if ( strcmp( argv[argn], "-d" ) == 0 && argn + 1 < argc )
{
++argn;
dir = argv[argn];
}
else if ( strcmp( argv[argn], "-dd" ) == 0 && argn + 1 < argc )
{
++argn;
data_dir = argv[argn];
}
else if ( strcmp( argv[argn], "-c" ) == 0 && argn + 1 < argc )
{
++argn;
cgi_pattern = argv[argn];
}
else if ( strcmp( argv[argn], "-u" ) == 0 && argn + 1 < argc )
{
++argn;
user = argv[argn];
}
else if ( strcmp( argv[argn], "-h" ) == 0 && argn + 1 < argc )
{
++argn;
hostname = argv[argn];
}
else if ( strcmp( argv[argn], "-r" ) == 0 )
do_chroot = 1;
else if ( strcmp( argv[argn], "-v" ) == 0 )
vhost = 1;
else if ( strcmp( argv[argn], "-l" ) == 0 && argn + 1 < argc )
{
++argn;
logfile = argv[argn];
}
else if ( strcmp( argv[argn], "-ll" ) == 0 && argn + 1 < argc )
{
++argn;
errfile = argv[argn];
}
else if ( strcmp( argv[argn], "-i" ) == 0 && argn + 1 < argc )
{
++argn;
pidfile = argv[argn];
}
else if ( strcmp( argv[argn], "-T" ) == 0 && argn + 1 < argc )
{
++argn;
charset = argv[argn];
}
else if ( strcmp( argv[argn], "-P" ) == 0 && argn + 1 < argc )
{
++argn;
p3p = argv[argn];
}
else if ( strcmp( argv[argn], "-X" ) == 0 && argn + 1 < argc )
{
++argn;
xrealip = argv[argn];
}
else if ( strcmp( argv[argn], "-M" ) == 0 && argn + 1 < argc )
{
++argn;
max_age = atoi( argv[argn] );
}
else
usage();
++argn;
}
if ( argn != argc )
usage();
cp = strrchr( argv0, '/' );
if ( cp != (char*) 0 )
++cp;
else
cp = argv0;
openlog( cp, LOG_NDELAY|LOG_PID, LOG_DAEMON );
if ( port == 0 )
{
#ifdef USE_SSL
if ( do_ssl )
port = DEFAULT_HTTPS_PORT;
else
port = DEFAULT_HTTP_PORT;
#else /* USE_SSL */
port = DEFAULT_HTTP_PORT;
#endif /* USE_SSL */
}
/* If we're root and we're going to become another user, get the uid/gid
** now.
*/
if ( getuid() == 0 )
{
pwd = getpwnam( user );
if ( pwd == (struct passwd*) 0 )
{
syslog( LOG_CRIT, "unknown user - '%s'", user );
(void) fprintf( stderr, "%s: unknown user - '%s'\n", argv0, user );
exit( 1 );
}
uid = pwd->pw_uid;
gid = pwd->pw_gid;
}
/* Log file. */
if ( logfile != (char*) 0 )
{
logfp = fopen( logfile, "a" );
if ( logfp == (FILE*) 0 )
{
syslog( LOG_CRIT, "%s - %m", logfile );
perror( logfile );
exit( 1 );
}
if ( logfile[0] != '/' )
{
syslog( LOG_WARNING, "logfile is not an absolute path, you may not be able to re-open it" );
(void) fprintf( stderr, "%s: logfile is not an absolute path, you may not be able to re-open it\n", argv0 );
}
if ( getuid() == 0 )
{
/* If we are root then we chown the log file to the user we'll
** be switching to.
*/
if ( fchown( fileno( logfp ), uid, gid ) < 0 )
{
syslog( LOG_WARNING, "fchown logfile - %m" );
perror( "fchown logfile" );
}
}
}
/* Error file. */
if ( errfile != (char*) 0 )
{
errfp = fopen( errfile, "a" );
if ( errfp == (FILE*) 0 )
{
syslog( LOG_CRIT, "%s - %m", errfile );
perror( errfile );
exit( 1 );
}
if ( errfile[0] != '/' )
{
syslog( LOG_WARNING, "errfile is not an absolute path, you may not be able to re-open it" );
(void) fprintf( stderr, "%s: errfile is not an absolute path, you may not be able to re-open it\n", argv0 );
}
if ( getuid() == 0 )
{
/* If we are root then we chown the log file to the user we'll
** be switching to.
*/
if ( fchown( fileno( errfp ), uid, gid ) < 0 )
{
syslog( LOG_WARNING, "fchown errfile - %m" );
perror( "fchown errfile" );
}
}
dup2( fileno( errfp ), STDERR_FILENO );
}
/* Look up hostname. */
lookup_hostname(
&host_addr4, sizeof(host_addr4), &gotv4,
&host_addr6, sizeof(host_addr6), &gotv6 );
if ( hostname == (char*) 0 )
{
(void) gethostname( hostname_buf, sizeof(hostname_buf) );
hostname = hostname_buf;
}
if ( ! ( gotv4 || gotv6 ) )
{
syslog( LOG_CRIT, "can't find any valid address" );
(void) fprintf( stderr, "%s: can't find any valid address\n", argv0 );
exit( 1 );
}
/* Initialize listen sockets. Try v6 first because of a Linux peculiarity;
** like some other systems, it has magical v6 sockets that also listen for
** v4, but in Linux if you bind a v4 socket first then the v6 bind fails.
*/
if ( gotv6 )
listen6_fd = initialize_listen_socket( &host_addr6 );
else
listen6_fd = -1;
if ( gotv4 )
listen4_fd = initialize_listen_socket( &host_addr4 );
else
listen4_fd = -1;
/* If we didn't get any valid sockets, fail. */
if ( listen4_fd == -1 && listen6_fd == -1 )
{
syslog( LOG_CRIT, "can't bind to any address" );
(void) fprintf( stderr, "%s: can't bind to any address\n", argv0 );
exit( 1 );
}
#ifdef USE_SSL
if ( do_ssl )
{
SSL_load_error_strings();
SSLeay_add_ssl_algorithms();
ssl_ctx = SSL_CTX_new( SSLv23_server_method() );
if ( certfile[0] != '\0' )
if ( SSL_CTX_use_certificate_chain_file( ssl_ctx, certfile ) == 0 ||
SSL_CTX_use_PrivateKey_file( ssl_ctx, certfile, SSL_FILETYPE_PEM ) == 0 ||
SSL_CTX_check_private_key( ssl_ctx ) == 0 )
{
ERR_print_errors_fp( stderr );
exit( 1 );
}
if ( cipher != (char*) 0 )
{
if ( SSL_CTX_set_cipher_list( ssl_ctx, cipher ) == 0 )
{
ERR_print_errors_fp( stderr );
exit( 1 );
}
if ( SSL_CTX_set_options( ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE ) == 0 )
{
ERR_print_errors_fp( stderr );
exit( 1 );
}
}
}
#endif /* USE_SSL */
if ( ! debug )
{
/* Make ourselves a daemon. */
#ifdef HAVE_DAEMON
if ( daemon( 1, 1 ) < 0 )
{
syslog( LOG_CRIT, "daemon - %m" );
perror( "daemon" );
exit( 1 );
}
#else
switch ( fork() )
{
case 0:
break;
case -1:
syslog( LOG_CRIT, "fork - %m" );
perror( "fork" );
exit( 1 );
default:
exit( 0 );
}
#ifdef HAVE_SETSID
(void) setsid();
#endif
#endif
}
else
{
/* Even if we don't daemonize, we still want to disown our parent
** process.
*/
#ifdef HAVE_SETSID
(void) setsid();
#endif /* HAVE_SETSID */
}
if ( pidfile != (char*) 0 )
{
/* Write the PID file. */
FILE* pidfp = fopen( pidfile, "w" );
if ( pidfp == (FILE*) 0 )
{
syslog( LOG_CRIT, "%s - %m", pidfile );
perror( pidfile );
exit( 1 );
}
(void) fprintf( pidfp, "%d\n", (int) getpid() );
(void) fclose( pidfp );
}
/* Read zone info now, in case we chroot(). */
tzset();
/* If we're root, start becoming someone else. */
if ( getuid() == 0 )
{
/* Set aux groups to null. */
if ( setgroups( 0, (gid_t*) 0 ) < 0 )
{
syslog( LOG_CRIT, "setgroups - %m" );
perror( "setgroups" );
exit( 1 );
}
/* Set primary group. */
if ( setgid( gid ) < 0 )
{
syslog( LOG_CRIT, "setgid - %m" );
perror( "setgid" );
exit( 1 );
}
/* Try setting aux groups correctly - not critical if this fails. */
if ( initgroups( user, gid ) < 0 )
{
syslog( LOG_ERR, "initgroups - %m" );
perror( "initgroups" );
}
#ifdef HAVE_SETLOGIN
/* Set login name. */
(void) setlogin( user );
#endif /* HAVE_SETLOGIN */
}
/* Switch directories if requested. */
if ( dir != (char*) 0 )
{
if ( chdir( dir ) < 0 )
{
syslog( LOG_CRIT, "chdir - %m" );
perror( "chdir" );
exit( 1 );
}
}
/* Get current directory. */
(void) getcwd( cwd, sizeof(cwd) - 1 );
if ( cwd[strlen( cwd ) - 1] != '/' )
(void) strcat( cwd, "/" );
/* Chroot if requested. */
if ( do_chroot )
{
if ( chroot( cwd ) < 0 )
{
syslog( LOG_CRIT, "chroot - %m" );
perror( "chroot" );
exit( 1 );
}
/* If we're logging and the logfile's pathname begins with the
** chroot tree's pathname, then elide the chroot pathname so
** that the logfile pathname still works from inside the chroot
** tree.
*/
if ( logfile != (char*) 0 )
if ( strncmp( logfile, cwd, strlen( cwd ) ) == 0 )
{
(void) strcpy( logfile, &logfile[strlen( cwd ) - 1] );
/* (We already guaranteed that cwd ends with a slash, so leaving
** that slash in logfile makes it an absolute pathname within
** the chroot tree.)
*/
}
else
{
syslog( LOG_WARNING, "logfile is not within the chroot tree, you will not be able to re-open it" );
(void) fprintf( stderr, "%s: logfile is not within the chroot tree, you will not be able to re-open it\n", argv0 );
}
/* If we're logging and the errfile's pathname begins with the
** chroot tree's pathname, then elide the chroot pathname so
** that the errfile pathname still works from inside the chroot
** tree.
*/
if ( errfile != (char*) 0 )
if ( strncmp( errfile, cwd, strlen( cwd ) ) == 0 )
{
(void) strcpy( errfile, &errfile[strlen( cwd ) - 1] );
/* (We already guaranteed that cwd ends with a slash, so leaving
** that slash in errfile makes it an absolute pathname within
** the chroot tree.)
*/
}
else
{
syslog( LOG_WARNING, "errfile is not within the chroot tree, you will not be able to re-open it" );
(void) fprintf( stderr, "%s: errfile is not within the chroot tree, you will not be able to re-open it\n", argv0 );
}
(void) strcpy( cwd, "/" );
/* Always chdir to / after a chroot. */
if ( chdir( cwd ) < 0 )
{
syslog( LOG_CRIT, "chroot chdir - %m" );
perror( "chroot chdir" );
exit( 1 );
}
}
/* Switch directories again if requested. */
if ( data_dir != (char*) 0 )
{
if ( chdir( data_dir ) < 0 )
{
syslog( LOG_CRIT, "data_dir chdir - %m" );
perror( "data_dir chdir" );
exit( 1 );
}
}
/* If we're root, become someone else. */
if ( getuid() == 0 )
{
/* Set uid. */
if ( setuid( uid ) < 0 )
{
syslog( LOG_CRIT, "setuid - %m" );
perror( "setuid" );
exit( 1 );
}
/* Check for unnecessary security exposure. */
if ( ! do_chroot )
{
syslog( LOG_WARNING,
"started as root without requesting chroot(), warning only" );
(void) fprintf( stderr,
"%s: started as root without requesting chroot(), warning only\n", argv0 );
}
}
/* Catch various signals. */
#ifdef HAVE_SIGSET
(void) sigset( SIGTERM, handle_sigterm );
(void) sigset( SIGINT, handle_sigterm );
(void) sigset( SIGUSR1, handle_sigterm );
(void) sigset( SIGHUP, handle_sighup );
(void) sigset( SIGCHLD, handle_sigchld );
(void) sigset( SIGPIPE, SIG_IGN );
#else /* HAVE_SIGSET */
(void) signal( SIGTERM, handle_sigterm );
(void) signal( SIGINT, handle_sigterm );
(void) signal( SIGUSR1, handle_sigterm );
(void) signal( SIGHUP, handle_sighup );
(void) signal( SIGCHLD, handle_sigchld );
(void) signal( SIGPIPE, SIG_IGN );
#endif /* HAVE_SIGSET */
got_hup = 0;
init_mime();
if ( hostname == (char*) 0 )
syslog(
LOG_NOTICE, "%.80s starting on port %d", SERVER_SOFTWARE,
(int) port );
else
syslog(
LOG_NOTICE, "%.80s starting on %.80s, port %d", SERVER_SOFTWARE,
hostname, (int) port );
/* Main loop. */
for (;;)
{
/* Do we need to re-open the log file? */
if ( got_hup )
{
re_open_logfile();
got_hup = 0;
}
/* Do a select() on at least one and possibly two listen fds.
** If there's only one listen fd then we could skip the select
** and just do the (blocking) accept(), saving one system call;
** that's what happened up through version 1.18. However there
** is one slight drawback to that method: the blocking accept()
** is not interrupted by a signal call. Since we definitely want
** signals to interrupt a waiting server, we use select() even
** if there's only one fd.
*/
FD_ZERO( &lfdset );
maxfd = -1;
if ( listen4_fd != -1 )
{
FD_SET( listen4_fd, &lfdset );
if ( listen4_fd > maxfd )
maxfd = listen4_fd;
}
if ( listen6_fd != -1 )
{
FD_SET( listen6_fd, &lfdset );
if ( listen6_fd > maxfd )
maxfd = listen6_fd;
}
if ( select( maxfd + 1, &lfdset, (fd_set*) 0, (fd_set*) 0, (struct timeval*) 0 ) < 0 )
{
if ( errno == EINTR || errno == EAGAIN )
continue; /* try again */
syslog( LOG_CRIT, "select - %m" );
perror( "select" );
exit( 1 );
}
/* Accept the new connection. */
sz = sizeof(usa);
if ( listen4_fd != -1 && FD_ISSET( listen4_fd, &lfdset ) )
conn_fd = accept( listen4_fd, &usa.sa, &sz );
else if ( listen6_fd != -1 && FD_ISSET( listen6_fd, &lfdset ) )
conn_fd = accept( listen6_fd, &usa.sa, &sz );
else
{
syslog( LOG_CRIT, "select failure" );
(void) fprintf( stderr, "%s: select failure\n", argv0 );
exit( 1 );
}
if ( conn_fd < 0 )
{
if ( errno == EINTR || errno == EAGAIN )
continue; /* try again */
#ifdef EPROTO
if ( errno == EPROTO )
continue; /* try again */
#endif /* EPROTO */
syslog( LOG_CRIT, "accept - %m" );
perror( "accept" );
exit( 1 );
}
/* Fork a sub-process to handle the connection. */
r = fork();
if ( r < 0 )
{
syslog( LOG_CRIT, "fork - %m" );
perror( "fork" );
exit( 1 );
}
if ( r == 0 )
{
/* Child process. */
client_addr = usa;
if ( listen4_fd != -1 )
(void) close( listen4_fd );
if ( listen6_fd != -1 )
(void) close( listen6_fd );
handle_request();
exit( 0 );
}
(void) close( conn_fd );
}
}
static void
usage( void )
{
#ifdef USE_SSL
(void) fprintf( stderr, "usage: %s [-C configfile] [-D] [-S] [-E certfile] [-Y cipher] [-p port] [-d dir] [-dd data_dir] [-c cgipat] [-u user] [-h hostname] [-r] [-v] [-l logfile] [-ll errfile] [-i pidfile] [-T charset] [-P P3P] [-M maxage]\n", argv0 );
#else /* USE_SSL */
(void) fprintf( stderr, "usage: %s [-C configfile] [-D] [-p port] [-d dir] [-dd data_dir] [-c cgipat] [-u user] [-h hostname] [-r] [-v] [-l logfile] [-ll errfile] [-i pidfile] [-T charset] [-P P3P] [-M maxage]\n", argv0 );
#endif /* USE_SSL */
exit( 1 );
}
static void
read_config( char* filename )
{
FILE* fp;
char line[10000];
char* cp;
char* cp2;
char* name;
char* value;
fp = fopen( filename, "r" );
if ( fp == (FILE*) 0 )
{
syslog( LOG_CRIT, "%s - %m", filename );
perror( filename );
exit( 1 );
}
while ( fgets( line, sizeof(line), fp ) != (char*) 0 )
{
/* Trim comments. */
if ( ( cp = strchr( line, '#' ) ) != (char*) 0 )
*cp = '\0';
/* Skip leading whitespace. */
cp = line;
cp += strspn( cp, " \t\012\015" );
/* Split line into words. */
while ( *cp != '\0' )
{
/* Find next whitespace. */
cp2 = cp + strcspn( cp, " \t\012\015" );
/* Insert EOS and advance next-word pointer. */
while ( *cp2 == ' ' || *cp2 == '\t' || *cp2 == '\012' || *cp2 == '\015' )
*cp2++ = '\0';
/* Split into name and value. */
name = cp;
value = strchr( name, '=' );
if ( value != (char*) 0 )
*value++ = '\0';
/* Interpret. */
if ( strcasecmp( name, "debug" ) == 0 )
{
no_value_required( name, value );
debug = 1;
}
else if ( strcasecmp( name, "port" ) == 0 )
{
value_required( name, value );
port = (unsigned short) atoi( value );
}
else if ( strcasecmp( name, "dir" ) == 0 )
{
value_required( name, value );
dir = e_strdup( value );
}
else if ( strcasecmp( name, "data_dir" ) == 0 )
{
value_required( name, value );