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