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