Home | History | Annotate | Line # | Download | only in httpd
main.c revision 1.14
      1  1.14       agc /*	$NetBSD: main.c,v 1.14 2016/05/24 21:18:29 agc Exp $	*/
      2   1.1       mrg 
      3   1.5       mrg /*	$eterna: main.c,v 1.6 2011/11/18 09:21:15 mrg Exp $	*/
      4   1.1       mrg /* from: eterna: bozohttpd.c,v 1.159 2009/05/23 02:14:30 mrg Exp 	*/
      5   1.1       mrg 
      6   1.1       mrg /*
      7   1.7       mrg  * Copyright (c) 1997-2014 Matthew R. Green
      8   1.1       mrg  * All rights reserved.
      9   1.1       mrg  *
     10   1.1       mrg  * Redistribution and use in source and binary forms, with or without
     11   1.1       mrg  * modification, are permitted provided that the following conditions
     12   1.1       mrg  * are met:
     13   1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     14   1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     15   1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       mrg  *    notice, this list of conditions and the following disclaimer and
     17   1.1       mrg  *    dedication in the documentation and/or other materials provided
     18   1.1       mrg  *    with the distribution.
     19   1.1       mrg  *
     20   1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21   1.1       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22   1.1       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23   1.1       mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24   1.1       mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     25   1.1       mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     26   1.1       mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     27   1.1       mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     28   1.1       mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.1       mrg  * SUCH DAMAGE.
     31   1.1       mrg  *
     32   1.1       mrg  */
     33   1.1       mrg 
     34   1.1       mrg /* this program is dedicated to the Great God of Processed Cheese */
     35   1.1       mrg 
     36   1.1       mrg /*
     37   1.1       mrg  * main.c:  C front end to bozohttpd
     38   1.1       mrg  */
     39   1.1       mrg 
     40   1.1       mrg #include <sys/types.h>
     41   1.1       mrg #include <sys/param.h>
     42   1.1       mrg 
     43   1.1       mrg #include <errno.h>
     44   1.1       mrg #include <stdlib.h>
     45   1.1       mrg #include <string.h>
     46   1.1       mrg #include <syslog.h>
     47   1.1       mrg #include <time.h>
     48   1.1       mrg #include <unistd.h>
     49   1.1       mrg 
     50   1.1       mrg #include "bozohttpd.h"
     51   1.1       mrg 
     52   1.1       mrg /* variables and functions */
     53   1.1       mrg #ifndef LOG_FTP
     54   1.1       mrg #define LOG_FTP LOG_DAEMON
     55   1.1       mrg #endif
     56   1.1       mrg 
     57   1.1       mrg /* print a usage message, and then exit */
     58   1.3     joerg BOZO_DEAD static void
     59   1.1       mrg usage(bozohttpd_t *httpd, char *progname)
     60   1.1       mrg {
     61  1.13       mrg 	bozowarn(httpd, "usage: %s [options] slashdir [virtualhostname]",
     62   1.1       mrg 			progname);
     63  1.13       mrg 	bozowarn(httpd, "options:");
     64   1.2      jmmv #ifndef NO_DEBUG
     65  1.13       mrg 	bozowarn(httpd, "   -d\t\t\tenable debug support");
     66   1.1       mrg #endif
     67  1.13       mrg 	bozowarn(httpd, "   -s\t\t\talways log to stderr");
     68   1.9       shm #ifndef NO_DYNAMIC_CONTENT
     69  1.13       mrg 	bozowarn(httpd, "   -M arg t c c11\tadd this mime extenstion");
     70   1.9       shm #endif
     71   1.1       mrg #ifndef NO_USER_SUPPORT
     72  1.13       mrg 	bozowarn(httpd, "   -u\t\t\tenable ~user/public_html support");
     73  1.13       mrg 	bozowarn(httpd, "   -p dir\t\tchange `public_html' directory name");
     74   1.9       shm #ifndef NO_CGIBIN_SUPPORT
     75  1.13       mrg 	bozowarn(httpd, "   -E\t\t\tenable CGI support for user dirs");
     76   1.1       mrg #endif
     77   1.1       mrg #endif
     78   1.1       mrg #ifndef NO_CGIBIN_SUPPORT
     79   1.1       mrg #ifndef NO_DYNAMIC_CONTENT
     80  1.13       mrg 	bozowarn(httpd, "   -C arg prog\t\tadd this CGI handler");
     81   1.1       mrg #endif
     82  1.13       mrg 	bozowarn(httpd,
     83   1.1       mrg 		"   -c cgibin\t\tenable cgi-bin support in this directory");
     84   1.1       mrg #endif
     85   1.6   mbalmer #ifndef NO_LUA_SUPPORT
     86  1.13       mrg 	bozowarn(httpd, "   -L arg script\tadd this Lua script");
     87   1.6   mbalmer #endif
     88  1.13       mrg 	bozowarn(httpd, "   -I port\t\tbind or use on this port");
     89   1.1       mrg #ifndef NO_DAEMON_MODE
     90  1.13       mrg 	bozowarn(httpd, "   -b\t\t\tbackground and go into daemon mode");
     91  1.13       mrg 	bozowarn(httpd, "   -f\t\t\tkeep daemon mode in the foreground");
     92  1.13       mrg 	bozowarn(httpd,
     93   1.1       mrg 		"   -i address\t\tbind on this address (daemon mode only)");
     94  1.13       mrg 	bozowarn(httpd, "   -P pidfile\t\tpath to the pid file to create");
     95   1.1       mrg #endif
     96  1.13       mrg 	bozowarn(httpd, "   -S version\t\tset server version string");
     97  1.13       mrg 	bozowarn(httpd, "   -t dir\t\tchroot to `dir'");
     98  1.13       mrg 	bozowarn(httpd, "   -U username\t\tchange user to `user'");
     99  1.13       mrg 	bozowarn(httpd,
    100   1.1       mrg 		"   -e\t\t\tdon't clean the environment (-t and -U only)");
    101  1.13       mrg 	bozowarn(httpd,
    102   1.1       mrg 		"   -v virtualroot\tenable virtual host support "
    103   1.1       mrg 		"in this directory");
    104   1.1       mrg #ifndef NO_DIRINDEX_SUPPORT
    105  1.13       mrg 	bozowarn(httpd,
    106   1.1       mrg 		"   -X\t\t\tenable automatic directory index support");
    107  1.13       mrg 	bozowarn(httpd,
    108   1.1       mrg 		"   -H\t\t\thide files starting with a period (.)"
    109   1.1       mrg 		" in index mode");
    110   1.1       mrg #endif
    111  1.13       mrg 	bozowarn(httpd,
    112   1.1       mrg 		"   -x index\t\tchange default `index.html' file name");
    113   1.1       mrg #ifndef NO_SSL_SUPPORT
    114  1.13       mrg 	bozowarn(httpd,
    115  1.11  christos 		"   -z ciphers\t\tspecify SSL ciphers");
    116  1.13       mrg 	bozowarn(httpd,
    117   1.1       mrg 		"   -Z cert privkey\tspecify path to server certificate"
    118   1.1       mrg 			" and private key file\n"
    119   1.1       mrg 		"\t\t\tin pem format and enable bozohttpd in SSL mode");
    120   1.1       mrg #endif /* NO_SSL_SUPPORT */
    121  1.14       agc 	bozowarn(httpd, "   -G print version number and exit");
    122  1.13       mrg 	bozoerr(httpd, 1, "%s failed to start", progname);
    123   1.1       mrg }
    124   1.1       mrg 
    125   1.1       mrg int
    126   1.1       mrg main(int argc, char **argv)
    127   1.1       mrg {
    128   1.1       mrg 	bozo_httpreq_t	*request;
    129   1.1       mrg 	bozohttpd_t	 httpd;
    130   1.1       mrg 	bozoprefs_t	 prefs;
    131   1.1       mrg 	char		*progname;
    132  1.12       mrg 	const char	*val;
    133   1.1       mrg 	int		 c;
    134   1.1       mrg 
    135   1.1       mrg 	(void) memset(&httpd, 0x0, sizeof(httpd));
    136   1.1       mrg 	(void) memset(&prefs, 0x0, sizeof(prefs));
    137   1.1       mrg 
    138   1.1       mrg 	if ((progname = strrchr(argv[0], '/')) == NULL)
    139   1.1       mrg 		progname = argv[0];
    140   1.1       mrg 	else
    141   1.1       mrg 		progname++;
    142   1.1       mrg 
    143   1.1       mrg 	openlog(progname, LOG_PID|LOG_NDELAY, LOG_FTP);
    144   1.1       mrg 
    145   1.1       mrg 	bozo_set_defaults(&httpd, &prefs);
    146   1.1       mrg 
    147   1.9       shm 	/*
    148   1.9       shm 	 * -r option was removed, do not reuse it for a while
    149   1.9       shm 	 */
    150   1.9       shm 
    151   1.1       mrg 	while ((c = getopt(argc, argv,
    152  1.14       agc 	    "C:EGHI:L:M:P:S:U:VXZ:bc:defhi:np:st:uv:x:z:")) != -1) {
    153   1.9       shm 		switch (c) {
    154   1.1       mrg 
    155   1.6   mbalmer 		case 'L':
    156   1.6   mbalmer #ifdef NO_LUA_SUPPORT
    157  1.13       mrg 			bozoerr(&httpd, 1,
    158   1.6   mbalmer 				"Lua support is not enabled");
    159   1.6   mbalmer 			/* NOTREACHED */
    160   1.6   mbalmer #else
    161   1.6   mbalmer 			/* make sure there's two argument */
    162   1.6   mbalmer 			if (argc - optind < 1)
    163   1.6   mbalmer 				usage(&httpd, progname);
    164   1.6   mbalmer 			bozo_add_lua_map(&httpd, optarg, argv[optind]);
    165   1.6   mbalmer 			optind++;
    166   1.6   mbalmer 			break;
    167   1.6   mbalmer #endif /* NO_LUA_SUPPORT */
    168   1.1       mrg 		case 'M':
    169   1.1       mrg #ifdef NO_DYNAMIC_CONTENT
    170  1.13       mrg 			bozoerr(&httpd, 1,
    171   1.1       mrg 				"dynamic mime content support is not enabled");
    172   1.1       mrg 			/* NOTREACHED */
    173   1.1       mrg #else
    174   1.1       mrg 			/* make sure there's four arguments */
    175   1.1       mrg 			if (argc - optind < 3)
    176   1.1       mrg 				usage(&httpd, progname);
    177   1.1       mrg 			bozo_add_content_map_mime(&httpd, optarg, argv[optind],
    178   1.1       mrg 			    argv[optind+1], argv[optind+2]);
    179   1.1       mrg 			optind += 3;
    180   1.1       mrg 			break;
    181   1.1       mrg #endif /* NO_DYNAMIC_CONTENT */
    182   1.1       mrg 
    183   1.1       mrg 		case 'n':
    184  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "numeric", "true");
    185   1.1       mrg 			break;
    186   1.1       mrg 
    187   1.1       mrg 		case 's':
    188  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "log to stderr", "true");
    189   1.1       mrg 			break;
    190   1.1       mrg 
    191   1.1       mrg 		case 'S':
    192  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "server software",
    193  1.12       mrg 				      optarg);
    194   1.1       mrg 			break;
    195   1.1       mrg 		case 'Z':
    196   1.1       mrg #ifdef NO_SSL_SUPPORT
    197  1.13       mrg 			bozoerr(&httpd, 1, "ssl support is not enabled");
    198   1.1       mrg 			/* NOT REACHED */
    199   1.1       mrg #else
    200   1.1       mrg 			/* make sure there's two arguments */
    201   1.1       mrg 			if (argc - optind < 1)
    202   1.1       mrg 				usage(&httpd, progname);
    203   1.1       mrg 			bozo_ssl_set_opts(&httpd, optarg, argv[optind++]);
    204   1.1       mrg 			break;
    205   1.1       mrg #endif /* NO_SSL_SUPPORT */
    206  1.11  christos 
    207  1.11  christos 		case 'z':
    208  1.11  christos #ifdef NO_SSL_SUPPORT
    209  1.13       mrg 			bozoerr(&httpd, 1, "ssl support is not enabled");
    210  1.11  christos 			/* NOT REACHED */
    211  1.11  christos #else
    212  1.11  christos 			bozo_ssl_set_ciphers(&httpd, optarg);
    213  1.11  christos 			break;
    214  1.11  christos #endif /* NO_SSL_SUPPORT */
    215  1.11  christos 
    216   1.1       mrg 		case 'U':
    217  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "username", optarg);
    218   1.1       mrg 			break;
    219   1.1       mrg 
    220   1.1       mrg 		case 'V':
    221  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "unknown slash", "true");
    222   1.1       mrg 			break;
    223   1.1       mrg 
    224   1.1       mrg 		case 'v':
    225  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "virtual base", optarg);
    226   1.1       mrg 			break;
    227   1.1       mrg 
    228   1.1       mrg 		case 'x':
    229  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "index.html", optarg);
    230   1.1       mrg 			break;
    231   1.1       mrg 
    232   1.4       mrg 		case 'I':
    233  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "port number", optarg);
    234   1.4       mrg 			break;
    235   1.4       mrg 
    236   1.1       mrg #ifdef NO_DAEMON_MODE
    237   1.1       mrg 		case 'b':
    238   1.1       mrg 		case 'e':
    239   1.1       mrg 		case 'f':
    240   1.1       mrg 		case 'i':
    241   1.2      jmmv 		case 'P':
    242  1.13       mrg 			bozoerr(&httpd, 1, "Daemon mode is not enabled");
    243   1.1       mrg 			/* NOTREACHED */
    244   1.1       mrg #else
    245   1.1       mrg 		case 'b':
    246   1.1       mrg 			/*
    247   1.1       mrg 			 * test suite support - undocumented
    248   1.1       mrg 			 * background == 2 (aka, -b -b) means to
    249   1.1       mrg 			 * only process 1 per kid
    250   1.1       mrg 			 */
    251  1.12       mrg 			val = bozo_get_pref(&prefs, "background") == NULL ?
    252  1.12       mrg 			    "1" : "2";
    253  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "background", val);
    254   1.1       mrg 			break;
    255   1.1       mrg 
    256   1.1       mrg 		case 'e':
    257  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "dirty environment",
    258  1.12       mrg 				      "true");
    259   1.1       mrg 			break;
    260   1.1       mrg 
    261   1.1       mrg 		case 'f':
    262  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "foreground", "true");
    263   1.1       mrg 			break;
    264   1.1       mrg 
    265   1.1       mrg 		case 'i':
    266  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "bind address", optarg);
    267   1.1       mrg 			break;
    268   1.1       mrg 
    269   1.2      jmmv 		case 'P':
    270  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "pid file", optarg);
    271   1.2      jmmv 			break;
    272   1.1       mrg #endif /* NO_DAEMON_MODE */
    273   1.1       mrg 
    274   1.1       mrg #ifdef NO_CGIBIN_SUPPORT
    275   1.1       mrg 		case 'c':
    276   1.1       mrg 		case 'C':
    277  1.13       mrg 			bozoerr(&httpd, 1, "CGI is not enabled");
    278   1.1       mrg 			/* NOTREACHED */
    279   1.1       mrg #else
    280   1.1       mrg 		case 'c':
    281   1.1       mrg 			bozo_cgi_setbin(&httpd, optarg);
    282   1.1       mrg 			break;
    283   1.1       mrg 
    284   1.1       mrg 		case 'C':
    285   1.1       mrg #  ifdef NO_DYNAMIC_CONTENT
    286  1.13       mrg 			bozoerr(&httpd, 1,
    287   1.1       mrg 				"dynamic CGI handler support is not enabled");
    288   1.1       mrg 			/* NOTREACHED */
    289   1.1       mrg #  else
    290   1.1       mrg 			/* make sure there's two arguments */
    291   1.1       mrg 			if (argc - optind < 1)
    292   1.1       mrg 				usage(&httpd, progname);
    293   1.1       mrg 			bozo_add_content_map_cgi(&httpd, optarg,
    294   1.1       mrg 					argv[optind++]);
    295   1.1       mrg 			break;
    296   1.1       mrg #  endif /* NO_DYNAMIC_CONTENT */
    297   1.1       mrg #endif /* NO_CGIBIN_SUPPORT */
    298   1.1       mrg 
    299   1.1       mrg 		case 'd':
    300   1.1       mrg 			httpd.debug++;
    301   1.2      jmmv #ifdef NO_DEBUG
    302   1.1       mrg 			if (httpd.debug == 1)
    303  1.13       mrg 				bozowarn(&httpd, "Debugging is not enabled");
    304   1.2      jmmv #endif /* NO_DEBUG */
    305   1.1       mrg 			break;
    306   1.1       mrg 
    307   1.8       mrg 		case 't':
    308  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "chroot dir", optarg);
    309   1.8       mrg 			break;
    310   1.8       mrg 
    311   1.1       mrg #ifdef NO_USER_SUPPORT
    312   1.1       mrg 		case 'p':
    313   1.1       mrg 		case 'u':
    314   1.9       shm 		case 'E':
    315  1.13       mrg 			bozoerr(&httpd, 1, "User support is not enabled");
    316   1.1       mrg 			/* NOTREACHED */
    317   1.1       mrg #else
    318   1.1       mrg 		case 'p':
    319  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "public_html", optarg);
    320   1.1       mrg 			break;
    321   1.1       mrg 
    322   1.1       mrg 		case 'u':
    323  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "enable users", "true");
    324   1.1       mrg 			break;
    325   1.9       shm #ifndef NO_CGIBIN_SUPPORT
    326   1.9       shm 		case 'E':
    327  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "enable user cgibin",
    328  1.12       mrg 				      "true");
    329   1.9       shm 			break;
    330   1.9       shm #else
    331   1.9       shm 		case 'E':
    332  1.13       mrg 			bozoerr(&httpd, 1, "CGI is not enabled");
    333   1.9       shm 			/* NOTREACHED */
    334   1.9       shm #endif /* NO_CGIBIN_SPPORT */
    335   1.1       mrg #endif /* NO_USER_SUPPORT */
    336   1.1       mrg 
    337   1.1       mrg #ifdef NO_DIRINDEX_SUPPORT
    338   1.1       mrg 		case 'H':
    339   1.1       mrg 		case 'X':
    340  1.13       mrg 			bozoerr(&httpd, 1,
    341   1.1       mrg 				"directory indexing is not enabled");
    342   1.1       mrg 			/* NOTREACHED */
    343   1.1       mrg #else
    344   1.1       mrg 		case 'H':
    345  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "hide dots", "true");
    346   1.1       mrg 			break;
    347   1.1       mrg 
    348   1.1       mrg 		case 'X':
    349  1.12       mrg 			bozo_set_pref(&httpd, &prefs, "directory indexing",
    350  1.12       mrg 				      "true");
    351   1.1       mrg 			break;
    352   1.1       mrg 
    353   1.1       mrg #endif /* NO_DIRINDEX_SUPPORT */
    354   1.1       mrg 
    355  1.14       agc 		case 'G':
    356  1.14       agc 			{
    357  1.14       agc 				char	version[128];
    358  1.14       agc 
    359  1.14       agc 				bozo_get_version(version, sizeof(version));
    360  1.14       agc 				printf("bozohttpd version %s\n", version);
    361  1.14       agc 			}
    362  1.14       agc 			return 0;
    363  1.14       agc 
    364   1.1       mrg 		default:
    365   1.1       mrg 			usage(&httpd, progname);
    366   1.1       mrg 			/* NOTREACHED */
    367   1.1       mrg 		}
    368   1.1       mrg 	}
    369   1.1       mrg 
    370   1.1       mrg 	argc -= optind;
    371   1.1       mrg 	argv += optind;
    372   1.1       mrg 
    373   1.1       mrg 	if (argc == 0 || argc > 2) {
    374   1.1       mrg 		usage(&httpd, progname);
    375   1.1       mrg 	}
    376   1.1       mrg 
    377   1.1       mrg 	/* virtual host, and root of tree to serve */
    378   1.1       mrg 	bozo_setup(&httpd, &prefs, argv[1], argv[0]);
    379   1.1       mrg 
    380   1.1       mrg 	/*
    381   1.1       mrg 	 * read and process the HTTP request.
    382   1.1       mrg 	 */
    383   1.1       mrg 	do {
    384   1.1       mrg 		if ((request = bozo_read_request(&httpd)) != NULL) {
    385   1.1       mrg 			bozo_process_request(request);
    386   1.1       mrg 			bozo_clean_request(request);
    387   1.1       mrg 		}
    388   1.1       mrg 	} while (httpd.background);
    389   1.1       mrg 
    390   1.1       mrg 	return (0);
    391   1.1       mrg }
    392