Bernhard Reutner-Fischer | 2c99851 | 2006-04-12 18:09:26 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 2 | /* |
| 3 | * httpd implementation for busybox |
| 4 | * |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 5 | * Copyright (C) 2002,2003 Glenn Engel <glenne@engel.org> |
"Vladimir N. Oleynik" | 79af7d5 | 2006-01-26 10:58:12 +0000 | [diff] [blame] | 6 | * Copyright (C) 2003-2006 Vladimir Oleynik <dzo@simtreas.ru> |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 7 | * |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 8 | * simplify patch stolen from libbb without using strdup |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 9 | * |
Bernhard Reutner-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 11 | * |
| 12 | ***************************************************************************** |
| 13 | * |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 14 | * Typical usage: |
| 15 | * for non root user |
| 16 | * httpd -p 8080 -h $HOME/public_html |
| 17 | * or for daemon start from rc script with uid=0: |
| 18 | * httpd -u www |
| 19 | * This is equivalent if www user have uid=80 to |
| 20 | * httpd -p 80 -u 80 -h /www -c /etc/httpd.conf -r "Web Server Authentication" |
| 21 | * |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 22 | * |
| 23 | * When a url contains "cgi-bin" it is assumed to be a cgi script. The |
| 24 | * server changes directory to the location of the script and executes it |
Glenn L McGrath | 1dc0cca | 2003-10-03 10:50:56 +0000 | [diff] [blame] | 25 | * after setting QUERY_STRING and other environment variables. |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 26 | * |
Denis Vlasenko | 8b45837 | 2006-11-21 21:23:21 +0000 | [diff] [blame] | 27 | * Doc: |
| 28 | * "CGI Environment Variables": http://hoohoo.ncsa.uiuc.edu/cgi/env.html |
| 29 | * |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 30 | * The server can also be invoked as a url arg decoder and html text encoder |
| 31 | * as follows: |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 32 | * foo=`httpd -d $foo` # decode "Hello%20World" as "Hello World" |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 33 | * bar=`httpd -e "<Hello World>"` # encode as "<Hello World>" |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 34 | * Note that url encoding for arguments is not the same as html encoding for |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 35 | * presentation. -d decodes a url-encoded argument while -e encodes in html |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 36 | * for page display. |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 37 | * |
| 38 | * httpd.conf has the following format: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 39 | * |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 40 | * A:172.20. # Allow address from 172.20.0.0/16 |
| 41 | * A:10.0.0.0/25 # Allow any address from 10.0.0.0-10.0.0.127 |
| 42 | * A:10.0.0.0/255.255.255.128 # Allow any address that previous set |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 43 | * A:127.0.0.1 # Allow local loopback connections |
| 44 | * D:* # Deny from other IP connections |
| 45 | * /cgi-bin:foo:bar # Require user foo, pwd bar on urls starting with /cgi-bin/ |
| 46 | * /adm:admin:setup # Require user admin, pwd setup on urls starting with /adm/ |
| 47 | * /adm:toor:PaSsWd # or user toor, pwd PaSsWd on urls starting with /adm/ |
| 48 | * .au:audio/basic # additional mime type for audio.au files |
"Vladimir N. Oleynik" | 4333a09 | 2006-01-31 13:53:30 +0000 | [diff] [blame] | 49 | * *.php:/path/php # running cgi.php scripts through an interpreter |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 50 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 51 | * A/D may be as a/d or allow/deny - first char case insensitive |
Glenn L McGrath | 393183d | 2003-05-26 14:07:50 +0000 | [diff] [blame] | 52 | * Deny IP rules take precedence over allow rules. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 53 | * |
| 54 | * |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 55 | * The Deny/Allow IP logic: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 56 | * |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 57 | * - Default is to allow all. No addresses are denied unless |
Eric Andersen | 97a1de1 | 2004-08-26 22:22:50 +0000 | [diff] [blame] | 58 | * denied with a D: rule. |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 59 | * - Order of Deny/Allow rules is significant |
| 60 | * - Deny rules take precedence over allow rules. |
| 61 | * - If a deny all rule (D:*) is used it acts as a catch-all for unmatched |
Eric Andersen | 97a1de1 | 2004-08-26 22:22:50 +0000 | [diff] [blame] | 62 | * addresses. |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 63 | * - Specification of Allow all (A:*) is a no-op |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 64 | * |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 65 | * Example: |
| 66 | * 1. Allow only specified addresses |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 67 | * A:172.20 # Allow any address that begins with 172.20. |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 68 | * A:10.10. # Allow any address that begins with 10.10. |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 69 | * A:127.0.0.1 # Allow local loopback connections |
| 70 | * D:* # Deny from other IP connections |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 71 | * |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 72 | * 2. Only deny specified addresses |
| 73 | * D:1.2.3. # deny from 1.2.3.0 - 1.2.3.255 |
| 74 | * D:2.3.4. # deny from 2.3.4.0 - 2.3.4.255 |
| 75 | * A:* # (optional line added for clarity) |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 76 | * |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 77 | * If a sub directory contains a config file it is parsed and merged with |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 78 | * any existing settings as if it was appended to the original configuration. |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 79 | * |
| 80 | * subdir paths are relative to the containing subdir and thus cannot |
| 81 | * affect the parent rules. |
| 82 | * |
| 83 | * Note that since the sub dir is parsed in the forked thread servicing the |
| 84 | * subdir http request, any merge is discarded when the process exits. As a |
| 85 | * result, the subdir settings only have a lifetime of a single request. |
| 86 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 87 | * |
| 88 | * If -c is not set, an attempt will be made to open the default |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 89 | * root configuration file. If -c is set and the file is not found, the |
| 90 | * server exits with an error. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 91 | * |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 92 | */ |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 93 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 94 | #include "libbb.h" |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 95 | |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 96 | /* amount of buffering in a pipe */ |
| 97 | #ifndef PIPE_BUF |
| 98 | # define PIPE_BUF 4096 |
| 99 | #endif |
| 100 | |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 101 | static const char default_path_httpd_conf[] = "/etc"; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 102 | static const char httpd_conf[] = "httpd.conf"; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 103 | |
Eric Andersen | 07f2fea | 2004-10-08 08:03:29 +0000 | [diff] [blame] | 104 | #define TIMEOUT 60 |
| 105 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 106 | // Note: busybox xfuncs are not used because we want the server to keep running |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 107 | // if something bad happens due to a malformed user request. |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 108 | // As a result, all memory allocation after daemonize |
| 109 | // is checked rigorously |
| 110 | |
| 111 | //#define DEBUG 1 |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 112 | #define DEBUG 0 |
"Vladimir N. Oleynik" | 6b903a2 | 2005-12-20 11:02:54 +0000 | [diff] [blame] | 113 | |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 114 | #define MAX_MEMORY_BUFF 8192 /* IO buffer */ |
| 115 | |
| 116 | typedef struct HT_ACCESS { |
| 117 | char *after_colon; |
| 118 | struct HT_ACCESS *next; |
| 119 | char before_colon[1]; /* really bigger, must last */ |
| 120 | } Htaccess; |
| 121 | |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 122 | typedef struct HT_ACCESS_IP { |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 123 | unsigned ip; |
| 124 | unsigned mask; |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 125 | int allow_deny; |
| 126 | struct HT_ACCESS_IP *next; |
| 127 | } Htaccess_IP; |
| 128 | |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 129 | struct globals { |
| 130 | int server_socket; |
| 131 | int accepted_socket; |
| 132 | volatile smallint alarm_signaled; |
| 133 | smallint flg_deny_all; |
| 134 | const char *g_query; |
| 135 | const char *configFile; |
| 136 | const char *home_httpd; |
| 137 | unsigned rmt_ip; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 138 | |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 139 | const char *found_mime_type; |
| 140 | const char *found_moved_temporarily; |
| 141 | off_t ContentLength; /* -1 - unknown */ |
| 142 | time_t last_mod; |
| 143 | Htaccess_IP *ip_a_d; /* config allow/deny lines */ |
| 144 | |
| 145 | USE_FEATURE_HTTPD_BASIC_AUTH(const char *g_realm;) |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 146 | USE_FEATURE_HTTPD_BASIC_AUTH(char *remoteuser;) |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 147 | USE_FEATURE_HTTPD_CGI(char *referer;) |
Glenn L McGrath | 1dc0cca | 2003-10-03 10:50:56 +0000 | [diff] [blame] | 148 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 149 | #if ENABLE_FEATURE_HTTPD_CGI || DEBUG |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 150 | char *rmt_ip_str; /* for set env REMOTE_ADDR */ |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 151 | #endif |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 152 | unsigned tcp_port; /* server initial port and for |
| 153 | set env REMOTE_PORT */ |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 154 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 155 | Htaccess *g_auth; /* config user:password lines */ |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 156 | #endif |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 157 | #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 158 | Htaccess *mime_a; /* config mime types */ |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 159 | #endif |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 160 | #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 161 | Htaccess *script_i; /* config script interpreters */ |
"Vladimir N. Oleynik" | 4333a09 | 2006-01-31 13:53:30 +0000 | [diff] [blame] | 162 | #endif |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 163 | char iobuf[MAX_MEMORY_BUFF]; |
| 164 | }; |
| 165 | #define G (*ptr_to_globals) |
| 166 | #define server_socket (G.server_socket ) |
| 167 | #define accepted_socket (G.accepted_socket) |
| 168 | #define alarm_signaled (G.alarm_signaled ) |
| 169 | #define flg_deny_all (G.flg_deny_all ) |
| 170 | #define g_query (G.g_query ) |
| 171 | #define configFile (G.configFile ) |
| 172 | #define home_httpd (G.home_httpd ) |
| 173 | #define rmt_ip (G.rmt_ip ) |
| 174 | #define found_mime_type (G.found_mime_type) |
| 175 | #define found_moved_temporarily (G.found_moved_temporarily) |
| 176 | #define ContentLength (G.ContentLength ) |
| 177 | #define last_mod (G.last_mod ) |
| 178 | #define ip_a_d (G.ip_a_d ) |
| 179 | #define g_realm (G.g_realm ) |
| 180 | #define remoteuser (G.remoteuser ) |
| 181 | #define referer (G.referer ) |
| 182 | #if ENABLE_FEATURE_HTTPD_CGI || DEBUG |
| 183 | #define rmt_ip_str (G.rmt_ip_str ) |
| 184 | #endif |
| 185 | #define tcp_port (G.tcp_port ) |
| 186 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
| 187 | #define g_auth (G.g_auth ) |
| 188 | #endif |
| 189 | #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES |
| 190 | #define mime_a (G.mime_a ) |
| 191 | #endif |
| 192 | #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR |
| 193 | #define script_i (G.script_i ) |
| 194 | #endif |
| 195 | #define iobuf (G.iobuf ) |
| 196 | #define INIT_G() do { \ |
| 197 | PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ |
| 198 | USE_FEATURE_HTTPD_BASIC_AUTH(g_realm = "Web Server Authentication";) \ |
| 199 | tcp_port = 80; \ |
| 200 | ContentLength = -1; \ |
| 201 | } while (0) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 202 | |
Mike Frysinger | fa6c484 | 2006-05-26 01:48:17 +0000 | [diff] [blame] | 203 | static const char request_GET[] = "GET"; /* size algorithmic optimize */ |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 204 | |
| 205 | static const char* const suffixTable [] = { |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 206 | /* Warning: shorted equivalent suffix in one line must be first */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 207 | ".htm.html", "text/html", |
| 208 | ".jpg.jpeg", "image/jpeg", |
| 209 | ".gif", "image/gif", |
| 210 | ".png", "image/png", |
| 211 | ".txt.h.c.cc.cpp", "text/plain", |
| 212 | ".css", "text/css", |
| 213 | ".wav", "audio/wav", |
| 214 | ".avi", "video/x-msvideo", |
| 215 | ".qt.mov", "video/quicktime", |
| 216 | ".mpe.mpeg", "video/mpeg", |
| 217 | ".mid.midi", "audio/midi", |
| 218 | ".mp3", "audio/mpeg", |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 219 | #if 0 /* unpopular */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 220 | ".au", "audio/basic", |
| 221 | ".pac", "application/x-ns-proxy-autoconfig", |
| 222 | ".vrml.wrl", "model/vrml", |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 223 | #endif |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 224 | 0, "application/octet-stream" /* default */ |
| 225 | }; |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 226 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 227 | typedef enum { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 228 | HTTP_OK = 200, |
| 229 | HTTP_MOVED_TEMPORARILY = 302, |
| 230 | HTTP_BAD_REQUEST = 400, /* malformed syntax */ |
| 231 | HTTP_UNAUTHORIZED = 401, /* authentication needed, respond with auth hdr */ |
| 232 | HTTP_NOT_FOUND = 404, |
| 233 | HTTP_FORBIDDEN = 403, |
| 234 | HTTP_REQUEST_TIMEOUT = 408, |
| 235 | HTTP_NOT_IMPLEMENTED = 501, /* used for unrecognized requests */ |
| 236 | HTTP_INTERNAL_SERVER_ERROR = 500, |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 237 | #if 0 /* future use */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 238 | HTTP_CONTINUE = 100, |
| 239 | HTTP_SWITCHING_PROTOCOLS = 101, |
| 240 | HTTP_CREATED = 201, |
| 241 | HTTP_ACCEPTED = 202, |
| 242 | HTTP_NON_AUTHORITATIVE_INFO = 203, |
| 243 | HTTP_NO_CONTENT = 204, |
| 244 | HTTP_MULTIPLE_CHOICES = 300, |
| 245 | HTTP_MOVED_PERMANENTLY = 301, |
| 246 | HTTP_NOT_MODIFIED = 304, |
| 247 | HTTP_PAYMENT_REQUIRED = 402, |
| 248 | HTTP_BAD_GATEWAY = 502, |
| 249 | HTTP_SERVICE_UNAVAILABLE = 503, /* overload, maintenance */ |
| 250 | HTTP_RESPONSE_SETSIZE = 0xffffffff |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 251 | #endif |
| 252 | } HttpResponseNum; |
| 253 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 254 | typedef struct { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 255 | HttpResponseNum type; |
| 256 | const char *name; |
| 257 | const char *info; |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 258 | } HttpEnumString; |
| 259 | |
| 260 | static const HttpEnumString httpResponseNames[] = { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 261 | { HTTP_OK, "OK", NULL }, |
| 262 | { HTTP_MOVED_TEMPORARILY, "Found", "Directories must end with a slash." }, |
| 263 | { HTTP_REQUEST_TIMEOUT, "Request Timeout", |
| 264 | "No request appeared within a reasonable time period." }, |
| 265 | { HTTP_NOT_IMPLEMENTED, "Not Implemented", |
| 266 | "The requested method is not recognized by this server." }, |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 267 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 268 | { HTTP_UNAUTHORIZED, "Unauthorized", "" }, |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 269 | #endif |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 270 | { HTTP_NOT_FOUND, "Not Found", |
| 271 | "The requested URL was not found on this server." }, |
| 272 | { HTTP_BAD_REQUEST, "Bad Request", "Unsupported method." }, |
| 273 | { HTTP_FORBIDDEN, "Forbidden", "" }, |
| 274 | { HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error", |
| 275 | "Internal Server Error" }, |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 276 | #if 0 /* not implemented */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 277 | { HTTP_CREATED, "Created" }, |
| 278 | { HTTP_ACCEPTED, "Accepted" }, |
| 279 | { HTTP_NO_CONTENT, "No Content" }, |
| 280 | { HTTP_MULTIPLE_CHOICES, "Multiple Choices" }, |
| 281 | { HTTP_MOVED_PERMANENTLY, "Moved Permanently" }, |
| 282 | { HTTP_NOT_MODIFIED, "Not Modified" }, |
| 283 | { HTTP_BAD_GATEWAY, "Bad Gateway", "" }, |
| 284 | { HTTP_SERVICE_UNAVAILABLE, "Service Unavailable", "" }, |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 285 | #endif |
| 286 | }; |
| 287 | |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 288 | |
| 289 | static const char RFC1123FMT[] = "%a, %d %b %Y %H:%M:%S GMT"; |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 290 | |
| 291 | |
| 292 | #define STRNCASECMP(a, str) strncasecmp((a), (str), sizeof(str)-1) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 293 | |
| 294 | |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 295 | static int scan_ip(const char **ep, unsigned *ip, unsigned char endc) |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 296 | { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 297 | const char *p = *ep; |
| 298 | int auto_mask = 8; |
| 299 | int j; |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 300 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 301 | *ip = 0; |
| 302 | for (j = 0; j < 4; j++) { |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 303 | unsigned octet; |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 304 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 305 | if ((*p < '0' || *p > '9') && (*p != '/' || j == 0) && *p != 0) |
| 306 | return -auto_mask; |
| 307 | octet = 0; |
| 308 | while (*p >= '0' && *p <= '9') { |
| 309 | octet *= 10; |
| 310 | octet += *p - '0'; |
| 311 | if (octet > 255) |
| 312 | return -auto_mask; |
| 313 | p++; |
| 314 | } |
| 315 | if (*p == '.') |
| 316 | p++; |
| 317 | if (*p != '/' && *p != 0) |
| 318 | auto_mask += 8; |
| 319 | *ip = ((*ip) << 8) | octet; |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 320 | } |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 321 | if (*p != 0) { |
| 322 | if (*p != endc) |
| 323 | return -auto_mask; |
| 324 | p++; |
| 325 | if (*p == 0) |
| 326 | return -auto_mask; |
| 327 | } |
| 328 | *ep = p; |
| 329 | return auto_mask; |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 332 | static int scan_ip_mask(const char *ipm, unsigned *ip, unsigned *mask) |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 333 | { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 334 | int i; |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 335 | unsigned msk; |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 336 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 337 | i = scan_ip(&ipm, ip, '/'); |
| 338 | if (i < 0) |
| 339 | return i; |
| 340 | if (*ipm) { |
| 341 | const char *p = ipm; |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 342 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 343 | i = 0; |
| 344 | while (*p) { |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 345 | if (*p < '0' || *p > '9') { |
| 346 | if (*p == '.') { |
| 347 | i = scan_ip(&ipm, mask, 0); |
| 348 | return i != 32; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 349 | } |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 350 | return -1; |
| 351 | } |
| 352 | i *= 10; |
| 353 | i += *p - '0'; |
| 354 | p++; |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 355 | } |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 356 | } |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 357 | if (i > 32 || i < 0) |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 358 | return -1; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 359 | msk = 0x80000000; |
| 360 | *mask = 0; |
| 361 | while (i > 0) { |
| 362 | *mask |= msk; |
| 363 | msk >>= 1; |
| 364 | i--; |
| 365 | } |
| 366 | return 0; |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 369 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH \ |
| 370 | || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES \ |
| 371 | || ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 372 | static void free_config_lines(Htaccess **pprev) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 373 | { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 374 | Htaccess *prev = *pprev; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 375 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 376 | while (prev) { |
| 377 | Htaccess *cur = prev; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 378 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 379 | prev = cur->next; |
| 380 | free(cur); |
| 381 | } |
| 382 | *pprev = NULL; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 383 | } |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 384 | #endif |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 385 | |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 386 | /* flag */ |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 387 | #define FIRST_PARSE 0 |
| 388 | #define SUBDIR_PARSE 1 |
| 389 | #define SIGNALED_PARSE 2 |
| 390 | #define FIND_FROM_HTTPD_ROOT 3 |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 391 | /**************************************************************************** |
| 392 | * |
| 393 | > $Function: parse_conf() |
| 394 | * |
| 395 | * $Description: parse configuration file into in-memory linked list. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 396 | * |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 397 | * The first non-white character is examined to determine if the config line |
| 398 | * is one of the following: |
| 399 | * .ext:mime/type # new mime type not compiled into httpd |
| 400 | * [adAD]:from # ip address allow/deny, * for wildcard |
| 401 | * /path:user:pass # username/password |
| 402 | * |
| 403 | * Any previous IP rules are discarded. |
| 404 | * If the flag argument is not SUBDIR_PARSE then all /path and mime rules |
| 405 | * are also discarded. That is, previous settings are retained if flag is |
| 406 | * SUBDIR_PARSE. |
| 407 | * |
| 408 | * $Parameters: |
| 409 | * (const char *) path . . null for ip address checks, path for password |
| 410 | * checks. |
| 411 | * (int) flag . . . . . . the source of the parse request. |
| 412 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 413 | * $Return: (None) |
Glenn L McGrath | 4fe3ff8 | 2003-05-19 05:56:16 +0000 | [diff] [blame] | 414 | * |
| 415 | ****************************************************************************/ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 416 | static void parse_conf(const char *path, int flag) |
| 417 | { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 418 | FILE *f; |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 419 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 420 | Htaccess *prev; |
| 421 | #endif |
| 422 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH \ |
| 423 | || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES \ |
| 424 | || ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 425 | Htaccess *cur; |
Glenn L McGrath | baaa6e9 | 2003-09-15 15:00:43 +0000 | [diff] [blame] | 426 | #endif |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 427 | |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 428 | const char *cf = configFile; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 429 | char buf[160]; |
| 430 | char *p0 = NULL; |
| 431 | char *c, *p; |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 432 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 433 | /* free previous ip setup if present */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 434 | Htaccess_IP *pip = ip_a_d; |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 435 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 436 | while (pip) { |
| 437 | Htaccess_IP *cur_ipl = pip; |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 438 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 439 | pip = cur_ipl->next; |
| 440 | free(cur_ipl); |
| 441 | } |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 442 | ip_a_d = NULL; |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 443 | |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 444 | flg_deny_all = 0; |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 445 | |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 446 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH \ |
| 447 | || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES \ |
| 448 | || ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 449 | /* retain previous auth and mime config only for subdir parse */ |
| 450 | if (flag != SUBDIR_PARSE) { |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 451 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 452 | free_config_lines(&g_auth); |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 453 | #endif |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 454 | #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 455 | free_config_lines(&mime_a); |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 456 | #endif |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 457 | #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 458 | free_config_lines(&script_i); |
"Vladimir N. Oleynik" | 4333a09 | 2006-01-31 13:53:30 +0000 | [diff] [blame] | 459 | #endif |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 460 | } |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 461 | #endif |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 462 | |
| 463 | if (flag == SUBDIR_PARSE || cf == NULL) { |
| 464 | cf = alloca(strlen(path) + sizeof(httpd_conf) + 2); |
| 465 | if (cf == NULL) { |
| 466 | if (flag == FIRST_PARSE) |
| 467 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
| 468 | return; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 469 | } |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 470 | sprintf((char *)cf, "%s/%s", path, httpd_conf); |
Glenn L McGrath | 393183d | 2003-05-26 14:07:50 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 473 | while ((f = fopen(cf, "r")) == NULL) { |
| 474 | if (flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) { |
| 475 | /* config file not found, no changes to config */ |
| 476 | return; |
| 477 | } |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 478 | if (configFile && flag == FIRST_PARSE) /* if -c option given */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 479 | bb_perror_msg_and_die("%s", cf); |
| 480 | flag = FIND_FROM_HTTPD_ROOT; |
| 481 | cf = httpd_conf; |
| 482 | } |
| 483 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 484 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 485 | prev = g_auth; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 486 | #endif |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 487 | /* This could stand some work */ |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 488 | while ((p0 = fgets(buf, sizeof(buf), f)) != NULL) { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 489 | c = NULL; |
| 490 | for (p = p0; *p0 != 0 && *p0 != '#'; p0++) { |
| 491 | if (!isspace(*p0)) { |
| 492 | *p++ = *p0; |
| 493 | if (*p0 == ':' && c == NULL) |
| 494 | c = p; |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 495 | } |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 496 | } |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 497 | *p = 0; |
| 498 | |
| 499 | /* test for empty or strange line */ |
| 500 | if (c == NULL || *c == 0) |
| 501 | continue; |
| 502 | p0 = buf; |
| 503 | if (*p0 == 'd') |
| 504 | *p0 = 'D'; |
| 505 | if (*c == '*') { |
| 506 | if (*p0 == 'D') { |
| 507 | /* memorize deny all */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 508 | flg_deny_all = 1; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 509 | } |
| 510 | /* skip default other "word:*" config lines */ |
| 511 | continue; |
| 512 | } |
| 513 | |
| 514 | if (*p0 == 'a') |
| 515 | *p0 = 'A'; |
| 516 | else if (*p0 != 'D' && *p0 != 'A' |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 517 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 518 | && *p0 != '/' |
| 519 | #endif |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 520 | #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 521 | && *p0 != '.' |
| 522 | #endif |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 523 | #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 524 | && *p0 != '*' |
| 525 | #endif |
| 526 | ) |
| 527 | continue; |
| 528 | if (*p0 == 'A' || *p0 == 'D') { |
| 529 | /* storing current config IP line */ |
Denis Vlasenko | 9b1381f | 2007-01-03 02:56:00 +0000 | [diff] [blame] | 530 | pip = xzalloc(sizeof(Htaccess_IP)); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 531 | if (pip) { |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 532 | if (scan_ip_mask(c, &(pip->ip), &(pip->mask))) { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 533 | /* syntax IP{/mask} error detected, protect all */ |
| 534 | *p0 = 'D'; |
| 535 | pip->mask = 0; |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 536 | } |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 537 | pip->allow_deny = *p0; |
| 538 | if (*p0 == 'D') { |
| 539 | /* Deny:form_IP move top */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 540 | pip->next = ip_a_d; |
| 541 | ip_a_d = pip; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 542 | } else { |
| 543 | /* add to bottom A:form_IP config line */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 544 | Htaccess_IP *prev_IP = ip_a_d; |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 545 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 546 | if (prev_IP == NULL) { |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 547 | ip_a_d = pip; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 548 | } else { |
| 549 | while (prev_IP->next) |
| 550 | prev_IP = prev_IP->next; |
| 551 | prev_IP->next = pip; |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | continue; |
| 556 | } |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 557 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 558 | if (*p0 == '/') { |
| 559 | /* make full path from httpd root / curent_path / config_line_path */ |
| 560 | cf = flag == SUBDIR_PARSE ? path : ""; |
| 561 | p0 = malloc(strlen(cf) + (c - buf) + 2 + strlen(c)); |
| 562 | if (p0 == NULL) |
| 563 | continue; |
| 564 | c[-1] = 0; |
| 565 | sprintf(p0, "/%s%s", cf, buf); |
| 566 | |
| 567 | /* another call bb_simplify_path */ |
| 568 | cf = p = p0; |
| 569 | |
| 570 | do { |
| 571 | if (*p == '/') { |
| 572 | if (*cf == '/') { /* skip duplicate (or initial) slash */ |
| 573 | continue; |
| 574 | } else if (*cf == '.') { |
| 575 | if (cf[1] == '/' || cf[1] == 0) { /* remove extra '.' */ |
| 576 | continue; |
| 577 | } else if ((cf[1] == '.') && (cf[2] == '/' || cf[2] == 0)) { |
| 578 | ++cf; |
| 579 | if (p > p0) { |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 580 | while (*--p != '/') /* omit previous dir */; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 581 | } |
| 582 | continue; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | *++p = *cf; |
| 587 | } while (*++cf); |
| 588 | |
| 589 | if ((p == p0) || (*p != '/')) { /* not a trailing slash */ |
| 590 | ++p; /* so keep last character */ |
| 591 | } |
| 592 | *p = 0; |
| 593 | sprintf(p0, "%s:%s", p0, c); |
| 594 | } |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 595 | #endif |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 596 | |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 597 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH \ |
| 598 | || ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES \ |
| 599 | || ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 600 | /* storing current config line */ |
Denis Vlasenko | 9b1381f | 2007-01-03 02:56:00 +0000 | [diff] [blame] | 601 | cur = xzalloc(sizeof(Htaccess) + strlen(p0)); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 602 | if (cur) { |
| 603 | cf = strcpy(cur->before_colon, p0); |
| 604 | c = strchr(cf, ':'); |
| 605 | *c++ = 0; |
| 606 | cur->after_colon = c; |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 607 | #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 608 | if (*cf == '.') { |
| 609 | /* config .mime line move top for overwrite previous */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 610 | cur->next = mime_a; |
| 611 | mime_a = cur; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 612 | continue; |
| 613 | } |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 614 | #endif |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 615 | #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 616 | if (*cf == '*' && cf[1] == '.') { |
| 617 | /* config script interpreter line move top for overwrite previous */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 618 | cur->next = script_i; |
| 619 | script_i = cur; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 620 | continue; |
| 621 | } |
"Vladimir N. Oleynik" | 4333a09 | 2006-01-31 13:53:30 +0000 | [diff] [blame] | 622 | #endif |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 623 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 624 | free(p0); |
| 625 | if (prev == NULL) { |
| 626 | /* first line */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 627 | g_auth = prev = cur; |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 628 | } else { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 629 | /* sort path, if current lenght eq or bigger then move up */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 630 | Htaccess *prev_hti = g_auth; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 631 | size_t l = strlen(cf); |
| 632 | Htaccess *hti; |
| 633 | |
| 634 | for (hti = prev_hti; hti; hti = hti->next) { |
| 635 | if (l >= strlen(hti->before_colon)) { |
| 636 | /* insert before hti */ |
| 637 | cur->next = hti; |
| 638 | if (prev_hti != hti) { |
| 639 | prev_hti->next = cur; |
| 640 | } else { |
| 641 | /* insert as top */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 642 | g_auth = cur; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 643 | } |
| 644 | break; |
| 645 | } |
| 646 | if (prev_hti != hti) |
| 647 | prev_hti = prev_hti->next; |
| 648 | } |
| 649 | if (!hti) { /* not inserted, add to bottom */ |
| 650 | prev->next = cur; |
| 651 | prev = cur; |
| 652 | } |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 653 | } |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 654 | #endif |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 655 | } |
Glenn L McGrath | baaa6e9 | 2003-09-15 15:00:43 +0000 | [diff] [blame] | 656 | #endif |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 657 | } |
| 658 | fclose(f); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 661 | #if ENABLE_FEATURE_HTTPD_ENCODE_URL_STR |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 662 | /**************************************************************************** |
| 663 | * |
| 664 | > $Function: encodeString() |
| 665 | * |
| 666 | * $Description: Given a string, html encode special characters. |
| 667 | * This is used for the -e command line option to provide an easy way |
| 668 | * for scripts to encode result data without confusing browsers. The |
| 669 | * returned string pointer is memory allocated by malloc(). |
| 670 | * |
| 671 | * $Parameters: |
| 672 | * (const char *) string . . The first string to encode. |
| 673 | * |
| 674 | * $Return: (char *) . . . .. . . A pointer to the encoded string. |
| 675 | * |
| 676 | * $Errors: Returns a null string ("") if memory is not available. |
| 677 | * |
| 678 | ****************************************************************************/ |
| 679 | static char *encodeString(const char *string) |
| 680 | { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 681 | /* take the simple route and encode everything */ |
| 682 | /* could possibly scan once to get length. */ |
| 683 | int len = strlen(string); |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 684 | char *out = xmalloc(len * 6 + 1); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 685 | char *p = out; |
| 686 | char ch; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 687 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 688 | while ((ch = *string++)) { |
| 689 | // very simple check for what to encode |
| 690 | if (isalnum(ch)) *p++ = ch; |
| 691 | else p += sprintf(p, "&#%d;", (unsigned char) ch); |
| 692 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 693 | *p = '\0'; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 694 | return out; |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 695 | } |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 696 | #endif /* FEATURE_HTTPD_ENCODE_URL_STR */ |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 697 | |
| 698 | /**************************************************************************** |
| 699 | * |
| 700 | > $Function: decodeString() |
| 701 | * |
| 702 | * $Description: Given a URL encoded string, convert it to plain ascii. |
| 703 | * Since decoding always makes strings smaller, the decode is done in-place. |
| 704 | * Thus, callers should strdup() the argument if they do not want the |
| 705 | * argument modified. The return is the original pointer, allowing this |
| 706 | * function to be easily used as arguments to other functions. |
| 707 | * |
| 708 | * $Parameters: |
| 709 | * (char *) string . . . The first string to decode. |
Denis Vlasenko | a35c9e9 | 2006-11-29 15:58:50 +0000 | [diff] [blame] | 710 | * (int) option_d . . 1 if called for httpd -d |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 711 | * |
| 712 | * $Return: (char *) . . . . A pointer to the decoded string (same as input). |
| 713 | * |
| 714 | * $Errors: None |
| 715 | * |
| 716 | ****************************************************************************/ |
Denis Vlasenko | a35c9e9 | 2006-11-29 15:58:50 +0000 | [diff] [blame] | 717 | static char *decodeString(char *orig, int option_d) |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 718 | { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 719 | /* note that decoded string is always shorter than original */ |
| 720 | char *string = orig; |
| 721 | char *ptr = string; |
Denis Vlasenko | a35c9e9 | 2006-11-29 15:58:50 +0000 | [diff] [blame] | 722 | char c; |
Eric Andersen | a3bb3e6 | 2003-06-26 09:05:32 +0000 | [diff] [blame] | 723 | |
Denis Vlasenko | a35c9e9 | 2006-11-29 15:58:50 +0000 | [diff] [blame] | 724 | while ((c = *ptr++) != '\0') { |
| 725 | unsigned value1, value2; |
| 726 | |
| 727 | if (option_d && c == '+') { |
Denis Vlasenko | 601ae13 | 2006-11-28 23:37:46 +0000 | [diff] [blame] | 728 | *string++ = ' '; |
Denis Vlasenko | a35c9e9 | 2006-11-29 15:58:50 +0000 | [diff] [blame] | 729 | continue; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 730 | } |
Denis Vlasenko | a35c9e9 | 2006-11-29 15:58:50 +0000 | [diff] [blame] | 731 | if (c != '%') { |
| 732 | *string++ = c; |
| 733 | continue; |
| 734 | } |
| 735 | if (sscanf(ptr, "%1X", &value1) != 1 |
| 736 | || sscanf(ptr+1, "%1X", &value2) != 1 |
| 737 | ) { |
| 738 | if (!option_d) |
| 739 | return NULL; |
| 740 | *string++ = '%'; |
| 741 | continue; |
| 742 | } |
| 743 | value1 = value1 * 16 + value2; |
| 744 | if (!option_d && (value1 == '/' || value1 == '\0')) { |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 745 | /* caller takes it as indication of invalid |
Denis Vlasenko | a35c9e9 | 2006-11-29 15:58:50 +0000 | [diff] [blame] | 746 | * (dangerous wrt exploits) chars */ |
| 747 | return orig + 1; |
| 748 | } |
| 749 | *string++ = value1; |
| 750 | ptr += 2; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 751 | } |
| 752 | *string = '\0'; |
| 753 | return orig; |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 757 | #if ENABLE_FEATURE_HTTPD_CGI |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 758 | /**************************************************************************** |
Denis Vlasenko | e867b7c | 2006-11-16 16:12:09 +0000 | [diff] [blame] | 759 | * setenv helpers |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 760 | ****************************************************************************/ |
Denis Vlasenko | e867b7c | 2006-11-16 16:12:09 +0000 | [diff] [blame] | 761 | static void setenv1(const char *name, const char *value) |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 762 | { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 763 | if (!value) |
| 764 | value = ""; |
Denis Vlasenko | e867b7c | 2006-11-16 16:12:09 +0000 | [diff] [blame] | 765 | setenv(name, value, 1); |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 766 | } |
Denis Vlasenko | e867b7c | 2006-11-16 16:12:09 +0000 | [diff] [blame] | 767 | static void setenv_long(const char *name, long value) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 768 | { |
Denis Vlasenko | e867b7c | 2006-11-16 16:12:09 +0000 | [diff] [blame] | 769 | char buf[sizeof(value)*3 + 1]; |
| 770 | sprintf(buf, "%ld", value); |
| 771 | setenv(name, buf, 1); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 772 | } |
Eric Andersen | a3bb3e6 | 2003-06-26 09:05:32 +0000 | [diff] [blame] | 773 | #endif |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 774 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 775 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 776 | /**************************************************************************** |
| 777 | * |
| 778 | > $Function: decodeBase64() |
| 779 | * |
| 780 | > $Description: Decode a base 64 data stream as per rfc1521. |
| 781 | * Note that the rfc states that none base64 chars are to be ignored. |
| 782 | * Since the decode always results in a shorter size than the input, it is |
| 783 | * OK to pass the input arg as an output arg. |
| 784 | * |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 785 | * $Parameter: |
| 786 | * (char *) Data . . . . A pointer to a base64 encoded string. |
| 787 | * Where to place the decoded data. |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 788 | * |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 789 | * $Return: void |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 790 | * |
| 791 | * $Errors: None |
| 792 | * |
| 793 | ****************************************************************************/ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 794 | static void decodeBase64(char *Data) |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 795 | { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 796 | const unsigned char *in = (const unsigned char *)Data; |
| 797 | // The decoded size will be at most 3/4 the size of the encoded |
Denis Vlasenko | 088b959 | 2007-04-18 21:14:46 +0000 | [diff] [blame] | 798 | unsigned ch = 0; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 799 | int i = 0; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 800 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 801 | while (*in) { |
| 802 | int t = *in++; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 803 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 804 | if (t >= '0' && t <= '9') |
| 805 | t = t - '0' + 52; |
| 806 | else if (t >= 'A' && t <= 'Z') |
| 807 | t = t - 'A'; |
| 808 | else if (t >= 'a' && t <= 'z') |
| 809 | t = t - 'a' + 26; |
| 810 | else if (t == '+') |
| 811 | t = 62; |
| 812 | else if (t == '/') |
| 813 | t = 63; |
| 814 | else if (t == '=') |
| 815 | t = 0; |
| 816 | else |
| 817 | continue; |
Glenn L McGrath | 874e338 | 2003-05-14 12:11:36 +0000 | [diff] [blame] | 818 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 819 | ch = (ch << 6) | t; |
| 820 | i++; |
| 821 | if (i == 4) { |
| 822 | *Data++ = (char) (ch >> 16); |
| 823 | *Data++ = (char) (ch >> 8); |
| 824 | *Data++ = (char) ch; |
| 825 | i = 0; |
| 826 | } |
| 827 | } |
Denis Vlasenko | 088b959 | 2007-04-18 21:14:46 +0000 | [diff] [blame] | 828 | *Data = '\0'; |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 829 | } |
| 830 | #endif |
| 831 | |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 832 | |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 833 | /**************************************************************************** |
| 834 | * |
| 835 | > $Function: openServer() |
| 836 | * |
| 837 | * $Description: create a listen server socket on the designated port. |
| 838 | * |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 839 | * $Return: (int) . . . A connection socket. -1 for errors. |
| 840 | * |
| 841 | * $Errors: None |
| 842 | * |
| 843 | ****************************************************************************/ |
Denis Vlasenko | 56258b6 | 2007-06-23 23:14:02 +0000 | [diff] [blame] | 844 | #if BB_MMU |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 845 | static int openServer(void) |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 846 | { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 847 | int fd; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 848 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 849 | /* create the socket right now */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 850 | fd = create_and_bind_stream_or_die(NULL, tcp_port); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 851 | xlisten(fd, 9); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 852 | return fd; |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 853 | } |
Denis Vlasenko | 56258b6 | 2007-06-23 23:14:02 +0000 | [diff] [blame] | 854 | #endif |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 855 | |
| 856 | /**************************************************************************** |
| 857 | * |
| 858 | > $Function: sendHeaders() |
| 859 | * |
| 860 | * $Description: Create and send HTTP response headers. |
| 861 | * The arguments are combined and sent as one write operation. Note that |
| 862 | * IE will puke big-time if the headers are not sent in one packet and the |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 863 | * second packet is delayed for any reason. |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 864 | * |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 865 | * $Parameter: |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 866 | * (HttpResponseNum) responseNum . . . The result code to send. |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 867 | * |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 868 | * $Return: (int) . . . . writing errors |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 869 | * |
| 870 | ****************************************************************************/ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 871 | static int sendHeaders(HttpResponseNum responseNum) |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 872 | { |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 873 | char *buf = iobuf; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 874 | const char *responseString = ""; |
| 875 | const char *infoString = 0; |
| 876 | const char *mime_type; |
Denis Vlasenko | b64eed6 | 2007-01-14 17:06:11 +0000 | [diff] [blame] | 877 | unsigned i; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 878 | time_t timer = time(0); |
| 879 | char timeStr[80]; |
| 880 | int len; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 881 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 882 | for (i = 0; i < ARRAY_SIZE(httpResponseNames); i++) { |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 883 | if (httpResponseNames[i].type == responseNum) { |
| 884 | responseString = httpResponseNames[i].name; |
| 885 | infoString = httpResponseNames[i].info; |
| 886 | break; |
| 887 | } |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 888 | } |
| 889 | /* error message is HTML */ |
| 890 | mime_type = responseNum == HTTP_OK ? |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 891 | found_mime_type : "text/html"; |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 892 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 893 | /* emit the current date */ |
| 894 | strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&timer)); |
| 895 | len = sprintf(buf, |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 896 | "HTTP/1.0 %d %s\r\nContent-type: %s\r\n" |
| 897 | "Date: %s\r\nConnection: close\r\n", |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 898 | responseNum, responseString, mime_type, timeStr); |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 899 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 900 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 901 | if (responseNum == HTTP_UNAUTHORIZED) { |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 902 | len += sprintf(buf+len, |
| 903 | "WWW-Authenticate: Basic realm=\"%s\"\r\n", |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 904 | g_realm); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 905 | } |
Glenn L McGrath | 3d2405c | 2003-02-10 22:28:21 +0000 | [diff] [blame] | 906 | #endif |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 907 | if (responseNum == HTTP_MOVED_TEMPORARILY) { |
| 908 | len += sprintf(buf+len, "Location: %s/%s%s\r\n", |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 909 | found_moved_temporarily, |
| 910 | (g_query ? "?" : ""), |
| 911 | (g_query ? g_query : "")); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 912 | } |
Eric Andersen | 07f2fea | 2004-10-08 08:03:29 +0000 | [diff] [blame] | 913 | |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 914 | if (ContentLength != -1) { /* file */ |
| 915 | strftime(timeStr, sizeof(timeStr), RFC1123FMT, gmtime(&last_mod)); |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 916 | len += sprintf(buf+len, "Last-Modified: %s\r\n%s %"OFF_FMT"d\r\n", |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 917 | timeStr, "Content-length:", ContentLength); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 918 | } |
| 919 | strcat(buf, "\r\n"); |
| 920 | len += 2; |
| 921 | if (infoString) { |
| 922 | len += sprintf(buf+len, |
| 923 | "<HEAD><TITLE>%d %s</TITLE></HEAD>\n" |
| 924 | "<BODY><H1>%d %s</H1>\n%s\n</BODY>\n", |
| 925 | responseNum, responseString, |
| 926 | responseNum, responseString, infoString); |
| 927 | } |
Denis Vlasenko | 6c85ddc | 2006-11-21 00:08:39 +0000 | [diff] [blame] | 928 | if (DEBUG) |
| 929 | fprintf(stderr, "headers: '%s'\n", buf); |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 930 | i = accepted_socket; |
Denis Vlasenko | b64eed6 | 2007-01-14 17:06:11 +0000 | [diff] [blame] | 931 | if (i == 0) i++; /* write to fd# 1 in inetd mode */ |
| 932 | return full_write(i, buf, len); |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | /**************************************************************************** |
| 936 | * |
| 937 | > $Function: getLine() |
| 938 | * |
| 939 | * $Description: Read from the socket until an end of line char found. |
| 940 | * |
| 941 | * Characters are read one at a time until an eol sequence is found. |
| 942 | * |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 943 | * $Return: (int) . . . . number of characters read. -1 if error. |
| 944 | * |
| 945 | ****************************************************************************/ |
Glenn L McGrath | 1dc0cca | 2003-10-03 10:50:56 +0000 | [diff] [blame] | 946 | static int getLine(void) |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 947 | { |
Denis Vlasenko | 6c5e5a0 | 2006-11-10 23:28:57 +0000 | [diff] [blame] | 948 | int count = 0; |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 949 | char *buf = iobuf; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 950 | |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 951 | while (read(accepted_socket, buf + count, 1) == 1) { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 952 | if (buf[count] == '\r') continue; |
| 953 | if (buf[count] == '\n') { |
| 954 | buf[count] = 0; |
| 955 | return count; |
| 956 | } |
Denis Vlasenko | 6c5e5a0 | 2006-11-10 23:28:57 +0000 | [diff] [blame] | 957 | if (count < (MAX_MEMORY_BUFF-1)) /* check overflow */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 958 | count++; |
| 959 | } |
| 960 | if (count) return count; |
| 961 | else return -1; |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 964 | #if ENABLE_FEATURE_HTTPD_CGI |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 965 | /**************************************************************************** |
| 966 | * |
| 967 | > $Function: sendCgi() |
| 968 | * |
| 969 | * $Description: Execute a CGI script and send it's stdout back |
| 970 | * |
| 971 | * Environment variables are set up and the script is invoked with pipes |
| 972 | * for stdin/stdout. If a post is being done the script is fed the POST |
| 973 | * data in addition to setting the QUERY_STRING variable (for GETs or POSTs). |
| 974 | * |
| 975 | * $Parameters: |
Glenn L McGrath | 1dc0cca | 2003-10-03 10:50:56 +0000 | [diff] [blame] | 976 | * (const char *) url . . . . . . The requested URL (with leading /). |
Glenn L McGrath | 1dc0cca | 2003-10-03 10:50:56 +0000 | [diff] [blame] | 977 | * (int bodyLen) . . . . . . . . Length of the post body. |
| 978 | * (const char *cookie) . . . . . For set HTTP_COOKIE. |
| 979 | * (const char *content_type) . . For set CONTENT_TYPE. |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 980 | * |
| 981 | * $Return: (char *) . . . . A pointer to the decoded string (same as input). |
| 982 | * |
| 983 | * $Errors: None |
| 984 | * |
| 985 | ****************************************************************************/ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 986 | static int sendCgi(const char *url, |
Denis Vlasenko | 6c5e5a0 | 2006-11-10 23:28:57 +0000 | [diff] [blame] | 987 | const char *request, int bodyLen, const char *cookie, |
| 988 | const char *content_type) |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 989 | { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 990 | int fromCgi[2]; /* pipe for reading data from CGI */ |
| 991 | int toCgi[2]; /* pipe for sending data to CGI */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 992 | char *fullpath; |
| 993 | char *argp[] = { NULL, NULL }; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 994 | int pid = 0; |
| 995 | int inFd; |
| 996 | int outFd; |
Denis Vlasenko | b5368bf | 2007-02-13 23:42:54 +0000 | [diff] [blame] | 997 | int buf_count; |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 998 | int status; |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 999 | size_t post_read_size, post_read_idx; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1000 | |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1001 | if (pipe(fromCgi) != 0) |
| 1002 | return 0; |
| 1003 | if (pipe(toCgi) != 0) |
| 1004 | return 0; |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1005 | |
Denis Vlasenko | 80281fe | 2007-03-07 22:16:38 +0000 | [diff] [blame] | 1006 | /* |
| 1007 | * Note: We can use vfork() here in the no-mmu case, although |
| 1008 | * the child modifies the parent's variables, due to: |
| 1009 | * 1) The parent does not use the child-modified variables. |
| 1010 | * 2) The allocated memory (in the child) is freed when the process |
| 1011 | * exits. This happens instantly after the child finishes, |
| 1012 | * since httpd is run from inetd (and it can't run standalone |
| 1013 | * in uClinux). |
| 1014 | */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1015 | |
| 1016 | // FIXME: setenv leaks memory! (old values of env vars are leaked) |
| 1017 | // Thus we have a bug on NOMMU. |
| 1018 | // Need to use this instead: |
| 1019 | // [malloc +] putenv + (in child: exec) + (in parent: unsetenv [+ free]) |
| 1020 | |
Denis Vlasenko | 473dae0 | 2007-04-11 07:04:23 +0000 | [diff] [blame] | 1021 | #if !BB_MMU |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1022 | fullpath = NULL; |
Denis Vlasenko | 80281fe | 2007-03-07 22:16:38 +0000 | [diff] [blame] | 1023 | pid = vfork(); |
| 1024 | #else |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1025 | pid = fork(); |
Denis Vlasenko | 80281fe | 2007-03-07 22:16:38 +0000 | [diff] [blame] | 1026 | #endif |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1027 | if (pid < 0) |
| 1028 | return 0; |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 1029 | |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1030 | if (!pid) { |
| 1031 | /* child process */ |
| 1032 | char *script; |
Denis Vlasenko | a305584 | 2007-02-11 19:51:06 +0000 | [diff] [blame] | 1033 | char *purl; |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1034 | |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1035 | if (accepted_socket > 1) |
| 1036 | close(accepted_socket); |
| 1037 | if (server_socket > 1) |
| 1038 | close(server_socket); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1039 | |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1040 | xmove_fd(toCgi[0], 0); /* replace stdin with the pipe */ |
| 1041 | xmove_fd(fromCgi[1], 1); /* replace stdout with the pipe */ |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1042 | close(fromCgi[0]); |
| 1043 | close(fromCgi[1]); |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1044 | /* Huh? User seeing stderr can be a security problem... |
| 1045 | * and if CGI really wants that, it can always dup2(1,2)... |
| 1046 | * dup2(fromCgi[1], 2); */ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1047 | |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1048 | /* |
| 1049 | * Find PATH_INFO. |
| 1050 | */ |
Denis Vlasenko | a305584 | 2007-02-11 19:51:06 +0000 | [diff] [blame] | 1051 | xfunc_error_retval = 242; |
| 1052 | purl = xstrdup(url); |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1053 | script = purl; |
| 1054 | while ((script = strchr(script + 1, '/')) != NULL) { |
| 1055 | /* have script.cgi/PATH_INFO or dirs/script.cgi[/PATH_INFO] */ |
| 1056 | struct stat sb; |
Denis Vlasenko | 9f60929 | 2006-11-05 19:47:33 +0000 | [diff] [blame] | 1057 | |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1058 | *script = '\0'; |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1059 | if (is_directory(purl + 1, 1, &sb) == 0) { |
| 1060 | /* not directory, found script.cgi/PATH_INFO */ |
| 1061 | *script = '/'; |
| 1062 | break; |
| 1063 | } |
| 1064 | *script = '/'; /* is directory, find next '/' */ |
| 1065 | } |
| 1066 | setenv1("PATH_INFO", script); /* set /PATH_INFO or "" */ |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1067 | setenv1("REQUEST_METHOD", request); |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1068 | if (g_query) { |
| 1069 | char *uri = alloca(strlen(purl) + 2 + strlen(g_query)); |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1070 | if (uri) |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1071 | sprintf(uri, "%s?%s", purl, g_query); |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1072 | setenv1("REQUEST_URI", uri); |
| 1073 | } else { |
| 1074 | setenv1("REQUEST_URI", purl); |
| 1075 | } |
| 1076 | if (script != NULL) |
Denis Vlasenko | 6c85ddc | 2006-11-21 00:08:39 +0000 | [diff] [blame] | 1077 | *script = '\0'; /* cut off /PATH_INFO */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1078 | |
| 1079 | /* SCRIPT_FILENAME required by PHP in CGI mode */ |
| 1080 | fullpath = concat_path_file(home_httpd, purl); |
| 1081 | setenv1("SCRIPT_FILENAME", fullpath); |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1082 | /* set SCRIPT_NAME as full path: /cgi-bin/dirs/script.cgi */ |
| 1083 | setenv1("SCRIPT_NAME", purl); |
Denis Vlasenko | 428f7ae | 2006-11-21 21:35:14 +0000 | [diff] [blame] | 1084 | /* http://hoohoo.ncsa.uiuc.edu/cgi/env.html: |
| 1085 | * QUERY_STRING: The information which follows the ? in the URL |
| 1086 | * which referenced this script. This is the query information. |
| 1087 | * It should not be decoded in any fashion. This variable |
| 1088 | * should always be set when there is query information, |
| 1089 | * regardless of command line decoding. */ |
Denis Vlasenko | a773af3 | 2007-01-03 23:02:18 +0000 | [diff] [blame] | 1090 | /* (Older versions of bbox seem to do some decoding) */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1091 | setenv1("QUERY_STRING", g_query); |
| 1092 | putenv((char*)"SERVER_SOFTWARE=busybox httpd/"BB_VER); |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 1093 | putenv((char*)"SERVER_PROTOCOL=HTTP/1.0"); |
| 1094 | putenv((char*)"GATEWAY_INTERFACE=CGI/1.1"); |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 1095 | /* Having _separate_ variables for IP and port defeats |
| 1096 | * the purpose of having socket abstraction. Which "port" |
| 1097 | * are you using on Unix domain socket? |
| 1098 | * IOW - REMOTE_PEER="1.2.3.4:56" makes much more sense. |
| 1099 | * Oh well... */ |
| 1100 | { |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1101 | char *p = rmt_ip_str ? : (char*)""; |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 1102 | char *cp = strrchr(p, ':'); |
| 1103 | if (ENABLE_FEATURE_IPV6 && cp && strchr(cp, ']')) |
| 1104 | cp = NULL; |
| 1105 | if (cp) *cp = '\0'; /* delete :PORT */ |
| 1106 | setenv1("REMOTE_ADDR", p); |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1107 | if (cp) *cp = ':'; |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 1108 | } |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1109 | #if ENABLE_FEATURE_HTTPD_SET_REMOTE_PORT_TO_ENV |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1110 | setenv_long("REMOTE_PORT", tcp_port); |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1111 | #endif |
| 1112 | if (bodyLen) |
| 1113 | setenv_long("CONTENT_LENGTH", bodyLen); |
| 1114 | if (cookie) |
| 1115 | setenv1("HTTP_COOKIE", cookie); |
| 1116 | if (content_type) |
| 1117 | setenv1("CONTENT_TYPE", content_type); |
| 1118 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1119 | if (remoteuser) { |
| 1120 | setenv1("REMOTE_USER", remoteuser); |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 1121 | putenv((char*)"AUTH_TYPE=Basic"); |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1122 | } |
| 1123 | #endif |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1124 | if (referer) |
| 1125 | setenv1("HTTP_REFERER", referer); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1126 | |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1127 | /* set execve argp[0] without path */ |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 1128 | argp[0] = (char*)bb_basename(purl); |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1129 | /* but script argp[0] must have absolute path and chdiring to this */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1130 | script = strrchr(fullpath, '/'); |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1131 | if (!script) |
| 1132 | goto error_execing_cgi; |
| 1133 | *script = '\0'; |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1134 | if (chdir(fullpath) == 0) { |
Denis Vlasenko | a773af3 | 2007-01-03 23:02:18 +0000 | [diff] [blame] | 1135 | // Now run the program. If it fails, |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1136 | // use _exit() so no destructors |
| 1137 | // get called and make a mess. |
| 1138 | #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR |
| 1139 | char *interpr = NULL; |
| 1140 | char *suffix = strrchr(purl, '.'); |
| 1141 | |
| 1142 | if (suffix) { |
| 1143 | Htaccess *cur; |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1144 | for (cur = script_i; cur; cur = cur->next) { |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1145 | if (strcmp(cur->before_colon + 1, suffix) == 0) { |
| 1146 | interpr = cur->after_colon; |
| 1147 | break; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1148 | } |
| 1149 | } |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1150 | } |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1151 | #endif |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1152 | *script = '/'; |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1153 | #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1154 | if (interpr) |
| 1155 | execv(interpr, argp); |
| 1156 | else |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1157 | #endif |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1158 | execv(fullpath, argp); |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1159 | } |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1160 | error_execing_cgi: |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1161 | /* send to stdout (even if we are not from inetd) */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1162 | accepted_socket = 1; |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1163 | sendHeaders(HTTP_NOT_FOUND); |
| 1164 | _exit(242); |
| 1165 | } /* end child */ |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1166 | |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1167 | /* parent process */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1168 | #if !BB_MMU |
| 1169 | free(fullpath); |
| 1170 | #endif |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1171 | |
Denis Vlasenko | b5368bf | 2007-02-13 23:42:54 +0000 | [diff] [blame] | 1172 | buf_count = 0; |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1173 | post_read_size = 0; |
| 1174 | post_read_idx = 0; /* for gcc */ |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame] | 1175 | inFd = fromCgi[0]; |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1176 | outFd = toCgi[1]; |
| 1177 | close(fromCgi[1]); |
| 1178 | close(toCgi[0]); |
| 1179 | signal(SIGPIPE, SIG_IGN); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1180 | |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1181 | while (1) { |
| 1182 | fd_set readSet; |
| 1183 | fd_set writeSet; |
| 1184 | char wbuf[128]; |
| 1185 | int nfound; |
| 1186 | int count; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1187 | |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1188 | FD_ZERO(&readSet); |
| 1189 | FD_ZERO(&writeSet); |
| 1190 | FD_SET(inFd, &readSet); |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1191 | if (bodyLen > 0 || post_read_size > 0) { |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1192 | FD_SET(outFd, &writeSet); |
| 1193 | nfound = outFd > inFd ? outFd : inFd; |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1194 | if (post_read_size == 0) { |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1195 | FD_SET(accepted_socket, &readSet); |
| 1196 | if (nfound < accepted_socket) |
| 1197 | nfound = accepted_socket; |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1198 | } |
| 1199 | /* Now wait on the set of sockets! */ |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1200 | nfound = select(nfound + 1, &readSet, &writeSet, NULL, NULL); |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1201 | } else { |
| 1202 | if (!bodyLen) { |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1203 | close(outFd); /* no more POST data to CGI */ |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1204 | bodyLen = -1; |
| 1205 | } |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1206 | nfound = select(inFd + 1, &readSet, NULL, NULL, NULL); |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1207 | } |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1208 | |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1209 | if (nfound <= 0) { |
Denis Vlasenko | b5368bf | 2007-02-13 23:42:54 +0000 | [diff] [blame] | 1210 | if (waitpid(pid, &status, WNOHANG) <= 0) { |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1211 | /* Weird. CGI didn't exit and no fd's |
Denis Vlasenko | b5368bf | 2007-02-13 23:42:54 +0000 | [diff] [blame] | 1212 | * are ready, yet select returned?! */ |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1213 | continue; |
Denis Vlasenko | b5368bf | 2007-02-13 23:42:54 +0000 | [diff] [blame] | 1214 | } |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1215 | close(inFd); |
| 1216 | if (DEBUG && WIFEXITED(status)) |
| 1217 | bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status)); |
| 1218 | if (DEBUG && WIFSIGNALED(status)) |
| 1219 | bb_error_msg("piped has exited with signal=%d", WTERMSIG(status)); |
| 1220 | break; |
| 1221 | } |
| 1222 | |
| 1223 | if (post_read_size > 0 && FD_ISSET(outFd, &writeSet)) { |
| 1224 | /* Have data from peer and can write to CGI */ |
| 1225 | // huh? why full_write? what if we will block? |
| 1226 | // (imagine that CGI does not read its stdin...) |
| 1227 | count = full_write(outFd, wbuf + post_read_idx, post_read_size); |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1228 | if (count > 0) { |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1229 | post_read_idx += count; |
| 1230 | post_read_size -= count; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1231 | } else { |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1232 | post_read_size = bodyLen = 0; /* broken pipe to CGI */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1233 | } |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1234 | } else if (bodyLen > 0 && post_read_size == 0 |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1235 | && FD_ISSET(accepted_socket, &readSet) |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1236 | ) { |
| 1237 | /* We expect data, prev data portion is eaten by CGI |
| 1238 | * and there *is* data to read from the peer |
Denis Vlasenko | b5368bf | 2007-02-13 23:42:54 +0000 | [diff] [blame] | 1239 | * (POSTDATA?) */ |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1240 | count = bodyLen > (int)sizeof(wbuf) ? (int)sizeof(wbuf) : bodyLen; |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1241 | count = safe_read(accepted_socket, wbuf, count); |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1242 | if (count > 0) { |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1243 | post_read_size = count; |
| 1244 | post_read_idx = 0; |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1245 | bodyLen -= count; |
| 1246 | } else { |
| 1247 | bodyLen = 0; /* closed */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1248 | } |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1249 | } |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1250 | |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1251 | #define PIPESIZE PIPE_BUF |
Eric Andersen | 97a1de1 | 2004-08-26 22:22:50 +0000 | [diff] [blame] | 1252 | #if PIPESIZE >= MAX_MEMORY_BUFF |
| 1253 | # error "PIPESIZE >= MAX_MEMORY_BUFF" |
| 1254 | #endif |
Denis Vlasenko | b5368bf | 2007-02-13 23:42:54 +0000 | [diff] [blame] | 1255 | if (FD_ISSET(inFd, &readSet)) { |
| 1256 | /* There is something to read from CGI */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1257 | int s = accepted_socket; |
| 1258 | char *rbuf = iobuf; |
Denis Vlasenko | a3ee69f | 2006-11-21 00:07:31 +0000 | [diff] [blame] | 1259 | |
Denis Vlasenko | b5368bf | 2007-02-13 23:42:54 +0000 | [diff] [blame] | 1260 | /* Are we still buffering CGI output? */ |
| 1261 | if (buf_count >= 0) { |
Denis Vlasenko | ec77ba1 | 2007-03-05 16:56:25 +0000 | [diff] [blame] | 1262 | static const char HTTP_200[] = "HTTP/1.0 200 OK\r\n"; |
Denis Vlasenko | b5368bf | 2007-02-13 23:42:54 +0000 | [diff] [blame] | 1263 | /* Must use safe_read, not full_read, because |
| 1264 | * CGI may output a few first bytes and then wait |
| 1265 | * for POSTDATA without closing stdout. |
| 1266 | * With full_read we may wait here forever. */ |
| 1267 | count = safe_read(inFd, rbuf + buf_count, PIPESIZE - 4); |
| 1268 | if (count <= 0) { |
| 1269 | /* eof (or error) and there was no "HTTP", |
| 1270 | * so add one and write out the received data */ |
| 1271 | if (buf_count) { |
| 1272 | full_write(s, HTTP_200, sizeof(HTTP_200)-1); |
| 1273 | full_write(s, rbuf, buf_count); |
| 1274 | } |
| 1275 | break; /* closed */ |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1276 | } |
Denis Vlasenko | b5368bf | 2007-02-13 23:42:54 +0000 | [diff] [blame] | 1277 | buf_count += count; |
| 1278 | count = 0; |
| 1279 | if (buf_count >= 4) { |
| 1280 | /* check to see if CGI added "HTTP" */ |
| 1281 | if (memcmp(rbuf, HTTP_200, 4) != 0) { |
| 1282 | /* there is no "HTTP", do it ourself */ |
| 1283 | if (full_write(s, HTTP_200, sizeof(HTTP_200)-1) != sizeof(HTTP_200)-1) |
| 1284 | break; |
| 1285 | } |
| 1286 | /* example of valid CGI without "Content-type:" |
| 1287 | * echo -en "HTTP/1.0 302 Found\r\n" |
| 1288 | * echo -en "Location: http://www.busybox.net\r\n" |
| 1289 | * echo -en "\r\n" |
| 1290 | if (!strstr(rbuf, "ontent-")) { |
| 1291 | full_write(s, "Content-type: text/plain\r\n\r\n", 28); |
| 1292 | } |
| 1293 | */ |
| 1294 | count = buf_count; |
| 1295 | buf_count = -1; /* buffering off */ |
Denis Vlasenko | a305584 | 2007-02-11 19:51:06 +0000 | [diff] [blame] | 1296 | } |
Denis Vlasenko | b5368bf | 2007-02-13 23:42:54 +0000 | [diff] [blame] | 1297 | } else { |
| 1298 | count = safe_read(inFd, rbuf, PIPESIZE); |
| 1299 | if (count <= 0) |
| 1300 | break; /* eof (or error) */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1301 | } |
Denis Vlasenko | 6998142 | 2007-01-07 21:25:12 +0000 | [diff] [blame] | 1302 | if (full_write(s, rbuf, count) != count) |
| 1303 | break; |
| 1304 | if (DEBUG) |
| 1305 | fprintf(stderr, "cgi read %d bytes: '%.*s'\n", count, count, rbuf); |
| 1306 | } /* if (FD_ISSET(inFd)) */ |
| 1307 | } /* while (1) */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1308 | return 0; |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1309 | } |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1310 | #endif /* FEATURE_HTTPD_CGI */ |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1311 | |
| 1312 | /**************************************************************************** |
| 1313 | * |
| 1314 | > $Function: sendFile() |
| 1315 | * |
Denis Vlasenko | 6c5e5a0 | 2006-11-10 23:28:57 +0000 | [diff] [blame] | 1316 | * $Description: Send a file response to a HTTP request |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1317 | * |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1318 | * $Parameter: |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1319 | * (const char *) url . . The URL requested. |
| 1320 | * |
| 1321 | * $Return: (int) . . . . . . Always 0. |
| 1322 | * |
| 1323 | ****************************************************************************/ |
Glenn L McGrath | 1dc0cca | 2003-10-03 10:50:56 +0000 | [diff] [blame] | 1324 | static int sendFile(const char *url) |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1325 | { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1326 | char * suffix; |
| 1327 | int f; |
| 1328 | const char * const * table; |
| 1329 | const char * try_suffix; |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1330 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1331 | suffix = strrchr(url, '.'); |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1332 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1333 | for (table = suffixTable; *table; table += 2) |
| 1334 | if (suffix != NULL && (try_suffix = strstr(*table, suffix)) != 0) { |
| 1335 | try_suffix += strlen(suffix); |
| 1336 | if (*try_suffix == 0 || *try_suffix == '.') |
| 1337 | break; |
| 1338 | } |
| 1339 | /* also, if not found, set default as "application/octet-stream"; */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1340 | found_mime_type = table[1]; |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 1341 | #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1342 | if (suffix) { |
| 1343 | Htaccess * cur; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1344 | |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1345 | for (cur = mime_a; cur; cur = cur->next) { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1346 | if (strcmp(cur->before_colon, suffix) == 0) { |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1347 | found_mime_type = cur->after_colon; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1348 | break; |
| 1349 | } |
| 1350 | } |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1351 | } |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1352 | #endif /* FEATURE_HTTPD_CONFIG_WITH_MIME_TYPES */ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1353 | |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1354 | if (DEBUG) |
| 1355 | fprintf(stderr, "sending file '%s' content-type: %s\n", |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1356 | url, found_mime_type); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1357 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1358 | f = open(url, O_RDONLY); |
| 1359 | if (f >= 0) { |
| 1360 | int count; |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1361 | char *buf = iobuf; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1362 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1363 | sendHeaders(HTTP_OK); |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1364 | /* TODO: sendfile() */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1365 | while ((count = full_read(f, buf, MAX_MEMORY_BUFF)) > 0) { |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1366 | int fd = accepted_socket; |
Denis Vlasenko | b64eed6 | 2007-01-14 17:06:11 +0000 | [diff] [blame] | 1367 | if (fd == 0) fd++; /* write to fd# 1 in inetd mode */ |
| 1368 | if (full_write(fd, buf, count) != count) |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1369 | break; |
| 1370 | } |
| 1371 | close(f); |
| 1372 | } else { |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1373 | if (DEBUG) |
| 1374 | bb_perror_msg("cannot open '%s'", url); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1375 | sendHeaders(HTTP_NOT_FOUND); |
| 1376 | } |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1377 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1378 | return 0; |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 1381 | static int checkPermIP(void) |
| 1382 | { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1383 | Htaccess_IP * cur; |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 1384 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1385 | /* This could stand some work */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1386 | for (cur = ip_a_d; cur; cur = cur->next) { |
Denis Vlasenko | b64eed6 | 2007-01-14 17:06:11 +0000 | [diff] [blame] | 1387 | #if ENABLE_FEATURE_HTTPD_CGI && DEBUG |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1388 | fprintf(stderr, "checkPermIP: '%s' ? ", rmt_ip_str); |
Denis Vlasenko | b64eed6 | 2007-01-14 17:06:11 +0000 | [diff] [blame] | 1389 | #endif |
| 1390 | #if DEBUG |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 1391 | fprintf(stderr, "'%u.%u.%u.%u/%u.%u.%u.%u'\n", |
| 1392 | (unsigned char)(cur->ip >> 24), |
| 1393 | (unsigned char)(cur->ip >> 16), |
| 1394 | (unsigned char)(cur->ip >> 8), |
| 1395 | (unsigned char)(cur->ip), |
| 1396 | (unsigned char)(cur->mask >> 24), |
| 1397 | (unsigned char)(cur->mask >> 16), |
| 1398 | (unsigned char)(cur->mask >> 8), |
| 1399 | (unsigned char)(cur->mask) |
| 1400 | ); |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 1401 | #endif |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1402 | if ((rmt_ip & cur->mask) == cur->ip) |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1403 | return cur->allow_deny == 'A'; /* Allow/Deny */ |
| 1404 | } |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 1405 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1406 | /* if unconfigured, return 1 - access from all */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1407 | return !flg_deny_all; |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1410 | /**************************************************************************** |
| 1411 | * |
| 1412 | > $Function: checkPerm() |
| 1413 | * |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 1414 | * $Description: Check the permission file for access password protected. |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1415 | * |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1416 | * If config file isn't present, everything is allowed. |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1417 | * Entries are of the form you can see example from header source |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1418 | * |
| 1419 | * $Parameters: |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 1420 | * (const char *) path . . . . The file path. |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1421 | * (const char *) request . . . User information to validate. |
| 1422 | * |
| 1423 | * $Return: (int) . . . . . . . . . 1 if request OK, 0 otherwise. |
| 1424 | * |
| 1425 | ****************************************************************************/ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1426 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 1427 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1428 | static int checkPerm(const char *path, const char *request) |
| 1429 | { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1430 | Htaccess * cur; |
| 1431 | const char *p; |
| 1432 | const char *p0; |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1433 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1434 | const char *prev = NULL; |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1435 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1436 | /* This could stand some work */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1437 | for (cur = g_auth; cur; cur = cur->next) { |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1438 | size_t l; |
| 1439 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1440 | p0 = cur->before_colon; |
| 1441 | if (prev != NULL && strcmp(prev, p0) != 0) |
| 1442 | continue; /* find next identical */ |
| 1443 | p = cur->after_colon; |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1444 | if (DEBUG) |
| 1445 | fprintf(stderr, "checkPerm: '%s' ? '%s'\n", p0, request); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1446 | |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1447 | l = strlen(p0); |
| 1448 | if (strncmp(p0, path, l) == 0 |
| 1449 | && (l == 1 || path[l] == '/' || path[l] == '\0') |
| 1450 | ) { |
| 1451 | char *u; |
| 1452 | /* path match found. Check request */ |
| 1453 | /* for check next /path:user:password */ |
| 1454 | prev = p0; |
| 1455 | u = strchr(request, ':'); |
| 1456 | if (u == NULL) { |
| 1457 | /* bad request, ':' required */ |
| 1458 | break; |
| 1459 | } |
Glenn L McGrath | 1dc0cca | 2003-10-03 10:50:56 +0000 | [diff] [blame] | 1460 | |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1461 | if (ENABLE_FEATURE_HTTPD_AUTH_MD5) { |
| 1462 | char *cipher; |
| 1463 | char *pp; |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 1464 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1465 | if (strncmp(p, request, u-request) != 0) { |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1466 | /* user uncompared */ |
| 1467 | continue; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1468 | } |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1469 | pp = strchr(p, ':'); |
| 1470 | if (pp && pp[1] == '$' && pp[2] == '1' && |
| 1471 | pp[3] == '$' && pp[4]) { |
| 1472 | pp++; |
| 1473 | cipher = pw_encrypt(u+1, pp); |
| 1474 | if (strcmp(cipher, pp) == 0) |
| 1475 | goto set_remoteuser_var; /* Ok */ |
| 1476 | /* unauthorized */ |
| 1477 | continue; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1478 | } |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1479 | } |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1480 | |
| 1481 | if (strcmp(p, request) == 0) { |
| 1482 | set_remoteuser_var: |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1483 | remoteuser = strdup(request); |
| 1484 | if (remoteuser) |
| 1485 | remoteuser[(u - request)] = 0; |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1486 | return 1; /* Ok */ |
| 1487 | } |
| 1488 | /* unauthorized */ |
Glenn L McGrath | 1dc0cca | 2003-10-03 10:50:56 +0000 | [diff] [blame] | 1489 | } |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1490 | } /* for */ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1491 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1492 | return prev == NULL; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1493 | } |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1494 | |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1495 | #endif /* FEATURE_HTTPD_BASIC_AUTH */ |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 1496 | |
Eric Andersen | 07f2fea | 2004-10-08 08:03:29 +0000 | [diff] [blame] | 1497 | /**************************************************************************** |
| 1498 | * |
Mike Frysinger | bb12d6f | 2006-01-03 23:59:01 +0000 | [diff] [blame] | 1499 | > $Function: handle_sigalrm() |
Eric Andersen | 07f2fea | 2004-10-08 08:03:29 +0000 | [diff] [blame] | 1500 | * |
Mike Frysinger | bb12d6f | 2006-01-03 23:59:01 +0000 | [diff] [blame] | 1501 | * $Description: Handle timeouts |
Eric Andersen | 07f2fea | 2004-10-08 08:03:29 +0000 | [diff] [blame] | 1502 | * |
| 1503 | ****************************************************************************/ |
| 1504 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1505 | static void handle_sigalrm(int sig) |
Eric Andersen | 07f2fea | 2004-10-08 08:03:29 +0000 | [diff] [blame] | 1506 | { |
Denis Vlasenko | 6c5e5a0 | 2006-11-10 23:28:57 +0000 | [diff] [blame] | 1507 | sendHeaders(HTTP_REQUEST_TIMEOUT); |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1508 | alarm_signaled = 1; |
Eric Andersen | 07f2fea | 2004-10-08 08:03:29 +0000 | [diff] [blame] | 1509 | } |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1510 | |
| 1511 | /**************************************************************************** |
| 1512 | * |
| 1513 | > $Function: handleIncoming() |
| 1514 | * |
| 1515 | * $Description: Handle an incoming http request. |
| 1516 | * |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1517 | ****************************************************************************/ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1518 | static void handleIncoming(void) |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1519 | { |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1520 | char *buf = iobuf; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1521 | char *url; |
| 1522 | char *purl; |
| 1523 | int blank = -1; |
| 1524 | char *test; |
| 1525 | struct stat sb; |
| 1526 | int ip_allowed; |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 1527 | #if ENABLE_FEATURE_HTTPD_CGI |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1528 | const char *prequest = request_GET; |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1529 | unsigned long length = 0; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1530 | char *cookie = 0; |
| 1531 | char *content_type = 0; |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1532 | #endif |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1533 | fd_set s_fd; |
| 1534 | struct timeval tv; |
| 1535 | int retval; |
| 1536 | struct sigaction sa; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1537 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 1538 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
Denis Vlasenko | 6c5e5a0 | 2006-11-10 23:28:57 +0000 | [diff] [blame] | 1539 | int credentials = -1; /* if not required this is Ok */ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1540 | #endif |
| 1541 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1542 | sa.sa_handler = handle_sigalrm; |
| 1543 | sigemptyset(&sa.sa_mask); |
| 1544 | sa.sa_flags = 0; /* no SA_RESTART */ |
| 1545 | sigaction(SIGALRM, &sa, NULL); |
Eric Andersen | 07f2fea | 2004-10-08 08:03:29 +0000 | [diff] [blame] | 1546 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1547 | do { |
| 1548 | int count; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1549 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1550 | (void) alarm(TIMEOUT); |
| 1551 | if (getLine() <= 0) |
| 1552 | break; /* closed */ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1553 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1554 | purl = strpbrk(buf, " \t"); |
| 1555 | if (purl == NULL) { |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1556 | BAD_REQUEST: |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1557 | sendHeaders(HTTP_BAD_REQUEST); |
| 1558 | break; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1559 | } |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1560 | *purl = '\0'; |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 1561 | #if ENABLE_FEATURE_HTTPD_CGI |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1562 | if (strcasecmp(buf, prequest) != 0) { |
| 1563 | prequest = "POST"; |
| 1564 | if (strcasecmp(buf, prequest) != 0) { |
| 1565 | sendHeaders(HTTP_NOT_IMPLEMENTED); |
| 1566 | break; |
| 1567 | } |
| 1568 | } |
| 1569 | #else |
| 1570 | if (strcasecmp(buf, request_GET) != 0) { |
| 1571 | sendHeaders(HTTP_NOT_IMPLEMENTED); |
| 1572 | break; |
| 1573 | } |
| 1574 | #endif |
| 1575 | *purl = ' '; |
| 1576 | count = sscanf(purl, " %[^ ] HTTP/%d.%*d", buf, &blank); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1577 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1578 | if (count < 1 || buf[0] != '/') { |
| 1579 | /* Garbled request/URL */ |
| 1580 | goto BAD_REQUEST; |
| 1581 | } |
Denis Vlasenko | 6c5e5a0 | 2006-11-10 23:28:57 +0000 | [diff] [blame] | 1582 | url = alloca(strlen(buf) + sizeof("/index.html")); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1583 | if (url == NULL) { |
| 1584 | sendHeaders(HTTP_INTERNAL_SERVER_ERROR); |
| 1585 | break; |
| 1586 | } |
| 1587 | strcpy(url, buf); |
| 1588 | /* extract url args if present */ |
| 1589 | test = strchr(url, '?'); |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1590 | g_query = NULL; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1591 | if (test) { |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1592 | *test++ = '\0'; |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1593 | g_query = test; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1594 | } |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1595 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1596 | test = decodeString(url, 0); |
| 1597 | if (test == NULL) |
| 1598 | goto BAD_REQUEST; |
Denis Vlasenko | a35c9e9 | 2006-11-29 15:58:50 +0000 | [diff] [blame] | 1599 | if (test == url+1) { |
| 1600 | /* '/' or NUL is encoded */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1601 | sendHeaders(HTTP_NOT_FOUND); |
| 1602 | break; |
| 1603 | } |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1604 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1605 | /* algorithm stolen from libbb bb_simplify_path(), |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 1606 | * but don't strdup and reducing trailing slash and protect out root */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1607 | purl = test = url; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1608 | do { |
| 1609 | if (*purl == '/') { |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1610 | /* skip duplicate (or initial) slash */ |
| 1611 | if (*test == '/') { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1612 | continue; |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1613 | } |
| 1614 | if (*test == '.') { |
| 1615 | /* skip extra '.' */ |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 1616 | if (test[1] == '/' || !test[1]) { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1617 | continue; |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 1618 | } |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1619 | /* '..': be careful */ |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 1620 | if (test[1] == '.' && (test[2] == '/' || !test[2])) { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1621 | ++test; |
| 1622 | if (purl == url) { |
| 1623 | /* protect out root */ |
| 1624 | goto BAD_REQUEST; |
| 1625 | } |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 1626 | while (*--purl != '/') /* omit previous dir */; |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 1627 | continue; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1628 | } |
| 1629 | } |
| 1630 | } |
| 1631 | *++purl = *test; |
| 1632 | } while (*++test); |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1633 | *++purl = '\0'; /* so keep last character */ |
| 1634 | test = purl; /* end ptr */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1635 | |
| 1636 | /* If URL is directory, adding '/' */ |
| 1637 | if (test[-1] != '/') { |
| 1638 | if (is_directory(url + 1, 1, &sb)) { |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1639 | found_moved_temporarily = url; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1640 | } |
| 1641 | } |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1642 | if (DEBUG) |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1643 | fprintf(stderr, "url='%s', args=%s\n", url, g_query); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1644 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1645 | test = url; |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 1646 | ip_allowed = checkPermIP(); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1647 | while (ip_allowed && (test = strchr(test + 1, '/')) != NULL) { |
| 1648 | /* have path1/path2 */ |
| 1649 | *test = '\0'; |
| 1650 | if (is_directory(url + 1, 1, &sb)) { |
| 1651 | /* may be having subdir config */ |
| 1652 | parse_conf(url + 1, SUBDIR_PARSE); |
| 1653 | ip_allowed = checkPermIP(); |
| 1654 | } |
| 1655 | *test = '/'; |
| 1656 | } |
| 1657 | if (blank >= 0) { |
Denis Vlasenko | 6c85ddc | 2006-11-21 00:08:39 +0000 | [diff] [blame] | 1658 | /* read until blank line for HTTP version specified, else parse immediate */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1659 | while (1) { |
| 1660 | alarm(TIMEOUT); |
| 1661 | count = getLine(); |
| 1662 | if (count <= 0) |
| 1663 | break; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1664 | |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1665 | if (DEBUG) |
| 1666 | fprintf(stderr, "header: '%s'\n", buf); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1667 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 1668 | #if ENABLE_FEATURE_HTTPD_CGI |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1669 | /* try and do our best to parse more lines */ |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1670 | if ((STRNCASECMP(buf, "Content-length:") == 0)) { |
| 1671 | /* extra read only for POST */ |
| 1672 | if (prequest != request_GET) { |
| 1673 | test = buf + sizeof("Content-length:")-1; |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 1674 | if (!test[0]) |
| 1675 | goto bail_out; |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1676 | errno = 0; |
| 1677 | /* not using strtoul: it ignores leading munis! */ |
| 1678 | length = strtol(test, &test, 10); |
| 1679 | /* length is "ulong", but we need to pass it to int later */ |
| 1680 | /* so we check for negative or too large values in one go: */ |
Denis Vlasenko | 6c85ddc | 2006-11-21 00:08:39 +0000 | [diff] [blame] | 1681 | /* (long -> ulong conv caused negatives to be seen as > INT_MAX) */ |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1682 | if (test[0] || errno || length > INT_MAX) |
| 1683 | goto bail_out; |
| 1684 | } |
| 1685 | } else if ((STRNCASECMP(buf, "Cookie:") == 0)) { |
| 1686 | cookie = strdup(skip_whitespace(buf + sizeof("Cookie:")-1)); |
| 1687 | } else if ((STRNCASECMP(buf, "Content-Type:") == 0)) { |
| 1688 | content_type = strdup(skip_whitespace(buf + sizeof("Content-Type:")-1)); |
| 1689 | } else if ((STRNCASECMP(buf, "Referer:") == 0)) { |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1690 | referer = strdup(skip_whitespace(buf + sizeof("Referer:")-1)); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1691 | } |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1692 | #endif |
| 1693 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 1694 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1695 | if (STRNCASECMP(buf, "Authorization:") == 0) { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1696 | /* We only allow Basic credentials. |
| 1697 | * It shows up as "Authorization: Basic <userid:password>" where |
| 1698 | * the userid:password is base64 encoded. |
| 1699 | */ |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1700 | test = skip_whitespace(buf + sizeof("Authorization:")-1); |
| 1701 | if (STRNCASECMP(test, "Basic") != 0) |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1702 | continue; |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1703 | test += sizeof("Basic")-1; |
| 1704 | /* decodeBase64() skips whitespace itself */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1705 | decodeBase64(test); |
| 1706 | credentials = checkPerm(url, test); |
| 1707 | } |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1708 | #endif /* FEATURE_HTTPD_BASIC_AUTH */ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1709 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1710 | } /* while extra header reading */ |
| 1711 | } |
Denis Vlasenko | 6c85ddc | 2006-11-21 00:08:39 +0000 | [diff] [blame] | 1712 | alarm(0); |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1713 | if (alarm_signaled) |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1714 | break; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1715 | |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 1716 | if (strcmp(bb_basename(url), httpd_conf) == 0 || ip_allowed == 0) { |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1717 | /* protect listing [/path]/httpd_conf or IP deny */ |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 1718 | #if ENABLE_FEATURE_HTTPD_CGI |
Denis Vlasenko | 6c85ddc | 2006-11-21 00:08:39 +0000 | [diff] [blame] | 1719 | FORBIDDEN: /* protect listing /cgi-bin */ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1720 | #endif |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1721 | sendHeaders(HTTP_FORBIDDEN); |
| 1722 | break; |
| 1723 | } |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1724 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 1725 | #if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1726 | if (credentials <= 0 && checkPerm(url, ":") == 0) { |
| 1727 | sendHeaders(HTTP_UNAUTHORIZED); |
| 1728 | break; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1729 | } |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1730 | #endif |
| 1731 | |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1732 | if (found_moved_temporarily) { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1733 | sendHeaders(HTTP_MOVED_TEMPORARILY); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1734 | /* clear unforked memory flag */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1735 | found_moved_temporarily = NULL; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1736 | break; |
| 1737 | } |
| 1738 | |
| 1739 | test = url + 1; /* skip first '/' */ |
| 1740 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 1741 | #if ENABLE_FEATURE_HTTPD_CGI |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1742 | if (strncmp(test, "cgi-bin", 7) == 0) { |
| 1743 | if (test[7] == '/' && test[8] == 0) |
Denis Vlasenko | 6c85ddc | 2006-11-21 00:08:39 +0000 | [diff] [blame] | 1744 | goto FORBIDDEN; /* protect listing cgi-bin/ */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1745 | sendCgi(url, prequest, length, cookie, content_type); |
Denis Vlasenko | 6c85ddc | 2006-11-21 00:08:39 +0000 | [diff] [blame] | 1746 | break; |
| 1747 | } |
Denis Vlasenko | 1ccd96f | 2007-03-05 19:24:33 +0000 | [diff] [blame] | 1748 | #if ENABLE_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR |
| 1749 | { |
| 1750 | char *suffix = strrchr(test, '.'); |
| 1751 | if (suffix) { |
| 1752 | Htaccess *cur; |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1753 | for (cur = script_i; cur; cur = cur->next) { |
Denis Vlasenko | 1ccd96f | 2007-03-05 19:24:33 +0000 | [diff] [blame] | 1754 | if (strcmp(cur->before_colon + 1, suffix) == 0) { |
| 1755 | sendCgi(url, prequest, length, cookie, content_type); |
| 1756 | goto bail_out; |
| 1757 | } |
| 1758 | } |
| 1759 | } |
| 1760 | } |
| 1761 | #endif |
Denis Vlasenko | 6c85ddc | 2006-11-21 00:08:39 +0000 | [diff] [blame] | 1762 | if (prequest != request_GET) { |
| 1763 | sendHeaders(HTTP_NOT_IMPLEMENTED); |
| 1764 | break; |
| 1765 | } |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame] | 1766 | #endif /* FEATURE_HTTPD_CGI */ |
| 1767 | if (purl[-1] == '/') |
| 1768 | strcpy(purl, "index.html"); |
| 1769 | if (stat(test, &sb) == 0) { |
| 1770 | /* It's a dir URL and there is index.html */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1771 | ContentLength = sb.st_size; |
| 1772 | last_mod = sb.st_mtime; |
Denis Vlasenko | 5d148e2 | 2006-11-21 00:12:09 +0000 | [diff] [blame] | 1773 | } |
| 1774 | #if ENABLE_FEATURE_HTTPD_CGI |
| 1775 | else if (purl[-1] == '/') { |
| 1776 | /* It's a dir URL and there is no index.html |
| 1777 | * Try cgi-bin/index.cgi */ |
| 1778 | if (access("/cgi-bin/index.cgi"+1, X_OK) == 0) { |
| 1779 | purl[0] = '\0'; |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1780 | g_query = url; |
Denis Vlasenko | 6c85ddc | 2006-11-21 00:08:39 +0000 | [diff] [blame] | 1781 | sendCgi("/cgi-bin/index.cgi", prequest, length, cookie, content_type); |
| 1782 | break; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1783 | } |
| 1784 | } |
Denis Vlasenko | 6c85ddc | 2006-11-21 00:08:39 +0000 | [diff] [blame] | 1785 | #endif /* FEATURE_HTTPD_CGI */ |
Denis Vlasenko | 6c85ddc | 2006-11-21 00:08:39 +0000 | [diff] [blame] | 1786 | sendFile(test); |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1787 | ContentLength = -1; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1788 | } while (0); |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1789 | |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 1790 | #if ENABLE_FEATURE_HTTPD_CGI |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1791 | bail_out: |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 1792 | #endif |
Denis Vlasenko | 0bb993f | 2006-11-21 00:06:28 +0000 | [diff] [blame] | 1793 | |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1794 | if (DEBUG) |
| 1795 | fprintf(stderr, "closing socket\n\n"); |
| 1796 | #if ENABLE_FEATURE_HTTPD_CGI |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1797 | free(cookie); |
| 1798 | free(content_type); |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1799 | free(referer); |
| 1800 | referer = NULL; |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1801 | # if ENABLE_FEATURE_HTTPD_BASIC_AUTH |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1802 | free(remoteuser); |
| 1803 | remoteuser = NULL; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1804 | # endif |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1805 | #endif |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1806 | shutdown(accepted_socket, SHUT_WR); |
Eric Andersen | d8746cd | 2004-02-24 07:28:38 +0000 | [diff] [blame] | 1807 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1808 | /* Properly wait for remote to closed */ |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 1809 | FD_ZERO(&s_fd); |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1810 | FD_SET(accepted_socket, &s_fd); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1811 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1812 | do { |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 1813 | tv.tv_sec = 2; |
| 1814 | tv.tv_usec = 0; |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1815 | retval = select(accepted_socket + 1, &s_fd, NULL, NULL, &tv); |
| 1816 | } while (retval > 0 && read(accepted_socket, buf, sizeof(iobuf) > 0)); |
Eric Andersen | d8746cd | 2004-02-24 07:28:38 +0000 | [diff] [blame] | 1817 | |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1818 | shutdown(accepted_socket, SHUT_RD); |
Denis Vlasenko | 0871bc8 | 2006-11-16 16:17:02 +0000 | [diff] [blame] | 1819 | /* In inetd case, we close fd 1 (stdout) here. We will exit soon anyway */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1820 | close(accepted_socket); |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Denis Vlasenko | 56258b6 | 2007-06-23 23:14:02 +0000 | [diff] [blame] | 1823 | #if BB_MMU |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1824 | /**************************************************************************** |
| 1825 | * |
| 1826 | > $Function: miniHttpd() |
| 1827 | * |
| 1828 | * $Description: The main http server function. |
| 1829 | * |
| 1830 | * Given an open socket fildes, listen for new connections and farm out |
| 1831 | * the processing as a forked process. |
| 1832 | * |
| 1833 | * $Parameters: |
| 1834 | * (int) server. . . The server socket fildes. |
| 1835 | * |
| 1836 | * $Return: (int) . . . . Always 0. |
| 1837 | * |
| 1838 | ****************************************************************************/ |
| 1839 | static int miniHttpd(int server) |
| 1840 | { |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1841 | fd_set readfd, portfd; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1842 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1843 | FD_ZERO(&portfd); |
| 1844 | FD_SET(server, &portfd); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1845 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1846 | /* copy the ports we are watching to the readfd set */ |
| 1847 | while (1) { |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 1848 | int s; |
| 1849 | union { |
| 1850 | struct sockaddr sa; |
| 1851 | struct sockaddr_in sin; |
| 1852 | USE_FEATURE_IPV6(struct sockaddr_in6 sin6;) |
| 1853 | } fromAddr; |
| 1854 | socklen_t fromAddrLen = sizeof(fromAddr); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1855 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1856 | /* Now wait INDEFINITELY on the set of sockets! */ |
Denis Vlasenko | 6c5e5a0 | 2006-11-10 23:28:57 +0000 | [diff] [blame] | 1857 | readfd = portfd; |
| 1858 | if (select(server + 1, &readfd, 0, 0, 0) <= 0) |
| 1859 | continue; |
| 1860 | if (!FD_ISSET(server, &readfd)) |
| 1861 | continue; |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 1862 | s = accept(server, &fromAddr.sa, &fromAddrLen); |
Denis Vlasenko | 6c5e5a0 | 2006-11-10 23:28:57 +0000 | [diff] [blame] | 1863 | if (s < 0) |
| 1864 | continue; |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1865 | accepted_socket = s; |
| 1866 | rmt_ip = 0; |
| 1867 | tcp_port = 0; |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 1868 | #if ENABLE_FEATURE_HTTPD_CGI || DEBUG |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1869 | free(rmt_ip_str); |
| 1870 | rmt_ip_str = xmalloc_sockaddr2dotted(&fromAddr.sa, fromAddrLen); |
"Vladimir N. Oleynik" | 6b903a2 | 2005-12-20 11:02:54 +0000 | [diff] [blame] | 1871 | #if DEBUG |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1872 | bb_error_msg("connection from '%s'", rmt_ip_str); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1873 | #endif |
Denis Vlasenko | b3a0715 | 2006-11-16 18:04:43 +0000 | [diff] [blame] | 1874 | #endif /* FEATURE_HTTPD_CGI */ |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 1875 | if (fromAddr.sa.sa_family == AF_INET) { |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1876 | rmt_ip = ntohl(fromAddr.sin.sin_addr.s_addr); |
| 1877 | tcp_port = ntohs(fromAddr.sin.sin_port); |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 1878 | } |
| 1879 | #if ENABLE_FEATURE_IPV6 |
| 1880 | if (fromAddr.sa.sa_family == AF_INET6) { |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1881 | //rmt_ip = ntohl(fromAddr.sin.sin_addr.s_addr); |
| 1882 | tcp_port = ntohs(fromAddr.sin6.sin6_port); |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 1883 | } |
| 1884 | #endif |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 1885 | |
Denis Vlasenko | 6c5e5a0 | 2006-11-10 23:28:57 +0000 | [diff] [blame] | 1886 | /* set the KEEPALIVE option to cull dead connections */ |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 1887 | setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1)); |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 1888 | |
| 1889 | if (DEBUG || fork() == 0) { |
| 1890 | /* child */ |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 1891 | #if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP |
Denis Vlasenko | 6c5e5a0 | 2006-11-10 23:28:57 +0000 | [diff] [blame] | 1892 | /* protect reload config, may be confuse checking */ |
| 1893 | signal(SIGHUP, SIG_IGN); |
Glenn L McGrath | c9163fe | 2003-05-13 16:20:11 +0000 | [diff] [blame] | 1894 | #endif |
Denis Vlasenko | 6c5e5a0 | 2006-11-10 23:28:57 +0000 | [diff] [blame] | 1895 | handleIncoming(); |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 1896 | if (!DEBUG) |
| 1897 | exit(0); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1898 | } |
Denis Vlasenko | 6c5e5a0 | 2006-11-10 23:28:57 +0000 | [diff] [blame] | 1899 | close(s); |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 1900 | } /* while (1) */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1901 | return 0; |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1902 | } |
Denis Vlasenko | 56258b6 | 2007-06-23 23:14:02 +0000 | [diff] [blame] | 1903 | #endif |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1904 | |
Denis Vlasenko | 0871bc8 | 2006-11-16 16:17:02 +0000 | [diff] [blame] | 1905 | /* from inetd */ |
| 1906 | static int miniHttpd_inetd(void) |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 1907 | { |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 1908 | union { |
| 1909 | struct sockaddr sa; |
| 1910 | struct sockaddr_in sin; |
| 1911 | USE_FEATURE_IPV6(struct sockaddr_in6 sin6;) |
| 1912 | } fromAddr; |
| 1913 | socklen_t fromAddrLen = sizeof(fromAddr); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1914 | |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 1915 | getpeername(0, &fromAddr.sa, &fromAddrLen); |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1916 | rmt_ip = 0; |
| 1917 | tcp_port = 0; |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 1918 | #if ENABLE_FEATURE_HTTPD_CGI || DEBUG |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1919 | free(rmt_ip_str); |
| 1920 | rmt_ip_str = xmalloc_sockaddr2dotted(&fromAddr.sa, fromAddrLen); |
Glenn L McGrath | b65422c | 2003-09-08 10:59:27 +0000 | [diff] [blame] | 1921 | #endif |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 1922 | if (fromAddr.sa.sa_family == AF_INET) { |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1923 | rmt_ip = ntohl(fromAddr.sin.sin_addr.s_addr); |
| 1924 | tcp_port = ntohs(fromAddr.sin.sin_port); |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 1925 | } |
| 1926 | #if ENABLE_FEATURE_IPV6 |
| 1927 | if (fromAddr.sa.sa_family == AF_INET6) { |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1928 | //rmt_ip = ntohl(fromAddr.sin.sin_addr.s_addr); |
| 1929 | tcp_port = ntohs(fromAddr.sin6.sin6_port); |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 1930 | } |
| 1931 | #endif |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1932 | handleIncoming(); |
| 1933 | return 0; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1934 | } |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1935 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 1936 | #if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1937 | static void sighup_handler(int sig) |
| 1938 | { |
| 1939 | /* set and reset */ |
| 1940 | struct sigaction sa; |
| 1941 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1942 | parse_conf(default_path_httpd_conf, sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1943 | sa.sa_handler = sighup_handler; |
| 1944 | sigemptyset(&sa.sa_mask); |
| 1945 | sa.sa_flags = SA_RESTART; |
| 1946 | sigaction(SIGHUP, &sa, NULL); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1947 | } |
| 1948 | #endif |
| 1949 | |
Denis Vlasenko | 9f60929 | 2006-11-05 19:47:33 +0000 | [diff] [blame] | 1950 | enum { |
"Vladimir N. Oleynik" | 9a51540 | 2006-02-15 13:27:18 +0000 | [diff] [blame] | 1951 | c_opt_config_file = 0, |
| 1952 | d_opt_decode_url, |
| 1953 | h_opt_home_httpd, |
| 1954 | USE_FEATURE_HTTPD_ENCODE_URL_STR(e_opt_encode_url,) |
Denis Vlasenko | 9f60929 | 2006-11-05 19:47:33 +0000 | [diff] [blame] | 1955 | USE_FEATURE_HTTPD_BASIC_AUTH( r_opt_realm ,) |
| 1956 | USE_FEATURE_HTTPD_AUTH_MD5( m_opt_md5 ,) |
| 1957 | USE_FEATURE_HTTPD_SETUID( u_opt_setuid ,) |
Denis Vlasenko | 0871bc8 | 2006-11-16 16:17:02 +0000 | [diff] [blame] | 1958 | p_opt_port , |
| 1959 | p_opt_inetd , |
| 1960 | p_opt_foreground, |
Denis Vlasenko | 9f60929 | 2006-11-05 19:47:33 +0000 | [diff] [blame] | 1961 | OPT_CONFIG_FILE = 1 << c_opt_config_file, |
| 1962 | OPT_DECODE_URL = 1 << d_opt_decode_url, |
| 1963 | OPT_HOME_HTTPD = 1 << h_opt_home_httpd, |
| 1964 | OPT_ENCODE_URL = USE_FEATURE_HTTPD_ENCODE_URL_STR((1 << e_opt_encode_url)) + 0, |
| 1965 | OPT_REALM = USE_FEATURE_HTTPD_BASIC_AUTH( (1 << r_opt_realm )) + 0, |
| 1966 | OPT_MD5 = USE_FEATURE_HTTPD_AUTH_MD5( (1 << m_opt_md5 )) + 0, |
| 1967 | OPT_SETUID = USE_FEATURE_HTTPD_SETUID( (1 << u_opt_setuid )) + 0, |
Denis Vlasenko | 0871bc8 | 2006-11-16 16:17:02 +0000 | [diff] [blame] | 1968 | OPT_PORT = 1 << p_opt_port, |
| 1969 | OPT_INETD = 1 << p_opt_inetd, |
| 1970 | OPT_FOREGROUND = 1 << p_opt_foreground, |
"Vladimir N. Oleynik" | 9a51540 | 2006-02-15 13:27:18 +0000 | [diff] [blame] | 1971 | }; |
Eric Andersen | a3bb3e6 | 2003-06-26 09:05:32 +0000 | [diff] [blame] | 1972 | |
Eric Andersen | a3bb3e6 | 2003-06-26 09:05:32 +0000 | [diff] [blame] | 1973 | |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 1974 | int httpd_main(int argc, char **argv); |
| 1975 | int httpd_main(int argc, char **argv) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1976 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 1977 | unsigned opt; |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1978 | char *url_for_decode; |
| 1979 | USE_FEATURE_HTTPD_ENCODE_URL_STR(const char *url_for_encode;) |
Denis Vlasenko | 0871bc8 | 2006-11-16 16:17:02 +0000 | [diff] [blame] | 1980 | const char *s_port; |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 1981 | USE_FEATURE_HTTPD_SETUID(const char *s_ugid = NULL;) |
| 1982 | USE_FEATURE_HTTPD_SETUID(struct bb_uidgid_t ugid;) |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 1983 | USE_FEATURE_HTTPD_AUTH_MD5(const char *pass;) |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 1984 | |
Denis Vlasenko | fcdb00f | 2006-11-21 00:09:37 +0000 | [diff] [blame] | 1985 | #if ENABLE_LOCALE_SUPPORT |
| 1986 | /* Undo busybox.c: we want to speak English in http (dates etc) */ |
| 1987 | setlocale(LC_TIME, "C"); |
| 1988 | #endif |
| 1989 | |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 1990 | INIT_G(); |
| 1991 | home_httpd = xrealloc_getcwd_or_warn(NULL); |
| 1992 | /* We do not "absolutize" path given by -h (home) opt. |
| 1993 | * If user gives relative path in -h, $SCRIPT_FILENAME can end up |
| 1994 | * relative too. */ |
Denis Vlasenko | 53091ec | 2007-03-26 13:35:09 +0000 | [diff] [blame] | 1995 | opt = getopt32(argc, argv, "c:d:h:" |
| 1996 | USE_FEATURE_HTTPD_ENCODE_URL_STR("e:") |
| 1997 | USE_FEATURE_HTTPD_BASIC_AUTH("r:") |
| 1998 | USE_FEATURE_HTTPD_AUTH_MD5("m:") |
| 1999 | USE_FEATURE_HTTPD_SETUID("u:") |
| 2000 | "p:if", |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 2001 | &(configFile), &url_for_decode, &home_httpd |
"Vladimir N. Oleynik" | 9a51540 | 2006-02-15 13:27:18 +0000 | [diff] [blame] | 2002 | USE_FEATURE_HTTPD_ENCODE_URL_STR(, &url_for_encode) |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 2003 | USE_FEATURE_HTTPD_BASIC_AUTH(, &g_realm) |
"Vladimir N. Oleynik" | 9a51540 | 2006-02-15 13:27:18 +0000 | [diff] [blame] | 2004 | USE_FEATURE_HTTPD_AUTH_MD5(, &pass) |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 2005 | USE_FEATURE_HTTPD_SETUID(, &s_ugid) |
Denis Vlasenko | 0871bc8 | 2006-11-16 16:17:02 +0000 | [diff] [blame] | 2006 | , &s_port |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 2007 | ); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 2008 | if (opt & OPT_DECODE_URL) { |
| 2009 | printf("%s", decodeString(url_for_decode, 1)); |
| 2010 | return 0; |
| 2011 | } |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 2012 | #if ENABLE_FEATURE_HTTPD_ENCODE_URL_STR |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 2013 | if (opt & OPT_ENCODE_URL) { |
| 2014 | printf("%s", encodeString(url_for_encode)); |
| 2015 | return 0; |
| 2016 | } |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 2017 | #endif |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 2018 | #if ENABLE_FEATURE_HTTPD_AUTH_MD5 |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 2019 | if (opt & OPT_MD5) { |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 2020 | puts(pw_encrypt(pass, "$1$")); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 2021 | return 0; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 2022 | } |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 2023 | #endif |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 2024 | if (opt & OPT_PORT) |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 2025 | tcp_port = xatou16(s_port); |
Denis Vlasenko | 0871bc8 | 2006-11-16 16:17:02 +0000 | [diff] [blame] | 2026 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 2027 | #if ENABLE_FEATURE_HTTPD_SETUID |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 2028 | if (opt & OPT_SETUID) { |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 2029 | if (!get_uidgid(&ugid, s_ugid, 1)) |
| 2030 | bb_error_msg_and_die("unrecognized user[:group] " |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 2031 | "name '%s'", s_ugid); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 2032 | } |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 2033 | #endif |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 2034 | |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 2035 | xchdir(home_httpd); |
Denis Vlasenko | 0871bc8 | 2006-11-16 16:17:02 +0000 | [diff] [blame] | 2036 | if (!(opt & OPT_INETD)) { |
Denis Vlasenko | 56258b6 | 2007-06-23 23:14:02 +0000 | [diff] [blame] | 2037 | #if BB_MMU |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 2038 | signal(SIGCHLD, SIG_IGN); |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 2039 | server_socket = openServer(); |
Denis Vlasenko | 0871bc8 | 2006-11-16 16:17:02 +0000 | [diff] [blame] | 2040 | #if ENABLE_FEATURE_HTTPD_SETUID |
| 2041 | /* drop privileges */ |
| 2042 | if (opt & OPT_SETUID) { |
| 2043 | if (ugid.gid != (gid_t)-1) { |
| 2044 | if (setgroups(1, &ugid.gid) == -1) |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 2045 | bb_perror_msg_and_die("setgroups"); |
Denis Vlasenko | 0871bc8 | 2006-11-16 16:17:02 +0000 | [diff] [blame] | 2046 | xsetgid(ugid.gid); |
| 2047 | } |
| 2048 | xsetuid(ugid.uid); |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 2049 | } |
Glenn L McGrath | 58c708a | 2003-01-05 04:01:56 +0000 | [diff] [blame] | 2050 | #endif |
Denis Vlasenko | 56258b6 | 2007-06-23 23:14:02 +0000 | [diff] [blame] | 2051 | #else /* BB_MMU */ |
| 2052 | bb_error_msg_and_die("-i is required"); |
| 2053 | #endif |
Denis Vlasenko | 0871bc8 | 2006-11-16 16:17:02 +0000 | [diff] [blame] | 2054 | } |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 2055 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 2056 | #if ENABLE_FEATURE_HTTPD_CGI |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 2057 | { |
| 2058 | char *p = getenv("PATH"); |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 2059 | /* env strings themself are not freed, no need to strdup(p): */ |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 2060 | clearenv(); |
| 2061 | if (p) |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 2062 | putenv(p - 5); |
Denis Vlasenko | 0871bc8 | 2006-11-16 16:17:02 +0000 | [diff] [blame] | 2063 | if (!(opt & OPT_INETD)) |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 2064 | setenv_long("SERVER_PORT", tcp_port); |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 2065 | } |
Glenn L McGrath | fe538ba | 2003-09-10 23:35:45 +0000 | [diff] [blame] | 2066 | #endif |
| 2067 | |
Denis Vlasenko | 55a9940 | 2006-09-30 20:41:44 +0000 | [diff] [blame] | 2068 | #if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 2069 | sighup_handler(0); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 2070 | #else |
Denis Vlasenko | 8b8c75e | 2006-09-26 10:07:41 +0000 | [diff] [blame] | 2071 | parse_conf(default_path_httpd_conf, FIRST_PARSE); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 2072 | #endif |
| 2073 | |
Denis Vlasenko | 0871bc8 | 2006-11-16 16:17:02 +0000 | [diff] [blame] | 2074 | if (opt & OPT_INETD) |
| 2075 | return miniHttpd_inetd(); |
| 2076 | |
Denis Vlasenko | 56258b6 | 2007-06-23 23:14:02 +0000 | [diff] [blame] | 2077 | #if BB_MMU |
Denis Vlasenko | 0871bc8 | 2006-11-16 16:17:02 +0000 | [diff] [blame] | 2078 | if (!(opt & OPT_FOREGROUND)) |
Denis Vlasenko | 5a14202 | 2007-03-26 13:20:54 +0000 | [diff] [blame] | 2079 | bb_daemonize(0); /* don't change current directory */ |
Denis Vlasenko | 77e44d6 | 2007-06-09 23:49:05 +0000 | [diff] [blame] | 2080 | return miniHttpd(server_socket); |
Denis Vlasenko | 56258b6 | 2007-06-23 23:14:02 +0000 | [diff] [blame] | 2081 | #else |
| 2082 | return 0; /* not reached */ |
| 2083 | #endif |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 2084 | } |