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