Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*	$NetBSD: main.c,v 1.31 2023/06/07 20:12:31 mrg Exp $	*/

/*	$eterna: main.c,v 1.6 2011/11/18 09:21:15 mrg Exp $	*/
/* from: eterna: bozohttpd.c,v 1.159 2009/05/23 02:14:30 mrg Exp 	*/

/*
 * Copyright (c) 1997-2023 Matthew R. Green
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer and
 *    dedication in the documentation and/or other materials provided
 *    with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

/* this program is dedicated to the Great God of Processed Cheese */

/*
 * main.c:  C front end to bozohttpd
 */

#include <sys/types.h>
#include <sys/param.h>

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>

#include "bozohttpd.h"

/* variables and functions */
#ifndef LOG_FTP
#define LOG_FTP LOG_DAEMON
#endif

/* print a usage message, and then exit */
BOZO_DEAD static void
usage(bozohttpd_t *httpd, char *progname)
{
	bozowarn(httpd, "usage: %s [options] slashdir [virtualhostname]",
			progname);
	bozowarn(httpd, "options:");

	if (have_daemon_mode)
		bozowarn(httpd, "   -b\t\t\tbackground in daemon mode");
	if (have_cgibin &&
	    have_dynamic_content)
		bozowarn(httpd, "   -C suffix handler\tadd this CGI handler "
				"for paths ending with `suffix'");
	if (have_cgibin)
		bozowarn(httpd, "   -c cgibin\t\tenable cgi-bin support in "
				"this directory");
	if (have_debug)
		bozowarn(httpd, "   -d\t\t\tenable debug support");
	if (have_user &&
	    have_cgibin)
		bozowarn(httpd, "   -E\t\t\tenable CGI support for user dirs");
	if (have_core)
		bozowarn(httpd, "   -e\t\t\tdon't clean the environment "
				"(-t and -U only)");
	if (have_daemon_mode)
		bozowarn(httpd, "   -f\t\t\tforeground in daemon mode");
	if (have_core)
		bozowarn(httpd, "   -G\t\t\tprint version number and exit");
	if (have_dirindex)
		bozowarn(httpd, "   -H\t\t\thide files starting with a period "
				"(.) in index mode");
	if (have_core)
		bozowarn(httpd, "   -I port\t\tbind or use on this port");
	if (have_daemon_mode)
		bozowarn(httpd, "   -i address\t\tbind on this address "
				"(daemon mode only)");
	if (have_lua)
		bozowarn(httpd, "   -L prefix script\tadd this Lua script for "
				"paths starting with `prefix'");
	if (have_dynamic_content)
		bozowarn(httpd, "   -M suffix t c c11\tadd this mime entry");
	if (have_core)
		bozowarn(httpd, "   -n\t\t\tdon't resolve host names");
	if (have_daemon_mode)
		bozowarn(httpd, "   -P pidfile\t\tpid file path");
	if (have_user)
		bozowarn(httpd, "   -p dir\t\t\"public_html\" directory name");
	if (have_core)
		bozowarn(httpd, "   -q\t\tquiet mode, no logging");
	if (have_dirindex)
		bozowarn(httpd, "   -R readme\t\tput readme file in footer "
				"of directory index");
	if (have_core) {
		bozowarn(httpd, "   -S version\t\tset server version string");
		bozowarn(httpd, "   -s\t\t\talways log to stderr");
		bozowarn(httpd, "   -T type timeout\t"
				"set <ssl|initial|header|request> timeout");
		bozowarn(httpd, "   -t dir\t\tchroot to `dir'");
		bozowarn(httpd, "   -U user\t\tchange user to `user'");
	}
	if (have_user)
		bozowarn(httpd, "   -u\t\t\tenable ~user/public_html support");
	if (have_core) {
		bozowarn(httpd, "   -V\t\t\tUnknown virtual hosts go to "
				"`slashdir'");
		bozowarn(httpd, "   -v virtualroot\tenable virtual host "
				"support in this directory");
	}
	if (have_dirindex)
		bozowarn(httpd, "   -X\t\t\tdirectory index support");
	if (have_core)
		bozowarn(httpd, "   -x index\t\tdefault \"index.html\" "
				"file name");
	if (have_ssl) {
		bozowarn(httpd, "   -Z cert privkey\tspecify path to server "
				"certificate and private key file\n"
				"\t\t\tin pem format and enable bozohttpd in "
				"SSL mode");
		bozowarn(httpd, "   -z ciphers\t\tspecify SSL ciphers");
	}

	bozoerr(httpd, 1, "%s failed to start", progname);
}

