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