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