int
main(int argc, char **argv)
{
	bozo_httpreq_t	*request;
	bozohttpd_t	 httpd;
	bozoprefs_t	 prefs;
	char		*progname;
	const char	*val;
	int		 c;

	(void) memset(&httpd, 0x0, sizeof(httpd));
	(void) memset(&prefs, 0x0, sizeof(prefs));

	if ((progname = strrchr(argv[0], '/')) == NULL)
		progname = argv[0];
	else
		progname++;

	openlog(progname, LOG_PID|LOG_NDELAY, LOG_FTP);

	bozo_set_defaults(&httpd, &prefs);

	/*
	 * -r option was removed, do not reuse it for a while
	 */

	while ((c = getopt(argc, argv,
	    "C:EGHI:L:M:m:P:R:S:T:U:VXZ:bc:defhi:np:qst:uv:x:z:")) != -1) {
		switch (c) {

		case 'b':
			if (!have_daemon_mode)
 no_daemon_mode:
				bozoerr(&httpd, 1, "Daemon mode not enabled");

			/*
			 * test suite support - undocumented
			 * background == 2 (aka, -b -b) means to
			 * only process 1 per kid
			 */
			val = bozo_get_pref(&prefs, "background") == NULL ?
			    "1" : "2";
			bozo_set_pref(&httpd, &prefs, "background", val);
			break;

		case 'C':
			if (!have_dynamic_content ||
			    !have_cgibin)
				bozoerr(&httpd, 1,
				    "dynamic CGI handler support not enabled");

			/* make sure there's two arguments */
			if (argc - optind < 1)
				usage(&httpd, progname);
			bozo_add_content_map_cgi(&httpd, optarg,
					argv[optind++]);
			break;

		case 'c':
			if (!have_cgibin)
				bozoerr(&httpd, 1, "CGI not enabled");

			bozo_cgi_setbin(&httpd, optarg);
			break;

		case 'd':
			if (!have_debug)
				bozowarn(&httpd, "Debugging not enabled");
			httpd.debug++;
			break;

		case 'E':
			if (!have_user ||
			    !have_cgibin)
				bozoerr(&httpd, 1, "CGI not enabled");

			bozo_set_pref(&httpd, &prefs, "enable user cgibin",
				      "true");
			break;

		case 'e':
			bozo_set_pref(&httpd, &prefs, "dirty environment",
				      "true");
			break;

		case 'f':
			if (!have_daemon_mode)
				goto no_daemon_mode;

			bozo_set_pref(&httpd, &prefs, "foreground", "true");
			break;

		case 'G':
			{
				char	version[128];

				bozo_get_version(version, sizeof(version));
				printf("bozohttpd version %s\n", version);
			}
			return 0;

		case 'H':
			if (!have_dirindex)
 no_dirindex_support:
				bozoerr(&httpd, 1,
					"directory indexing not enabled");

			bozo_set_pref(&httpd, &prefs, "hide dots", "true");
			break;

		case 'I':
			bozo_set_pref(&httpd, &prefs, "port number", optarg);
			break;

		case 'i':
			if (!have_daemon_mode)
				goto no_daemon_mode;

			bozo_set_pref(&httpd, &prefs, "bind address", optarg);
			break;

		case 'L':
			if (!have_lua)
				bozoerr(&httpd, 1, "Lua support not enabled");

			/* make sure there's two argument */
			if (argc - optind < 1)
				usage(&httpd, progname);
			bozo_add_lua_map(&httpd, optarg, argv[optind]);
			optind++;
			break;

		case 'M':
			if (!have_dynamic_content)
				bozoerr(&httpd, 1,
				    "dynamic mime content support not enabled");

			/* make sure there're four arguments */
			if (argc - optind < 3)
				usage(&httpd, progname);
			bozo_add_content_map_mime(&httpd, optarg, argv[optind],
			    argv[optind+1], argv[optind+2]);
			optind += 3;
			break;

		case 'm':
			if (!have_ssl)
				goto no_ssl;

			httpd.ssl_min_proto = optarg;
			debug((&httpd, DEBUG_NORMAL,
			    "using minimum protocol version: %s", optarg));
			break;

		case 'n':
			bozo_set_pref(&httpd, &prefs, "numeric", "true");
			break;

		case 'P':
			if (!have_daemon_mode)
				goto no_daemon_mode;

			bozo_set_pref(&httpd, &prefs, "pid file", optarg);
			break;

		case 'p':
			if (!have_user)
 no_user_support:
				bozoerr(&httpd, 1, "User support not enabled");

			bozo_set_pref(&httpd, &prefs, "public_html", optarg);
			break;

		case 'q':
			bozo_set_pref(&httpd, &prefs, "no log", "true");
			break;

		case 'R':
			if (!have_dirindex)
				goto no_dirindex_support;

			bozo_set_pref(&httpd, &prefs, "directory index readme",
				      optarg);
			break;

		case 'S':
			bozo_set_pref(&httpd, &prefs, "server software",
				      optarg);
			break;

		case 's':
			bozo_set_pref(&httpd, &prefs, "log to stderr", "true");
			break;

		case 'T':
			/* make sure there're two arguments */
			if (argc - optind < 1)
				usage(&httpd, progname);
			if (bozo_set_timeout(&httpd, &prefs,
					     optarg, argv[optind])) {
				bozoerr(&httpd, 1,
					"invalid type '%s'", optarg);
				/* NOTREACHED */
			}
			optind++;
			break;

		case 't':
			bozo_set_pref(&httpd, &prefs, "chroot dir", optarg);
			break;

		case 'U':
			bozo_set_pref(&httpd, &prefs, "username", optarg);
			break;

		case 'u':
			if (!have_user)
				goto no_user_support;

			bozo_set_pref(&httpd, &prefs, "enable users", "true");
			break;

		case 'V':
			bozo_set_pref(&httpd, &prefs, "unknown slash", "true");
			break;

		case 'v':
			bozo_set_pref(&httpd, &prefs, "virtual base", optarg);
			break;

		case 'X':
			if (!have_dirindex)
				goto no_dirindex_support;

			bozo_set_pref(&httpd, &prefs, "directory indexing",
				      "true");
			break;

		case 'x':
			bozo_set_pref(&httpd, &prefs, "index.html", optarg);
			break;

		case 'Z':
			if (!have_ssl)
 no_ssl:
				bozoerr(&httpd, 1, "ssl support not enabled");

			/* make sure there's two arguments */
			if (argc - optind < 1)
				usage(&httpd, progname);
			bozo_ssl_set_opts(&httpd, optarg, argv[optind++]);
			break;

		case 'z':
			if (!have_ssl)
				goto no_ssl;

			bozo_ssl_set_ciphers(&httpd, optarg);
			break;

		default:
			usage(&httpd, progname);
			/* NOTREACHED */
		}
	}

	argc -= optind;
	argv += optind;

	if (argc == 0 || argc > 2) {
		usage(&httpd, progname);
	}

	/* virtual host, and root of tree to serve */
	bozo_setup(&httpd, &prefs, argv[1], argv[0]);

	/*
	 * read and process the HTTP request.
	 */
	do {
		if ((request = bozo_read_request(&httpd)) != NULL) {
			bozo_process_request(request);
			bozo_clean_request(request);
		}
	} while (httpd.background);

	bozo_cleanup(&httpd, &prefs);

	return (0);
}