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