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