1 /* $NetBSD: main.c,v 1.8 2014/07/16 07:41:43 mrg Exp $ */
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 */
7 * Copyright (c) 1997-2014 Matthew R. Green
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
34 /* this program is dedicated to the Great God of Processed Cheese */
37 * main.c: C front end to bozohttpd
40 #include <sys/types.h>
41 #include <sys/param.h>
50 #include "bozohttpd.h"
52 /* variables and functions */
54 #define LOG_FTP LOG_DAEMON
57 /* print a usage message, and then exit */
59 usage(bozohttpd_t
*httpd
, char *progname
)
61 bozo_warn(httpd
, "usage: %s [options] slashdir [virtualhostname]",
63 bozo_warn(httpd
, "options:");
65 bozo_warn(httpd
, " -d\t\t\tenable debug support");
67 bozo_warn(httpd
, " -s\t\t\talways log to stderr");
68 #ifndef NO_USER_SUPPORT
69 bozo_warn(httpd
, " -u\t\t\tenable ~user/public_html support");
70 bozo_warn(httpd
, " -p dir\t\tchange `public_html' directory name]");
72 #ifndef NO_DYNAMIC_CONTENT
73 bozo_warn(httpd
, " -M arg t c c11\tadd this mime extenstion");
75 #ifndef NO_CGIBIN_SUPPORT
76 #ifndef NO_DYNAMIC_CONTENT
77 bozo_warn(httpd
, " -C arg prog\t\tadd this CGI handler");
80 " -c cgibin\t\tenable cgi-bin support in this directory");
82 #ifndef NO_LUA_SUPPORT
83 bozo_warn(httpd
, " -L arg script\tadd this Lua script");
85 bozo_warn(httpd
, " -I port\t\tbind or use on this port");
86 #ifndef NO_DAEMON_MODE
87 bozo_warn(httpd
, " -b\t\t\tbackground and go into daemon mode");
88 bozo_warn(httpd
, " -f\t\t\tkeep daemon mode in the foreground");
90 " -i address\t\tbind on this address (daemon mode only)");
91 bozo_warn(httpd
, " -P pidfile\t\tpath to the pid file to create");
93 bozo_warn(httpd
, " -S version\t\tset server version string");
94 bozo_warn(httpd
, " -t dir\t\tchroot to `dir'");
95 bozo_warn(httpd
, " -U username\t\tchange user to `user'");
97 " -e\t\t\tdon't clean the environment (-t and -U only)");
99 " -v virtualroot\tenable virtual host support "
100 "in this directory");
102 " -r\t\t\tmake sure sub-pages come from "
103 "this host via referrer");
104 #ifndef NO_DIRINDEX_SUPPORT
106 " -X\t\t\tenable automatic directory index support");
108 " -H\t\t\thide files starting with a period (.)"
112 " -x index\t\tchange default `index.html' file name");
113 #ifndef NO_SSL_SUPPORT
115 " -Z cert privkey\tspecify path to server certificate"
116 " and private key file\n"
117 "\t\t\tin pem format and enable bozohttpd in SSL mode");
118 #endif /* NO_SSL_SUPPORT */
119 bozo_err(httpd
, 1, "%s failed to start", progname
);
123 main(int argc
, char **argv
)
125 bozo_httpreq_t
*request
;
131 (void) memset(&httpd
, 0x0, sizeof(httpd
));
132 (void) memset(&prefs
, 0x0, sizeof(prefs
));
134 if ((progname
= strrchr(argv
[0], '/')) == NULL
)
139 openlog(progname
, LOG_PID
|LOG_NDELAY
, LOG_FTP
);
141 bozo_set_defaults(&httpd
, &prefs
);
143 while ((c
= getopt(argc
, argv
,
144 "C:HI:L:M:P:S:U:VXZ:bc:defhi:np:rst:uv:x:z:")) != -1) {
148 #ifdef NO_LUA_SUPPORT
150 "Lua support is not enabled");
153 /* make sure there's two argument */
154 if (argc
- optind
< 1)
155 usage(&httpd
, progname
);
156 bozo_add_lua_map(&httpd
, optarg
, argv
[optind
]);
159 #endif /* NO_LUA_SUPPORT */
161 #ifdef NO_DYNAMIC_CONTENT
163 "dynamic mime content support is not enabled");
166 /* make sure there's four arguments */
167 if (argc
- optind
< 3)
168 usage(&httpd
, progname
);
169 bozo_add_content_map_mime(&httpd
, optarg
, argv
[optind
],
170 argv
[optind
+1], argv
[optind
+2]);
173 #endif /* NO_DYNAMIC_CONTENT */
176 bozo_set_pref(&prefs
, "numeric", "true");
180 bozo_set_pref(&prefs
, "trusted referal", "true");
184 bozo_set_pref(&prefs
, "log to stderr", "true");
188 bozo_set_pref(&prefs
, "server software", optarg
);
191 #ifdef NO_SSL_SUPPORT
192 bozo_err(&httpd
, 1, "ssl support is not enabled");
195 /* make sure there's two arguments */
196 if (argc
- optind
< 1)
197 usage(&httpd
, progname
);
198 bozo_ssl_set_opts(&httpd
, optarg
, argv
[optind
++]);
200 #endif /* NO_SSL_SUPPORT */
202 bozo_set_pref(&prefs
, "username", optarg
);
206 bozo_set_pref(&prefs
, "unknown slash", "true");
210 bozo_set_pref(&prefs
, "virtual base", optarg
);
214 bozo_set_pref(&prefs
, "index.html", optarg
);
218 bozo_set_pref(&prefs
, "port number", optarg
);
221 #ifdef NO_DAEMON_MODE
227 bozo_err(&httpd
, 1, "Daemon mode is not enabled");
232 * test suite support - undocumented
233 * background == 2 (aka, -b -b) means to
234 * only process 1 per kid
236 if (bozo_get_pref(&prefs
, "background") == NULL
) {
237 bozo_set_pref(&prefs
, "background", "1");
239 bozo_set_pref(&prefs
, "background", "2");
244 bozo_set_pref(&prefs
, "dirty environment", "true");
248 bozo_set_pref(&prefs
, "foreground", "true");
252 bozo_set_pref(&prefs
, "bind address", optarg
);
256 bozo_set_pref(&prefs
, "pid file", optarg
);
258 #endif /* NO_DAEMON_MODE */
260 #ifdef NO_CGIBIN_SUPPORT
263 bozo_err(&httpd
, 1, "CGI is not enabled");
267 bozo_cgi_setbin(&httpd
, optarg
);
271 # ifdef NO_DYNAMIC_CONTENT
273 "dynamic CGI handler support is not enabled");
276 /* make sure there's two arguments */
277 if (argc
- optind
< 1)
278 usage(&httpd
, progname
);
279 bozo_add_content_map_cgi(&httpd
, optarg
,
282 # endif /* NO_DYNAMIC_CONTENT */
283 #endif /* NO_CGIBIN_SUPPORT */
288 if (httpd
.debug
== 1)
289 bozo_warn(&httpd
, "Debugging is not enabled");
290 #endif /* NO_DEBUG */
294 bozo_set_pref(&prefs
, "chroot dir", optarg
);
297 #ifdef NO_USER_SUPPORT
300 bozo_err(&httpd
, 1, "User support is not enabled");
304 bozo_set_pref(&prefs
, "public_html", optarg
);
308 bozo_set_pref(&prefs
, "enable users", "true");
310 #endif /* NO_USER_SUPPORT */
312 #ifdef NO_DIRINDEX_SUPPORT
316 "directory indexing is not enabled");
320 bozo_set_pref(&prefs
, "hide dots", "true");
324 bozo_set_pref(&prefs
, "directory indexing", "true");
327 #endif /* NO_DIRINDEX_SUPPORT */
330 usage(&httpd
, progname
);
338 if (argc
== 0 || argc
> 2) {
339 usage(&httpd
, progname
);
342 /* virtual host, and root of tree to serve */
343 bozo_setup(&httpd
, &prefs
, argv
[1], argv
[0]);
346 * read and process the HTTP request.
349 if ((request
= bozo_read_request(&httpd
)) != NULL
) {
350 bozo_process_request(request
);
351 bozo_clean_request(request
);
353 } while (httpd
.background
);