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