1 /* $NetBSD: main.c,v 1.1.1.1 2010/05/10 03:30:04 mrg Exp $ */ 2 3 /* $eterna: main.c,v 1.3 2010/05/10 02:51:28 mrg Exp $ */ 4 /* from: eterna: bozohttpd.c,v 1.159 2009/05/23 02:14:30 mrg Exp */ 5 6 /* 7 * Copyright (c) 1997-2010 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 #ifndef __attribute__ 51 #define __attribute__(x) 52 #endif /* __attribute__ */ 53 54 #include "bozohttpd.h" 55 56 /* variables and functions */ 57 #ifndef LOG_FTP 58 #define LOG_FTP LOG_DAEMON 59 #endif 60 61 /* print a usage message, and then exit */ 62 static void 63 usage(bozohttpd_t *httpd, char *progname) 64 { 65 bozo_warn(httpd, "usage: %s [options] slashdir [virtualhostname]", 66 progname); 67 bozo_warn(httpd, "options:"); 68 #ifdef DEBUG 69 bozo_warn(httpd, " -d\t\t\tenable debug support"); 70 #endif 71 bozo_warn(httpd, " -s\t\t\talways log to stderr"); 72 #ifndef NO_USER_SUPPORT 73 bozo_warn(httpd, " -u\t\t\tenable ~user/public_html support"); 74 bozo_warn(httpd, " -p dir\t\tchange `public_html' directory name]"); 75 #endif 76 #ifndef NO_DYNAMIC_CONTENT 77 bozo_warn(httpd, " -M arg t c c11\tadd this mime extenstion"); 78 #endif 79 #ifndef NO_CGIBIN_SUPPORT 80 #ifndef NO_DYNAMIC_CONTENT 81 bozo_warn(httpd, " -C arg prog\t\tadd this CGI handler"); 82 #endif 83 bozo_warn(httpd, 84 " -c cgibin\t\tenable cgi-bin support in this directory"); 85 #endif 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"); 89 bozo_warn(httpd, 90 " -i address\t\tbind on this address (daemon mode only)"); 91 bozo_warn(httpd, " -I port\t\tbind on this port (daemon mode only)"); 92 #endif 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'"); 96 bozo_warn(httpd, 97 " -e\t\t\tdon't clean the environment (-t and -U only)"); 98 bozo_warn(httpd, 99 " -v virtualroot\tenable virtual host support " 100 "in this directory"); 101 bozo_warn(httpd, 102 " -r\t\t\tmake sure sub-pages come from " 103 "this host via referrer"); 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 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); 120 } 121 122 int 123 main(int argc, char **argv) 124 { 125 bozo_httpreq_t *request; 126 bozohttpd_t httpd; 127 bozoprefs_t prefs; 128 char *progname; 129 int c; 130 131 (void) memset(&httpd, 0x0, sizeof(httpd)); 132 (void) memset(&prefs, 0x0, sizeof(prefs)); 133 134 if ((progname = strrchr(argv[0], '/')) == NULL) 135 progname = argv[0]; 136 else 137 progname++; 138 139 openlog(progname, LOG_PID|LOG_NDELAY, LOG_FTP); 140 141 bozo_set_defaults(&httpd, &prefs); 142 143 while ((c = getopt(argc, argv, 144 "C:HI:M:S:U:VXZ:bc:defhi:np:rst:uv:x:z:")) != -1) { 145 switch(c) { 146 147 case 'M': 148 #ifdef NO_DYNAMIC_CONTENT 149 bozo_err(&httpd, 1, 150 "dynamic mime content support is not enabled"); 151 /* NOTREACHED */ 152 #else 153 /* make sure there's four arguments */ 154 if (argc - optind < 3) 155 usage(&httpd, progname); 156 bozo_add_content_map_mime(&httpd, optarg, argv[optind], 157 argv[optind+1], argv[optind+2]); 158 optind += 3; 159 break; 160 #endif /* NO_DYNAMIC_CONTENT */ 161 162 case 'n': 163 bozo_set_pref(&prefs, "numeric", "true"); 164 break; 165 166 case 'r': 167 bozo_set_pref(&prefs, "trusted referal", "true"); 168 break; 169 170 case 's': 171 bozo_set_pref(&prefs, "log to stderr", "true"); 172 break; 173 174 case 'S': 175 bozo_set_pref(&prefs, "server software", optarg); 176 break; 177 case 'Z': 178 #ifdef NO_SSL_SUPPORT 179 bozo_err(&httpd, 1, "ssl support is not enabled"); 180 /* NOT REACHED */ 181 #else 182 /* make sure there's two arguments */ 183 if (argc - optind < 1) 184 usage(&httpd, progname); 185 bozo_ssl_set_opts(&httpd, optarg, argv[optind++]); 186 break; 187 #endif /* NO_SSL_SUPPORT */ 188 case 'U': 189 bozo_set_pref(&prefs, "username", optarg); 190 break; 191 192 case 'V': 193 bozo_set_pref(&prefs, "unknown slash", "true"); 194 break; 195 196 case 'v': 197 bozo_set_pref(&prefs, "virtual base", optarg); 198 break; 199 200 case 'x': 201 bozo_set_pref(&prefs, "index.html", optarg); 202 break; 203 204 #ifdef NO_DAEMON_MODE 205 case 'b': 206 case 'e': 207 case 'f': 208 case 'i': 209 case 'I': 210 bozo_err(&httpd, 1, "Daemon mode is not enabled"); 211 /* NOTREACHED */ 212 #else 213 case 'b': 214 /* 215 * test suite support - undocumented 216 * background == 2 (aka, -b -b) means to 217 * only process 1 per kid 218 */ 219 if (bozo_get_pref(&prefs, "background") == NULL) { 220 bozo_set_pref(&prefs, "background", "1"); 221 } else { 222 bozo_set_pref(&prefs, "background", "2"); 223 } 224 break; 225 226 case 'e': 227 bozo_set_pref(&prefs, "dirty environment", "true"); 228 break; 229 230 case 'f': 231 bozo_set_pref(&prefs, "foreground", "true"); 232 break; 233 234 case 'i': 235 bozo_set_pref(&prefs, "bind address", optarg); 236 break; 237 238 case 'I': 239 bozo_set_pref(&prefs, "port number", optarg); 240 break; 241 #endif /* NO_DAEMON_MODE */ 242 243 #ifdef NO_CGIBIN_SUPPORT 244 case 'c': 245 case 'C': 246 bozo_err(&httpd, 1, "CGI is not enabled"); 247 /* NOTREACHED */ 248 #else 249 case 'c': 250 bozo_cgi_setbin(&httpd, optarg); 251 break; 252 253 case 'C': 254 # ifdef NO_DYNAMIC_CONTENT 255 bozo_err(&httpd, 1, 256 "dynamic CGI handler support is not enabled"); 257 /* NOTREACHED */ 258 # else 259 /* make sure there's two arguments */ 260 if (argc - optind < 1) 261 usage(&httpd, progname); 262 bozo_add_content_map_cgi(&httpd, optarg, 263 argv[optind++]); 264 break; 265 # endif /* NO_DYNAMIC_CONTENT */ 266 #endif /* NO_CGIBIN_SUPPORT */ 267 268 case 'd': 269 httpd.debug++; 270 #ifndef DEBUG 271 if (httpd.debug == 1) 272 bozo_warn(&httpd, "Debugging is not enabled"); 273 #endif /* !DEBUG */ 274 break; 275 276 #ifdef NO_USER_SUPPORT 277 case 'p': 278 case 't': 279 case 'u': 280 bozo_err(&httpd, 1, "User support is not enabled"); 281 /* NOTREACHED */ 282 #else 283 case 'p': 284 bozo_set_pref(&prefs, "public_html", optarg); 285 break; 286 287 case 't': 288 bozo_set_pref(&prefs, "chroot dir", optarg); 289 break; 290 291 case 'u': 292 bozo_set_pref(&prefs, "enable users", "true"); 293 break; 294 #endif /* NO_USER_SUPPORT */ 295 296 #ifdef NO_DIRINDEX_SUPPORT 297 case 'H': 298 case 'X': 299 bozo_err(&httpd, 1, 300 "directory indexing is not enabled"); 301 /* NOTREACHED */ 302 #else 303 case 'H': 304 bozo_set_pref(&prefs, "hide dots", "true"); 305 break; 306 307 case 'X': 308 bozo_set_pref(&prefs, "directory indexing", "true"); 309 break; 310 311 #endif /* NO_DIRINDEX_SUPPORT */ 312 313 default: 314 usage(&httpd, progname); 315 /* NOTREACHED */ 316 } 317 } 318 319 argc -= optind; 320 argv += optind; 321 322 if (argc == 0 || argc > 2) { 323 usage(&httpd, progname); 324 } 325 326 /* virtual host, and root of tree to serve */ 327 bozo_setup(&httpd, &prefs, argv[1], argv[0]); 328 329 /* 330 * read and process the HTTP request. 331 */ 332 do { 333 if ((request = bozo_read_request(&httpd)) != NULL) { 334 bozo_process_request(request); 335 bozo_clean_request(request); 336 } 337 } while (httpd.background); 338 339 return (0); 340 } 341