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