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