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