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