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