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