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