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