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