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