Home | History | Annotate | Line # | Download | only in httpd
main.c revision 1.11
      1  1.11  christos /*	$NetBSD: main.c,v 1.11 2015/12/12 16:57:53 christos 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.1       mrg 	bozo_warn(httpd, "usage: %s [options] slashdir [virtualhostname]",
     62   1.1       mrg 			progname);
     63   1.1       mrg 	bozo_warn(httpd, "options:");
     64   1.2      jmmv #ifndef NO_DEBUG
     65   1.1       mrg 	bozo_warn(httpd, "   -d\t\t\tenable debug support");
     66   1.1       mrg #endif
     67   1.1       mrg 	bozo_warn(httpd, "   -s\t\t\talways log to stderr");
     68   1.9       shm #ifndef NO_DYNAMIC_CONTENT
     69   1.9       shm 	bozo_warn(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.1       mrg 	bozo_warn(httpd, "   -u\t\t\tenable ~user/public_html support");
     73   1.9       shm 	bozo_warn(httpd, "   -p dir\t\tchange `public_html' directory name");
     74   1.9       shm #ifndef NO_CGIBIN_SUPPORT
     75   1.9       shm 	bozo_warn(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.1       mrg 	bozo_warn(httpd, "   -C arg prog\t\tadd this CGI handler");
     81   1.1       mrg #endif
     82   1.1       mrg 	bozo_warn(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.6   mbalmer 	bozo_warn(httpd, "   -L arg script\tadd this Lua script");
     87   1.6   mbalmer #endif
     88   1.5       mrg 	bozo_warn(httpd, "   -I port\t\tbind or use on this port");
     89   1.1       mrg #ifndef NO_DAEMON_MODE
     90   1.1       mrg 	bozo_warn(httpd, "   -b\t\t\tbackground and go into daemon mode");
     91   1.1       mrg 	bozo_warn(httpd, "   -f\t\t\tkeep daemon mode in the foreground");
     92   1.1       mrg 	bozo_warn(httpd,
     93   1.1       mrg 		"   -i address\t\tbind on this address (daemon mode only)");
     94   1.2      jmmv 	bozo_warn(httpd, "   -P pidfile\t\tpath to the pid file to create");
     95   1.1       mrg #endif
     96   1.1       mrg 	bozo_warn(httpd, "   -S version\t\tset server version string");
     97   1.1       mrg 	bozo_warn(httpd, "   -t dir\t\tchroot to `dir'");
     98   1.1       mrg 	bozo_warn(httpd, "   -U username\t\tchange user to `user'");
     99   1.1       mrg 	bozo_warn(httpd,
    100   1.1       mrg 		"   -e\t\t\tdon't clean the environment (-t and -U only)");
    101   1.1       mrg 	bozo_warn(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.1       mrg 	bozo_warn(httpd,
    106   1.1       mrg 		"   -X\t\t\tenable automatic directory index support");
    107   1.1       mrg 	bozo_warn(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.1       mrg 	bozo_warn(httpd,
    112   1.1       mrg 		"   -x index\t\tchange default `index.html' file name");
    113   1.1       mrg #ifndef NO_SSL_SUPPORT
    114   1.1       mrg 	bozo_warn(httpd,
    115  1.11  christos 		"   -z ciphers\t\tspecify SSL ciphers");
    116  1.11  christos 	bozo_warn(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.1       mrg 	bozo_err(httpd, 1, "%s failed to start", progname);
    122   1.1       mrg }
    123   1.1       mrg 
    124   1.1       mrg int
    125   1.1       mrg main(int argc, char **argv)
    126   1.1       mrg {
    127   1.1       mrg 	bozo_httpreq_t	*request;
    128   1.1       mrg 	bozohttpd_t	 httpd;
    129   1.1       mrg 	bozoprefs_t	 prefs;
    130   1.1       mrg 	char		*progname;
    131   1.1       mrg 	int		 c;
    132   1.1       mrg 
    133   1.1       mrg 	(void) memset(&httpd, 0x0, sizeof(httpd));
    134   1.1       mrg 	(void) memset(&prefs, 0x0, sizeof(prefs));
    135   1.1       mrg 
    136   1.1       mrg 	if ((progname = strrchr(argv[0], '/')) == NULL)
    137   1.1       mrg 		progname = argv[0];
    138   1.1       mrg 	else
    139   1.1       mrg 		progname++;
    140   1.1       mrg 
    141   1.1       mrg 	openlog(progname, LOG_PID|LOG_NDELAY, LOG_FTP);
    142   1.1       mrg 
    143   1.1       mrg 	bozo_set_defaults(&httpd, &prefs);
    144   1.1       mrg 
    145   1.9       shm 	/*
    146   1.9       shm 	 * -r option was removed, do not reuse it for a while
    147   1.9       shm 	 */
    148   1.9       shm 
    149   1.1       mrg 	while ((c = getopt(argc, argv,
    150  1.11  christos 	    "C:EHI:L:M:P:S:U:VXZ:bc:defhi:np:st:uv:x:z:")) != -1) {
    151   1.9       shm 		switch (c) {
    152   1.1       mrg 
    153   1.6   mbalmer 		case 'L':
    154   1.6   mbalmer #ifdef NO_LUA_SUPPORT
    155   1.6   mbalmer 			bozo_err(&httpd, 1,
    156   1.6   mbalmer 				"Lua support is not enabled");
    157   1.6   mbalmer 			/* NOTREACHED */
    158   1.6   mbalmer #else
    159   1.6   mbalmer 			/* make sure there's two argument */
    160   1.6   mbalmer 			if (argc - optind < 1)
    161   1.6   mbalmer 				usage(&httpd, progname);
    162   1.6   mbalmer 			bozo_add_lua_map(&httpd, optarg, argv[optind]);
    163   1.6   mbalmer 			optind++;
    164   1.6   mbalmer 			break;
    165   1.6   mbalmer #endif /* NO_LUA_SUPPORT */
    166   1.1       mrg 		case 'M':
    167   1.1       mrg #ifdef NO_DYNAMIC_CONTENT
    168   1.1       mrg 			bozo_err(&httpd, 1,
    169   1.1       mrg 				"dynamic mime content support is not enabled");
    170   1.1       mrg 			/* NOTREACHED */
    171   1.1       mrg #else
    172   1.1       mrg 			/* make sure there's four arguments */
    173   1.1       mrg 			if (argc - optind < 3)
    174   1.1       mrg 				usage(&httpd, progname);
    175   1.1       mrg 			bozo_add_content_map_mime(&httpd, optarg, argv[optind],
    176   1.1       mrg 			    argv[optind+1], argv[optind+2]);
    177   1.1       mrg 			optind += 3;
    178   1.1       mrg 			break;
    179   1.1       mrg #endif /* NO_DYNAMIC_CONTENT */
    180   1.1       mrg 
    181   1.1       mrg 		case 'n':
    182   1.1       mrg 			bozo_set_pref(&prefs, "numeric", "true");
    183   1.1       mrg 			break;
    184   1.1       mrg 
    185   1.1       mrg 		case 's':
    186   1.1       mrg 			bozo_set_pref(&prefs, "log to stderr", "true");
    187   1.1       mrg 			break;
    188   1.1       mrg 
    189   1.1       mrg 		case 'S':
    190   1.1       mrg 			bozo_set_pref(&prefs, "server software", optarg);
    191   1.1       mrg 			break;
    192   1.1       mrg 		case 'Z':
    193   1.1       mrg #ifdef NO_SSL_SUPPORT
    194   1.1       mrg 			bozo_err(&httpd, 1, "ssl support is not enabled");
    195   1.1       mrg 			/* NOT REACHED */
    196   1.1       mrg #else
    197   1.1       mrg 			/* make sure there's two arguments */
    198   1.1       mrg 			if (argc - optind < 1)
    199   1.1       mrg 				usage(&httpd, progname);
    200   1.1       mrg 			bozo_ssl_set_opts(&httpd, optarg, argv[optind++]);
    201   1.1       mrg 			break;
    202   1.1       mrg #endif /* NO_SSL_SUPPORT */
    203  1.11  christos 
    204  1.11  christos 		case 'z':
    205  1.11  christos #ifdef NO_SSL_SUPPORT
    206  1.11  christos 			bozo_err(&httpd, 1, "ssl support is not enabled");
    207  1.11  christos 			/* NOT REACHED */
    208  1.11  christos #else
    209  1.11  christos 			bozo_ssl_set_ciphers(&httpd, optarg);
    210  1.11  christos 			break;
    211  1.11  christos #endif /* NO_SSL_SUPPORT */
    212  1.11  christos 
    213   1.1       mrg 		case 'U':
    214   1.1       mrg 			bozo_set_pref(&prefs, "username", optarg);
    215   1.1       mrg 			break;
    216   1.1       mrg 
    217   1.1       mrg 		case 'V':
    218   1.1       mrg 			bozo_set_pref(&prefs, "unknown slash", "true");
    219   1.1       mrg 			break;
    220   1.1       mrg 
    221   1.1       mrg 		case 'v':
    222   1.1       mrg 			bozo_set_pref(&prefs, "virtual base", optarg);
    223   1.1       mrg 			break;
    224   1.1       mrg 
    225   1.1       mrg 		case 'x':
    226   1.1       mrg 			bozo_set_pref(&prefs, "index.html", optarg);
    227   1.1       mrg 			break;
    228   1.1       mrg 
    229   1.4       mrg 		case 'I':
    230   1.4       mrg 			bozo_set_pref(&prefs, "port number", optarg);
    231   1.4       mrg 			break;
    232   1.4       mrg 
    233   1.1       mrg #ifdef NO_DAEMON_MODE
    234   1.1       mrg 		case 'b':
    235   1.1       mrg 		case 'e':
    236   1.1       mrg 		case 'f':
    237   1.1       mrg 		case 'i':
    238   1.2      jmmv 		case 'P':
    239   1.1       mrg 			bozo_err(&httpd, 1, "Daemon mode is not enabled");
    240   1.1       mrg 			/* NOTREACHED */
    241   1.1       mrg #else
    242   1.1       mrg 		case 'b':
    243   1.1       mrg 			/*
    244   1.1       mrg 			 * test suite support - undocumented
    245   1.1       mrg 			 * background == 2 (aka, -b -b) means to
    246   1.1       mrg 			 * only process 1 per kid
    247   1.1       mrg 			 */
    248   1.1       mrg 			if (bozo_get_pref(&prefs, "background") == NULL) {
    249   1.1       mrg 				bozo_set_pref(&prefs, "background", "1");
    250   1.1       mrg 			} else {
    251   1.1       mrg 				bozo_set_pref(&prefs, "background", "2");
    252   1.1       mrg 			}
    253   1.1       mrg 			break;
    254   1.1       mrg 
    255   1.1       mrg 		case 'e':
    256   1.1       mrg 			bozo_set_pref(&prefs, "dirty environment", "true");
    257   1.1       mrg 			break;
    258   1.1       mrg 
    259   1.1       mrg 		case 'f':
    260   1.1       mrg 			bozo_set_pref(&prefs, "foreground", "true");
    261   1.1       mrg 			break;
    262   1.1       mrg 
    263   1.1       mrg 		case 'i':
    264   1.1       mrg 			bozo_set_pref(&prefs, "bind address", optarg);
    265   1.1       mrg 			break;
    266   1.1       mrg 
    267   1.2      jmmv 		case 'P':
    268   1.2      jmmv 			bozo_set_pref(&prefs, "pid file", optarg);
    269   1.2      jmmv 			break;
    270   1.1       mrg #endif /* NO_DAEMON_MODE */
    271   1.1       mrg 
    272   1.1       mrg #ifdef NO_CGIBIN_SUPPORT
    273   1.1       mrg 		case 'c':
    274   1.1       mrg 		case 'C':
    275   1.1       mrg 			bozo_err(&httpd, 1, "CGI is not enabled");
    276   1.1       mrg 			/* NOTREACHED */
    277   1.1       mrg #else
    278   1.1       mrg 		case 'c':
    279   1.1       mrg 			bozo_cgi_setbin(&httpd, optarg);
    280   1.1       mrg 			break;
    281   1.1       mrg 
    282   1.1       mrg 		case 'C':
    283   1.1       mrg #  ifdef NO_DYNAMIC_CONTENT
    284   1.1       mrg 			bozo_err(&httpd, 1,
    285   1.1       mrg 				"dynamic CGI handler support is not enabled");
    286   1.1       mrg 			/* NOTREACHED */
    287   1.1       mrg #  else
    288   1.1       mrg 			/* make sure there's two arguments */
    289   1.1       mrg 			if (argc - optind < 1)
    290   1.1       mrg 				usage(&httpd, progname);
    291   1.1       mrg 			bozo_add_content_map_cgi(&httpd, optarg,
    292   1.1       mrg 					argv[optind++]);
    293   1.1       mrg 			break;
    294   1.1       mrg #  endif /* NO_DYNAMIC_CONTENT */
    295   1.1       mrg #endif /* NO_CGIBIN_SUPPORT */
    296   1.1       mrg 
    297   1.1       mrg 		case 'd':
    298   1.1       mrg 			httpd.debug++;
    299   1.2      jmmv #ifdef NO_DEBUG
    300   1.1       mrg 			if (httpd.debug == 1)
    301   1.1       mrg 				bozo_warn(&httpd, "Debugging is not enabled");
    302   1.2      jmmv #endif /* NO_DEBUG */
    303   1.1       mrg 			break;
    304   1.1       mrg 
    305   1.8       mrg 		case 't':
    306   1.8       mrg 			bozo_set_pref(&prefs, "chroot dir", optarg);
    307   1.8       mrg 			break;
    308   1.8       mrg 
    309   1.1       mrg #ifdef NO_USER_SUPPORT
    310   1.1       mrg 		case 'p':
    311   1.1       mrg 		case 'u':
    312   1.9       shm 		case 'E':
    313   1.1       mrg 			bozo_err(&httpd, 1, "User support is not enabled");
    314   1.1       mrg 			/* NOTREACHED */
    315   1.1       mrg #else
    316   1.1       mrg 		case 'p':
    317   1.1       mrg 			bozo_set_pref(&prefs, "public_html", optarg);
    318   1.1       mrg 			break;
    319   1.1       mrg 
    320   1.1       mrg 		case 'u':
    321   1.1       mrg 			bozo_set_pref(&prefs, "enable users", "true");
    322   1.1       mrg 			break;
    323   1.9       shm #ifndef NO_CGIBIN_SUPPORT
    324   1.9       shm 		case 'E':
    325   1.9       shm 			bozo_set_pref(&prefs, "enable user cgibin", "true");
    326   1.9       shm 			break;
    327   1.9       shm #else
    328   1.9       shm 		case 'E':
    329   1.9       shm 			bozo_err(&httpd, 1, "CGI is not enabled");
    330   1.9       shm 			/* NOTREACHED */
    331   1.9       shm #endif /* NO_CGIBIN_SPPORT */
    332   1.1       mrg #endif /* NO_USER_SUPPORT */
    333   1.1       mrg 
    334   1.1       mrg #ifdef NO_DIRINDEX_SUPPORT
    335   1.1       mrg 		case 'H':
    336   1.1       mrg 		case 'X':
    337   1.1       mrg 			bozo_err(&httpd, 1,
    338   1.1       mrg 				"directory indexing is not enabled");
    339   1.1       mrg 			/* NOTREACHED */
    340   1.1       mrg #else
    341   1.1       mrg 		case 'H':
    342   1.1       mrg 			bozo_set_pref(&prefs, "hide dots", "true");
    343   1.1       mrg 			break;
    344   1.1       mrg 
    345   1.1       mrg 		case 'X':
    346   1.1       mrg 			bozo_set_pref(&prefs, "directory indexing", "true");
    347   1.1       mrg 			break;
    348   1.1       mrg 
    349   1.1       mrg #endif /* NO_DIRINDEX_SUPPORT */
    350   1.1       mrg 
    351   1.1       mrg 		default:
    352   1.1       mrg 			usage(&httpd, progname);
    353   1.1       mrg 			/* NOTREACHED */
    354   1.1       mrg 		}
    355   1.1       mrg 	}
    356   1.1       mrg 
    357   1.1       mrg 	argc -= optind;
    358   1.1       mrg 	argv += optind;
    359   1.1       mrg 
    360   1.1       mrg 	if (argc == 0 || argc > 2) {
    361   1.1       mrg 		usage(&httpd, progname);
    362   1.1       mrg 	}
    363   1.1       mrg 
    364   1.1       mrg 	/* virtual host, and root of tree to serve */
    365   1.1       mrg 	bozo_setup(&httpd, &prefs, argv[1], argv[0]);
    366   1.1       mrg 
    367   1.1       mrg 	/*
    368   1.1       mrg 	 * read and process the HTTP request.
    369   1.1       mrg 	 */
    370   1.1       mrg 	do {
    371   1.1       mrg 		if ((request = bozo_read_request(&httpd)) != NULL) {
    372   1.1       mrg 			bozo_process_request(request);
    373   1.1       mrg 			bozo_clean_request(request);
    374   1.1       mrg 		}
    375   1.1       mrg 	} while (httpd.background);
    376   1.1       mrg 
    377   1.1       mrg 	return (0);
    378   1.1       mrg }
    379