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