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