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